Fix coding style (Issue #2192).

This commit is contained in:
Vladimir Golovnev (Glassez) 2015-09-24 10:33:02 +03:00 committed by Vladimir Golovnev (qlassez)
parent 54979e6b53
commit ac365c5efb
16 changed files with 797 additions and 604 deletions

View file

@ -30,8 +30,8 @@
#include <QDomDocument>
#include <QDomNode>
#include <QDomElement>
#include <QProcess>
#include <QDir>
#include <QProcess>
#include <QDebug>
#include "base/utils/fs.h"
@ -62,8 +62,8 @@ static inline void removePythonScriptIfExists(const QString &scriptPath)
const QHash<QString, QString> SearchEngine::m_categoryNames = SearchEngine::initializeCategoryNames();
SearchEngine::SearchEngine()
: m_searchStopped(false)
, m_updateUrl(QString("https://raw.github.com/qbittorrent/qBittorrent/master/src/searchengine/%1/engines/").arg(Utils::Misc::pythonVersion() >= 3 ? "nova3" : "nova"))
: 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();

View file

@ -30,11 +30,12 @@
#ifndef SEARCHENGINE_H
#define SEARCHENGINE_H
#include <QObject>
#include <QHash>
#include <QStringList>
#include <QProcess>
#include <QList>
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);

View file

@ -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

View file

