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

onedrivelistdirectoryrequest.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/onedrive/onedrivelistdirectoryrequest.h"
00024 #include "backends/cloud/onedrive/onedrivestorage.h"
00025 #include "backends/cloud/onedrive/onedrivetokenrefresher.h"
00026 #include "backends/cloud/iso8601.h"
00027 #include "backends/networking/curl/connectionmanager.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/json.h"
00030 
00031 namespace Cloud {
00032 namespace OneDrive {
00033 
00034 #define ONEDRIVE_API_SPECIAL_APPROOT_CHILDREN "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/children"
00035 
00036 OneDriveListDirectoryRequest::OneDriveListDirectoryRequest(OneDriveStorage *storage, Common::String path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive):
00037     Networking::Request(nullptr, ecb),
00038     _requestedPath(path), _requestedRecursive(recursive), _storage(storage), _listDirectoryCallback(cb),
00039     _workingRequest(nullptr), _ignoreCallback(false) {
00040     start();
00041 }
00042 
00043 OneDriveListDirectoryRequest::~OneDriveListDirectoryRequest() {
00044     _ignoreCallback = true;
00045     if (_workingRequest)
00046         _workingRequest->finish();
00047     delete _listDirectoryCallback;
00048 }
00049 
00050 void OneDriveListDirectoryRequest::start() {
00051     //cleanup
00052     _ignoreCallback = true;
00053     if (_workingRequest)
00054         _workingRequest->finish();
00055     _workingRequest = nullptr;
00056     _files.clear();
00057     _directoriesQueue.clear();
00058     _currentDirectory = "";
00059     _ignoreCallback = false;
00060 
00061     _directoriesQueue.push_back(_requestedPath);
00062     listNextDirectory();
00063 }
00064 
00065 void OneDriveListDirectoryRequest::listNextDirectory() {
00066     if (_directoriesQueue.empty()) {
00067         finishListing(_files);
00068         return;
00069     }
00070 
00071     _currentDirectory = _directoriesQueue.back();
00072     _directoriesQueue.pop_back();
00073 
00074     if (_currentDirectory != "" && _currentDirectory.lastChar() != '/' && _currentDirectory.lastChar() != '\\')
00075         _currentDirectory += '/';
00076 
00077     Common::String dir = _currentDirectory;
00078     dir.deleteLastChar();
00079     Common::String url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_CHILDREN, ConnMan.urlEncode(dir).c_str());
00080     makeRequest(url);
00081 }
00082 
00083 void OneDriveListDirectoryRequest::makeRequest(Common::String url) {
00084     Networking::JsonCallback callback = new Common::Callback<OneDriveListDirectoryRequest, Networking::JsonResponse>(this, &OneDriveListDirectoryRequest::listedDirectoryCallback);
00085     Networking::ErrorCallback failureCallback = new Common::Callback<OneDriveListDirectoryRequest, Networking::ErrorResponse>(this, &OneDriveListDirectoryRequest::listedDirectoryErrorCallback);
00086     Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(_storage, callback, failureCallback, url.c_str());
00087     request->addHeader("Authorization: Bearer " + _storage->accessToken());
00088     _workingRequest = ConnMan.addRequest(request);
00089 }
00090 
00091 void OneDriveListDirectoryRequest::listedDirectoryCallback(Networking::JsonResponse response) {
00092     _workingRequest = nullptr;
00093     Common::JSONValue *json = response.value;
00094 
00095     if (_ignoreCallback) {
00096         delete json;
00097         return;
00098     }
00099 
00100     if (response.request)
00101         _date = response.request->date();
00102 
00103     Networking::ErrorResponse error(this);
00104     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00105     if (rq && rq->getNetworkReadStream())
00106         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00107 
00108     if (json == nullptr) {
00109         error.response = "Failed to parse JSON, null passed!";
00110         finishError(error);
00111         return;
00112     }
00113 
00114     if (!json->isObject()) {
00115         error.response = "Passed JSON is not an object!";
00116         finishError(error);
00117         delete json;
00118         return;
00119     }
00120 
00121     Common::JSONObject object = json->asObject();
00122 
00123     //check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults
00124     if (!Networking::CurlJsonRequest::jsonContainsArray(object, "value", "OneDriveListDirectoryRequest")) {
00125         error.response = "\"value\" not found or that's not an array!";
00126         finishError(error);
00127         delete json;
00128         return;
00129     }
00130 
00131     Common::JSONArray items = object.getVal("value")->asArray();
00132     for (uint32 i = 0; i < items.size(); ++i) {
00133         if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "OneDriveListDirectoryRequest")) continue;
00134 
00135         Common::JSONObject item = items[i]->asObject();
00136 
00137         if (!Networking::CurlJsonRequest::jsonContainsAttribute(item, "folder", "OneDriveListDirectoryRequest", true)) continue;
00138         if (!Networking::CurlJsonRequest::jsonContainsString(item, "name", "OneDriveListDirectoryRequest")) continue;
00139         if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(item, "size", "OneDriveListDirectoryRequest")) continue;
00140         if (!Networking::CurlJsonRequest::jsonContainsString(item, "lastModifiedDateTime", "OneDriveListDirectoryRequest")) continue;
00141 
00142         Common::String path = _currentDirectory + item.getVal("name")->asString();
00143         bool isDirectory = item.contains("folder");
00144         uint32 size = item.getVal("size")->asIntegerNumber();
00145         uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("lastModifiedDateTime")->asString());
00146 
00147         StorageFile file(path, size, timestamp, isDirectory);
00148         _files.push_back(file);
00149         if (_requestedRecursive && file.isDirectory()) {
00150             _directoriesQueue.push_back(file.path());
00151         }
00152     }
00153 
00154     bool hasMore = object.contains("@odata.nextLink");
00155     if (hasMore) {
00156         if (!Networking::CurlJsonRequest::jsonContainsString(object, "@odata.nextLink", "OneDriveListDirectoryRequest")) {
00157             error.response = "\"@odata.nextLink\" is not a string!";
00158             finishError(error);
00159             delete json;
00160             return;
00161         }
00162 
00163         makeRequest(object.getVal("@odata.nextLink")->asString());
00164     } else {
00165         listNextDirectory();
00166     }
00167 
00168     delete json;
00169 }
00170 
00171 void OneDriveListDirectoryRequest::listedDirectoryErrorCallback(Networking::ErrorResponse error) {
00172     _workingRequest = nullptr;
00173     if (_ignoreCallback)
00174         return;
00175     if (error.request)
00176         _date = error.request->date();
00177     finishError(error);
00178 }
00179 
00180 void OneDriveListDirectoryRequest::handle() {}
00181 
00182 void OneDriveListDirectoryRequest::restart() { start(); }
00183 
00184 Common::String OneDriveListDirectoryRequest::date() const { return _date; }
00185 
00186 void OneDriveListDirectoryRequest::finishListing(Common::Array<StorageFile> &files) {
00187     Request::finishSuccess();
00188     if (_listDirectoryCallback)
00189         (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
00190 }
00191 
00192 } // End of namespace OneDrive
00193 } // End of namespace Cloud


Generated on Sat May 25 2019 05:00:50 for ResidualVM by doxygen 1.7.1
curved edge   curved edge