File indexing completed on 2024-05-12 15:44:52

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kdbusservicestarter.h"
0009 #include "kservice.h"
0010 #include "kservicetypetrader.h"
0011 #include <KLocalizedString>
0012 #include <QDBusConnection>
0013 #include <QDBusConnectionInterface>
0014 #include <QDebug>
0015 #include <ktoolinvocation.h>
0016 
0017 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 61)
0018 
0019 class KDBusServiceStarterPrivate
0020 {
0021 public:
0022     KDBusServiceStarterPrivate()
0023         : q(nullptr)
0024     {
0025     }
0026     ~KDBusServiceStarterPrivate()
0027     {
0028         delete q;
0029     }
0030     KDBusServiceStarter *q;
0031 };
0032 
0033 Q_GLOBAL_STATIC(KDBusServiceStarterPrivate, privateObject)
0034 
0035 KDBusServiceStarter *KDBusServiceStarter::self()
0036 {
0037     if (!privateObject()->q) {
0038         new KDBusServiceStarter;
0039         Q_ASSERT(privateObject()->q);
0040     }
0041     return privateObject()->q;
0042 }
0043 
0044 KDBusServiceStarter::KDBusServiceStarter()
0045 {
0046     // Set the singleton instance - useful when a derived KDBusServiceStarter
0047     // was created (before self() was called)
0048     Q_ASSERT(!privateObject()->q);
0049     privateObject()->q = this;
0050 }
0051 
0052 KDBusServiceStarter::~KDBusServiceStarter()
0053 {
0054 }
0055 
0056 int KDBusServiceStarter::findServiceFor(const QString &serviceType, const QString &_constraint, QString *error, QString *pDBusService, int flags)
0057 {
0058     // Ask the trader which service is preferred for this servicetype
0059     // We want one that provides a DBus interface
0060     QString constraint = _constraint;
0061     if (!constraint.isEmpty()) {
0062         constraint += QLatin1String(" and ");
0063     }
0064     constraint += QLatin1String("exist [X-DBUS-ServiceName]");
0065     const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
0066     if (offers.isEmpty()) {
0067         if (error) {
0068             *error = i18n("No service implementing %1", serviceType);
0069         }
0070         qWarning() << "KDBusServiceStarter: No service implementing " << serviceType;
0071         return -1;
0072     }
0073     KService::Ptr ptr = offers.first();
0074     QString dbusService = ptr->property(QStringLiteral("X-DBUS-ServiceName")).toString();
0075 
0076     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(dbusService)) {
0077         QString err;
0078         if (startServiceFor(serviceType, constraint, &err, &dbusService, flags) != 0) {
0079             if (error) {
0080                 *error = err;
0081             }
0082             qWarning() << "Couldn't start service" << dbusService << "for" << serviceType << ":" << err;
0083             return -2;
0084         }
0085     }
0086     // qDebug() << "DBus service is available now, as" << dbusService;
0087     if (pDBusService) {
0088         *pDBusService = dbusService;
0089     }
0090     return 0;
0091 }
0092 
0093 int KDBusServiceStarter::startServiceFor(const QString &serviceType, const QString &constraint, QString *error, QString *dbusService, int /*flags*/)
0094 {
0095     const KService::List offers = KServiceTypeTrader::self()->query(serviceType, constraint);
0096     if (offers.isEmpty()) {
0097         return -1;
0098     }
0099     KService::Ptr ptr = offers.first();
0100     // qDebug() << "starting" << ptr->entryPath();
0101     return KToolInvocation::startServiceByDesktopPath(ptr->entryPath(), QStringList(), error, dbusService);
0102 }
0103 
0104 #endif