Messagelib

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

KDE's Doxygen guidelines are available online.