Browse Source

Add WebUI search API controller

Closes #2495.
adaptive-webui-19844
Thomas Piccirello 7 years ago
parent
commit
0b6ae68801
  1. 26
      src/base/search/searchpluginmanager.cpp
  2. 3
      src/gui/search/searchwidget.cpp
  3. 2
      src/webui/CMakeLists.txt
  4. 5
      src/webui/api/isessionmanager.h
  5. 367
      src/webui/api/searchcontroller.cpp
  6. 74
      src/webui/api/searchcontroller.h
  7. 2
      src/webui/webapplication.cpp
  8. 2
      src/webui/webui.pri

26
src/base/search/searchpluginmanager.cpp

@ -187,8 +187,6 @@ void SearchPluginManager::updatePlugin(const QString &name) @@ -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) @@ -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 @@ -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 @@ -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() @@ -494,10 +493,8 @@ void SearchPluginManager::update()
void SearchPluginManager::parseVersionInfo(const QByteArray &info)
{
qDebug("Checking if update is needed");
QHash<QString, PluginVersion> updateInfo;
bool dataCorrect = false;
int numCorrectData = 0;
QList<QByteArray> lines = info.split('\n');
foreach (QByteArray line, lines) {
line = line.trimmed();
@ -514,15 +511,15 @@ void SearchPluginManager::parseVersionInfo(const QByteArray &info) @@ -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 @@ -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);
}

3
src/gui/search/searchwidget.cpp

@ -226,7 +226,6 @@ void SearchWidget::selectActivePage() @@ -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() @@ -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;
}

2
src/webui/CMakeLists.txt

@ -7,6 +7,7 @@ api/isessionmanager.h @@ -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 @@ -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

5
src/webui/api/isessionmanager.h

@ -37,6 +37,11 @@ struct ISession @@ -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 <class T>
T getData(const QString &id) const {
return this->getData(id).value<T>();
}
};
struct ISessionManager

367
src/webui/api/searchcontroller.cpp

@ -0,0 +1,367 @@ @@ -0,0 +1,367 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* 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 <QSharedPointer>
#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<SearchHandler>;
using SearchHandlerDict = QMap<int, SearchHandlerPtr>;
namespace
{
const QLatin1String ACTIVE_SEARCHES("activeSearches");
const QLatin1String SEARCH_HANDLERS("searchHandlers");
void removeActiveSearch(ISession *session, const int id)
{
auto activeSearches = session->getData<QSet<int>>(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<QSet<int>>(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<SearchHandlerDict>(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<SearchHandlerDict>(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<SearchHandlerDict>(SEARCH_HANDLERS);
if ((id != 0) && !searchHandlers.contains(id))
throw APIError(APIErrorType::NotFound);
QJsonArray statusArray;
const QList<int> searchIds {(id == 0) ? searchHandlers.keys() : QList<int> {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<SearchHandlerDict>(SEARCH_HANDLERS);
if (!searchHandlers.contains(id))
throw APIError(APIErrorType::NotFound);
const SearchHandlerPtr searchHandler = searchHandlers[id];
const QList<SearchResult> 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<SearchHandlerDict>(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<QString, PluginVersion> &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<SearchHandlerDict>(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<SearchResult> &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;
}

74
src/webui/api/searchcontroller.h

@ -0,0 +1,74 @@ @@ -0,0 +1,74 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
*
* 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 <QHash>
#include <QJsonArray>
#include <QJsonObject>
#include <QList>
#include <QString>
#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<QString, PluginVersion> &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<SearchResult> &searchResults, bool isSearchActive, int totalResults) const;
QJsonArray getPluginsInfo(const QStringList &plugins) const;
};

2
src/webui/webapplication.cpp

@ -61,6 +61,7 @@ @@ -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) @@ -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));

2
src/webui/webui.pri

@ -6,6 +6,7 @@ HEADERS += \ @@ -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 += \ @@ -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 \

Loading…
Cancel
Save