forked from AdunanzA/Tsunami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemdetails.cpp
More file actions
279 lines (240 loc) · 8.2 KB
/
itemdetails.cpp
File metadata and controls
279 lines (240 loc) · 8.2 KB
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
#include "itemdetails.h"
#include "ui_itemdetails.h"
#include "mainwindow.h"
MainWindow *p_mw;
itemDetails::itemDetails(QWidget *parent) :
QDialog(parent),
ui(new Ui::itemDetails)
{
ui->setupUi(this);
p_mw = reinterpret_cast<MainWindow*>(parent->parent());
p_refreshTimer = new QTimer(this);
connect(p_refreshTimer, SIGNAL(timeout()), this, SLOT(timerTick()));
// p_refreshTimer->start(1000);
}
itemDetails::~itemDetails()
{
delete ui;
}
void itemDetails::setHash(const std::string &hash)
{
p_hash = std::string(hash);
lt::sha1_hash sh(p_hash);
lt::session *ses = p_mw->sessionManager->p_session.data();
th = QSharedPointer<lt::torrent_handle>::create(ses->find_torrent(sh));
lt::torrent_status ts = th.data()->status();
ui->lblTorrentName->setText(ts.name.c_str());
int pieceLength = -1;
int piecesCount = -1;
if (th.data()->torrent_file())
{
pieceLength = th.data()->torrent_file()->piece_length();
}
ui->txtPiecesLength->setText(QLocale().toString(pieceLength));
if (th.data()->torrent_file())
{
piecesCount = th.data()->torrent_file()->num_pieces();
}
ui->txtPiecesCount->setText(QLocale().toString(piecesCount));
ui->chkSequential->setChecked(ts.sequential_download);
update();
}
std::string itemDetails::getHash() const
{
return p_hash;
}
void itemDetails::update()
{
if (p_hash.empty() || !this->isVisible()) return;
if (!th.data()->is_valid()) {
this->close();
}
lt::torrent_status ts = th.data()->status();
bool isPaused = ts.paused && !ts.auto_managed;
float ratio = 0;
int eta = -1;
if (ts.all_time_download > 0)
{
ratio = (float)ts.all_time_upload / (float)ts.all_time_download;
}
if (!isPaused)
{
int64_t remaining_bytes = ts.total_wanted - ts.total_wanted_done;
if (remaining_bytes > 0 && ts.download_payload_rate > 0)
{
eta = (int)(remaining_bytes / ts.download_payload_rate);
}
}
std::vector<int64_t> progress;
th.data()->file_progress(progress, lt::torrent_handle::file_progress_flags_t::piece_granularity);
/* FILES LIST */
ui->tableFiles->setRowCount(0);
// ui->lblPieces->setPlaceholderText("");
if (!th.data()->torrent_file()) {
return;
}
lt::file_storage files = th.data()->torrent_file()->files();
for (int i = 0; i < files.num_files(); i++)
{
lt::file_index_t fi(i);
double prg = (double)((float)progress.at(i) / files.file_size(fi))*100;
ui->tableFiles->insertRow(i);
QTableWidgetItem *item1 = new QTableWidgetItem(QString(files.file_path(fi).c_str()));
QTableWidgetItem *item2 = new QTableWidgetItem(QString::number(th.data()->file_priority(fi)));
QTableWidgetItem *item3 = new QTableWidgetItem(QString("%0%").arg(QString::number(prg)));
QTableWidgetItem *item4 = new QTableWidgetItem(QLocale().toString(files.file_size(fi)));
ui->tableFiles->setItem(i, 0, item1);
ui->tableFiles->setItem(i, 1, item2);
ui->tableFiles->setItem(i, 2, item3);
ui->tableFiles->setItem(i, 3, item4);
}
// QString htmlOff = "<span style='background-color:#808080;font-size:7px;border:1px solid #999999'> </span>";
// QString htmlOff = "<img src='qrc:/images/htmlOff.png'></img>"; background-image: url(:/images/htmlOff.png);
// QString htmlOff = "<div style='background-image: url(:/images/htmlOff.png);background-repeat:no-repeat;background-position: center center;min-height:8px;min-width:8px'></div>";
// QString htmlOn = "<span style='background-color:#90EE90;font-size:7px;border:1px solid #999999'> </span>";
// QString htmlOn = "<img src='qrc:/images/htmlOn.png'></img>";
// QString htmlOn = "<span style='background-image: url(:/images/htmlOn.png);background-repeat:no-repeat;background-position: center center;min-height:8px;min-width:8px'></span>";
ui->listWidget->clear();
// QString result = "";
// ui->lblPieces->setHtml(result);
for (int j = 0; j < ts.pieces.size(); ++j) {
QListWidgetItem *item = new QListWidgetItem(" ", ui->listWidget);
item->setFont(QFont("Arial", 5));
item->setFlags(Qt::NoItemFlags | Qt::ItemNeverHasChildren);
lt::piece_index_t pi(j);
if (ts.pieces.get_bit(pi)) {
item->setBackground(QColor(144, 238, 144));
// result += htmlOn;
} else {
item->setBackground(QColor(128, 128, 128));
// result += htmlOff;;
}
}
// ui->lblPieces->setHtml(result);
/* STATE */
bool hasError = ts.paused && ts.errc;
bool isSeeding = (ts.state == lt::torrent_status::state_t::finished || ts.state == lt::torrent_status::state_t::seeding);
bool isQueued = (ts.paused && ts.auto_managed);
bool isChecking = (ts.state == lt::torrent_status::state_t::checking_files || ts.state == lt::torrent_status::state_t::checking_resume_data);
bool isForced = (!ts.paused && !ts.auto_managed);
QString state = "Torrent::State::Unknown";
if (isPaused)
{
if (hasError)
{
state = "Torrent::State::Error";
}
else
{
if (isSeeding)
{
state = "Torrent::State::UploadingPaused";
}
else
{
state = "Torrent::State::DownloadingPaused";
}
}
}
else
{
if (isQueued && !isChecking)
{
if (isSeeding)
{
state = "Torrent::State::UploadingQueued";
}
else
{
state = "Torrent::State::DownloadingQueued";
}
}
else
{
switch (ts.state)
{
case lt::torrent_status::state_t::finished:
case lt::torrent_status::state_t::seeding:
{
if (isForced)
{
state = "Torrent::State::UploadingForced";
}
else if (isPaused)
{
state = "Torrent::State::Complete";
}
else
{
if (ts.upload_payload_rate > 0)
{
state = "Torrent::State::Uploading";
}
else
{
state = "Torrent::State::UploadingStalled";
}
}
break;
}
case lt::torrent_status::state_t::checking_resume_data:
{
state = "Torrent::State::CheckingResumeData2";
break;
}
case lt::torrent_status::state_t::checking_files:
{
state = "Torrent::State::DownloadingChecking";
break;
}
case lt::torrent_status::state_t::downloading_metadata:
{
state = "Torrent::State::DownloadingMetadata";
break;
}
case lt::torrent_status::state_t::downloading:
{
if (isForced)
{
state = "Torrent::State::DownloadingForced";
}
else
{
if (ts.download_payload_rate > 0)
{
state = "Torrent::State::Downloading";
}
else
{
state = "Torrent::State::DownloadingStalled";
}
}
break;
}
}
}
}
ui->lblState->setText(state);
}
void itemDetails::timerTick()
{
update();
}
void itemDetails::closeEvent(QCloseEvent *event)
{
p_refreshTimer->stop();
event->accept();
}
void itemDetails::showEvent(QShowEvent *event)
{
lt::torrent_status ts = th.data()->status();
ui->chkSequential->setChecked(ts.sequential_download);
update();
p_refreshTimer->start(1000);
qDebug("showing details");
event->accept();
}
void itemDetails::on_chkSequential_toggled(bool checked)
{
th.data()->set_sequential_download(checked);
}