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/settingsdialogplugins.cpp | 97 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 5faa0dc9..8a70475c 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -3,9 +3,10 @@ #include "noeditdelegate.h" #include +#include "organizercore.h" #include "plugincontainer.h" -using MOBase::IPlugin; +using namespace MOBase; PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginContainer, SettingsDialog& d) : SettingsTab(s, d), m_pluginContainer(pluginContainer) @@ -66,15 +67,36 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta QObject::connect( ui->pluginsList, &QTreeWidget::currentItemChanged, [&](auto* current, auto* previous) { on_pluginsList_currentItemChanged(current, previous); }); + QObject::connect( + ui->enabledCheckbox, &QCheckBox::clicked, + [&](bool checked) { on_checkboxEnabled_clicked(checked); }); QShortcut *delShortcut = new QShortcut( QKeySequence(Qt::Key_Delete), ui->pluginBlacklist); QObject::connect(delShortcut, &QShortcut::activated, &dialog(), [&] { deleteBlacklistItem(); }); - QObject::connect(&m_filter, &MOBase::FilterWidget::changed, [&] { filterPluginList(); }); + QObject::connect(&m_filter, &FilterWidget::changed, [&] { filterPluginList(); }); + updateListItems(); filterPluginList(); } +void PluginsSettingsTab::updateListItems() +{ + for (auto i = 0; i < ui->pluginsList->topLevelItemCount(); ++i) { + auto* topLevelItem = ui->pluginsList->topLevelItem(i); + for (auto j = 0; j < topLevelItem->childCount(); ++j) { + auto* item = topLevelItem->child(j); + auto* plugin = this->plugin(item); + + if (!m_pluginContainer->implementInterface(plugin) + && !m_pluginContainer->isEnabled(plugin)) { + item->setBackgroundColor(0, Qt::gray); + } + } + } + +} + void PluginsSettingsTab::filterPluginList() { QTreeWidgetItem* firstNotHidden = nullptr; @@ -109,7 +131,7 @@ void PluginsSettingsTab::filterPluginList() auto selectedItems = ui->pluginsList->selectedItems(); if (!selectedItems.isEmpty() && selectedItems[0]->isHidden()) { selectedItems[0]->setSelected(false); - + if (firstNotHidden) { firstNotHidden->setSelected(true); } @@ -150,6 +172,71 @@ void PluginsSettingsTab::closing() storeSettings(ui->pluginsList->currentItem()); } +void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) +{ + // Retrieve the plugin: + auto *item = ui->pluginsList->currentItem(); + if (!item || !item->data(0, ROLE_PLUGIN).isValid()) { + return; + } + IPlugin* plugin = this->plugin(item); + const auto& requirements = m_pluginContainer->requirements(plugin); + + // User wants to enable: + if (checked) { + auto problems = requirements.problems(); + if (!problems.empty()) { + QStringList descriptions; + for (auto& problem : problems) { + descriptions.append(problem.description()); + } + QMessageBox::warning( + parentWidget(), QObject::tr("Cannot enable plugin"), + QObject::tr("

This plugin cannot be enabled:

    %1
