CLOUD: Introduce CloudConfigHelper

This commit is contained in:
Peter Bozsó 2016-06-12 23:02:32 +02:00 committed by Alexander Tkachev
parent bfc5cab9e8
commit 219e565c32
6 changed files with 166 additions and 59 deletions

View file

@ -0,0 +1,58 @@
/* 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.
*
*/
#include "backends/cloud/cloudconfighelper.h"
#include "common/config-manager.h"
namespace Common {
DECLARE_SINGLETON(Cloud::CloudConfigHelper);
}
namespace Cloud {
bool CloudConfigHelper::hasKey(const Common::String &key) const {
return ConfMan.hasKey(key, ConfMan.kCloudDomain);
}
void CloudConfigHelper::removeKey(const Common::String &key) {
ConfMan.removeKey(key, ConfMan.kCloudDomain);
}
const Common::String &CloudConfigHelper::get(const Common::String &key) const {
return ConfMan.get(key, ConfMan.kCloudDomain);
}
int CloudConfigHelper::getInt(const Common::String &key) const {
return ConfMan.getInt(key, ConfMan.kCloudDomain);
}
void CloudConfigHelper::set(const Common::String &key, const Common::String &value) {
ConfMan.set(key, value, ConfMan.kCloudDomain);
}
void CloudConfigHelper::flushToDisk() {
ConfMan.flushToDisk();
}
} // End of namespace Cloud

View file

@ -0,0 +1,49 @@
/* 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.
*
*/
#ifndef CLOUD_CLOUDCONFIGHELPER_H
#define CLOUD_CLOUDCONFIGHELPER_H
#include "common/singleton.h"
#include "common/str.h"
namespace Cloud {
/**
* Convenience wrapper around ConfMan for the cloud backend.
*/
class CloudConfigHelper : public Common::Singleton<CloudConfigHelper> {
public:
bool hasKey(const Common::String &key) const;
void removeKey(const Common::String &key);
const Common::String &get(const Common::String &key) const;
int getInt(const Common::String &key) const;
void set(const Common::String &key, const Common::String &value);
void flushToDisk();
};
/** Shortcut for accessing the cloud configuration helper. */
#define CloudConfig Cloud::CloudConfigHelper::instance()
} // End of namespace Cloud
#endif

View file

