diff options
author | Alexander Tkachev | 2016-05-21 00:44:09 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 17eb5f91433f2414dc73f89abfdd316407259b61 (patch) | |
tree | 0d605093c094c475ceb87f9aa1e394325f93d899 /backends/cloud/dropbox | |
parent | f913675c43ada5c5f9128d904fd913129da35fe8 (diff) | |
download | scummvm-rg350-17eb5f91433f2414dc73f89abfdd316407259b61.tar.gz scummvm-rg350-17eb5f91433f2414dc73f89abfdd316407259b61.tar.bz2 scummvm-rg350-17eb5f91433f2414dc73f89abfdd316407259b61.zip |
CLOUD: Add complex callbacks
Originally, I intended to add Storage API, StorageFile and StorageInfo
stubs. When I tried to implement a simple info() call, I ended up fixing
Request to contain some pointer field and all callbacks to have Request*
parameter. And, now I have to place callback pointer into Request. which
calls another callback.
And, eventually, these "simple" callbacks would again require another
pointer (to some caller class).
Diffstat (limited to 'backends/cloud/dropbox')
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 42 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 33 |
2 files changed, 64 insertions, 11 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 93f0eebcf6..f2270f79cd 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -35,7 +35,7 @@ namespace Dropbox { Common::String DropboxStorage::KEY; //can't use ConfMan there yet, loading it on instance creation/auth Common::String DropboxStorage::SECRET; //TODO: hide these secrets somehow -static void printJsonCallback(void *ptr) { +static void printJsonCallback(Networking::Request* rq, void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; if (json) { debug("printJsonCallback:"); @@ -46,7 +46,7 @@ static void printJsonCallback(void *ptr) { } } -static void saveAccessTokenCallback(void *ptr) { +static void saveAccessTokenCallback(Networking::Request* rq, void *ptr) { Common::JSONValue *json = (Common::JSONValue *)ptr; if (json) { debug("saveAccessTokenCallback:"); @@ -69,6 +69,29 @@ static void saveAccessTokenCallback(void *ptr) { } } +void infoCallback(Networking::Request* request, void *jsonPointer) { + if (!request) { + warning("infoCallback: got NULL instead of Request"); + + Common::JSONValue *json = (Common::JSONValue *)jsonPointer; + if (json) delete json; //yeah I know we can delete NULL safely + return; + } + + Storage::InfoCallback callback = (Storage::InfoCallback)request->pointer(); + + Common::JSONValue *json = (Common::JSONValue *)jsonPointer; + if (json) { + //Common::JSONObject result = json->asObject(); + if (callback) { + callback(StorageInfo(json->stringify())); + } + delete json; + } else { + warning("infoCallback: got NULL instead of JSON!"); + } +} + DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) { curl_global_init(CURL_GLOBAL_ALL); } @@ -77,18 +100,21 @@ DropboxStorage::~DropboxStorage() { curl_global_cleanup(); } -void DropboxStorage::listDirectory(Common::String path) { +void syncSavesInfoCallback(StorageInfo info) { + debug("info: %s", info.info().c_str()); } -void DropboxStorage::syncSaves() { - //not syncing, but already something: - printInfo(); +void DropboxStorage::syncSaves(OperationCallback callback) { + //this is not the real syncSaves() implementation + info(syncSavesInfoCallback); } -void DropboxStorage::printInfo() { - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(printJsonCallback, "https://api.dropboxapi.com/1/account/info"); +void DropboxStorage::info(InfoCallback callback) { + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(infoCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); ConnMan.addRequest(request); + + request->setPointer(callback); } DropboxStorage *DropboxStorage::loadFromConfig() { diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index afa9447566..d60dec29de 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -42,9 +42,36 @@ class DropboxStorage: public Cloud::Storage { public: virtual ~DropboxStorage(); - virtual void listDirectory(Common::String path); - virtual void syncSaves(); - virtual void printInfo(); + /** Returns pointer to Common::Array<CloudFile>. */ + virtual void listDirectory(Common::String path, ListDirectoryCallback callback) {} //TODO + + /** Calls the callback when finished. */ + virtual void upload(Common::String path, Common::ReadStream* contents, OperationCallback callback) {} //TODO + + /** Returns pointer to Common::ReadStream. */ + virtual void download(Common::String path, DownloadCallback callback) {} //TODO + + /** Calls the callback when finished. */ + virtual void remove(Common::String path, OperationCallback callback) {} //TODO + + /** Calls the callback when finished. */ + virtual void syncSaves(OperationCallback callback); + + /** Calls the callback when finished. */ + virtual void createDirectory(Common::String path, OperationCallback callback) {} //TODO + + /** Calls the callback when finished. */ + virtual void touch(Common::String path, OperationCallback callback) {} //TODO + + /** Returns pointer to the ServiceInfo struct. */ + virtual void info(InfoCallback callback); + + /** Returns whether saves sync process is running. */ + virtual bool isSyncing() { return false; } //TODO + + /** Returns whether there are any requests running. */ + virtual bool isWorking() { return false; } //TODO + /** * 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. |