Move basic search-related code into Core.

Also use qBittorrent torrent file download routines instead of
nova2dl.py script.
This commit is contained in:
Vladimir Golovnev (Glassez) 2015-08-27 14:25:14 +03:00 committed by Vladimir Golovnev (qlassez)
parent 8754fd5646
commit 54979e6b53
22 changed files with 1379 additions and 1235 deletions

View file

@ -42,7 +42,8 @@ HEADERS += \
$$PWD/utils/string.h \
$$PWD/unicodestrings.h \
$$PWD/torrentfilter.h \
$$PWD/scanfoldersmodel.h
$$PWD/scanfoldersmodel.h \
$$PWD/searchengine.h
SOURCES += \
$$PWD/tristatebool.cpp \
@ -83,4 +84,5 @@ SOURCES += \
$$PWD/utils/misc.cpp \
$$PWD/utils/string.cpp \
$$PWD/torrentfilter.cpp \
$$PWD/scanfoldersmodel.cpp
$$PWD/scanfoldersmodel.cpp \
$$PWD/searchengine.cpp

View file

@ -968,6 +968,10 @@ bool Session::addTorrent(QString source, const AddTorrentParams &params)
qDebug("Converting bc link to magnet link");
source = Utils::Misc::bcLinkToMagnet(source);
}
else if (((source.size() == 40) && !source.contains(QRegExp("[^0-9A-Fa-f]")))
|| ((source.size() == 32) && !source.contains(QRegExp("[^2-7A-Za-z]")))) {
source = "magnet:?xt=urn:btih:" + source;
}
if (Utils::Misc::isUrl(source)) {
Logger::instance()->addMessage(tr("Downloading '%1', please wait...", "e.g: Downloading 'xxx.torrent', please wait...").arg(source));

657
src/base/searchengine.cpp Normal file
View file

@ -0,0 +1,657 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* 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 <QDomDocument>
#include <QDomNode>
#include <QDomElement>
#include <QProcess>
#include <QDir>
#include <QDebug>
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/preferences.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "searchengine.h"
enum SearchResultColumn
{
PL_DL_LINK,
PL_NAME,
PL_SIZE,
PL_SEEDS,
PL_LEECHS,
PL_ENGINE_URL,
PL_DESC_LINK,
NB_PLUGIN_COLUMNS
};
static inline void removePythonScriptIfExists(const QString &scriptPath)
{
Utils::Fs::forceRemove(scriptPath);
Utils::Fs::forceRemove(scriptPath + "c");
}
const QHash<QString, QString> SearchEngine::m_categoryNames = SearchEngine::initializeCategoryNames();
SearchEngine::SearchEngine()
: m_searchStopped(false)
, m_updateUrl(QString("https://raw.github.com/qbittorrent/qBittorrent/master/src/searchengine/%1/engines/").arg(Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova"))
{
updateNova();
m_searchProcess = new QProcess(this);
m_searchProcess->setEnvironment(QProcess::systemEnvironment());
connect(m_searchProcess, SIGNAL(started()), this, SIGNAL(searchStarted()));
connect(m_searchProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readSearchOutput()));
connect(m_searchProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
m_searchTimeout = new QTimer(this);
m_searchTimeout->setSingleShot(true);
connect(m_searchTimeout, SIGNAL(timeout()), this, SLOT(onTimeout()));
update();
}
SearchEngine::~SearchEngine()
{
qDeleteAll(m_plugins.values());
cancelSearch();
}
QStringList SearchEngine::allPlugins() const
{
return m_plugins.keys();
}
QStringList SearchEngine::enabledPlugins() const
{
QStringList plugins;
foreach (const PluginInfo *plugin, m_plugins.values()) {
if (plugin->enabled)
plugins << plugin->name;
}
return plugins;
}
QStringList SearchEngine::supportedCategories() const
{
QStringList result;
foreach (const PluginInfo *plugin, m_plugins.values()) {
if (plugin->enabled) {
foreach (QString cat, plugin->supportedCategories) {
if (!result.contains(cat))
result << cat;
}
}
}
return result;
}
PluginInfo *SearchEngine::pluginInfo(const QString &name) const
{
return m_plugins.value(name, 0);
}
bool SearchEngine::isActive() const
{
return (m_searchProcess->state() != QProcess::NotRunning);
}
void SearchEngine::enablePlugin(const QString &name, bool enabled)
{
PluginInfo *plugin = m_plugins.value(name, 0);
if (plugin) {
plugin->enabled = enabled;
// Save to Hard disk
Preferences *const pref = Preferences::instance();
QStringList disabledPlugins = pref->getSearchEngDisabled();
if (enabled)
disabledPlugins.removeAll(name);
else if (!disabledPlugins.contains(name))
disabledPlugins.append(name);
pref->setSearchEngDisabled(disabledPlugins);
}
}
// Updates shipped plugin
void SearchEngine::updatePlugin(const QString &name)
{
installPlugin(QString("%1%2.py").arg(m_updateUrl).arg(name));
}
// Install or update plugin from file or url
void SearchEngine::installPlugin(const QString &source)
{
qDebug("Asked to install plugin at %s", qPrintable(source));
if (Utils::Misc::isUrl(source)) {
Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(source, true);
connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(pluginDownloaded(QString, QString)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(pluginDownloadFailed(QString, QString)));
}
else {
QString path = source;
if (path.startsWith("file:", Qt::CaseInsensitive))
path = QUrl(path).toLocalFile();
QString pluginName = Utils::Fs::fileName(path);
pluginName.chop(pluginName.size() - pluginName.lastIndexOf("."));
if (!path.endsWith(".py", Qt::CaseInsensitive))
emit pluginInstallationFailed(pluginName, tr("Unknown search engine plugin file format."));
else
installPlugin_impl(pluginName, path);
}
}
void SearchEngine::installPlugin_impl(const QString &name, const QString &path)
{
qreal newVersion = getPluginVersion(path);
qDebug("Version to be installed: %.2f", newVersion);
PluginInfo *plugin = pluginInfo(name);
if (plugin && (plugin->version >= newVersion)) {
qDebug("Apparently update is not needed, we have a more recent version");
emit pluginUpdateFailed(name, tr("A more recent version of this plugin is already installed."));
return;
}
// Process with install
QString destPath = pluginPath(name);
bool updated = false;
if (QFile::exists(destPath)) {
// Backup in case install fails
QFile::copy(destPath, destPath + ".bak");
Utils::Fs::forceRemove(destPath);
Utils::Fs::forceRemove(destPath + "c");
updated = true;
}
// Copy the plugin
QFile::copy(path, destPath);
// Update supported plugins
update();
// Check if this was correctly installed
if (!m_plugins.contains(name)) {
// Remove broken file
Utils::Fs::forceRemove(destPath);
if (updated) {
// restore backup
QFile::copy(destPath + ".bak", destPath);
Utils::Fs::forceRemove(destPath + ".bak");
// Update supported plugins
update();
emit pluginUpdateFailed(name, tr("Plugin is not supported."));
}
else {
emit pluginInstallationFailed(name, tr("Plugin is not supported."));
}
}
else {
// Install was successful, remove backup
if (updated)
Utils::Fs::forceRemove(destPath + ".bak");
}
}
bool SearchEngine::uninstallPlugin(const QString &name)
{
if (QFile::exists(":/nova/engines/" + name + ".py"))
return false;
// Proceed with uninstall
// remove it from hard drive
QDir pluginsFolder(pluginsLocation());
QStringList filters;
filters << name + ".*";
QStringList files = pluginsFolder.entryList(filters, QDir::Files, QDir::Unsorted);
QString file;
foreach (file, files)
Utils::Fs::forceRemove(pluginsFolder.absoluteFilePath(file));
// Remove it from supported engines
delete m_plugins.take(name);
return true;
}
void SearchEngine::checkForUpdates()
{
// Download version file from update server on sourceforge
Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(m_updateUrl + "versions.txt");
connect(handler, SIGNAL(downloadFinished(QString, QByteArray)), this, SLOT(versionInfoDownloaded(QString, QByteArray)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(versionInfoDownloadFailed(QString, QString)));
}
void SearchEngine::cancelSearch()
{
if (m_searchProcess->state() != QProcess::NotRunning) {
#ifdef Q_OS_WIN
m_searchProcess->kill();
#else
m_searchProcess->terminate();
#endif
m_searchStopped = true;
m_searchTimeout->stop();
m_searchProcess->waitForFinished(1000);
}
}
void SearchEngine::startSearch(const QString &pattern, const QString &category, const QStringList &usedPlugins)
{
// Search process already running or
// No search pattern entered
if ((m_searchProcess->state() != QProcess::NotRunning) || pattern.isEmpty()) {
emit searchFailed();
return;
}
// Reload environment variables (proxy)
m_searchProcess->setEnvironment(QProcess::systemEnvironment());
QStringList params;
m_searchStopped = false;
params << Utils::Fs::toNativePath(engineLocation() + "/nova2.py");
params << usedPlugins.join(",");
params << category;
params << pattern.split(" ");
// Launch search
m_searchProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
m_searchTimeout->start(180000); // 3min
}
QString SearchEngine::categoryFullName(const QString &categoryName)
{
return tr(m_categoryNames.value(categoryName).toUtf8().constData());
}
QString SearchEngine::pluginsLocation()
{
return QString("%1/engines").arg(engineLocation());
}
QString SearchEngine::engineLocation()
{
QString folder = "nova";
if (Utils::Misc::pythonVersion() >= 3)
folder = "nova3";
const QString location = Utils::Fs::expandPathAbs(Utils::Fs::QDesktopServicesDataLocation() + folder);
QDir locationDir(location);
if (!locationDir.exists())
locationDir.mkpath(locationDir.absolutePath());
return location;
}
// Slot called when QProcess is Finished
// QProcess can be finished for 3 reasons :
// Error | Stopped by user | Finished normally
void SearchEngine::processFinished(int exitcode, QProcess::ExitStatus)
{
m_searchTimeout->stop();
if (exitcode == 0)
emit searchFinished(m_searchStopped);
else
emit searchFailed();
}
void SearchEngine::versionInfoDownloaded(const QString &url, const QByteArray &data)
{
Q_UNUSED(url)
parseVersionInfo(data);
}
void SearchEngine::versionInfoDownloadFailed(const QString &url, const QString &reason)
{
Q_UNUSED(url)
emit checkForUpdatesFailed(tr("Update server is temporarily unavailable. %1").arg(reason));
}
void SearchEngine::pluginDownloaded(const QString &url, QString filePath)
{
filePath = Utils::Fs::fromNativePath(filePath);
QString pluginName = Utils::Fs::fileName(url);
pluginName.chop(pluginName.size() - pluginName.lastIndexOf(".")); // Remove extension
installPlugin_impl(pluginName, filePath);
Utils::Fs::forceRemove(filePath);
}
void SearchEngine::pluginDownloadFailed(const QString &url, const QString &reason)
{
QString pluginName = url.split('/').last();
pluginName.replace(".py", "", Qt::CaseInsensitive);
if (pluginInfo(pluginName))
emit pluginUpdateFailed(pluginName, tr("Failed to download the plugin file. %1").arg(reason));
else
emit pluginInstallationFailed(pluginName, tr("Failed to download the plugin file. %1").arg(reason));
}
// Update nova.py search plugin if necessary
void SearchEngine::updateNova()
{
qDebug("Updating nova");
// create nova directory if necessary
QDir searchDir(engineLocation());
QString novaFolder = Utils::Misc::pythonVersion() >= 3 ? "searchengine/nova3" : "searchengine/nova";
QFile packageFile(searchDir.absoluteFilePath("__init__.py"));
packageFile.open(QIODevice::WriteOnly | QIODevice::Text);
packageFile.close();
if (!searchDir.exists("engines"))
searchDir.mkdir("engines");
Utils::Fs::removeDirRecursive(searchDir.absoluteFilePath("__pycache__"));
QFile packageFile2(searchDir.absolutePath() + "/engines/__init__.py");
packageFile2.open(QIODevice::WriteOnly | QIODevice::Text);
packageFile2.close();
// Copy search plugin files (if necessary)
QString filePath = searchDir.absoluteFilePath("nova2.py");
if (getPluginVersion(":/" + novaFolder + "/nova2.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/nova2.py", filePath);
}
filePath = searchDir.absoluteFilePath("fix_encoding.py");
QFile::copy(":/" + novaFolder + "/fix_encoding.py", filePath);
filePath = searchDir.absoluteFilePath("novaprinter.py");
if (getPluginVersion(":/" + novaFolder + "/novaprinter.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/novaprinter.py", filePath);
}
filePath = searchDir.absoluteFilePath("helpers.py");
if (getPluginVersion(":/" + novaFolder + "/helpers.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/helpers.py", filePath);
}
filePath = searchDir.absoluteFilePath("socks.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/socks.py", filePath);
if (novaFolder.endsWith("nova")) {
filePath = searchDir.absoluteFilePath("fix_encoding.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/fix_encoding.py", filePath);
}
else if (novaFolder.endsWith("nova3")) {
filePath = searchDir.absoluteFilePath("sgmllib3.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + novaFolder + "/sgmllib3.py", filePath);
}
QDir destDir(pluginsLocation());
Utils::Fs::removeDirRecursive(destDir.absoluteFilePath("__pycache__"));
QDir shippedSubdir(":/" + novaFolder + "/engines/");
QStringList files = shippedSubdir.entryList();
foreach (const QString &file, files) {
QString shippedFile = shippedSubdir.absoluteFilePath(file);
// Copy python classes
if (file.endsWith(".py")) {
const QString destFile = destDir.absoluteFilePath(file);
if (getPluginVersion(shippedFile) > getPluginVersion(destFile) ) {
qDebug("shipped %s is more recent then local plugin, updating...", qPrintable(file));
removePythonScriptIfExists(destFile);
qDebug("%s copied to %s", qPrintable(shippedFile), qPrintable(destFile));
QFile::copy(shippedFile, destFile);
}
}
else {
// Copy icons
if (file.endsWith(".png"))
if (!QFile::exists(destDir.absoluteFilePath(file)))
QFile::copy(shippedFile, destDir.absoluteFilePath(file));
}
}
}
void SearchEngine::onTimeout()
{
cancelSearch();
}
// search QProcess return output as soon as it gets new
// stuff to read. We split it into lines and parse each
// line to SearchResult calling parseSearchResult().
void SearchEngine::readSearchOutput()
{
QByteArray output = m_searchProcess->readAllStandardOutput();
output.replace("\r", "");
QList<QByteArray> lines = output.split('\n');
if (!m_searchResultLineTruncated.isEmpty())
lines.prepend(m_searchResultLineTruncated + lines.takeFirst());
m_searchResultLineTruncated = lines.takeLast().trimmed();
QList<SearchResult> searchResultList;
foreach (const QByteArray &line, lines) {
SearchResult searchResult;
if (parseSearchResult(QString::fromUtf8(line), searchResult))
searchResultList << searchResult;
}
if (!searchResultList.isEmpty())
emit newSearchResults(searchResultList);
}
void SearchEngine::update()
{
QProcess nova;
nova.setEnvironment(QProcess::systemEnvironment());
QStringList params;
params << Utils::Fs::toNativePath(engineLocation() + "/nova2.py");
params << "--capabilities";
nova.start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
nova.waitForStarted();
nova.waitForFinished();
QString capabilities = QString(nova.readAll());
QDomDocument xmlDoc;
if (!xmlDoc.setContent(capabilities)) {
qWarning() << "Could not parse Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
qWarning() << "Error: " << nova.readAllStandardError().constData();
return;
}
QDomElement root = xmlDoc.documentElement();
if (root.tagName() != "capabilities") {
qWarning() << "Invalid XML file for Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
return;
}
for (QDomNode engineNode = root.firstChild(); !engineNode.isNull(); engineNode = engineNode.nextSibling()) {
QDomElement engineElem = engineNode.toElement();
if (!engineElem.isNull()) {
QString pluginName = engineElem.tagName();
PluginInfo *plugin = new PluginInfo;
plugin->name = pluginName;
plugin->version = getPluginVersion(pluginPath(pluginName));
plugin->fullName = engineElem.elementsByTagName("name").at(0).toElement().text();
plugin->url = engineElem.elementsByTagName("url").at(0).toElement().text();
foreach (QString cat, engineElem.elementsByTagName("categories").at(0).toElement().text().split(" ")) {
cat = cat.trimmed();
if (!cat.isEmpty())
plugin->supportedCategories << cat;
}
QStringList disabledEngines = Preferences::instance()->getSearchEngDisabled();
plugin->enabled = !disabledEngines.contains(pluginName);
// Handle icon
QString iconPath = QString("%1/%2.png").arg(pluginsLocation()).arg(pluginName);
if (QFile::exists(iconPath)) {
plugin->iconPath = iconPath;
}
else {
iconPath = QString("%1/%2.ico").arg(pluginsLocation()).arg(pluginName);
if (QFile::exists(iconPath))
plugin->iconPath = iconPath;
}
if (!m_plugins.contains(pluginName)) {
m_plugins[pluginName] = plugin;
emit pluginInstalled(pluginName);
}
else if (m_plugins[pluginName]->version != plugin->version) {
delete m_plugins.take(pluginName);
m_plugins[pluginName] = plugin;
emit pluginUpdated(pluginName);
}
}
}
}
// Parse one line of search results list
// Line is in the following form:
// file url | file name | file size | nb seeds | nb leechers | Search engine url
bool SearchEngine::parseSearchResult(const QString &line, SearchResult &searchResult)
{
const QStringList parts = line.split("|");
const int nbFields = parts.size();
if (nbFields < (NB_PLUGIN_COLUMNS - 1)) return false; // -1 because desc_link is optional
searchResult = SearchResult();
searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed(); // download URL
searchResult.fileName = parts.at(PL_NAME).trimmed(); // Name
searchResult.fileSize = parts.at(PL_SIZE).trimmed().toLongLong(); // Size
bool ok = false;
searchResult.nbSeeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok); // Seeders
if (!ok || (searchResult.nbSeeders < 0))
searchResult.nbSeeders = -1;
searchResult.nbLeechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok); // Leechers
if (!ok || (searchResult.nbLeechers < 0))
searchResult.nbLeechers = -1;
searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed(); // Search site URL
if (nbFields == NB_PLUGIN_COLUMNS)
searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed(); // Description Link
return true;
}
void SearchEngine::parseVersionInfo(const QByteArray &info)
{
qDebug("Checking if update is needed");
QHash<QString, qreal> updateInfo;
bool dataCorrect = false;
QList<QByteArray> lines = info.split('\n');
foreach (QByteArray line, lines) {
line = line.trimmed();
if (line.isEmpty()) continue;
if (line.startsWith("#")) continue;
QList<QByteArray> list = line.split(' ');
if (list.size() != 2) continue;
QString pluginName = QString(list.first());
if (!pluginName.endsWith(":")) continue;
pluginName.chop(1); // remove trailing ':'
bool ok;
qreal version = list.last().toFloat(&ok);
qDebug("read line %s: %.2f", qPrintable(pluginName), version);
if (!ok) continue;
dataCorrect = true;
if (isUpdateNeeded(pluginName, version)) {
qDebug("Plugin: %s is outdated", qPrintable(pluginName));
updateInfo[pluginName] = version;
}
}
if (!dataCorrect)
emit checkForUpdatesFailed(tr("An incorrect update info received."));
else
emit checkForUpdatesFinished(updateInfo);
}
bool SearchEngine::isUpdateNeeded(QString pluginName, qreal newVersion) const
{
PluginInfo *plugin = pluginInfo(pluginName);
if (!plugin) return true;
qreal oldVersion = plugin->version;
qDebug("IsUpdate needed? to be installed: %.2f, already installed: %.2f", newVersion, oldVersion);
return (newVersion > oldVersion);
}
QString SearchEngine::pluginPath(const QString &name)
{
return QString("%1/%2.py").arg(pluginsLocation()).arg(name);
}
QHash<QString, QString> SearchEngine::initializeCategoryNames()
{
QHash<QString, QString> result;
result["all"] = QT_TRANSLATE_NOOP("SearchEngine", "All categories");
result["movies"] = QT_TRANSLATE_NOOP("SearchEngine", "Movies");
result["tv"] = QT_TRANSLATE_NOOP("SearchEngine", "TV shows");
result["music"] = QT_TRANSLATE_NOOP("SearchEngine", "Music");
result["games"] = QT_TRANSLATE_NOOP("SearchEngine", "Games");
result["anime"] = QT_TRANSLATE_NOOP("SearchEngine", "Anime");
result["software"] = QT_TRANSLATE_NOOP("SearchEngine", "Software");
result["pictures"] = QT_TRANSLATE_NOOP("SearchEngine", "Pictures");
result["books"] = QT_TRANSLATE_NOOP("SearchEngine", "Books");
return result;
}
qreal SearchEngine::getPluginVersion(QString filePath)
{
QFile plugin(filePath);
if (!plugin.exists()) {
qDebug("%s plugin does not exist, returning 0.0", qPrintable(filePath));
return 0.0;
}
if (!plugin.open(QIODevice::ReadOnly | QIODevice::Text))
return 0.0;
qreal version = 0.0;
while (!plugin.atEnd()) {
QByteArray line = plugin.readLine();
if (line.startsWith("#VERSION: ")) {
line = line.split(' ').last().trimmed();
version = line.toFloat();
qDebug("plugin %s version: %.2f", qPrintable(filePath), version);
break;
}
}
return version;
}

136
src/base/searchengine.h Normal file
View file

@ -0,0 +1,136 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* 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.
*/
#ifndef SEARCHENGINE_H
#define SEARCHENGINE_H
#include <QHash>
#include <QStringList>
#include <QProcess>
#include <QList>
class QTimer;
struct PluginInfo
{
QString name;
qreal version;
QString fullName;
QString url;
QStringList supportedCategories;
QString iconPath;
bool enabled;
};
struct SearchResult
{
QString fileName;
QString fileUrl;
qlonglong fileSize;
qlonglong nbSeeders;
qlonglong nbLeechers;
QString siteUrl;
QString descrLink;
};
class SearchEngine: public QObject
{
Q_OBJECT
public:
SearchEngine();
~SearchEngine();
QStringList allPlugins() const;
QStringList enabledPlugins() const;
QStringList supportedCategories() const;
PluginInfo *pluginInfo(const QString &name) const;
bool isActive() const;
void enablePlugin(const QString &name, bool enabled = true);
void updatePlugin(const QString &name);
void installPlugin(const QString &source);
bool uninstallPlugin(const QString &name);
void checkForUpdates();
void startSearch(const QString &pattern, const QString &category, const QStringList &usedPlugins);
void cancelSearch();
static qreal getPluginVersion(QString filePath);
static QString categoryFullName(const QString &categoryName);
static QString pluginsLocation();
signals:
void searchStarted();
void searchFinished(bool cancelled);
void searchFailed();
void newSearchResults(const QList<SearchResult> &results);
void pluginInstalled(const QString &name);
void pluginInstallationFailed(const QString &name, const QString &reason);
void pluginUpdated(const QString &name);
void pluginUpdateFailed(const QString &name, const QString &reason);
void checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo);
void checkForUpdatesFailed(const QString &reason);
private slots:
void onTimeout();
void readSearchOutput();
void processFinished(int exitcode, QProcess::ExitStatus);
void versionInfoDownloaded(const QString &url, const QByteArray &data);
void versionInfoDownloadFailed(const QString &url, const QString &reason);
void pluginDownloaded(const QString &url, QString filePath);
void pluginDownloadFailed(const QString &url, const QString &reason);
private:
void update();
void updateNova();
bool parseSearchResult(const QString &line, SearchResult &searchResult);
void parseVersionInfo(const QByteArray &info);
void installPlugin_impl(const QString &name, const QString &path);
bool isUpdateNeeded(QString pluginName, qreal newVersion) const;
static QString engineLocation();
static QString pluginPath(const QString &name);
static QHash<QString, QString> initializeCategoryNames();
static const QHash<QString, QString> m_categoryNames;
const QString m_updateUrl;
QHash<QString, PluginInfo*> m_plugins;
QProcess *m_searchProcess;
bool m_searchStopped;
QTimer *m_searchTimeout;
QByteArray m_searchResultLineTruncated;
};
#endif // SEARCHENGINE_H

View file

@ -509,18 +509,6 @@ QString Utils::Fs::QDesktopServicesDownloadLocation()
#endif
}
QString Utils::Fs::searchEngineLocation()
{
QString folder = "nova";
if (Utils::Misc::pythonVersion() >= 3)
folder = "nova3";
const QString location = expandPathAbs(QDesktopServicesDataLocation() + folder);
QDir locationDir(location);
if (!locationDir.exists())
locationDir.mkpath(locationDir.absolutePath());
return location;
}
QString Utils::Fs::cacheLocation()
{
QString location = expandPathAbs(QDesktopServicesCacheLocation());

View file

@ -65,7 +65,7 @@ namespace Utils
QString QDesktopServicesCacheLocation();
QString QDesktopServicesDownloadLocation();
/* End of Qt4 code */
QString searchEngineLocation();
QString cacheLocation();
}
}

