Merge pull request #3832 from glassez/search

Search Engine code redesign (Issue #2433).
This commit is contained in:
sledgehammer999
2015-12-21 11:13:38 -06:00
37 changed files with 2354 additions and 2023 deletions

View File

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

View File

@@ -41,7 +41,13 @@ 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/pluginselectdlg.h \
$$PWD/search/pluginsourcedlg.h \
$$PWD/search/searchlistdelegate.h \
$$PWD/search/searchsortmodel.h
SOURCES += \
$$PWD/mainwindow.cpp \
@@ -72,7 +78,13 @@ 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/pluginselectdlg.cpp \
$$PWD/search/pluginsourcedlg.cpp \
$$PWD/search/searchlistdelegate.cpp \
$$PWD/search/searchsortmodel.cpp
win32|macx {
HEADERS += $$PWD/programupdater.h
@@ -94,6 +106,9 @@ FORMS += \
$$PWD/autoexpandabledialog.ui \
$$PWD/statsdialog.ui \
$$PWD/options.ui \
$$PWD/torrentcreatordlg.ui
$$PWD/torrentcreatordlg.ui \
$$PWD/search/searchwidget.ui \
$$PWD/search/pluginselectdlg.ui \
$$PWD/search/pluginsourcedlg.ui
RESOURCES += $$PWD/about.qrc

View File

@@ -34,6 +34,7 @@
#include "notifications.h"
#endif
#include <QDebug>
#include <QFileDialog>
#include <QFileSystemWatcher>
#include <QMessageBox>
@@ -50,10 +51,11 @@
#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"
#include "searchengine.h"
#include "search/searchwidget.h"
#include "rss_imp.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/sessionstatus.h"
@@ -531,7 +533,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"));
}
}

View File

@@ -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> searchEngine;
QPointer<SearchWidget> searchEngine;
// RSS
QPointer<RSSImp> rssWidget;
// Execution Log

View File

