Messagelib

plugineditormanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 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 
67 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
69 #else
71 #endif
72  i.toBack();
73  while (i.hasPrevious()) {
74  PluginEditorInfo info;
75  const KPluginMetaData data = i.previous();
76 
77  // 1) get plugin data => name/description etc.
78  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
79  // 2) look at if plugin is activated
80  const bool isPluginActivated =
81  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
82  info.isEnabled = isPluginActivated;
83  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
84  info.metaDataFileName = data.fileName();
85  info.data = data;
86  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-KMailEditor-Order")).toVariant();
87  int order = -1;
88  if (p.isValid()) {
89  order = p.toInt();
90  }
91  info.order = order;
92  if (pluginEditorVersion() == data.version()) {
93  info.plugin = nullptr;
94  mPluginList.push_back(info);
95  } else {
96  qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
97  }
98  }
99  QVector<PluginEditorInfo>::iterator end(mPluginList.end());
100  for (QVector<PluginEditorInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
101  loadPlugin(&(*it));
102  }
103 }
104 
105 void PluginEditorManagerPrivate::loadPlugin(PluginEditorInfo *item)
106 {
107  if (auto plugin = KPluginFactory::instantiatePlugin<PluginEditor>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
108  item->plugin = plugin;
109  item->plugin->setIsEnabled(item->isEnabled);
110  item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
111  item->plugin->setOrder(item->order);
112  mPluginDataList.append(item->pluginData);
113  }
114 }
115 
116 QVector<PluginEditor *> PluginEditorManagerPrivate::pluginsList() const
117 {
119  QVector<PluginEditorInfo>::ConstIterator end(mPluginList.constEnd());
120  for (QVector<PluginEditorInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
121  if (auto plugin = (*it).plugin) {
122  lst << plugin;
123  }
124  }
125  return lst;
126 }
127 
128 QVector<PimCommon::PluginUtilData> PluginEditorManagerPrivate::pluginDataList() const
129 {
130  return mPluginDataList;
131 }
132 
133 QString PluginEditorManagerPrivate::configGroupName() const
134 {
135  return QStringLiteral("KMailPluginEditor");
136 }
137 
138 QString PluginEditorManagerPrivate::configPrefixSettingKey() const
139 {
140  return QStringLiteral("KMailEditorPlugin");
141 }
142 
143 PluginEditor *PluginEditorManagerPrivate::pluginFromIdentifier(const QString &id)
144 {
145  QVector<PluginEditorInfo>::ConstIterator end(mPluginList.constEnd());
146  for (QVector<PluginEditorInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
147  if ((*it).pluginData.mIdentifier == id) {
148  return (*it).plugin;
149  }
150  }
151  return {};
152 }
153 
154 PluginEditorManager::PluginEditorManager(QObject *parent)
155  : QObject(parent)
156  , d(new MessageComposer::PluginEditorManagerPrivate(this))
157 {
158 }
159 
160 PluginEditorManager::~PluginEditorManager() = default;
161 
162 PluginEditorManager *PluginEditorManager::self()
163 {
164  static PluginEditorManager s_self;
165  return &s_self;
166 }
167 
168 QVector<PluginEditor *> PluginEditorManager::pluginsList() const
169 {
170  return d->pluginsList();
171 }
172 
173 QVector<PimCommon::PluginUtilData> PluginEditorManager::pluginsDataList() const
174 {
175  return d->pluginDataList();
176 }
177 
178 QString PluginEditorManager::configGroupName() const
179 {
180  return d->configGroupName();
181 }
182 
183 QString PluginEditorManager::configPrefixSettingKey() const
184 {
185  return d->configPrefixSettingKey();
186 }
187 
188 PluginEditor *PluginEditorManager::pluginFromIdentifier(const QString &id)
189 {
190  return d->pluginFromIdentifier(id);
191 }
The PluginEditor class.
Definition: plugineditor.h:19
QJsonObject rawData() const
bool isValid() const const
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QVector::iterator begin()
QString fileName() const
QString version() const
QString name() const
QString baseName() const const
int toInt(bool *ok) const const
QJsonValue value(const QString &key) const const
QVariant toVariant() const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
QVector::const_iterator constBegin() const const
The PluginEditorManager class.
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 19 2022 03:53:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.