From ca456a7241fb4b46f9c76c86eeecc273ca31d8f6 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sat, 21 May 2016 14:06:50 +0600 Subject: CLOUD: Add object-oriented Callbacks These callbacks can call object's methods, not some global C functions. DropboxStorage::info2() and DropboxStorage::infoMethodCallback() demonstrate the idea. --- backends/cloud/dropbox/dropboxstorage.cpp | 41 +++++++++++++++++++++++++++++-- backends/cloud/dropbox/dropboxstorage.h | 4 +++ 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'backends/cloud') diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index f2270f79cd..94bdb43450 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -92,6 +92,30 @@ void infoCallback(Networking::Request* request, void *jsonPointer) { } } +void info2Callback(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; + } + + Common::BaseCallback *callback = (Common::BaseCallback *)request->pointer(); + + Common::JSONValue *json = (Common::JSONValue *)jsonPointer; + if (json) { + //Common::JSONObject result = json->asObject(); + if (callback) { + (*callback)(new StorageInfo(json->stringify())); + delete callback; + } + 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); } @@ -104,9 +128,14 @@ void syncSavesInfoCallback(StorageInfo info) { debug("info: %s", info.info().c_str()); } +void DropboxStorage::infoMethodCallback(void *storageInfo) { + StorageInfo *info = (StorageInfo *)storageInfo; + debug("info: %s", info->info().c_str()); +} + void DropboxStorage::syncSaves(OperationCallback callback) { - //this is not the real syncSaves() implementation - info(syncSavesInfoCallback); + //this is not the real syncSaves() implementation + info2(new Common::Callback(this, &DropboxStorage::infoMethodCallback)); } void DropboxStorage::info(InfoCallback callback) { @@ -117,6 +146,14 @@ void DropboxStorage::info(InfoCallback callback) { request->setPointer(callback); } +void DropboxStorage::info2(Common::Callback *callback) { + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(info2Callback, "https://api.dropboxapi.com/1/account/info"); + request->addHeader("Authorization: Bearer " + _token); + ConnMan.addRequest(request); + + request->setPointer(callback); +} + DropboxStorage *DropboxStorage::loadFromConfig() { KEY = ConfMan.get("DROPBOX_KEY", "cloud"); SECRET = ConfMan.get("DROPBOX_SECRET", "cloud"); diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index d60dec29de..1cf657bc36 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -25,6 +25,7 @@ #include "backends/cloud/storage.h" #include "backends/cloud/manager.h" +#include "common/callback.h" namespace Cloud { namespace Dropbox { @@ -39,6 +40,8 @@ class DropboxStorage: public Cloud::Storage { static void getAccessToken(Common::String code); + void infoMethodCallback(void *serviceInfoPtr); + public: virtual ~DropboxStorage(); @@ -65,6 +68,7 @@ public: /** Returns pointer to the ServiceInfo struct. */ virtual void info(InfoCallback callback); + void info2(Common::Callback *callback); /** Returns whether saves sync process is running. */ virtual bool isSyncing() { return false; } //TODO -- cgit v1.2.3