@@ -0,0 +1,438 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QFileDialog>
#include <QDropEvent>
#include <QTemporaryFile>
#include <QMimeData>
#include <QClipboard>
#ifdef QBT_USES_QT5
#include <QTableView>
#endif
#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);
#ifdef QBT_USES_QT5
// This hack fixes reordering of first column with Qt5.
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
QTableView unused;
unused.setVerticalHeader(pluginsTree->header());
pluginsTree->header()->setParent(pluginsTree);
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
#endif
pluginsTree->setRootIsDecorated(false);
pluginsTree->header()->resizeSection(0, 160);
pluginsTree->header()->resizeSection(1, 80);
pluginsTree->header()->resizeSection(2, 200);
pluginsTree->hideColumn(PLUGIN_ID);
actionUninstall->setIcon(GuiIconProvider::instance()->getIcon("list-remove"));
connect(actionEnable, SIGNAL(toggled(bool)), this, SLOT(enableSelection(bool)));
connect(pluginsTree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayContextMenu(const QPoint&)));
connect(pluginsTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(togglePluginState(QTreeWidgetItem*, int)));
loadSupportedSearchPlugins();
connect(m_pluginManager, SIGNAL(pluginInstalled(QString)), SLOT(pluginInstalled(QString)));
connect(m_pluginManager, SIGNAL(pluginInstallationFailed(QString, QString)), SLOT(pluginInstallationFailed(QString, QString)));
connect(m_pluginManager, SIGNAL(pluginUpdated(QString)), SLOT(pluginUpdated(QString)));
connect(m_pluginManager, SIGNAL(pluginUpdateFailed(QString, QString)), SLOT(pluginUpdateFailed(QString, QString)));
connect(m_pluginManager, SIGNAL(checkForUpdatesFinished(QHash<QString, qreal>)), SLOT(checkForUpdatesFinished(QHash<QString, qreal>)));
connect(m_pluginManager, SIGNAL(checkForUpdatesFailed(QString)), SLOT(checkForUpdatesFailed(QString)));
show();
}
PluginSelectDlg::~PluginSelectDlg()
{
emit pluginsChanged();
}
void PluginSelectDlg::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
QStringList files;
if (event->mimeData()->hasUrls()) {
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"));
}
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(QLatin1String("text/plain")) || event->mimeData()->hasFormat(QLatin1String("text/uri-list"))) {
event->acceptProposedAction();
}
}
void PluginSelectDlg::on_updateButton_clicked()
{
startAsyncOp();
m_pluginManager->checkForUpdates();
}
void PluginSelectDlg::togglePluginState(QTreeWidgetItem *item, int)
{
PluginInfo *plugin = m_pluginManager->pluginInfo(item->text(PLUGIN_ID));
m_pluginManager->enablePlugin(plugin->name, !plugin->enabled);
if (plugin->enabled) {
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "green");
}
else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
}
void PluginSelectDlg::displayContextMenu(const QPoint&)
{
QMenu myContextMenu(this);
// Enable/disable pause/start action given the DL state
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
if (items.isEmpty()) return;
QString first_id = items.first()->text(PLUGIN_ID);
actionEnable->setChecked(m_pluginManager->pluginInfo(first_id)->enabled);
myContextMenu.addAction(actionEnable);
myContextMenu.addSeparator();
myContextMenu.addAction(actionUninstall);
myContextMenu.exec(QCursor::pos());
}
void PluginSelectDlg::on_closeButton_clicked()
{
close();
}
void PluginSelectDlg::on_actionUninstall_triggered()
{
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"));
}
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; i < pluginsTree->columnCount(); ++i) {
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
}
}
QList<QTreeWidgetItem*> PluginSelectDlg::findItemsWithUrl(QString url)
{
QList<QTreeWidgetItem*> res;
for (int i = 0; i < pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (url.startsWith(item->text(PLUGIN_URL), Qt::CaseInsensitive))
res << item;
}
return res;
}
QTreeWidgetItem* PluginSelectDlg::findItemWithID(QString id)
{
for (int i = 0; i < pluginsTree->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = pluginsTree->topLevelItem(i);
if (id == item->text(PLUGIN_ID))
return item;
}
return 0;
}
void PluginSelectDlg::loadSupportedSearchPlugins()
{
// Some clean up first
pluginsTree->clear();
foreach (QString name, m_pluginManager->allPlugins())
addNewPlugin(name);
}
void PluginSelectDlg::addNewPlugin(QString 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()
{
++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 (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
);
}
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") + 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)) {
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);
}
void PluginSelectDlg::iconDownloadFailed(const QString &url, const QString &reason)
{
qDebug("Could not download favicon: %s, reason: %s", qPrintable(url), qPrintable(reason));
}
void PluginSelectDlg::checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo)
{
finishAsyncOp();
if (updateInfo.isEmpty()) {
QMessageBox::information(this, tr("Search plugin update"), tr("All your plugins are already up to date."));
return;
}
foreach (const QString &pluginName, updateInfo.keys()) {
startAsyncOp();
m_pluginManager->updatePlugin(pluginName);
}
}
void PluginSelectDlg::checkForUpdatesFailed(const QString &reason)
{
finishAsyncOp();
QMessageBox::warning(this, tr("Search plugin update"), tr("Sorry, couldn't check for plugin updates. %1").arg(reason));
}
void PluginSelectDlg::pluginInstalled(const QString &name)
{
addNewPlugin(name);
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully installed.", "%1 is the name of the search engine").arg(name));
}
void PluginSelectDlg::pluginInstallationFailed(const QString &name, const QString &reason)
{
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin install"), tr("Couldn't install \"%1\" search engine plugin. %2").arg(name).arg(reason));
}
void PluginSelectDlg::pluginUpdated(const QString &name)
{
finishAsyncOp();
qreal version = m_pluginManager->pluginInfo(name)->version;
QTreeWidgetItem *item = findItemWithID(name);
item->setText(PLUGIN_VERSION, QString::number(version, 'f', 2));
QMessageBox::information(this, tr("Search plugin install"), tr("\"%1\" search engine plugin was successfully updated.", "%1 is the name of the search engine").arg(name));
}
void PluginSelectDlg::pluginUpdateFailed(const QString &name, const QString &reason)
{
finishAsyncOp();
QMessageBox::information(this, tr("Search plugin update"), tr("Couldn't update \"%1\" search engine plugin. %2").arg(name).arg(reason));
}

View File

