KIO
kdbusservicestarter.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2003 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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 // Set the singleton instance - useful when a derived KDBusServiceStarter 00053 // was created (before self() was called) 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 // Ask the trader which service is preferred for this servicetype 00068 // We want one that provides a DBus interface 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 /*flags*/ ) 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 }
KDE 4.6 API Reference