• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

  • akonadi
  • contact
  • actions
qskypedialer.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "qskypedialer.h"
23 
24 #include "../dbusconnectionpool.h"
25 
26 #include <QtCore/QProcess>
27 #include <QtDBus/QDBusConnection>
28 #include <QtDBus/QDBusConnectionInterface>
29 #include <QtDBus/QDBusInterface>
30 #include <QtDBus/QDBusReply>
31 #include <QDebug>
32 #include <klocalizedstring.h>
33 
34 #include <unistd.h>
35 
36 static bool isSkypeServiceRegistered()
37 {
38  const QLatin1String service( "com.Skype.API" );
39 
40  QDBusConnectionInterface *interface = QDBusConnection::systemBus().interface();
41  if ( interface->isServiceRegistered( service ) ) {
42  return true;
43  }
44 
45  interface = Akonadi::DBusConnectionPool::threadConnection().interface();
46  if ( interface->isServiceRegistered( service ) ) {
47  return true;
48  }
49 
50  return false;
51 }
52 
53 static QDBusInterface* searchSkypeDBusInterface()
54 {
55  const QLatin1String service( "com.Skype.API" );
56  const QLatin1String path( "/com/Skype" );
57 
58  QDBusInterface *interface = new QDBusInterface( service, path, QString(), QDBusConnection::systemBus() );
59  if ( !interface->isValid() ) {
60  delete interface;
61  interface = new QDBusInterface( service, path, QString(), Akonadi::DBusConnectionPool::threadConnection() );
62  }
63 
64  return interface;
65 }
66 
67 QSkypeDialer::QSkypeDialer( const QString &applicationName )
68  : QDialer( applicationName ), mInterface( 0 )
69 {
70 }
71 
72 QSkypeDialer::~QSkypeDialer()
73 {
74  delete mInterface;
75 }
76 
77 bool QSkypeDialer::initializeSkype()
78 {
79  if ( mInterface && mInterface->isValid() ) {
80  return true;
81  }
82 
83  // first check whether dbus interface is available yet
84  if ( !isSkypeServiceRegistered() ) {
85 
86  // it could be skype is not running yet, so start it now
87  if ( !QProcess::startDetached( QLatin1String( "skype" ), QStringList() ) ) {
88  mErrorMessage = i18n( "Unable to start skype process, check that skype executable is in your PATH variable." );
89  return false;
90  }
91 
92  const int runs = 100;
93  for ( int i = 0; i < runs; ++i ) {
94  if ( !isSkypeServiceRegistered() ) {
95  ::sleep( 2 );
96  } else {
97  break;
98  }
99  }
100  }
101 
102  // check again for the dbus interface
103  mInterface = searchSkypeDBusInterface();
104 
105  if ( !mInterface->isValid() ) {
106  delete mInterface;
107  mInterface = 0;
108 
109  mErrorMessage = i18n( "Skype Public API (D-Bus) seems to be disabled." );
110  return false;
111  }
112 
113 
114  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "NAME %1" ).arg( mApplicationName ) );
115  if ( reply.value() != QLatin1String( "OK" ) ) {
116  delete mInterface;
117  mInterface = 0;
118 
119  mErrorMessage = i18n( "Skype registration failed." );
120  return false;
121  }
122 
123  reply = mInterface->call( QLatin1String( "Invoke" ), QLatin1String( "PROTOCOL 1" ) );
124  if ( reply.value() != QLatin1String( "PROTOCOL 1" ) ) {
125  delete mInterface;
126  mInterface = 0;
127 
128  mErrorMessage = i18n( "Protocol mismatch." );
129  return false;
130  }
131 
132  return true;
133 }
134 
135 bool QSkypeDialer::dialNumber( const QString &number )
136 {
137  if ( !initializeSkype() ) {
138  return false;
139  }
140 
141  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CALL %1" ).arg( number ) );
142 
143  return true;
144 }
145 
146 bool QSkypeDialer::sendSms( const QString &number, const QString &text )
147 {
148  if ( !initializeSkype() ) {
149  return false;
150  }
151 
152  // First we create a new SMS object that gets an ID. We need that ID later...
153  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CREATE SMS OUTGOING %1" ).arg( number ) );
154  const QString messageId = reply.value().section( QLatin1String( " " ), 1, 1 );
155 
156  // Set the SMS text
157  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "SET SMS %1 BODY %2" ).arg( messageId, text ) );
158 
159  // Send the SMS
160  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "ALTER SMS %1 SEND" ).arg( messageId ) );
161  if ( reply.value().contains( QLatin1String( "ERROR" ) ) ) {
162  mErrorMessage = reply.value();
163  // As sending the message failed (not enough Skype credit), lets delete the message
164  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "DELETE SMS %1" ).arg( messageId ) );
165  return false;
166  }
167 
168  return true;
169 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:48 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal