• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.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 
32 #include <klocale.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  interface = Akonadi::DBusConnectionPool::threadConnection().interface();
45  if ( interface->isServiceRegistered( service ) )
46  return true;
47 
48  return false;
49 }
50 
51 static QDBusInterface* searchSkypeDBusInterface()
52 {
53  const QLatin1String service( "com.Skype.API" );
54  const QLatin1String path( "/com/Skype" );
55 
56  QDBusInterface *interface = new QDBusInterface( service, path, QString(), QDBusConnection::systemBus() );
57  if ( !interface->isValid() ) {
58  delete interface;
59  interface = new QDBusInterface( service, path, QString(), Akonadi::DBusConnectionPool::threadConnection() );
60  }
61 
62  return interface;
63 }
64 
65 QSkypeDialer::QSkypeDialer( const QString &applicationName )
66  : QDialer( applicationName ), mInterface( 0 )
67 {
68 }
69 
70 QSkypeDialer::~QSkypeDialer()
71 {
72  delete mInterface;
73 }
74 
75 bool QSkypeDialer::initializeSkype()
76 {
77  if ( mInterface && mInterface->isValid() )
78  return true;
79 
80  // first check whether dbus interface is available yet
81  if ( !isSkypeServiceRegistered() ) {
82 
83  // it could be skype is not running yet, so start it now
84  if ( !QProcess::startDetached( QLatin1String( "skype" ), QStringList() ) ) {
85  mErrorMessage = i18n( "Unable to start skype process, check that skype executable is in your PATH variable." );
86  return false;
87  }
88 
89  const int runs = 100;
90  for ( int i = 0; i < runs; ++i ) {
91  if ( !isSkypeServiceRegistered() )
92  ::sleep( 2 );
93  else
94  break;
95  }
96  }
97 
98  // check again for the dbus interface
99  mInterface = searchSkypeDBusInterface();
100 
101  if ( !mInterface->isValid() ) {
102  delete mInterface;
103  mInterface = 0;
104 
105  mErrorMessage = i18n( "Skype Public API (D-Bus) seems to be disabled." );
106  return false;
107  }
108 
109 
110  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "NAME %1" ).arg( mApplicationName ) );
111  if ( reply.value() != QLatin1String( "OK" ) ) {
112  delete mInterface;
113  mInterface = 0;
114 
115  mErrorMessage = i18n( "Skype registration failed." );
116  return false;
117  }
118 
119  reply = mInterface->call( QLatin1String( "Invoke" ), QLatin1String( "PROTOCOL 1" ) );
120  if ( reply.value() != QLatin1String( "PROTOCOL 1" ) ) {
121  delete mInterface;
122  mInterface = 0;
123 
124  mErrorMessage = i18n( "Protocol mismatch." );
125  return false;
126  }
127 
128  return true;
129 }
130 
131 bool QSkypeDialer::dialNumber( const QString &number )
132 {
133  if ( !initializeSkype() )
134  return false;
135 
136  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CALL %1" ).arg( number ) );
137 
138  return true;
139 }
140 
141 bool QSkypeDialer::sendSms( const QString &number, const QString &text )
142 {
143  if ( !initializeSkype() )
144  return false;
145 
146  // First we create a new SMS object that gets an ID. We need that ID later...
147  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CREATE SMS OUTGOING %1" ).arg( number ) );
148  const QString messageId = reply.value().section( QLatin1String( " " ), 1, 1 );
149 
150  // Set the SMS text
151  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "SET SMS %1 BODY %2" ).arg( messageId, text ) );
152 
153  // Send the SMS
154  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "ALTER SMS %1 SEND" ).arg( messageId ) );
155  if ( reply.value().contains( QLatin1String( "ERROR" ) ) ) {
156  mErrorMessage = reply.value();
157  // As sending the message failed (not enough Skype credit), lets delete the message
158  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "DELETE SMS %1" ).arg( messageId ) );
159  return false;
160  }
161 
162  return true;
163 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:55:24 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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