") + .arg("
  • " + descriptions.join("
  • ") + "
  • "), QMessageBox::Ok); + ui->enabledCheckbox->setChecked(false); + return; + } + + m_pluginContainer->setEnabled(plugin, false, true); + } + else { + // Custom check for proxy + current game: + if (m_pluginContainer->implementInterface(plugin)) { + auto* game = m_pluginContainer->managedGame(); + if (m_pluginContainer->requirements(game).proxy() == plugin) { + QMessageBox::warning( + parentWidget(), QObject::tr("Cannot disable plugin"), + QObject::tr("This plugin is used by the current game plugin and cannot disabled."), QMessageBox::Ok); + ui->enabledCheckbox->setChecked(true); + return; + } + } + + // Check if the plugins is required for other plugins: + auto requiredFor = requirements.requiredFor(); + if (!requiredFor.empty()) { + QStringList pluginNames; + for (auto& p : requiredFor) { + pluginNames.append(p->localizedName()); + } + pluginNames.sort(); + QString message = QObject::tr( + "

    Disabling this plugin will also disable the following plugins:

      %1

    Do you want to continue?

    ") + .arg("
  • " + pluginNames.join("
  • ") + "
  • "); + if (QMessageBox::warning( + parentWidget(), QObject::tr("Really disable plugin?"), message, + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { + ui->enabledCheckbox->setChecked(true); + return; + } + } + m_pluginContainer->setEnabled(plugin, false, true); + } + + updateListItems(); +} + void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { storeSettings(previous); @@ -164,6 +251,10 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->versionLabel->setText(plugin->version().canonicalString()); ui->descriptionLabel->setText(plugin->description()); + ui->enabledCheckbox->setVisible( + !m_pluginContainer->implementInterface(plugin)); + ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); + QVariantMap settings = current->data(0, ROLE_SETTINGS).toMap(); QVariantMap descriptions = current->data(0, ROLE_DESCRIPTIONS).toMap(); ui->pluginSettingsList->setEnabled(settings.count() != 0); -- cgit v1.3.1 From e528d8b0ad843bf2e728f26c3a1bade145e5ad07 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 11 Nov 2020 14:59:11 +0100 Subject: Fix display of tools in the MainWindow menu. --- src/iuserinterface.h | 2 -- src/mainwindow.cpp | 51 ++++++++++++++++++++++--------------------- src/mainwindow.h | 9 ++++---- src/plugincontainer.cpp | 4 ---- src/settingsdialogplugins.cpp | 9 ++++---- 5 files changed, 34 insertions(+), 41 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/iuserinterface.h b/src/iuserinterface.h index cce89070..a2a91e62 100644 --- a/src/iuserinterface.h +++ b/src/iuserinterface.h @@ -13,8 +13,6 @@ class IUserInterface { public: - virtual void registerPluginTool(MOBase::IPluginTool *tool, QString name = QString(), QMenu *menu = nullptr) = 0; - virtual void registerPluginTools(std::vector toolPlugins) = 0; virtual void registerModPage(MOBase::IPluginModPage *modPage) = 0; virtual void installTranslator(const QString &name) = 0; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9bbe3da9..b5a6382f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -467,6 +467,7 @@ MainWindow::MainWindow(Settings &settings connect(ui->toolBar, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(toolBar_customContextMenuRequested(QPoint))); connect(ui->menuToolbars, &QMenu::aboutToShow, [&]{ updateToolbarMenu(); }); connect(ui->menuView, &QMenu::aboutToShow, [&]{ updateViewMenu(); }); + connect(ui->actionTool->menu(), &QMenu::aboutToShow, [&] { updateToolMenu(); }); connect(&m_OrganizerCore, &OrganizerCore::modInstalled, this, &MainWindow::modInstalled); connect(&m_OrganizerCore, &OrganizerCore::close, this, &QMainWindow::close); @@ -508,8 +509,6 @@ MainWindow::MainWindow(Settings &settings installTranslator(QFileInfo(fileName).baseName()); } - registerPluginTools(m_PluginContainer.plugins()); - for (IPluginModPage *modPagePlugin : m_PluginContainer.plugins()) { registerModPage(modPagePlugin); } @@ -1597,22 +1596,6 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event) return false; } - -void MainWindow::toolPluginInvoke() -{ - QAction *triggeredAction = qobject_cast(sender()); - IPluginTool *plugin = qobject_cast(triggeredAction->data().value()); - if (plugin != nullptr) { - try { - plugin->display(); - } catch (const std::exception &e) { - reportError(tr("Plugin \"%1\" failed: %2").arg(plugin->name()).arg(e.what())); - } catch (...) { - reportError(tr("Plugin \"%1\" failed").arg(plugin->name())); - } - } -} - void MainWindow::modPagePluginInvoke() { QAction *triggeredAction = qobject_cast(sender()); @@ -1648,24 +1631,42 @@ void MainWindow::registerPluginTool(IPluginTool *tool, QString name, QMenu *menu QAction *action = new QAction(tool->icon(), name, menu); action->setToolTip(tool->tooltip()); tool->setParentWidget(this); - action->setData(QVariant::fromValue((QObject*)tool)); - connect(action, SIGNAL(triggered()), this, SLOT(toolPluginInvoke()), Qt::QueuedConnection); + connect(action, &QAction::triggered, this, [this, tool]() { + try { + tool->display(); + } + catch (const std::exception& e) { + reportError(tr("Plugin \"%1\" failed: %2").arg(tool->localizedName()).arg(e.what())); + } + catch (...) { + reportError(tr("Plugin \"%1\" failed").arg(tool->localizedName())); + } + }, Qt::QueuedConnection); menu->addAction(action); - if (!m_PluginContainer.isEnabled(tool)) { - action->setVisible(false); - } } -void MainWindow::registerPluginTools(std::vector toolPlugins) +void MainWindow::updateToolMenu() { + // Clear the menu: + ui->actionTool->menu()->clear(); + + std::vector toolPlugins = m_PluginContainer.plugins(); + // Sort the plugins by display name - std::sort(toolPlugins.begin(), toolPlugins.end(), + std::sort(std::begin(toolPlugins), std::end(toolPlugins), [](IPluginTool *left, IPluginTool *right) { return left->displayName().toLower() < right->displayName().toLower(); } ); + // Remove disabled plugins: + toolPlugins.erase( + std::remove_if(std::begin(toolPlugins), std::end(toolPlugins), [&](auto* tool) { + return !m_PluginContainer.isEnabled(tool); + }), + toolPlugins.end()); + // Group the plugins into submenus QMap>> submenuMap; for (auto toolPlugin : toolPlugins) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 8b2188c8..d4aa0bc2 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -129,10 +129,6 @@ public: void saveArchiveList(); - void registerPluginTool(MOBase::IPluginTool *tool, QString name = QString(), QMenu *menu = nullptr); - void registerPluginTools(std::vector toolPlugins); - void registerModPage(MOBase::IPluginModPage *modPage); - void addPrimaryCategoryCandidates(QMenu *primaryCategoryMenu, ModInfo::Ptr info); void installTranslator(const QString &name); @@ -160,7 +156,6 @@ public slots: void directory_refreshed(); - void toolPluginInvoke(); void modPagePluginInvoke(); signals: @@ -212,7 +207,11 @@ private: void setToolbarSize(const QSize& s); void setToolbarButtonStyle(Qt::ToolButtonStyle s); + void registerModPage(MOBase::IPluginModPage* modPage); + void registerPluginTool(MOBase::IPluginTool* tool, QString name = QString(), QMenu* menu = nullptr); + void updateToolbarMenu(); + void updateToolMenu(); void updateViewMenu(); QMenu* createPopupMenu() override; diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 021fe3c8..030fdb31 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -180,10 +180,6 @@ void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *w for (IPluginModPage *modPage : bf::at_key(m_Plugins)) { userInterface->registerModPage(modPage); } - - for (IPluginTool *tool : bf::at_key(m_Plugins)) { - userInterface->registerPluginTool(tool); - } } m_UserInterface = userInterface; diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 8a70475c..0c02ffb0 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -88,10 +88,9 @@ void PluginsSettingsTab::updateListItems() auto* item = topLevelItem->child(j); auto* plugin = this->plugin(item); - if (!m_pluginContainer->implementInterface(plugin) - && !m_pluginContainer->isEnabled(plugin)) { - item->setBackgroundColor(0, Qt::gray); - } + bool inactive = !m_pluginContainer->implementInterface(plugin) + && !m_pluginContainer->isEnabled(plugin); + // TODO: Better display. } } @@ -198,7 +197,7 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) return; } - m_pluginContainer->setEnabled(plugin, false, true); + m_pluginContainer->setEnabled(plugin, true, false); } else { // Custom check for proxy + current game: -- cgit v1.3.1 From a449098db96527f8ac772cd518657d8e22018468 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 11 Nov 2020 16:14:55 +0100 Subject: Cleaner IPluginRequirement. --- src/plugincontainer.cpp | 9 +++++---- src/plugincontainer.h | 21 +-------------------- src/settingsdialogplugins.cpp | 2 +- 3 files changed, 7 insertions(+), 25 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 030fdb31..a218b1f0 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -74,12 +74,13 @@ MOBase::IPluginProxy* PluginRequirements::proxy() const return m_PluginProxy; } -std::vector PluginRequirements::problems() const +std::vector PluginRequirements::problems() const { - std::vector result; + std::vector result; for (auto& requirement : m_Requirements) { - for (auto p : requirement->problems(m_Organizer)) { - result.push_back(Problem(requirement.get(), p)); + auto p = requirement->check(m_Organizer); + if (p) { + result.push_back(*p); } } return result; diff --git a/src/plugincontainer.h b/src/plugincontainer.h index cd1d03ca..520ee83e 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -32,25 +32,6 @@ class OrganizerProxy; * 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; - }; - public: /** @@ -66,7 +47,7 @@ public: /** * @return the list of problems to be resolved before enabling the plugin. */ - std::vector problems() const; + std::vector problems() const; /** * @return the name of the games (gameName()) this plugin can be used with, or an empty diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 0c02ffb0..1e2117cf 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -187,7 +187,7 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) if (!problems.empty()) { QStringList descriptions; for (auto& problem : problems) { - descriptions.append(problem.description()); + descriptions.append(problem.shortDescription()); } QMessageBox::warning( parentWidget(), QObject::tr("Cannot enable plugin"), -- cgit v1.3.1 From c0ace25a871c4d938fb4be10b0357e263db677cb Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 11 Nov 2020 21:01:17 +0100 Subject: Use IPlugin::master() when present. --- src/plugincontainer.cpp | 40 ++++++++++++++++++++++++++++++++++++++- src/plugincontainer.h | 15 +++++++++++++++ src/settingsdialogplugins.cpp | 44 +++++++++++++++++++++++++++++++++---------- 3 files changed, 88 insertions(+), 11 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 85068d2d..ab68cc1d 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -69,11 +69,31 @@ PluginRequirements::PluginRequirements(PluginContainer* pluginContainer, MOBase: } } -MOBase::IPluginProxy* PluginRequirements::proxy() const +IPluginProxy* PluginRequirements::proxy() const { return m_PluginProxy; } +IPlugin* PluginRequirements::master() const +{ + if (m_Plugin->master().isEmpty()) { + return nullptr; + } + return m_PluginContainer->plugin(m_Plugin->master()); +} + +std::vector PluginRequirements::children() const +{ + std::vector children; + for (auto* obj : m_PluginContainer->plugins()) { + auto* plugin = qobject_cast(obj); + if (plugin && plugin->master().compare(m_Plugin->name(), Qt::CaseInsensitive) == 0) { + children.push_back(plugin); + } + } + return children; +} + std::vector PluginRequirements::problems() const { std::vector result; @@ -90,6 +110,11 @@ bool PluginRequirements::canEnable() const return problems().empty(); } +bool PluginRequirements::hasRequirements() const +{ + return !m_Requirements.empty(); +} + QStringList PluginRequirements::requiredGames() const { // We look for a "GameDependencyRequirement" - There can be only one since otherwise @@ -304,6 +329,11 @@ bool PluginContainer::initPlugin(IPlugin *plugin, IPluginProxy *pluginProxy) m_Requirements.emplace(plugin, PluginRequirements(this, plugin, proxy, pluginProxy)); + if (!plugin->master().isEmpty() && m_Requirements.at(plugin).hasRequirements()) { + log::warn("a plugin cannot have requirements if it has a master"); + return false; + } + return true; } @@ -503,11 +533,19 @@ bool PluginContainer::isEnabled(IPlugin* plugin) const void PluginContainer::setEnabled(MOBase::IPlugin* plugin, bool enable, bool dependencies) { + // If required, disable dependencies: if (!enable && dependencies) { for (auto* p : requirements(plugin).requiredFor()) { setEnabled(p, false, false); // No need to "recurse" here since requiredFor already does it. } } + + // Always disable/enable child plugins: + for (auto* p : requirements(plugin).children()) { + // "Child" plugin should have no dependencies. + setEnabled(p, enable, false); + } + m_Organizer->setPersistent(plugin->name(), "enabled", enable, true); } diff --git a/src/plugincontainer.h b/src/plugincontainer.h index 520ee83e..e9761cb1 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -39,11 +39,26 @@ public: */ bool canEnable() const; + /** + * @return true if this plugin has requirements (satisfied or not). + */ + bool hasRequirements() const; + /** * @return the proxy that created this plugin, if any. */ MOBase::IPluginProxy* proxy() const; + /** + * @return the master of this plugin, if any. + */ + MOBase::IPlugin* master() const; + + /** + * @return the plugins this plugin is master of. + */ + std::vector children() const; + /** * @return the list of problems to be resolved before enabling the plugin. */ diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 1e2117cf..15c0b424 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -31,14 +31,10 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta // display plugin settings QSet handledNames; for (IPlugin* plugin : settings().plugins().plugins()) { - if (handledNames.contains(plugin->name())) { - continue; - } - if (!m_filter.matches([plugin](const QRegularExpression& regex) { - return regex.match(plugin->localizedName()).hasMatch(); - })) { + if (handledNames.contains(plugin->name()) || !plugin->master().isEmpty()) { continue; } + QTreeWidgetItem* listItem = new QTreeWidgetItem( topItems.at(m_pluginContainer->topImplementedInterface(plugin))); listItem->setData(0, Qt::DisplayRole, plugin->localizedName()); @@ -46,6 +42,18 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta listItem->setData(0, ROLE_SETTINGS, settings().plugins().settings(plugin->name())); listItem->setData(0, ROLE_DESCRIPTIONS, settings().plugins().descriptions(plugin->name())); + // Handle child item: + auto children = m_pluginContainer->requirements(plugin).children(); + for (auto* child : children) { + QTreeWidgetItem* childItem = new QTreeWidgetItem(listItem); + childItem->setData(0, Qt::DisplayRole, child->localizedName()); + childItem->setData(0, ROLE_PLUGIN, QVariant::fromValue((void*)child)); + childItem->setData(0, ROLE_SETTINGS, settings().plugins().settings(child->name())); + childItem->setData(0, ROLE_DESCRIPTIONS, settings().plugins().descriptions(child->name())); + + handledNames.insert(child->name()); + } + handledNames.insert(plugin->name()); } @@ -90,7 +98,13 @@ void PluginsSettingsTab::updateListItems() bool inactive = !m_pluginContainer->implementInterface(plugin) && !m_pluginContainer->isEnabled(plugin); - // TODO: Better display. + + auto font = item->font(0); + font.setItalic(inactive); + item->setFont(0, font); + for (auto k = 0; k < item->childCount(); ++k) { + item->child(k)->setFont(0, font); + } } } @@ -108,9 +122,18 @@ void PluginsSettingsTab::filterPluginList() auto* item = topLevelItem->child(j); auto* plugin = this->plugin(item); - if (m_filter.matches([plugin](const QRegularExpression& regex) { + // Check the item or the child - If any match (item or child), the whole + // group is displayed. + bool match = m_filter.matches([plugin](const QRegularExpression& regex) { return regex.match(plugin->localizedName()).hasMatch(); - })) { + }); + for (auto* child : m_pluginContainer->requirements(plugin).children()) { + m_filter.matches([child](const QRegularExpression& regex) { + return regex.match(child->localizedName()).hasMatch(); + }); + } + + if (match) { found = true; item->setHidden(false); @@ -251,7 +274,8 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->descriptionLabel->setText(plugin->description()); ui->enabledCheckbox->setVisible( - !m_pluginContainer->implementInterface(plugin)); + !m_pluginContainer->implementInterface(plugin) + && plugin->master().isEmpty()); ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); QVariantMap settings = current->data(0, ROLE_SETTINGS).toMap(); -- cgit v1.3.1 From 90beed8c7b356ef58431721ffe8eb4abff3da032 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 11 Nov 2020 22:45:33 +0100 Subject: Handle proxy dependencies differently. --- src/plugincontainer.cpp | 37 ++++++++++++++++++++++++------------- src/plugincontainer.h | 10 ++++++++++ src/settingsdialogplugins.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 13 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index bcd0db44..05c6790d 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -58,14 +58,11 @@ PluginRequirements::PluginRequirements(PluginContainer* pluginContainer, MOBase: , m_Plugin(plugin) , m_PluginProxy(pluginProxy) , m_Organizer(proxy) -{ - for (auto* requirement : plugin->requirements()) { - m_Requirements.emplace_back(requirement); - } +{ } - // TODO: - if (pluginProxy) { - m_Requirements.emplace_back(PluginRequirementFactory::pluginDependency(pluginProxy->name())); +void PluginRequirements::fetchRequirements() { + for (auto* requirement : m_Plugin->requirements()) { + m_Requirements.emplace_back(requirement); } } @@ -74,6 +71,20 @@ IPluginProxy* PluginRequirements::proxy() const return m_PluginProxy; } +std::vector PluginRequirements::proxied() const +{ + std::vector children; + if (dynamic_cast(m_Plugin)) { + for (auto* obj : m_PluginContainer->plugins()) { + auto* plugin = qobject_cast(obj); + if (plugin && m_PluginContainer->requirements(plugin).proxy() == m_Plugin) { + children.push_back(plugin); + } + } + } + return children; +} + IPlugin* PluginRequirements::master() const { if (m_Plugin->master().isEmpty()) { @@ -322,12 +333,15 @@ bool PluginContainer::initPlugin(IPlugin *plugin, IPluginProxy *pluginProxy) return true; } + auto [it, bl] = m_Requirements.emplace(plugin, PluginRequirements(this, plugin, proxy, pluginProxy)); + if (!plugin->init(proxy)) { log::warn("plugin failed to initialize"); return false; } - m_Requirements.emplace(plugin, PluginRequirements(this, plugin, proxy, pluginProxy)); + // Update requirements: + it->second.fetchRequirements(); return true; } @@ -519,11 +533,8 @@ bool PluginContainer::isEnabled(IPlugin* plugin) const return false; } - // Check the requirements (if a plugin checks in init(), the requirements have - // not been computed yet): - auto it = m_Requirements.find(plugin); - - return it == std::end(m_Requirements) ? true : it->second.canEnable(); + // Check the requirements: + return m_Requirements.at(plugin).canEnable(); } void PluginContainer::setEnabled(MOBase::IPlugin* plugin, bool enable, bool dependencies) diff --git a/src/plugincontainer.h b/src/plugincontainer.h index e9761cb1..b37b48cd 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -49,6 +49,11 @@ public: */ MOBase::IPluginProxy* proxy() const; + /** + * @return the list of plugins this plugin proxies (if it's a proxy plugin). + */ + std::vector proxied() const; + /** * @return the master of this plugin, if any. */ @@ -81,6 +86,11 @@ private: // Accumulator version for requiredFor() to avoid infinite recursion. void requiredFor(std::vector& required, std::set& visited) const; + // Retrieve the requirements from the underlying plugin, take ownership on them + // and store them. We cannot do this in the constructor because we want to have a + // constructed object before calling init(). + void fetchRequirements(); + friend class PluginContainer; PluginContainer* m_PluginContainer; diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 15c0b424..e9d36c57 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -225,6 +225,8 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) else { // Custom check for proxy + current game: if (m_pluginContainer->implementInterface(plugin)) { + + // Current game: auto* game = m_pluginContainer->managedGame(); if (m_pluginContainer->requirements(game).proxy() == plugin) { QMessageBox::warning( @@ -233,6 +235,25 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) ui->enabledCheckbox->setChecked(true); return; } + + // Check the proxied plugins: + auto proxied = requirements.proxied(); + QStringList pluginNames; + for (auto& p : proxied) { + pluginNames.append(p->localizedName()); + } + pluginNames.sort(); + QString message = QObject::tr( + "

    Disabling this plugin will prevent the following plugins from working correctly:

      %1
    " + "

    Do you want to continue? You will need to restart ModOrganizer2 for the change to take effect.

    ") + .arg("
  • " + pluginNames.join("
  • ") + "
  • "); + if (QMessageBox::warning( + parentWidget(), QObject::tr("Really disable plugin?"), message, + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { + ui->enabledCheckbox->setChecked(true); + return; + } + } // Check if the plugins is required for other plugins: @@ -256,6 +277,11 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) m_pluginContainer->setEnabled(plugin, false, true); } + // Proxy was disabled / enabled, need restart: + if (m_pluginContainer->implementInterface(plugin)) { + dialog().setExitNeeded(Exit::Restart); + } + updateListItems(); } -- cgit v1.3.1 From 4fa44fe37e9b2f9837f7fcbaf9361f9675669782 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 11 Nov 2020 22:54:45 +0100 Subject: Remove duplicate dependencies. --- src/settingsdialogplugins.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index e9d36c57..556cadcb 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -242,12 +242,13 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) for (auto& p : proxied) { pluginNames.append(p->localizedName()); } + pluginNames.removeDuplicates(); pluginNames.sort(); QString message = QObject::tr( - "

    Disabling this plugin will prevent the following plugins from working correctly:

      %1
    " + "

    Disabling this plugin will prevent the following plugins from working:

      %1
    " "

    Do you want to continue? You will need to restart ModOrganizer2 for the change to take effect.

    ") .arg("
  • " + pluginNames.join("
  • ") + "
  • "); - if (QMessageBox::warning( + if (QMessageBox::critical( parentWidget(), QObject::tr("Really disable plugin?"), message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { ui->enabledCheckbox->setChecked(true); @@ -263,6 +264,7 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) for (auto& p : requiredFor) { pluginNames.append(p->localizedName()); } + pluginNames.removeDuplicates(); pluginNames.sort(); QString message = QObject::tr( "

    Disabling this plugin will also disable the following plugins:

      %1

    Do you want to continue?

    ") -- cgit v1.3.1 From 15059907c9bd0061f502c64457fce20c7e60a07f Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 12 Nov 2020 20:47:12 +0100 Subject: Proper handling of proxied plugins that implement multiple interfaces. --- src/plugincontainer.cpp | 149 ++++++++++++++++++++++++++++++++++-------- src/plugincontainer.h | 24 ++++++- src/settingsdialogplugins.cpp | 2 - 3 files changed, 144 insertions(+), 31 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 05c6790d..94e4760c 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -53,12 +53,18 @@ QStringList PluginContainer::pluginInterfaces() // PluginRequirementProxy -PluginRequirements::PluginRequirements(PluginContainer* pluginContainer, MOBase::IPlugin* plugin, IOrganizer* proxy, MOBase::IPluginProxy* pluginProxy) +PluginRequirements::PluginRequirements( + PluginContainer* pluginContainer, MOBase::IPlugin* plugin, IOrganizer* proxy, + MOBase::IPluginProxy* pluginProxy) : m_PluginContainer(pluginContainer) , m_Plugin(plugin) , m_PluginProxy(pluginProxy) + , m_Master(nullptr) , m_Organizer(proxy) -{ } +{ + // There are a lots of things we cannot set here (e.g. m_Master) because we do not + // know the order plugins are loaded. +} void PluginRequirements::fetchRequirements() { for (auto* requirement : m_Plugin->requirements()) { @@ -87,18 +93,36 @@ std::vector PluginRequirements::proxied() const IPlugin* PluginRequirements::master() const { + // If we have a m_Master, it was forced and thus override the default master(). + if (m_Master) { + return m_Master; + } + if (m_Plugin->master().isEmpty()) { return nullptr; } + return m_PluginContainer->plugin(m_Plugin->master()); } +void PluginRequirements::setMaster(IPlugin* master) +{ + m_Master = master; +} + std::vector PluginRequirements::children() const { std::vector children; for (auto* obj : m_PluginContainer->plugins()) { auto* plugin = qobject_cast(obj); - if (plugin && plugin->master().compare(m_Plugin->name(), Qt::CaseInsensitive) == 0) { + + // Not checking master() but requirements().master() due to "hidden" + // masters. + // If the master has the same name as the plugin, this is a "hidden" + // master, we do not had it here. + if (plugin + && m_PluginContainer->requirements(plugin).master() == m_Plugin + && plugin->name() != m_Plugin->name()) { children.push_back(plugin); } } @@ -205,7 +229,6 @@ PluginContainer::~PluginContainer() { unloadPlugins(); } - void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *widget) { for (IPluginProxy *proxy : bf::at_key(m_Plugins)) { @@ -221,7 +244,6 @@ void PluginContainer::setUserInterface(IUserInterface *userInterface, QWidget *w m_UserInterface = userInterface; } - QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const { // We need a QObject to be able to qobject_cast<> to the plugin types: @@ -251,7 +273,6 @@ QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const return names; } - QString PluginContainer::topImplementedInterface(IPlugin* plugin) const { // We need a QObject to be able to qobject_cast<> to the plugin types: @@ -276,6 +297,21 @@ QString PluginContainer::topImplementedInterface(IPlugin* plugin) const return name; } +bool PluginContainer::isBetterInterface(QObject* lhs, QObject* rhs) const +{ + int count = 0, lhsIdx = -1, rhsIdx = -1; + boost::mp11::mp_for_each([&](const auto* p) { + using plugin_type = std::decay_t; + if (lhsIdx < 0 && qobject_cast(lhs)) { + lhsIdx = count; + } + if (rhsIdx < 0 && qobject_cast(rhs)) { + rhsIdx = count; + } + ++count; + }); + return lhsIdx < rhsIdx; +} QStringList PluginContainer::pluginFileNames() const { @@ -292,14 +328,13 @@ QStringList PluginContainer::pluginFileNames() const return result; } - QObject* PluginContainer::as_qobject(MOBase::IPlugin* plugin) const { // Find the correspond QObject - Can this be done safely with a cast? auto& objects = bf::at_key(m_Plugins); auto it = std::find_if(std::begin(objects), std::end(objects), [plugin](QObject* obj) { return qobject_cast(obj) == plugin; - }); + }); if (it == std::end(objects)) { return nullptr; @@ -351,19 +386,44 @@ void PluginContainer::registerGame(IPluginGame *game) m_SupportedGames.insert({ game->gameName(), game }); } -bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, MOBase::IPluginProxy* pluginProxy) +IPlugin* PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, MOBase::IPluginProxy* pluginProxy) { - // Storing the original QObject* is a bit of a hack as I couldn't figure out any - // way to cast directly between IPlugin* and IPluginDiagnose* - bf::at_key(m_Plugins).push_back(plugin); // generic treatment for all plugins IPlugin *pluginObj = qobject_cast(plugin); if (pluginObj == nullptr) { - log::debug("not an IPlugin"); - return false; + log::debug("PluginContainer::registerPlugin() called with a non IPlugin QObject."); + return nullptr; } - bf::at_key(m_AccessPlugins)[pluginObj->name()] = pluginObj; + + // If we already a plugin with this name: + auto& mapNames = bf::at_key(m_AccessPlugins); + if (mapNames.contains(pluginObj->name())) { + + IPlugin* other = mapNames[pluginObj->name()]; + + // If both plugins are from the same proxy and the same file, this is usually + // ok (in theory some one could write two different classes from the same Python file/module): + if (pluginProxy && m_Requirements.at(other).proxy() == pluginProxy + && as_qobject(other)->property("filename") == fileName) { + if (isBetterInterface(plugin, as_qobject(other))) { + log::debug("replacing plugin '{}' with interfaces [{}] by one with interfaces [{}]", + pluginObj->name(), implementedInterfaces(other).join(", "), implementedInterfaces(pluginObj).join(", ")); + bf::at_key(m_AccessPlugins)[pluginObj->name()] = pluginObj; + } + } + else { + log::warn("Trying to register two plugins with the name '{}', the second one will not be registered.", + pluginObj->name()); + } + } + else { + bf::at_key(m_AccessPlugins)[pluginObj->name()] = pluginObj; + } + + // Storing the original QObject* is a bit of a hack as I couldn't figure out any + // way to cast directly between IPlugin* and IPluginDiagnose* + bf::at_key(m_Plugins).push_back(plugin); plugin->setProperty("filename", fileName); @@ -392,7 +452,7 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M IPluginModPage *modPage = qobject_cast(plugin); if (initPlugin(modPage, pluginProxy)) { bf::at_key(m_Plugins).push_back(modPage); - return true; + return modPage; } } { // game plugin @@ -402,7 +462,7 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M if (initPlugin(game, pluginProxy)) { bf::at_key(m_Plugins).push_back(game); registerGame(game); - return true; + return game; } } } @@ -410,7 +470,7 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M IPluginTool *tool = qobject_cast(plugin); if (initPlugin(tool, pluginProxy)) { bf::at_key(m_Plugins).push_back(tool); - return true; + return tool; } } { // installer plugins @@ -420,7 +480,7 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M if (m_Organizer) { m_Organizer->installationManager()->registerInstaller(installer); } - return true; + return installer; } } { // preview plugins @@ -428,7 +488,7 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M if (initPlugin(preview, pluginProxy)) { bf::at_key(m_Plugins).push_back(preview); m_PreviewGenerator.registerPlugin(preview); - return true; + return preview; } } { // proxy plugins @@ -439,12 +499,21 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M QCoreApplication::applicationDirPath() + "/" + ToQString(AppConfig::pluginPath())); for (const QString &pluginName : pluginNames) { try { - // we get a list of matching plugins as proxies don't necessarily have a good way of supporting multiple inheritance + // We get a list of matching plugins as proxies can return multiple plugins + // per file and do not have a good way of supporting multiple inheritance. QList matchingPlugins = proxy->instantiate(pluginName); + + // We are going to group plugin by names and "fix" them later: + std::map> proxiedByNames; + for (QObject *proxiedPlugin : matchingPlugins) { if (proxiedPlugin != nullptr) { - if (registerPlugin(proxiedPlugin, pluginName, proxy)) { - log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName()); + if (IPlugin* proxied = registerPlugin(proxiedPlugin, pluginName, proxy); proxied) { + log::debug("loaded plugin '{}' from '{}' - [{}]", + proxied->name(), QFileInfo(pluginName).fileName(), implementedInterfaces(proxied).join(", ")); + + // Store the plugin for later: + proxiedByNames[proxied->name()].push_back(proxied); } else { log::warn( @@ -454,11 +523,28 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M } } } + + // Fake masters: + for (auto& [name, proxiedPlugins] : proxiedByNames) { + if (proxiedPlugins.size() > 1) { + auto it = std::min_element(std::begin(proxiedPlugins), std::end(proxiedPlugins), + [&](auto const& lhs, auto const& rhs) { + return isBetterInterface(as_qobject(lhs), as_qobject(rhs)); + }); + + for (auto& proxiedPlugin : proxiedPlugins) { + if (proxiedPlugin != *it) { + m_Requirements.at(proxiedPlugin).setMaster(*it); + } + } + } + } + } catch (const std::exception &e) { reportError(QObject::tr("failed to initialize plugin %1: %2").arg(pluginName).arg(e.what())); } } - return true; + return proxy; } } @@ -467,13 +553,13 @@ bool PluginContainer::registerPlugin(QObject *plugin, const QString &fileName, M IPlugin *dummy = qobject_cast(plugin); if (initPlugin(dummy, pluginProxy)) { bf::at_key(m_Plugins).push_back(dummy); - return true; + return dummy; } } log::debug("no matching plugin interface"); - return false; + return nullptr; } struct clearPlugins @@ -533,6 +619,12 @@ bool PluginContainer::isEnabled(IPlugin* plugin) const return false; } + auto& requirements = m_Requirements.at(plugin); + + if (requirements.master()) { + return isEnabled(requirements.master()); + } + // Check the requirements: return m_Requirements.at(plugin).canEnable(); } @@ -721,8 +813,9 @@ void PluginContainer::loadPlugins() "failed to load plugin {}: {}", pluginName, pluginLoader->errorString()); } else { - if (registerPlugin(pluginLoader->instance(), pluginName, nullptr)) { - log::debug("loaded plugin \"{}\"", QFileInfo(pluginName).fileName()); + if (IPlugin* plugin = registerPlugin(pluginLoader->instance(), pluginName, nullptr); plugin) { + log::debug("loaded plugin '{}' from '{}' - [{}]", + plugin->name(), QFileInfo(pluginName).fileName(), implementedInterfaces(plugin).join(", ")); m_PluginLoaders.push_back(pluginLoader.release()); } else { m_FailedPlugins.push_back(pluginName); diff --git a/src/plugincontainer.h b/src/plugincontainer.h index b37b48cd..89ab5528 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -91,11 +91,15 @@ private: // constructed object before calling init(). void fetchRequirements(); + // Set the master for this plugin. This is required to "fake" masters for proxied plugins. + void setMaster(MOBase::IPlugin* master); + friend class PluginContainer; PluginContainer* m_PluginContainer; MOBase::IPlugin* m_Plugin; MOBase::IPluginProxy* m_PluginProxy; + MOBase::IPlugin* m_Master; std::vector> m_Requirements; MOBase::IOrganizer* m_Organizer; std::vector m_RequiredFor; @@ -107,6 +111,9 @@ private: }; +/** + * + */ class PluginContainer : public QObject, public MOBase::IPluginDiagnose { @@ -229,6 +236,10 @@ public: * @param t Name of the plugin to retrieve, or non-IPlugin interface. * * @return the corresponding plugin, or a null pointer. + * + * @note It is possible to have multiple plugins for the same name when + * dealing with proxied plugins (e.g. Python), in which case the + * most important one will be returned, as specified in PluginTypeOrder. */ MOBase::IPlugin* plugin(QString const& pluginName) const; MOBase::IPlugin* plugin(MOBase::IPluginDiagnose* diagnose) const; @@ -320,6 +331,17 @@ private: friend class PluginRequirements; + /** + * @brief Check if a plugin implements a "better" interface than another + * one, as specified by PluginTypeOrder. + * + * @param lhs, rhs The plugin to compare. + * + * @return true if the left plugin implements a better interface than the right + * one, false otherwise (or if both implements the same interface). + */ + bool isBetterInterface(QObject* lhs, QObject* rhs) const; + /** * @brief Find the QObject* corresponding to the given plugin. * @@ -341,7 +363,7 @@ private: void registerGame(MOBase::IPluginGame *game); - bool registerPlugin(QObject *pluginObj, const QString &fileName, MOBase::IPluginProxy *proxy); + MOBase::IPlugin* registerPlugin(QObject *pluginObj, const QString &fileName, MOBase::IPluginProxy *proxy); OrganizerCore *m_Organizer; diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 556cadcb..ba690d41 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -242,7 +242,6 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) for (auto& p : proxied) { pluginNames.append(p->localizedName()); } - pluginNames.removeDuplicates(); pluginNames.sort(); QString message = QObject::tr( "

    Disabling this plugin will prevent the following plugins from working:

      %1
    " @@ -264,7 +263,6 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) for (auto& p : requiredFor) { pluginNames.append(p->localizedName()); } - pluginNames.removeDuplicates(); pluginNames.sort(); QString message = QObject::tr( "

    Disabling this plugin will also disable the following plugins:

      %1

    Do you want to continue?

    ") -- cgit v1.3.1 From 56dcdd33a2110a8c5ad13cc9401ce87fb3d7bf80 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 12 Nov 2020 21:23:35 +0100 Subject: It's 2020. --- src/plugincontainer.cpp | 51 +++++++++++++------------------------------ src/plugincontainer.h | 14 ++++++++++++ src/settingsdialogplugins.cpp | 2 +- 3 files changed, 30 insertions(+), 37 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 94e4760c..d2f42fb4 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -253,6 +253,11 @@ QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const return {}; } + return implementedInterfaces(oPlugin); +} + +QStringList PluginContainer::implementedInterfaces(QObject * oPlugin) const +{ // Find all the names: QStringList names; boost::mp11::mp_for_each([oPlugin, &names](const auto* p) { @@ -275,26 +280,8 @@ QStringList PluginContainer::implementedInterfaces(IPlugin* plugin) const QString PluginContainer::topImplementedInterface(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 {}; - } - - // Find all the names: - QString name; - boost::mp11::mp_for_each([oPlugin, &name](auto* p) { - using plugin_type = std::decay_t; - if (name.isEmpty() && qobject_cast(oPlugin)) { - auto tname = PluginTypeName::value(); - if (!tname.isEmpty()) { - name = tname; - } - } - }); - - return name; + auto interfaces = implementedInterfaces(plugin); + return interfaces.isEmpty() ? "" : interfaces[0]; } bool PluginContainer::isBetterInterface(QObject* lhs, QObject* rhs) const @@ -408,7 +395,7 @@ IPlugin* PluginContainer::registerPlugin(QObject *plugin, const QString &fileNam && as_qobject(other)->property("filename") == fileName) { if (isBetterInterface(plugin, as_qobject(other))) { log::debug("replacing plugin '{}' with interfaces [{}] by one with interfaces [{}]", - pluginObj->name(), implementedInterfaces(other).join(", "), implementedInterfaces(pluginObj).join(", ")); + pluginObj->name(), implementedInterfaces(other).join(", "), implementedInterfaces(plugin).join(", ")); bf::at_key(m_AccessPlugins)[pluginObj->name()] = pluginObj; } } @@ -562,15 +549,6 @@ IPlugin* PluginContainer::registerPlugin(QObject *plugin, const QString &fileNam return nullptr; } -struct clearPlugins -{ - template - void operator()(T& t) const - { - t.second.clear(); - } -}; - void PluginContainer::unloadPlugins() { if (m_UserInterface != nullptr) { @@ -582,7 +560,7 @@ void PluginContainer::unloadPlugins() m_Organizer->disconnectPlugins(); } - bf::for_each(m_Plugins, clearPlugins()); + bf::for_each(m_Plugins, [](auto& t) { t.second.clear(); }); for (const boost::signals2::connection &connection : m_DiagnosisConnections) { connection.disconnect(); @@ -614,17 +592,18 @@ bool PluginContainer::isEnabled(IPlugin* plugin) const return plugin == m_Organizer->managedGame(); } - // Check if the plugin is enabled: - if (!m_Organizer->persistent(plugin->name(), "enabled", true).toBool()) { - return false; - } - + // Check the master, if any: auto& requirements = m_Requirements.at(plugin); if (requirements.master()) { return isEnabled(requirements.master()); } + // Check if the plugin is enabled: + if (!m_Organizer->persistent(plugin->name(), "enabled", true).toBool()) { + return false; + } + // Check the requirements: return m_Requirements.at(plugin).canEnable(); } diff --git a/src/plugincontainer.h b/src/plugincontainer.h index 89ab5528..ac40e413 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -331,6 +331,20 @@ private: friend class PluginRequirements; + + /** + * @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. + * + * @note This function can be used to get implemented interfaces before registering + * a plugin. + */ + QStringList implementedInterfaces(QObject* plugin) const; + /** * @brief Check if a plugin implements a "better" interface than another * one, as specified by PluginTypeOrder. diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index ba690d41..f019cd2f 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -31,7 +31,7 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta // display plugin settings QSet handledNames; for (IPlugin* plugin : settings().plugins().plugins()) { - if (handledNames.contains(plugin->name()) || !plugin->master().isEmpty()) { + if (handledNames.contains(plugin->name()) || m_pluginContainer->requirements(plugin).master()) { continue; } -- cgit v1.3.1 From 037ca381a4c39490998c7642537dd54cc4f4aeee Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 10:27:52 +0100 Subject: Fix filter for child plugins. --- src/settingsdialogplugins.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index f019cd2f..19515426 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -128,7 +128,7 @@ void PluginsSettingsTab::filterPluginList() return regex.match(plugin->localizedName()).hasMatch(); }); for (auto* child : m_pluginContainer->requirements(plugin).children()) { - m_filter.matches([child](const QRegularExpression& regex) { + match = match || m_filter.matches([child](const QRegularExpression& regex) { return regex.match(child->localizedName()).hasMatch(); }); } -- cgit v1.3.1 From e0b73a7d7d27d42b87b3061c51d5d22f57646c45 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 10:45:29 +0100 Subject: Fix selection in Plugins settings. --- src/settingsdialog.ui | 130 +++++++++++++++++++++++------------------- src/settingsdialogplugins.cpp | 22 +++++-- 2 files changed, 87 insertions(+), 65 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/settingsdialog.ui b/src/settingsdialog.ui index 4fd34b21..d05910cb 100644 --- a/src/settingsdialog.ui +++ b/src/settingsdialog.ui @@ -1111,8 +1111,8 @@ - - + + 0 @@ -1126,63 +1126,65 @@ 0 - - - 6 - - - - - Author: - - - - - - - - - - - - - - Version: - - - - - - - - - - - - - - - - - true - - - - - - - Description: - - - - - - - Enabled - - - - + + + + 6 + + + + + Author: + + + + + + + + + + + + + + Version: + + + + + + + + + + + + + + Description: + + + + + + + + + + true + + + + + + + Enabled + + + + + @@ -1216,6 +1218,16 @@ + + + + No plugin found. + + + Qt::AlignCenter + + + diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 19515426..a86ba5c9 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -25,6 +25,7 @@ PluginsSettingsTab::PluginsSettingsTab(Settings& s, PluginContainer* pluginConta item->setFont(0, font); topItems[interfaceName] = item; item->setExpanded(true); + item->setFlags(item->flags() & ~Qt::ItemIsSelectable); } ui->pluginsList->setHeaderHidden(true); @@ -112,6 +113,7 @@ void PluginsSettingsTab::updateListItems() void PluginsSettingsTab::filterPluginList() { + auto selectedItems = ui->pluginsList->selectedItems(); QTreeWidgetItem* firstNotHidden = nullptr; for (auto i = 0; i < ui->pluginsList->topLevelItemCount(); ++i) { @@ -150,13 +152,21 @@ void PluginsSettingsTab::filterPluginList() } // Unselect item if hidden: - auto selectedItems = ui->pluginsList->selectedItems(); - if (!selectedItems.isEmpty() && selectedItems[0]->isHidden()) { - selectedItems[0]->setSelected(false); - - if (firstNotHidden) { - firstNotHidden->setSelected(true); + if (firstNotHidden) { + ui->pluginDescription->setVisible(true); + ui->pluginSettingsList->setVisible(true); + ui->noPluginLabel->setVisible(false); + if (selectedItems.isEmpty()) { + ui->pluginsList->setCurrentItem(firstNotHidden); } + else if (selectedItems[0]->isHidden()) { + ui->pluginsList->setCurrentItem(firstNotHidden); + } + } + else { + ui->pluginDescription->setVisible(false); + ui->pluginSettingsList->setVisible(false); + ui->noPluginLabel->setVisible(true); } } -- cgit v1.3.1 From 1e67c3e68dbe4141c9cd3bfd7b29c747fca03753 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 12:30:59 +0100 Subject: Add core plugins and better messages. --- src/plugincontainer.cpp | 20 +++++++++++++++++++- src/plugincontainer.h | 9 +++++++++ src/settingsdialogplugins.cpp | 13 +++++++------ 3 files changed, 35 insertions(+), 7 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/plugincontainer.cpp b/src/plugincontainer.cpp index 8c2ece76..7884498c 100644 --- a/src/plugincontainer.cpp +++ b/src/plugincontainer.cpp @@ -101,6 +101,10 @@ QStringList PluginContainer::pluginInterfaces() // PluginRequirementProxy +const std::set PluginRequirements::s_CorePlugins{ + "INI Bakery" +}; + PluginRequirements::PluginRequirements( PluginContainer* pluginContainer, MOBase::IPlugin* plugin, IOrganizer* proxy, MOBase::IPluginProxy* pluginProxy) @@ -193,6 +197,16 @@ bool PluginRequirements::canEnable() const return problems().empty(); } +bool PluginRequirements::isCorePlugin() const +{ + // Let's consider game plugins as "core": + if (m_PluginContainer->implementInterface(m_Plugin)) { + return true; + } + + return s_CorePlugins.contains(m_Plugin->name()); +} + bool PluginRequirements::hasRequirements() const { return !m_Requirements.empty(); @@ -405,7 +419,11 @@ bool PluginContainer::initPlugin(IPlugin *plugin, IPluginProxy *pluginProxy, boo auto [it, bl] = m_Requirements.emplace(plugin, PluginRequirements(this, plugin, proxy, pluginProxy)); - if (!skipInit && !plugin->init(proxy)) { + if (skipInit) { + return true; + } + + if (!plugin->init(proxy)) { log::warn("plugin failed to initialize"); return false; } diff --git a/src/plugincontainer.h b/src/plugincontainer.h index e9f0f453..36edfad9 100644 --- a/src/plugincontainer.h +++ b/src/plugincontainer.h @@ -39,6 +39,12 @@ public: */ bool canEnable() const; + /** + * @return true if this is a core plugin, i.e. a plugin that should not be + * manually enabled or disabled by the user. + */ + bool isCorePlugin() const; + /** * @return true if this plugin has requirements (satisfied or not). */ @@ -83,6 +89,9 @@ public: private: + // The list of "Core" plugins. + static const std::set s_CorePlugins; + // Accumulator version for requiredFor() to avoid infinite recursion. void requiredFor(std::vector& required, std::set& visited) const; diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index a86ba5c9..0c860fa0 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -241,7 +241,8 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) if (m_pluginContainer->requirements(game).proxy() == plugin) { QMessageBox::warning( parentWidget(), QObject::tr("Cannot disable plugin"), - QObject::tr("This plugin is used by the current game plugin and cannot disabled."), QMessageBox::Ok); + QObject::tr("The '%1' plugin is used by the current game plugin and cannot disabled.") + .arg(plugin->localizedName()), QMessageBox::Ok); ui->enabledCheckbox->setChecked(true); return; } @@ -254,9 +255,9 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) } pluginNames.sort(); QString message = QObject::tr( - "

    Disabling this plugin will prevent the following plugins from working:

      %1
    " + "

    Disabling the '%1' plugin will prevent the following plugins from working:

      %1
    " "

    Do you want to continue? You will need to restart ModOrganizer2 for the change to take effect.

    ") - .arg("
  • " + pluginNames.join("
  • ") + "
  • "); + .arg(plugin->localizedName()).arg("
  • " + pluginNames.join("
  • ") + "
  • "); if (QMessageBox::critical( parentWidget(), QObject::tr("Really disable plugin?"), message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { @@ -275,8 +276,8 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) } pluginNames.sort(); QString message = QObject::tr( - "

    Disabling this plugin will also disable the following plugins:

      %1

    Do you want to continue?

    ") - .arg("
  • " + pluginNames.join("
  • ") + "
  • "); + "

    Disabling the '%1' plugin will also disable the following plugins:

      %1

    Do you want to continue?

    ") + .arg(plugin->localizedName()).arg("
  • " + pluginNames.join("
  • ") + "
  • "); if (QMessageBox::warning( parentWidget(), QObject::tr("Really disable plugin?"), message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { @@ -310,7 +311,7 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->descriptionLabel->setText(plugin->description()); ui->enabledCheckbox->setVisible( - !m_pluginContainer->implementInterface(plugin) + !m_pluginContainer->requirements(plugin).isCorePlugin() && plugin->master().isEmpty()); ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); -- cgit v1.3.1 From e9a4f91fac08087ef83ce5b7716abeda90b6bde3 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 13:24:57 +0100 Subject: Display inactive checkbox for core plugins (not for games). --- src/settingsdialogplugins.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 0c860fa0..eb0035b2 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -310,9 +310,17 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->versionLabel->setText(plugin->version().canonicalString()); ui->descriptionLabel->setText(plugin->description()); + ui->enabledCheckbox->setDisabled(false); + ui->enabledCheckbox->setToolTip(""); ui->enabledCheckbox->setVisible( - !m_pluginContainer->requirements(plugin).isCorePlugin() + !m_pluginContainer->implementInterface(plugin) && plugin->master().isEmpty()); + if (m_pluginContainer->requirements(plugin).isCorePlugin()) { + ui->enabledCheckbox->setDisabled(true); + ui->enabledCheckbox->setToolTip( + QObject::tr("This plugin is required for Mod Organizer to work properly and cannot be disabled.")); + } + ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); QVariantMap settings = current->data(0, ROLE_SETTINGS).toMap(); -- cgit v1.3.1 From 8886674379c507fe46bfc6cb2ef013537567441b Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 14:28:43 +0100 Subject: Better dialog for warning when disabling proxy plugin. --- src/CMakeLists.txt | 1 + src/disableproxyplugindialog.cpp | 30 +++++++ src/disableproxyplugindialog.h | 43 ++++++++++ src/disableproxyplugindialog.ui | 174 +++++++++++++++++++++++++++++++++++++++ src/settingsdialogplugins.cpp | 28 +++---- 5 files changed, 258 insertions(+), 18 deletions(-) create mode 100644 src/disableproxyplugindialog.cpp create mode 100644 src/disableproxyplugindialog.h create mode 100644 src/disableproxyplugindialog.ui (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb4b151f..a53d845d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -186,6 +186,7 @@ add_filter(NAME src/settingsdialog GROUPS settingsdialogplugins settingsdialogsteam settingsdialogworkarounds + disableproxyplugindialog ) add_filter(NAME src/utilities GROUPS diff --git a/src/disableproxyplugindialog.cpp b/src/disableproxyplugindialog.cpp new file mode 100644 index 00000000..f392113b --- /dev/null +++ b/src/disableproxyplugindialog.cpp @@ -0,0 +1,30 @@ +#include "disableproxyplugindialog.h" + +#include "ui_proxyplugindialog.h" + +using namespace MOBase; + +DisableProxyPluginDialog::DisableProxyPluginDialog( + MOBase::IPlugin* proxyPlugin, std::vector const& required, QWidget* parent) + : QDialog(parent), ui(new Ui::DisableProxyPluginDialog) +{ + ui->setupUi(this); + + ui->topLabel->setText(QObject::tr( + "Disabling the '%1' plugin will prevent the following %2 plugin(s) from working:", "", required.size()) + .arg(proxyPlugin->localizedName()) + .arg(required.size())); + + connect(ui->noBtn, &QPushButton::clicked, this, &QDialog::reject); + connect(ui->yesBtn, &QPushButton::clicked, this, &QDialog::accept); + + ui->requiredPlugins->setSelectionMode(QAbstractItemView::NoSelection); + ui->requiredPlugins->setRowCount(required.size()); + for (int i = 0; i < required.size(); ++i) { + ui->requiredPlugins->setItem(i, 0, new QTableWidgetItem(required[i]->localizedName())); + ui->requiredPlugins->setItem(i, 1, new QTableWidgetItem(required[i]->description())); + ui->requiredPlugins->setRowHeight(i, 9); + } + ui->requiredPlugins->verticalHeader()->setVisible(false); + ui->requiredPlugins->sortByColumn(0, Qt::AscendingOrder); +} diff --git a/src/disableproxyplugindialog.h b/src/disableproxyplugindialog.h new file mode 100644 index 00000000..52f552e1 --- /dev/null +++ b/src/disableproxyplugindialog.h @@ -0,0 +1,43 @@ +/* +Copyright (C) 2020 Mikaël Capelle. All rights reserved. + +This file is part of Mod Organizer. + +Mod Organizer is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Mod Organizer is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Mod Organizer. If not, see . +*/ + +#ifndef DISABLEPROXYPLUGINDIALOG_H +#define DISABLEPROXYPLUGINDIALOG_H + +#include + +#include "ipluginproxy.h" + +namespace Ui { class DisableProxyPluginDialog; } + +class DisableProxyPluginDialog : public QDialog { +public: + + DisableProxyPluginDialog( + MOBase::IPlugin* proxyPlugin, + std::vector const& required, + QWidget* parent = nullptr); + +private slots: + + Ui::DisableProxyPluginDialog* ui; + +}; + +#endif diff --git a/src/disableproxyplugindialog.ui b/src/disableproxyplugindialog.ui new file mode 100644 index 00000000..9f068787 --- /dev/null +++ b/src/disableproxyplugindialog.ui @@ -0,0 +1,174 @@ + + + DisableProxyPluginDialog + + + + 0 + 0 + 522 + 417 + + + + Really disable plugin? + + + + + + + 0 + 0 + + + + + + + + 0 + 0 + + + + + + + Qt::PlainText + + + :/MO/gui/remove + + + Qt::AlignCenter + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 10 + 20 + + + + + + + + Disabling the '%1' plugin will prevent the following plugins from working: + + + + + + + + + + 2 + + + true + + + + Plugin + + + + + Description + + + + + + + + Do you want to continue? You will need to restart Mod Organizer for the change to take effect. + + + + + + + + 0 + 0 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 80 + 0 + + + + Yes + + + + :/MO/gui/remove:/MO/gui/remove + + + + + + + + 0 + 0 + + + + + 80 + 0 + + + + No + + + true + + + false + + + + + + + + + + + + + diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index eb0035b2..4313b85e 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -3,6 +3,7 @@ #include "noeditdelegate.h" #include +#include "disableproxyplugindialog.h" #include "organizercore.h" #include "plugincontainer.h" @@ -242,29 +243,20 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) QMessageBox::warning( parentWidget(), QObject::tr("Cannot disable plugin"), QObject::tr("The '%1' plugin is used by the current game plugin and cannot disabled.") - .arg(plugin->localizedName()), QMessageBox::Ok); - ui->enabledCheckbox->setChecked(true); - return; + .arg(plugin->localizedName()), QMessageBox::Ok); + ui->enabledCheckbox->setChecked(true); + return; } // Check the proxied plugins: auto proxied = requirements.proxied(); - QStringList pluginNames; - for (auto& p : proxied) { - pluginNames.append(p->localizedName()); - } - pluginNames.sort(); - QString message = QObject::tr( - "

    Disabling the '%1' plugin will prevent the following plugins from working:

      %1
    " - "

    Do you want to continue? You will need to restart ModOrganizer2 for the change to take effect.

    ") - .arg(plugin->localizedName()).arg("
  • " + pluginNames.join("
  • ") + "
  • "); - if (QMessageBox::critical( - parentWidget(), QObject::tr("Really disable plugin?"), message, - QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { - ui->enabledCheckbox->setChecked(true); - return; + if (!proxied.empty()) { + DisableProxyPluginDialog dialog(plugin, proxied, parentWidget()); + if (dialog.exec() != QDialog::Accepted) { + ui->enabledCheckbox->setChecked(true); + return; + } } - } // Check if the plugins is required for other plugins: -- cgit v1.3.1 From 1d6d01dec887126b70d1de4bbf562d3803072f68 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Tue, 17 Nov 2020 18:24:10 +0100 Subject: Disable checkbox when plugin cannot be enabled. --- src/disableproxyplugindialog.cpp | 2 +- src/settingsdialogplugins.cpp | 46 ++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 19 deletions(-) (limited to 'src/settingsdialogplugins.cpp') diff --git a/src/disableproxyplugindialog.cpp b/src/disableproxyplugindialog.cpp index f392113b..c05e30e3 100644 --- a/src/disableproxyplugindialog.cpp +++ b/src/disableproxyplugindialog.cpp @@ -1,6 +1,6 @@ #include "disableproxyplugindialog.h" -#include "ui_proxyplugindialog.h" +#include "ui_disableproxyplugindialog.h" using namespace MOBase; diff --git a/src/settingsdialogplugins.cpp b/src/settingsdialogplugins.cpp index 4313b85e..16f6fb0a 100644 --- a/src/settingsdialogplugins.cpp +++ b/src/settingsdialogplugins.cpp @@ -217,20 +217,6 @@ void PluginsSettingsTab::on_checkboxEnabled_clicked(bool checked) // User wants to enable: if (checked) { - auto problems = requirements.problems(); - if (!problems.empty()) { - QStringList descriptions; - for (auto& problem : problems) { - descriptions.append(problem.shortDescription()); - } - QMessageBox::warning( - parentWidget(), QObject::tr("Cannot enable plugin"), - QObject::tr("

    This plugin cannot be enabled:

      %1
    ") - .arg("
  • " + descriptions.join("
  • ") + "
  • "), QMessageBox::Ok); - ui->enabledCheckbox->setChecked(false); - return; - } - m_pluginContainer->setEnabled(plugin, true, false); } else { @@ -302,18 +288,42 @@ void PluginsSettingsTab::on_pluginsList_currentItemChanged(QTreeWidgetItem *curr ui->versionLabel->setText(plugin->version().canonicalString()); ui->descriptionLabel->setText(plugin->description()); - ui->enabledCheckbox->setDisabled(false); - ui->enabledCheckbox->setToolTip(""); + // Checkbox, do not show for children or game plugins, disable + // if the plugin cannot be enabled. ui->enabledCheckbox->setVisible( !m_pluginContainer->implementInterface(plugin) && plugin->master().isEmpty()); + + bool enabled = m_pluginContainer->isEnabled(plugin); + auto& requirements = m_pluginContainer->requirements(plugin); + auto problems = requirements.problems(); + if (m_pluginContainer->requirements(plugin).isCorePlugin()) { ui->enabledCheckbox->setDisabled(true); ui->enabledCheckbox->setToolTip( QObject::tr("This plugin is required for Mod Organizer to work properly and cannot be disabled.")); } - - ui->enabledCheckbox->setChecked(m_pluginContainer->isEnabled(plugin)); + // Plugin is enable or can be enabled. + else if (enabled || problems.empty()) { + ui->enabledCheckbox->setDisabled(false); + ui->enabledCheckbox->setToolTip(""); + ui->enabledCheckbox->setChecked(enabled); + } + // Plugin is disable and cannot be enabled. + else { + ui->enabledCheckbox->setDisabled(true); + ui->enabledCheckbox->setChecked(false); + if (problems.size() == 1) { + ui->enabledCheckbox->setToolTip(problems[0].shortDescription()); + } + else { + QStringList descriptions; + for (auto& problem : problems) { + descriptions.append(problem.shortDescription()); + } + ui->enabledCheckbox->setToolTip("
    • " + descriptions.join("
    • ") + "
    "); + } + } QVariantMap settings = current->data(0, ROLE_SETTINGS).toMap(); QVariantMap descriptions = current->data(0, ROLE_DESCRIPTIONS).toMap(); -- cgit v1.3.1