GUI: Show bytes sizes in saves sync dialog

And also makes percentage calculated based on bytes sizes, not files count.
This commit is contained in:
Alexander Tkachev 2022-07-31 18:08:29 +03:00 committed by Eugene Sandulenko
parent 645e1a0c83
commit 85739018fe
7 changed files with 80 additions and 3 deletions

View file

@ -421,6 +421,20 @@ double CloudManager::getSyncDownloadingProgress() const {
return 1; return 1;
} }
uint64 CloudManager::getSyncDownloadBytesNumber() const {
Storage *storage = getCurrentStorage();
if (storage)
return storage->getSyncDownloadBytesNumber();
return 0;
}
uint64 CloudManager::getSyncDownloadTotalBytesNumber() const {
Storage *storage = getCurrentStorage();
if (storage)
return storage->getSyncDownloadTotalBytesNumber();
return 0;
}
double CloudManager::getSyncProgress() const { double CloudManager::getSyncProgress() const {
Storage *storage = getCurrentStorage(); Storage *storage = getCurrentStorage();
if (storage) if (storage)

View file

@ -251,6 +251,12 @@ public:
/** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */ /** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */
double getSyncDownloadingProgress() const; double getSyncDownloadingProgress() const;
/** Returns a number of bytes that is downloaded in current sync downloading progress. */
uint64 getSyncDownloadBytesNumber() const;
/** Returns a total number of bytes to be downloaded in current sync downloading progress. */
uint64 getSyncDownloadTotalBytesNumber() const;
/** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */
double getSyncProgress() const; double getSyncProgress() const;

View file

@ -21,6 +21,8 @@
#include "backends/cloud/savessyncrequest.h" #include "backends/cloud/savessyncrequest.h"
#include "backends/cloud/cloudmanager.h" #include "backends/cloud/cloudmanager.h"
#include "backends/cloud/downloadrequest.h"
#include "backends/cloud/id/iddownloadrequest.h"
#include "backends/networking/curl/curljsonrequest.h" #include "backends/networking/curl/curljsonrequest.h"
#include "backends/saves/default/default-saves.h" #include "backends/saves/default/default-saves.h"
#include "common/config-manager.h" #include "common/config-manager.h"
@ -35,7 +37,7 @@ namespace Cloud {
SavesSyncRequest::SavesSyncRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb): SavesSyncRequest::SavesSyncRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb):
Request(nullptr, ecb), _storage(storage), _boolCallback(callback), Request(nullptr, ecb), _storage(storage), _boolCallback(callback),
_workingRequest(nullptr), _ignoreCallback(false) { _workingRequest(nullptr), _ignoreCallback(false), _bytesToDownload(0), _bytesDownloaded(0) {
start(); start();
} }
@ -136,11 +138,14 @@ void SavesSyncRequest::directoryListedCallback(Storage::ListDirectoryResponse re
} }
} }
_bytesToDownload = 0;
_bytesDownloaded = 0;
debug(9, "\nSavesSyncRequest: "); debug(9, "\nSavesSyncRequest: ");
if (_filesToDownload.size() > 0) { if (_filesToDownload.size() > 0) {
debug(9, "download files:"); debug(9, "download files:");
for (uint32 i = 0; i < _filesToDownload.size(); ++i) { for (uint32 i = 0; i < _filesToDownload.size(); ++i) {
debug(9, " %s", _filesToDownload[i].name().c_str()); debug(9, " %s", _filesToDownload[i].name().c_str());
_bytesToDownload += _filesToDownload[i].size();
} }
debug(9, "%s", ""); debug(9, "%s", "");
} else { } else {
@ -303,6 +308,7 @@ void SavesSyncRequest::fileDownloadedCallback(Storage::BoolResponse response) {
//update local timestamp for downloaded file //update local timestamp for downloaded file
_localFilesTimestamps[_currentDownloadingFile.name()] = _currentDownloadingFile.timestamp(); _localFilesTimestamps[_currentDownloadingFile.name()] = _currentDownloadingFile.timestamp();
DefaultSaveFileManager::saveTimestamps(_localFilesTimestamps); DefaultSaveFileManager::saveTimestamps(_localFilesTimestamps);
_bytesDownloaded += _currentDownloadingFile.size();
//continue downloading files //continue downloading files
downloadNextFile(); downloadNextFile();
@ -381,6 +387,11 @@ double SavesSyncRequest::getDownloadingProgress() const {
if (_totalFilesToHandle == _filesToUpload.size()) if (_totalFilesToHandle == _filesToUpload.size())
return 1; //nothing to download => download complete return 1; //nothing to download => download complete
if (_bytesToDownload > 0) {
// can calculate more precise progress
return (double)(getDownloadedBytes()) / (double)(_bytesToDownload);
}
uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload.size(); uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload.size();
uint32 filesLeftToDownload = _filesToDownload.size() + (_currentDownloadingFile.name() != "" ? 1 : 0); uint32 filesLeftToDownload = _filesToDownload.size() + (_currentDownloadingFile.name() != "" ? 1 : 0);
return (double)(totalFilesToDownload - filesLeftToDownload) / (double)(totalFilesToDownload); return (double)(totalFilesToDownload - filesLeftToDownload) / (double)(totalFilesToDownload);
@ -405,6 +416,20 @@ Common::Array<Common::String> SavesSyncRequest::getFilesToDownload() {
return result; return result;
} }
uint32 SavesSyncRequest::getDownloadedBytes() const {
double currentFileProgress = 0;
if (const DownloadRequest *downloadRequest = dynamic_cast<DownloadRequest *>(_workingRequest))
currentFileProgress = downloadRequest->getProgress();
else if (const Id::IdDownloadRequest *idDownloadRequest = dynamic_cast<Id::IdDownloadRequest *>(_workingRequest))
currentFileProgress = idDownloadRequest->getProgress();
return _bytesDownloaded + currentFileProgress * _currentDownloadingFile.size();
}
uint32 SavesSyncRequest::getBytesToDownload() const {
return _bytesToDownload;
}
void SavesSyncRequest::finishError(Networking::ErrorResponse error, Networking::RequestState state) { void SavesSyncRequest::finishError(Networking::ErrorResponse error, Networking::RequestState state) {
debug(9, "SavesSync::finishError"); debug(9, "SavesSync::finishError");
//if we were downloading a file - remember the name //if we were downloading a file - remember the name

View file

@ -41,6 +41,7 @@ class SavesSyncRequest: public Networking::Request {
bool _ignoreCallback; bool _ignoreCallback;
uint32 _totalFilesToHandle; uint32 _totalFilesToHandle;
Common::String _date; Common::String _date;
uint32 _bytesToDownload, _bytesDownloaded;
void start(); void start();
void directoryListedCallback(Storage::ListDirectoryResponse response); void directoryListedCallback(Storage::ListDirectoryResponse response);
@ -71,6 +72,9 @@ public:
/** Returns an array of saves names which are not downloaded yet. */ /** Returns an array of saves names which are not downloaded yet. */
Common::Array<Common::String> getFilesToDownload(); Common::Array<Common::String> getFilesToDownload();
uint32 getDownloadedBytes() const;
uint32 getBytesToDownload() const;
}; };
} // End of namespace Cloud } // End of namespace Cloud

View file

@ -189,6 +189,24 @@ double Storage::getSyncDownloadingProgress() {
return result; return result;
} }
uint64 Storage::getSyncDownloadBytesNumber() {
uint64 result = 0;
_runningRequestsMutex.lock();
if (_savesSyncRequest)
result = _savesSyncRequest->getDownloadedBytes();
_runningRequestsMutex.unlock();
return result;
}
uint64 Storage::getSyncDownloadTotalBytesNumber() {
uint64 result = 0;
_runningRequestsMutex.lock();
if (_savesSyncRequest)
result = _savesSyncRequest->getBytesToDownload();
_runningRequestsMutex.unlock();
return result;
}
double Storage::getSyncProgress() { double Storage::getSyncProgress() {
double result = 1; double result = 1;
_runningRequestsMutex.lock(); _runningRequestsMutex.lock();

View file

@ -182,9 +182,15 @@ public:
/** Returns whether there is a SavesSyncRequest running. */ /** Returns whether there is a SavesSyncRequest running. */
virtual bool isSyncing(); virtual bool isSyncing();
/** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ /** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */
virtual double getSyncDownloadingProgress(); virtual double getSyncDownloadingProgress();
/** Returns a number of bytes that is downloaded in current sync downloading progress. */
virtual uint64 getSyncDownloadBytesNumber();
/** Returns a total number of bytes to be downloaded in current sync download progress. */
virtual uint64 getSyncDownloadTotalBytesNumber();
/** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */
virtual double getSyncProgress(); virtual double getSyncProgress();

View file

@ -109,7 +109,11 @@ void SaveLoadCloudSyncProgressDialog::pollCloudMan() {
_close = true; _close = true;
} }
_percentLabel->setLabel(Common::String::format("%u %%", progress)); Common::String downloaded, downloadedUnits, total, totalUnits;
downloaded = getHumanReadableBytes(CloudMan.getSyncDownloadBytesNumber(), downloadedUnits);
total = getHumanReadableBytes(CloudMan.getSyncDownloadTotalBytesNumber(), totalUnits);
_percentLabel->setLabel(Common::String::format("%u %% (%s %S / %s %S)", progress, downloaded.c_str(), _(downloadedUnits).c_str(), total.c_str(), _(totalUnits).c_str()));
_progressBar->setValue(progress); _progressBar->setValue(progress);
_progressBar->markAsDirty(); _progressBar->markAsDirty();