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

onedrivecreatedirectoryrequest.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/onedrivecreatedirectoryrequest.h"
00024 #include "backends/cloud/onedrive/onedrivestorage.h"
00025 #include "backends/cloud/onedrive/onedrivetokenrefresher.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 
00031 namespace Cloud {
00032 namespace OneDrive {
00033 
00034 #define ONEDRIVE_API_SPECIAL_APPROOT "https://api.onedrive.com/v1.0/drive/special/approot"
00035 
00036 OneDriveCreateDirectoryRequest::OneDriveCreateDirectoryRequest(OneDriveStorage *storage, Common::String path, Storage::BoolCallback cb, Networking::ErrorCallback ecb):
00037     Networking::Request(nullptr, ecb), _storage(storage), _path(path), _boolCallback(cb),
00038     _workingRequest(nullptr), _ignoreCallback(false) {
00039     start();
00040 }
00041 
00042 OneDriveCreateDirectoryRequest::~OneDriveCreateDirectoryRequest() {
00043     _ignoreCallback = true;
00044     if (_workingRequest)
00045         _workingRequest->finish();
00046     delete _boolCallback;
00047 }
00048 
00049 void OneDriveCreateDirectoryRequest::start() {
00050     _ignoreCallback = true;
00051     if (_workingRequest)
00052         _workingRequest->finish();
00053     _ignoreCallback = false;
00054 
00055     Common::String name = _path, parent = _path;
00056     if (name.size() != 0) {
00057         uint32 i = name.size() - 1;
00058         while (true) {
00059             parent.deleteLastChar();
00060             if (name[i] == '/' || name[i] == '\\') {
00061                 name.erase(0, i + 1);
00062                 break;
00063             }
00064             if (i == 0)
00065                 break;
00066             --i;
00067         }
00068     }
00069 
00070     Common::String url = ONEDRIVE_API_SPECIAL_APPROOT;
00071     if (parent != "")
00072         url += ":/" + ConnMan.urlEncode(parent) + ":";
00073     url += "/children";
00074     Networking::JsonCallback innerCallback = new Common::Callback<OneDriveCreateDirectoryRequest, Networking::JsonResponse>(this, &OneDriveCreateDirectoryRequest::responseCallback);
00075     Networking::ErrorCallback errorResponseCallback = new Common::Callback<OneDriveCreateDirectoryRequest, Networking::ErrorResponse>(this, &OneDriveCreateDirectoryRequest::errorCallback);
00076     Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(_storage, innerCallback, errorResponseCallback, url.c_str());
00077     request->addHeader("Authorization: Bearer " + _storage->accessToken());
00078     request->addHeader("Content-Type: application/json");
00079 
00080     Common::JSONObject jsonRequestParameters;
00081     jsonRequestParameters.setVal("name", new Common::JSONValue(name));
00082     jsonRequestParameters.setVal("folder", new Common::JSONValue(Common::JSONObject()));
00083     Common::JSONValue value(jsonRequestParameters);
00084     request->addPostField(Common::JSON::stringify(&value));
00085 
00086     _workingRequest = ConnMan.addRequest(request);
00087 }
00088 
00089 void OneDriveCreateDirectoryRequest::responseCallback(Networking::JsonResponse response) {
00090     Common::JSONValue *json = response.value;
00091     _workingRequest = nullptr;
00092     if (_ignoreCallback) {
00093         delete json;
00094         return;
00095     }
00096     if (response.request)
00097         _date = response.request->date();
00098 
00099     Networking::ErrorResponse error(this);
00100     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00101     if (rq && rq->getNetworkReadStream())
00102         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00103 
00104     if (json == nullptr) {
00105         error.response = "Failed to parse JSON, null passed!";
00106         finishError(error);
00107         return;
00108     }
00109 
00110     if (!json->isObject()) {
00111         error.response = "Passed JSON is not an object!";
00112         finishError(error);
00113         delete json;
00114         return;
00115     }
00116 
00117     Common::JSONObject info = json->asObject();
00118     if (info.contains("id")) {
00119         finishCreation(true);
00120     } else {
00121         error.response = json->stringify(true);
00122         finishError(error);
00123     }
00124 
00125     delete json;
00126 }
00127 
00128 void OneDriveCreateDirectoryRequest::errorCallback(Networking::ErrorResponse error) {
00129     _workingRequest = nullptr;
00130     if (_ignoreCallback)
00131         return;
00132     if (error.request)
00133         _date = error.request->date();
00134     finishError(error);
00135 }
00136 
00137 void OneDriveCreateDirectoryRequest::handle() {}
00138 
00139 void OneDriveCreateDirectoryRequest::restart() { start(); }
00140 
00141 Common::String OneDriveCreateDirectoryRequest::date() const { return _date; }
00142 
00143 void OneDriveCreateDirectoryRequest::finishCreation(bool success) {
00144     Request::finishSuccess();
00145     if (_boolCallback)
00146         (*_boolCallback)(Storage::BoolResponse(this, success));
00147 }
00148 
00149 } // End of namespace OneDrive
00150 } // End of namespace Cloud


Generated on Sat Dec 7 2019 05:00:24 for ResidualVM by doxygen 1.7.1
curved edge   curved edge