2009-11-10 23:47:48 +03: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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENTFILESMODEL_H
|
|
|
|
#define TORRENTFILESMODEL_H
|
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QString>
|
|
|
|
#include <libtorrent/torrent_info.hpp>
|
2009-11-24 16:25:11 +03:00
|
|
|
#include "proplistdelegate.h"
|
|
|
|
#include "misc.h"
|
|
|
|
using namespace libtorrent;
|
2009-11-10 23:47:48 +03:00
|
|
|
|
|
|
|
enum TreeItemType {TFILE, FOLDER, ROOT};
|
|
|
|
|
|
|
|
class TreeItem {
|
|
|
|
private:
|
|
|
|
QList<TreeItem*> childItems;
|
|
|
|
QList<QVariant> itemData;
|
|
|
|
TreeItem *parentItem;
|
|
|
|
TreeItemType type;
|
2009-11-29 23:29:40 +03:00
|
|
|
qulonglong total_done;
|
2009-12-19 17:59:38 +03:00
|
|
|
int file_index;
|
2009-11-10 23:47:48 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
// File Construction
|
2009-12-19 17:59:38 +03:00
|
|
|
TreeItem(file_entry f, TreeItem *parent, int _file_index) {
|
2009-11-20 10:18:40 +03:00
|
|
|
Q_ASSERT(parent);
|
2009-11-10 23:47:48 +03:00
|
|
|
parentItem = parent;
|
|
|
|
type = TFILE;
|
2009-12-19 17:59:38 +03:00
|
|
|
file_index = _file_index;
|
2009-12-18 20:22:53 +03:00
|
|
|
QString name = misc::toQString(f.path.string()).split("/").last();
|
|
|
|
// Do not display incomplete extensions
|
|
|
|
if(name.endsWith(".!qB"))
|
|
|
|
name.chop(4);
|
|
|
|
itemData << name;
|
2009-11-11 14:49:59 +03:00
|
|
|
qDebug("Created a TreeItem file with name %s", getName().toLocal8Bit().data());
|
|
|
|
qDebug("parent is %s", parent->getName().toLocal8Bit().data());
|
2009-11-21 01:06:36 +03:00
|
|
|
itemData << QVariant((qulonglong)f.size);
|
2009-11-29 23:29:40 +03:00
|
|
|
total_done = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
itemData << 0.; // Progress;
|
|
|
|
itemData << 1; // Priority
|
2009-11-20 10:18:40 +03:00
|
|
|
if(parent) {
|
|
|
|
parent->appendChild(this);
|
2009-11-11 14:49:59 +03:00
|
|
|
parent->updateSize();
|
2009-11-20 10:18:40 +03:00
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Folder constructor
|
|
|
|
TreeItem(QString name, TreeItem *parent=0) {
|
|
|
|
parentItem = parent;
|
2009-11-11 14:49:59 +03:00
|
|
|
type = FOLDER;
|
2009-12-18 20:22:53 +03:00
|
|
|
// Do not display incomplete extensions
|
|
|
|
if(name.endsWith(".!qB"))
|
|
|
|
name.chop(4);
|
2009-11-10 23:47:48 +03:00
|
|
|
itemData << name;
|
|
|
|
itemData << 0.; // Size
|
|
|
|
itemData << 0.; // Progress;
|
2009-11-29 23:29:40 +03:00
|
|
|
total_done = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
itemData << 1; // Priority
|
2009-11-20 10:18:40 +03:00
|
|
|
if(parent) {
|
|
|
|
parent->appendChild(this);
|
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem(QList<QVariant> data) {
|
2009-11-11 14:49:59 +03:00
|
|
|
parentItem = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
type = ROOT;
|
|
|
|
itemData = data;
|
2009-11-29 23:29:40 +03:00
|
|
|
total_done = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~TreeItem() {
|
2009-11-11 14:49:59 +03:00
|
|
|
qDebug("Deleting item: %s", getName().toLocal8Bit().data());
|
|
|
|
qDeleteAll(childItems);
|
|
|
|
}
|
|
|
|
|
2009-12-19 17:59:38 +03:00
|
|
|
TreeItemType getType() const {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getFileIndex() const {
|
|
|
|
Q_ASSERT(type ==TFILE);
|
|
|
|
return file_index;
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
void deleteAllChildren() {
|
|
|
|
Q_ASSERT(type==ROOT);
|
2009-11-10 23:47:48 +03:00
|
|
|
qDeleteAll(childItems);
|
2009-11-11 14:49:59 +03:00
|
|
|
childItems.clear();
|
|
|
|
Q_ASSERT(childItems.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<TreeItem*>& children() const {
|
|
|
|
return childItems;
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getName() const {
|
2009-11-11 14:49:59 +03:00
|
|
|
//Q_ASSERT(type != ROOT);
|
2009-11-10 23:47:48 +03:00
|
|
|
return itemData.first().toString();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
void setName(QString name) {
|
|
|
|
Q_ASSERT(type != ROOT);
|
|
|
|
itemData.replace(0, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
qulonglong getSize() const {
|
2009-11-10 23:47:48 +03:00
|
|
|
return itemData.value(1).toULongLong();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
void setSize(qulonglong size) {
|
2009-11-10 23:47:48 +03:00
|
|
|
if(getSize() == size) return;
|
2009-11-11 14:49:59 +03:00
|
|
|
itemData.replace(1, (qulonglong)size);
|
2009-11-10 23:47:48 +03:00
|
|
|
if(parentItem)
|
|
|
|
parentItem->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateSize() {
|
|
|
|
if(type == ROOT) return;
|
|
|
|
Q_ASSERT(type == FOLDER);
|
2009-11-11 14:49:59 +03:00
|
|
|
qulonglong size = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
foreach(TreeItem* child, childItems) {
|
2009-12-01 00:30:14 +03:00
|
|
|
if(child->getPriority() > 0)
|
|
|
|
size += child->getSize();
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
setSize(size);
|
|
|
|
}
|
|
|
|
|
2009-11-29 23:29:40 +03:00
|
|
|
void setProgress(qulonglong done) {
|
|
|
|
total_done = done;
|
|
|
|
qulonglong size = getSize();
|
|
|
|
Q_ASSERT(total_done <= size);
|
|
|
|
float progress;
|
|
|
|
if(size > 0)
|
|
|
|
progress = total_done/(float)size;
|
|
|
|
else
|
|
|
|
progress = 1.;
|
2009-11-29 17:37:33 +03:00
|
|
|
Q_ASSERT(progress >= 0. && progress <= 1.);
|
2009-12-01 00:30:14 +03:00
|
|
|
//qDebug("setProgress(%s): %f", getName().toLocal8Bit().data(), progress);
|
2009-11-10 23:47:48 +03:00
|
|
|
itemData.replace(2, progress);
|
|
|
|
if(parentItem)
|
|
|
|
parentItem->updateProgress();
|
|
|
|
}
|
|
|
|
|
2009-11-29 23:29:40 +03:00
|
|
|
qulonglong getTotalDone() const {
|
|
|
|
return total_done;
|
|
|
|
}
|
|
|
|
|
2009-11-10 23:47:48 +03:00
|
|
|
float getProgress() const {
|
2009-11-29 23:29:40 +03:00
|
|
|
qulonglong size = getSize();
|
|
|
|
if(size > 0)
|
|
|
|
return total_done/(float)getSize();
|
|
|
|
return 1.;
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void updateProgress() {
|
|
|
|
if(type == ROOT) return;
|
|
|
|
Q_ASSERT(type == FOLDER);
|
2009-11-29 23:29:40 +03:00
|
|
|
total_done = 0;
|
2009-12-01 00:30:14 +03:00
|
|
|
//qDebug("Folder %s is updating its progress", getName().toLocal8Bit().data());
|
2009-11-10 23:47:48 +03:00
|
|
|
foreach(TreeItem* child, childItems) {
|
2009-12-01 00:30:14 +03:00
|
|
|
if(child->getPriority() > 0)
|
|
|
|
total_done += child->getTotalDone();
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
2009-12-01 00:30:14 +03:00
|
|
|
//qDebug("Folder: total_done: %llu/%llu", total_done, getSize());
|
2009-11-29 23:29:40 +03:00
|
|
|
Q_ASSERT(total_done <= getSize());
|
|
|
|
setProgress(total_done);
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int getPriority() const {
|
|
|
|
return itemData.value(3).toInt();
|
|
|
|
}
|
|
|
|
|
2009-12-06 13:19:29 +03:00
|
|
|
void setPriority(int new_prio, bool update_children=true) {
|
2009-12-01 00:30:14 +03:00
|
|
|
int old_prio = getPriority();
|
|
|
|
if(old_prio != new_prio) {
|
|
|
|
itemData.replace(3, new_prio);
|
2009-11-10 23:47:48 +03:00
|
|
|
// Update parent
|
2009-12-01 00:30:14 +03:00
|
|
|
if(parentItem) {
|
|
|
|
if(new_prio == 0 || old_prio == 0) {
|
|
|
|
// Files got filtered or unfiltered
|
|
|
|
// Update parent size and progress
|
|
|
|
parentItem->updateSize();
|
|
|
|
parentItem->updateProgress();
|
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
parentItem->updatePriority();
|
2009-12-01 00:30:14 +03:00
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
// Update children
|
2009-12-06 13:19:29 +03:00
|
|
|
if(update_children) {
|
|
|
|
foreach(TreeItem* child, childItems) {
|
|
|
|
child->setPriority(new_prio);
|
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updatePriority() {
|
|
|
|
if(type == ROOT) return;
|
|
|
|
Q_ASSERT(type == FOLDER);
|
|
|
|
int priority = getPriority();
|
|
|
|
bool first = true;
|
|
|
|
foreach(TreeItem* child, childItems) {
|
|
|
|
if(first) {
|
|
|
|
priority = child->getPriority();
|
|
|
|
first = false;
|
|
|
|
} else {
|
2009-12-06 13:19:29 +03:00
|
|
|
if(child->getPriority() != priority) {
|
|
|
|
setPriority(NORMAL, false);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setPriority(priority);
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
TreeItem* childWithName(QString name) const {
|
|
|
|
foreach(TreeItem *child, childItems) {
|
|
|
|
if(child->getName() == name) return child;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-10 23:47:48 +03:00
|
|
|
bool isFolder() const {
|
|
|
|
return (type==FOLDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendChild(TreeItem *item) {
|
2009-11-11 14:49:59 +03:00
|
|
|
Q_ASSERT(item);
|
2009-11-11 15:33:38 +03:00
|
|
|
//Q_ASSERT(!childWithName(item->getName()));
|
2009-11-11 14:49:59 +03:00
|
|
|
Q_ASSERT(type != TFILE);
|
2009-11-15 17:56:25 +03:00
|
|
|
int i=0;
|
|
|
|
for(i=0; i<childItems.size(); ++i) {
|
|
|
|
QString newchild_name = item->getName();
|
|
|
|
if(QString::localeAwareCompare(newchild_name, childItems.at(i)->getName()) < 0) break;
|
|
|
|
}
|
|
|
|
childItems.insert(i, item);
|
|
|
|
//childItems.append(item);
|
2009-11-11 15:33:38 +03:00
|
|
|
//Q_ASSERT(type != ROOT || childItems.size() == 1);
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *child(int row) {
|
2009-11-11 14:49:59 +03:00
|
|
|
//Q_ASSERT(row >= 0 && row < childItems.size());
|
|
|
|
return childItems.value(row, 0);
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int childCount() const {
|
|
|
|
return childItems.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int columnCount() const {
|
|
|
|
return itemData.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant data(int column) const {
|
|
|
|
return itemData.value(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
int row() const {
|
2009-11-11 14:49:59 +03:00
|
|
|
if (parentItem) {
|
|
|
|
return parentItem->children().indexOf(const_cast<TreeItem*>(this));
|
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *parent() {
|
|
|
|
return parentItem;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TorrentFilesModel: public QAbstractItemModel {
|
2009-11-24 16:25:11 +03:00
|
|
|
Q_OBJECT
|
|
|
|
|
2009-11-10 23:47:48 +03:00
|
|
|
private:
|
|
|
|
TreeItem *rootItem;
|
|
|
|
TreeItem **files_index;
|
|
|
|
|
|
|
|
public:
|
2009-11-11 14:49:59 +03:00
|
|
|
TorrentFilesModel(QObject *parent=0): QAbstractItemModel(parent) {
|
|
|
|
files_index = 0;
|
2009-11-10 23:47:48 +03:00
|
|
|
QList<QVariant> rootData;
|
2009-11-24 16:10:17 +03:00
|
|
|
rootData << tr("Name") << tr("Size") << tr("Progress") << tr("Priority");
|
2009-11-10 23:47:48 +03:00
|
|
|
rootItem = new TreeItem(rootData);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TorrentFilesModel() {
|
|
|
|
delete [] files_index;
|
|
|
|
delete rootItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateFilesProgress(std::vector<size_type> fp) {
|
|
|
|
for(unsigned int i=0; i<fp.size(); ++i) {
|
2009-11-29 23:29:40 +03:00
|
|
|
Q_ASSERT(fp[i] >= 0);
|
|
|
|
files_index[i]->setProgress(fp[i]);
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
2009-11-21 13:38:45 +03:00
|
|
|
emit layoutChanged();
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void updateFilesPriorities(std::vector<int> fprio) {
|
|
|
|
for(unsigned int i=0; i<fprio.size(); ++i) {
|
2009-12-19 18:31:50 +03:00
|
|
|
//qDebug("Called updateFilesPriorities with %d", fprio[i]);
|
2009-11-10 23:47:48 +03:00
|
|
|
files_index[i]->setPriority(fprio[i]);
|
|
|
|
}
|
2009-11-21 13:38:45 +03:00
|
|
|
emit layoutChanged();
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
std::vector<int> getFilesPriorities(unsigned int nbFiles) const {
|
|
|
|
std::vector<int> prio;
|
|
|
|
for(unsigned int i=0; i<nbFiles; ++i) {
|
2009-12-19 18:31:50 +03:00
|
|
|
//qDebug("Called getFilesPriorities: %d", files_index[i]->getPriority());
|
2009-11-11 14:49:59 +03:00
|
|
|
prio.push_back(files_index[i]->getPriority());
|
|
|
|
}
|
|
|
|
return prio;
|
|
|
|
}
|
|
|
|
|
2009-11-11 17:06:07 +03:00
|
|
|
bool allFiltered() const {
|
|
|
|
if(!rootItem->childCount()) return true;
|
|
|
|
return (rootItem->child(0)->getPriority() == IGNORED);
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
int columnCount(const QModelIndex &parent=QModelIndex()) const {
|
2009-11-10 23:47:48 +03:00
|
|
|
if (parent.isValid())
|
|
|
|
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
|
|
|
|
else
|
|
|
|
return rootItem->columnCount();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) {
|
|
|
|
if(!index.isValid()) return false;
|
|
|
|
if (role != Qt::EditRole) return false;
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
switch(index.column()) {
|
|
|
|
case 0:
|
|
|
|
item->setName(value.toString());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
item->setSize(value.toULongLong());
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
item->setProgress(value.toDouble());
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
item->setPriority(value.toInt());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-19 17:59:38 +03:00
|
|
|
TreeItemType getType(const QModelIndex &index) {
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
int getFileIndex(const QModelIndex &index) {
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
return item->getFileIndex();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
|
2009-11-10 23:47:48 +03:00
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
if(index.column() == 0 && role == Qt::DecorationRole) {
|
|
|
|
if(item->isFolder())
|
|
|
|
return QIcon(":/Icons/oxygen/folder.png");
|
|
|
|
else
|
|
|
|
return QIcon(":/Icons/oxygen/file.png");
|
|
|
|
}
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
return item->data(index.column());
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const {
|
|
|
|
if (!index.isValid())
|
|
|
|
return 0;
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const {
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
|
|
|
return rootItem->data(section);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const {
|
|
|
|
if (parent.isValid() && parent.column() != 0)
|
2009-11-11 15:22:48 +03:00
|
|
|
return QModelIndex();
|
2009-11-10 23:47:48 +03:00
|
|
|
|
|
|
|
TreeItem *parentItem;
|
|
|
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
|
|
|
|
|
|
|
TreeItem *childItem = parentItem->child(row);
|
2009-11-11 15:22:48 +03:00
|
|
|
if (childItem) {
|
2009-11-10 23:47:48 +03:00
|
|
|
return createIndex(row, column, childItem);
|
2009-11-11 15:22:48 +03:00
|
|
|
} else {
|
2009-11-10 23:47:48 +03:00
|
|
|
return QModelIndex();
|
2009-11-11 15:22:48 +03:00
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex parent(const QModelIndex &index) const {
|
|
|
|
if (!index.isValid())
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
|
|
|
|
TreeItem *parentItem = childItem->parent();
|
|
|
|
|
|
|
|
if (parentItem == rootItem)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return createIndex(parentItem->row(), 0, parentItem);
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
int rowCount(const QModelIndex &parent=QModelIndex()) const {
|
2009-11-10 23:47:48 +03:00
|
|
|
TreeItem *parentItem;
|
2009-11-11 14:49:59 +03:00
|
|
|
|
2009-11-10 23:47:48 +03:00
|
|
|
if (parent.column() > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
parentItem = rootItem;
|
|
|
|
else
|
|
|
|
parentItem = static_cast<TreeItem*>(parent.internalPointer());
|
|
|
|
|
|
|
|
return parentItem->childCount();
|
|
|
|
}
|
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
void clear() {
|
|
|
|
qDebug("clear called");
|
|
|
|
if(files_index) {
|
|
|
|
delete [] files_index;
|
|
|
|
files_index = 0;
|
|
|
|
}
|
|
|
|
rootItem->deleteAllChildren();
|
|
|
|
reset();
|
|
|
|
emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupModelData(torrent_info const& t) {
|
|
|
|
qDebug("setup model data called");
|
2009-11-10 23:47:48 +03:00
|
|
|
if(t.num_files() == 0) return;
|
2009-11-11 14:49:59 +03:00
|
|
|
// Initialize files_index array
|
|
|
|
files_index = new TreeItem*[t.num_files()];
|
2009-11-10 23:47:48 +03:00
|
|
|
|
2009-11-11 14:49:59 +03:00
|
|
|
TreeItem *parent = this->rootItem;
|
2009-11-10 23:47:48 +03:00
|
|
|
if(t.num_files() ==1) {
|
2009-12-19 17:59:38 +03:00
|
|
|
TreeItem *f = new TreeItem(t.file_at(0), parent, 0);
|
2009-11-20 10:18:40 +03:00
|
|
|
//parent->appendChild(f);
|
2009-11-10 23:47:48 +03:00
|
|
|
files_index[0] = f;
|
2009-11-11 14:49:59 +03:00
|
|
|
emit layoutChanged();
|
2009-11-10 23:47:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Create parent folder
|
|
|
|
TreeItem *current_parent = new TreeItem(misc::toQString(t.name()), parent);
|
2009-11-20 10:18:40 +03:00
|
|
|
//parent->appendChild(current_parent);
|
2009-11-11 14:49:59 +03:00
|
|
|
TreeItem *root_folder = current_parent;
|
2009-11-10 23:47:48 +03:00
|
|
|
|
|
|
|
// Iterate over files
|
|
|
|
int i = 0;
|
|
|
|
torrent_info::file_iterator fi = t.begin_files();
|
|
|
|
while(fi != t.end_files()) {
|
2009-11-11 14:49:59 +03:00
|
|
|
current_parent = root_folder;
|
2009-11-10 23:47:48 +03:00
|
|
|
QString path = QDir::cleanPath(misc::toQString(fi->path.string()));
|
|
|
|
// Iterate of parts of the path to create necessary folders
|
|
|
|
QStringList pathFolders = path.split('/');
|
|
|
|
Q_ASSERT(pathFolders.size() >= 2);
|
|
|
|
QString fileName = pathFolders.takeLast();
|
|
|
|
QString currentFolderName = pathFolders.takeFirst();
|
|
|
|
Q_ASSERT(currentFolderName == current_parent->getName());
|
|
|
|
foreach(const QString &pathPart, pathFolders) {
|
2009-11-11 14:49:59 +03:00
|
|
|
TreeItem *new_parent = current_parent->childWithName(pathPart);
|
|
|
|
if(!new_parent) {
|
|
|
|
new_parent = new TreeItem(pathPart, current_parent);
|
2009-11-20 10:18:40 +03:00
|
|
|
//current_parent->appendChild(new_parent);
|
2009-11-11 14:49:59 +03:00
|
|
|
}
|
2009-11-10 23:47:48 +03:00
|
|
|
current_parent = new_parent;
|
|
|
|
}
|
|
|
|
// Actually create the file
|
2009-12-19 17:59:38 +03:00
|
|
|
TreeItem *f = new TreeItem(*fi, current_parent, i);
|
2009-11-20 10:18:40 +03:00
|
|
|
//current_parent->appendChild(f);
|
2009-11-10 23:47:48 +03:00
|
|
|
files_index[i] = f;
|
|
|
|
fi++;
|
|
|
|
++i;
|
|
|
|
}
|
2009-11-11 14:49:59 +03:00
|
|
|
emit layoutChanged();
|
2009-11-10 23:47:48 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // TORRENTFILESMODEL_H
|