summaryrefslogtreecommitdiff
path: root/src/plugincontainer.h
blob: 1a3173fbc8f3fc2525066c94582d8640bddad532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifndef PLUGINCONTAINER_H
#define PLUGINCONTAINER_H

#include "previewgenerator.h"

class OrganizerCore;
class IUserInterface;

#include <iplugindiagnose.h>
#include <ipluginmodpage.h>
#include <iplugingame.h>
#include <iplugintool.h>
#include <ipluginproxy.h>
#include <iplugininstaller.h>
#include <ipluginfilemapper.h>
#include <QtPlugin>
#include <QPluginLoader>
#include <QFile>
#ifndef Q_MOC_RUN
#include <boost/fusion/container.hpp>
#include <boost/fusion/include/at_key.hpp>
#include <boost/mp11.hpp>
#endif // Q_MOC_RUN
#include <vector>
#include <memory>


class OrganizerProxy;


// Small class that allows calling check() for plugin requirements
// without passing the IOrganizer.
class PluginRequirementProxy {
public:

  std::vector<unsigned int> problems() const;
  QString description(unsigned int id) const;

private:

  const MOBase::PluginRequirement* m_Requirement;
  OrganizerProxy* m_Proxy;

  PluginRequirementProxy(const MOBase::PluginRequirement* requirement, OrganizerProxy* proxy);

  friend class PluginContainer;

};


class PluginContainer : public QObject, public MOBase::IPluginDiagnose
{

  Q_OBJECT
  Q_INTERFACES(MOBase::IPluginDiagnose)

private:

  using PluginMap = boost::fusion::map<
    boost::fusion::pair<QObject, std::vector<QObject*>>,
    boost::fusion::pair<MOBase::IPlugin, std::vector<MOBase::IPlugin*>>,
    boost::fusion::pair<MOBase::IPluginDiagnose, std::vector<MOBase::IPluginDiagnose*>>,
    boost::fusion::pair<MOBase::IPluginGame, std::vector<MOBase::IPluginGame*>>,
    boost::fusion::pair<MOBase::IPluginInstaller, std::vector<MOBase::IPluginInstaller*>>,
    boost::fusion::pair<MOBase::IPluginModPage, std::vector<MOBase::IPluginModPage*>>,
    boost::fusion::pair<MOBase::IPluginPreview, std::vector<MOBase::IPluginPreview*>>,
    boost::fusion::pair<MOBase::IPluginTool, std::vector<MOBase::IPluginTool*>>,
    boost::fusion::pair<MOBase::IPluginProxy, std::vector<MOBase::IPluginProxy*>>,
    boost::fusion::pair<MOBase::IPluginFileMapper, std::vector<MOBase::IPluginFileMapper*>>
  >;

  using AccessPluginMap = boost::fusion::map<
    boost::fusion::pair<MOBase::IPluginDiagnose, std::map<MOBase::IPluginDiagnose*, MOBase::IPlugin*>>,
    boost::fusion::pair<MOBase::IPluginFileMapper, std::map<MOBase::IPluginFileMapper*, MOBase::IPlugin*>>,
    boost::fusion::pair<QString, std::map<QString, MOBase::IPlugin*>>
  >;

  static const unsigned int PROBLEM_PLUGINSNOTLOADED = 1;

  /**
  * This typedefs defines the order of plugin interface. This is increasing order of
  * importance".
  *
  * @note IPlugin is the less important interface, followed by IPluginDiagnose and
  *     IPluginFileMapper as those are usually implemented together with another interface.
  *     Other interfaces are in a alphabetical order since it is unlikely a plugin will
  *     implement multiple ones.
  */
  using PluginTypeOrder =
    boost::mp11::mp_transform<
      std::add_pointer_t,
      boost::mp11::mp_list<
        MOBase::IPluginGame, MOBase::IPluginInstaller, MOBase::IPluginModPage, MOBase::IPluginPreview,
        MOBase::IPluginProxy, MOBase::IPluginTool, MOBase::IPluginDiagnose, MOBase::IPluginFileMapper,
        MOBase::IPlugin
      >
    >;

  static_assert(
    boost::mp11::mp_size<PluginTypeOrder>::value == boost::mp11::mp_size<PluginContainer::PluginMap>::value - 1);

public:

  /**
   * @brief Retrieved the (localized) names of the various plugin interfaces.
   *
   * @return the (localized) names of the various plugin interfaces.
   */
  static QStringList pluginInterfaces();

public:

  PluginContainer(OrganizerCore *organizer);
  virtual ~PluginContainer();

  void setUserInterface(IUserInterface *userInterface, QWidget *widget);

  void loadPlugins();
  void unloadPlugins();

  /**
   * @brief Find the game plugin corresponding to the given name.
   *
   * @param name The name of the game to find a plugin for (as returned by
   *     IPluginGame::gameName()).
   *
   * @return the game plugin for the given name, or a null pointer if no
   *     plugin exists for this game.
   */
  MOBase::IPluginGame *managedGame(const QString &name) const;