View file

@ -137,6 +137,10 @@ void AddNewTorrentDialog::show(QString source, QWidget *parent)
qDebug("Converting bc link to magnet link");
source = Utils::Misc::bcLinkToMagnet(source);
}
else if (((source.size() == 40) && !source.contains(QRegExp("[^0-9A-Fa-f]")))
|| ((source.size() == 32) && !source.contains(QRegExp("[^2-7A-Za-z]")))) {
source = "magnet:?xt=urn:btih:" + source;
}
AddNewTorrentDialog *dlg = new AddNewTorrentDialog(parent);

View file

@ -44,10 +44,9 @@ HEADERS += \
$$PWD/torrentcreatordlg.h \
$$PWD/search/searchwidget.h \
$$PWD/search/searchtab.h \
$$PWD/search/engineselectdlg.h \
$$PWD/search/pluginselectdlg.h \
$$PWD/search/pluginsourcedlg.h \
$$PWD/search/searchlistdelegate.h \
$$PWD/search/supportedengines.h \
$$PWD/search/searchsortmodel.h
SOURCES += \
@ -82,7 +81,7 @@ SOURCES += \
$$PWD/torrentcreatordlg.cpp \
$$PWD/search/searchwidget.cpp \
$$PWD/search/searchtab.cpp \
$$PWD/search/engineselectdlg.cpp
$$PWD/search/pluginselectdlg.cpp
win32|macx {
HEADERS += $$PWD/programupdater.h
@ -106,7 +105,7 @@ FORMS += \
$$PWD/options.ui \
$$PWD/torrentcreatordlg.ui \
$$PWD/search/searchwidget.ui \
$$PWD/search/engineselectdlg.ui \
$$PWD/search/pluginselectdlg.ui \
$$PWD/search/pluginsourcedlg.ui
RESOURCES += $$PWD/about.qrc

View file

@ -34,6 +34,7 @@
#include "notifications.h"
#endif
#include <QDebug>
#include <QFileDialog>
#include <QFileSystemWatcher>
#include <QMessageBox>
@ -50,6 +51,7 @@
#include "mainwindow.h"
#include "transferlistwidget.h"
#include "base/utils/misc.h"
#include "base/utils/fs.h"
#include "torrentcreatordlg.h"
#include "downloadfromurldlg.h"
#include "addnewtorrentdialog.h"

View file

@ -1,519 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#include "engineselectdlg.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "ico.h"
#include "searchwidget.h"
#include "pluginsourcedlg.h"
#include "guiiconprovider.h"
#include "autoexpandabledialog.h"
#include <QProcess>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QDropEvent>
#include <QTemporaryFile>
#include <QMimeData>
#include <QClipboard>
#ifdef QBT_USES_QT5
#include <QTableView>
#endif
enum EngineColumns {ENGINE_NAME, ENGINE_VERSION, ENGINE_URL, ENGINE_STATE, ENGINE_ID};
EngineSelectDlg::EngineSelectDlg(QWidget *parent, SupportedEngines *supported_engines)
: QDialog(parent)
, supported_engines(supported_engines)
, m_updateUrl(QString("https://raw.github.com/qbittorrent/qBittorrent/master/src/searchengine/") + (Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova") + "/engines/")
{
setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
#ifdef QBT_USES_QT5
// This hack fixes reordering of first column with Qt5.
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
QTableView unused;
unused.setVerticalHeader(pluginsTree->header());
pluginsTree->header()->setParent(pluginsTree);
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
#endif
pluginsTree->setRootIsDecorated(false);
pluginsTree->header()->resizeSection(0, 160);
pluginsTree->header()->resizeSection(1, 80);
pluginsTree->header()->resizeSection(2, 200);
pluginsTree->hideColumn(ENGINE_ID);
actionUninstall->setIcon(GuiIconProvider::instance()->getIcon("list-remove"));
connect(actionEnable, SIGNAL(toggled(bool)), this, SLOT(enableSelection(bool)));
connect(pluginsTree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayContextMenu(const QPoint&)));
loadSupportedSearchEngines();
connect(supported_engines, SIGNAL(newSupportedEngine(QString)), this, SLOT(addNewEngine(QString)));
connect(pluginsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(toggleEngineState(QTreeWidgetItem*, int)));
show();
}
EngineSelectDlg::~EngineSelectDlg() {
qDebug("Destroying engineSelectDlg");
emit enginesChanged();
qDebug("Engine plugins dialog destroyed");
}
void EngineSelectDlg::dropEvent(QDropEvent *event) {
event->acceptProposedAction();
QStringList files;
if (event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls();
foreach (const QUrl &url, urls) {
if (!url.isEmpty()) {
if (url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
else
files << url.toString();
}
}
}
else {
files = event->mimeData()->text().split(QString::fromUtf8("\n"));
}
foreach (QString file, files) {
qDebug("dropped %s", qPrintable(file));
if (Utils::Misc::isUrl(file)) {
setCursor(QCursor(Qt::WaitCursor));
downloadFromUrl(file);
continue;
}
if (file.endsWith(".py", Qt::CaseInsensitive)) {
if (file.startsWith("file:", Qt::CaseInsensitive))
file = QUrl(file).toLocalFile();
QString plugin_name = Utils::Fs::fileName(file);
plugin_name.chop(3); // Remove extension
installPlugin(file, plugin_name);
}
}
}
// Decode if we accept drag 'n drop or not
void EngineSelectDlg::dragEnterEvent(QDragEnterEvent *event) {
QString mime;
foreach (mime, event->mimeData()->formats()) {
qDebug("mimeData: %s", qPrintable(mime));
}
if (event->mimeData()->hasFormat(QString::fromUtf8("text/plain")) || event->mimeData()->hasFormat(QString::fromUtf8("text/uri-list"))) {
event->acceptProposedAction();
}
}
void EngineSelectDlg::on_updateButton_clicked() {
// Download version file from update server on sourceforge
setCursor(QCursor(Qt::WaitCursor));
downloadFromUrl(m_updateUrl + "versions.txt");
}
void EngineSelectDlg::toggleEngineState(QTreeWidgetItem *item, int) {
SupportedEngine *engine = supported_engines->value(item->text(ENGINE_ID));
engine->setEnabled(!engine->isEnabled());
if (engine->isEnabled()) {
item->setText(ENGINE_STATE, tr("Yes"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "green");
} else {
item->setText(ENGINE_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
}
void EngineSelectDlg::displayContextMenu(const QPoint&) {
QMenu myContextMenu(this);
// Enable/disable pause/start action given the DL state
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
if (items.isEmpty()) return;
QString first_id = items.first()->text(ENGINE_ID);
actionEnable->setChecked(supported_engines->value(first_id)->isEnabled());
myContextMenu.addAction(actionEnable);
myContextMenu.addSeparator();
myContextMenu.addAction(actionUninstall);
myContextMenu.exec(QCursor::pos());
}
void EngineSelectDlg::on_closeButton_clicked() {
close();
}
void EngineSelectDlg::on_actionUninstall_triggered() {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
bool error = false;
foreach (item, items) {
int index = pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(ENGINE_ID);
if (QFile::exists(":/nova/engines/"+id+".py")) {
error = true;
// Disable it instead
supported_engines->value(id)->setEnabled(false);
item->setText(ENGINE_STATE, tr("No"));
setRowColor(index, "red");
continue;
}else {
// Proceed with uninstall
// remove it from hard drive
QDir enginesFolder(Utils::Fs::searchEngineLocation() + "/engines");
QStringList filters;
filters << id+".*";
QStringList files = enginesFolder.entryList(filters, QDir::Files, QDir::Unsorted);
QString file;
foreach (file, files) {
enginesFolder.remove(file);
}
// Remove it from supported engines
delete supported_engines->take(id);
delete item;
}
}
if (error)
QMessageBox::warning(0, tr("Uninstall warning"), tr("Some plugins could not be uninstalled because they are included in qBittorrent. Only the ones you added yourself can be uninstalled.\nThose plugins were disabled."));
else
QMessageBox::information(0, tr("Uninstall success"), tr("All selected plugins were uninstalled successfully"));
}
void EngineSelectDlg::enableSelection(bool enable) {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
foreach (item, items) {
int index = pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(ENGINE_ID);
supported_engines->value(id)->setEnabled(enable);
if (enable) {
item->setText(ENGINE_STATE, tr("Yes"));
setRowColor(index, "green");
} else {
item->setText(ENGINE_STATE, tr("No"));
setRowColor(index, "red");
}
}
}
// Set the color of a row in data model
void EngineSelectDlg::setRowColor(int row, QString color) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(row);
for (int i=0; i<pluginsTree->columnCount(); ++i) {
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
}
}
QList<QTreeWidgetItem*> EngineSelectDlg::findItemsWithUrl(QString url) {
QList<QTreeWidgetItem*> res;
for (int i=0; i<pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (url.startsWith(item->text(ENGINE_URL), Qt::CaseInsensitive))
res << item;
}
return res;
}
QTreeWidgetItem* EngineSelectDlg::findItemWithID(QString id) {
QList<QTreeWidgetItem*> res;
for (int i=0; i<pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (id == item->text(ENGINE_ID))
return item;
}
return 0;
}
bool EngineSelectDlg::isUpdateNeeded(QString plugin_name, qreal new_version) const {
qreal old_version = SearchWidget::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + plugin_name + ".py");
qDebug("IsUpdate needed? tobeinstalled: %.2f, alreadyinstalled: %.2f", new_version, old_version);
return (new_version > old_version);
}
void EngineSelectDlg::installPlugin(QString path, QString plugin_name) {
qDebug("Asked to install plugin at %s", qPrintable(path));
qreal new_version = SearchWidget::getPluginVersion(path);
if (new_version == 0.0) {
QMessageBox::warning(this, tr("Invalid plugin"), tr("The search engine plugin is invalid, please contact the author."));
return;
}
qDebug("Version to be installed: %.2f", new_version);
if (!isUpdateNeeded(plugin_name, new_version)) {
qDebug("Apparently update is not needed, we have a more recent version");
QMessageBox::information(this, tr("Search plugin install"), tr("A more recent version of '%1' search engine plugin is already installed.", "%1 is the name of the search engine").arg(plugin_name));
return;
}
// Process with install
QString dest_path = Utils::Fs::searchEngineLocation() + "/engines/" + plugin_name + ".py";
bool update = false;
if (QFile::exists(dest_path)) {
// Backup in case install fails
QFile::copy(dest_path, dest_path+".bak");
Utils::Fs::forceRemove(dest_path);
Utils::Fs::forceRemove(dest_path+"c");
update = true;
}
// Copy the plugin
QFile::copy(path, dest_path);
// Update supported plugins
supported_engines->update();
// Check if this was correctly installed
if (!supported_engines->contains(plugin_name)) {
if (update) {
// Remove broken file
Utils::Fs::forceRemove(dest_path);
// restore backup
QFile::copy(dest_path+".bak", dest_path);
Utils::Fs::forceRemove(dest_path+".bak");
QMessageBox::warning(this, tr("Search plugin install"), tr("'%1' search engine plugin could not be updated, keeping old version.", "%1 is the name of the search engine").arg(plugin_name));
return;
} else {
// Remove broken file
Utils::Fs::forceRemove(dest_path);
QMessageBox::warning(this, tr("Search plugin install"), tr("'%1' search engine plugin could not be installed.", "%1 is the name of the search engine").arg(plugin_name));
return;
}
}
// Install was successful, remove backup and update plugin version
if (update) {
Utils::Fs::forceRemove(dest_path+".bak");
qreal version = SearchWidget::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + plugin_name + ".py");
QTreeWidgetItem *item = findItemWithID(plugin_name);
item->setText(ENGINE_VERSION, QString::number(version, 'f', 2));
QMessageBox::information(this, tr("Search plugin install"), tr("'%1' search engine plugin was successfully updated.", "%1 is the name of the search engine").arg(plugin_name));
return;
} else {
QMessageBox::information(this, tr("Search plugin install"), tr("'%1' search engine plugin was successfully installed.", "%1 is the name of the search engine").arg(plugin_name));
return;
}
}
void EngineSelectDlg::loadSupportedSearchEngines() {
// Some clean up first
pluginsTree->clear();
foreach (QString name, supported_engines->keys()) {
addNewEngine(name);
}
}
void EngineSelectDlg::addNewEngine(QString engine_name) {
QTreeWidgetItem *item = new QTreeWidgetItem(pluginsTree);
SupportedEngine *engine = supported_engines->value(engine_name);
item->setText(ENGINE_NAME, engine->getFullName());
item->setText(ENGINE_URL, engine->getUrl());
item->setText(ENGINE_ID, engine->getName());
if (engine->isEnabled()) {
item->setText(ENGINE_STATE, tr("Yes"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "green");
} else {
item->setText(ENGINE_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
// Handle icon
QString iconPath = Utils::Fs::searchEngineLocation() + "/engines/" + engine->getName() + ".png";
if (QFile::exists(iconPath)) {
// Good, we already have the icon
item->setData(ENGINE_NAME, Qt::DecorationRole, QVariant(QIcon(iconPath)));
} else {
iconPath = Utils::Fs::searchEngineLocation() + "/engines/" + engine->getName() + ".ico";
if (QFile::exists(iconPath)) { // ICO support
item->setData(ENGINE_NAME, Qt::DecorationRole, QVariant(QIcon(iconPath)));
} else {
// Icon is missing, we must download it
downloadFromUrl(engine->getUrl() + "/favicon.ico");
}
}
// Load version
qreal version = SearchWidget::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + engine->getName() + ".py");
item->setText(ENGINE_VERSION, QString::number(version, 'f', 2));
}
void EngineSelectDlg::on_installButton_clicked() {
PluginSourceDlg *dlg = new PluginSourceDlg(this);
connect(dlg, SIGNAL(askForLocalFile()), this, SLOT(askForLocalPlugin()));
connect(dlg, SIGNAL(askForUrl()), this, SLOT(askForPluginUrl()));
}
void EngineSelectDlg::askForPluginUrl() {
bool ok(false);
QString clipTxt = qApp->clipboard()->text();
QString defaultUrl = "http://";
if ((clipTxt.startsWith("http://", Qt::CaseInsensitive)
|| clipTxt.startsWith("https://", Qt::CaseInsensitive)
|| clipTxt.startsWith("ftp://", Qt::CaseInsensitive))
&& clipTxt.endsWith(".py"))
defaultUrl = clipTxt;
QString url = AutoExpandableDialog::getText(this, tr("New search engine plugin URL"),
tr("URL:"), QLineEdit::Normal,
defaultUrl, &ok);
while(true) {
if (!ok || url.isEmpty())
return;
if (!url.endsWith(".py")) {
QMessageBox::warning(this, tr("Invalid link"), tr("The link doesn't seem to point to a search engine plugin."));
url = AutoExpandableDialog::getText(this, tr("New search engine plugin URL"),
tr("URL:"), QLineEdit::Normal,
url, &ok);
}
else
break;
}
setCursor(QCursor(Qt::WaitCursor));
downloadFromUrl(url);
}
void EngineSelectDlg::askForLocalPlugin() {
QStringList pathsList = QFileDialog::getOpenFileNames(0,
tr("Select search plugins"), QDir::homePath(),
tr("qBittorrent search plugin")+QString::fromUtf8(" (*.py)"));
foreach (QString path, pathsList) {
if (path.endsWith(".py", Qt::CaseInsensitive)) {
QString plugin_name = Utils::Fs::fileName(path);
plugin_name.chop(3); // Remove extension
installPlugin(path, plugin_name);
}
}
}
bool EngineSelectDlg::parseVersionsFile(QString versions_file) {
qDebug("Checking if update is needed");
bool file_correct = false;
QFile versions(versions_file);
if (!versions.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("* Error: Could not read versions.txt file");
return false;
}
bool updated = false;
while(!versions.atEnd()) {
QByteArray line = versions.readLine();
line.replace("\n", "");
line = line.trimmed();
if (line.isEmpty()) continue;
if (line.startsWith("#")) continue;
QList<QByteArray> list = line.split(' ');
if (list.size() != 2) continue;
QString plugin_name = QString(list.first());
if (!plugin_name.endsWith(":")) continue;
plugin_name.chop(1); // remove trailing ':'
bool ok;
qreal version = list.last().toFloat(&ok);
qDebug("read line %s: %.2f", qPrintable(plugin_name), version);
if (!ok) continue;
file_correct = true;
if (isUpdateNeeded(plugin_name, version)) {
qDebug("Plugin: %s is outdated", qPrintable(plugin_name));
// Downloading update
setCursor(QCursor(Qt::WaitCursor));
downloadFromUrl(m_updateUrl + plugin_name + ".py");
//downloadFromUrl(m_updateUrl + plugin_name + ".png");
updated = true;
}else {
qDebug("Plugin: %s is up to date", qPrintable(plugin_name));
}
}
// Close file
versions.close();
// Clean up tmp file
Utils::Fs::forceRemove(versions_file);
if (file_correct && !updated) {
QMessageBox::information(this, tr("Search plugin update"), tr("All your plugins are already up to date."));
}
return file_correct;
}
void EngineSelectDlg::downloadFromUrl(const QString &url)
{
Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(url, true);
connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(processDownloadedFile(QString, QString)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(handleDownloadFailure(QString, QString)));
}
void EngineSelectDlg::processDownloadedFile(const QString &url, QString filePath) {
filePath = Utils::Fs::fromNativePath(filePath);
setCursor(QCursor(Qt::ArrowCursor));
qDebug("engineSelectDlg received %s", qPrintable(url));
if (url.endsWith("favicon.ico", Qt::CaseInsensitive)) {
// Icon downloaded
QImage fileIcon;
if (fileIcon.load(filePath)) {
QList<QTreeWidgetItem*> items = findItemsWithUrl(url);
QTreeWidgetItem *item;
foreach (item, items) {
QString id = item->text(ENGINE_ID);
QString iconPath;
QFile icon(filePath);
icon.open(QIODevice::ReadOnly);
if (ICOHandler::canRead(&icon))
iconPath = Utils::Fs::searchEngineLocation() + "/engines/" + id + ".ico";
else
iconPath = Utils::Fs::searchEngineLocation() + "/engines/" + id + ".png";
QFile::copy(filePath, iconPath);
item->setData(ENGINE_NAME, Qt::DecorationRole, QVariant(QIcon(iconPath)));
}
}
// Delete tmp file
Utils::Fs::forceRemove(filePath);
return;
}
if (url.endsWith("versions.txt")) {
if (!parseVersionsFile(filePath)) {
QMessageBox::warning(this, tr("Search plugin update"), tr("Sorry, update server is temporarily unavailable."));
}
Utils::Fs::forceRemove(filePath);
return;
}
if (url.endsWith(".py", Qt::CaseInsensitive)) {
QString plugin_name = Utils::Fs::fileName(url);
plugin_name.chop(3); // Remove extension
installPlugin(filePath, plugin_name);
Utils::Fs::forceRemove(filePath);
return;
}
}
void EngineSelectDlg::handleDownloadFailure(const QString &url, const QString &reason) {
setCursor(QCursor(Qt::ArrowCursor));
if (url.endsWith("favicon.ico", Qt::CaseInsensitive)) {
qDebug("Could not download favicon: %s, reason: %s", qPrintable(url), qPrintable(reason));
return;
}
if (url.endsWith("versions.txt")) {
QMessageBox::warning(this, tr("Search plugin update"), tr("Sorry, update server is temporarily unavailable."));
return;
}
if (url.endsWith(".py", Qt::CaseInsensitive)) {
// a plugin update download has been failed
QString plugin_name = url.split('/').last();
plugin_name.replace(".py", "", Qt::CaseInsensitive);
QMessageBox::warning(this, tr("Search plugin update"), tr("Sorry, '%1' search plugin installation failed.", "%1 is the name of the search engine").arg(plugin_name));
}
}

View file

@ -0,0 +1,408 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#include "pluginselectdlg.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/net/downloadmanager.h"
#include "base/net/downloadhandler.h"
#include "base/searchengine.h"
#include "ico.h"
#include "searchwidget.h"
#include "pluginsourcedlg.h"
#include "guiiconprovider.h"
#include "autoexpandabledialog.h"
#include <QProcess>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QDropEvent>
#include <QTemporaryFile>
#include <QMimeData>
#include <QClipboard>
#ifdef QBT_USES_QT5
#include <QTableView>
#endif
enum PluginColumns {PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_URL, PLUGIN_STATE, PLUGIN_ID};
PluginSelectDlg::PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent)
: QDialog(parent)
, m_pluginManager(pluginManager)
, m_asyncOps(0)
{
setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
#ifdef QBT_USES_QT5
// This hack fixes reordering of first column with Qt5.
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
QTableView unused;
unused.setVerticalHeader(pluginsTree->header());
pluginsTree->header()->setParent(pluginsTree);
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
#endif
pluginsTree->setRootIsDecorated(false);
pluginsTree->header()->resizeSection(0, 160);
pluginsTree->header()->resizeSection(1, 80);
pluginsTree->header()->resizeSection(2, 200);
pluginsTree->hideColumn(PLUGIN_ID);
actionUninstall->setIcon(GuiIconProvider::instance()->getIcon("list-remove"));
connect(actionEnable, SIGNAL(toggled(bool)), this, SLOT(enableSelection(bool)));
connect(pluginsTree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayContextMenu(const QPoint&)));
connect(pluginsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(togglePluginState(QTreeWidgetItem*, int)));
loadSupportedSearchPlugins();
connect(m_pluginManager, SIGNAL(pluginInstalled(QString)), SLOT(pluginInstalled(QString)));
connect(m_pluginManager, SIGNAL(pluginInstallationFailed(QString, QString)), SLOT(pluginInstallationFailed(QString, QString)));
connect(m_pluginManager, SIGNAL(pluginUpdated(QString)), SLOT(pluginUpdated(QString)));
connect(m_pluginManager, SIGNAL(pluginUpdateFailed(QString, QString)), SLOT(pluginUpdateFailed(QString, QString)));
connect(m_pluginManager, SIGNAL(checkForUpdatesFinished(QHash<QString, qreal>)), SLOT(checkForUpdatesFinished(QHash<QString, qreal>)));
connect(m_pluginManager, SIGNAL(checkForUpdatesFailed(QString)), SLOT(checkForUpdatesFailed(QString)));
show();
}
PluginSelectDlg::~PluginSelectDlg() {
emit pluginsChanged();
}
void PluginSelectDlg::dropEvent(QDropEvent *event) {
event->acceptProposedAction();
QStringList files;
if (event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls();
foreach (const QUrl &url, urls) {
if (!url.isEmpty()) {
if (url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
else
files << url.toString();
}
}
}
else {
files = event->mimeData()->text().split(QString::fromUtf8("\n"));
}
if (files.isEmpty()) return;
foreach (QString file, files) {
qDebug("dropped %s", qPrintable(file));
startAsyncOp();
m_pluginManager->installPlugin(file);
}
}
// Decode if we accept drag 'n drop or not
void PluginSelectDlg::dragEnterEvent(QDragEnterEvent *event) {
QString mime;
foreach (mime, event->mimeData()->formats()) {
qDebug("mimeData: %s", qPrintable(mime));
}
if (event->mimeData()->hasFormat(QString::fromUtf8("text/plain")) || event->mimeData()->hasFormat(QString::fromUtf8("text/uri-list"))) {
event->acceptProposedAction();
}
}
void PluginSelectDlg::on_updateButton_clicked() {
startAsyncOp();
m_pluginManager->checkForUpdates();
}
void PluginSelectDlg::togglePluginState(QTreeWidgetItem *item, int) {
PluginInfo *plugin = m_pluginManager->pluginInfo(item->text(PLUGIN_ID));
m_pluginManager->enablePlugin(plugin->name, !plugin->enabled);
if (plugin->enabled) {
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "green");
} else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
}
void PluginSelectDlg::displayContextMenu(const QPoint&) {
QMenu myContextMenu(this);
// Enable/disable pause/start action given the DL state
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
if (items.isEmpty()) return;
QString first_id = items.first()->text(PLUGIN_ID);
actionEnable->setChecked(m_pluginManager->pluginInfo(first_id)->enabled);
myContextMenu.addAction(actionEnable);
myContextMenu.addSeparator();
myContextMenu.addAction(actionUninstall);
myContextMenu.exec(QCursor::pos());
}
void PluginSelectDlg::on_closeButton_clicked() {
close();
}
void PluginSelectDlg::on_actionUninstall_triggered() {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
bool error = false;
foreach (item, items) {
int index = pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(PLUGIN_ID);
if (m_pluginManager->uninstallPlugin(id)) {
delete item;
}
else {
error = true;
// Disable it instead
m_pluginManager->enablePlugin(id, false);
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(index, "red");
continue;
}
}
if (error)
QMessageBox::warning(0, tr("Uninstall warning"), tr("Some plugins could not be uninstalled because they are included in qBittorrent. Only the ones you added yourself can be uninstalled.\nThose plugins were disabled."));
else
QMessageBox::information(0, tr("Uninstall success"), tr("All selected plugins were uninstalled successfully"));
}
void PluginSelectDlg::enableSelection(bool enable) {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
foreach (item, items) {
int index = pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(PLUGIN_ID);
m_pluginManager->enablePlugin(id, enable);
if (enable) {
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(index, "green");
} else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(index, "red");
}
}
}
// Set the color of a row in data model
void PluginSelectDlg::setRowColor(int row, QString color) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(row);
for (int i=0; i<pluginsTree->columnCount(); ++i) {
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
}
}
QList<QTreeWidgetItem*> PluginSelectDlg::findItemsWithUrl(QString url) {
QList<QTreeWidgetItem*> res;
for (int i=0; i<pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (url.startsWith(item->text(PLUGIN_URL), Qt::CaseInsensitive))
res << item;
}
return res;
}
QTreeWidgetItem* PluginSelectDlg::findItemWithID(QString id) {
for (int i=0; i<pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (id == item->text(PLUGIN_ID))
return item;
}
return 0;
}
void PluginSelectDlg::loadSupportedSearchPlugins() {
// Some clean up first
pluginsTree->clear();
foreach (QString name, m_pluginManager->allPlugins())
addNewPlugin(name);
}
void PluginSelectDlg::addNewPlugin(QString engine_name) {
QTreeWidgetItem *item = new QTreeWidgetItem(pluginsTree);
PluginInfo *plugin = m_pluginManager->pluginInfo(engine_name);
item->setText(PLUGIN_NAME, plugin->fullName);
item->setText(PLUGIN_URL, plugin->url);
item->setText(PLUGIN_ID, plugin->name);
if (plugin->enabled) {
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "green");
} else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
// Handle icon
if (QFile::exists(plugin->iconPath)) {
// Good, we already have the icon
item->setData(PLUGIN_NAME, Qt::DecorationRole, QVariant(QIcon(plugin->iconPath)));
} else {
// Icon is missing, we must download it
Net::DownloadHandler *handler = Net::DownloadManager::instance()->downloadUrl(plugin->url + "/favicon.ico", true);
connect(handler, SIGNAL(downloadFinished(QString, QString)), this, SLOT(iconDownloaded(QString, QString)));
connect(handler, SIGNAL(downloadFailed(QString, QString)), this, SLOT(iconDownloadFailed(QString, QString)));
}
item->setText(PLUGIN_VERSION, QString::number(plugin->version, 'f', 2));
}
void PluginSelectDlg::startAsyncOp()
{
++m_asyncOps;
if (m_asyncOps == 1)
setCursor(QCursor(Qt::WaitCursor));
}
void PluginSelectDlg::finishAsyncOp()
{
--m_asyncOps;
if (m_asyncOps == 0)
setCursor(QCursor(Qt::ArrowCursor));
}
void PluginSelectDlg::on_installButton_clicked() {
PluginSourceDlg *dlg = new PluginSourceDlg(this);
connect(dlg, SIGNAL(askForLocalFile()), this, SLOT(askForLocalPlugin()));
connect(dlg, SIGNAL(askForUrl()), this, SLOT(askForPluginUrl()));
}
void PluginSelectDlg::askForPluginUrl() {
bool ok(false);
QString clipTxt = qApp->clipboard()->text();
QString defaultUrl = "http://";
if (Utils::Misc::isUrl(clipTxt) && clipTxt.endsWith(".py"))
defaultUrl = clipTxt;
QString url = AutoExpandableDialog::getText(this, tr("New search engine plugin URL"),
tr("URL:"), QLineEdit::Normal,
defaultUrl, &ok);
while(true) {
if (!ok || url.isEmpty())
return;
if (!url.endsWith(".py")) {
QMessageBox::warning(this, tr("Invalid link"), tr("The link doesn't seem to point to a search engine plugin."));
url = AutoExpandableDialog::getText(this, tr("New search engine plugin URL"),
tr("URL:"), QLineEdit::Normal,
url, &ok);
}
else
break;
}
startAsyncOp();
m_pluginManager->installPlugin(url);
}
void PluginSelectDlg::askForLocalPlugin() {
QStringList pathsList = QFileDialog::getOpenFileNames(0,
tr("Select search plugins"), QDir::homePath(),
tr("qBittorrent search plugin")+QString::fromUtf8(" (*.py)"));
foreach (QString path, pathsList) {
startAsyncOp();
m_pluginManager->installPlugin(path);
}
}
void PluginSelectDlg::iconDownloaded(const QString &url, QString filePath) {
filePath = Utils::Fs::fromNativePath(filePath);
// Icon downloaded
QImage fileIcon;
if (fileIcon.load(filePath)) {
QList<QTreeWidgetItem*> items = findItemsWithUrl(url);
QTreeWidgetItem *item;
foreach (item, items) {
QString id = item->text(PLUGIN_ID);
PluginInfo *plugin = m_pluginManager->pluginInfo(id);
if (!plugin) continue;
QFile icon(filePath);
icon.open(QIODevice::ReadOnly);
QString iconPath = QString("%1/%2.%3").arg(SearchEngine::pluginsLocation()).arg(id).arg(ICOHandler::canRead(&icon) ? "ico" : "png");
if (QFile::copy(filePath, iconPath))
item->setData(PLUGIN_NAME, Qt::DecorationRole, QVariant(QIcon(iconPath)));
}
}
// Delete tmp file
Utils::Fs::forceRemove(filePath);
}
void PluginSelectDlg::iconDownloadFailed(const QString &url, const QString &reason) {
qDebug("Could not download favicon: %s, reason: %s", qPrintable(url), qPrintable(reason));
}
void PluginSelectDlg::checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo)
{
finishAsyncOp();
if (updateInfo.isEmpty()) {
QMessageBox::information(this, tr("Search plugin update"), tr("All your plugins are already up to date."));
return;
}
foreach (const QString &pluginName, updateInfo.keys()) {
startAsyncOp();
m_pluginManager->updatePlugin(pluginName);
}
}
void PluginSelectDlg::checkForUpdatesFailed(const QString &reason)
{
finishAsyncOp();
QMessageBox::warning(this, tr("Search plugin update"), tr("Sorry, couldn't check for plugin updates. %1").arg(reason));
}
void PluginSelectDlg::pluginInstalled(const QString &name)
{
addNewPlugin(name);
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully installed.", "%1 is the name of the search engine").arg(name));
}
void PluginSelectDlg::pluginInstallationFailed(const QString &name, const QString &reason)
{
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin install"), tr("Couldn't install \"%1\" search engine plugin. %2").arg(name).arg(reason));
}
void PluginSelectDlg::pluginUpdated(const QString &name)
{
finishAsyncOp();
qreal version = m_pluginManager->pluginInfo(name)->version;
QTreeWidgetItem *item = findItemWithID(name);
item->setText(PLUGIN_VERSION, QString::number(version, 'f', 2));
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully updated.", "%1 is the name of the search engine").arg(name));
}
void PluginSelectDlg::pluginUpdateFailed(const QString &name, const QString &reason)
{
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin update"), tr("Couldn't update \"%1\" search engine plugin. %2").arg(name).arg(reason));
}

