File indexing completed on 2024-11-10 04:40:36

0001 /*
0002     SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "agentconfigurationmanager_p.h"
0008 #include "akonadicore_debug.h"
0009 #include "servermanager.h"
0010 
0011 #include <QCoreApplication>
0012 #include <QDBusConnectionInterface>
0013 #include <QDir>
0014 #include <QPluginLoader>
0015 
0016 #include <QDBusConnection>
0017 
0018 namespace Akonadi
0019 {
0020 class AgentConfigurationManagerPrivate
0021 {
0022 public:
0023     QString serviceName(const QString &instance) const
0024     {
0025         QString service = QStringLiteral("org.freedesktop.Akonadi.AgentConfig.%1").arg(instance);
0026         if (ServerManager::self()->hasInstanceIdentifier()) {
0027             service += QLatin1Char('.') + ServerManager::self()->instanceIdentifier();
0028         }
0029         return service;
0030     }
0031 };
0032 } // namespace Akonadi
0033 
0034 using namespace Akonadi;
0035 
0036 AgentConfigurationManager *AgentConfigurationManager::sInstance = nullptr;
0037 
0038 AgentConfigurationManager::AgentConfigurationManager(QObject *parent)
0039     : QObject(parent)
0040 {
0041 }
0042 
0043 AgentConfigurationManager *AgentConfigurationManager::self()
0044 {
0045     if (sInstance == nullptr) {
0046         sInstance = new AgentConfigurationManager();
0047     }
0048     return sInstance;
0049 }
0050 
0051 AgentConfigurationManager::~AgentConfigurationManager()
0052 {
0053 }
0054 
0055 bool AgentConfigurationManager::registerInstanceConfiguration(const QString &instance)
0056 {
0057     const auto serviceName = d->serviceName(instance);
0058     QDBusConnection conn = QDBusConnection::sessionBus();
0059     if (conn.interface()->isServiceRegistered(serviceName)) {
0060         qCDebug(AKONADICORE_LOG) << "Service " << serviceName << " is already registered";
0061         return false;
0062     }
0063 
0064     return conn.registerService(serviceName);
0065 }
0066 
0067 void AgentConfigurationManager::unregisterInstanceConfiguration(const QString &instance)
0068 {
0069     const auto serviceName = d->serviceName(instance);
0070     QDBusConnection::sessionBus().unregisterService(serviceName);
0071 }
0072 
0073 bool AgentConfigurationManager::isInstanceRegistered(const QString &instance) const
0074 {
0075     const auto serviceName = d->serviceName(instance);
0076     return QDBusConnection::sessionBus().interface()->isServiceRegistered(serviceName);
0077 }
0078 
0079 QString AgentConfigurationManager::findConfigPlugin(const QString &type) const
0080 {
0081     const auto libPaths = QCoreApplication::libraryPaths();
0082     const QString prefixPluginPath = QStringLiteral("pim6/akonadi/config");
0083     for (const auto &libPath : libPaths) {
0084         const QString pluginPath = QStringLiteral("%1/%2/").arg(libPath, prefixPluginPath);
0085         const auto libs = QDir(pluginPath).entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
0086         for (const auto &lib : libs) {
0087             QPluginLoader loader(lib.absoluteFilePath());
0088             const auto metaData = loader.metaData().toVariantMap();
0089             if (metaData.value(QStringLiteral("IID")).toString() != QLatin1StringView("org.freedesktop.Akonadi.AgentConfig")) {
0090                 continue;
0091             }
0092             const auto md = metaData.value(QStringLiteral("MetaData")).toMap();
0093             if (md.value(QStringLiteral("X-Akonadi-PluginType")).toString() != QLatin1StringView("AgentConfig")) {
0094                 continue;
0095             }
0096             if (!type.startsWith(md.value(QStringLiteral("X-Akonadi-AgentConfig-Type")).toString())) {
0097                 continue;
0098             }
0099             return loader.fileName();
0100         }
0101     }
0102 
0103     return {};
0104 }
0105 
0106 #include "moc_agentconfigurationmanager_p.cpp"