• 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
iwfiletreemodel.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 "iwfiletreemodel.h"
22 
23 #include <math.h>
24 #include <klocale.h>
25 #include <kglobal.h>
26 #include <util/functions.h>
27 #include <interfaces/torrentinterface.h>
28 #include <interfaces/torrentfileinterface.h>
29 
30 using namespace bt;
31 
32 namespace kt
33 {
34 
35  IWFileTreeModel::IWFileTreeModel(bt::TorrentInterface* tc,QObject* parent)
36  : TorrentFileTreeModel(tc,KEEP_FILES,parent)
37  {
38  mmfile = IsMultimediaFile(tc->getStats().output_path);
39  preview = false;
40  percentage = 0;
41 
42  if (root)
43  {
44  BitSet d = tc->downloadedChunksBitSet();
45  d -= tc->onlySeedChunksBitSet();
46  root->initPercentage(tc,d);
47  }
48  }
49 
50 
51  IWFileTreeModel::~IWFileTreeModel()
52  {
53  }
54 
55  int IWFileTreeModel::columnCount(const QModelIndex & /*parent*/) const
56  {
57  return 5;
58  }
59 
60  QVariant IWFileTreeModel::headerData(int section, Qt::Orientation orientation,int role) const
61  {
62  if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
63  return QVariant();
64 
65  if (section < 2)
66  return TorrentFileTreeModel::headerData(section,orientation,role);
67 
68  switch (section)
69  {
70  case 2: return i18n("Priority");
71  case 3: return i18n("Preview");
72  // xgettext: no-c-format
73  case 4: return i18nc("Percent of File Downloaded", "% Complete");
74  default: return QVariant();
75  }
76  }
77 
78  static QString PriorityString(const bt::TorrentFileInterface* file)
79  {
80  switch(file->getPriority())
81  {
82  case FIRST_PRIORITY: return i18nc("Download first", "First");
83  case LAST_PRIORITY: return i18nc("Download last", "Last");
84  case ONLY_SEED_PRIORITY:
85  case EXCLUDED:
86  case PREVIEW_PRIORITY:
87  return QString();
88  default:return i18nc("Download normally(not as first or last)", "Normal");
89  }
90  }
91 
92  QVariant IWFileTreeModel::data(const QModelIndex & index, int role) const
93  {
94  Node* n = 0;
95  if (index.column() < 2 && role != Qt::ForegroundRole)
96  return TorrentFileTreeModel::data(index,role);
97 
98  if (!index.isValid() || !(n = (Node*)index.internalPointer()))
99  return QVariant();
100 
101  if (role == Qt::ForegroundRole && index.column() == 2 && tc->getStats().multi_file_torrent && n->file)
102  {
103  const bt::TorrentFileInterface* file = n->file;
104  switch (file->getPriority())
105  {
106  /*case FIRST_PRIORITY:
107  return InfoWidgetPluginSettings::firstColor();
108  case LAST_PRIORITY:
109  return InfoWidgetPluginSettings::lastColor();
110  case NORMAL_PRIORITY:
111  return InfoWidgetPluginSettings::normalColor();
112  case ONLY_SEED_PRIORITY:
113  case EXCLUDED:
114  case PREVIEW_PRIORITY:*/
115  default:
116  return QVariant();
117  }
118  }
119 
120  if (role == Qt::DisplayRole)
121  return displayData(n,index);
122  else if (role == Qt::UserRole)
123  return sortData(n,index);
124 
125  return QVariant();
126  }
127 
128  QVariant IWFileTreeModel::displayData(Node* n,const QModelIndex & index) const
129  {
130  if (tc->getStats().multi_file_torrent && n->file)
131  {
132  const bt::TorrentFileInterface* file = n->file;
133  switch (index.column())
134  {
135  case 2: return PriorityString(file);
136  case 3:
137  if (file->isMultimedia())
138  {
139  if (file->isPreviewAvailable())
140  return i18nc("preview available", "Available");
141  else
142  return i18nc("Preview pending", "Pending");
143  }
144  else
145  return i18nc("No preview available", "No");
146  case 4:
147  if (file->getPriority() == ONLY_SEED_PRIORITY || file->getPriority() == EXCLUDED)
148  return QVariant();
149  else
150  return ki18n("%1 %").subs(n->percentage, 0, 'f', 2).toString();
151  default: return QVariant();
152  }
153  }
154  else if (!tc->getStats().multi_file_torrent)
155  {
156  switch (index.column())
157  {
158  case 2: return QVariant();
159  case 3:
160  if (mmfile)
161  {
162  if (tc->readyForPreview())
163  return i18nc("Preview available", "Available");
164  else
165  return i18nc("Preview pending", "Pending");
166  }
167  else
168  return i18nc("No preview available", "No");
169  case 4:
170  return ki18n("%1 %").subs(bt::Percentage(tc->getStats()), 0, 'f', 2).toString();
171  default: return QVariant();
172  }
173  }
174  else if (tc->getStats().multi_file_torrent && index.column() == 4)
175  {
176  return ki18n("%1 %").subs(n->percentage, 0, 'f', 2).toString();
177  }
178 
179  return QVariant();
180  }
181 
182  QVariant IWFileTreeModel::sortData(Node* n,const QModelIndex & index) const
183  {
184  if (tc->getStats().multi_file_torrent && n->file)
185  {
186  const bt::TorrentFileInterface* file = n->file;
187  switch (index.column())
188  {
189  case 2: return (int)file->getPriority();
190  case 3:
191  if (file->isMultimedia())
192  {
193  if (file->isPreviewAvailable())
194  return 3;
195  else
196  return 2;
197  }
198  else
199  return 1;
200  case 4:
201  return n->percentage;
202  }
203  }
204  else if (!tc->getStats().multi_file_torrent)
205  {
206  switch (index.column())
207  {
208  case 2: return QVariant();
209  case 3:
210  if (mmfile)
211  {
212  if (tc->readyForPreview())
213  return 3;
214  else
215  return 2;
216  }
217  else
218  return 1;
219  case 4:
220  return bt::Percentage(tc->getStats());
221  }
222  }
223  else if (tc->getStats().multi_file_torrent && index.column() == 4)
224  {
225  return n->percentage;
226  }
227 
228  return QVariant();
229  }
230 
231 
232  bool IWFileTreeModel::setData(const QModelIndex & index, const QVariant & value, int role)
233  {
234  if (role == Qt::CheckStateRole)
235  return TorrentFileTreeModel::setData(index,value,role);
236 
237  if (!index.isValid() || role != Qt::UserRole)
238  return false;
239 
240  Node* n = static_cast<Node*>(index.internalPointer());
241  if (!n)
242  return false;
243 
244  if (!n->file)
245  {
246  for (int i = 0;i < n->children.count();i++)
247  {
248  // recurse down the tree
249  setData(index.child(i,0),value,role);
250  }
251  }
252  else
253  {
254  bt::TorrentFileInterface* file = n->file;
255  Priority prio = (bt::Priority)value.toInt();
256  Priority old = file->getPriority();
257 
258  if (prio != old)
259  {
260  file->setPriority(prio);
261  dataChanged(createIndex(index.row(),0),createIndex(index.row(),4));
262  QModelIndex parent = index.parent();
263  if (parent.isValid())
264  dataChanged(parent,parent); // parent needs to be updated to
265  }
266  }
267 
268  return true;
269  }
270 
271  void IWFileTreeModel::filePercentageChanged(bt::TorrentFileInterface* file,float percentage)
272  {
273  Q_UNUSED(percentage)
274  update(index(0,0,QModelIndex()),file,4);
275  }
276 
277  void IWFileTreeModel::filePreviewChanged(bt::TorrentFileInterface* file,bool preview)
278  {
279  Q_UNUSED(preview)
280  update(index(0,0,QModelIndex()),file,3);
281  }
282 
283  void IWFileTreeModel::update(const QModelIndex & idx,bt::TorrentFileInterface* file,int col)
284  {
285  Node* n = (Node*)idx.internalPointer();
286  if (n->file && n->file == file)
287  {
288  QModelIndex i = createIndex(idx.row(),col,n);
289  emit dataChanged(i,i);
290  if(col == 4)
291  {
292  // update percentages along the tree
293  // this will go back up the tree and update the percentage of
294  // all directories involved
295  BitSet d = tc->downloadedChunksBitSet();
296  d -= tc->onlySeedChunksBitSet();
297  n->updatePercentage(d);
298 
299  // emit necessary signals
300  QModelIndex parent = idx.parent();
301  while (parent.isValid())
302  {
303  Node* nd = (Node*)parent.internalPointer();
304  i = createIndex(parent.row(),4,nd);
305  emit dataChanged(i,i);
306  parent = parent.parent();
307  }
308  }
309  }
310  else
311  {
312  for (int i = 0;i < n->children.count();i++)
313  {
314  // recurse down the tree
315  update(idx.child(i,0),file,col);
316  }
317  }
318  }
319 
320  void IWFileTreeModel::update()
321  {
322  if (!tc->getStats().multi_file_torrent)
323  {
324  bool changed = false;
325  bool np = mmfile && tc->readyForPreview();
326  if (preview != np)
327  {
328  preview = np;
329  changed = true;
330  }
331 
332  double perc = bt::Percentage(tc->getStats());
333  if (fabs(perc - percentage) > 0.01)
334  {
335  percentage = perc;
336  changed = true;
337  }
338 
339  if (changed)
340  dataChanged(createIndex(0,2),createIndex(0,4));
341  }
342  }
343 }
344 
345 #include "iwfiletreemodel.moc"
iwfiletreemodel.h
kt::TorrentFileModel::tc
bt::TorrentInterface * tc
Definition: torrentfilemodel.h:141
kt::TorrentFileTreeModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: torrentfiletreemodel.cpp:369
QObject
kt::IWFileTreeModel::filePercentageChanged
void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
Definition: iwfiletreemodel.cpp:271
kt::TorrentFileTreeModel::parent
virtual QModelIndex parent(const QModelIndex &index) const
Definition: torrentfiletreemodel.cpp:436
kt::TorrentFileTreeModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: torrentfiletreemodel.cpp:576
kt::TorrentFileTreeModel::Node::initPercentage
void initPercentage(const bt::TorrentInterface *tc, const bt::BitSet &havechunks)
Definition: torrentfiletreemodel.cpp:166
kt::IWFileTreeModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: iwfiletreemodel.cpp:60
kt::TorrentFileTreeModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Definition: torrentfiletreemodel.cpp:452
kt::TorrentFileTreeModel::Node
Definition: torrentfiletreemodel.h:46
kt::TorrentFileTreeModel
Model for displaying file trees of a torrent.
Definition: torrentfiletreemodel.h:42
kt::TorrentFileTreeModel::Node::file
bt::TorrentFileInterface * file
Definition: torrentfiletreemodel.h:49
kt::TorrentFileTreeModel::root
Node * root
Definition: torrentfiletreemodel.h:105
kt::IWFileTreeModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Definition: iwfiletreemodel.cpp:55
kt::TorrentFileTreeModel::Node::children
QList< Node * > children
Definition: torrentfiletreemodel.h:51
kt::PriorityString
static QString PriorityString(const bt::TorrentFileInterface *file)
Definition: iwfilelistmodel.cpp:74
kt::IWFileTreeModel::~IWFileTreeModel
virtual ~IWFileTreeModel()
Definition: iwfiletreemodel.cpp:51
kt::IWFileTreeModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: iwfiletreemodel.cpp:92
kt::IWFileTreeModel::filePreviewChanged
void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
Definition: iwfiletreemodel.cpp:277
kt::IWFileTreeModel::update
virtual void update()
Update gui if necessary.
Definition: iwfiletreemodel.cpp:320
kt::TorrentFileTreeModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: torrentfiletreemodel.cpp:383
kt::IWFileTreeModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: iwfiletreemodel.cpp:232
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