@@ -0,0 +1,89 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#ifndef PLUGINSELECTDLG_H
#define PLUGINSELECTDLG_H
#include "ui_pluginselectdlg.h"
class QDropEvent;
class SearchEngine;
class PluginSelectDlg: public QDialog, private Ui::PluginSelectDlg
{
Q_OBJECT
public:
explicit PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0);
~PluginSelectDlg();
QList<QTreeWidgetItem*> findItemsWithUrl(QString url);
QTreeWidgetItem* findItemWithID(QString id);
signals:
void pluginsChanged();
protected:
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
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 askForLocalPlugin();
void askForPluginUrl();
void iconDownloaded(const QString &url, QString filePath);
void iconDownloadFailed(const QString &url, const QString &reason);
void checkForUpdatesFinished(const QHash<QString, qreal> &updateInfo);
void checkForUpdatesFailed(const QString &reason);
void pluginInstalled(const QString &name);
void pluginInstallationFailed(const QString &name, const QString &reason);
void pluginUpdated(const QString &name);
void pluginUpdateFailed(const QString &name, const QString &reason);
private:
void loadSupportedSearchPlugins();
void addNewPlugin(QString pluginName);
void startAsyncOp();
void finishAsyncOp();
SearchEngine *m_pluginManager;
int m_asyncOps;
};
#endif // PLUGINSELECTDLG_H

View File

@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PluginSelectDlg</class>
<widget class="QDialog" name="PluginSelectDlg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>300</height>
</rect>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="windowTitle">
<string>Search plugins</string>
</property>
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="lbl_plugins">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="text">
<string>Installed search plugins:</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="pluginsTree">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
<property name="itemsExpandable">
<bool>false</bool>
</property>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Version</string>
</property>
</column>
<column>
<property name="text">
<string>Url</string>
</property>
</column>
<column>
<property name="text">
<string>Enabled</string>
</property>
</column>
<column>
<property name="text">
<string/>
</property>
</column>
</widget>
</item>
<item>
<widget class="QLabel" name="getNewPlugin_lbl">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>You can get new search engine plugins here: &lt;a href=&quot;http://plugins.qbittorrent.org&quot;&gt;http://plugins.qbittorrent.org&lt;/a&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QPushButton" name="installButton">
<property name="text">
<string>Install a new one</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="updateButton">
<property name="text">
<string>Check for updates</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="closeButton">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
<action name="actionEnable">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Enabled</string>
</property>
</action>
<action name="actionUninstall">
<property name="text">
<string>Uninstall</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -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();
}

View File

@@ -0,0 +1,53 @@
/*
* 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
*/
#ifndef PLUGINSOURCEDLG_H
#define PLUGINSOURCEDLG_H
#include <QDialog>
#include "ui_pluginsourcedlg.h"
class PluginSourceDlg: public QDialog, private Ui::PluginSourceDlg
{
Q_OBJECT
public:
explicit PluginSourceDlg(QWidget *parent = 0);
signals:
void askForUrl();
void askForLocalFile();
private slots:
void on_localButton_clicked();
void on_urlButton_clicked();
};
#endif // PLUGINSOURCEDLG_H

View File

@@ -0,0 +1,52 @@
<ui version="4.0" >
<class>PluginSourceDlg</class>
<widget class="QDialog" name="PluginSourceDlg" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>207</width>
<height>76</height>
</rect>
</property>
<property name="windowTitle" >
<string>Plugin source</string>
</property>
<layout class="QVBoxLayout" >
<item>
<widget class="QLabel" name="source_lbl" >
<property name="font" >
<font>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="text" >
<string>Search plugin source:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" >
<item>
<widget class="QPushButton" name="localButton" >
<property name="text" >
<string>Local file</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="urlButton" >
<property name="text" >
<string>Web link</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -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 <QStyleOptionViewItemV2>
#include <QModelIndex>
#include <QPainter>
#include <QProgressBar>
#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;
}

View File

@@ -0,0 +1,45 @@
/*
* 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
*/
#ifndef SEARCHLISTDELEGATE_H
#define SEARCHLISTDELEGATE_H
#include <QItemDelegate>
class SearchListDelegate: public QItemDelegate
{
public:
explicit SearchListDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QWidget* createEditor(QWidget*, const QStyleOptionViewItem &, const QModelIndex &) const;
};
#endif

View File

@@ -0,0 +1,54 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2013 sledgehammer999 <hammered999@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "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);
}

View File

@@ -0,0 +1,56 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2013 sledgehammer999 <hammered999@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef SEARCHSORTMODEL_H
#define SEARCHSORTMODEL_H
#include <QSortFilterProxyModel>
#include "base/utils/string.h"
class SearchSortModel: public QSortFilterProxyModel
{
public:
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;
};
#endif // SEARCHSORTMODEL_H

View File