View file

@ -1,6 +1,7 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -28,57 +29,64 @@
* Contact : chris@qbittorrent.org
*/
#ifndef ENGINE_SELECT_DLG_H
#define ENGINE_SELECT_DLG_H
#ifndef PLUGINSELECTDLG_H
#define PLUGINSELECTDLG_H
#include "ui_engineselectdlg.h"
#include "supportedengines.h"
#include "ui_pluginselectdlg.h"
QT_BEGIN_NAMESPACE
class QDropEvent;
QT_END_NAMESPACE
class EngineSelectDlg : public QDialog, public Ui::EngineSelectDlg
class SearchEngine;
class PluginSelectDlg : public QDialog, public Ui::PluginSelectDlg
{
Q_OBJECT
private:
void downloadFromUrl(const QString &url);
SupportedEngines *supported_engines;
const QString m_updateUrl;
SearchEngine *m_pluginManager;
int m_asyncOps;
public:
EngineSelectDlg(QWidget *parent, SupportedEngines *supported_engines);
~EngineSelectDlg();
PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0);
~PluginSelectDlg();
QList<QTreeWidgetItem*> findItemsWithUrl(QString url);
QTreeWidgetItem* findItemWithID(QString id);
protected:
bool parseVersionsFile(QString versions_file);
bool isUpdateNeeded(QString plugin_name, qreal new_version) const;
signals:
void enginesChanged();
void pluginsChanged();
protected slots:
protected:
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
private slots:
void on_closeButton_clicked();
void loadSupportedSearchEngines();
void addNewEngine(QString engine_name);
void toggleEngineState(QTreeWidgetItem*, int);
void togglePluginState(QTreeWidgetItem*, int);
void setRowColor(int row, QString color);
void processDownloadedFile(const QString &url, QString filePath);
void handleDownloadFailure(const QString &url, const QString &reason);
void displayContextMenu(const QPoint& pos);
void enableSelection(bool enable);
void on_actionUninstall_triggered();
void on_updateButton_clicked();
void on_installButton_clicked();
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void installPlugin(QString plugin_path, QString plugin_name);
void askForLocalPlugin();
void askForPluginUrl();
void iconDownloaded(const QString &url, QString filePath);
void iconDownloadFailed(const QString &url, const QString &reason);
void checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo);
void checkForUpdatesFailed(const QString &reason);
void pluginInstalled(const QString &name);
void pluginInstallationFailed(const QString &name, const QString &reason);
void pluginUpdated(const QString &name);
void pluginUpdateFailed(const QString &name, const QString &reason);
private:
void loadSupportedSearchPlugins();
void addNewPlugin(QString engine_name);
void startAsyncOp();
void finishAsyncOp();
};
#endif
#endif // PLUGINSELECTDLG_H

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EngineSelectDlg</class>
<widget class="QDialog" name="EngineSelectDlg">
<class>PluginSelectDlg</class>
<widget class="QDialog" name="PluginSelectDlg">
<property name="geometry">
<rect>
<x>0</x>
@ -18,7 +18,7 @@
</property>
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="lbl_engines">
<widget class="QLabel" name="lbl_plugins">
<property name="font">
<font>
<weight>75</weight>
@ -27,7 +27,7 @@
</font>
</property>
<property name="text">
<string>Installed search engines:</string>
<string>Installed search plugins:</string>
</property>
</widget>
</item>
@ -73,7 +73,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="getNewEngine_lbl">
<widget class="QLabel" name="getNewPlugin_lbl">
<property name="font">
<font>
<italic>true</italic>

