From b570499164bf94fc4735bad54e7a722498ae56ea Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sun, 22 May 2016 00:04:00 +0600 Subject: CLOUD: Add Callback typedefs And do some minor cleanup work. --- backends/cloud/dropbox/dropboxstorage.cpp | 10 +++-- backends/cloud/dropbox/dropboxstorage.h | 24 +++++------ backends/cloud/manager.cpp | 2 +- backends/cloud/manager.h | 2 +- backends/cloud/storage.h | 66 ++++++++----------------------- backends/cloud/storagefile.cpp | 48 ++++++++++++++++++++++ backends/cloud/storagefile.h | 53 +++++++++++++++++++++++++ backends/cloud/storageinfo.h | 47 ++++++++++++++++++++++ 8 files changed, 185 insertions(+), 67 deletions(-) create mode 100644 backends/cloud/storagefile.cpp create mode 100644 backends/cloud/storagefile.h create mode 100644 backends/cloud/storageinfo.h (limited to 'backends/cloud') diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 964b95bb09..49355fa845 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -66,14 +66,14 @@ DropboxStorage::~DropboxStorage() { curl_global_cleanup(); } -void DropboxStorage::syncSaves(Common::BaseCallback *callback) { +void DropboxStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation info(new Common::Callback(this, &DropboxStorage::infoMethodCallback)); //that line meant the following: //"please, do the info API request and, when it's finished, call the infoMethodCallback() of me" } -void DropboxStorage::info(Common::BaseCallback *outerCallback) { +void DropboxStorage::info(StorageInfoCallback outerCallback) { Common::BaseCallback<> *innerCallback = new Common::CallbackBridge(this, &DropboxStorage::infoInnerCallback, outerCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); @@ -84,8 +84,8 @@ void DropboxStorage::info(Common::BaseCallback *outerCallback) { //and then calls the outerCallback (which wants to receive StorageInfo, not void *) } -void DropboxStorage::infoInnerCallback(Common::BaseCallback *outerCallback, void *ptr) { - Common::JSONValue *json = (Common::JSONValue *)ptr; +void DropboxStorage::infoInnerCallback(StorageInfoCallback outerCallback, void *jsonPointer) { + Common::JSONValue *json = (Common::JSONValue *)jsonPointer; if (!json) { warning("NULL passed instead of JSON"); delete outerCallback; @@ -93,6 +93,8 @@ void DropboxStorage::infoInnerCallback(Common::BaseCallback *outerC } if (outerCallback) { + //TODO: check that JSON doesn't contain some error message instead of an actual response + //TODO: use JSON fields to construct StorageInfo (*outerCallback)(StorageInfo(json->stringify())); delete outerCallback; } diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 8cc9312a87..3fc38bc1bb 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -39,37 +39,37 @@ class DropboxStorage: public Cloud::Storage { static void getAccessToken(Common::String code); + /** Constructs StorageInfo based on JSON response from cloud. */ + void infoInnerCallback(StorageInfoCallback outerCallback, void *json); + public: virtual ~DropboxStorage(); /** Returns pointer to Common::Array. */ - virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array > *callback) {} //TODO + virtual void listDirectory(Common::String path, FileArrayCallback callback) {} //TODO /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback *callback) {} //TODO + virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) {} //TODO /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, Common::BaseCallback *callback) {} //TODO + virtual void download(Common::String path, ReadStreamCallback callback) {} //TODO /** Calls the callback when finished. */ - virtual void remove(Common::String path, Common::BaseCallback *callback) {} //TODO + virtual void remove(Common::String path, BoolCallback callback) {} //TODO /** Calls the callback when finished. */ - virtual void syncSaves(Common::BaseCallback *callback); + virtual void syncSaves(BoolCallback callback); /** Calls the callback when finished. */ - virtual void createDirectory(Common::String path, Common::BaseCallback *callback) {} //TODO + virtual void createDirectory(Common::String path, BoolCallback callback) {} //TODO /** Calls the callback when finished. */ - virtual void touch(Common::String path, Common::BaseCallback *callback) {} //TODO + virtual void touch(Common::String path, BoolCallback callback) {} //TODO /** Returns pointer to the StorageInfo struct. */ - virtual void info(Common::BaseCallback *callback); - - /** This is what is called by CurlJsonRequest. */ - void infoInnerCallback(Common::BaseCallback *outerCallback, void *ptr); + virtual void info(StorageInfoCallback callback); - /** This is what is called by infoInnerCallback() (it's its outer callback). */ + /** This method is passed into info(). (Temporary) */ void infoMethodCallback(StorageInfo storageInfo); /** Returns whether saves sync process is running. */ diff --git a/backends/cloud/manager.cpp b/backends/cloud/manager.cpp index a9a2b8a232..7caf241497 100644 --- a/backends/cloud/manager.cpp +++ b/backends/cloud/manager.cpp @@ -46,7 +46,7 @@ Storage* Manager::getCurrentStorage() { return _currentStorage; } -void Manager::syncSaves(Common::BaseCallback *callback) { +void Manager::syncSaves(Storage::BoolCallback callback) { Storage* storage = getCurrentStorage(); if (storage) storage->syncSaves(callback); } diff --git a/backends/cloud/manager.h b/backends/cloud/manager.h index c247132707..3ad2e0d607 100644 --- a/backends/cloud/manager.h +++ b/backends/cloud/manager.h @@ -38,7 +38,7 @@ public: virtual void init(); virtual Storage* getCurrentStorage(); - virtual void syncSaves(Common::BaseCallback *callback); + virtual void syncSaves(Storage::BoolCallback callback); }; } //end of namespace Cloud diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index f2079eb919..eaf0ba1cc8 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -27,76 +27,44 @@ #include "common/stream.h" #include "common/str.h" #include "common/callback.h" +#include "backends/cloud/storagefile.h" +#include "backends/cloud/storageinfo.h" namespace Cloud { -class StorageFile { - Common::String _path, _name; - uint32 _size, _timestamp; - bool _isDirectory; - -public: - StorageFile(Common::String pth, uint32 sz, uint32 ts, bool dir) { - _path = pth; - - _name = pth; - for (uint32 i = _name.size() - 1; i >= 0; --i) { - if (_name[i] == '/' || _name[i] == '\\') { - _name.erase(0, i); - break; - } - if (i == 0) break; //OK, I admit that's strange - } - - _size = sz; - _timestamp = ts; - _isDirectory = dir; - } - - Common::String path() const { return _path; } - Common::String name() const { return _name; } - uint32 size() const { return _size; } - uint32 timestamp() const { return _timestamp; } - bool isDirectory() const { return _isDirectory; } -}; - -class StorageInfo { - Common::String _info; - -public: - StorageInfo(Common::String info): _info(info) {} - - Common::String info() const { return _info; } -}; - class Storage { public: + typedef Common::BaseCallback< Common::Array > *FileArrayCallback; + typedef Common::BaseCallback *ReadStreamCallback; + typedef Common::BaseCallback *StorageInfoCallback; + typedef Common::BaseCallback *BoolCallback; + Storage() {} virtual ~Storage() {} - /** Returns pointer to Common::Array. */ - virtual void listDirectory(Common::String path, Common::BaseCallback< Common::Array > *callback) = 0; + /** Returns Common::Array. */ + virtual void listDirectory(Common::String path, FileArrayCallback callback) = 0; /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, Common::BaseCallback *callback) = 0; + virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) = 0; /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, Common::BaseCallback *callback) = 0; + virtual void download(Common::String path, ReadStreamCallback callback) = 0; /** Calls the callback when finished. */ - virtual void remove(Common::String path, Common::BaseCallback *callback) = 0; + virtual void remove(Common::String path, BoolCallback callback) = 0; /** Calls the callback when finished. */ - virtual void syncSaves(Common::BaseCallback *callback) = 0; + virtual void syncSaves(BoolCallback callback) = 0; /** Calls the callback when finished. */ - virtual void createDirectory(Common::String path, Common::BaseCallback *callback) = 0; + virtual void createDirectory(Common::String path, BoolCallback callback) = 0; /** Calls the callback when finished. */ - virtual void touch(Common::String path, Common::BaseCallback *callback) = 0; + virtual void touch(Common::String path, BoolCallback callback) = 0; - /** Returns pointer to the StorageInfo struct. */ - virtual void info(Common::BaseCallback *callback) = 0; + /** Returns the StorageInfo struct. */ + virtual void info(StorageInfoCallback callback) = 0; /** Returns whether saves sync process is running. */ virtual bool isSyncing() = 0; diff --git a/backends/cloud/storagefile.cpp b/backends/cloud/storagefile.cpp new file mode 100644 index 0000000000..0d40698823 --- /dev/null +++ b/backends/cloud/storagefile.cpp @@ -0,0 +1,48 @@ +/* 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/storagefile.h" + +namespace Cloud { + +StorageFile::StorageFile(Common::String pth, uint32 sz, uint32 ts, bool dir) { + _path = pth; + + _name = pth; + if (_name.size() != 0) { + uint32 i = _name.size() - 1; + while (true) { + if (_name[i] == '/' || _name[i] == '\\') { + _name.erase(0, i); + break; + } + if (i == 0) break; + --i; + } + } + + _size = sz; + _timestamp = ts; + _isDirectory = dir; +} + +} //end of namespace Cloud diff --git a/backends/cloud/storagefile.h b/backends/cloud/storagefile.h new file mode 100644 index 0000000000..8706ba18e9 --- /dev/null +++ b/backends/cloud/storagefile.h @@ -0,0 +1,53 @@ +/* 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 BACKENDS_CLOUD_STORAGEFILE_H +#define BACKENDS_CLOUD_STORAGEFILE_H + +#include "common/str.h" + +namespace Cloud { + +/** +* StorageFile represents a file storaged on remote cloud storage. +* It contains basic information about a file, and might be used +* when listing directories or syncing files. +*/ + +class StorageFile { + Common::String _path, _name; + uint32 _size, _timestamp; + bool _isDirectory; + +public: + StorageFile(Common::String pth, uint32 sz, uint32 ts, bool dir); + + Common::String path() const { return _path; } + Common::String name() const { return _name; } + uint32 size() const { return _size; } + uint32 timestamp() const { return _timestamp; } + bool isDirectory() const { return _isDirectory; } +}; + +} //end of namespace Cloud + +#endif diff --git a/backends/cloud/storageinfo.h b/backends/cloud/storageinfo.h new file mode 100644 index 0000000000..510acb3778 --- /dev/null +++ b/backends/cloud/storageinfo.h @@ -0,0 +1,47 @@ +/* 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 BACKENDS_CLOUD_STORAGEINFO_H +#define BACKENDS_CLOUD_STORAGEINFO_H + +#include "common/str.h" + +namespace Cloud { + +/** +* StorageInfo contains information about remote cloud storage. +* It's disk quota usage, owner name, and such. +*/ + +class StorageInfo { + /** Temporary StorageInfo just contains raw JSON, received from cloud storage. */ + Common::String _info; + +public: + StorageInfo(Common::String info): _info(info) {} + + Common::String info() const { return _info; } +}; + +} //end of namespace Cloud + +#endif -- cgit v1.2.3