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


Generated on Sat Dec 14 2019 05:00:26 for ResidualVM by doxygen 1.7.1
curved edge   curved edge