2016-05-11 01:10:37 +06: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.
|
|
|
|
*
|
|
|
|
* 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-06-01 16:22:42 +06:00
|
|
|
#include "backends/cloud/cloudmanager.h"
|
2016-05-11 20:24:53 +06:00
|
|
|
#include "backends/cloud/dropbox/dropboxstorage.h"
|
2016-05-29 20:12:22 +06:00
|
|
|
#include "backends/cloud/onedrive/onedrivestorage.h"
|
2016-06-03 15:14:12 +06:00
|
|
|
#include "backends/cloud/googledrive/googledrivestorage.h"
|
2016-05-18 14:08:05 +06:00
|
|
|
#include "common/config-manager.h"
|
2016-05-31 20:54:41 +06:00
|
|
|
#include "common/debug.h"
|
2016-06-08 18:51:00 +06:00
|
|
|
#include "common/translation.h"
|
2016-05-11 01:10:37 +06:00
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
DECLARE_SINGLETON(Cloud::CloudManager);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-11 20:24:53 +06:00
|
|
|
namespace Cloud {
|
2016-05-11 15:15:23 +06:00
|
|
|
|
2016-06-09 13:49:52 +06:00
|
|
|
CloudManager::CloudManager() : _currentStorageIndex(0), _activeStorage(nullptr) {}
|
2016-05-11 15:15:23 +06:00
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
CloudManager::~CloudManager() {
|
2016-05-23 11:23:33 +06:00
|
|
|
//TODO: do we have to save storages on manager destruction?
|
2016-06-09 13:49:52 +06:00
|
|
|
delete _activeStorage;
|
2016-05-23 11:23:33 +06:00
|
|
|
}
|
2016-05-11 15:15:23 +06:00
|
|
|
|
2016-06-09 13:49:52 +06:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
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;
|
2016-05-18 14:08:05 +06:00
|
|
|
}
|
2016-06-09 13:49:52 +06:00
|
|
|
|
|
|
|
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);
|
2016-05-18 14:08:05 +06:00
|
|
|
}
|
2016-06-09 13:49:52 +06:00
|
|
|
|
|
|
|
//load an active storage if there is any
|
|
|
|
_currentStorageIndex = kStorageNoneId;
|
|
|
|
if (ConfMan.hasKey("current_storage", "cloud"))
|
|
|
|
_currentStorageIndex = ConfMan.getInt("current_storage", "cloud");
|
|
|
|
|
|
|
|
loadStorage();
|
2016-05-18 14:08:05 +06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
void CloudManager::save() {
|
2016-06-09 13:49:52 +06:00
|
|
|
ConfMan.set("current_storage", Common::String::format("%d", _currentStorageIndex), "cloud");
|
|
|
|
if (_activeStorage)
|
|
|
|
_activeStorage->saveConfig("storage_" + getStorageConfigName(_currentStorageIndex) + "_");
|
2016-05-23 11:23:33 +06:00
|
|
|
ConfMan.flushToDisk();
|
|
|
|
}
|
|
|
|
|
2016-06-09 13:49:52 +06:00
|
|
|
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();
|
2016-05-24 16:19:22 +06:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:51:00 +06:00
|
|
|
Storage *CloudManager::getCurrentStorage() const {
|
2016-06-09 13:49:52 +06:00
|
|
|
return _activeStorage;
|
2016-06-08 18:51:00 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CloudManager::getStorageIndex() const {
|
|
|
|
return _currentStorageIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::StringArray CloudManager::listStorages() const {
|
|
|
|
Common::StringArray result;
|
|
|
|
for (uint32 i = 0; i < _storages.size(); ++i) {
|
2016-06-09 13:49:52 +06:00
|
|
|
result.push_back(_storages[i].name);
|
2016-06-08 18:51:00 +06:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudManager::switchStorage(uint32 index) {
|
2016-06-09 12:41:51 +06:00
|
|
|
if (index >= _storages.size()) {
|
2016-06-08 18:51:00 +06:00
|
|
|
warning("CloudManager::switchStorage: invalid index passed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage && storage->isWorking()) {
|
|
|
|
warning("CloudManager::switchStorage: another storage is working now");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_currentStorageIndex = index;
|
2016-06-09 13:49:52 +06:00
|
|
|
loadStorage();
|
2016-06-08 18:51:00 +06:00
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:41:51 +06:00
|
|
|
Common::String CloudManager::getStorageUsername(uint32 index) {
|
|
|
|
if (index >= _storages.size()) return "";
|
2016-06-09 13:49:52 +06:00
|
|
|
return _storages[index].username;
|
2016-06-09 12:41:51 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CloudManager::getStorageUsedSpace(uint32 index) {
|
|
|
|
if (index >= _storages.size()) return 0;
|
2016-06-09 13:49:52 +06:00
|
|
|
return _storages[index].usedBytes;
|
2016-06-09 12:41:51 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::String CloudManager::getStorageLastSync(uint32 index) {
|
|
|
|
if (index >= _storages.size()) return "";
|
2016-06-09 13:49:52 +06:00
|
|
|
if (index == _currentStorageIndex && isSyncing()) return "";
|
|
|
|
return _storages[index].lastSyncDate;
|
2016-06-09 12:41:51 +06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
void CloudManager::printBool(Storage::BoolResponse response) const {
|
2016-05-31 20:54:41 +06:00
|
|
|
debug("bool = %s", (response.value ? "true" : "false"));
|
|
|
|
}
|
|
|
|
|
2016-06-05 15:22:26 +06:00
|
|
|
SavesSyncRequest *CloudManager::syncSaves(Storage::BoolCallback callback, Networking::ErrorCallback errorCallback) {
|
2016-05-23 11:23:33 +06:00
|
|
|
Storage *storage = getCurrentStorage();
|
2016-06-05 15:22:26 +06:00
|
|
|
if (storage) return storage->syncSaves(callback, errorCallback);
|
|
|
|
return nullptr;
|
2016-05-11 20:24:53 +06:00
|
|
|
}
|
2016-05-11 01:10:37 +06:00
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
void CloudManager::testFeature() {
|
2016-05-31 20:54:41 +06:00
|
|
|
Storage *storage = getCurrentStorage();
|
2016-06-06 20:04:13 +06:00
|
|
|
//if (storage) storage->info(nullptr, nullptr);
|
|
|
|
GoogleDrive::GoogleDriveStorage *gd = dynamic_cast<GoogleDrive::GoogleDriveStorage *>(storage);
|
2016-06-06 21:24:53 +06:00
|
|
|
if (gd) {
|
|
|
|
}
|
|
|
|
//gd->resolveFileId("firstfolder/subfolder", nullptr, nullptr);
|
2016-06-06 20:04:13 +06:00
|
|
|
//gd->listDirectoryById("appDataFolder", nullptr, nullptr);
|
|
|
|
//gd->listDirectoryById("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", nullptr, nullptr);
|
|
|
|
//gd->createDirectoryWithParentId("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", "subfolder", nullptr, nullptr);
|
|
|
|
//gd->createDirectoryWithParentId("appDataFolder", "firstfolder", nullptr, nullptr);
|
|
|
|
else debug("FAILURE");
|
2016-05-31 20:54:41 +06:00
|
|
|
}
|
|
|
|
|
2016-06-05 11:35:18 +06:00
|
|
|
bool CloudManager::isWorking() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) return storage->isWorking();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudManager::isSyncing() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) return storage->isSyncing();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-05 21:07:55 +06:00
|
|
|
double CloudManager::getSyncDownloadingProgress() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) return storage->getSyncDownloadingProgress();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-05 11:35:18 +06:00
|
|
|
double CloudManager::getSyncProgress() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) return storage->getSyncProgress();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Array<Common::String> CloudManager::getSyncingFiles() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) return storage->getSyncingFiles();
|
|
|
|
return Common::Array<Common::String>();
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:20:22 +06:00
|
|
|
void CloudManager::cancelSync() {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) storage->cancelSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::setSyncTarget(GUI::CommandReceiver *target) {
|
|
|
|
Storage *storage = getCurrentStorage();
|
|
|
|
if (storage) storage->setSyncTarget(target);
|
|
|
|
}
|
|
|
|
|
2016-06-01 16:22:42 +06:00
|
|
|
} // End of namespace Common
|