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 <QtCore/QProcess>
00025 #include <QtDBus/QDBusConnection>
00026 #include <QtDBus/QDBusConnectionInterface>
00027 #include <QtDBus/QDBusInterface>
00028 #include <QtDBus/QDBusReply>
00029
00030 #include <klocale.h>
00031
00032 #include <unistd.h>
00033
00034 static bool isSkypeServiceRegistered()
00035 {
00036 const QLatin1String service( "com.Skype.API" );
00037
00038 QDBusConnectionInterface *interface = QDBusConnection::systemBus().interface();
00039 if ( interface->isServiceRegistered( service ) )
00040 return true;
00041
00042 interface = QDBusConnection::sessionBus().interface();
00043 if ( interface->isServiceRegistered( service ) )
00044 return true;
00045
00046 return false;
00047 }
00048
00049 static QDBusInterface* searchSkypeDBusInterface()
00050 {
00051 const QLatin1String service( "com.Skype.API" );
00052 const QLatin1String path( "/com/Skype" );
00053
00054 QDBusInterface *interface = new QDBusInterface( service, path, QString(), QDBusConnection::systemBus() );
00055 if ( !interface->isValid() ) {
00056 delete interface;
00057 interface = new QDBusInterface( service, path, QString(), QDBusConnection::sessionBus() );
00058 }
00059
00060 return interface;
00061 }
00062
00063 QSkypeDialer::QSkypeDialer( const QString &applicationName )
00064 : mApplicationName( applicationName )
00065 {
00066 }
00067
00068 bool QSkypeDialer::dialNumber( const QString &number )
00069 {
00070
00071 if ( !isSkypeServiceRegistered() ) {
00072
00073
00074 if ( !QProcess::startDetached( QLatin1String( "skype" ), QStringList() ) ) {
00075 mErrorMessage = i18n( "Unable to start skype process, check that skype executable is in your PATH variable." );
00076 return false;
00077 }
00078
00079 const int runs = 100;
00080 for ( int i = 0; i < runs; ++i ) {
00081 if ( !isSkypeServiceRegistered() )
00082 ::sleep( 2 );
00083 else
00084 break;
00085 }
00086 }
00087
00088
00089 QDBusInterface *interface = searchSkypeDBusInterface();
00090
00091 if ( !interface->isValid() ) {
00092 delete interface;
00093
00094 mErrorMessage = i18n( "Skype Public API (D-Bus) seems to be disabled." );
00095 return false;
00096 }
00097
00098 QDBusReply<QString> reply = interface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "NAME %1" ).arg( mApplicationName ) );
00099 if ( reply.value() != QLatin1String( "OK" ) ) {
00100 delete interface;
00101
00102 mErrorMessage = i18n( "Skype registration failed." );
00103 return false;
00104 }
00105
00106 reply = interface->call( QLatin1String( "Invoke" ), QLatin1String( "PROTOCOL 1" ) );
00107 if ( reply.value() != QLatin1String( "PROTOCOL 1" ) ) {
00108 delete interface;
00109
00110 mErrorMessage = i18n( "Protocol mismatch." );
00111 return false;
00112 }
00113
00114 reply = interface->call( QLatin1String( "Invoke" ), QString::fromLatin1( "CALL %1" ).arg( number ) );
00115
00116 delete interface;
00117
00118 return true;
00119 }
00120
00121 QString QSkypeDialer::errorMessage() const
00122 {
00123 return mErrorMessage;
00124 }