| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2015 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include <QApplication> |
| 9 | #include <QDBusConnectionInterface> |
| 10 | #include <QDBusMessage> |
| 11 | #include <QHash> |
| 12 | |
| 13 | #include <KAboutData> |
| 14 | #include <KCrash> |
| 15 | #include <KDBusService> |
| 16 | #include <KDEDModule> |
| 17 | #include <KPluginFactory> |
| 18 | #include <KPluginMetaData> |
| 19 | |
| 20 | #include <kio_version.h> |
| 21 | |
| 22 | #include <QLoggingCategory> |
| 23 | Q_DECLARE_LOGGING_CATEGORY(KIOD_CATEGORY) |
| 24 | Q_LOGGING_CATEGORY(KIOD_CATEGORY, "kf.kio.kiod" ) |
| 25 | |
| 26 | class KIOD : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | public: |
| 30 | ~KIOD() override; |
| 31 | |
| 32 | public Q_SLOTS: |
| 33 | void loadModule(const QString &name); |
| 34 | |
| 35 | private: |
| 36 | QHash<QString, KDEDModule *> m_modules; |
| 37 | }; |
| 38 | |
| 39 | void KIOD::loadModule(const QString &name) |
| 40 | { |
| 41 | // Make sure this method is only called with valid module names. |
| 42 | Q_ASSERT(name.indexOf(QLatin1Char('/')) == -1); |
| 43 | |
| 44 | KDEDModule *module = m_modules.value(key: name, defaultValue: nullptr); |
| 45 | if (module) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | qCDebug(KIOD_CATEGORY) << "loadModule" << name; |
| 50 | auto result = KPluginFactory::instantiatePlugin<KDEDModule>(data: KPluginMetaData(QStringLiteral("kf6/kiod/" ) + name)); |
| 51 | if (result) { |
| 52 | module = result.plugin; |
| 53 | module->setModuleName(name); // makes it register to DBus |
| 54 | m_modules.insert(key: name, value: module); |
| 55 | } else { |
| 56 | qCWarning(KIOD_CATEGORY) << "Error loading plugin:" << result.errorText; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | KIOD::~KIOD() |
| 61 | { |
| 62 | qDeleteAll(c: m_modules); |
| 63 | } |
| 64 | |
| 65 | Q_GLOBAL_STATIC(KIOD, self) |
| 66 | |
| 67 | // on-demand module loading |
| 68 | // this function is called by the D-Bus message processing function before |
| 69 | // calls are delivered to objects |
| 70 | static void messageFilter(const QDBusMessage &message) |
| 71 | { |
| 72 | const QString name = KDEDModule::moduleForMessage(message); |
| 73 | if (name.isEmpty()) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | self()->loadModule(name); |
| 78 | } |
| 79 | |
| 80 | extern Q_DBUS_EXPORT void qDBusAddSpyHook(void (*)(const QDBusMessage &)); |
| 81 | |
| 82 | int main(int argc, char *argv[]) |
| 83 | { |
| 84 | #ifdef Q_OS_MACOS |
| 85 | // do the "early" step to make this an "agent" application: |
| 86 | // set the LSUIElement InfoDict key programmatically. |
| 87 | extern void makeAgentApplication(); |
| 88 | makeAgentApplication(); |
| 89 | #endif |
| 90 | qunsetenv(varName: "SESSION_MANAGER" ); // disable session management |
| 91 | |
| 92 | QApplication app(argc, argv); // GUI needed for kpasswdserver's dialogs |
| 93 | app.setQuitOnLastWindowClosed(false); |
| 94 | |
| 95 | KAboutData about(QStringLiteral("kiod6" ), QString(), QStringLiteral(KIO_VERSION_STRING)); |
| 96 | KAboutData::setApplicationData(about); |
| 97 | |
| 98 | KCrash::initialize(); |
| 99 | |
| 100 | KDBusService service(KDBusService::Unique); |
| 101 | |
| 102 | QDBusConnectionInterface *bus = QDBusConnection::sessionBus().interface(); |
| 103 | // Also register as all the names we should respond to (org.kde.kssld, org.kde.kcookiejar, etc.) |
| 104 | // so that the calling code is independent from the physical "location" of the service. |
| 105 | const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("kf6/kiod" )); |
| 106 | for (const KPluginMetaData &metaData : plugins) { |
| 107 | const QString serviceName = metaData.value(QStringLiteral("X-KDE-DBus-ServiceName" )); |
| 108 | if (serviceName.isEmpty()) { |
| 109 | qCWarning(KIOD_CATEGORY) << "No X-KDE-DBus-ServiceName found in" << metaData.fileName(); |
| 110 | continue; |
| 111 | } |
| 112 | if (!bus->registerService(serviceName)) { |
| 113 | qCWarning(KIOD_CATEGORY) << "Couldn't register name" << serviceName << "with DBUS - another process owns it already!" ; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | self(); // create it in this thread |
| 118 | qDBusAddSpyHook(messageFilter); |
| 119 | |
| 120 | #ifdef Q_OS_MACOS |
| 121 | // In the case of kiod6 we need to confirm the agent nature, |
| 122 | // possibly because of how things have been set up after creating |
| 123 | // the QApplication instance. Failure to do this will disable |
| 124 | // text input into dialogs we may post. |
| 125 | extern void setAgentActivationPolicy(); |
| 126 | setAgentActivationPolicy(); |
| 127 | #endif |
| 128 | return app.exec(); |
| 129 | } |
| 130 | |
| 131 | #include "kiod_main.moc" |
| 132 | |