Messagelib

plugineditorgrammarmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2021 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 
83  i.toBack();
84  while (i.hasPrevious()) {
85  PluginEditorGrammarInfo info;
86  const KPluginMetaData data = i.previous();
87 
88  // 1) get plugin data => name/description etc.
89  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
90  // 2) look at if plugin is activated
91  const bool isPluginActivated =
92  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  info.data = data;
97  if (pluginEditorGrammarVersion() == data.version()) {
98  info.plugin = nullptr;
99  mPluginList.push_back(info);
100  } else {
101  qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
102  }
103  }
105  for (QVector<PluginEditorGrammarInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
106  loadPlugin(&(*it));
107  }
108 }
109 
110 void PluginEditorGrammarManagerPrivate::loadPlugin(PluginEditorGrammarInfo *item)
111 {
112  if (auto plugin = KPluginFactory::instantiatePlugin<PimCommon::CustomToolsPlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
113  item->plugin = plugin;
114  item->plugin->setIsEnabled(item->isEnabled);
115  item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
116  mPluginDataList.append(item->pluginData);
117  }
118 }
119 
120 QVector<PimCommon::CustomToolsPlugin *> PluginEditorGrammarManagerPrivate::pluginsList() const
121 {
124  for (QVector<PluginEditorGrammarInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
125  if (auto plugin = (*it).plugin) {
126  lst << plugin;
127  }
128  }
129  return lst;
130 }
131 
132 PimCommon::CustomToolsPlugin *PluginEditorGrammarManagerPrivate::pluginFromIdentifier(const QString &id)
133 {
135  for (QVector<PluginEditorGrammarInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
136  if ((*it).pluginData.mIdentifier == id) {
137  return (*it).plugin;
138  }
139  }
140  return {};
141 }
142 
143 PluginEditorGrammarManager::PluginEditorGrammarManager(QObject *parent)
144  : QObject(parent)
145  , d(new MessageComposer::PluginEditorGrammarManagerPrivate(this))
146 {
147  qRegisterMetaType<MessageComposer::PluginGrammarAction>();
148 }
149 
150 PluginEditorGrammarManager::~PluginEditorGrammarManager() = default;
151 
152 PluginEditorGrammarManager *PluginEditorGrammarManager::self()
153 {
154  static PluginEditorGrammarManager s_self;
155  return &s_self;
156 }
157 
158 QVector<PimCommon::CustomToolsPlugin *> PluginEditorGrammarManager::pluginsList() const
159 {
160  return d->pluginsList();
161 }
162 
163 QString PluginEditorGrammarManager::configGroupName() const
164 {
165  return d->configGroupName();
166 }
167 
168 QString PluginEditorGrammarManager::configPrefixSettingKey() const
169 {
170  return d->configPrefixSettingKey();
171 }
172 
173 QVector<PimCommon::PluginUtilData> PluginEditorGrammarManager::pluginsDataList() const
174 {
175  return d->pluginsDataList();
176 }
177 
178 PimCommon::CustomToolsPlugin *PluginEditorGrammarManager::pluginFromIdentifier(const QString &id)
179 {
180  return d->pluginFromIdentifier(id);
181 }
QString fileName() const
QString name() const
QVector::iterator begin()
The PluginEditorGrammarManager 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 Sat Dec 4 2021 23:12:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.