]> git.sur5r.net Git - minitube/blob - lib/updater/src/impl/defaultupdater.cpp
New upstream version 3.8
[minitube] / lib / updater / src / impl / defaultupdater.cpp
1 #include "defaultupdater.h"
2
3 #include "checker.h"
4 #include "dialog.h"
5 #include "downloader.h"
6 #include "installer.h"
7 #include "openinstaller.h"
8 #include "parser.h"
9
10 namespace updater {
11
12 DefaultUpdater::DefaultUpdater() {
13     checkTimer = new QTimer(this);
14     checkTimer->setInterval(checkInterval);
15     checkTimer->setTimerType(Qt::VeryCoarseTimer);
16     auto autoCheck = [this] {
17         // auto check after interval
18         qint64 lastCheck = Checker::getLastCheck();
19         int secondsSinceLastCheck = QDateTime::currentSecsSinceEpoch() - lastCheck;
20         if (secondsSinceLastCheck >= checkInterval) {
21             auto checker = new Checker(this);
22             connect(checker, &Checker::done, this, [this, checker] {
23                 if (!getAutomaticDownload() && getStatus() != Updater::Status::UpToDate) {
24                     showDialog();
25                 }
26                 checker->deleteLater();
27             });
28             checker->check();
29         }
30     };
31     connect(checkTimer, &QTimer::timeout, this, autoCheck);
32     QTimer::singleShot(5000, autoCheck);
33     checkTimer->start();
34 }
35
36 void DefaultUpdater::setCheckInterval(const qint64 &value) {
37     checkInterval = value;
38     checkTimer->setInterval(checkInterval);
39 }
40
41 void DefaultUpdater::setInstaller(Installer *value) {
42     installer = value;
43     installer->setUpdater(this);
44 }
45
46 void DefaultUpdater::setParser(Parser *value) {
47     parser = value;
48     parser->setUpdater(this);
49 }
50
51 void DefaultUpdater::checkAndShowUI() {
52     auto checker = new Checker(this);
53     connect(checker, &Checker::done, this, [this, checker] {
54         if (getStatus() == Updater::Status::UpToDate) {
55             QMessageBox msgBox(qApp->activeWindow());
56             msgBox.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
57             msgBox.setWindowModality(Qt::WindowModal);
58             msgBox.setIconPixmap(getIcon().pixmap(64, 64));
59             msgBox.setText(tr("There are currently no updates available."));
60             msgBox.exec();
61         } else {
62             if (getStatus() != Updater::Status::UpdateAvailable) {
63                 downloadUpdate();
64             }
65             showDialog();
66         }
67
68         checker->deleteLater();
69     });
70     checker->check();
71 }
72
73 void DefaultUpdater::checkAndMaybeShowUI() {
74     auto checker = new Checker(this);
75     connect(checker, &Checker::done, this, [this, checker] {
76         if (getStatus() != Updater::Status::UpToDate) {
77             showDialog();
78         }
79         checker->deleteLater();
80     });
81     checker->check();
82 }
83
84 void DefaultUpdater::checkWithoutUI() {
85     auto checker = new Checker(this);
86     connect(checker, &Checker::done, this, [checker] { checker->deleteLater(); });
87     checker->check();
88 }
89
90 void DefaultUpdater::update() {
91     if (!installer) {
92         installer = new OpenInstaller();
93         installer->setUpdater(this);
94     }
95     connect(installer, &Installer::error, this, [](auto message) { qWarning() << message; });
96     installer->start(downloadedFilename);
97 }
98
99 Downloader *DefaultUpdater::downloadUpdate() {
100     if (downloader) return downloader;
101     if (!downloadedFilename.isEmpty()) {
102         qDebug() << "Update already downloded";
103     }
104
105     setStatus(Updater::Status::DownloadingUpdate);
106
107     downloader = new Downloader();
108     connect(downloader, &Downloader::fileReady, [this](auto filename) {
109         downloader->deleteLater();
110         downloader = nullptr;
111         downloadedFilename = filename;
112
113         if (getImmediateInstallAndRelaunch()) {
114             update();
115             qApp->quit();
116         } else {
117             connect(qApp, &QCoreApplication::aboutToQuit, this, [this] { update(); });
118         }
119
120         setStatus(Updater::Status::UpdateDownloaded);
121     });
122
123     connect(downloader, &Downloader::error, [this](auto message) {
124         qWarning() << message;
125         downloader->deleteLater();
126         downloader = nullptr;
127         downloadedFilename.clear();
128         setStatus(Updater::Status::UpdateDownloadFailed);
129     });
130
131     connect(downloader, &Downloader::progress,
132             [](int percent) { qDebug() << QString("Downloading update %1%").arg(percent); });
133
134     downloader->download(downloadUrl);
135
136     return downloader;
137 }
138
139 void DefaultUpdater::showDialog() {
140     if (!dialog) {
141         dialog = new Dialog(this, qApp->activeWindow());
142         connect(dialog, &QWidget::destroyed, this, [this] { dialog = nullptr; });
143     }
144     dialog->show();
145 }
146
147 } // namespace updater