1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
19 #include <QtCore/QVariant>
20 #include <QtCore/QDebug>
22 #include "QDeclarativeDownloadListModel.h"
23 #include "QDeclarativeDownloadListModel_p.h"
24 #include "qtrapids/format.hpp"
29 QDeclarativeDownloadListModel::QDeclarativeDownloadListModel(QObject *parent):
30 QAbstractListModel(parent)
32 d = new QDeclarativeDownloadListModelPrivate;
34 // These roles are the names that we use to access the model data on QML
35 // in the string format. So, use the value on the right-hand side of assignment operator in QML.
36 // The role enum value is passed from QML side to the C++ model for fetching the data.
37 QHash<int, QByteArray> roles;
38 roles[DownloadRole] = "download";
39 roles[SeedRole] = "seed";
40 roles[HashRole] = "hash";
41 roles[NameRole] = "name";
42 roles[SizeRole] = "size";
43 roles[StatusRole] = "status";
44 roles[StateRole] = "state";
45 roles[ProgressRole] = "progress";
46 roles[DownRateRole] = "downRate";
47 roles[UpRateRole] = "upRate";
48 roles[SeedsRole] = "seeds";
49 roles[LeechesRole] = "leeches";
50 roles[RatioRole] = "ratio";
51 roles[TotaSizeRole] = "totalSize";
52 roles[TotalDoneRole] = "totalDone";
56 QDeclarativeDownloadListModel::~QDeclarativeDownloadListModel()
62 QDeclarativeDownloadListModel::updateItem(TorrentState const& info,
63 ParamsMap_t other_info)
65 qDebug() << Q_FUNC_INFO << " enter";
67 DownloadItems_t::iterator p = d->items_.find(info.hash);
68 switch (info.action) {
69 case TorrentState::action_add :
70 if (p == d->items_.end()) {
71 addItem_(info, other_info);
73 qWarning() << "item with similar info hash marked as added";
74 updateItem_(info, other_info);
77 case TorrentState::action_update :
78 if (p != d->items_.end()) {
79 updateItem_(info, other_info);
81 qWarning() << "item does not exist in list but information update arrived";
84 case TorrentState::action_remove :
85 if (p != d->items_.end()) {
88 qWarning() << "item removal request arrived but there is no such item";
92 qWarning() << "unknown action requested: " << info.action;
98 // Returns the hash of the torrent be removed.
99 QString QDeclarativeDownloadListModel::prepareRemoveSelected()
104 int QDeclarativeDownloadListModel::rowCount(const QModelIndex &parent) const
106 return d->items_.count();
109 QVariant QDeclarativeDownloadListModel::data(const QModelIndex &index, int role) const
112 qDebug() << Q_FUNC_INFO << " enter";
113 int rowIndex = index.row();
117 // Get the item hash corresponding to row number:
118 if (d->itemIndexes_.contains(rowIndex))
119 hash = d->itemIndexes_[rowIndex];
123 // Get the torrent info for the hash:
124 if (d->items_.contains(hash))
125 item = d->items_[hash];
130 // case DownloadRole:
131 // // TODO: What to do here??
134 // // TODO: What to do here??
137 return QVariant(item.hash);
139 return QVariant(item.name);
141 // return QVariant(item.total_size);
145 return QVariant(item.state);
147 return QVariant(item.progress);
149 return QVariant(item.down_rate);
151 return QVariant(item.up_rate);
153 return QVariant(item.seeds);
155 return QVariant(item.leeches);
157 return QVariant(item.ratio);
159 return QVariant(item.total_size);
161 return QVariant(item.total_done);
163 qWarning() << Q_FUNC_INFO << "Unknown role requested from model";
169 bool QDeclarativeDownloadListModel::insertRow(int row, const QModelIndex &parent)
171 qDebug() << "QDeclarativeDownloadListModel::insertRow()";
175 bool QDeclarativeDownloadListModel::setData(const QModelIndex &index, const QVariant &value, int role)
177 qDebug() << "QDeclarativeDownloadListModel::setData()";
182 void QDeclarativeDownloadListModel::classBegin()
184 qDebug() << "QDeclarativeDownloadListModel::classBegin()";
187 void QDeclarativeDownloadListModel::componentComplete()
189 qDebug() << "QDeclarativeDownloadListModel::componentComplete()";
193 void QDeclarativeDownloadListModel::removeItem_(TorrentState const& info)
195 qDebug() << Q_FUNC_INFO << " enter";
196 QString hash = info.hash;
198 // TODO: Emit rows remove start
199 int removed = d->items_.remove(hash);
200 // TODO: Edit rows insert end
202 qDebug() << "Inconsistent download view state on item removal";
205 int index = indexOfTopLevelItem(item);
207 takeTopLevelItem(index);
212 void QDeclarativeDownloadListModel::addItem_(TorrentState const& info,
215 qDebug() << Q_FUNC_INFO << " enter";
217 DownloadViewItem *item = new DownloadViewItem
221 << formatSize(info.total_size)
222 << GetStatusString((TorrentStatus::Id)info.state)
223 << formatProgress(info.progress)
224 << formatSize(info.down_rate)
225 << formatSize(info.up_rate)
226 << QString::number(info.seeds) + "/" + QString::number(info.leeches)
227 << QString::number(info.ratio)
230 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
231 item->setForeground(2, brushTmp);
233 addTopLevelItem(item);
236 int itemsCount = d->items_.count();
237 beginInsertRows(QModelIndex(), itemsCount, itemsCount+1);
238 d->items_[info.hash] = info;
239 d->itemIndexes_[itemsCount+1] = info.hash;
244 void QDeclarativeDownloadListModel::updateItem_(TorrentState const& info, ParamsMap_t)
246 qDebug() << Q_FUNC_INFO << " enter";
248 item->setData(2, Qt::DisplayRole,
249 QVariant(GetStatusString((TorrentStatus::Id)info.state)));
250 item->setData(3, Qt::DisplayRole,
251 QVariant(formatProgress(info.progress)));
252 item->setData(4, Qt::DisplayRole,
253 QVariant(formatSize(info.down_rate)));
254 item->setData(5, Qt::DisplayRole,
255 QVariant(formatSize(info.up_rate)));
256 item->setData(6, Qt::DisplayRole,
257 QString::number(info.seeds) + "/" + QString::number(info.leeches));
258 item->setData(7, Qt::DisplayRole, QString::number(info.ratio));
261 if (info.down_rate > 0) {
262 qulonglong eta = (info.total_size - info.total_done) / info.down_rate;
263 item->setData(8, Qt::DisplayRole, formatElapsedTime(eta));
265 item->setData(8, Qt::DisplayRole, "N/A");
268 // TODO: Update items data & Emit data changed.
271 // Set color for status text
272 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
273 item->setForeground(2, brushTmp);
279 QString QDeclarativeDownloadListModel::GetStatusString(TorrentStatus::Id status)
281 qDebug() << Q_FUNC_INFO << " enter";
283 case TorrentStatus::QUEUED_FOR_CHECKING :
285 case TorrentStatus::CHECKING_FILES :
286 return tr("Checking");
287 case TorrentStatus::DOWNLOADING_METADATA :
288 return tr("DL meta");
289 case TorrentStatus::DOWNLOADING :
290 return tr("Downloading");
291 case TorrentStatus::FINISHED :
292 return tr("Finished");
293 case TorrentStatus::SEEDING :
294 return tr("Seeding");
295 case TorrentStatus::ALLOCATING :
296 return tr("Allocating");
297 case TorrentStatus::CHECKING_RESUME_DATA :
298 return tr("Checking resume");
304 } // namespace qtrapids