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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "NAME %1" ).arg( mApplicationName ) );
114  if ( reply.value() != QLatin1String( "OK" ) ) {
115  delete mInterface;
116  mInterface = 0;
117 
118  mErrorMessage = i18n( "Skype registration failed." );
119  return false;
120  }
121 
122  reply = mInterface->call( QLatin1String( "Invoke" ), QLatin1String( "PROTOCOL 1" ) );
123  if ( reply.value() != QLatin1String( "PROTOCOL 1" ) ) {
124  delete mInterface;
125  mInterface = 0;
126 
127  mErrorMessage = i18n( "Protocol mismatch." );
128  return false;
129  }
130 
131  return true;
132 }
133 
134 bool QSkypeDialer::dialNumber( const QString &number )
135 {
136  if ( !initializeSkype() ) {
137  return false;
138  }
139 
140  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CALL %1" ).arg( number ) );
141 
142  return true;
143 }
144 
145 bool QSkypeDialer::sendSms( const QString &number, const QString &text )
146 {
147  if ( !initializeSkype() ) {
148  return false;
149  }
150 
151  // First we create a new SMS object that gets an ID. We need that ID later...
152  QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CREATE SMS OUTGOING %1" ).arg( number ) );
153  const QString messageId = reply.value().section( QLatin1String( " " ), 1, 1 );
154 
155  // Set the SMS text
156  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "SET SMS %1 BODY %2" ).arg( messageId, text ) );
157 
158  // Send the SMS
159  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "ALTER SMS %1 SEND" ).arg( messageId ) );
160  if ( reply.value().contains( QLatin1String( "ERROR" ) ) ) {
161  mErrorMessage = reply.value();
162  // As sending the message failed (not enough Skype credit), lets delete the message
163  reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "DELETE SMS %1" ).arg( messageId ) );
164  return false;
165  }
166 
167  return true;
168 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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