View file

@ -97,10 +97,9 @@ SearchTab::SearchTab(SearchWidget *parent) : QWidget(), parent(parent)
}
void SearchTab::downloadSelectedItem(const QModelIndex& index) {
QString engine_url = proxyModel->data(proxyModel->index(index.row(), SearchSortModel::ENGINE_URL)).toString();
QString torrent_url = proxyModel->data(proxyModel->index(index.row(), SearchSortModel::DL_LINK)).toString();
setRowColor(index.row(), "blue");
parent->downloadTorrent(engine_url, torrent_url);
parent->downloadTorrent(torrent_url);
}
SearchTab::~SearchTab() {

View file

@ -1,6 +1,7 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -43,6 +44,8 @@
#include <QFileDialog>
#include <QDesktopServices>
#include <QClipboard>
#include <QProcess>
#include <QDebug>
#ifdef Q_OS_WIN
#include <stdlib.h>
@ -52,6 +55,7 @@
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/preferences.h"
#include "base/searchengine.h"
#include "searchlistdelegate.h"
#include "mainwindow.h"
#include "addnewtorrentdialog.h"
@ -74,52 +78,44 @@ SearchWidget::SearchWidget(MainWindow* parent)
search_button->setIcon(GuiIconProvider::instance()->getIcon("edit-find"));
download_button->setIcon(GuiIconProvider::instance()->getIcon("download"));
goToDescBtn->setIcon(GuiIconProvider::instance()->getIcon("application-x-mswinurl"));
enginesButton->setIcon(GuiIconProvider::instance()->getIcon("preferences-system-network"));
pluginsButton->setIcon(GuiIconProvider::instance()->getIcon("preferences-system-network"));
copyURLBtn->setIcon(GuiIconProvider::instance()->getIcon("edit-copy"));
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
// Boolean initialization
search_stopped = false;
// Creating Search Process
searchProcess = new QProcess(this);
searchProcess->setEnvironment(QProcess::systemEnvironment());
connect(searchProcess, SIGNAL(started()), this, SLOT(searchStarted()));
connect(searchProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readSearchOutput()));
connect(searchProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(searchFinished(int,QProcess::ExitStatus)));
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int)));
searchTimeout = new QTimer(this);
searchTimeout->setSingleShot(true);
connect(searchTimeout, SIGNAL(timeout()), this, SLOT(on_search_button_clicked()));
// Update nova.py search plugin if necessary
updateNova();
supported_engines = new SupportedEngines();
m_searchEngine = new SearchEngine;
connect(m_searchEngine, SIGNAL(searchStarted()), SLOT(searchStarted()));
connect(m_searchEngine, SIGNAL(newSearchResults(QList<SearchResult>)), SLOT(appendSearchResults(QList<SearchResult>)));
connect(m_searchEngine, SIGNAL(searchFinished(bool)), SLOT(searchFinished(bool)));
connect(m_searchEngine, SIGNAL(searchFailed()), SLOT(searchFailed()));
// Fill in category combobox
fillCatCombobox();
fillEngineComboBox();
fillPluginComboBox();
connect(search_pattern, SIGNAL(textEdited(QString)), this, SLOT(searchTextEdited(QString)));
connect(selectEngine, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(selectMultipleBox(const QString &)));
connect(selectPlugin, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(selectMultipleBox(const QString &)));
}
void SearchWidget::fillCatCombobox()
{
comboCategory->clear();
comboCategory->addItem(full_cat_names["all"], QVariant("all"));
QStringList supported_cat = supported_engines->supportedCategories();
comboCategory->addItem(SearchEngine::categoryFullName("all"), QVariant("all"));
QStringList supported_cat = m_searchEngine->supportedCategories();
foreach (QString cat, supported_cat) {
qDebug("Supported category: %s", qPrintable(cat));
comboCategory->addItem(full_cat_names[cat], QVariant(cat));
comboCategory->addItem(SearchEngine::categoryFullName(cat), QVariant(cat));
}
}
void SearchWidget::fillEngineComboBox()
void SearchWidget::fillPluginComboBox()
{
selectEngine->clear();
selectEngine->addItem(tr("All enabled"), QVariant("enabled"));
selectEngine->addItem(tr("All engines"), QVariant("all"));
foreach (QString engi, supported_engines->enginesEnabled())
selectEngine->addItem(engi, QVariant(engi));
selectEngine->addItem(tr("Multiple..."), QVariant("multi"));
selectPlugin->clear();
selectPlugin->addItem(tr("All enabled"), QVariant("enabled"));
selectPlugin->addItem(tr("All plugins"), QVariant("all"));
foreach (QString name, m_searchEngine->enabledPlugins())
selectPlugin->addItem(name, QVariant(name));
selectPlugin->addItem(tr("Multiple..."), QVariant("multi"));
}
QString SearchWidget::selectedCategory() const
@ -129,26 +125,15 @@ QString SearchWidget::selectedCategory() const
QString SearchWidget::selectedEngine() const
{
return selectEngine->itemData(selectEngine->currentIndex()).toString();
return selectPlugin->itemData(selectPlugin->currentIndex()).toString();
}
SearchWidget::~SearchWidget()
{
qDebug("Search destruction");
searchProcess->kill();
searchProcess->waitForFinished();
foreach (QProcess *downloader, downloaders) {
// Make sure we disconnect the SIGNAL/SLOT first
// To avoid qreal free
downloader->disconnect();
downloader->kill();
downloader->waitForFinished();
delete downloader;
}
delete search_pattern;
delete searchTimeout;
delete searchProcess;
delete supported_engines;
delete m_searchEngine;
}
void SearchWidget::tab_changed(int t)
@ -174,14 +159,14 @@ void SearchWidget::tab_changed(int t)
void SearchWidget::selectMultipleBox(const QString &text)
{
if (text == tr("Multiple...")) on_enginesButton_clicked();
if (text == tr("Multiple...")) on_pluginsButton_clicked();
}
void SearchWidget::on_enginesButton_clicked()
void SearchWidget::on_pluginsButton_clicked()
{
EngineSelectDlg *dlg = new EngineSelectDlg(this, supported_engines);
connect(dlg, SIGNAL(enginesChanged()), this, SLOT(fillCatCombobox()));
connect(dlg, SIGNAL(enginesChanged()), this, SLOT(fillEngineComboBox()));
PluginSelectDlg *dlg = new PluginSelectDlg(m_searchEngine, this);
connect(dlg, SIGNAL(pluginsChanged()), this, SLOT(fillCatCombobox()));
connect(dlg, SIGNAL(pluginsChanged()), this, SLOT(fillPluginComboBox()));
}
void SearchWidget::searchTextEdited(QString)
@ -204,17 +189,8 @@ void SearchWidget::on_search_button_clicked()
return;
}
if (searchProcess->state() != QProcess::NotRunning) {
#ifdef Q_OS_WIN
searchProcess->kill();
#else
searchProcess->terminate();
#endif
search_stopped = true;
if (searchTimeout->isActive())
searchTimeout->stop();
searchProcess->waitForFinished(1000);
if (m_searchEngine->isActive()) {
m_searchEngine->cancelSearch();
if (!newQueryString) {
search_button->setText(tr("Search"));
@ -224,15 +200,13 @@ void SearchWidget::on_search_button_clicked()
newQueryString = false;
// Reload environment variables (proxy)
searchProcess->setEnvironment(QProcess::systemEnvironment());
const QString pattern = search_pattern->text().trimmed();
// No search pattern entered
if (pattern.isEmpty()) {
QMessageBox::critical(0, tr("Empty search pattern"), tr("Please type a search pattern first"));
return;
}
// Tab Addition
currentSearchTab = new SearchTab(this);
activeSearchTab = currentSearchTab;
@ -243,26 +217,23 @@ void SearchWidget::on_search_button_clicked()
tabWidget->addTab(currentSearchTab, tabName);
tabWidget->setCurrentWidget(currentSearchTab);
// Getting checked search engines
QStringList params;
search_stopped = false;
params << Utils::Fs::toNativePath(Utils::Fs::searchEngineLocation() + "/nova2.py");
if (selectedEngine() == "all") params << supported_engines->enginesAll().join(",");
else if (selectedEngine() == "enabled") params << supported_engines->enginesEnabled().join(",");
else if (selectedEngine() == "multi") params << supported_engines->enginesEnabled().join(",");
else params << selectedEngine();
QStringList plugins;
if (selectedEngine() == "all") plugins = m_searchEngine->allPlugins();
else if (selectedEngine() == "enabled") plugins = m_searchEngine->enabledPlugins();
else if (selectedEngine() == "multi") plugins = m_searchEngine->enabledPlugins();
else plugins << selectedEngine();
qDebug("Search with category: %s", qPrintable(selectedCategory()));
params << selectedCategory();
params << pattern.split(" ");
// Update SearchEngine widgets
no_search_results = true;
nb_search_results = 0;
search_result_line_truncated.clear();
// Changing the text of the current label
activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "i.e: Search results").arg(0));
// Launch search
searchProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
searchTimeout->start(180000); // 3min
m_searchEngine->startSearch(pattern, selectedCategory(), plugins);
}
void SearchWidget::saveResultsColumnsWidth()
@ -281,30 +252,12 @@ void SearchWidget::saveResultsColumnsWidth()
pref->setSearchColsWidth(new_width_list.join(" "));
}
void SearchWidget::downloadTorrent(QString engine_url, QString torrent_url)
void SearchWidget::downloadTorrent(QString url)
{
if (torrent_url.startsWith("bc://bt/", Qt::CaseInsensitive)) {
qDebug("Converting bc link to magnet link");
torrent_url = Utils::Misc::bcLinkToMagnet(torrent_url);
}
qDebug() << Q_FUNC_INFO << torrent_url;
if (torrent_url.startsWith("magnet:")) {
QStringList urls;
urls << torrent_url;
mp_mainWindow->downloadFromURLList(urls);
}
else {
QProcess *downloadProcess = new QProcess(this);
downloadProcess->setEnvironment(QProcess::systemEnvironment());
connect(downloadProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(downloadFinished(int,QProcess::ExitStatus)));
downloaders << downloadProcess;
QStringList params;
params << Utils::Fs::toNativePath(Utils::Fs::searchEngineLocation() + "/nova2dl.py");
params << engine_url;
params << torrent_url;
// Launch search
downloadProcess->start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
}
if (Preferences::instance()->useAdditionDialog())
AddNewTorrentDialog::show(url, this);
else
BitTorrent::Session::instance()->addTorrent(url);
}
void SearchWidget::searchStarted()
@ -316,220 +269,75 @@ void SearchWidget::searchStarted()
search_button->setText(tr("Stop"));
}
// search Qprocess return output as soon as it gets new
// stuff to read. We split it into lines and add each
// line to search results calling appendSearchResult().
void SearchWidget::readSearchOutput()
{
QByteArray output = searchProcess->readAllStandardOutput();
output.replace("\r", "");
QList<QByteArray> lines_list = output.split('\n');
if (!search_result_line_truncated.isEmpty()) {
QByteArray end_of_line = lines_list.takeFirst();
lines_list.prepend(search_result_line_truncated + end_of_line);
}
search_result_line_truncated = lines_list.takeLast().trimmed();
foreach (const QByteArray &line, lines_list)
appendSearchResult(QString::fromUtf8(line));
activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "i.e: Search results").arg(nb_search_results));
}
void SearchWidget::downloadFinished(int exitcode, QProcess::ExitStatus)
{
QProcess *downloadProcess = (QProcess*)sender();
if (exitcode == 0) {
QString line = QString::fromUtf8(downloadProcess->readAllStandardOutput()).trimmed();
QStringList parts = line.split(' ');
if (parts.size() == 2) {
QString path = parts[0];
if (Preferences::instance()->useAdditionDialog())
AddNewTorrentDialog::show(path, mp_mainWindow);
else
BitTorrent::Session::instance()->addTorrent(path);
}
}
qDebug("Deleting downloadProcess");
downloaders.removeOne(downloadProcess);
delete downloadProcess;
}
static inline void removePythonScriptIfExists(const QString& script_path)
{
Utils::Fs::forceRemove(script_path);
Utils::Fs::forceRemove(script_path + "c");
}
// Update nova.py search plugin if necessary
void SearchWidget::updateNova()
{
qDebug("Updating nova");
// create nova directory if necessary
QDir search_dir(Utils::Fs::searchEngineLocation());
QString nova_folder = Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova";
QFile package_file(search_dir.absoluteFilePath("__init__.py"));
package_file.open(QIODevice::WriteOnly | QIODevice::Text);
package_file.close();
if (!search_dir.exists("engines"))
search_dir.mkdir("engines");
Utils::Fs::removeDirRecursive(search_dir.absoluteFilePath("__pycache__"));
QFile package_file2(search_dir.absolutePath() + "/engines/__init__.py");
package_file2.open(QIODevice::WriteOnly | QIODevice::Text);
package_file2.close();
// Copy search plugin files (if necessary)
QString filePath = search_dir.absoluteFilePath("nova2.py");
if (getPluginVersion(":/" + nova_folder + "/nova2.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/nova2.py", filePath);
}
filePath = search_dir.absoluteFilePath("nova2dl.py");
if (getPluginVersion(":/" + nova_folder + "/nova2dl.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/nova2dl.py", filePath);
}
filePath = search_dir.absoluteFilePath("novaprinter.py");
if (getPluginVersion(":/" + nova_folder + "/novaprinter.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/novaprinter.py", filePath);
}
filePath = search_dir.absoluteFilePath("helpers.py");
if (getPluginVersion(":/" + nova_folder + "/helpers.py") > getPluginVersion(filePath)) {
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/helpers.py", filePath);
}
filePath = search_dir.absoluteFilePath("socks.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/socks.py", filePath);
if (nova_folder == "nova") {
filePath = search_dir.absoluteFilePath("fix_encoding.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/fix_encoding.py", filePath);
}
else if (nova_folder == "nova3") {
filePath = search_dir.absoluteFilePath("sgmllib3.py");
removePythonScriptIfExists(filePath);
QFile::copy(":/" + nova_folder + "/sgmllib3.py", filePath);
}
QDir destDir(QDir(Utils::Fs::searchEngineLocation()).absoluteFilePath("engines"));
Utils::Fs::removeDirRecursive(destDir.absoluteFilePath("__pycache__"));
QDir shipped_subDir(":/" + nova_folder + "/engines/");
QStringList files = shipped_subDir.entryList();
foreach (const QString &file, files) {
QString shipped_file = shipped_subDir.absoluteFilePath(file);
// Copy python classes
if (file.endsWith(".py")) {
const QString dest_file = destDir.absoluteFilePath(file);
if (getPluginVersion(shipped_file) > getPluginVersion(dest_file) ) {
qDebug("shipped %s is more recent then local plugin, updating...", qPrintable(file));
removePythonScriptIfExists(dest_file);
qDebug("%s copied to %s", qPrintable(shipped_file), qPrintable(dest_file));
QFile::copy(shipped_file, dest_file);
}
}
else {
// Copy icons
if (file.endsWith(".png"))
if (!QFile::exists(destDir.absoluteFilePath(file)))
QFile::copy(shipped_file, destDir.absoluteFilePath(file));
}
}
}
// Slot called when search is Finished
// Search can be finished for 3 reasons :
// Error | Stopped by user | Finished normally
void SearchWidget::searchFinished(int exitcode, QProcess::ExitStatus)
void SearchWidget::searchFinished(bool cancelled)
{
if (searchTimeout->isActive())
searchTimeout->stop();
bool useNotificationBalloons = Preferences::instance()->useProgramNotification();
if (useNotificationBalloons && mp_mainWindow->getCurrentTabWidget() != this)
mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished"));
if (activeSearchTab.isNull())
// The active tab was closed
return;
if (activeSearchTab.isNull()) return; // The active tab was closed
if (exitcode) {
#ifdef Q_OS_WIN
if (cancelled)
activeSearchTab->status = tr("Search aborted");
#else
activeSearchTab->status = tr("An error occurred during search...");
#endif
}
else {
if (search_stopped) {
activeSearchTab->status = tr("Search aborted");
}
else {
if (no_search_results)
activeSearchTab->status = tr("Search returned no results");
else
activeSearchTab->status = tr("Search has finished");
}
}
else if (no_search_results)
activeSearchTab->status = tr("Search returned no results");
else
activeSearchTab->status = tr("Search has finished");
search_status->setText(currentSearchTab->status);
activeSearchTab = 0;
search_button->setText(tr("Search"));
}
void SearchWidget::searchFailed()
{
bool useNotificationBalloons = Preferences::instance()->useProgramNotification();
if (useNotificationBalloons && mp_mainWindow->getCurrentTabWidget() != this)
mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has failed"));
if (activeSearchTab.isNull()) return; // The active tab was closed
#ifdef Q_OS_WIN
activeSearchTab->status = tr("Search aborted");
#else
activeSearchTab->status = tr("An error occurred during search...");
#endif
}
// SLOT to append one line to search results list
// Line is in the following form :
// file url | file name | file size | nb seeds | nb leechers | Search engine url
void SearchWidget::appendSearchResult(const QString &line)
void SearchWidget::appendSearchResults(const QList<SearchResult> &results)
{
if (activeSearchTab.isNull()) {
if (searchProcess->state() != QProcess::NotRunning) {
#ifdef Q_OS_WIN
searchProcess->kill();
#else
searchProcess->terminate();
#endif
searchProcess->waitForFinished(1000);
}
if (searchTimeout->isActive())
searchTimeout->stop();
search_stopped = true;
m_searchEngine->cancelSearch();
return;
}
const QStringList parts = line.split("|");
const int nb_fields = parts.size();
if (nb_fields < NB_PLUGIN_COLUMNS - 1) //-1 because desc_link is optional
return;
Q_ASSERT(activeSearchTab);
// Add item to search result list
QStandardItemModel* cur_model = activeSearchTab->getCurrentSearchListModel();
Q_ASSERT(cur_model);
int row = cur_model->rowCount();
cur_model->insertRow(row);
cur_model->setData(cur_model->index(row, SearchSortModel::DL_LINK), parts.at(PL_DL_LINK).trimmed()); // download URL
cur_model->setData(cur_model->index(row, SearchSortModel::NAME), parts.at(PL_NAME).trimmed()); // Name
cur_model->setData(cur_model->index(row, SearchSortModel::SIZE), parts.at(PL_SIZE).trimmed().toLongLong()); // Size
bool ok = false;
qlonglong nb_seeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok);
if (!ok || nb_seeders < 0)
cur_model->setData(cur_model->index(row, SearchSortModel::SEEDS), -1); // Seeders
else
cur_model->setData(cur_model->index(row, SearchSortModel::SEEDS), nb_seeders); // Seeders
qlonglong nb_leechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok);
if (!ok || nb_leechers < 0)
cur_model->setData(cur_model->index(row, SearchSortModel::LEECHS), -1); // Leechers
else
cur_model->setData(cur_model->index(row, SearchSortModel::LEECHS), nb_leechers); // Leechers
cur_model->setData(cur_model->index(row, SearchSortModel::ENGINE_URL), parts.at(PL_ENGINE_URL).trimmed()); // Engine URL
// Description Link
if (nb_fields == NB_PLUGIN_COLUMNS)
cur_model->setData(cur_model->index(row, SearchSortModel::DESC_LINK), parts.at(PL_DESC_LINK).trimmed());
foreach (const SearchResult &result, results) {
// Add item to search result list
int row = cur_model->rowCount();
cur_model->insertRow(row);
cur_model->setData(cur_model->index(row, SearchSortModel::DL_LINK), result.fileUrl); // download URL
cur_model->setData(cur_model->index(row, SearchSortModel::NAME), result.fileName); // Name
cur_model->setData(cur_model->index(row, SearchSortModel::SIZE), result.fileSize); // Size
cur_model->setData(cur_model->index(row, SearchSortModel::SEEDS), result.nbSeeders); // Seeders
cur_model->setData(cur_model->index(row, SearchSortModel::LEECHS), result.nbLeechers); // Leechers
cur_model->setData(cur_model->index(row, SearchSortModel::ENGINE_URL), result.siteUrl); // Search site URL
cur_model->setData(cur_model->index(row, SearchSortModel::DESC_LINK), result.descrLink); // Description Link
}
no_search_results = false;
++nb_search_results;
nb_search_results += results.size();
activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "i.e: Search results").arg(nb_search_results));
// Enable clear & download buttons
download_button->setEnabled(true);
goToDescBtn->setEnabled(true);
@ -541,17 +349,8 @@ void SearchWidget::closeTab(int index)
// Search is run for active tab so if user decided to close it, then stop search
if (!activeSearchTab.isNull() && index == tabWidget->indexOf(activeSearchTab)) {
qDebug("Closed active search Tab");
if (searchProcess->state() != QProcess::NotRunning) {
#ifdef Q_OS_WIN
searchProcess->kill();
#else
searchProcess->terminate();
#endif
searchProcess->waitForFinished(1000);
}
if (searchTimeout->isActive())
searchTimeout->stop();
search_stopped = true;
if (m_searchEngine->isActive())
m_searchEngine->cancelSearch();
activeSearchTab = 0;
}
delete all_tab.takeAt(index);
@ -573,8 +372,7 @@ void SearchWidget::on_download_button_clicked()
// Get Item url
QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy();
QString torrent_url = model->data(model->index(index.row(), URL_COLUMN)).toString();
QString engine_url = model->data(model->index(index.row(), ENGINE_URL_COLUMN)).toString();
downloadTorrent(engine_url, torrent_url);
downloadTorrent(torrent_url);
all_tab.at(tabWidget->currentIndex())->setRowColor(index.row(), "blue");
}
}