@ -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 <QProcess>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
@ -53,7 +41,26 @@
#include <QTableView>
#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)
@ -62,6 +69,7 @@ PluginSelectDlg::PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent)
{
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
@ -75,6 +83,7 @@ PluginSelectDlg::PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent)
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)));
@ -93,16 +102,18 @@ PluginSelectDlg::PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent)
show();
}
PluginSelectDlg::~PluginSelectDlg() {
PluginSelectDlg::~PluginSelectDlg()
{
emit pluginsChanged();
}
void PluginSelectDlg::dropEvent(QDropEvent *event) {
void PluginSelectDlg::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
QStringList files;
if (event->mimeData()->hasUrls()) {
const QList<QUrl> urls = event->mimeData()->urls();
foreach (const QUrl &url, urls) {
foreach (const QUrl &url, event->mimeData()->urls()) {
if (!url.isEmpty()) {
if (url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
@ -112,7 +123,7 @@ void PluginSelectDlg::dropEvent(QDropEvent *event) {
}
}
else {
files = event->mimeData()->text().split(QString::fromUtf8("\n"));
files = event->mimeData()->text().split(QLatin1String("\n"));
}
if (files.isEmpty()) return;
@ -125,38 +136,45 @@ void PluginSelectDlg::dropEvent(QDropEvent *event) {
}
// Decode if we accept drag 'n drop or not
void PluginSelectDlg::dragEnterEvent(QDragEnterEvent *event) {
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"))) {
if (event->mimeData()->hasFormat(QLatin1String("text/plain")) || event->mimeData()->hasFormat(QLatin1String("text/uri-list"))) {
event->acceptProposedAction();
}
}
void PluginSelectDlg::on_updateButton_clicked() {
void PluginSelectDlg::on_updateButton_clicked()
{
startAsyncOp();
m_pluginManager->checkForUpdates();
}
void PluginSelectDlg::togglePluginState(QTreeWidgetItem *item, int) {
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 {
}
else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
}
void PluginSelectDlg::displayContextMenu(const QPoint&) {
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);
@ -165,15 +183,15 @@ void PluginSelectDlg::displayContextMenu(const QPoint&) {
myContextMenu.exec(QCursor::pos());
}
void PluginSelectDlg::on_closeButton_clicked() {
void PluginSelectDlg::on_closeButton_clicked()
{
close();
}
void PluginSelectDlg::on_actionUninstall_triggered() {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
void PluginSelectDlg::on_actionUninstall_triggered()
{
bool error = false;
foreach (item, items) {
foreach (QTreeWidgetItem *item, pluginsTree->selectedItems()) {
int index = pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(PLUGIN_ID);
@ -186,19 +204,18 @@ void PluginSelectDlg::on_actionUninstall_triggered() {
m_pluginManager->enablePlugin(id, false);
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(index, "red");
continue;
}
}
if (error)
QMessageBox::warning(0, tr("Uninstall warning"), tr("Some plugins could not be uninstalled because they are included in qBittorrent. Only the ones you added yourself can be uninstalled.\nThose plugins were disabled."));
else
QMessageBox::information(0, tr("Uninstall success"), tr("All selected plugins were uninstalled successfully"));
}
void PluginSelectDlg::enableSelection(bool enable) {
QList<QTreeWidgetItem *> items = pluginsTree->selectedItems();
QTreeWidgetItem *item;
foreach (item, items) {
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);
@ -206,7 +223,8 @@ void PluginSelectDlg::enableSelection(bool enable) {
if (enable) {
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(index, "green");
} else {
}
else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(index, "red");
}
@ -214,49 +232,58 @@ void PluginSelectDlg::enableSelection(bool enable) {
}
// Set the color of a row in data model
void PluginSelectDlg::setRowColor(int row, QString color) {
void PluginSelectDlg::setRowColor(int row, QString color)
{
QTreeWidgetItem *item = pluginsTree->topLevelItem(row);
for (int i=0; i<pluginsTree->columnCount(); ++i) {
for (int i = 0; i < pluginsTree->columnCount(); ++i) {
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
}
}
QList<QTreeWidgetItem*> PluginSelectDlg::findItemsWithUrl(QString url) {
QList<QTreeWidgetItem*> PluginSelectDlg::findItemsWithUrl(QString url)
{
QList<QTreeWidgetItem*> res;
for (int i=0; i<pluginsTree->topLevelItemCount(); ++i) {
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* 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() {
void PluginSelectDlg::loadSupportedSearchPlugins()
{
// Some clean up first
pluginsTree->clear();
foreach (QString name, m_pluginManager->allPlugins())
addNewPlugin(name);
}
void PluginSelectDlg::addNewPlugin(QString engine_name) {
void PluginSelectDlg::addNewPlugin(QString pluginName)
{
QTreeWidgetItem *item = new QTreeWidgetItem(pluginsTree);
PluginInfo *plugin = m_pluginManager->pluginInfo(engine_name);
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 {
}
else {
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(pluginsTree->indexOfTopLevelItem(item), "red");
}
@ -264,7 +291,8 @@ void PluginSelectDlg::addNewPlugin(QString engine_name) {
if (QFile::exists(plugin->iconPath)) {
// Good, we already have the icon
item->setData(PLUGIN_NAME, Qt::DecorationRole, QVariant(QIcon(plugin->iconPath)));
} else {
}
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)));
@ -287,58 +315,59 @@ void PluginSelectDlg::finishAsyncOp()
setCursor(QCursor(Qt::ArrowCursor));
}
void PluginSelectDlg::on_installButton_clicked() {
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);
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);
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")) {
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;
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")+QString::fromUtf8(" (*.py)"));
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) {
void PluginSelectDlg::iconDownloaded(const QString &url, QString filePath)
{
filePath = Utils::Fs::fromNativePath(filePath);
// Icon downloaded
QImage fileIcon;
if (fileIcon.load(filePath)) {
QList<QTreeWidgetItem*> items = findItemsWithUrl(url);
QTreeWidgetItem *item;
foreach (item, items) {
foreach (QTreeWidgetItem *item, findItemsWithUrl(url)) {
QString id = item->text(PLUGIN_ID);
PluginInfo *plugin = m_pluginManager->pluginInfo(id);
if (!plugin) continue;
@ -354,7 +383,8 @@ void PluginSelectDlg::iconDownloaded(const QString &url, QString filePath) {
Utils::Fs::forceRemove(filePath);
}
void PluginSelectDlg::iconDownloadFailed(const QString &url, const QString &reason) {
void PluginSelectDlg::iconDownloadFailed(const QString &url, const QString &reason)
{
qDebug("Could not download favicon: %s, reason: %s", qPrintable(url), qPrintable(reason));
}

View file

@ -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;
public:
PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0);
public:
explicit PluginSelectDlg(SearchEngine *pluginManager, QWidget *parent = 0);
~PluginSelectDlg();
QList<QTreeWidgetItem*> 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

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

@ -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 <QDialog>
#include "ui_pluginsourcedlg.h"
class PluginSourceDlg: public QDialog, private Ui::PluginSourceDlg {
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

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

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

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

@ -1,35 +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 {
Q_OBJECT
class SearchSortModel: public QSortFilterProxyModel
{
public:
enum SearchColumn { NAME, SIZE, SEEDS, LEECHS, ENGINE_URL, DL_LINK, DESC_LINK, NB_SEARCH_COLUMNS };
enum SearchColumn
{
NAME,
SIZE,
SEEDS,
LEECHS,
ENGINE_URL,
DL_LINK,
DESC_LINK,
NB_SEARCH_COLUMNS
};
SearchSortModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
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

View file

@ -33,132 +33,140 @@
#include <QStandardItemModel>
#include <QHeaderView>
#include <QSortFilterProxyModel>
#include <QLabel>
#include <QVBoxLayout>
#ifdef QBT_USES_QT5
#include <QTableView>
#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);
m_parent->downloadTorrent(torrentUrl);
}
SearchTab::~SearchTab() {
delete box;
delete results_lbl;
delete resultsBrowser;
delete SearchListModel;
delete proxyModel;
delete SearchDelegate;
QHeaderView* SearchTab::header() const
{
return m_resultsBrowser->header();
}
QHeaderView* SearchTab::header() const {
return resultsBrowser->header();
}
bool SearchTab::loadColWidthResultsList() {
bool SearchTab::loadColWidthResultsList()
{
QString line = Preferences::instance()->getSearchColsWidth();
if (line.isEmpty())
if (line.isEmpty()) return false;
QStringList widthList = line.split(' ');
if (widthList.size() > m_searchListModel->columnCount())
return false;
QStringList width_list = line.split(' ');
if (width_list.size() > SearchListModel->columnCount())
return false;
unsigned int listSize = width_list.size();
for (unsigned int i=0; i<listSize; ++i) {
resultsBrowser->header()->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; i<proxyModel->columnCount(); ++i) {
proxyModel->setData(proxyModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole);
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);
}
proxyModel->setDynamicSortFilter(true);
m_proxyModel->setDynamicSortFilter(true);
}
QString SearchTab::status() const
{
return m_status;
}
void SearchTab::setStatus(const QString &value)
{
m_status = value;
}

View file

@ -31,48 +31,50 @@
#ifndef SEARCHTAB_H
#define SEARCHTAB_H
#include <QLabel>
#include <QVBoxLayout>
#include "searchsortmodel.h"
#define ENGINE_URL_COLUMN 4
#define URL_COLUMN 5
class SearchListDelegate;
class SearchWidget;
#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
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

View file

@ -29,17 +29,16 @@
* Contact : chris@qbittorrent.org
*/
#include <QStandardItemModel>
#include <QHeaderView>
#include <QMessageBox>
#include <QTemporaryFile>
#include <QSystemTrayIcon>
#include <iostream>
#include <QTimer>
#include <QDir>
#include <QMenu>
#include <QClipboard>
#include <QMimeData>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QFileDialog>
#include <QDesktopServices>
@ -47,6 +46,7 @@
#include <QProcess>
#include <QDebug>
#include <iostream>
#ifdef Q_OS_WIN
#include <stdlib.h>
#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 <i>(%1)</i>:", "i.e: Search results").arg(0));
m_activeSearchTab->getCurrentLabel()->setText(tr("Results <i>(%1)</i>:", "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<SearchResult> &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);
int row = curModel->rowCount();
curModel->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
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 <i>(%1)</i>:", "i.e: Search results").arg(nb_search_results));
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
download_button->setEnabled(true);
downloadButton->setEnabled(true);
goToDescBtn->setEnabled(true);
copyURLBtn->setEnabled(true);
}
@ -347,46 +346,48 @@ void SearchWidget::appendSearchResults(const QList<SearchResult> &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"));

View file

@ -33,69 +33,63 @@
#define SEARCHWIDGET_H
#include <QList>
#include <QPair>
#include <QPointer>
#include <QStringListModel>
#include "ui_searchwidget.h"
#include "pluginselectdlg.h"
#include "searchtab.h"
class SearchWidget;
#include "ui_searchwidget.h"
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<SearchResult> &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<SearchTab> currentSearchTab; // Selected tab
QPointer<SearchTab> activeSearchTab; // Tab with running search
QList<QPointer<SearchTab> > all_tab; // To store all tabs
MainWindow *mp_mainWindow;
bool newQueryString;
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

@ -23,7 +23,7 @@
<widget class="QComboBox" name="selectPlugin"/>
</item>
<item>
<widget class="QPushButton" name="search_button">
<widget class="QPushButton" name="searchButton">
<property name="text">
<string>Search</string>
</property>
@ -53,7 +53,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="search_status">
<widget class="QLabel" name="searchStatus">
<property name="minimumSize">
<size>
<width>200</width>
@ -99,7 +99,7 @@
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QPushButton" name="download_button">
<widget class="QPushButton" name="downloadButton">
<property name="enabled">
<bool>false</bool>
</property>