aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp114
-rw-r--r--backends/cloud/dropbox/dropboxlistdirectoryrequest.h51
-rw-r--r--backends/cloud/dropbox/dropboxstorage.cpp32
-rw-r--r--backends/cloud/dropbox/dropboxstorage.h2
-rw-r--r--backends/cloud/iso8601.cpp117
-rw-r--r--backends/cloud/iso8601.h37
-rw-r--r--backends/cloud/storagefile.cpp2
-rw-r--r--backends/module.mk4
8 files changed, 333 insertions, 26 deletions
diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp
new file mode 100644
index 0000000000..e28a445d63
--- /dev/null
+++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp
@@ -0,0 +1,114 @@
+/* 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/dropbox/dropboxlistdirectoryrequest.h"
+#include "backends/cloud/iso8601.h"
+#include "backends/networking/curl/connectionmanager.h"
+#include "backends/networking/curl/curljsonrequest.h"
+
+namespace Cloud {
+namespace Dropbox {
+
+DropboxListDirectoryRequest::DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::FileArrayCallback cb, bool recursive):
+ Networking::Request(0), _filesCallback(cb), _token(token), _complete(false) {
+ Common::BaseCallback<> *innerCallback = new Common::Callback<DropboxListDirectoryRequest>(this, &DropboxListDirectoryRequest::responseCallback);//new Common::GlobalFunctionCallback(printJson); //okay
+ Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder");
+ request->addHeader("Authorization: Bearer " + _token);
+ request->addHeader("Content-Type: application/json");
+
+ Common::JSONObject jsonRequestParameters;
+ jsonRequestParameters.setVal("path", new Common::JSONValue(path));
+ jsonRequestParameters.setVal("recursive", new Common::JSONValue(recursive));
+ jsonRequestParameters.setVal("include_media_info", new Common::JSONValue(false));
+ jsonRequestParameters.setVal("include_deleted", new Common::JSONValue(false));
+
+ Common::JSONValue value(jsonRequestParameters);
+ request->addPostField(Common::JSON::stringify(&value));
+
+ ConnMan.addRequest(request);
+}
+
+void DropboxListDirectoryRequest::responseCallback(void *jsonPtr) {
+ Common::JSONValue *json = (Common::JSONValue *)jsonPtr;
+ if (json) {
+ Common::JSONObject response = json->asObject();
+
+ if (response.contains("error") || response.contains("error_summary")) {
+ warning("Dropbox returned error: %s", response.getVal("error_summary")->asString().c_str());
+ _complete = true;
+ delete json;
+ return;
+ }
+
+ //TODO: check that all keys exist to avoid segfaults
+ //TODO: get more files in the folder to check "has_more" case
+
+ Common::JSONArray items = response.getVal("entries")->asArray();
+ for (uint32 i = 0; i < items.size(); ++i) {
+ Common::JSONObject item = items[i]->asObject();
+ Common::String path = item.getVal("path_lower")->asString();
+ bool isDirectory = (item.getVal(".tag")->asString() == "folder");
+ uint32 size = 0, timestamp = 0;
+ if (!isDirectory) {
+ size = item.getVal("size")->asNumber();
+ timestamp = ISO8601::convertToTimestamp(item.getVal("server_modified")->asString());
+ }
+ _files.push_back(StorageFile(path, size, timestamp, isDirectory));
+ }
+
+ bool hasMore = response.getVal("has_more")->asBool();
+
+ if (hasMore) {
+ Common::BaseCallback<> *innerCallback = new Common::Callback<DropboxListDirectoryRequest>(this, &DropboxListDirectoryRequest::responseCallback);
+ Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder/continue");
+ request->addHeader("Authorization: Bearer " + _token);
+ request->addHeader("Content-Type: application/json");
+
+ Common::JSONObject jsonRequestParameters;
+ jsonRequestParameters.setVal("cursor", new Common::JSONValue(response.getVal("cursor")->asString()));
+
+ Common::JSONValue value(jsonRequestParameters);
+ request->addPostField(Common::JSON::stringify(&value));
+
+ ConnMan.addRequest(request);
+ } else {
+ _complete = true;
+ }
+ } else {
+ warning("null, not json");
+ _complete = true;
+ }
+
+ delete json;
+}
+
+bool DropboxListDirectoryRequest::handle() {
+ if (_complete && _filesCallback) {
+ (*_filesCallback)(_files);
+ }
+
+ return _complete;
+}
+
+
+} //end of namespace Dropbox
+} //end of namespace Cloud
diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.h b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h
new file mode 100644
index 0000000000..03b4fc121a
--- /dev/null
+++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.h
@@ -0,0 +1,51 @@
+/* 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_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H
+#define BACKENDS_CLOUD_DROPBOX_DROPBOXLISTDIRECTORYREQUEST_H
+
+#include "backends/cloud/storage.h"
+#include "common/callback.h"
+#include "backends/networking/curl/request.h"
+
+namespace Cloud {
+namespace Dropbox {
+
+class DropboxListDirectoryRequest: public Networking::Request {
+ Storage::FileArrayCallback _filesCallback;
+ Common::String _token;
+ bool _complete;
+ Common::Array<StorageFile> _files;
+
+ void responseCallback(void *jsonPtr);
+
+public:
+ DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::FileArrayCallback cb, bool recursive = false);
+ virtual ~DropboxListDirectoryRequest() { delete _filesCallback; }
+
+ virtual bool handle();
+};
+
+} //end of namespace Dropbox
+} //end of namespace Cloud
+
+#endif
diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp
index 28d14c6a2e..6de9424efc 100644
--- a/backends/cloud/dropbox/dropboxstorage.cpp
+++ b/backends/cloud/dropbox/dropboxstorage.cpp
@@ -22,6 +22,7 @@
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "backends/cloud/dropbox/dropboxstorage.h"
+#include "backends/cloud/dropbox/dropboxlistdirectoryrequest.h"
#include "backends/networking/curl/connectionmanager.h"
#include "backends/networking/curl/curljsonrequest.h"
#include "common/config-manager.h"
@@ -75,37 +76,20 @@ void DropboxStorage::saveConfig(Common::String keyPrefix) {
ConfMan.set(keyPrefix + "user_id", _uid, "cloud");
}
-void printJson(void *ptr) {
- Common::JSONValue *json = (Common::JSONValue *)ptr;
- if (json) {
- debug("%s", json->stringify(true).c_str());
- } else {
- warning("null, not json");
- }
+void DropboxStorage::printFiles(Common::Array<StorageFile> files) {
+ debug("files:");
+ for (uint32 i = 0; i < files.size(); ++i)
+ debug("\t%s", files[i].name().c_str());
}
void DropboxStorage::listDirectory(Common::String path, FileArrayCallback outerCallback, bool recursive) {
- //Common::BaseCallback<> *innerCallback = new Common::CallbackBridge<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::listDirectoryInnerCallback, outerCallback);
- Common::BaseCallback<> *innerCallback = new Common::GlobalFunctionCallback(printJson); //okay
- Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, "https://api.dropboxapi.com/2/files/list_folder");
- request->addHeader("Authorization: Bearer " + _token);
- request->addHeader("Content-Type: application/json");
-
- Common::JSONObject jsonRequestParameters;
- jsonRequestParameters.setVal("path", new Common::JSONValue(path));
- jsonRequestParameters.setVal("recursive", new Common::JSONValue(recursive));
- jsonRequestParameters.setVal("include_media_info", new Common::JSONValue(false));
- jsonRequestParameters.setVal("include_deleted", new Common::JSONValue(false));
-
- Common::JSONValue value(jsonRequestParameters);
- request->addPostField(Common::JSON::stringify(&value));
-
- ConnMan.addRequest(request);
+ ConnMan.addRequest(new DropboxListDirectoryRequest(_token, path, outerCallback, recursive));
}
void DropboxStorage::syncSaves(BoolCallback callback) {
//this is not the real syncSaves() implementation
- listDirectory("", 0); //"" is root in Dropbox, not "/"
+ //"" is root in Dropbox, not "/"
+ listDirectory("", new Common::Callback<DropboxStorage, Common::Array<StorageFile> >(this, &DropboxStorage::printFiles), true);
}
void DropboxStorage::info(StorageInfoCallback outerCallback) {
diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h
index 3077b98763..c1c2e03497 100644
--- a/backends/cloud/dropbox/dropboxstorage.h
+++ b/backends/cloud/dropbox/dropboxstorage.h
@@ -42,6 +42,8 @@ class DropboxStorage: public Cloud::Storage {
/** Constructs StorageInfo based on JSON response from cloud. */
void infoInnerCallback(StorageInfoCallback outerCallback, void *json);
+ void printFiles(Common::Array<StorageFile> files);
+
public:
virtual ~DropboxStorage();
diff --git a/backends/cloud/iso8601.cpp b/backends/cloud/iso8601.cpp
new file mode 100644
index 0000000000..d34c98e7b8
--- /dev/null
+++ b/backends/cloud/iso8601.cpp
@@ -0,0 +1,117 @@
+/* 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/iso8601.h"
+#include "common/str.h"
+
+namespace {
+
+uint32 find(const Common::String &haystack, const Common::String &needle, uint32 pos = 0) {
+ if (pos >= haystack.size()) {
+ return Common::String::npos;
+ }
+
+ //TODO: write something smarter
+ uint32 lastIndex = haystack.size() - needle.size();
+ for (uint32 i = pos; i < lastIndex; ++i) {
+ bool found = true;
+ for (uint32 j = 0; j < needle.size(); ++j)
+ if (haystack[i + j] != needle[j]) {
+ found = false;
+ break;
+ }
+
+ if (found) return i;
+ }
+
+ return Common::String::npos;
+}
+
+Common::String getSubstring(const Common::String &s, uint32 beginning, uint32 ending) {
+ //beginning inclusive, ending exclusive
+ if (beginning == -1 || ending == -1) return ""; //bad
+ Common::String result = s;
+ result.erase(ending);
+ result.erase(0, beginning);
+ return result;
+}
+
+int parseInt(Common::String s) {
+ //TODO: not sure this is not forbidden at all
+ return atoi(s.c_str());
+}
+
+}
+
+namespace Cloud {
+namespace ISO8601 {
+
+uint32 convertToTimestamp(const Common::String &iso8601Date) {
+ //2015-05-12T15:50:38Z
+ uint32 firstHyphen = find(iso8601Date, "-");
+ uint32 secondHyphen = find(iso8601Date, "-", firstHyphen + 1);
+ uint32 tSeparator = find(iso8601Date, "T", secondHyphen + 1);
+ uint32 firstColon = find(iso8601Date, ":", tSeparator + 1);
+ uint32 secondColon = find(iso8601Date, ":", firstColon + 1);
+ uint32 zSeparator = find(iso8601Date, "Z", secondColon + 1);
+ //now note '+1' which means if there ever was '-1' result of find(), we still did a valid find() from 0th char
+
+ Common::String year = getSubstring(iso8601Date, 0, firstHyphen);
+ Common::String month = getSubstring(iso8601Date, firstHyphen + 1, secondHyphen);
+ Common::String day = getSubstring(iso8601Date, secondHyphen + 1, tSeparator);
+ Common::String hour = getSubstring(iso8601Date, tSeparator + 1, firstColon);
+ Common::String minute = getSubstring(iso8601Date, firstColon + 1, secondColon);
+ Common::String second = getSubstring(iso8601Date, secondColon + 1, zSeparator);
+ //now note only 'ending' argument was not '+1' (which means I could've make that function such that -1 means 'until the end')
+
+ int Y = parseInt(year);
+ int M = parseInt(month);
+ int D = parseInt(day);
+ int h = parseInt(hour);
+ int m = parseInt(minute);
+ int s = parseInt(second);
+
+ //ok, now I compose a timestamp based on my basic perception of time/date
+ //yeah, I know about leap years and leap seconds and all, but still we don't care there
+
+ uint32 days = D - 1;
+ for (int i = 1970; i < Y; ++i)
+ if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
+ days += 366;
+ else
+ days += 365;
+
+ int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+ for (int i = 1; i < M; ++i) {
+ days += mdays[i - 1];
+ if (i == 2)
+ if ((Y % 4 == 0 && Y % 100 != 0) || (Y % 400 == 0))
+ days += 1;
+ }
+
+ uint32 hours = days * 24 + h;
+ uint32 minutes = hours * 60 + m;
+ return minutes * 60 + s;
+}
+
+} //end of namespace ISO8601
+} //end of namespace Cloud
diff --git a/backends/cloud/iso8601.h b/backends/cloud/iso8601.h
new file mode 100644
index 0000000000..a53b3bb501
--- /dev/null
+++ b/backends/cloud/iso8601.h
@@ -0,0 +1,37 @@
+/* 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_ISO8601_H
+#define BACKENDS_CLOUD_ISO8601_H
+
+#include "common/str.h"
+
+namespace Cloud {
+namespace ISO8601 {
+
+ /** Returns timestamp corresponding to given ISO 8601 date */
+ uint32 convertToTimestamp(const Common::String &iso8601Date);
+
+} //end of namespace ISO8601
+} //end of namespace Cloud
+
+#endif
diff --git a/backends/cloud/storagefile.cpp b/backends/cloud/storagefile.cpp
index 0d40698823..02c9cae3cf 100644
--- a/backends/cloud/storagefile.cpp
+++ b/backends/cloud/storagefile.cpp
@@ -32,7 +32,7 @@ StorageFile::StorageFile(Common::String pth, uint32 sz, uint32 ts, bool dir) {
uint32 i = _name.size() - 1;
while (true) {
if (_name[i] == '/' || _name[i] == '\\') {
- _name.erase(0, i);
+ _name.erase(0, i+1);
break;
}
if (i == 0) break;
diff --git a/backends/module.mk b/backends/module.mk
index 2cee007264..7e83192ee0 100644
--- a/backends/module.mk
+++ b/backends/module.mk
@@ -21,9 +21,11 @@ MODULE_OBJS := \
ifdef USE_CLOUD
MODULE_OBJS += \
+ cloud/iso8601.o \
cloud/manager.o \
cloud/storagefile.o \
- cloud/dropbox/dropboxstorage.o
+ cloud/dropbox/dropboxstorage.o \
+ cloud/dropbox/dropboxlistdirectoryrequest.o
endif
ifdef USE_LIBCURL