From 4b9a244f243e898b60e871c30cde99183d9b4273 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Fri, 13 Dec 2013 15:25:36 +0100 Subject: [PATCH] Some updates to the updater dialog. --- src/global.cpp | 58 +++++++++++++++++++++++++++++++++-- src/global.h | 2 ++ src/thread_updater.cpp | 2 +- src/win_main.cpp | 1 + src/win_updater.cpp | 69 ++++++++++++++++++++++++++++-------------- src/win_updater.h | 2 ++ 6 files changed, 107 insertions(+), 27 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index d256258..f6cb0ce 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -1715,6 +1715,18 @@ unsigned int x264_process_id(void) return GetCurrentProcessId(); } +/* + * Current process ID + */ +unsigned int x264_process_id(QProcess &process) +{ + if(Q_PID pid = process.pid()) + { + return pid->dwProcessId; + } + return NULL; +} + /* * Make a window blink (to draw user's attention) */ @@ -1766,14 +1778,54 @@ void x264_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay } } +/* + * Bring the specifed window to the front + */ +static bool x264_bring_to_front(const HWND hWin) +{ + if(hWin) + { + const bool ret = (SetForegroundWindow(hWin) != FALSE); + SwitchToThisWindow(hWin, TRUE); + return ret; + } + return false; +} + /* * Bring the specifed window to the front */ bool x264_bring_to_front(const QWidget *win) { - const bool ret = (SetForegroundWindow(win->winId()) == TRUE); - SwitchToThisWindow(win->winId(), TRUE); - return ret; + if(win) + { + return x264_bring_to_front(win->winId()); + } + return false; +} + +/* + * Bring window of the specifed process to the front (callback) + */ +static BOOL CALLBACK x264_bring_process_to_front_helper(HWND hwnd, LPARAM lParam) +{ + DWORD processId = *reinterpret_cast(lParam); + DWORD windowProcessId = NULL; + GetWindowThreadProcessId(hwnd, &windowProcessId); + if(windowProcessId == processId) + { + x264_bring_to_front(hwnd); + return FALSE; + } + return TRUE; +} + +/* + * Bring window of the specifed process to the front + */ +bool x264_bring_process_to_front(const unsigned long pid) +{ + return EnumWindows(x264_bring_process_to_front_helper, reinterpret_cast(&pid)) == TRUE; } /* diff --git a/src/global.h b/src/global.h index e378c76..07ab1a3 100644 --- a/src/global.h +++ b/src/global.h @@ -120,6 +120,7 @@ extern const x264_os_version_t x264_winver_win81; const QStringList &x264_arguments(void); bool x264_beep(int beepType); void x264_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay); +bool x264_bring_process_to_front(const unsigned long pid); bool x264_bring_to_front(const QWidget *win); bool x264_change_process_priority(const QProcess *proc, const int priority); bool x264_change_process_priority(const int priority); @@ -143,6 +144,7 @@ QString x264_path2ansi(const QString &longPath, bool makeLowercase = false); bool x264_play_sound(const unsigned short uiSoundIdx, const bool bAsync, const wchar_t *alias = NULL); bool x264_portable(void); unsigned int x264_process_id(void); +unsigned int x264_process_id(QProcess &process); QString x264_query_reg_string(const bool bUser, const QString &path, const QString &name); unsigned int x264_rand(void); QString x264_rand_str(const bool bLong = false); diff --git a/src/thread_updater.cpp b/src/thread_updater.cpp index 5cc75e8..617ba7a 100644 --- a/src/thread_updater.cpp +++ b/src/thread_updater.cpp @@ -36,7 +36,7 @@ /////////////////////////////////////////////////////////////////////////////// static const char *header_id = "!Update"; -static const char *section_id = "x264_launcher"; +static const char *section_id = "Simple x264 Launcher"; static const char *mirror_url_postfix[] = { diff --git a/src/win_main.cpp b/src/win_main.cpp index a03426d..60dfd8d 100644 --- a/src/win_main.cpp +++ b/src/win_main.cpp @@ -985,6 +985,7 @@ void MainWindow::checkUpdates(void) X264_DELETE(updater); qWarning("Exitting to install update..."); close(); + QApplication::quit(); } X264_DELETE(updater); diff --git a/src/win_updater.cpp b/src/win_updater.cpp index 6e7d094..c1c4e40 100644 --- a/src/win_updater.cpp +++ b/src/win_updater.cpp @@ -49,6 +49,7 @@ ui->labelLoadingRight->setVisible((FLAG)); \ ui->labelInfo->setVisible(!(FLAG)); \ ui->labelUrl->setVisible(!(FLAG)); \ + if((FLAG)) m_animator->start(); else m_animator->stop(); \ } \ while(0) @@ -64,6 +65,7 @@ UpdaterDialog::UpdaterDialog(QWidget *parent, const QString &binDir) m_binDir(binDir), m_status(UpdateCheckThread::UpdateStatus_NotStartedYet), m_thread(NULL), + m_updaterProcess(NULL), m_firstShow(true) { //Init the dialog, from the .ui file @@ -130,6 +132,15 @@ UpdaterDialog::~UpdaterDialog(void) // Events /////////////////////////////////////////////////////////////////////////////// +bool UpdaterDialog::event(QEvent *e) +{ + if((e->type() == QEvent::ActivationChange) && (m_updaterProcess != NULL)) + { + x264_bring_process_to_front(m_updaterProcess); + } + return QDialog::event(e); +} + void UpdaterDialog::showEvent(QShowEvent *event) { if(m_firstShow) @@ -220,7 +231,6 @@ void UpdaterDialog::checkForUpdates(void) //Start animation SHOW_ANIMATION(true); - m_animator->start(); //Update cursor QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); @@ -284,25 +294,27 @@ void UpdaterDialog::threadFinished(void) //Restore cursor QApplication::restoreOverrideCursor(); - //Stop animation - m_animator->stop(); - - //Process final updater state - switch(m_status) + //If update was successfull, process final updater state + if(m_thread->getSuccess()) { - case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable: - UPDATE_ICON(3, "shield_exclamation"); - UPDATE_TEXT(3, tr("A newer version is available!")); - ui->buttonDownload->show(); - break; - case UpdateCheckThread::UpdateStatus_CompletedNoUpdates: - UPDATE_ICON(3, "shield_green"); - UPDATE_TEXT(3, tr("Your version is up-to-date.")); - break; - case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder: - UPDATE_ICON(3, "shield_error"); - UPDATE_TEXT(3, tr("Your are using a pre-release version!")); - break; + switch(m_status) + { + case UpdateCheckThread::UpdateStatus_CompletedUpdateAvailable: + UPDATE_ICON(3, "shield_exclamation"); + UPDATE_TEXT(3, tr("A newer version is available!")); + ui->buttonDownload->show(); + break; + case UpdateCheckThread::UpdateStatus_CompletedNoUpdates: + UPDATE_ICON(3, "shield_green"); + UPDATE_TEXT(3, tr("Your version is up-to-date.")); + break; + case UpdateCheckThread::UpdateStatus_CompletedNewVersionOlder: + UPDATE_ICON(3, "shield_error"); + UPDATE_TEXT(3, tr("Your are using a pre-release version!")); + break; + default: + qWarning("Update thread succeeded with unexpected status code: %d", m_status); + } } //Show update info or retry button @@ -317,8 +329,11 @@ void UpdaterDialog::threadFinished(void) case UpdateCheckThread::UpdateStatus_ErrorNoConnection: case UpdateCheckThread::UpdateStatus_ErrorConnectionTestFailed: case UpdateCheckThread::UpdateStatus_ErrorFetchUpdateInfo: + m_animator->stop(); ui->buttonRetry->show(); break; + default: + qWarning("Update thread finished with unexpected status code: %d", m_status); } //Re-enbale cancel button @@ -345,6 +360,11 @@ void UpdaterDialog::installUpdate(void) return; } + QApplication::setOverrideCursor(Qt::WaitCursor); + ui->buttonDownload->hide(); + ui->buttonCancel->setEnabled(false); + SHOW_ANIMATION(true); + const UpdateInfo *updateInfo = m_thread->getUpdateInfo(); QProcess process; @@ -366,14 +386,15 @@ void UpdaterDialog::installUpdate(void) process.start(m_wupdFile, args); if(!process.waitForStarted()) { + QApplication::restoreOverrideCursor(); + SHOW_ANIMATION(false); QMessageBox::critical(this, tr("Update Failed"), tr("Sorry, failed to launch web-update program!")); + ui->buttonDownload->show(); + ui->buttonCancel->setEnabled(true); return; } - QApplication::setOverrideCursor(Qt::WaitCursor); - ui->buttonDownload->hide(); - ui->buttonCancel->setEnabled(false); - + m_updaterProcess = x264_process_id(process); loop.exec(QEventLoop::ExcludeUserInputEvents); if(!process.waitForFinished()) @@ -382,9 +403,11 @@ void UpdaterDialog::installUpdate(void) process.waitForFinished(); } + m_updaterProcess = NULL; QApplication::restoreOverrideCursor(); ui->buttonDownload->show(); ui->buttonCancel->setEnabled(true); + SHOW_ANIMATION(false); if(process.exitCode() == 0) { diff --git a/src/win_updater.h b/src/win_updater.h index 3afe5e4..fb516a9 100644 --- a/src/win_updater.h +++ b/src/win_updater.h @@ -40,6 +40,7 @@ public: ~UpdaterDialog(void); protected: + virtual bool event(QEvent *e); virtual void showEvent(QShowEvent *event); virtual void closeEvent(QCloseEvent *e); virtual void keyPressEvent(QKeyEvent *event); @@ -63,6 +64,7 @@ private: const QString m_binDir; QMovie *m_animator; UpdateCheckThread *m_thread; + unsigned long m_updaterProcess; QStringList m_logFile; QString m_keysFile; QString m_wupdFile;