@ -24,9 +24,9 @@
#include "backends/cloud/dropbox/dropboxstorage.h"
#include "backends/cloud/onedrive/onedrivestorage.h"
#include "backends/cloud/googledrive/googledrivestorage.h"
#include "common/config-manager.h"
#include "common/debug.h"
#include "common/translation.h"
#include "backends/cloud/cloudconfighelper.h"
namespace Common {
@ -85,19 +85,19 @@ void CloudManager::init() {
config.username = "";
config.lastSyncDate = "";
config.usedBytes = 0;
if (ConfMan.hasKey(kStoragePrefix + name + "_username", "cloud"))
config.username = ConfMan.get(kStoragePrefix + name + "_username", "cloud");
if (ConfMan.hasKey(kStoragePrefix + name + "_lastSync", "cloud"))
config.lastSyncDate = ConfMan.get(kStoragePrefix + name + "_lastSync", "cloud");
if (ConfMan.hasKey(kStoragePrefix + name + "_usedBytes", "cloud"))
config.usedBytes = ConfMan.get(kStoragePrefix + name + "_usedBytes", "cloud").asUint64();
if (CloudConfig.hasKey(kStoragePrefix + name + "_username"))
config.username = CloudConfig.get(kStoragePrefix + name + "_username");
if (CloudConfig.hasKey(kStoragePrefix + name + "_lastSync"))
config.lastSyncDate = CloudConfig.get(kStoragePrefix + name + "_lastSync");
if (CloudConfig.hasKey(kStoragePrefix + name + "_usedBytes"))
config.usedBytes = CloudConfig.get(kStoragePrefix + name + "_usedBytes").asUint64();
_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");
if (CloudConfig.hasKey("current_storage"))
_currentStorageIndex = CloudConfig.getInt("current_storage");
loadStorage();
}
@ -106,15 +106,15 @@ void CloudManager::save() {
for (uint32 i = 0; i < _storages.size(); ++i) {
if (i == kStorageNoneId) continue;
Common::String name = getStorageConfigName(i);
ConfMan.set(kStoragePrefix + name + "_username", _storages[i].username, "cloud");
ConfMan.set(kStoragePrefix + name + "_lastSync", _storages[i].lastSyncDate, "cloud");
ConfMan.set(kStoragePrefix + name + "_usedBytes", Common::String::format("%llu", _storages[i].usedBytes), "cloud");
CloudConfig.set(kStoragePrefix + name + "_username", _storages[i].username);
CloudConfig.set(kStoragePrefix + name + "_lastSync", _storages[i].lastSyncDate);
CloudConfig.set(kStoragePrefix + name + "_usedBytes", Common::String::format("%llu", _storages[i].usedBytes));
}
ConfMan.set("current_storage", Common::String::format("%d", _currentStorageIndex), "cloud");
CloudConfig.set("current_storage", Common::String::format("%d", _currentStorageIndex));
if (_activeStorage)
_activeStorage->saveConfig(kStoragePrefix + getStorageConfigName(_currentStorageIndex) + "_");
ConfMan.flushToDisk();
CloudConfig.flushToDisk();
}
void CloudManager::replaceStorage(Storage *storage, uint32 index) {

View file

@ -28,24 +28,24 @@
#include "backends/cloud/cloudmanager.h"
#include "backends/networking/curl/connectionmanager.h"
#include "backends/networking/curl/curljsonrequest.h"
#include "common/config-manager.h"
#include "common/debug.h"
#include "common/json.h"
#include <curl/curl.h>
#include "backends/cloud/cloudconfighelper.h"
namespace Cloud {
namespace Dropbox {
char *DropboxStorage::KEY = nullptr; //can't use ConfMan there yet, loading it on instance creation/auth
char *DropboxStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth
char *DropboxStorage::SECRET = nullptr; //TODO: hide these secrets somehow
void DropboxStorage::loadKeyAndSecret() {
Common::String k = ConfMan.get("DROPBOX_KEY", "cloud");
Common::String k = CloudConfig.get("DROPBOX_KEY");
KEY = new char[k.size() + 1];
memcpy(KEY, k.c_str(), k.size());
KEY[k.size()] = 0;
k = ConfMan.get("DROPBOX_SECRET", "cloud");
k = CloudConfig.get("DROPBOX_SECRET");
SECRET = new char[k.size() + 1];
memcpy(SECRET, k.c_str(), k.size());
SECRET[k.size()] = 0;
@ -80,9 +80,9 @@ void DropboxStorage::codeFlowComplete(Networking::JsonResponse response) {
} else {
_token = result.getVal("access_token")->asString();
_uid = result.getVal("uid")->asString();
ConfMan.removeKey("dropbox_code", "cloud");
CloudConfig.removeKey("dropbox_code");
CloudMan.replaceStorage(this, kStorageDropboxId);
ConfMan.flushToDisk();
CloudConfig.flushToDisk();
debug("Done! You can use Dropbox now! Look:");
CloudMan.testFeature();
}
@ -94,8 +94,8 @@ void DropboxStorage::codeFlowComplete(Networking::JsonResponse response) {
}
void DropboxStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
CloudConfig.set(keyPrefix + "access_token", _token);
CloudConfig.set(keyPrefix + "user_id", _uid);
}
Common::String DropboxStorage::name() const {
@ -199,18 +199,18 @@ void DropboxStorage::infoMethodCallback(StorageInfoResponse response) {
DropboxStorage *DropboxStorage::loadFromConfig(Common::String keyPrefix) {
loadKeyAndSecret();
if (!ConfMan.hasKey(keyPrefix + "access_token", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "access_token")) {
warning("No access_token found");
return 0;
}
if (!ConfMan.hasKey(keyPrefix + "user_id", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "user_id")) {
warning("No user_id found");
return 0;
}
Common::String accessToken = ConfMan.get(keyPrefix + "access_token", "cloud");
Common::String userId = ConfMan.get(keyPrefix + "user_id", "cloud");
Common::String accessToken = CloudConfig.get(keyPrefix + "access_token");
Common::String userId = CloudConfig.get(keyPrefix + "user_id");
return new DropboxStorage(accessToken, userId);
}

View file

@ -27,7 +27,6 @@
#include "backends/networking/curl/connectionmanager.h"
#include "backends/networking/curl/curljsonrequest.h"
#include "backends/networking/curl/networkreadstream.h"
#include "common/config-manager.h"
#include "common/debug.h"
#include "common/json.h"
#include <curl/curl.h>
@ -38,20 +37,21 @@
#include "googledrivestreamfilerequest.h"
#include "googledrivedownloadrequest.h"
#include "googledriveuploadrequest.h"
#include "backends/cloud/cloudconfighelper.h"
namespace Cloud {
namespace GoogleDrive {
char *GoogleDriveStorage::KEY = nullptr; //can't use ConfMan there yet, loading it on instance creation/auth
char *GoogleDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth
char *GoogleDriveStorage::SECRET = nullptr; //TODO: hide these secrets somehow
void GoogleDriveStorage::loadKeyAndSecret() {
Common::String k = ConfMan.get("GOOGLE_DRIVE_KEY", "cloud");
Common::String k = CloudConfig.get("GOOGLE_DRIVE_KEY");
KEY = new char[k.size() + 1];
memcpy(KEY, k.c_str(), k.size());
KEY[k.size()] = 0;
k = ConfMan.get("GOOGLE_DRIVE_SECRET", "cloud");
k = CloudConfig.get("GOOGLE_DRIVE_SECRET");
SECRET = new char[k.size() + 1];
memcpy(SECRET, k.c_str(), k.size());
SECRET[k.size()] = 0;
@ -122,16 +122,16 @@ void GoogleDriveStorage::codeFlowComplete(BoolResponse response) {
return;
}
ConfMan.removeKey("googledrive_code", "cloud");
CloudConfig.removeKey("googledrive_code");
CloudMan.replaceStorage(this, kStorageGoogleDriveId);
ConfMan.flushToDisk();
CloudConfig.flushToDisk();
debug("Done! You can use Google Drive now! Look:");
CloudMan.testFeature();
}
void GoogleDriveStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
ConfMan.set(keyPrefix + "refresh_token", _refreshToken, "cloud");
CloudConfig.set(keyPrefix + "access_token", _token);
CloudConfig.set(keyPrefix + "refresh_token", _refreshToken);
}
Common::String GoogleDriveStorage::name() const {
@ -339,18 +339,18 @@ Common::String GoogleDriveStorage::savesDirectoryPath() { return "scummvm/saves/
GoogleDriveStorage *GoogleDriveStorage::loadFromConfig(Common::String keyPrefix) {
loadKeyAndSecret();
if (!ConfMan.hasKey(keyPrefix + "access_token", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "access_token")) {
warning("No access_token found");
return 0;
}
if (!ConfMan.hasKey(keyPrefix + "refresh_token", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "refresh_token")) {
warning("No refresh_token found");
return 0;
}
Common::String accessToken = ConfMan.get(keyPrefix + "access_token", "cloud");
Common::String refreshToken = ConfMan.get(keyPrefix + "refresh_token", "cloud");
Common::String accessToken = CloudConfig.get(keyPrefix + "access_token");
Common::String refreshToken = CloudConfig.get(keyPrefix + "refresh_token");
return new GoogleDriveStorage(accessToken, refreshToken);
}
@ -365,16 +365,16 @@ Common::String GoogleDriveStorage::getAuthLink() {
}
void GoogleDriveStorage::authThroughConsole() {
if (!ConfMan.hasKey("GOOGLE_DRIVE_KEY", "cloud") || !ConfMan.hasKey("GOOGLE_DRIVE_SECRET", "cloud")) {
if (!CloudConfig.hasKey("GOOGLE_DRIVE_KEY") || !CloudConfig.hasKey("GOOGLE_DRIVE_SECRET")) {
warning("No Google Drive keys available, cannot do auth");
return;
}
loadKeyAndSecret();
if (ConfMan.hasKey("googledrive_code", "cloud")) {
if (CloudConfig.hasKey("googledrive_code")) {
//phase 2: get access_token using specified code
new GoogleDriveStorage(ConfMan.get("googledrive_code", "cloud"));
new GoogleDriveStorage(CloudConfig.get("googledrive_code"));
return;
}

View file

@ -30,24 +30,24 @@
#include "backends/networking/curl/connectionmanager.h"
#include "backends/networking/curl/curljsonrequest.h"
#include "backends/networking/curl/networkreadstream.h"
#include "common/config-manager.h"
#include "common/debug.h"
#include "common/json.h"
#include <curl/curl.h>
#include "backends/cloud/cloudconfighelper.h"
namespace Cloud {
namespace OneDrive {
char *OneDriveStorage::KEY = nullptr; //can't use ConfMan there yet, loading it on instance creation/auth
char *OneDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth
char *OneDriveStorage::SECRET = nullptr; //TODO: hide these secrets somehow
void OneDriveStorage::loadKeyAndSecret() {
Common::String k = ConfMan.get("ONEDRIVE_KEY", "cloud");
Common::String k = CloudConfig.get("ONEDRIVE_KEY");
KEY = new char[k.size() + 1];
memcpy(KEY, k.c_str(), k.size());
KEY[k.size()] = 0;
k = ConfMan.get("ONEDRIVE_SECRET", "cloud");
k = CloudConfig.get("ONEDRIVE_SECRET");
SECRET = new char[k.size() + 1];
memcpy(SECRET, k.c_str(), k.size());
SECRET[k.size()] = 0;
@ -116,17 +116,17 @@ void OneDriveStorage::codeFlowComplete(BoolResponse response) {
return;
}
ConfMan.removeKey("onedrive_code", "cloud");
CloudConfig.removeKey("onedrive_code");
CloudMan.replaceStorage(this, kStorageOneDriveId);
ConfMan.flushToDisk();
CloudConfig.flushToDisk();
debug("Done! You can use OneDrive now! Look:");
CloudMan.syncSaves();
}
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");
CloudConfig.set(keyPrefix + "access_token", _token);
CloudConfig.set(keyPrefix + "user_id", _uid);
CloudConfig.set(keyPrefix + "refresh_token", _refreshToken);
}
Common::String OneDriveStorage::name() const {
@ -262,24 +262,24 @@ Common::String OneDriveStorage::savesDirectoryPath() { return "saves/"; }
OneDriveStorage *OneDriveStorage::loadFromConfig(Common::String keyPrefix) {
loadKeyAndSecret();
if (!ConfMan.hasKey(keyPrefix + "access_token", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "access_token")) {
warning("No access_token found");
return 0;
}
if (!ConfMan.hasKey(keyPrefix + "user_id", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "user_id")) {
warning("No user_id found");
return 0;
}
if (!ConfMan.hasKey(keyPrefix + "refresh_token", "cloud")) {
if (!CloudConfig.hasKey(keyPrefix + "refresh_token")) {
warning("No refresh_token found");
return 0;
}
Common::String accessToken = ConfMan.get(keyPrefix + "access_token", "cloud");
Common::String userId = ConfMan.get(keyPrefix + "user_id", "cloud");
Common::String refreshToken = ConfMan.get(keyPrefix + "refresh_token", "cloud");
Common::String accessToken = CloudConfig.get(keyPrefix + "access_token");
Common::String userId = CloudConfig.get(keyPrefix + "user_id");
Common::String refreshToken = CloudConfig.get(keyPrefix + "refresh_token");
return new OneDriveStorage(accessToken, userId, refreshToken);
}
@ -294,16 +294,16 @@ Common::String OneDriveStorage::getAuthLink() {
}
void OneDriveStorage::authThroughConsole() {
if (!ConfMan.hasKey("ONEDRIVE_KEY", "cloud") || !ConfMan.hasKey("ONEDRIVE_SECRET", "cloud")) {
if (!CloudConfig.hasKey("ONEDRIVE_KEY") || !CloudConfig.hasKey("ONEDRIVE_SECRET")) {
warning("No OneDrive keys available, cannot do auth");
return;
}
loadKeyAndSecret();
if (ConfMan.hasKey("onedrive_code", "cloud")) {
if (CloudConfig.hasKey("onedrive_code")) {
//phase 2: get access_token using specified code
new OneDriveStorage(ConfMan.get("onedrive_code", "cloud"));
new OneDriveStorage(CloudConfig.get("onedrive_code"));
return;
}