2006-10-24 01:42:37 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENTADDITION_H
|
|
|
|
#define TORRENTADDITION_H
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFile>
|
|
|
|
#include <fstream>
|
|
|
|
#include <QMessageBox>
|
2006-10-24 02:18:06 +04:00
|
|
|
#include <QMenu>
|
2006-11-15 00:17:51 +03:00
|
|
|
#include <QSettings>
|
2006-10-24 01:42:37 +04:00
|
|
|
|
|
|
|
#include <libtorrent/session.hpp>
|
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include "misc.h"
|
|
|
|
#include "ui_addTorrentDialog.h"
|
|
|
|
|
|
|
|
#define NAME 0
|
|
|
|
#define SIZE 1
|
|
|
|
#define SELECTED 2
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2006-10-29 12:32:28 +03:00
|
|
|
signals:
|
|
|
|
void setInfoBarGUI(const QString& info, const QString& color);
|
|
|
|
void torrentAddition(const QString& filePath, bool fromScanDir, const QString& from_url);
|
|
|
|
|
2006-10-24 01:42:37 +04:00
|
|
|
private:
|
|
|
|
QString fileName;
|
2007-02-24 01:52:24 +03:00
|
|
|
QString fileHash;
|
2006-10-24 01:42:37 +04:00
|
|
|
QString filePath;
|
|
|
|
QList<bool> selection;
|
|
|
|
bool fromScanDir;
|
|
|
|
QString from_url;
|
|
|
|
|
|
|
|
public:
|
2006-10-29 12:32:28 +03:00
|
|
|
torrentAdditionDialog(QWidget *parent) : QDialog(parent) {
|
2006-10-24 01:42:37 +04:00
|
|
|
setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2006-10-24 02:18:06 +04:00
|
|
|
actionSelect->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png")));
|
|
|
|
actionUnselect->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png")));
|
|
|
|
connect(actionSelect, SIGNAL(triggered()), this, SLOT(selectItems()));
|
|
|
|
connect(actionUnselect, SIGNAL(triggered()), this, SLOT(unselectItems()));
|
2006-10-24 01:42:37 +04:00
|
|
|
QString home = QDir::homePath();
|
|
|
|
if(home[home.length()-1] != QDir::separator()){
|
|
|
|
home += QDir::separator();
|
|
|
|
}
|
2006-11-15 00:17:51 +03:00
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
savePathTxt->setText(settings.value("LastDirTorrentAdd", home+"qBT_dir").toString());
|
2006-10-29 12:32:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void showLoad(QString filePath, bool fromScanDir=false, QString from_url=QString::null){
|
|
|
|
this->filePath = filePath;
|
|
|
|
this->fromScanDir = fromScanDir;
|
|
|
|
this->from_url = from_url;
|
2006-10-26 00:35:41 +04:00
|
|
|
std::ifstream in((const char*)filePath.toUtf8(), std::ios_base::binary);
|
2006-10-24 01:42:37 +04:00
|
|
|
in.unsetf(std::ios_base::skipws);
|
|
|
|
try{
|
|
|
|
// Decode torrent file
|
|
|
|
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
|
|
|
// Getting torrent file informations
|
|
|
|
torrent_info t(e);
|
|
|
|
// Setting file name
|
|
|
|
fileName = QString(t.name().c_str());
|
2007-02-24 01:52:24 +03:00
|
|
|
fileHash = QString(misc::toString(t.info_hash()).c_str());
|
2006-10-24 01:42:37 +04:00
|
|
|
fileNameLbl->setText("<center><b>"+fileName+"</b></center>");
|
|
|
|
// List files in torrent
|
|
|
|
for(int i=0; i<t.num_files(); ++i){
|
|
|
|
QStringList line;
|
|
|
|
line << QString(t.file_at(i).path.leaf().c_str());
|
|
|
|
line << misc::friendlyUnit((qlonglong)t.file_at(i).size);
|
|
|
|
selection << true;
|
|
|
|
line << tr("True");
|
|
|
|
torrentContentList->addTopLevelItem(new QTreeWidgetItem(line));
|
|
|
|
setLineColor(torrentContentList->topLevelItemCount()-1, "green");
|
|
|
|
}
|
|
|
|
}catch (invalid_torrent_file&){ // Raised by torrent_info constructor
|
|
|
|
// Display warning to tell user we can't decode the torrent file
|
|
|
|
if(!from_url.isNull()){
|
2006-10-26 02:17:26 +04:00
|
|
|
emit setInfoBarGUI(tr("Unable to decode torrent file:")+" '"+from_url+"'", "red");
|
2006-10-24 01:42:37 +04:00
|
|
|
}else{
|
2006-10-26 02:17:26 +04:00
|
|
|
emit setInfoBarGUI(tr("Unable to decode torrent file:")+" '"+filePath+"'", "red");
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
2006-10-29 12:32:28 +03:00
|
|
|
emit setInfoBarGUI(tr("This file is either corrupted or this isn't a torrent."), "red");
|
2006-10-24 01:42:37 +04:00
|
|
|
if(fromScanDir){
|
2006-10-26 02:17:26 +04:00
|
|
|
// Remove .corrupt file in case it already exists
|
|
|
|
QFile::remove(filePath+".corrupt");
|
|
|
|
//Rename file extension so that it won't display error message more than once
|
|
|
|
QFile::rename(filePath,filePath+".corrupt");
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
catch(invalid_encoding& e){
|
|
|
|
std::cerr << "Could not decode file, reason: " << e.what() << '\n';
|
|
|
|
// Display warning to tell user we can't decode the torrent file
|
|
|
|
if(!from_url.isNull()){
|
|
|
|
emit setInfoBarGUI(tr("Unable to decode torrent file:")+" '"+from_url+"'", "red");
|
|
|
|
}else{
|
|
|
|
emit setInfoBarGUI(tr("Unable to decode torrent file:")+" '"+filePath+"'", "red");
|
|
|
|
}
|
2006-10-29 12:32:28 +03:00
|
|
|
emit setInfoBarGUI(tr("This file is either corrupted or this isn't a torrent."), "red");
|
2006-10-26 02:17:26 +04:00
|
|
|
if(fromScanDir){
|
|
|
|
// Remove .corrupt file in case it already exists
|
|
|
|
QFile::remove(filePath+".corrupt");
|
2006-10-24 01:42:37 +04:00
|
|
|
//Rename file extension so that it won't display error message more than once
|
|
|
|
QFile::rename(filePath,filePath+".corrupt");
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
//Connects
|
|
|
|
connect(torrentContentList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(toggleSelection(QTreeWidgetItem*, int)));
|
2006-10-24 02:18:06 +04:00
|
|
|
connect(torrentContentList, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displaySelectionMenu(const QPoint&)));
|
2006-10-24 01:42:37 +04:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void on_browseButton_clicked(){
|
2006-10-29 12:46:38 +03:00
|
|
|
QString dir;
|
|
|
|
QDir saveDir(savePathTxt->text());
|
|
|
|
if(saveDir.exists()){
|
|
|
|
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), savePathTxt->text());
|
|
|
|
}else{
|
|
|
|
dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), QDir::homePath());
|
|
|
|
}
|
2006-10-24 01:42:37 +04:00
|
|
|
if(!dir.isNull()){
|
|
|
|
savePathTxt->setText(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_CancelButton_clicked(){
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2006-10-24 02:18:06 +04:00
|
|
|
void selectItems(){
|
|
|
|
QList<QTreeWidgetItem *> selectedItems = torrentContentList->selectedItems();
|
|
|
|
QTreeWidgetItem *item;
|
|
|
|
foreach(item, selectedItems){
|
|
|
|
int row = torrentContentList->indexOfTopLevelItem(item);
|
|
|
|
setLineColor(row, "green");
|
|
|
|
item->setText(SELECTED, tr("True"));
|
|
|
|
selection.replace(row, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unselectItems(){
|
|
|
|
QList<QTreeWidgetItem *> selectedItems = torrentContentList->selectedItems();
|
|
|
|
QTreeWidgetItem *item;
|
|
|
|
foreach(item, selectedItems){
|
|
|
|
int row = torrentContentList->indexOfTopLevelItem(item);
|
|
|
|
setLineColor(row, "red");
|
|
|
|
item->setText(SELECTED, tr("False"));
|
|
|
|
selection.replace(row, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-24 01:42:37 +04:00
|
|
|
void toggleSelection(QTreeWidgetItem *item, int){
|
|
|
|
int row = torrentContentList->indexOfTopLevelItem(item);
|
|
|
|
if(row == -1){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(selection.at(row)){
|
|
|
|
setLineColor(row, "red");
|
|
|
|
item->setText(SELECTED, tr("False"));
|
|
|
|
selection.replace(row, false);
|
|
|
|
}else{
|
|
|
|
setLineColor(row, "green");
|
|
|
|
item->setText(SELECTED, tr("True"));
|
|
|
|
selection.replace(row, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-24 02:18:06 +04:00
|
|
|
void displaySelectionMenu(const QPoint& pos){
|
|
|
|
QMenu mySelectionMenu(this);
|
|
|
|
mySelectionMenu.addAction(actionSelect);
|
|
|
|
mySelectionMenu.addAction(actionUnselect);
|
|
|
|
mySelectionMenu.exec(mapToGlobal(pos)+QPoint(10, 150));
|
|
|
|
}
|
|
|
|
|
2006-10-24 01:42:37 +04:00
|
|
|
void saveFilteredFiles(){
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile pieces_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".pieces");
|
2006-10-24 01:42:37 +04:00
|
|
|
// First, remove old file
|
|
|
|
pieces_file.remove();
|
|
|
|
// Write new files
|
|
|
|
if(!pieces_file.open(QIODevice::WriteOnly | QIODevice::Text)){
|
|
|
|
std::cerr << "Error: Could not save filtered pieces\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(int i=0; i<torrentContentList->topLevelItemCount(); ++i){
|
|
|
|
if(selection.at(i)){
|
|
|
|
pieces_file.write(QByteArray("0\n"));
|
|
|
|
}else{
|
|
|
|
pieces_file.write(QByteArray("1\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pieces_file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLineColor(int row, QString color){
|
|
|
|
QTreeWidgetItem *item = torrentContentList->topLevelItem(row);
|
|
|
|
for(int i=0; i<item->columnCount(); ++i){
|
|
|
|
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_OkButton_clicked(){
|
|
|
|
QDir savePath(savePathTxt->text());
|
|
|
|
if(savePathTxt->text().trimmed().isEmpty()){
|
|
|
|
QMessageBox::critical(0, tr("Empty save path"), tr("Please enter a save path"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if savePath exists
|
|
|
|
if(!savePath.exists()){
|
|
|
|
if(!savePath.mkpath(savePath.path())){
|
|
|
|
QMessageBox::critical(0, tr("Save path creation error"), tr("Could not create the save path"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save savepath
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile savepath_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".savepath");
|
2006-10-24 01:42:37 +04:00
|
|
|
savepath_file.open(QIODevice::WriteOnly | QIODevice::Text);
|
2006-10-26 00:35:41 +04:00
|
|
|
savepath_file.write(savePath.path().toUtf8());
|
2006-10-24 01:42:37 +04:00
|
|
|
savepath_file.close();
|
2006-10-29 12:46:38 +03:00
|
|
|
// Save last dir to remember it
|
2006-11-15 00:17:51 +03:00
|
|
|
QSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
settings.setValue("LastDirTorrentAdd", savePathTxt->text());
|
2006-10-24 01:42:37 +04:00
|
|
|
// Create .incremental file if necessary
|
|
|
|
if(checkIncrementalDL->isChecked()){
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile incremental_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".incremental");
|
2006-10-24 01:42:37 +04:00
|
|
|
incremental_file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
incremental_file.close();
|
|
|
|
}else{
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".incremental");
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
|
|
|
// Create .paused file if necessary
|
|
|
|
if(addInPause->isChecked()){
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile paused_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".paused");
|
2006-10-24 01:42:37 +04:00
|
|
|
paused_file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
paused_file.close();
|
|
|
|
}else{
|
2007-02-24 01:52:24 +03:00
|
|
|
QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".paused");
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
|
|
|
// Check if there is at least one selected file
|
|
|
|
bool selected_file = false;
|
|
|
|
for(int i=0; i<torrentContentList->topLevelItemCount(); ++i){
|
|
|
|
if(selection.at(i)){
|
|
|
|
selected_file = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!selected_file){
|
|
|
|
QMessageBox::critical(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// save filtered files
|
|
|
|
saveFilteredFiles();
|
|
|
|
// Add to download list
|
|
|
|
emit torrentAddition(filePath, fromScanDir, from_url);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|