OSDN Git Service

Updated web-update tool to latest binary version.
[x264-launcher/x264-launcher.git] / src / win_updater.cpp
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.
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_Blake2.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
48 ///////////////////////////////////////////////////////////////////////////////
49
50 const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
51 {
52         { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e", 1 },
53         { "netc.exe", "c199ea12d761fa3191006da250f8f600ad426265fdf4a43e551cdf04a451a105692efd3ef82ac621c0799394aa21ac65bfbb4bab90c3fbb1f557e93f490fcb75", 1 },
54         { "gpgv.exe", "18c5456cbb9ebf5cb9012a939b199d9eaa71c92a39f574f1e032babad0bbd9e72a064af96ca9d3d01f2892b064ec239fd61f27bac2eb9a64f7b2ece7beea3158", 1 },
55         { "gpgv.gpg", "745c7a9c040196d9d322b1580e0046ff26ec13238cfd04325ceb3d4c8948294c593c027f895dc8ec427295175003e75d34f083019b706b0f4f06f81cce8df47d", 0 },
56         { "wupd.exe", "41bc1ddfcfc0b04a0b8b2c4f95b189d4031afdceaac7bb4a3a31e9235997c1afec4a65c7c71b9c3546c1b6c66950bc764929b89c80fef66f5516a9fd181c2581", 1 },
57         { NULL, NULL, 0 }
58 };
59
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))
62
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)
72
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // Constructor & Destructor
76 ///////////////////////////////////////////////////////////////////////////////
77
78 UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
79 :
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)
89 {
90         //Init the dialog, from the .ui file
91         ui->setupUi(this);
92         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
93
94         //Fix size
95         setFixedSize(size());
96
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()));
101         
102         //Enable info label
103         connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
104         
105         //Init animation
106         m_animator.reset(new QMovie(":/images/loading.gif"));
107         ui->labelLoadingCenter->setMovie(m_animator.data());
108
109         //Init buttons
110         ui->buttonCancel->setEnabled(false);
111         ui->buttonRetry->hide();
112         ui->buttonDownload->hide();
113
114         //Start animation
115         SHOW_ANIMATION(true);
116 }
117
118 UpdaterDialog::~UpdaterDialog(void)
119 {
120         if(!m_thread.isNull())
121         {
122                 if(!m_thread->wait(5000))
123                 {
124                         m_thread->terminate();
125                         m_thread->wait();
126                 }
127         }
128
129         cleanFiles();
130         delete ui;
131 }
132
133 ///////////////////////////////////////////////////////////////////////////////
134 // Public Functions
135 ///////////////////////////////////////////////////////////////////////////////
136
137 /*None yet*/
138
139 ///////////////////////////////////////////////////////////////////////////////
140 // Events
141 ///////////////////////////////////////////////////////////////////////////////
142
143 bool UpdaterDialog::event(QEvent *e)
144 {
145         if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
146         {
147                 MUtils::GUI::bring_to_front(m_updaterProcess);
148         }
149         return QDialog::event(e);
150 }
151
152 void UpdaterDialog::showEvent(QShowEvent *event)
153 {
154         if(m_firstShow)
155         {
156                 m_firstShow = false;
157                 QTimer::singleShot(16, this, SLOT(initUpdate()));
158         }
159 }
160
161 void UpdaterDialog::closeEvent(QCloseEvent *e)
162 {
163         if(!ui->buttonCancel->isEnabled())
164         {
165                 e->ignore();
166         }
167 }
168
169 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
170 {
171         if(event->key() == Qt::Key_F11)
172         {
173                 QFile logFile(QString("%1/%2.log").arg(MUtils::temp_folder(), MUtils::rand_str()));
174                 if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
175                 {
176                         logFile.write("\xEF\xBB\xBF");
177                         for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
178                         {
179                                 logFile.write(iter->toUtf8());
180                                 logFile.write("\r\n");
181                         }
182                         logFile.close();
183                         QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
184                 }
185         }
186 }
187
188 ///////////////////////////////////////////////////////////////////////////////
189 // Slots
190 ///////////////////////////////////////////////////////////////////////////////
191
192 void UpdaterDialog::initUpdate(void)
193 {
194         //Clean up files from previous attempt
195         if(!m_binaries.isEmpty())
196         {
197                 cleanFiles();
198         }
199
200         //Check binary files
201         if(!checkBinaries())
202         {
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)
206                 {
207                         QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
208                 }
209                 close(); return;
210         }
211
212         //Make sure user does have admin access
213         if(!MUtils::OS::user_is_admin())
214         {
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)
220                 {
221                         ui->buttonCancel->setEnabled(true);
222                         close(); return;
223                 }
224         }
225         
226         //Create and setup thread
227         if(!m_thread)
228         {
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)));
234         }
235
236         //Begin updater run
237         QTimer::singleShot(16, this, SLOT(checkForUpdates()));
238 }
239
240 void UpdaterDialog::checkForUpdates(void)
241 {
242         if((!m_thread) || m_thread->isRunning())
243         {
244                 qWarning("Update in progress, cannot check for updates now!");
245         }
246
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")));
250
251         //Init buttons
252         ui->buttonCancel->setEnabled(false);
253         ui->buttonRetry->hide();
254         ui->buttonDownload->hide();
255
256         //Hide labels
257         ui->labelInfo->hide();
258         ui->labelUrl->hide();
259
260         //Update status
261         threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
262
263         //Start animation
264         SHOW_ANIMATION(true);
265
266         //Update cursor
267         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
268         QApplication::setOverrideCursor(Qt::WaitCursor);
269
270         //Clear log
271         m_logFile.clear();
272
273         //Start the updater thread
274         QTimer::singleShot(250, m_thread.data(), SLOT(start()));
275 }
276
277 void UpdaterDialog::threadStatusChanged(int status)
278 {
279         switch(m_status = status)
280         {
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!");
320         }
321 }
322
323 void UpdaterDialog::threadFinished(void)
324 {
325         m_success = m_thread->getSuccess();
326         QTimer::singleShot((m_success ? 1000 : 0), this, SLOT(updateFinished()));
327 }
328
329 void UpdaterDialog::updateFinished(void)
330 {
331         //Restore cursor
332         QApplication::restoreOverrideCursor();
333
334         //If update was successfull, process final updater state
335         if(m_thread->getSuccess())
336         {
337                 switch(m_status)
338                 {
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);
354                 }
355         }
356
357         //Show update info or retry button
358         switch(m_status)
359         {
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);
375         }
376
377         //Re-enbale cancel button
378         ui->buttonCancel->setEnabled(true);
379         
380 }
381
382 void UpdaterDialog::threadMessageLogged(const QString &message)
383 {
384         m_logFile << message;
385 }
386
387 void UpdaterDialog::openUrl(const QString &url)
388 {
389         qDebug("Open URL: %s", url.toLatin1().constData());
390         QDesktopServices::openUrl(QUrl(url));
391 }
392
393 void UpdaterDialog::installUpdate(void)
394 {
395         if(!((m_thread) && m_thread->getSuccess()))
396         {
397                 qWarning("Cannot download/install update at this point!");
398                 return;
399         }
400
401         QApplication::setOverrideCursor(Qt::WaitCursor);
402         ui->buttonDownload->hide();
403         ui->buttonCancel->setEnabled(false);
404         SHOW_ANIMATION(true);
405
406         const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
407
408         QProcess process;
409         QStringList args;
410         QEventLoop loop;
411
412         MUtils::init_process(process, MUtils::temp_folder(), false);
413
414         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
415         connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
416
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()));
424
425         process.start(m_binaries.value("wupd.exe"), args);
426         if(!process.waitForStarted())
427         {
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;
434         }
435
436         m_updaterProcess = MUtils::OS::process_id(&process);
437         loop.exec(QEventLoop::ExcludeUserInputEvents);
438         
439         if(!process.waitForFinished())
440         {
441                 process.kill();
442                 process.waitForFinished();
443         }
444
445         m_updaterProcess = NULL;
446         QApplication::restoreOverrideCursor();
447         ui->buttonDownload->show();
448         ui->buttonCancel->setEnabled(true);
449         SHOW_ANIMATION(false);
450
451         if(process.exitCode() == 0)
452         {
453                 done(READY_TO_INSTALL_UPDATE);
454         }
455 }
456
457 ///////////////////////////////////////////////////////////////////////////////
458 // Private Functions
459 ///////////////////////////////////////////////////////////////////////////////
460
461 bool UpdaterDialog::checkBinaries(void)
462 {
463         qDebug("[File Verification]");
464         m_binaries.clear();
465
466         //Validate hashes first
467         const QString tempPath = MUtils::temp_folder();
468         for(size_t i = 0; BINARIES[i].name; i++)
469         {
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))
474                 {
475                         qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName));
476                         return false;
477                 }
478                 if(!QFile::copy(binPath, outPath))
479                 {
480                         qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName));
481                         return false;
482                 }
483                 QFile::setPermissions(outPath, QFile::ReadOwner);
484                 m_binaries.insert(BINARIES[i].name, outPath);
485                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
486         }
487
488         return true;
489 }
490
491 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
492 {
493         qDebug("Checking file: %s", filePath.toUtf8().constData());
494         MUtils::Hash::Blake2 checksum2;
495         QFile file(filePath);
496         if(file.open(QIODevice::ReadOnly))
497         {
498                 checksum2.update(file);
499                 const QByteArray fileHash = checksum2.finalize();
500                 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
501                 {
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;
505                 }
506                 return true;
507         }
508         else
509         {
510                 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
511                 return false;
512         }
513 }
514
515 void UpdaterDialog::cleanFiles(void)
516 {
517         const QStringList keys = m_binaries.keys();
518         foreach(const QString &key, keys)
519         {
520                 const QString fileName = m_binaries.value(key);
521                 QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
522                 if(!QFile::remove(fileName))
523                 {
524                         qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName));
525                 }
526                 m_binaries.remove(key);
527         }
528 }