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

dropboxlistdirectoryrequest.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 #include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h"
00024 #include "backends/cloud/iso8601.h"
00025 #include "backends/cloud/storage.h"
00026 #include "backends/networking/curl/connectionmanager.h"
00027 #include "backends/networking/curl/curljsonrequest.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/json.h"
00030 #include "common/debug.h"
00031 
00032 namespace Cloud {
00033 namespace Dropbox {
00034 
00035 #define DROPBOX_API_LIST_FOLDER "https://api.dropboxapi.com/2/files/list_folder"
00036 #define DROPBOX_API_LIST_FOLDER_CONTINUE "https://api.dropboxapi.com/2/files/list_folder/continue"
00037 
00038 DropboxListDirectoryRequest::DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive):
00039     Networking::Request(nullptr, ecb), _requestedPath(path), _requestedRecursive(recursive), _listDirectoryCallback(cb),
00040     _token(token), _workingRequest(nullptr), _ignoreCallback(false) {
00041     start();
00042 }
00043 
00044 DropboxListDirectoryRequest::~DropboxListDirectoryRequest() {
00045     _ignoreCallback = true;
00046     if (_workingRequest)
00047         _workingRequest->finish();
00048     delete _listDirectoryCallback;
00049 }
00050 
00051 void DropboxListDirectoryRequest::start() {
00052     _ignoreCallback = true;
00053     if (_workingRequest)
00054         _workingRequest->finish();
00055     _files.clear();
00056     _ignoreCallback = false;
00057 
00058     Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(this, &DropboxListDirectoryRequest::responseCallback);
00059     Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::ErrorResponse>(this, &DropboxListDirectoryRequest::errorCallback);
00060     Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, DROPBOX_API_LIST_FOLDER);
00061     request->addHeader("Authorization: Bearer " + _token);
00062     request->addHeader("Content-Type: application/json");
00063 
00064     Common::JSONObject jsonRequestParameters;
00065     jsonRequestParameters.setVal("path", new Common::JSONValue(_requestedPath));
00066     jsonRequestParameters.setVal("recursive", new Common::JSONValue(_requestedRecursive));
00067     jsonRequestParameters.setVal("include_media_info", new Common::JSONValue(false));
00068     jsonRequestParameters.setVal("include_deleted", new Common::JSONValue(false));
00069 
00070     Common::JSONValue value(jsonRequestParameters);
00071     request->addPostField(Common::JSON::stringify(&value));
00072 
00073     _workingRequest = ConnMan.addRequest(request);
00074 }
00075 
00076 void DropboxListDirectoryRequest::responseCallback(Networking::JsonResponse response) {
00077     _workingRequest = nullptr;
00078 
00079     if (_ignoreCallback) {
00080         delete response.value;
00081         return;
00082     }
00083 
00084     if (response.request)
00085         _date = response.request->date();
00086 
00087     Networking::ErrorResponse error(this);
00088     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00089     if (rq && rq->getNetworkReadStream())
00090         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00091 
00092     Common::JSONValue *json = response.value;
00093     if (json == nullptr) {
00094         error.response = "Failed to parse JSON, null passed!";
00095         finishError(error);
00096         return;
00097     }
00098 
00099     if (!json->isObject()) {
00100         error.response = "Passed JSON is not an object!";
00101         finishError(error);
00102         delete json;
00103         return;
00104     }
00105 
00106     Common::JSONObject responseObject = json->asObject();
00107 
00108     if (responseObject.contains("error") || responseObject.contains("error_summary")) {
00109         if (responseObject.contains("error_summary") && responseObject.getVal("error_summary")->isString()) {
00110             warning("Dropbox returned error: %s", responseObject.getVal("error_summary")->asString().c_str());
00111         }
00112         error.failed = true;
00113         error.response = json->stringify();
00114         finishError(error);
00115         delete json;
00116         return;
00117     }
00118 
00119     //check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults
00120     if (responseObject.contains("entries")) {
00121         if (!responseObject.getVal("entries")->isArray()) {
00122             error.response = Common::String::format(
00123                 "\"entries\" found, but that's not an array!\n%s",
00124                 responseObject.getVal("entries")->stringify(true).c_str()
00125                 );
00126             finishError(error);
00127             delete json;
00128             return;
00129         }
00130 
00131         Common::JSONArray items = responseObject.getVal("entries")->asArray();
00132         for (uint32 i = 0; i < items.size(); ++i) {
00133             if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "DropboxListDirectoryRequest"))
00134                 continue;
00135 
00136             Common::JSONObject item = items[i]->asObject();
00137 
00138             if (!Networking::CurlJsonRequest::jsonContainsString(item, "path_lower", "DropboxListDirectoryRequest"))
00139                 continue;
00140             if (!Networking::CurlJsonRequest::jsonContainsString(item, ".tag", "DropboxListDirectoryRequest"))
00141                 continue;
00142 
00143             Common::String path = item.getVal("path_lower")->asString();
00144             bool isDirectory = (item.getVal(".tag")->asString() == "folder");
00145             uint32 size = 0, timestamp = 0;
00146             if (!isDirectory) {
00147                 if (!Networking::CurlJsonRequest::jsonContainsString(item, "server_modified", "DropboxListDirectoryRequest"))
00148                     continue;
00149                 if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(item, "size", "DropboxListDirectoryRequest"))
00150                     continue;
00151 
00152                 size = item.getVal("size")->asIntegerNumber();
00153                 timestamp = ISO8601::convertToTimestamp(item.getVal("server_modified")->asString());
00154             }
00155             _files.push_back(StorageFile(path, size, timestamp, isDirectory));
00156         }
00157     }
00158 
00159     bool hasMore = false;
00160     if (responseObject.contains("has_more")) {
00161         if (!responseObject.getVal("has_more")->isBool()) {
00162             warning("DropboxListDirectoryRequest: \"has_more\" is not a boolean");
00163             debug(9, "%s", responseObject.getVal("has_more")->stringify(true).c_str());
00164             error.response = "\"has_more\" is not a boolean!";
00165             finishError(error);
00166             delete json;
00167             return;
00168         }
00169 
00170         hasMore = responseObject.getVal("has_more")->asBool();
00171     }
00172 
00173     if (hasMore) {
00174         if (!Networking::CurlJsonRequest::jsonContainsString(responseObject, "cursor", "DropboxListDirectoryRequest")) {
00175             error.response = "\"has_more\" found, but \"cursor\" is not (or it's not a string)!";
00176             finishError(error);
00177             delete json;
00178             return;
00179         }
00180 
00181         Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(this, &DropboxListDirectoryRequest::responseCallback);
00182         Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::ErrorResponse>(this, &DropboxListDirectoryRequest::errorCallback);
00183         Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, DROPBOX_API_LIST_FOLDER_CONTINUE);
00184         request->addHeader("Authorization: Bearer " + _token);
00185         request->addHeader("Content-Type: application/json");
00186 
00187         Common::JSONObject jsonRequestParameters;
00188         jsonRequestParameters.setVal("cursor", new Common::JSONValue(responseObject.getVal("cursor")->asString()));
00189 
00190         Common::JSONValue value(jsonRequestParameters);
00191         request->addPostField(Common::JSON::stringify(&value));
00192 
00193         _workingRequest = ConnMan.addRequest(request);
00194     } else {
00195         finishListing(_files);
00196     }
00197 
00198     delete json;
00199 }
00200 
00201 void DropboxListDirectoryRequest::errorCallback(Networking::ErrorResponse error) {
00202     _workingRequest = nullptr;
00203     if (_ignoreCallback)
00204         return;
00205     if (error.request)
00206         _date = error.request->date();
00207     finishError(error);
00208 }
00209 
00210 void DropboxListDirectoryRequest::handle() {}
00211 
00212 void DropboxListDirectoryRequest::restart() { start(); }
00213 
00214 Common::String DropboxListDirectoryRequest::date() const { return _date; }
00215 
00216 void DropboxListDirectoryRequest::finishListing(Common::Array<StorageFile> &files) {
00217     Request::finishSuccess();
00218     if (_listDirectoryCallback)
00219         (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
00220 }
00221 
00222 } // End of namespace Dropbox
00223 } // End of namespace Cloud


Generated on Sat Feb 16 2019 05:00:49 for ResidualVM by doxygen 1.7.1
curved edge   curved edge