summaryrefslogtreecommitdiffstats
path: root/libktorrent/interfaces/filetreediritem.cpp
blob: 86481f238e442683708244e7df3d3447d1de57b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [email protected]                                               *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#include <kglobal.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kmimetype.h>
#include <util/functions.h>
#include <interfaces/functions.h>
#include <torrent/globals.h>
#include "filetreediritem.h"
#include "filetreeitem.h"
#include "torrentfileinterface.h"
#include <torrent/torrentfile.h>

using namespace bt;

namespace kt
{

	FileTreeDirItem::FileTreeDirItem(KListView* klv,const TQString & name,FileTreeRootListener* rl)
	: TQCheckListItem(klv,TQString(),TQCheckListItem::CheckBox),name(name),root_listener(rl)
	{
		parent = 0;
		size = 0;
		setPixmap(0,KGlobal::iconLoader()->loadIcon("folder",KIcon::Small));
		setText(0,name);
		setText(1,BytesToString(size));
		setText(2,i18n("Yes"));
		manual_change = true;
		setOn(true);
		manual_change = false;
	}

	FileTreeDirItem::FileTreeDirItem(FileTreeDirItem* parent,const TQString & name)
	: TQCheckListItem(parent,TQString(),TQCheckListItem::CheckBox),
	name(name),parent(parent)
	{
		size = 0;
		setPixmap(0,KGlobal::iconLoader()->loadIcon("folder",KIcon::Small));
		setText(0,name);
		setText(1,BytesToString(size));
		setText(2,i18n("Yes"));
		manual_change = true;
		setOn(true);
		manual_change = false;
	}

	FileTreeDirItem::~FileTreeDirItem()
	{
	}

	void FileTreeDirItem::insert(const TQString & path,kt::TorrentFileInterface & file)
	{
		size += file.getSize();
		setText(1,BytesToString(size));
		int p = path.find(bt::DirSeparator());
		if (p == -1)
		{
			tqchildren.insert(path,newFileTreeItem(path,file));
		}
		else
		{
			TQString subdir = path.left(p);
			FileTreeDirItem* sd = subdirs.find(subdir);
			if (!sd)
			{
				sd = newFileTreeDirItem(subdir);
				subdirs.insert(subdir,sd);
			}
			
			sd->insert(path.mid(p+1),file);
		}
	}
	
	void FileTreeDirItem::setAllChecked(bool on,bool keep_data)
	{
		if (!manual_change)
		{
			manual_change = true;
			setOn(on);
			manual_change = false;
		}
	// first set all the child items
		bt::PtrMap<TQString,FileTreeItem>::iterator i = tqchildren.begin();
		while (i != tqchildren.end())
		{
			i->second->setChecked(on,keep_data);
			i++;
		}

	// then recursivly move on to subdirs
		bt::PtrMap<TQString,FileTreeDirItem>::iterator j = subdirs.begin();
		while (j != subdirs.end())
		{
			j->second->setAllChecked(on,keep_data);
			j++;
		}
	}
	

	void FileTreeDirItem::invertChecked()
	{
	// first set all the child items
		bt::PtrMap<TQString,FileTreeItem>::iterator i = tqchildren.begin();
		while (i != tqchildren.end())
		{
			FileTreeItem* item = i->second;
			item->setChecked(!item->isOn());
			i++;
		}

	// then recursivly move on to subdirs
		bt::PtrMap<TQString,FileTreeDirItem>::iterator j = subdirs.begin();
		while (j != subdirs.end())
		{
			j->second->invertChecked();
			j++;
		}
	}

	void FileTreeDirItem::stateChange(bool on)
	{
		if (!manual_change)
		{
			if (on)
			{
				setAllChecked(true);
			}
			else
			{
				switch (confirmationDialog())
				{
					case KEEP_DATA:
						setAllChecked(false,true);
						break;
					case THROW_AWAY_DATA:
						setAllChecked(false,false);
						break;
					case CANCELED:
					default:
						manual_change = true;
						setOn(true);
						manual_change = false;
						return;
				}
			}
			if (parent)
				parent->childStateChange();
		}
		setText(2,on ? i18n("Yes") : i18n("No"));
	}
	
