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

idcreatedirectoryrequest.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/id/idcreatedirectoryrequest.h"
00024 #include "backends/cloud/id/idstorage.h"
00025 #include "common/debug.h"
00026 
00027 namespace Cloud {
00028 namespace Id {
00029 
00030 IdCreateDirectoryRequest::IdCreateDirectoryRequest(IdStorage *storage, Common::String parentPath, Common::String directoryName, Storage::BoolCallback cb, Networking::ErrorCallback ecb):
00031     Networking::Request(nullptr, ecb),
00032     _requestedParentPath(parentPath), _requestedDirectoryName(directoryName), _storage(storage), _boolCallback(cb),
00033     _workingRequest(nullptr), _ignoreCallback(false) {
00034     start();
00035 }
00036 
00037 IdCreateDirectoryRequest::~IdCreateDirectoryRequest() {
00038     _ignoreCallback = true;
00039     if (_workingRequest)
00040         _workingRequest->finish();
00041     delete _boolCallback;
00042 }
00043 
00044 void IdCreateDirectoryRequest::start() {
00045     //cleanup
00046     _ignoreCallback = true;
00047     if (_workingRequest)
00048         _workingRequest->finish();
00049     _workingRequest = nullptr;
00050     _ignoreCallback = false;
00051 
00052     //the only exception when we create parent folder - is when it's ScummVM/ base folder
00053     Common::String prefix = _requestedParentPath;
00054     if (prefix.size() > 7)
00055         prefix.erase(7);
00056     if (prefix.equalsIgnoreCase("ScummVM")) {
00057         Storage::BoolCallback callback = new Common::Callback<IdCreateDirectoryRequest, Storage::BoolResponse>(this, &IdCreateDirectoryRequest::createdBaseDirectoryCallback);
00058         Networking::ErrorCallback failureCallback = new Common::Callback<IdCreateDirectoryRequest, Networking::ErrorResponse>(this, &IdCreateDirectoryRequest::createdBaseDirectoryErrorCallback);
00059         _workingRequest = _storage->createDirectory("ScummVM", callback, failureCallback);
00060         return;
00061     }
00062 
00063     resolveId();
00064 }
00065 
00066 void IdCreateDirectoryRequest::createdBaseDirectoryCallback(Storage::BoolResponse response) {
00067     _workingRequest = nullptr;
00068     if (_ignoreCallback)
00069         return;
00070     if (response.request)
00071         _date = response.request->date();
00072     resolveId();
00073 }
00074 
00075 void IdCreateDirectoryRequest::createdBaseDirectoryErrorCallback(Networking::ErrorResponse error) {
00076     _workingRequest = nullptr;
00077     if (_ignoreCallback)
00078         return;
00079     if (error.request)
00080         _date = error.request->date();
00081     finishError(error);
00082 }
00083 
00084 void IdCreateDirectoryRequest::resolveId() {
00085     //check whether such folder already exists
00086     Storage::UploadCallback innerCallback = new Common::Callback<IdCreateDirectoryRequest, Storage::UploadResponse>(this, &IdCreateDirectoryRequest::idResolvedCallback);
00087     Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdCreateDirectoryRequest, Networking::ErrorResponse>(this, &IdCreateDirectoryRequest::idResolveFailedCallback);
00088     Common::String path = _requestedParentPath;
00089     if (_requestedParentPath != "")
00090         path += "/";
00091     path += _requestedDirectoryName;
00092     _workingRequest = _storage->resolveFileId(path, innerCallback, innerErrorCallback);
00093 }
00094 
00095 void IdCreateDirectoryRequest::idResolvedCallback(Storage::UploadResponse response) {
00096     _workingRequest = nullptr;
00097     if (_ignoreCallback)
00098         return;
00099     if (response.request)
00100         _date = response.request->date();
00101 
00102     //resolved => folder already exists
00103     finishCreation(false);
00104 }
00105 
00106 void IdCreateDirectoryRequest::idResolveFailedCallback(Networking::ErrorResponse error) {
00107     _workingRequest = nullptr;
00108     if (_ignoreCallback)
00109         return;
00110     if (error.request)
00111         _date = error.request->date();
00112 
00113     //not resolved => folder not exists
00114     if (error.response.contains("no such file found in its parent directory")) {
00115         //parent's id after the '\n'
00116         Common::String parentId = error.response;
00117         for (uint32 i = 0; i < parentId.size(); ++i)
00118             if (parentId[i] == '\n') {
00119                 parentId.erase(0, i + 1);
00120                 break;
00121             }
00122 
00123         Storage::BoolCallback callback = new Common::Callback<IdCreateDirectoryRequest, Storage::BoolResponse>(this, &IdCreateDirectoryRequest::createdDirectoryCallback);
00124         Networking::ErrorCallback failureCallback = new Common::Callback<IdCreateDirectoryRequest, Networking::ErrorResponse>(this, &IdCreateDirectoryRequest::createdDirectoryErrorCallback);
00125         _workingRequest = _storage->createDirectoryWithParentId(parentId, _requestedDirectoryName, callback, failureCallback);
00126         return;
00127     }
00128 
00129     finishError(error);
00130 }
00131 
00132 void IdCreateDirectoryRequest::createdDirectoryCallback(Storage::BoolResponse response) {
00133     _workingRequest = nullptr;
00134     if (_ignoreCallback)
00135         return;
00136     if (response.request)
00137         _date = response.request->date();
00138     finishCreation(response.value);
00139 }
00140 
00141 void IdCreateDirectoryRequest::createdDirectoryErrorCallback(Networking::ErrorResponse error) {
00142     _workingRequest = nullptr;
00143     if (_ignoreCallback)
00144         return;
00145     if (error.request)
00146         _date = error.request->date();
00147     finishError(error);
00148 }
00149 
00150 void IdCreateDirectoryRequest::handle() {}
00151 
00152 void IdCreateDirectoryRequest::restart() { start(); }
00153 
00154 Common::String IdCreateDirectoryRequest::date() const { return _date; }
00155 
00156 void IdCreateDirectoryRequest::finishCreation(bool success) {
00157     Request::finishSuccess();
00158     if (_boolCallback)
00159         (*_boolCallback)(Storage::BoolResponse(this, success));
00160 }
00161 
00162 } // End of namespace Id
00163 } // End of namespace Cloud


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