diff options
author | Alexander Tkachev | 2016-07-18 19:00:14 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 5163fb4e02926bcc9fe12de3f3b031b1c8349c8b (patch) | |
tree | 8b9eae6798028fdd592d1269ddb956b2bd82f4b3 | |
parent | 8de753b68ad92913b73e1f7f5552b209f7a72876 (diff) | |
download | scummvm-rg350-5163fb4e02926bcc9fe12de3f3b031b1c8349c8b.tar.gz scummvm-rg350-5163fb4e02926bcc9fe12de3f3b031b1c8349c8b.tar.bz2 scummvm-rg350-5163fb4e02926bcc9fe12de3f3b031b1c8349c8b.zip |
CLOUD: Add ListAjaxHandler
"/list" now returns JSON with directory information. Would be used in
AJAX-based Files Manager.
-rw-r--r-- | backends/module.mk | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/listajaxhandler.cpp | 134 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/listajaxhandler.h | 65 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 2 |
5 files changed, 203 insertions, 0 deletions
diff --git a/backends/module.mk b/backends/module.mk index f04397d15b..a753b4d382 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -68,6 +68,7 @@ MODULE_OBJS += \ networking/sdl_net/handlers/filesbasehandler.o \ networking/sdl_net/handlers/filespagehandler.o \ networking/sdl_net/handlers/indexpagehandler.o \ + networking/sdl_net/handlers/listajaxhandler.o \ networking/sdl_net/handlers/resourcehandler.o \ networking/sdl_net/handlers/uploadfilehandler.o \ networking/sdl_net/handlerutils.o \ diff --git a/backends/networking/sdl_net/handlers/listajaxhandler.cpp b/backends/networking/sdl_net/handlers/listajaxhandler.cpp new file mode 100644 index 0000000000..8657ab52ed --- /dev/null +++ b/backends/networking/sdl_net/handlers/listajaxhandler.cpp @@ -0,0 +1,134 @@ +/* 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/networking/sdl_net/handlers/listajaxhandler.h" +#include "backends/networking/sdl_net/handlerutils.h" +#include "backends/networking/sdl_net/localwebserver.h" +#include "common/translation.h" +#include "common/json.h" + +namespace Networking { + +ListAjaxHandler::ListAjaxHandler() {} + +ListAjaxHandler::~ListAjaxHandler() {} + +void ListAjaxHandler::handle(Client &client) { + Common::String path = client.queryParameter("path"); + Common::JSONValue jsonResponse = listDirectory(path); + Common::String response = jsonResponse.stringify(true); + LocalWebserver::setClientGetHandler(client, response); +} + +Common::JSONObject ListAjaxHandler::listDirectory(Common::String path) { + Common::JSONArray itemsList; + Common::JSONObject errorResult; + Common::JSONObject successResult; + successResult.setVal("type", new Common::JSONValue("success")); + errorResult.setVal("type", new Common::JSONValue("error")); + + if (path == "" || path == "/") { + addItem(itemsList, IT_DIRECTORY, "/root/", _("File system root")); + addItem(itemsList, IT_DIRECTORY, "/saves/", _("Saved games")); + successResult.setVal("items", new Common::JSONValue(itemsList)); + return successResult; + } + + Common::String prefixToRemove = "", prefixToAdd = ""; + if (!transformPath(path, prefixToRemove, prefixToAdd)) return errorResult; + + Common::FSNode node = Common::FSNode(path); + if (path == "/") node = node.getParent(); // absolute root + if (!node.isDirectory()) return errorResult; + + // list directory + Common::FSList _nodeContent; + if (!node.getChildren(_nodeContent, Common::FSNode::kListAll, false)) // do not show hidden files + _nodeContent.clear(); + else + Common::sort(_nodeContent.begin(), _nodeContent.end()); + + // add parent directory link + { + Common::String filePath = path; + if (filePath.hasPrefix(prefixToRemove)) + filePath.erase(0, prefixToRemove.size()); + if (filePath == "" || filePath == "/" || filePath == "\\") + filePath = "/"; + else + filePath = parentPath(prefixToAdd + filePath); + addItem(itemsList, IT_PARENT_DIRECTORY, filePath, _("Parent directory")); + } + + // fill the content + for (Common::FSList::iterator i = _nodeContent.begin(); i != _nodeContent.end(); ++i) { + Common::String name = i->getDisplayName(); + if (i->isDirectory()) name += "/"; + + Common::String filePath = i->getPath(); + if (filePath.hasPrefix(prefixToRemove)) + filePath.erase(0, prefixToRemove.size()); + filePath = prefixToAdd + filePath; + + addItem(itemsList, detectType(i->isDirectory(), name), filePath, name); + } + + successResult.setVal("items", new Common::JSONValue(itemsList)); + return successResult; +} + +ListAjaxHandler::ItemType ListAjaxHandler::detectType(bool isDirectory, const Common::String &name) const { + if (isDirectory) return IT_DIRECTORY; + if (name.hasSuffix(".txt")) return IT_TXT; + if (name.hasSuffix(".zip")) return IT_ZIP; + if (name.hasSuffix(".7z")) return IT_7Z; + return IT_UNKNOWN; +} + +void ListAjaxHandler::addItem(Common::JSONArray &responseItemsList, ItemType itemType, Common::String path, Common::String name, Common::String size) { + Common::String icon; + bool isDirectory = (itemType == IT_DIRECTORY || itemType == IT_PARENT_DIRECTORY); + switch (itemType) { + case IT_DIRECTORY: icon = "dir.png"; break; + case IT_PARENT_DIRECTORY: icon = "up.png"; break; + case IT_TXT: icon = "txt.png"; break; + case IT_ZIP: icon = "zip.png"; break; + case IT_7Z: icon = "7z.png"; break; + default: icon = "unk.png"; + } + + Common::JSONObject item; + item.setVal("name", new Common::JSONValue(name)); + item.setVal("path", new Common::JSONValue(path)); + item.setVal("isDirectory", new Common::JSONValue(isDirectory)); + item.setVal("size", new Common::JSONValue(size)); + item.setVal("icon", new Common::JSONValue(icon)); + responseItemsList.push_back(new Common::JSONValue(item)); +} + +/// public + +ClientHandlerCallback ListAjaxHandler::getHandler() { + return new Common::Callback<ListAjaxHandler, Client &>(this, &ListAjaxHandler::handle); +} + +} // End of namespace Networking diff --git a/backends/networking/sdl_net/handlers/listajaxhandler.h b/backends/networking/sdl_net/handlers/listajaxhandler.h new file mode 100644 index 0000000000..cef6a071a7 --- /dev/null +++ b/backends/networking/sdl_net/handlers/listajaxhandler.h @@ -0,0 +1,65 @@ +/* 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_NETWORKING_SDL_NET_LISTAJAXHANDLER_H +#define BACKENDS_NETWORKING_SDL_NET_LISTAJAXHANDLER_H + +#include "backends/networking/sdl_net/handlers/filesbasehandler.h" +#include "common/json.h" + +namespace Networking { + +class ListAjaxHandler: public FilesBaseHandler { + enum ItemType { + IT_DIRECTORY, + IT_PARENT_DIRECTORY, + IT_TXT, + IT_ZIP, + IT_7Z, + IT_UNKNOWN + }; + + void handle(Client &client); + + /** + * Lists the directory <path>. + * + * Returns JSON with either listed directory or error response. + */ + Common::JSONObject listDirectory(Common::String path); + + /** Helper method for detecting items' type. */ + ItemType detectType(bool isDirectory, const Common::String &name) const; + + /** Helper method for adding items into the files list. */ + void addItem(Common::JSONArray &responseItemsList, ItemType itemType, Common::String path, Common::String name, Common::String size = ""); + +public: + ListAjaxHandler(); + virtual ~ListAjaxHandler(); + + virtual ClientHandlerCallback getHandler(); +}; + +} // End of namespace Networking + +#endif diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 1999fe9f7f..2ac4834005 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -53,6 +53,7 @@ LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerS addPathHandler("/create", _createDirectoryHandler.getHandler()); addPathHandler("/download", _downloadFileHandler.getHandler()); addPathHandler("/upload", _uploadFileHandler.getHandler()); + addPathHandler("/list", _listAjaxHandler.getHandler()); _defaultHandler = _resourceHandler.getHandler(); } diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index cdd991d9de..b4655119da 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -29,6 +29,7 @@ #include "backends/networking/sdl_net/handlers/downloadfilehandler.h" #include "backends/networking/sdl_net/handlers/filespagehandler.h" #include "backends/networking/sdl_net/handlers/indexpagehandler.h" +#include "backends/networking/sdl_net/handlers/listajaxhandler.h" #include "backends/networking/sdl_net/handlers/resourcehandler.h" #include "backends/networking/sdl_net/handlers/uploadfilehandler.h" #include "common/hash-str.h" @@ -65,6 +66,7 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { CreateDirectoryHandler _createDirectoryHandler; DownloadFileHandler _downloadFileHandler; UploadFileHandler _uploadFileHandler; + ListAjaxHandler _listAjaxHandler; ResourceHandler _resourceHandler; uint32 _idlingFrames; Common::Mutex _handleMutex; |