ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

dropboxstorage.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00024 
00025 #include <curl/curl.h>
00026 #include "backends/cloud/dropbox/dropboxstorage.h"
00027 #include "backends/cloud/dropbox/dropboxcreatedirectoryrequest.h"
00028 #include "backends/cloud/dropbox/dropboxinforequest.h"
00029 #include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h"
00030 #include "backends/cloud/dropbox/dropboxuploadrequest.h"
00031 #include "backends/cloud/cloudmanager.h"
00032 #include "backends/networking/curl/connectionmanager.h"
00033 #include "backends/networking/curl/curljsonrequest.h"
00034 #include "common/config-manager.h"
00035 #include "common/debug.h"
00036 #include "common/json.h"
00037 
00038 namespace Cloud {
00039 namespace Dropbox {
00040 
00041 #define DROPBOX_API_FILES_DOWNLOAD "https://content.dropboxapi.com/2/files/download"
00042 
00043 DropboxStorage::DropboxStorage(Common::String accessToken, bool enabled): BaseStorage(accessToken, "", enabled) {}
00044 
00045 DropboxStorage::DropboxStorage(Common::String code, Networking::ErrorCallback cb): BaseStorage() {
00046     getAccessToken(code, cb);
00047 }
00048 
00049 DropboxStorage::~DropboxStorage() {}
00050 
00051 Common::String DropboxStorage::cloudProvider() { return "dropbox"; }
00052 
00053 uint32 DropboxStorage::storageIndex() { return kStorageDropboxId; }
00054 
00055 bool DropboxStorage::needsRefreshToken() { return false; }
00056 
00057 bool DropboxStorage::canReuseRefreshToken() { return false; }
00058 
00059 void DropboxStorage::saveConfig(Common::String keyPrefix) {
00060     ConfMan.set(keyPrefix + "access_token", _token, ConfMan.kCloudDomain);
00061     saveIsEnabledFlag(keyPrefix);
00062 }
00063 
00064 Common::String DropboxStorage::name() const {
00065     return "Dropbox";
00066 }
00067 
00068 Networking::Request *DropboxStorage::listDirectory(Common::String path, ListDirectoryCallback outerCallback, Networking::ErrorCallback errorCallback, bool recursive) {
00069     return addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, errorCallback, recursive));
00070 }
00071 
00072 Networking::Request *DropboxStorage::upload(Common::String path, Common::SeekableReadStream *contents, UploadCallback callback, Networking::ErrorCallback errorCallback) {
00073     return addRequest(new DropboxUploadRequest(_token, path, contents, callback, errorCallback));
00074 }
00075 
00076 Networking::Request *DropboxStorage::streamFileById(Common::String path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
00077     Common::JSONObject jsonRequestParameters;
00078     jsonRequestParameters.setVal("path", new Common::JSONValue(path));
00079     Common::JSONValue value(jsonRequestParameters);
00080 
00081     Networking::CurlRequest *request = new Networking::CurlRequest(nullptr, nullptr, DROPBOX_API_FILES_DOWNLOAD); //TODO: is it OK to pass no callbacks?
00082     request->addHeader("Authorization: Bearer " + _token);
00083     request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value));
00084     request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded)
00085 
00086     Networking::NetworkReadStreamResponse response = request->execute();
00087     if (callback)
00088         (*callback)(response);
00089     return response.request; // no leak here, response.request == request
00090 }
00091 
00092 Networking::Request *DropboxStorage::createDirectory(Common::String path, BoolCallback callback, Networking::ErrorCallback errorCallback) {
00093     if (!errorCallback)
00094         errorCallback = getErrorPrintingCallback();
00095     return addRequest(new DropboxCreateDirectoryRequest(_token, path, callback, errorCallback));
00096 }
00097 
00098 Networking::Request *DropboxStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) {
00099     if (!errorCallback)
00100         errorCallback = getErrorPrintingCallback();
00101     return addRequest(new DropboxInfoRequest(_token, callback, errorCallback));
00102 }
00103 
00104 Common::String DropboxStorage::savesDirectoryPath() { return "/saves/"; }
00105 
00106 DropboxStorage *DropboxStorage::loadFromConfig(Common::String keyPrefix) {
00107     if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
00108         warning("DropboxStorage: no access_token found");
00109         return nullptr;
00110     }
00111 
00112     Common::String accessToken = ConfMan.get(keyPrefix + "access_token", ConfMan.kCloudDomain); 
00113     return new DropboxStorage(accessToken, loadIsEnabledFlag(keyPrefix));
00114 }
00115 
00116 void DropboxStorage::removeFromConfig(Common::String keyPrefix) {
00117     ConfMan.removeKey(keyPrefix + "access_token", ConfMan.kCloudDomain);
00118     removeIsEnabledFlag(keyPrefix);
00119 }
00120 
00121 } // End of namespace Dropbox
00122 } // End of namespace Cloud


Generated on Sat Feb 15 2020 05:00:42 for ResidualVM by doxygen 1.7.1
curved edge   curved edge