	Uint64 FileTreeDirItem::bytesToDownload() const
	{
		Uint64 tot = 0;
		// first check all the child items
		bt::PtrMap<TQString,FileTreeItem>::const_iterator i = tqchildren.begin();
		while (i != tqchildren.end())
		{
			const FileTreeItem* item = i->second;
			tot += item->bytesToDownload();
			i++;
		}

		// then recursivly move on to subdirs
		bt::PtrMap<TQString,FileTreeDirItem>::const_iterator j = subdirs.begin();
		while (j != subdirs.end())
		{
			tot += j->second->bytesToDownload();
			j++;
		}
		return tot;
	}

	bool FileTreeDirItem::allChildrenOn()
	{
		// first check all the child items
		bt::PtrMap<TQString,FileTreeItem>::iterator i = tqchildren.begin();
		while (i != tqchildren.end())
		{
			FileTreeItem* item = i->second;
			if (!item->isOn())
				return false;
			i++;
		}

		// then recursivly move on to subdirs
		bt::PtrMap<TQString,FileTreeDirItem>::iterator j = subdirs.begin();
		while (j != subdirs.end())
		{
			if (!j->second->allChildrenOn())
				return false;
			j++;
		}
		return true;
	}

	void FileTreeDirItem::childStateChange()
	{
	// only set this dir on if all tqchildren are on
		manual_change = true;
		setOn(allChildrenOn());
		manual_change = false;
	
		if (parent)
			parent->childStateChange();
		else if (root_listener)
			root_listener->treeItemChanged();
			
	}

	int FileTreeDirItem::compare(TQListViewItem* i, int col, bool ascending) const
	{
		if (col == 1)
		{
			FileTreeDirItem* other = dynamic_cast<FileTreeDirItem*>(i);
			if (!other)
				return 0;
			else
				return (int)(size - other->size);
		}
		else
		{
			//return TQCheckListItem::compare(i, col, ascending);
			// case insensitive comparison
			return TQString::compare(text(col).lower(),i->text(col).lower());
		}
	}

	TorrentFileInterface & FileTreeDirItem::findTorrentFile(TQListViewItem* item)
	{
	// first check all the child items
		TorrentFileInterface & nullfile = (TorrentFileInterface &)TorrentFile::null;
		bt::PtrMap<TQString,FileTreeItem>::iterator i = tqchildren.begin();
		while (i != tqchildren.end())
		{
			FileTreeItem* file = i->second;
			if (file == (FileTreeItem*)item)
				return file->getTorrentFile();
			i++;
		}

	// then recursivly move on to subdirs
		bt::PtrMap<TQString,FileTreeDirItem>::iterator j = subdirs.begin();
		while (j != subdirs.end())
		{
			TorrentFileInterface & thefile = j->second->findTorrentFile(item);
			if(!thefile.isNull())
				return thefile;
			j++;
		}
		return nullfile;
	}

	FileTreeItem* FileTreeDirItem::newFileTreeItem(const TQString & name,TorrentFileInterface & file)
	{
		return new FileTreeItem(this,name,file);
	}

	FileTreeDirItem* FileTreeDirItem::newFileTreeDirItem(const TQString & subdir)
	{
		return new FileTreeDirItem(this,subdir);
	}

	bt::ConfirmationResult FileTreeDirItem::confirmationDialog()
	{
		return bt::THROW_AWAY_DATA;
	}
	
	TQString FileTreeDirItem::getPath() const
	{
		if (!parent)
			return bt::DirSeparator();
		else
			return parent->getPath() + name + bt::DirSeparator();
	}
}