1 #include "defaultupdater.h"
5 #include "downloader.h"
7 #include "openinstaller.h"
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) {
26 checker->deleteLater();
31 connect(checkTimer, &QTimer::timeout, this, autoCheck);
32 QTimer::singleShot(5000, autoCheck);
36 void DefaultUpdater::setCheckInterval(const qint64 &value) {
37 checkInterval = value;
38 checkTimer->setInterval(checkInterval);
41 void DefaultUpdater::setInstaller(Installer *value) {
43 installer->setUpdater(this);
46 void DefaultUpdater::setParser(Parser *value) {
48 parser->setUpdater(this);
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."));
62 if (getStatus() != Updater::Status::UpdateAvailable) {
68 checker->deleteLater();
73 void DefaultUpdater::checkAndMaybeShowUI() {
74 auto checker = new Checker(this);
75 connect(checker, &Checker::done, this, [this, checker] {
76 if (getStatus() != Updater::Status::UpToDate) {
79 checker->deleteLater();
84 void DefaultUpdater::checkWithoutUI() {
85 auto checker = new Checker(this);
86 connect(checker, &Checker::done, this, [checker] { checker->deleteLater(); });
90 void DefaultUpdater::update() {
92 installer = new OpenInstaller();
93 installer->setUpdater(this);
95 connect(installer, &Installer::error, this, [](auto message) { qWarning() << message; });
96 installer->start(downloadedFilename);
99 Downloader *DefaultUpdater::downloadUpdate() {
100 if (downloader) return downloader;
101 if (!downloadedFilename.isEmpty()) {
102 qDebug() << "Update already downloded";
105 setStatus(Updater::Status::DownloadingUpdate);
107 downloader = new Downloader();
108 connect(downloader, &Downloader::fileReady, [this](auto filename) {
109 downloader->deleteLater();
110 downloader = nullptr;
111 downloadedFilename = filename;
113 if (getImmediateInstallAndRelaunch()) {
117 connect(qApp, &QCoreApplication::aboutToQuit, this, [this] { update(); });
120 setStatus(Updater::Status::UpdateDownloaded);
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);
131 connect(downloader, &Downloader::progress,
132 [](int percent) { qDebug() << QString("Downloading update %1%").arg(percent); });
134 downloader->download(downloadUrl);
139 void DefaultUpdater::showDialog() {
141 dialog = new Dialog(this, qApp->activeWindow());
142 connect(dialog, &QWidget::destroyed, this, [this] { dialog = nullptr; });
147 } // namespace updater