Initial commit for the QML client:
[qtrapids] / src / client / models / QDeclarativeDownloadListModel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
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.               *
7  *                                                                         *
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.                          *
12  *                                                                         *
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  ***************************************************************************/
18
19 #include <QtCore/QVariant>
20 #include <QtCore/QDebug>
21
22 #include "QDeclarativeDownloadListModel.h"
23 #include "QDeclarativeDownloadListModel_p.h"
24 #include "qtrapids/format.hpp"
25
26 namespace qtrapids
27 {
28
29 QDeclarativeDownloadListModel::QDeclarativeDownloadListModel(QObject *parent):
30         QAbstractListModel(parent)
31 {
32     d = new QDeclarativeDownloadListModelPrivate;
33
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";
53     setRoleNames(roles);
54 }
55
56 QDeclarativeDownloadListModel::~QDeclarativeDownloadListModel()
57 {
58 }
59
60
61 void
62 QDeclarativeDownloadListModel::updateItem(TorrentState const& info,
63                                           ParamsMap_t other_info)
64 {
65     qDebug() << Q_FUNC_INFO << " enter";
66
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);
72             } else {
73                 qWarning() << "item with similar info hash marked as added";
74                 updateItem_(info, other_info);
75             }
76             break;
77         case TorrentState::action_update :
78             if (p != d->items_.end()) {
79                 updateItem_(info, other_info);
80             } else {
81                 qWarning() << "item does not exist in list but information update arrived";
82             }
83             break;
84         case TorrentState::action_remove :
85             if (p != d->items_.end()) {
86                 removeItem_(info);
87             } else {
88                 qWarning() << "item removal request arrived but there is no such item";
89             }
90             break;
91         default:
92             qWarning() << "unknown action requested: " << info.action;
93             break;
94     }
95 }
96
97
98     // Returns the hash of the torrent be removed.
99 QString QDeclarativeDownloadListModel::prepareRemoveSelected()
100 {
101     return "DUMMY_HASH";
102 }
103
104 int QDeclarativeDownloadListModel::rowCount(const QModelIndex &parent) const
105 {
106     return d->items_.count();
107 }
108
109 QVariant QDeclarativeDownloadListModel::data(const QModelIndex &index, int role) const
110 {
111
112     qDebug() << Q_FUNC_INFO << " enter";
113     int rowIndex = index.row();
114     QString hash;
115     TorrentState item;
116
117     // Get the item hash corresponding to row number:
118     if (d->itemIndexes_.contains(rowIndex))
119         hash = d->itemIndexes_[rowIndex];
120     else
121         return QVariant();
122
123     // Get the torrent info for the hash:
124     if (d->items_.contains(hash))
125         item = d->items_[hash];
126     else 
127         return QVariant();
128
129     switch (role) {
130 //         case DownloadRole:
131 //             // TODO: What to do here??
132 //             break;
133 //         case SeedRole:
134 //             // TODO: What to do here??
135 //             break;
136         case HashRole:
137             return QVariant(item.hash);
138         case NameRole:
139             return QVariant(item.name);
140 //         case SizeRole:
141 //             return QVariant(item.total_size);
142         case StatusRole:
143             return QVariant();
144         case StateRole:
145             return QVariant(item.state);
146         case ProgressRole:
147             return QVariant(item.progress);
148         case DownRateRole:
149             return QVariant(item.down_rate);
150         case UpRateRole:
151             return QVariant(item.up_rate);
152         case SeedsRole:
153             return QVariant(item.seeds);
154         case LeechesRole:
155             return QVariant(item.leeches);
156         case RatioRole:
157             return QVariant(item.ratio);
158         case TotaSizeRole:
159             return QVariant(item.total_size);
160         case TotalDoneRole:
161             return QVariant(item.total_done);
162         default:
163             qWarning() << Q_FUNC_INFO << "Unknown role requested from model";
164             return QVariant();
165     }
166     return QVariant();
167 }
168
169 bool QDeclarativeDownloadListModel::insertRow(int row, const QModelIndex &parent)
170 {
171     qDebug() << "QDeclarativeDownloadListModel::insertRow()";
172     return false;
173 }
174
175 bool QDeclarativeDownloadListModel::setData(const QModelIndex &index, const QVariant &value, int role)
176 {
177     qDebug() << "QDeclarativeDownloadListModel::setData()";
178     return false;
179 }
180
181
182 void QDeclarativeDownloadListModel::classBegin()
183 {
184     qDebug() << "QDeclarativeDownloadListModel::classBegin()";
185 }
186
187 void QDeclarativeDownloadListModel::componentComplete()
188 {
189     qDebug() << "QDeclarativeDownloadListModel::componentComplete()";
190 }
191
192
193 void QDeclarativeDownloadListModel::removeItem_(TorrentState const& info)
194 {
195     qDebug() << Q_FUNC_INFO << " enter";
196     QString hash = info.hash;
197
198     // TODO: Emit rows remove start
199     int removed = d->items_.remove(hash);
200     // TODO: Edit rows insert end
201     if (!removed)
202         qDebug() << "Inconsistent download view state on item removal";
203
204     /*
205     int index = indexOfTopLevelItem(item);
206     if (index >= 0) {
207             takeTopLevelItem(index);
208     }
209     */
210 }
211
212 void QDeclarativeDownloadListModel::addItem_(TorrentState const& info,
213                                              ParamsMap_t)
214 {
215      qDebug() << Q_FUNC_INFO << " enter";
216 /*
217     DownloadViewItem *item = new DownloadViewItem
218     ( info.hash,
219       QStringList()
220       << info.name
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)
228       << "ETA" );
229
230     QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
231     item->setForeground(2, brushTmp);
232
233     addTopLevelItem(item);
234     */
235
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;
240     endInsertRows();
241 }
242
243
244 void QDeclarativeDownloadListModel::updateItem_(TorrentState const& info, ParamsMap_t)
245 {
246      qDebug() << Q_FUNC_INFO << " enter";
247     /*
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));
259
260     // Calculate ETA
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));
264     }       else {
265         item->setData(8, Qt::DisplayRole, "N/A");
266     }
267 */
268     // TODO: Update items data & Emit data changed.
269
270 /*
271     // Set color for status text
272     QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
273     item->setForeground(2, brushTmp);
274     */
275 }
276
277
278
279 QString QDeclarativeDownloadListModel::GetStatusString(TorrentStatus::Id status)
280 {
281     qDebug() << Q_FUNC_INFO << " enter";
282     switch (status) {
283         case TorrentStatus::QUEUED_FOR_CHECKING :
284             return tr("Queued");
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");
299         default:
300             return tr("N/A");
301     }
302 }
303
304 } // namespace qtrapids