KIO

kiod_main.cpp
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>
23Q_DECLARE_LOGGING_CATEGORY(KIOD_CATEGORY)
24Q_LOGGING_CATEGORY(KIOD_CATEGORY, "kf.kio.kiod")
25
26class KIOD : public QObject
27{
28 Q_OBJECT
29public:
30 ~KIOD() override;
31
32public Q_SLOTS:
33 void loadModule(const QString &name);
34
35private:
37};
38
39void 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(name, nullptr);
45 if (module) {
46 return;
47 }
48
49 qCDebug(KIOD_CATEGORY) << "loadModule" << name;
50 auto result = KPluginFactory::instantiatePlugin<KDEDModule>(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(name, module);
55 } else {
56 qCWarning(KIOD_CATEGORY) << "Error loading plugin:" << result.errorText;
57 }
58}
59
60KIOD::~KIOD()
61{
62 qDeleteAll(m_modules);
63}
64
65Q_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
70static void messageFilter(const QDBusMessage &message)
71{
73 if (name.isEmpty()) {
74 return;
75 }
76
77 self()->loadModule(name);
78}
79
80extern Q_DBUS_EXPORT void qDBusAddSpyHook(void (*)(const QDBusMessage &));
81
82int 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("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));
97
99
101
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"
static void setApplicationData(const KAboutData &aboutData)
static QString moduleForMessage(const QDBusMessage &message)
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
KCMUTILS_EXPORT KCModule * loadModule(const KPluginMetaData &metaData, QWidget *parent=nullptr, const QVariantList &args={}, const std::shared_ptr< QQmlEngine > &engine={})
KCRASH_EXPORT void initialize()
QString name(StandardAction id)
QDBusConnectionInterface * interface() const const
QDBusConnection sessionBus()
QDBusReply< QDBusConnectionInterface::RegisterServiceReply > registerService(const QString &serviceName, ServiceQueueOptions qoption, ServiceReplacementOptions roption)
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.