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

kget

  • sources
  • kde-4.14
  • kdenetwork
  • kget
  • transfer-plugins
  • bittorrent
  • advanceddetails
iwfilelistmodel.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 "iwfilelistmodel.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  IWFileListModel::IWFileListModel(bt::TorrentInterface* tc,QObject* parent)
36  : TorrentFileListModel(tc,KEEP_FILES,parent)
37  {
38  mmfile = IsMultimediaFile(tc->getStats().output_path);
39  preview = false;
40  percentage = 0;
41  }
42 
43 
44  IWFileListModel::~IWFileListModel()
45  {
46  }
47 
48  int IWFileListModel::columnCount(const QModelIndex & parent) const
49  {
50  if (!parent.isValid())
51  return 5;
52  else
53  return 0;
54  }
55 
56  QVariant IWFileListModel::headerData(int section, Qt::Orientation orientation,int role) const
57  {
58  if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
59  return QVariant();
60 
61  if (section < 2)
62  return TorrentFileListModel::headerData(section,orientation,role);
63 
64  switch (section)
65  {
66  case 2: return i18n("Priority");
67  case 3: return i18n("Preview");
68  // xgettext: no-c-format
69  case 4: return i18nc("Percent of File Downloaded", "% Complete");
70  default: return QVariant();
71  }
72  }
73 
74  static QString PriorityString(const bt::TorrentFileInterface* file)
75  {
76  switch(file->getPriority())
77  {
78  case FIRST_PRIORITY: return i18nc("Download first", "First");
79  case LAST_PRIORITY: return i18nc("Download last", "Last");
80  case ONLY_SEED_PRIORITY:
81  case EXCLUDED:
82  case PREVIEW_PRIORITY:
83  return QString();
84  default:return i18nc("Download Normal (not as first or last)", "Normal");
85  }
86  }
87 
88  QVariant IWFileListModel::data(const QModelIndex & index, int role) const
89  {
90  if (index.column() < 2 && role != Qt::ForegroundRole)
91  return TorrentFileListModel::data(index,role);
92 
93  if (!index.isValid() || index.row() < 0 || index.row() >= rowCount(QModelIndex()))
94  return QVariant();
95 
96  if (role == Qt::ForegroundRole && index.column() == 2 && tc->getStats().multi_file_torrent)
97  {
98  const bt::TorrentFileInterface* file = &tc->getTorrentFile(index.row());
99  switch (file->getPriority())
100  {
101  /*case FIRST_PRIORITY:
102  return InfoWidgetPluginSettings::firstColor();
103  case LAST_PRIORITY:
104  return InfoWidgetPluginSettings::lastColor();
105  case NORMAL_PRIORITY:
106  return InfoWidgetPluginSettings::normalColor();
107  case ONLY_SEED_PRIORITY:
108  case EXCLUDED:
109  case PREVIEW_PRIORITY:*/
110  default:
111  return QVariant();
112  }
113  }
114 
115  if (role == Qt::DisplayRole)
116  return displayData(index);
117  else if (role == Qt::UserRole)
118  return sortData(index);
119 
120  return QVariant();
121  }
122 
123  QVariant IWFileListModel::displayData(const QModelIndex & index) const
124  {
125  if (tc->getStats().multi_file_torrent)
126  {
127  const bt::TorrentFileInterface* file = &tc->getTorrentFile(index.row());
128  switch (index.column())
129  {
130  case 2: return PriorityString(file);
131  case 3:
132  if (file->isMultimedia())
133  {
134  if (file->isPreviewAvailable())
135  return i18nc("Preview available", "Available");
136  else
137  return i18nc("Preview pending", "Pending");
138  }
139  else
140  return i18nc("No preview available", "No");
141  case 4:
142  {
143  float percent = file->getDownloadPercentage();
144  return ki18n("%1 %").subs(percent, 0, 'f', 2).toString();
145  }
146  default: return QVariant();
147  }
148  }
149  else
150  {
151  switch (index.column())
152  {
153  case 2: return QVariant();
154  case 3:
155  if (mmfile)
156  {
157  if (tc->readyForPreview())
158  return i18nc("Preview available", "Available");
159  else
160  return i18nc("Preview pending", "Pending");
161  }
162  else
163  return i18nc("No preview available", "No");
164  case 4:
165  {
166  double percent = bt::Percentage(tc->getStats());
167  return ki18n("%1 %").subs(percent, 0, 'f', 2).toString();
168  }
169  default: return QVariant();
170  }
171  }
172  return QVariant();
173  }
174 
175  QVariant IWFileListModel::sortData(const QModelIndex & index) const
176  {
177  if (tc->getStats().multi_file_torrent)
178  {
179  const bt::TorrentFileInterface* file = &tc->getTorrentFile(index.row());
180  switch (index.column())
181  {
182  case 2: return (int)file->getPriority();
183  case 3:
184  if (file->isMultimedia())
185  {
186  if (file->isPreviewAvailable())
187  return 3;
188  else
189  return 2;
190  }
191  else
192  return 1;
193  case 4:
194  return file->getDownloadPercentage();
195  }
196  }
197  else
198  {
199  switch (index.column())
200  {
201  case 2: return QVariant();
202  case 3:
203  if (mmfile)
204  {
205  if (tc->readyForPreview())
206  return 3;
207  else
208  return 2;
209  }
210  else
211  return 1;
212  case 4:
213  return bt::Percentage(tc->getStats());
214  }
215  }
216  return QVariant();
217  }
218 
219 
220  bool IWFileListModel::setData(const QModelIndex & index, const QVariant & value, int role)
221  {
222  if (role == Qt::CheckStateRole)
223  return TorrentFileListModel::setData(index,value,role);
224 
225  if (!index.isValid() || role != Qt::UserRole)
226  return false;
227 
228  int r = index.row();
229  if (r < 0 || r >= rowCount(QModelIndex()))
230  return false;
231 
232  bt::TorrentFileInterface & file = tc->getTorrentFile(r);;
233  Priority prio = (bt::Priority)value.toInt();
234  Priority old = file.getPriority();
235 
236  if (prio != old)
237  {
238  file.setPriority(prio);
239  dataChanged(createIndex(index.row(),0),createIndex(index.row(),4));
240  }
241 
242  return true;
243  }
244 
245  void IWFileListModel::filePercentageChanged(bt::TorrentFileInterface* file,float percentage)
246  {
247  Q_UNUSED(percentage)
248  QModelIndex idx = createIndex(file->getIndex(),4,file);
249  emit dataChanged(idx,idx);
250  }
251 
252  void IWFileListModel::filePreviewChanged(bt::TorrentFileInterface* file,bool preview)
253  {
254  Q_UNUSED(preview)
255  QModelIndex idx = createIndex(file->getIndex(),3,file);
256  emit dataChanged(idx,idx);
257  }
258 
259  void IWFileListModel::update()
260  {
261  if (!tc->getStats().multi_file_torrent)
262  {
263  bool changed = false;
264  bool np = mmfile && tc->readyForPreview();
265  if (preview != np)
266  {
267  preview = np;
268  changed = true;
269  }
270 
271  double perc = bt::Percentage(tc->getStats());
272  if (fabs(perc - percentage) > 0.01)
273  {
274  percentage = perc;
275  changed = true;
276  }
277 
278  if (changed)
279  dataChanged(createIndex(0,0),createIndex(0,4));
280  }
281  }
282 }
283 
284 #include "iwfilelistmodel.moc"
QModelIndex
kt::TorrentFileListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: torrentfilelistmodel.cpp:76
kt::IWFileListModel::update
void update()
Update gui if necessary.
Definition: iwfilelistmodel.cpp:259
kt::TorrentFileListModel
Model for displaying file trees of a torrent.
Definition: torrentfilelistmodel.h:33
kt::TorrentFileModel::tc
bt::TorrentInterface * tc
Definition: torrentfilemodel.h:141
kt::TorrentFileListModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: torrentfilelistmodel.cpp:157
QModelIndex::isValid
bool isValid() const
kt::TorrentFileListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: torrentfilelistmodel.cpp:62
QVariant::toInt
int toInt(bool *ok) const
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QObject
kt::TorrentFileListModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Definition: torrentfilelistmodel.cpp:46
QModelIndex::row
int row() const
kt::IWFileListModel::filePreviewChanged
void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
Definition: iwfilelistmodel.cpp:252
iwfilelistmodel.h
kt::IWFileListModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: iwfilelistmodel.cpp:88
QString
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
kt::IWFileListModel::~IWFileListModel
virtual ~IWFileListModel()
Definition: iwfilelistmodel.cpp:44
kt::IWFileListModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Definition: iwfilelistmodel.cpp:48
kt::PriorityString
static QString PriorityString(const bt::TorrentFileInterface *file)
Definition: iwfilelistmodel.cpp:74
kt::IWFileListModel::filePercentageChanged
void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
Definition: iwfilelistmodel.cpp:245
kt::IWFileListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: iwfilelistmodel.cpp:56
QModelIndex::column
int column() const
kt::IWFileListModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
Definition: iwfilelistmodel.cpp:220
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:28:43 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