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 "kservice.h" 00022 #include <kdebug.h> 00023 #include <klocale.h> 00024 #include <ktoolinvocation.h> 00025 #include "QtDBus/QtDBus" 00026 00027 class KDBusServiceStarterPrivate 00028 { 00029 public: 00030 KDBusServiceStarterPrivate() : q(0) {} 00031 ~KDBusServiceStarterPrivate() 00032 { 00033 delete q; 00034 } 00035 KDBusServiceStarter *q; 00036 }; 00037 00038 K_GLOBAL_STATIC(KDBusServiceStarterPrivate, privateObject) 00039 00040 KDBusServiceStarter* KDBusServiceStarter::self() 00041 { 00042 if (!privateObject->q) { 00043 new KDBusServiceStarter; 00044 Q_ASSERT(privateObject->q); 00045 } 00046 return privateObject->q; 00047 } 00048 00049 KDBusServiceStarter::KDBusServiceStarter() 00050 { 00051 // Set the singleton instance - useful when a derived KDBusServiceStarter 00052 // was created (before self() was called) 00053 Q_ASSERT(!privateObject->q); 00054 privateObject->q = this; 00055 } 00056 00057 KDBusServiceStarter::~KDBusServiceStarter() 00058 { 00059 } 00060 00061 int KDBusServiceStarter::findServiceFor( const QString& serviceType, 00062 const QString& _constraint, 00063 QString *error, QString* pDBusService, 00064 int flags ) 00065 { 00066 // Ask the trader which service is preferred for this servicetype 00067 // We want one that provides a DBus interface 00068 QString constraint = _constraint; 00069 if ( !constraint.isEmpty() ) 00070 constraint += " and "; 00071 constraint += "exist [X-DBUS-ServiceName]"; 00072 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint); 00073 if ( offers.isEmpty() ) { 00074 if ( error ) 00075 *error = i18n("No service implementing %1", serviceType ); 00076 kWarning() << "KDBusServiceStarter: No service implementing " << serviceType; 00077 return -1; 00078 } 00079 KService::Ptr ptr = offers.first(); 00080 QString dbusService = ptr->property("X-DBUS-ServiceName").toString(); 00081 00082 if ( !QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusService ) ) 00083 { 00084 QString error; 00085 if ( startServiceFor( serviceType, constraint, &error, &dbusService, flags ) != 0 ) 00086 { 00087 kDebug() << "Couldn't start service:" << error; 00088 return -2; 00089 } 00090 } 00091 kDebug() << "DBus service is available now, as" << dbusService; 00092 if ( pDBusService ) 00093 *pDBusService = dbusService; 00094 return 0; 00095 } 00096 00097 int KDBusServiceStarter::startServiceFor( const QString& serviceType, 00098 const QString& constraint, 00099 QString *error, QString* dbusService, int /*flags*/ ) 00100 { 00101 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint); 00102 if ( offers.isEmpty() ) 00103 return -1; 00104 KService::Ptr ptr = offers.first(); 00105 kDebug() << "starting" << ptr->entryPath(); 00106 return KToolInvocation::startServiceByDesktopPath( ptr->entryPath(), QStringList(), error, dbusService ); 00107 }
KDE 4.7 API Reference