From c7a24df9156993cdfa89975722fbba172c57a723 Mon Sep 17 00:00:00 2001 From: gera Date: Fri, 11 Jun 2021 00:12:02 +0300 Subject: [PATCH] Add recommends from review. --- CMakeLists.txt | 2 +- src/AudacityApp.cpp | 2 +- src/update/UpdateManager.cpp | 54 ++++++++++++++++---------------- src/update/UpdateManager.h | 11 +++---- src/update/UpdatePopupDialog.cpp | 4 +-- 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d305c064f..aa44af93f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,7 +193,7 @@ cmake_dependent_option( cmake_dependent_option( ${_OPT}has_updates_check - "Has a builtin support for checking of updates" + "Build updates checking features into Audacity" On "${_OPT}has_networking" Off diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index e30069e60..5bb5666e7 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1491,7 +1491,7 @@ bool AudacityApp::InitPart2() } #if defined(HAVE_UPDATES_CHECK) - mUpdateManager = std::make_unique(*project); + mUpdateManager = std::make_unique(); #endif #ifdef USE_FFMPEG diff --git a/src/update/UpdateManager.cpp b/src/update/UpdateManager.cpp index cba3d503d..9f3cf4a3b 100644 --- a/src/update/UpdateManager.cpp +++ b/src/update/UpdateManager.cpp @@ -14,10 +14,11 @@ #include "IResponse.h" #include "Request.h" -#include "widgets/AudacityMessageBox.h" +#include "widgets/ErrorDialog.h" #include #include +#include static const char* prefsUpdatePopupDialogShown = "/Update/UpdatePopupDialogShown"; static const char* prefsUpdateScheduledTime = "/Update/UpdateScheduledTime"; @@ -28,13 +29,10 @@ BEGIN_EVENT_TABLE(UpdateManager, wxEvtHandler) EVT_TIMER(ID_TIMER, UpdateManager::OnTimer) END_EVENT_TABLE() -UpdateManager::UpdateManager(AudacityProject& project) +UpdateManager::UpdateManager() : mTrackingInterval( std::chrono::milliseconds(std::chrono::hours(12)).count()) { - mParent = (wxWindow*)(&GetProjectFrame(project)); - wxASSERT(mParent); - mTimer.SetOwner(this, ID_TIMER); mTimer.StartOnce(); } @@ -44,13 +42,13 @@ UpdateManager::~UpdateManager() mTimer.Stop(); } -void UpdateManager::enableTracking(bool enable) +void UpdateManager::enableUpdatesChecking(bool enable) { gPrefs->Write(prefsUpdatePopupDialogShown, enable); gPrefs->Flush(); } -bool UpdateManager::isTrackingEnabled() +bool UpdateManager::isUpdatesCheckingEnabled() { return gPrefs->ReadBool(prefsUpdatePopupDialogShown, true); } @@ -62,48 +60,50 @@ VersionPatch UpdateManager::getVersionPatch() const void UpdateManager::getUpdates() { - audacity::network_manager::Request request("https://updates.audacityteam.org/feed/latest.xml"); + const audacity::network_manager::Request request("https://updates.audacityteam.org/feed/latest.xml"); auto response = audacity::network_manager::NetworkManager::GetInstance().doGet(request); response->setRequestFinishedCallback([response, this](audacity::network_manager::IResponse*) { + wxFrame* parent = FindProjectFrame(GetActiveProject()); + wxASSERT(parent); + + if (!parent) return; + if (response->getError() != audacity::network_manager::NetworkError::NoError) { - AudacityMessageBox( - XO("Unable to connect to Audacity update server."), + ShowExceptionDialog(parent, XO("Error checking for update"), - wxOK | wxCENTRE, - NULL); + XO("Unable to connect to Audacity update server."), + wxString()); return; } if (!mUpdateDataParser.Parse(response->readAll(), &mVersionPatch)) { - AudacityMessageBox( - XO("Update data was corrupted."), + ShowExceptionDialog(parent, XO("Error checking for update"), - wxOK | wxCENTRE, - NULL); + XO("Update data was corrupted."), + wxString()); return; } if (mVersionPatch.version > CurrentBuildVersion()) { - mParent->CallAfter([this] { - UpdatePopupDialog dlg(mParent, this); + parent->CallAfter([this, parent] { + UpdatePopupDialog dlg(parent, this); const int code = dlg.ShowModal(); if (code == wxID_YES) { if (!wxLaunchDefaultBrowser(mVersionPatch.download)) { - AudacityMessageBox( - XO("Can't open the Audacity download link."), + ShowExceptionDialog(parent, XO("Error downloading update"), - wxOK | wxCENTRE, - NULL); + XO("Can't open the Audacity download link."), + wxString()); return; } @@ -115,7 +115,7 @@ void UpdateManager::getUpdates() void UpdateManager::OnTimer(wxTimerEvent& WXUNUSED(event)) { - if (isTrackingEnabled() && isTimeToUpdate()) + if (isUpdatesCheckingEnabled() && isTimeToUpdate()) getUpdates(); mTimer.StartOnce(mTrackingInterval); @@ -123,7 +123,7 @@ void UpdateManager::OnTimer(wxTimerEvent& WXUNUSED(event)) bool UpdateManager::isTimeToUpdate() { - long long nextTrackingTime = std::stoll( + long long nextUpdatesCheckingTime = std::stoll( gPrefs->Read(prefsUpdateScheduledTime, "0").ToStdString()); // Get current time in milliseconds @@ -135,12 +135,12 @@ bool UpdateManager::isTimeToUpdate() // If next update time 0 or less then current time -> show update dialog, // else this condition allow us to avoid from duplicating update notifications. - if (nextTrackingTime < currentTimeInMillisec) + if (nextUpdatesCheckingTime < currentTimeInMillisec) { - nextTrackingTime = currentTimeInMillisec + mTrackingInterval; + nextUpdatesCheckingTime = currentTimeInMillisec + mTrackingInterval; gPrefs->Write(prefsUpdateScheduledTime, - wxString(std::to_string(nextTrackingTime))); + wxString(std::to_string(nextUpdatesCheckingTime))); gPrefs->Flush(); return true; diff --git a/src/update/UpdateManager.h b/src/update/UpdateManager.h index 8b9514cda..51bcf5abb 100644 --- a/src/update/UpdateManager.h +++ b/src/update/UpdateManager.h @@ -12,7 +12,6 @@ #include "VersionPatch.h" #include "UpdateDataParser.h" -#include "Project.h" #include "Prefs.h" #include @@ -28,13 +27,13 @@ class UpdateManager final : public wxEvtHandler { public: - UpdateManager(AudacityProject& project); + UpdateManager(); ~UpdateManager(); void getUpdates(); - void enableTracking(bool enable); - bool isTrackingEnabled(); + void enableUpdatesChecking(bool enable); + bool isUpdatesCheckingEnabled(); VersionPatch getVersionPatch() const; @@ -42,14 +41,12 @@ private: UpdateDataParser mUpdateDataParser; VersionPatch mVersionPatch; - wxWindow* mParent; - wxTimer mTimer; const int mTrackingInterval; void OnTimer(wxTimerEvent& event); - /// Scheduling update time for avoiding multiplying update notifications. + /// Scheduling update time for avoiding multiplying update notifications. bool isTimeToUpdate(); public: diff --git a/src/update/UpdatePopupDialog.cpp b/src/update/UpdatePopupDialog.cpp index fe9ad3a2d..09e0f75fd 100644 --- a/src/update/UpdatePopupDialog.cpp +++ b/src/update/UpdatePopupDialog.cpp @@ -45,7 +45,7 @@ UpdatePopupDialog::UpdatePopupDialog (wxWindow* parent, UpdateManager* updateMan S.SetBorder (5); S.Id (DontShowID).AddCheckBox ( - XO ("Don't show this again at start up"), !mUpdateManager->isTrackingEnabled()); + XO ("Don't show this again at start up"), !mUpdateManager->isUpdatesCheckingEnabled()); S.Prop(1).AddSpace(1, 0, 1); @@ -80,7 +80,7 @@ void UpdatePopupDialog::OnSkip (wxCommandEvent&) void UpdatePopupDialog::OnDontShow (wxCommandEvent& event) { - mUpdateManager->enableTracking(!event.IsChecked()); + mUpdateManager->enableUpdatesChecking(!event.IsChecked()); } HtmlWindow* UpdatePopupDialog::AddHtmlContent (wxWindow* parent)