diff options
author | Alexander Tkachev | 2016-06-01 16:22:42 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | b3bf5322117d03c003011839ea1e7897c48183fa (patch) | |
tree | 7991bd7d812bcc9dbd4dfd078a55b16f7ec8e0ba | |
parent | 1f974a7a2a2073074391fbf090d2bf909006e773 (diff) | |
download | scummvm-rg350-b3bf5322117d03c003011839ea1e7897c48183fa.tar.gz scummvm-rg350-b3bf5322117d03c003011839ea1e7897c48183fa.tar.bz2 scummvm-rg350-b3bf5322117d03c003011839ea1e7897c48183fa.zip |
CLOUD: Make CloudManager singleton
It's needed to ::destroy() it in main().
-rw-r--r-- | backends/cloud/cloudmanager.cpp (renamed from backends/cloud/manager.cpp) | 51 | ||||
-rw-r--r-- | backends/cloud/cloudmanager.h (renamed from common/cloudmanager.h) | 36 | ||||
-rw-r--r-- | backends/cloud/manager.h | 53 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.cpp | 9 | ||||
-rw-r--r-- | backends/module.mk | 2 | ||||
-rw-r--r-- | backends/platform/sdl/sdl.cpp | 6 | ||||
-rw-r--r-- | base/main.cpp | 14 | ||||
-rw-r--r-- | common/system.h | 22 |
8 files changed, 60 insertions, 133 deletions
diff --git a/backends/cloud/manager.cpp b/backends/cloud/cloudmanager.cpp index 13424ba8c6..d18bb6ff9a 100644 --- a/backends/cloud/manager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -20,37 +20,33 @@ * */ -#include "backends/cloud/manager.h" +#include "backends/cloud/cloudmanager.h" #include "backends/cloud/dropbox/dropboxstorage.h" #include "backends/cloud/onedrive/onedrivestorage.h" #include "common/config-manager.h" -#include "common/random.h" #include "common/debug.h" +namespace Common { + +DECLARE_SINGLETON(Cloud::CloudManager); + +} + namespace Cloud { -Manager::Manager(): _currentStorageIndex(0), _deviceId(0) {} +CloudManager::CloudManager() : _currentStorageIndex(0) {} -Manager::~Manager() { +CloudManager::~CloudManager() { //TODO: do we have to save storages on manager destruction? for (uint32 i = 0; i < _storages.size(); ++i) delete _storages[i]; - _storages.clear(); + _storages.clear(); } -void Manager::init() { +void CloudManager::init() { bool offerDropbox = false; bool offerOneDrive = true; - - if (!ConfMan.hasKey("device_id", "cloud")) { - Common::RandomSource source("Cloud Random Source"); - _deviceId = source.getRandomNumber(UINT_MAX - 1); - ConfMan.setInt("device_id", _deviceId, "cloud"); - ConfMan.flushToDisk(); - } else { - _deviceId = ConfMan.getInt("device_id", "cloud"); - } - + if (ConfMan.hasKey("storages_number", "cloud")) { int storages = ConfMan.getInt("storages_number", "cloud"); for (int i = 1; i <= storages; ++i) { @@ -80,50 +76,47 @@ void Manager::init() { } else { offerDropbox = true; } - if (offerDropbox) { //this is temporary console offer to auth with Dropbox Dropbox::DropboxStorage::authThroughConsole(); - } else if(offerOneDrive) { + } else if (offerOneDrive) { //OneDrive time OneDrive::OneDriveStorage::authThroughConsole(); } } -void Manager::save() { +void CloudManager::save() { 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)); + _storages[i]->saveConfig(Common::String::format("storage%d_", i + 1)); ConfMan.flushToDisk(); } -void Manager::addStorage(Cloud::Storage *storage, bool makeCurrent, bool saveConfig) { - if (!storage) error("Cloud::Manager: NULL storage passed"); +void CloudManager::addStorage(Storage *storage, bool makeCurrent, bool saveConfig) { + if (!storage) error("Cloud::CloudManager: NULL storage passed"); _storages.push_back(storage); if (makeCurrent) _currentStorageIndex = _storages.size() - 1; if (saveConfig) save(); } -Storage *Manager::getCurrentStorage() { +Storage *CloudManager::getCurrentStorage() { if (_currentStorageIndex < _storages.size()) return _storages[_currentStorageIndex]; return nullptr; } -void Manager::printBool(Storage::BoolResponse response) { +void CloudManager::printBool(Storage::BoolResponse response) const { debug("bool = %s", (response.value ? "true" : "false")); } -void Manager::syncSaves(Storage::BoolCallback callback, Networking::ErrorCallback errorCallback) { +void CloudManager::syncSaves(Storage::BoolCallback callback, Networking::ErrorCallback errorCallback) { Storage *storage = getCurrentStorage(); if (storage) storage->syncSaves(callback, errorCallback); } -void Manager::testFeature() { +void CloudManager::testFeature() { Storage *storage = getCurrentStorage(); - if (storage) storage->createDirectory("base/belong_to_us", - new Common::Callback<Manager, Storage::BoolResponse>(this, &Manager::printBool), nullptr); } -} // End of namespace Cloud +} // End of namespace Common diff --git a/common/cloudmanager.h b/backends/cloud/cloudmanager.h index 936f0e0108..a13eeebb94 100644 --- a/common/cloudmanager.h +++ b/backends/cloud/cloudmanager.h @@ -20,29 +20,36 @@ * */ -#ifndef COMMON_CLOUDMANAGER_H -#define COMMON_CLOUDMANAGER_H +#ifndef CLOUD_CLOUDMANAGER_H +#define CLOUD_CLOUDMANAGER_H #include "backends/cloud/storage.h" +#include "common/array.h" +#include "common/singleton.h" -namespace Common { +namespace Cloud { + +class CloudManager : public Common::Singleton<CloudManager> { + Common::Array<Cloud::Storage *> _storages; + uint _currentStorageIndex; + + void printBool(Cloud::Storage::BoolResponse response) const; -class CloudManager { public: - CloudManager() {} - virtual ~CloudManager() {} + CloudManager(); + virtual ~CloudManager(); /** * Loads all information from configs and creates current Storage instance. * * @note It's called once on startup in scummvm_main(). */ - virtual void init() = 0; + void init(); /** * Saves all information into configuration file. */ - virtual void save() = 0; + void save(); /** * Adds new Storage into list. @@ -51,7 +58,7 @@ public: * @param makeCurrent whether added storage should be the new current storage. * @param saveConfig whether save() should be called to update configuration file. */ - virtual void addStorage(Cloud::Storage *storage, bool makeCurrent = true, bool saveConfig = true) = 0; + void addStorage(Cloud::Storage *storage, bool makeCurrent = true, bool saveConfig = true); /** * Returns active Storage, which could be used to interact @@ -59,19 +66,22 @@ public: * * @return active Cloud::Storage or null, if there is no active Storage. */ - virtual Cloud::Storage *getCurrentStorage() = 0; + Cloud::Storage *getCurrentStorage(); /** * Starts saves syncing process in currently active storage if there is any. */ - virtual void syncSaves(Cloud::Storage::BoolCallback callback = nullptr, Networking::ErrorCallback errorCallback = nullptr) = 0; + void syncSaves(Cloud::Storage::BoolCallback callback = nullptr, Networking::ErrorCallback errorCallback = nullptr); /** * Starts feature testing (the one I'm working on currently). (Temporary) */ - virtual void testFeature() = 0; + void testFeature(); }; -} // End of namespace Common +/** Shortcut for accessing the connection manager. */ +#define CloudMan Cloud::CloudManager::instance() + +} // End of namespace Cloud #endif diff --git a/backends/cloud/manager.h b/backends/cloud/manager.h deleted file mode 100644 index f68b33517d..0000000000 --- a/backends/cloud/manager.h +++ /dev/null @@ -1,53 +0,0 @@ -/* 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_MANAGER_H -#define BACKENDS_CLOUD_MANAGER_H - -#include "common/cloudmanager.h" -#include "common/str.h" - -namespace Cloud { - -class Manager: public Common::CloudManager { - Common::Array<Storage *> _storages; - uint _currentStorageIndex; - uint _deviceId; - - void printBool(Storage::BoolResponse response); - -public: - Manager(); - virtual ~Manager(); - - virtual void init(); - virtual void save(); - virtual void addStorage(Cloud::Storage *storage, bool makeCurrent = true, bool saveConfig = true); - - virtual Storage *getCurrentStorage(); - virtual void syncSaves(Storage::BoolCallback callback, Networking::ErrorCallback errorCallback); - virtual void testFeature(); -}; - -} // End of namespace Cloud - -#endif diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp index 98f0ac5a4d..ca8a2346ad 100644 --- a/backends/cloud/onedrive/onedrivestorage.cpp +++ b/backends/cloud/onedrive/onedrivestorage.cpp @@ -22,6 +22,7 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/cloud/onedrive/onedrivestorage.h" +#include "backends/cloud/cloudmanager.h" #include "backends/cloud/onedrive/onedrivecreatedirectoryrequest.h" #include "backends/cloud/onedrive/onedrivetokenrefresher.h" #include "backends/cloud/onedrive/onedrivelistdirectoryrequest.h" @@ -29,11 +30,9 @@ #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" #include "backends/networking/curl/networkreadstream.h" -#include "common/cloudmanager.h" #include "common/config-manager.h" #include "common/debug.h" #include "common/json.h" -#include "common/system.h" #include <curl/curl.h> namespace Cloud { @@ -104,7 +103,7 @@ void OneDriveStorage::tokenRefreshed(BoolCallback callback, Networking::JsonResp _token = result.getVal("access_token")->asString(); _uid = result.getVal("user_id")->asString(); _refreshToken = result.getVal("refresh_token")->asString(); - g_system->getCloudManager()->save(); //ask CloudManager to save our new refreshToken + CloudMan.save(); //ask CloudManager to save our new refreshToken if (callback) (*callback)(BoolResponse(nullptr, true)); } delete json; @@ -116,10 +115,10 @@ void OneDriveStorage::codeFlowComplete(BoolResponse response) { return; } - g_system->getCloudManager()->addStorage(this); + CloudMan.addStorage(this); ConfMan.removeKey("onedrive_code", "cloud"); debug("Done! You can use OneDrive now! Look:"); - g_system->getCloudManager()->syncSaves(); + CloudMan.syncSaves(); } void OneDriveStorage::saveConfig(Common::String keyPrefix) { diff --git a/backends/module.mk b/backends/module.mk index 4733509f24..40ccd17c78 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -21,8 +21,8 @@ MODULE_OBJS := \ ifdef USE_CLOUD MODULE_OBJS += \ + cloud/cloudmanager.o \ cloud/iso8601.o \ - cloud/manager.o \ cloud/storage.o \ cloud/storagefile.o \ cloud/downloadrequest.o \ diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index acb4d999c3..dca6891fef 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -33,7 +33,6 @@ #include "gui/EventRecorder.h" #include "common/taskbar.h" #include "common/textconsole.h" -#include "backends/cloud/manager.h" #include "backends/saves/default/default-saves.h" @@ -159,11 +158,6 @@ void OSystem_SDL::init() { _taskbarManager = new Common::TaskbarManager(); #endif -#if defined(USE_CLOUD) - if (_cloudManager == 0) - _cloudManager = new Cloud::Manager(); -#endif - } void OSystem_SDL::initBackend() { diff --git a/base/main.cpp b/base/main.cpp index aede61790c..4edc7a957a 100644 --- a/base/main.cpp +++ b/base/main.cpp @@ -66,7 +66,9 @@ #endif #include "backends/keymapper/keymapper.h" -#include "common/cloudmanager.h" +#ifdef USE_CLOUD +#include "backends/cloud/cloudmanager.h" +#endif #ifdef USE_LIBCURL #include "backends/networking/curl/connectionmanager.h" #endif @@ -481,9 +483,9 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) { #endif #ifdef USE_CLOUD - system.getCloudManager()->init(); - system.getCloudManager()->syncSaves(); - system.getCloudManager()->testFeature(); //TODO: remove later + CloudMan.init(); + CloudMan.syncSaves(); + CloudMan.testFeature(); //TODO: remove later #endif // Unless a game was specified, show the launcher dialog @@ -597,6 +599,10 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) { #ifdef USE_LIBCURL Networking::ConnectionManager::destroy(); #endif +#ifdef USE_CLOUD + //I think it's important to destroy it after ConnectionManager + Cloud::CloudManager::destroy(); +#endif PluginManager::instance().unloadAllPlugins(); PluginManager::destroy(); GUI::GuiManager::destroy(); diff --git a/common/system.h b/common/system.h index 815fa9d19d..6d185d3075 100644 --- a/common/system.h +++ b/common/system.h @@ -56,7 +56,6 @@ class HardwareInputSet; class Keymap; class KeymapperDefaultBindings; #endif -class CloudManager; } class AudioCDManager; @@ -179,15 +178,6 @@ protected: Common::UpdateManager *_updateManager; #endif -#if defined(USE_CLOUD) - /** - * No default value is provided for _cloudThread by OSystem. - * - * @note _cloudThread is deleted by the OSystem destructor. - */ - Common::CloudManager *_cloudManager; -#endif - /** * No default value is provided for _fsFactory by OSystem. * @@ -1126,18 +1116,6 @@ public: } #endif -#if defined(USE_CLOUD) - /** - * Returns the CloudManager, used to sync save games and - * upload/download files from user's cloud storage. - * - * @return the CloudManager for the current architecture - */ - virtual Common::CloudManager *getCloudManager() { - return _cloudManager; - } -#endif - /** * Returns the FilesystemFactory object, depending on the current architecture. * |