• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • transfer-plugins
  • bittorrent
  • advanceddetails
torrentfilelistmodel.cpp
Go to the documentation of this file.
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 #include "torrentfilelistmodel.h"
22 
23 #include <klocale.h>
24 #include <kicon.h>
25 #include <kmimetype.h>
26 #include <QTreeView>
27 #include <interfaces/torrentinterface.h>
28 #include <interfaces/torrentfileinterface.h>
29 #include <util/functions.h>
30 
31 using namespace bt;
32 
33 namespace kt
34 {
35 
36 
37  TorrentFileListModel::TorrentFileListModel(bt::TorrentInterface* tc,DeselectMode mode,QObject* parent)
38  : TorrentFileModel(tc,mode,parent)
39  {
40  }
41 
42 
43  TorrentFileListModel::~TorrentFileListModel()
44  {}
45 
46  int TorrentFileListModel::rowCount(const QModelIndex & parent) const
47  {
48  if (!parent.isValid())
49  return tc->getStats().multi_file_torrent ? tc->getNumFiles() : 1;
50  else
51  return 0;
52  }
53 
54  int TorrentFileListModel::columnCount(const QModelIndex & parent) const
55  {
56  if (!parent.isValid())
57  return 2;
58  else
59  return 0;
60  }
61 
62  QVariant TorrentFileListModel::headerData(int section, Qt::Orientation orientation,int role) const
63  {
64  if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
65  return QVariant();
66 
67  switch (section)
68  {
69  case 0: return i18n("File");
70  case 1: return i18n("Size");
71  default:
72  return QVariant();
73  }
74  }
75 
76  QVariant TorrentFileListModel::data(const QModelIndex & index, int role) const
77  {
78  if (!index.isValid())
79  return QVariant();
80 
81 
82  int r = index.row();
83  int nfiles = rowCount(QModelIndex());
84  bool multi = tc->getStats().multi_file_torrent;
85  if (r < 0 || r >= nfiles)
86  return QVariant();
87 
88  const TorrentStats & s = tc->getStats();
89  if (role == Qt::DisplayRole || role == Qt::EditRole)
90  {
91  switch (index.column())
92  {
93  case 0:
94  if (multi)
95  return tc->getTorrentFile(r).getUserModifiedPath();
96  else
97  return tc->getUserModifiedFileName();
98  case 1:
99  if (multi)
100  return BytesToString(tc->getTorrentFile(r).getSize());
101  else
102  return BytesToString(s.total_bytes);
103  default: return QVariant();
104  }
105  }
106  else if (role == Qt::UserRole) // sorting
107  {
108  switch (index.column())
109  {
110  case 0:
111  if (multi)
112  return tc->getTorrentFile(r).getUserModifiedPath();
113  else
114  return tc->getUserModifiedFileName();
115  case 1:
116  if (multi)
117  return tc->getTorrentFile(r).getSize();
118  else
119  return s.total_bytes;
120  default: return QVariant();
121  }
122  }
123  else if (role == Qt::DecorationRole && index.column() == 0)
124  {
125  // if this is an empty folder then we are in the single file case
126  if (multi)
127  return KIcon(KMimeType::findByPath(tc->getTorrentFile(r).getPath())->iconName());
128  else
129  return KIcon(KMimeType::findByPath(s.torrent_name)->iconName());
130  }
131  else if (role == Qt::CheckStateRole && index.column() == 0)
132  {
133  if (multi)
134  return tc->getTorrentFile(r).doNotDownload() ? Qt::Unchecked : Qt::Checked;;
135  }
136 
137  return QVariant();
138  }
139 
140  QModelIndex TorrentFileListModel::parent(const QModelIndex & index) const
141  {
142  Q_UNUSED(index)
143  return QModelIndex();
144  }
145 
146  QModelIndex TorrentFileListModel::index(int row,int column,const QModelIndex & parent) const
147  {
148  if (!hasIndex(row, column, parent))
149  return QModelIndex();
150  else
151  {
152  bt::TorrentFileInterface* f = &tc->getTorrentFile(row);
153  return createIndex(row,column,f);
154  }
155  }
156 
157  bool TorrentFileListModel::setData(const QModelIndex & index, const QVariant & value, int role)
158  {
159  if (!index.isValid())
160  return false;
161 
162  if (role == Qt::CheckStateRole)
163  {
164  Qt::CheckState newState = static_cast<Qt::CheckState>(value.toInt());
165  bt::TorrentFileInterface & file = tc->getTorrentFile(index.row());
166  if (newState == Qt::Checked)
167  {
168  if (file.getPriority() == ONLY_SEED_PRIORITY)
169  file.setPriority(NORMAL_PRIORITY);
170  else
171  file.setDoNotDownload(false);
172  }
173  else
174  {
175  if (mode == KEEP_FILES)
176  file.setPriority(ONLY_SEED_PRIORITY);
177  else
178  file.setDoNotDownload(true);
179  }
180  dataChanged(createIndex(index.row(),0),createIndex(index.row(),columnCount(index) - 1));
181  checkStateChanged();
182  return true;
183  }
184  else if (role == Qt::EditRole)
185  {
186  QString path = value.toString();
187  if (path.isEmpty())
188  return false;
189 
190  if (tc->getStats().multi_file_torrent)
191  {
192  bt::TorrentFileInterface & file = tc->getTorrentFile(index.row());
193  // keep track of modified paths
194  file.setUserModifiedPath(path);
195  }
196  else
197  {
198  // change the name of the file or toplevel directory
199  tc->setUserModifiedFileName(path);
200  }
201  dataChanged(createIndex(index.row(),0),createIndex(index.row(),columnCount(index) - 1));
202  return true;
203  }
204 
205  return false;
206  }
207 
208  void TorrentFileListModel::checkAll()
209  {
210  if (tc->getStats().multi_file_torrent)
211  {
212  for (Uint32 i = 0;i < tc->getNumFiles();++i)
213  setData(index(i,0,QModelIndex()),Qt::Checked,Qt::CheckStateRole);
214  }
215  }
216 
217  void TorrentFileListModel::uncheckAll()
218  {
219  if (tc->getStats().multi_file_torrent)
220  {
221  for (Uint32 i = 0;i < tc->getNumFiles();++i)
222  setData(index(i,0,QModelIndex()),Qt::Unchecked,Qt::CheckStateRole);
223  }
224  }
225 
226  void TorrentFileListModel::invertCheck()
227  {
228  if (!tc->getStats().multi_file_torrent)
229  return;
230 
231  for (Uint32 i = 0;i < tc->getNumFiles();++i)
232  invertCheck(index(i,0,QModelIndex()));
233  }
234 
235  void TorrentFileListModel::invertCheck(const QModelIndex & idx)
236  {
237  if (tc->getTorrentFile(idx.row()).doNotDownload())
238  setData(idx,Qt::Checked,Qt::CheckStateRole);
239  else
240  setData(idx,Qt::Unchecked,Qt::CheckStateRole);
241  }
242 
243  bt::Uint64 TorrentFileListModel::bytesToDownload()
244  {
245  if (tc->getStats().multi_file_torrent)
246  {
247  bt::Uint64 ret = 0;
248  for (Uint32 i = 0;i < tc->getNumFiles();++i)
249  {
250  const bt::TorrentFileInterface & file = tc->getTorrentFile(i);
251  if (!file.doNotDownload())
252  ret += file.getSize();
253  }
254  return ret;
255  }
256  else
257  return tc->getStats().total_bytes;
258  }
259 
260  bt::TorrentFileInterface* TorrentFileListModel::indexToFile(const QModelIndex & idx)
261  {
262  if (!idx.isValid())
263  return 0;
264 
265  int r = idx.row();
266  if (r < 0 || r >= rowCount(QModelIndex()))
267  return 0;
268  else
269  return &tc->getTorrentFile(r);
270  }
271 
272  QString TorrentFileListModel::dirPath(const QModelIndex & idx)
273  {
274  if (!idx.isValid())
275  return QString();
276 
277  int r = idx.row();
278  if (r < 0 || r >= rowCount(QModelIndex()))
279  return QString();
280  else
281  return tc->getTorrentFile(r).getPath();
282  }
283 
284  void TorrentFileListModel::changePriority(const QModelIndexList & indexes,bt::Priority newpriority)
285  {
286  foreach (const QModelIndex &idx,indexes)
287  {
288  setData(idx,newpriority,Qt::UserRole);
289  }
290  }
291 }
292 
293 #include "torrentfilelistmodel.moc"
kt::TorrentFileModel::mode
DeselectMode mode
Definition: torrentfilemodel.h:142
kt::TorrentFileListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: torrentfilelistmodel.cpp:76
kt::TorrentFileListModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Definition: torrentfilelistmodel.cpp:54
kt::TorrentFileModel::tc
bt::TorrentInterface * tc
Definition: torrentfilemodel.h:141
kt::TorrentFileListModel::indexToFile
virtual bt::TorrentFileInterface * indexToFile(const QModelIndex &idx)
Convert a model index to a file.
Definition: torrentfilelistmodel.cpp:260
kt::TorrentFileListModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: torrentfilelistmodel.cpp:157
kt::TorrentFileListModel::changePriority
virtual void changePriority(const QModelIndexList &indexes, bt::Priority newpriority)
Change the priority of a bunch of items.
Definition: torrentfilelistmodel.cpp:284
QObject
kt::TorrentFileListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: torrentfilelistmodel.cpp:62
kt::TorrentFileListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Definition: torrentfilelistmodel.cpp:46
kt::TorrentFileModel::KEEP_FILES
Definition: torrentfilemodel.h:45
kt::TorrentFileListModel::invertCheck
virtual void invertCheck()
Invert the check of each file of the torrent.
Definition: torrentfilelistmodel.cpp:226
kt::TorrentFileListModel::checkAll
virtual void checkAll()
Check all the files in the torrent.
Definition: torrentfilelistmodel.cpp:208
kt::TorrentFileListModel::dirPath
virtual QString dirPath(const QModelIndex &idx)
Get the path of a directory (root directory not included)
Definition: torrentfilelistmodel.cpp:272
kt::TorrentFileModel::DeselectMode
DeselectMode
Definition: torrentfilemodel.h:43
torrentfilelistmodel.h
kt::TorrentFileListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Definition: torrentfilelistmodel.cpp:146
kt::TorrentFileListModel::parent
virtual QModelIndex parent(const QModelIndex &index) const
Definition: torrentfilelistmodel.cpp:140
kt::TorrentFileModel::checkStateChanged
void checkStateChanged()
Emitted whenever one or more items changes check state.
kt::TorrentFileListModel::uncheckAll
virtual void uncheckAll()
Uncheck all files in the torrent.
Definition: torrentfilelistmodel.cpp:217
kt::TorrentFileListModel::bytesToDownload
virtual bt::Uint64 bytesToDownload()
Calculate the number of bytes to download.
Definition: torrentfilelistmodel.cpp:243
kt::TorrentFileListModel::~TorrentFileListModel
virtual ~TorrentFileListModel()
Definition: torrentfilelistmodel.cpp:43
kt::TorrentFileModel
Definition: torrentfilemodel.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal