Warning, file /plasma/khotkeys/libkhotkeysprivate/daemon/daemon.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0002 
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "daemon.h"
0007 
0008 #include <memory>
0009 
0010 #include "khotkeysglobal.h"
0011 
0012 #include <QDBusConnection>
0013 #include <QDBusError>
0014 #include <QDBusInterface>
0015 #include <QDBusMessage>
0016 #include <QDBusReply>
0017 
0018 #include "KDE/KDebug"
0019 #include "KDE/KLocale"
0020 #include <KConfig>
0021 #include <KConfigGroup>
0022 
0023 namespace KHotKeys
0024 {
0025 namespace Daemon
0026 {
0027 bool isEnabled()
0028 {
0029     KConfig khotkeysrc(KHOTKEYS_CONFIG_FILE);
0030     KConfigGroup main(&khotkeysrc, "Main");
0031     return !main.readEntry("Disabled", false);
0032 }
0033 
0034 static QDBusInterface *Kded()
0035 {
0036     QDBusInterface *iface = new QDBusInterface("org.kde.kded5", "/kded", "org.kde.kded5");
0037     if (!iface->isValid()) {
0038         QDBusError err = iface->lastError();
0039         if (err.isValid()) {
0040             qCritical() << "Failed to contact kded [" << err.name() << "]:" << err.message();
0041         }
0042     }
0043     return iface;
0044 }
0045 
0046 bool isRunning()
0047 {
0048     std::unique_ptr<QDBusInterface> kded(Kded());
0049     if (!kded->isValid()) {
0050         return false;
0051     }
0052 
0053     // I started with checking if i could get a valid /KHotKeys Interface. But
0054     // it resisted to work. So lets do the other thing.
0055     QDBusReply<QStringList> modules = kded->call("loadedModules");
0056     return modules.value().contains("khotkeys");
0057 }
0058 
0059 bool reload()
0060 {
0061     // No kded no reload
0062     std::unique_ptr<QDBusInterface> kded(Kded());
0063     if (!kded->isValid()) {
0064         return false;
0065     }
0066 
0067     // Inform kdedkhotkeys demon to reload settings
0068     QDBusConnection bus = QDBusConnection::sessionBus();
0069     QDBusInterface iface("org.kde.kded5", "/modules/khotkeys", "org.kde.khotkeys", bus);
0070     if (!iface.isValid()) {
0071         QDBusError err = iface.lastError();
0072         if (err.isValid()) {
0073             qCritical() << err.name() << ":" << err.message();
0074         }
0075         return start();
0076     }
0077 
0078     QDBusMessage reply = iface.call("reread_configuration");
0079     QDBusError err = iface.lastError();
0080     if (err.isValid()) {
0081         qCritical() << err.name() << ":" << err.message();
0082         return false;
0083     }
0084 
0085     return true;
0086 }
0087 
0088 bool start()
0089 {
0090     std::unique_ptr<QDBusInterface> kded(Kded());
0091     if (!kded->isValid()) {
0092         return false;
0093     }
0094     QDBusReply<bool> reply = kded->call("loadModule", "khotkeys");
0095     QDBusError err = reply.error();
0096 
0097     if (err.isValid()) {
0098         qCritical() << "Unable to start server org.kde.khotkeys (kded module) [" << err.name() << "]:" << err.message();
0099         return false;
0100     }
0101 
0102     Q_ASSERT(reply.isValid());
0103 
0104     if (reply.value()) {
0105         return true;
0106     } else {
0107         qCritical() << "Unable to start server org.kde.khotkeys (kded module)";
0108         return false;
0109     }
0110 }
0111 
0112 bool stop()
0113 {
0114     if (!isRunning()) {
0115         return true;
0116     }
0117 
0118     std::unique_ptr<QDBusInterface> kded(Kded());
0119     if (!kded->isValid()) {
0120         return false;
0121     }
0122 
0123     QDBusReply<bool> reply = kded->call("unloadModule", "khotkeys");
0124     QDBusError err = reply.error();
0125 
0126     if (err.isValid()) {
0127         qCritical() << "Error when stopping khotkeys kded module [" << err.name() << "]:" << err.message();
0128         return false;
0129     }
0130 
0131     Q_ASSERT(reply.isValid());
0132 
0133     if (reply.value()) {
0134         return true;
0135     } else {
0136         qCritical() << "Failed to stop server org.kde.khotkeys (kded module)";
0137         return false;
0138     }
0139 }
0140 
0141 }
0142 } // namespace KHotKeys::Daemon