From 0b6ae688016229aad4ba895d63900459eaeda41a Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Wed, 6 Jun 2018 20:47:27 -0400 Subject: [PATCH] Add WebUI search API controller Closes #2495. --- src/base/search/searchpluginmanager.cpp | 26 +- src/gui/search/searchwidget.cpp | 3 +- src/webui/CMakeLists.txt | 2 + src/webui/api/isessionmanager.h | 5 + src/webui/api/searchcontroller.cpp | 367 ++++++++++++++++++++++++ src/webui/api/searchcontroller.h | 74 +++++ src/webui/webapplication.cpp | 2 + src/webui/webui.pri | 2 + 8 files changed, 464 insertions(+), 17 deletions(-) create mode 100644 src/webui/api/searchcontroller.cpp create mode 100644 src/webui/api/searchcontroller.h diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index a72eb0472..57ea35e61 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -187,8 +187,6 @@ void SearchPluginManager::updatePlugin(const QString &name) // Install or update plugin from file or url void SearchPluginManager::installPlugin(const QString &source) { - qDebug("Asked to install plugin at %s", qUtf8Printable(source)); - clearPythonCache(engineLocation()); if (Utils::Misc::isUrl(source)) { @@ -215,12 +213,10 @@ void SearchPluginManager::installPlugin(const QString &source) void SearchPluginManager::installPlugin_impl(const QString &name, const QString &path) { - PluginVersion newVersion = getPluginVersion(path); - qDebug() << "Version to be installed:" << newVersion; - + const PluginVersion newVersion = getPluginVersion(path); PluginInfo *plugin = pluginInfo(name); if (plugin && !(plugin->version < newVersion)) { - qDebug("Apparently update is not needed, we have a more recent version"); + LogMsg(tr("Plugin already at version %1, which is greater than %2").arg(plugin->version, newVersion), Log::INFO); emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed.")); return; } @@ -242,6 +238,7 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString if (!m_plugins.contains(name)) { // Remove broken file Utils::Fs::forceRemove(destPath); + LogMsg(tr("Plugin %1 is not supported.").arg(name), Log::INFO); if (updated) { // restore backup QFile::copy(destPath + ".bak", destPath); @@ -256,8 +253,10 @@ void SearchPluginManager::installPlugin_impl(const QString &name, const QString } else { // Install was successful, remove backup - if (updated) + if (updated) { + LogMsg(tr("Plugin %1 has been successfully updated.").arg(name), Log::INFO); Utils::Fs::forceRemove(destPath + ".bak"); + } } } @@ -494,10 +493,8 @@ void SearchPluginManager::update() void SearchPluginManager::parseVersionInfo(const QByteArray &info) { - qDebug("Checking if update is needed"); - QHash updateInfo; - bool dataCorrect = false; + int numCorrectData = 0; QList lines = info.split('\n'); foreach (QByteArray line, lines) { line = line.trimmed(); @@ -514,15 +511,15 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info) PluginVersion version = PluginVersion::tryParse(list.last(), {}); if (version == PluginVersion()) continue; - dataCorrect = true; + ++numCorrectData; if (isUpdateNeeded(pluginName, version)) { - qDebug("Plugin: %s is outdated", qUtf8Printable(pluginName)); + LogMsg(tr("Plugin %1 is outdated").arg(pluginName), Log::INFO); updateInfo[pluginName] = version; } } - if (!dataCorrect) - emit checkForUpdatesFailed(tr("An incorrect update info received.")); + if (numCorrectData < lines.size()) + emit checkForUpdatesFailed(tr("Incorrect update info received for %1 out of %2 plugins.").arg((lines.size() - numCorrectData), lines.size())); else emit checkForUpdatesFinished(updateInfo); } @@ -533,7 +530,6 @@ bool SearchPluginManager::isUpdateNeeded(QString pluginName, PluginVersion newVe if (!plugin) return true; PluginVersion oldVersion = plugin->version; - qDebug() << "IsUpdate needed? to be installed:" << newVersion << ", already installed:" << oldVersion; return (newVersion > oldVersion); } diff --git a/src/gui/search/searchwidget.cpp b/src/gui/search/searchwidget.cpp index e50c6d62d..55e6c9096 100644 --- a/src/gui/search/searchwidget.cpp +++ b/src/gui/search/searchwidget.cpp @@ -226,7 +226,6 @@ void SearchWidget::selectActivePage() SearchWidget::~SearchWidget() { qDebug("Search destruction"); - delete SearchPluginManager::instance(); delete m_ui; } @@ -279,7 +278,7 @@ void SearchWidget::giveFocusToSearchInput() // Function called when we click on search button void SearchWidget::on_searchButton_clicked() { - if (Utils::ForeignApps::pythonInfo().version.majorNumber() <= 0) { + if (!Utils::ForeignApps::pythonInfo().isValid()) { m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); return; } diff --git a/src/webui/CMakeLists.txt b/src/webui/CMakeLists.txt index 0f3471168..db72501d2 100644 --- a/src/webui/CMakeLists.txt +++ b/src/webui/CMakeLists.txt @@ -7,6 +7,7 @@ api/isessionmanager.h api/authcontroller.h api/logcontroller.h api/rsscontroller.h +api/searchcontroller.h api/synccontroller.h api/torrentscontroller.h api/transfercontroller.h @@ -22,6 +23,7 @@ api/appcontroller.cpp api/authcontroller.cpp api/logcontroller.cpp api/rsscontroller.cpp +api/searchcontroller.cpp api/synccontroller.cpp api/torrentscontroller.cpp api/transfercontroller.cpp diff --git a/src/webui/api/isessionmanager.h b/src/webui/api/isessionmanager.h index 980b9952b..9d77dcdb1 100644 --- a/src/webui/api/isessionmanager.h +++ b/src/webui/api/isessionmanager.h @@ -37,6 +37,11 @@ struct ISession virtual QString id() const = 0; virtual QVariant getData(const QString &id) const = 0; virtual void setData(const QString &id, const QVariant &data) = 0; + + template + T getData(const QString &id) const { + return this->getData(id).value(); + } }; struct ISessionManager diff --git a/src/webui/api/searchcontroller.cpp b/src/webui/api/searchcontroller.cpp new file mode 100644 index 000000000..57b379576 --- /dev/null +++ b/src/webui/api/searchcontroller.cpp @@ -0,0 +1,367 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2018 Thomas Piccirello + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include "searchcontroller.h" + +#include + +#include "base/global.h" +#include "base/logger.h" +#include "base/search/searchhandler.h" +#include "base/utils/foreignapps.h" +#include "base/utils/random.h" +#include "base/utils/string.h" +#include "apierror.h" + +class SearchPluginManager; + +using SearchHandlerPtr = QSharedPointer; +using SearchHandlerDict = QMap; + +namespace +{ + const QLatin1String ACTIVE_SEARCHES("activeSearches"); + const QLatin1String SEARCH_HANDLERS("searchHandlers"); + + void removeActiveSearch(ISession *session, const int id) + { + auto activeSearches = session->getData>(ACTIVE_SEARCHES); + if (activeSearches.remove(id) > 0) + session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches)); + } +} + +void SearchController::startAction() +{ + checkParams({"pattern", "category", "plugins"}); + + if (!Utils::ForeignApps::pythonInfo().isValid()) + throw APIError(APIErrorType::Conflict, "Python must be installed to use the Search Engine."); + + const QString pattern = params()["pattern"].trimmed(); + const QString category = params()["category"].trimmed(); + const QStringList plugins = params()["plugins"].split('|'); + + QStringList pluginsToUse; + if (plugins.size() == 1) { + const QString pluginsLower = plugins[0].toLower(); + if (pluginsLower == "all") + pluginsToUse = SearchPluginManager::instance()->allPlugins(); + else if ((pluginsLower == "enabled") || (pluginsLower == "multi")) + pluginsToUse = SearchPluginManager::instance()->enabledPlugins(); + else + pluginsToUse << plugins; + } + else { + pluginsToUse << plugins; + } + + ISession *const session = sessionManager()->session(); + auto activeSearches = session->getData>(ACTIVE_SEARCHES); + if (activeSearches.size() >= MAX_CONCURRENT_SEARCHES) + throw APIError(APIErrorType::Conflict, QString("Unable to create more than %1 concurrent searches.").arg(MAX_CONCURRENT_SEARCHES)); + + const auto id = generateSearchId(); + const SearchHandlerPtr searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)}; + QObject::connect(searchHandler.data(), &SearchHandler::searchFinished, this, [session, id, this]() { searchFinished(session, id); }); + QObject::connect(searchHandler.data(), &SearchHandler::searchFailed, this, [session, id, this]() { searchFailed(session, id); }); + + auto searchHandlers = session->getData(SEARCH_HANDLERS); + searchHandlers.insert(id, searchHandler); + session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers)); + + activeSearches.insert(id); + session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches)); + + const QJsonObject result = {{"id", id}}; + setResult(result); +} + +void SearchController::stopAction() +{ + checkParams({"id"}); + + const int id = params()["id"].toInt(); + ISession *const session = sessionManager()->session(); + + auto searchHandlers = session->getData(SEARCH_HANDLERS); + if (!searchHandlers.contains(id)) + throw APIError(APIErrorType::NotFound); + + const SearchHandlerPtr searchHandler = searchHandlers[id]; + + if (searchHandler->isActive()) { + searchHandler->cancelSearch(); + removeActiveSearch(session, id); + } +} + +void SearchController::statusAction() +{ + const int id = params()["id"].toInt(); + + const auto searchHandlers = sessionManager()->session()->getData(SEARCH_HANDLERS); + if ((id != 0) && !searchHandlers.contains(id)) + throw APIError(APIErrorType::NotFound); + + QJsonArray statusArray; + const QList searchIds {(id == 0) ? searchHandlers.keys() : QList {id}}; + + for (const int searchId : searchIds) { + const SearchHandlerPtr searchHandler = searchHandlers[searchId]; + statusArray << QJsonObject { + {"id", searchId}, + {"status", searchHandler->isActive() ? "Running" : "Stopped"}, + {"total", searchHandler->results().size()} + }; + } + + setResult(statusArray); +} + +void SearchController::resultsAction() +{ + checkParams({"id"}); + + const int id = params()["id"].toInt(); + int limit = params()["limit"].toInt(); + int offset = params()["offset"].toInt(); + + const auto searchHandlers = sessionManager()->session()->getData(SEARCH_HANDLERS); + if (!searchHandlers.contains(id)) + throw APIError(APIErrorType::NotFound); + + const SearchHandlerPtr searchHandler = searchHandlers[id]; + const QList searchResults = searchHandler->results(); + const int size = searchResults.size(); + + if (offset > size) + throw APIError(APIErrorType::Conflict, tr("Offset is out of range")); + + // normalize values + if (offset < 0) + offset = size + offset; + if (offset < 0) // check again + throw APIError(APIErrorType::Conflict, tr("Offset is out of range")); + if (limit <= 0) + limit = -1; + + if ((limit > 0) || (offset > 0)) + setResult(getResults(searchResults.mid(offset, limit), searchHandler->isActive(), size)); + else + setResult(getResults(searchResults, searchHandler->isActive(), size)); +} + +void SearchController::deleteAction() +{ + checkParams({"id"}); + + const int id = params()["id"].toInt(); + ISession *const session = sessionManager()->session(); + + auto searchHandlers = session->getData(SEARCH_HANDLERS); + if (!searchHandlers.contains(id)) + throw APIError(APIErrorType::NotFound); + + const SearchHandlerPtr searchHandler = searchHandlers[id]; + searchHandler->cancelSearch(); + searchHandlers.remove(id); + session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers)); + + removeActiveSearch(session, id); +} + +void SearchController::categoriesAction() +{ + QStringList categories; + const QString name = params()["pluginName"].trimmed(); + + categories << SearchPluginManager::categoryFullName("all"); + for (const QString &category : copyAsConst(SearchPluginManager::instance()->getPluginCategories(name))) + categories << SearchPluginManager::categoryFullName(category); + + const QJsonArray result = QJsonArray::fromStringList(categories); + setResult(result); +} + +void SearchController::pluginsAction() +{ + const QStringList allPlugins = SearchPluginManager::instance()->allPlugins(); + setResult(getPluginsInfo(allPlugins)); +} + +void SearchController::installPluginAction() +{ + checkParams({"sources"}); + + const QStringList sources = params()["sources"].split('|'); + for (const QString &source : sources) + SearchPluginManager::instance()->installPlugin(source); +} + +void SearchController::uninstallPluginAction() +{ + checkParams({"names"}); + + const QStringList names = params()["names"].split('|'); + for (const QString &name : names) + SearchPluginManager::instance()->uninstallPlugin(name.trimmed()); +} + +void SearchController::enablePluginAction() +{ + checkParams({"names", "enable"}); + + const QStringList names = params()["names"].split('|'); + const bool enable = Utils::String::parseBool(params()["enable"].trimmed(), false); + + for (const QString &name : names) + SearchPluginManager::instance()->enablePlugin(name.trimmed(), enable); +} + +void SearchController::updatePluginsAction() +{ + SearchPluginManager *const pluginManager = SearchPluginManager::instance(); + + connect(pluginManager, &SearchPluginManager::checkForUpdatesFinished, this, &SearchController::checkForUpdatesFinished); + connect(pluginManager, &SearchPluginManager::checkForUpdatesFailed, this, &SearchController::checkForUpdatesFailed); + pluginManager->checkForUpdates(); +} + +void SearchController::checkForUpdatesFinished(const QHash &updateInfo) +{ + if (updateInfo.isEmpty()) { + LogMsg(tr("All plugins are already up to date."), Log::INFO); + return; + } + + LogMsg(tr("Updating %1 plugins").arg(updateInfo.size()), Log::INFO); + + SearchPluginManager *const pluginManager = SearchPluginManager::instance(); + for (const QString &pluginName : updateInfo.keys()) { + LogMsg(tr("Updating plugin %1").arg(pluginName), Log::INFO); + pluginManager->updatePlugin(pluginName); + } +} + +void SearchController::checkForUpdatesFailed(const QString &reason) +{ + LogMsg(tr("Failed to check for plugin updates: %1").arg(reason), Log::INFO); +} + +void SearchController::searchFinished(ISession *session, const int id) +{ + removeActiveSearch(session, id); +} + +void SearchController::searchFailed(ISession *session, const int id) +{ + removeActiveSearch(session, id); +} + +int SearchController::generateSearchId() const +{ + const auto searchHandlers = sessionManager()->session()->getData(SEARCH_HANDLERS); + + while (true) + { + const auto id = Utils::Random::rand(1, INT_MAX); + if (!searchHandlers.contains(id)) + return id; + } +} + +/** + * Returns the search results in JSON format. + * + * The return value is an object with a status and an array of dictionaries. + * The dictionary keys are: + * - "fileName" + * - "fileUrl" + * - "fileSize" + * - "nbSeeders" + * - "nbLeechers" + * - "siteUrl" + * - "descrLink" + */ +QJsonObject SearchController::getResults(const QList &searchResults, const bool isSearchActive, const int totalResults) const +{ + QJsonArray searchResultsArray; + for (const SearchResult &searchResult : searchResults) { + searchResultsArray << QJsonObject { + {"fileName", searchResult.fileName}, + {"fileUrl", searchResult.fileUrl}, + {"fileSize", searchResult.fileSize}, + {"nbSeeders", searchResult.nbSeeders}, + {"nbLeechers", searchResult.nbLeechers}, + {"siteUrl", searchResult.siteUrl}, + {"descrLink", searchResult.descrLink} + }; + } + + const QJsonObject result = { + {"status", isSearchActive ? "Running" : "Stopped"}, + {"results", searchResultsArray}, + {"total", totalResults} + }; + + return result; +} + +/** + * Returns the search plugins in JSON format. + * + * The return value is an array of dictionaries. + * The dictionary keys are: + * - "name" + * - "version" + * - "fullName" + * - "url" + * - "supportedCategories" + * - "iconPath" + * - "enabled" + */ +QJsonArray SearchController::getPluginsInfo(const QStringList &plugins) const +{ + QJsonArray pluginsArray; + + for (const QString &plugin : plugins) { + const PluginInfo *const pluginInfo = SearchPluginManager::instance()->pluginInfo(plugin); + + pluginsArray << QJsonObject { + {"name", pluginInfo->name}, + {"version", QString(pluginInfo->version)}, + {"fullName", pluginInfo->fullName}, + {"url", pluginInfo->url}, + {"supportedCategories", QJsonArray::fromStringList(pluginInfo->supportedCategories)}, + {"enabled", pluginInfo->enabled} + }; + } + + return pluginsArray; +} diff --git a/src/webui/api/searchcontroller.h b/src/webui/api/searchcontroller.h new file mode 100644 index 000000000..9f3fcd7d9 --- /dev/null +++ b/src/webui/api/searchcontroller.h @@ -0,0 +1,74 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2018 Thomas Piccirello + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#pragma once + +#include +#include +#include +#include +#include + +#include "base/search/searchpluginmanager.h" +#include "apicontroller.h" +#include "isessionmanager.h" + +struct SearchResult; + +class SearchController : public APIController +{ + Q_OBJECT + Q_DISABLE_COPY(SearchController) + +public: + using APIController::APIController; + +private slots: + void startAction(); + void stopAction(); + void statusAction(); + void resultsAction(); + void deleteAction(); + void categoriesAction(); + void pluginsAction(); + void installPluginAction(); + void uninstallPluginAction(); + void enablePluginAction(); + void updatePluginsAction(); + +private: + const int MAX_CONCURRENT_SEARCHES = 5; + + void checkForUpdatesFinished(const QHash &updateInfo); + void checkForUpdatesFailed(const QString &reason); + void searchFinished(ISession *session, const int id); + void searchFailed(ISession *session, const int id); + int generateSearchId() const; + QJsonObject getResults(const QList &searchResults, bool isSearchActive, int totalResults) const; + QJsonArray getPluginsInfo(const QStringList &plugins) const; +}; diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index 1c5d866af..056ac5ddf 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -61,6 +61,7 @@ #include "api/authcontroller.h" #include "api/logcontroller.h" #include "api/rsscontroller.h" +#include "api/searchcontroller.h" #include "api/synccontroller.h" #include "api/torrentscontroller.h" #include "api/transfercontroller.h" @@ -164,6 +165,7 @@ WebApplication::WebApplication(QObject *parent) registerAPIController(QLatin1String("auth"), new AuthController(this, this)); registerAPIController(QLatin1String("log"), new LogController(this, this)); registerAPIController(QLatin1String("rss"), new RSSController(this, this)); + registerAPIController(QLatin1String("search"), new SearchController(this, this)); registerAPIController(QLatin1String("sync"), new SyncController(this, this)); registerAPIController(QLatin1String("torrents"), new TorrentsController(this, this)); registerAPIController(QLatin1String("transfer"), new TransferController(this, this)); diff --git a/src/webui/webui.pri b/src/webui/webui.pri index 2c960ca7c..a2b693f78 100644 --- a/src/webui/webui.pri +++ b/src/webui/webui.pri @@ -6,6 +6,7 @@ HEADERS += \ $$PWD/api/isessionmanager.h \ $$PWD/api/logcontroller.h \ $$PWD/api/rsscontroller.h \ + $$PWD/api/searchcontroller.h \ $$PWD/api/synccontroller.h \ $$PWD/api/torrentscontroller.h \ $$PWD/api/transfercontroller.h \ @@ -21,6 +22,7 @@ SOURCES += \ $$PWD/api/authcontroller.cpp \ $$PWD/api/logcontroller.cpp \ $$PWD/api/rsscontroller.cpp \ + $$PWD/api/searchcontroller.cpp \ $$PWD/api/synccontroller.cpp \ $$PWD/api/torrentscontroller.cpp \ $$PWD/api/transfercontroller.cpp \