File indexing completed on 2024-05-12 04:59:21

0001 /***************************************************************************
0002  *   Copyright (C) 2007 by Joris Guisson and Ivan Vasic                    *
0003  *   joris.guisson@gmail.com                                               *
0004  *   ivasic@gmail.com                                                      *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, write to the                         *
0018  *   Free Software Foundation, Inc.,                                       *
0019  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0020  ***************************************************************************/
0021 #ifndef KTTORRENTFILETREEMODEL_H
0022 #define KTTORRENTFILETREEMODEL_H
0023 
0024 #include "torrentfilemodel.h"
0025 #include <util/bitset.h>
0026 
0027 class QSortFilterProxyModel;
0028 
0029 namespace bt
0030 {
0031 class BEncoder;
0032 class BNode;
0033 }
0034 
0035 namespace kt
0036 {
0037 
0038 /**
0039  * Model for displaying file trees of a torrent
0040  * @author Joris Guisson
0041  */
0042 class TorrentFileTreeModel : public TorrentFileModel
0043 {
0044     Q_OBJECT
0045 protected:
0046     struct Node {
0047         Node *parent;
0048         bt::TorrentFileInterface *file; // file (0 if this is a directory)
0049         QString name; // name or directory
0050         QList<Node *> children; // child dirs
0051         bt::Uint64 size;
0052         bt::BitSet chunks;
0053         bool chunks_set;
0054         float percentage;
0055 
0056         Node(Node *parent, bt::TorrentFileInterface *file, const QString &name, bt::Uint32 total_chunks);
0057         Node(Node *parent, const QString &name, bt::Uint32 total_chunks);
0058         ~Node();
0059 
0060         void insert(const QString &path, bt::TorrentFileInterface *file, bt::Uint32 num_chunks);
0061         int row();
0062         bt::Uint64 fileSize(const bt::TorrentInterface *tc);
0063         bt::Uint64 bytesToDownload(const bt::TorrentInterface *tc);
0064         Qt::CheckState checkState(const bt::TorrentInterface *tc) const;
0065         QString path();
0066         void fillChunks();
0067         void updatePercentage(const bt::BitSet &havechunks);
0068         void initPercentage(const bt::TorrentInterface *tc, const bt::BitSet &havechunks);
0069 
0070         void saveExpandedState(const QModelIndex &index, QSortFilterProxyModel *pm, QTreeView *tv, bt::BEncoder *enc);
0071         void loadExpandedState(const QModelIndex &index, QSortFilterProxyModel *pm, QTreeView *tv, bt::BNode *node);
0072     };
0073 
0074 public:
0075     TorrentFileTreeModel(bt::TorrentInterface *tc, DeselectMode mode, QObject *parent);
0076     ~TorrentFileTreeModel() override;
0077 
0078     int rowCount(const QModelIndex &parent) const override;
0079     int columnCount(const QModelIndex &parent) const override;
0080     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0081     QVariant data(const QModelIndex &index, int role) const override;
0082     QModelIndex parent(const QModelIndex &index) const override;
0083     QModelIndex index(int row, int column, const QModelIndex &parent) const override;
0084     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0085     void checkAll() override;
0086     void uncheckAll() override;
0087     void invertCheck() override;
0088     bt::Uint64 bytesToDownload() override;
0089     QByteArray saveExpandedState(QSortFilterProxyModel *pm, QTreeView *tv) override;
0090     void loadExpandedState(QSortFilterProxyModel *pm, QTreeView *tv, const QByteArray &state) override;
0091     bt::TorrentFileInterface *indexToFile(const QModelIndex &idx) override;
0092     QString dirPath(const QModelIndex &idx) override;
0093     void changePriority(const QModelIndexList &indexes, bt::Priority newpriority) override;
0094     void onCodecChange() override;
0095 
0096 private:
0097     void constructTree();
0098     void invertCheck(const QModelIndex &idx);
0099     bool setCheckState(const QModelIndex &index, Qt::CheckState state);
0100     bool setName(const QModelIndex &index, const QString &name);
0101     void modifyPathOfFiles(Node *n, const QString &path);
0102 
0103 protected:
0104     Node *root;
0105     bool emit_check_state_change;
0106 };
0107 }
0108 
0109 #endif