Browse Source
Also use qBittorrent torrent file download routines instead of nova2dl.py script.adaptive-webui-19844
Vladimir Golovnev (Glassez)
9 years ago
committed by
Vladimir Golovnev (qlassez)
22 changed files with 1379 additions and 1235 deletions
@ -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; |
||||||
|
} |
@ -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
|
@ -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)); |
|
||||||
} |
|
||||||
} |
|
@ -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)); |
||||||
|
} |
@ -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
|
|
@ -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) |
|
@ -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) |
|
Loading…
Reference in new issue