1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "backends/cloud/storage.h"
#include "backends/cloud/downloadrequest.h"
#include "backends/cloud/folderdownloadrequest.h"
#include "backends/cloud/savessyncrequest.h"
#include "backends/networking/curl/connectionmanager.h"
#include "common/debug.h"
#include "common/file.h"
#include <common/translation.h>
#include "common/osd_message_queue.h"
namespace Cloud {
Storage::Storage():
_runningRequestsCount(0), _savesSyncRequest(nullptr), _syncRestartRequestsed(false),
_downloadFolderRequest(nullptr), _isEnabled(false) {}
Storage::~Storage() {}
bool Storage::isEnabled() const {
return _isEnabled;
}
void Storage::enable() {
_isEnabled = true;
}
Networking::ErrorCallback Storage::getErrorPrintingCallback() {
return new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::printErrorResponse);
}
void Storage::printErrorResponse(Networking::ErrorResponse error) {
debug(9, "Storage: error response (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode);
debug(9, "%s", error.response.c_str());
}
Networking::Request *Storage::addRequest(Networking::Request *request) {
_runningRequestsMutex.lock();
++_runningRequestsCount;
if (_runningRequestsCount == 1)
debug(9, "Storage is working now");
_runningRequestsMutex.unlock();
return ConnMan.addRequest(request, new Common::Callback<Storage, Networking::Request *>(this, &Storage::requestFinishedCallback));
}
void Storage::requestFinishedCallback(Networking::Request *invalidRequestPointer) {
bool restartSync = false;
_runningRequestsMutex.lock();
if (invalidRequestPointer == _savesSyncRequest)
_savesSyncRequest = nullptr;
--_runningRequestsCount;
if (_syncRestartRequestsed)
restartSync = true;
if (_runningRequestsCount == 0 && !restartSync)
debug(9, "Storage is not working now");
_runningRequestsMutex.unlock();
if (restartSync)
syncSaves(nullptr, nullptr);
}
Networking::Request *Storage::upload(Common::String remotePath, Common::String localPath, UploadCallback callback, Networking::ErrorCallback errorCallback) {
if (!errorCallback) errorCallback = getErrorPrintingCallback();
Common::File *f = new Common::File();
if (!f->open(localPath)) {
warning("Storage: unable to open file to upload from");
if (errorCallback)
(*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "", -1));
delete errorCallback;
delete callback;
delete f;
return nullptr;
}
return upload(remotePath, f, callback, errorCallback);
}
bool Storage::uploadStreamSupported() {
return true;
}
Networking::Request *Storage::streamFile(Common::String path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
//most Storages use paths instead of ids, so this should work
return streamFileById(path, callback, errorCallback);
}
Networking::Request *Storage::download(Common::String remotePath, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback) {
//most Storages use paths instead of ids, so this should work
return downloadById(remotePath, localPath, callback, errorCallback);
}
Networking::Request *Storage::downloadById(Common::String remoteId, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback) {
if (!errorCallback) errorCallback = getErrorPrintingCallback();
Common::DumpFile *f = new Common::DumpFile();
if (!f->open(localPath, true)) {
warning("Storage: unable to open file to download into");
if (errorCallback) (*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "", -1));
delete errorCallback;
delete callback;
delete f;
return nullptr;
}
return addRequest(new DownloadRequest(this, callback, errorCallback, remoteId, f));
}
Networking::Request *Storage::downloadFolder(Common::String remotePath, Common::String localPath, FileArrayCallback callback, Networking::ErrorCallback errorCallback, bool recursive) {
if (!_isEnabled) {
warning("Storage::downloadFolder: cannot be run while Storage is disabled");
if (errorCallback)
(*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "Storage is disabled.", -1));
return nullptr;
}
if (!errorCallback)
errorCallback = getErrorPrintingCallback();
return addRequest(new FolderDownloadRequest(this, callback, errorCallback, remotePath, localPath, recursive));
}
SavesSyncRequest *Storage::syncSaves(BoolCallback callback, Networking::ErrorCallback errorCallback) {
_runningRequestsMutex.lock();
if (!_isEnabled) {
warning("Storage::syncSaves: cannot be run while Storage is disabled");
if (errorCallback)
(*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "Storage is disabled.", -1));
_runningRequestsMutex.unlock();
return nullptr;
}
if (_savesSyncRequest) {
warning("Storage::syncSaves: there is a sync in progress already");
_syncRestartRequestsed = true;
_runningRequestsMutex.unlock();
return _savesSyncRequest;
}
if (!callback)
callback = new Common::Callback<Storage, BoolResponse>(this, &Storage::savesSyncDefaultCallback);
if (!errorCallback)
errorCallback = new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::savesSyncDefaultErrorCallback);
_savesSyncRequest = new SavesSyncRequest(this, callback, errorCallback);
_syncRestartRequestsed = false;
_runningRequestsMutex.unlock();
return (SavesSyncRequest *)addRequest(_savesSyncRequest); //who knows what that ConnMan could return in the future
}
bool Storage::isWorking() {
_runningRequestsMutex.lock();
bool working = _runningRequestsCount > 0;
_runningRequestsMutex.unlock();
return working;
}
///// SavesSyncRequest-related /////
bool Storage::isSyncing() {
_runningRequestsMutex.lock();
bool syncing = _savesSyncRequest != nullptr;
_runningRequestsMutex.unlock();
return syncing;
}
double Storage::getSyncDownloadingProgress() {
double result = 1;
_runningRequestsMutex.lock();
if (_savesSyncRequest)
result = _savesSyncRequest->getDownloadingProgress();
_runningRequestsMutex.unlock();
return result;
}
double Storage::getSyncProgress() {
double result = 1;
_runningRequestsMutex.lock();
if (_savesSyncRequest)
result = _savesSyncRequest->getProgress();
_runningRequestsMutex.unlock();
return result;
}
Common::Array<Common::String> Storage::getSyncingFiles() {
Common::Array<Common::String> result;
_runningRequestsMutex.lock();
if (_savesSyncRequest)
result = _savesSyncRequest->getFilesToDownload();
_runningRequestsMutex.unlock();
return result;
}
void Storage::cancelSync() {
_runningRequestsMutex.lock();
if (_savesSyncRequest)
_savesSyncRequest->finish();
_runningRequestsMutex.unlock();
}
void Storage::setSyncTarget(GUI::CommandReceiver *target) {
_runningRequestsMutex.lock();
if (_savesSyncRequest)
_savesSyncRequest->setTarget(target);
_runningRequestsMutex.unlock();
}
void Storage::savesSyncDefaultCallback(BoolResponse response) {
_runningRequestsMutex.lock();
_savesSyncRequest = nullptr;
_runningRequestsMutex.unlock();
if (!response.value)
warning("SavesSyncRequest called success callback with `false` argument");
}
void Storage::savesSyncDefaultErrorCallback(Networking::ErrorResponse error) {
_runningRequestsMutex.lock();
_savesSyncRequest = nullptr;
_runningRequestsMutex.unlock();
printErrorResponse(error);
if (error.interrupted)
Common::OSDMessageQueue::instance().addMessage(_("Saved games sync was cancelled."));
else
Common::OSDMessageQueue::instance().addMessage(_("Saved games sync failed.\nCheck your Internet connection."));
}
///// DownloadFolderRequest-related /////
bool Storage::startDownload(Common::String remotePath, Common::String localPath) {
_runningRequestsMutex.lock();
if (_downloadFolderRequest) {
warning("Storage::startDownload: there is a download in progress already");
_runningRequestsMutex.unlock();
return false;
}
_downloadFolderRequest = (FolderDownloadRequest *)downloadFolder(
remotePath, localPath,
new Common::Callback<Storage, FileArrayResponse>(this, &Storage::directoryDownloadedCallback),
new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::directoryDownloadedErrorCallback),
true
);
_runningRequestsMutex.unlock();
return true;
}
void Storage::cancelDownload() {
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
_downloadFolderRequest->finish();
_runningRequestsMutex.unlock();
}
void Storage::setDownloadTarget(GUI::CommandReceiver *target) {
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
_downloadFolderRequest->setTarget(target);
_runningRequestsMutex.unlock();
}
bool Storage::isDownloading() {
_runningRequestsMutex.lock();
bool syncing = _downloadFolderRequest != nullptr;
_runningRequestsMutex.unlock();
return syncing;
}
double Storage::getDownloadingProgress() {
double result = 1;
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getProgress();
_runningRequestsMutex.unlock();
return result;
}
uint64 Storage::getDownloadBytesNumber() {
uint64 result = 0;
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getDownloadedBytes();
_runningRequestsMutex.unlock();
return result;
}
uint64 Storage::getDownloadTotalBytesNumber() {
uint64 result = 0;
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getTotalBytesToDownload();
_runningRequestsMutex.unlock();
return result;
}
uint64 Storage::getDownloadSpeed() {
uint64 result = 0;
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getDownloadSpeed();
_runningRequestsMutex.unlock();
return result;
}
Common::String Storage::getDownloadRemoteDirectory() {
Common::String result = "";
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getRemotePath();
_runningRequestsMutex.unlock();
return result;
}
Common::String Storage::getDownloadLocalDirectory() {
Common::String result = "";
_runningRequestsMutex.lock();
if (_downloadFolderRequest)
result = _downloadFolderRequest->getLocalPath();
_runningRequestsMutex.unlock();
return result;
}
void Storage::directoryDownloadedCallback(FileArrayResponse response) {
_runningRequestsMutex.lock();
_downloadFolderRequest = nullptr;
_runningRequestsMutex.unlock();
Common::String message;
if (response.value.size()) {
message = Common::String::format(_("Download complete.\nFailed to download %u files."), response.value.size());
} else {
message = _("Download complete.");
}
Common::OSDMessageQueue::instance().addMessage(message.c_str());
}
void Storage::directoryDownloadedErrorCallback(Networking::ErrorResponse error) {
_runningRequestsMutex.lock();
_downloadFolderRequest = nullptr;
_runningRequestsMutex.unlock();
Common::OSDMessageQueue::instance().addMessage(_("Download failed."));
}
} // End of namespace Cloud
|