OSDN Git Service

Updated cURL binary to v7.76.1 (2021-04-14).
[x264-launcher/x264-launcher.git] / src / win_updater.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "win_updater.h"
23 #include "UIC_win_updater.h"
24
25 //Internal
26 #include "global.h"
27 #include "model_sysinfo.h"
28
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>
35
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>
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51 static const char *const DIGEST_KEY = "~Dv/bW3/7t>6?RXVwkaZk-hmS0#O4JS/5YQAO>\\8hvr0B~7[n!X~KMYruemu:MDq";
52
53 const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
54 {
55         { "curl.exe", "16cf6c857df62440b68ff40d44b3870cf503a18f3b6a3ce611812c7d91c55e40afbc0eaae72f702e9a1a37587455899fe7e1b49b571b8ff887dd90cf309a8b7c", 1 },
56         { "gpgv.exe", "d0869bd858294520c992b66e1c7594176ebfb51bd64c4f50f782a4749118498a29c9fc70da5ed08cda9837ff7099d950428ca9f1968fa69883929bd0dba8c9e5", 1 },
57         { "gpgv.gpg", "1a2f528e551b9abfb064f08674fdd421d3abe403469ddfee2beafd007775a6c684212a6274dc2b41a0b20dd5c2200021c91320e737f7a90b2ac5a40a6221d93f", 0 },
58         { "wupd.exe", "018a8d0d848407fb0cb530b4540c6f025fd4c280885becd37f83feed8aeb3af6f8e8e0d45066a36549efac7e64706ac1ef09aaa5c75ab8d12c4a70f41518a894", 1 },
59         { NULL, NULL, 0 }
60 };
61
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))
64
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)
74
75 static inline QString getBin(const QMap<QString, QSharedPointer<QFile>> &binaries, const QString &nameName)
76 {
77         const QSharedPointer<QFile> file = binaries.value(nameName);
78         return file.isNull() ? QString() : file->fileName();
79 }
80
81 static void qFileDeleter(QFile *const file)
82 {
83         if(file)
84         {
85                 file->close();
86                 delete file;
87         }
88 }
89
90 ///////////////////////////////////////////////////////////////////////////////
91 // Constructor & Destructor
92 ///////////////////////////////////////////////////////////////////////////////
93
94 UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
95 :
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)
105 {
106         //Init the dialog, from the .ui file
107         ui->setupUi(this);
108         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
109
110         //Scale and fix size
111         MUtils::GUI::scale_widget(this);
112         setFixedSize(size());
113
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()));
118         
119         //Enable info label
120         connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
121         
122         //Init animation
123         m_animator.reset(new QMovie(":/images/loading.gif"));
124         ui->labelLoadingCenter->setMovie(m_animator.data());
125
126         //Init buttons
127         ui->buttonCancel->setEnabled(false);
128         ui->buttonRetry->hide();
129         ui->buttonDownload->hide();
130         ui->labelCancel->hide();
131
132         //Start animation
133         SHOW_ANIMATION(true);
134 }
135
136 UpdaterDialog::~UpdaterDialog(void)
137 {
138         if(!m_thread.isNull())
139         {
140                 if(!m_thread->wait(5000))
141                 {
142                         m_thread->terminate();
143                         m_thread->wait();
144                 }
145         }
146         delete ui;
147 }
148
149 ///////////////////////////////////////////////////////////////////////////////
150 // Events
151 ///////////////////////////////////////////////////////////////////////////////
152
153 bool UpdaterDialog::event(QEvent *e)
154 {
155         if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
156         {
157                 MUtils::GUI::bring_to_front(m_updaterProcess);
158         }
159         return QDialog::event(e);
160 }
161
162 void UpdaterDialog::showEvent(QShowEvent *event)
163 {
164         if(m_firstShow)
165         {
166                 m_firstShow = false;
167                 QTimer::singleShot(16, this, SLOT(initUpdate()));
168         }
169 }
170
171 void UpdaterDialog::closeEvent(QCloseEvent *e)
172 {
173         if(!ui->buttonCancel->isEnabled())
174         {
175                 e->ignore();
176         }
177 }
178
179 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
180 {
181         switch (event->key())
182         {
183         case Qt::Key_Escape:
184                 if ((!m_thread.isNull()) && m_thread->isRunning())
185                 {
186                         if (m_status >= MUtils::UpdateChecker::UpdateStatus_FetchingUpdates)
187                         {
188                                 UPDATE_TEXT(2, tr("Cancellation requested..."));
189                         }
190                         else
191                         {
192                                 UPDATE_TEXT(1, tr("Cancellation requested..."));
193                         }
194                         m_thread->cancel();
195                 }
196                 break;
197         case Qt::Key_F11:
198                 {
199                         const QString logFilePath = MUtils::make_temp_file(MUtils::temp_folder(), "txt", true);
200                         if (!logFilePath.isEmpty())
201                         {
202                                 qWarning("Write log to: '%s'", MUTILS_UTF8(logFilePath));
203                                 QFile logFile(logFilePath);
204                                 if (logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
205                                 {
206                                         logFile.write("\xEF\xBB\xBF");
207                                         for (QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
208                                         {
209                                                 logFile.write(iter->toUtf8());
210                                                 logFile.write("\r\n");
211                                         }
212                                         logFile.close();
213                                         QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
214                                 }
215                         }
216                 }
217                 break;
218         default:
219                 QDialog::keyPressEvent(event);
220         }
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
224 // Slots
225 ///////////////////////////////////////////////////////////////////////////////
226
227 void UpdaterDialog::initUpdate(void)
228 {
229         //Check binary files
230         if(!checkBinaries())
231         {
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)
235                 {
236                         QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
237                 }
238                 close();
239                 return;
240         }
241
242         //Make sure user does have admin access
243         if(!MUtils::OS::user_is_admin())
244         {
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)
250                 {
251                         ui->buttonCancel->setEnabled(true);
252                         close();
253                         return;
254                 }
255         }
256         
257         //Create and setup thread
258         if(!m_thread)
259         {
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)));
265         }
266
267         //Begin updater run
268         QTimer::singleShot(16, this, SLOT(checkForUpdates()));
269 }
270
271 void UpdaterDialog::checkForUpdates(void)
272 {
273         if((!m_thread) || m_thread->isRunning())
274         {
275                 qWarning("Update in progress, cannot check for updates now!");
276         }
277
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")));
281
282         //Init buttons
283         ui->buttonCancel->setEnabled(false);
284         ui->buttonRetry->hide();
285         ui->buttonDownload->hide();
286
287         //Hide labels
288         ui->labelInfo->hide();
289         ui->labelUrl->hide();
290         ui->labelCancel->show();
291
292         //Update status
293         threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
294
295         //Start animation
296         SHOW_ANIMATION(true);
297
298         //Update cursor
299         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
300         QApplication::setOverrideCursor(Qt::WaitCursor);
301
302         //Clear log
303         m_logFile.clear();
304
305         //Init timer
306         m_elapsed.reset(new QElapsedTimer());
307         m_elapsed->start();
308
309         //Start the updater thread
310         QTimer::singleShot(125, m_thread.data(), SLOT(start()));
311 }
312
313 void UpdaterDialog::threadStatusChanged(int status)
314 {
315         const int prevStatus = m_status;
316         switch(m_status = status)
317         {
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)
357                 {
358                         UPDATE_ICON(2, "shield_error");
359                         UPDATE_TEXT(2, tr("Operation was cancelled by the user!"));
360                         UPDATE_ICON(3, "shield_grey");
361                 }
362                 else
363                 {
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");
368                 }
369                 break;
370         default:
371                 MUTILS_THROW("Unknown status code!");
372         }
373 }
374
375 void UpdaterDialog::threadFinished(void)
376 {
377         m_success = m_thread->getSuccess();
378         ui->labelCancel->hide();
379         QTimer::singleShot((m_success ? 500 : 0), this, SLOT(updateFinished()));
380 }
381
382 void UpdaterDialog::updateFinished(void)
383 {
384         //Query the timer, if available
385         if (!m_elapsed.isNull())
386         {
387                 const quint64 elapsed = m_elapsed->restart();
388                 qDebug("Update check completed after %.2f seconds.", double(elapsed) / 1000.0);
389         }
390
391         //Restore cursor
392         QApplication::restoreOverrideCursor();
393
394         //If update was successfull, process final updater state
395         if(m_thread->getSuccess())
396         {
397                 switch(m_status)
398                 {
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);
414                 }
415         }
416
417         //Show update info or retry button
418         switch(m_status)
419         {
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);
436         }
437
438         //Re-enbale cancel button
439         ui->buttonCancel->setEnabled(true);
440         
441 }
442
443 void UpdaterDialog::threadMessageLogged(const QString &message)
444 {
445         m_logFile << message;
446 }
447
448 void UpdaterDialog::openUrl(const QString &url)
449 {
450         qDebug("Open URL: %s", url.toLatin1().constData());
451         QDesktopServices::openUrl(QUrl(url));
452 }
453
454 void UpdaterDialog::installUpdate(void)
455 {
456         if(!((m_thread) && m_thread->getSuccess()))
457         {
458                 qWarning("Cannot download/install update at this point!");
459                 return;
460         }
461
462         QApplication::setOverrideCursor(Qt::WaitCursor);
463         ui->buttonDownload->hide();
464         ui->buttonCancel->setEnabled(false);
465         SHOW_ANIMATION(true);
466
467         const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
468
469         QProcess process;
470         QStringList args;
471         QEventLoop loop;
472
473         MUtils::init_process(process, MUtils::temp_folder(), false);
474
475         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
476         connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
477
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()));
485
486         process.start(getBin(m_binaries, "wupd.exe"), args);
487         if(!process.waitForStarted())
488         {
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;
495         }
496
497         m_updaterProcess = MUtils::OS::process_id(&process);
498         loop.exec(QEventLoop::ExcludeUserInputEvents);
499         
500         if(!process.waitForFinished())
501         {
502                 process.kill();
503                 process.waitForFinished();
504         }
505
506         m_updaterProcess = NULL;
507         QApplication::restoreOverrideCursor();
508         ui->buttonDownload->show();
509         ui->buttonCancel->setEnabled(true);
510         SHOW_ANIMATION(false);
511
512         if(process.exitCode() == 0)
513         {
514                 done(READY_TO_INSTALL_UPDATE);
515         }
516 }
517
518 ///////////////////////////////////////////////////////////////////////////////
519 // Private Functions
520 ///////////////////////////////////////////////////////////////////////////////
521
522 bool UpdaterDialog::checkBinaries(void)
523 {
524         qDebug("[File Verification]");
525         for(size_t i = 0; BINARIES[i].name; i++)
526         {
527                 const QString name = QString::fromLatin1(BINARIES[i].name);
528                 if (!m_binaries.contains(name))
529                 {
530                         QScopedPointer<QFile> binary(new QFile(QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), name)));
531                         if (binary->open(QIODevice::ReadOnly))
532                         {
533                                 if (checkFileHash(binary->fileName(), BINARIES[i].hash))
534                                 {
535                                         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
536                                         m_binaries.insert(name, QSharedPointer<QFile>(binary.take(), qFileDeleter));
537                                 }
538                                 else
539                                 {
540                                         qWarning("Verification of '%s' has failed!", MUTILS_UTF8(name));
541                                         binary->close();
542                                         return false;
543                                 }
544                         }
545                         else
546                         {
547                                 qWarning("File '%s' could not be opened!", MUTILS_UTF8(name));
548                                 return false;
549                         }
550                 }
551         }
552         qDebug("File check completed.\n");
553         return true;
554 }
555
556 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
557 {
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))
562         {
563                 checksum->update(file);
564                 const QByteArray fileHash = checksum->digest();
565                 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
566                 {
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;
570                 }
571                 return true;
572         }
573         else
574         {
575                 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
576                 return false;
577         }
578 }