-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrentfilesmodel.cpp
300 lines (253 loc) · 7.39 KB
/
torrentfilesmodel.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// SPDX-FileCopyrightText: 2024 Nick Korotysh <[email protected]>
//
// SPDX-License-Identifier: MIT
#include "torrentfilesmodel.hpp"
#include <QIcon>
#include <QMimeDatabase>
#include "utils.hpp"
namespace {
class FileIconProvider
{
public:
static FileIconProvider& instance();
QIcon icon(const QString& filename) const;
QIcon folder() const { return _folder_icon; }
QIcon unknown() const { return _unknown_icon; }
QString mimetype(const QString& filename) const;
protected:
FileIconProvider();
private:
QIcon _folder_icon;
QIcon _unknown_icon;
QMimeDatabase _mdb;
mutable QHash<QString, QIcon> _icons_cache;
};
QIcon pick_fallback_icon(const QString& name)
{
if (name == "application/vnd.efi.iso")
return QIcon::fromTheme("application-x-iso");
if (name.startsWith("application"))
return QIcon::fromTheme("application-octet-stream");
if (name.startsWith("audio"))
return QIcon::fromTheme("audio-x-generic");
if (name.startsWith("image"))
return QIcon::fromTheme("image-x-generic");
if (name.startsWith("package"))
return QIcon::fromTheme("package-x-generic");
if (name.startsWith("text"))
return QIcon::fromTheme("text-x-generic");
if (name.startsWith("video"))
return QIcon::fromTheme("video-x-generic");
return FileIconProvider::instance().unknown();
}
FileIconProvider& FileIconProvider::instance()
{
static FileIconProvider inst;
return inst;
}
QIcon FileIconProvider::icon(const QString& filename) const
{
auto ext = _mdb.suffixForFileName(filename);
if (ext.isEmpty()) {
return _unknown_icon;
}
if (auto iter = _icons_cache.find(ext); iter != _icons_cache.end()) {
return *iter;
}
auto types = _mdb.mimeTypesForFileName(filename);
QIcon ires;
if (types.empty()) {
ires = _unknown_icon;
} else {
const auto& mtype = types.first();
ires = QIcon::fromTheme(mtype.iconName(), pick_fallback_icon(mtype.name()));
}
_icons_cache[ext] = ires;
return ires;
}
QString FileIconProvider::mimetype(const QString& filename) const
{
auto types = _mdb.mimeTypesForFileName(filename);
return types.isEmpty() ? QString("unknown") : types.first().name();
}
FileIconProvider::FileIconProvider()
: _folder_icon(QIcon::fromTheme("folder"))
, _unknown_icon(QIcon::fromTheme("unknown"))
{}
FileNode* add_path(FileNode& root, QStringView path)
{
FileNode* node = &root;
const auto parts = path.split('/');
for (auto name : parts) {
if (auto iter = std::ranges::find(node->nodes(), name, &FileNode::name);
iter != node->nodes().end()) {
auto idx = std::distance(node->nodes().begin(), iter);
node = node->nodes()[idx].get();
} else {
node->addNode(std::make_unique<FileNode>(name.toString()));
node = node->nodes().back().get();
}
}
return node;
}
} // namespace
void FileNode::updateSize()
{
if (_nodes.empty()) {
return;
}
std::ranges::for_each(_nodes, &FileNode::updateSize);
auto get_size = [](auto&& n) { return n->size(); };
_file_size = std::transform_reduce(_nodes.begin(), _nodes.end(),
0LL, std::plus<>(), get_size);
}
TorrentFilesModel::TorrentFilesModel(QObject* parent)
: QAbstractItemModel(parent)
, _root(std::make_unique<FileNode>())
{
}
QVariant TorrentFilesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal) {
return QVariant();
}
if (role != Qt::DisplayRole) {
return QVariant();
}
switch (section) {
case 0:
return tr("Name");
case 1:
return tr("Size");
case 2:
return tr("Mime Type");
default:
break;
}
return QVariant();
}
QModelIndex TorrentFilesModel::index(int row, int column, const QModelIndex& parent) const
{
auto p = parent.isValid() ?
reinterpret_cast<FileNode*>(parent.internalPointer()) :
_root.get(); // top-level items have no parent index
Q_ASSERT(p);
if (row < 0 || row >= p->nodes().size()) { // out of range
return QModelIndex();
}
return createIndex(row, column, p->nodes()[row].get());
}
QModelIndex TorrentFilesModel::parent(const QModelIndex& index) const
{
if (!index.isValid()) { // invalid index
return QModelIndex();
}
// get parent node
auto p = reinterpret_cast<FileNode*>(index.internalPointer())->parent();
if (!p) { // root node has no parent
Q_ASSERT(reinterpret_cast<FileNode*>(index.internalPointer()) == _root.get());
return QModelIndex();
}
// top-level items should not have parent index
if (p == _root.get()) {
return QModelIndex();
}
// find the row index in parent's parent
auto pp = p->parent();
Q_ASSERT(pp); // all nodes at this point must have parent
auto iter = std::ranges::find(pp->nodes(), p, &std::unique_ptr<FileNode>::get);
Q_ASSERT(iter != pp->nodes().end());
int row = std::distance(pp->nodes().begin(), iter);
return createIndex(row, 0, p);
}
int TorrentFilesModel::rowCount(const QModelIndex& parent) const
{
if (!parent.isValid()) { // top-level items
return _root->nodes().size();
}
auto p = reinterpret_cast<FileNode*>(parent.internalPointer());
Q_ASSERT(p);
return p->nodes().size();
}
int TorrentFilesModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant TorrentFilesModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
auto node = reinterpret_cast<FileNode*>(index.internalPointer());
Q_ASSERT(node);
switch (index.column()) {
case 0: {
switch (role) {
case Qt::DecorationRole:
return node->nodes().empty() ?
FileIconProvider::instance().icon(node->name()) :
FileIconProvider::instance().folder();
case Qt::DisplayRole:
case Qt::EditRole:
return node->name();
default:
break;
}
break;
}
case 1: {
switch (role) {
case Qt::DisplayRole:
return human_size(node->size(), 1000);
case Qt::EditRole:
return node->size();
case Qt::TextAlignmentRole:
return static_cast<int>(Qt::AlignVCenter | Qt::AlignRight);
default:
break;
}
break;
}
case 2: {
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return node->nodes().empty() ?
FileIconProvider::instance().mimetype(node->name()) :
QString("-");
default:
break;
}
break;
}
default:
break;
}
return QVariant();
}
void TorrentFilesModel::setTorrentInfo(const libtorrent::torrent_info& ti)
{
beginResetModel();
_root = std::make_unique<FileNode>();
const auto& fst = ti.files();
for (auto i : fst.file_range()) {
add_path(*_root, QString::fromStdString(fst.file_path(i)))->setSize(fst.file_size(i));
}
_root->updateSize();
endResetModel();
}
bool SortDirsFirstProxyModel::lessThan(const QModelIndex& lhs, const QModelIndex& rhs) const
{
auto lnode = reinterpret_cast<FileNode*>(lhs.internalPointer());
auto rnode = reinterpret_cast<FileNode*>(rhs.internalPointer());
Q_ASSERT(lnode);
Q_ASSERT(rnode);
if (lnode->nodes().empty() != rnode->nodes().empty()) {
if (lnode->nodes().empty())
return sortOrder() != Qt::AscendingOrder;
if (rnode->nodes().empty())
return sortOrder() == Qt::AscendingOrder;
}
return QSortFilterProxyModel::lessThan(lhs, rhs);
}