Small build script improvement.
[simple-x264-launcher.git] / src / win_updater.cpp
blobf34399a063bca7aedaa57c388a96d2a27f881436
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "win_updater.h"
23 #include "UIC_win_updater.h"
25 //Internal
26 #include "global.h"
27 #include "model_sysinfo.h"
29 //MUtils
30 #include <MUtils/UpdateChecker.h>
31 #include <MUtils/Hash.h>
32 #include <MUtils/GUI.h>
33 #include <MUtils/OSSupport.h>
34 #include <MUtils/Exception.h>
36 //Qt
37 #include <QMovie>
38 #include <QCloseEvent>
39 #include <QTimer>
40 #include <QMessageBox>
41 #include <QDesktopServices>
42 #include <QUrl>
43 #include <QProcess>
44 #include <QFileInfo>
45 #include <QDir>
46 #include <QMap>
47 #include <QElapsedTimer>
49 ///////////////////////////////////////////////////////////////////////////////
51 static const char *const DIGEST_KEY = "~Dv/bW3/7t>6?RXVwkaZk-hmS0#O4JS/5YQAO>\\8hvr0B~7[n!X~KMYruemu:MDq";
53 const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
55 { "curl.exe", "4d51862b5df757e177a578fd6fabf631b78d9eccedfb71249db0dfb5c1b721f59f4eb7f7046e1c1054b76b9af7a0bed2479b09b7599df51236aee42c3625e67c", 1 },
56 { "gpgv.exe", "d0869bd858294520c992b66e1c7594176ebfb51bd64c4f50f782a4749118498a29c9fc70da5ed08cda9837ff7099d950428ca9f1968fa69883929bd0dba8c9e5", 1 },
57 { "gpgv.gpg", "1a2f528e551b9abfb064f08674fdd421d3abe403469ddfee2beafd007775a6c684212a6274dc2b41a0b20dd5c2200021c91320e737f7a90b2ac5a40a6221d93f", 0 },
58 { "wupd.exe", "018a8d0d848407fb0cb530b4540c6f025fd4c280885becd37f83feed8aeb3af6f8e8e0d45066a36549efac7e64706ac1ef09aaa5c75ab8d12c4a70f41518a894", 1 },
59 { NULL, NULL, 0 }
62 #define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
63 #define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
65 #define SHOW_ANIMATION(FLAG) do \
66 { \
67 ui->frameAnimation->setVisible((FLAG)); \
68 ui->labelInfo->setVisible(!(FLAG)); \
69 ui->labelUrl->setVisible(!(FLAG)); \
70 ui->labelBuildNo->setVisible(!(FLAG)); \
71 if((FLAG)) m_animator->start(); else m_animator->stop(); \
72 } \
73 while(0)
75 static inline QString getBin(const QMap<QString, QSharedPointer<QFile>> &binaries, const QString &nameName)
77 const QSharedPointer<QFile> file = binaries.value(nameName);
78 return file.isNull() ? QString() : file->fileName();
81 static void qFileDeleter(QFile *const file)
83 if(file)
85 file->close();
86 delete file;
90 ///////////////////////////////////////////////////////////////////////////////
91 // Constructor & Destructor
92 ///////////////////////////////////////////////////////////////////////////////
94 UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
96 QDialog(parent),
97 ui(new Ui::UpdaterDialog()),
98 m_sysinfo(sysinfo),
99 m_updateUrl(updateUrl),
100 m_status(MUtils::UpdateChecker::UpdateStatus_NotStartedYet),
101 m_thread(NULL),
102 m_updaterProcess(NULL),
103 m_success(false),
104 m_firstShow(true)
106 //Init the dialog, from the .ui file
107 ui->setupUi(this);
108 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
110 //Scale and fix size
111 MUtils::GUI::scale_widget(this);
112 setFixedSize(size());
114 //Enable buttons
115 connect(ui->buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
116 connect(ui->buttonDownload, SIGNAL(clicked()), this, SLOT(installUpdate()));
117 connect(ui->buttonRetry, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
119 //Enable info label
120 connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
122 //Init animation
123 m_animator.reset(new QMovie(":/images/loading.gif"));
124 ui->labelLoadingCenter->setMovie(m_animator.data());
126 //Init buttons
127 ui->buttonCancel->setEnabled(false);
128 ui->buttonRetry->hide();
129 ui->buttonDownload->hide();
130 ui->labelCancel->hide();
132 //Start animation
133 SHOW_ANIMATION(true);
136 UpdaterDialog::~UpdaterDialog(void)
138 if(!m_thread.isNull())
140 if(!m_thread->wait(5000))
142 m_thread->terminate();
143 m_thread->wait();
146 delete ui;
149 ///////////////////////////////////////////////////////////////////////////////
150 // Events
151 ///////////////////////////////////////////////////////////////////////////////
153 bool UpdaterDialog::event(QEvent *e)
155 if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
157 MUtils::GUI::bring_to_front(m_updaterProcess);
159 return QDialog::event(e);
162 void UpdaterDialog::showEvent(QShowEvent *event)
164 if(m_firstShow)
166 m_firstShow = false;
167 QTimer::singleShot(16, this, SLOT(initUpdate()));
171 void UpdaterDialog::closeEvent(QCloseEvent *e)
173 if(!ui->buttonCancel->isEnabled())
175 e->ignore();
179 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
181 switch (event->key())
183 case Qt::Key_Escape:
184 if ((!m_thread.isNull()) && m_thread->isRunning())
186 if (m_status >= MUtils::UpdateChecker::UpdateStatus_FetchingUpdates)
188 UPDATE_TEXT(2, tr("Cancellation requested..."));
190 else
192 UPDATE_TEXT(1, tr("Cancellation requested..."));
194 m_thread->cancel();
196 break;
197 case Qt::Key_F11:
199 const QString logFilePath = MUtils::make_temp_file(MUtils::temp_folder(), "txt", true);
200 if (!logFilePath.isEmpty())
202 qWarning("Write log to: '%s'", MUTILS_UTF8(logFilePath));
203 QFile logFile(logFilePath);
204 if (logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
206 logFile.write("\xEF\xBB\xBF");
207 for (QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
209 logFile.write(iter->toUtf8());
210 logFile.write("\r\n");
212 logFile.close();
213 QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
217 break;
218 default:
219 QDialog::keyPressEvent(event);
223 ///////////////////////////////////////////////////////////////////////////////
224 // Slots
225 ///////////////////////////////////////////////////////////////////////////////
227 void UpdaterDialog::initUpdate(void)
229 //Check binary files
230 if(!checkBinaries())
232 ui->buttonCancel->setEnabled(true);
233 const QString message = QString("%1<br><br><nobr><a href=\"%2\">%3</a></nobr><br>").arg(tr("At least one file required by the web-update tool is missing or corrupted.<br>Please re-install this application and then try again!"), QString::fromLatin1(m_updateUrl), QString::fromLatin1(m_updateUrl).replace("-", "&minus;"));
234 if(QMessageBox::critical(this, tr("File Error"), message, tr("Download Latest Version"), tr("Discard")) == 0)
236 QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
238 close();
239 return;
242 //Make sure user does have admin access
243 if(!MUtils::OS::user_is_admin())
245 qWarning("User is not in the \"admin\" group, cannot update!");
246 QString message;
247 message += QString("<nobr>%1</nobr><br>").arg(tr("Sorry, but only users in the \"Administrators\" group can install updates."));
248 message += QString("<nobr>%1</nobr>").arg(tr("Please start application from an administrator account and try again!"));
249 if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
251 ui->buttonCancel->setEnabled(true);
252 close();
253 return;
257 //Create and setup thread
258 if(!m_thread)
260 m_thread.reset(new MUtils::UpdateChecker(getBin(m_binaries, "curl.exe"), getBin(m_binaries, "gpgv.exe"), getBin(m_binaries, "gpgv.gpg"), "Simple x264 Launcher", x264_version_build(), false));
261 connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
262 connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
263 connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
264 connect(m_thread.data(), SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
267 //Begin updater run
268 QTimer::singleShot(16, this, SLOT(checkForUpdates()));
271 void UpdaterDialog::checkForUpdates(void)
273 if((!m_thread) || m_thread->isRunning())
275 qWarning("Update in progress, cannot check for updates now!");
278 //Clear texts
279 ui->retranslateUi(this);
280 ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), tr("N/A")));
282 //Init buttons
283 ui->buttonCancel->setEnabled(false);
284 ui->buttonRetry->hide();
285 ui->buttonDownload->hide();
287 //Hide labels
288 ui->labelInfo->hide();
289 ui->labelUrl->hide();
290 ui->labelCancel->show();
292 //Update status
293 threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
295 //Start animation
296 SHOW_ANIMATION(true);
298 //Update cursor
299 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
300 QApplication::setOverrideCursor(Qt::WaitCursor);
302 //Clear log
303 m_logFile.clear();
305 //Init timer
306 m_elapsed.reset(new QElapsedTimer());
307 m_elapsed->start();
309 //Start the updater thread
310 QTimer::singleShot(125, m_thread.data(), SLOT(start()));
313 void UpdaterDialog::threadStatusChanged(int status)
315 const int prevStatus = m_status;
316 switch(m_status = status)
318 case MUtils::UpdateChecker::UpdateStatus_NotStartedYet:
319 UPDATE_ICON(1, "clock");
320 UPDATE_ICON(2, "clock");
321 UPDATE_ICON(3, "clock");
322 break;
323 case MUtils::UpdateChecker::UpdateStatus_CheckingConnection:
324 UPDATE_ICON(1, "play");
325 break;
326 case MUtils::UpdateChecker::UpdateStatus_FetchingUpdates:
327 UPDATE_ICON(1, "shield_green");
328 UPDATE_TEXT(1, tr("Internet connection is working."));
329 UPDATE_ICON(2, "play");
330 break;
331 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
332 UPDATE_ICON(1, "shield_error");
333 UPDATE_TEXT(1, tr("Computer is currently offline!"));
334 UPDATE_ICON(2, "shield_grey");
335 UPDATE_ICON(3, "shield_grey");
336 break;
337 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
338 UPDATE_ICON(1, "shield_error");
339 UPDATE_TEXT(1, tr("Internet connectivity test failed!"));
340 UPDATE_ICON(2, "shield_grey");
341 UPDATE_ICON(3, "shield_grey");
342 break;
343 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
344 UPDATE_ICON(2, "shield_error");
345 UPDATE_TEXT(2, tr("Failed to download the update information!"));
346 UPDATE_ICON(3, "shield_grey");
347 break;
348 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
349 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
350 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
351 UPDATE_ICON(2, "shield_green");
352 UPDATE_TEXT(2, tr("Update information received successfully."));
353 UPDATE_ICON(3, "play");
354 break;
355 case MUtils::UpdateChecker::UpdateStatus_CancelledByUser:
356 if (prevStatus >= MUtils::UpdateChecker::UpdateStatus_FetchingUpdates)
358 UPDATE_ICON(2, "shield_error");
359 UPDATE_TEXT(2, tr("Operation was cancelled by the user!"));
360 UPDATE_ICON(3, "shield_grey");
362 else
364 UPDATE_ICON(1, "shield_error");
365 UPDATE_TEXT(1, tr("Operation was cancelled by the user!"));
366 UPDATE_ICON(2, "shield_grey");
367 UPDATE_ICON(3, "shield_grey");
369 break;
370 default:
371 MUTILS_THROW("Unknown status code!");
375 void UpdaterDialog::threadFinished(void)
377 m_success = m_thread->getSuccess();
378 ui->labelCancel->hide();
379 QTimer::singleShot((m_success ? 500 : 0), this, SLOT(updateFinished()));
382 void UpdaterDialog::updateFinished(void)
384 //Query the timer, if available
385 if (!m_elapsed.isNull())
387 const quint64 elapsed = m_elapsed->restart();
388 qDebug("Update check completed after %.2f seconds.", double(elapsed) / 1000.0);
391 //Restore cursor
392 QApplication::restoreOverrideCursor();
394 //If update was successfull, process final updater state
395 if(m_thread->getSuccess())
397 switch(m_status)
399 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
400 UPDATE_ICON(3, "shield_exclamation");
401 UPDATE_TEXT(3, tr("A newer version is available!"));
402 ui->buttonDownload->show();
403 break;
404 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
405 UPDATE_ICON(3, "shield_green");
406 UPDATE_TEXT(3, tr("Your version is up-to-date."));
407 break;
408 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
409 UPDATE_ICON(3, "shield_blue");
410 UPDATE_TEXT(3, tr("You are using a pre-release version!"));
411 break;
412 default:
413 qWarning("Update thread succeeded with unexpected status code: %d", m_status);
417 //Show update info or retry button
418 switch(m_status)
420 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
421 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
422 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
423 SHOW_ANIMATION(false);
424 ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), QString::number(m_thread->getUpdateInfo()->getBuildNo())));
425 ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->getDownloadSite()));
426 break;
427 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
428 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
429 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
430 case MUtils::UpdateChecker::UpdateStatus_CancelledByUser:
431 m_animator->stop();
432 ui->buttonRetry->show();
433 break;
434 default:
435 qWarning("Update thread finished with unexpected status code: %d", m_status);
438 //Re-enbale cancel button
439 ui->buttonCancel->setEnabled(true);
443 void UpdaterDialog::threadMessageLogged(const QString &message)
445 m_logFile << message;
448 void UpdaterDialog::openUrl(const QString &url)
450 qDebug("Open URL: %s", url.toLatin1().constData());
451 QDesktopServices::openUrl(QUrl(url));
454 void UpdaterDialog::installUpdate(void)
456 if(!((m_thread) && m_thread->getSuccess()))
458 qWarning("Cannot download/install update at this point!");
459 return;
462 QApplication::setOverrideCursor(Qt::WaitCursor);
463 ui->buttonDownload->hide();
464 ui->buttonCancel->setEnabled(false);
465 SHOW_ANIMATION(true);
467 const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
469 QProcess process;
470 QStringList args;
471 QEventLoop loop;
473 MUtils::init_process(process, MUtils::temp_folder(), false);
475 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
476 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
478 args << QString("/Location=%1").arg(updateInfo->getDownloadAddress());
479 args << QString("/Filename=%1").arg(updateInfo->getDownloadFilename());
480 args << QString("/TicketID=%1").arg(updateInfo->getDownloadFilecode());
481 args << QString("/CheckSum=%1").arg(updateInfo->getDownloadChecksum());
482 args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
483 args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
484 args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
486 process.start(getBin(m_binaries, "wupd.exe"), args);
487 if(!process.waitForStarted())
489 QApplication::restoreOverrideCursor();
490 SHOW_ANIMATION(false);
491 QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!"));
492 ui->buttonDownload->show();
493 ui->buttonCancel->setEnabled(true);
494 return;
497 m_updaterProcess = MUtils::OS::process_id(&process);
498 loop.exec(QEventLoop::ExcludeUserInputEvents);
500 if(!process.waitForFinished())
502 process.kill();
503 process.waitForFinished();
506 m_updaterProcess = NULL;
507 QApplication::restoreOverrideCursor();
508 ui->buttonDownload->show();
509 ui->buttonCancel->setEnabled(true);
510 SHOW_ANIMATION(false);
512 if(process.exitCode() == 0)
514 done(READY_TO_INSTALL_UPDATE);
518 ///////////////////////////////////////////////////////////////////////////////
519 // Private Functions
520 ///////////////////////////////////////////////////////////////////////////////
522 bool UpdaterDialog::checkBinaries(void)
524 qDebug("[File Verification]");
525 for(size_t i = 0; BINARIES[i].name; i++)
527 const QString name = QString::fromLatin1(BINARIES[i].name);
528 if (!m_binaries.contains(name))
530 QScopedPointer<QFile> binary(new QFile(QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), name)));
531 if (binary->open(QIODevice::ReadOnly))
533 if (checkFileHash(binary->fileName(), BINARIES[i].hash))
535 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
536 m_binaries.insert(name, QSharedPointer<QFile>(binary.take(), qFileDeleter));
538 else
540 qWarning("Verification of '%s' has failed!", MUTILS_UTF8(name));
541 binary->close();
542 return false;
545 else
547 qWarning("File '%s' could not be opened!", MUTILS_UTF8(name));
548 return false;
552 qDebug("File check completed.\n");
553 return true;
556 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
558 qDebug("Checking file: %s", MUTILS_UTF8(filePath));
559 QScopedPointer<MUtils::Hash::Hash> checksum(MUtils::Hash::create(MUtils::Hash::HASH_BLAKE2_512, DIGEST_KEY));
560 QFile file(filePath);
561 if(file.open(QIODevice::ReadOnly))
563 checksum->update(file);
564 const QByteArray fileHash = checksum->digest();
565 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
567 qWarning("\nFile appears to be corrupted:\n%s\n", filePath.toUtf8().constData());
568 qWarning("Expected Hash: %s\nDetected Hash: %s\n", expectedHash, fileHash.constData());
569 return false;
571 return true;
573 else
575 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
576 return false;