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 @@ @@ -35,6 +35,7 @@
#include <QDomElement>
#include <QDomNode>
#include <QList>
#include <QPointer>
#include <QProcess>
#include "base/logger.h"
@ -47,11 +48,16 @@ @@ -47,11 +48,16 @@
#include "searchdownloadhandler.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 + "c");
}
}
QPointer<SearchPluginManager> SearchPluginManager::m_instance = nullptr;
const QHash<QString, QString> SearchPluginManager::m_categoryNames {
{"all", QT_TRANSLATE_NOOP("SearchEngine", "All categories")},
@ -68,6 +74,9 @@ const QHash<QString, QString> SearchPluginManager::m_categoryNames { @@ -68,6 +74,9 @@ const QHash<QString, QString> 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() @@ -77,6 +86,11 @@ SearchPluginManager::~SearchPluginManager()
qDeleteAll(m_plugins);
}
SearchPluginManager *SearchPluginManager::instance()
{
return m_instance;
}
QStringList SearchPluginManager::allPlugins() const
{
return m_plugins.keys();

3
src/base/search/searchpluginmanager.h

@ -61,6 +61,8 @@ public: @@ -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: @@ -107,6 +109,7 @@ private:
static QString pluginPath(const QString &name);
static QPointer<SearchPluginManager> m_instance;
static const QHash<QString, QString> m_categoryNames;
const QString m_updateUrl;

31
src/gui/search/searchwidget.cpp

@ -135,17 +135,18 @@ SearchWidget::SearchWidget(MainWindow *mainWindow) @@ -135,17 +135,18 @@ SearchWidget::SearchWidget(MainWindow *mainWindow)
connect(m_tabStatusChangedMapper, static_cast<void (QSignalMapper::*)(QWidget *)>(&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() @@ -163,7 +164,7 @@ void SearchWidget::fillCatCombobox()
using QStrPair = QPair<QString, QString>;
QList<QStrPair> 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() @@ -185,8 +186,8 @@ void SearchWidget::fillPluginComboBox()
using QStrPair = QPair<QString, QString>;
QList<QStrPair> 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 @@ -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() @@ -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) @@ -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() @@ -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);

2
src/gui/search/searchwidget.h

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

Loading…
Cancel
Save