OSDN Git Service

Happy new year 2014!
[x264-launcher/x264-launcher.git] / src / win_updater.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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 #include "global.h"
26 #include "thread_updater.h"
27 #include "checksum.h"
28
29 #include <QMovie>
30 #include <QCloseEvent>
31 #include <QTimer>
32 #include <QMessageBox>
33 #include <QDesktopServices>
34 #include <QUrl>
35 #include <QProcess>
36 #include <QFileInfo>
37 #include <QDir>
38 #include <QMap>
39
40 ///////////////////////////////////////////////////////////////////////////////
41
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))
44
45 /*
46 ui->labelLoadingLeft->setVisible((FLAG)); \
47 ui->labelLoadingCenter->setVisible((FLAG)); \
48 ui->labelLoadingRight->setVisible((FLAG)); \
49 */
50
51 #define SHOW_ANIMATION(FLAG) do  \
52 { \
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(); \
58 } \
59 while(0)
60
61
62 ///////////////////////////////////////////////////////////////////////////////
63 // Constructor & Destructor
64 ///////////////////////////////////////////////////////////////////////////////
65
66 UpdaterDialog::UpdaterDialog(QWidget *parent, const QString &binDir)
67 :
68         QDialog(parent),
69         ui(new Ui::UpdaterDialog()),
70         m_binDir(binDir),
71         m_status(UpdateCheckThread::UpdateStatus_NotStartedYet),
72         m_thread(NULL),
73         m_updaterProcess(NULL),
74         m_success(false),
75         m_firstShow(true)
76 {
77         //Init the dialog, from the .ui file
78         ui->setupUi(this);
79         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
80
81         //Fix size
82         setFixedSize(size());
83
84         //Enable buttons
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()));
88         
89         //Enable info label
90         connect(ui->labelUrl, SIGNAL(linkActivated(QString)), this, SLOT(openUrl(QString)));
91         
92         //Init animation
93         m_animator = new QMovie(":/images/loading.gif");
94         ui->labelLoadingCenter->setMovie(m_animator);
95
96         //Init buttons
97         ui->buttonCancel->setEnabled(false);
98         ui->buttonRetry->hide();
99         ui->buttonDownload->hide();
100
101         //Start animation
102         SHOW_ANIMATION(true);
103 }
104
105 UpdaterDialog::~UpdaterDialog(void)
106 {
107         if(m_thread)
108         {
109                 if(!m_thread->wait(1000))
110                 {
111                         m_thread->terminate();
112                         m_thread->wait();
113                 }
114         }
115
116         if((!m_keysFile.isEmpty()) && QFile::exists(m_keysFile))
117         {
118                 QFile::setPermissions(m_keysFile, QFile::ReadOwner | QFile::WriteOwner);
119                 QFile::remove(m_keysFile);
120                 m_keysFile.clear();
121         }
122
123         X264_DELETE(m_thread);
124         X264_DELETE(m_animator);
125
126         delete ui;
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // Public Functions
131 ///////////////////////////////////////////////////////////////////////////////
132
133 /*None yet*/
134
135 ///////////////////////////////////////////////////////////////////////////////
136 // Events
137 ///////////////////////////////////////////////////////////////////////////////
138
139 bool UpdaterDialog::event(QEvent *e)
140 {
141         if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
142         {
143                 x264_bring_process_to_front(m_updaterProcess);
144         }
145         return QDialog::event(e);
146 }
147
148 void UpdaterDialog::showEvent(QShowEvent *event)
149 {
150         if(m_firstShow)
151         {
152                 m_firstShow = false;
153                 QTimer::singleShot(16, this, SLOT(initUpdate()));
154         }
155 }
156
157 void UpdaterDialog::closeEvent(QCloseEvent *e)
158 {
159         if(!ui->buttonCancel->isEnabled())
160         {
161                 e->ignore();
162         }
163 }
164
165 void UpdaterDialog::keyPressEvent(QKeyEvent *event)
166 {
167         if(event->key() == Qt::Key_F11)
168         {
169                 QFile logFile(QString("%1/%2.log").arg(x264_temp_directory(), x264_rand_str()));
170                 if(logFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
171                 {
172                         logFile.write("\xEF\xBB\xBF");
173                         for(QStringList::ConstIterator iter = m_logFile.constBegin(); iter != m_logFile.constEnd(); iter++)
174                         {
175                                 logFile.write(iter->toUtf8());
176                                 logFile.write("\r\n");
177                         }
178                         logFile.close();
179                         QDesktopServices::openUrl(QUrl::fromLocalFile(logFile.fileName()));
180                 }
181         }
182 }
183
184 ///////////////////////////////////////////////////////////////////////////////
185 // Slots
186 ///////////////////////////////////////////////////////////////////////////////
187
188 void UpdaterDialog::initUpdate(void)
189 {
190         //Check binary files
191         QString wgetBin, gpgvBin;
192         if(!checkBinaries(wgetBin, gpgvBin))
193         {
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!"));
196                 close(); return;
197         }
198
199         //Make sure user does have admin access
200         if(!x264_user_is_admin())
201         {
202                 qWarning("User is not in the \"admin\" group, cannot update!");
203                 QString message;
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)
207                 {
208                         ui->buttonCancel->setEnabled(true);
209                         close(); return;
210                 }
211         }
212         
213         //Create and setup thread
214         if(!m_thread)
215         {
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)));
221         }
222
223         //Begin updater run
224         QTimer::singleShot(16, this, SLOT(checkForUpdates()));
225 }
226
227 void UpdaterDialog::checkForUpdates(void)
228 {
229         if((!m_thread) || m_thread->isRunning())
230         {
231                 qWarning("Update in progress, cannot check for updates now!");
232         }
233
234         //Clear texts
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")));
237
238         //Init buttons
239         ui->buttonCancel->setEnabled(false);
240         ui->buttonRetry->hide();
241         ui->buttonDownload->hide();
242
243         //Hide labels
244         ui->labelInfo->hide();
245         ui->labelUrl->hide();
246
247         //Update status
248         threadStatusChanged(UpdateCheckThread::UpdateStatus_NotStartedYet);
249
250         //Start animation
251         SHOW_ANIMATION(true);
252
253         //Update cursor
254         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
255         QApplication::setOverrideCursor(Qt::WaitCursor);
256
257         //Clear log
258         m_logFile.clear();
259
260         //Start the updater thread
261         QTimer::singleShot(250, m_thread, SLOT(start()));
262 }
263
264 void UpdaterDialog::threadStatusChanged(int status)
265 {
266         switch(m_status = status)
267         {
268         case UpdateCheckThread::UpdateStatus_NotStartedYet:
269                 UPDATE_ICON(1, "clock");
270                 UPDATE_ICON(2, "clock");
271                 UPDATE_ICON(3, "clock");
272                 break;
273         case UpdateCheckThread::UpdateStatus_CheckingConnection:
274                 UPDATE_ICON(1, "play");
275                 break;
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");
280                 break;
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");
286                 break;
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");
292                 break;
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");
297                 break;
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");
304                 break;
305         default:
306                 throw "Unknown status code!";
307         }
308 }
309
310 void UpdaterDialog::threadFinished(void)
311 {
312         m_success = m_thread->getSuccess();
313         QTimer::singleShot((m_success ? 1000 : 0), this, SLOT(updateFinished()));
314 }
315
316 void UpdaterDialog::updateFinished(void)
317 {
318         //Restore cursor
319         QApplication::restoreOverrideCursor();
320
321         //If update was successfull, process final updater state
322         if(m_thread->getSuccess())
323         {
324                 switch(m_status)
325                 {
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();
330                         break;
331                 case UpdateCheckThread::UpdateStatus_CompletedNoUpdates:
332                         UPDATE_ICON(3, "shield_green");
333                         UPDATE_TEXT(3, tr("Your version is up-to-date."));
334                         break;
335                 case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder:
336                         UPDATE_ICON(3, "shield_blue");
337                         UPDATE_TEXT(3, tr("You are using a pre-release version!"));
338                         break;
339                 default:
340                         qWarning("Update thread succeeded with unexpected status code: %d", m_status);
341                 }
342         }
343
344         //Show update info or retry button
345         switch(m_status)
346         {
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));
353                 break;
354         case UpdateCheckThread::UpdateStatus_ErrorNoConnection:
355         case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed:
356         case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo:
357                 m_animator->stop();
358                 ui->buttonRetry->show();
359                 break;
360         default:
361                 qWarning("Update thread finished with unexpected status code: %d", m_status);
362         }
363
364         //Re-enbale cancel button
365         ui->buttonCancel->setEnabled(true);
366         
367 }
368
369 void UpdaterDialog::threadMessageLogged(const QString &message)
370 {
371         m_logFile << message;
372 }
373
374 void UpdaterDialog::openUrl(const QString &url)
375 {
376         qDebug("Open URL: %s", url.toLatin1().constData());
377         QDesktopServices::openUrl(QUrl(url));
378 }
379
380 void UpdaterDialog::installUpdate(void)
381 {
382         if(!((m_thread) && m_thread->getSuccess()))
383         {
384                 qWarning("Cannot download/install update at this point!");
385                 return;
386         }
387
388         QApplication::setOverrideCursor(Qt::WaitCursor);
389         ui->buttonDownload->hide();
390         ui->buttonCancel->setEnabled(false);
391         SHOW_ANIMATION(true);
392
393         const UpdateInfo *updateInfo = m_thread->getUpdateInfo();
394
395         QProcess process;
396         QStringList args;
397         QEventLoop loop;
398
399         x264_init_process(process, x264_temp_directory(), false);
400
401         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
402         connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
403
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));
410
411         process.start(m_wupdFile, args);
412         if(!process.waitForStarted())
413         {
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);
419                 return;
420         }
421
422         m_updaterProcess = x264_process_id(process);
423         loop.exec(QEventLoop::ExcludeUserInputEvents);
424         
425         if(!process.waitForFinished())
426         {
427                 process.kill();
428                 process.waitForFinished();
429         }
430
431         m_updaterProcess = NULL;
432         QApplication::restoreOverrideCursor();
433         ui->buttonDownload->show();
434         ui->buttonCancel->setEnabled(true);
435         SHOW_ANIMATION(false);
436
437         if(process.exitCode() == 0)
438         {
439                 done(READY_TO_INSTALL_UPDATE);
440         }
441 }
442
443 ///////////////////////////////////////////////////////////////////////////////
444 // Private Functions
445 ///////////////////////////////////////////////////////////////////////////////
446
447 bool UpdaterDialog::checkBinaries(QString &wgetBin, QString &gpgvBin)
448 {
449         qDebug("[File Verification]");
450
451         static struct
452         {
453                 const char* name;
454                 const char* hash;
455         }
456         FILE_INFO[] =
457         {
458                 { "wget.exe", "7b522345239bcb95b5b0f7f50a883ba5957894a1feb769763e38ed789a8a0f63fead0155f54b9ffd0f1cdc5dfd855d207a6e7a8e4fd192589a8838ce646c504e" },
459                 { "gpgv.exe", "e61d28e4c47b2422ceec7b8fc08f9c70f10a3056e3779a974026eb24fe09551eedc2e7f34fbe5ef8e844fab0dbe68b85c4ca69d63bf85d445f7cae152c17f589" },
460                 { "gpgv.gpg", "58e0f0e462bbd0b5aa4f638801c1097da7da4b3eb38c8c88ad1db23705c0f11e174b083fa55fe76bd3ba196341c967833a6f3427d6f63ad8565900745535d8fa" },
461                 { "wupd.exe", "9dd0ae5f386aaf5df9e261a53b81569df51bccf2f9290ed42f04a5b52b4a4e1118f2c9ce3a9b2492fd5ae597600411dfa47bd7c96e2fd7bee205d603324452a2" },
462                 { NULL, NULL }
463         };
464
465         QMap<QString, QString> binaries;
466
467         m_keysFile.clear();
468         m_wupdFile.clear();
469         wgetBin.clear();
470         gpgvBin.clear();
471
472         bool okay = true;
473
474         for(size_t i = 0; FILE_INFO[i].name; i++)
475         {
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))
478                 {
479                         binaries.insert(FILE_INFO[i].name, binPath);
480                 }
481                 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
482         }
483
484         if(okay)
485         {
486                 wgetBin = binaries.value("wget.exe");
487                 gpgvBin = binaries.value("gpgv.exe");
488
489                 m_wupdFile = binaries.value("wupd.exe");
490                 m_keysFile = QString("%1/%2.gpg").arg(x264_temp_directory(), x264_rand_str());
491
492                 if(okay = QFile::copy(binaries.value("gpgv.gpg"), m_keysFile))
493                 {
494                         QFile::setPermissions(m_keysFile, QFile::ReadOwner);
495                 }
496                 qDebug("%s\n", okay ? "Completed." : "Failed to copy GPG file!");
497         }
498
499         return okay;
500 }
501
502 bool UpdaterDialog::checkFileHash(const QString &filePath, const char *expectedHash)
503 {
504         qDebug("Checking file: %s", filePath.toUtf8().constData());
505         QBlake2Checksum checksum2;
506         QFile file(filePath);
507         if(file.open(QIODevice::ReadOnly))
508         {
509                 checksum2.update(file);
510                 const QByteArray fileHash = checksum2.finalize();
511                 if((strlen(expectedHash) != fileHash.size()) || (memcmp(fileHash.constData(), expectedHash, fileHash.size()) != 0))
512                 {
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());
515                         return false;
516                 }
517                 return true;
518         }
519         else
520         {
521                 qWarning("Failed to open file:\n%s\n", filePath.toUtf8().constData());
522                 return false;
523         }
524 }