@@ -0,0 +1,172 @@
/*
* 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 <QDir>
#include <QTreeView>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QSortFilterProxyModel>
#include <QLabel>
#include <QVBoxLayout>
#ifdef QBT_USES_QT5
#include <QTableView>
#endif
#include "base/utils/misc.h"
#include "base/preferences.h"
#include "searchsortmodel.h"
#include "searchlistdelegate.h"
#include "searchwidget.h"
#include "searchtab.h"
SearchTab::SearchTab(SearchWidget *parent)
: QWidget(parent)
, m_parent(parent)
{
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(m_resultsBrowser->header());
m_resultsBrowser->header()->setParent(m_resultsBrowser);
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
#endif
m_resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_box->addWidget(m_resultsLbl);
m_box->addWidget(m_resultsBrowser);
setLayout(m_box);
// Set Search results list model
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"));
m_proxyModel = new SearchSortModel(this);
m_proxyModel->setDynamicSortFilter(true);
m_proxyModel->setSourceModel(m_searchListModel);
m_resultsBrowser->setModel(m_proxyModel);
m_searchDelegate = new SearchListDelegate(this);
m_resultsBrowser->setItemDelegate(m_searchDelegate);
m_resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column
m_resultsBrowser->hideColumn(SearchSortModel::DESC_LINK);
m_resultsBrowser->setRootIsDecorated(false);
m_resultsBrowser->setAllColumnsShowFocus(true);
m_resultsBrowser->setSortingEnabled(true);
// Connect signals to slots (search part)
connect(m_resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadSelectedItem(const QModelIndex&)));
// Load last columns width for search results list
if (!loadColWidthResultsList())
m_resultsBrowser->header()->resizeSection(0, 275);
// Sort by Seeds
m_resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder);
}
void SearchTab::downloadSelectedItem(const QModelIndex &index)
{
QString torrentUrl = m_proxyModel->data(m_proxyModel->index(index.row(), SearchSortModel::DL_LINK)).toString();
setRowColor(index.row(), "blue");
m_parent->downloadTorrent(torrentUrl);
}
QHeaderView* SearchTab::header() const
{
return m_resultsBrowser->header();
}
bool SearchTab::loadColWidthResultsList()
{
QString line = Preferences::instance()->getSearchColsWidth();
if (line.isEmpty()) return false;
QStringList widthList = line.split(' ');
if (widthList.size() > m_searchListModel->columnCount())
return false;
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() const
{
return m_resultsLbl;
}
QTreeView* SearchTab::getCurrentTreeView() const
{
return m_resultsBrowser;
}
QSortFilterProxyModel* SearchTab::getCurrentSearchListProxy() const
{
return m_proxyModel;
}
QStandardItemModel* SearchTab::getCurrentSearchListModel() const
{
return m_searchListModel;
}
// Set the color of a row in data model
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;
}

View File

@@ -0,0 +1,81 @@
/*
* 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 SEARCHTAB_H
#define SEARCHTAB_H
#include <QWidget>
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
public:
explicit SearchTab(SearchWidget *m_parent);
QLabel* getCurrentLabel() const;
QStandardItemModel* getCurrentSearchListModel() const;
QSortFilterProxyModel* getCurrentSearchListProxy() const;
QTreeView* getCurrentTreeView() const;
QHeaderView* header() const;
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

View File

@@ -0,0 +1,413 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#include <QHeaderView>
#include <QMessageBox>
#include <QTemporaryFile>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QDir>
#include <QMenu>
#include <QClipboard>
#include <QMimeData>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QFileDialog>
#include <QDesktopServices>
#include <QClipboard>
#include <QProcess>
#include <QDebug>
#include <iostream>
#ifdef Q_OS_WIN
#include <stdlib.h>
#endif
#include "base/bittorrent/session.h"
#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"
#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
SearchWidget::SearchWidget(MainWindow *mainWindow)
: QWidget(mainWindow)
, m_mainWindow(mainWindow)
{
setupUi(this);
m_searchPattern = new LineEdit(this);
searchBarLayout->insertWidget(0, m_searchPattern);
connect(m_searchPattern, SIGNAL(returnPressed()), searchButton, SLOT(click()));
// Icons
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"));
tabWidget->setTabsClosable(true);
connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
m_searchEngine = new SearchEngine;
connect(m_searchEngine, SIGNAL(searchStarted()), SLOT(searchStarted()));
connect(m_searchEngine, SIGNAL(newSearchResults(QList<SearchResult>)), SLOT(appendSearchResults(QList<SearchResult>)));
connect(m_searchEngine, SIGNAL(searchFinished(bool)), SLOT(searchFinished(bool)));
connect(m_searchEngine, SIGNAL(searchFailed()), SLOT(searchFailed()));
// Fill in category combobox
fillCatCombobox();
fillPluginComboBox();
connect(m_searchPattern, SIGNAL(textEdited(QString)), this, SLOT(searchTextEdited(QString)));
connect(selectPlugin, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(selectMultipleBox(const QString &)));
}
void SearchWidget::fillCatCombobox()
{
comboCategory->clear();
comboCategory->addItem(SearchEngine::categoryFullName("all"), QVariant("all"));
foreach (QString cat, m_searchEngine->supportedCategories()) {
qDebug("Supported category: %s", qPrintable(cat));
comboCategory->addItem(SearchEngine::categoryFullName(cat), QVariant(cat));
}
}
void SearchWidget::fillPluginComboBox()
{
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
{
return comboCategory->itemData(comboCategory->currentIndex()).toString();
}
QString SearchWidget::selectedPlugin() const
{
return selectPlugin->itemData(selectPlugin->currentIndex()).toString();
}
SearchWidget::~SearchWidget()
{
qDebug("Search destruction");
delete m_searchEngine;
}
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
if (t > -1) {
//-1 = no more tab
m_currentSearchTab = m_allTabs.at(tabWidget->currentIndex());
if (m_currentSearchTab->getCurrentSearchListModel()->rowCount()) {
downloadButton->setEnabled(true);
goToDescBtn->setEnabled(true);
copyURLBtn->setEnabled(true);
}
else {
downloadButton->setEnabled(false);
goToDescBtn->setEnabled(false);
copyURLBtn->setEnabled(false);
}
searchStatus->setText(m_currentSearchTab->status());
}
}
void SearchWidget::selectMultipleBox(const QString &text)
{
if (text == tr("Multiple..."))
on_pluginsButton_clicked();
}
void SearchWidget::on_pluginsButton_clicked()
{
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)
{
// Enable search button
searchButton->setText(tr("Search"));
m_isNewQueryString = true;
}
void SearchWidget::giveFocusToSearchInput()
{
m_searchPattern->setFocus();
}
// Function called when we click on search button
void SearchWidget::on_searchButton_clicked()
{
if (Utils::Misc::pythonVersion() < 0) {
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine."));
return;
}
if (m_searchEngine->isActive()) {
m_searchEngine->cancelSearch();
if (!m_isNewQueryString) {
searchButton->setText(tr("Search"));
return;
}
}
m_isNewQueryString = false;
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"));
return;
}
// Tab Addition
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(m_currentSearchTab, tabName);
tabWidget->setCurrentWidget(m_currentSearchTab);
QStringList plugins;
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
m_noSearchResults = true;
m_nbSearchResults = 0;
// Changing the text of the current label
m_activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "i.e: Search results").arg(0));
// Launch search
m_searchEngine->startSearch(pattern, selectedCategory(), plugins);
}
void SearchWidget::saveResultsColumnsWidth()
{
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)
newWidthList << QString::number(treeview->columnWidth(i));
// Don't save the width of the last column (auto column width)
newWidthList.removeLast();
Preferences::instance()->setSearchColsWidth(newWidthList.join(" "));
}
void SearchWidget::downloadTorrent(QString url)
{
if (Preferences::instance()->useAdditionDialog())
AddNewTorrentDialog::show(url, this);
else
BitTorrent::Session::instance()->addTorrent(url);
}
void SearchWidget::searchStarted()
{
// Update SearchEngine widgets
m_activeSearchTab->setStatus(tr("Searching..."));
searchStatus->setText(m_currentSearchTab->status());
searchStatus->repaint();
searchButton->setText(tr("Stop"));
}
// Slot called when search is Finished
// Search can be finished for 3 reasons :
// Error | Stopped by user | Finished normally
void SearchWidget::searchFinished(bool cancelled)
{
if (Preferences::instance()->useProgramNotification() && (m_mainWindow->getCurrentTabWidget() != this))
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has finished"));
if (m_activeSearchTab.isNull()) return; // The active tab was closed
if (cancelled)
m_activeSearchTab->setStatus(tr("Search aborted"));
else if (m_noSearchResults)
m_activeSearchTab->setStatus(tr("Search returned no results"));
else
m_activeSearchTab->setStatus(tr("Search has finished"));
searchStatus->setText(m_currentSearchTab->status());
m_activeSearchTab = 0;
searchButton->setText(tr("Search"));
}
void SearchWidget::searchFailed()
{
if (Preferences::instance()->useProgramNotification() && (m_mainWindow->getCurrentTabWidget() != this))
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has failed"));
if (m_activeSearchTab.isNull()) return; // The active tab was closed
#ifdef Q_OS_WIN
m_activeSearchTab->setStatus(tr("Search aborted"));
#else
m_activeSearchTab->setStatus(tr("An error occurred during search..."));
#endif
}
void SearchWidget::appendSearchResults(const QList<SearchResult> &results)
{
if (m_activeSearchTab.isNull()) {
m_searchEngine->cancelSearch();
return;
}
Q_ASSERT(m_activeSearchTab);
QStandardItemModel *curModel = m_activeSearchTab->getCurrentSearchListModel();
Q_ASSERT(curModel);
foreach (const SearchResult &result, results) {
// Add item to search result list
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
}
m_noSearchResults = false;
m_nbSearchResults += results.size();
m_activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "i.e: Search results").arg(m_nbSearchResults));
// Enable clear & download buttons
downloadButton->setEnabled(true);
goToDescBtn->setEnabled(true);
copyURLBtn->setEnabled(true);
}
void SearchWidget::closeTab(int index)
{
// Search is run for active tab so if user decided to close it, then stop search
if (!m_activeSearchTab.isNull() && index == tabWidget->indexOf(m_activeSearchTab)) {
qDebug("Closed active search Tab");
if (m_searchEngine->isActive())
m_searchEngine->cancelSearch();
m_activeSearchTab = 0;
}
delete m_allTabs.takeAt(index);
if (!m_allTabs.size()) {
downloadButton->setEnabled(false);
goToDescBtn->setEnabled(false);
searchStatus->setText(tr("Stopped"));
copyURLBtn->setEnabled(false);
}
}
// Download selected items in search results list
void SearchWidget::on_downloadButton_clicked()
{
//QModelIndexList selectedIndexes = currentSearchTab->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 = 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 = m_allTabs.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, selectedIndexes) {
if (index.column() == SearchSortModel::NAME) {
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()));
}
}
}
void SearchWidget::on_copyURLBtn_clicked()
{
QStringList urls;
QModelIndexList selectedIndexes = m_allTabs.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes();
foreach (const QModelIndex &index, selectedIndexes) {
if (index.column() == SearchSortModel::NAME) {
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"));
}
}

View File

@@ -0,0 +1,95 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#ifndef SEARCHWIDGET_H
#define SEARCHWIDGET_H
#include <QList>
#include <QPointer>
#include "ui_searchwidget.h"
class MainWindow;
class LineEdit;
class SearchEngine;
struct SearchResult;
class SearchTab;
class SearchWidget: public QWidget, private Ui::SearchWidget
{
Q_OBJECT
Q_DISABLE_COPY(SearchWidget)
public:
explicit SearchWidget(MainWindow *mainWindow);
~SearchWidget();
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_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<SearchResult> &results);
void searchStarted();
void searchFinished(bool cancelled);
void searchFailed();
void selectMultipleBox(const QString &text);
void saveResultsColumnsWidth();
void fillCatCombobox();
void fillPluginComboBox();
void searchTextEdited(QString);
private:
QString selectedCategory() const;
QString selectedPlugin() const;
LineEdit *m_searchPattern;
SearchEngine *m_searchEngine;
QPointer<SearchTab> m_currentSearchTab; // Selected tab
QPointer<SearchTab> m_activeSearchTab; // Tab with running search
QList<QPointer<SearchTab> > 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

View File

@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SearchWidget</class>
<widget class="QWidget" name="SearchWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>820</width>
<height>453</height>
</rect>
</property>
<property name="windowTitle">
<string>Search</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="searchBarLayout">
<item>
<widget class="QComboBox" name="comboCategory"/>
</item>
<item>
<widget class="QComboBox" name="selectPlugin"/>
</item>
<item>
<widget class="QPushButton" name="searchButton">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="status_lbl">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Status:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="searchStatus">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>35</height>
</size>
</property>
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Stopped</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>188</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QTabWidget" name="tabWidget"/>
</item>
<item>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QPushButton" name="downloadButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Download</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="goToDescBtn">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Go to description page</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="copyURLBtn">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Copy description page URL</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>601</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pluginsButton">
<property name="text">
<string>Search plugins...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>