-
Notifications
You must be signed in to change notification settings - Fork 1
/
qtorrentlistmodel.cpp
executable file
·105 lines (88 loc) · 3.09 KB
/
qtorrentlistmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "qtorrentlistmodel.h"
#include "qtorrentobject.h"
QTorrentListModel::QTorrentListModel(QObject *parent)
: QAbstractListModel(parent)
{
// Map role names to string equivalents accesible from qml
m_roleNames[NameRole] = "name";
m_roleNames[SpeedRole] = "speed";
m_roleNames[TotalBytesRole] = "totalBytes";
m_roleNames[BytesDownloadedRole] = "bytesDownloaded";
m_roleNames[dateTimeAddedRole] = "dateTimeAdded";
m_roleNames[torrentStatusRole] = "torrentStatus";
}
int QTorrentListModel::rowCount(const QModelIndex &parent) const
{
return m_torrents.count();
}
QVariant QTorrentListModel::data(const QModelIndex &index, int role) const {
// Boilerplate bounds checking on the index
int row = index.row();
if (row < 0 || row >= m_torrents.count()) {
return QVariant();
}
QSharedPointer<QTorrentObject> torrent = m_torrents.at(row);
switch(role) {
case NameRole:
return torrent->name();
case SpeedRole:
return torrent->speed();
case TotalBytesRole:
return torrent->totalBytes();
case BytesDownloadedRole:
return torrent->bytesDownloaded();
case dateTimeAddedRole:
return torrent->dateTimeAdded();
case torrentStatusRole:
return torrent->torrentStatus();
}
// Fallthrough
return QVariant();
}
bool QTorrentListModel::insert(int insert_index, QSharedPointer<QTorrentObject>torrent)
{
if (insert_index < 0|| insert_index > m_torrents.count())
return false;
// Notify before insert, insert, notify after insert
emit beginInsertRows(QModelIndex(), insert_index, insert_index);
m_torrents.insert(insert_index, torrent);
emit countChanged(m_torrents.count());
emit endInsertRows();
return true;
}
QSharedPointer<QTorrentObject> QTorrentListModel::get(int index)
{
// Boilerplate bounds checking on the index (copied from data())
int row = index;
if (row < 0 || row >= m_torrents.count()) {
return QSharedPointer<QTorrentObject>();
}
return m_torrents.at(row);
}
bool QTorrentListModel::append(QSharedPointer<QTorrentObject> torrent)
{
insert(count(), torrent);
return true;
}
int QTorrentListModel::count()
{
return m_torrents.count();
}
void QTorrentListModel::notifyOfUpdate(QSharedPointer<QTorrentObject> torrent)
{
int collectionIndex = m_torrents.indexOf(torrent);
if (collectionIndex != -1) {
QModelIndex modelIndex = index(collectionIndex);
QModelIndex endIndex = index(m_torrents.count() - 1);
// Notify QML of changes
emit dataChanged(modelIndex, endIndex);
}
}
QHash<int, QByteArray> QTorrentListModel::roleNames() const {
return m_roleNames;
}
QSharedPointer<QTorrentObject> QTorrentListModel::lookupTorrent(const Argo::SHA1Hash &hash) {
QList<QSharedPointer<QTorrentObject>>::iterator it =
std::find_if(m_torrents.begin(), m_torrents.end(), [hash] (QSharedPointer<QTorrentObject> t) { return t->hash() == hash; } );
return it == m_torrents.end() ? QSharedPointer<QTorrentObject>() : (*it);
}