  /**
   * @brief Retrieve the list of plugins of the given type.
   *
   * @return the list of plugins of the specified type.
   *
   * @tparam T The type of plugin to retrieve.
   */
  template <typename T>
  const std::vector<T*> &plugins() const {
    typename boost::fusion::result_of::at_key<const PluginMap, T>::type temp = boost::fusion::at_key<T>(m_Plugins);
    return temp;
  }

  /**
   * @brief Check if a plugin implement a given interface.
   *
   * @param plugin The plugin to check.
   *
   * @return true if the plugin implements the interface, false otherwise.
   *
   * @tparam The interface type.
   */
  template <typename T>
  bool implementInterface(MOBase::IPlugin* plugin) const {
    // We need a QObject to be able to qobject_cast<> to the plugin types:
    QObject* oPlugin = as_qobject(plugin);

    if (!oPlugin) {
      return false;
    }

    // Find all the names:
    bool implement = false;
    boost::mp11::mp_for_each<PluginTypeOrder>([oPlugin, &implement](const auto* p) {
      using plugin_type = std::decay_t<decltype(*p)>;
      if (qobject_cast<plugin_type*>(oPlugin)) {
        implement = true;
      }
    });

    return implement;
  }

  /**
   * @brief Retrieve a plugin from its name or a corresponding non-IPlugin
   *     interface.
   *
   * @param t Name of the plugin to retrieve, or non-IPlugin interface.
   *
   * @return the corresponding plugin, or a null pointer.
   */
  MOBase::IPlugin* plugin(QString const& pluginName) const;
  MOBase::IPlugin* plugin(MOBase::IPluginDiagnose* diagnose) const;
  MOBase::IPlugin* plugin(MOBase::IPluginFileMapper* mapper) const;

  /**
   * @brief Check if the given plugin is enabled.
   *
   * @param plugin The plugin to check.
   *
   * @return true if the plugin is enabled, false otherwise.
   */
  bool isEnabled(MOBase::IPlugin *plugin) const;

  // These are friendly methods that called isEnabled(plugin(arg)).
  bool isEnabled(QString const& pluginName) const;
  bool isEnabled(MOBase::IPluginDiagnose* diagnose) const;
  bool isEnabled(MOBase::IPluginFileMapper* mapper) const;

  /**
   * @brief Retrieve the requirements for the given plugin.
   *
   * @param plugin The plugin to retrieve the requirements for.
   *
   * @return the requirements (as proxy) for the given plugin.
   */
  std::vector<PluginRequirementProxy> requirements(MOBase::IPlugin* plugin) const;

  /**
   * @brief Retrieved the (localized) names of interfaces implemented by the given
   *     plugin.
   *
   * @param plugin The plugin to retrieve interface for.
   *
   * @return the (localized) names of interfaces implemented by this plugin.
   */
  QStringList implementedInterfaces(MOBase::IPlugin* plugin) const;

  /**
   * @brief Return the (localized) name of the most important interface implemented by
   *     the given plugin.
   *
   * The order of interfaces is defined in X.
   *
   * @param plugin The plugin to retrieve the interface for.
   *
   * @return the (localized) name of the most important interface implemented by this plugin.
   */
  QString topImplementedInterface(MOBase::IPlugin* plugin) const;

  /**
   * @return the preview generator.
   */
  const PreviewGenerator &previewGenerator() const;

  /**
   * @return the list of plugin file names, including proxied plugins.
   */
  QStringList pluginFileNames() const;

public: // IPluginDiagnose interface

  virtual std::vector<unsigned int> activeProblems() const;
  virtual QString shortDescription(unsigned int key) const;
  virtual QString fullDescription(unsigned int key) const;
  virtual bool hasGuidedFix(unsigned int key) const;
  virtual void startGuidedFix(unsigned int key) const;

signals:

  void diagnosisUpdate();

private:

  /**
   * @brief Find the QObject* corresponding to the given plugin.
   *
   * @param plugin The plugin to find the QObject* for.
   *
   * @return a QObject* for the given plugin.
   */
  QObject* as_qobject(MOBase::IPlugin* plugin) const;


  bool initPlugin(MOBase::IPlugin *plugin);
  bool initProxyPlugin(MOBase::IPlugin *plugin);
  void registerGame(MOBase::IPluginGame *game);
  bool registerPlugin(QObject *pluginObj, const QString &fileName);

  OrganizerCore *m_Organizer;

  IUserInterface *m_UserInterface;

  PluginMap m_Plugins;

  // This maps allow access to IPlugin* from name or diagnose/mapper object.
  AccessPluginMap m_AccessPlugins;

  std::map<MOBase::IPlugin*, OrganizerProxy*> m_Proxies;
  std::map<MOBase::IPlugin*, std::vector<std::unique_ptr<const MOBase::PluginRequirement>>> m_Requirements;

  std::map<QString, MOBase::IPluginGame*> m_SupportedGames;
  std::vector<boost::signals2::connection> m_DiagnosisConnections;
  QStringList m_FailedPlugins;
  std::vector<QPluginLoader*> m_PluginLoaders;

  PreviewGenerator m_PreviewGenerator;

  QFile m_PluginsCheck;
};


#endif // PLUGINCONTAINER_H