-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrenttrackersmodel.cpp
82 lines (68 loc) · 1.82 KB
/
torrenttrackersmodel.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
// SPDX-FileCopyrightText: 2024 Nick Korotysh <[email protected]>
//
// SPDX-License-Identifier: MIT
#include "torrenttrackersmodel.hpp"
TorrentTrackersModel::TorrentTrackersModel(QObject* parent)
: QAbstractTableModel(parent)
{
}
int TorrentTrackersModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return _trackers.size() + _web_seeds.size();
}
int TorrentTrackersModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return 2;
}
QVariant TorrentTrackersModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
switch (index.column()) {
case 0: {
switch (role) {
case Qt::DisplayRole:
return index.row() < _trackers.size() ?
QString::number(_trackers[index.row()].first) : QString("WEB");
case Qt::TextAlignmentRole:
return static_cast<int>(Qt::AlignVCenter | Qt::AlignRight);
default:
break;
}
break;
}
case 1: {
switch (role) {
case Qt::DisplayRole:
return index.row() < _trackers.size() ?
_trackers[index.row()].second :
_web_seeds[index.row() - _trackers.size()];
default:
break;
}
break;
}
default:
break;
}
return QVariant();
}
void TorrentTrackersModel::setTorrentInfo(const libtorrent::torrent_info& ti)
{
beginResetModel();
_trackers.clear();
_web_seeds.clear();
_trackers.reserve(ti.trackers().size());
for (const auto& e : ti.trackers()) {
_trackers.emplace_back(e.tier, QString::fromStdString(e.url));
}
_web_seeds.reserve(ti.web_seeds().size());
for (const auto& e : ti.web_seeds()) {
_web_seeds.push_back(QString::fromStdString(e.url));
}
endResetModel();
}