View file

@ -1,6 +1,7 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -31,19 +32,19 @@
#ifndef SEARCHWIDGET_H
#define SEARCHWIDGET_H
#include <QProcess>
#include <QList>
#include <QPair>
#include <QPointer>
#include <QStringListModel>
#include "ui_searchwidget.h"
#include "engineselectdlg.h"
#include "pluginselectdlg.h"
#include "searchtab.h"
#include "supportedengines.h"
class SearchWidget;
class MainWindow;
class LineEdit;
class SearchEngine;
struct SearchResult;
QT_BEGIN_NAMESPACE
class QTimer;
@ -53,79 +54,46 @@ class SearchWidget : public QWidget, private Ui::SearchWidget{
Q_OBJECT
Q_DISABLE_COPY(SearchWidget)
private:
enum PluginColumn { PL_DL_LINK, PL_NAME, PL_SIZE, PL_SEEDS, PL_LEECHS, PL_ENGINE_URL, PL_DESC_LINK, NB_PLUGIN_COLUMNS };
public:
SearchWidget(MainWindow *mp_mainWindow);
~SearchWidget();
QString selectedCategory() const;
QString selectedEngine() const;
static qreal getPluginVersion(QString filePath) {
QFile plugin(filePath);
if (!plugin.exists()) {
qDebug("%s plugin does not exist, returning 0.0", qPrintable(filePath));
return 0.0;
}
if (!plugin.open(QIODevice::ReadOnly | QIODevice::Text)) {
return 0.0;
}
qreal version = 0.0;
while (!plugin.atEnd()) {
QByteArray line = plugin.readLine();
if (line.startsWith("#VERSION: ")) {
line = line.split(' ').last().trimmed();
version = line.toFloat();
qDebug("plugin %s version: %.2f", qPrintable(filePath), version);
break;
}
}
return version;
}
public slots:
void on_download_button_clicked();
void downloadTorrent(QString engine_url, QString torrent_url);
void downloadTorrent(QString url);
void giveFocusToSearchInput();
protected slots:
private slots:
// Search slots
void tab_changed(int);//to prevent the use of the download button when the tab is empty
void on_search_button_clicked();
void on_download_button_clicked();
void closeTab(int index);
void appendSearchResult(const QString &line);
void searchFinished(int exitcode,QProcess::ExitStatus);
void readSearchOutput();
void appendSearchResults(const QList<SearchResult> &results);
void searchStarted();
void updateNova();
void searchFinished(bool cancelled);
void searchFailed();
void selectMultipleBox(const QString &text);
void on_enginesButton_clicked();
void on_pluginsButton_clicked();
void saveResultsColumnsWidth();
void downloadFinished(int exitcode, QProcess::ExitStatus);
void fillCatCombobox();
void fillEngineComboBox();
void fillPluginComboBox();
void searchTextEdited(QString);
private slots:
void on_goToDescBtn_clicked();
void on_copyURLBtn_clicked();
private:
// Search related
LineEdit* search_pattern;
QProcess *searchProcess;
QList<QProcess*> downloaders;
bool search_stopped;
bool no_search_results;
QByteArray search_result_line_truncated;
unsigned long nb_search_results;
SupportedEngines *supported_engines;
QTimer *searchTimeout;
SearchEngine *m_searchEngine;
QPointer<SearchTab> currentSearchTab; // Selected tab
QPointer<SearchTab> activeSearchTab; // Tab with running search
QList<QPointer<SearchTab> > all_tab; // To store all tabs
const SearchCategories full_cat_names;
MainWindow *mp_mainWindow;
bool newQueryString;
};

