2021-11-16 23:36:21 +01:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers , whose names
* are too numerous to list here . Please refer to the COPYRIGHT
* file distributed with this source distribution .
*
2021-12-26 18:47:58 +01:00
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
2021-11-16 23:36:21 +01:00
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2021-12-26 18:47:58 +01:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2021-11-16 23:36:21 +01:00
*
*/
2021-11-18 21:19:18 +01:00
# include "backends/networking/curl/request.h"
2023-01-08 00:01:38 +01:00
# include "gui/downloadpacksdialog.h"
2021-11-16 23:36:21 +01:00
# include "gui/downloaddialog.h"
# include "backends/networking/curl/session.h"
# include "common/config-manager.h"
# include "common/translation.h"
# include "common/util.h"
# include "engines/metaengine.h"
# include "gui/browser.h"
# include "gui/chooser.h"
# include "gui/editgamedialog.h"
# include "gui/gui-manager.h"
# include "gui/launcher.h"
# include "gui/message.h"
# include "gui/remotebrowser.h"
# include "gui/widgets/edittext.h"
# include "gui/widgets/list.h"
namespace GUI {
enum {
2021-11-17 15:25:23 +01:00
kDownloadProceedCmd = ' Dlpr ' ,
2021-11-20 00:55:09 +01:00
kListDownloadFinishedCmd = ' DlLE ' ,
2022-07-09 16:48:09 +02:00
kCleanupCmd = ' DlCL ' ,
kClearCacheCmd = ' DlCC '
2021-11-16 23:36:21 +01:00
} ;
2021-11-17 23:13:05 +01:00
struct DialogState {
2023-01-08 00:12:12 +01:00
DownloadPacksDialog * dialog ;
2021-11-17 23:13:05 +01:00
Networking : : Session session ;
Common : : HashMap < Common : : String , uint32 > fileHash ;
IconProcessState state ;
2021-11-18 22:04:31 +01:00
uint32 downloadedSize ;
uint32 totalSize ;
2021-11-18 22:38:10 +01:00
uint32 totalFiles ;
2021-11-18 22:04:31 +01:00
uint32 startTime ;
2023-01-08 00:35:27 +01:00
const char * listfname ;
2023-01-08 15:09:34 +01:00
uint32 lastUpdate ;
2021-11-18 21:19:18 +01:00
2023-01-08 00:35:27 +01:00
DialogState ( const char * fname ) {
listfname = fname ;
state = kDownloadStateNone ;
2023-01-08 15:09:34 +01:00
downloadedSize = totalSize = totalFiles = startTime = lastUpdate = 0 ;
2023-01-08 00:35:27 +01:00
dialog = nullptr ;
}
2022-06-27 22:36:18 +01:00
void downloadList ( ) ;
void proceedDownload ( ) ;
void downloadListCallback ( Networking : : DataResponse response ) ;
void downloadFileCallback ( Networking : : DataResponse response ) ;
void errorCallback ( Networking : : ErrorResponse error ) ;
private :
bool takeOneFile ( ) ;
2021-11-17 23:13:05 +01:00
} static * g_state ;
2021-11-16 23:36:21 +01:00
2022-06-27 22:36:18 +01:00
void DialogState : : downloadList ( ) {
2023-01-08 00:35:27 +01:00
Networking : : SessionRequest * rq = session . get ( Common : : String : : format ( " https://downloads.scummvm.org/frs/icons/%s " , listfname ) , " " ,
2022-06-27 22:36:18 +01:00
new Common : : Callback < DialogState , Networking : : DataResponse > ( this , & DialogState : : downloadListCallback ) ,
new Common : : Callback < DialogState , Networking : : ErrorResponse > ( this , & DialogState : : errorCallback ) ,
true ) ;
rq - > start ( ) ;
}
void DialogState : : proceedDownload ( ) {
2023-01-08 15:09:34 +01:00
startTime = lastUpdate = g_system - > getMillis ( ) ;
2022-06-27 22:36:18 +01:00
takeOneFile ( ) ;
}
bool DialogState : : takeOneFile ( ) {
auto f = fileHash . begin ( ) ;
if ( f = = fileHash . end ( ) )
return false ;
Common : : String fname = f - > _key ;
fileHash . erase ( fname ) ;
Common : : String url = Common : : String : : format ( " https://downloads.scummvm.org/frs/icons/%s " , fname . c_str ( ) ) ;
Common : : String localFile = normalizePath ( ConfMan . get ( " iconspath " ) + " / " + fname , ' / ' ) ;
Networking : : SessionRequest * rq = session . get ( url , localFile ,
new Common : : Callback < DialogState , Networking : : DataResponse > ( this , & DialogState : : downloadFileCallback ) ,
new Common : : Callback < DialogState , Networking : : ErrorResponse > ( this , & DialogState : : errorCallback ) ) ;
rq - > start ( ) ;
return true ;
}
void DialogState : : downloadListCallback ( Networking : : DataResponse r ) {
Networking : : SessionFileResponse * response = static_cast < Networking : : SessionFileResponse * > ( r . value ) ;
Common : : MemoryReadStream stream ( response - > buffer , response - > len ) ;
int nline = 0 ;
while ( ! stream . eos ( ) ) {
Common : : String s = stream . readString ( ' \n ' ) ;
nline + + ;
if ( s . empty ( ) )
continue ;
size_t pos = s . findFirstOf ( ' , ' ) ;
if ( pos = = Common : : String : : npos ) {
2023-01-08 00:12:12 +01:00
warning ( " DownloadPacksDialog: wrong string format at line %d: <%s> " , nline , s . c_str ( ) ) ;
2022-06-27 22:36:18 +01:00
continue ;
}
fileHash . setVal ( s . substr ( 0 , pos ) , atol ( s . substr ( pos + 1 ) . c_str ( ) ) ) ;
}
state = kDownloadStateListDownloaded ;
if ( dialog )
dialog - > sendCommand ( kListDownloadFinishedCmd , 0 ) ;
}
void DialogState : : downloadFileCallback ( Networking : : DataResponse r ) {
Networking : : SessionFileResponse * response = static_cast < Networking : : SessionFileResponse * > ( r . value ) ;
downloadedSize + = response - > len ;
if ( response - > eos ) {
if ( ! takeOneFile ( ) ) {
state = kDownloadComplete ;
if ( dialog )
dialog - > sendCommand ( kDownloadEndedCmd , 0 ) ;
return ;
}
}
2023-01-08 15:09:34 +01:00
if ( dialog & & g_system - > getMillis ( ) > lastUpdate + 500 ) {
lastUpdate = g_system - > getMillis ( ) ;
2022-06-27 22:36:18 +01:00
dialog - > sendCommand ( kDownloadProgressCmd , 0 ) ;
2023-01-08 15:09:34 +01:00
}
2022-06-27 22:36:18 +01:00
}
void DialogState : : errorCallback ( Networking : : ErrorResponse error ) {
Common : : U32String message = Common : : U32String : : format ( _ ( " ERROR %d: %s " ) , error . httpResponseCode , error . response . c_str ( ) ) ;
if ( dialog )
dialog - > setError ( message ) ;
}
2021-11-18 21:19:18 +01:00
static uint32 getDownloadingProgress ( ) {
2021-11-18 22:04:31 +01:00
if ( ! g_state | | g_state - > totalSize = = 0 )
2021-11-18 21:19:18 +01:00
return 0 ;
2021-11-18 22:04:31 +01:00
uint32 progress = ( uint32 ) ( 100 * ( ( double ) g_state - > downloadedSize / ( double ) g_state - > totalSize ) ) ;
2021-11-18 21:37:59 +01:00
return progress ;
2021-11-18 21:19:18 +01:00
}
static uint32 getDownloadSpeed ( ) {
2021-11-18 22:04:31 +01:00
uint32 speed = 1000 * ( ( double ) g_state - > downloadedSize / ( g_system - > getMillis ( ) - g_state - > startTime ) ) ;
return speed ;
2021-11-18 21:19:18 +01:00
}
2023-01-08 00:35:27 +01:00
DownloadPacksDialog : : DownloadPacksDialog ( Common : : U32String packname , const char * listfname , const char * packsglob ) :
Dialog ( " GlobalOptions_DownloadPacksDialog " ) , CommandSender ( this ) , _close ( false ) , _packname ( packname ) ,
_packsglob ( packsglob ) {
2021-11-16 23:36:21 +01:00
_backgroundType = GUI : : ThemeEngine : : kDialogBackgroundPlain ;
2023-01-08 00:35:27 +01:00
// I18N: String like "Downloading icon packs list..."
_statusText = new StaticTextWidget ( this , " GlobalOptions_DownloadPacksDialog.StatusText " , Common : : U32String : : format ( _ ( " Downloading %S list... " ) , _packname . c_str ( ) ) ) ;
2023-01-08 00:12:12 +01:00
_errorText = new StaticTextWidget ( this , " GlobalOptions_DownloadPacksDialog.ErrorText " , Common : : U32String ( " " ) ) ;
2021-11-17 00:42:43 +01:00
2021-11-18 21:19:18 +01:00
uint32 progress = getDownloadingProgress ( ) ;
2023-01-08 00:12:12 +01:00
_progressBar = new SliderWidget ( this , " GlobalOptions_DownloadPacksDialog.ProgressBar " ) ;
2021-11-16 23:36:21 +01:00
_progressBar - > setMinValue ( 0 ) ;
_progressBar - > setMaxValue ( 100 ) ;
_progressBar - > setValue ( progress ) ;
_progressBar - > setEnabled ( false ) ;
2023-01-08 00:12:12 +01:00
_percentLabel = new StaticTextWidget ( this , " GlobalOptions_DownloadPacksDialog.PercentText " , Common : : String : : format ( " %u %% " , progress ) ) ;
_downloadSizeLabel = new StaticTextWidget ( this , " GlobalOptions_DownloadPacksDialog.DownloadSize " , Common : : U32String ( ) ) ;
_downloadSpeedLabel = new StaticTextWidget ( this , " GlobalOptions_DownloadPacksDialog.DownloadSpeed " , Common : : U32String ( ) ) ;
_cancelButton = new ButtonWidget ( this , " GlobalOptions_DownloadPacksDialog.MainButton " , _ ( " Cancel download " ) , Common : : U32String ( ) , kCleanupCmd ) ;
_closeButton = new ButtonWidget ( this , " GlobalOptions_DownloadPacksDialog.CloseButton " , _ ( " Hide " ) , Common : : U32String ( ) , kCloseCmd ) ;
_clearCacheButton = new ButtonWidget ( this , " GlobalOptions_DownloadPacksDialog.ResetButton " , _ ( " Clear Cache " ) , Common : : U32String ( ) , kClearCacheCmd ) ;
2021-11-16 23:36:21 +01:00
2021-11-17 23:13:05 +01:00
if ( ! g_state ) {
2023-01-08 00:35:27 +01:00
g_state = new DialogState ( listfname ) ;
2021-11-17 23:13:05 +01:00
g_state - > dialog = this ;
setState ( kDownloadStateList ) ;
refreshWidgets ( ) ;
2022-06-27 22:36:18 +01:00
g_state - > downloadList ( ) ;
2021-11-17 23:13:05 +01:00
} else {
g_state - > dialog = this ;
2021-11-16 23:36:21 +01:00
2021-11-17 23:13:05 +01:00
setState ( g_state - > state ) ;
refreshWidgets ( ) ;
}
2021-11-16 23:36:21 +01:00
}
2023-01-08 00:12:12 +01:00
DownloadPacksDialog : : ~ DownloadPacksDialog ( ) {
2021-11-16 23:36:21 +01:00
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : open ( ) {
2021-11-16 23:36:21 +01:00
Dialog : : open ( ) ;
reflowLayout ( ) ;
g_gui . scheduleTopDialogRedraw ( ) ;
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : close ( ) {
2021-11-17 23:13:05 +01:00
if ( g_state )
g_state - > dialog = nullptr ;
2021-11-16 23:36:21 +01:00
Dialog : : close ( ) ;
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : setState ( IconProcessState state ) {
2021-11-17 23:13:05 +01:00
g_state - > state = state ;
switch ( state ) {
2021-11-18 21:19:18 +01:00
case kDownloadStateNone :
2021-11-17 23:13:05 +01:00
case kDownloadStateList :
2023-01-08 00:35:27 +01:00
_statusText - > setLabel ( Common : : U32String : : format ( _ ( " Downloading %S list... " ) , _packname . c_str ( ) ) ) ;
2021-11-17 23:13:05 +01:00
_cancelButton - > setLabel ( _ ( " Cancel download " ) ) ;
2021-11-20 00:55:09 +01:00
_cancelButton - > setCmd ( kCleanupCmd ) ;
2022-07-09 16:48:09 +02:00
_closeButton - > setVisible ( true ) ;
2021-11-17 23:13:05 +01:00
2021-11-18 22:04:31 +01:00
g_state - > totalSize = 0 ;
2021-11-17 23:13:05 +01:00
g_state - > fileHash . clear ( ) ;
break ;
case kDownloadStateListDownloaded :
2023-01-08 00:35:27 +01:00
_statusText - > setLabel ( Common : : U32String : : format ( _ ( " Downloading %S list... %d entries " ) , _packname . c_str ( ) , g_state - > fileHash . size ( ) ) ) ;
2021-11-17 23:13:05 +01:00
_cancelButton - > setLabel ( _ ( " Cancel download " ) ) ;
2021-11-20 00:55:09 +01:00
_cancelButton - > setCmd ( kCleanupCmd ) ;
2022-07-09 16:48:09 +02:00
_closeButton - > setVisible ( true ) ;
2021-11-17 23:13:05 +01:00
break ;
case kDownloadStateListCalculated : {
2023-02-03 15:28:31 +00:00
const char * sizeUnits ;
Common : : String size = Common : : getHumanReadableBytes ( g_state - > totalSize , sizeUnits ) ;
2021-11-17 23:13:05 +01:00
_statusText - > setLabel ( Common : : U32String : : format ( _ ( " Detected %d new packs, %s %S " ) , g_state - > fileHash . size ( ) , size . c_str ( ) , _ ( sizeUnits ) . c_str ( ) ) ) ;
_cancelButton - > setLabel ( _ ( " Download " ) ) ;
_cancelButton - > setCmd ( kDownloadProceedCmd ) ;
_closeButton - > setVisible ( true ) ;
_closeButton - > setLabel ( _ ( " Cancel " ) ) ;
2021-11-20 00:55:09 +01:00
_closeButton - > setCmd ( kCleanupCmd ) ;
2021-11-17 23:13:05 +01:00
_closeButton - > setEnabled ( true ) ;
break ;
}
case kDownloadStateDownloading :
_cancelButton - > setLabel ( _ ( " Cancel download " ) ) ;
2021-11-20 00:55:09 +01:00
_cancelButton - > setCmd ( kCleanupCmd ) ;
2021-11-17 23:13:05 +01:00
_closeButton - > setVisible ( true ) ;
_closeButton - > setLabel ( _ ( " Hide " ) ) ;
_closeButton - > setCmd ( kCloseCmd ) ;
_closeButton - > setEnabled ( true ) ;
2022-07-09 16:48:09 +02:00
_clearCacheButton - > setEnabled ( false ) ;
2021-11-17 23:13:05 +01:00
break ;
2021-11-18 21:37:59 +01:00
case kDownloadComplete : {
2023-02-03 15:28:31 +00:00
const char * sizeUnits ;
Common : : String size = Common : : getHumanReadableBytes ( g_state - > totalSize , sizeUnits ) ;
2021-11-18 22:38:10 +01:00
_statusText - > setLabel ( Common : : U32String : : format ( _ ( " Download complete, downloaded %d packs, %s %S " ) , g_state - > totalFiles , size . c_str ( ) , _ ( sizeUnits ) . c_str ( ) ) ) ;
2021-11-18 22:04:31 +01:00
_cancelButton - > setVisible ( false ) ;
2021-11-18 21:37:59 +01:00
_cancelButton - > setLabel ( _ ( " Cancel download " ) ) ;
2021-11-20 00:55:09 +01:00
_cancelButton - > setCmd ( kCleanupCmd ) ;
2021-11-18 21:37:59 +01:00
_closeButton - > setVisible ( true ) ;
_closeButton - > setLabel ( _ ( " Close " ) ) ;
2021-11-20 00:55:09 +01:00
_closeButton - > setCmd ( kCleanupCmd ) ;
2021-11-18 21:37:59 +01:00
_closeButton - > setEnabled ( true ) ;
2022-07-09 16:48:09 +02:00
_clearCacheButton - > setEnabled ( true ) ;
2021-11-18 21:37:59 +01:00
break ;
}
2021-11-17 23:13:05 +01:00
}
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : handleCommand ( CommandSender * sender , uint32 cmd , uint32 data ) {
2021-11-16 23:36:21 +01:00
switch ( cmd ) {
2021-11-20 00:55:09 +01:00
case kCleanupCmd :
2021-11-16 23:36:21 +01:00
{
2021-11-20 00:16:31 +01:00
g_state - > session . abortRequest ( ) ;
2021-11-17 23:13:05 +01:00
delete g_state ;
g_state = nullptr ;
2021-11-16 23:36:21 +01:00
close ( ) ;
break ;
}
case kDownloadProgressCmd :
if ( ! _close ) {
refreshWidgets ( ) ;
g_gui . scheduleTopDialogRedraw ( ) ;
}
break ;
case kDownloadEndedCmd :
2021-11-18 21:37:59 +01:00
setState ( kDownloadComplete ) ;
2021-11-16 23:36:21 +01:00
break ;
2021-11-17 15:25:23 +01:00
case kListDownloadFinishedCmd :
2021-11-17 23:13:05 +01:00
setState ( kDownloadStateListDownloaded ) ;
2021-11-17 00:42:43 +01:00
calculateList ( ) ;
2021-11-16 23:36:21 +01:00
break ;
2021-11-17 15:25:23 +01:00
case kDownloadProceedCmd :
2021-11-17 23:13:05 +01:00
setState ( kDownloadStateDownloading ) ;
2022-06-27 22:36:18 +01:00
g_state - > proceedDownload ( ) ;
2021-11-17 15:25:23 +01:00
break ;
2022-07-09 16:48:09 +02:00
case kClearCacheCmd :
_clearCacheButton - > setEnabled ( false ) ;
clearCache ( ) ;
break ;
2021-11-16 23:36:21 +01:00
default :
Dialog : : handleCommand ( sender , cmd , data ) ;
}
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : handleTickle ( ) {
2021-11-16 23:36:21 +01:00
if ( _close ) {
close ( ) ;
_close = false ;
return ;
}
2021-11-18 21:19:18 +01:00
int32 progress = getDownloadingProgress ( ) ;
2021-11-16 23:36:21 +01:00
if ( _progressBar - > getValue ( ) ! = progress ) {
refreshWidgets ( ) ;
g_gui . scheduleTopDialogRedraw ( ) ;
}
Dialog : : handleTickle ( ) ;
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : reflowLayout ( ) {
2021-11-16 23:36:21 +01:00
Dialog : : reflowLayout ( ) ;
refreshWidgets ( ) ;
}
2023-01-08 00:12:12 +01:00
Common : : U32String DownloadPacksDialog : : getSizeLabelText ( ) {
2023-02-03 15:28:31 +00:00
const char * downloadedUnits , * totalUnits ;
Common : : String downloaded = Common : : getHumanReadableBytes ( g_state - > downloadedSize , downloadedUnits ) ;
Common : : String total = Common : : getHumanReadableBytes ( g_state - > totalSize , totalUnits ) ;
2021-11-16 23:36:21 +01:00
return Common : : U32String : : format ( _ ( " Downloaded %s %S / %s %S " ) , downloaded . c_str ( ) , _ ( downloadedUnits ) . c_str ( ) , total . c_str ( ) , _ ( totalUnits ) . c_str ( ) ) ;
}
2023-01-08 00:12:12 +01:00
Common : : U32String DownloadPacksDialog : : getSpeedLabelText ( ) {
2023-02-03 15:28:31 +00:00
const char * speedUnits ;
Common : : String speed = Common : : getHumanReadableBytes ( getDownloadSpeed ( ) , speedUnits ) ;
return Common : : U32String : : format ( _ ( " Download speed: %s %S/s " ) , speed . c_str ( ) , _ ( speedUnits ) . c_str ( ) ) ;
2021-11-16 23:36:21 +01:00
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : refreshWidgets ( ) {
2021-11-18 21:19:18 +01:00
uint32 progress = getDownloadingProgress ( ) ;
2021-11-16 23:36:21 +01:00
_percentLabel - > setLabel ( Common : : String : : format ( " %u %% " , progress ) ) ;
_downloadSizeLabel - > setLabel ( getSizeLabelText ( ) ) ;
_downloadSpeedLabel - > setLabel ( getSpeedLabelText ( ) ) ;
_progressBar - > setValue ( progress ) ;
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : setError ( Common : : U32String & msg ) {
2021-11-17 12:50:26 +01:00
_errorText - > setLabel ( msg ) ;
2021-11-17 15:25:23 +01:00
_cancelButton - > setLabel ( _ ( " Close " ) ) ;
2021-11-20 00:55:09 +01:00
_cancelButton - > setCmd ( kCleanupCmd ) ;
2021-11-17 12:50:26 +01:00
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : calculateList ( ) {
2022-06-20 00:47:38 -05:00
Common : : String iconsPath = ConfMan . get ( " iconspath " ) ;
if ( iconsPath . empty ( ) ) {
2021-11-17 15:25:23 +01:00
Common : : U32String str ( _ ( " ERROR: No icons path set " ) ) ;
setError ( str ) ;
2021-11-17 00:42:43 +01:00
return ;
}
2022-07-09 13:54:49 +02:00
// Scan all files in iconspath and remove present and incomplete ones from the
2021-11-17 00:42:43 +01:00
// donwloaded files list
2022-06-20 00:47:38 -05:00
Common : : FSDirectory * iconDir = new Common : : FSDirectory ( iconsPath ) ;
2021-11-17 00:42:43 +01:00
Common : : ArchiveMemberList iconFiles ;
2023-01-08 00:35:27 +01:00
iconDir - > listMatchingMembers ( iconFiles , _packsglob ) ;
2021-11-17 00:42:43 +01:00
for ( auto ic = iconFiles . begin ( ) ; ic ! = iconFiles . end ( ) ; + + ic ) {
Common : : String fname = ( * ic ) - > getName ( ) ;
2022-07-09 13:54:49 +02:00
Common : : SeekableReadStream * str = ( * ic ) - > createReadStream ( ) ;
uint32 size = str - > size ( ) ;
delete str ;
2021-11-17 00:42:43 +01:00
2022-07-09 13:54:49 +02:00
if ( g_state - > fileHash . contains ( fname ) & & size = = g_state - > fileHash [ fname ] )
2021-11-17 23:13:05 +01:00
g_state - > fileHash . erase ( fname ) ;
2021-11-17 00:42:43 +01:00
}
delete iconDir ;
// Now calculate the size of the missing files
2021-11-18 22:04:31 +01:00
g_state - > totalSize = 0 ;
2021-11-17 23:13:05 +01:00
for ( auto f = g_state - > fileHash . begin ( ) ; f ! = g_state - > fileHash . end ( ) ; + + f ) {
2021-11-18 22:04:31 +01:00
g_state - > totalSize + = f - > _value ;
2021-11-17 00:42:43 +01:00
}
2021-11-18 22:38:10 +01:00
g_state - > totalFiles = g_state - > fileHash . size ( ) ;
2021-11-18 22:04:31 +01:00
if ( g_state - > totalSize = = 0 ) {
2023-01-08 00:35:27 +01:00
// I18N: String like "No new icon packs available"
Common : : U32String error ( Common : : U32String : : format ( _ ( " No new %S available " ) , _packname . c_str ( ) ) ) ;
2022-07-09 16:48:09 +02:00
_closeButton - > setEnabled ( false ) ;
2021-11-18 21:19:18 +01:00
setError ( error ) ;
2021-11-17 00:42:43 +01:00
return ;
}
2021-11-17 23:13:05 +01:00
setState ( kDownloadStateListCalculated ) ;
2021-11-17 00:42:43 +01:00
}
2023-01-08 00:12:12 +01:00
void DownloadPacksDialog : : clearCache ( ) {
2022-07-09 16:48:09 +02:00
Common : : String iconsPath = ConfMan . get ( " iconspath " ) ;
if ( iconsPath . empty ( ) ) {
Common : : U32String str ( _ ( " ERROR: No icons path set " ) ) ;
setError ( str ) ;
return ;
}
Common : : FSDirectory * iconDir = new Common : : FSDirectory ( iconsPath ) ;
Common : : ArchiveMemberList iconFiles ;
2023-01-08 00:35:27 +01:00
iconDir - > listMatchingMembers ( iconFiles , _packsglob ) ;
2022-07-10 15:50:09 +02:00
int totalSize = 0 ;
2022-07-09 16:48:09 +02:00
for ( auto ic = iconFiles . begin ( ) ; ic ! = iconFiles . end ( ) ; + + ic ) {
Common : : String fname = ( * ic ) - > getName ( ) ;
Common : : SeekableReadStream * str = ( * ic ) - > createReadStream ( ) ;
uint32 size = str - > size ( ) ;
delete str ;
2022-07-10 15:50:09 +02:00
totalSize + = size ;
2022-07-09 16:48:09 +02:00
}
2023-02-03 15:28:31 +00:00
const char * sizeUnits ;
Common : : String size = Common : : getHumanReadableBytes ( totalSize , sizeUnits ) ;
2022-07-09 16:48:09 +02:00
2023-02-03 15:28:31 +00:00
GUI : : MessageDialog dialog ( Common : : U32String : : format ( _ ( " You are about to remove %s %S of data, deleting all previously downloaded %S. Do you want to proceed? " ) , size . c_str ( ) , _ ( sizeUnits ) . c_str ( ) , _packname . c_str ( ) ) , _ ( " Proceed " ) , _ ( " Cancel " ) ) ;
2022-07-09 16:48:09 +02:00
if ( dialog . runModal ( ) = = : : GUI : : kMessageOK ) {
// Build list of previously downloaded icon files
for ( auto ic = iconFiles . begin ( ) ; ic ! = iconFiles . end ( ) ; + + ic ) {
Common : : String fname = ( * ic ) - > getName ( ) ;
2022-08-02 00:09:28 +02:00
Common : : FSNode fs ( Common : : Path ( iconsPath ) . join ( fname ) ) ;
2022-07-09 16:48:09 +02:00
Common : : WriteStream * str = fs . createWriteStream ( ) ;
2023-01-08 00:35:27 +01:00
// Overwrite previously downloaded pack files with dummy data
2022-07-09 16:48:09 +02:00
str - > writeByte ( 0 ) ;
str - > finalize ( ) ;
}
g_state - > fileHash . clear ( ) ;
2023-01-08 00:35:27 +01:00
// Fetch current packs list file and re-trigger downloads
2022-07-09 16:48:09 +02:00
g_state - > downloadList ( ) ;
calculateList ( ) ;
_cancelButton - > setVisible ( true ) ;
} else {
_clearCacheButton - > setEnabled ( true ) ;
}
}
2021-11-17 15:25:23 +01:00
2021-11-16 23:36:21 +01:00
} // End of namespace GUI