Fixed CQ and CRF mode being mixed up.
[simple-x264-launcher.git] / src / win_updater.cpp
blobfd84bf7a459a44ad58185eaf98edb9d9226f119e
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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_Blake2.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>
48 ///////////////////////////////////////////////////////////////////////////////
50 const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
52 { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e", 1 },
53 { "netc.exe", "c199ea12d761fa3191006da250f8f600ad426265fdf4a43e551cdf04a451a105692efd3ef82ac621c0799394aa21ac65bfbb4bab90c3fbb1f557e93f490fcb75", 1 },
54 { "gpgv.exe", "18c5456cbb9ebf5cb9012a939b199d9eaa71c92a39f574f1e032babad0bbd9e72a064af96ca9d3d01f2892b064ec239fd61f27bac2eb9a64f7b2ece7beea3158", 1 },
55 { "gpgv.gpg", "745c7a9c040196d9d322b1580e0046ff26ec13238cfd04325ceb3d4c8948294c593c027f895dc8ec427295175003e75d34f083019b706b0f4f06f81cce8df47d", 0 },
56 { "wupd.exe", "7eb8338efe0ddf973ee9fda5e5bfa4728876f2fcff2b3add30cb8c439d3d59a5de1a0636176c34be0b0da1a764363d0cfec014679749dc504999aa184f35dfd5", 1 },
57 { NULL, NULL, 0 }
60 #define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
61 #define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
63 #define SHOW_ANIMATION(FLAG) do \
64 { \
65 ui->frameAnimation->setVisible((FLAG)); \
66 ui->labelInfo->setVisible(!(FLAG)); \
67 ui->labelUrl->setVisible(!(FLAG)); \
68 ui->labelBuildNo->setVisible(!(FLAG)); \
69 if((FLAG)) m_animator->start(); else m_animator->stop(); \
70 } \
71 while(0)
74 ///////////////////////////////////////////////////////////////////////////////
75 // Constructor & Destructor
76 ///////////////////////////////////////////////////////////////////////////////
78 UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
80 QDialog(parent),
81 ui(new Ui::UpdaterDialog()),
82 m_sysinfo(sysinfo),
83 m_updateUrl(updateUrl),
84 m_status(MUtils::UpdateChecker::UpdateStatus_NotStartedYet),
85 m_thread(NULL),
86 m_updaterProcess(NULL),
87 m_success(false),
88 m_firstShow(true)
90 //Init the dialog, from the .ui file
91 ui->setupUi(this);
92 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
94 //Fix size
95 setFixedSize(size());
97 //Enable buttons
98 connect(ui->buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
99 connect(ui->buttonDownload, SIGNAL(clicked()), this, SLOT(installUpdate()));
100 connect(ui->buttonRetry, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
102 //Enable info label
103 connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
105 //Init animation
106 m_animator.reset(new QMovie(":/images/loading.gif"));
107 ui->labelLoadingCenter->setMovie(m_animator.data());
109 //Init buttons
110 ui->buttonCancel->setEnabled(false);
111 ui->buttonRetry->hide();
112 ui->buttonDownload->hide();
114 //Start animation
115 SHOW_ANIMATION(true);
118 UpdaterDialog::~UpdaterDialog(void)
120 if(!m_thread.isNull())
122 if(!m_thread->wait(5000))
124 m_thread->terminate();
125 m_thread->wait();
129 cleanFiles();
130 delete ui;
133 ///////////////////////////////////////////////////////////////////////////////
134 // Public Functions
135 ///////////////////////////////////////////////////////////////////////////////
137 /*None yet*/
139 ///////////////////////////////////////////////////////////////////////////////
140 // Events
141 ///////////////////////////////////////////////////////////////////////////////
143 bool UpdaterDialog::event(QEvent *e)
145 if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
147 MUtils::GUI::bring_to_front(m_updaterProcess);
149 return QDialog::event(e);
152 void UpdaterDialog::showEvent(QShowEvent *event)
154 if(m_firstShow)
156 m_firstShow = false;
157 QTimer::singleShot(16, this, SLOT(initUpdate()));
161 void UpdaterDialog::closeEvent(QCloseEvent *e)
163 if(!ui->buttonCancel->isEnabled())
165 e->ignore();
169 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
171 if(event->key() == Qt::Key_F11)
173 QFile logFile(QString("%1/%2.log").arg(MUtils::temp_folder(), MUtils::rand_str()));
174 if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
176 logFile.write("\xEF\xBB\xBF");
177 for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
179 logFile.write(iter->toUtf8());
180 logFile.write("\r\n");
182 logFile.close();
183 QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
188 ///////////////////////////////////////////////////////////////////////////////
189 // Slots
190 ///////////////////////////////////////////////////////////////////////////////
192 void UpdaterDialog::initUpdate(void)
194 //Clean up files from previous attempt
195 if(!m_binaries.isEmpty())
197 cleanFiles();
200 //Check binary files
201 if(!checkBinaries())
203 ui->buttonCancel->setEnabled(true);
204 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;"));
205 if(QMessageBox::critical(this, tr("File Error"), message, tr("Download Latest Version"), tr("Discard")) == 0)
207 QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
209 close(); return;
212 //Make sure user does have admin access
213 if(!MUtils::OS::user_is_admin())
215 qWarning("User is not in the \"admin\" group, cannot update!");
216 QString message;
217 message += QString("<nobr>%1</nobr><br>").arg(tr("Sorry, but only users in the \"Administrators\" group can install updates."));
218 message += QString("<nobr>%1</nobr>").arg(tr("Please start application from an administrator account and try again!"));
219 if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
221 ui->buttonCancel->setEnabled(true);
222 close(); return;
226 //Create and setup thread
227 if(!m_thread)
229 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));
230 connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
231 connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
232 connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
233 connect(m_thread.data(), SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
236 //Begin updater run
237 QTimer::singleShot(16, this, SLOT(checkForUpdates()));
240 void UpdaterDialog::checkForUpdates(void)
242 if((!m_thread) || m_thread->isRunning())
244 qWarning("Update in progress, cannot check for updates now!");
247 //Clear texts
248 ui->retranslateUi(this);
249 ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), tr("N/A")));
251 //Init buttons
252 ui->buttonCancel->setEnabled(false);
253 ui->buttonRetry->hide();
254 ui->buttonDownload->hide();
256 //Hide labels
257 ui->labelInfo->hide();
258 ui->labelUrl->hide();
260 //Update status
261 threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
263 //Start animation
264 SHOW_ANIMATION(true);
266 //Update cursor
267 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
268 QApplication::setOverrideCursor(Qt::WaitCursor);
270 //Clear log
271 m_logFile.clear();
273 //Start the updater thread
274 QTimer::singleShot(250, m_thread.data(), SLOT(start()));
277 void UpdaterDialog::threadStatusChanged(int status)
279 switch(m_status = status)
281 case MUtils::UpdateChecker::UpdateStatus_NotStartedYet:
282 UPDATE_ICON(1, "clock");
283 UPDATE_ICON(2, "clock");
284 UPDATE_ICON(3, "clock");
285 break;
286 case MUtils::UpdateChecker::UpdateStatus_CheckingConnection:
287 UPDATE_ICON(1, "play");
288 break;
289 case MUtils::UpdateChecker::UpdateStatus_FetchingUpdates:
290 UPDATE_ICON(1, "shield_green");
291 UPDATE_TEXT(1, tr("Internet connection is working."));
292 UPDATE_ICON(2, "play");
293 break;
294 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
295 UPDATE_ICON(1, "shield_error");
296 UPDATE_TEXT(1, tr("Computer is currently offline!"));
297 UPDATE_ICON(2, "shield_grey");
298 UPDATE_ICON(3, "shield_grey");
299 break;
300 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
301 UPDATE_ICON(1, "shield_error");
302 UPDATE_TEXT(1, tr("Internet connectivity test failed!"));
303 UPDATE_ICON(2, "shield_grey");
304 UPDATE_ICON(3, "shield_grey");
305 break;
306 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
307 UPDATE_ICON(2, "shield_error");
308 UPDATE_TEXT(2, tr("Failed to download the update information!"));
309 UPDATE_ICON(3, "shield_grey");
310 break;
311 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
312 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
313 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
314 UPDATE_ICON(2, "shield_green");
315 UPDATE_TEXT(2, tr("Update information received successfully."));
316 UPDATE_ICON(3, "play");
317 break;
318 default:
319 MUTILS_THROW("Unknown status code!");
323 void UpdaterDialog::threadFinished(void)
325 m_success = m_thread->getSuccess();
326 QTimer::singleShot((m_success ? 1000 : 0), this, SLOT(updateFinished()));
329 void UpdaterDialog::updateFinished(void)
331 //Restore cursor
332 QApplication::restoreOverrideCursor();
334 //If update was successfull, process final updater state
335 if(m_thread->getSuccess())
337 switch(m_status)
339 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
340 UPDATE_ICON(3, "shield_exclamation");
341 UPDATE_TEXT(3, tr("A newer version is available!"));
342 ui->buttonDownload->show();
343 break;
344 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
345 UPDATE_ICON(3, "shield_green");
346 UPDATE_TEXT(3, tr("Your version is up-to-date."));
347 break;
348 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
349 UPDATE_ICON(3, "shield_blue");
350 UPDATE_TEXT(3, tr("You are using a pre-release version!"));
351 break;
352 default:
353 qWarning("Update thread succeeded with unexpected status code: %d", m_status);
357 //Show update info or retry button
358 switch(m_status)
360 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
361 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
362 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
363 SHOW_ANIMATION(false);
364 ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), QString::number(m_thread->getUpdateInfo()->getBuildNo())));
365 ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->getDownloadSite()));
366 break;
367 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
368 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
369 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
370 m_animator->stop();
371 ui->buttonRetry->show();
372 break;
373 default:
374 qWarning("Update thread finished with unexpected status code: %d", m_status);
377 //Re-enbale cancel button
378 ui->buttonCancel->setEnabled(true);
382 void UpdaterDialog::threadMessageLogged(const QString &message)
384 m_logFile << message;
387 void UpdaterDialog::openUrl(const QString &url)
389 qDebug("Open URL: %s", url.toLatin1().constData());
390 QDesktopServices::openUrl(QUrl(url));
393 void UpdaterDialog::installUpdate(void)
395 if(!((m_thread) && m_thread->getSuccess()))
397 qWarning("Cannot download/install update at this point!");
398 return;
401 QApplication::setOverrideCursor(Qt::WaitCursor);
402 ui->buttonDownload->hide();
403 ui->buttonCancel->setEnabled(false);
404 SHOW_ANIMATION(true);
406 const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
408 QProcess process;
409 QStringList args;
410 QEventLoop loop;
412 MUtils::init_process(process, MUtils::temp_folder(), false);
414 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
415 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
417 args << QString("/Location=%1").arg(updateInfo->getDownloadAddress());
418 args << QString("/Filename=%1").arg(updateInfo->getDownloadFilename());
419 args << QString("/TicketID=%1").arg(updateInfo->getDownloadFilecode());
420 args << QString("/CheckSum=%1").arg(updateInfo->getDownloadChecksum());
421 args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
422 args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
423 args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
425 process.start(m_binaries.value("wupd.exe"), args);
426 if(!process.waitForStarted())
428 QApplication::restoreOverrideCursor();
429 SHOW_ANIMATION(false);
430 QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!"));
431 ui->buttonDownload->show();
432 ui->buttonCancel->setEnabled(true);
433 return;
436 m_updaterProcess = MUtils::OS::process_id(&process);
437 loop.exec(QEventLoop::ExcludeUserInputEvents);
439 if(!process.waitForFinished())
441 process.kill();
442 process.waitForFinished();
445 m_updaterProcess = NULL;
446 QApplication::restoreOverrideCursor();
447 ui->buttonDownload->show();
448 ui->buttonCancel->setEnabled(true);
449 SHOW_ANIMATION(false);
451 if(process.exitCode() == 0)
453 done(READY_TO_INSTALL_UPDATE);
457 ///////////////////////////////////////////////////////////////////////////////
458 // Private Functions
459 ///////////////////////////////////////////////////////////////////////////////
461 bool UpdaterDialog::checkBinaries(void)
463 qDebug("[File Verification]");
464 m_binaries.clear();
466 //Validate hashes first
467 const QString tempPath = MUtils::temp_folder();
468 for(size_t i = 0; BINARIES[i].name; i++)
470 const QString orgName = QString::fromLatin1(BINARIES[i].name);
471 const QString binPath = QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), orgName);
472 const QString outPath = QString("%1/%2_%3.%4").arg(tempPath, QFileInfo(orgName).baseName(), MUtils::rand_str(), QFileInfo(orgName).suffix());
473 if(!checkFileHash(binPath, BINARIES[i].hash))
475 qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName));
476 return false;
478 if(!QFile::copy(binPath, outPath))
480 qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName));
481 return false;
483 QFile::setPermissions(outPath, QFile::ReadOwner);
484 m_binaries.insert(BINARIES[i].name, outPath);
485 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
488 return true;
491 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
493 qDebug("Checking file: %s", filePath.toUtf8().constData());
494 MUtils::Hash::Blake2 checksum2;
495 QFile file(filePath);
496 if(file.open(QIODevice::ReadOnly))
498 checksum2.update(file);
499 const QByteArray fileHash = checksum2.finalize();
500 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
502 qWarning("\nFile appears to be corrupted:\n%s\n", filePath.toUtf8().constData());
503 qWarning("Expected Hash: %s\nDetected Hash: %s\n", expectedHash, fileHash.constData());
504 return false;
506 return true;
508 else
510 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
511 return false;
515 void UpdaterDialog::cleanFiles(void)
517 const QStringList keys = m_binaries.keys();
518 foreach(const QString &key, keys)
520 const QString fileName = m_binaries.value(key);
521 QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
522 if(!QFile::remove(fileName))
524 qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName));
526 m_binaries.remove(key);