1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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"
26 #include "thread_updater.h"
30 #include <QCloseEvent>
32 #include <QMessageBox>
33 #include <QDesktopServices>
40 ///////////////////////////////////////////////////////////////////////////////
42 #define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
43 #define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
46 ui->labelLoadingLeft->setVisible((FLAG)); \
47 ui->labelLoadingCenter->setVisible((FLAG)); \
48 ui->labelLoadingRight->setVisible((FLAG)); \
51 #define SHOW_ANIMATION(FLAG) do \
53 ui->frameAnimation->setVisible((FLAG)); \
54 ui->labelInfo->setVisible(!(FLAG)); \
55 ui->labelUrl->setVisible(!(FLAG)); \
56 ui->labelBuildNo->setVisible(!(FLAG)); \
57 if((FLAG)) m_animator->start(); else m_animator->stop(); \
62 ///////////////////////////////////////////////////////////////////////////////
63 // Constructor & Destructor
64 ///////////////////////////////////////////////////////////////////////////////
66 UpdaterDialog::UpdaterDialog(QWidget *parent, const QString &binDir)
69 ui(new Ui::UpdaterDialog()),
71 m_status(UpdateCheckThread::UpdateStatus_NotStartedYet),
73 m_updaterProcess(NULL),
77 //Init the dialog, from the .ui file
79 setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
85 connect(ui->buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
86 connect(ui->buttonDownload, SIGNAL(clicked()), this, SLOT(installUpdate()));
87 connect(ui->buttonRetry, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
90 connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
93 m_animator = new QMovie(":/images/loading.gif");
94 ui->labelLoadingCenter->setMovie(m_animator);
97 ui->buttonCancel->setEnabled(false);
98 ui->buttonRetry->hide();
99 ui->buttonDownload->hide();
102 SHOW_ANIMATION(true);
105 UpdaterDialog::~UpdaterDialog(void)
109 if(!m_thread->wait(1000))
111 m_thread->terminate();
116 if((!m_keysFile.isEmpty()) && QFile::exists(m_keysFile))
118 QFile::setPermissions(m_keysFile, QFile::ReadOwner | QFile::WriteOwner);
119 QFile::remove(m_keysFile);
123 X264_DELETE(m_thread);
124 X264_DELETE(m_animator);
129 ///////////////////////////////////////////////////////////////////////////////
131 ///////////////////////////////////////////////////////////////////////////////
135 ///////////////////////////////////////////////////////////////////////////////
137 ///////////////////////////////////////////////////////////////////////////////
139 bool UpdaterDialog::event(QEvent *e)
141 if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
143 x264_bring_process_to_front(m_updaterProcess);
145 return QDialog::event(e);
148 void UpdaterDialog::showEvent(QShowEvent *event)
153 QTimer::singleShot(16, this, SLOT(initUpdate()));
157 void UpdaterDialog::closeEvent(QCloseEvent *e)
159 if(!ui->buttonCancel->isEnabled())
165 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
167 if(event->key() == Qt::Key_F11)
169 QFile logFile(QString("%1/%2.log").arg(x264_temp_directory(), x264_rand_str()));
170 if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
172 logFile.write("\xEF\xBB\xBF");
173 for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
175 logFile.write(iter->toUtf8());
176 logFile.write("\r\n");
179 QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
184 ///////////////////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////////////////
188 void UpdaterDialog::initUpdate(void)
191 QString wgetBin, gpgvBin;
192 if(!checkBinaries(wgetBin, gpgvBin))
194 ui->buttonCancel->setEnabled(true);
195 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!"));
199 //Make sure user does have admin access
200 if(!x264_user_is_admin())
202 qWarning("User is not in the \"admin\" group, cannot update!");
204 message += QString("<nobr>%1</nobr><br>").arg(tr("Sorry, but only users in the \"Administrators\" group can install updates."));
205 message += QString("<nobr>%1</nobr>").arg(tr("Please start application from an administrator account and try again!"));
206 if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
208 ui->buttonCancel->setEnabled(true);
213 //Create and setup thread
216 m_thread = new UpdateCheckThread(wgetBin, gpgvBin, m_keysFile, false);
217 connect(m_thread, SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
218 connect(m_thread, SIGNAL(finished()), this, SLOT(threadFinished()));
219 connect(m_thread, SIGNAL(terminated()), this, SLOT(threadFinished()));
220 connect(m_thread, SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
224 QTimer::singleShot(16, this, SLOT(checkForUpdates()));
227 void UpdaterDialog::checkForUpdates(void)
229 if((!m_thread) || m_thread->isRunning())
231 qWarning("Update in progress, cannot check for updates now!");
235 ui->retranslateUi(this);
236 ui->labelBuildNo->setText(tr("Installed build is #%1 | Latest build is #%2").arg(QString::number(x264_version_build()), tr("N/A")));
239 ui->buttonCancel->setEnabled(false);
240 ui->buttonRetry->hide();
241 ui->buttonDownload->hide();
244 ui->labelInfo->hide();
245 ui->labelUrl->hide();
248 threadStatusChanged(UpdateCheckThread::UpdateStatus_NotStartedYet);
251 SHOW_ANIMATION(true);
254 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
255 QApplication::setOverrideCursor(Qt::WaitCursor);
260 //Start the updater thread
261 QTimer::singleShot(250, m_thread, SLOT(start()));
264 void UpdaterDialog::threadStatusChanged(int status)
266 switch(m_status = status)
268 case UpdateCheckThread::UpdateStatus_NotStartedYet:
269 UPDATE_ICON(1, "clock");
270 UPDATE_ICON(2, "clock");
271 UPDATE_ICON(3, "clock");
273 case UpdateCheckThread::UpdateStatus_CheckingConnection:
274 UPDATE_ICON(1, "play");
276 case UpdateCheckThread::UpdateStatus_FetchingUpdates:
277 UPDATE_ICON(1, "shield_green");
278 UPDATE_TEXT(1, tr("Internet connection is working."));
279 UPDATE_ICON(2, "play");
281 case UpdateCheckThread::UpdateStatus_ErrorNoConnection:
282 UPDATE_ICON(1, "shield_error");
283 UPDATE_TEXT(1, tr("Computer is currently offline!"));
284 UPDATE_ICON(2, "shield_grey");
285 UPDATE_ICON(3, "shield_grey");
287 case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed:
288 UPDATE_ICON(1, "shield_error");
289 UPDATE_TEXT(1, tr("Internet connectivity test failed!"));
290 UPDATE_ICON(2, "shield_grey");
291 UPDATE_ICON(3, "shield_grey");
293 case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo:
294 UPDATE_ICON(2, "shield_error");
295 UPDATE_TEXT(2, tr("Failed to download the update information!"));
296 UPDATE_ICON(3, "shield_grey");
298 case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
299 case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
300 case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
301 UPDATE_ICON(2, "shield_green");
302 UPDATE_TEXT(2, tr("Update information received successfully."));
303 UPDATE_ICON(3, "play");
306 throw "Unknown status code!";
310 void UpdaterDialog::threadFinished(void)
312 m_success = m_thread->getSuccess();
313 QTimer::singleShot((m_success ? 1000 : 0), this, SLOT(updateFinished()));
316 void UpdaterDialog::updateFinished(void)
319 QApplication::restoreOverrideCursor();
321 //If update was successfull, process final updater state
322 if(m_thread->getSuccess())
326 case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
327 UPDATE_ICON(3, "shield_exclamation");
328 UPDATE_TEXT(3, tr("A newer version is available!"));
329 ui->buttonDownload->show();
331 case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
332 UPDATE_ICON(3, "shield_green");
333 UPDATE_TEXT(3, tr("Your version is up-to-date."));
335 case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
336 UPDATE_ICON(3, "shield_blue");
337 UPDATE_TEXT(3, tr("You are using a pre-release version!"));
340 qWarning("Update thread succeeded with unexpected status code: %d", m_status);
344 //Show update info or retry button
347 case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable:
348 case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
349 case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
350 SHOW_ANIMATION(false);
351 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)));
352 ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->m_downloadSite));
354 case UpdateCheckThread::UpdateStatus_ErrorNoConnection:
355 case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed:
356 case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo:
358 ui->buttonRetry->show();
361 qWarning("Update thread finished with unexpected status code: %d", m_status);
364 //Re-enbale cancel button
365 ui->buttonCancel->setEnabled(true);
369 void UpdaterDialog::threadMessageLogged(const QString &message)
371 m_logFile << message;
374 void UpdaterDialog::openUrl(const QString &url)
376 qDebug("Open URL: %s", url.toLatin1().constData());
377 QDesktopServices::openUrl(QUrl(url));
380 void UpdaterDialog::installUpdate(void)
382 if(!((m_thread) && m_thread->getSuccess()))
384 qWarning("Cannot download/install update at this point!");
388 QApplication::setOverrideCursor(Qt::WaitCursor);
389 ui->buttonDownload->hide();
390 ui->buttonCancel->setEnabled(false);
391 SHOW_ANIMATION(true);
393 const UpdateInfo *updateInfo = m_thread->getUpdateInfo();
399 x264_init_process(process, x264_temp_directory(), false);
401 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
402 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
404 args << QString("/Location=%1").arg(updateInfo->m_downloadAddress);
405 args << QString("/Filename=%1").arg(updateInfo->m_downloadFilename);
406 args << QString("/TicketID=%1").arg(updateInfo->m_downloadFilecode);
407 args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
408 args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
409 args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->m_buildNo));
411 process.start(m_wupdFile, args);
412 if(!process.waitForStarted())
414 QApplication::restoreOverrideCursor();
415 SHOW_ANIMATION(false);
416 QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!"));
417 ui->buttonDownload->show();
418 ui->buttonCancel->setEnabled(true);
422 m_updaterProcess = x264_process_id(process);
423 loop.exec(QEventLoop::ExcludeUserInputEvents);
425 if(!process.waitForFinished())
428 process.waitForFinished();
431 m_updaterProcess = NULL;
432 QApplication::restoreOverrideCursor();
433 ui->buttonDownload->show();
434 ui->buttonCancel->setEnabled(true);
435 SHOW_ANIMATION(false);
437 if(process.exitCode() == 0)
439 done(READY_TO_INSTALL_UPDATE);
443 ///////////////////////////////////////////////////////////////////////////////
445 ///////////////////////////////////////////////////////////////////////////////
447 bool UpdaterDialog::checkBinaries(QString &wgetBin, QString &gpgvBin)
449 qDebug("[File Verification]");
458 { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e" },
459 { "gpgv.exe", "e61d28e4c47b2422ceec7b8fc08f9c70f10a3056e3779a974026eb24fe09551eedc2e7f34fbe5ef8e844fab0dbe68b85c4ca69d63bf85d445f7cae152c17f589" },
460 { "gpgv.gpg", "58e0f0e462bbd0b5aa4f638801c1097da7da4b3eb38c8c88ad1db23705c0f11e174b083fa55fe76bd3ba196341c967833a6f3427d6f63ad8565900745535d8fa" },
461 { "wupd.exe", "9dd0ae5f386aaf5df9e261a53b81569df51bccf2f9290ed42f04a5b52b4a4e1118f2c9ce3a9b2492fd5ae597600411dfa47bd7c96e2fd7bee205d603324452a2" },
465 QMap<QString, QString> binaries;
474 for(size_t i = 0; FILE_INFO[i].name; i++)
476 const QString binPath = QString("%1/common/%2").arg(m_binDir, QString::fromLatin1(FILE_INFO[i].name));
477 if(okay = okay && checkFileHash(binPath, FILE_INFO[i].hash))
479 binaries.insert(FILE_INFO[i].name, binPath);
481 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
486 wgetBin = binaries.value("wget.exe");
487 gpgvBin = binaries.value("gpgv.exe");
489 m_wupdFile = binaries.value("wupd.exe");
490 m_keysFile = QString("%1/%2.gpg").arg(x264_temp_directory(), x264_rand_str());
492 if(okay = QFile::copy(binaries.value("gpgv.gpg"), m_keysFile))
494 QFile::setPermissions(m_keysFile, QFile::ReadOwner);
496 qDebug("%s\n", okay ? "Completed." : "Failed to copy GPG file!");
502 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
504 qDebug("Checking file: %s", filePath.toUtf8().constData());
505 QBlake2Checksum checksum2;
506 QFile file(filePath);
507 if(file.open(QIODevice::ReadOnly))
509 checksum2.update(file);
510 const QByteArray fileHash = checksum2.finalize();
511 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
513 qWarning("\nFile appears to be corrupted:\n%s\n", filePath.toUtf8().constData());
514 qWarning("Expected Hash: %s\nDetected Hash: %s\n", expectedHash, fileHash.constData());
521 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());