2016-05-29 23:25:08 +06:00
/* ScummVM - Graphic Adventure Engine
2016-09-03 12:46:38 +02:00
*
* 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 .
*
* 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 2
* of the License , or ( at your option ) any later version .
*
* 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
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
*/
2016-05-29 23:25:08 +06:00
# include "backends/cloud/savessyncrequest.h"
2016-06-09 18:49:17 +06:00
# include "backends/cloud/cloudmanager.h"
2016-07-26 15:04:56 +06:00
# include "backends/networking/curl/curljsonrequest.h"
# include "backends/saves/default/default-saves.h"
2016-05-30 21:21:31 +06:00
# include "common/config-manager.h"
2016-05-29 23:25:08 +06:00
# include "common/debug.h"
# include "common/file.h"
2016-06-05 20:20:22 +06:00
# include "common/json.h"
2016-05-30 13:35:53 +06:00
# include "common/savefile.h"
2016-05-30 21:21:31 +06:00
# include "common/system.h"
2016-06-06 10:25:49 +06:00
# include "gui/saveload-dialog.h"
2016-05-29 23:25:08 +06:00
namespace Cloud {
2016-05-31 01:51:32 +06:00
SavesSyncRequest : : SavesSyncRequest ( Storage * storage , Storage : : BoolCallback callback , Networking : : ErrorCallback ecb ) :
2016-06-05 15:44:05 +06:00
Request ( nullptr , ecb ) , CommandSender ( nullptr ) , _storage ( storage ) , _boolCallback ( callback ) ,
2016-05-29 23:25:08 +06:00
_workingRequest ( nullptr ) , _ignoreCallback ( false ) {
start ( ) ;
}
SavesSyncRequest : : ~ SavesSyncRequest ( ) {
_ignoreCallback = true ;
2016-07-22 15:38:24 +03:00
if ( _workingRequest )
_workingRequest - > finish ( ) ;
2016-05-29 23:25:08 +06:00
delete _boolCallback ;
}
void SavesSyncRequest : : start ( ) {
//cleanup
_ignoreCallback = true ;
2016-07-22 15:38:24 +03:00
if ( _workingRequest )
_workingRequest - > finish ( ) ;
2016-05-29 23:25:08 +06:00
_currentDownloadingFile = StorageFile ( ) ;
_currentUploadingFile = " " ;
_filesToDownload . clear ( ) ;
_filesToUpload . clear ( ) ;
_localFilesTimestamps . clear ( ) ;
2016-06-05 00:06:36 +06:00
_totalFilesToHandle = 0 ;
2016-05-29 23:25:08 +06:00
_ignoreCallback = false ;
//load timestamps
2016-06-18 19:35:57 +06:00
_localFilesTimestamps = DefaultSaveFileManager : : loadTimestamps ( ) ;
2016-05-29 23:25:08 +06:00
//list saves directory
2016-05-31 20:54:41 +06:00
Common : : String dir = _storage - > savesDirectoryPath ( ) ;
2016-07-22 15:38:24 +03:00
if ( dir . lastChar ( ) = = ' / ' )
dir . deleteLastChar ( ) ;
2016-05-31 01:51:32 +06:00
_workingRequest = _storage - > listDirectory (
2016-05-31 20:54:41 +06:00
dir ,
2016-05-31 01:51:32 +06:00
new Common : : Callback < SavesSyncRequest , Storage : : ListDirectoryResponse > ( this , & SavesSyncRequest : : directoryListedCallback ) ,
new Common : : Callback < SavesSyncRequest , Networking : : ErrorResponse > ( this , & SavesSyncRequest : : directoryListedErrorCallback )
) ;
2019-08-25 14:30:21 +07:00
if ( ! _workingRequest ) finishError ( Networking : : ErrorResponse ( this , " SavesSyncRequest::start: Storage couldn't create Request to list directory " ) ) ;
2016-05-29 23:25:08 +06:00
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : directoryListedCallback ( Storage : : ListDirectoryResponse response ) {
2016-05-30 21:21:31 +06:00
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-30 18:13:31 +06:00
2016-06-10 15:01:56 +06:00
if ( response . request ) _date = response . request - > date ( ) ;
2016-05-31 01:51:32 +06:00
Common : : HashMap < Common : : String , bool > localFileNotAvailableInCloud ;
2016-07-21 11:44:36 +06:00
for ( Common : : HashMap < Common : : String , uint32 > : : iterator i = _localFilesTimestamps . begin ( ) ; i ! = _localFilesTimestamps . end ( ) ; + + i ) {
2016-05-31 01:51:32 +06:00
localFileNotAvailableInCloud [ i - > _key ] = true ;
2016-05-30 21:21:31 +06:00
}
2016-05-29 23:25:08 +06:00
//determine which files to download and which files to upload
2016-05-31 01:51:32 +06:00
Common : : Array < StorageFile > & remoteFiles = response . value ;
2016-06-09 18:49:17 +06:00
uint64 totalSize = 0 ;
2019-07-16 20:13:50 +07:00
debug ( 9 , " SavesSyncRequest decisions: " ) ;
2016-05-29 23:25:08 +06:00
for ( uint32 i = 0 ; i < remoteFiles . size ( ) ; + + i ) {
StorageFile & file = remoteFiles [ i ] ;
2016-07-22 15:38:24 +03:00
if ( file . isDirectory ( ) )
continue ;
2016-06-09 18:49:17 +06:00
totalSize + = file . size ( ) ;
2019-07-24 19:12:14 +07:00
if ( file . name ( ) = = DefaultSaveFileManager : : TIMESTAMPS_FILENAME | | ! CloudMan . canSyncFilename ( file . name ( ) ) )
2016-07-22 15:38:24 +03:00
continue ;
2016-05-31 01:51:32 +06:00
2016-05-29 23:25:08 +06:00
Common : : String name = file . name ( ) ;
2016-07-22 15:38:24 +03:00
if ( ! _localFilesTimestamps . contains ( name ) ) {
2016-05-29 23:25:08 +06:00
_filesToDownload . push_back ( file ) ;
2019-07-16 20:13:50 +07:00
debug ( 9 , " - downloading file %s, because it is not present on local " , name . c_str ( ) ) ;
2016-07-22 15:38:24 +03:00
} else {
2016-05-31 01:51:32 +06:00
localFileNotAvailableInCloud [ name ] = false ;
2016-07-21 11:44:36 +06:00
2016-06-18 19:35:57 +06:00
if ( _localFilesTimestamps [ name ] = = file . timestamp ( ) )
continue ;
//we actually can have some files not only with timestamp < remote
//but also with timestamp > remote (when we have been using ANOTHER CLOUD and then switched back)
if ( _localFilesTimestamps [ name ] > file . timestamp ( ) | | _localFilesTimestamps [ name ] = = DefaultSaveFileManager : : INVALID_TIMESTAMP )
_filesToUpload . push_back ( file . name ( ) ) ;
else
_filesToDownload . push_back ( file ) ;
2019-07-16 20:13:50 +07:00
if ( _localFilesTimestamps [ name ] = = DefaultSaveFileManager : : INVALID_TIMESTAMP )
debug ( 9 , " - uploading file %s, because it is has invalid timestamp " , name . c_str ( ) ) ;
else if ( _localFilesTimestamps [ name ] > file . timestamp ( ) )
debug ( 9 , " - uploading file %s, because it is %d seconds newer than remote \n \t local = %d; \t remote = %d " , name . c_str ( ) , _localFilesTimestamps [ name ] - file . timestamp ( ) , _localFilesTimestamps [ name ] , file . timestamp ( ) ) ;
else
2019-07-23 23:46:25 +07:00
debug ( 9 , " - downloading file %s, because it is %d seconds older than remote \n \t local = %d; \t remote = %d " , name . c_str ( ) , file . timestamp ( ) - _localFilesTimestamps [ name ] , _localFilesTimestamps [ name ] , file . timestamp ( ) ) ;
2016-05-29 23:25:08 +06:00
}
}
2016-06-09 18:49:17 +06:00
CloudMan . setStorageUsedSpace ( CloudMan . getStorageIndex ( ) , totalSize ) ;
2016-05-31 01:51:32 +06:00
//upload files which are unavailable in cloud
for ( Common : : HashMap < Common : : String , bool > : : iterator i = localFileNotAvailableInCloud . begin ( ) ; i ! = localFileNotAvailableInCloud . end ( ) ; + + i ) {
2019-07-24 19:12:14 +07:00
if ( i - > _key = = DefaultSaveFileManager : : TIMESTAMPS_FILENAME | | ! CloudMan . canSyncFilename ( i - > _key ) )
2016-07-22 15:38:24 +03:00
continue ;
2019-07-16 20:13:50 +07:00
if ( i - > _value ) {
2016-07-22 15:38:24 +03:00
_filesToUpload . push_back ( i - > _key ) ;
2019-07-16 20:13:50 +07:00
debug ( 9 , " - uploading file %s, because it is not present on remote " , i - > _key . c_str ( ) ) ;
}
2016-05-29 23:25:08 +06:00
}
2019-08-03 14:52:57 +01:00
debug ( 9 , " \n SavesSyncRequest: " ) ;
if ( _filesToDownload . size ( ) > 0 ) {
debug ( 9 , " download files: " ) ;
for ( uint32 i = 0 ; i < _filesToDownload . size ( ) ; + + i ) {
debug ( 9 , " %s " , _filesToDownload [ i ] . name ( ) . c_str ( ) ) ;
}
debug ( 9 , " %s " , " " ) ;
2019-08-24 16:59:17 +01:00
} else {
debug ( 9 , " nothing to download " ) ;
2016-05-30 21:21:31 +06:00
}
2019-08-03 14:52:57 +01:00
debug ( 9 , " SavesSyncRequest: " ) ;
if ( _filesToUpload . size ( ) > 0 ) {
debug ( 9 , " upload files: " ) ;
for ( uint32 i = 0 ; i < _filesToUpload . size ( ) ; + + i ) {
debug ( 9 , " %s " , _filesToUpload [ i ] . c_str ( ) ) ;
}
2019-08-24 16:59:17 +01:00
} else {
debug ( 9 , " nothing to upload " ) ;
2016-05-30 21:21:31 +06:00
}
2016-06-05 00:06:36 +06:00
_totalFilesToHandle = _filesToDownload . size ( ) + _filesToUpload . size ( ) ;
2016-05-30 21:21:31 +06:00
2016-05-29 23:25:08 +06:00
//start downloading files
downloadNextFile ( ) ;
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : directoryListedErrorCallback ( Networking : : ErrorResponse error ) {
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-31 01:51:32 +06:00
2019-07-16 14:12:45 +07:00
if ( error . failed ) debug ( 9 , " %s " , error . response . c_str ( ) ) ;
2016-05-31 01:51:32 +06:00
bool irrecoverable = error . interrupted | | error . failed ;
if ( error . failed ) {
Common : : JSONValue * value = Common : : JSON : : parse ( error . response . c_str ( ) ) ;
2019-07-16 14:12:45 +07:00
// somehow OneDrive returns JSON with '.' in unexpected places, try fixing it
if ( ! value ) {
Common : : String fixedResponse = error . response ;
for ( uint32 i = 0 ; i < fixedResponse . size ( ) ; + + i ) {
if ( fixedResponse [ i ] = = ' . ' )
fixedResponse . replace ( i , 1 , " " ) ;
}
value = Common : : JSON : : parse ( fixedResponse . c_str ( ) ) ;
}
2016-05-31 01:51:32 +06:00
if ( value ) {
if ( value - > isObject ( ) ) {
Common : : JSONObject object = value - > asObject ( ) ;
//Dropbox-related error:
2016-07-26 15:54:48 +06:00
if ( object . contains ( " error_summary " ) & & object . getVal ( " error_summary " ) - > isString ( ) ) {
2016-05-31 01:51:32 +06:00
Common : : String summary = object . getVal ( " error_summary " ) - > asString ( ) ;
2016-07-21 11:44:36 +06:00
if ( summary . contains ( " not_found " ) ) {
2016-05-31 01:51:32 +06:00
irrecoverable = false ;
}
}
//OneDrive-related error:
2016-07-26 15:54:48 +06:00
if ( object . contains ( " error " ) & & object . getVal ( " error " ) - > isObject ( ) ) {
2016-05-31 01:51:32 +06:00
Common : : JSONObject errorNode = object . getVal ( " error " ) - > asObject ( ) ;
2016-07-26 15:04:56 +06:00
if ( Networking : : CurlJsonRequest : : jsonContainsString ( errorNode , " code " , " SavesSyncRequest " ) ) {
2016-05-31 01:51:32 +06:00
Common : : String code = errorNode . getVal ( " code " ) - > asString ( ) ;
if ( code = = " itemNotFound " ) {
irrecoverable = false ;
}
}
}
}
delete value ;
}
2016-06-07 16:27:04 +06:00
2019-07-16 14:12:45 +07:00
//Google Drive, Box and OneDrive-related ScummVM-based error
2016-06-07 16:27:04 +06:00
if ( error . response . contains ( " subdirectory not found " ) ) {
irrecoverable = false ; //base "/ScummVM/" folder not found
} else if ( error . response . contains ( " no such file found in its parent directory " ) ) {
irrecoverable = false ; //"Saves" folder within "/ScummVM/" not found
2019-07-16 14:12:45 +07:00
} else if ( error . response . contains ( " itemNotFound " ) & & error . response . contains ( " Item does not exist " ) ) {
irrecoverable = false ; //"saves" folder within application folder is not found
2016-06-07 16:27:04 +06:00
}
2016-05-31 01:51:32 +06:00
}
if ( irrecoverable ) {
finishError ( error ) ;
return ;
}
2016-05-31 21:47:57 +06:00
//we're lucky - user just lacks his "/cloud/" folder - let's create one
Common : : String dir = _storage - > savesDirectoryPath ( ) ;
2016-07-22 15:38:24 +03:00
if ( dir . lastChar ( ) = = ' / ' )
dir . deleteLastChar ( ) ;
2019-07-16 20:13:50 +07:00
debug ( 9 , " \n SavesSyncRequest: creating %s " , dir . c_str ( ) ) ;
2016-07-21 09:29:54 +02:00
_workingRequest = _storage - > createDirectory (
dir ,
2016-05-31 21:47:57 +06:00
new Common : : Callback < SavesSyncRequest , Storage : : BoolResponse > ( this , & SavesSyncRequest : : directoryCreatedCallback ) ,
new Common : : Callback < SavesSyncRequest , Networking : : ErrorResponse > ( this , & SavesSyncRequest : : directoryCreatedErrorCallback )
) ;
2016-07-22 15:38:24 +03:00
if ( ! _workingRequest )
2019-08-25 14:30:21 +07:00
finishError ( Networking : : ErrorResponse ( this , " SavesSyncRequest::directoryListedErrorCallback: Storage couldn't create Request to create remote directory " ) ) ;
2016-05-31 21:47:57 +06:00
}
void SavesSyncRequest : : directoryCreatedCallback ( Storage : : BoolResponse response ) {
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-31 21:47:57 +06:00
//stop syncing if failed to create saves directory
if ( ! response . value ) {
2019-08-25 14:30:21 +07:00
finishError ( Networking : : ErrorResponse ( this , false , true , " SavesSyncRequest::directoryCreatedCallback: failed to create remote directory " , - 1 ) ) ;
2016-05-31 21:47:57 +06:00
return ;
}
//continue with empty files list
2016-05-31 01:51:32 +06:00
Common : : Array < StorageFile > files ;
2016-05-31 21:47:57 +06:00
directoryListedCallback ( Storage : : ListDirectoryResponse ( response . request , files ) ) ;
}
void SavesSyncRequest : : directoryCreatedErrorCallback ( Networking : : ErrorResponse error ) {
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-31 21:47:57 +06:00
//stop syncing if failed to create saves directory
finishError ( error ) ;
2016-05-31 01:51:32 +06:00
}
2016-05-29 23:25:08 +06:00
void SavesSyncRequest : : downloadNextFile ( ) {
if ( _filesToDownload . empty ( ) ) {
2016-06-05 21:07:55 +06:00
_currentDownloadingFile = StorageFile ( " " , 0 , 0 , false ) ; //so getFilesToDownload() would return an empty array
2016-06-06 10:25:49 +06:00
sendCommand ( GUI : : kSavesSyncEndedCmd , 0 ) ;
2016-05-29 23:25:08 +06:00
uploadNextFile ( ) ;
return ;
}
_currentDownloadingFile = _filesToDownload . back ( ) ;
_filesToDownload . pop_back ( ) ;
2016-06-06 10:25:49 +06:00
sendCommand ( GUI : : kSavesSyncProgressCmd , ( int ) ( getDownloadingProgress ( ) * 100 ) ) ;
2016-07-21 11:44:36 +06:00
2019-07-16 20:13:50 +07:00
debug ( 9 , " \n SavesSyncRequest: downloading %s (%d %%) " , _currentDownloadingFile . name ( ) . c_str ( ) , ( int ) ( getProgress ( ) * 100 ) ) ;
2016-07-21 09:29:54 +02:00
_workingRequest = _storage - > downloadById (
_currentDownloadingFile . id ( ) ,
DefaultSaveFileManager : : concatWithSavesPath ( _currentDownloadingFile . name ( ) ) ,
2016-05-31 01:51:32 +06:00
new Common : : Callback < SavesSyncRequest , Storage : : BoolResponse > ( this , & SavesSyncRequest : : fileDownloadedCallback ) ,
new Common : : Callback < SavesSyncRequest , Networking : : ErrorResponse > ( this , & SavesSyncRequest : : fileDownloadedErrorCallback )
2016-05-29 23:25:08 +06:00
) ;
2016-07-22 15:38:24 +03:00
if ( ! _workingRequest )
2019-08-25 14:30:21 +07:00
finishError ( Networking : : ErrorResponse ( this , " SavesSyncRequest::downloadNextFile: Storage couldn't create Request to download a file " ) ) ;
2016-05-29 23:25:08 +06:00
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : fileDownloadedCallback ( Storage : : BoolResponse response ) {
2016-05-30 21:21:31 +06:00
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-29 23:25:08 +06:00
//stop syncing if download failed
2016-05-31 01:51:32 +06:00
if ( ! response . value ) {
2016-06-19 13:50:11 +06:00
//delete the incomplete file
g_system - > getSavefileManager ( ) - > removeSavefile ( _currentDownloadingFile . name ( ) ) ;
2019-08-25 14:30:21 +07:00
finishError ( Networking : : ErrorResponse ( this , false , true , " SavesSyncRequest::fileDownloadedCallback: failed to download a file " , - 1 ) ) ;
2016-05-29 23:25:08 +06:00
return ;
}
//update local timestamp for downloaded file
2016-06-18 19:35:57 +06:00
_localFilesTimestamps = DefaultSaveFileManager : : loadTimestamps ( ) ;
2016-05-29 23:25:08 +06:00
_localFilesTimestamps [ _currentDownloadingFile . name ( ) ] = _currentDownloadingFile . timestamp ( ) ;
2016-06-18 19:35:57 +06:00
DefaultSaveFileManager : : saveTimestamps ( _localFilesTimestamps ) ;
2016-05-29 23:25:08 +06:00
//continue downloading files
downloadNextFile ( ) ;
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : fileDownloadedErrorCallback ( Networking : : ErrorResponse error ) {
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-31 01:51:32 +06:00
//stop syncing if download failed
2016-07-21 11:44:36 +06:00
finishError ( error ) ;
2016-05-31 01:51:32 +06:00
}
2016-05-29 23:25:08 +06:00
void SavesSyncRequest : : uploadNextFile ( ) {
if ( _filesToUpload . empty ( ) ) {
2016-06-21 17:07:23 +06:00
finishSync ( true ) ;
2016-05-29 23:25:08 +06:00
return ;
}
_currentUploadingFile = _filesToUpload . back ( ) ;
_filesToUpload . pop_back ( ) ;
2016-07-21 11:44:36 +06:00
2019-07-16 20:13:50 +07:00
debug ( 9 , " \n SavesSyncRequest: uploading %s (%d %%) " , _currentUploadingFile . c_str ( ) , ( int ) ( getProgress ( ) * 100 ) ) ;
2016-07-14 07:44:58 +06:00
if ( _storage - > uploadStreamSupported ( ) ) {
2016-07-21 09:29:54 +02:00
_workingRequest = _storage - > upload (
_storage - > savesDirectoryPath ( ) + _currentUploadingFile ,
g_system - > getSavefileManager ( ) - > openRawFile ( _currentUploadingFile ) ,
2016-07-14 07:44:58 +06:00
new Common : : Callback < SavesSyncRequest , Storage : : UploadResponse > ( this , & SavesSyncRequest : : fileUploadedCallback ) ,
new Common : : Callback < SavesSyncRequest , Networking : : ErrorResponse > ( this , & SavesSyncRequest : : fileUploadedErrorCallback )
) ;
} else {
2016-07-21 09:29:54 +02:00
_workingRequest = _storage - > upload (
_storage - > savesDirectoryPath ( ) + _currentUploadingFile ,
DefaultSaveFileManager : : concatWithSavesPath ( _currentUploadingFile ) ,
2016-07-14 07:44:58 +06:00
new Common : : Callback < SavesSyncRequest , Storage : : UploadResponse > ( this , & SavesSyncRequest : : fileUploadedCallback ) ,
new Common : : Callback < SavesSyncRequest , Networking : : ErrorResponse > ( this , & SavesSyncRequest : : fileUploadedErrorCallback )
) ;
}
2019-08-25 14:30:21 +07:00
if ( ! _workingRequest ) finishError ( Networking : : ErrorResponse ( this , " SavesSyncRequest::uploadNextFile: Storage couldn't create Request to upload a file " ) ) ;
2016-05-29 23:25:08 +06:00
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : fileUploadedCallback ( Storage : : UploadResponse response ) {
2016-05-30 21:21:31 +06:00
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-07-21 11:44:36 +06:00
2016-05-30 13:35:53 +06:00
//update local timestamp for the uploaded file
2016-06-18 19:35:57 +06:00
_localFilesTimestamps = DefaultSaveFileManager : : loadTimestamps ( ) ;
2016-05-31 01:51:32 +06:00
_localFilesTimestamps [ _currentUploadingFile ] = response . value . timestamp ( ) ;
2016-06-18 19:35:57 +06:00
DefaultSaveFileManager : : saveTimestamps ( _localFilesTimestamps ) ;
2016-05-29 23:25:08 +06:00
//continue uploading files
uploadNextFile ( ) ;
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : fileUploadedErrorCallback ( Networking : : ErrorResponse error ) {
_workingRequest = nullptr ;
2016-07-22 15:38:24 +03:00
if ( _ignoreCallback )
return ;
2016-05-31 01:51:32 +06:00
//stop syncing if upload failed
finishError ( error ) ;
}
2016-05-29 23:25:08 +06:00
void SavesSyncRequest : : handle ( ) { }
void SavesSyncRequest : : restart ( ) { start ( ) ; }
2016-07-20 18:47:34 +06:00
double SavesSyncRequest : : getDownloadingProgress ( ) const {
2016-06-05 21:07:55 +06:00
if ( _totalFilesToHandle = = 0 ) {
2016-07-22 15:38:24 +03:00
if ( _state = = Networking : : FINISHED )
return 1 ; //nothing to upload and download => Request ends soon
2016-06-05 21:07:55 +06:00
return 0 ; //directory not listed yet
}
2016-07-22 15:38:24 +03:00
if ( _totalFilesToHandle = = _filesToUpload . size ( ) )
return 1 ; //nothing to download => download complete
2016-06-05 21:07:55 +06:00
uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload . size ( ) ;
uint32 filesLeftToDownload = _filesToDownload . size ( ) + ( _currentDownloadingFile . name ( ) ! = " " ? 1 : 0 ) ;
return ( double ) ( totalFilesToDownload - filesLeftToDownload ) / ( double ) ( totalFilesToDownload ) ;
}
2016-07-20 18:47:34 +06:00
double SavesSyncRequest : : getProgress ( ) const {
2016-06-05 00:06:36 +06:00
if ( _totalFilesToHandle = = 0 ) {
2016-07-22 15:38:24 +03:00
if ( _state = = Networking : : FINISHED )
return 1 ; //nothing to upload and download => Request ends soon
2016-06-05 00:06:36 +06:00
return 0 ; //directory not listed yet
}
return ( double ) ( _totalFilesToHandle - _filesToDownload . size ( ) - _filesToUpload . size ( ) ) / ( double ) ( _totalFilesToHandle ) ;
}
2016-06-05 00:13:33 +06:00
Common : : Array < Common : : String > SavesSyncRequest : : getFilesToDownload ( ) {
Common : : Array < Common : : String > result ;
for ( uint32 i = 0 ; i < _filesToDownload . size ( ) ; + + i )
result . push_back ( _filesToDownload [ i ] . name ( ) ) ;
if ( _currentDownloadingFile . name ( ) ! = " " )
result . push_back ( _currentDownloadingFile . name ( ) ) ;
2016-06-05 00:06:36 +06:00
return result ;
}
2016-05-31 01:51:32 +06:00
void SavesSyncRequest : : finishError ( Networking : : ErrorResponse error ) {
2016-07-23 12:52:27 +06:00
debug ( 9 , " SavesSync::finishError " ) ;
2016-06-19 13:50:11 +06:00
//if we were downloading a file - remember the name
//and make the Request close() it, so we can delete it
2016-07-21 11:44:36 +06:00
Common : : String name = _currentDownloadingFile . name ( ) ;
2016-06-19 13:50:11 +06:00
if ( _workingRequest ) {
_ignoreCallback = true ;
_workingRequest - > finish ( ) ;
_workingRequest = nullptr ;
_ignoreCallback = false ;
}
//unlock all the files by making getFilesToDownload() return empty array
_currentDownloadingFile = StorageFile ( ) ;
_filesToDownload . clear ( ) ;
//delete the incomplete file
2016-07-22 15:38:24 +03:00
if ( name ! = " " )
g_system - > getSavefileManager ( ) - > removeSavefile ( name ) ;
2016-05-31 01:51:32 +06:00
Request : : finishError ( error ) ;
}
2016-05-29 23:25:08 +06:00
2016-06-21 17:07:23 +06:00
void SavesSyncRequest : : finishSync ( bool success ) {
2016-05-31 01:51:32 +06:00
Request : : finishSuccess ( ) ;
2016-05-29 23:25:08 +06:00
2016-06-10 15:01:56 +06:00
//update last successful sync date
CloudMan . setStorageLastSync ( CloudMan . getStorageIndex ( ) , _date ) ;
2016-07-22 15:38:24 +03:00
if ( _boolCallback )
( * _boolCallback ) ( Storage : : BoolResponse ( this , success ) ) ;
2016-05-29 23:25:08 +06:00
}
} // End of namespace Cloud