2008-06-25 23:29:29 +04:00
|
|
|
/*
|
2018-01-29 17:05:29 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
2008-06-25 23:29:29 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* 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.
|
2008-06-25 23:29:29 +04:00
|
|
|
*/
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
#include "searchjobwidget.h"
|
2018-01-29 17:05:29 +03:00
|
|
|
|
2016-05-26 18:07:19 +03:00
|
|
|
#include <QApplication>
|
2018-01-29 17:05:29 +03:00
|
|
|
#include <QClipboard>
|
|
|
|
#include <QDesktopServices>
|
2008-06-25 23:29:29 +04:00
|
|
|
#include <QHeaderView>
|
2019-03-22 21:01:35 +03:00
|
|
|
#include <QKeyEvent>
|
2018-01-29 17:05:29 +03:00
|
|
|
#include <QMenu>
|
2016-05-26 18:07:19 +03:00
|
|
|
#include <QPalette>
|
2018-01-29 17:05:29 +03:00
|
|
|
#include <QStandardItemModel>
|
2015-11-18 19:13:25 +03:00
|
|
|
#include <QTableView>
|
2019-03-02 08:22:13 +03:00
|
|
|
#include <QUrl>
|
2008-06-25 23:29:29 +04:00
|
|
|
|
2018-01-29 17:05:29 +03:00
|
|
|
#include "base/bittorrent/session.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/preferences.h"
|
2018-01-29 17:05:29 +03:00
|
|
|
#include "base/search/searchdownloadhandler.h"
|
|
|
|
#include "base/search/searchhandler.h"
|
|
|
|
#include "base/search/searchpluginmanager.h"
|
2017-07-05 13:19:58 +03:00
|
|
|
#include "base/settingvalue.h"
|
2018-01-29 17:05:29 +03:00
|
|
|
#include "base/utils/misc.h"
|
|
|
|
#include "addnewtorrentdialog.h"
|
2015-09-23 23:20:22 +03:00
|
|
|
#include "guiiconprovider.h"
|
2018-02-25 21:07:37 +03:00
|
|
|
#include "lineedit.h"
|
2015-09-24 10:33:02 +03:00
|
|
|
#include "searchlistdelegate.h"
|
2018-01-29 17:05:29 +03:00
|
|
|
#include "searchsortmodel.h"
|
2018-06-14 12:54:23 +03:00
|
|
|
#include "ui_searchjobwidget.h"
|
2018-02-25 21:07:37 +03:00
|
|
|
#include "utils.h"
|
2008-06-25 23:29:29 +04:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
SearchJobWidget::SearchJobWidget(SearchHandler *searchHandler, QWidget *parent)
|
2015-09-24 10:33:02 +03:00
|
|
|
: QWidget(parent)
|
2018-06-14 12:54:23 +03:00
|
|
|
, m_ui(new Ui::SearchJobWidget)
|
2018-01-29 17:05:29 +03:00
|
|
|
, m_searchHandler(searchHandler)
|
2008-06-25 23:29:29 +04:00
|
|
|
{
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->setupUi(this);
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2015-11-18 19:13:25 +03:00
|
|
|
// This hack fixes reordering of first column with Qt5.
|
|
|
|
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
|
|
|
|
QTableView unused;
|
2016-02-27 23:37:41 +03:00
|
|
|
unused.setVerticalHeader(m_ui->resultsBrowser->header());
|
|
|
|
m_ui->resultsBrowser->header()->setParent(m_ui->resultsBrowser);
|
2015-11-18 19:13:25 +03:00
|
|
|
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
|
2017-01-19 15:10:09 +03:00
|
|
|
|
2016-06-22 16:34:53 +03:00
|
|
|
loadSettings();
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
2016-06-22 16:34:53 +03:00
|
|
|
header()->setStretchLastSection(false);
|
2015-09-24 10:33:02 +03:00
|
|
|
|
2015-04-11 14:26:25 +03:00
|
|
|
// Set Search results list model
|
2015-09-24 10:33:02 +03:00
|
|
|
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"));
|
2015-09-23 23:20:22 +03:00
|
|
|
m_searchListModel->setHeaderData(SearchSortModel::LEECHES, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources"));
|
2015-09-24 10:33:02 +03:00
|
|
|
m_searchListModel->setHeaderData(SearchSortModel::ENGINE_URL, Qt::Horizontal, tr("Search engine"));
|
2016-06-16 21:44:33 +03:00
|
|
|
// Set columns text alignment
|
|
|
|
m_searchListModel->setHeaderData(SearchSortModel::SIZE, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
|
|
|
|
m_searchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
|
|
|
|
m_searchListModel->setHeaderData(SearchSortModel::LEECHES, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
|
2015-04-11 14:26:25 +03:00
|
|
|
|
2015-09-24 10:33:02 +03:00
|
|
|
m_proxyModel = new SearchSortModel(this);
|
|
|
|
m_proxyModel->setDynamicSortFilter(true);
|
|
|
|
m_proxyModel->setSourceModel(m_searchListModel);
|
2018-01-29 17:05:29 +03:00
|
|
|
m_proxyModel->setNameFilter(searchHandler->pattern());
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsBrowser->setModel(m_proxyModel);
|
2015-04-11 14:26:25 +03:00
|
|
|
|
2015-09-24 10:33:02 +03:00
|
|
|
m_searchDelegate = new SearchListDelegate(this);
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsBrowser->setItemDelegate(m_searchDelegate);
|
2015-04-11 14:26:25 +03:00
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column
|
|
|
|
m_ui->resultsBrowser->hideColumn(SearchSortModel::DESC_LINK);
|
2015-04-11 14:26:25 +03:00
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsBrowser->setRootIsDecorated(false);
|
|
|
|
m_ui->resultsBrowser->setAllColumnsShowFocus(true);
|
|
|
|
m_ui->resultsBrowser->setSortingEnabled(true);
|
2015-04-11 14:26:25 +03:00
|
|
|
|
2018-05-31 12:19:07 +03:00
|
|
|
// Ensure that at least one column is visible at all times
|
2016-06-22 18:25:53 +03:00
|
|
|
bool atLeastOne = false;
|
2018-09-07 14:12:38 +03:00
|
|
|
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
|
2016-06-22 18:25:53 +03:00
|
|
|
if (!m_ui->resultsBrowser->isColumnHidden(i)) {
|
|
|
|
atLeastOne = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!atLeastOne)
|
|
|
|
m_ui->resultsBrowser->setColumnHidden(SearchSortModel::NAME, false);
|
2018-05-31 12:19:07 +03:00
|
|
|
// To also mitigate the above issue, we have to resize each column when
|
|
|
|
// its size is 0, because explicitly 'showing' the column isn't enough
|
|
|
|
// in the above scenario.
|
2018-09-07 14:12:38 +03:00
|
|
|
for (int i = 0; i < SearchSortModel::DL_LINK; ++i)
|
2016-06-22 18:25:53 +03:00
|
|
|
if ((m_ui->resultsBrowser->columnWidth(i) <= 0) && !m_ui->resultsBrowser->isColumnHidden(i))
|
|
|
|
m_ui->resultsBrowser->resizeColumnToContents(i);
|
|
|
|
|
|
|
|
header()->setContextMenuPolicy(Qt::CustomContextMenu);
|
2018-06-14 12:54:23 +03:00
|
|
|
connect(header(), &QWidget::customContextMenuRequested, this, &SearchJobWidget::displayToggleColumnsMenu);
|
|
|
|
connect(header(), &QHeaderView::sectionResized, this, &SearchJobWidget::saveSettings);
|
|
|
|
connect(header(), &QHeaderView::sectionMoved, this, &SearchJobWidget::saveSettings);
|
|
|
|
connect(header(), &QHeaderView::sortIndicatorChanged, this, &SearchJobWidget::saveSettings);
|
2015-09-23 23:20:22 +03:00
|
|
|
|
|
|
|
fillFilterComboBoxes();
|
|
|
|
|
|
|
|
updateFilter();
|
|
|
|
|
2018-02-25 21:07:37 +03:00
|
|
|
m_lineEditSearchResultsFilter = new LineEdit(this);
|
|
|
|
m_lineEditSearchResultsFilter->setPlaceholderText(tr("Filter search results..."));
|
2018-08-12 17:40:17 +03:00
|
|
|
m_lineEditSearchResultsFilter->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(m_lineEditSearchResultsFilter, &QWidget::customContextMenuRequested, this, &SearchJobWidget::showFilterContextMenu);
|
2018-02-25 21:07:37 +03:00
|
|
|
m_ui->horizontalLayout->insertWidget(0, m_lineEditSearchResultsFilter);
|
|
|
|
|
|
|
|
connect(m_lineEditSearchResultsFilter, &LineEdit::textChanged, this, &SearchJobWidget::filterSearchResults);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->filterMode, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
|
|
|
connect(m_ui->minSeeds, &QAbstractSpinBox::editingFinished, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->minSeeds, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
|
|
|
connect(m_ui->maxSeeds, &QAbstractSpinBox::editingFinished, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->maxSeeds, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
|
|
|
connect(m_ui->minSize, &QAbstractSpinBox::editingFinished, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->minSize, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
|
|
|
connect(m_ui->maxSize, &QAbstractSpinBox::editingFinished, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->maxSize, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->minSizeUnit, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_ui->maxSizeUnit, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
|
2018-06-14 12:54:23 +03:00
|
|
|
, this, &SearchJobWidget::updateFilter);
|
2018-01-29 17:05:29 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
connect(m_ui->resultsBrowser, &QAbstractItemView::doubleClicked, this, &SearchJobWidget::onItemDoubleClicked);
|
2018-01-29 17:05:29 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
connect(searchHandler, &SearchHandler::newSearchResults, this, &SearchJobWidget::appendSearchResults);
|
|
|
|
connect(searchHandler, &SearchHandler::searchFinished, this, &SearchJobWidget::searchFinished);
|
|
|
|
connect(searchHandler, &SearchHandler::searchFailed, this, &SearchJobWidget::searchFailed);
|
2018-01-29 17:05:29 +03:00
|
|
|
connect(this, &QObject::destroyed, searchHandler, &QObject::deleteLater);
|
2018-07-29 15:24:12 +03:00
|
|
|
|
2019-03-05 22:26:59 +03:00
|
|
|
setStatusTip(statusText(m_status));
|
2016-02-27 23:37:41 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
SearchJobWidget::~SearchJobWidget()
|
2016-02-27 23:37:41 +03:00
|
|
|
{
|
2016-06-22 16:34:53 +03:00
|
|
|
saveSettings();
|
2016-02-27 23:37:41 +03:00
|
|
|
delete m_ui;
|
2009-08-27 16:37:39 +04:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::onItemDoubleClicked(const QModelIndex &index)
|
2015-09-24 10:33:02 +03:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
downloadTorrent(index);
|
2008-06-25 23:29:29 +04:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
QHeaderView *SearchJobWidget::header() const
|
2015-09-24 10:33:02 +03:00
|
|
|
{
|
2016-02-27 23:37:41 +03:00
|
|
|
return m_ui->resultsBrowser->header();
|
2009-03-10 00:24:40 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:05:29 +03:00
|
|
|
// Set the color of a row in data model
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::setRowColor(int row, const QColor &color)
|
2008-06-25 23:29:29 +04:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
m_proxyModel->setDynamicSortFilter(false);
|
|
|
|
for (int i = 0; i < m_proxyModel->columnCount(); ++i)
|
|
|
|
m_proxyModel->setData(m_proxyModel->index(row, i), color, Qt::ForegroundRole);
|
|
|
|
|
|
|
|
m_proxyModel->setDynamicSortFilter(true);
|
2008-06-25 23:29:29 +04:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
SearchJobWidget::Status SearchJobWidget::status() const
|
2009-10-25 02:49:10 +04:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
return m_status;
|
2009-10-25 02:49:10 +04:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
int SearchJobWidget::visibleResultsCount() const
|
2008-06-25 23:29:29 +04:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
return m_proxyModel->rowCount();
|
2008-06-25 23:29:29 +04:00
|
|
|
}
|
|
|
|
|
2018-07-05 11:16:14 +03:00
|
|
|
LineEdit *SearchJobWidget::lineEditSearchResultsFilter() const
|
|
|
|
{
|
|
|
|
return m_lineEditSearchResultsFilter;
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::cancelSearch()
|
2015-09-24 10:33:02 +03:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
m_searchHandler->cancelSearch();
|
|
|
|
}
|
2015-09-24 10:33:02 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::downloadTorrents()
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
const QModelIndexList rows {m_ui->resultsBrowser->selectionModel()->selectedRows()};
|
|
|
|
for (const QModelIndex &rowIndex : rows)
|
|
|
|
downloadTorrent(rowIndex);
|
2008-06-25 23:29:29 +04:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::openTorrentPages()
|
2015-09-24 10:33:02 +03:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
const QModelIndexList rows {m_ui->resultsBrowser->selectionModel()->selectedRows()};
|
|
|
|
for (const QModelIndex &rowIndex : rows) {
|
|
|
|
const QString descrLink = m_proxyModel->data(
|
|
|
|
m_proxyModel->index(rowIndex.row(), SearchSortModel::DESC_LINK)).toString();
|
|
|
|
if (!descrLink.isEmpty())
|
|
|
|
QDesktopServices::openUrl(QUrl::fromEncoded(descrLink.toUtf8()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::copyTorrentURLs()
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
const QModelIndexList rows {m_ui->resultsBrowser->selectionModel()->selectedRows()};
|
|
|
|
QStringList urls;
|
|
|
|
for (const QModelIndex &rowIndex : rows) {
|
|
|
|
const QString descrLink = m_proxyModel->data(
|
|
|
|
m_proxyModel->index(rowIndex.row(), SearchSortModel::DESC_LINK)).toString();
|
|
|
|
if (!descrLink.isEmpty())
|
|
|
|
urls << descrLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!urls.empty()) {
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
2018-05-31 12:19:07 +03:00
|
|
|
clipboard->setText(urls.join('\n'));
|
2018-01-29 17:05:29 +03:00
|
|
|
}
|
2015-09-24 10:33:02 +03:00
|
|
|
}
|
2008-06-25 23:29:29 +04:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::setStatus(Status value)
|
2015-09-24 10:33:02 +03:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
if (m_status == value) return;
|
|
|
|
|
2015-09-24 10:33:02 +03:00
|
|
|
m_status = value;
|
2015-09-23 23:20:22 +03:00
|
|
|
setStatusTip(statusText(value));
|
2018-01-29 17:05:29 +03:00
|
|
|
emit statusChanged();
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::downloadTorrent(const QModelIndex &rowIndex)
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
const QString torrentUrl = m_proxyModel->data(
|
|
|
|
m_proxyModel->index(rowIndex.row(), SearchSortModel::DL_LINK)).toString();
|
|
|
|
const QString siteUrl = m_proxyModel->data(
|
|
|
|
m_proxyModel->index(rowIndex.row(), SearchSortModel::ENGINE_URL)).toString();
|
|
|
|
|
2019-06-03 12:38:02 +03:00
|
|
|
if (torrentUrl.startsWith("magnet:", Qt::CaseInsensitive)) {
|
2018-01-29 17:05:29 +03:00
|
|
|
addTorrentToSession(torrentUrl);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SearchDownloadHandler *downloadHandler = m_searchHandler->manager()->downloadTorrent(siteUrl, torrentUrl);
|
2018-06-14 12:54:23 +03:00
|
|
|
connect(downloadHandler, &SearchDownloadHandler::downloadFinished, this, &SearchJobWidget::addTorrentToSession);
|
2018-01-29 17:05:29 +03:00
|
|
|
connect(downloadHandler, &SearchDownloadHandler::downloadFinished, downloadHandler, &SearchDownloadHandler::deleteLater);
|
|
|
|
}
|
2018-07-29 15:33:06 +03:00
|
|
|
setRowColor(rowIndex.row(), QApplication::palette().color(QPalette::LinkVisited));
|
2018-01-29 17:05:29 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::addTorrentToSession(const QString &source)
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
if (source.isEmpty()) return;
|
|
|
|
|
|
|
|
if (AddNewTorrentDialog::isEnabled())
|
|
|
|
AddNewTorrentDialog::show(source, this);
|
|
|
|
else
|
|
|
|
BitTorrent::Session::instance()->addTorrent(source);
|
2015-09-23 23:20:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::updateResultsCount()
|
2015-09-23 23:20:22 +03:00
|
|
|
{
|
2018-01-29 17:05:29 +03:00
|
|
|
const int totalResults = m_searchListModel->rowCount();
|
|
|
|
const int filteredResults = m_proxyModel->rowCount();
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->resultsLbl->setText(tr("Results (showing <i>%1</i> out of <i>%2</i>):", "i.e: Search results")
|
|
|
|
.arg(filteredResults).arg(totalResults));
|
2018-01-29 17:05:29 +03:00
|
|
|
|
|
|
|
m_noSearchResults = (totalResults == 0);
|
|
|
|
emit resultsCountUpdated();
|
2015-09-23 23:20:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::updateFilter()
|
2015-09-23 23:20:22 +03:00
|
|
|
{
|
|
|
|
using Utils::Misc::SizeUnit;
|
2018-01-29 17:05:29 +03:00
|
|
|
|
|
|
|
m_proxyModel->enableNameFilter(filteringMode() == NameFilteringMode::OnlyNames);
|
2015-09-23 23:20:22 +03:00
|
|
|
// we update size and seeds filter parameters in the model even if they are disabled
|
2018-01-29 17:05:29 +03:00
|
|
|
m_proxyModel->setSeedsFilter(m_ui->minSeeds->value(), m_ui->maxSeeds->value());
|
|
|
|
m_proxyModel->setSizeFilter(
|
2016-02-27 23:37:41 +03:00
|
|
|
sizeInBytes(m_ui->minSize->value(), static_cast<SizeUnit>(m_ui->minSizeUnit->currentIndex())),
|
|
|
|
sizeInBytes(m_ui->maxSize->value(), static_cast<SizeUnit>(m_ui->maxSizeUnit->currentIndex())));
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2017-07-05 13:19:58 +03:00
|
|
|
nameFilteringModeSetting() = filteringMode();
|
2016-02-27 23:37:41 +03:00
|
|
|
|
2018-01-29 17:05:29 +03:00
|
|
|
m_proxyModel->invalidate();
|
2015-09-23 23:20:22 +03:00
|
|
|
updateResultsCount();
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::fillFilterComboBoxes()
|
2015-09-23 23:20:22 +03:00
|
|
|
{
|
|
|
|
using Utils::Misc::SizeUnit;
|
2018-04-02 19:53:44 +03:00
|
|
|
using Utils::Misc::unitString;
|
|
|
|
|
2015-09-23 23:20:22 +03:00
|
|
|
QStringList unitStrings;
|
|
|
|
unitStrings.append(unitString(SizeUnit::Byte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::KibiByte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::MebiByte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::GibiByte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::TebiByte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::PebiByte));
|
|
|
|
unitStrings.append(unitString(SizeUnit::ExbiByte));
|
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->minSizeUnit->clear();
|
|
|
|
m_ui->maxSizeUnit->clear();
|
|
|
|
m_ui->minSizeUnit->addItems(unitStrings);
|
|
|
|
m_ui->maxSizeUnit->addItems(unitStrings);
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->minSize->setValue(0);
|
|
|
|
m_ui->minSizeUnit->setCurrentIndex(static_cast<int>(SizeUnit::MebiByte));
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->maxSize->setValue(-1);
|
2016-05-30 23:51:09 +03:00
|
|
|
m_ui->maxSizeUnit->setCurrentIndex(static_cast<int>(SizeUnit::GibiByte));
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2016-02-27 23:37:41 +03:00
|
|
|
m_ui->filterMode->clear();
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2017-07-05 13:19:58 +03:00
|
|
|
m_ui->filterMode->addItem(tr("Torrent names only"), static_cast<int>(NameFilteringMode::OnlyNames));
|
|
|
|
m_ui->filterMode->addItem(tr("Everywhere"), static_cast<int>(NameFilteringMode::Everywhere));
|
2015-09-23 23:20:22 +03:00
|
|
|
|
2017-07-05 13:19:58 +03:00
|
|
|
QVariant selectedMode = static_cast<int>(nameFilteringModeSetting().value());
|
2016-02-27 23:37:41 +03:00
|
|
|
int index = m_ui->filterMode->findData(selectedMode);
|
2018-05-31 12:19:07 +03:00
|
|
|
m_ui->filterMode->setCurrentIndex((index == -1) ? 0 : index);
|
2015-09-23 23:20:22 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 21:07:37 +03:00
|
|
|
void SearchJobWidget::filterSearchResults(const QString &name)
|
|
|
|
{
|
2018-08-12 17:40:17 +03:00
|
|
|
const QRegExp::PatternSyntax patternSyntax = Preferences::instance()->getRegexAsFilteringPatternForSearchJob()
|
|
|
|
? QRegExp::RegExp : QRegExp::WildcardUnix;
|
|
|
|
m_proxyModel->setFilterRegExp(QRegExp(name, Qt::CaseInsensitive, patternSyntax));
|
2018-02-25 21:07:37 +03:00
|
|
|
updateResultsCount();
|
|
|
|
}
|
|
|
|
|
2018-08-12 17:40:17 +03:00
|
|
|
void SearchJobWidget::showFilterContextMenu(const QPoint &)
|
|
|
|
{
|
|
|
|
const Preferences *pref = Preferences::instance();
|
|
|
|
|
|
|
|
QMenu *menu = m_lineEditSearchResultsFilter->createStandardContextMenu();
|
|
|
|
menu->addSeparator();
|
|
|
|
QAction *useRegexAct = new QAction(tr("Use regular expressions"), menu);
|
|
|
|
useRegexAct->setCheckable(true);
|
|
|
|
useRegexAct->setChecked(pref->getRegexAsFilteringPatternForSearchJob());
|
|
|
|
menu->addAction(useRegexAct);
|
|
|
|
|
|
|
|
connect(useRegexAct, &QAction::toggled, pref, &Preferences::setRegexAsFilteringPatternForSearchJob);
|
|
|
|
connect(useRegexAct, &QAction::toggled, this, [this]() { filterSearchResults(m_lineEditSearchResultsFilter->text()); });
|
|
|
|
|
|
|
|
menu->exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
QString SearchJobWidget::statusText(SearchJobWidget::Status st)
|
2015-09-23 23:20:22 +03:00
|
|
|
{
|
|
|
|
switch (st) {
|
|
|
|
case Status::Ongoing:
|
|
|
|
return tr("Searching...");
|
|
|
|
case Status::Finished:
|
|
|
|
return tr("Search has finished");
|
|
|
|
case Status::Aborted:
|
|
|
|
return tr("Search aborted");
|
|
|
|
case Status::Error:
|
|
|
|
return tr("An error occurred during search...");
|
|
|
|
case Status::NoResults:
|
|
|
|
return tr("Search returned no results");
|
|
|
|
default:
|
2019-02-14 20:16:42 +03:00
|
|
|
return {};
|
2015-09-23 23:20:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
SearchJobWidget::NameFilteringMode SearchJobWidget::filteringMode() const
|
2015-09-23 23:20:22 +03:00
|
|
|
{
|
2017-07-05 13:19:58 +03:00
|
|
|
return static_cast<NameFilteringMode>(m_ui->filterMode->itemData(m_ui->filterMode->currentIndex()).toInt());
|
2015-09-24 10:33:02 +03:00
|
|
|
}
|
2016-06-22 16:34:53 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::loadSettings()
|
2016-06-22 16:34:53 +03:00
|
|
|
{
|
|
|
|
header()->restoreState(Preferences::instance()->getSearchTabHeaderState());
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::saveSettings() const
|
2016-06-22 16:34:53 +03:00
|
|
|
{
|
|
|
|
Preferences::instance()->setSearchTabHeaderState(header()->saveState());
|
|
|
|
}
|
2016-06-22 18:25:53 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::displayToggleColumnsMenu(const QPoint&)
|
2016-06-22 18:25:53 +03:00
|
|
|
{
|
|
|
|
QMenu hideshowColumn(this);
|
|
|
|
hideshowColumn.setTitle(tr("Column visibility"));
|
|
|
|
QList<QAction*> actions;
|
|
|
|
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
|
|
|
|
QAction *myAct = hideshowColumn.addAction(m_searchListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
|
|
|
|
myAct->setCheckable(true);
|
|
|
|
myAct->setChecked(!m_ui->resultsBrowser->isColumnHidden(i));
|
|
|
|
actions.append(myAct);
|
|
|
|
}
|
|
|
|
int visibleCols = 0;
|
2018-09-07 14:12:38 +03:00
|
|
|
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
|
2016-06-22 18:25:53 +03:00
|
|
|
if (!m_ui->resultsBrowser->isColumnHidden(i))
|
2018-05-30 20:06:28 +03:00
|
|
|
++visibleCols;
|
2016-06-22 18:25:53 +03:00
|
|
|
|
|
|
|
if (visibleCols > 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call menu
|
|
|
|
QAction *act = hideshowColumn.exec(QCursor::pos());
|
|
|
|
if (act) {
|
|
|
|
int col = actions.indexOf(act);
|
|
|
|
Q_ASSERT(col >= 0);
|
|
|
|
Q_ASSERT(visibleCols > 0);
|
|
|
|
if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (visibleCols == 1))
|
|
|
|
return;
|
|
|
|
m_ui->resultsBrowser->setColumnHidden(col, !m_ui->resultsBrowser->isColumnHidden(col));
|
|
|
|
if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (m_ui->resultsBrowser->columnWidth(col) <= 5))
|
2017-12-08 17:05:21 +03:00
|
|
|
m_ui->resultsBrowser->resizeColumnToContents(col);
|
2016-06-22 18:25:53 +03:00
|
|
|
saveSettings();
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 13:19:58 +03:00
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::searchFinished(bool cancelled)
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
if (cancelled)
|
|
|
|
setStatus(Status::Aborted);
|
|
|
|
else if (m_noSearchResults)
|
|
|
|
setStatus(Status::NoResults);
|
|
|
|
else
|
|
|
|
setStatus(Status::Finished);
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::searchFailed()
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
setStatus(Status::Error);
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
void SearchJobWidget::appendSearchResults(const QList<SearchResult> &results)
|
2018-01-29 17:05:29 +03:00
|
|
|
{
|
|
|
|
for (const SearchResult &result : results) {
|
|
|
|
// Add item to search result list
|
|
|
|
int row = m_searchListModel->rowCount();
|
|
|
|
m_searchListModel->insertRow(row);
|
|
|
|
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::NAME), result.fileName); // Name
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::DL_LINK), result.fileUrl); // download URL
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::SIZE), result.fileSize); // Size
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::SEEDS), result.nbSeeders); // Seeders
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::LEECHES), result.nbLeechers); // Leechers
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::ENGINE_URL), result.siteUrl); // Search site URL
|
|
|
|
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::DESC_LINK), result.descrLink); // Description Link
|
|
|
|
}
|
|
|
|
|
|
|
|
updateResultsCount();
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:54:23 +03:00
|
|
|
CachedSettingValue<SearchJobWidget::NameFilteringMode> &SearchJobWidget::nameFilteringModeSetting()
|
2017-07-05 13:19:58 +03:00
|
|
|
{
|
|
|
|
static CachedSettingValue<NameFilteringMode> setting("Search/FilteringMode", NameFilteringMode::OnlyNames);
|
|
|
|
return setting;
|
|
|
|
}
|
2019-03-22 21:01:35 +03:00
|
|
|
|
|
|
|
void SearchJobWidget::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
downloadTorrents();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|