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

idlistdirectoryrequest.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/idlistdirectoryrequest.h"
00024 #include "backends/cloud/id/idstorage.h"
00025 
00026 namespace Cloud {
00027 namespace Id {
00028 
00029 IdListDirectoryRequest::IdListDirectoryRequest(IdStorage *storage, Common::String path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive):
00030     Networking::Request(nullptr, ecb),
00031     _requestedPath(path), _requestedRecursive(recursive), _storage(storage), _listDirectoryCallback(cb),
00032     _workingRequest(nullptr), _ignoreCallback(false) {
00033     start();
00034 }
00035 
00036 IdListDirectoryRequest::~IdListDirectoryRequest() {
00037     _ignoreCallback = true;
00038     if (_workingRequest)
00039         _workingRequest->finish();
00040     delete _listDirectoryCallback;
00041 }
00042 
00043 void IdListDirectoryRequest::start() {
00044     //cleanup
00045     _ignoreCallback = true;
00046     if (_workingRequest)
00047         _workingRequest->finish();
00048     _workingRequest = nullptr;
00049     _files.clear();
00050     _directoriesQueue.clear();
00051     _currentDirectory = StorageFile();
00052     _ignoreCallback = false;
00053 
00054     //find out that directory's id
00055     Storage::UploadCallback innerCallback = new Common::Callback<IdListDirectoryRequest, Storage::UploadResponse>(this, &IdListDirectoryRequest::idResolvedCallback);
00056     Networking::ErrorCallback innerErrorCallback = new Common::Callback<IdListDirectoryRequest, Networking::ErrorResponse>(this, &IdListDirectoryRequest::idResolveErrorCallback);
00057     _workingRequest = _storage->resolveFileId(_requestedPath, innerCallback, innerErrorCallback);
00058 }
00059 
00060 void IdListDirectoryRequest::idResolvedCallback(Storage::UploadResponse response) {
00061     _workingRequest = nullptr;
00062     if (_ignoreCallback)
00063         return;
00064     if (response.request)
00065         _date = response.request->date();
00066 
00067     StorageFile directory = response.value;
00068     directory.setPath(_requestedPath);
00069     _directoriesQueue.push_back(directory);
00070     listNextDirectory();
00071 }
00072 
00073 void IdListDirectoryRequest::idResolveErrorCallback(Networking::ErrorResponse error) {
00074     _workingRequest = nullptr;
00075     if (_ignoreCallback)
00076         return;
00077     if (error.request)
00078         _date = error.request->date();
00079     finishError(error);
00080 }
00081 
00082 void IdListDirectoryRequest::listNextDirectory() {
00083     if (_directoriesQueue.empty()) {
00084         finishListing(_files);
00085         return;
00086     }
00087 
00088     _currentDirectory = _directoriesQueue.back();
00089     _directoriesQueue.pop_back();
00090 
00091     Storage::FileArrayCallback callback = new Common::Callback<IdListDirectoryRequest, Storage::FileArrayResponse>(this, &IdListDirectoryRequest::listedDirectoryCallback);
00092     Networking::ErrorCallback failureCallback = new Common::Callback<IdListDirectoryRequest, Networking::ErrorResponse>(this, &IdListDirectoryRequest::listedDirectoryErrorCallback);
00093     _workingRequest = _storage->listDirectoryById(_currentDirectory.id(), callback, failureCallback);
00094 }
00095 
00096 void IdListDirectoryRequest::listedDirectoryCallback(Storage::FileArrayResponse response) {
00097     _workingRequest = nullptr;
00098     if (_ignoreCallback)
00099         return;
00100     if (response.request)
00101         _date = response.request->date();
00102 
00103     for (uint32 i = 0; i < response.value.size(); ++i) {
00104         StorageFile &file = response.value[i];
00105         Common::String path = _currentDirectory.path();
00106         if (path.size() && path.lastChar() != '/' && path.lastChar() != '\\')
00107             path += '/';
00108         path += file.name();
00109         file.setPath(path);
00110         _files.push_back(file);
00111         if (_requestedRecursive && file.isDirectory()) {
00112             _directoriesQueue.push_back(file);
00113         }
00114     }
00115 
00116     listNextDirectory();
00117 }
00118 
00119 void IdListDirectoryRequest::listedDirectoryErrorCallback(Networking::ErrorResponse error) {
00120     _workingRequest = nullptr;
00121     if (_ignoreCallback)
00122         return;
00123     if (error.request)
00124         _date = error.request->date();
00125     finishError(error);
00126 }
00127 
00128 void IdListDirectoryRequest::handle() {}
00129 
00130 void IdListDirectoryRequest::restart() { start(); }
00131 
00132 Common::String IdListDirectoryRequest::date() const { return _date; }
00133 
00134 void IdListDirectoryRequest::finishListing(Common::Array<StorageFile> &files) {
00135     Request::finishSuccess();
00136     if (_listDirectoryCallback)
00137         (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
00138 }
00139 
00140 } // End of namespace Id
00141 } // End of namespace Cloud


Generated on Sat Mar 23 2019 05:01:42 for ResidualVM by doxygen 1.7.1
curved edge   curved edge