2006-10-24 01:42:37 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2006-10-24 01:42:37 +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.
|
2006-10-24 01:42:37 +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
|
2006-10-24 01:42:37 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2007-04-13 00:22:42 +04:00
|
|
|
#include <QStandardItemModel>
|
2007-04-13 00:28:10 +04:00
|
|
|
#include <QHeaderView>
|
2006-10-24 01:42:37 +04:00
|
|
|
|
|
|
|
#include <libtorrent/session.hpp>
|
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include "misc.h"
|
2007-04-13 00:22:42 +04:00
|
|
|
#include "PropListDelegate.h"
|
2006-10-24 01:42:37 +04:00
|
|
|
#include "ui_addTorrentDialog.h"
|
|
|
|
|
|
|
|
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);
|
2007-06-29 17:42:10 +04:00
|
|
|
void torrentAddition(const QString& filePath, bool fromScanDir, bool onStartup, const QString& from_url);
|
2006-10-29 12:32:28 +03:00
|
|
|
|
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;
|
|
|
|
bool fromScanDir;
|
|
|
|
QString from_url;
|
2007-04-13 00:22:42 +04:00
|
|
|
QStandardItemModel *PropListModel;
|
|
|
|
PropListDelegate *PropDelegate;
|
2006-10-24 01:42:37 +04:00
|
|
|
|
|
|
|
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);
|
2007-04-13 00:22:42 +04:00
|
|
|
// Set Properties list model
|
|
|
|
PropListModel = new QStandardItemModel(0,4);
|
|
|
|
PropListModel->setHeaderData(NAME, Qt::Horizontal, tr("File name"));
|
|
|
|
PropListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
|
|
|
|
PropListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
|
|
|
|
PropListModel->setHeaderData(PRIORITY, Qt::Horizontal, tr("Priority"));
|
|
|
|
torrentContentList->setModel(PropListModel);
|
|
|
|
torrentContentList->hideColumn(PROGRESS);
|
|
|
|
PropDelegate = new PropListDelegate();
|
|
|
|
torrentContentList->setItemDelegate(PropDelegate);
|
|
|
|
connect(torrentContentList, SIGNAL(clicked(const QModelIndex&)), torrentContentList, SLOT(edit(const QModelIndex&)));
|
2007-07-20 17:21:46 +04:00
|
|
|
connect(torrentContentList, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayFilesListMenu(const QPoint&)));
|
|
|
|
connect(actionIgnored, SIGNAL(triggered()), this, SLOT(ignoreSelection()));
|
|
|
|
connect(actionNormal, SIGNAL(triggered()), this, SLOT(normalSelection()));
|
|
|
|
connect(actionHigh, SIGNAL(triggered()), this, SLOT(highSelection()));
|
|
|
|
connect(actionMaximum, SIGNAL(triggered()), this, SLOT(maximumSelection()));
|
2007-04-13 00:28:10 +04:00
|
|
|
torrentContentList->header()->resizeSection(0, 200);
|
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());
|
2007-03-08 01:36:01 +03:00
|
|
|
// Use left() to remove .old extension
|
|
|
|
QString newFileName;
|
|
|
|
if(fileName.endsWith(".old")){
|
|
|
|
newFileName = fileName.left(fileName.size()-4);
|
|
|
|
}else{
|
|
|
|
newFileName = fileName;
|
|
|
|
}
|
|
|
|
fileNameLbl->setText("<center><b>"+newFileName+"</b></center>");
|
2006-10-24 01:42:37 +04:00
|
|
|
// List files in torrent
|
2007-04-13 00:22:42 +04:00
|
|
|
unsigned int nbFiles = t.num_files();
|
|
|
|
for(unsigned int i=0; i<nbFiles; ++i){
|
|
|
|
unsigned int row = PropListModel->rowCount();
|
|
|
|
PropListModel->insertRow(row);
|
|
|
|
PropListModel->setData(PropListModel->index(row, NAME), QVariant(t.file_at(i).path.leaf().c_str()));
|
|
|
|
PropListModel->setData(PropListModel->index(row, SIZE), QVariant((qlonglong)t.file_at(i).size));
|
|
|
|
PropListModel->setData(PropListModel->index(i, PRIORITY), QVariant(NORMAL));
|
|
|
|
setRowColor(i, "green");
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
|
|
|
}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();
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-04-13 00:22:42 +04:00
|
|
|
// Set the color of a row in data model
|
|
|
|
void setRowColor(int row, QString color){
|
|
|
|
for(int i=0; i<PropListModel->columnCount(); ++i){
|
|
|
|
PropListModel->setData(PropListModel->index(row, i), QVariant(QColor(color)), Qt::TextColorRole);
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:21:46 +04:00
|
|
|
void displayFilesListMenu(const QPoint& pos){
|
|
|
|
QMenu myFilesLlistMenu(this);
|
|
|
|
QModelIndex index;
|
|
|
|
// Enable/disable pause/start action given the DL state
|
|
|
|
QModelIndexList selectedIndexes = torrentContentList->selectionModel()->selectedIndexes();
|
|
|
|
myFilesLlistMenu.setTitle(tr("Priority"));
|
|
|
|
myFilesLlistMenu.addAction(actionIgnored);
|
|
|
|
myFilesLlistMenu.addAction(actionNormal);
|
|
|
|
myFilesLlistMenu.addAction(actionHigh);
|
|
|
|
myFilesLlistMenu.addAction(actionMaximum);
|
|
|
|
// Call menu
|
|
|
|
// XXX: why mapToGlobal() is not enough?
|
|
|
|
myFilesLlistMenu.exec(mapToGlobal(pos)+QPoint(22,95));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ignoreSelection(){
|
|
|
|
QModelIndexList selectedIndexes = torrentContentList->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndex index;
|
|
|
|
foreach(index, selectedIndexes){
|
|
|
|
if(index.column() == PRIORITY){
|
|
|
|
PropListModel->setData(index, QVariant(IGNORED));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void normalSelection(){
|
|
|
|
QModelIndexList selectedIndexes = torrentContentList->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndex index;
|
|
|
|
foreach(index, selectedIndexes){
|
|
|
|
if(index.column() == PRIORITY){
|
|
|
|
PropListModel->setData(index, QVariant(NORMAL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void highSelection(){
|
|
|
|
QModelIndexList selectedIndexes = torrentContentList->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndex index;
|
|
|
|
foreach(index, selectedIndexes){
|
|
|
|
if(index.column() == PRIORITY){
|
|
|
|
PropListModel->setData(index, QVariant(HIGH));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void maximumSelection(){
|
|
|
|
QModelIndexList selectedIndexes = torrentContentList->selectionModel()->selectedIndexes();
|
|
|
|
QModelIndex index;
|
|
|
|
foreach(index, selectedIndexes){
|
|
|
|
if(index.column() == PRIORITY){
|
|
|
|
PropListModel->setData(index, QVariant(MAXIMUM));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-13 00:22:42 +04:00
|
|
|
void savePiecesPriorities(){
|
|
|
|
qDebug("Saving pieces priorities");
|
|
|
|
QFile pieces_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".priorities");
|
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)){
|
2007-04-13 00:22:42 +04:00
|
|
|
std::cerr << "Error: Could not save pieces priorities\n";
|
2006-10-24 01:42:37 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-04-13 00:22:42 +04:00
|
|
|
unsigned int nbRows = PropListModel->rowCount();
|
|
|
|
for(unsigned int i=0; i<nbRows; ++i){
|
|
|
|
QStandardItem *item = PropListModel->item(i, PRIORITY);
|
|
|
|
unsigned short priority = item->text().toInt();
|
|
|
|
pieces_file.write(QByteArray((misc::toString(priority)+"\n").c_str()));
|
2006-10-24 01:42:37 +04:00
|
|
|
}
|
|
|
|
pieces_file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2007-04-13 00:22:42 +04:00
|
|
|
if(!hasSelectedFiles()){
|
2006-10-24 01:42:37 +04:00
|
|
|
QMessageBox::critical(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// save filtered files
|
2007-04-13 00:22:42 +04:00
|
|
|
savePiecesPriorities();
|
2006-10-24 01:42:37 +04:00
|
|
|
// Add to download list
|
2007-06-29 17:42:10 +04:00
|
|
|
// TODO : quick fix
|
|
|
|
emit torrentAddition(filePath, fromScanDir, false, from_url);
|
2006-10-24 01:42:37 +04:00
|
|
|
close();
|
|
|
|
}
|
2007-04-13 00:22:42 +04:00
|
|
|
|
|
|
|
bool hasSelectedFiles(){
|
|
|
|
unsigned int nbRows = PropListModel->rowCount();
|
|
|
|
for(unsigned int i=0; i<nbRows; ++i){
|
|
|
|
QStandardItem *item = PropListModel->item(i, PRIORITY);
|
|
|
|
unsigned short priority = item->text().toInt();
|
|
|
|
if(priority) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-10-24 01:42:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|