Messagelib

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

KDE's Doxygen guidelines are available online.