2007-03-27 22:49:29 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2007-03-27 22:49:29 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +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.
|
2007-03-27 22:49:29 +04:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2007-03-27 22:49:29 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QCompleter>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QSystemTrayIcon>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QSystemTrayIcon>
|
|
|
|
#include <iostream>
|
2007-11-30 13:48:00 +03:00
|
|
|
#include <QTimer>
|
2007-03-27 22:49:29 +04:00
|
|
|
|
|
|
|
#include "SearchListDelegate.h"
|
|
|
|
#include "searchEngine.h"
|
|
|
|
#include "bittorrent.h"
|
2007-07-20 21:11:02 +04:00
|
|
|
#include "downloadThread.h"
|
2007-03-27 22:49:29 +04:00
|
|
|
|
|
|
|
#define SEARCH_NAME 0
|
|
|
|
#define SEARCH_SIZE 1
|
|
|
|
#define SEARCH_SEEDERS 2
|
|
|
|
#define SEARCH_LEECHERS 3
|
|
|
|
#define SEARCH_ENGINE 4
|
|
|
|
|
|
|
|
#define SEARCHHISTORY_MAXSIZE 50
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
/*TAB SYSTEM TO MOVE IN ANOTHER FILE*/
|
|
|
|
QList<TabbedSearch*>* TabbedSearch::all_tab = new QList<TabbedSearch*>();
|
|
|
|
TabbedSearch::TabbedSearch(QString &title,QTabWidget *tab_barWidget,SearchEngine *searchEngi) : QWidget()
|
|
|
|
{
|
|
|
|
box=new QVBoxLayout();
|
|
|
|
results_lbl=new QLabel();
|
|
|
|
resultsBrowser = new QTreeView();
|
|
|
|
box->addWidget(results_lbl);
|
|
|
|
box->addWidget(resultsBrowser);
|
|
|
|
|
|
|
|
setLayout(box);
|
|
|
|
// Set Search results list model
|
|
|
|
SearchListModel = new QStandardItemModel(0,5);
|
|
|
|
SearchListModel->setHeaderData(SEARCH_NAME, Qt::Horizontal, tr("Name", "i.e: file name"));
|
|
|
|
SearchListModel->setHeaderData(SEARCH_SIZE, Qt::Horizontal, tr("Size", "i.e: file size"));
|
|
|
|
SearchListModel->setHeaderData(SEARCH_SEEDERS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources"));
|
|
|
|
SearchListModel->setHeaderData(SEARCH_LEECHERS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources"));
|
|
|
|
SearchListModel->setHeaderData(SEARCH_ENGINE, Qt::Horizontal, tr("Search engine"));
|
|
|
|
resultsBrowser->setModel(SearchListModel);
|
|
|
|
SearchDelegate = new SearchListDelegate();
|
|
|
|
resultsBrowser->setItemDelegate(SearchDelegate);
|
|
|
|
// Make search list header clickable for sorting
|
|
|
|
resultsBrowser->header()->setClickable(true);
|
|
|
|
resultsBrowser->header()->setSortIndicatorShown(true);
|
|
|
|
|
|
|
|
// Connect signals to slots (search part)
|
|
|
|
connect(resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), searchEngi, SLOT(downloadSelectedItem(const QModelIndex&)));
|
|
|
|
connect(resultsBrowser->header(), SIGNAL(sectionPressed(int)), this, SLOT(sortSearchList(int)));
|
|
|
|
|
|
|
|
// Load last columns width for search results list
|
|
|
|
if(!loadColWidthSearchList()){
|
|
|
|
resultsBrowser->header()->resizeSection(0, 275);
|
|
|
|
}
|
|
|
|
tab_barWidget->addTab(this, title);
|
|
|
|
all_tab->append(this);
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
TabbedSearch::~TabbedSearch()
|
|
|
|
{
|
|
|
|
saveColWidthSearchList();
|
|
|
|
delete resultsBrowser;
|
|
|
|
delete SearchListModel;
|
|
|
|
delete SearchDelegate;
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
/*LES FONCTIONS POUR GET LA BETE*/
|
|
|
|
QLabel* TabbedSearch::getCurrentLabel()
|
|
|
|
{
|
|
|
|
return results_lbl;
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
QTreeView* TabbedSearch::getCurrentTreeView()
|
|
|
|
{
|
|
|
|
return resultsBrowser;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItemModel* TabbedSearch::getCurrentSearchListModel()
|
|
|
|
{
|
|
|
|
return SearchListModel;
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the color of a row in data model
|
2008-06-25 23:04:42 +04:00
|
|
|
void TabbedSearch::setRowColor(int row, QString color){
|
2007-03-27 22:49:29 +04:00
|
|
|
for(int i=0; i<SearchListModel->columnCount(); ++i){
|
2007-07-31 14:39:03 +04:00
|
|
|
SearchListModel->setData(SearchListModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
void TabbedSearch::sortSearchList(int index){
|
2007-03-27 22:49:29 +04:00
|
|
|
static Qt::SortOrder sortOrder = Qt::AscendingOrder;
|
|
|
|
if(resultsBrowser->header()->sortIndicatorSection() == index){
|
2008-06-25 23:04:42 +04:00
|
|
|
sortOrder = (sortOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder; ;
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
resultsBrowser->header()->setSortIndicator(index, sortOrder);
|
|
|
|
switch(index){
|
|
|
|
//case SIZE:
|
|
|
|
case SEEDERS:
|
|
|
|
case LEECHERS:
|
|
|
|
case SIZE:
|
|
|
|
sortSearchListInt(index, sortOrder);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sortSearchListString(index, sortOrder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
void TabbedSearch::sortSearchListInt(int index, Qt::SortOrder sortOrder){
|
2007-03-27 22:49:29 +04:00
|
|
|
QList<QPair<int, qlonglong> > lines;
|
|
|
|
// Insertion sorting
|
|
|
|
for(int i=0; i<SearchListModel->rowCount(); ++i){
|
|
|
|
misc::insertSort(lines, QPair<int,qlonglong>(i, SearchListModel->data(SearchListModel->index(i, index)).toLongLong()), sortOrder);
|
|
|
|
}
|
|
|
|
// Insert items in new model, in correct order
|
|
|
|
int nbRows_old = lines.size();
|
|
|
|
for(int row=0; row<lines.size(); ++row){
|
|
|
|
SearchListModel->insertRow(SearchListModel->rowCount());
|
|
|
|
int sourceRow = lines[row].first;
|
|
|
|
for(int col=0; col<5; ++col){
|
|
|
|
SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col)));
|
2007-07-31 14:39:03 +04:00
|
|
|
SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove old rows
|
|
|
|
SearchListModel->removeRows(0, nbRows_old);
|
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
void TabbedSearch::sortSearchListString(int index, Qt::SortOrder sortOrder){
|
2007-03-27 22:49:29 +04:00
|
|
|
QList<QPair<int, QString> > lines;
|
|
|
|
// Insetion sorting
|
|
|
|
for(int i=0; i<SearchListModel->rowCount(); ++i){
|
|
|
|
misc::insertSortString(lines, QPair<int, QString>(i, SearchListModel->data(SearchListModel->index(i, index)).toString()), sortOrder);
|
|
|
|
}
|
|
|
|
// Insert items in new model, in correct order
|
|
|
|
int nbRows_old = lines.size();
|
|
|
|
for(int row=0; row<nbRows_old; ++row){
|
|
|
|
SearchListModel->insertRow(SearchListModel->rowCount());
|
|
|
|
int sourceRow = lines[row].first;
|
|
|
|
for(int col=0; col<5; ++col){
|
|
|
|
SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col)));
|
2007-07-31 14:39:03 +04:00
|
|
|
SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove old rows
|
|
|
|
SearchListModel->removeRows(0, nbRows_old);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save columns width in a file to remember them
|
|
|
|
// (download list)
|
2008-06-25 23:04:42 +04:00
|
|
|
void TabbedSearch::saveColWidthSearchList() const{
|
2007-03-27 22:49:29 +04:00
|
|
|
qDebug("Saving columns width in search list");
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QStringList width_list;
|
|
|
|
for(int i=0; i<SearchListModel->columnCount(); ++i){
|
2007-08-20 11:03:41 +04:00
|
|
|
width_list << misc::toQString(resultsBrowser->columnWidth(i));
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
settings.setValue("SearchListColsWidth", width_list.join(" "));
|
|
|
|
qDebug("Search list columns width saved");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load columns width in a file that were saved previously
|
|
|
|
// (search list)
|
2008-06-25 23:04:42 +04:00
|
|
|
bool TabbedSearch::loadColWidthSearchList(){
|
2007-03-27 22:49:29 +04:00
|
|
|
qDebug("Loading columns width for search list");
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QString line = settings.value("SearchListColsWidth", QString()).toString();
|
|
|
|
if(line.isEmpty())
|
|
|
|
return false;
|
|
|
|
QStringList width_list = line.split(' ');
|
|
|
|
if(width_list.size() != SearchListModel->columnCount())
|
|
|
|
return false;
|
|
|
|
for(int i=0; i<width_list.size(); ++i){
|
|
|
|
resultsBrowser->header()->resizeSection(i, width_list.at(i).toInt());
|
|
|
|
}
|
|
|
|
qDebug("Search list columns width loaded");
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-25 23:04:42 +04:00
|
|
|
/*END TAB SYSTME*/
|
|
|
|
|
|
|
|
/*SEARCH ENGINE START*/
|
|
|
|
SearchEngine::SearchEngine(bittorrent *BTSession, QSystemTrayIcon *myTrayIcon, bool systrayIntegration) : QWidget(), BTSession(BTSession), myTrayIcon(myTrayIcon), systrayIntegration(systrayIntegration){
|
|
|
|
setupUi(this);
|
|
|
|
downloader = new downloadThread(this);
|
|
|
|
|
|
|
|
// new qCompleter to the search pattern
|
|
|
|
startSearchHistory();
|
|
|
|
searchCompleter = new QCompleter(searchHistory, this);
|
|
|
|
searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
search_pattern->setCompleter(searchCompleter);
|
|
|
|
|
|
|
|
// Boolean initialization
|
|
|
|
search_stopped = false;
|
|
|
|
// Creating Search Process
|
|
|
|
searchProcess = new QProcess(this);
|
|
|
|
connect(searchProcess, SIGNAL(started()), this, SLOT(searchStarted()));
|
|
|
|
connect(searchProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readSearchOutput()));
|
|
|
|
connect(searchProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(searchFinished(int,QProcess::ExitStatus)));
|
|
|
|
searchTimeout = new QTimer(this);
|
|
|
|
searchTimeout->setSingleShot(true);
|
|
|
|
connect(searchTimeout, SIGNAL(timeout()), this, SLOT(on_stop_search_button_clicked()));
|
|
|
|
// Check last enabled search engines
|
|
|
|
loadEngineSettings();
|
|
|
|
// Update nova.py search plugin if necessary
|
|
|
|
updateNova();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchEngine::~SearchEngine(){
|
|
|
|
qDebug("Search destruction");
|
|
|
|
// save the searchHistory for later uses
|
|
|
|
saveSearchHistory();
|
|
|
|
searchProcess->kill();
|
|
|
|
searchProcess->waitForFinished();
|
|
|
|
delete searchTimeout;
|
|
|
|
delete searchProcess;
|
|
|
|
delete searchCompleter;
|
|
|
|
delete downloader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEngine::on_enginesButton_clicked() {
|
|
|
|
engineSelectDlg *dlg = new engineSelectDlg(this);
|
|
|
|
connect(dlg, SIGNAL(enginesChanged()), this, SLOT(loadEngineSettings()));
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
|
|
|
|
// get the last searchs from a QSettings to a QStringList
|
|
|
|
void SearchEngine::startSearchHistory(){
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
settings.beginGroup("Search");
|
|
|
|
searchHistory = settings.value("searchHistory",-1).toStringList();
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
2007-08-31 16:06:31 +04:00
|
|
|
void SearchEngine::loadEngineSettings() {
|
|
|
|
qDebug("Loading engine settings");
|
|
|
|
enabled_engines.clear();
|
|
|
|
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
QStringList known_engines = settings.value(QString::fromUtf8("SearchEngines/knownEngines"), QStringList()).toStringList();
|
|
|
|
QVariantList known_enginesEnabled = settings.value(QString::fromUtf8("SearchEngines/knownEnginesEnabled"), QList<QVariant>()).toList();
|
|
|
|
QString engine;
|
|
|
|
unsigned int i = 0;
|
|
|
|
foreach(engine, known_engines) {
|
|
|
|
if(known_enginesEnabled.at(i).toBool())
|
|
|
|
enabled_engines << engine;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if(enabled_engines.empty())
|
|
|
|
enabled_engines << "all";
|
|
|
|
qDebug("Engine settings loaded");
|
|
|
|
}
|
|
|
|
|
2007-03-27 22:49:29 +04:00
|
|
|
// Save the history list into the QSettings for the next session
|
|
|
|
void SearchEngine::saveSearchHistory()
|
|
|
|
{
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
settings.beginGroup("Search");
|
|
|
|
settings.setValue("searchHistory",searchHistory);
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function called when we click on search button
|
|
|
|
void SearchEngine::on_search_button_clicked(){
|
2007-07-22 17:42:09 +04:00
|
|
|
if(searchProcess->state() != QProcess::NotRunning){
|
|
|
|
searchProcess->kill();
|
|
|
|
searchProcess->waitForFinished();
|
|
|
|
}
|
2007-11-30 13:48:00 +03:00
|
|
|
if(searchTimeout->isActive()) {
|
|
|
|
searchTimeout->stop();
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
QString pattern = search_pattern->text().trimmed();
|
2008-06-25 23:04:42 +04:00
|
|
|
//AJOUT TAB obligé de passé le widget en param sinon crash
|
|
|
|
tab_search=new TabbedSearch(pattern,tabWidget,this);
|
2007-03-27 22:49:29 +04:00
|
|
|
// No search pattern entered
|
|
|
|
if(pattern.isEmpty()){
|
|
|
|
QMessageBox::critical(0, tr("Empty search pattern"), tr("Please type a search pattern first"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if the pattern is not in the pattern
|
|
|
|
if(searchHistory.indexOf(pattern) == -1){
|
|
|
|
//update the searchHistory list
|
|
|
|
searchHistory.append(pattern);
|
|
|
|
// verify the max size of the history
|
|
|
|
if(searchHistory.size() > SEARCHHISTORY_MAXSIZE)
|
|
|
|
searchHistory = searchHistory.mid(searchHistory.size()/2,searchHistory.size()/2);
|
|
|
|
searchCompleter = new QCompleter(searchHistory, this);
|
|
|
|
searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
search_pattern->setCompleter(searchCompleter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Getting checked search engines
|
2007-08-31 16:06:31 +04:00
|
|
|
Q_ASSERT(!enabled_engines.empty());
|
2007-03-27 22:49:29 +04:00
|
|
|
QStringList params;
|
|
|
|
QStringList engineNames;
|
|
|
|
search_stopped = false;
|
2007-12-12 20:14:15 +03:00
|
|
|
params << misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2.py";
|
2007-08-31 16:06:31 +04:00
|
|
|
params << enabled_engines.join(",");
|
2007-03-27 22:49:29 +04:00
|
|
|
params << pattern.split(" ");
|
|
|
|
// Update SearchEngine widgets
|
|
|
|
no_search_results = true;
|
|
|
|
nb_search_results = 0;
|
|
|
|
search_result_line_truncated.clear();
|
2008-06-25 23:04:42 +04:00
|
|
|
//on change le texte du label courrant
|
|
|
|
tab_search->getCurrentLabel()->setText(tr("Results")+" <i>(0)</i>:");
|
2007-03-27 22:49:29 +04:00
|
|
|
// Launch search
|
2007-12-12 20:14:15 +03:00
|
|
|
searchProcess->start("python", params, QIODevice::ReadOnly);
|
2007-11-30 13:48:00 +03:00
|
|
|
searchTimeout->start(180000); // 3min
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEngine::searchStarted(){
|
|
|
|
// Update SearchEngine widgets
|
|
|
|
search_status->setText(tr("Searching..."));
|
|
|
|
search_status->repaint();
|
|
|
|
stop_search_button->setEnabled(true);
|
|
|
|
stop_search_button->repaint();
|
2008-06-25 23:04:42 +04:00
|
|
|
// clear results window ... not needed since we got Tabbed
|
|
|
|
//SearchListModel->removeRows(0, SearchListModel->rowCount());
|
2007-03-27 22:49:29 +04:00
|
|
|
// Clear previous results urls too
|
2008-06-25 23:04:42 +04:00
|
|
|
//searchResultsUrls.clear();
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download the given item from search results list
|
|
|
|
void SearchEngine::downloadSelectedItem(const QModelIndex& index){
|
|
|
|
int row = index.row();
|
|
|
|
// Get Item url
|
2008-06-25 23:04:42 +04:00
|
|
|
QString url = searchResultsUrls.value(TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->data(TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->index(row, NAME)).toString());
|
2007-03-27 22:49:29 +04:00
|
|
|
// Download from url
|
|
|
|
BTSession->downloadFromUrl(url);
|
|
|
|
// Set item color to RED
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->setRowColor(row, "red");
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// search Qprocess return output as soon as it gets new
|
|
|
|
// stuff to read. We split it into lines and add each
|
|
|
|
// line to search results calling appendSearchResult().
|
|
|
|
void SearchEngine::readSearchOutput(){
|
|
|
|
QByteArray output = searchProcess->readAllStandardOutput();
|
2007-12-13 00:45:44 +03:00
|
|
|
output.replace("\r", "");
|
2007-03-27 22:49:29 +04:00
|
|
|
QList<QByteArray> lines_list = output.split('\n');
|
|
|
|
QByteArray line;
|
|
|
|
if(!search_result_line_truncated.isEmpty()){
|
|
|
|
QByteArray end_of_line = lines_list.takeFirst();
|
|
|
|
lines_list.prepend(search_result_line_truncated+end_of_line);
|
|
|
|
}
|
|
|
|
search_result_line_truncated = lines_list.takeLast().trimmed();
|
|
|
|
foreach(line, lines_list){
|
|
|
|
appendSearchResult(QString(line));
|
|
|
|
}
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentLabel()->setText(tr("Results")+QString::fromUtf8(" <i>(")+misc::toQString(nb_search_results)+QString::fromUtf8(")</i>:"));
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
2007-08-31 16:06:31 +04:00
|
|
|
// Update nova.py search plugin if necessary
|
|
|
|
void SearchEngine::updateNova() {
|
|
|
|
qDebug("Updating nova");
|
|
|
|
// create search_engine directory if necessary
|
|
|
|
QDir search_dir(misc::qBittorrentPath()+"search_engine");
|
|
|
|
if(!search_dir.exists()){
|
|
|
|
search_dir.mkdir(misc::qBittorrentPath()+"search_engine");
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2007-08-31 16:06:31 +04:00
|
|
|
QFile package_file(search_dir.path()+QDir::separator()+"__init__.py");
|
|
|
|
package_file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
package_file.close();
|
|
|
|
if(!search_dir.exists("engines")){
|
|
|
|
search_dir.mkdir("engines");
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2007-08-31 16:06:31 +04:00
|
|
|
QFile package_file2(search_dir.path()+QDir::separator()+"engines"+QDir::separator()+"__init__.py");
|
|
|
|
package_file2.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
package_file2.close();
|
|
|
|
// Copy search plugin files (if necessary)
|
|
|
|
QString filePath = misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2.py";
|
|
|
|
if(misc::getPluginVersion(":/search_engine/nova2.py") > misc::getPluginVersion(filePath)) {
|
|
|
|
if(QFile::exists(filePath))
|
|
|
|
QFile::remove(filePath);
|
2007-09-02 11:58:25 +04:00
|
|
|
QFile::copy(":/search_engine/nova2.py", filePath);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2007-08-31 16:06:31 +04:00
|
|
|
// Set permissions
|
2007-03-27 22:49:29 +04:00
|
|
|
QFile::Permissions perm=QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadUser | QFile::WriteUser | QFile::ExeUser | QFile::ReadGroup | QFile::ReadGroup;
|
2007-08-31 16:06:31 +04:00
|
|
|
QFile(misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2.py").setPermissions(perm);
|
2007-09-02 11:58:25 +04:00
|
|
|
filePath = misc::qBittorrentPath()+"search_engine"+QDir::separator()+"novaprinter.py";
|
|
|
|
if(misc::getPluginVersion(":/search_engine/novaprinter.py") > misc::getPluginVersion(filePath)) {
|
|
|
|
if(QFile::exists(filePath)){
|
|
|
|
QFile::remove(filePath);
|
|
|
|
}
|
|
|
|
QFile::copy(":/search_engine/novaprinter.py", filePath);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2007-08-31 20:02:01 +04:00
|
|
|
QString destDir = misc::qBittorrentPath()+"search_engine"+QDir::separator()+"engines"+QDir::separator();
|
|
|
|
QDir shipped_subDir(":/search_engine/engines/");
|
|
|
|
QStringList files = shipped_subDir.entryList();
|
2007-08-31 16:06:31 +04:00
|
|
|
QString file;
|
|
|
|
foreach(file, files){
|
2007-12-12 19:56:32 +03:00
|
|
|
QString shipped_file = shipped_subDir.path()+"/"+file;
|
2007-08-31 16:06:31 +04:00
|
|
|
// Copy python classes
|
|
|
|
if(file.endsWith(".py")) {
|
2007-08-31 20:02:01 +04:00
|
|
|
if(misc::getPluginVersion(shipped_file) > misc::getPluginVersion(destDir+file) ) {
|
|
|
|
qDebug("shippped %s is more recent then local plugin, updating", file.toUtf8().data());
|
|
|
|
if(QFile::exists(destDir+file)) {
|
|
|
|
qDebug("Removing old %s", (destDir+file).toUtf8().data());
|
|
|
|
QFile::remove(destDir+file);
|
|
|
|
}
|
|
|
|
qDebug("%s copied to %s", shipped_file.toUtf8().data(), (destDir+file).toUtf8().data());
|
|
|
|
QFile::copy(shipped_file, destDir+file);
|
2007-08-31 16:06:31 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy icons
|
|
|
|
if(file.endsWith(".png")) {
|
2007-08-31 20:02:01 +04:00
|
|
|
if(!QFile::exists(destDir+file)) {
|
|
|
|
QFile::copy(shipped_file, destDir+file);
|
2007-08-31 16:06:31 +04:00
|
|
|
}
|
2007-07-21 00:30:33 +04:00
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slot called when search is Finished
|
|
|
|
// Search can be finished for 3 reasons :
|
|
|
|
// Error | Stopped by user | Finished normally
|
|
|
|
void SearchEngine::searchFinished(int exitcode,QProcess::ExitStatus){
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
2007-09-08 21:07:29 +04:00
|
|
|
bool useNotificationBalloons = settings.value("Preferences/General/NotificationBaloons", true).toBool();
|
|
|
|
if(systrayIntegration && useNotificationBalloons) {
|
2007-03-27 22:49:29 +04:00
|
|
|
myTrayIcon->showMessage(tr("Search Engine"), tr("Search has finished"), QSystemTrayIcon::Information, TIME_TRAY_BALLOON);
|
|
|
|
}
|
|
|
|
if(exitcode){
|
|
|
|
search_status->setText(tr("An error occured during search..."));
|
|
|
|
}else{
|
|
|
|
if(search_stopped){
|
|
|
|
search_status->setText(tr("Search aborted"));
|
|
|
|
}else{
|
|
|
|
if(no_search_results){
|
|
|
|
search_status->setText(tr("Search returned no results"));
|
|
|
|
}else{
|
|
|
|
search_status->setText(tr("Search has finished"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentLabel()->setText(tr("Results", "i.e: Search results")+QString::fromUtf8(" <i>(")+misc::toQString(nb_search_results)+QString::fromUtf8(")</i>:"));
|
2007-03-27 22:49:29 +04:00
|
|
|
search_button->setEnabled(true);
|
|
|
|
stop_search_button->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// SLOT to append one line to search results list
|
|
|
|
// Line is in the following form :
|
|
|
|
// file url | file name | file size | nb seeds | nb leechers | Search engine url
|
2007-07-22 13:47:27 +04:00
|
|
|
void SearchEngine::appendSearchResult(QString line){
|
2007-03-27 22:49:29 +04:00
|
|
|
QStringList parts = line.split("|");
|
|
|
|
if(parts.size() != 6){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString url = parts.takeFirst();
|
|
|
|
QString filename = parts.first();
|
|
|
|
// XXX: Two results can't have the same name (right?)
|
|
|
|
if(searchResultsUrls.contains(filename)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add item to search result list
|
2008-06-25 23:04:42 +04:00
|
|
|
int row = tab_search->getCurrentSearchListModel()->rowCount();
|
|
|
|
tab_search->getCurrentSearchListModel()->insertRow(row);
|
2007-03-27 22:49:29 +04:00
|
|
|
for(int i=0; i<5; ++i){
|
2007-09-02 11:58:25 +04:00
|
|
|
if(parts.at(i).toFloat() == -1 && i != SIZE)
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentSearchListModel()->setData(tab_search->getCurrentSearchListModel()->index(row, i), tr("Unknown"));
|
2007-09-02 10:39:55 +04:00
|
|
|
else
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentSearchListModel()->setData(tab_search->getCurrentSearchListModel()->index(row, i), QVariant(parts.at(i)));
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
// Add url to searchResultsUrls associative array
|
|
|
|
searchResultsUrls.insert(filename, url);
|
|
|
|
no_search_results = false;
|
|
|
|
++nb_search_results;
|
|
|
|
// Enable clear & download buttons
|
|
|
|
clear_button->setEnabled(true);
|
|
|
|
download_button->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop search while it is working in background
|
|
|
|
void SearchEngine::on_stop_search_button_clicked(){
|
|
|
|
// Kill process
|
|
|
|
searchProcess->terminate();
|
|
|
|
search_stopped = true;
|
2007-11-30 13:48:00 +03:00
|
|
|
searchTimeout->stop();
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear search results list
|
|
|
|
void SearchEngine::on_clear_button_clicked(){
|
2007-11-07 11:41:34 +03:00
|
|
|
// Kill process
|
|
|
|
searchProcess->terminate();
|
|
|
|
search_stopped = true;
|
2007-12-03 22:29:57 +03:00
|
|
|
searchTimeout->stop();
|
2007-03-27 22:49:29 +04:00
|
|
|
searchResultsUrls.clear();
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentSearchListModel()->removeRows(0, tab_search->getCurrentSearchListModel()->rowCount());
|
2007-03-27 22:49:29 +04:00
|
|
|
// Disable clear & download buttons
|
|
|
|
clear_button->setEnabled(false);
|
|
|
|
download_button->setEnabled(false);
|
2007-12-03 22:29:57 +03:00
|
|
|
nb_search_results = 0;
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->getCurrentLabel()->setText(tr("Results")+" <i>(0)</i>:");
|
2007-11-07 11:41:34 +03:00
|
|
|
// focus on search pattern
|
|
|
|
search_pattern->clear();
|
|
|
|
search_pattern->setFocus();
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
2007-10-14 16:21:51 +04:00
|
|
|
void SearchEngine::on_clearPatternButton_clicked() {
|
|
|
|
search_pattern->clear();
|
|
|
|
search_pattern->setFocus();
|
|
|
|
}
|
|
|
|
|
2007-03-27 22:49:29 +04:00
|
|
|
// Download selected items in search results list
|
|
|
|
void SearchEngine::on_download_button_clicked(){
|
2008-06-25 23:04:42 +04:00
|
|
|
//QModelIndexList selectedIndexes = tab_search->getCurrentTreeView()->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndexList selectedIndexes = TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes();
|
2007-03-27 22:49:29 +04:00
|
|
|
QModelIndex index;
|
|
|
|
foreach(index, selectedIndexes){
|
|
|
|
if(index.column() == NAME){
|
|
|
|
// Get Item url
|
|
|
|
QString url = searchResultsUrls.value(index.data().toString());
|
|
|
|
BTSession->downloadFromUrl(url);
|
2008-06-25 23:04:42 +04:00
|
|
|
tab_search->setRowColor(index.row(), "red");
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|