diff options
author | Alexander Tkachev | 2016-06-10 16:35:23 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 9ee2eb4e60a34948797620a0f80ae0a80037efc0 (patch) | |
tree | 582ec42766dbdb505a803678580f4f52c5d4b504 /backends/cloud | |
parent | 3e6503743c2f5d90c64bf37e943338c33fc58d2b (diff) | |
download | scummvm-rg350-9ee2eb4e60a34948797620a0f80ae0a80037efc0.tar.gz scummvm-rg350-9ee2eb4e60a34948797620a0f80ae0a80037efc0.tar.bz2 scummvm-rg350-9ee2eb4e60a34948797620a0f80ae0a80037efc0.zip |
GUI: Add EditText in StorageWizardDialog
One can enter the code, press 'Connect' button and get a working
Storage!
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/cloudmanager.cpp | 10 | ||||
-rw-r--r-- | backends/cloud/cloudmanager.h | 99 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 80 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 12 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivestorage.h | 10 | ||||
-rw-r--r-- | backends/cloud/onedrive/onedrivestorage.h | 8 |
6 files changed, 101 insertions, 118 deletions
diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp index fed35a9f38..adfebdca88 100644 --- a/backends/cloud/cloudmanager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -195,6 +195,16 @@ void CloudManager::setStorageLastSync(uint32 index, Common::String date) { save(); } +void CloudManager::connectStorage(uint32 index, Common::String code) { + Storage *storage = nullptr; + switch (index) { + case kStorageDropboxId: storage = new Dropbox::DropboxStorage(code); break; + case kStorageOneDriveId: storage = new OneDrive::OneDriveStorage(code); break; + case kStorageGoogleDriveId: storage = new GoogleDrive::GoogleDriveStorage(code); break; + } + //these would automatically request replaceStorage() when they receive the token +} + void CloudManager::printBool(Storage::BoolResponse response) const { debug("bool = %s", (response.value ? "true" : "false")); } diff --git a/backends/cloud/cloudmanager.h b/backends/cloud/cloudmanager.h index fd130c5ee8..9f4882d772 100644 --- a/backends/cloud/cloudmanager.h +++ b/backends/cloud/cloudmanager.h @@ -97,79 +97,88 @@ public: Cloud::Storage *getCurrentStorage() const; /** - * Return active Storage's index. - * - * @return active Storage's index. - */ + * Return active Storage's index. + * + * @return active Storage's index. + */ uint32 getStorageIndex() const; /** - * Return Storages names as list. - * - * @return a list of Storages names. - */ + * Return Storages names as list. + * + * @return a list of Storages names. + */ Common::StringArray listStorages() const; /** - * Changes the storage to the one with given index. - * - * @param new Storage's index. - */ + * Changes the storage to the one with given index. + * + * @param new Storage's index. + */ bool switchStorage(uint32 index); /** - * Return username used by Storage. - * - * @param Storage's index. - * @returns username or "" if index is invalid (no such Storage). - */ + * Return username used by Storage. + * + * @param Storage's index. + * @returns username or "" if index is invalid (no such Storage). + */ Common::String getStorageUsername(uint32 index); /** - * Return space used by Storage. - * - * @param Storage's index. - * @returns used space in bytes or 0 if index is invalid (no such Storage). - */ + * Return space used by Storage. + * + * @param Storage's index. + * @returns used space in bytes or 0 if index is invalid (no such Storage). + */ uint64 getStorageUsedSpace(uint32 index); /** - * Return Storage's last sync date. - * - * @param Storage's index. - * @returns last sync date or "" if index is invalid (no such Storage). + * Return Storage's last sync date. + * + * @param Storage's index. + * @returns last sync date or "" if index is invalid (no such Storage). It also returns "" if there never was any sync or if storage is syncing right now. - */ + */ Common::String getStorageLastSync(uint32 index); /** - * Set Storage's username. - * Automatically saves changes to the config. - * - * @param index Storage's index. - * @param name username to set - */ + * Set Storage's username. + * Automatically saves changes to the config. + * + * @param index Storage's index. + * @param name username to set + */ void setStorageUsername(uint32 index, Common::String name); /** - * Set Storage's used space field. - * Automatically saves changes to the config. - * - * @param index Storage's index. - * @param used value to set - */ + * Set Storage's used space field. + * Automatically saves changes to the config. + * + * @param index Storage's index. + * @param used value to set + */ void setStorageUsedSpace(uint32 index, uint64 used); /** - * Set Storage's last sync date. - * Automatically saves changes to the config. - * - * @param index Storage's index. - * @param date date to set - */ + * Set Storage's last sync date. + * Automatically saves changes to the config. + * + * @param index Storage's index. + * @param date date to set + */ void setStorageLastSync(uint32 index, Common::String date); + /** + * Replace Storage which has given index with a + * storage created with given code. + * + * @param index Storage's index + * @param code OAuth2 code received from user + */ + void connectStorage(uint32 index, Common::String code); + /** Returns ListDirectoryResponse with list of files. */ Networking::Request *listDirectory(Common::String path, Storage::ListDirectoryCallback callback, Networking::ErrorCallback errorCallback, bool recursive = false); diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index faff10f1d9..180b40c3d5 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -51,41 +51,47 @@ void DropboxStorage::loadKeyAndSecret() { SECRET[k.size()] = 0; } -static void saveAccessTokenCallback(Networking::JsonResponse pair) { - Common::JSONValue *json = (Common::JSONValue *)pair.value; - if (json) { - debug("saveAccessTokenCallback:"); - debug("%s", json->stringify(true).c_str()); +DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {} + +DropboxStorage::DropboxStorage(Common::String code) { + getAccessToken(code); +} + +DropboxStorage::~DropboxStorage() {} + +void DropboxStorage::getAccessToken(Common::String code) { + Networking::JsonCallback callback = new Common::Callback<DropboxStorage, Networking::JsonResponse>(this, &DropboxStorage::codeFlowComplete); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, nullptr, "https://api.dropboxapi.com/1/oauth2/token"); + request->addPostField("code=" + code); + request->addPostField("grant_type=authorization_code"); + request->addPostField("client_id=" + Common::String(KEY)); + request->addPostField("client_secret=" + Common::String(SECRET)); + request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"); + addRequest(request); +} +void DropboxStorage::codeFlowComplete(Networking::JsonResponse response) { + Common::JSONValue *json = (Common::JSONValue *)response.value; + if (json) { Common::JSONObject result = json->asObject(); if (!result.contains("access_token") || !result.contains("uid")) { warning("Bad response, no token/uid passed"); } else { - //we suppose that's the first storage - //TODO: update it to use CloudMan.replaceStorage() - ConfMan.set("current_storage", "1", "cloud"); - ConfMan.set("storage_Dropbox_type", "Dropbox", "cloud"); - ConfMan.set("storage_Dropbox_access_token", result.getVal("access_token")->asString(), "cloud"); - ConfMan.set("storage_Dropbox_user_id", result.getVal("uid")->asString(), "cloud"); + _token = result.getVal("access_token")->asString(); + _uid = result.getVal("user_id")->asString(); ConfMan.removeKey("dropbox_code", "cloud"); + CloudMan.replaceStorage(this, kStorageDropboxId); ConfMan.flushToDisk(); - debug("Now please restart ScummVM to apply the changes."); + debug("Done! You can use Dropbox now! Look:"); + CloudMan.testFeature(); } delete json; } else { - debug("saveAccessTokenCallback: got NULL instead of JSON!"); + debug("DropboxStorage::codeFlowComplete: got NULL instead of JSON!"); } } -DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) { - curl_global_init(CURL_GLOBAL_ALL); -} - -DropboxStorage::~DropboxStorage() { - curl_global_cleanup(); -} - void DropboxStorage::saveConfig(Common::String keyPrefix) { ConfMan.set(keyPrefix + "access_token", _token, "cloud"); ConfMan.set(keyPrefix + "user_id", _uid, "cloud"); @@ -216,37 +222,5 @@ Common::String DropboxStorage::getAuthLink() { return url; } -void DropboxStorage::authThroughConsole() { - if (!ConfMan.hasKey("DROPBOX_KEY", "cloud") || !ConfMan.hasKey("DROPBOX_SECRET", "cloud")) { - warning("No Dropbox keys available, cannot do auth"); - return; - } - - loadKeyAndSecret(); - - if (ConfMan.hasKey("dropbox_code", "cloud")) { - //phase 2: get access_token using specified code - 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"); -} - -void DropboxStorage::getAccessToken(Common::String code) { - Networking::JsonCallback callback = new Common::GlobalFunctionCallback<Networking::JsonResponse>(saveAccessTokenCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, nullptr, "https://api.dropboxapi.com/1/oauth2/token"); //TODO - request->addPostField("code=" + code); - request->addPostField("grant_type=authorization_code"); - request->addPostField("client_id=" + Common::String(KEY)); - request->addPostField("client_secret=" + Common::String(SECRET)); - request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F"); - ConnMan.addRequest(request); -} - } // End of namespace Dropbox } // End of namespace Cloud diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index 60a8075201..d256e0562b 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -40,7 +40,8 @@ class DropboxStorage: public Cloud::Storage { /** This private constructor is called from loadFromConfig(). */ DropboxStorage(Common::String token, Common::String uid); - static void getAccessToken(Common::String code); + void getAccessToken(Common::String code); + void codeFlowComplete(Networking::JsonResponse response); /** Constructs StorageInfo based on JSON response from cloud. */ void infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse json); @@ -49,7 +50,9 @@ class DropboxStorage: public Cloud::Storage { void printBool(BoolResponse response); void printStorageFile(UploadResponse response); -public: +public: + /** This constructor uses OAuth code flow to get tokens. */ + DropboxStorage(Common::String code); virtual ~DropboxStorage(); /** @@ -107,11 +110,6 @@ public: * Returns Dropbox auth link. */ static Common::String getAuthLink(); - - /** - * Show message with Dropbox auth instructions. (Temporary) - */ - static void authThroughConsole(); }; } // End of namespace Dropbox diff --git a/backends/cloud/googledrive/googledrivestorage.h b/backends/cloud/googledrive/googledrivestorage.h index 489260db09..8093ef1938 100644 --- a/backends/cloud/googledrive/googledrivestorage.h +++ b/backends/cloud/googledrive/googledrivestorage.h @@ -40,12 +40,6 @@ class GoogleDriveStorage: public Cloud::Storage { /** This private constructor is called from loadFromConfig(). */ GoogleDriveStorage(Common::String token, Common::String refreshToken); - /** - * This private constructor is called from authThroughConsole() (phase 2). - * It uses OAuth code flow to get tokens. - */ - GoogleDriveStorage(Common::String code); - void tokenRefreshed(BoolCallback callback, Networking::JsonResponse response); void codeFlowComplete(BoolResponse response); @@ -61,7 +55,9 @@ class GoogleDriveStorage: public Cloud::Storage { void printBool(BoolResponse response); void printFile(UploadResponse response); void printInfo(StorageInfoResponse response); -public: +public: + /** This constructor uses OAuth code flow to get tokens. */ + GoogleDriveStorage(Common::String code); virtual ~GoogleDriveStorage(); /** diff --git a/backends/cloud/onedrive/onedrivestorage.h b/backends/cloud/onedrive/onedrivestorage.h index 3932d44aae..061d0fa172 100644 --- a/backends/cloud/onedrive/onedrivestorage.h +++ b/backends/cloud/onedrive/onedrivestorage.h @@ -40,12 +40,6 @@ class OneDriveStorage: public Cloud::Storage { /** This private constructor is called from loadFromConfig(). */ OneDriveStorage(Common::String token, Common::String uid, Common::String refreshToken); - /** - * This private constructor is called from authThroughConsole() (phase 2). - * It uses OAuth code flow to get tokens. - */ - OneDriveStorage(Common::String code); - void tokenRefreshed(BoolCallback callback, Networking::JsonResponse response); void codeFlowComplete(BoolResponse response); @@ -60,6 +54,8 @@ class OneDriveStorage: public Cloud::Storage { void fileInfoCallback(Networking::NetworkReadStreamCallback outerCallback, Networking::JsonResponse response); public: + /** This constructor uses OAuth code flow to get tokens. */ + OneDriveStorage(Common::String code); virtual ~OneDriveStorage(); /** |