KIO
kdbusservicestarter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kdbusservicestarter.h"
00020 #include "kservicetypetrader.h"
00021 #include <kapplication.h>
00022 #include "kservice.h"
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 #include <ktoolinvocation.h>
00026 #include "QtDBus/QtDBus"
00027
00028 class KDBusServiceStarterPrivate
00029 {
00030 public:
00031 KDBusServiceStarterPrivate() : q(0) {}
00032 ~KDBusServiceStarterPrivate()
00033 {
00034 delete q;
00035 }
00036 KDBusServiceStarter *q;
00037 };
00038
00039 K_GLOBAL_STATIC(KDBusServiceStarterPrivate, privateObject)
00040
00041 KDBusServiceStarter* KDBusServiceStarter::self()
00042 {
00043 if (!privateObject->q) {
00044 new KDBusServiceStarter;
00045 Q_ASSERT(privateObject->q);
00046 }
00047 return privateObject->q;
00048 }
00049
00050 KDBusServiceStarter::KDBusServiceStarter()
00051 {
00052
00053
00054 Q_ASSERT(!privateObject->q);
00055 privateObject->q = this;
00056 }
00057
00058 KDBusServiceStarter::~KDBusServiceStarter()
00059 {
00060 }
00061
00062 int KDBusServiceStarter::findServiceFor( const QString& serviceType,
00063 const QString& _constraint,
00064 QString *error, QString* pDBusService,
00065 int flags )
00066 {
00067
00068
00069 QString constraint = _constraint;
00070 if ( !constraint.isEmpty() )
00071 constraint += " and ";
00072 constraint += "exist [X-DBUS-ServiceName]";
00073 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
00074 if ( offers.isEmpty() ) {
00075 if ( error )
00076 *error = i18n("No service implementing %1", serviceType );
00077 kWarning() << "KDBusServiceStarter: No service implementing " << serviceType;
00078 return -1;
00079 }
00080 KService::Ptr ptr = offers.first();
00081 QString dbusService = ptr->property("X-DBUS-ServiceName").toString();
00082
00083 if ( !QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusService ) )
00084 {
00085 QString error;
00086 if ( startServiceFor( serviceType, constraint, &error, &dbusService, flags ) != 0 )
00087 {
00088 kDebug() << "Couldn't start service:" << error;
00089 return -2;
00090 }
00091 }
00092 kDebug() << "DBus service is available now, as" << dbusService;
00093 if ( pDBusService )
00094 *pDBusService = dbusService;
00095 return 0;
00096 }
00097
00098 int KDBusServiceStarter::startServiceFor( const QString& serviceType,
00099 const QString& constraint,
00100 QString *error, QString* dbusService, int )
00101 {
00102 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
00103 if ( offers.isEmpty() )
00104 return -1;
00105 KService::Ptr ptr = offers.first();
00106 kDebug() << "starting" << ptr->entryPath();
00107 return KToolInvocation::startServiceByDesktopPath( ptr->entryPath(), QStringList(), error, dbusService );
00108 }