OSDN Git Service

Moved some more functions into MUtilities libraries.
[x264-launcher/x264-launcher.git] / src / win_updater.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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
33 #include <QMovie>
34 #include <QCloseEvent>
35 #include <QTimer>
36 #include <QMessageBox>
37 #include <QDesktopServices>
38 #include <QUrl>
39 #include <QProcess>
40 #include <QFileInfo>
41 #include <QDir>
42 #include <QMap>
43
44 ///////////////////////////////////////////////////////////////////////////////
45
46 const UpdaterDialog::binary_t UpdaterDialog::BINARIES[] =
47 {
48         { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e", 1 },
49         { "gpgv.exe", "97eebd656ad2799cbb69250ea9434b5afc8bcf578097703594277451f4e752166464a077ba71d5960824fa71e52f2a9e94c6368ae70a93d86a10720b3fced2fd", 1 },
50         { "gpgv.gpg", "58e0f0e462bbd0b5aa4f638801c1097da7da4b3eb38c8c88ad1db23705c0f11e174b083fa55fe76bd3ba196341c967833a6f3427d6f63ad8565900745535d8fa", 0 },
51         { "wupd.exe", "1156dd9aa47df35dfeb68cb9bea26cf5236c1bf4c29c22676b450e827aa8e4419858d2b9a2c9e94747ad43704a145724ec5b0280d16922e52c6c47e324739571", 1 },
52         { NULL, NULL, 0 }
53 };
54
55 #define UPDATE_TEXT(N, TEXT) ui->label_phase##N->setText((TEXT))
56 #define UPDATE_ICON(N, ICON) ui->icon_phase##N->setPixmap(QIcon(":/buttons/" ICON ".png").pixmap(16, 16))
57
58 #define SHOW_ANIMATION(FLAG) do  \
59 { \
60         ui->frameAnimation->setVisible((FLAG)); \
61         ui->labelInfo->setVisible(!(FLAG)); \
62         ui->labelUrl->setVisible(!(FLAG)); \
63         ui->labelBuildNo->setVisible(!(FLAG)); \
64         if((FLAG)) m_animator->start(); else m_animator->stop(); \
65 } \
66 while(0)
67
68
69 ///////////////////////////////////////////////////////////////////////////////
70 // Constructor & Destructor
71 ///////////////////////////////////////////////////////////////////////////////
72
73 UpdaterDialog::UpdaterDialog(QWidget *parent, const SysinfoModel *sysinfo, const char *const updateUrl)
74 :
75         QDialog(parent),
76         ui(new Ui::UpdaterDialog()),
77         m_sysinfo(sysinfo),
78         m_updateUrl(updateUrl),
79         m_status(MUtils::UpdateChecker::UpdateStatus_NotStartedYet),
80         m_thread(NULL),
81         m_updaterProcess(NULL),
82         m_success(false),
83         m_firstShow(true)
84 {
85         //Init the dialog, from the .ui file
86         ui->setupUi(this);
87         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
88
89         //Fix size
90         setFixedSize(size());
91
92         //Enable buttons
93         connect(ui->buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
94         connect(ui->buttonDownload, SIGNAL(clicked()), this, SLOT(installUpdate()));
95         connect(ui->buttonRetry, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
96         
97         //Enable info label
98         connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
99         
100         //Init animation
101         m_animator.reset(new QMovie(":/images/loading.gif"));
102         ui->labelLoadingCenter->setMovie(m_animator.data());
103
104         //Init buttons
105         ui->buttonCancel->setEnabled(false);
106         ui->buttonRetry->hide();
107         ui->buttonDownload->hide();
108
109         //Start animation
110         SHOW_ANIMATION(true);
111 }
112
113 UpdaterDialog::~UpdaterDialog(void)
114 {
115         if(!m_thread.isNull())
116         {
117                 if(!m_thread->wait(5000))
118                 {
119                         m_thread->terminate();
120                         m_thread->wait();
121                 }
122         }
123
124         cleanFiles();
125         delete ui;
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////
129 // Public Functions
130 ///////////////////////////////////////////////////////////////////////////////
131
132 /*None yet*/
133
134 ///////////////////////////////////////////////////////////////////////////////
135 // Events
136 ///////////////////////////////////////////////////////////////////////////////
137
138 bool UpdaterDialog::event(QEvent *e)
139 {
140         if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
141         {
142                 x264_bring_process_to_front(m_updaterProcess);
143         }
144         return QDialog::event(e);
145 }
146
147 void UpdaterDialog::showEvent(QShowEvent *event)
148 {
149         if(m_firstShow)
150         {
151                 m_firstShow = false;
152                 QTimer::singleShot(16, this, SLOT(initUpdate()));
153         }
154 }
155
156 void UpdaterDialog::closeEvent(QCloseEvent *e)
157 {
158         if(!ui->buttonCancel->isEnabled())
159         {
160                 e->ignore();
161         }
162 }
163
164 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
165 {
166         if(event->key() == Qt::Key_F11)
167         {
168                 QFile logFile(QString("%1/%2.log").arg(x264_temp_directory(), x264_rand_str()));
169                 if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
170                 {
171                         logFile.write("\xEF\xBB\xBF");
172                         for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
173                         {
174                                 logFile.write(iter->toUtf8());
175                                 logFile.write("\r\n");
176                         }
177                         logFile.close();
178                         QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
179                 }
180         }
181 }
182
183 ///////////////////////////////////////////////////////////////////////////////
184 // Slots
185 ///////////////////////////////////////////////////////////////////////////////
186
187 void UpdaterDialog::initUpdate(void)
188 {
189         //Clean up files from previous attempt
190         if(!m_binaries.isEmpty())
191         {
192                 cleanFiles();
193         }
194
195         //Check binary files
196         QString wgetBin, gpgvBin;
197         if(!checkBinaries(wgetBin, gpgvBin))
198         {
199                 ui->buttonCancel->setEnabled(true);
200                 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;"));
201                 if(QMessageBox::critical(this, tr("File Error"), message, tr("Download Latest Version"), tr("Discard")) == 0)
202                 {
203                         QDesktopServices::openUrl(QUrl(QString::fromLatin1(m_updateUrl)));
204                 }
205                 close(); return;
206         }
207
208         //Make sure user does have admin access
209         if(!x264_user_is_admin())
210         {
211                 qWarning("User is not in the \"admin\" group, cannot update!");
212                 QString message;
213                 message += QString("<nobr>%1</nobr><br>").arg(tr("Sorry, but only users in the \"Administrators\" group can install updates."));
214                 message += QString("<nobr>%1</nobr>").arg(tr("Please start application from an administrator account and try again!"));
215                 if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
216                 {
217                         ui->buttonCancel->setEnabled(true);
218                         close(); return;
219                 }
220         }
221         
222         //Create and setup thread
223         if(!m_thread)
224         {
225                 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));
226                 connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
227                 connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
228                 connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
229                 connect(m_thread.data(), SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
230         }
231
232         //Begin updater run
233         QTimer::singleShot(16, this, SLOT(checkForUpdates()));
234 }
235
236 void UpdaterDialog::checkForUpdates(void)
237 {
238         if((!m_thread) || m_thread->isRunning())
239         {
240                 qWarning("Update in progress, cannot check for updates now!");
241         }
242
243         //Clear texts
244         ui->retranslateUi(this);
245         ui->labelBuildNo->setText(tr("Installed build is #%1  |  Latest build is #%2").arg(QString::number(x264_version_build()), tr("N/A")));
246
247         //Init buttons
248         ui->buttonCancel->setEnabled(false);
249         ui->buttonRetry->hide();
250         ui->buttonDownload->hide();
251
252         //Hide labels
253         ui->labelInfo->hide();
254         ui->labelUrl->hide();
255
256         //Update status
257         threadStatusChanged(MUtils::UpdateChecker::UpdateStatus_NotStartedYet);
258
259         //Start animation
260         SHOW_ANIMATION(true);
261
262         //Update cursor
263         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
264         QApplication::setOverrideCursor(Qt::WaitCursor);
265
266         //Clear log
267         m_logFile.clear();
268
269         //Start the updater thread
270         QTimer::singleShot(250, m_thread.data(), SLOT(start()));
271 }
272
273 void UpdaterDialog::threadStatusChanged(int status)
274 {
275         switch(m_status = status)
276         {
277         case MUtils::UpdateChecker::UpdateStatus_NotStartedYet:
278                 UPDATE_ICON(1, "clock");
279                 UPDATE_ICON(2, "clock");
280                 UPDATE_ICON(3, "clock");
281                 break;
282         case MUtils::UpdateChecker::UpdateStatus_CheckingConnection:
283                 UPDATE_ICON(1, "play");
284                 break;
285         case MUtils::UpdateChecker::UpdateStatus_FetchingUpdates:
286                 UPDATE_ICON(1, "shield_green");
287                 UPDATE_TEXT(1, tr("Internet connection is working."));
288                 UPDATE_ICON(2, "play");
289                 break;
290         case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
291                 UPDATE_ICON(1, "shield_error");
292                 UPDATE_TEXT(1, tr("Computer is currently offline!"));
293                 UPDATE_ICON(2, "shield_grey");
294                 UPDATE_ICON(3, "shield_grey");
295                 break;
296         case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
297                 UPDATE_ICON(1, "shield_error");
298                 UPDATE_TEXT(1, tr("Internet connectivity test failed!"));
299                 UPDATE_ICON(2, "shield_grey");
300                 UPDATE_ICON(3, "shield_grey");
301                 break;
302         case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
303                 UPDATE_ICON(2, "shield_error");
304                 UPDATE_TEXT(2, tr("Failed to download the update information!"));
305                 UPDATE_ICON(3, "shield_grey");
306                 break;
307         case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
308         case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
309         case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
310                 UPDATE_ICON(2, "shield_green");
311                 UPDATE_TEXT(2, tr("Update information received successfully."));
312                 UPDATE_ICON(3, "play");
313                 break;
314         default:
315                 THROW("Unknown status code!");
316         }
317 }
318
319 void UpdaterDialog::threadFinished(void)
320 {
321         m_success = m_thread->getSuccess();
322         QTimer::singleShot((m_success ? 1000 : 0), this, SLOT(updateFinished()));
323 }
324
325 void UpdaterDialog::updateFinished(void)
326 {
327         //Restore cursor
328         QApplication::restoreOverrideCursor();
329
330         //If update was successfull, process final updater state
331         if(m_thread->getSuccess())
332         {
333                 switch(m_status)
334                 {
335                 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
336                         UPDATE_ICON(3, "shield_exclamation");
337                         UPDATE_TEXT(3, tr("A newer version is available!"));
338                         ui->buttonDownload->show();
339                         break;
340                 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
341                         UPDATE_ICON(3, "shield_green");
342                         UPDATE_TEXT(3, tr("Your version is up-to-date."));
343                         break;
344                 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
345                         UPDATE_ICON(3, "shield_blue");
346                         UPDATE_TEXT(3, tr("You are using a pre-release version!"));
347                         break;
348                 default:
349                         qWarning("Update thread succeeded with unexpected status code: %d", m_status);
350                 }
351         }
352
353         //Show update info or retry button
354         switch(m_status)
355         {
356         case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
357         case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
358         case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
359                 SHOW_ANIMATION(false);
360                 ui->labelBuildNo->setText(tr("Installed build is #%1  |  Latest build is #%2").arg(QString::number(x264_version_build()), QString::number(m_thread->getUpdateInfo()->getBuildNo())));
361                 ui->labelUrl->setText(QString("<a href=\"%1\">%1</a>").arg(m_thread->getUpdateInfo()->getDownloadSite()));
362                 break;
363         case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
364         case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
365         case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
366                 m_animator->stop();
367                 ui->buttonRetry->show();
368                 break;
369         default:
370                 qWarning("Update thread finished with unexpected status code: %d", m_status);
371         }
372
373         //Re-enbale cancel button
374         ui->buttonCancel->setEnabled(true);
375         
376 }
377
378 void UpdaterDialog::threadMessageLogged(const QString &message)
379 {
380         m_logFile << message;
381 }
382
383 void UpdaterDialog::openUrl(const QString &url)
384 {
385         qDebug("Open URL: %s", url.toLatin1().constData());
386         QDesktopServices::openUrl(QUrl(url));
387 }
388
389 void UpdaterDialog::installUpdate(void)
390 {
391         if(!((m_thread) && m_thread->getSuccess()))
392         {
393                 qWarning("Cannot download/install update at this point!");
394                 return;
395         }
396
397         QApplication::setOverrideCursor(Qt::WaitCursor);
398         ui->buttonDownload->hide();
399         ui->buttonCancel->setEnabled(false);
400         SHOW_ANIMATION(true);
401
402         const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo();
403
404         QProcess process;
405         QStringList args;
406         QEventLoop loop;
407
408         x264_init_process(process, x264_temp_directory(), false);
409
410         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
411         connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
412
413         args << QString("/Location=%1").arg(updateInfo->getDownloadAddress());
414         args << QString("/Filename=%1").arg(updateInfo->getDownloadFilename());
415         args << QString("/TicketID=%1").arg(updateInfo->getDownloadFilecode());
416         args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath())); 
417         args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
418         args << QString("/AppTitle=Simple x264 Launcher (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
419
420         process.start(m_binaries.value("wupd.exe"), args);
421         if(!process.waitForStarted())
422         {
423                 QApplication::restoreOverrideCursor();
424                 SHOW_ANIMATION(false);
425                 QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!"));
426                 ui->buttonDownload->show();
427                 ui->buttonCancel->setEnabled(true);
428                 return;
429         }
430
431         m_updaterProcess = x264_process_id(process);
432         loop.exec(QEventLoop::ExcludeUserInputEvents);
433         
434         if(!process.waitForFinished())
435         {
436                 process.kill();
437                 process.waitForFinished();
438         }
439
440         m_updaterProcess = NULL;
441         QApplication::restoreOverrideCursor();
442         ui->buttonDownload->show();
443         ui->buttonCancel->setEnabled(true);
444         SHOW_ANIMATION(false);
445
446         if(process.exitCode() == 0)
447         {
448                 done(READY_TO_INSTALL_UPDATE);
449         }
450 }
451
452 ///////////////////////////////////////////////////////////////////////////////
453 // Private Functions
454 ///////////////////////////////////////////////////////////////////////////////
455
456 bool UpdaterDialog::checkBinaries(QString &wgetBin, QString &gpgvBin)
457 {
458         qDebug("[File Verification]");
459         m_binaries.clear();
460
461         //Validate hashes first
462         const QString tempPath = MUtils::temp_folder();
463         for(size_t i = 0; BINARIES[i].name; i++)
464         {
465                 const QString orgName = QString::fromLatin1(BINARIES[i].name);
466                 const QString binPath = QString("%1/toolset/common/%2").arg(m_sysinfo->getAppPath(), orgName);
467                 const QString outPath = QString("%1/%2_%3.%4").arg(tempPath, QFileInfo(orgName).baseName(), MUtils::rand_str(), QFileInfo(orgName).suffix());
468                 if(!checkFileHash(binPath, BINARIES[i].hash))
469                 {
470                         qWarning("Verification of '%s' has failed!", MUTILS_UTF8(orgName));
471                         return false;
472                 }
473                 if(!QFile::copy(binPath, outPath))
474                 {
475                         qWarning("Copying of '%s' has failed!", MUTILS_UTF8(orgName));
476                         return false;
477                 }
478                 QFile::setPermissions(outPath, QFile::ReadOwner);
479                 m_binaries.insert(BINARIES[i].name, outPath);
480                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
481         }
482
483         return true;
484 }
485
486 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
487 {
488         qDebug("Checking file: %s", filePath.toUtf8().constData());
489         MUtils::Hash::Blake2 checksum2;
490         QFile file(filePath);
491         if(file.open(QIODevice::ReadOnly))
492         {
493                 checksum2.update(file);
494                 const QByteArray fileHash = checksum2.finalize();
495                 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
496                 {
497                         qWarning("\nFile appears to be corrupted:\n%s\n", filePath.toUtf8().constData());
498                         qWarning("Expected Hash: %s\nDetected Hash: %s\n", expectedHash, fileHash.constData());
499                         return false;
500                 }
501                 return true;
502         }
503         else
504         {
505                 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
506                 return false;
507         }
508 }
509
510 void UpdaterDialog::cleanFiles(void)
511 {
512         const QStringList keys = m_binaries.keys();
513         foreach(const QString &key, keys)
514         {
515                 const QString fileName = m_binaries.value(key);
516                 QFile::setPermissions(fileName, QFile::ReadOwner | QFile::WriteOwner);
517                 if(!QFile::remove(fileName))
518                 {
519                         qWarning("Failed to remove file: %s", MUTILS_UTF8(fileName));
520                 }
521                 m_binaries.remove(key);
522         }
523 }