Messagelib

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

KDE's Doxygen guidelines are available online.