PCM: reuse download prrogress dialog

This commit is contained in:
qu1ck 2021-11-24 21:34:41 -08:00 committed by Jon Evans
parent 250de83bf2
commit 78aa76d0e6
4 changed files with 49 additions and 34 deletions

View File

@ -100,7 +100,10 @@ void DIALOG_MANAGE_REPOSITORIES::OnAddButtonClicked( wxCommandEvent& event )
} }
else else
{ {
if( m_pcm->FetchRepository( url, repository ) ) std::unique_ptr<WX_PROGRESS_REPORTER> reporter(
new WX_PROGRESS_REPORTER( this, wxT( "" ), 1 ) );
if( m_pcm->FetchRepository( url, repository, reporter.get() ) )
{ {
wxString name = repository.name; wxString name = repository.name;
int increment = 1; int increment = 1;

View File

@ -494,7 +494,10 @@ void PANEL_PACKAGES_VIEW::OnDownloadVersionClicked( wxCommandEvent& event )
std::ofstream output( path.ToUTF8(), std::ios_base::binary ); std::ofstream output( path.ToUTF8(), std::ios_base::binary );
bool success = m_pcm->DownloadToStream( url, &output, _( "Downloading package" ), 0 ); std::unique_ptr<WX_PROGRESS_REPORTER> reporter(
new WX_PROGRESS_REPORTER( this, _( "Downloading package" ), 1 ) );
bool success = m_pcm->DownloadToStream( url, &output, reporter.get(), 0 );
output.close(); output.close();

View File

@ -30,7 +30,6 @@
#include "pgm_base.h" #include "pgm_base.h"
#include "picosha2.h" #include "picosha2.h"
#include "settings/settings_manager.h" #include "settings/settings_manager.h"
#include "widgets/wx_progress_reporters.h"
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
@ -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, bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
const wxString& aDialogTitle, WX_PROGRESS_REPORTER* aReporter,
const size_t aSizeLimit ) const size_t aSizeLimit )
{ {
bool size_exceeded = false; bool size_exceeded = false;
std::unique_ptr<WX_PROGRESS_REPORTER> reporter(
new WX_PROGRESS_REPORTER( m_dialog, aDialogTitle, 1 ) );
TRANSFER_CALLBACK callback = [&]( size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow ) TRANSFER_CALLBACK callback = [&]( size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow )
{ {
if( aSizeLimit > 0 && ( dltotal > aSizeLimit || dlnow > aSizeLimit ) ) if( aSizeLimit > 0 && ( dltotal > aSizeLimit || dlnow > aSizeLimit ) )
@ -206,16 +202,16 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea
if( dltotal > 1024 ) if( dltotal > 1024 )
{ {
reporter->SetCurrentProgress( dlnow / (double) dltotal ); aReporter->SetCurrentProgress( dlnow / (double) dltotal );
reporter->Report( wxString::Format( _( "Downloading %lld/%lld Kb" ), dlnow / 1024, aReporter->Report( wxString::Format( _( "Downloading %lld/%lld Kb" ), dlnow / 1024,
dltotal / 1024 ) ); dltotal / 1024 ) );
} }
else else
{ {
reporter->SetCurrentProgress( 0.0 ); aReporter->SetCurrentProgress( 0.0 );
} }
return !reporter->KeepRefreshing(); return !aReporter->KeepRefreshing();
}; };
KICAD_CURL_EASY curl; KICAD_CURL_EASY curl;
@ -226,8 +222,8 @@ bool PLUGIN_CONTENT_MANAGER::DownloadToStream( const wxString& aUrl, std::ostrea
int code = curl.Perform(); int code = curl.Perform();
if( !reporter->IsCancelled() ) if( !aReporter->IsCancelled() )
reporter->SetCurrentProgress( 1.0 ); aReporter->SetCurrentProgress( 1.0 );
if( code != CURLE_OK ) 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; 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" ) ); wxLogError( _( "Unable to load repository url" ) );
return false; return false;
@ -282,11 +282,14 @@ void PLUGIN_CONTENT_MANAGER::ValidateJson( const nlohmann::json& aJson,
bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUrl, bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUrl,
const boost::optional<wxString>& aHash, const boost::optional<wxString>& aHash,
std::vector<PCM_PACKAGE>& aPackages ) std::vector<PCM_PACKAGE>& aPackages,
WX_PROGRESS_REPORTER* aReporter )
{ {
std::stringstream packages_stream; 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." ) ); wxLogError( _( "Unable to load repository packages url." ) );
return false; return false;
@ -305,10 +308,7 @@ bool PLUGIN_CONTENT_MANAGER::fetchPackages( const wxString& aUr
nlohmann::json packages_json = nlohmann::json::parse( packages_stream.str() ); nlohmann::json packages_json = nlohmann::json::parse( packages_stream.str() );
ValidateJson( packages_json, nlohmann::json_uri( "#/definitions/PackageArray" ) ); ValidateJson( packages_json, nlohmann::json_uri( "#/definitions/PackageArray" ) );
for( nlohmann::json& package : packages_json["packages"] ) aPackages = packages_json["packages"].get<std::vector<PCM_PACKAGE>>();
{
aPackages.push_back( package.get<PCM_PACKAGE>() );
}
} }
catch( std::exception& e ) catch( std::exception& e )
{ {
@ -362,7 +362,10 @@ const bool PLUGIN_CONTENT_MANAGER::CacheRepository( const wxString& aRepositoryI
nlohmann::json js; nlohmann::json js;
PCM_REPOSITORY current_repo; PCM_REPOSITORY current_repo;
if( !FetchRepository( url, current_repo ) ) std::unique_ptr<WX_PROGRESS_REPORTER> reporter(
new WX_PROGRESS_REPORTER( m_dialog, wxT( "" ), 1 ) );
if( !FetchRepository( url, current_repo, reporter.get() ) )
return false; return false;
bool packages_cache_exists = 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 // Cache doesn't exist or is out of date
if( !fetchPackages( current_repo.packages.url, current_repo.packages.sha256, if( !fetchPackages( current_repo.packages.url, current_repo.packages.sha256,
current_repo.package_list ) ) current_repo.package_list, reporter.get() ) )
{ {
return false; return false;
} }
@ -447,9 +450,11 @@ const bool PLUGIN_CONTENT_MANAGER::CacheRepository( const wxString& aRepositoryI
std::ofstream resources_stream( resource_file.GetFullPath().ToUTF8(), std::ofstream resources_stream( resource_file.GetFullPath().ToUTF8(),
std::ios_base::binary ); std::ios_base::binary );
reporter->SetTitle( _( "Downloading resources" ) );
// 100 Mb resource file limit // 100 Mb resource file limit
bool success = DownloadToStream( resources.url, &resources_stream, bool success = DownloadToStream( resources.url, &resources_stream, reporter.get(),
_( "Downloading resources" ), 100 * 1024 * 1024 ); 100 * 1024 * 1024 );
resources_stream.close(); resources_stream.close();

View File

@ -23,6 +23,7 @@
#include "core/wx_stl_compat.h" #include "core/wx_stl_compat.h"
#include "pcm_data.h" #include "pcm_data.h"
#include "widgets/wx_progress_reporters.h"
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <nlohmann/json-schema.hpp> #include <nlohmann/json-schema.hpp>
@ -98,12 +99,14 @@ public:
/** /**
* @brief Fetches repository metadata from given url * @brief Fetches repository metadata from given url
* *
* @param url URL of the repository * @param aUrl URL of the repository
* @param repository fetched repository metadata * @param aRepository fetched repository metadata
* @param aReporter progress reporter dialog to use for download
* @return true if successful * @return true if successful
* @return false if URL could not be downloaded or result could not be parsed * @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 * @brief Validates json against a specific definition in the PCM schema
@ -226,13 +229,13 @@ public:
* *
* @param aUrl URL to download * @param aUrl URL to download
* @param aOutput output stream * @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 * @param aSizeLimit maximum download size, 0 for unlimited
* @return true if download was successful * @return true if download was successful
* @return false if download failed or was too large * @return false if download failed or was too large
*/ */
bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput, bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
const wxString& aDialogTitle, WX_PROGRESS_REPORTER* aReporter,
const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT ); const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT );
/** /**
@ -275,10 +278,11 @@ private:
* @param aUrl URL of the packages metadata * @param aUrl URL of the packages metadata
* @param aHash optional sha256 hash * @param aHash optional sha256 hash
* @param aPackages resulting packages metadata list * @param aPackages resulting packages metadata list
* @param aReporter progress dialog to use for download
* @return true if packages were successfully downloaded, verified and parsed * @return true if packages were successfully downloaded, verified and parsed
*/ */
bool fetchPackages( const wxString& aUrl, const boost::optional<wxString>& aHash, bool fetchPackages( const wxString& aUrl, const boost::optional<wxString>& aHash,
std::vector<PCM_PACKAGE>& aPackages ); std::vector<PCM_PACKAGE>& aPackages, WX_PROGRESS_REPORTER* aReporter );
/** /**
* @brief Get the cached repository metadata * @brief Get the cached repository metadata