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

idresolveidrequest.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/idresolveidrequest.h"
00024 #include "backends/cloud/id/idstorage.h"
00025 
00026 namespace Cloud {
00027 namespace Id {
00028 
00029 IdResolveIdRequest::IdResolveIdRequest(IdStorage *storage, Common::String path, Storage::UploadCallback cb, Networking::ErrorCallback ecb, bool recursive):
00030     Networking::Request(nullptr, ecb),
00031     _requestedPath(path), _storage(storage), _uploadCallback(cb),
00032     _workingRequest(nullptr), _ignoreCallback(false) {
00033     start();
00034 }
00035 
00036 IdResolveIdRequest::~IdResolveIdRequest() {
00037     _ignoreCallback = true;
00038     if (_workingRequest)
00039         _workingRequest->finish();
00040     delete _uploadCallback;
00041 }
00042 
00043 void IdResolveIdRequest::start() {
00044     //cleanup
00045     _ignoreCallback = true;
00046     if (_workingRequest)
00047         _workingRequest->finish();
00048     _workingRequest = nullptr;
00049     _currentDirectory = "";
00050     _currentDirectoryId = _storage->getRootDirectoryId();
00051     _ignoreCallback = false;
00052 
00053     listNextDirectory(StorageFile(_currentDirectoryId, 0, 0, true));
00054 }
00055 
00056 void IdResolveIdRequest::listNextDirectory(StorageFile fileToReturn) {
00057     if (_currentDirectory.equalsIgnoreCase(_requestedPath)) {
00058         finishFile(fileToReturn);
00059         return;
00060     }
00061 
00062     Storage::FileArrayCallback callback = new Common::Callback<IdResolveIdRequest, Storage::FileArrayResponse>(this, &IdResolveIdRequest::listedDirectoryCallback);
00063     Networking::ErrorCallback failureCallback = new Common::Callback<IdResolveIdRequest, Networking::ErrorResponse>(this, &IdResolveIdRequest::listedDirectoryErrorCallback);
00064     _workingRequest = _storage->listDirectoryById(_currentDirectoryId, callback, failureCallback);
00065 }
00066 
00067 void IdResolveIdRequest::listedDirectoryCallback(Storage::FileArrayResponse response) {
00068     _workingRequest = nullptr;
00069     if (_ignoreCallback)
00070         return;
00071 
00072     Common::String currentLevelName = _requestedPath;
00074     if (_currentDirectory.size())
00075         currentLevelName.erase(0, _currentDirectory.size());
00076     if (currentLevelName.size() && (currentLevelName[0] == '/' || currentLevelName[0] == '\\'))
00077         currentLevelName.erase(0, 1);
00079     for (uint32 i = 0; i < currentLevelName.size(); ++i) {
00080         if (currentLevelName[i] == '/' || currentLevelName[i] == '\\') {
00081             currentLevelName.erase(i);
00083             break;
00084         }
00085     }
00086 
00087     Common::String path = _currentDirectory;
00088     if (path != "")
00089         path += "/";
00090     path += currentLevelName;
00091     bool lastLevel = (path.equalsIgnoreCase(_requestedPath));
00092 
00094 
00095     Common::Array<StorageFile> &files = response.value;
00096     bool found = false;
00097     for (uint32 i = 0; i < files.size(); ++i) {
00098         if ((files[i].isDirectory() || lastLevel) && files[i].name().equalsIgnoreCase(currentLevelName)) {
00099             if (_currentDirectory != "")
00100                 _currentDirectory += "/";
00101             _currentDirectory += files[i].name();
00102             _currentDirectoryId = files[i].id();
00104             listNextDirectory(files[i]);
00105             found = true;
00106             break;
00107         }
00108     }
00109 
00110     if (!found) {
00111         if (lastLevel)
00112             finishError(Networking::ErrorResponse(this, false, true, Common::String("no such file found in its parent directory\n") + _currentDirectoryId, 404));
00113         else
00114             finishError(Networking::ErrorResponse(this, false, true, "subdirectory not found", 400));
00115     }
00116 }
00117 
00118 void IdResolveIdRequest::listedDirectoryErrorCallback(Networking::ErrorResponse error) {
00119     _workingRequest = nullptr;
00120     if (_ignoreCallback)
00121         return;
00122     finishError(error);
00123 }
00124 
00125 void IdResolveIdRequest::handle() {}
00126 
00127 void IdResolveIdRequest::restart() { start(); }
00128 
00129 void IdResolveIdRequest::finishFile(StorageFile file) {
00130     Request::finishSuccess();
00131     if (_uploadCallback)
00132         (*_uploadCallback)(Storage::UploadResponse(this, file));
00133 }
00134 
00135 } // End of namespace Id
00136 } // End of namespace Cloud


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