diff options
author | Alexander Tkachev | 2016-05-18 15:21:09 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 0a947618fb1faa023df03d355e9749905424dd58 (patch) | |
tree | db663626a23c333da1f9279cd6f92e8ce9c6e610 /backends/cloud | |
parent | e743a65636a674def45e955cb7a5632aead2a033 (diff) | |
download | scummvm-rg350-0a947618fb1faa023df03d355e9749905424dd58.tar.gz scummvm-rg350-0a947618fb1faa023df03d355e9749905424dd58.tar.bz2 scummvm-rg350-0a947618fb1faa023df03d355e9749905424dd58.zip |
CLOUD: Make ConnectionManager singleton
With ConnectionManager singleton one can start their Requests without
creating Storage instance. Moreover, Storage instance should contain
cloud API, not Requests-related handling and timer starting methods.
Thus, these methods were moved into ConnectionManager itself.
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 26 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 8 | ||||
-rw-r--r-- | backends/cloud/request.h | 55 | ||||
-rw-r--r-- | backends/cloud/storage.cpp | 73 | ||||
-rw-r--r-- | backends/cloud/storage.h | 19 |
5 files changed, 16 insertions, 165 deletions
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index add6bff54c..93f0eebcf6 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -22,6 +22,7 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/cloud/dropbox/dropboxstorage.h" +#include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" #include "common/config-manager.h" #include "common/debug.h" @@ -76,8 +77,7 @@ DropboxStorage::~DropboxStorage() { curl_global_cleanup(); } -void DropboxStorage::listDirectory(Common::String path) { - startTimer(1000000); //in one second +void DropboxStorage::listDirectory(Common::String path) { } void DropboxStorage::syncSaves() { @@ -88,7 +88,7 @@ void DropboxStorage::syncSaves() { void DropboxStorage::printInfo() { Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(printJsonCallback, "https://api.dropboxapi.com/1/account/info"); request->addHeader("Authorization: Bearer " + _token); - addRequest(request); + ConnMan.addRequest(request); } DropboxStorage *DropboxStorage::loadFromConfig() { @@ -119,10 +119,10 @@ Common::String DropboxStorage::getAuthLink() { return url; } -DropboxStorage *DropboxStorage::authThroughConsole() { +void DropboxStorage::authThroughConsole() { if (!ConfMan.hasKey("DROPBOX_KEY", "cloud") || !ConfMan.hasKey("DROPBOX_SECRET", "cloud")) { warning("No Dropbox keys available, cannot do auth"); - return 0; + return; } KEY = ConfMan.get("DROPBOX_KEY", "cloud"); @@ -130,29 +130,25 @@ DropboxStorage *DropboxStorage::authThroughConsole() { if (ConfMan.hasKey("dropbox_code", "cloud")) { //phase 2: get access_token using specified code - return getAccessToken(ConfMan.get("dropbox_code", "cloud")); + getAccessToken(ConfMan.get("dropbox_code", "cloud")); + return; } debug("Navigate to this URL and press \"Allow\":"); debug("%s\n", getAuthLink().c_str()); debug("Then, add dropbox_code key in [cloud] section of configuration file. You should copy the <code> value from URL and put it as value for that key.\n"); debug("Navigate to this URL to get more information on ScummVM's configuration files:"); - debug("http://wiki.scummvm.org/index.php/User_Manual/Configuring_ScummVM#Using_the_configuration_file_to_configure_ScummVM\n"); - return 0; + debug("http://wiki.scummvm.org/index.php/User_Manual/Configuring_ScummVM#Using_the_configuration_file_to_configure_ScummVM\n"); } -DropboxStorage *DropboxStorage::getAccessToken(Common::String code) { +void DropboxStorage::getAccessToken(Common::String code) { Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(saveAccessTokenCallback, "https://api.dropboxapi.com/1/oauth2/token"); request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); request->addPostField("client_id=" + KEY); request->addPostField("client_secret=" + SECRET); - request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"); - - //OK, that's not how I imagined that... - DropboxStorage *storage = new DropboxStorage("", ""); - storage->addRequest(request); - return storage; + request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"); + ConnMan.addRequest(request); } } //end of namespace Dropbox diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index f2281f146f..0500db5d49 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -24,7 +24,7 @@ #define BACKENDS_CLOUD_DROPBOX_STORAGE_H #include "backends/cloud/storage.h" -#include "../manager.h" +#include "backends/cloud/manager.h" namespace Cloud { namespace Dropbox { @@ -37,7 +37,7 @@ class DropboxStorage: public Cloud::Storage { /** This private constructor is called from loadFromConfig(). */ DropboxStorage(Common::String token, Common::String uid); - static DropboxStorage *getAccessToken(Common::String code); + static void getAccessToken(Common::String code); public: virtual ~DropboxStorage(); @@ -58,10 +58,8 @@ public: /** * Show message with Dropbox auth instructions. (Temporary) - * Returns temporary DropboxStorage, which does network requests - * to get access token. */ - static DropboxStorage *authThroughConsole(); + static void authThroughConsole(); }; } //end of namespace Dropbox diff --git a/backends/cloud/request.h b/backends/cloud/request.h deleted file mode 100644 index d85a68d570..0000000000 --- a/backends/cloud/request.h +++ /dev/null @@ -1,55 +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_REQUEST_H -#define BACKENDS_CLOUD_REQUEST_H - -#include "backends/networking/curl/connectionmanager.h" - -namespace Cloud { - -class Request { -protected: - /** - * Callback, which should be called before Request returns true in handle(). - * That's the way Requests pass the result to the code which asked to create this request. - */ - - typedef void(*Callback)(void *result); - Callback _callback; - -public: - Request(Callback cb): _callback(cb) {}; - virtual ~Request() {}; - - /** - * Method, which does actual work. Depends on what this Request is doing. - * - * @return true if request's work is complete and it may be removed from Storage's list - */ - - virtual bool handle(Networking::ConnectionManager &manager) = 0; -}; - -} //end of namespace Cloud - -#endif diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp deleted file mode 100644 index d7217a57cd..0000000000 --- a/backends/cloud/storage.cpp +++ /dev/null @@ -1,73 +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. -* -*/ - -#include "backends/cloud/storage.h" -#include "common/debug.h" -#include "common/system.h" -#include "common/timer.h" - -namespace Cloud { - -void cloudThread(void *thread) { - Storage *cloudThread = (Storage *)thread; - cloudThread->handler(); -} - -Storage::Storage() : _timerStarted(false) {} - -void Storage::addRequest(Request *request) { - _requests.push_back(request); - if (!_timerStarted) startTimer(); -} - -void Storage::handler() { - //TODO: lock mutex here (in case another handler() would be called before this one ends) - debug("\nhandler's here"); - for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) { - if ((*i)->handle(_connectionManager)) { - delete (*i); - _requests.erase(i); - } - else ++i; - } - if (_requests.empty()) stopTimer(); - - _connectionManager.handle(); - //TODO: unlock mutex here -} - -void Storage::startTimer(int interval) { - Common::TimerManager *manager = g_system->getTimerManager(); - if (manager->installTimerProc(cloudThread, interval, this, "Cloud Thread")) { - _timerStarted = true; - } else { - warning("Failed to create cloud thread"); - } -} - -void Storage::stopTimer() { - Common::TimerManager *manager = g_system->getTimerManager(); - manager->removeTimerProc(cloudThread); - _timerStarted = false; -} - -} //end of namespace Cloud diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index a5d048d3af..9e23e97761 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -24,28 +24,13 @@ #define BACKENDS_CLOUD_STORAGE_H #include "common/str.h" -#include "common/array.h" -#include "backends/cloud/request.h" -#include "backends/networking/curl/connectionmanager.h" namespace Cloud { class Storage { - friend void cloudThread(void *); //calls handler() - bool _timerStarted; - -protected: - Common::Array<Request *> _requests; - Networking::ConnectionManager _connectionManager; - - virtual void addRequest(Request *request); //starts the timer if it's not started - virtual void handler(); - virtual void startTimer(int interval = 1000000); //1 second is the default interval - virtual void stopTimer(); - public: - Storage(); - virtual ~Storage() {}; + Storage() {} + virtual ~Storage() {} /** * Lists given directory. |