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.
|
|
|
|
*
|
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.
|
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* 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>
|
2008-06-25 23:29:29 +04:00
|
|
|
#include <QDir>
|
2009-07-12 10:42:38 +04:00
|
|
|
#include <QMenu>
|
2009-07-12 11:12:43 +04:00
|
|
|
#include <QClipboard>
|
|
|
|
#include <QMimeData>
|
2009-10-25 02:49:10 +04:00
|
|
|
#include <QSortFilterProxyModel>
|
2007-03-27 22:49:29 +04:00
|
|
|
|
|
|
|
#include "searchEngine.h"
|
|
|
|
#include "bittorrent.h"
|
2007-07-20 21:11:02 +04:00
|
|
|
#include "downloadThread.h"
|
2008-06-25 23:29:29 +04:00
|
|
|
#include "misc.h"
|
|
|
|
#include "SearchListDelegate.h"
|
2007-03-27 22:49:29 +04:00
|
|
|
|
|
|
|
#define SEARCHHISTORY_MAXSIZE 50
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
/*SEARCH ENGINE START*/
|
|
|
|
SearchEngine::SearchEngine(bittorrent *BTSession, QSystemTrayIcon *myTrayIcon, bool systrayIntegration) : QWidget(), BTSession(BTSession), myTrayIcon(myTrayIcon), systrayIntegration(systrayIntegration){
|
|
|
|
setupUi(this);
|
|
|
|
// new qCompleter to the search pattern
|
|
|
|
startSearchHistory();
|
2009-07-12 10:42:38 +04:00
|
|
|
createCompleter();
|
2008-07-06 22:39:07 +04:00
|
|
|
// Add close tab button
|
|
|
|
closeTab_button = new QPushButton();
|
2009-08-24 14:18:11 +04:00
|
|
|
closeTab_button->setIcon(QIcon(QString::fromUtf8(":/Icons/oxygen/tab-close.png")));
|
2008-07-06 22:39:07 +04:00
|
|
|
closeTab_button->setFlat(true);
|
|
|
|
connect(closeTab_button, SIGNAL(clicked()), this, SLOT(closeTab_button_clicked()));
|
|
|
|
tabWidget->setCornerWidget(closeTab_button);
|
2008-06-25 23:04:42 +04:00
|
|
|
// Boolean initialization
|
|
|
|
search_stopped = false;
|
|
|
|
// Creating Search Process
|
|
|
|
searchProcess = new QProcess(this);
|
2009-03-26 18:46:35 +03:00
|
|
|
QStringList env = QProcess::systemEnvironment();
|
|
|
|
searchProcess->setEnvironment(env);
|
2008-06-25 23:04:42 +04:00
|
|
|
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)));
|
2008-07-04 20:49:08 +04:00
|
|
|
connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tab_changed(int)));
|
2008-06-25 23:04:42 +04:00
|
|
|
searchTimeout = new QTimer(this);
|
|
|
|
searchTimeout->setSingleShot(true);
|
2009-08-25 06:31:36 +04:00
|
|
|
connect(searchTimeout, SIGNAL(timeout()), this, SLOT(on_search_button_clicked()));
|
2008-06-25 23:04:42 +04:00
|
|
|
// Update nova.py search plugin if necessary
|
|
|
|
updateNova();
|
2009-08-25 06:31:36 +04:00
|
|
|
supported_engines = new SupportedEngines();
|
|
|
|
// Fill in category combobox
|
|
|
|
fillCatCombobox();
|
2009-07-12 10:42:38 +04:00
|
|
|
connect(search_pattern, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(displayPatternContextMenu(QPoint)));
|
2008-06-25 23:04:42 +04:00
|
|
|
}
|
|
|
|
|
2009-08-25 06:31:36 +04:00
|
|
|
void SearchEngine::fillCatCombobox() {
|
|
|
|
comboCategory->clear();
|
|
|
|
comboCategory->addItem(full_cat_names["all"], QVariant("all"));
|
|
|
|
QStringList supported_cat = supported_engines->supportedCategories();
|
|
|
|
foreach(QString cat, supported_cat) {
|
|
|
|
qDebug("Supported category: %s", cat.toLocal8Bit().data());
|
|
|
|
comboCategory->addItem(full_cat_names[cat], QVariant(cat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchEngine::selectedCategory() const {
|
|
|
|
return comboCategory->itemData(comboCategory->currentIndex()).toString();
|
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
SearchEngine::~SearchEngine(){
|
|
|
|
qDebug("Search destruction");
|
|
|
|
// save the searchHistory for later uses
|
|
|
|
saveSearchHistory();
|
|
|
|
searchProcess->kill();
|
|
|
|
searchProcess->waitForFinished();
|
2009-08-25 06:31:36 +04:00
|
|
|
foreach(QProcess *downloader, downloaders) {
|
|
|
|
downloader->kill();
|
|
|
|
downloader->waitForFinished();
|
2009-11-18 13:29:20 +03:00
|
|
|
//delete downloader;
|
2009-08-25 06:31:36 +04:00
|
|
|
}
|
2008-06-25 23:04:42 +04:00
|
|
|
delete searchTimeout;
|
|
|
|
delete searchProcess;
|
2009-08-25 06:31:36 +04:00
|
|
|
delete supported_engines;
|
2009-07-12 10:42:38 +04:00
|
|
|
if(searchCompleter)
|
|
|
|
delete searchCompleter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEngine::displayPatternContextMenu(QPoint) {
|
|
|
|
QMenu myMenu(this);
|
2009-07-12 11:12:43 +04:00
|
|
|
QAction cutAct(QIcon(":/Icons/oxygen/edit-cut.png"), tr("Cut"), &myMenu);
|
|
|
|
QAction copyAct(QIcon(":/Icons/oxygen/edit-copy.png"), tr("Copy"), &myMenu);
|
|
|
|
QAction pasteAct(QIcon(":/Icons/oxygen/edit-paste.png"), tr("Paste"), &myMenu);
|
|
|
|
QAction clearAct(QIcon(":/Icons/oxygen/edit_clear.png"), tr("Clear field"), &myMenu);
|
|
|
|
QAction clearHistoryAct(QIcon(":/Icons/oxygen/edit-clear.png"), tr("Clear completion history"), &myMenu);
|
|
|
|
bool hasCopyAct = false;
|
|
|
|
if(search_pattern->hasSelectedText()) {
|
|
|
|
myMenu.addAction(&cutAct);
|
|
|
|
myMenu.addAction(©Act);
|
|
|
|
hasCopyAct = true;
|
|
|
|
}
|
|
|
|
if(qApp->clipboard()->mimeData()->hasText()) {
|
|
|
|
myMenu.addAction(&pasteAct);
|
|
|
|
hasCopyAct = true;
|
|
|
|
}
|
|
|
|
if(hasCopyAct)
|
|
|
|
myMenu.addSeparator();
|
2009-07-12 10:42:38 +04:00
|
|
|
myMenu.addAction(&clearHistoryAct);
|
2009-07-12 11:12:43 +04:00
|
|
|
myMenu.addAction(&clearAct);
|
2009-07-12 10:42:38 +04:00
|
|
|
QAction *act = myMenu.exec(QCursor::pos());
|
|
|
|
if(act != 0) {
|
|
|
|
if(act == &clearHistoryAct) {
|
2009-11-14 23:24:39 +03:00
|
|
|
searchHistory.setStringList(QStringList());
|
2009-07-12 11:12:43 +04:00
|
|
|
} else if (act == &pasteAct) {
|
|
|
|
} else if (act == &pasteAct) {
|
|
|
|
search_pattern->paste();
|
|
|
|
}
|
|
|
|
else if (act == &cutAct) {
|
|
|
|
search_pattern->cut();
|
|
|
|
}
|
|
|
|
else if (act == ©Act) {
|
|
|
|
search_pattern->copy();
|
|
|
|
}
|
|
|
|
else if (act == &clearAct) {
|
|
|
|
search_pattern->clear();
|
2009-07-12 10:42:38 +04:00
|
|
|
}
|
|
|
|
}
|
2008-06-25 23:04:42 +04:00
|
|
|
}
|
|
|
|
|
2008-07-04 20:49:08 +04:00
|
|
|
void SearchEngine::tab_changed(int t)
|
2008-06-26 15:24:08 +04:00
|
|
|
{//when we switch from a tab that is not empty to another that is empty the download button
|
2009-08-25 06:31:36 +04:00
|
|
|
//doesn't have to be available
|
|
|
|
if(t>-1)
|
|
|
|
{//-1 = no more tab
|
2008-06-26 15:24:08 +04:00
|
|
|
if(all_tab.at(tabWidget->currentIndex())->getCurrentSearchListModel()->rowCount()) {
|
2009-08-25 06:31:36 +04:00
|
|
|
download_button->setEnabled(true);
|
2008-06-26 15:24:08 +04:00
|
|
|
} else {
|
2009-08-25 06:31:36 +04:00
|
|
|
download_button->setEnabled(false);
|
2008-06-26 15:24:08 +04:00
|
|
|
}
|
2009-08-25 06:31:36 +04:00
|
|
|
}
|
2008-06-26 15:24:08 +04:00
|
|
|
}
|
|
|
|
|
2008-06-25 23:04:42 +04:00
|
|
|
void SearchEngine::on_enginesButton_clicked() {
|
2009-08-25 06:37:11 +04:00
|
|
|
engineSelectDlg *dlg = new engineSelectDlg(this, supported_engines);
|
|
|
|
connect(dlg, SIGNAL(enginesChanged()), this, SLOT(fillCatCombobox()));
|
2008-06-25 23:04:42 +04:00
|
|
|
}
|
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");
|
2009-11-14 23:24:39 +03:00
|
|
|
searchHistory.setStringList(settings.value("Search/searchHistory",QStringList()).toStringList());
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the history list into the QSettings for the next session
|
2009-11-14 23:24:39 +03:00
|
|
|
void SearchEngine::saveSearchHistory() {
|
2007-03-27 22:49:29 +04:00
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
2009-11-14 23:24:39 +03:00
|
|
|
settings.setValue("Search/searchHistory",searchHistory.stringList());
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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){
|
2009-08-25 06:31:36 +04:00
|
|
|
searchProcess->terminate();
|
|
|
|
search_stopped = true;
|
|
|
|
if(searchTimeout->isActive()) {
|
|
|
|
searchTimeout->stop();
|
|
|
|
}
|
2009-08-24 14:18:11 +04:00
|
|
|
search_button->setText("Search");
|
|
|
|
return;
|
2007-07-22 17:42:09 +04:00
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
QString pattern = search_pattern->text().trimmed();
|
|
|
|
// No search pattern entered
|
|
|
|
if(pattern.isEmpty()){
|
|
|
|
QMessageBox::critical(0, tr("Empty search pattern"), tr("Please type a search pattern first"));
|
|
|
|
return;
|
|
|
|
}
|
2008-06-25 23:52:39 +04:00
|
|
|
// Tab Addition
|
2008-06-26 00:46:19 +04:00
|
|
|
currentSearchTab=new SearchTab(this);
|
2009-03-10 00:24:40 +03:00
|
|
|
connect(currentSearchTab->header(), SIGNAL(sectionResized(int, int, int)), this, SLOT(propagateSectionResized(int,int,int)));
|
2008-06-26 00:46:19 +04:00
|
|
|
all_tab.append(currentSearchTab);
|
2008-06-26 15:24:08 +04:00
|
|
|
tabWidget->addTab(currentSearchTab, pattern);
|
2008-06-26 00:48:58 +04:00
|
|
|
tabWidget->setCurrentWidget(currentSearchTab);
|
2008-06-25 23:52:39 +04:00
|
|
|
closeTab_button->setEnabled(true);
|
2007-03-27 22:49:29 +04:00
|
|
|
// if the pattern is not in the pattern
|
2009-11-14 23:24:39 +03:00
|
|
|
QStringList wordList = searchHistory.stringList();
|
|
|
|
if(wordList.indexOf(pattern) == -1){
|
2007-03-27 22:49:29 +04:00
|
|
|
//update the searchHistory list
|
2009-11-14 23:24:39 +03:00
|
|
|
wordList.append(pattern);
|
2007-03-27 22:49:29 +04:00
|
|
|
// verify the max size of the history
|
2009-11-14 23:24:39 +03:00
|
|
|
if(wordList.size() > SEARCHHISTORY_MAXSIZE)
|
|
|
|
wordList = wordList.mid(wordList.size()/2);
|
|
|
|
searchHistory.setStringList(wordList);
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getting checked search engines
|
|
|
|
QStringList params;
|
|
|
|
QStringList engineNames;
|
|
|
|
search_stopped = false;
|
2007-12-12 20:14:15 +03:00
|
|
|
params << misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2.py";
|
2009-08-25 06:31:36 +04:00
|
|
|
params << supported_engines->enginesEnabled().join(",");
|
|
|
|
qDebug("Search with category: %s", selectedCategory().toLocal8Bit().data());
|
|
|
|
params << selectedCategory();
|
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
|
2008-06-26 00:46:19 +04:00
|
|
|
currentSearchTab->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
|
|
|
}
|
|
|
|
|
2009-07-12 10:42:38 +04:00
|
|
|
void SearchEngine::createCompleter() {
|
|
|
|
if(searchCompleter)
|
|
|
|
delete searchCompleter;
|
2009-11-14 23:24:39 +03:00
|
|
|
searchCompleter = new QCompleter(&searchHistory);
|
2009-07-12 10:42:38 +04:00
|
|
|
searchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
search_pattern->setCompleter(searchCompleter);
|
|
|
|
}
|
|
|
|
|
2009-03-10 00:24:40 +03:00
|
|
|
void SearchEngine::propagateSectionResized(int index, int , int newsize) {
|
|
|
|
foreach(SearchTab * tab, all_tab) {
|
|
|
|
tab->getCurrentTreeView()->setColumnWidth(index, newsize);
|
|
|
|
}
|
|
|
|
saveResultsColumnsWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEngine::saveResultsColumnsWidth() {
|
|
|
|
if(all_tab.size() > 0) {
|
|
|
|
QTreeView* treeview = all_tab.first()->getCurrentTreeView();
|
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QStringList width_list;
|
|
|
|
QStringList new_width_list;
|
|
|
|
short nbColumns = all_tab.first()->getCurrentSearchListModel()->columnCount();
|
|
|
|
|
|
|
|
QString line = settings.value("SearchResultsColsWidth", QString()).toString();
|
|
|
|
if(!line.isEmpty()) {
|
|
|
|
width_list = line.split(' ');
|
|
|
|
}
|
|
|
|
for(short i=0; i<nbColumns; ++i){
|
|
|
|
if(treeview->columnWidth(i)<1 && width_list.size() == nbColumns && width_list.at(i).toInt()>=1) {
|
|
|
|
// load the former width
|
|
|
|
new_width_list << width_list.at(i);
|
|
|
|
} else if(treeview->columnWidth(i)>=1) {
|
|
|
|
// usual case, save the current width
|
|
|
|
new_width_list << QString::fromUtf8(misc::toString(treeview->columnWidth(i)).c_str());
|
|
|
|
} else {
|
|
|
|
// default width
|
|
|
|
treeview->resizeColumnToContents(i);
|
|
|
|
new_width_list << QString::fromUtf8(misc::toString(treeview->columnWidth(i)).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings.setValue("SearchResultsColsWidth", new_width_list.join(" "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 01:11:41 +03:00
|
|
|
void SearchEngine::downloadTorrent(QString engine_url, QString torrent_url) {
|
2009-08-25 06:31:36 +04:00
|
|
|
QProcess *downloadProcess = new QProcess(this);
|
|
|
|
connect(downloadProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(downloadFinished(int,QProcess::ExitStatus)));
|
|
|
|
downloaders << downloadProcess;
|
|
|
|
QStringList params;
|
|
|
|
params << misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2dl.py";
|
|
|
|
params << engine_url;
|
|
|
|
params << torrent_url;
|
|
|
|
// Launch search
|
|
|
|
downloadProcess->start("python", params, QIODevice::ReadOnly);
|
2009-03-28 01:11:41 +03:00
|
|
|
}
|
|
|
|
|
2007-03-27 22:49:29 +04:00
|
|
|
void SearchEngine::searchStarted(){
|
|
|
|
// Update SearchEngine widgets
|
|
|
|
search_status->setText(tr("Searching..."));
|
|
|
|
search_status->repaint();
|
2009-08-24 14:18:11 +04:00
|
|
|
search_button->setText("Stop");
|
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');
|
|
|
|
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();
|
2009-01-24 23:04:19 +03:00
|
|
|
foreach(const QByteArray &line, lines_list){
|
2009-03-26 18:46:35 +03:00
|
|
|
appendSearchResult(QString::fromUtf8(line));
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2008-06-26 00:46:19 +04:00
|
|
|
currentSearchTab->getCurrentLabel()->setText(tr("Results")+QString::fromUtf8(" <i>(")+misc::toQString(nb_search_results)+QString::fromUtf8(")</i>:"));
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
2009-03-28 01:11:41 +03:00
|
|
|
void SearchEngine::downloadFinished(int exitcode, QProcess::ExitStatus) {
|
2009-08-25 06:31:36 +04:00
|
|
|
QProcess *downloadProcess = (QProcess*)sender();
|
|
|
|
if(exitcode == 0) {
|
|
|
|
QString line = QString::fromUtf8(downloadProcess->readAllStandardOutput()).trimmed();
|
|
|
|
QStringList parts = line.split(' ');
|
|
|
|
if(parts.size() == 2) {
|
|
|
|
QString path = parts[0];
|
|
|
|
QString url = parts[1];
|
|
|
|
BTSession->processDownloadedFile(url, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qDebug("Deleting downloadProcess");
|
2009-11-18 13:29:20 +03:00
|
|
|
downloaders.removeOne(downloadProcess);
|
2009-08-25 06:31:36 +04:00
|
|
|
delete downloadProcess;
|
2009-03-28 01:11:41 +03: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);
|
2009-03-31 00:31:30 +04:00
|
|
|
|
2009-08-25 06:31:36 +04:00
|
|
|
filePath = misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2dl.py";
|
|
|
|
if(misc::getPluginVersion(":/search_engine/nova2dl.py") > misc::getPluginVersion(filePath)) {
|
2009-03-28 01:11:41 +03:00
|
|
|
if(QFile::exists(filePath)){
|
|
|
|
QFile::remove(filePath);
|
|
|
|
}
|
|
|
|
QFile::copy(":/search_engine/nova2dl.py", filePath);
|
|
|
|
}
|
|
|
|
QFile(misc::qBittorrentPath()+"search_engine"+QDir::separator()+"nova2dl.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
|
|
|
}
|
2009-03-28 01:11:41 +03:00
|
|
|
QFile(misc::qBittorrentPath()+"search_engine"+QDir::separator()+"novaprinter.py").setPermissions(perm);
|
2009-03-26 19:49:44 +03:00
|
|
|
filePath = misc::qBittorrentPath()+"search_engine"+QDir::separator()+"helpers.py";
|
|
|
|
if(misc::getPluginVersion(":/search_engine/helpers.py") > misc::getPluginVersion(filePath)) {
|
|
|
|
if(QFile::exists(filePath)){
|
|
|
|
QFile::remove(filePath);
|
|
|
|
}
|
|
|
|
QFile::copy(":/search_engine/helpers.py", filePath);
|
|
|
|
}
|
2009-08-25 06:31:36 +04:00
|
|
|
QFile(misc::qBittorrentPath()+"search_engine"+QDir::separator()+"helpers.py").setPermissions(perm);
|
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();
|
2009-01-24 23:04:19 +03:00
|
|
|
foreach(const QString &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) ) {
|
2009-07-12 12:13:00 +04:00
|
|
|
qDebug("shippped %s is more recent then local plugin, updating", file.toLocal8Bit().data());
|
2007-08-31 20:02:01 +04:00
|
|
|
if(QFile::exists(destDir+file)) {
|
2009-07-12 12:13:00 +04:00
|
|
|
qDebug("Removing old %s", (destDir+file).toLocal8Bit().data());
|
2007-08-31 20:02:01 +04:00
|
|
|
QFile::remove(destDir+file);
|
|
|
|
}
|
2009-07-12 12:13:00 +04:00
|
|
|
qDebug("%s copied to %s", shipped_file.toLocal8Bit().data(), (destDir+file).toLocal8Bit().data());
|
2007-08-31 20:02:01 +04:00
|
|
|
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-26 00:46:19 +04:00
|
|
|
if(currentSearchTab)
|
|
|
|
currentSearchTab->getCurrentLabel()->setText(tr("Results", "i.e: Search results")+QString::fromUtf8(" <i>(")+misc::toQString(nb_search_results)+QString::fromUtf8(")</i>:"));
|
2009-08-24 14:18:11 +04:00
|
|
|
search_button->setText("Search");
|
2009-08-25 06:31:36 +04:00
|
|
|
if(searchTimeout->isActive()) {
|
|
|
|
searchTimeout->stop();
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-08-27 16:37:39 +04:00
|
|
|
|
2007-03-27 22:49:29 +04:00
|
|
|
// Add item to search result list
|
2009-10-25 02:49:10 +04:00
|
|
|
QStandardItemModel* cur_model = currentSearchTab->getCurrentSearchListModel();
|
2009-08-27 16:37:39 +04:00
|
|
|
int row = cur_model->rowCount();
|
|
|
|
cur_model->insertRow(row);
|
|
|
|
|
|
|
|
cur_model->setData(cur_model->index(row, 5), parts.at(0).trimmed()); // download URL
|
|
|
|
cur_model->setData(cur_model->index(row, 0), parts.at(1).trimmed()); // Name
|
|
|
|
cur_model->setData(cur_model->index(row, 1), parts.at(2).trimmed().toLongLong()); // Size
|
|
|
|
bool ok = false;
|
|
|
|
qlonglong nb_seeders = parts.at(3).trimmed().toLongLong(&ok);
|
|
|
|
if(!ok || nb_seeders < 0) {
|
|
|
|
cur_model->setData(cur_model->index(row, 2), tr("Unknown")); // Seeders
|
|
|
|
} else {
|
|
|
|
cur_model->setData(cur_model->index(row, 2), nb_seeders); // Seeders
|
|
|
|
}
|
|
|
|
qlonglong nb_leechers = parts.at(4).trimmed().toLongLong(&ok);
|
|
|
|
if(!ok || nb_leechers < 0) {
|
|
|
|
cur_model->setData(cur_model->index(row, 3), tr("Unknown")); // Leechers
|
|
|
|
} else {
|
|
|
|
cur_model->setData(cur_model->index(row, 3), nb_leechers); // Leechers
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
2009-08-27 16:37:39 +04:00
|
|
|
cur_model->setData(cur_model->index(row, 4), parts.at(5).trimmed()); // Engine URL
|
|
|
|
|
2007-03-27 22:49:29 +04:00
|
|
|
no_search_results = false;
|
|
|
|
++nb_search_results;
|
|
|
|
// Enable clear & download buttons
|
|
|
|
download_button->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear search results list
|
2008-07-06 22:39:07 +04:00
|
|
|
void SearchEngine::closeTab_button_clicked(){
|
2008-06-26 00:46:19 +04:00
|
|
|
if(all_tab.size()) {
|
|
|
|
qDebug("currentTab rank: %d", tabWidget->currentIndex());
|
|
|
|
qDebug("currentSearchTab rank: %d", tabWidget->indexOf(currentSearchTab));
|
|
|
|
if(tabWidget->currentIndex() == tabWidget->indexOf(currentSearchTab)) {
|
2009-08-25 06:31:36 +04:00
|
|
|
qDebug("Deleted current search Tab");
|
|
|
|
if(searchProcess->state() != QProcess::NotRunning){
|
|
|
|
searchProcess->terminate();
|
|
|
|
}
|
|
|
|
if(searchTimeout->isActive()) {
|
|
|
|
searchTimeout->stop();
|
|
|
|
}
|
|
|
|
search_stopped = true;
|
|
|
|
currentSearchTab = 0;
|
2008-06-26 00:46:19 +04:00
|
|
|
}
|
|
|
|
delete all_tab.takeAt(tabWidget->currentIndex());
|
|
|
|
if(!all_tab.size()) {
|
2009-08-25 06:31:36 +04:00
|
|
|
closeTab_button->setEnabled(false);
|
|
|
|
download_button->setEnabled(false);
|
2008-06-26 00:46:19 +04:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download selected items in search results list
|
|
|
|
void SearchEngine::on_download_button_clicked(){
|
2008-06-26 00:46:19 +04:00
|
|
|
//QModelIndexList selectedIndexes = currentSearchTab->getCurrentTreeView()->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes();
|
2009-01-24 23:04:19 +03:00
|
|
|
foreach(const QModelIndex &index, selectedIndexes){
|
2007-03-27 22:49:29 +04:00
|
|
|
if(index.column() == NAME){
|
|
|
|
// Get Item url
|
2009-10-25 02:49:10 +04:00
|
|
|
QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy();
|
2009-03-28 01:11:41 +03:00
|
|
|
QString torrent_url = model->data(model->index(index.row(), URL_COLUMN)).toString();
|
2009-08-25 06:31:36 +04:00
|
|
|
QString engine_url = model->data(model->index(index.row(), ENGINE_URL_COLUMN)).toString();
|
2009-03-28 01:11:41 +03:00
|
|
|
downloadTorrent(engine_url, torrent_url);
|
2008-06-26 00:46:19 +04:00
|
|
|
all_tab.at(tabWidget->currentIndex())->setRowColor(index.row(), "red");
|
2007-03-27 22:49:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|