• 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
chunkdownloadmodel.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 "chunkdownloadmodel.h"
22 
23 #include <klocale.h>
24 #include <interfaces/torrentinterface.h>
25 #include <interfaces/torrentfileinterface.h>
26 #include <interfaces/chunkdownloadinterface.h>
27 #include <util/functions.h>
28 
29 using namespace bt;
30 
31 namespace kt
32 {
33 
34  ChunkDownloadModel::Item::Item(ChunkDownloadInterface* cd,const QString & files) : cd(cd),files(files)
35  {
36  cd->getStats(stats);
37  }
38 
39  bool ChunkDownloadModel::Item::changed(int col,bool & modified) const
40  {
41  ChunkDownloadInterface::Stats s;
42  cd->getStats(s);
43  bool ret = false;
44  switch (col)
45  {
46  case 1: ret = s.pieces_downloaded != stats.pieces_downloaded; break;
47  case 2: ret = s.current_peer_id != stats.current_peer_id; break;
48  case 3: ret = s.download_speed != stats.download_speed; break;
49  default: break;
50  }
51 
52  modified = s.pieces_downloaded != stats.pieces_downloaded ||
53  s.download_speed != stats.download_speed ||
54  s.current_peer_id != stats.current_peer_id;
55 
56  stats = s;
57  return ret;
58  }
59 
60  QVariant ChunkDownloadModel::Item::data(int col) const
61  {
62  switch (col)
63  {
64  case 0: return stats.chunk_index;
65  case 1: return QString("%1 / %2").arg(stats.pieces_downloaded).arg(stats.total_pieces);
66  case 2: return stats.current_peer_id;
67  case 3: return BytesPerSecToString(stats.download_speed);
68  case 4: return files;
69  }
70  return QVariant();
71  }
72 
73  bool ChunkDownloadModel::Item::lessThan(int col,const Item* other) const
74  {
75  switch (col)
76  {
77  case 0: return stats.chunk_index < other->stats.chunk_index;
78  case 1: return stats.pieces_downloaded < other->stats.pieces_downloaded;
79  case 2: return stats.current_peer_id < other->stats.current_peer_id;
80  case 3: return stats.download_speed < other->stats.download_speed;
81  case 4: return files < other->files;
82  }
83  return false;
84  }
85 
87 
88  ChunkDownloadModel::ChunkDownloadModel ( QObject* parent )
89  : QAbstractTableModel(parent),tc(0)
90  {
91  sort_column = 0;
92  sort_order = Qt::AscendingOrder;
93  }
94 
95 
96  ChunkDownloadModel::~ChunkDownloadModel()
97  {
98  qDeleteAll(items);
99  }
100 
101  void ChunkDownloadModel::downloadAdded(bt::ChunkDownloadInterface* cd)
102  {
103  if (!tc)
104  return;
105 
106  bt::ChunkDownloadInterface::Stats stats;
107  cd->getStats(stats);
108  QString files;
109  int n = 0;
110  if (tc->getStats().multi_file_torrent)
111  {
112  for (Uint32 i = 0;i < tc->getNumFiles();++i)
113  {
114  const bt::TorrentFileInterface & tf = tc->getTorrentFile(i);
115  if (stats.chunk_index >= tf.getFirstChunk() && stats.chunk_index <= tf.getLastChunk())
116  {
117  if (n > 0)
118  files += '\n';
119 
120  files += tf.getPath();
121  n++;
122  }
123  else if (stats.chunk_index < tf.getFirstChunk())
124  break;
125  }
126  }
127 
128  Item* nitem = new Item(cd,files);
129  items.append(nitem);
130  insertRow(items.count() - 1);
131  sort(sort_column,sort_order);
132  }
133 
134  void ChunkDownloadModel::downloadRemoved(bt::ChunkDownloadInterface* cd)
135  {
136  int idx = 0;
137  for (QList<Item*>::iterator i = items.begin();i != items.end();i++)
138  {
139  const Item* item = *i;
140  if (item->cd == cd)
141  {
142  items.erase(i);
143  delete item;
144  removeRow(idx);
145  break;
146  }
147  idx++;
148  }
149  }
150 
151  void ChunkDownloadModel::changeTC(bt::TorrentInterface* tc)
152  {
153  qDeleteAll(items);
154  items.clear();
155  this->tc = tc;
156  reset();
157  }
158 
159  void ChunkDownloadModel::clear()
160  {
161  qDeleteAll(items);
162  items.clear();
163  reset();
164  }
165 
166  void ChunkDownloadModel::update()
167  {
168  bool resort = false;
169  Uint32 idx=0;
170  foreach (Item* i,items)
171  {
172  bool modified = false;
173  if (i->changed(sort_column,modified))
174  resort = true;
175 
176  if (modified && !resort)
177  emit dataChanged(index(idx,1),index(idx,3));
178  idx++;
179  }
180 
181  if (resort)
182  sort(sort_column,sort_order);
183  }
184 
185  int ChunkDownloadModel::rowCount(const QModelIndex & parent) const
186  {
187  if (parent.isValid())
188  return 0;
189  else
190  return items.count();
191  }
192 
193  int ChunkDownloadModel::columnCount(const QModelIndex & parent) const
194  {
195  if (parent.isValid())
196  return 0;
197  else
198  return 5;
199  }
200 
201  QVariant ChunkDownloadModel::headerData(int section,Qt::Orientation orientation,int role) const
202  {
203  if (orientation != Qt::Horizontal)
204  return QVariant();
205 
206  if (role == Qt::DisplayRole)
207  {
208  switch (section)
209  {
210  case 0: return i18n("Chunk");
211  case 1: return i18n("Progress");
212  case 2: return i18n("Peer");
213  case 3: return i18n("Down Speed");
214  case 4: return i18n("Files");
215  default: return QVariant();
216  }
217  }
218  else if (role == Qt::ToolTipRole)
219  {
220  switch (section)
221  {
222  case 0: return i18n("Number of the chunk");
223  case 1: return i18n("Download progress of the chunk");
224  case 2: return i18n("Which peer we are downloading it from");
225  case 3: return i18n("Download speed of the chunk");
226  case 4: return i18n("Which files the chunk is located in");
227  default: return QVariant();
228  }
229  }
230 
231  return QVariant();
232  }
233 
234  QModelIndex ChunkDownloadModel::index(int row,int column,const QModelIndex & parent) const
235  {
236  if (!hasIndex(row,column,parent) || parent.isValid())
237  return QModelIndex();
238  else
239  return createIndex(row,column,items[row]);
240  }
241 
242  QVariant ChunkDownloadModel::data(const QModelIndex & index,int role) const
243  {
244  if (!index.isValid() || index.row() >= items.count() || index.row() < 0)
245  return QVariant();
246 
247  if (role == Qt::DisplayRole)
248  return items[index.row()]->data(index.column());
249 
250  return QVariant();
251  }
252 
253  bool ChunkDownloadModel::removeRows(int row,int count,const QModelIndex & /*parent*/ )
254  {
255  beginRemoveRows(QModelIndex(),row,row + count - 1);
256  endRemoveRows();
257  return true;
258  }
259 
260  bool ChunkDownloadModel::insertRows(int row,int count,const QModelIndex & /*parent*/ )
261  {
262  beginInsertRows(QModelIndex(),row,row + count - 1);
263  endInsertRows();
264  return true;
265  }
266 
267  class ChunkDownloadModelItemCmp
268  {
269  public:
270  ChunkDownloadModelItemCmp(int col,Qt::SortOrder order) : col(col),order(order)
271  {}
272 
273  bool operator()(ChunkDownloadModel::Item* a,ChunkDownloadModel::Item* b)
274  {
275  if (order == Qt::AscendingOrder)
276  return a->lessThan(col,b);
277  else
278  return !a->lessThan(col,b);
279  }
280 
281  int col;
282  Qt::SortOrder order;
283  };
284 
285  void ChunkDownloadModel::sort(int col, Qt::SortOrder order)
286  {
287  sort_column = col;
288  sort_order = order;
289  emit layoutAboutToBeChanged();
290  qStableSort(items.begin(),items.end(),ChunkDownloadModelItemCmp(col,order));
291  emit layoutChanged();
292  }
293 }
QAbstractItemModel::hasIndex
bool hasIndex(int row, int column, const QModelIndex &parent) const
QAbstractItemModel::insertRow
bool insertRow(int row, const QModelIndex &parent)
QList::clear
void clear()
QModelIndex
QAbstractItemModel::layoutChanged
void layoutChanged()
QAbstractItemModel::removeRow
bool removeRow(int row, const QModelIndex &parent)
QAbstractTableModel
QList::erase
iterator erase(iterator pos)
kt::ChunkDownloadModel::Item
Definition: chunkdownloadmodel.h:74
kt::ChunkDownloadModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Definition: chunkdownloadmodel.cpp:185
QAbstractItemModel::layoutAboutToBeChanged
void layoutAboutToBeChanged()
QAbstractItemModel::reset
void reset()
QModelIndex::isValid
bool isValid() const
kt::ChunkDownloadModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent)
Definition: chunkdownloadmodel.cpp:253
QList::count
int count(const T &value) const
kt::ChunkDownloadModel::columnCount
virtual int columnCount(const QModelIndex &parent) const
Definition: chunkdownloadmodel.cpp:193
QList::append
void append(const T &value)
kt::ChunkDownloadModel::changeTC
void changeTC(bt::TorrentInterface *tc)
change the current torrent
Definition: chunkdownloadmodel.cpp:151
chunkdownloadmodel.h
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
kt::ChunkDownloadModel::update
void update()
Update the model.
Definition: chunkdownloadmodel.cpp:166
kt::ChunkDownloadModel::Item::cd
bt::ChunkDownloadInterface * cd
Definition: chunkdownloadmodel.h:77
kt::ChunkDownloadModel::downloadAdded
void downloadAdded(bt::ChunkDownloadInterface *cd)
A peer has been added.
Definition: chunkdownloadmodel.cpp:101
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
kt::ChunkDownloadModel::~ChunkDownloadModel
virtual ~ChunkDownloadModel()
Definition: chunkdownloadmodel.cpp:96
kt::ChunkDownloadModel::Item::changed
bool changed(int col, bool &modified) const
Definition: chunkdownloadmodel.cpp:39
QString
QList
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
kt::ChunkDownloadModel::downloadRemoved
void downloadRemoved(bt::ChunkDownloadInterface *cd)
A download has been removed.
Definition: chunkdownloadmodel.cpp:134
QList::end
iterator end()
kt::ChunkDownloadModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: chunkdownloadmodel.cpp:242
kt::ChunkDownloadModel::ChunkDownloadModel
ChunkDownloadModel(QObject *parent)
Definition: chunkdownloadmodel.cpp:88
kt::ChunkDownloadModel::Item::files
QString files
Definition: chunkdownloadmodel.h:78
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
kt::ChunkDownloadModel::Item::lessThan
bool lessThan(int col, const Item *other) const
Definition: chunkdownloadmodel.cpp:73
kt::ChunkDownloadModel::Item::data
QVariant data(int col) const
Definition: chunkdownloadmodel.cpp:60
QModelIndex::column
int column() const
kt::ChunkDownloadModel::insertRows
virtual bool insertRows(int row, int count, const QModelIndex &parent)
Definition: chunkdownloadmodel.cpp:260
kt::ChunkDownloadModel::sort
void sort(int col, Qt::SortOrder order)
Definition: chunkdownloadmodel.cpp:285
QAbstractItemModel::endRemoveRows
void endRemoveRows()
QObject::parent
QObject * parent() const
kt::ChunkDownloadModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: chunkdownloadmodel.cpp:234
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
kt::ChunkDownloadModel::clear
void clear()
Definition: chunkdownloadmodel.cpp:159
QList::begin
iterator begin()
kt::ChunkDownloadModel::Item::stats
bt::ChunkDownloadInterface::Stats stats
Definition: chunkdownloadmodel.h:76
kt::ChunkDownloadModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: chunkdownloadmodel.cpp:201
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