aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backends/cloud/cloudmanager.cpp155
-rw-r--r--backends/cloud/cloudmanager.h42
-rw-r--r--backends/cloud/dropbox/dropboxstorage.cpp11
-rw-r--r--backends/cloud/googledrive/googledrivestorage.cpp7
-rw-r--r--backends/cloud/onedrive/onedrivestorage.cpp6
-rw-r--r--gui/options.cpp63
-rw-r--r--gui/options.h4
7 files changed, 165 insertions, 123 deletions
diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp
index cb7e6f7cb3..5c69e30520 100644
--- a/backends/cloud/cloudmanager.cpp
+++ b/backends/cloud/cloudmanager.cpp
@@ -36,87 +36,101 @@ DECLARE_SINGLETON(Cloud::CloudManager);
namespace Cloud {
-CloudManager::CloudManager() : _currentStorageIndex(0) {}
+CloudManager::CloudManager() : _currentStorageIndex(0), _activeStorage(nullptr) {}
CloudManager::~CloudManager() {
//TODO: do we have to save storages on manager destruction?
- for (uint32 i = 0; i < _storages.size(); ++i)
- delete _storages[i];
- _storages.clear();
+ delete _activeStorage;
}
-void CloudManager::init() {
- bool offerDropbox = false;
- bool offerOneDrive = false;
- bool offerGoogleDrive = true;
-
- if (ConfMan.hasKey("storages_number", "cloud")) {
- int storages = ConfMan.getInt("storages_number", "cloud");
- for (int i = 1; i <= storages; ++i) {
- Storage *loaded = 0;
- Common::String keyPrefix = Common::String::format("storage%d_", i);
- if (ConfMan.hasKey(keyPrefix + "type", "cloud")) {
- Common::String storageType = ConfMan.get(keyPrefix + "type", "cloud");
- if (storageType == "Dropbox") loaded = Dropbox::DropboxStorage::loadFromConfig(keyPrefix);
- else if (storageType == "OneDrive") loaded = OneDrive::OneDriveStorage::loadFromConfig(keyPrefix);
- else if (storageType == "Google Drive") {
- loaded = GoogleDrive::GoogleDriveStorage::loadFromConfig(keyPrefix);
- offerGoogleDrive = false;
- } else warning("Unknown cloud storage type '%s' passed", storageType.c_str());
- } else {
- warning("Cloud storage #%d (out of %d) is missing.", i, storages);
- }
- if (loaded) _storages.push_back(loaded);
- }
-
- uint32 index = 0;
- if (ConfMan.hasKey("current_storage", "cloud")) {
- index = ConfMan.getInt("current_storage", "cloud") - 1; //count from 1, all for UX
- }
- if (index >= _storages.size()) index = 0;
- _currentStorageIndex = index;
-
- if (_storages.size() == 0) offerDropbox = true;
- } else {
- offerDropbox = true;
+namespace {
+uint64 atoull(Common::String s) {
+ uint64 result = 0;
+ for (uint32 i = 0; i < s.size(); ++i) {
+ if (s[i] < '0' || s[i] > '9') break;
+ result = result * 10L + (s[i] - '0');
}
- if (offerDropbox) {
- //this is temporary console offer to auth with Dropbox
- Dropbox::DropboxStorage::authThroughConsole();
- } else if (offerOneDrive) {
- //OneDrive time
- OneDrive::OneDriveStorage::authThroughConsole();
- } else if (offerGoogleDrive) {
- GoogleDrive::GoogleDriveStorage::authThroughConsole();
- _currentStorageIndex = 100;
+ return result;
+}
+}
+
+Common::String CloudManager::getStorageConfigName(uint32 index) const {
+ switch (index) {
+ case kStorageNoneId: return "<none>";
+ case kStorageDropboxId: return "Dropbox";
+ case kStorageOneDriveId: return "OneDrive";
+ case kStorageGoogleDriveId: return "GoogleDrive";
+ }
+ return "Unknown";
+}
+
+void CloudManager::loadStorage() {
+ switch (_currentStorageIndex) {
+ case kStorageDropboxId:
+ _activeStorage = Dropbox::DropboxStorage::loadFromConfig("storage_" + getStorageConfigName(_currentStorageIndex) + "_");
+ break;
+
+ case kStorageOneDriveId:
+ _activeStorage = OneDrive::OneDriveStorage::loadFromConfig("storage_" + getStorageConfigName(_currentStorageIndex) + "_");
+ break;
+
+ case kStorageGoogleDriveId:
+ _activeStorage = GoogleDrive::GoogleDriveStorage::loadFromConfig("storage_" + getStorageConfigName(_currentStorageIndex) + "_");
+ break;
+
+ default:
+ _activeStorage = nullptr;
+ }
+
+ if (!_activeStorage) {
+ _currentStorageIndex = kStorageNoneId;
}
}
+void CloudManager::init() {
+ //init configs structs
+ for (uint32 i = 0; i < kStorageTotal; ++i) {
+ Common::String name = getStorageConfigName(i);
+ StorageConfig config;
+ config.name = _(name);
+ config.username = "";
+ config.lastSyncDate = "";
+ config.usedBytes = 0;
+ if (ConfMan.hasKey("storage_" + name + "_username", "cloud"))
+ config.username = ConfMan.get("storage_" + name + "_username", "cloud");
+ if (ConfMan.hasKey("storage_" + name + "_lastSync", "cloud"))
+ config.lastSyncDate = ConfMan.get("storage_" + name + "_lastSync", "cloud");
+ if (ConfMan.hasKey("storage_" + name + "_usedBytes", "cloud"))
+ config.usedBytes = atoull(ConfMan.get("storage_" + name + "_usedBytes", "cloud"));
+ _storages.push_back(config);
+ }
+
+ //load an active storage if there is any
+ _currentStorageIndex = kStorageNoneId;
+ if (ConfMan.hasKey("current_storage", "cloud"))
+ _currentStorageIndex = ConfMan.getInt("current_storage", "cloud");
+
+ loadStorage();
+}
+
void CloudManager::save() {
- ConfMan.set("storages_number", Common::String::format("%d", _storages.size()), "cloud");
- ConfMan.set("current_storage", Common::String::format("%d", _currentStorageIndex + 1), "cloud");
- for (uint32 i = 0; i < _storages.size(); ++i)
- _storages[i]->saveConfig(Common::String::format("storage%d_", i + 1));
+ ConfMan.set("current_storage", Common::String::format("%d", _currentStorageIndex), "cloud");
+ if (_activeStorage)
+ _activeStorage->saveConfig("storage_" + getStorageConfigName(_currentStorageIndex) + "_");
ConfMan.flushToDisk();
}
-void CloudManager::addStorage(Storage *storage, bool makeCurrent, bool saveConfig) {
- if (!storage) error("Cloud::CloudManager: NULL storage passed");
- _storages.push_back(storage);
- if (makeCurrent) _currentStorageIndex = _storages.size() - 1;
- if (saveConfig) save();
+void CloudManager::replaceStorage(Storage *storage, uint32 index) {
+ if (!storage) error("CloudManager::replaceStorage: NULL storage passed");
+ if (index >= kStorageTotal) error("CloudManager::replaceStorage: invalid index passed");
+ delete _activeStorage;
+ _activeStorage = storage;
+ _currentStorageIndex = index;
+ save();
}
Storage *CloudManager::getCurrentStorage() const {
- if (_currentStorageIndex < _storages.size())
- return _storages[_currentStorageIndex];
- return nullptr;
-}
-
-Common::String CloudManager::getStorageName() const {
- Storage *storage = getCurrentStorage();
- if (storage) return storage->name();
- return _("No active storage");
+ return _activeStorage;
}
uint32 CloudManager::getStorageIndex() const {
@@ -126,7 +140,7 @@ uint32 CloudManager::getStorageIndex() const {
Common::StringArray CloudManager::listStorages() const {
Common::StringArray result;
for (uint32 i = 0; i < _storages.size(); ++i) {
- result.push_back(_storages[i]->name());
+ result.push_back(_storages[i].name);
}
return result;
}
@@ -144,24 +158,25 @@ bool CloudManager::switchStorage(uint32 index) {
}
_currentStorageIndex = index;
+ loadStorage();
save();
return true;
}
Common::String CloudManager::getStorageUsername(uint32 index) {
if (index >= _storages.size()) return "";
- return _storages[index]->name(); //TODO
+ return _storages[index].username;
}
uint64 CloudManager::getStorageUsedSpace(uint32 index) {
if (index >= _storages.size()) return 0;
- return 0; //return _storages[index]->usedSpace(); //TODO
+ return _storages[index].usedBytes;
}
Common::String CloudManager::getStorageLastSync(uint32 index) {
if (index >= _storages.size()) return "";
- if (_storages[index]->isSyncing()) return "";
- return _storages[index]->name(); //->lastSyncDate(); //TODO
+ if (index == _currentStorageIndex && isSyncing()) return "";
+ return _storages[index].lastSyncDate;
}
void CloudManager::printBool(Storage::BoolResponse response) const {
diff --git a/backends/cloud/cloudmanager.h b/backends/cloud/cloudmanager.h
index 9956a92824..5e26ece088 100644
--- a/backends/cloud/cloudmanager.h
+++ b/backends/cloud/cloudmanager.h
@@ -36,12 +36,33 @@ class CommandReceiver;
namespace Cloud {
+//that's actual indexes in CloudManager's array
+enum StorageIDs {
+ kStorageNoneId = 0,
+ kStorageDropboxId = 1,
+ kStorageOneDriveId = 2,
+ kStorageGoogleDriveId = 3,
+
+ kStorageTotal
+};
+
class CloudManager : public Common::Singleton<CloudManager> {
- Common::Array<Cloud::Storage *> _storages;
- uint _currentStorageIndex;
+ struct StorageConfig {
+ Common::String name, username;
+ uint64 usedBytes;
+ Common::String lastSyncDate;
+ };
+
+ Common::Array<StorageConfig> _storages;
+ uint _currentStorageIndex;
+ Storage *_activeStorage;
void printBool(Cloud::Storage::BoolResponse response) const;
+ void loadStorage();
+
+ Common::String getStorageConfigName(uint32 index) const;
+
public:
CloudManager();
virtual ~CloudManager();
@@ -59,13 +80,13 @@ public:
void save();
/**
- * Adds new Storage into list.
+ * Replace active Storage.
+ * @note this method automatically saves the changes with ConfMan.
*
- * @param storage Cloud::Storage to add.
- * @param makeCurrent whether added storage should be the new current storage.
- * @param saveConfig whether save() should be called to update configuration file.
+ * @param storage Cloud::Storage to replace active storage with.
+ * @param index one of Cloud::StorageIDs enum values to indicate what storage type is replaced.
*/
- void addStorage(Cloud::Storage *storage, bool makeCurrent = true, bool saveConfig = true);
+ void replaceStorage(Storage *storage, uint32 index);
/**
* Returns active Storage, which could be used to interact
@@ -76,13 +97,6 @@ public:
Cloud::Storage *getCurrentStorage() const;
/**
- * Return active Storage's name.
- *
- * @return active Storage's or _("No active storage"), if there is no active Storage.
- */
- Common::String getStorageName() const;
-
- /**
* Return active Storage's index.
*
* @return active Storage's index.
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp
index d11d97da39..af73138a4f 100644
--- a/backends/cloud/dropbox/dropboxstorage.cpp
+++ b/backends/cloud/dropbox/dropboxstorage.cpp
@@ -61,11 +61,11 @@ static void saveAccessTokenCallback(Networking::JsonResponse pair) {
warning("Bad response, no token/uid passed");
} else {
//we suppose that's the first storage
- ConfMan.set("storages_number", "1", "cloud");
+ //TODO: update it to use CloudMan.replaceStorage()
ConfMan.set("current_storage", "1", "cloud");
- ConfMan.set("storage1_type", "Dropbox", "cloud");
- ConfMan.set("storage1_access_token", result.getVal("access_token")->asString(), "cloud");
- ConfMan.set("storage1_user_id", result.getVal("uid")->asString(), "cloud");
+ ConfMan.set("storage_Dropbox_type", "Dropbox", "cloud");
+ ConfMan.set("storage_Dropbox_access_token", result.getVal("access_token")->asString(), "cloud");
+ ConfMan.set("storage_Dropbox_user_id", result.getVal("uid")->asString(), "cloud");
ConfMan.removeKey("dropbox_code", "cloud");
ConfMan.flushToDisk();
debug("Now please restart ScummVM to apply the changes.");
@@ -85,8 +85,7 @@ DropboxStorage::~DropboxStorage() {
curl_global_cleanup();
}
-void DropboxStorage::saveConfig(Common::String keyPrefix) {
- ConfMan.set(keyPrefix + "type", "Dropbox", "cloud");
+void DropboxStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
}
diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp
index 30ca1be7e6..18ddca5e94 100644
--- a/backends/cloud/googledrive/googledrivestorage.cpp
+++ b/backends/cloud/googledrive/googledrivestorage.cpp
@@ -121,15 +121,14 @@ void GoogleDriveStorage::codeFlowComplete(BoolResponse response) {
return;
}
- ConfMan.removeKey("googledrive_code", "cloud");
- CloudMan.addStorage(this);
+ ConfMan.removeKey("googledrive_code", "cloud");
+ CloudMan.replaceStorage(this, kStorageGoogleDriveId);
ConfMan.flushToDisk();
debug("Done! You can use Google Drive now! Look:");
CloudMan.testFeature();
}
-void GoogleDriveStorage::saveConfig(Common::String keyPrefix) {
- ConfMan.set(keyPrefix + "type", "Google Drive", "cloud");
+void GoogleDriveStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "refresh_token", _refreshToken, "cloud");
}
diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp
index c391065396..d73bcdbe34 100644
--- a/backends/cloud/onedrive/onedrivestorage.cpp
+++ b/backends/cloud/onedrive/onedrivestorage.cpp
@@ -115,14 +115,14 @@ void OneDriveStorage::codeFlowComplete(BoolResponse response) {
return;
}
- CloudMan.addStorage(this);
ConfMan.removeKey("onedrive_code", "cloud");
+ CloudMan.replaceStorage(this, kStorageOneDriveId);
+ ConfMan.flushToDisk();
debug("Done! You can use OneDrive now! Look:");
CloudMan.syncSaves();
}
-void OneDriveStorage::saveConfig(Common::String keyPrefix) {
- ConfMan.set(keyPrefix + "type", "OneDrive", "cloud");
+void OneDriveStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
ConfMan.set(keyPrefix + "refresh_token", _refreshToken, "cloud");
diff --git a/gui/options.cpp b/gui/options.cpp
index b0a7968b2d..2febf84f9f 100644
--- a/gui/options.cpp
+++ b/gui/options.cpp
@@ -1271,16 +1271,18 @@ GlobalOptionsDialog::GlobalOptionsDialog()
else
tab->addTab(_c("Cloud", "lowres"));
+ _selectedStorageIndex = CloudMan.getStorageIndex();
+
new ButtonWidget(tab, "GlobalOptions_Cloud.StorageButton", _("Storage:"), 0, kChooseStorageCmd);
- _curStorage = new StaticTextWidget(tab, "GlobalOptions_Cloud.CurStorage", CloudMan.getStorageName());
+ _curStorage = new StaticTextWidget(tab, "GlobalOptions_Cloud.CurStorage", CloudMan.listStorages()[_selectedStorageIndex]);
- new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsernameDesc", _("Username:"), _("Username used by this storage"));
+ _storageUsernameDesc = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsernameDesc", _("Username:"), _("Username used by this storage"));
_storageUsername = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsernameLabel", "<none>");
- new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsedSpaceDesc", _("Used space:"), _("Space used by ScummVM on this storage"));
+ _storageUsedSpaceDesc = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsedSpaceDesc", _("Used space:"), _("Space used by ScummVM on this storage"));
_storageUsedSpace = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageUsedSpaceLabel", "0 bytes");
- new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageLastSyncDesc", _("Last sync time:"), _("When this storage did last saves sync"));
+ _storageLastSyncDesc = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageLastSyncDesc", _("Last sync time:"), _("When this storage did last saves sync"));
_storageLastSync = new StaticTextWidget(tab, "GlobalOptions_Cloud.StorageLastSyncLabel", "<never>");
setupCloudTab();
@@ -1437,6 +1439,21 @@ void GlobalOptionsDialog::close() {
}
#endif
+#ifdef USE_CLOUD
+ if (CloudMan.getStorageIndex() != _selectedStorageIndex) {
+ if (!CloudMan.switchStorage(_selectedStorageIndex)) {
+ bool anotherStorageIsWorking = CloudMan.isWorking();
+ Common::String message = _("Failed to change cloud storage!");
+ if (anotherStorageIsWorking) {
+ message += "\n";
+ message += _("Current cloud storage is working at the moment.");
+ }
+ MessageDialog dialog(message);
+ dialog.runModal();
+ }
+ }
+#endif
+
}
OptionsDialog::close();
}
@@ -1555,23 +1572,8 @@ void GlobalOptionsDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3
StorageBrowser storageBrowser;
if (storageBrowser.runModal() > 0) {
// User made his choice...
- uint32 storageIndex = storageBrowser.getSelected();
- // FIXME: Actually, any changes (including the storage change?) should
- // only become active *after* the options dialog has closed.
- if (CloudMan.switchStorage(storageIndex)) {
- _curStorage->setLabel(CloudMan.getStorageName());
- //automatically saves in the config if switched successfully
- setupCloudTab();
- } else {
- bool anotherStorageIsWorking = CloudMan.isWorking();
- Common::String message = _("Failed to change cloud storage!");
- if (anotherStorageIsWorking) {
- message += "\n";
- message += _("Current cloud storage is working at the moment.");
- }
- MessageDialog dialog(message);
- dialog.runModal();
- }
+ _selectedStorageIndex = storageBrowser.getSelected();
+ setupCloudTab();
draw();
}
break;
@@ -1635,23 +1637,32 @@ void GlobalOptionsDialog::reflowLayout() {
#ifdef USE_CLOUD
void GlobalOptionsDialog::setupCloudTab() {
- uint32 index = CloudMan.getStorageIndex();
+ if (_curStorage)
+ _curStorage->setLabel(CloudMan.listStorages()[_selectedStorageIndex]);
+
+ bool shown = (_selectedStorageIndex != Cloud::kStorageNoneId);
+ if (_storageUsernameDesc) _storageUsernameDesc->setVisible(shown);
if (_storageUsername) {
- Common::String username = CloudMan.getStorageUsername(index);
+ Common::String username = CloudMan.getStorageUsername(_selectedStorageIndex);
if (username == "") username = _("<none>");
_storageUsername->setLabel(username);
+ _storageUsername->setVisible(shown);
}
+ if (_storageUsedSpaceDesc) _storageUsedSpaceDesc->setVisible(shown);
if (_storageUsedSpace) {
- uint64 usedSpace = CloudMan.getStorageUsedSpace(index);
+ uint64 usedSpace = CloudMan.getStorageUsedSpace(_selectedStorageIndex);
_storageUsedSpace->setLabel(Common::String::format(_("%llu bytes"), usedSpace));
+ _storageUsedSpace->setVisible(shown);
}
+ if (_storageLastSyncDesc) _storageLastSyncDesc->setVisible(shown);
if (_storageLastSync) {
- Common::String sync = CloudMan.getStorageLastSync(index);
+ Common::String sync = CloudMan.getStorageLastSync(_selectedStorageIndex);
if (sync == "") {
- if (CloudMan.isSyncing()) sync = _("<right now>");
+ if (_selectedStorageIndex == CloudMan.getStorageIndex() && CloudMan.isSyncing()) sync = _("<right now>");
else sync = _("<never>");
}
_storageLastSync->setLabel(sync);
+ _storageLastSync->setVisible(shown);
}
}
#endif
diff --git a/gui/options.h b/gui/options.h
index 228a415b92..16ea424b6f 100644
--- a/gui/options.h
+++ b/gui/options.h
@@ -246,9 +246,13 @@ protected:
//
// Cloud controls
//
+ uint32 _selectedStorageIndex;
StaticTextWidget *_curStorage;
+ StaticTextWidget *_storageUsernameDesc;
StaticTextWidget *_storageUsername;
+ StaticTextWidget *_storageUsedSpaceDesc;
StaticTextWidget *_storageUsedSpace;
+ StaticTextWidget *_storageLastSyncDesc;
StaticTextWidget *_storageLastSync;
void setupCloudTab();