Messagelib

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

KDE's Doxygen guidelines are available online.