Messagelib

plugineditorinitmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2017-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "plugineditorinitmanager.h"
8 #include "messagecomposer_debug.h"
9 #include "plugineditorinit.h"
10 #include <KPluginFactory>
11 #include <KPluginMetaData>
12 #include <QFileInfo>
13 
14 using namespace MessageComposer;
15 
16 class PluginEditorInitInfo
17 {
18 public:
19  PluginEditorInitInfo() = default;
20 
21  PimCommon::PluginUtilData pluginData;
22  QString metaDataFileNameBaseName;
23  QString metaDataFileName;
24  KPluginMetaData data;
25  PluginEditorInit *plugin = nullptr;
26  bool isEnabled = true;
27 };
28 
29 namespace
30 {
31 QString pluginEditorInitVersion()
32 {
33  return QStringLiteral("1.0");
34 }
35 }
36 
37 class MessageComposer::PluginEditorInitManagerPrivate
38 {
39 public:
40  PluginEditorInitManagerPrivate(PluginEditorInitManager *qq)
41  : q(qq)
42  {
43  initializePlugins();
44  }
45 
46  void loadPlugin(PluginEditorInitInfo *item);
47  Q_REQUIRED_RESULT QVector<PluginEditorInit *> pluginsList() const;
48  void initializePlugins();
50  Q_REQUIRED_RESULT QString configPrefixSettingKey() const;
51  Q_REQUIRED_RESULT QString configGroupName() const;
52  Q_REQUIRED_RESULT QVector<PimCommon::PluginUtilData> pluginsDataList() const;
53  PluginEditorInit *pluginFromIdentifier(const QString &id);
54 
55 private:
56  QVector<PimCommon::PluginUtilData> mPluginDataList;
57  PluginEditorInitManager *const q;
58 };
59 
60 QString PluginEditorInitManagerPrivate::configGroupName() const
61 {
62  return QStringLiteral("KMailPluginEditorInit");
63 }
64 
65 QString PluginEditorInitManagerPrivate::configPrefixSettingKey() const
66 {
67  return QStringLiteral("PluginEditorInit");
68 }
69 
70 QVector<PimCommon::PluginUtilData> PluginEditorInitManagerPrivate::pluginsDataList() const
71 {
72  return mPluginDataList;
73 }
74 
75 void PluginEditorInitManagerPrivate::initializePlugins()
76 {
77  const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("kmail/plugineditorinit"));
78 
79  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80 
81 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
83 #else
85 #endif
86  i.toBack();
87  while (i.hasPrevious()) {
88  PluginEditorInitInfo info;
89  const KPluginMetaData data = i.previous();
90 
91  // 1) get plugin data => name/description etc.
92  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
93  // 2) look at if plugin is activated
94  const bool isPluginActivated =
95  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
96  info.isEnabled = isPluginActivated;
97  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
98  info.metaDataFileName = data.fileName();
99  info.data = data;
100  if (pluginEditorInitVersion() == data.version()) {
101  info.plugin = nullptr;
102  mPluginList.push_back(info);
103  } else {
104  qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
105  }
106  }
107  QVector<PluginEditorInitInfo>::iterator end(mPluginList.end());
108  for (QVector<PluginEditorInitInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
109  loadPlugin(&(*it));
110  }
111 }
112 
113 void PluginEditorInitManagerPrivate::loadPlugin(PluginEditorInitInfo *item)
114 {
115  if (auto plugin = KPluginFactory::instantiatePlugin<PluginEditorInit>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
116  item->plugin = plugin;
117  item->plugin->setIsEnabled(item->isEnabled);
118  item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
119  mPluginDataList.append(item->pluginData);
120  }
121 }
122 
123 QVector<PluginEditorInit *> PluginEditorInitManagerPrivate::pluginsList() const
124 {
126  QVector<PluginEditorInitInfo>::ConstIterator end(mPluginList.constEnd());
127  for (QVector<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
128  if (auto plugin = (*it).plugin) {
129  lst << plugin;
130  }
131  }
132  return lst;
133 }
134 
135 PluginEditorInit *PluginEditorInitManagerPrivate::pluginFromIdentifier(const QString &id)
136 {
137  QVector<PluginEditorInitInfo>::ConstIterator end(mPluginList.constEnd());
138  for (QVector<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
139  if ((*it).pluginData.mIdentifier == id) {
140  return (*it).plugin;
141  }
142  }
143  return {};
144 }
145 
146 PluginEditorInitManager::PluginEditorInitManager(QObject *parent)
147  : QObject(parent)
148  , d(new MessageComposer::PluginEditorInitManagerPrivate(this))
149 {
150 }
151 
152 PluginEditorInitManager::~PluginEditorInitManager() = default;
153 
154 PluginEditorInitManager *PluginEditorInitManager::self()
155 {
156  static PluginEditorInitManager s_self;
157  return &s_self;
158 }
159 
160 QVector<PluginEditorInit *> PluginEditorInitManager::pluginsList() const
161 {
162  return d->pluginsList();
163 }
164 
165 QString PluginEditorInitManager::configGroupName() const
166 {
167  return d->configGroupName();
168 }
169 
170 QString PluginEditorInitManager::configPrefixSettingKey() const
171 {
172  return d->configPrefixSettingKey();
173 }
174 
175 QVector<PimCommon::PluginUtilData> PluginEditorInitManager::pluginsDataList() const
176 {
177  return d->pluginsDataList();
178 }
179 
180 PluginEditorInit *PluginEditorInitManager::pluginFromIdentifier(const QString &id)
181 {
182  return d->pluginFromIdentifier(id);
183 }
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QVector::iterator begin()
QString fileName() const
QString version() const
The PluginEditorInitManager class.
QString name() const
QString baseName() const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
The PluginEditorInit class.
QVector::const_iterator constBegin() const const
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat May 21 2022 03:53:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.