CLOUD: Add Storage saving mechanism
In this commit CloudManager starts supporting multiple Storage. Now, in its init() it loads all the Storages and determines the current one. It now also has save() method. In that method all Storages are saved with their new saveConfig() method. CloudManager::save() not called from anywhere, though. The only one Storage that could be added is DropboxStorage in case you have no cloud-related config keys or you have no storages connected.
This commit is contained in:
parent
c4b1fdc727
commit
41e65db7d0
6 changed files with 112 additions and 24 deletions
|
@ -45,9 +45,12 @@ static void saveAccessTokenCallback(void *ptr) {
|
||||||
if (!result.contains("access_token") || !result.contains("uid")) {
|
if (!result.contains("access_token") || !result.contains("uid")) {
|
||||||
warning("Bad response, no token/uid passed");
|
warning("Bad response, no token/uid passed");
|
||||||
} else {
|
} else {
|
||||||
ConfMan.set("current_storage_type", "Dropbox", "cloud");
|
//we suppose that's the first storage
|
||||||
ConfMan.set("current_storage_access_token", result.getVal("access_token")->asString(), "cloud");
|
ConfMan.set("storages_number", "1", "cloud");
|
||||||
ConfMan.set("current_storage_user_id", result.getVal("uid")->asString(), "cloud");
|
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.removeKey("dropbox_code", "cloud");
|
ConfMan.removeKey("dropbox_code", "cloud");
|
||||||
debug("Now please restart ScummVM to apply the changes.");
|
debug("Now please restart ScummVM to apply the changes.");
|
||||||
}
|
}
|
||||||
|
@ -66,6 +69,12 @@ DropboxStorage::~DropboxStorage() {
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DropboxStorage::saveConfig(Common::String keyPrefix) {
|
||||||
|
ConfMan.set(keyPrefix + "type", "Dropbox", "cloud");
|
||||||
|
ConfMan.set(keyPrefix + "access_token", _token, "cloud");
|
||||||
|
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
|
||||||
|
}
|
||||||
|
|
||||||
void DropboxStorage::syncSaves(BoolCallback callback) {
|
void DropboxStorage::syncSaves(BoolCallback callback) {
|
||||||
//this is not the real syncSaves() implementation
|
//this is not the real syncSaves() implementation
|
||||||
info(new Common::Callback<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoMethodCallback));
|
info(new Common::Callback<DropboxStorage, StorageInfo>(this, &DropboxStorage::infoMethodCallback));
|
||||||
|
@ -106,22 +115,22 @@ void DropboxStorage::infoMethodCallback(StorageInfo storageInfo) {
|
||||||
debug("info: %s", storageInfo.info().c_str());
|
debug("info: %s", storageInfo.info().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
DropboxStorage *DropboxStorage::loadFromConfig() {
|
DropboxStorage *DropboxStorage::loadFromConfig(Common::String keyPrefix) {
|
||||||
KEY = ConfMan.get("DROPBOX_KEY", "cloud");
|
KEY = ConfMan.get("DROPBOX_KEY", "cloud");
|
||||||
SECRET = ConfMan.get("DROPBOX_SECRET", "cloud");
|
SECRET = ConfMan.get("DROPBOX_SECRET", "cloud");
|
||||||
|
|
||||||
if (!ConfMan.hasKey("current_storage_access_token", "cloud")) {
|
if (!ConfMan.hasKey(keyPrefix + "access_token", "cloud")) {
|
||||||
warning("No access_token found");
|
warning("No access_token found");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ConfMan.hasKey("current_storage_user_id", "cloud")) {
|
if (!ConfMan.hasKey(keyPrefix + "user_id", "cloud")) {
|
||||||
warning("No user_id found");
|
warning("No user_id found");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::String accessToken = ConfMan.get("current_storage_access_token", "cloud");
|
Common::String accessToken = ConfMan.get(keyPrefix + "access_token", "cloud");
|
||||||
Common::String userId = ConfMan.get("current_storage_user_id", "cloud");
|
Common::String userId = ConfMan.get(keyPrefix + "user_id", "cloud");
|
||||||
return new DropboxStorage(accessToken, userId);
|
return new DropboxStorage(accessToken, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,23 @@ class DropboxStorage: public Cloud::Storage {
|
||||||
public:
|
public:
|
||||||
virtual ~DropboxStorage();
|
virtual ~DropboxStorage();
|
||||||
|
|
||||||
/** Returns pointer to Common::Array<StorageFile>. */
|
/**
|
||||||
|
* Storage methods, which are used by CloudManager to save
|
||||||
|
* storage in configuration file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save storage data using ConfMan.
|
||||||
|
* @param keyPrefix all saved keys must start with this prefix.
|
||||||
|
* @note every Storage must write keyPrefix + "type" key
|
||||||
|
* with common value (e.g. "Dropbox").
|
||||||
|
*/
|
||||||
|
|
||||||
|
virtual void saveConfig(Common::String keyPrefix);
|
||||||
|
|
||||||
|
/** Public Cloud API comes down there. */
|
||||||
|
|
||||||
|
/** Returns Common::Array<StorageFile>. */
|
||||||
virtual void listDirectory(Common::String path, FileArrayCallback callback) {} //TODO
|
virtual void listDirectory(Common::String path, FileArrayCallback callback) {} //TODO
|
||||||
|
|
||||||
/** Calls the callback when finished. */
|
/** Calls the callback when finished. */
|
||||||
|
@ -66,7 +82,7 @@ public:
|
||||||
/** Calls the callback when finished. */
|
/** Calls the callback when finished. */
|
||||||
virtual void touch(Common::String path, BoolCallback callback) {} //TODO
|
virtual void touch(Common::String path, BoolCallback callback) {} //TODO
|
||||||
|
|
||||||
/** Returns pointer to the StorageInfo struct. */
|
/** Returns the StorageInfo struct. */
|
||||||
virtual void info(StorageInfoCallback callback);
|
virtual void info(StorageInfoCallback callback);
|
||||||
|
|
||||||
/** This method is passed into info(). (Temporary) */
|
/** This method is passed into info(). (Temporary) */
|
||||||
|
@ -82,7 +98,7 @@ public:
|
||||||
* Load token and user id from configs and return DropboxStorage for those.
|
* Load token and user id from configs and return DropboxStorage for those.
|
||||||
* @return pointer to the newly created DropboxStorage or 0 if some problem occured.
|
* @return pointer to the newly created DropboxStorage or 0 if some problem occured.
|
||||||
*/
|
*/
|
||||||
static DropboxStorage *loadFromConfig();
|
static DropboxStorage *loadFromConfig(Common::String keyPrefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns Dropbox auth link.
|
* Returns Dropbox auth link.
|
||||||
|
|
|
@ -26,28 +26,67 @@
|
||||||
|
|
||||||
namespace Cloud {
|
namespace Cloud {
|
||||||
|
|
||||||
Manager::Manager(): _currentStorage(0) {}
|
Manager::Manager(): _currentStorageIndex(0) {}
|
||||||
|
|
||||||
Manager::~Manager() { delete _currentStorage; }
|
Manager::~Manager() {
|
||||||
|
//TODO: do we have to save storages on manager destruction?
|
||||||
|
for (uint32 i = 0; i < _storages.size(); ++i)
|
||||||
|
delete _storages[i];
|
||||||
|
_storages.clear();
|
||||||
|
}
|
||||||
|
|
||||||
void Manager::init() {
|
void Manager::init() {
|
||||||
if (ConfMan.hasKey("current_storage_type", "cloud")) {
|
bool offerDropbox = false;
|
||||||
Common::String storageType = ConfMan.get("current_storage_type", "cloud");
|
|
||||||
if (storageType == "Dropbox") _currentStorage = Dropbox::DropboxStorage::loadFromConfig();
|
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 warning("Unknown cloud storage type '%s' passed", storageType.c_str());
|
else warning("Unknown cloud storage type '%s' passed", storageType.c_str());
|
||||||
|
} else {
|
||||||
|
warning("Cloud storage #%d (out of %d) is missing.", i, storages);
|
||||||
}
|
}
|
||||||
else {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offerDropbox) {
|
||||||
//this is temporary console offer to auth with Dropbox (because there is no other storage type yet anyway)
|
//this is temporary console offer to auth with Dropbox (because there is no other storage type yet anyway)
|
||||||
Dropbox::DropboxStorage::authThroughConsole();
|
Dropbox::DropboxStorage::authThroughConsole();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage* Manager::getCurrentStorage() {
|
void Manager::save() {
|
||||||
return _currentStorage;
|
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.flushToDisk();
|
||||||
|
}
|
||||||
|
|
||||||
|
Storage *Manager::getCurrentStorage() {
|
||||||
|
if (_currentStorageIndex < _storages.size())
|
||||||
|
return _storages[_currentStorageIndex];
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Manager::syncSaves(Storage::BoolCallback callback) {
|
void Manager::syncSaves(Storage::BoolCallback callback) {
|
||||||
Storage* storage = getCurrentStorage();
|
Storage *storage = getCurrentStorage();
|
||||||
if (storage) storage->syncSaves(callback);
|
if (storage) storage->syncSaves(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,15 +29,17 @@
|
||||||
namespace Cloud {
|
namespace Cloud {
|
||||||
|
|
||||||
class Manager: public Common::CloudManager {
|
class Manager: public Common::CloudManager {
|
||||||
Storage* _currentStorage;
|
Common::Array<Storage *> _storages;
|
||||||
|
uint _currentStorageIndex;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Manager();
|
Manager();
|
||||||
virtual ~Manager();
|
virtual ~Manager();
|
||||||
|
|
||||||
virtual void init();
|
virtual void init();
|
||||||
|
virtual void save();
|
||||||
|
|
||||||
virtual Storage* getCurrentStorage();
|
virtual Storage *getCurrentStorage();
|
||||||
virtual void syncSaves(Storage::BoolCallback callback);
|
virtual void syncSaves(Storage::BoolCallback callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,22 @@ public:
|
||||||
Storage() {}
|
Storage() {}
|
||||||
virtual ~Storage() {}
|
virtual ~Storage() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Storage methods, which are used by CloudManager to save
|
||||||
|
* storage in configuration file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save storage data using ConfMan.
|
||||||
|
* @param keyPrefix all saved keys must start with this prefix.
|
||||||
|
* @note every Storage must write keyPrefix + "type" key
|
||||||
|
* with common value (e.g. "Dropbox").
|
||||||
|
*/
|
||||||
|
|
||||||
|
virtual void saveConfig(Common::String keyPrefix) = 0;
|
||||||
|
|
||||||
|
/** Public Cloud API comes down there. */
|
||||||
|
|
||||||
/** Returns Common::Array<StorageFile>. */
|
/** Returns Common::Array<StorageFile>. */
|
||||||
virtual void listDirectory(Common::String path, FileArrayCallback callback) = 0;
|
virtual void listDirectory(Common::String path, FileArrayCallback callback) = 0;
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,12 @@ public:
|
||||||
|
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves all information into configuration file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
virtual void save() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns active Storage, which could be used to interact
|
* Returns active Storage, which could be used to interact
|
||||||
* with cloud storage.
|
* with cloud storage.
|
||||||
|
@ -47,7 +53,7 @@ public:
|
||||||
* @return active Cloud::Storage or null, if there is no active Storage.
|
* @return active Cloud::Storage or null, if there is no active Storage.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
virtual Cloud::Storage* getCurrentStorage() = 0;
|
virtual Cloud::Storage *getCurrentStorage() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts saves syncing process in currently active storage if there is any.
|
* Starts saves syncing process in currently active storage if there is any.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue