diff options
Diffstat (limited to 'backends/cloud')
-rw-r--r-- | backends/cloud/downloadrequest.cpp | 67 | ||||
-rw-r--r-- | backends/cloud/downloadrequest.h | 45 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp | 1 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxlistdirectoryrequest.h | 2 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.cpp | 21 | ||||
-rw-r--r-- | backends/cloud/dropbox/dropboxstorage.h | 9 | ||||
-rw-r--r-- | backends/cloud/storage.h | 10 |
7 files changed, 147 insertions, 8 deletions
diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp new file mode 100644 index 0000000000..ed84d4f0e5 --- /dev/null +++ b/backends/cloud/downloadrequest.cpp @@ -0,0 +1,67 @@ +/* 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/downloadrequest.h" +#include "common/debug.h" +#include "common/textconsole.h" + +namespace Cloud { + +DownloadRequest::DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream): + Request(0), _boolCallback(callback), _stream(stream) {} + +bool DownloadRequest::handle() { + if (!_stream) { + warning("DownloadRequest: no stream to read"); + return true; + } + + const int kBufSize = 16 * 1024; + char buf[kBufSize]; + uint32 readBytes = _stream->read(buf, kBufSize); + + //TODO: save into file + /* + if (readBytes != 0) + if (_outputStream.write(buf, readBytes) != readBytes) + warning("DropboxDownloadRequest: unable to write all received bytes into output stream"); + */ + + buf[readBytes] = 0; + debug("%s", buf); //TODO: remove + + if (_stream->eos()) { + if (_stream->httpResponseCode() != 200) { + warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode()); + //TODO: do something about it actually + } + + if (_boolCallback) (*_boolCallback)(_stream->httpResponseCode() == 200); + + //TODO: close file stream + return true; + } + + return false; +} + +} //end of namespace Cloud diff --git a/backends/cloud/downloadrequest.h b/backends/cloud/downloadrequest.h new file mode 100644 index 0000000000..a819910b1b --- /dev/null +++ b/backends/cloud/downloadrequest.h @@ -0,0 +1,45 @@ +/* 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. +* +*/ + +#ifndef BACKENDS_CLOUD_DOWNLOADREQUEST_H +#define BACKENDS_CLOUD_DOWNLOADREQUEST_H + +#include "backends/networking/curl/request.h" +#include "backends/networking/curl/networkreadstream.h" +#include "backends/cloud/storage.h" + +namespace Cloud { + +class DownloadRequest: public Networking::Request { + Networking::NetworkReadStream *_stream; + Storage::BoolCallback _boolCallback; + +public: + DownloadRequest(Storage::BoolCallback callback, Networking::NetworkReadStream *stream); + virtual ~DownloadRequest() {} + + virtual bool handle(); +}; + +} //end of namespace Cloud + +#endif diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index e28a445d63..5e5957b12c 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -24,6 +24,7 @@ #include "backends/cloud/iso8601.h" #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" +#include "common/json.h" namespace Cloud { namespace Dropbox { diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h index 03b4fc121a..0c10512782 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h @@ -24,8 +24,8 @@ #define BACKENDS_CLOUD_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H #include "backends/cloud/storage.h" -#include "common/callback.h" #include "backends/networking/curl/request.h" +#include "common/callback.h" namespace Cloud { namespace Dropbox { diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 6de9424efc..1b6dc1b92f 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -23,6 +23,7 @@ #include "backends/cloud/dropbox/dropboxstorage.h" #include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h" +#include "backends/cloud/downloadrequest.h" #include "backends/networking/curl/connectionmanager.h" #include "backends/networking/curl/curljsonrequest.h" #include "common/config-manager.h" @@ -86,10 +87,28 @@ void DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerC ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive)); } +Networking::NetworkReadStream *DropboxStorage::streamFile(Common::String path) { + Common::JSONObject jsonRequestParameters; + jsonRequestParameters.setVal("path", new Common::JSONValue(path)); + Common::JSONValue value(jsonRequestParameters); + + Networking::CurlRequest *request = new Networking::CurlRequest(0, "https://content.dropboxapi.com/2/files/download"); + request->addHeader("Authorization: Bearer " + _token); + request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value)); + request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded) + + return request->execute(); +} + +void DropboxStorage::download(Common::String path, BoolCallback callback) { + ConnMan.addRequest(new DownloadRequest(callback, streamFile(path))); +} + void DropboxStorage::syncSaves(BoolCallback callback) { //this is not the real syncSaves() implementation //"" is root in Dropbox, not "/" - listDirectory("", new Common::Callback<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true); + //listDirectory("", new Common::Callback<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true); + download("/notempty.txt", 0); } void DropboxStorage::info(StorageInfoCallback outerCallback) { diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index c1c2e03497..92c3746da5 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -67,10 +67,13 @@ public: virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false); /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) {} //TODO + virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) {} //TODO - /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, ReadStreamCallback callback) {} //TODO + /** Returns pointer to Networking::NetworkReadStream. */ + virtual Networking::NetworkReadStream *streamFile(Common::String path); + + /** Calls the callback when finished. */ + virtual void download(Common::String path, BoolCallback callback); /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) {} //TODO diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 1435be8329..92f3bb96b2 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -29,6 +29,7 @@ #include "common/callback.h" #include "backends/cloud/storagefile.h" #include "backends/cloud/storageinfo.h" +#include "backends/networking/curl/networkreadstream.h" namespace Cloud { @@ -62,10 +63,13 @@ public: virtual void listDirectory(Common::String path, FileArrayCallback callback, bool recursive = false) = 0; /** Calls the callback when finished. */ - virtual void upload(Common::String path, Common::ReadStream* contents, BoolCallback callback) = 0; + virtual void upload(Common::String path, Common::ReadStream *contents, BoolCallback callback) = 0; - /** Returns pointer to Common::ReadStream. */ - virtual void download(Common::String path, ReadStreamCallback callback) = 0; + /** Returns pointer to Networking::NetworkReadStream. */ + virtual Networking::NetworkReadStream *streamFile(Common::String path) = 0; + + /** Calls the callback when finished. */ + virtual void download(Common::String path, BoolCallback callback) = 0; /** Calls the callback when finished. */ virtual void remove(Common::String path, BoolCallback callback) = 0; |