From 09639e7f230bf51974492dcdcaf58066dd5db40d Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Tue, 2 Aug 2022 01:42:08 +0300 Subject: [PATCH] GUI: Add files amount to SaveLoadCloudSyncProgressDialog Also adds Cloud::Storage::SyncDownloadingInfo struct to pass around a bunch of numbers instead of having 3 methods for each number (in CloudManager, Storage and SavesSyncRequest). --- backends/cloud/cloudmanager.cpp | 12 ++--------- backends/cloud/cloudmanager.h | 7 ++----- backends/cloud/savessyncrequest.cpp | 16 +++++++++++++++ backends/cloud/savessyncrequest.h | 9 +++++--- backends/cloud/storage.cpp | 18 ++++------------ backends/cloud/storage.h | 11 ++++++---- gui/saveload-dialog.cpp | 32 ++++++++++++++++++++++------- 7 files changed, 62 insertions(+), 43 deletions(-) diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp index dce237683db..251674174ec 100644 --- a/backends/cloud/cloudmanager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -421,18 +421,10 @@ double CloudManager::getSyncDownloadingProgress() const { return 1; } -uint64 CloudManager::getSyncDownloadBytesNumber() const { +void CloudManager::getSyncDownloadingInfo(Storage::SyncDownloadingInfo &info) 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; + storage->getSyncDownloadingInfo(info); } double CloudManager::getSyncProgress() const { diff --git a/backends/cloud/cloudmanager.h b/backends/cloud/cloudmanager.h index 6fe12d85546..a0d36b87602 100644 --- a/backends/cloud/cloudmanager.h +++ b/backends/cloud/cloudmanager.h @@ -251,11 +251,8 @@ public: /** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */ 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; + /** Fills a struct with numbers about current sync downloading progress. */ + void getSyncDownloadingInfo(Storage::SyncDownloadingInfo &info) const; /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ double getSyncProgress() const; diff --git a/backends/cloud/savessyncrequest.cpp b/backends/cloud/savessyncrequest.cpp index bc0734c26e7..6f7646ea4e9 100644 --- a/backends/cloud/savessyncrequest.cpp +++ b/backends/cloud/savessyncrequest.cpp @@ -394,9 +394,25 @@ double SavesSyncRequest::getDownloadingProgress() const { uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload.size(); uint32 filesLeftToDownload = _filesToDownload.size() + (_currentDownloadingFile.name() != "" ? 1 : 0); + if (filesLeftToDownload > totalFilesToDownload) + filesLeftToDownload = totalFilesToDownload; return (double)(totalFilesToDownload - filesLeftToDownload) / (double)(totalFilesToDownload); } +void SavesSyncRequest::getDownloadingInfo(Storage::SyncDownloadingInfo &info) const { + info.bytesDownloaded = getDownloadedBytes(); + info.bytesToDownload = getBytesToDownload(); + + uint32 totalFilesToDownload = _totalFilesToHandle - _filesToUpload.size(); + uint32 filesLeftToDownload = _filesToDownload.size() + (_currentDownloadingFile.name() != "" ? 1 : 0); + if (filesLeftToDownload > totalFilesToDownload) + filesLeftToDownload = totalFilesToDownload; + info.filesDownloaded = totalFilesToDownload - filesLeftToDownload; + info.filesToDownload = totalFilesToDownload; + + info.inProgress = (totalFilesToDownload > 0 && filesLeftToDownload > 0); +} + double SavesSyncRequest::getProgress() const { if (_totalFilesToHandle == 0) { if (_state == Networking::FINISHED) diff --git a/backends/cloud/savessyncrequest.h b/backends/cloud/savessyncrequest.h index 44bf7457c7f..1a8bcc0a20a 100644 --- a/backends/cloud/savessyncrequest.h +++ b/backends/cloud/savessyncrequest.h @@ -57,6 +57,9 @@ class SavesSyncRequest: public Networking::Request { virtual void finishError(Networking::ErrorResponse error, Networking::RequestState state = Networking::FINISHED); void finishSync(bool success); + uint32 getDownloadedBytes() const; + uint32 getBytesToDownload() const; + public: SavesSyncRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb); virtual ~SavesSyncRequest(); @@ -67,14 +70,14 @@ public: /** Returns a number in range [0, 1], where 1 is "complete". */ double getDownloadingProgress() const; + /** Fills a struct with numbers about current sync downloading progress. */ + void getDownloadingInfo(Storage::SyncDownloadingInfo &info) const; + /** Returns a number in range [0, 1], where 1 is "complete". */ double getProgress() const; /** Returns an array of saves names which are not downloaded yet. */ Common::Array getFilesToDownload(); - - uint32 getDownloadedBytes() const; - uint32 getBytesToDownload() const; }; } // End of namespace Cloud diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp index 70cc9247bb8..ae8c09bcff7 100644 --- a/backends/cloud/storage.cpp +++ b/backends/cloud/storage.cpp @@ -189,22 +189,12 @@ double Storage::getSyncDownloadingProgress() { return result; } -uint64 Storage::getSyncDownloadBytesNumber() { - uint64 result = 0; +void Storage::getSyncDownloadingInfo(SyncDownloadingInfo& info) { _runningRequestsMutex.lock(); - if (_savesSyncRequest) - result = _savesSyncRequest->getDownloadedBytes(); + if (_savesSyncRequest) { + _savesSyncRequest->getDownloadingInfo(info); + } _runningRequestsMutex.unlock(); - return result; -} - -uint64 Storage::getSyncDownloadTotalBytesNumber() { - uint64 result = 0; - _runningRequestsMutex.lock(); - if (_savesSyncRequest) - result = _savesSyncRequest->getBytesToDownload(); - _runningRequestsMutex.unlock(); - return result; } double Storage::getSyncProgress() { diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 9ebdd6e5215..a802e403c36 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -185,11 +185,14 @@ public: /** Returns a number in [0, 1] range which represents current sync downloading progress (1 = complete). */ virtual double getSyncDownloadingProgress(); - /** Returns a number of bytes that is downloaded in current sync downloading progress. */ - virtual uint64 getSyncDownloadBytesNumber(); + struct SyncDownloadingInfo { + uint64 bytesDownloaded = 0, bytesToDownload = 0; + uint64 filesDownloaded = 0, filesToDownload = 0; + bool inProgress = false; + }; - /** Returns a total number of bytes to be downloaded in current sync download progress. */ - virtual uint64 getSyncDownloadTotalBytesNumber(); + /** Fills a struct with numbers about current sync downloading progress. */ + virtual void getSyncDownloadingInfo(SyncDownloadingInfo &info); /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ virtual double getSyncProgress(); diff --git a/gui/saveload-dialog.cpp b/gui/saveload-dialog.cpp index 62e5a8dbedc..cde42c93a77 100644 --- a/gui/saveload-dialog.cpp +++ b/gui/saveload-dialog.cpp @@ -100,7 +100,8 @@ void SaveLoadCloudSyncProgressDialog::handleTickle() { void SaveLoadCloudSyncProgressDialog::pollCloudMan() { _pollFrame = (_pollFrame + 1) % 60; - if (_pollFrame != 1) return; + if (_pollFrame != 1) + return; const bool syncing = CloudMan.isSyncing(); const uint32 progress = (uint32)(100 * CloudMan.getSyncDownloadingProgress()); @@ -109,11 +110,26 @@ void SaveLoadCloudSyncProgressDialog::pollCloudMan() { _close = true; } - Common::String downloaded, downloadedUnits, total, totalUnits; - downloaded = getHumanReadableBytes(CloudMan.getSyncDownloadBytesNumber(), downloadedUnits); - total = getHumanReadableBytes(CloudMan.getSyncDownloadTotalBytesNumber(), totalUnits); + Cloud::Storage::SyncDownloadingInfo info; + CloudMan.getSyncDownloadingInfo(info); - _percentLabel->setLabel(Common::String::format("%u %% (%s %S / %s %S)", progress, downloaded.c_str(), _(downloadedUnits).c_str(), total.c_str(), _(totalUnits).c_str())); + Common::String downloaded, downloadedUnits, total, totalUnits; + downloaded = getHumanReadableBytes(info.bytesDownloaded, downloadedUnits); + total = getHumanReadableBytes(info.bytesToDownload, totalUnits); + + Common::String progressPercent = Common::String::format("%u %%", progress); + Common::String filesDownloaded = Common::String::format("%llu", info.filesDownloaded); + Common::String filesToDownload = Common::String::format("%llu", info.filesToDownload); + + _percentLabel->setLabel( + Common::U32String::format( + _("%s (%s %S / %s %S, %s / %s files)"), + progressPercent.c_str(), + downloaded.c_str(), _(downloadedUnits).c_str(), + total.c_str(), _(totalUnits).c_str(), + filesDownloaded.c_str(), filesToDownload.c_str() + ) + ); _progressBar->setValue(progress); _progressBar->markAsDirty(); @@ -389,7 +405,8 @@ ButtonWidget *SaveLoadChooserDialog::createSwitchButton(const Common::String &na #if defined(USE_CLOUD) && defined(USE_LIBCURL) void SaveLoadChooserDialog::pollCloudMan() { _pollFrame = (_pollFrame + 1) % 60; - if (_pollFrame != 1) return; + if (_pollFrame != 1) + return; const bool syncing = CloudMan.isSyncing(); const uint32 progress = (uint32)(100 * CloudMan.getSyncDownloadingProgress()); @@ -405,7 +422,8 @@ void SaveLoadChooserDialog::pollCloudMan() { } } - if (update) updateSaveList(); + if (update) + updateSaveList(); } #endif