File indexing completed on 2024-11-24 04:42:07

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 using namespace KCalendarCoreExtras;
0014 
0015 struct PluginLoader {
0016     PluginLoader();
0017     std::unique_ptr<KCalendarCore::CalendarPlugin> plugin;
0018 };
0019 
0020 PluginLoader::PluginLoader()
0021 {
0022     // static plugins
0023     const auto staticPluginData = QPluginLoader::staticPlugins();
0024     for (const auto &data : staticPluginData) {
0025         if (data.metaData().value(QLatin1StringView("IID")).toString() == QLatin1StringView("org.kde.kcalendarcore.CalendarPlugin")) {
0026             plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin*>(data.instance()));
0027         }
0028         if (plugin) {
0029             return;
0030         }
0031     }
0032 
0033     // dynamic plugins
0034     QStringList searchPaths(QCoreApplication::applicationDirPath());
0035     searchPaths += QCoreApplication::libraryPaths();
0036 
0037     for (const auto &searchPath : std::as_const(searchPaths)) {
0038         const QString pluginPath = searchPath + QLatin1StringView("/kf6/org.kde.kcalendarcore.calendars");
0039         for (QDirIterator it(pluginPath, QDir::Files); it.hasNext() && !plugin;) {
0040             it.next();
0041             QPluginLoader loader(it.fileInfo().absoluteFilePath());
0042             if (loader.load()) {
0043                 plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin*>(loader.instance()));
0044             } else {
0045                 qDebug() << loader.errorString();
0046             }
0047         }
0048     }
0049 }
0050 
0051 Q_GLOBAL_STATIC(PluginLoader, s_pluginLoader)
0052 
0053 bool CalendarPluginLoader::hasPlugin()
0054 {
0055     return (bool)s_pluginLoader->plugin;
0056 }
0057 
0058 KCalendarCore::CalendarPlugin* CalendarPluginLoader::plugin()
0059 {
0060     return s_pluginLoader->plugin.get();
0061 }
0062 
0063 #include "moc_calendarpluginloader.cpp"