From 78aa76d0e6394318c24ef1b3b7265d4fd2a62ebd Mon Sep 17 00:00:00 2001 From: qu1ck Date: Wed, 24 Nov 2021 21:34:41 -0800 Subject: [PATCH] PCM: reuse download prrogress dialog --- .../dialogs/dialog_manage_repositories.cpp | 5 +- kicad/pcm/dialogs/panel_packages_view.cpp | 5 +- kicad/pcm/pcm.cpp | 55 ++++++++++--------- kicad/pcm/pcm.h | 18 +++--- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/kicad/pcm/dialogs/dialog_manage_repositories.cpp b/kicad/pcm/dialogs/dialog_manage_repositories.cpp index 0389684a4c..8e57907440 100644 --- a/kicad/pcm/dialogs/dialog_manage_repositories.cpp +++ b/kicad/pcm/dialogs/dialog_manage_repositories.cpp @@ -100,7 +100,10 @@ void DIALOG_MANAGE_REPOSITORIES::OnAddButtonClicked( wxCommandEvent& event ) } else { - if( m_pcm->FetchRepository( url, repository ) ) + std::unique_ptr reporter( + new WX_PROGRESS_REPORTER( this, wxT( "" ), 1 ) ); + + if( m_pcm->FetchRepository( url, repository, reporter.get() ) ) { wxString name = repository.name; int increment = 1; diff --git a/kicad/pcm/dialogs/panel_packages_view.cpp b/kicad/pcm/dialogs/panel_packages_view.cpp index 5e96ffc6b7..7693e8f51c 100644 --- a/kicad/pcm/dialogs/panel_packages_view.cpp +++ b/kicad/pcm/dialogs/panel_packages_view.cpp @@ -494,7 +494,10 @@ void PANEL_PACKAGES_VIEW::OnDownloadVersionClicked( wxCommandEvent& event ) std::ofstream output( path.ToUTF8(), std::ios_base::binary ); - bool success = m_pcm->DownloadToStream( url, &output, _( "Downloading package" ), 0 ); + std::unique_ptr reporter( + new WX_PROGRESS_REPORTER( this, _( "Downloading package" ), 1 ) ); + + bool success = m_pcm->DownloadToStream( url, &output, reporter.get(), 0 ); output.close(); diff --git a/kicad/pcm/pcm.cpp b/kicad/pcm/pcm.cpp index ef81ec35ba..9bb06ec30a 100644 --- a/kicad/pcm/pcm.cpp +++ b/kicad/pcm/pcm.cpp @@ -30,7 +30,6 @@ #include "pgm_base.h" #include "picosha2.h" #include "settings/settings_manager.h" -#include "widgets/wx_progress_reporters.h" #include #include @@ -186,14 +185,11 @@ PLUGIN_CONTENT_MANAGER::PLUGIN_CONTENT_MANAGER( wxWindow* aParent ) : m_dialog( bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostream* aOutput, - const wxString& aDialogTitle, - const size_t aSizeLimit ) + WX_PROGRESS_REPORTER* aReporter, + const size_t aSizeLimit ) { bool size_exceeded = false; - std::unique_ptr reporter( - new WX_PROGRESS_REPORTER( m_dialog, aDialogTitle, 1 ) ); - TRANSFER_CALLBACK callback = [&]( size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow ) { if( aSizeLimit > 0 && ( dltotal > aSizeLimit || dlnow > aSizeLimit ) ) @@ -206,16 +202,16 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea if( dltotal > 1024 ) { - reporter->SetCurrentProgress( dlnow / (double) dltotal ); - reporter->Report( wxString::Format( _( "Downloading %lld/%lld Kb" ), dlnow / 1024, - dltotal / 1024 ) ); + aReporter->SetCurrentProgress( dlnow / (double) dltotal ); + aReporter->Report( wxString::Format( _( "Downloading %lld/%lld Kb" ), dlnow / 1024, + dltotal / 1024 ) ); } else { - reporter->SetCurrentProgress( 0.0 ); + aReporter->SetCurrentProgress( 0.0 ); } - return !reporter->KeepRefreshing(); + return !aReporter->KeepRefreshing(); }; KICAD_CURL_EASY curl; @@ -226,8 +222,8 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea int code = curl.Perform(); - if( !reporter->IsCancelled() ) - reporter->SetCurrentProgress( 1.0 ); + if( !aReporter->IsCancelled() ) + aReporter->SetCurrentProgress( 1.0 ); if( code != CURLE_OK ) { @@ -243,10 +239,14 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea } -bool PLUGIN_CONTENT_MANAGER::FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository ) +bool PLUGIN_CONTENT_MANAGER::FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository, + WX_PROGRESS_REPORTER* aReporter ) { std::stringstream repository_stream; - if( !DownloadToStream( aUrl, &repository_stream, _( "Fetching repository" ), 20480 ) ) + + aReporter->SetTitle( _( "Fetching repository" ) ); + + if( !DownloadToStream( aUrl, &repository_stream, aReporter, 20480 ) ) { wxLogError( _( "Unable to load repository url" ) ); return false; @@ -282,11 +282,14 @@ void PLUGIN_CONTENT_MANAGER::ValidateJson( const nlohmann::json& aJson, bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUrl, const boost::optional& aHash, - std::vector& aPackages ) + std::vector& aPackages, + WX_PROGRESS_REPORTER* aReporter ) { std::stringstream packages_stream; - if( !DownloadToStream( aUrl, &packages_stream, _( "Fetching repository packages" ) ) ) + aReporter->SetTitle( _( "Fetching repository packages" ) ); + + if( !DownloadToStream( aUrl, &packages_stream, aReporter ) ) { wxLogError( _( "Unable to load repository packages url." ) ); return false; @@ -305,10 +308,7 @@ bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUr nlohmann::json packages_json = nlohmann::json::parse( packages_stream.str() ); ValidateJson( packages_json, nlohmann::json_uri( "#/definitions/PackageArray" ) ); - for( nlohmann::json& package : packages_json["packages"] ) - { - aPackages.push_back( package.get() ); - } + aPackages = packages_json["packages"].get>(); } catch( std::exception& e ) { @@ -362,7 +362,10 @@ const bool PLUGIN_CONTENT_MANAGER::CacheRepository( const wxString& aRepositoryI nlohmann::json js; PCM_REPOSITORY current_repo; - if( !FetchRepository( url, current_repo ) ) + std::unique_ptr reporter( + new WX_PROGRESS_REPORTER( m_dialog, wxT( "" ), 1 ) ); + + if( !FetchRepository( url, current_repo, reporter.get() ) ) return false; bool packages_cache_exists = false; @@ -409,7 +412,7 @@ const bool PLUGIN_CONTENT_MANAGER::CacheRepository( const wxString& aRepositoryI { // Cache doesn't exist or is out of date if( !fetchPackages( current_repo.packages.url, current_repo.packages.sha256, - current_repo.package_list ) ) + current_repo.package_list, reporter.get() ) ) { return false; } @@ -447,9 +450,11 @@ const bool PLUGIN_CONTENT_MANAGER::CacheRepository( const wxString& aRepositoryI std::ofstream resources_stream( resource_file.GetFullPath().ToUTF8(), std::ios_base::binary ); + reporter->SetTitle( _( "Downloading resources" ) ); + // 100 Mb resource file limit - bool success = DownloadToStream( resources.url, &resources_stream, - _( "Downloading resources" ), 100 * 1024 * 1024 ); + bool success = DownloadToStream( resources.url, &resources_stream, reporter.get(), + 100 * 1024 * 1024 ); resources_stream.close(); diff --git a/kicad/pcm/pcm.h b/kicad/pcm/pcm.h index 8169a736d2..f3c739f675 100644 --- a/kicad/pcm/pcm.h +++ b/kicad/pcm/pcm.h @@ -23,6 +23,7 @@ #include "core/wx_stl_compat.h" #include "pcm_data.h" +#include "widgets/wx_progress_reporters.h" #include #include #include @@ -98,12 +99,14 @@ public: /** * @brief Fetches repository metadata from given url * - * @param url URL of the repository - * @param repository fetched repository metadata + * @param aUrl URL of the repository + * @param aRepository fetched repository metadata + * @param aReporter progress reporter dialog to use for download * @return true if successful * @return false if URL could not be downloaded or result could not be parsed */ - bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository ); + bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository, + WX_PROGRESS_REPORTER* aReporter ); /** * @brief Validates json against a specific definition in the PCM schema @@ -226,14 +229,14 @@ public: * * @param aUrl URL to download * @param aOutput output stream - * @param aDialogTitle title of the spawned WX_PROGRESS_REPORTER dialog + * @param aReporter progress dialog to use * @param aSizeLimit maximum download size, 0 for unlimited * @return true if download was successful * @return false if download failed or was too large */ bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput, - const wxString& aDialogTitle, - const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT ); + WX_PROGRESS_REPORTER* aReporter, + const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT ); /** * @brief Get the approximate measure of how much given package matches the search term @@ -275,10 +278,11 @@ private: * @param aUrl URL of the packages metadata * @param aHash optional sha256 hash * @param aPackages resulting packages metadata list + * @param aReporter progress dialog to use for download * @return true if packages were successfully downloaded, verified and parsed */ bool fetchPackages( const wxString& aUrl, const boost::optional& aHash, - std::vector& aPackages ); + std::vector& aPackages, WX_PROGRESS_REPORTER* aReporter ); /** * @brief Get the cached repository metadata