1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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"
27 #include "model_sysinfo.h"
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>
38 #include <QCloseEvent>
40 #include <QMessageBox>
41 #include <QDesktopServices>
48 ///////////////////////////////////////////////////////////////////////////////
50 const UpdaterDialog::binary_t
UpdaterDialog::BINARIES
[] =
52 { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e", 1 },
53 { "gpgv.exe", "97eebd656ad2799cbb69250ea9434b5afc8bcf578097703594277451f4e752166464a077ba71d5960824fa71e52f2a9e94c6368ae70a93d86a10720b3fced2fd", 1 },
54 { "gpgv.gpg", "58e0f0e462bbd0b5aa4f638801c1097da7da4b3eb38c8c88ad1db23705c0f11e174b083fa55fe76bd3ba196341c967833a6f3427d6f63ad8565900745535d8fa", 0 },
55 { "wupd.exe", "1156dd9aa47df35dfeb68cb9bea26cf5236c1bf4c29c22676b450e827aa8e4419858d2b9a2c9e94747ad43704a145724ec5b0280d16922e52c6c47e324739571", 1 },
59 #define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
60 #define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
62 #define SHOW_ANIMATION(FLAG) do \
64 ui->frameAnimation->setVisible((FLAG)); \
65 ui->labelInfo->setVisible(!(FLAG)); \
66 ui->labelUrl->setVisible(!(FLAG)); \
67 ui->labelBuildNo->setVisible(!(FLAG)); \
68 if((FLAG)) m_animator->start(); else m_animator->stop(); \
73 ///////////////////////////////////////////////////////////////////////////////
74 // Constructor & Destructor
75 ///////////////////////////////////////////////////////////////////////////////
77 UpdaterDialog::UpdaterDialog(QWidget
*parent
, const SysinfoModel
*sysinfo
, const char *const updateUrl
)
80 ui(new Ui::UpdaterDialog()),
82 m_updateUrl(updateUrl
),
83 m_status(MUtils::UpdateChecker::UpdateStatus_NotStartedYet
),
85 m_updaterProcess(NULL
),
89 //Init the dialog, from the .ui file
91 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint
));
97 connect(ui
->buttonCancel
, SIGNAL(clicked()), this, SLOT(close()));
98 connect(ui
->buttonDownload
, SIGNAL(clicked()), this, SLOT(installUpdate()));
99 connect(ui
->buttonRetry
, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
102 connect(ui
->labelUrl
, SIGNAL(linkActivated(QString
)), this, SLOT(openUrl(QString
)));
105 m_animator
.reset(new QMovie(":/images/loading.gif"));
106 ui
->labelLoadingCenter
->setMovie(m_animator
.data());
109 ui
->buttonCancel
->setEnabled(false);
110 ui
->buttonRetry
->hide();
111 ui
->buttonDownload
->hide();
114 SHOW_ANIMATION(true);
117 UpdaterDialog::~UpdaterDialog(void)
119 if(!m_thread
.isNull())
121 if(!m_thread
->wait(5000))
123 m_thread
->terminate();
132 ///////////////////////////////////////////////////////////////////////////////
134 ///////////////////////////////////////////////////////////////////////////////
138 ///////////////////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////////////////
142 bool UpdaterDialog::event(QEvent
*e
)
144 if((e
->type() == QEvent::ActivationChange
) && (m_updaterProcess
!= NULL
))
146 MUtils::GUI::bring_to_front(m_updaterProcess
);
148 return QDialog::event(e
);
151 void UpdaterDialog::showEvent(QShowEvent
*event
)
156 QTimer::singleShot(16, this, SLOT(initUpdate()));
160 void UpdaterDialog::closeEvent(QCloseEvent
*e
)
162 if(!ui
->buttonCancel
->isEnabled())
168 void UpdaterDialog::keyPressEvent(QKeyEvent
*event
)
170 if(event
->key() == Qt::Key_F11
)
172 QFile
logFile(QString("%1/%2.log").arg(MUtils::temp_folder(), MUtils::rand_str()));
173 if(logFile
.open(QIODevice::WriteOnly
| QIODevice::Truncate
))
175 logFile
.write("\xEF\xBB\xBF");
176 for(QStringList::ConstIterator iter
= m_logFile
.constBegin(); iter
!= m_logFile
.constEnd(); iter
++)
178 logFile
.write(iter
->toUtf8());
179 logFile
.write("\r\n");
182 QDesktopServices::openUrl(QUrl::fromLocalFile(logFile
.fileName()));
187 ///////////////////////////////////////////////////////////////////////////////
189 ///////////////////////////////////////////////////////////////////////////////
191 void UpdaterDialog::initUpdate(void)
193 //Clean up files from previous attempt
194 if(!m_binaries
.isEmpty())
200 QString wgetBin
, gpgvBin
;
201 if(!checkBinaries(wgetBin
, gpgvBin
))
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("-", "−"));
205 if(QMessageBox::critical(this, tr("File Error"), message
, tr("Download Latest Version"), tr("Discard")) == 0)
207 QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl
)));
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!");
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);
226 //Create and setup thread
229 m_thread
.reset(new MUtils::UpdateChecker(m_binaries
.value("wget.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
)));
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!");
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")));
252 ui
->buttonCancel
->setEnabled(false);
253 ui
->buttonRetry
->hide();
254 ui
->buttonDownload
->hide();
257 ui
->labelInfo
->hide();
258 ui
->labelUrl
->hide();
261 threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet
);
264 SHOW_ANIMATION(true);
267 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
268 QApplication::setOverrideCursor(Qt::WaitCursor
);
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");
286 case MUtils::UpdateChecker::UpdateStatus_CheckingConnection
:
287 UPDATE_ICON(1, "play");
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");
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");
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");
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");
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");
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)
332 QApplication::restoreOverrideCursor();
334 //If update was successfull, process final updater state
335 if(m_thread
->getSuccess())
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();
344 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates
:
345 UPDATE_ICON(3, "shield_green");
346 UPDATE_TEXT(3, tr("Your version is up-to-date."));
348 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder
:
349 UPDATE_ICON(3, "shield_blue");
350 UPDATE_TEXT(3, tr("You are using a pre-release version!"));
353 qWarning("Update thread succeeded with unexpected status code: %d", m_status
);
357 //Show update info or retry button
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()));
367 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection
:
368 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed
:
369 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo
:
371 ui
->buttonRetry
->show();
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!");
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();
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("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
421 args
<< QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
422 args
<< QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo
->getBuildNo()));
424 process
.start(m_binaries
.value("wupd.exe"), args
);
425 if(!process
.waitForStarted())
427 QApplication::restoreOverrideCursor();
428 SHOW_ANIMATION(false);
429 QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!"));
430 ui
->buttonDownload
->show();
431 ui
->buttonCancel
->setEnabled(true);
435 m_updaterProcess
= MUtils::OS::process_id(&process
);
436 loop
.exec(QEventLoop::ExcludeUserInputEvents
);
438 if(!process
.waitForFinished())
441 process
.waitForFinished();
444 m_updaterProcess
= NULL
;
445 QApplication::restoreOverrideCursor();
446 ui
->buttonDownload
->show();
447 ui
->buttonCancel
->setEnabled(true);
448 SHOW_ANIMATION(false);
450 if(process
.exitCode() == 0)
452 done(READY_TO_INSTALL_UPDATE
);
456 ///////////////////////////////////////////////////////////////////////////////
458 ///////////////////////////////////////////////////////////////////////////////
460 bool UpdaterDialog::checkBinaries(QString
&wgetBin
, QString
&gpgvBin
)
462 qDebug("[File Verification]");
465 //Validate hashes first
466 const QString tempPath
= MUtils::temp_folder();
467 for(size_t i
= 0; BINARIES
[i
].name
; i
++)
469 const QString orgName
= QString::fromLatin1(BINARIES
[i
].name
);
470 const QString binPath
= QString("%1/toolset/common/%2").arg(m_sysinfo
->getAppPath(), orgName
);
471 const QString outPath
= QString("%1/%2_%3.%4").arg(tempPath
, QFileInfo(orgName
).baseName(), MUtils::rand_str(), QFileInfo(orgName
).suffix());
472 if(!checkFileHash(binPath
, BINARIES
[i
].hash
))
474 qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName
));
477 if(!QFile::copy(binPath
, outPath
))
479 qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName
));
482 QFile::setPermissions(outPath
, QFile::ReadOwner
);
483 m_binaries
.insert(BINARIES
[i
].name
, outPath
);
484 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents
);
490 bool UpdaterDialog::checkFileHash(const QString
&filePath
, const char *expectedHash
)
492 qDebug("Checking file: %s", filePath
.toUtf8().constData());
493 MUtils::Hash::Blake2 checksum2
;
494 QFile
file(filePath
);
495 if(file
.open(QIODevice::ReadOnly
))
497 checksum2
.update(file
);
498 const QByteArray fileHash
= checksum2
.finalize();
499 if((strlen(expectedHash
) != fileHash
.size()) || (memcmp(fileHash
.constData(), expectedHash
, fileHash
.size()) != 0))
501 qWarning("\nFile appears to be corrupted:\n%s\n", filePath
.toUtf8().constData());
502 qWarning("Expected Hash: %s\nDetected Hash: %s\n", expectedHash
, fileHash
.constData());
509 qWarning("Failed to open file:\n%s\n", filePath
.toUtf8().constData());
514 void UpdaterDialog::cleanFiles(void)
516 const QStringList keys
= m_binaries
.keys();
517 foreach(const QString
&key
, keys
)
519 const QString fileName
= m_binaries
.value(key
);
520 QFile::setPermissions(fileName
, QFile::ReadOwner
| QFile::WriteOwner
);
521 if(!QFile::remove(fileName
))
523 qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName
));
525 m_binaries
.remove(key
);