From 365d46914d91818391bb552ec63d41db4e71deee Mon Sep 17 00:00:00 2001 From: sguionni Date: Tue, 8 Oct 2024 22:24:06 +0200 Subject: [PATCH] Download dialog: can load from cache --- lib/app/src/app/action/scene.cpp | 2 +- .../src/app/core/network/network_system.cpp | 2 +- lib/ui/qt/include/ui/qt/dialog/download.hpp | 11 +- lib/ui/qt/src/ui/qt/dialog/download.cpp | 118 +++++++++++++----- 4 files changed, 97 insertions(+), 36 deletions(-) diff --git a/lib/app/src/app/action/scene.cpp b/lib/app/src/app/action/scene.cpp index 2ac12918a..4ef7adf1d 100644 --- a/lib/app/src/app/action/scene.cpp +++ b/lib/app/src/app/action/scene.cpp @@ -39,7 +39,7 @@ namespace VTX::App::Action::Scene std::string data; const FilePath cachePath = Filesystem::getCachePath( _filename ); - NETWORK_SYSTEM().getFile( _filename.string(), &data, _url ); + NETWORK_SYSTEM().downloadFile( _url, _filename.string(), &data, true ); App::ACTION_SYSTEM().execute( _filename, &data ); } diff --git a/lib/app/src/app/core/network/network_system.cpp b/lib/app/src/app/core/network/network_system.cpp index f17481d5e..da42e1726 100644 --- a/lib/app/src/app/core/network/network_system.cpp +++ b/lib/app/src/app/core/network/network_system.cpp @@ -39,7 +39,7 @@ namespace VTX::App::Core::Network const bool p_overwrite ) { - // assert( not std::filesystem::exists( cachePath ) ); + // TODO: assert or exception? if ( not p_overwrite and std::filesystem::exists( Filesystem::getCachePath( p_filename ) ) ) { assert( 1 ); diff --git a/lib/ui/qt/include/ui/qt/dialog/download.hpp b/lib/ui/qt/include/ui/qt/dialog/download.hpp index 0872bba9b..3a7928365 100644 --- a/lib/ui/qt/include/ui/qt/dialog/download.hpp +++ b/lib/ui/qt/include/ui/qt/dialog/download.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace VTX::UI::QT::Dialog @@ -30,10 +31,12 @@ namespace VTX::UI::QT::Dialog // TODO: move to settings. inline static const uint _MAX_HISTORY_SIZE = 10; - QPointer _comboBoxURL; - QPointer _comboBoxPDB; - std::string _url; - std::string _pdb; + QPointer _comboBoxURL; + QPointer _comboBoxPDB; + QPointer _radioButtonOpen; + QPointer _radioButtonDownload; + std::string _url; + std::string _pdb; void _loadHistory( const std::string & p_key, QComboBox * const p_comboBox ); void _saveHistory( const std::string & p_key, const std::string & p_value ); diff --git a/lib/ui/qt/src/ui/qt/dialog/download.cpp b/lib/ui/qt/src/ui/qt/dialog/download.cpp index cebf62952..75b07d389 100644 --- a/lib/ui/qt/src/ui/qt/dialog/download.cpp +++ b/lib/ui/qt/src/ui/qt/dialog/download.cpp @@ -1,5 +1,6 @@ #include "ui/qt/dialog/download.hpp" #include "ui/qt/application.hpp" +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include namespace VTX::UI::QT::Dialog { @@ -15,7 +17,7 @@ namespace VTX::UI::QT::Dialog { setWindowTitle( "Download" ); // TODO: try size policy? - setFixedSize( 300, 150 ); + setFixedSize( 300, 180 ); auto * layout = new QVBoxLayout( this ); @@ -37,23 +39,56 @@ namespace VTX::UI::QT::Dialog QDialogButtonBox::StandardButton::Cancel | QDialogButtonBox::StandardButton::Open, this ); - // Load histories manually because dialog is destroyed when closed. - restore(); + // Cache found label. + // QLabel * labelCache = new QLabel( "File found", this ); - // FIXME: Avoid losing default url if not in history. - if ( _comboBoxURL->findText( QString::fromStdString( _DEFAULT_URL ) ) == -1 ) - { - _comboBoxURL->addItem( _DEFAULT_URL.c_str() ); - } + // Radio buttons. + auto * buttonGroup = new QButtonGroup( this ); + auto * layoutCacheRadio = new QHBoxLayout( this ); + _radioButtonOpen = new QRadioButton( "Open from cache" ); + _radioButtonDownload = new QRadioButton( "Download" ); + buttonGroup->addButton( _radioButtonOpen ); + buttonGroup->addButton( _radioButtonDownload ); + layoutCacheRadio->addWidget( _radioButtonOpen ); + layoutCacheRadio->addWidget( _radioButtonDownload ); // Layout. layout->addWidget( labelURL ); layout->addWidget( _comboBoxURL ); layout->addWidget( labelPDB ); layout->addWidget( _comboBoxPDB ); + layout->addLayout( layoutCacheRadio ); layout->addWidget( buttonBox ); // Callbacks. + // Update radio button if file is found in cache. + connect( + _comboBoxPDB, + &QComboBox::editTextChanged, + [ this ]( const QString & ) + { + auto pdb = _comboBoxPDB->currentText().toStdString(); + Util::String::trim( pdb ); + if ( pdb.length() == 4 ) + { + FilePath path = App::Filesystem::getCachePath( pdb + ".pdb" ); + if ( std::filesystem::exists( path ) ) + { + // Show radio buttons. + _radioButtonOpen->setEnabled( true ); + _radioButtonDownload->setEnabled( true ); + _radioButtonOpen->setChecked( true ); + return; + } + } + // Hide radio buttons. + _radioButtonOpen->setEnabled( false ); + _radioButtonDownload->setEnabled( true ); + _radioButtonDownload->setChecked( true ); + } + ); + + // Open button. connect( buttonBox->button( QDialogButtonBox::StandardButton::Open ), &QPushButton::clicked, @@ -65,44 +100,67 @@ namespace VTX::UI::QT::Dialog _pdb = _comboBoxPDB->currentText().toStdString(); Util::String::trim( _pdb ); - if ( _url.empty() ) + FilePath path = App::Filesystem::getCachePath( _pdb + ".pdb" ); + if ( std::filesystem::exists( path ) and _radioButtonOpen->isChecked() ) { - VTX_WARNING( "URL is empty" ); - return; - } - - if ( _url.empty() || _pdb.empty() ) - { - VTX_WARNING( "PDB id is empty" ); - return; - } - - std::string urlReplaced = _url; - const size_t pos = _url.find( _PDB_ID_TEMPLATE ); - if ( pos == std::string::npos ) - { - VTX_WARNING( "URL does not contain {}", _PDB_ID_TEMPLATE ); + App::ACTION_SYSTEM().execute( path ); } else { - urlReplaced - = urlReplaced.replace( urlReplaced.find( _PDB_ID_TEMPLATE ), _PDB_ID_TEMPLATE.length(), _pdb ); + if ( _url.empty() ) + { + VTX_ERROR( "URL is empty" ); + return; + } + + if ( _pdb.empty() ) + { + VTX_ERROR( "PDB id is empty" ); + return; + } + + if ( _pdb.length() != 4 ) + { + VTX_ERROR( "PDB id must be 4 characters" ); + return; + } + + std::string urlReplaced = _url; + const size_t pos = _url.find( _PDB_ID_TEMPLATE ); + if ( pos == std::string::npos ) + { + VTX_ERROR( "URL does not contain {}", _PDB_ID_TEMPLATE ); + } + else + { + urlReplaced = urlReplaced.replace( + urlReplaced.find( _PDB_ID_TEMPLATE ), _PDB_ID_TEMPLATE.length(), _pdb + ); + } + + App::ACTION_SYSTEM().execute( urlReplaced, _pdb + ".pdb" ); } - // Save histories. save(); - - App::ACTION_SYSTEM().execute( urlReplaced, _pdb + ".pdb" ); - close(); } ); + // Cancel button. connect( buttonBox->button( QDialogButtonBox::StandardButton::Cancel ), &QPushButton::clicked, [ this ]() { close(); } ); + + // Load histories manually because dialog is destroyed when closed. + restore(); + + // FIXME: Avoid losing default url if not in history. + if ( _comboBoxURL->findText( QString::fromStdString( _DEFAULT_URL ) ) == -1 ) + { + _comboBoxURL->addItem( _DEFAULT_URL.c_str() ); + } } void Download::_loadHistory( const std::string & p_key, QComboBox * const p_comboBox )