forked from gyunaev/birdtray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatedownloaddialog.cpp
95 lines (86 loc) · 2.75 KB
/
updatedownloaddialog.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
#include "updatedownloaddialog.h"
#include "ui_updatedownloaddialog.h"
#ifdef Q_OS_WIN
# include <QtWinExtras/QWinTaskbarProgress>
#endif
UpdateDownloadDialog::UpdateDownloadDialog(QWidget* parent) :
QDialog(parent),
ui(new Ui::UpdateDownloadDialog) {
#ifdef Q_OS_WIN
taskBarButton = new QWinTaskbarButton(this);
#endif
ui->setupUi(this);
actionButton = new QPushButton(tr("Background"), this);
ui->buttonBox->addButton(actionButton, QDialogButtonBox::ButtonRole::ActionRole);
connect(actionButton, &QPushButton::clicked, this, &UpdateDownloadDialog::onActionPressed);
ui->buttonBox->addButton(QDialogButtonBox::StandardButton::Cancel);
setResult(Accepted);
}
UpdateDownloadDialog::~UpdateDownloadDialog() {
#ifdef Q_OS_WIN
delete taskBarButton;
#endif
delete ui;
actionButton->deleteLater();
}
void UpdateDownloadDialog::reset() {
downloadCompleted = false;
setResult(Accepted);
ui->label->setText(tr("Downloading Birdtray installer..."));
ui->progressBar->setValue(0);
ui->progressBar->show();
#ifdef Q_OS_WIN
taskBarButton->progress()->reset();
taskBarButton->progress()->show();
#endif
}
void UpdateDownloadDialog::onDownloadComplete() {
downloadCompleted = true;
ui->progressBar->hide();
#ifdef Q_OS_WIN
taskBarButton->progress()->setRange(0, 100);
taskBarButton->progress()->setValue(100);
#endif
actionButton->setText( QCoreApplication::translate("UpdateDialog", "Update and restart"));
ui->label->setText(tr("Download finished. Restart and update Birdtray?"));
show();
raise();
activateWindow();
}
void UpdateDownloadDialog::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
if (bytesTotal <= 0) {
ui->label->setText(tr("Downloading Birdtray installer..."));
ui->progressBar->setRange(0, 0);
#ifdef Q_OS_WIN
taskBarButton->progress()->setRange(0, 0);
#endif
return;
}
ui->label->setText(
tr("Downloading Birdtray installer... (%1 Mb / %2 Mb).")
.arg(qRound(bytesReceived / 1000000.0))
.arg(qRound(bytesTotal / 1000000.0)));
int percent = qRound((bytesReceived / (double) bytesTotal) * 100.0);
ui->progressBar->setRange(0, 100);
ui->progressBar->setValue(percent);
#ifdef Q_OS_WIN
taskBarButton->progress()->setRange(0, 100);
taskBarButton->progress()->setValue(percent);
#endif
}
bool UpdateDownloadDialog::wasCanceled() const {
return result() == Rejected;
}
void UpdateDownloadDialog::onActionPressed() {
if (downloadCompleted) {
accept();
} else {
hide();
}
}
void UpdateDownloadDialog::showEvent(QShowEvent* event) {
QDialog::showEvent(event);
#ifdef Q_OS_WIN
taskBarButton->setWindow(windowHandle());
#endif
}