diff options
author | Alexander Tkachev | 2016-06-06 21:24:53 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | b4b6ee0186750d6f2e8143313fc059c20512c306 (patch) | |
tree | 008eacfbb5e0224c872cddc2b9e91db9dd4f5bc8 | |
parent | bb207ae513ef02bfaf8e76374af40419a20742fe (diff) | |
download | scummvm-rg350-b4b6ee0186750d6f2e8143313fc059c20512c306.tar.gz scummvm-rg350-b4b6ee0186750d6f2e8143313fc059c20512c306.tar.bz2 scummvm-rg350-b4b6ee0186750d6f2e8143313fc059c20512c306.zip |
CLOUD: Add GoogleDriveCreateDirectory
Now we can create directories in Google Drive by path, not parent id +
directory name!
-rw-r--r-- | backends/cloud/cloudmanager.cpp | 33 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivecreatedirectoryrequest.cpp | 114 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivecreatedirectoryrequest.h | 62 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledriveresolveidrequest.cpp | 2 | ||||
-rw-r--r-- | backends/cloud/googledrive/googledrivestorage.cpp | 37 | ||||
-rw-r--r-- | backends/module.mk | 1 |
6 files changed, 234 insertions, 15 deletions
diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp index 03cc8f264b..0be52cd886 100644 --- a/backends/cloud/cloudmanager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -126,7 +126,38 @@ void CloudManager::testFeature() { Storage *storage = getCurrentStorage(); //if (storage) storage->info(nullptr, nullptr); GoogleDrive::GoogleDriveStorage *gd = dynamic_cast<GoogleDrive::GoogleDriveStorage *>(storage); - if (gd) gd->resolveFileId("firstfolder/subfolder", nullptr, nullptr); + if (gd) { + //new folder in root: + + //gd->createDirectory("newfolder1", nullptr, nullptr); + + //check it's there: + + //gd->listDirectoryById("appDataFolder", nullptr, nullptr); + + //new folder in firstfolder: + + //gd->createDirectory("firstfolder/newfolder2", nullptr, nullptr); + + //check it's there: + + //gd->listDirectoryById("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", nullptr, nullptr); + + //create existing folder in firstfolder: + + //gd->createDirectory("firstfolder/subfolder", nullptr, nullptr); + + //check no new folder there: + + //gd->listDirectoryById("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", nullptr, nullptr); + + //create folder in subfolder: + + //gd->createDirectory("firstfolder/subfolder/newfolder3", nullptr, nullptr); + + //check it's there: + + //gd->listDirectoryById("1OysvorQlmGl2ObMGb1c-JnjfC5yFL-Zj7AsQQhNNBnrk", nullptr, nullptr); + + //one more time: + + //gd->createDirectory("firstfolder/subfolder/newfolder3/megafolder", nullptr, nullptr); + + //check it's there: + + gd->listDirectoryById("1OXWPtfNgnmR_1K7SDm2v5J923bbAWrTdVDj-zRppLZDw", nullptr, nullptr); + } + //gd->resolveFileId("firstfolder/subfolder", nullptr, nullptr); //gd->listDirectoryById("appDataFolder", nullptr, nullptr); //gd->listDirectoryById("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", nullptr, nullptr); //gd->createDirectoryWithParentId("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", "subfolder", nullptr, nullptr); diff --git a/backends/cloud/googledrive/googledrivecreatedirectoryrequest.cpp b/backends/cloud/googledrive/googledrivecreatedirectoryrequest.cpp new file mode 100644 index 0000000000..213f030c26 --- /dev/null +++ b/backends/cloud/googledrive/googledrivecreatedirectoryrequest.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/googledrive/googledrivecreatedirectoryrequest.h" +#include "backends/cloud/googledrive/googledrivestorage.h" +#include "common/debug.h" + +namespace Cloud { +namespace GoogleDrive { + +GoogleDriveCreateDirectoryRequest::GoogleDriveCreateDirectoryRequest(GoogleDriveStorage *storage, Common::String parentPath, Common::String directoryName, Storage::BoolCallback cb, Networking::ErrorCallback ecb): + Networking::Request(nullptr, ecb), + _requestedParentPath(parentPath), _requestedDirectoryName(directoryName), _storage(storage), _boolCallback(cb), + _workingRequest(nullptr), _ignoreCallback(false) { + start(); +} + +GoogleDriveCreateDirectoryRequest::~GoogleDriveCreateDirectoryRequest() { + _ignoreCallback = true; + if (_workingRequest) _workingRequest->finish(); + delete _boolCallback; +} + +void GoogleDriveCreateDirectoryRequest::start() { + //cleanup + _ignoreCallback = true; + if (_workingRequest) _workingRequest->finish(); + _workingRequest = nullptr; + _currentDirectory = ""; + _currentDirectoryId = "appDataFolder"; + _ignoreCallback = false; + + //find out the parent id + Storage::UploadCallback innerCallback = new Common::Callback<GoogleDriveCreateDirectoryRequest, Storage::UploadResponse>(this, &GoogleDriveCreateDirectoryRequest::idResolvedCallback); + Networking::ErrorCallback innerErrorCallback = new Common::Callback<GoogleDriveCreateDirectoryRequest, Networking::ErrorResponse>(this, &GoogleDriveCreateDirectoryRequest::idResolveFailedCallback); + Common::String path = _requestedParentPath; + path += "/"; + path += _requestedDirectoryName; + _workingRequest = _storage->resolveFileId(path, innerCallback, innerErrorCallback); +} + +void GoogleDriveCreateDirectoryRequest::idResolvedCallback(Storage::UploadResponse response) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + + //resolved => folder already exists + finishSuccess(false); +} + +void GoogleDriveCreateDirectoryRequest::idResolveFailedCallback(Networking::ErrorResponse error) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + + //not resolved => folder not exists + if (error.response.contains("no such file found in its parent directory")) { + //parent's id after the '\n' + Common::String parentId = error.response; + for (uint32 i = 0; i < parentId.size(); ++i) + if (parentId[i] == '\n') { + parentId.erase(0, i+1); + break; + } + + Storage::BoolCallback callback = new Common::Callback<GoogleDriveCreateDirectoryRequest, Storage::BoolResponse>(this, &GoogleDriveCreateDirectoryRequest::createdDirectoryCallback); + Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveCreateDirectoryRequest, Networking::ErrorResponse>(this, &GoogleDriveCreateDirectoryRequest::createdDirectoryErrorCallback); + _workingRequest = _storage->createDirectoryWithParentId(parentId, _requestedDirectoryName, callback, failureCallback); + return; + } + + finishError(error); +} + +void GoogleDriveCreateDirectoryRequest::createdDirectoryCallback(Storage::BoolResponse response) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + finishSuccess(response.value); +} + +void GoogleDriveCreateDirectoryRequest::createdDirectoryErrorCallback(Networking::ErrorResponse error) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + finishError(error); +} + +void GoogleDriveCreateDirectoryRequest::handle() {} + +void GoogleDriveCreateDirectoryRequest::restart() { start(); } + +void GoogleDriveCreateDirectoryRequest::finishSuccess(bool success) { + Request::finishSuccess(); + if (_boolCallback) (*_boolCallback)(Storage::BoolResponse(this, success)); +} + +} // End of namespace GoogleDrive +} // End of namespace Cloud diff --git a/backends/cloud/googledrive/googledrivecreatedirectoryrequest.h b/backends/cloud/googledrive/googledrivecreatedirectoryrequest.h new file mode 100644 index 0000000000..0ac178e8a6 --- /dev/null +++ b/backends/cloud/googledrive/googledrivecreatedirectoryrequest.h @@ -0,0 +1,62 @@ +/* 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_GOOGLEDRIVE_GOOGLEDRIVECREATEDIRECTORYREQUEST_H +#define BACKENDS_CLOUD_GOOGLEDRIVE_GOOGLEDRIVECREATEDIRECTORYREQUEST_H + +#include "backends/cloud/storage.h" +#include "backends/networking/curl/request.h" +#include "common/callback.h" + +namespace Cloud { +namespace GoogleDrive { + +class GoogleDriveStorage; + +class GoogleDriveCreateDirectoryRequest: public Networking::Request { + Common::String _requestedParentPath; + Common::String _requestedDirectoryName; + GoogleDriveStorage *_storage; + Storage::BoolCallback _boolCallback; + Common::String _currentDirectory; + Common::String _currentDirectoryId; + Request *_workingRequest; + bool _ignoreCallback; + + void start(); + void idResolvedCallback(Storage::UploadResponse response); + void idResolveFailedCallback(Networking::ErrorResponse error); + void createdDirectoryCallback(Storage::BoolResponse response); + void createdDirectoryErrorCallback(Networking::ErrorResponse error); + void finishSuccess(bool success); +public: + GoogleDriveCreateDirectoryRequest(GoogleDriveStorage *storage, Common::String parentPath, Common::String directoryName, Storage::BoolCallback cb, Networking::ErrorCallback ecb); + virtual ~GoogleDriveCreateDirectoryRequest(); + + virtual void handle(); + virtual void restart(); +}; + +} // End of namespace GoogleDrive +} // End of namespace Cloud + +#endif diff --git a/backends/cloud/googledrive/googledriveresolveidrequest.cpp b/backends/cloud/googledrive/googledriveresolveidrequest.cpp index bde17b4533..5dcb5c6e11 100644 --- a/backends/cloud/googledrive/googledriveresolveidrequest.cpp +++ b/backends/cloud/googledrive/googledriveresolveidrequest.cpp @@ -104,7 +104,7 @@ void GoogleDriveResolveIdRequest::listedDirectoryCallback(Storage::FileArrayResp Common::String path = _currentDirectory; if (path != "") path += "/"; path += currentLevelName; - if (path == _requestedPath) finishError(Networking::ErrorResponse(this, false, true, "no such file found in its parent directory", 404)); + if (path == _requestedPath) finishError(Networking::ErrorResponse(this, false, true, Common::String("no such file found in its parent directory\n")+_currentDirectoryId, 404)); else finishError(Networking::ErrorResponse(this, false, true, "subdirectory not found", 400)); } } diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp index b0c0d47ecf..1750dc9646 100644 --- a/backends/cloud/googledrive/googledrivestorage.cpp +++ b/backends/cloud/googledrive/googledrivestorage.cpp @@ -33,6 +33,7 @@ #include <curl/curl.h> #include "googledrivelistdirectorybyidrequest.h" #include "googledriveresolveidrequest.h" +#include "googledrivecreatedirectoryrequest.h" namespace Cloud { namespace GoogleDrive { @@ -187,11 +188,9 @@ void GoogleDriveStorage::createDirectoryInnerCallback(BoolCallback outerCallback return; } - debug("%s", json->stringify(true).c_str()); - if (outerCallback) { - Common::JSONObject info = json->asObject(); - ///(*outerCallback)(BoolResponse(nullptr, true); + Common::JSONObject info = json->asObject(); + (*outerCallback)(BoolResponse(nullptr, info.contains("id"))); delete outerCallback; } @@ -240,7 +239,7 @@ Networking::Request *GoogleDriveStorage::resolveFileId(Common::String path, Uplo Networking::Request *GoogleDriveStorage::listDirectory(Common::String path, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback, bool recursive) { //return addRequest(new GoogleDriveListDirectoryRequest(this, path, callback, errorCallback, recursive)); - return nullptr; //TODO + return nullptr; } Networking::Request *GoogleDriveStorage::listDirectoryById(Common::String id, ListDirectoryCallback callback, Networking::ErrorCallback errorCallback) { @@ -301,16 +300,30 @@ void GoogleDriveStorage::printInfo(StorageInfoResponse response) { Networking::Request *GoogleDriveStorage::createDirectory(Common::String path, BoolCallback callback, Networking::ErrorCallback errorCallback) { if (!errorCallback) errorCallback = getErrorPrintingCallback(); - //return addRequest(new GoogleDriveCreateDirectoryRequest(this, path, callback, errorCallback)); - return nullptr; //TODO + if (!callback) callback = new Common::Callback<GoogleDriveStorage, BoolResponse>(this, &GoogleDriveStorage::printBool); + + //find out the parent path and directory name + Common::String parentPath = "", directoryName = path; + for (uint32 i = path.size(); i > 0; --i) { + if (path[i-1] == '/' || path[i-1] == '\\') { + parentPath = path; + parentPath.erase(i-1); + directoryName.erase(0, i); + break; + } + } + + if (parentPath == "") { + return createDirectoryWithParentId("appDataFolder", directoryName, callback, errorCallback); + } + + return addRequest(new GoogleDriveCreateDirectoryRequest(this, parentPath, directoryName, callback, errorCallback)); } Networking::Request *GoogleDriveStorage::createDirectoryWithParentId(Common::String parentId, Common::String name, BoolCallback callback, Networking::ErrorCallback errorCallback) { if (!errorCallback) errorCallback = getErrorPrintingCallback(); - //return addRequest(new GoogleDriveCreateDirectoryRequest(this, path, callback, errorCallback)); - Common::String url = "https://www.googleapis.com/drive/v3/files"; - //Networking::JsonCallback callback = new Common::Callback<GoogleDriveListDirectoryByIdRequest, Networking::JsonResponse>(this, &GoogleDriveListDirectoryByIdRequest::responseCallback); - //Networking::ErrorCallback failureCallback = new Common::Callback<GoogleDriveListDirectoryByIdRequest, Networking::ErrorResponse>(this, &GoogleDriveListDirectoryByIdRequest::errorCallback); + + Common::String url = "https://www.googleapis.com/drive/v3/files"; Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::createDirectoryInnerCallback, callback); Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, url.c_str()); request->addHeader("Authorization: Bearer " + accessToken()); @@ -323,13 +336,11 @@ Networking::Request *GoogleDriveStorage::createDirectoryWithParentId(Common::Str jsonRequestParameters.setVal("mimeType", new Common::JSONValue("application/vnd.google-apps.folder")); jsonRequestParameters.setVal("name", new Common::JSONValue(name)); jsonRequestParameters.setVal("parents", new Common::JSONValue(parentsArray)); - //jsonRequestParameters.setVal("include_deleted", new Common::JSONValue(false)); Common::JSONValue value(jsonRequestParameters); request->addPostField(Common::JSON::stringify(&value)); return addRequest(request); - return nullptr; //TODO } Networking::Request *GoogleDriveStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) { diff --git a/backends/module.mk b/backends/module.mk index 14755b21eb..9387be5c2b 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -32,6 +32,7 @@ MODULE_OBJS += \ cloud/dropbox/dropboxcreatedirectoryrequest.o \ cloud/dropbox/dropboxlistdirectoryrequest.o \ cloud/dropbox/dropboxuploadrequest.o \ + cloud/googledrive/googledrivecreatedirectoryrequest.o \ cloud/googledrive/googledrivelistdirectorybyidrequest.o \ cloud/googledrive/googledriveresolveidrequest.o \ cloud/googledrive/googledrivestorage.o \ |