-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrentinfoview.cpp
117 lines (96 loc) · 3.26 KB
/
torrentinfoview.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
// SPDX-FileCopyrightText: 2024 Nick Korotysh <[email protected]>
//
// SPDX-License-Identifier: MIT
#include "torrentinfoview.hpp"
#include "ui_torrentinfoview.h"
#include <QDateTime>
#include "torrentfilesmodel.hpp"
#include "torrenttrackersmodel.hpp"
#include "utils.hpp"
namespace {
QString format_created(const lt::torrent_info& ti)
{
QString res;
if (ti.creation_date() != 0) {
res += QDateTime::fromSecsSinceEpoch(ti.creation_date()).toString();
}
if (ti.creation_date() != 0 && !ti.creator().empty()) {
res += ", ";
}
if (!ti.creator().empty()) {
res += QString::fromStdString(ti.creator());
}
return res;
}
QString format_hashes(const lt::torrent_info& ti)
{
QString res;
const auto& h = ti.info_hashes();
if (h.has_v1()) {
res += QByteArray(h.v1.data(), h.v1.size()).toHex();
}
if (h.has_v1() && h.has_v2()) {
res += "\n";
}
if (h.has_v2()) {
res += QByteArray(h.v2.data(), h.v2.size()).toHex();
}
return res;
}
QString format_pieces(const lt::torrent_info& ti)
{
return QString("%1 x %2 = %3")
.arg(ti.num_pieces())
.arg(human_size(ti.piece_length()))
.arg(human_size(ti.total_size()));
}
QString format_files(const lt::torrent_info& ti)
{
return QString::number(ti.num_files());
}
} // namespace
TorrentInfoView::TorrentInfoView(QWidget* parent)
: QWidget(parent)
, ui(new Ui::TorrentInfoView)
{
ui->setupUi(this);
files = new TorrentFilesModel(this);
auto sort_model = new SortDirsFirstProxyModel(this);
sort_model->setSourceModel(files);
sort_model->setSortRole(Qt::EditRole);
sort_model->setSortCaseSensitivity(Qt::CaseInsensitive);
ui->files_view->setModel(sort_model);
ui->files_view->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->files_view->header()->setSectionResizeMode(0, QHeaderView::Stretch);
trackers = new TorrentTrackersModel(this);
ui->trackers_list->setModel(trackers);
ui->trackers_list->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->splitter->setSizes({ui->trackers_list->minimumSizeHint().height(), height()});
}
TorrentInfoView::~TorrentInfoView()
{
delete ui;
}
void TorrentInfoView::setTorrentInfo(const libtorrent::torrent_info& ti)
{
ui->label_name_v->setText(QString::fromStdString(ti.name()));
ui->label_type_v->setText(ti.priv() ? tr("private") : tr("public"));
const bool has_comment = !ti.comment().empty();
ui->label_comment_s->setVisible(has_comment);
ui->label_comment_v->setVisible(has_comment);
ui->label_comment_v->setText(QString::fromStdString(ti.comment()));
const bool has_created = ti.creation_date() !=0 || !ti.creator().empty();
ui->label_created_s->setVisible(has_created);
ui->label_created_v->setVisible(has_created);
ui->label_created_v->setText(format_created(ti));
ui->label_info_hash_v->setText(format_hashes(ti));
ui->label_pieces_v->setText(format_pieces(ti));
ui->label_files_v->setText(format_files(ti));
trackers->setTorrentInfo(ti);
ui->trackers_list->setVisible(trackers->rowCount() > 0);
files->setTorrentInfo(ti);
if (ui->files_view->model()->rowCount(ui->files_view->rootIndex()) == 1) {
ui->files_view->expand(ui->files_view->model()->index(0, 0, ui->files_view->rootIndex()));
}
ui->files_view->sortByColumn(0, Qt::AscendingOrder);
}