///////////////////////////////////////////////////////////////////////////////
// Simple x264 Launcher
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
///////////////////////////////////////////////////////////////////////////////
#include "win_updater.h"
-#include "uic_win_updater.h"
+#include "UIC_win_updater.h"
+//Internal
#include "global.h"
#include "model_sysinfo.h"
-#include "thread_updater.h"
-#include "checksum.h"
+//MUtils
+#include <MUtils/UpdateChecker.h>
+#include <MUtils/Hash.h>
+#include <MUtils/GUI.h>
+#include <MUtils/OSSupport.h>
+#include <MUtils/Exception.h>
+
+//Qt
#include <QMovie>
#include <QCloseEvent>
#include <QTimer>
///////////////////////////////////////////////////////////////////////////////
+static const char *const DIGEST_KEY = "~Dv/bW3/7t>6?RXVwkaZk-hmS0#O4JS/5YQAO>\\8hvr0B~7[n!X~KMYruemu:MDq";
+
+const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
+{
+ { "wget.exe", "35d70bf8a1799956b5de3975ff99088a4444a2d17202059afb63949b297e2cc81e5e49e2b95df1c4e26b49ab7430399c293bf805a0b250d686c6f4dd994a0764", 1 },
+ { "netc.exe", "2e890533473134e074d65e1670c4212a06e4f685c4ee202593b0427defdf2f01e4d22cf7b48855436f1c57109c131f3469daa1a523a5a05fc9d23fb41d2e6ea9", 1 },
+ { "gpgv.exe", "a8d4d1702e5fb1eee5a2c22fdaf255816a9199ae48142aeec1c8ce16bbcf61d6d634f1e769e62d05cf52c204ba2611f09c9bb661bc6688b937749d478af3e47d", 1 },
+ { "gpgv.gpg", "1a2f528e551b9abfb064f08674fdd421d3abe403469ddfee2beafd007775a6c684212a6274dc2b41a0b20dd5c2200021c91320e737f7a90b2ac5a40a6221d93f", 0 },
+ { "wupd.exe", "c7fe72259ae781889a18f688321275e3bae39d75fb96c9c650446e177cb3af3d3ea84db2c1590e44bc2440b2ea79f9684e3a14e47e57e6083ec6f98c5bf72a73", 1 },
+ { NULL, NULL, 0 }
+};
+
#define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
#define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
-/*
-ui->labelLoadingLeft->setVisible((FLAG)); \
-ui->labelLoadingCenter->setVisible((FLAG)); \
-ui->labelLoadingRight->setVisible((FLAG)); \
-*/
-
#define SHOW_ANIMATION(FLAG) do \
{ \
ui->frameAnimation->setVisible((FLAG)); \
// Constructor & Destructor
///////////////////////////////////////////////////////////////////////////////
-UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo)
+UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
:
QDialog(parent),
ui(new Ui::UpdaterDialog()),
m_sysinfo(sysinfo),
- m_status(UpdateCheckThread::UpdateStatus_NotStartedYet),
+ m_updateUrl(updateUrl),
+ m_status(MUtils::UpdateChecker::UpdateStatus_NotStartedYet),
m_thread(NULL),
m_updaterProcess(NULL),
m_success(false),
connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
//Init animation
- m_animator = new QMovie(":/images/loading.gif");
- ui->labelLoadingCenter->setMovie(m_animator);
+ m_animator.reset(new QMovie(":/images/loading.gif"));
+ ui->labelLoadingCenter->setMovie(m_animator.data());
//Init buttons
ui->buttonCancel->setEnabled(false);
UpdaterDialog::~UpdaterDialog(void)
{
- if(m_thread)
+ if(!m_thread.isNull())
{
- if(!m_thread->wait(1000))
+ if(!m_thread->wait(5000))
{
m_thread->terminate();
m_thread->wait();
}
}
- if((!m_keysFile.isEmpty()) && QFile::exists(m_keysFile))
- {
- QFile::setPermissions(m_keysFile, QFile::ReadOwner | QFile::WriteOwner);
- QFile::remove(m_keysFile);
- m_keysFile.clear();
- }
-
- X264_DELETE(m_thread);
- X264_DELETE(m_animator);
-
+ cleanFiles();
delete ui;
}
{
if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
{
- x264_bring_process_to_front(m_updaterProcess);
+ MUtils::GUI::bring_to_front(m_updaterProcess);
}
return QDialog::event(e);
}
{
if(event->key() == Qt::Key_F11)
{
- QFile logFile(QString("%1/%2.log").arg(x264_temp_directory(), x264_rand_str()));
- if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
+ const QString logFilePath = MUtils::make_temp_file(MUtils::temp_folder(), "log", true);
+ if (!logFilePath.isEmpty())
{
- logFile.write("\xEF\xBB\xBF");
- for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
+ QFile logFile(logFilePath);
+ if (logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
- logFile.write(iter->toUtf8());
- logFile.write("\r\n");
+ logFile.write("\xEF\xBB\xBF");
+ for (QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
+ {
+ logFile.write(iter->toUtf8());
+ logFile.write("\r\n");
+ }
+ logFile.close();
+ QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
}
- logFile.close();
- QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
}
}
}
void UpdaterDialog::initUpdate(void)
{
+ //Clean up files from previous attempt
+ if(!m_binaries.isEmpty())
+ {
+ cleanFiles();
+ }
+
//Check binary files
- QString wgetBin, gpgvBin;
- if(!checkBinaries(wgetBin, gpgvBin))
+ if(!checkBinaries())
{
ui->buttonCancel->setEnabled(true);
- QMessageBox::critical(this, tr("File Error"), tr("At least one file required by web-update is missing or corrupted.<br>Please re-install this application and then try again!"));
+ 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("-", "−"));
+ if(QMessageBox::critical(this, tr("File Error"), message, tr("Download Latest Version"), tr("Discard")) == 0)
+ {
+ QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
+ }
close(); return;
}
//Make sure user does have admin access
- if(!x264_user_is_admin())
+ if(!MUtils::OS::user_is_admin())
{
qWarning("User is not in the \"admin\" group, cannot update!");
QString message;
//Create and setup thread
if(!m_thread)
{
- m_thread = new UpdateCheckThread(wgetBin, gpgvBin, m_keysFile, false);
- connect(m_thread, SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
- connect(m_thread, SIGNAL(finished()), this, SLOT(threadFinished()));
- connect(m_thread, SIGNAL(terminated()), this, SLOT(threadFinished()));
- connect(m_thread, SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
+ m_thread.reset(new MUtils::UpdateChecker(m_binaries.value("wget.exe"), m_binaries.value("netc.exe"), m_binaries.value("gpgv.exe"), m_binaries.value("gpgv.gpg"), "Simple x264 Launcher", x264_version_build(), false));
+ connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
+ connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
+ connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
+ connect(m_thread.data(), SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
}
//Begin updater run
ui->labelUrl->hide();
//Update status
- threadStatusChanged(UpdateCheckThread::UpdateStatus_NotStartedYet);
+ threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
//Start animation
SHOW_ANIMATION(true);
m_logFile.clear();
//Start the updater thread
- QTimer::singleShot(250, m_thread, SLOT(start()));
+ QTimer::singleShot(250, m_thread.data(), SLOT(start()));
}
void UpdaterDialog::threadStatusChanged(int status)
{
switch(m_status = status)
{
- case UpdateCheckThread::UpdateStatus_NotStartedYet:
+ case MUtils::UpdateChecker::UpdateStatus_NotStartedYet:
UPDATE_ICON(1, "clock");
UPDATE_ICON(2, "clock");
UPDATE_ICON(3, "clock");
break;
- case UpdateCheckThread::UpdateStatus_CheckingConnection:
+ case MUtils::UpdateChecker::UpdateStatus_CheckingConnection:
UPDATE_ICON(1, "play");
break;
- case UpdateCheckThread::UpdateStatus_FetchingUpdates:
+ case MUtils::UpdateChecker::UpdateStatus_FetchingUpdates:
UPDATE_ICON(1, "shield_green");
UPDATE_TEXT(1, tr("Internet connection is working."));
UPDATE_ICON(2, "play");
break;
- case UpdateCheckThread::UpdateStatus_ErrorNoConnection:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
UPDATE_ICON(1, "shield_error");
UPDATE_TEXT(1, tr("Computer is currently offline!"));
UPDATE_ICON(2, "shield_grey");
UPDATE_ICON(3, "shield_grey");
break;
- case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
UPDATE_ICON(1, "shield_error");
UPDATE_TEXT(1, tr("Internet connectivity test failed!"));
UPDATE_ICON(2, "shield_grey");
UPDATE_ICON(3, "shield_grey");
break;
- case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
UPDATE_ICON(2, "shield_error");
UPDATE_TEXT(2, tr("Failed to download the update information!"));
UPDATE_ICON(3, "shield_grey");
break;
- case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
- case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
- case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
UPDATE_ICON(2, "shield_green");
UPDATE_TEXT(2, tr("Update information received successfully."));
UPDATE_ICON(3, "play");
break;
default:
- throw "Unknown status code!";
+ MUTILS_THROW("Unknown status code!");
}
}
{
switch(m_status)
{
- case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
UPDATE_ICON(3, "shield_exclamation");
UPDATE_TEXT(3, tr("A newer version is available!"));
ui->buttonDownload->show();
break;
- case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
UPDATE_ICON(3, "shield_green");
UPDATE_TEXT(3, tr("Your version is up-to-date."));
break;
- case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
UPDATE_ICON(3, "shield_blue");
UPDATE_TEXT(3, tr("You are using a pre-release version!"));
break;
//Show update info or retry button
switch(m_status)
{
- case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
- case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
- case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
+ case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
SHOW_ANIMATION(false);
- ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), QString::number(m_thread->getUpdateInfo()->m_buildNo)));
- ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->m_downloadSite));
+ ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), QString::number(m_thread->getUpdateInfo()->getBuildNo())));
+ ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->getDownloadSite()));
break;
- case UpdateCheckThread::UpdateStatus_ErrorNoConnection:
- case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed:
- case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
+ case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
m_animator->stop();
ui->buttonRetry->show();
break;
ui->buttonCancel->setEnabled(false);
SHOW_ANIMATION(true);
- const UpdateInfo *updateInfo = m_thread->getUpdateInfo();
+ const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
QProcess process;
QStringList args;
QEventLoop loop;
- x264_init_process(process, x264_temp_directory(), false);
+ MUtils::init_process(process, MUtils::temp_folder(), false);
connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
- args << QString("/Location=%1").arg(updateInfo->m_downloadAddress);
- args << QString("/Filename=%1").arg(updateInfo->m_downloadFilename);
- args << QString("/TicketID=%1").arg(updateInfo->m_downloadFilecode);
- args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
+ args << QString("/Location=%1").arg(updateInfo->getDownloadAddress());
+ args << QString("/Filename=%1").arg(updateInfo->getDownloadFilename());
+ args << QString("/TicketID=%1").arg(updateInfo->getDownloadFilecode());
+ args << QString("/CheckSum=%1").arg(updateInfo->getDownloadChecksum());
+ args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
- args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->m_buildNo));
+ args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
- process.start(m_wupdFile, args);
+ process.start(m_binaries.value("wupd.exe"), args);
if(!process.waitForStarted())
{
QApplication::restoreOverrideCursor();
return;
}
- m_updaterProcess = x264_process_id(process);
+ m_updaterProcess = MUtils::OS::process_id(&process);
loop.exec(QEventLoop::ExcludeUserInputEvents);
if(!process.waitForFinished())
// Private Functions
///////////////////////////////////////////////////////////////////////////////
-bool UpdaterDialog::checkBinaries(QString &wgetBin, QString &gpgvBin)
+bool UpdaterDialog::checkBinaries(void)
{
qDebug("[File Verification]");
+ m_binaries.clear();
- static struct
+ //Validate hashes first
+ const QString tempPath = MUtils::temp_folder();
+ for(size_t i = 0; BINARIES[i].name; i++)
{
- const char* name;
- const char* hash;
- }
- FILE_INFO[] =
- {
- { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e" },
- { "gpgv.exe", "b42b7ef5650cd78d92773f03d4eefc90d9ba6ffe6af19d389851e32b5ab1c58c91c3dfceb2cbe0d0d13774ee2cf100c20f0add7f33463229999da5aaa861f064" },
- { "gpgv.gpg", "58e0f0e462bbd0b5aa4f638801c1097da7da4b3eb38c8c88ad1db23705c0f11e174b083fa55fe76bd3ba196341c967833a6f3427d6f63ad8565900745535d8fa" },
- { "wupd.exe", "e8ee5fb11e4964c0091311a41b46e2ea49cf675755ee830c38a26027c81aecc78842c25facc0ac6b797586e4c4b22ac116dd1735b0b11b67c13e4a17fb1e5f5e" },
- { NULL, NULL }
- };
-
- QMap<QString, QString> binaries;
-
- m_keysFile.clear();
- m_wupdFile.clear();
- wgetBin.clear();
- gpgvBin.clear();
-
- bool okay = true;
-
- for(size_t i = 0; FILE_INFO[i].name; i++)
- {
- const QString binPath = QString("%1/common/%2").arg(m_sysinfo->getAppPath(), QString::fromLatin1(FILE_INFO[i].name));
- if(okay = okay && checkFileHash(binPath, FILE_INFO[i].hash))
+ const QString orgName = QString::fromLatin1(BINARIES[i].name);
+ const QString binPath = QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), orgName);
+ const QString outPath = MUtils::make_unique_file(tempPath, QFileInfo(orgName).baseName(), QFileInfo(orgName).suffix());
+ if(!checkFileHash(binPath, BINARIES[i].hash))
{
- binaries.insert(FILE_INFO[i].name, binPath);
+ qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName));
+ return false;
}
- QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
- }
-
- if(okay)
- {
- wgetBin = binaries.value("wget.exe");
- gpgvBin = binaries.value("gpgv.exe");
-
- m_wupdFile = binaries.value("wupd.exe");
- m_keysFile = QString("%1/%2.gpg").arg(x264_temp_directory(), x264_rand_str());
-
- if(okay = QFile::copy(binaries.value("gpgv.gpg"), m_keysFile))
+ if(outPath.isEmpty() || (!QFile::copy(binPath, outPath)))
{
- QFile::setPermissions(m_keysFile, QFile::ReadOwner);
+ qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName));
+ return false;
}
- qDebug("%s\n", okay ? "Completed." : "Failed to copy GPG file!");
+ QFile::setPermissions(outPath, QFile::ReadOwner);
+ m_binaries.insert(BINARIES[i].name, outPath);
+ QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
- return okay;
+ return true;
}
bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
{
qDebug("Checking file: %s", filePath.toUtf8().constData());
- QBlake2Checksum checksum2;
+ QScopedPointer<MUtils::Hash::Hash> checksum(MUtils::Hash::create(MUtils::Hash::HASH_BLAKE2_512, DIGEST_KEY));
QFile file(filePath);
if(file.open(QIODevice::ReadOnly))
{
- checksum2.update(file);
- const QByteArray fileHash = checksum2.finalize();
+ checksum->update(file);
+ const QByteArray fileHash = checksum->digest();
if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
{
qWarning("\nFile appears to be corrupted:\n%s\n", filePath.toUtf8().constData());
return false;
}
}
+
+void UpdaterDialog::cleanFiles(void)
+{
+ const QStringList keys = m_binaries.keys();
+ foreach(const QString &key, keys)
+ {
+ const QString fileName = m_binaries.value(key);
+ QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
+ if(!QFile::remove(fileName))
+ {
+ qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName));
+ }
+ m_binaries.remove(key);
+ }
+}