View file

@ -20,7 +20,7 @@
<widget class="QComboBox" name="comboCategory"/>
</item>
<item>
<widget class="QComboBox" name="selectEngine"/>
<widget class="QComboBox" name="selectPlugin"/>
</item>
<item>
<widget class="QPushButton" name="search_button">
@ -142,9 +142,9 @@
</spacer>
</item>
<item>
<widget class="QPushButton" name="enginesButton">
<widget class="QPushButton" name="pluginsButton">
<property name="text">
<string>Search engines...</string>
<string>Search plugins...</string>
</property>
</widget>
</item>

View file

@ -1,186 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
#ifndef SEARCHENGINES_H
#define SEARCHENGINES_H
#include <QHash>
#include <QStringList>
#include <QDomDocument>
#include <QDomNode>
#include <QDomElement>
#include <QProcess>
#include <QDir>
#include <QApplication>
#include <QDebug>
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/preferences.h"
class SearchCategories: public QObject, public QHash<QString, QString> {
Q_OBJECT
public:
SearchCategories() {
(*this)["all"] = tr("All categories");
(*this)["movies"] = tr("Movies");
(*this)["tv"] = tr("TV shows");
(*this)["music"] = tr("Music");
(*this)["games"] = tr("Games");
(*this)["anime"] = tr("Anime");
(*this)["software"] = tr("Software");
(*this)["pictures"] = tr("Pictures");
(*this)["books"] = tr("Books");
}
};
class SupportedEngine {
private:
QString name;
QString full_name;
QString url;
QStringList supported_categories;
bool enabled;
public:
SupportedEngine(QDomElement engine_elem) {
name = engine_elem.tagName();
full_name = engine_elem.elementsByTagName("name").at(0).toElement().text();
url = engine_elem.elementsByTagName("url").at(0).toElement().text();
supported_categories = engine_elem.elementsByTagName("categories").at(0).toElement().text().split(" ");
QStringList disabled_engines = Preferences::instance()->getSearchEngDisabled();
enabled = !disabled_engines.contains(name);
}
QString getName() const { return name; }
QString getUrl() const { return url; }
QString getFullName() const { return full_name; }
QStringList getSupportedCategories() const { return supported_categories; }
bool isEnabled() const { return enabled; }
void setEnabled(bool _enabled) {
enabled = _enabled;
// Save to Hard disk
Preferences* const pref = Preferences::instance();
QStringList disabled_engines = pref->getSearchEngDisabled();
if (enabled) {
disabled_engines.removeAll(name);
} else {
disabled_engines.append(name);
}
pref->setSearchEngDisabled(disabled_engines);
}
};
class SupportedEngines: public QObject, public QHash<QString, SupportedEngine*> {
Q_OBJECT
signals:
void newSupportedEngine(QString name);
public:
SupportedEngines() {
update();
}
~SupportedEngines() {
qDeleteAll(this->values());
}
QStringList enginesAll() const {
QStringList engines;
foreach (const SupportedEngine *engine, values()) engines << engine->getName();
return engines;
}
QStringList enginesEnabled() const {
QStringList engines;
foreach (const SupportedEngine *engine, values()) {
if (engine->isEnabled())
engines << engine->getName();
}
return engines;
}
QStringList supportedCategories() const {
QStringList supported_cat;
foreach (const SupportedEngine *engine, values()) {
if (engine->isEnabled()) {
const QStringList &s = engine->getSupportedCategories();
foreach (QString cat, s) {
cat = cat.trimmed();
if (!cat.isEmpty() && !supported_cat.contains(cat))
supported_cat << cat;
}
}
}
return supported_cat;
}
public slots:
void update() {
QProcess nova;
nova.setEnvironment(QProcess::systemEnvironment());
QStringList params;
params << Utils::Fs::toNativePath(Utils::Fs::searchEngineLocation()+"/nova2.py");
params << "--capabilities";
nova.start(Utils::Misc::pythonExecutable(), params, QIODevice::ReadOnly);
nova.waitForStarted();
nova.waitForFinished();
QString capabilities = QString(nova.readAll());
QDomDocument xml_doc;
if (!xml_doc.setContent(capabilities)) {
qWarning() << "Could not parse Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
qWarning() << "Error: " << nova.readAllStandardError().constData();
return;
}
QDomElement root = xml_doc.documentElement();
if (root.tagName() != "capabilities") {
qWarning() << "Invalid XML file for Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data();
return;
}
for (QDomNode engine_node = root.firstChild(); !engine_node.isNull(); engine_node = engine_node.nextSibling()) {
QDomElement engine_elem = engine_node.toElement();
if (!engine_elem.isNull()) {
SupportedEngine *s = new SupportedEngine(engine_elem);
if (this->contains(s->getName())) {
// Already in the list
delete s;
} else {
qDebug("Supported search engine: %s", s->getFullName().toLocal8Bit().data());
(*this)[s->getName()] = s;
emit newSupportedEngine(s->getName());
}
}
}
}
};
#endif // SEARCHENGINES_H

