2012-02-21 23:54:24 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006-2012 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "iconprovider.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "torrentcontentmodel.h"
|
|
|
|
#include "torrentcontentmodelitem.h"
|
2012-08-26 19:10:32 +04:00
|
|
|
#include "torrentcontentmodelFolder.h"
|
|
|
|
#include "torrentcontentmodelFile.h"
|
2012-02-21 23:54:24 +04:00
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
TorrentContentModel::TorrentContentModel(QObject *parent):
|
2012-02-25 23:02:19 +04:00
|
|
|
QAbstractItemModel(parent),
|
2012-08-26 19:10:32 +04:00
|
|
|
m_rootItem(new TorrentContentModelFolder(QList<QVariant>() << tr("Name") << tr("Size")
|
2012-02-25 22:05:11 +04:00
|
|
|
<< tr("Progress") << tr("Priority")))
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentContentModel::~TorrentContentModel()
|
|
|
|
{
|
2012-02-25 22:05:11 +04:00
|
|
|
delete m_rootItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentContentModel::updateFilesProgress(const std::vector<libtorrent::size_type>& fp)
|
|
|
|
{
|
2012-02-25 23:02:19 +04:00
|
|
|
Q_ASSERT(m_filesIndex.size() == (int)fp.size());
|
2012-08-26 21:08:26 +04:00
|
|
|
// XXX: Why is this necessary?
|
|
|
|
if (m_filesIndex.size() != (int)fp.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
for (uint i = 0; i < fp.size(); ++i) {
|
2012-02-25 22:05:11 +04:00
|
|
|
m_filesIndex[i]->setProgress(fp[i]);
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
emit dataChanged(index(0,0), index(rowCount(), columnCount()));
|
|
|
|
}
|
|
|
|
|
2012-08-26 16:00:37 +04:00
|
|
|
void TorrentContentModel::updateFilesPriorities(const std::vector<int>& fprio)
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
2012-04-08 12:17:32 +04:00
|
|
|
Q_ASSERT(m_filesIndex.size() == (int)fprio.size());
|
2012-08-26 21:08:26 +04:00
|
|
|
// XXX: Why is this necessary?
|
|
|
|
if (m_filesIndex.size() != (int)fprio.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
for (uint i = 0; i < fprio.size(); ++i) {
|
2012-02-25 22:05:11 +04:00
|
|
|
m_filesIndex[i]->setPriority(fprio[i]);
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
2012-08-26 21:08:26 +04:00
|
|
|
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
2012-04-08 12:17:32 +04:00
|
|
|
std::vector<int> TorrentContentModel::getFilesPriorities() const
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
|
|
|
std::vector<int> prio;
|
2012-08-26 16:00:37 +04:00
|
|
|
prio.reserve(m_filesIndex.size());
|
2012-08-26 21:15:55 +04:00
|
|
|
foreach (const TorrentContentModelFile* file, m_filesIndex) {
|
2012-08-26 19:10:32 +04:00
|
|
|
prio.push_back(file->priority());
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
return prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentContentModel::allFiltered() const
|
|
|
|
{
|
2012-08-26 21:15:55 +04:00
|
|
|
foreach (const TorrentContentModelFile* fileItem, m_filesIndex) {
|
2012-08-26 21:08:26 +04:00
|
|
|
if (fileItem->priority() != prio::IGNORED)
|
2012-02-21 23:54:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentContentModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return static_cast<TorrentContentModelItem*>(parent.internalPointer())->columnCount();
|
|
|
|
else
|
2012-02-25 22:05:11 +04:00
|
|
|
return m_rootItem->columnCount();
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentContentModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (index.column() == 0 && role == Qt::CheckStateRole) {
|
|
|
|
TorrentContentModelItem *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
|
2012-08-26 19:10:32 +04:00
|
|
|
qDebug("setData(%s, %d", qPrintable(item->name()), value.toInt());
|
|
|
|
if (item->priority() != value.toInt()) {
|
2012-02-21 23:54:24 +04:00
|
|
|
if (value.toInt() == Qt::PartiallyChecked)
|
2012-08-26 21:52:53 +04:00
|
|
|
item->setPriority(prio::MIXED);
|
2012-02-21 23:54:24 +04:00
|
|
|
else if (value.toInt() == Qt::Unchecked)
|
|
|
|
item->setPriority(prio::IGNORED);
|
|
|
|
else
|
|
|
|
item->setPriority(prio::NORMAL);
|
2012-07-02 23:51:03 +04:00
|
|
|
emit dataChanged(this->index(0,0), this->index(rowCount()-1, columnCount()-1));
|
2012-02-21 23:54:24 +04:00
|
|
|
emit filteredFilesChanged();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == Qt::EditRole) {
|
2012-08-26 21:15:55 +04:00
|
|
|
Q_ASSERT(index.isValid());
|
|
|
|
TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer());
|
2012-02-21 23:54:24 +04:00
|
|
|
switch(index.column()) {
|
|
|
|
case TorrentContentModelItem::COL_NAME:
|
|
|
|
item->setName(value.toString());
|
|
|
|
break;
|
|
|
|
case TorrentContentModelItem::COL_PRIO:
|
|
|
|
item->setPriority(value.toInt());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-26 21:08:26 +04:00
|
|
|
|
2012-02-21 23:54:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-26 19:10:32 +04:00
|
|
|
TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex& index) const
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
2012-08-26 21:08:26 +04:00
|
|
|
return static_cast<const TorrentContentModelItem*>(index.internalPointer())->itemType();
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentContentModel::getFileIndex(const QModelIndex& index)
|
|
|
|
{
|
2012-08-26 19:10:32 +04:00
|
|
|
TorrentContentModelFile* item = dynamic_cast<TorrentContentModelFile*>(static_cast<TorrentContentModelItem*>(index.internalPointer()));
|
|
|
|
Q_ASSERT(item);
|
|
|
|
return item->fileIndex();
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
2012-08-26 21:15:55 +04:00
|
|
|
QVariant TorrentContentModel::data(const QModelIndex& index, int role) const
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelItem* item = static_cast<TorrentContentModelItem*>(index.internalPointer());
|
2012-02-21 23:54:24 +04:00
|
|
|
if (index.column() == 0 && role == Qt::DecorationRole) {
|
2012-08-26 19:10:32 +04:00
|
|
|
if (item->itemType() == TorrentContentModelItem::FolderType)
|
2012-02-21 23:54:24 +04:00
|
|
|
return IconProvider::instance()->getIcon("inode-directory");
|
|
|
|
else
|
|
|
|
return IconProvider::instance()->getIcon("text-plain");
|
|
|
|
}
|
|
|
|
if (index.column() == 0 && role == Qt::CheckStateRole) {
|
|
|
|
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::IGNORED)
|
|
|
|
return Qt::Unchecked;
|
2012-08-26 21:52:53 +04:00
|
|
|
if (item->data(TorrentContentModelItem::COL_PRIO).toInt() == prio::MIXED)
|
2012-02-21 23:54:24 +04:00
|
|
|
return Qt::PartiallyChecked;
|
|
|
|
return Qt::Checked;
|
|
|
|
}
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
return item->data(index.column());
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags TorrentContentModel::flags(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return 0;
|
|
|
|
|
2012-08-26 19:10:32 +04:00
|
|
|
if (itemType(index) == TorrentContentModelItem::FolderType)
|
2012-02-21 23:54:24 +04:00
|
|
|
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
|
2012-08-26 21:08:26 +04:00
|
|
|
|
2012-02-21 23:54:24 +04:00
|
|
|
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
2012-02-25 22:05:11 +04:00
|
|
|
return m_rootItem->data(section);
|
2012-02-21 23:54:24 +04:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid() && parent.column() != 0)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
if (column >= TorrentContentModelItem::NB_COL)
|
|
|
|
return QModelIndex();
|
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelFolder* parentItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
if (!parent.isValid())
|
2012-02-25 22:05:11 +04:00
|
|
|
parentItem = m_rootItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
else
|
2012-08-26 19:10:32 +04:00
|
|
|
parentItem = static_cast<TorrentContentModelFolder*>(parent.internalPointer());
|
2012-02-21 23:54:24 +04:00
|
|
|
Q_ASSERT(parentItem);
|
2012-08-26 21:08:26 +04:00
|
|
|
|
2012-02-21 23:54:24 +04:00
|
|
|
if (row >= parentItem->childCount())
|
|
|
|
return QModelIndex();
|
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelItem* childItem = parentItem->child(row);
|
|
|
|
if (childItem)
|
2012-02-21 23:54:24 +04:00
|
|
|
return createIndex(row, column, childItem);
|
2012-08-26 21:08:26 +04:00
|
|
|
return QModelIndex();
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TorrentContentModel::parent(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QModelIndex();
|
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelItem* childItem = static_cast<TorrentContentModelItem*>(index.internalPointer());
|
2012-02-25 22:05:11 +04:00
|
|
|
if (!childItem)
|
|
|
|
return QModelIndex();
|
2012-02-21 23:54:24 +04:00
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelItem *parentItem = childItem->parent();
|
2012-02-25 22:05:11 +04:00
|
|
|
if (parentItem == m_rootItem)
|
2012-02-21 23:54:24 +04:00
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return createIndex(parentItem->row(), 0, parentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentContentModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (parent.column() > 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-26 21:08:26 +04:00
|
|
|
TorrentContentModelFolder* parentItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
if (!parent.isValid())
|
2012-02-25 22:05:11 +04:00
|
|
|
parentItem = m_rootItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
else
|
2012-08-26 19:10:32 +04:00
|
|
|
parentItem = dynamic_cast<TorrentContentModelFolder*>(static_cast<TorrentContentModelItem*>(parent.internalPointer()));
|
2012-02-21 23:54:24 +04:00
|
|
|
|
2012-08-26 19:10:32 +04:00
|
|
|
return parentItem ? parentItem->childCount() : 0;
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentContentModel::clear()
|
|
|
|
{
|
|
|
|
qDebug("clear called");
|
|
|
|
beginResetModel();
|
2012-02-25 22:05:11 +04:00
|
|
|
m_filesIndex.clear();
|
|
|
|
m_rootItem->deleteAllChildren();
|
2012-02-21 23:54:24 +04:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2012-08-26 16:00:37 +04:00
|
|
|
void TorrentContentModel::setupModelData(const libtorrent::torrent_info& t)
|
2012-02-21 23:54:24 +04:00
|
|
|
{
|
|
|
|
qDebug("setup model data called");
|
|
|
|
if (t.num_files() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
// Initialize files_index array
|
|
|
|
qDebug("Torrent contains %d files", t.num_files());
|
2012-02-25 22:05:11 +04:00
|
|
|
m_filesIndex.reserve(t.num_files());
|
2012-02-21 23:54:24 +04:00
|
|
|
|
2012-08-26 19:10:32 +04:00
|
|
|
TorrentContentModelFolder* current_parent;
|
2012-02-21 23:54:24 +04:00
|
|
|
// Iterate over files
|
2012-08-26 20:55:09 +04:00
|
|
|
for (int i = 0; i < t.num_files(); ++i) {
|
2012-08-26 16:00:37 +04:00
|
|
|
const libtorrent::file_entry& fentry = t.file_at(i);
|
2012-08-26 20:55:09 +04:00
|
|
|
current_parent = m_rootItem;
|
2012-02-21 23:54:24 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR >= 16
|
2012-08-26 16:00:37 +04:00
|
|
|
QString path = misc::toQStringU(fentry.path);
|
2012-02-21 23:54:24 +04:00
|
|
|
#else
|
2012-08-26 16:00:37 +04:00
|
|
|
QString path = misc::toQStringU(fentry.path.string());
|
2012-02-21 23:54:24 +04:00
|
|
|
#endif
|
|
|
|
// Iterate of parts of the path to create necessary folders
|
2012-08-26 16:00:37 +04:00
|
|
|
QStringList pathFolders = path.split(QRegExp("[/\\\\]"), QString::SkipEmptyParts);
|
|
|
|
pathFolders.removeLast();
|
|
|
|
foreach (const QString& pathPart, pathFolders) {
|
|
|
|
if (pathPart == ".unwanted")
|
|
|
|
continue;
|
2012-08-26 19:10:32 +04:00
|
|
|
TorrentContentModelFolder* new_parent = current_parent->childFolderWithName(pathPart);
|
2012-08-27 22:11:54 +04:00
|
|
|
if (!new_parent) {
|
2012-08-26 19:10:32 +04:00
|
|
|
new_parent = new TorrentContentModelFolder(pathPart, current_parent);
|
2012-08-27 22:11:54 +04:00
|
|
|
current_parent->appendChild(new_parent);
|
|
|
|
}
|
2012-02-21 23:54:24 +04:00
|
|
|
current_parent = new_parent;
|
|
|
|
}
|
|
|
|
// Actually create the file
|
2012-08-27 22:11:54 +04:00
|
|
|
TorrentContentModelFile* fileItem = new TorrentContentModelFile(fentry, current_parent, i);
|
|
|
|
current_parent->appendChild(fileItem);
|
|
|
|
m_filesIndex.push_back(fileItem);
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentContentModel::selectAll()
|
|
|
|
{
|
2012-02-25 22:05:11 +04:00
|
|
|
for (int i=0; i<m_rootItem->childCount(); ++i) {
|
2012-08-26 16:00:37 +04:00
|
|
|
TorrentContentModelItem* child = m_rootItem->child(i);
|
2012-08-26 19:10:32 +04:00
|
|
|
if (child->priority() == prio::IGNORED)
|
2012-02-21 23:54:24 +04:00
|
|
|
child->setPriority(prio::NORMAL);
|
|
|
|
}
|
2012-08-26 21:08:26 +04:00
|
|
|
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentContentModel::selectNone()
|
|
|
|
{
|
2012-02-25 22:05:11 +04:00
|
|
|
for (int i=0; i<m_rootItem->childCount(); ++i) {
|
|
|
|
m_rootItem->child(i)->setPriority(prio::IGNORED);
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|
2012-08-26 21:08:26 +04:00
|
|
|
emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
|
2012-02-21 23:54:24 +04:00
|
|
|
}
|