• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
kdbusservicestarter.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2003 David Faure <faure@kde.org>
3
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.
7
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.
12
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.
17*/
18
19#include "kdbusservicestarter.h"
20#include "kservicetypetrader.h"
21#include "kservice.h"
22#include <kdebug.h>
23#include <klocale.h>
24#include <ktoolinvocation.h>
25#include "QtDBus/QtDBus"
26
27class KDBusServiceStarterPrivate
28{
29 public:
30 KDBusServiceStarterPrivate() : q(0) {}
31 ~KDBusServiceStarterPrivate()
32 {
33 delete q;
34 }
35 KDBusServiceStarter *q;
36};
37
38K_GLOBAL_STATIC(KDBusServiceStarterPrivate, privateObject)
39
40KDBusServiceStarter* KDBusServiceStarter::self()
41{
42 if (!privateObject->q) {
43 new KDBusServiceStarter;
44 Q_ASSERT(privateObject->q);
45 }
46 return privateObject->q;
47}
48
49KDBusServiceStarter::KDBusServiceStarter()
50{
51 // Set the singleton instance - useful when a derived KDBusServiceStarter
52 // was created (before self() was called)
53 Q_ASSERT(!privateObject->q);
54 privateObject->q = this;
55}
56
57KDBusServiceStarter::~KDBusServiceStarter()
58{
59}
60
61int KDBusServiceStarter::findServiceFor( const QString& serviceType,
62 const QString& _constraint,
63 QString *error, QString* pDBusService,
64 int flags )
65{
66 // Ask the trader which service is preferred for this servicetype
67 // We want one that provides a DBus interface
68 QString constraint = _constraint;
69 if ( !constraint.isEmpty() )
70 constraint += " and ";
71 constraint += "exist [X-DBUS-ServiceName]";
72 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
73 if ( offers.isEmpty() ) {
74 if ( error )
75 *error = i18n("No service implementing %1", serviceType );
76 kWarning() << "KDBusServiceStarter: No service implementing " << serviceType;
77 return -1;
78 }
79 KService::Ptr ptr = offers.first();
80 QString dbusService = ptr->property("X-DBUS-ServiceName").toString();
81
82 if ( !QDBusConnection::sessionBus().interface()->isServiceRegistered( dbusService ) )
83 {
84 QString err;
85 if ( startServiceFor( serviceType, constraint, &err, &dbusService, flags ) != 0 )
86 {
87 if ( error )
88 *error = err;
89 kWarning() << "Couldn't start service" << dbusService << "for" << serviceType << ":" << err;
90 return -2;
91 }
92 }
93 kDebug() << "DBus service is available now, as" << dbusService;
94 if ( pDBusService )
95 *pDBusService = dbusService;
96 return 0;
97}
98
99int KDBusServiceStarter::startServiceFor( const QString& serviceType,
100 const QString& constraint,
101 QString *error, QString* dbusService, int /*flags*/ )
102{
103 const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
104 if ( offers.isEmpty() )
105 return -1;
106 KService::Ptr ptr = offers.first();
107 kDebug() << "starting" << ptr->entryPath();
108 return KToolInvocation::startServiceByDesktopPath( ptr->entryPath(), QStringList(), error, dbusService );
109}
KDBusServiceStarter
A generic DBUS service starter, using KServiceTypeTrader.
Definition: kdbusservicestarter.h:37
KDBusServiceStarter::startServiceFor
virtual int startServiceFor(const QString &serviceType, const QString &constraint=QString(), QString *error=0, QString *dbusService=0, int flags=0)
Find an implementation of the given serviceType, and start it, to use its DBus interface.
Definition: kdbusservicestarter.cpp:99
KDBusServiceStarter::~KDBusServiceStarter
virtual ~KDBusServiceStarter()
Definition: kdbusservicestarter.cpp:57
KDBusServiceStarter::findServiceFor
int findServiceFor(const QString &serviceType, const QString &constraint=QString(), QString *error=0, QString *dbusService=0, int flags=0)
Check if a given DBus service is available - from the serviceType it's supposed to implement.
Definition: kdbusservicestarter.cpp:61
KDBusServiceStarter::KDBusServiceStarter
KDBusServiceStarter()
Definition: kdbusservicestarter.cpp:49
KServiceTypeTrader::self
static KServiceTypeTrader * self()
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
KSharedPtr< KService >
KToolInvocation::startServiceByDesktopPath
static int startServiceByDesktopPath(const QString &_name, const QString &URL, QString *error=0, QString *serviceName=0, int *pid=0, const QByteArray &startup_id=QByteArray(), bool noWait=false)
QList< Ptr >
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kDebug
#define kDebug
kWarning
#define kWarning
kdbusservicestarter.h
kdebug.h
klocale.h
i18n
QString i18n(const char *text)
kservice.h
kservicetypetrader.h
ktoolinvocation.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal