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

folderdownloadrequest.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/folderdownloadrequest.h"
00024 #include "backends/cloud/downloadrequest.h"
00025 #include "backends/cloud/id/iddownloadrequest.h"
00026 #include "common/debug.h"
00027 #include "gui/downloaddialog.h"
00028 #include <backends/networking/curl/connectionmanager.h>
00029 #include "cloudmanager.h"
00030 
00031 namespace Cloud {
00032 
00033 FolderDownloadRequest::FolderDownloadRequest(Storage *storage, Storage::FileArrayCallback callback, Networking::ErrorCallback ecb, Common::String remoteDirectoryPath, Common::String localDirectoryPath, bool recursive):
00034     Request(nullptr, ecb), CommandSender(nullptr), _storage(storage), _fileArrayCallback(callback),
00035     _remoteDirectoryPath(remoteDirectoryPath), _localDirectoryPath(localDirectoryPath), _recursive(recursive),
00036     _workingRequest(nullptr), _ignoreCallback(false), _totalFiles(0) {
00037     start();
00038 }
00039 
00040 FolderDownloadRequest::~FolderDownloadRequest() {
00041     sendCommand(GUI::kDownloadEndedCmd, 0);
00042     _ignoreCallback = true;
00043     if (_workingRequest)
00044         _workingRequest->finish();
00045     delete _fileArrayCallback;
00046 }
00047 
00048 void FolderDownloadRequest::start() {
00049     //cleanup
00050     _ignoreCallback = true;
00051     if (_workingRequest)
00052         _workingRequest->finish();
00053     _currentFile = StorageFile();
00054     _pendingFiles.clear();
00055     _failedFiles.clear();
00056     _ignoreCallback = false;
00057     _totalFiles = 0;
00058     _downloadedBytes = _totalBytes = _wasDownloadedBytes = _currentDownloadSpeed = 0;
00059 
00060     //list directory first
00061     _workingRequest = _storage->listDirectory(
00062         _remoteDirectoryPath,
00063         new Common::Callback<FolderDownloadRequest, Storage::ListDirectoryResponse>(this, &FolderDownloadRequest::directoryListedCallback),
00064         new Common::Callback<FolderDownloadRequest, Networking::ErrorResponse>(this, &FolderDownloadRequest::directoryListedErrorCallback),
00065         _recursive
00066     );
00067 }
00068 
00069 void FolderDownloadRequest::directoryListedCallback(Storage::ListDirectoryResponse response) {
00070     _workingRequest = nullptr;
00071     if (_ignoreCallback)
00072         return;
00073     _pendingFiles = response.value;
00074 
00075     // remove all directories
00076     // non-empty directories would be created by DumpFile, and empty ones are just ignored
00077     // also skip all hidden files (with names starting with '.') or with other names that are forbidden to sync in CloudManager
00078     for (Common::Array<StorageFile>::iterator i = _pendingFiles.begin(); i != _pendingFiles.end();)
00079         if (i->isDirectory() || !CloudMan.canSyncFilename(i->name()))
00080             _pendingFiles.erase(i);
00081         else {
00082             _totalBytes += i->size();
00083             ++i;
00084         }
00085 
00086     _totalFiles = _pendingFiles.size();
00087     downloadNextFile();
00088 }
00089 
00090 void FolderDownloadRequest::directoryListedErrorCallback(Networking::ErrorResponse error) {
00091     _workingRequest = nullptr;
00092     if (_ignoreCallback)
00093         return;
00094     finishError(error);
00095 }
00096 
00097 void FolderDownloadRequest::fileDownloadedCallback(Storage::BoolResponse response) {
00098     _workingRequest = nullptr;
00099     if (_ignoreCallback)
00100         return;
00101     if (!response.value) _failedFiles.push_back(_currentFile);
00102     _downloadedBytes += _currentFile.size();
00103     downloadNextFile();
00104 }
00105 
00106 void FolderDownloadRequest::fileDownloadedErrorCallback(Networking::ErrorResponse error) {
00107     _workingRequest = nullptr;
00108     if (_ignoreCallback)
00109         return;
00110     fileDownloadedCallback(Storage::BoolResponse(error.request, false));
00111 }
00112 
00113 void FolderDownloadRequest::downloadNextFile() {
00114     do {
00115         if (_pendingFiles.empty()) {
00116             sendCommand(GUI::kDownloadEndedCmd, 0);
00117             finishDownload(_failedFiles);
00118             return;
00119         }
00120 
00121         _currentFile = _pendingFiles.back();
00122         _pendingFiles.pop_back();
00123     } while (_currentFile.isDirectory()); // directories are actually removed earlier, in the directoryListedCallback()
00124 
00125     sendCommand(GUI::kDownloadProgressCmd, (int)(getProgress() * 100));
00126 
00127     Common::String remotePath = _currentFile.path();
00128     Common::String localPath = remotePath;
00129     if (_remoteDirectoryPath == "" || remotePath.hasPrefix(_remoteDirectoryPath)) {
00130         localPath.erase(0, _remoteDirectoryPath.size());
00131         if (_remoteDirectoryPath != "" && (_remoteDirectoryPath.lastChar() != '/' && _remoteDirectoryPath.lastChar() != '\\'))
00132             localPath.erase(0, 1);
00133     } else {
00134         warning("FolderDownloadRequest: Can't process the following paths:");
00135         warning("remote directory: %s", _remoteDirectoryPath.c_str());
00136         warning("remote file under that directory: %s", remotePath.c_str());
00137     }
00138     if (_localDirectoryPath != "") {
00139         if (_localDirectoryPath.lastChar() == '/' || _localDirectoryPath.lastChar() == '\\')
00140             localPath = _localDirectoryPath + localPath;
00141         else
00142             localPath = _localDirectoryPath + "/" + localPath;
00143     }
00144     debug(9, "FolderDownloadRequest: %s -> %s", remotePath.c_str(), localPath.c_str());
00145     _workingRequest = _storage->downloadById(
00146         _currentFile.id(), localPath,
00147         new Common::Callback<FolderDownloadRequest, Storage::BoolResponse>(this, &FolderDownloadRequest::fileDownloadedCallback),
00148         new Common::Callback<FolderDownloadRequest, Networking::ErrorResponse>(this, &FolderDownloadRequest::fileDownloadedErrorCallback)
00149     );
00150 }
00151 
00152 void FolderDownloadRequest::handle() {
00153     uint32 microsecondsPassed = Networking::ConnectionManager::getCloudRequestsPeriodInMicroseconds();
00154     uint64 currentDownloadedBytes = getDownloadedBytes();
00155     uint64 downloadedThisPeriod = currentDownloadedBytes - _wasDownloadedBytes;
00156     _currentDownloadSpeed = downloadedThisPeriod * (1000000L / microsecondsPassed);
00157     _wasDownloadedBytes = currentDownloadedBytes;
00158 }
00159 
00160 void FolderDownloadRequest::restart() { start(); }
00161 
00162 void FolderDownloadRequest::finishDownload(Common::Array<StorageFile> &files) {
00163     Request::finishSuccess();
00164     if (_fileArrayCallback)
00165         (*_fileArrayCallback)(Storage::FileArrayResponse(this, files));
00166 }
00167 
00168 double FolderDownloadRequest::getProgress() const {
00169     if (_totalFiles == 0 || _totalBytes == 0)
00170         return 0;
00171     return (double)getDownloadedBytes() / (double)getTotalBytesToDownload();
00172 }
00173 
00174 uint64 FolderDownloadRequest::getDownloadedBytes() const {
00175     if (_totalFiles == 0)
00176         return 0;
00177 
00178     double currentFileProgress = 0;
00179     DownloadRequest *downloadRequest = dynamic_cast<DownloadRequest *>(_workingRequest);
00180     if (downloadRequest != nullptr) {
00181         currentFileProgress = downloadRequest->getProgress();
00182     } else {
00183         Id::IdDownloadRequest *idDownloadRequest = dynamic_cast<Id::IdDownloadRequest *>(_workingRequest);
00184         if (idDownloadRequest != nullptr)
00185             currentFileProgress = idDownloadRequest->getProgress();
00186     }
00187 
00188     return _downloadedBytes + (uint64)(currentFileProgress * _currentFile.size());
00189 }
00190 
00191 uint64 FolderDownloadRequest::getTotalBytesToDownload() const {
00192     return _totalBytes;
00193 }
00194 
00195 uint64 FolderDownloadRequest::getDownloadSpeed() const {
00196     return _currentDownloadSpeed;
00197 }
00198 
00199 } // End of namespace Cloud


Generated on Sat Aug 8 2020 05:01:03 for ResidualVM by doxygen 1.7.1
curved edge   curved edge