00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00081 if ( !isSkypeServiceRegistered() ) {
00082
00083
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
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
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
00151 reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "SET SMS %1 BODY %2" ).arg( messageId, text ) );
00152
00153
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
00158 reply = mInterface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "DELETE SMS %1" ).arg( messageId ) );
00159 return false;
00160 }
00161
00162 return true;
00163 }