View file

@ -3,7 +3,6 @@
<file>searchengine/nova/fix_encoding.py</file>
<file>searchengine/nova/helpers.py</file>
<file>searchengine/nova/nova2.py</file>
<file>searchengine/nova/nova2dl.py</file>
<file>searchengine/nova/novaprinter.py</file>
<file>searchengine/nova/socks.py</file>
<file>searchengine/nova/engines/btdigg.png</file>
@ -26,7 +25,6 @@
<file>searchengine/nova/engines/torrentz.py</file>
<file>searchengine/nova3/helpers.py</file>
<file>searchengine/nova3/nova2.py</file>
<file>searchengine/nova3/nova2dl.py</file>
<file>searchengine/nova3/novaprinter.py</file>
<file>searchengine/nova3/sgmllib3.py</file>
<file>searchengine/nova3/socks.py</file>

View file

@ -1,61 +0,0 @@
#VERSION: 1.20
# Author:
# Christophe DUMEZ (chris@qbittorrent.org)
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import os
import glob
from helpers import download_file
supported_engines = dict()
engines = glob.glob(os.path.join(os.path.dirname(__file__), 'engines','*.py'))
for engine in engines:
e = engine.split(os.sep)[-1][:-3]
if len(e.strip()) == 0: continue
if e.startswith('_'): continue
try:
exec("from engines.%s import %s"%(e,e))
exec("engine_url = %s.url"%e)
supported_engines[engine_url] = e
except:
pass
if __name__ == '__main__':
if len(sys.argv) < 3:
raise SystemExit('./nova2dl.py engine_url download_parameter')
engine_url = sys.argv[1].strip()
download_param = sys.argv[2].strip()
if engine_url not in list(supported_engines.keys()):
raise SystemExit('./nova2dl.py: this engine_url was not recognized')
exec("engine = %s()"%supported_engines[engine_url])
if hasattr(engine, 'download_torrent'):
engine.download_torrent(download_param)
else:
print(download_file(download_param))
sys.exit(0)

