From 7a4f6d5344031db828f5dc2257c1d19f251e7f05 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Wed, 11 Nov 2020 13:39:53 +0100 Subject: Improve requirements handling. --- src/plugincontainer.h | 109 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 16 deletions(-) (limited to 'src/plugincontainer.h') diff --git a/src/plugincontainer.h b/src/plugincontainer.h index 26ad2046..cd1d03ca 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -27,24 +27,77 @@ class IUserInterface; class OrganizerProxy; +/** + * @brief Class that wrap multiple requirements for a plugin together. THis + * class owns the requirements. + */ +class PluginRequirements { +public: + + // Small intermediate class. + struct Problem { + public: + + QString description() const { return m_Requirement->description(m_Id); } + + + private: + Problem(const MOBase::IPluginRequirement* requirement, unsigned int id) : + m_Requirement(requirement), m_Id(id) { } + + const MOBase::IPluginRequirement* m_Requirement; + unsigned int m_Id; + + friend class PluginRequirements; + }; -// Small class that allows calling check() for plugin requirements -// without passing the IOrganizer. -class PluginRequirementProxy { public: - std::vector problems() const; - QString description(unsigned int id) const; + /** + * @return true if the plugin can be enabled (all requirements are met). + */ + bool canEnable() const; -private: + /** + * @return the proxy that created this plugin, if any. + */ + MOBase::IPluginProxy* proxy() const; - const MOBase::IPluginRequirement* m_Requirement; - OrganizerProxy* m_Proxy; + /** + * @return the list of problems to be resolved before enabling the plugin. + */ + std::vector problems() const; - PluginRequirementProxy(const MOBase::IPluginRequirement* requirement, OrganizerProxy* proxy); + /** + * @return the name of the games (gameName()) this plugin can be used with, or an empty + * list if this plugin does not require particular games. + */ + QStringList requiredGames() const; + + /** + * @return the list of plugins currently enabled that would have to be disabled + * if this plugin was disabled. + */ + std::vector requiredFor() const; + +private: + + // Accumulator version for requiredFor() to avoid infinite recursion. + void requiredFor(std::vector& required, std::set& visited) const; friend class PluginContainer; + PluginContainer* m_PluginContainer; + MOBase::IPlugin* m_Plugin; + MOBase::IPluginProxy* m_PluginProxy; + std::vector> m_Requirements; + MOBase::IOrganizer* m_Organizer; + std::vector m_RequiredFor; + + PluginRequirements( + PluginContainer* pluginContainer, MOBase::IPlugin* plugin, + MOBase::IOrganizer* proxy, MOBase::IPluginProxy* pluginProxy); + }; @@ -175,6 +228,11 @@ public: MOBase::IPlugin* plugin(MOBase::IPluginDiagnose* diagnose) const; MOBase::IPlugin* plugin(MOBase::IPluginFileMapper* mapper) const; + /** + * @return the IPlugin interface to the currently managed game. + */ + MOBase::IPlugin* managedGame() const; + /** * @brief Check if the given plugin is enabled. * @@ -182,13 +240,23 @@ public: * * @return true if the plugin is enabled, false otherwise. */ - bool isEnabled(MOBase::IPlugin *plugin) const; + 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 Enable or disable a plugin. + * + * @param plugin The plugin to enable or disable. + * @param enable true to enable, false to disable. + * @param dependencies If true and enable is false, dependencies will also + * be disabled (see PluginRequirements::requiredFor). + */ + void setEnabled(MOBase::IPlugin* plugin, bool enable, bool dependencies = true); + /** * @brief Retrieve the requirements for the given plugin. * @@ -196,7 +264,7 @@ public: * * @return the requirements (as proxy) for the given plugin. */ - std::vector requirements(MOBase::IPlugin* plugin) const; + const PluginRequirements& requirements(MOBase::IPlugin* plugin) const; /** * @brief Retrieved the (localized) names of interfaces implemented by the given @@ -244,6 +312,8 @@ signals: private: + friend class PluginRequirements; + /** * @brief Find the QObject* corresponding to the given plugin. * @@ -253,11 +323,19 @@ private: */ QObject* as_qobject(MOBase::IPlugin* plugin) const; + /** + * @brief Initialize a plugin. + * + * @param plugin The plugin to initialize. + * @param proxy The proxy that created this plugin (can be null). + * + * @return true if the plugin was initialized correctly, false otherwise. + */ + bool initPlugin(MOBase::IPlugin *plugin, MOBase::IPluginProxy* proxy); - bool initPlugin(MOBase::IPlugin *plugin); - bool initProxyPlugin(MOBase::IPlugin *plugin); void registerGame(MOBase::IPluginGame *game); - bool registerPlugin(QObject *pluginObj, const QString &fileName); + + bool registerPlugin(QObject *pluginObj, const QString &fileName, MOBase::IPluginProxy *proxy); OrganizerCore *m_Organizer; @@ -268,8 +346,7 @@ private: // This maps allow access to IPlugin* from name or diagnose/mapper object. AccessPluginMap m_AccessPlugins; - std::map m_Proxies; - std::map>> m_Requirements; + std::map m_Requirements; std::map m_SupportedGames; std::vector m_DiagnosisConnections; -- cgit v1.3.1