From 279bce2014079c39209829be3e1690b621ff91a1 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 5 Feb 2018 11:43:29 +0300 Subject: [PATCH] Make SearchManager singleton --- src/base/search/searchpluginmanager.cpp | 20 +++++++++++++--- src/base/search/searchpluginmanager.h | 3 +++ src/gui/search/searchwidget.cpp | 31 +++++++++++++------------ src/gui/search/searchwidget.h | 2 -- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index f02870869..9849a2164 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include "base/logger.h" @@ -47,12 +48,17 @@ #include "searchdownloadhandler.h" #include "searchhandler.h" -static inline void removePythonScriptIfExists(const QString &scriptPath) +namespace { - Utils::Fs::forceRemove(scriptPath); - Utils::Fs::forceRemove(scriptPath + "c"); + inline void removePythonScriptIfExists(const QString &scriptPath) + { + Utils::Fs::forceRemove(scriptPath); + Utils::Fs::forceRemove(scriptPath + "c"); + } } +QPointer SearchPluginManager::m_instance = nullptr; + const QHash SearchPluginManager::m_categoryNames { {"all", QT_TRANSLATE_NOOP("SearchEngine", "All categories")}, {"movies", QT_TRANSLATE_NOOP("SearchEngine", "Movies")}, @@ -68,6 +74,9 @@ const QHash SearchPluginManager::m_categoryNames { SearchPluginManager::SearchPluginManager() : m_updateUrl(QString("http://searchplugins.qbittorrent.org/%1/engines/").arg(Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova")) { + Q_ASSERT(!m_instance); // only one instance is allowed + m_instance = this; + updateNova(); update(); } @@ -77,6 +86,11 @@ SearchPluginManager::~SearchPluginManager() qDeleteAll(m_plugins); } +SearchPluginManager *SearchPluginManager::instance() +{ + return m_instance; +} + QStringList SearchPluginManager::allPlugins() const { return m_plugins.keys(); diff --git a/src/base/search/searchpluginmanager.h b/src/base/search/searchpluginmanager.h index 2e815aa6e..e3723c540 100644 --- a/src/base/search/searchpluginmanager.h +++ b/src/base/search/searchpluginmanager.h @@ -61,6 +61,8 @@ public: SearchPluginManager(); ~SearchPluginManager() override; + static SearchPluginManager *instance(); + QStringList allPlugins() const; QStringList enabledPlugins() const; QStringList supportedCategories() const; @@ -107,6 +109,7 @@ private: static QString pluginPath(const QString &name); + static QPointer m_instance; static const QHash m_categoryNames; const QString m_updateUrl; diff --git a/src/gui/search/searchwidget.cpp b/src/gui/search/searchwidget.cpp index cae9f7c17..26995f171 100644 --- a/src/gui/search/searchwidget.cpp +++ b/src/gui/search/searchwidget.cpp @@ -135,17 +135,18 @@ SearchWidget::SearchWidget(MainWindow *mainWindow) connect(m_tabStatusChangedMapper, static_cast(&QSignalMapper::mapped) , this, &SearchWidget::tabStatusChanged); - m_searchManager = new SearchPluginManager; + // NOTE: Although SearchManager is Application-wide component now, we still create it the legacy way. + auto *searchManager = new SearchPluginManager; const auto onPluginChanged = [this]() { fillCatCombobox(); fillPluginComboBox(); selectActivePage(); }; - connect(m_searchManager, &SearchPluginManager::pluginInstalled, this, onPluginChanged); - connect(m_searchManager, &SearchPluginManager::pluginUninstalled, this, onPluginChanged); - connect(m_searchManager, &SearchPluginManager::pluginUpdated, this, onPluginChanged); - connect(m_searchManager, &SearchPluginManager::pluginEnabled, this, onPluginChanged); + connect(searchManager, &SearchPluginManager::pluginInstalled, this, onPluginChanged); + connect(searchManager, &SearchPluginManager::pluginUninstalled, this, onPluginChanged); + connect(searchManager, &SearchPluginManager::pluginUpdated, this, onPluginChanged); + connect(searchManager, &SearchPluginManager::pluginEnabled, this, onPluginChanged); // Fill in category combobox onPluginChanged(); @@ -163,7 +164,7 @@ void SearchWidget::fillCatCombobox() using QStrPair = QPair; QList tmpList; - foreach (const QString &cat, m_searchManager->supportedCategories()) + foreach (const QString &cat, SearchPluginManager::instance()->supportedCategories()) tmpList << qMakePair(SearchPluginManager::categoryFullName(cat), cat); std::sort(tmpList.begin(), tmpList.end(), [](const QStrPair &l, const QStrPair &r) { return (QString::localeAwareCompare(l.first, r.first) < 0); }); @@ -185,8 +186,8 @@ void SearchWidget::fillPluginComboBox() using QStrPair = QPair; QList tmpList; - foreach (const QString &name, m_searchManager->enabledPlugins()) - tmpList << qMakePair(m_searchManager->pluginFullName(name), name); + foreach (const QString &name, SearchPluginManager::instance()->enabledPlugins()) + tmpList << qMakePair(SearchPluginManager::instance()->pluginFullName(name), name); std::sort(tmpList.begin(), tmpList.end(), [](const QStrPair &l, const QStrPair &r) { return (l.first < r.first); } ); foreach (const QStrPair &p, tmpList) @@ -208,7 +209,7 @@ QString SearchWidget::selectedPlugin() const void SearchWidget::selectActivePage() { - if (m_searchManager->allPlugins().isEmpty()) { + if (SearchPluginManager::instance()->allPlugins().isEmpty()) { m_ui->stackedPages->setCurrentWidget(m_ui->emptyPage); m_ui->m_searchPattern->setEnabled(false); m_ui->comboCategory->setEnabled(false); @@ -227,7 +228,7 @@ void SearchWidget::selectActivePage() SearchWidget::~SearchWidget() { qDebug("Search destruction"); - delete m_searchManager; + delete SearchPluginManager::instance(); delete m_ui; } @@ -262,7 +263,7 @@ void SearchWidget::selectMultipleBox(int index) void SearchWidget::on_pluginsButton_clicked() { - new PluginSelectDlg(m_searchManager, this); + new PluginSelectDlg(SearchPluginManager::instance(), this); } void SearchWidget::searchTextEdited(QString) @@ -303,15 +304,15 @@ void SearchWidget::on_searchButton_clicked() } QStringList plugins; - if (selectedPlugin() == "all") plugins = m_searchManager->allPlugins(); - else if (selectedPlugin() == "enabled") plugins = m_searchManager->enabledPlugins(); - else if (selectedPlugin() == "multi") plugins = m_searchManager->enabledPlugins(); + if (selectedPlugin() == "all") plugins = SearchPluginManager::instance()->allPlugins(); + else if (selectedPlugin() == "enabled") plugins = SearchPluginManager::instance()->enabledPlugins(); + else if (selectedPlugin() == "multi") plugins = SearchPluginManager::instance()->enabledPlugins(); else plugins << selectedPlugin(); qDebug("Search with category: %s", qUtf8Printable(selectedCategory())); // Launch search - auto *searchHandler = m_searchManager->startSearch(pattern, selectedCategory(), plugins); + auto *searchHandler = SearchPluginManager::instance()->startSearch(pattern, selectedCategory(), plugins); // Tab Addition auto *newTab = new SearchTab(searchHandler, this); diff --git a/src/gui/search/searchwidget.h b/src/gui/search/searchwidget.h index b4537fdae..319d9093d 100644 --- a/src/gui/search/searchwidget.h +++ b/src/gui/search/searchwidget.h @@ -37,7 +37,6 @@ class QSignalMapper; class QTabWidget; class MainWindow; -class SearchPluginManager; class SearchTab; namespace Ui @@ -80,7 +79,6 @@ private: QString selectedPlugin() const; Ui::SearchWidget *m_ui; - SearchPluginManager *m_searchManager; QSignalMapper *m_tabStatusChangedMapper; QPointer m_currentSearchTab; // Selected tab QPointer m_activeSearchTab; // Tab with running search