Updated changelog.
[LameXP.git] / src / Dialog_Update.cpp
blob5ad8e589fd6bf706a6aaec184de6973c823b6a6b
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Dialog_Update.h"
25 //UIC includes
26 #include "UIC_UpdateDialog.h"
28 //LameXP includes
29 #include "Global.h"
30 #include "Dialog_LogView.h"
31 #include "Model_Settings.h"
33 //MUtils
34 #include <MUtils/UpdateChecker.h>
35 #include <MUtils/Version.h>
36 #include <MUtils/Exception.h>
37 #include <MUtils/Sound.h>
38 #include <MUtils/GUI.h>
39 #include <MUtils/OSSupport.h>
40 #include <MUtils/Taskbar7.h>
42 //Qt includes
43 #include <QClipboard>
44 #include <QFileDialog>
45 #include <QTimer>
46 #include <QProcess>
47 #include <QDesktopServices>
48 #include <QUrl>
49 #include <QCloseEvent>
50 #include <QMovie>
51 #include <QMessageBox>
53 ///////////////////////////////////////////////////////////////////////////////
55 #define SHOW_HINT(TEXT, ICON) do \
56 { \
57 ui->hintLabel->setText((TEXT)); \
58 ui->hintIcon->setPixmap((ICON)->pixmap(16,16)); \
59 ui->hintIcon->show(); \
60 ui->hintLabel->show(); \
61 } \
62 while(0)
64 #define UPDATE_TASKBAR(STATE, ICON) do \
65 { \
66 m_taskbar->setTaskbarState((STATE)); \
67 m_taskbar->setOverlayIcon((ICON).data()); \
68 } \
69 while(0)
71 ///////////////////////////////////////////////////////////////////////////////
73 UpdateDialog::UpdateDialog(const SettingsModel *const settings, QWidget *parent)
75 QDialog(parent),
76 ui(new Ui::UpdateDialog),
77 m_taskbar(new MUtils::Taskbar7(parent)),
78 m_settings(settings),
79 m_logFile(new QStringList()),
80 m_betaUpdates(settings ? (settings->autoUpdateCheckBeta() || lamexp_version_test()) : lamexp_version_test()),
81 m_success(false),
82 m_haveNewVersion(false),
83 m_firstShow(true),
84 m_updateReadyToInstall(false),
85 m_updaterProcess(NULL),
86 m_binaryUpdater(lamexp_tools_lookup("wupdate.exe")),
87 m_binaryCurl(lamexp_tools_lookup("curl.exe")),
88 m_binaryVerify(lamexp_tools_lookup("verify.exe"))
90 if(m_binaryUpdater.isEmpty() || m_binaryCurl.isEmpty() || m_binaryVerify.isEmpty())
92 MUTILS_THROW("Tools not initialized correctly!");
95 //Init the dialog, from the .ui file
96 ui->setupUi(this);
97 setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
99 //Disable "X" button
100 MUtils::GUI::enable_close_button(this, false);
102 //Load the icons
103 m_iconTransmitting.reset (new QIcon(":/icons/transmit_blue.png"));
104 m_iconFailure.reset (new QIcon(":/icons/exclamation.png"));
105 m_iconUpdateAvailable.reset(new QIcon(":/icons/shield_exclamation.png"));
106 m_iconNoUpdates.reset (new QIcon(":/icons/shield_green.png"));
107 m_iconNewVersionOlder.reset(new QIcon(":/icons/shield_blue.png"));
108 m_iconVersionError.reset (new QIcon(":/icons/shield_error.png"));
109 m_iconNetworkError.reset (new QIcon(":/icons/network_error.png"));
110 m_iconServerError.reset (new QIcon(":/icons/server_error.png"));
112 //Init animation
113 m_animator.reset(new QMovie(":/images/Loading3.gif"));
114 ui->labelAnimationCenter->setMovie(m_animator.data());
115 m_animator->start();
117 //Indicate beta updates
118 if(m_betaUpdates)
120 setWindowTitle(windowTitle().append(" [Beta]"));
123 //Enable button
124 connect(ui->retryButton, SIGNAL(clicked()), this, SLOT(checkForUpdates()));
125 connect(ui->installButton, SIGNAL(clicked()), this, SLOT(applyUpdate()));
126 connect(ui->infoLabel, SIGNAL(linkActivated(QString)), this, SLOT(linkActivated(QString)));
127 connect(ui->logButton, SIGNAL(clicked()), this, SLOT(logButtonClicked()));
129 //Enable progress bar
130 connect(ui->progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressBarValueChanged(int)));
133 UpdateDialog::~UpdateDialog(void)
135 if(m_animator)
137 m_animator->stop();
140 if(!m_thread.isNull())
142 if(!m_thread->wait(1000))
144 m_thread->terminate();
145 m_thread->wait();
149 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
150 m_taskbar->setOverlayIcon(NULL);
152 delete ui;
155 void UpdateDialog::showEvent(QShowEvent *event)
157 QDialog::showEvent(event);
159 if(m_firstShow)
161 if(m_thread.isNull())
163 m_thread.reset(new MUtils::UpdateChecker(m_binaryCurl, m_binaryVerify, QLatin1String("LameXP"), lamexp_version_build(), m_betaUpdates));
164 connect(m_thread.data(), SIGNAL(statusChanged(int)), this, SLOT(threadStatusChanged(int)));
165 connect(m_thread.data(), SIGNAL(progressChanged(int)), this, SLOT(threadProgressChanged(int)));
166 connect(m_thread.data(), SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
167 connect(m_thread.data(), SIGNAL(finished()), this, SLOT(threadFinished()));
168 connect(m_thread.data(), SIGNAL(terminated()), this, SLOT(threadFinished()));
171 threadStatusChanged(m_thread->getUpdateStatus());
172 ui->labelVersionInstalled->setText(QString("%1 %2 (%3)").arg(tr("Build"), QString::number(lamexp_version_build()), MUtils::Version::app_build_date().toString(Qt::ISODate)));
173 ui->labelVersionLatest->setText(QString("(%1)").arg(tr("Unknown")));
175 ui->installButton->setEnabled(false);
176 ui->closeButton->setEnabled(false);
177 ui->retryButton->setEnabled(false);
178 ui->logButton->setEnabled(false);
179 ui->retryButton->hide();
180 ui->logButton->hide();
181 ui->infoLabel->hide();
182 ui->hintLabel->hide();
183 ui->hintIcon->hide();
184 ui->frameAnimation->hide();
185 ui->cancelLabel->hide();
187 ui->progressBar->setMaximum(m_thread->getMaximumProgress());
188 ui->progressBar->setValue(0);
190 m_updaterProcess = NULL;
192 QTimer::singleShot(0, this, SLOT(updateInit()));
193 m_firstShow = false;
197 void UpdateDialog::closeEvent(QCloseEvent *event)
199 if(!ui->closeButton->isEnabled())
201 event->ignore();
203 else
205 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
206 m_taskbar->setOverlayIcon(NULL);
210 void UpdateDialog::keyPressEvent(QKeyEvent *e)
212 if (e->key() == Qt::Key_Escape)
214 if (!m_thread.isNull() && m_thread->isRunning())
216 ui->cancelLabel->hide();
217 ui->statusLabel->setText(tr("Stopping update check, please wait..."));
218 m_thread->cancel();
221 else if(e->key() == Qt::Key_F11)
223 if(ui->closeButton->isEnabled()) logButtonClicked();
225 else if((e->key() == Qt::Key_F12) && e->modifiers().testFlag(Qt::ControlModifier))
227 if(ui->closeButton->isEnabled()) testKnownHosts();
229 else
231 QDialog::keyPressEvent(e);
235 bool UpdateDialog::event(QEvent *e)
237 if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL))
239 MUtils::GUI::bring_to_front(m_updaterProcess);
241 return QDialog::event(e);
244 void UpdateDialog::updateInit(void)
246 setMinimumSize(size());
247 setMaximumHeight(height());
248 QTimer::singleShot(0, this, SLOT(checkForUpdates()));
251 void UpdateDialog::checkForUpdates(void)
253 if(m_thread->isRunning())
255 qWarning("Update in progress, cannot check for updates now!");
258 if(!MUtils::OS::user_is_admin())
260 qWarning("User is not in the \"admin\" group, cannot update!");
261 QString message;
262 message += QString("<nobr>%1</nobr><br>").arg(tr("Sorry, but only users in the \"Administrators\" group can install updates."));
263 message += QString("<nobr>%1</nobr>").arg(tr("Please start application from an administrator account and try again!"));
264 if(QMessageBox::critical(this, this->windowTitle(), message, tr("Discard"), tr("Ignore")) != 1)
266 ui->closeButton->setEnabled(true);
267 close(); return;
271 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NORMAL);
272 m_taskbar->setOverlayIcon(m_iconTransmitting.data());
274 ui->progressBar->setValue(0);
275 ui->installButton->setEnabled(false);
276 ui->closeButton->setEnabled(false);
277 ui->retryButton->setEnabled(false);
278 ui->logButton->setEnabled(false);
279 if(ui->infoLabel->isVisible()) ui->infoLabel->hide();
280 if(ui->hintLabel->isVisible()) ui->hintLabel->hide();
281 if(ui->hintIcon->isVisible()) ui->hintIcon->hide();
282 ui->cancelLabel->show();
283 ui->frameAnimation->show();
285 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
286 QApplication::setOverrideCursor(Qt::WaitCursor);
288 m_logFile->clear();
289 m_thread->start();
292 void UpdateDialog::threadStatusChanged(const int status)
294 switch(status)
296 case MUtils::UpdateChecker::UpdateStatus_NotStartedYet:
297 ui->statusLabel->setText(tr("Initializing, please wait..."));
298 break;
299 case MUtils::UpdateChecker::UpdateStatus_CheckingConnection:
300 ui->statusLabel->setText(tr("Testing your internet connection, please wait..."));
301 break;
302 case MUtils::UpdateChecker::UpdateStatus_FetchingUpdates:
303 ui->statusLabel->setText(tr("Checking for new updates online, please wait..."));
304 break;
305 case MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable:
306 ui->statusLabel->setText(tr("A new version of LameXP is available!"));
307 SHOW_HINT(tr("We highly recommend all users to install this update as soon as possible."), m_iconUpdateAvailable);
308 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconUpdateAvailable);
309 break;
310 case MUtils::UpdateChecker::UpdateStatus_CompletedNoUpdates:
311 ui->statusLabel->setText(tr("No new updates available at this time."));
312 SHOW_HINT(tr("Your version of LameXP is still up-to-date. Please check for updates regularly!"), m_iconNoUpdates);
313 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconNoUpdates);
314 break;
315 case MUtils::UpdateChecker::UpdateStatus_CompletedNewVersionOlder:
316 ui->statusLabel->setText(tr("Your version appears to be newer than the latest release."));
317 SHOW_HINT(tr("This usually indicates your are currently using a pre-release version of LameXP."), m_iconNewVersionOlder);
318 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconVersionError);
319 break;
320 case MUtils::UpdateChecker::UpdateStatus_ErrorNoConnection:
321 ui->statusLabel->setText(tr("It appears that the computer currently is offline!"));
322 SHOW_HINT(tr("Please make sure your computer is connected to the internet and try again."), m_iconNetworkError);
323 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconFailure);
324 break;
325 case MUtils::UpdateChecker::UpdateStatus_ErrorConnectionTestFailed:
326 ui->statusLabel->setText(tr("Network connectivity test has failed!"));
327 SHOW_HINT(tr("Please make sure your computer is connected to the internet and try again."), m_iconNetworkError);
328 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconFailure);
329 break;
330 case MUtils::UpdateChecker::UpdateStatus_ErrorFetchUpdateInfo:
331 ui->statusLabel->setText(tr("Failed to fetch update information from server!"));
332 SHOW_HINT(tr("Sorry, the update server might be busy at this time. Plase try again later."), m_iconNetworkError);
333 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconFailure);
334 break;
335 case MUtils::UpdateChecker::UpdateStatus_CancelledByUser:
336 ui->statusLabel->setText(tr("Update check has been cancelled!"));
337 SHOW_HINT(tr("The update check has been cancelled by the user. Please try again later."), m_iconServerError);
338 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_NORMAL, m_iconFailure);
339 break;
340 default:
341 qWarning("Unknown status %d !!!", int(status));
345 void UpdateDialog::threadProgressChanged(const int progress)
347 ui->progressBar->setValue(progress);
350 void UpdateDialog::threadMessageLogged(const QString &message)
352 (*m_logFile) << message;
355 void UpdateDialog::threadFinished(void)
357 const bool bSuccess = m_thread->getSuccess();
359 ui->closeButton->setEnabled(true);
360 ui->cancelLabel->hide();
361 if(ui->frameAnimation->isVisible()) ui->frameAnimation->hide();
362 ui->progressBar->setValue(ui->progressBar->maximum());
364 if(!bSuccess)
366 if(m_settings->soundsEnabled()) MUtils::Sound::play_sound("error", true);
368 else
370 const bool bHaveNewVersion = (m_thread->getUpdateStatus() == MUtils::UpdateChecker::UpdateStatus_CompletedUpdateAvailable);
371 ui->installButton->setEnabled(bHaveNewVersion);
372 MUtils::Sound::beep(bHaveNewVersion ? MUtils::Sound::BEEP_NFO : MUtils::Sound::BEEP_WRN);
374 if(const MUtils::UpdateCheckerInfo *const updateInfo = m_thread->getUpdateInfo())
376 ui->infoLabel->setText(QString("%1<br><a href=\"%2\">%2</a>").arg(tr("More information available at:"), updateInfo->getDownloadSite()));
377 ui->labelVersionLatest->setText(QString("%1 %2 (%3)").arg(tr("Build"), QString::number(updateInfo->getBuildNo()), updateInfo->getBuildDate().toString(Qt::ISODate)));
378 ui->infoLabel->show();
381 m_success = true;
382 m_haveNewVersion = bHaveNewVersion;
385 ui->retryButton->setVisible(!bSuccess);
386 ui->logButton->setVisible(!bSuccess);
387 ui->retryButton->setEnabled(!bSuccess);
388 ui->logButton->setEnabled(!bSuccess);
390 QApplication::restoreOverrideCursor();
393 void UpdateDialog::linkActivated(const QString &link)
395 QDesktopServices::openUrl(QUrl(link));
398 void UpdateDialog::applyUpdate(void)
400 ui->installButton->setEnabled(false);
401 ui->closeButton->setEnabled(false);
402 ui->retryButton->setEnabled(false);
404 if(const MUtils::UpdateCheckerInfo *updateInfo = m_thread->getUpdateInfo())
406 ui->statusLabel->setText(tr("Update is being downloaded, please be patient..."));
407 ui->frameAnimation->show();
408 if(ui->hintLabel->isVisible()) ui->hintLabel->hide();
409 if(ui->hintIcon->isVisible()) ui->hintIcon->hide();
410 int oldMax = ui->progressBar->maximum();
411 int oldMin = ui->progressBar->minimum();
412 ui->progressBar->setRange(0, 0);
413 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
415 QProcess process;
416 QStringList args;
417 QEventLoop loop;
419 MUtils::init_process(process, QFileInfo(m_binaryUpdater).absolutePath(), false);
421 connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
422 connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), &loop, SLOT(quit()));
424 args << QString("/Location=%1").arg(updateInfo->getDownloadAddress());
425 args << QString("/Filename=%1").arg(updateInfo->getDownloadFilename());
426 args << QString("/TicketID=%1").arg(updateInfo->getDownloadFilecode());
427 args << QString("/CheckSum=%1").arg(updateInfo->getDownloadChecksum());
428 args << QString("/ToFolder=%1").arg(QDir::toNativeSeparators(QDir(QApplication::applicationDirPath()).canonicalPath()));
429 args << QString("/ToExFile=%1.exe").arg(QFileInfo(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()).completeBaseName());
430 args << QString("/AppTitle=LameXP (Build #%1)").arg(QString::number(updateInfo->getBuildNo()));
432 QApplication::setOverrideCursor(Qt::WaitCursor);
433 UPDATE_TASKBAR(MUtils::Taskbar7::TASKBAR_STATE_INTERMEDIATE, m_iconTransmitting);
435 process.start(m_binaryUpdater, args);
436 bool updateStarted = process.waitForStarted();
437 if(updateStarted)
439 m_updaterProcess = MUtils::OS::process_id(&process);
440 loop.exec(QEventLoop::ExcludeUserInputEvents);
443 m_updaterProcess = NULL;
444 QApplication::restoreOverrideCursor();
446 ui->hintLabel->show();
447 ui->hintIcon->show();
448 ui->progressBar->setRange(oldMin, oldMax);
449 ui->progressBar->setValue(oldMax);
450 ui->frameAnimation->hide();
452 if(updateStarted && (process.exitCode() == 0))
454 ui->statusLabel->setText(tr("Update ready to install. Applicaion will quit..."));
455 m_updateReadyToInstall = true;
456 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_NONE);
457 m_taskbar->setOverlayIcon(NULL);
458 accept();
460 else
462 ui->statusLabel->setText(tr("Update failed. Please try again or download manually!"));
463 m_taskbar->setTaskbarState(MUtils::Taskbar7::TASKBAR_STATE_ERROR);
464 m_taskbar->setOverlayIcon(m_iconFailure.data());
465 m_taskbar->setTaskbarProgress(100, 100);
469 ui->installButton->setEnabled(true);
470 ui->closeButton->setEnabled(true);
473 void UpdateDialog::logButtonClicked(void)
475 LogViewDialog *logView = new LogViewDialog(this);
476 logView->exec(*m_logFile);
477 MUTILS_DELETE(logView);
480 void UpdateDialog::progressBarValueChanged(int value)
482 m_taskbar->setTaskbarProgress(value, ui->progressBar->maximum());
485 void UpdateDialog::testKnownHosts(void)
487 ui->statusLabel->setText("Testing all known hosts, this may take a few minutes...");
489 if(MUtils::UpdateChecker *testThread = new MUtils::UpdateChecker(m_binaryCurl, m_binaryVerify, QLatin1String("LameXP"), lamexp_version_build(), m_betaUpdates, true))
491 QEventLoop loop;
492 m_logFile->clear();
494 connect(testThread, SIGNAL(messageLogged(QString)), this, SLOT(threadMessageLogged(QString)));
495 connect(testThread, SIGNAL(finished()), &loop, SLOT(quit()));
496 connect(testThread, SIGNAL(terminated()), &loop, SLOT(quit()));
498 testThread->start();
500 ui->progressBar->setMaximum(0);
501 ui->progressBar->setMinimum(0);
503 bool status[4];
504 status[0] = ui->closeButton ->isEnabled(); ui->closeButton ->setEnabled(false);
505 status[1] = ui->installButton->isEnabled(); ui->installButton->setEnabled(false);
506 status[2] = ui->retryButton ->isEnabled(); ui->retryButton ->setEnabled(false);
507 status[3] = ui->logButton ->isEnabled(); ui->logButton ->setEnabled(false);
509 while(testThread->isRunning())
511 QTimer::singleShot(8000, &loop, SLOT(quit()));
512 loop.exec(QEventLoop::ExcludeUserInputEvents);
515 ui->progressBar->setMaximum(m_thread.isNull() ? 100 : m_thread->getMaximumProgress());
516 ui->progressBar->setValue(ui->progressBar->maximum());
518 ui->closeButton ->setEnabled(status[0]);
519 ui->installButton->setEnabled(status[1]);
520 ui->retryButton ->setEnabled(status[2]);
521 ui->logButton ->setEnabled(status[3]);
523 MUTILS_DELETE(testThread);
524 logButtonClicked();
527 ui->statusLabel->setText("Test completed.");
528 MUtils::Sound::beep(MUtils::Sound::BEEP_NFO);