fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / kdbusservicestarter.cpp
blobe37c08cde246f3a465670e8cfa2d842117dea888
1 /* This file is part of the KDE libraries
2 Copyright (C) 2003 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "kdbusservicestarter.h"
20 #include "kservicetypetrader.h"
21 #include <kapplication.h>
22 #include "kservice.h"
23 #include <kdebug.h>
24 #include <klocale.h>
25 #include <ktoolinvocation.h>
26 #include "QtDBus/QtDBus"
28 class KDBusServiceStarterPrivate
30 public:
31 KDBusServiceStarterPrivate() : q(0) {}
32 ~KDBusServiceStarterPrivate()
34 delete q;
36 KDBusServiceStarter *q;
39 K_GLOBAL_STATIC(KDBusServiceStarterPrivate, privateObject)
41 KDBusServiceStarter* KDBusServiceStarter::self()
43 if (!privateObject->q) {
44 new KDBusServiceStarter;
45 Q_ASSERT(privateObject->q);
47 return privateObject->q;
50 KDBusServiceStarter::KDBusServiceStarter()
52 // Set the singleton instance - useful when a derived KDBusServiceStarter
53 // was created (before self() was called)
54 Q_ASSERT(!privateObject->q);
55 privateObject->q = this;
58 KDBusServiceStarter::~KDBusServiceStarter()
62 int KDBusServiceStarter::findServiceFor( const QString& serviceType,
63 const QString& _constraint,
64 QString *error, QString* pDBusService,
65 int flags )
67 // Ask the trader which service is preferred for this servicetype
68 // We want one that provides a DBus interface
69 QString constraint = _constraint;
70 if ( !constraint.isEmpty() )
71 constraint += " and ";
72 constraint += "exist [X-DBUS-ServiceName]";
73 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
74 if ( offers.isEmpty() ) {
75 if ( error )
76 *error = i18n("No service implementing %1", serviceType );
77 kWarning() << "KDBusServiceStarter: No service implementing " << serviceType;
78 return -1;
80 KService::Ptr ptr = offers.first();
81 QString dbusService = ptr->property("X-DBUS-ServiceName").toString();
83 if ( !QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusService ) )
85 QString error;
86 if ( startServiceFor( serviceType, constraint, &error, &dbusService, flags ) != 0 )
88 kDebug() << "Couldn't start service:" << error;
89 return -2;
92 kDebug() << "DBus service is available now, as" << dbusService;
93 if ( pDBusService )
94 *pDBusService = dbusService;
95 return 0;
98 int KDBusServiceStarter::startServiceFor( const QString& serviceType,
99 const QString& constraint,
100 QString *error, QString* dbusService, int /*flags*/ )
102 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
103 if ( offers.isEmpty() )
104 return -1;
105 KService::Ptr ptr = offers.first();
106 kDebug() << "starting" << ptr->entryPath();
107 return KToolInvocation::startServiceByDesktopPath( ptr->entryPath(), QStringList(), error, dbusService );