From 3797cc767f873e1c15e0296248771f1e659fdc73 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Wed, 26 Aug 2015 20:05:57 +0300 Subject: [PATCH 1/4] Fix search-related file/class names. --- src/gui/mainwindow.cpp | 4 +- src/gui/mainwindow.h | 4 +- src/searchengine/engineselectdlg.cpp | 62 +++++++++---------- src/searchengine/engineselectdlg.h | 9 +-- .../{engineselect.ui => engineselectdlg.ui} | 4 +- .../{pluginsource.h => pluginsourcedlg.h} | 14 ++--- .../{pluginsource.ui => pluginsourcedlg.ui} | 4 +- src/searchengine/searchengine.pri | 12 ++-- src/searchengine/searchlistdelegate.h | 2 +- src/searchengine/searchtab.cpp | 4 +- src/searchengine/searchtab.h | 21 ++++--- .../{searchengine.cpp => searchwidget.cpp} | 52 ++++++++-------- .../{searchengine.h => searchwidget.h} | 18 +++--- .../{search.ui => searchwidget.ui} | 4 +- 14 files changed, 108 insertions(+), 106 deletions(-) rename src/searchengine/{engineselect.ui => engineselectdlg.ui} (97%) rename src/searchengine/{pluginsource.h => pluginsourcedlg.h} (87%) rename src/searchengine/{pluginsource.ui => pluginsourcedlg.ui} (93%) rename src/searchengine/{searchengine.cpp => searchwidget.cpp} (95%) rename src/searchengine/{searchengine.h => searchwidget.h} (93%) rename src/searchengine/{search.ui => searchwidget.ui} (98%) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index ac9bbd729..953392430 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -53,7 +53,7 @@ #include "torrentcreatordlg.h" #include "downloadfromurldlg.h" #include "addnewtorrentdialog.h" -#include "searchengine.h" +#include "searchwidget.h" #include "rss_imp.h" #include "base/bittorrent/session.h" #include "base/bittorrent/sessionstatus.h" @@ -526,7 +526,7 @@ void MainWindow::displaySearchTab(bool enable) if (enable) { // RSS tab if (!searchEngine) { - searchEngine = new SearchEngine(this); + searchEngine = new SearchWidget(this); tabs->insertTab(1, searchEngine, GuiIconProvider::instance()->getIcon("edit-find"), tr("Search")); } } diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index a8cd49944..5c2338c3e 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -38,7 +38,7 @@ #include "statsdialog.h" class downloadFromURL; -class SearchEngine; +class SearchWidget; class RSSImp; class about; class options_imp; @@ -192,7 +192,7 @@ private: QSplitter *hSplitter; QSplitter *vSplitter; // Search - QPointer searchEngine; + QPointer searchEngine; // RSS QPointer rssWidget; // Execution Log diff --git a/src/searchengine/engineselectdlg.cpp b/src/searchengine/engineselectdlg.cpp index 0d78a81ef..d51f4cb2d 100644 --- a/src/searchengine/engineselectdlg.cpp +++ b/src/searchengine/engineselectdlg.cpp @@ -34,8 +34,8 @@ #include "base/utils/fs.h" #include "base/utils/misc.h" #include "ico.h" -#include "searchengine.h" -#include "pluginsource.h" +#include "searchwidget.h" +#include "pluginsourcedlg.h" #include "guiiconprovider.h" #include "autoexpandabledialog.h" #include @@ -53,7 +53,7 @@ enum EngineColumns {ENGINE_NAME, ENGINE_VERSION, ENGINE_URL, ENGINE_STATE, ENGINE_ID}; -engineSelectDlg::engineSelectDlg(QWidget *parent, SupportedEngines *supported_engines) +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/") @@ -82,13 +82,13 @@ engineSelectDlg::engineSelectDlg(QWidget *parent, SupportedEngines *supported_en show(); } -engineSelectDlg::~engineSelectDlg() { +EngineSelectDlg::~EngineSelectDlg() { qDebug("Destroying engineSelectDlg"); emit enginesChanged(); qDebug("Engine plugins dialog destroyed"); } -void engineSelectDlg::dropEvent(QDropEvent *event) { +void EngineSelectDlg::dropEvent(QDropEvent *event) { event->acceptProposedAction(); QStringList files; if (event->mimeData()->hasUrls()) { @@ -123,7 +123,7 @@ void engineSelectDlg::dropEvent(QDropEvent *event) { } // Decode if we accept drag 'n drop or not -void engineSelectDlg::dragEnterEvent(QDragEnterEvent *event) { +void EngineSelectDlg::dragEnterEvent(QDragEnterEvent *event) { QString mime; foreach (mime, event->mimeData()->formats()) { qDebug("mimeData: %s", qPrintable(mime)); @@ -133,13 +133,13 @@ void engineSelectDlg::dragEnterEvent(QDragEnterEvent *event) { } } -void engineSelectDlg::on_updateButton_clicked() { +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) { +void EngineSelectDlg::toggleEngineState(QTreeWidgetItem *item, int) { SupportedEngine *engine = supported_engines->value(item->text(ENGINE_ID)); engine->setEnabled(!engine->isEnabled()); if (engine->isEnabled()) { @@ -151,7 +151,7 @@ void engineSelectDlg::toggleEngineState(QTreeWidgetItem *item, int) { } } -void engineSelectDlg::displayContextMenu(const QPoint&) { +void EngineSelectDlg::displayContextMenu(const QPoint&) { QMenu myContextMenu(this); // Enable/disable pause/start action given the DL state QList items = pluginsTree->selectedItems(); @@ -164,11 +164,11 @@ void engineSelectDlg::displayContextMenu(const QPoint&) { myContextMenu.exec(QCursor::pos()); } -void engineSelectDlg::on_closeButton_clicked() { +void EngineSelectDlg::on_closeButton_clicked() { close(); } -void engineSelectDlg::on_actionUninstall_triggered() { +void EngineSelectDlg::on_actionUninstall_triggered() { QList items = pluginsTree->selectedItems(); QTreeWidgetItem *item; bool error = false; @@ -205,7 +205,7 @@ void engineSelectDlg::on_actionUninstall_triggered() { QMessageBox::information(0, tr("Uninstall success"), tr("All selected plugins were uninstalled successfully")); } -void engineSelectDlg::enableSelection(bool enable) { +void EngineSelectDlg::enableSelection(bool enable) { QList items = pluginsTree->selectedItems(); QTreeWidgetItem *item; foreach (item, items) { @@ -224,14 +224,14 @@ void engineSelectDlg::enableSelection(bool enable) { } // Set the color of a row in data model -void engineSelectDlg::setRowColor(int row, QString color) { +void EngineSelectDlg::setRowColor(int row, QString color) { QTreeWidgetItem *item = pluginsTree->topLevelItem(row); for (int i=0; icolumnCount(); ++i) { item->setData(i, Qt::ForegroundRole, QVariant(QColor(color))); } } -QList engineSelectDlg::findItemsWithUrl(QString url) { +QList EngineSelectDlg::findItemsWithUrl(QString url) { QList res; for (int i=0; itopLevelItemCount(); ++i) { QTreeWidgetItem *item = pluginsTree->topLevelItem(i); @@ -241,7 +241,7 @@ QList engineSelectDlg::findItemsWithUrl(QString url) { return res; } -QTreeWidgetItem* engineSelectDlg::findItemWithID(QString id) { +QTreeWidgetItem* EngineSelectDlg::findItemWithID(QString id) { QList res; for (int i=0; itopLevelItemCount(); ++i) { QTreeWidgetItem *item = pluginsTree->topLevelItem(i); @@ -251,15 +251,15 @@ QTreeWidgetItem* engineSelectDlg::findItemWithID(QString id) { return 0; } -bool engineSelectDlg::isUpdateNeeded(QString plugin_name, qreal new_version) const { - qreal old_version = SearchEngine::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + plugin_name + ".py"); +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) { +void EngineSelectDlg::installPlugin(QString path, QString plugin_name) { qDebug("Asked to install plugin at %s", qPrintable(path)); - qreal new_version = SearchEngine::getPluginVersion(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; @@ -304,7 +304,7 @@ void engineSelectDlg::installPlugin(QString path, QString plugin_name) { // Install was successful, remove backup and update plugin version if (update) { Utils::Fs::forceRemove(dest_path+".bak"); - qreal version = SearchEngine::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + plugin_name + ".py"); + 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)); @@ -315,7 +315,7 @@ void engineSelectDlg::installPlugin(QString path, QString plugin_name) { } } -void engineSelectDlg::loadSupportedSearchEngines() { +void EngineSelectDlg::loadSupportedSearchEngines() { // Some clean up first pluginsTree->clear(); foreach (QString name, supported_engines->keys()) { @@ -323,7 +323,7 @@ void engineSelectDlg::loadSupportedSearchEngines() { } } -void engineSelectDlg::addNewEngine(QString engine_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()); @@ -351,17 +351,17 @@ void engineSelectDlg::addNewEngine(QString engine_name) { } } // Load version - qreal version = SearchEngine::getPluginVersion(Utils::Fs::searchEngineLocation() + "/engines/" + engine->getName() + ".py"); + 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); +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() { +void EngineSelectDlg::askForPluginUrl() { bool ok(false); QString clipTxt = qApp->clipboard()->text(); QString defaultUrl = "http://"; @@ -391,7 +391,7 @@ void engineSelectDlg::askForPluginUrl() { downloadFromUrl(url); } -void engineSelectDlg::askForLocalPlugin() { +void EngineSelectDlg::askForLocalPlugin() { QStringList pathsList = QFileDialog::getOpenFileNames(0, tr("Select search plugins"), QDir::homePath(), tr("qBittorrent search plugin")+QString::fromUtf8(" (*.py)")); @@ -404,7 +404,7 @@ void engineSelectDlg::askForLocalPlugin() { } } -bool engineSelectDlg::parseVersionsFile(QString versions_file) { +bool EngineSelectDlg::parseVersionsFile(QString versions_file) { qDebug("Checking if update is needed"); bool file_correct = false; QFile versions(versions_file); @@ -450,14 +450,14 @@ bool engineSelectDlg::parseVersionsFile(QString versions_file) { return file_correct; } -void engineSelectDlg::downloadFromUrl(const QString &url) +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) { +void EngineSelectDlg::processDownloadedFile(const QString &url, QString filePath) { filePath = Utils::Fs::fromNativePath(filePath); setCursor(QCursor(Qt::ArrowCursor)); qDebug("engineSelectDlg received %s", qPrintable(url)); @@ -500,7 +500,7 @@ void engineSelectDlg::processDownloadedFile(const QString &url, QString filePath } } -void engineSelectDlg::handleDownloadFailure(const QString &url, const QString &reason) { +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)); diff --git a/src/searchengine/engineselectdlg.h b/src/searchengine/engineselectdlg.h index abcb19001..a98f54bad 100644 --- a/src/searchengine/engineselectdlg.h +++ b/src/searchengine/engineselectdlg.h @@ -31,14 +31,15 @@ #ifndef ENGINE_SELECT_DLG_H #define ENGINE_SELECT_DLG_H -#include "ui_engineselect.h" +#include "ui_engineselectdlg.h" #include "supportedengines.h" QT_BEGIN_NAMESPACE class QDropEvent; QT_END_NAMESPACE -class engineSelectDlg : public QDialog, public Ui::engineSelect{ +class EngineSelectDlg : public QDialog, public Ui::EngineSelectDlg +{ Q_OBJECT private: @@ -48,8 +49,8 @@ class engineSelectDlg : public QDialog, public Ui::engineSelect{ const QString m_updateUrl; public: - engineSelectDlg(QWidget *parent, SupportedEngines *supported_engines); - ~engineSelectDlg(); + EngineSelectDlg(QWidget *parent, SupportedEngines *supported_engines); + ~EngineSelectDlg(); QList findItemsWithUrl(QString url); QTreeWidgetItem* findItemWithID(QString id); diff --git a/src/searchengine/engineselect.ui b/src/searchengine/engineselectdlg.ui similarity index 97% rename from src/searchengine/engineselect.ui rename to src/searchengine/engineselectdlg.ui index 7db8f90e3..4ba4f2f54 100644 --- a/src/searchengine/engineselect.ui +++ b/src/searchengine/engineselectdlg.ui @@ -1,7 +1,7 @@ - engineSelect - + EngineSelectDlg + 0 diff --git a/src/searchengine/pluginsource.h b/src/searchengine/pluginsourcedlg.h similarity index 87% rename from src/searchengine/pluginsource.h rename to src/searchengine/pluginsourcedlg.h index aea63711b..cfabee362 100644 --- a/src/searchengine/pluginsource.h +++ b/src/searchengine/pluginsourcedlg.h @@ -28,13 +28,13 @@ * Contact : chris@qbittorrent.org */ -#ifndef PLUGIN_SOURCE_H -#define PLUGIN_SOURCE_H +#ifndef PLUGINSOURCEDLG_H +#define PLUGINSOURCEDLG_H #include -#include "ui_pluginsource.h" +#include "ui_pluginsourcedlg.h" -class pluginSourceDlg: public QDialog, private Ui::pluginSourceDlg { +class PluginSourceDlg: public QDialog, private Ui::PluginSourceDlg { Q_OBJECT signals: @@ -53,13 +53,13 @@ class pluginSourceDlg: public QDialog, private Ui::pluginSourceDlg { } public: - pluginSourceDlg(QWidget* parent): QDialog(parent) { + PluginSourceDlg(QWidget* parent): QDialog(parent) { setupUi(this); setAttribute(Qt::WA_DeleteOnClose); show(); } - ~pluginSourceDlg() {} + ~PluginSourceDlg() {} }; -#endif +#endif // PLUGINSOURCEDLG_H diff --git a/src/searchengine/pluginsource.ui b/src/searchengine/pluginsourcedlg.ui similarity index 93% rename from src/searchengine/pluginsource.ui rename to src/searchengine/pluginsourcedlg.ui index ecfcbc49c..a2ca22d95 100644 --- a/src/searchengine/pluginsource.ui +++ b/src/searchengine/pluginsourcedlg.ui @@ -1,6 +1,6 @@ - pluginSourceDlg - + PluginSourceDlg + 0 diff --git a/src/searchengine/searchengine.pri b/src/searchengine/searchengine.pri index 9a514a4b4..9a2812827 100644 --- a/src/searchengine/searchengine.pri +++ b/src/searchengine/searchengine.pri @@ -1,18 +1,18 @@ INCLUDEPATH += $$PWD -FORMS += $$PWD/search.ui \ - $$PWD/engineselect.ui \ - $$PWD/pluginsource.ui +FORMS += $$PWD/searchwidget.ui \ + $$PWD/engineselectdlg.ui \ + $$PWD/pluginsourcedlg.ui -HEADERS += $$PWD/searchengine.h \ +HEADERS += $$PWD/searchwidget.h \ $$PWD/searchtab.h \ $$PWD/engineselectdlg.h \ - $$PWD/pluginsource.h \ + $$PWD/pluginsourcedlg.h \ $$PWD/searchlistdelegate.h \ $$PWD/supportedengines.h \ $$PWD/searchsortmodel.h -SOURCES += $$PWD/searchengine.cpp \ +SOURCES += $$PWD/searchwidget.cpp \ $$PWD/searchtab.cpp \ $$PWD/engineselectdlg.cpp diff --git a/src/searchengine/searchlistdelegate.h b/src/searchengine/searchlistdelegate.h index 78ba36ac5..2986e6d41 100644 --- a/src/searchengine/searchlistdelegate.h +++ b/src/searchengine/searchlistdelegate.h @@ -37,7 +37,7 @@ #include #include #include "base/utils/misc.h" -#include "searchengine.h" +#include "searchwidget.h" class SearchListDelegate: public QItemDelegate { Q_OBJECT diff --git a/src/searchengine/searchtab.cpp b/src/searchengine/searchtab.cpp index e4b4e414f..5d2d69624 100644 --- a/src/searchengine/searchtab.cpp +++ b/src/searchengine/searchtab.cpp @@ -40,10 +40,10 @@ #include "searchtab.h" #include "searchlistdelegate.h" #include "base/utils/misc.h" -#include "searchengine.h" +#include "searchwidget.h" #include "base/preferences.h" -SearchTab::SearchTab(SearchEngine *parent) : QWidget(), parent(parent) +SearchTab::SearchTab(SearchWidget *parent) : QWidget(), parent(parent) { box = new QVBoxLayout(); results_lbl = new QLabel(); diff --git a/src/searchengine/searchtab.h b/src/searchengine/searchtab.h index 05920007d..cab2ab714 100644 --- a/src/searchengine/searchtab.h +++ b/src/searchengine/searchtab.h @@ -28,25 +28,26 @@ * Contact : chris@qbittorrent.org */ -#ifndef SEARCH_TAB_H -#define SEARCH_TAB_H +#ifndef SEARCHTAB_H +#define SEARCHTAB_H + +#include +#include -#include "ui_search.h" #include "searchsortmodel.h" #define ENGINE_URL_COLUMN 4 #define URL_COLUMN 5 class SearchListDelegate; -class SearchEngine; +class SearchWidget; -QT_BEGIN_NAMESPACE class QTreeView; class QHeaderView; class QStandardItemModel; -QT_END_NAMESPACE -class SearchTab: public QWidget, public Ui::search_engine { +class SearchTab: public QWidget +{ Q_OBJECT private: @@ -56,13 +57,13 @@ private: QStandardItemModel *SearchListModel; SearchSortModel *proxyModel; SearchListDelegate *SearchDelegate; - SearchEngine *parent; + SearchWidget *parent; protected slots: void downloadSelectedItem(const QModelIndex& index); public: - SearchTab(SearchEngine *parent); + SearchTab(SearchWidget *parent); ~SearchTab(); bool loadColWidthResultsList(); QLabel * getCurrentLabel(); @@ -74,5 +75,5 @@ public: QString status; }; -#endif +#endif // SEARCHTAB_H diff --git a/src/searchengine/searchengine.cpp b/src/searchengine/searchwidget.cpp similarity index 95% rename from src/searchengine/searchengine.cpp rename to src/searchengine/searchwidget.cpp index dd3f143a3..b28c6857a 100644 --- a/src/searchengine/searchengine.cpp +++ b/src/searchengine/searchwidget.cpp @@ -48,7 +48,6 @@ #include #endif -#include "searchengine.h" #include "base/bittorrent/session.h" #include "base/utils/fs.h" #include "base/utils/misc.h" @@ -58,11 +57,12 @@ #include "addnewtorrentdialog.h" #include "guiiconprovider.h" #include "lineedit.h" +#include "searchwidget.h" #define SEARCHHISTORY_MAXSIZE 50 /*SEARCH ENGINE START*/ -SearchEngine::SearchEngine(MainWindow* parent) +SearchWidget::SearchWidget(MainWindow* parent) : QWidget(parent) , search_pattern(new LineEdit(this)) , mp_mainWindow(parent) @@ -101,7 +101,7 @@ SearchEngine::SearchEngine(MainWindow* parent) connect(selectEngine, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(selectMultipleBox(const QString &))); } -void SearchEngine::fillCatCombobox() +void SearchWidget::fillCatCombobox() { comboCategory->clear(); comboCategory->addItem(full_cat_names["all"], QVariant("all")); @@ -112,7 +112,7 @@ void SearchEngine::fillCatCombobox() } } -void SearchEngine::fillEngineComboBox() +void SearchWidget::fillEngineComboBox() { selectEngine->clear(); selectEngine->addItem(tr("All enabled"), QVariant("enabled")); @@ -122,17 +122,17 @@ void SearchEngine::fillEngineComboBox() selectEngine->addItem(tr("Multiple..."), QVariant("multi")); } -QString SearchEngine::selectedCategory() const +QString SearchWidget::selectedCategory() const { return comboCategory->itemData(comboCategory->currentIndex()).toString(); } -QString SearchEngine::selectedEngine() const +QString SearchWidget::selectedEngine() const { return selectEngine->itemData(selectEngine->currentIndex()).toString(); } -SearchEngine::~SearchEngine() +SearchWidget::~SearchWidget() { qDebug("Search destruction"); searchProcess->kill(); @@ -151,7 +151,7 @@ SearchEngine::~SearchEngine() delete supported_engines; } -void SearchEngine::tab_changed(int t) +void SearchWidget::tab_changed(int t) { //when we switch from a tab that is not empty to another that is empty the download button //doesn't have to be available @@ -172,32 +172,32 @@ void SearchEngine::tab_changed(int t) } } -void SearchEngine::selectMultipleBox(const QString &text) +void SearchWidget::selectMultipleBox(const QString &text) { if (text == tr("Multiple...")) on_enginesButton_clicked(); } -void SearchEngine::on_enginesButton_clicked() +void SearchWidget::on_enginesButton_clicked() { - engineSelectDlg *dlg = new engineSelectDlg(this, supported_engines); + EngineSelectDlg *dlg = new EngineSelectDlg(this, supported_engines); connect(dlg, SIGNAL(enginesChanged()), this, SLOT(fillCatCombobox())); connect(dlg, SIGNAL(enginesChanged()), this, SLOT(fillEngineComboBox())); } -void SearchEngine::searchTextEdited(QString) +void SearchWidget::searchTextEdited(QString) { // Enable search button search_button->setText(tr("Search")); newQueryString = true; } -void SearchEngine::giveFocusToSearchInput() +void SearchWidget::giveFocusToSearchInput() { search_pattern->setFocus(); } // Function called when we click on search button -void SearchEngine::on_search_button_clicked() +void SearchWidget::on_search_button_clicked() { if (Utils::Misc::pythonVersion() < 0) { mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); @@ -265,7 +265,7 @@ void SearchEngine::on_search_button_clicked() searchTimeout->start(180000); // 3min } -void SearchEngine::saveResultsColumnsWidth() +void SearchWidget::saveResultsColumnsWidth() { if (all_tab.isEmpty()) return; @@ -281,7 +281,7 @@ void SearchEngine::saveResultsColumnsWidth() pref->setSearchColsWidth(new_width_list.join(" ")); } -void SearchEngine::downloadTorrent(QString engine_url, QString torrent_url) +void SearchWidget::downloadTorrent(QString engine_url, QString torrent_url) { if (torrent_url.startsWith("bc://bt/", Qt::CaseInsensitive)) { qDebug("Converting bc link to magnet link"); @@ -307,7 +307,7 @@ void SearchEngine::downloadTorrent(QString engine_url, QString torrent_url) } } -void SearchEngine::searchStarted() +void SearchWidget::searchStarted() { // Update SearchEngine widgets activeSearchTab->status = tr("Searching..."); @@ -319,7 +319,7 @@ void SearchEngine::searchStarted() // 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 SearchEngine::readSearchOutput() +void SearchWidget::readSearchOutput() { QByteArray output = searchProcess->readAllStandardOutput(); output.replace("\r", ""); @@ -334,7 +334,7 @@ void SearchEngine::readSearchOutput() activeSearchTab->getCurrentLabel()->setText(tr("Results (%1):", "i.e: Search results").arg(nb_search_results)); } -void SearchEngine::downloadFinished(int exitcode, QProcess::ExitStatus) +void SearchWidget::downloadFinished(int exitcode, QProcess::ExitStatus) { QProcess *downloadProcess = (QProcess*)sender(); if (exitcode == 0) { @@ -361,7 +361,7 @@ static inline void removePythonScriptIfExists(const QString& script_path) } // Update nova.py search plugin if necessary -void SearchEngine::updateNova() +void SearchWidget::updateNova() { qDebug("Updating nova"); // create nova directory if necessary @@ -444,7 +444,7 @@ void SearchEngine::updateNova() // Slot called when search is Finished // Search can be finished for 3 reasons : // Error | Stopped by user | Finished normally -void SearchEngine::searchFinished(int exitcode, QProcess::ExitStatus) +void SearchWidget::searchFinished(int exitcode, QProcess::ExitStatus) { if (searchTimeout->isActive()) searchTimeout->stop(); @@ -482,7 +482,7 @@ void SearchEngine::searchFinished(int exitcode, QProcess::ExitStatus) // 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 SearchEngine::appendSearchResult(const QString &line) +void SearchWidget::appendSearchResult(const QString &line) { if (activeSearchTab.isNull()) { if (searchProcess->state() != QProcess::NotRunning) { @@ -536,7 +536,7 @@ void SearchEngine::appendSearchResult(const QString &line) copyURLBtn->setEnabled(true); } -void SearchEngine::closeTab(int index) +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)) { @@ -564,7 +564,7 @@ void SearchEngine::closeTab(int index) } // Download selected items in search results list -void SearchEngine::on_download_button_clicked() +void SearchWidget::on_download_button_clicked() { //QModelIndexList selectedIndexes = currentSearchTab->getCurrentTreeView()->selectionModel()->selectedIndexes(); QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); @@ -580,7 +580,7 @@ void SearchEngine::on_download_button_clicked() } } -void SearchEngine::on_goToDescBtn_clicked() +void SearchWidget::on_goToDescBtn_clicked() { QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); foreach (const QModelIndex &index, selectedIndexes) { @@ -593,7 +593,7 @@ void SearchEngine::on_goToDescBtn_clicked() } } -void SearchEngine::on_copyURLBtn_clicked() +void SearchWidget::on_copyURLBtn_clicked() { QStringList urls; QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); diff --git a/src/searchengine/searchengine.h b/src/searchengine/searchwidget.h similarity index 93% rename from src/searchengine/searchengine.h rename to src/searchengine/searchwidget.h index bc2d26626..9751a620b 100644 --- a/src/searchengine/searchengine.h +++ b/src/searchengine/searchwidget.h @@ -28,20 +28,20 @@ * Contact : chris@qbittorrent.org */ -#ifndef SEARCH_H -#define SEARCH_H +#ifndef SEARCHWIDGET_H +#define SEARCHWIDGET_H #include #include #include #include #include -#include "ui_search.h" +#include "ui_searchwidget.h" #include "engineselectdlg.h" #include "searchtab.h" #include "supportedengines.h" -class SearchEngine; +class SearchWidget; class MainWindow; class LineEdit; @@ -49,16 +49,16 @@ QT_BEGIN_NAMESPACE class QTimer; QT_END_NAMESPACE -class SearchEngine : public QWidget, public Ui::search_engine{ +class SearchWidget : public QWidget, private Ui::SearchWidget{ Q_OBJECT - Q_DISABLE_COPY(SearchEngine) + 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: - SearchEngine(MainWindow *mp_mainWindow); - ~SearchEngine(); + SearchWidget(MainWindow *mp_mainWindow); + ~SearchWidget(); QString selectedCategory() const; QString selectedEngine() const; @@ -130,4 +130,4 @@ private: bool newQueryString; }; -#endif +#endif // SEARCHWIDGET_H diff --git a/src/searchengine/search.ui b/src/searchengine/searchwidget.ui similarity index 98% rename from src/searchengine/search.ui rename to src/searchengine/searchwidget.ui index ae35d4927..03e422c28 100644 --- a/src/searchengine/search.ui +++ b/src/searchengine/searchwidget.ui @@ -1,7 +1,7 @@ - search_engine - + SearchWidget + 0 From 8754fd56462ac1b9f9fc359227d26ac418ecbca5 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Wed, 26 Aug 2015 20:18:33 +0300 Subject: [PATCH 2/4] Move Search-related files into Gui. --- src/gui/gui.pri | 19 +++++-- src/gui/mainwindow.cpp | 2 +- .../search}/engineselectdlg.cpp | 0 .../search}/engineselectdlg.h | 0 .../search}/engineselectdlg.ui | 0 .../search}/pluginsourcedlg.h | 0 .../search}/pluginsourcedlg.ui | 0 .../search}/searchlistdelegate.h | 0 .../search}/searchsortmodel.h | 0 .../search}/searchtab.cpp | 0 src/{searchengine => gui/search}/searchtab.h | 0 .../search}/searchwidget.cpp | 0 .../search}/searchwidget.h | 0 .../search}/searchwidget.ui | 0 .../search}/supportedengines.h | 0 src/searchengine.qrc | 52 +++++++++++++++++++ src/searchengine/search.qrc | 52 ------------------- src/searchengine/searchengine.pri | 19 ------- src/src.pro | 8 ++- 19 files changed, 72 insertions(+), 80 deletions(-) rename src/{searchengine => gui/search}/engineselectdlg.cpp (100%) rename src/{searchengine => gui/search}/engineselectdlg.h (100%) rename src/{searchengine => gui/search}/engineselectdlg.ui (100%) rename src/{searchengine => gui/search}/pluginsourcedlg.h (100%) rename src/{searchengine => gui/search}/pluginsourcedlg.ui (100%) rename src/{searchengine => gui/search}/searchlistdelegate.h (100%) rename src/{searchengine => gui/search}/searchsortmodel.h (100%) rename src/{searchengine => gui/search}/searchtab.cpp (100%) rename src/{searchengine => gui/search}/searchtab.h (100%) rename src/{searchengine => gui/search}/searchwidget.cpp (100%) rename src/{searchengine => gui/search}/searchwidget.h (100%) rename src/{searchengine => gui/search}/searchwidget.ui (100%) rename src/{searchengine => gui/search}/supportedengines.h (100%) create mode 100644 src/searchengine.qrc delete mode 100644 src/searchengine/search.qrc delete mode 100644 src/searchengine/searchengine.pri diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 40267792d..847373a85 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -41,7 +41,14 @@ HEADERS += \ $$PWD/advancedsettings.h \ $$PWD/shutdownconfirm.h \ $$PWD/torrentmodel.h \ - $$PWD/torrentcreatordlg.h + $$PWD/torrentcreatordlg.h \ + $$PWD/search/searchwidget.h \ + $$PWD/search/searchtab.h \ + $$PWD/search/engineselectdlg.h \ + $$PWD/search/pluginsourcedlg.h \ + $$PWD/search/searchlistdelegate.h \ + $$PWD/search/supportedengines.h \ + $$PWD/search/searchsortmodel.h SOURCES += \ $$PWD/mainwindow.cpp \ @@ -72,7 +79,10 @@ SOURCES += \ $$PWD/options_imp.cpp \ $$PWD/shutdownconfirm.cpp \ $$PWD/torrentmodel.cpp \ - $$PWD/torrentcreatordlg.cpp + $$PWD/torrentcreatordlg.cpp \ + $$PWD/search/searchwidget.cpp \ + $$PWD/search/searchtab.cpp \ + $$PWD/search/engineselectdlg.cpp win32|macx { HEADERS += $$PWD/programupdater.h @@ -94,6 +104,9 @@ FORMS += \ $$PWD/autoexpandabledialog.ui \ $$PWD/statsdialog.ui \ $$PWD/options.ui \ - $$PWD/torrentcreatordlg.ui + $$PWD/torrentcreatordlg.ui \ + $$PWD/search/searchwidget.ui \ + $$PWD/search/engineselectdlg.ui \ + $$PWD/search/pluginsourcedlg.ui RESOURCES += $$PWD/about.qrc diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 953392430..b2c121285 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -53,7 +53,7 @@ #include "torrentcreatordlg.h" #include "downloadfromurldlg.h" #include "addnewtorrentdialog.h" -#include "searchwidget.h" +#include "search/searchwidget.h" #include "rss_imp.h" #include "base/bittorrent/session.h" #include "base/bittorrent/sessionstatus.h" diff --git a/src/searchengine/engineselectdlg.cpp b/src/gui/search/engineselectdlg.cpp similarity index 100% rename from src/searchengine/engineselectdlg.cpp rename to src/gui/search/engineselectdlg.cpp diff --git a/src/searchengine/engineselectdlg.h b/src/gui/search/engineselectdlg.h similarity index 100% rename from src/searchengine/engineselectdlg.h rename to src/gui/search/engineselectdlg.h diff --git a/src/searchengine/engineselectdlg.ui b/src/gui/search/engineselectdlg.ui similarity index 100% rename from src/searchengine/engineselectdlg.ui rename to src/gui/search/engineselectdlg.ui diff --git a/src/searchengine/pluginsourcedlg.h b/src/gui/search/pluginsourcedlg.h similarity index 100% rename from src/searchengine/pluginsourcedlg.h rename to src/gui/search/pluginsourcedlg.h diff --git a/src/searchengine/pluginsourcedlg.ui b/src/gui/search/pluginsourcedlg.ui similarity index 100% rename from src/searchengine/pluginsourcedlg.ui rename to src/gui/search/pluginsourcedlg.ui diff --git a/src/searchengine/searchlistdelegate.h b/src/gui/search/searchlistdelegate.h similarity index 100% rename from src/searchengine/searchlistdelegate.h rename to src/gui/search/searchlistdelegate.h diff --git a/src/searchengine/searchsortmodel.h b/src/gui/search/searchsortmodel.h similarity index 100% rename from src/searchengine/searchsortmodel.h rename to src/gui/search/searchsortmodel.h diff --git a/src/searchengine/searchtab.cpp b/src/gui/search/searchtab.cpp similarity index 100% rename from src/searchengine/searchtab.cpp rename to src/gui/search/searchtab.cpp diff --git a/src/searchengine/searchtab.h b/src/gui/search/searchtab.h similarity index 100% rename from src/searchengine/searchtab.h rename to src/gui/search/searchtab.h diff --git a/src/searchengine/searchwidget.cpp b/src/gui/search/searchwidget.cpp similarity index 100% rename from src/searchengine/searchwidget.cpp rename to src/gui/search/searchwidget.cpp diff --git a/src/searchengine/searchwidget.h b/src/gui/search/searchwidget.h similarity index 100% rename from src/searchengine/searchwidget.h rename to src/gui/search/searchwidget.h diff --git a/src/searchengine/searchwidget.ui b/src/gui/search/searchwidget.ui similarity index 100% rename from src/searchengine/searchwidget.ui rename to src/gui/search/searchwidget.ui diff --git a/src/searchengine/supportedengines.h b/src/gui/search/supportedengines.h similarity index 100% rename from src/searchengine/supportedengines.h rename to src/gui/search/supportedengines.h diff --git a/src/searchengine.qrc b/src/searchengine.qrc new file mode 100644 index 000000000..a3022d37f --- /dev/null +++ b/src/searchengine.qrc @@ -0,0 +1,52 @@ + + + searchengine/nova/fix_encoding.py + searchengine/nova/helpers.py + searchengine/nova/nova2.py + searchengine/nova/nova2dl.py + searchengine/nova/novaprinter.py + searchengine/nova/socks.py + searchengine/nova/engines/btdigg.png + searchengine/nova/engines/btdigg.py + searchengine/nova/engines/demonoid.png + searchengine/nova/engines/demonoid.py + searchengine/nova/engines/extratorrent.png + searchengine/nova/engines/extratorrent.py + searchengine/nova/engines/kickasstorrents.png + searchengine/nova/engines/kickasstorrents.py + searchengine/nova/engines/legittorrents.png + searchengine/nova/engines/legittorrents.py + searchengine/nova/engines/mininova.png + searchengine/nova/engines/mininova.py + searchengine/nova/engines/piratebay.png + searchengine/nova/engines/piratebay.py + searchengine/nova/engines/torrentreactor.png + searchengine/nova/engines/torrentreactor.py + searchengine/nova/engines/torrentz.png + searchengine/nova/engines/torrentz.py + searchengine/nova3/helpers.py + searchengine/nova3/nova2.py + searchengine/nova3/nova2dl.py + searchengine/nova3/novaprinter.py + searchengine/nova3/sgmllib3.py + searchengine/nova3/socks.py + searchengine/nova3/engines/btdigg.png + searchengine/nova3/engines/btdigg.py + searchengine/nova3/engines/demonoid.png + searchengine/nova3/engines/demonoid.py + searchengine/nova3/engines/extratorrent.png + searchengine/nova3/engines/extratorrent.py + searchengine/nova3/engines/kickasstorrents.png + searchengine/nova3/engines/kickasstorrents.py + searchengine/nova3/engines/legittorrents.png + searchengine/nova3/engines/legittorrents.py + searchengine/nova3/engines/mininova.png + searchengine/nova3/engines/mininova.py + searchengine/nova3/engines/piratebay.png + searchengine/nova3/engines/piratebay.py + searchengine/nova3/engines/torrentreactor.png + searchengine/nova3/engines/torrentreactor.py + searchengine/nova3/engines/torrentz.png + searchengine/nova3/engines/torrentz.py + + diff --git a/src/searchengine/search.qrc b/src/searchengine/search.qrc deleted file mode 100644 index a1aa7edc9..000000000 --- a/src/searchengine/search.qrc +++ /dev/null @@ -1,52 +0,0 @@ - - - nova/fix_encoding.py - nova/helpers.py - nova/nova2.py - nova/nova2dl.py - nova/novaprinter.py - nova/socks.py - nova/engines/btdigg.png - nova/engines/btdigg.py - nova/engines/demonoid.png - nova/engines/demonoid.py - nova/engines/extratorrent.png - nova/engines/extratorrent.py - nova/engines/kickasstorrents.png - nova/engines/kickasstorrents.py - nova/engines/legittorrents.png - nova/engines/legittorrents.py - nova/engines/mininova.png - nova/engines/mininova.py - nova/engines/piratebay.png - nova/engines/piratebay.py - nova/engines/torrentreactor.png - nova/engines/torrentreactor.py - nova/engines/torrentz.png - nova/engines/torrentz.py - nova3/helpers.py - nova3/nova2.py - nova3/nova2dl.py - nova3/novaprinter.py - nova3/sgmllib3.py - nova3/socks.py - nova3/engines/btdigg.png - nova3/engines/btdigg.py - nova3/engines/demonoid.png - nova3/engines/demonoid.py - nova3/engines/extratorrent.png - nova3/engines/extratorrent.py - nova3/engines/kickasstorrents.png - nova3/engines/kickasstorrents.py - nova3/engines/legittorrents.png - nova3/engines/legittorrents.py - nova3/engines/mininova.png - nova3/engines/mininova.py - nova3/engines/piratebay.png - nova3/engines/piratebay.py - nova3/engines/torrentreactor.png - nova3/engines/torrentreactor.py - nova3/engines/torrentz.png - nova3/engines/torrentz.py - - diff --git a/src/searchengine/searchengine.pri b/src/searchengine/searchengine.pri deleted file mode 100644 index 9a2812827..000000000 --- a/src/searchengine/searchengine.pri +++ /dev/null @@ -1,19 +0,0 @@ -INCLUDEPATH += $$PWD - -FORMS += $$PWD/searchwidget.ui \ - $$PWD/engineselectdlg.ui \ - $$PWD/pluginsourcedlg.ui - -HEADERS += $$PWD/searchwidget.h \ - $$PWD/searchtab.h \ - $$PWD/engineselectdlg.h \ - $$PWD/pluginsourcedlg.h \ - $$PWD/searchlistdelegate.h \ - $$PWD/supportedengines.h \ - $$PWD/searchsortmodel.h - -SOURCES += $$PWD/searchwidget.cpp \ - $$PWD/searchtab.cpp \ - $$PWD/engineselectdlg.cpp - -RESOURCES += $$PWD/search.qrc diff --git a/src/src.pro b/src/src.pro index 7d87e9f64..b1cd75c7e 100644 --- a/src/src.pro +++ b/src/src.pro @@ -63,16 +63,14 @@ INCLUDEPATH += $$PWD include(app/app.pri) include(base/base.pri) !nowebui: include(webui/webui.pri) -!nogui { - include(gui/gui.pri) - include(searchengine/searchengine.pri) -} +!nogui: include(gui/gui.pri) # Resource files QMAKE_RESOURCE_FLAGS += -compress 9 -threshold 5 RESOURCES += \ icons.qrc \ - lang.qrc + lang.qrc \ + searchengine.qrc # Translations TRANSLATIONS = \ From 54979e6b53f8e3a3528f032baaeafdd5278bec64 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 27 Aug 2015 14:25:14 +0300 Subject: [PATCH 3/4] Move basic search-related code into Core. Also use qBittorrent torrent file download routines instead of nova2dl.py script. --- src/base/base.pri | 6 +- src/base/bittorrent/session.cpp | 4 + src/base/searchengine.cpp | 657 ++++++++++++++++++ src/base/searchengine.h | 136 ++++ src/base/utils/fs.cpp | 12 - src/base/utils/fs.h | 2 +- src/gui/addnewtorrentdialog.cpp | 4 + src/gui/gui.pri | 7 +- src/gui/mainwindow.cpp | 2 + src/gui/search/engineselectdlg.cpp | 519 -------------- src/gui/search/pluginselectdlg.cpp | 408 +++++++++++ .../{engineselectdlg.h => pluginselectdlg.h} | 64 +- ...{engineselectdlg.ui => pluginselectdlg.ui} | 10 +- src/gui/search/searchtab.cpp | 3 +- src/gui/search/searchwidget.cpp | 398 +++-------- src/gui/search/searchwidget.h | 64 +- src/gui/search/searchwidget.ui | 6 +- src/gui/search/supportedengines.h | 186 ----- src/searchengine.qrc | 2 - src/searchengine/nova/nova2dl.py | 61 -- src/searchengine/nova3/nova2dl.py | 61 -- src/src.pro | 2 +- 22 files changed, 1379 insertions(+), 1235 deletions(-) create mode 100644 src/base/searchengine.cpp create mode 100644 src/base/searchengine.h delete mode 100644 src/gui/search/engineselectdlg.cpp create mode 100644 src/gui/search/pluginselectdlg.cpp rename src/gui/search/{engineselectdlg.h => pluginselectdlg.h} (61%) rename src/gui/search/{engineselectdlg.ui => pluginselectdlg.ui} (93%) delete mode 100644 src/gui/search/supportedengines.h delete mode 100644 src/searchengine/nova/nova2dl.py delete mode 100644 src/searchengine/nova3/nova2dl.py diff --git a/src/base/base.pri b/src/base/base.pri index bb4643dd9..3ce02d04d 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -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 diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 71fd9fe62..5b15a99ec 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -968,6 +968,10 @@ bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) 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)); diff --git a/src/base/searchengine.cpp b/src/base/searchengine.cpp new file mode 100644 index 000000000..19d056fdf --- /dev/null +++ b/src/base/searchengine.cpp @@ -0,0 +1,657 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * 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. + */ + +#include +#include +#include +#include +#include +#include + +#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 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 lines = output.split('\n'); + if (!m_searchResultLineTruncated.isEmpty()) + lines.prepend(m_searchResultLineTruncated + lines.takeFirst()); + m_searchResultLineTruncated = lines.takeLast().trimmed(); + + QList 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 updateInfo; + bool dataCorrect = false; + QList lines = info.split('\n'); + foreach (QByteArray line, lines) { + line = line.trimmed(); + if (line.isEmpty()) continue; + if (line.startsWith("#")) continue; + + QList 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 SearchEngine::initializeCategoryNames() +{ + QHash 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; +} diff --git a/src/base/searchengine.h b/src/base/searchengine.h new file mode 100644 index 000000000..908e3d0d5 --- /dev/null +++ b/src/base/searchengine.h @@ -0,0 +1,136 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * 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. + */ + +#ifndef SEARCHENGINE_H +#define SEARCHENGINE_H + +#include +#include +#include +#include + +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 &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 &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 initializeCategoryNames(); + + static const QHash m_categoryNames; + + const QString m_updateUrl; + + QHash m_plugins; + QProcess *m_searchProcess; + bool m_searchStopped; + QTimer *m_searchTimeout; + QByteArray m_searchResultLineTruncated; +}; + +#endif // SEARCHENGINE_H diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index da5349511..f411e8668 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -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()); diff --git a/src/base/utils/fs.h b/src/base/utils/fs.h index 9f04a2726..77d0833d5 100644 --- a/src/base/utils/fs.h +++ b/src/base/utils/fs.h @@ -65,7 +65,7 @@ namespace Utils QString QDesktopServicesCacheLocation(); QString QDesktopServicesDownloadLocation(); /* End of Qt4 code */ - QString searchEngineLocation(); + QString cacheLocation(); } } diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 7bee8afa5..fe7ff46b3 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -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); diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 847373a85..13ab015f4 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -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 diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index b2c121285..25ba17446 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -34,6 +34,7 @@ #include "notifications.h" #endif +#include #include #include #include @@ -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" diff --git a/src/gui/search/engineselectdlg.cpp b/src/gui/search/engineselectdlg.cpp deleted file mode 100644 index d51f4cb2d..000000000 --- a/src/gui/search/engineselectdlg.cpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef QBT_USES_QT5 -#include -#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 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 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 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 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; icolumnCount(); ++i) { - item->setData(i, Qt::ForegroundRole, QVariant(QColor(color))); - } -} - -QList EngineSelectDlg::findItemsWithUrl(QString url) { - QList res; - for (int i=0; itopLevelItemCount(); ++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 res; - for (int i=0; itopLevelItemCount(); ++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 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 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)); - } -} diff --git a/src/gui/search/pluginselectdlg.cpp b/src/gui/search/pluginselectdlg.cpp new file mode 100644 index 000000000..9f5c5e175 --- /dev/null +++ b/src/gui/search/pluginselectdlg.cpp @@ -0,0 +1,408 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * 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 "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 +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef QBT_USES_QT5 +#include +#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)), SLOT(checkForUpdatesFinished(QHash))); + 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 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 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 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 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; icolumnCount(); ++i) { + item->setData(i, Qt::ForegroundRole, QVariant(QColor(color))); + } +} + +QList PluginSelectDlg::findItemsWithUrl(QString url) { + QList res; + for (int i=0; itopLevelItemCount(); ++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; itopLevelItemCount(); ++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 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 &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)); +} diff --git a/src/gui/search/engineselectdlg.h b/src/gui/search/pluginselectdlg.h similarity index 61% rename from src/gui/search/engineselectdlg.h rename to src/gui/search/pluginselectdlg.h index a98f54bad..1292bfe49 100644 --- a/src/gui/search/engineselectdlg.h +++ b/src/gui/search/pluginselectdlg.h @@ -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 + * 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 @@ -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 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 &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 diff --git a/src/gui/search/engineselectdlg.ui b/src/gui/search/pluginselectdlg.ui similarity index 93% rename from src/gui/search/engineselectdlg.ui rename to src/gui/search/pluginselectdlg.ui index 4ba4f2f54..a87d23c6c 100644 --- a/src/gui/search/engineselectdlg.ui +++ b/src/gui/search/pluginselectdlg.ui @@ -1,7 +1,7 @@ - EngineSelectDlg - + PluginSelectDlg + 0 @@ -18,7 +18,7 @@ - + 75 @@ -27,7 +27,7 @@ - Installed search engines: + Installed search plugins: @@ -73,7 +73,7 @@ - + true diff --git a/src/gui/search/searchtab.cpp b/src/gui/search/searchtab.cpp index 5d2d69624..dba224ff1 100644 --- a/src/gui/search/searchtab.cpp +++ b/src/gui/search/searchtab.cpp @@ -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() { diff --git a/src/gui/search/searchwidget.cpp b/src/gui/search/searchwidget.cpp index b28c6857a..c4b70931d 100644 --- a/src/gui/search/searchwidget.cpp +++ b/src/gui/search/searchwidget.cpp @@ -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 + * 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 @@ -43,6 +44,8 @@ #include #include #include +#include +#include #ifdef Q_OS_WIN #include @@ -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)), SLOT(appendSearchResults(QList))); + 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 (%1):", "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 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 (%1):", "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")); } -// 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::searchFailed() { - if (activeSearchTab.isNull()) { - if (searchProcess->state() != QProcess::NotRunning) { + 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 - searchProcess->kill(); + activeSearchTab->status = tr("Search aborted"); #else - searchProcess->terminate(); + activeSearchTab->status = tr("An error occurred during search..."); #endif - searchProcess->waitForFinished(1000); - } - if (searchTimeout->isActive()) - searchTimeout->stop(); - search_stopped = true; +} + +// SLOT to append one line to search results list +void SearchWidget::appendSearchResults(const QList &results) +{ + if (activeSearchTab.isNull()) { + 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 (%1):", "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"); } } diff --git a/src/gui/search/searchwidget.h b/src/gui/search/searchwidget.h index 9751a620b..c77ac1e6e 100644 --- a/src/gui/search/searchwidget.h +++ b/src/gui/search/searchwidget.h @@ -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 + * 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 @@ -31,19 +32,19 @@ #ifndef SEARCHWIDGET_H #define SEARCHWIDGET_H -#include #include #include #include #include #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 &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 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 currentSearchTab; // Selected tab QPointer activeSearchTab; // Tab with running search QList > all_tab; // To store all tabs - const SearchCategories full_cat_names; MainWindow *mp_mainWindow; bool newQueryString; }; diff --git a/src/gui/search/searchwidget.ui b/src/gui/search/searchwidget.ui index 03e422c28..d6bfa5877 100644 --- a/src/gui/search/searchwidget.ui +++ b/src/gui/search/searchwidget.ui @@ -20,7 +20,7 @@ - + @@ -142,9 +142,9 @@ - + - Search engines... + Search plugins... diff --git a/src/gui/search/supportedengines.h b/src/gui/search/supportedengines.h deleted file mode 100644 index 7835aefdb..000000000 --- a/src/gui/search/supportedengines.h +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include - -#include "base/utils/fs.h" -#include "base/utils/misc.h" -#include "base/preferences.h" - -class SearchCategories: public QObject, public QHash { - 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 { - 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 diff --git a/src/searchengine.qrc b/src/searchengine.qrc index a3022d37f..e9fe88c57 100644 --- a/src/searchengine.qrc +++ b/src/searchengine.qrc @@ -3,7 +3,6 @@ searchengine/nova/fix_encoding.py searchengine/nova/helpers.py searchengine/nova/nova2.py - searchengine/nova/nova2dl.py searchengine/nova/novaprinter.py searchengine/nova/socks.py searchengine/nova/engines/btdigg.png @@ -26,7 +25,6 @@ searchengine/nova/engines/torrentz.py searchengine/nova3/helpers.py searchengine/nova3/nova2.py - searchengine/nova3/nova2dl.py searchengine/nova3/novaprinter.py searchengine/nova3/sgmllib3.py searchengine/nova3/socks.py diff --git a/src/searchengine/nova/nova2dl.py b/src/searchengine/nova/nova2dl.py deleted file mode 100644 index 31681a268..000000000 --- a/src/searchengine/nova/nova2dl.py +++ /dev/null @@ -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) diff --git a/src/searchengine/nova3/nova2dl.py b/src/searchengine/nova3/nova2dl.py deleted file mode 100644 index 31681a268..000000000 --- a/src/searchengine/nova3/nova2dl.py +++ /dev/null @@ -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) diff --git a/src/src.pro b/src/src.pro index b1cd75c7e..5ced20049 100644 --- a/src/src.pro +++ b/src/src.pro @@ -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 From ac365c5efba27e26aed01e617124d6fc4c058178 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 24 Sep 2015 10:33:02 +0300 Subject: [PATCH 4/4] Fix coding style (Issue #2192). --- src/base/searchengine.cpp | 10 +- src/base/searchengine.h | 5 +- src/gui/gui.pri | 5 +- src/gui/search/pluginselectdlg.cpp | 520 ++++++++++++++------------ src/gui/search/pluginselectdlg.h | 35 +- src/gui/search/pluginsourcedlg.cpp | 51 +++ src/gui/search/pluginsourcedlg.h | 34 +- src/gui/search/searchlistdelegate.cpp | 74 ++++ src/gui/search/searchlistdelegate.h | 47 +-- src/gui/search/searchsortmodel.cpp | 54 +++ src/gui/search/searchsortmodel.h | 67 ++-- src/gui/search/searchtab.cpp | 146 ++++---- src/gui/search/searchtab.h | 60 +-- src/gui/search/searchwidget.cpp | 229 ++++++------ src/gui/search/searchwidget.h | 54 ++- src/gui/search/searchwidget.ui | 6 +- 16 files changed, 795 insertions(+), 602 deletions(-) create mode 100644 src/gui/search/pluginsourcedlg.cpp create mode 100644 src/gui/search/searchlistdelegate.cpp create mode 100644 src/gui/search/searchsortmodel.cpp diff --git a/src/base/searchengine.cpp b/src/base/searchengine.cpp index 19d056fdf..624f47d5a 100644 --- a/src/base/searchengine.cpp +++ b/src/base/searchengine.cpp @@ -30,8 +30,8 @@ #include #include #include -#include #include +#include #include #include "base/utils/fs.h" @@ -62,8 +62,8 @@ static inline void removePythonScriptIfExists(const QString &scriptPath) const QHash 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")) + : m_updateUrl(QString("https://raw.github.com/qbittorrent/qBittorrent/master/src/searchengine/%1/engines/").arg(Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova")) + , m_searchStopped(false) { updateNova(); @@ -71,7 +71,7 @@ SearchEngine::SearchEngine() 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))); + connect(m_searchProcess, SIGNAL(finished(int)), this, SLOT(processFinished(int))); m_searchTimeout = new QTimer(this); m_searchTimeout->setSingleShot(true); @@ -315,7 +315,7 @@ QString SearchEngine::engineLocation() // 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) +void SearchEngine::processFinished(int exitcode) { m_searchTimeout->stop(); diff --git a/src/base/searchengine.h b/src/base/searchengine.h index 908e3d0d5..084affdf3 100644 --- a/src/base/searchengine.h +++ b/src/base/searchengine.h @@ -30,11 +30,12 @@ #ifndef SEARCHENGINE_H #define SEARCHENGINE_H +#include #include #include -#include #include +class QProcess; class QTimer; struct PluginInfo @@ -104,7 +105,7 @@ signals: private slots: void onTimeout(); void readSearchOutput(); - void processFinished(int exitcode, QProcess::ExitStatus); + void processFinished(int exitcode); void versionInfoDownloaded(const QString &url, const QByteArray &data); void versionInfoDownloadFailed(const QString &url, const QString &reason); void pluginDownloaded(const QString &url, QString filePath); diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 13ab015f4..d5c285fef 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -81,7 +81,10 @@ SOURCES += \ $$PWD/torrentcreatordlg.cpp \ $$PWD/search/searchwidget.cpp \ $$PWD/search/searchtab.cpp \ - $$PWD/search/pluginselectdlg.cpp + $$PWD/search/pluginselectdlg.cpp \ + $$PWD/search/pluginsourcedlg.cpp \ + $$PWD/search/searchlistdelegate.cpp \ + $$PWD/search/searchsortmodel.cpp win32|macx { HEADERS += $$PWD/programupdater.h diff --git a/src/gui/search/pluginselectdlg.cpp b/src/gui/search/pluginselectdlg.cpp index 9f5c5e175..ecea3068f 100644 --- a/src/gui/search/pluginselectdlg.cpp +++ b/src/gui/search/pluginselectdlg.cpp @@ -29,18 +29,6 @@ * 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 #include #include #include @@ -53,224 +41,264 @@ #include #endif -enum PluginColumns {PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_URL, PLUGIN_STATE, PLUGIN_ID}; +#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 "pluginselectdlg.h" + +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); + 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)); + // 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)), SLOT(checkForUpdatesFinished(QHash))); - connect(m_pluginManager, SIGNAL(checkForUpdatesFailed(QString)), SLOT(checkForUpdatesFailed(QString))); - - show(); + 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)), SLOT(checkForUpdatesFinished(QHash))); + connect(m_pluginManager, SIGNAL(checkForUpdatesFailed(QString)), SLOT(checkForUpdatesFailed(QString))); + + show(); } -PluginSelectDlg::~PluginSelectDlg() { - emit pluginsChanged(); +PluginSelectDlg::~PluginSelectDlg() +{ + emit pluginsChanged(); } -void PluginSelectDlg::dropEvent(QDropEvent *event) { - event->acceptProposedAction(); - QStringList files; - if (event->mimeData()->hasUrls()) { - const QList 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(); - } +void PluginSelectDlg::dropEvent(QDropEvent *event) +{ + event->acceptProposedAction(); + + QStringList files; + if (event->mimeData()->hasUrls()) { + foreach (const QUrl &url, event->mimeData()->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(QLatin1String("\n")); } - } - else { - files = event->mimeData()->text().split(QString::fromUtf8("\n")); - } - if (files.isEmpty()) return; + if (files.isEmpty()) return; - foreach (QString file, files) { - qDebug("dropped %s", qPrintable(file)); - startAsyncOp(); - m_pluginManager->installPlugin(file); - } + 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::dragEnterEvent(QDragEnterEvent *event) +{ + QString mime; + foreach (mime, event->mimeData()->formats()) { + qDebug("mimeData: %s", qPrintable(mime)); + } + + if (event->mimeData()->hasFormat(QLatin1String("text/plain")) || event->mimeData()->hasFormat(QLatin1String("text/uri-list"))) { + event->acceptProposedAction(); + } } -void PluginSelectDlg::on_updateButton_clicked() { - startAsyncOp(); - m_pluginManager->checkForUpdates(); +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::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 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::displayContextMenu(const QPoint&) +{ + QMenu myContextMenu(this); + // Enable/disable pause/start action given the DL state + QList 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_closeButton_clicked() +{ + close(); } -void PluginSelectDlg::on_actionUninstall_triggered() { - QList 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; +void PluginSelectDlg::on_actionUninstall_triggered() +{ + bool error = false; + foreach (QTreeWidgetItem *item, pluginsTree->selectedItems()) { + 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"); + } } - } - 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")); + + 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 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"); +void PluginSelectDlg::enableSelection(bool enable) +{ + foreach (QTreeWidgetItem *item, pluginsTree->selectedItems()) { + 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; icolumnCount(); ++i) { - item->setData(i, Qt::ForegroundRole, QVariant(QColor(color))); - } +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 PluginSelectDlg::findItemsWithUrl(QString url) { - QList res; - for (int i=0; itopLevelItemCount(); ++i) { - QTreeWidgetItem *item = pluginsTree->topLevelItem(i); - if (url.startsWith(item->text(PLUGIN_URL), Qt::CaseInsensitive)) - res << item; - } - return res; +QList PluginSelectDlg::findItemsWithUrl(QString url) +{ + QList 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; itopLevelItemCount(); ++i) { - QTreeWidgetItem *item = pluginsTree->topLevelItem(i); - if (id == item->text(PLUGIN_ID)) - return item; - } - return 0; +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::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::addNewPlugin(QString pluginName) +{ + QTreeWidgetItem *item = new QTreeWidgetItem(pluginsTree); + PluginInfo *plugin = m_pluginManager->pluginInfo(pluginName); + 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() @@ -287,75 +315,77 @@ void PluginSelectDlg::finishAsyncOp() 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::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); +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 (ok && !url.isEmpty() && !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); + if (ok && !url.isEmpty()) { + 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::askForLocalPlugin() +{ + QStringList pathsList = QFileDialog::getOpenFileNames( + 0, tr("Select search plugins"), QDir::homePath(), + tr("qBittorrent search plugin") + QLatin1String(" (*.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 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))); +void PluginSelectDlg::iconDownloaded(const QString &url, QString filePath) +{ + filePath = Utils::Fs::fromNativePath(filePath); + + // Icon downloaded + QImage fileIcon; + if (fileIcon.load(filePath)) { + foreach (QTreeWidgetItem *item, findItemsWithUrl(url)) { + 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); + // 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::iconDownloadFailed(const QString &url, const QString &reason) +{ + qDebug("Could not download favicon: %s, reason: %s", qPrintable(url), qPrintable(reason)); } void PluginSelectDlg::checkForUpdatesFinished(const QHash &updateInfo) diff --git a/src/gui/search/pluginselectdlg.h b/src/gui/search/pluginselectdlg.h index 1292bfe49..85accdab8 100644 --- a/src/gui/search/pluginselectdlg.h +++ b/src/gui/search/pluginselectdlg.h @@ -34,42 +34,36 @@ #include "ui_pluginselectdlg.h" -QT_BEGIN_NAMESPACE class QDropEvent; -QT_END_NAMESPACE - class SearchEngine; -class PluginSelectDlg : public QDialog, public Ui::PluginSelectDlg +class PluginSelectDlg: public QDialog, private Ui::PluginSelectDlg { - Q_OBJECT - - private: - SearchEngine *m_pluginManager; - int m_asyncOps; + Q_OBJECT - public: - PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0); +public: + explicit PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0); ~PluginSelectDlg(); + QList findItemsWithUrl(QString url); QTreeWidgetItem* findItemWithID(QString id); - signals: +signals: void pluginsChanged(); - protected: +protected: void dropEvent(QDropEvent *event); void dragEnterEvent(QDragEnterEvent *event); - private slots: +private slots: + void on_actionUninstall_triggered(); + void on_updateButton_clicked(); + void on_installButton_clicked(); void on_closeButton_clicked(); void togglePluginState(QTreeWidgetItem*, int); void setRowColor(int row, QString color); void displayContextMenu(const QPoint& pos); void enableSelection(bool enable); - void on_actionUninstall_triggered(); - void on_updateButton_clicked(); - void on_installButton_clicked(); void askForLocalPlugin(); void askForPluginUrl(); void iconDownloaded(const QString &url, QString filePath); @@ -82,11 +76,14 @@ class PluginSelectDlg : public QDialog, public Ui::PluginSelectDlg void pluginUpdated(const QString &name); void pluginUpdateFailed(const QString &name, const QString &reason); - private: +private: void loadSupportedSearchPlugins(); - void addNewPlugin(QString engine_name); + void addNewPlugin(QString pluginName); void startAsyncOp(); void finishAsyncOp(); + + SearchEngine *m_pluginManager; + int m_asyncOps; }; #endif // PLUGINSELECTDLG_H diff --git a/src/gui/search/pluginsourcedlg.cpp b/src/gui/search/pluginsourcedlg.cpp new file mode 100644 index 000000000..8e3e6aa44 --- /dev/null +++ b/src/gui/search/pluginsourcedlg.cpp @@ -0,0 +1,51 @@ +/* + * Bittorrent Client using Qt 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 "pluginsourcedlg.h" + +PluginSourceDlg::PluginSourceDlg(QWidget *parent) + : QDialog(parent) +{ + setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + show(); +} + +void PluginSourceDlg::on_localButton_clicked() +{ + emit askForLocalFile(); + close(); +} + +void PluginSourceDlg::on_urlButton_clicked() +{ + emit askForUrl(); + close(); +} diff --git a/src/gui/search/pluginsourcedlg.h b/src/gui/search/pluginsourcedlg.h index cfabee362..4bfa514e1 100644 --- a/src/gui/search/pluginsourcedlg.h +++ b/src/gui/search/pluginsourcedlg.h @@ -1,5 +1,5 @@ /* - * Bittorrent Client using Qt4 and libtorrent. + * Bittorrent Client using Qt and libtorrent. * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -34,32 +34,20 @@ #include #include "ui_pluginsourcedlg.h" -class PluginSourceDlg: public QDialog, private Ui::PluginSourceDlg { - Q_OBJECT +class PluginSourceDlg: public QDialog, private Ui::PluginSourceDlg +{ + Q_OBJECT - signals: +public: + explicit PluginSourceDlg(QWidget *parent = 0); + +signals: void askForUrl(); void askForLocalFile(); - protected slots: - void on_localButton_clicked() { - emit askForLocalFile(); - close(); - } - - void on_urlButton_clicked() { - emit askForUrl(); - close(); - } - - public: - PluginSourceDlg(QWidget* parent): QDialog(parent) { - setupUi(this); - setAttribute(Qt::WA_DeleteOnClose); - show(); - } - - ~PluginSourceDlg() {} +private slots: + void on_localButton_clicked(); + void on_urlButton_clicked(); }; #endif // PLUGINSOURCEDLG_H diff --git a/src/gui/search/searchlistdelegate.cpp b/src/gui/search/searchlistdelegate.cpp new file mode 100644 index 000000000..95250d99d --- /dev/null +++ b/src/gui/search/searchlistdelegate.cpp @@ -0,0 +1,74 @@ +/* + * Bittorrent Client using Qt 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 +#include +#include +#include + +#include "base/utils/misc.h" +#include "searchsortmodel.h" +#include "searchlistdelegate.h" + +SearchListDelegate::SearchListDelegate(QObject *parent) + : QItemDelegate(parent) +{ +} + +void SearchListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + painter->save(); + + QStyleOptionViewItemV2 opt = QItemDelegate::setOptions(index, option); + switch(index.column()) { + case SearchSortModel::SIZE: + QItemDelegate::drawBackground(painter, opt, index); + QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); + break; + case SearchSortModel::SEEDS: + QItemDelegate::drawBackground(painter, opt, index); + QItemDelegate::drawDisplay(painter, opt, option.rect, (index.data().toLongLong() >= 0) ? index.data().toString() : tr("Unknown")); + break; + case SearchSortModel::LEECHS: + QItemDelegate::drawBackground(painter, opt, index); + QItemDelegate::drawDisplay(painter, opt, option.rect, (index.data().toLongLong() >= 0) ? index.data().toString() : tr("Unknown")); + break; + default: + QItemDelegate::paint(painter, option, index); + } + + painter->restore(); +} + +QWidget *SearchListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const +{ + // No editor here + return 0; +} diff --git a/src/gui/search/searchlistdelegate.h b/src/gui/search/searchlistdelegate.h index 2986e6d41..27d813735 100644 --- a/src/gui/search/searchlistdelegate.h +++ b/src/gui/search/searchlistdelegate.h @@ -1,5 +1,5 @@ /* - * Bittorrent Client using Qt4 and libtorrent. + * Bittorrent Client using Qt and libtorrent. * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -32,47 +32,14 @@ #define SEARCHLISTDELEGATE_H #include -#include -#include -#include -#include -#include "base/utils/misc.h" -#include "searchwidget.h" -class SearchListDelegate: public QItemDelegate { - Q_OBJECT +class SearchListDelegate: public QItemDelegate +{ +public: + explicit SearchListDelegate(QObject *parent = 0); - public: - SearchListDelegate(QObject *parent=0) : QItemDelegate(parent) {} - - ~SearchListDelegate() {} - - void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const { - painter->save(); - QStyleOptionViewItemV2 opt = QItemDelegate::setOptions(index, option); - switch(index.column()) { - case SearchSortModel::SIZE: - QItemDelegate::drawBackground(painter, opt, index); - QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); - break; - case SearchSortModel::SEEDS: - QItemDelegate::drawBackground(painter, opt, index); - QItemDelegate::drawDisplay(painter, opt, option.rect, (index.data().toLongLong() >= 0) ? index.data().toString() : tr("Unknown")); - break; - case SearchSortModel::LEECHS: - QItemDelegate::drawBackground(painter, opt, index); - QItemDelegate::drawDisplay(painter, opt, option.rect, (index.data().toLongLong() >= 0) ? index.data().toString() : tr("Unknown")); - break; - default: - QItemDelegate::paint(painter, option, index); - } - painter->restore(); - } - - QWidget* createEditor(QWidget*, const QStyleOptionViewItem &, const QModelIndex &) const { - // No editor here - return 0; - } + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + QWidget* createEditor(QWidget*, const QStyleOptionViewItem &, const QModelIndex &) const; }; #endif diff --git a/src/gui/search/searchsortmodel.cpp b/src/gui/search/searchsortmodel.cpp new file mode 100644 index 000000000..4245fe389 --- /dev/null +++ b/src/gui/search/searchsortmodel.cpp @@ -0,0 +1,54 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2013 sledgehammer999 + * + * 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 "searchsortmodel.h" + +SearchSortModel::SearchSortModel(QObject *parent) + : QSortFilterProxyModel(parent) +{ +} + +bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const +{ + if ((sortColumn() == NAME) || (sortColumn() == ENGINE_URL)) { + QVariant vL = sourceModel()->data(left); + QVariant vR = sourceModel()->data(right); + if (!(vL.isValid() && vR.isValid())) + return QSortFilterProxyModel::lessThan(left, right); + Q_ASSERT(vL.isValid()); + Q_ASSERT(vR.isValid()); + + bool res = false; + if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) + return res; + + return QSortFilterProxyModel::lessThan(left, right); + } + + return QSortFilterProxyModel::lessThan(left, right); +} diff --git a/src/gui/search/searchsortmodel.h b/src/gui/search/searchsortmodel.h index 9b58e9964..6f85fa449 100644 --- a/src/gui/search/searchsortmodel.h +++ b/src/gui/search/searchsortmodel.h @@ -1,35 +1,56 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2013 sledgehammer999 + * + * 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 SEARCHSORTMODEL_H #define SEARCHSORTMODEL_H #include #include "base/utils/string.h" -class SearchSortModel : public QSortFilterProxyModel { - Q_OBJECT - +class SearchSortModel: public QSortFilterProxyModel +{ public: - enum SearchColumn { NAME, SIZE, SEEDS, LEECHS, ENGINE_URL, DL_LINK, DESC_LINK, NB_SEARCH_COLUMNS }; - - SearchSortModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {} + enum SearchColumn + { + NAME, + SIZE, + SEEDS, + LEECHS, + ENGINE_URL, + DL_LINK, + DESC_LINK, + NB_SEARCH_COLUMNS + }; + + explicit SearchSortModel(QObject *parent = 0); protected: - virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const { - if (sortColumn() == NAME || sortColumn() == ENGINE_URL) { - QVariant vL = sourceModel()->data(left); - QVariant vR = sourceModel()->data(right); - if (!(vL.isValid() && vR.isValid())) - return QSortFilterProxyModel::lessThan(left, right); - Q_ASSERT(vL.isValid()); - Q_ASSERT(vR.isValid()); - - bool res = false; - if (Utils::String::naturalSort(vL.toString(), vR.toString(), res)) - return res; - - return QSortFilterProxyModel::lessThan(left, right); - } - return QSortFilterProxyModel::lessThan(left, right); - } + virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const; }; #endif // SEARCHSORTMODEL_H diff --git a/src/gui/search/searchtab.cpp b/src/gui/search/searchtab.cpp index dba224ff1..8717924ea 100644 --- a/src/gui/search/searchtab.cpp +++ b/src/gui/search/searchtab.cpp @@ -33,132 +33,140 @@ #include #include #include +#include +#include #ifdef QBT_USES_QT5 #include #endif -#include "searchtab.h" -#include "searchlistdelegate.h" #include "base/utils/misc.h" -#include "searchwidget.h" #include "base/preferences.h" +#include "searchsortmodel.h" +#include "searchlistdelegate.h" +#include "searchwidget.h" +#include "searchtab.h" -SearchTab::SearchTab(SearchWidget *parent) : QWidget(), parent(parent) +SearchTab::SearchTab(SearchWidget *parent) + : QWidget(parent) + , m_parent(parent) { - box = new QVBoxLayout(); - results_lbl = new QLabel(); - resultsBrowser = new QTreeView(); + m_box = new QVBoxLayout(this); + m_resultsLbl = new QLabel(this); + m_resultsBrowser = new QTreeView(this); #ifdef QBT_USES_QT5 // This hack fixes reordering of first column with Qt5. // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777 QTableView unused; - unused.setVerticalHeader(resultsBrowser->header()); - resultsBrowser->header()->setParent(resultsBrowser); + unused.setVerticalHeader(m_resultsBrowser->header()); + m_resultsBrowser->header()->setParent(m_resultsBrowser); unused.setVerticalHeader(new QHeaderView(Qt::Horizontal)); #endif - resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection); - box->addWidget(results_lbl); - box->addWidget(resultsBrowser); + m_resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection); + m_box->addWidget(m_resultsLbl); + m_box->addWidget(m_resultsBrowser); + + setLayout(m_box); - setLayout(box); // Set Search results list model - SearchListModel = new QStandardItemModel(0, SearchSortModel::NB_SEARCH_COLUMNS); - SearchListModel->setHeaderData(SearchSortModel::NAME, Qt::Horizontal, tr("Name", "i.e: file name")); - SearchListModel->setHeaderData(SearchSortModel::SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); - SearchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); - SearchListModel->setHeaderData(SearchSortModel::LEECHS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); - SearchListModel->setHeaderData(SearchSortModel::ENGINE_URL, Qt::Horizontal, tr("Search engine")); + m_searchListModel = new QStandardItemModel(0, SearchSortModel::NB_SEARCH_COLUMNS, this); + m_searchListModel->setHeaderData(SearchSortModel::NAME, Qt::Horizontal, tr("Name", "i.e: file name")); + m_searchListModel->setHeaderData(SearchSortModel::SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); + m_searchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); + m_searchListModel->setHeaderData(SearchSortModel::LEECHS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); + m_searchListModel->setHeaderData(SearchSortModel::ENGINE_URL, Qt::Horizontal, tr("Search engine")); - proxyModel = new SearchSortModel(); - proxyModel->setDynamicSortFilter(true); - proxyModel->setSourceModel(SearchListModel); - resultsBrowser->setModel(proxyModel); + m_proxyModel = new SearchSortModel(this); + m_proxyModel->setDynamicSortFilter(true); + m_proxyModel->setSourceModel(m_searchListModel); + m_resultsBrowser->setModel(m_proxyModel); - SearchDelegate = new SearchListDelegate(); - resultsBrowser->setItemDelegate(SearchDelegate); + m_searchDelegate = new SearchListDelegate(this); + m_resultsBrowser->setItemDelegate(m_searchDelegate); - resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column - resultsBrowser->hideColumn(SearchSortModel::DESC_LINK); + m_resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column + m_resultsBrowser->hideColumn(SearchSortModel::DESC_LINK); - resultsBrowser->setRootIsDecorated(false); - resultsBrowser->setAllColumnsShowFocus(true); - resultsBrowser->setSortingEnabled(true); + m_resultsBrowser->setRootIsDecorated(false); + m_resultsBrowser->setAllColumnsShowFocus(true); + m_resultsBrowser->setSortingEnabled(true); // Connect signals to slots (search part) - connect(resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadSelectedItem(const QModelIndex&))); + connect(m_resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadSelectedItem(const QModelIndex&))); // Load last columns width for search results list - if (!loadColWidthResultsList()) { - resultsBrowser->header()->resizeSection(0, 275); - } + if (!loadColWidthResultsList()) + m_resultsBrowser->header()->resizeSection(0, 275); // Sort by Seeds - resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder); + m_resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder); } -void SearchTab::downloadSelectedItem(const QModelIndex& index) { - QString torrent_url = proxyModel->data(proxyModel->index(index.row(), SearchSortModel::DL_LINK)).toString(); +void SearchTab::downloadSelectedItem(const QModelIndex &index) +{ + QString torrentUrl = m_proxyModel->data(m_proxyModel->index(index.row(), SearchSortModel::DL_LINK)).toString(); setRowColor(index.row(), "blue"); - parent->downloadTorrent(torrent_url); -} - -SearchTab::~SearchTab() { - delete box; - delete results_lbl; - delete resultsBrowser; - delete SearchListModel; - delete proxyModel; - delete SearchDelegate; + m_parent->downloadTorrent(torrentUrl); } -QHeaderView* SearchTab::header() const { - return resultsBrowser->header(); +QHeaderView* SearchTab::header() const +{ + return m_resultsBrowser->header(); } -bool SearchTab::loadColWidthResultsList() { +bool SearchTab::loadColWidthResultsList() +{ QString line = Preferences::instance()->getSearchColsWidth(); - if (line.isEmpty()) - return false; + if (line.isEmpty()) return false; - QStringList width_list = line.split(' '); - if (width_list.size() > SearchListModel->columnCount()) + QStringList widthList = line.split(' '); + if (widthList.size() > m_searchListModel->columnCount()) return false; - unsigned int listSize = width_list.size(); - for (unsigned int i=0; iheader()->resizeSection(i, width_list.at(i).toInt()); + unsigned int listSize = widthList.size(); + for (unsigned int i = 0; i < listSize; ++i) { + m_resultsBrowser->header()->resizeSection(i, widthList.at(i).toInt()); } return true; } -QLabel* SearchTab::getCurrentLabel() +QLabel* SearchTab::getCurrentLabel() const { - return results_lbl; + return m_resultsLbl; } -QTreeView* SearchTab::getCurrentTreeView() +QTreeView* SearchTab::getCurrentTreeView() const { - return resultsBrowser; + return m_resultsBrowser; } QSortFilterProxyModel* SearchTab::getCurrentSearchListProxy() const { - return proxyModel; + return m_proxyModel; } QStandardItemModel* SearchTab::getCurrentSearchListModel() const { - return SearchListModel; + return m_searchListModel; } // Set the color of a row in data model -void SearchTab::setRowColor(int row, QString color) { - proxyModel->setDynamicSortFilter(false); - for (int i=0; icolumnCount(); ++i) { - proxyModel->setData(proxyModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole); - } - proxyModel->setDynamicSortFilter(true); +void SearchTab::setRowColor(int row, QString color) +{ + m_proxyModel->setDynamicSortFilter(false); + for (int i = 0; i < m_proxyModel->columnCount(); ++i) { + m_proxyModel->setData(m_proxyModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole); + } + + m_proxyModel->setDynamicSortFilter(true); } +QString SearchTab::status() const +{ + return m_status; +} +void SearchTab::setStatus(const QString &value) +{ + m_status = value; +} diff --git a/src/gui/search/searchtab.h b/src/gui/search/searchtab.h index cab2ab714..5d92afeb5 100644 --- a/src/gui/search/searchtab.h +++ b/src/gui/search/searchtab.h @@ -31,48 +31,50 @@ #ifndef SEARCHTAB_H #define SEARCHTAB_H -#include -#include - -#include "searchsortmodel.h" - -#define ENGINE_URL_COLUMN 4 -#define URL_COLUMN 5 - -class SearchListDelegate; -class SearchWidget; +#include +class QLabel; class QTreeView; class QHeaderView; class QStandardItemModel; +class QSortFilterProxyModel; +class QModelIndex; +class QVBoxLayout; + +class SearchSortModel; +class SearchListDelegate; +class SearchWidget; class SearchTab: public QWidget { Q_OBJECT -private: - QVBoxLayout *box; - QLabel *results_lbl; - QTreeView *resultsBrowser; - QStandardItemModel *SearchListModel; - SearchSortModel *proxyModel; - SearchListDelegate *SearchDelegate; - SearchWidget *parent; - -protected slots: - void downloadSelectedItem(const QModelIndex& index); - public: - SearchTab(SearchWidget *parent); - ~SearchTab(); - bool loadColWidthResultsList(); - QLabel * getCurrentLabel(); + explicit SearchTab(SearchWidget *m_parent); + + QLabel* getCurrentLabel() const; QStandardItemModel* getCurrentSearchListModel() const; QSortFilterProxyModel* getCurrentSearchListProxy() const; - QTreeView * getCurrentTreeView(); - void setRowColor(int row, QString color); + QTreeView* getCurrentTreeView() const; QHeaderView* header() const; - QString status; + QString status() const; + + bool loadColWidthResultsList(); + void setRowColor(int row, QString color); + void setStatus(const QString &value); + +private slots: + void downloadSelectedItem(const QModelIndex &index); + +private: + QVBoxLayout *m_box; + QLabel *m_resultsLbl; + QTreeView *m_resultsBrowser; + QStandardItemModel *m_searchListModel; + SearchSortModel *m_proxyModel; + SearchListDelegate *m_searchDelegate; + SearchWidget *m_parent; + QString m_status; }; #endif // SEARCHTAB_H diff --git a/src/gui/search/searchwidget.cpp b/src/gui/search/searchwidget.cpp index c4b70931d..e7e403933 100644 --- a/src/gui/search/searchwidget.cpp +++ b/src/gui/search/searchwidget.cpp @@ -29,17 +29,16 @@ * Contact : chris@qbittorrent.org */ -#include #include #include #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -47,6 +46,7 @@ #include #include +#include #ifdef Q_OS_WIN #include #endif @@ -61,22 +61,27 @@ #include "addnewtorrentdialog.h" #include "guiiconprovider.h" #include "lineedit.h" +#include "pluginselectdlg.h" +#include "searchsortmodel.h" +#include "searchtab.h" #include "searchwidget.h" #define SEARCHHISTORY_MAXSIZE 50 +#define URL_COLUMN 5 -/*SEARCH ENGINE START*/ -SearchWidget::SearchWidget(MainWindow* parent) - : QWidget(parent) - , search_pattern(new LineEdit(this)) - , mp_mainWindow(parent) +SearchWidget::SearchWidget(MainWindow *mainWindow) + : QWidget(mainWindow) + , m_mainWindow(mainWindow) { setupUi(this); - searchBarLayout->insertWidget(0, search_pattern); - connect(search_pattern, SIGNAL(returnPressed()), search_button, SLOT(click())); + + m_searchPattern = new LineEdit(this); + searchBarLayout->insertWidget(0, m_searchPattern); + connect(m_searchPattern, SIGNAL(returnPressed()), searchButton, SLOT(click())); + // Icons - search_button->setIcon(GuiIconProvider::instance()->getIcon("edit-find")); - download_button->setIcon(GuiIconProvider::instance()->getIcon("download")); + searchButton->setIcon(GuiIconProvider::instance()->getIcon("edit-find")); + downloadButton->setIcon(GuiIconProvider::instance()->getIcon("download")); goToDescBtn->setIcon(GuiIconProvider::instance()->getIcon("application-x-mswinurl")); pluginsButton->setIcon(GuiIconProvider::instance()->getIcon("preferences-system-network")); copyURLBtn->setIcon(GuiIconProvider::instance()->getIcon("edit-copy")); @@ -93,7 +98,7 @@ SearchWidget::SearchWidget(MainWindow* parent) fillCatCombobox(); fillPluginComboBox(); - connect(search_pattern, SIGNAL(textEdited(QString)), this, SLOT(searchTextEdited(QString))); + connect(m_searchPattern, SIGNAL(textEdited(QString)), this, SLOT(searchTextEdited(QString))); connect(selectPlugin, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(selectMultipleBox(const QString &))); } @@ -101,8 +106,7 @@ void SearchWidget::fillCatCombobox() { comboCategory->clear(); comboCategory->addItem(SearchEngine::categoryFullName("all"), QVariant("all")); - QStringList supported_cat = m_searchEngine->supportedCategories(); - foreach (QString cat, supported_cat) { + foreach (QString cat, m_searchEngine->supportedCategories()) { qDebug("Supported category: %s", qPrintable(cat)); comboCategory->addItem(SearchEngine::categoryFullName(cat), QVariant(cat)); } @@ -123,7 +127,7 @@ QString SearchWidget::selectedCategory() const return comboCategory->itemData(comboCategory->currentIndex()).toString(); } -QString SearchWidget::selectedEngine() const +QString SearchWidget::selectedPlugin() const { return selectPlugin->itemData(selectPlugin->currentIndex()).toString(); } @@ -131,8 +135,6 @@ QString SearchWidget::selectedEngine() const SearchWidget::~SearchWidget() { qDebug("Search destruction"); - - delete search_pattern; delete m_searchEngine; } @@ -142,24 +144,25 @@ void SearchWidget::tab_changed(int t) //doesn't have to be available if (t > -1) { //-1 = no more tab - currentSearchTab = all_tab.at(tabWidget->currentIndex()); - if (currentSearchTab->getCurrentSearchListModel()->rowCount()) { - download_button->setEnabled(true); + m_currentSearchTab = m_allTabs.at(tabWidget->currentIndex()); + if (m_currentSearchTab->getCurrentSearchListModel()->rowCount()) { + downloadButton->setEnabled(true); goToDescBtn->setEnabled(true); copyURLBtn->setEnabled(true); } else { - download_button->setEnabled(false); + downloadButton->setEnabled(false); goToDescBtn->setEnabled(false); copyURLBtn->setEnabled(false); } - search_status->setText(currentSearchTab->status); + searchStatus->setText(m_currentSearchTab->status()); } } void SearchWidget::selectMultipleBox(const QString &text) { - if (text == tr("Multiple...")) on_pluginsButton_clicked(); + if (text == tr("Multiple...")) + on_pluginsButton_clicked(); } void SearchWidget::on_pluginsButton_clicked() @@ -172,35 +175,35 @@ void SearchWidget::on_pluginsButton_clicked() void SearchWidget::searchTextEdited(QString) { // Enable search button - search_button->setText(tr("Search")); - newQueryString = true; + searchButton->setText(tr("Search")); + m_isNewQueryString = true; } void SearchWidget::giveFocusToSearchInput() { - search_pattern->setFocus(); + m_searchPattern->setFocus(); } // Function called when we click on search button -void SearchWidget::on_search_button_clicked() +void SearchWidget::on_searchButton_clicked() { if (Utils::Misc::pythonVersion() < 0) { - mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); + m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine.")); return; } if (m_searchEngine->isActive()) { m_searchEngine->cancelSearch(); - if (!newQueryString) { - search_button->setText(tr("Search")); + if (!m_isNewQueryString) { + searchButton->setText(tr("Search")); return; } } - newQueryString = false; + m_isNewQueryString = false; - const QString pattern = search_pattern->text().trimmed(); + const QString pattern = m_searchPattern->text().trimmed(); // No search pattern entered if (pattern.isEmpty()) { QMessageBox::critical(0, tr("Empty search pattern"), tr("Please type a search pattern first")); @@ -208,29 +211,29 @@ void SearchWidget::on_search_button_clicked() } // Tab Addition - currentSearchTab = new SearchTab(this); - activeSearchTab = currentSearchTab; - connect(currentSearchTab->header(), SIGNAL(sectionResized(int, int, int)), this, SLOT(saveResultsColumnsWidth())); - all_tab.append(currentSearchTab); + m_currentSearchTab = new SearchTab(this); + m_activeSearchTab = m_currentSearchTab; + connect(m_currentSearchTab->header(), SIGNAL(sectionResized(int, int, int)), this, SLOT(saveResultsColumnsWidth())); + m_allTabs.append(m_currentSearchTab); QString tabName = pattern; tabName.replace(QRegExp("&{1}"), "&&"); - tabWidget->addTab(currentSearchTab, tabName); - tabWidget->setCurrentWidget(currentSearchTab); + tabWidget->addTab(m_currentSearchTab, tabName); + tabWidget->setCurrentWidget(m_currentSearchTab); 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(); + if (selectedPlugin() == "all") plugins = m_searchEngine->allPlugins(); + else if (selectedPlugin() == "enabled") plugins = m_searchEngine->enabledPlugins(); + else if (selectedPlugin() == "multi") plugins = m_searchEngine->enabledPlugins(); + else plugins << selectedPlugin(); qDebug("Search with category: %s", qPrintable(selectedCategory())); // Update SearchEngine widgets - no_search_results = true; - nb_search_results = 0; + m_noSearchResults = true; + m_nbSearchResults = 0; // Changing the text of the current label - activeSearchTab->getCurrentLabel()->setText(tr("Results (%1):", "i.e: Search results").arg(0)); + m_activeSearchTab->getCurrentLabel()->setText(tr("Results (%1):", "i.e: Search results").arg(0)); // Launch search m_searchEngine->startSearch(pattern, selectedCategory(), plugins); @@ -238,18 +241,17 @@ void SearchWidget::on_search_button_clicked() void SearchWidget::saveResultsColumnsWidth() { - if (all_tab.isEmpty()) - return; - QTreeView* treeview = all_tab.first()->getCurrentTreeView(); - Preferences* const pref = Preferences::instance(); - QStringList new_width_list; - short nbColumns = all_tab.first()->getCurrentSearchListModel()->columnCount(); + if (m_allTabs.isEmpty()) return; + + QTreeView *treeview = m_allTabs.first()->getCurrentTreeView(); + QStringList newWidthList; + short nbColumns = m_allTabs.first()->getCurrentSearchListModel()->columnCount(); for (short i = 0; i < nbColumns; ++i) if (treeview->columnWidth(i) > 0) - new_width_list << QString::number(treeview->columnWidth(i)); + newWidthList << QString::number(treeview->columnWidth(i)); // Don't save the width of the last column (auto column width) - new_width_list.removeLast(); - pref->setSearchColsWidth(new_width_list.join(" ")); + newWidthList.removeLast(); + Preferences::instance()->setSearchColsWidth(newWidthList.join(" ")); } void SearchWidget::downloadTorrent(QString url) @@ -263,10 +265,10 @@ void SearchWidget::downloadTorrent(QString url) void SearchWidget::searchStarted() { // Update SearchEngine widgets - activeSearchTab->status = tr("Searching..."); - search_status->setText(currentSearchTab->status); - search_status->repaint(); - search_button->setText(tr("Stop")); + m_activeSearchTab->setStatus(tr("Searching...")); + searchStatus->setText(m_currentSearchTab->status()); + searchStatus->repaint(); + searchButton->setText(tr("Stop")); } // Slot called when search is Finished @@ -274,72 +276,69 @@ void SearchWidget::searchStarted() // Error | Stopped by user | Finished normally void SearchWidget::searchFinished(bool cancelled) { - bool useNotificationBalloons = Preferences::instance()->useProgramNotification(); - if (useNotificationBalloons && mp_mainWindow->getCurrentTabWidget() != this) - mp_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished")); + if (Preferences::instance()->useProgramNotification() && (m_mainWindow->getCurrentTabWidget() != this)) + m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished")); - if (activeSearchTab.isNull()) return; // The active tab was closed + if (m_activeSearchTab.isNull()) return; // The active tab was closed if (cancelled) - activeSearchTab->status = tr("Search aborted"); - else if (no_search_results) - activeSearchTab->status = tr("Search returned no results"); + m_activeSearchTab->setStatus(tr("Search aborted")); + else if (m_noSearchResults) + m_activeSearchTab->setStatus(tr("Search returned no results")); else - activeSearchTab->status = tr("Search has finished"); + m_activeSearchTab->setStatus(tr("Search has finished")); - search_status->setText(currentSearchTab->status); - activeSearchTab = 0; - search_button->setText(tr("Search")); + searchStatus->setText(m_currentSearchTab->status()); + m_activeSearchTab = 0; + searchButton->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 (Preferences::instance()->useProgramNotification() && (m_mainWindow->getCurrentTabWidget() != this)) + m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has failed")); - if (activeSearchTab.isNull()) return; // The active tab was closed + if (m_activeSearchTab.isNull()) return; // The active tab was closed #ifdef Q_OS_WIN - activeSearchTab->status = tr("Search aborted"); + m_activeSearchTab->setStatus(tr("Search aborted")); #else - activeSearchTab->status = tr("An error occurred during search..."); + m_activeSearchTab->setStatus(tr("An error occurred during search...")); #endif } -// SLOT to append one line to search results list void SearchWidget::appendSearchResults(const QList &results) { - if (activeSearchTab.isNull()) { + if (m_activeSearchTab.isNull()) { m_searchEngine->cancelSearch(); return; } - Q_ASSERT(activeSearchTab); + Q_ASSERT(m_activeSearchTab); - QStandardItemModel* cur_model = activeSearchTab->getCurrentSearchListModel(); - Q_ASSERT(cur_model); + QStandardItemModel *curModel = m_activeSearchTab->getCurrentSearchListModel(); + Q_ASSERT(curModel); 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 + int row = curModel->rowCount(); + curModel->insertRow(row); + + curModel->setData(curModel->index(row, SearchSortModel::DL_LINK), result.fileUrl); // download URL + curModel->setData(curModel->index(row, SearchSortModel::NAME), result.fileName); // Name + curModel->setData(curModel->index(row, SearchSortModel::SIZE), result.fileSize); // Size + curModel->setData(curModel->index(row, SearchSortModel::SEEDS), result.nbSeeders); // Seeders + curModel->setData(curModel->index(row, SearchSortModel::LEECHS), result.nbLeechers); // Leechers + curModel->setData(curModel->index(row, SearchSortModel::ENGINE_URL), result.siteUrl); // Search site URL + curModel->setData(curModel->index(row, SearchSortModel::DESC_LINK), result.descrLink); // Description Link } - no_search_results = false; - nb_search_results += results.size(); - activeSearchTab->getCurrentLabel()->setText(tr("Results (%1):", "i.e: Search results").arg(nb_search_results)); + m_noSearchResults = false; + m_nbSearchResults += results.size(); + m_activeSearchTab->getCurrentLabel()->setText(tr("Results (%1):", "i.e: Search results").arg(m_nbSearchResults)); // Enable clear & download buttons - download_button->setEnabled(true); + downloadButton->setEnabled(true); goToDescBtn->setEnabled(true); copyURLBtn->setEnabled(true); } @@ -347,46 +346,48 @@ void SearchWidget::appendSearchResults(const QList &results) 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)) { + if (!m_activeSearchTab.isNull() && index == tabWidget->indexOf(m_activeSearchTab)) { qDebug("Closed active search Tab"); if (m_searchEngine->isActive()) m_searchEngine->cancelSearch(); - activeSearchTab = 0; + m_activeSearchTab = 0; } - delete all_tab.takeAt(index); - if (!all_tab.size()) { - download_button->setEnabled(false); + + delete m_allTabs.takeAt(index); + + if (!m_allTabs.size()) { + downloadButton->setEnabled(false); goToDescBtn->setEnabled(false); - search_status->setText(tr("Stopped")); + searchStatus->setText(tr("Stopped")); copyURLBtn->setEnabled(false); } } // Download selected items in search results list -void SearchWidget::on_download_button_clicked() +void SearchWidget::on_downloadButton_clicked() { //QModelIndexList selectedIndexes = currentSearchTab->getCurrentTreeView()->selectionModel()->selectedIndexes(); - QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); + QModelIndexList selectedIndexes = m_allTabs.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); foreach (const QModelIndex &index, selectedIndexes) { if (index.column() == SearchSortModel::NAME) { // Get Item url - QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); - QString torrent_url = model->data(model->index(index.row(), URL_COLUMN)).toString(); - downloadTorrent(torrent_url); - all_tab.at(tabWidget->currentIndex())->setRowColor(index.row(), "blue"); + QSortFilterProxyModel *model = m_allTabs.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); + QString torrentUrl = model->data(model->index(index.row(), URL_COLUMN)).toString(); + downloadTorrent(torrentUrl); + m_allTabs.at(tabWidget->currentIndex())->setRowColor(index.row(), "blue"); } } } void SearchWidget::on_goToDescBtn_clicked() { - QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); + QModelIndexList selectedIndexes = m_allTabs.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); foreach (const QModelIndex &index, selectedIndexes) { if (index.column() == SearchSortModel::NAME) { - QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); - const QString desc_url = model->data(model->index(index.row(), SearchSortModel::DESC_LINK)).toString(); - if (!desc_url.isEmpty()) - QDesktopServices::openUrl(QUrl::fromEncoded(desc_url.toUtf8())); + QSortFilterProxyModel *model = m_allTabs.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); + const QString descUrl = model->data(model->index(index.row(), SearchSortModel::DESC_LINK)).toString(); + if (!descUrl.isEmpty()) + QDesktopServices::openUrl(QUrl::fromEncoded(descUrl.toUtf8())); } } } @@ -394,15 +395,17 @@ void SearchWidget::on_goToDescBtn_clicked() void SearchWidget::on_copyURLBtn_clicked() { QStringList urls; - QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); + QModelIndexList selectedIndexes = m_allTabs.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); + foreach (const QModelIndex &index, selectedIndexes) { if (index.column() == SearchSortModel::NAME) { - QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); + QSortFilterProxyModel *model = m_allTabs.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); const QString descUrl = model->data(model->index(index.row(), SearchSortModel::DESC_LINK)).toString(); if (!descUrl.isEmpty()) urls << descUrl.toUtf8(); } } + if (!urls.empty()) { QClipboard *clipboard = QApplication::clipboard(); clipboard->setText(urls.join("\n")); diff --git a/src/gui/search/searchwidget.h b/src/gui/search/searchwidget.h index c77ac1e6e..f52742e9e 100644 --- a/src/gui/search/searchwidget.h +++ b/src/gui/search/searchwidget.h @@ -33,69 +33,63 @@ #define SEARCHWIDGET_H #include -#include #include -#include + #include "ui_searchwidget.h" -#include "pluginselectdlg.h" -#include "searchtab.h" -class SearchWidget; class MainWindow; class LineEdit; class SearchEngine; struct SearchResult; +class SearchTab; -QT_BEGIN_NAMESPACE -class QTimer; -QT_END_NAMESPACE - -class SearchWidget : public QWidget, private Ui::SearchWidget{ +class SearchWidget: public QWidget, private Ui::SearchWidget +{ Q_OBJECT Q_DISABLE_COPY(SearchWidget) public: - SearchWidget(MainWindow *mp_mainWindow); + explicit SearchWidget(MainWindow *mainWindow); ~SearchWidget(); - QString selectedCategory() const; - QString selectedEngine() const; -public slots: void downloadTorrent(QString url); void giveFocusToSearchInput(); 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 tab_changed(int); //to prevent the use of the download button when the tab is empty + void on_searchButton_clicked(); + void on_downloadButton_clicked(); + void on_goToDescBtn_clicked(); + void on_copyURLBtn_clicked(); + void on_pluginsButton_clicked(); + void closeTab(int index); void appendSearchResults(const QList &results); void searchStarted(); void searchFinished(bool cancelled); void searchFailed(); void selectMultipleBox(const QString &text); - void on_pluginsButton_clicked(); + void saveResultsColumnsWidth(); void fillCatCombobox(); void fillPluginComboBox(); void searchTextEdited(QString); - void on_goToDescBtn_clicked(); - void on_copyURLBtn_clicked(); private: - // Search related - LineEdit* search_pattern; + QString selectedCategory() const; + QString selectedPlugin() const; - bool no_search_results; - QByteArray search_result_line_truncated; - unsigned long nb_search_results; + LineEdit *m_searchPattern; SearchEngine *m_searchEngine; - QPointer currentSearchTab; // Selected tab - QPointer activeSearchTab; // Tab with running search - QList > all_tab; // To store all tabs - MainWindow *mp_mainWindow; - bool newQueryString; + QPointer m_currentSearchTab; // Selected tab + QPointer m_activeSearchTab; // Tab with running search + QList > m_allTabs; // To store all tabs + MainWindow *m_mainWindow; + bool m_isNewQueryString; + bool m_noSearchResults; + QByteArray m_searchResultLineTruncated; + unsigned long m_nbSearchResults; }; #endif // SEARCHWIDGET_H diff --git a/src/gui/search/searchwidget.ui b/src/gui/search/searchwidget.ui index d6bfa5877..a9c543c5a 100644 --- a/src/gui/search/searchwidget.ui +++ b/src/gui/search/searchwidget.ui @@ -23,7 +23,7 @@ - + Search @@ -53,7 +53,7 @@ - + 200 @@ -99,7 +99,7 @@ - + false