1 #include "updater.h"
2
3 namespace {
4 Updater *sharedInstance = nullptr;
5 }
6
setInstance(Updater * value)7 void Updater::setInstance(Updater *value) {
8 sharedInstance = value;
9 }
10
instance()11 Updater &Updater::instance() {
12 return *sharedInstance;
13 }
14
Updater(QObject * parent)15 Updater::Updater(QObject *parent) : QObject(parent) {}
16
getAction()17 QAction *Updater::getAction() {
18 if (!action) {
19 action = new QAction(this);
20 connect(action, &QWidget::destroyed, this, [this] { action = nullptr; });
21 action->setMenuRole(QAction::ApplicationSpecificRole);
22 connect(action, &QAction::triggered, this, &Updater::onUserAction);
23 auto onStatusChange = [this](Updater::Status status) {
24 QString v = displayVersion.isEmpty() ? version : displayVersion;
25 QString t;
26 switch (status) {
27 case Updater::Status::UpToDate:
28 t = tr("Check for Updates...");
29 break;
30 case Updater::Status::UpdateAvailable:
31 t = tr("Version %1 is available...").arg(v);
32 break;
33 case Updater::Status::DownloadingUpdate:
34 t = tr("Downloading version %1...").arg(v);
35 break;
36 case Updater::Status::UpdateDownloaded:
37 t = tr("Restart to Update");
38 break;
39 case Updater::Status::UpdateDownloadFailed:
40 t = tr("Version %1 download failed").arg(v);
41 break;
42 }
43 action->setText(t);
44 };
45 connect(this, &Updater::statusChanged, this, onStatusChange);
46 onStatusChange(status);
47 }
48 return action;
49 }
50
getButton()51 QPushButton *Updater::getButton() {
52 if (!button) {
53 button = new QPushButton();
54 connect(button, &QWidget::destroyed, this, [this] { button = nullptr; });
55 connect(button, &QPushButton::clicked, this, &Updater::onUserAction);
56 auto onStatusChange = [this](Updater::Status status) {
57 QString t;
58 bool visible = true;
59 switch (status) {
60 case Updater::Status::UpToDate:
61 t = tr("Check for Updates");
62 break;
63 case Updater::Status::UpdateAvailable:
64 t = tr("Download Update");
65 break;
66 case Updater::Status::DownloadingUpdate:
67 t = tr("Downloading update...");
68 visible = false;
69 break;
70 case Updater::Status::UpdateDownloaded:
71 t = tr("Restart to Update");
72 break;
73 case Updater::Status::UpdateDownloadFailed:
74 t = tr("Retry Update Download");
75 break;
76 }
77 button->setText(t);
78 button->setVisible(visible);
79 };
80 connect(this, &Updater::statusChanged, this, onStatusChange);
81 onStatusChange(status);
82 }
83 return button;
84 }
85
getLabel()86 QLabel *Updater::getLabel() {
87 if (!label) {
88 label = new QLabel();
89 connect(label, &QWidget::destroyed, this, [this] { label = nullptr; });
90 auto onStatusChange = [this](Updater::Status status) {
91 QString v = displayVersion.isEmpty() ? version : displayVersion;
92 QString t;
93 switch (status) {
94 case Updater::Status::UpToDate:
95 t = tr("You have the latest version.");
96 break;
97 case Updater::Status::UpdateAvailable:
98 t = tr("Version %1 is available.").arg(v);
99 break;
100 case Updater::Status::DownloadingUpdate:
101 t = tr("Downloading update...");
102 break;
103 case Updater::Status::UpdateDownloaded:
104 t = tr("An update has been downloaded and is ready to be installed.");
105 break;
106 case Updater::Status::UpdateDownloadFailed:
107 t = tr("Version %1 download failed").arg(v);
108 break;
109 }
110 label->setText(t);
111 };
112 connect(this, &Updater::statusChanged, label, onStatusChange);
113 onStatusChange(status);
114 }
115 return label;
116 }
117
setStatus(Status v)118 void Updater::setStatus(Status v) {
119 if (status != v) {
120 status = v;
121 emit statusChanged(status);
122 }
123 }
124
onUserAction()125 void Updater::onUserAction() {
126 if (status == Updater::Status::UpdateDownloaded) {
127 // tell Installer we want the app to be restarted
128 setRelaunchAfterInstall(true);
129 // update will be installed on quit
130 qApp->quit();
131 return;
132 } else {
133 checkAndShowUI();
134 }
135 }
136