View file

@ -1,61 +0,0 @@
#VERSION: 1.20
# Author:
# Christophe DUMEZ (chris@qbittorrent.org)
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import os
import glob
from helpers import download_file
supported_engines = dict()
engines = glob.glob(os.path.join(os.path.dirname(__file__), 'engines','*.py'))
for engine in engines:
e = engine.split(os.sep)[-1][:-3]
if len(e.strip()) == 0: continue
if e.startswith('_'): continue
try:
exec("from engines.%s import %s"%(e,e))
exec("engine_url = %s.url"%e)
supported_engines[engine_url] = e
except:
pass
if __name__ == '__main__':
if len(sys.argv) < 3:
raise SystemExit('./nova2dl.py engine_url download_parameter')
engine_url = sys.argv[1].strip()
download_param = sys.argv[2].strip()
if engine_url not in list(supported_engines.keys()):
raise SystemExit('./nova2dl.py: this engine_url was not recognized')
exec("engine = %s()"%supported_engines[engine_url])
if hasattr(engine, 'download_torrent'):
engine.download_torrent(download_param)
else:
print(download_file(download_param))
sys.exit(0)

View file

@ -34,7 +34,7 @@ nogui {
}
nowebui: DEFINES += DISABLE_WEBUI
strace_win: DEFINES += STACKTRACE_WIN
QT += network
QT += network xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# Vars