File indexing completed on 2024-04-21 03:52:44

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "calendarpluginloader.h"
0007 
0008 #include <QCoreApplication>
0009 #include <QDirIterator>
0010 #include <QPluginLoader>
0011 
0012 using namespace KCalendarCore;
0013 
0014 struct PluginLoader {
0015     PluginLoader();
0016     std::unique_ptr<KCalendarCore::CalendarPlugin> plugin;
0017 };
0018 
0019 PluginLoader::PluginLoader()
0020 {
0021     // static plugins
0022     const auto staticPluginData = QPluginLoader::staticPlugins();
0023     for (const auto &data : staticPluginData) {
0024         if (data.metaData().value(QLatin1String("IID")).toString() == QLatin1String("org.kde.kcalendarcore.CalendarPlugin")) {
0025             plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin *>(data.instance()));
0026         }
0027         if (plugin) {
0028             return;
0029         }
0030     }
0031 
0032     // dynamic plugins
0033     QStringList searchPaths(QCoreApplication::applicationDirPath());
0034     searchPaths += QCoreApplication::libraryPaths();
0035 
0036     for (const auto &searchPath : std::as_const(searchPaths)) {
0037         const QString pluginPath = searchPath + QLatin1String("/kf6/org.kde.kcalendarcore.calendars");
0038         for (QDirIterator it(pluginPath, QDir::Files); it.hasNext() && !plugin;) {
0039             it.next();
0040             QPluginLoader loader(it.fileInfo().absoluteFilePath());
0041             if (loader.load()) {
0042                 plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin *>(loader.instance()));
0043             } else {
0044                 qDebug() << loader.errorString();
0045             }
0046         }
0047     }
0048 }
0049 
0050 Q_GLOBAL_STATIC(PluginLoader, s_pluginLoader)
0051 
0052 bool CalendarPluginLoader::hasPlugin()
0053 {
0054     return (bool)s_pluginLoader->plugin;
0055 }
0056 
0057 KCalendarCore::CalendarPlugin *CalendarPluginLoader::plugin()
0058 {
0059     return s_pluginLoader->plugin.get();
0060 }
0061 
0062 #include "moc_calendarpluginloader.cpp"