Browse Source

Make SearchManager singleton

adaptive-webui-19844
Vladimir Golovnev (Glassez) 7 years ago
parent
commit
279bce2014
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 16
      src/base/search/searchpluginmanager.cpp
  2. 3
      src/base/search/searchpluginmanager.h
  3. 31
      src/gui/search/searchwidget.cpp
  4. 2
      src/gui/search/searchwidget.h

16
src/base/search/searchpluginmanager.cpp

@ -35,6 +35,7 @@
#include <QDomElement> #include <QDomElement>
#include <QDomNode> #include <QDomNode>
#include <QList> #include <QList>
#include <QPointer>
#include <QProcess> #include <QProcess>
#include "base/logger.h" #include "base/logger.h"
@ -47,11 +48,16 @@
#include "searchdownloadhandler.h" #include "searchdownloadhandler.h"
#include "searchhandler.h" #include "searchhandler.h"
static inline void removePythonScriptIfExists(const QString &scriptPath) namespace
{
inline void removePythonScriptIfExists(const QString &scriptPath)
{ {
Utils::Fs::forceRemove(scriptPath); Utils::Fs::forceRemove(scriptPath);
Utils::Fs::forceRemove(scriptPath + "c"); Utils::Fs::forceRemove(scriptPath + "c");
} }
}
QPointer<SearchPluginManager> SearchPluginManager::m_instance = nullptr;
const QHash<QString, QString> SearchPluginManager::m_categoryNames { const QHash<QString, QString> SearchPluginManager::m_categoryNames {
{"all", QT_TRANSLATE_NOOP("SearchEngine", "All categories")}, {"all", QT_TRANSLATE_NOOP("SearchEngine", "All categories")},
@ -68,6 +74,9 @@ const QHash<QString, QString> SearchPluginManager::m_categoryNames {
SearchPluginManager::SearchPluginManager() SearchPluginManager::SearchPluginManager()
: m_updateUrl(QString("http://searchplugins.qbittorrent.org/%1/engines/").arg(Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova")) : 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(); updateNova();
update(); update();
} }
@ -77,6 +86,11 @@ SearchPluginManager::~SearchPluginManager()
qDeleteAll(m_plugins); qDeleteAll(m_plugins);
} }
SearchPluginManager *SearchPluginManager::instance()
{
return m_instance;
}
QStringList SearchPluginManager::allPlugins() const QStringList SearchPluginManager::allPlugins() const
{ {
return m_plugins.keys(); return m_plugins.keys();

3
src/base/search/searchpluginmanager.h

@ -61,6 +61,8 @@ public:
SearchPluginManager(); SearchPluginManager();
~SearchPluginManager() override; ~SearchPluginManager() override;
static SearchPluginManager *instance();
QStringList allPlugins() const; QStringList allPlugins() const;
QStringList enabledPlugins() const; QStringList enabledPlugins() const;
QStringList supportedCategories() const; QStringList supportedCategories() const;
@ -107,6 +109,7 @@ private:
static QString pluginPath(const QString &name); static QString pluginPath(const QString &name);
static QPointer<SearchPluginManager> m_instance;
static const QHash<QString, QString> m_categoryNames; static const QHash<QString, QString> m_categoryNames;
const QString m_updateUrl; const QString m_updateUrl;

31
src/gui/search/searchwidget.cpp

@ -135,17 +135,18 @@ SearchWidget::SearchWidget(MainWindow *mainWindow)
connect(m_tabStatusChangedMapper, static_cast<void (QSignalMapper::*)(QWidget *)>(&QSignalMapper::mapped) connect(m_tabStatusChangedMapper, static_cast<void (QSignalMapper::*)(QWidget *)>(&QSignalMapper::mapped)
, this, &SearchWidget::tabStatusChanged); , 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]() const auto onPluginChanged = [this]()
{ {
fillCatCombobox(); fillCatCombobox();
fillPluginComboBox(); fillPluginComboBox();
selectActivePage(); selectActivePage();
}; };
connect(m_searchManager, &SearchPluginManager::pluginInstalled, this, onPluginChanged); connect(searchManager, &SearchPluginManager::pluginInstalled, this, onPluginChanged);
connect(m_searchManager, &SearchPluginManager::pluginUninstalled, this, onPluginChanged); connect(searchManager, &SearchPluginManager::pluginUninstalled, this, onPluginChanged);
connect(m_searchManager, &SearchPluginManager::pluginUpdated, this, onPluginChanged); connect(searchManager, &SearchPluginManager::pluginUpdated, this, onPluginChanged);
connect(m_searchManager, &SearchPluginManager::pluginEnabled, this, onPluginChanged); connect(searchManager, &SearchPluginManager::pluginEnabled, this, onPluginChanged);
// Fill in category combobox // Fill in category combobox
onPluginChanged(); onPluginChanged();
@ -163,7 +164,7 @@ void SearchWidget::fillCatCombobox()
using QStrPair = QPair<QString, QString>; using QStrPair = QPair<QString, QString>;
QList<QStrPair> tmpList; QList<QStrPair> tmpList;
foreach (const QString &cat, m_searchManager->supportedCategories()) foreach (const QString &cat, SearchPluginManager::instance()->supportedCategories())
tmpList << qMakePair(SearchPluginManager::categoryFullName(cat), cat); 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); }); 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<QString, QString>; using QStrPair = QPair<QString, QString>;
QList<QStrPair> tmpList; QList<QStrPair> tmpList;
foreach (const QString &name, m_searchManager->enabledPlugins()) foreach (const QString &name, SearchPluginManager::instance()->enabledPlugins())
tmpList << qMakePair(m_searchManager->pluginFullName(name), name); tmpList << qMakePair(SearchPluginManager::instance()->pluginFullName(name), name);
std::sort(tmpList.begin(), tmpList.end(), [](const QStrPair &l, const QStrPair &r) { return (l.first < r.first); } ); std::sort(tmpList.begin(), tmpList.end(), [](const QStrPair &l, const QStrPair &r) { return (l.first < r.first); } );
foreach (const QStrPair &p, tmpList) foreach (const QStrPair &p, tmpList)
@ -208,7 +209,7 @@ QString SearchWidget::selectedPlugin() const
void SearchWidget::selectActivePage() void SearchWidget::selectActivePage()
{ {
if (m_searchManager->allPlugins().isEmpty()) { if (SearchPluginManager::instance()->allPlugins().isEmpty()) {
m_ui->stackedPages->setCurrentWidget(m_ui->emptyPage); m_ui->stackedPages->setCurrentWidget(m_ui->emptyPage);
m_ui->m_searchPattern->setEnabled(false); m_ui->m_searchPattern->setEnabled(false);
m_ui->comboCategory->setEnabled(false); m_ui->comboCategory->setEnabled(false);
@ -227,7 +228,7 @@ void SearchWidget::selectActivePage()
SearchWidget::~SearchWidget() SearchWidget::~SearchWidget()
{ {
qDebug("Search destruction"); qDebug("Search destruction");
delete m_searchManager; delete SearchPluginManager::instance();
delete m_ui; delete m_ui;
} }
@ -262,7 +263,7 @@ void SearchWidget::selectMultipleBox(int index)
void SearchWidget::on_pluginsButton_clicked() void SearchWidget::on_pluginsButton_clicked()
{ {
new PluginSelectDlg(m_searchManager, this); new PluginSelectDlg(SearchPluginManager::instance(), this);
} }
void SearchWidget::searchTextEdited(QString) void SearchWidget::searchTextEdited(QString)
@ -303,15 +304,15 @@ void SearchWidget::on_searchButton_clicked()
} }
QStringList plugins; QStringList plugins;
if (selectedPlugin() == "all") plugins = m_searchManager->allPlugins(); if (selectedPlugin() == "all") plugins = SearchPluginManager::instance()->allPlugins();
else if (selectedPlugin() == "enabled") plugins = m_searchManager->enabledPlugins(); else if (selectedPlugin() == "enabled") plugins = SearchPluginManager::instance()->enabledPlugins();
else if (selectedPlugin() == "multi") plugins = m_searchManager->enabledPlugins(); else if (selectedPlugin() == "multi") plugins = SearchPluginManager::instance()->enabledPlugins();
else plugins << selectedPlugin(); else plugins << selectedPlugin();
qDebug("Search with category: %s", qUtf8Printable(selectedCategory())); qDebug("Search with category: %s", qUtf8Printable(selectedCategory()));
// Launch search // Launch search
auto *searchHandler = m_searchManager->startSearch(pattern, selectedCategory(), plugins); auto *searchHandler = SearchPluginManager::instance()->startSearch(pattern, selectedCategory(), plugins);
// Tab Addition // Tab Addition
auto *newTab = new SearchTab(searchHandler, this); auto *newTab = new SearchTab(searchHandler, this);

2
src/gui/search/searchwidget.h

@ -37,7 +37,6 @@ class QSignalMapper;
class QTabWidget; class QTabWidget;
class MainWindow; class MainWindow;
class SearchPluginManager;
class SearchTab; class SearchTab;
namespace Ui namespace Ui
@ -80,7 +79,6 @@ private:
QString selectedPlugin() const; QString selectedPlugin() const;
Ui::SearchWidget *m_ui; Ui::SearchWidget *m_ui;
SearchPluginManager *m_searchManager;
QSignalMapper *m_tabStatusChangedMapper; QSignalMapper *m_tabStatusChangedMapper;
QPointer<SearchTab> m_currentSearchTab; // Selected tab QPointer<SearchTab> m_currentSearchTab; // Selected tab
QPointer<SearchTab> m_activeSearchTab; // Tab with running search QPointer<SearchTab> m_activeSearchTab; // Tab with running search

Loading…
Cancel
Save