2007-09-16 13:19:22 +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 ARBORESCENCE_H
|
|
|
|
#define ARBORESCENCE_H
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QDir>
|
|
|
|
#include "misc.h"
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
class torrent_file {
|
2007-09-16 13:19:22 +04:00
|
|
|
private:
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *parent;
|
2007-09-16 13:19:22 +04:00
|
|
|
bool is_dir;
|
|
|
|
QString rel_path;
|
2008-02-03 13:43:12 +03:00
|
|
|
QList<torrent_file*> children;
|
2007-09-16 13:19:22 +04:00
|
|
|
size_type size;
|
|
|
|
float progress;
|
|
|
|
int priority;
|
|
|
|
int index; // Index in torrent_info
|
|
|
|
|
|
|
|
public:
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file(torrent_file *parent, QString path, bool dir, size_type size=0, int index=-1, float progress=0., int priority=1): parent(parent), is_dir(dir), size(size), progress(progress), priority(priority), index(index){
|
2007-09-16 13:41:05 +04:00
|
|
|
qDebug("created a file with index %d", index);
|
2007-09-16 13:19:22 +04:00
|
|
|
rel_path = QDir::cleanPath(path);
|
|
|
|
if(parent) {
|
|
|
|
parent->updateProgress();
|
|
|
|
parent->updatePriority(priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
~torrent_file() {
|
2007-09-16 13:19:22 +04:00
|
|
|
qDeleteAll(children);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path() const {
|
|
|
|
return rel_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString name() const {
|
|
|
|
return rel_path.split(QDir::separator()).last();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateProgress() {
|
|
|
|
Q_ASSERT(is_dir);
|
2007-10-19 19:51:13 +04:00
|
|
|
if(children.isEmpty()) {
|
|
|
|
progress = 0.;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float wanted = 0.;
|
|
|
|
float done = 0.;
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *child;
|
2007-09-16 13:19:22 +04:00
|
|
|
foreach(child, children) {
|
2007-10-19 19:51:13 +04:00
|
|
|
wanted += child->getSize();
|
|
|
|
done += child->getSize()*child->getProgress();
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
2007-10-19 19:51:13 +04:00
|
|
|
progress = done / wanted;
|
|
|
|
Q_ASSERT(progress >= 0.);
|
|
|
|
Q_ASSERT(progress <= 1.);
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void updatePriority(int prio) {
|
|
|
|
Q_ASSERT(is_dir);
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *child;
|
2007-09-16 13:19:22 +04:00
|
|
|
foreach(child, children) {
|
|
|
|
if(child->getPriority() != prio) return;
|
|
|
|
}
|
|
|
|
priority = prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPriority() const {
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type getSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getProgress() const {
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getIndex() const {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDir() const {
|
|
|
|
return is_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasChildren() const {
|
|
|
|
return (!children.isEmpty());
|
|
|
|
}
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
QList<torrent_file*> getChildren() const {
|
2007-09-16 13:19:22 +04:00
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file* getChild(QString fileName) const {
|
2007-09-16 13:19:22 +04:00
|
|
|
Q_ASSERT(is_dir);
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file* f;
|
2007-09-16 13:19:22 +04:00
|
|
|
foreach(f, children) {
|
|
|
|
if(f->name() == fileName) return f;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addBytes(size_type b) {
|
|
|
|
size += b;
|
|
|
|
if(parent)
|
|
|
|
parent->addBytes(b);
|
|
|
|
}
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file* addChild(QString fileName, bool dir, size_type size=0, int index = -1, float progress=0., int priority=1) {
|
2007-09-16 13:19:22 +04:00
|
|
|
Q_ASSERT(is_dir);
|
|
|
|
qDebug("Adding a new child of size: %ld", (long)size);
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *f = new torrent_file(this, QDir::cleanPath(rel_path+QDir::separator()+fileName), dir, size, index, progress, priority);
|
2007-09-16 13:19:22 +04:00
|
|
|
children << f;
|
|
|
|
if(size) {
|
|
|
|
addBytes(size);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeFromFS(QString saveDir) {
|
|
|
|
QString full_path = saveDir + QDir::separator() + rel_path;
|
|
|
|
if(!QFile::exists(full_path)) {
|
|
|
|
qDebug("%s does not exist, no need to remove it", full_path.toUtf8().data());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool success = true;
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *f;
|
2007-09-16 19:10:25 +04:00
|
|
|
qDebug("We have %d children", children.size());
|
2007-09-16 13:19:22 +04:00
|
|
|
foreach(f, children) {
|
2007-09-16 19:13:32 +04:00
|
|
|
bool s = f->removeFromFS(saveDir);
|
|
|
|
success = s && success;
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
|
|
|
if(is_dir) {
|
|
|
|
qDebug("trying to remove directory: %s", full_path.toUtf8().data());
|
|
|
|
QDir dir(full_path);
|
|
|
|
dir.rmdir(full_path);
|
|
|
|
} else {
|
|
|
|
qDebug("trying to remove file: %s", full_path.toUtf8().data());
|
2007-09-16 19:13:32 +04:00
|
|
|
bool s = QFile::remove(full_path);
|
|
|
|
success = s && success;
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class arborescence {
|
|
|
|
private:
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *root;
|
2007-09-16 13:19:22 +04:00
|
|
|
|
|
|
|
public:
|
2008-11-02 00:42:56 +03:00
|
|
|
arborescence(boost::intrusive_ptr<torrent_info> t) {
|
|
|
|
torrent_info::file_iterator fi = t->begin_files();
|
|
|
|
if(t->num_files() > 1) {
|
|
|
|
root = new torrent_file(0, misc::toQString(t->name()), true);
|
2007-09-16 13:19:22 +04:00
|
|
|
} else {
|
|
|
|
// XXX: Will crash if there is no file in torrent
|
2008-11-02 00:42:56 +03:00
|
|
|
root = new torrent_file(0, misc::toQString(t->name()), false, fi->size, 0);
|
2007-09-16 13:19:22 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i = 0;
|
2008-11-02 00:42:56 +03:00
|
|
|
while(fi != t->end_files()) {
|
2007-09-16 13:19:22 +04:00
|
|
|
QString path = QDir::cleanPath(misc::toQString(fi->path.string()));
|
|
|
|
addFile(path, fi->size, i);
|
|
|
|
fi++;
|
|
|
|
++i;
|
|
|
|
}
|
2008-11-02 00:42:56 +03:00
|
|
|
qDebug("real size: %ld, tree size: %ld", (long)t->total_size(), (long)root->getSize());
|
|
|
|
Q_ASSERT(root->getSize() == t->total_size());
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
arborescence(torrent_info const& t, std::vector<size_type> fp, int *prioritiesTab) {
|
2007-09-16 13:19:22 +04:00
|
|
|
torrent_info::file_iterator fi = t.begin_files();
|
|
|
|
if(t.num_files() > 1) {
|
2007-09-16 13:41:05 +04:00
|
|
|
qDebug("More than one file in the torrent, setting a folder as root");
|
2008-02-03 13:43:12 +03:00
|
|
|
root = new torrent_file(0, misc::toQString(t.name()), true);
|
2007-09-16 13:19:22 +04:00
|
|
|
} else {
|
|
|
|
// XXX: Will crash if there is no file in torrent
|
2007-09-16 13:41:05 +04:00
|
|
|
qDebug("one file in the torrent, setting it as root with index 0");
|
2008-11-02 00:42:56 +03:00
|
|
|
root = new torrent_file(0, misc::toQString(t.name()), false, fi->size, 0, fp[0]/t.file_at(0).size, prioritiesTab[0]);
|
2007-09-16 13:19:22 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while(fi != t.end_files()) {
|
|
|
|
QString path = QDir::cleanPath(misc::toQString(fi->path.string()));
|
2008-11-02 00:42:56 +03:00
|
|
|
addFile(path, fi->size, i, fp[i]/t.file_at(i).size, prioritiesTab[i]);
|
2007-09-16 13:19:22 +04:00
|
|
|
fi++;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
qDebug("real size: %ld, tree size: %ld", (long)t.total_size(), (long)root->getSize());
|
|
|
|
Q_ASSERT(root->getSize() == t.total_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
~arborescence() {
|
|
|
|
delete root;
|
|
|
|
}
|
|
|
|
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file* getRoot() const {
|
2007-09-16 13:19:22 +04:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeFromFS(QString saveDir) {
|
|
|
|
if(!QFile::exists(saveDir+QDir::separator()+root->path())) return true;
|
|
|
|
bool success = root->removeFromFS(saveDir);
|
|
|
|
QDir root_dir(root->path());
|
|
|
|
root_dir.rmdir(saveDir+QDir::separator()+root->path());
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void addFile(QString path, size_type file_size, int index, float progress=0., int priority=1) {
|
|
|
|
Q_ASSERT(root->isDir());
|
|
|
|
path = QDir::cleanPath(path);
|
2008-07-22 18:40:01 +04:00
|
|
|
//Q_ASSERT(path.startsWith(root->path()));
|
2007-09-16 13:19:22 +04:00
|
|
|
QString relative_path = path.remove(0, root->path().size());
|
|
|
|
if(relative_path.at(0) ==QDir::separator())
|
|
|
|
relative_path.remove(0, 1);
|
|
|
|
QStringList fileNames = relative_path.split(QDir::separator());
|
|
|
|
QString fileName;
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file *dad = root;
|
2007-09-16 13:19:22 +04:00
|
|
|
unsigned int nb_i = 0;
|
|
|
|
unsigned int size = fileNames.size();
|
|
|
|
foreach(fileName, fileNames) {
|
|
|
|
++nb_i;
|
|
|
|
if(fileName == ".") continue;
|
2008-02-03 13:43:12 +03:00
|
|
|
torrent_file* child = dad->getChild(fileName);
|
2007-09-16 13:19:22 +04:00
|
|
|
if(!child) {
|
|
|
|
if(nb_i != size) {
|
|
|
|
// Folder
|
|
|
|
child = dad->addChild(fileName, true);
|
|
|
|
} else {
|
|
|
|
// File
|
2007-09-16 13:41:05 +04:00
|
|
|
child = dad->addChild(fileName, false, file_size, index, progress, priority);
|
2007-09-16 13:19:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dad = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|