KCalendarCore

calendarpluginloader.cpp
1 /*
2  SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3  SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5 
6 #include "calendarpluginloader.h"
7 
8 #include <QCoreApplication>
9 #include <QDirIterator>
10 #include <QPluginLoader>
11 
12 using namespace KCalendarCore;
13 
14 struct PluginLoader {
15  PluginLoader();
16  std::unique_ptr<KCalendarCore::CalendarPlugin> plugin;
17 };
18 
19 PluginLoader::PluginLoader()
20 {
21  // static plugins
22  const auto staticPluginData = QPluginLoader::staticPlugins();
23  for (const auto &data : staticPluginData) {
24  if (data.metaData().value(QLatin1String("IID")).toString() == QLatin1String("org.kde.kcalendarcore.CalendarPlugin")) {
25  plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin *>(data.instance()));
26  }
27  if (plugin) {
28  return;
29  }
30  }
31 
32  // dynamic plugins
34  searchPaths += QCoreApplication::libraryPaths();
35 
36  for (const auto &searchPath : std::as_const(searchPaths)) {
37  const QString pluginPath = searchPath + QLatin1String("/kf6/org.kde.kcalendarcore.calendars");
38  for (QDirIterator it(pluginPath, QDir::Files); it.hasNext() && !plugin;) {
39  it.next();
40  QPluginLoader loader(it.fileInfo().absoluteFilePath());
41  if (loader.load()) {
42  plugin.reset(qobject_cast<KCalendarCore::CalendarPlugin *>(loader.instance()));
43  } else {
44  qDebug() << loader.errorString();
45  }
46  }
47  }
48 }
49 
50 Q_GLOBAL_STATIC(PluginLoader, s_pluginLoader)
51 
52 bool CalendarPluginLoader::hasPlugin()
53 {
54  return (bool)s_pluginLoader->plugin;
55 }
56 
57 KCalendarCore::CalendarPlugin *CalendarPluginLoader::plugin()
58 {
59  return s_pluginLoader->plugin.get();
60 }
61 
62 #include "moc_calendarpluginloader.cpp"
QVector< QStaticPlugin > staticPlugins()
A plugin that provides calendar data.
Namespace for all KCalendarCore types.
Definition: alarm.h:36
bool hasNext() const const
QString applicationDirPath()
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
QStringList libraryPaths()
char * toString(const EngineQuery &query)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:58:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.