1 /***************************************************************************
2  *   Copyright (C) 2007 by Joris Guisson and Ivan Vasic                    *
3  *   joris.guisson@gmail.com                                               *
4  *   ivasic@gmail.com                                                      *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  ***************************************************************************/
21 #ifndef KTTORRENTFILETREEMODEL_H
22 #define KTTORRENTFILETREEMODEL_H
23 
24 #include "torrentfilemodel.h"
25 #include <util/bitset.h>
26 
27 class QSortFilterProxyModel;
28 
29 namespace bt
30 {
31 	class BEncoder;
32 	class BNode;
33 }
34 
35 namespace kt
36 {
37 
38 	/**
39 	 * Model for displaying file trees of a torrent
40 	 * @author Joris Guisson
41 	*/
42 	class TorrentFileTreeModel : public TorrentFileModel
43 	{
44 		Q_OBJECT
45 	protected:
46 		struct Node
47 		{
48 			Node* parent;
49 			bt::TorrentFileInterface* file; // file (0 if this is a directory)
50 			QString name; // name or directory
51 			QList<Node*> children; // child dirs
52 			bt::Uint64 size;
53 			bt::BitSet chunks;
54 			bool chunks_set;
55 			float percentage;
56 
57 			Node(Node* parent,bt::TorrentFileInterface* file,const QString & name,
58 				bt::Uint32 total_chunks);
59 			Node(Node* parent,const QString & name, bt::Uint32 total_chunks);
60 			~Node();
61 
62 			void insert(const QString & path,bt::TorrentFileInterface* file, bt::Uint32 num_chunks);
63 			int row();
64 			bt::Uint64 fileSize(const bt::TorrentInterface* tc);
65 			bt::Uint64 bytesToDownload(const bt::TorrentInterface* tc);
66 			Qt::CheckState checkState(const bt::TorrentInterface* tc) const;
67 			QString path();
68 			void fillChunks();
69 			void updatePercentage(const bt::BitSet & havechunks);
70 			void initPercentage(const bt::TorrentInterface* tc,const bt::BitSet & havechunks);
71 
72 			void saveExpandedState(const QModelIndex & index,QSortFilterProxyModel* pm,QTreeView* tv,bt::BEncoder* enc);
73 			void loadExpandedState(const QModelIndex & index,QSortFilterProxyModel* pm,QTreeView* tv,bt::BNode* node);
74 		};
75 	public:
76 		TorrentFileTreeModel(bt::TorrentInterface* tc,DeselectMode mode,QObject* parent);
77 		~TorrentFileTreeModel() override;
78 
79 		int rowCount(const QModelIndex & parent) const override;
80 		int columnCount(const QModelIndex & parent) const override;
81 		QVariant headerData(int section, Qt::Orientation orientation,int role) const override;
82 		QVariant data(const QModelIndex & index, int role) const override;
83 		QModelIndex parent(const QModelIndex & index) const override;
84 		QModelIndex index(int row,int column,const QModelIndex & parent) const override;
85 		bool setData(const QModelIndex & index, const QVariant & value, int role) override;
86 		void checkAll() override;
87 		void uncheckAll() override;
88 		void invertCheck() override;
89 		bt::Uint64 bytesToDownload() override;
90 		QByteArray saveExpandedState(QSortFilterProxyModel* pm,QTreeView* tv) override;
91 		void loadExpandedState(QSortFilterProxyModel* pm,QTreeView* tv,const QByteArray & state) override;
92 		bt::TorrentFileInterface* indexToFile(const QModelIndex & idx) override;
93 		QString dirPath(const QModelIndex & idx) override;
94 		void changePriority(const QModelIndexList & indexes,bt::Priority newpriority) override;
95 		void onCodecChange() override;
96 	private:
97 		void constructTree();
98 		void invertCheck(const QModelIndex & idx);
99 		bool setCheckState(const QModelIndex & index, Qt::CheckState state);
100 		bool setName(const QModelIndex & index,const QString & name);
101 		void modifyPathOfFiles(Node* n,const QString & path);
102 
103 
104 	protected:
105 		Node* root;
106 		bool emit_check_state_change;
107 	};
108 
109 }
110 
111 #endif
112