akonadi
qskypedialer.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "qskypedialer.h" 00023 00024 #include "../dbusconnectionpool.h" 00025 00026 #include <QtCore/QProcess> 00027 #include <QtDBus/QDBusConnection> 00028 #include <QtDBus/QDBusConnectionInterface> 00029 #include <QtDBus/QDBusInterface> 00030 #include <QtDBus/QDBusReply> 00031 00032 #include <klocale.h> 00033 00034 #include <unistd.h> 00035 00036 static bool isSkypeServiceRegistered() 00037 { 00038 const QLatin1String service( "com.Skype.API" ); 00039 00040 QDBusConnectionInterface *interface = QDBusConnection::systemBus().interface(); 00041 if ( interface->isServiceRegistered( service ) ) 00042 return true; 00043 00044 interface = Akonadi::DBusConnectionPool::threadConnection().interface(); 00045 if ( interface->isServiceRegistered( service ) ) 00046 return true; 00047 00048 return false; 00049 } 00050 00051 static QDBusInterface* searchSkypeDBusInterface() 00052 { 00053 const QLatin1String service( "com.Skype.API" ); 00054 const QLatin1String path( "/com/Skype" ); 00055 00056 QDBusInterface *interface = new QDBusInterface( service, path, QString(), QDBusConnection::systemBus() ); 00057 if ( !interface->isValid() ) { 00058 delete interface; 00059 interface = new QDBusInterface( service, path, QString(), Akonadi::DBusConnectionPool::threadConnection() ); 00060 } 00061 00062 return interface; 00063 } 00064 00065 QSkypeDialer::QSkypeDialer( const QString &applicationName ) 00066 : QDialer( applicationName ), mInterface( 0 ) 00067 { 00068 } 00069 00070 QSkypeDialer::~QSkypeDialer() 00071 { 00072 delete mInterface; 00073 } 00074 00075 bool QSkypeDialer::initializeSkype() 00076 { 00077 if ( mInterface && mInterface->isValid() ) 00078 return true; 00079 00080 // first check whether dbus interface is available yet 00081 if ( !isSkypeServiceRegistered() ) { 00082 00083 // it could be skype is not running yet, so start it now 00084 if ( !QProcess::startDetached( QLatin1String( "skype" ), QStringList() ) ) { 00085 mErrorMessage = i18n( "Unable to start skype process, check that skype executable is in your PATH variable." ); 00086 return false; 00087 } 00088 00089 const int runs = 100; 00090 for ( int i = 0; i < runs; ++i ) { 00091 if ( !isSkypeServiceRegistered() ) 00092 ::sleep( 2 ); 00093 else 00094 break; 00095 } 00096 } 00097 00098 // check again for the dbus interface 00099 mInterface = searchSkypeDBusInterface(); 00100 00101 if ( !mInterface->isValid() ) { 00102 delete mInterface; 00103 mInterface = 0; 00104 00105 mErrorMessage = i18n( "Skype Public API (D-Bus) seems to be disabled." ); 00106 return false; 00107 } 00108 00109 00110 QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "NAME %1" ).arg( mApplicationName ) ); 00111 if ( reply.value() != QLatin1String( "OK" ) ) { 00112 delete mInterface; 00113 mInterface = 0; 00114 00115 mErrorMessage = i18n( "Skype registration failed." ); 00116 return false; 00117 } 00118 00119 reply = mInterface->call( QLatin1String( "Invoke" ), QLatin1String( "PROTOCOL 1" ) ); 00120 if ( reply.value() != QLatin1String( "PROTOCOL 1" ) ) { 00121 delete mInterface; 00122 mInterface = 0; 00123 00124 mErrorMessage = i18n( "Protocol mismatch." ); 00125 return false; 00126 } 00127 00128 return true; 00129 } 00130 00131 bool QSkypeDialer::dialNumber( const QString &number ) 00132 { 00133 if ( !initializeSkype() ) 00134 return false; 00135 00136 QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CALL %1" ).arg( number ) ); 00137 00138 return true; 00139 } 00140 00141 bool QSkypeDialer::sendSms( const QString &number, const QString &text ) 00142 { 00143 if ( !initializeSkype() ) 00144 return false; 00145 00146 // First we create a new SMS object that gets an ID. We need that ID later... 00147 QDBusReply<QString> reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CREATE SMS OUTGOING %1" ).arg( number ) ); 00148 const QString messageId = reply.value().section( QLatin1String( " " ), 1, 1 ); 00149 00150 // Set the SMS text 00151 reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "SET SMS %1 BODY %2" ).arg( messageId, text ) ); 00152 00153 // Send the SMS 00154 reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "ALTER SMS %1 SEND" ).arg( messageId ) ); 00155 if ( reply.value().contains( QLatin1String( "ERROR" ) ) ) { 00156 mErrorMessage = reply.value(); 00157 // As sending the message failed (not enough Skype credit), lets delete the message 00158 reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "DELETE SMS %1" ).arg( messageId ) ); 00159 return false; 00160 } 00161 00162 return true; 00163 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.