1 /*
2   This file is part of KOrganizer.
3 
4   SPDX-FileCopyrightText: 2001, 2003 Cornelius Schumacher <schumacher@kde.org>
5   SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
8 */
9 
10 #include "kocore.h"
11 #include "prefs/koprefs.h"
12 
13 #include <CalendarSupport/IdentityManager>
14 
15 #include "korganizer_debug.h"
16 #include <KServiceTypeTrader>
17 #include <KXMLGUIFactory>
18 
19 KOCore *KOCore::mSelf = nullptr;
20 
self()21 KOCore *KOCore::self()
22 {
23     if (!mSelf) {
24         mSelf = new KOCore;
25     }
26 
27     return mSelf;
28 }
29 
KOCore()30 KOCore::KOCore()
31 {
32 }
33 
~KOCore()34 KOCore::~KOCore()
35 {
36     mSelf = nullptr;
37 }
38 
availablePlugins(const QString & type,int version)39 KService::List KOCore::availablePlugins(const QString &type, int version)
40 {
41     QString constraint;
42     if (version >= 0) {
43         constraint = QStringLiteral("[X-KDE-PluginInterfaceVersion] == %1").arg(QString::number(version));
44     }
45 
46     return KServiceTypeTrader::self()->query(type, constraint);
47 }
48 
availableCalendarDecorations()49 KService::List KOCore::availableCalendarDecorations()
50 {
51     return availablePlugins(EventViews::CalendarDecoration::Decoration::serviceType(), EventViews::CalendarDecoration::Decoration::interfaceVersion());
52 }
53 
loadCalendarDecoration(const KService::Ptr & service)54 EventViews::CalendarDecoration::Decoration *KOCore::loadCalendarDecoration(const KService::Ptr &service)
55 {
56     KPluginLoader loader(*service);
57     auto factory = loader.factory();
58 
59     if (!factory) {
60         qCDebug(KORGANIZER_LOG) << "Factory creation failed";
61         return nullptr;
62     }
63 
64     return factory->create<EventViews::CalendarDecoration::Decoration>();
65 }
66 
addXMLGUIClient(QWidget * wdg,KXMLGUIClient * guiclient)67 void KOCore::addXMLGUIClient(QWidget *wdg, KXMLGUIClient *guiclient)
68 {
69     mXMLGUIClients.insert(wdg, guiclient);
70 }
71 
removeXMLGUIClient(QWidget * wdg)72 void KOCore::removeXMLGUIClient(QWidget *wdg)
73 {
74     mXMLGUIClients.remove(wdg);
75 }
76 
xmlguiClient(QWidget * wdg) const77 KXMLGUIClient *KOCore::xmlguiClient(QWidget *wdg) const
78 {
79     if (!wdg) {
80         return nullptr;
81     }
82 
83     QWidget *topLevel = wdg->topLevelWidget();
84     QMap<QWidget *, KXMLGUIClient *>::ConstIterator it = mXMLGUIClients.find(topLevel);
85     if (it != mXMLGUIClients.constEnd()) {
86         return it.value();
87     }
88 
89     return nullptr;
90 }
91 
loadCalendarDecorations()92 EventViews::CalendarDecoration::Decoration::List KOCore::loadCalendarDecorations()
93 {
94     if (!mCalendarDecorationsLoaded) {
95         const QStringList selectedPlugins = KOPrefs::instance()->mSelectedPlugins;
96 
97         mCalendarDecorations.clear();
98         const KService::List plugins = availableCalendarDecorations();
99         KService::List::ConstIterator it;
100         const KService::List::ConstIterator end(plugins.constEnd());
101         for (it = plugins.constBegin(); it != end; ++it) {
102             if ((*it)->hasServiceType(EventViews::CalendarDecoration::Decoration::serviceType())) {
103                 QString name = (*it)->desktopEntryName();
104                 if (selectedPlugins.contains(name)) {
105                     EventViews::CalendarDecoration::Decoration *d = loadCalendarDecoration(*it);
106                     mCalendarDecorations.append(d);
107                 }
108             }
109         }
110         mCalendarDecorationsLoaded = true;
111     }
112 
113     return mCalendarDecorations;
114 }
115 
unloadPlugins()116 void KOCore::unloadPlugins()
117 {
118     qDeleteAll(mCalendarDecorations);
119     mCalendarDecorations.clear();
120     mCalendarDecorationsLoaded = false;
121 }
122 
reloadPlugins()123 void KOCore::reloadPlugins()
124 {
125     // TODO: does this still apply?
126     // Plugins should be unloaded, but e.g. komonthview keeps using the old ones
127     unloadPlugins();
128     loadCalendarDecorations();
129 }
130 
identityManager()131 KIdentityManagement::IdentityManager *KOCore::identityManager()
132 {
133     return CalendarSupport::IdentityManager::self();
134 }
135