ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

listajaxhandler.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/networking/sdl_net/handlers/listajaxhandler.h"
00024 #include "backends/networking/sdl_net/handlerutils.h"
00025 #include "backends/networking/sdl_net/localwebserver.h"
00026 #include "common/config-manager.h"
00027 #include "common/json.h"
00028 #include "common/translation.h"
00029 
00030 namespace Networking {
00031 
00032 ListAjaxHandler::ListAjaxHandler() {}
00033 
00034 ListAjaxHandler::~ListAjaxHandler() {}
00035 
00036 Common::JSONObject ListAjaxHandler::listDirectory(Common::String path) {
00037     Common::JSONArray itemsList;
00038     Common::JSONObject errorResult;
00039     Common::JSONObject successResult;
00040     successResult.setVal("type", new Common::JSONValue("success"));
00041     errorResult.setVal("type", new Common::JSONValue("error"));
00042 
00043     if (path == "" || path == "/") {
00044         if (ConfMan.hasKey("rootpath", "cloud"))
00045             addItem(itemsList, IT_DIRECTORY, "/root/", _("File system root"));
00046         addItem(itemsList, IT_DIRECTORY, "/saves/", _("Saved games"));
00047         successResult.setVal("items", new Common::JSONValue(itemsList));
00048         return successResult;
00049     }
00050 
00051     if (HandlerUtils::hasForbiddenCombinations(path))
00052         return errorResult;
00053 
00054     Common::String prefixToRemove = "", prefixToAdd = "";
00055     if (!transformPath(path, prefixToRemove, prefixToAdd))
00056         return errorResult;
00057 
00058     Common::FSNode node = Common::FSNode(path);
00059     if (path == "/")
00060         node = node.getParent(); // absolute root
00061 
00062     if (!HandlerUtils::permittedPath(node.getPath()))
00063         return errorResult;
00064 
00065     if (!node.isDirectory())
00066         return errorResult;
00067 
00068     // list directory
00069     Common::FSList _nodeContent;
00070     if (!node.getChildren(_nodeContent, Common::FSNode::kListAll, false)) // do not show hidden files
00071         _nodeContent.clear();
00072     else
00073         Common::sort(_nodeContent.begin(), _nodeContent.end());
00074 
00075     // add parent directory link
00076     {
00077         Common::String filePath = path;
00078         if (filePath.hasPrefix(prefixToRemove))
00079             filePath.erase(0, prefixToRemove.size());
00080         if (filePath == "" || filePath == "/" || filePath == "\\")
00081             filePath = "/";
00082         else
00083             filePath = parentPath(prefixToAdd + filePath);
00084         addItem(itemsList, IT_PARENT_DIRECTORY, filePath, _("Parent directory"));
00085     }
00086 
00087     // fill the content
00088     for (Common::FSList::iterator i = _nodeContent.begin(); i != _nodeContent.end(); ++i) {
00089         Common::String name = i->getDisplayName();
00090         if (i->isDirectory()) name += "/";
00091 
00092         Common::String filePath = i->getPath();
00093         if (filePath.hasPrefix(prefixToRemove))
00094             filePath.erase(0, prefixToRemove.size());
00095         filePath = prefixToAdd + filePath;
00096 
00097         addItem(itemsList, detectType(i->isDirectory(), name), filePath, name);
00098     }
00099 
00100     successResult.setVal("items", new Common::JSONValue(itemsList));
00101     return successResult;
00102 }
00103 
00104 ListAjaxHandler::ItemType ListAjaxHandler::detectType(bool isDirectory, const Common::String &name) {
00105     if (isDirectory)
00106         return IT_DIRECTORY;
00107     if (name.hasSuffix(".txt"))
00108         return IT_TXT;
00109     if (name.hasSuffix(".zip"))
00110         return IT_ZIP;
00111     if (name.hasSuffix(".7z"))
00112         return IT_7Z;
00113     return IT_UNKNOWN;
00114 }
00115 
00116 void ListAjaxHandler::addItem(Common::JSONArray &responseItemsList, ItemType itemType, Common::String path, Common::String name, Common::String size) {
00117     Common::String icon;
00118     bool isDirectory = (itemType == IT_DIRECTORY || itemType == IT_PARENT_DIRECTORY);
00119     switch (itemType) {
00120     case IT_DIRECTORY:
00121         icon = "dir.png";
00122         break;
00123     case IT_PARENT_DIRECTORY:
00124         icon = "up.png";
00125         break;
00126     case IT_TXT:
00127         icon = "txt.png";
00128         break;
00129     case IT_ZIP:
00130         icon = "zip.png";
00131         break;
00132     case IT_7Z:
00133         icon = "7z.png";
00134         break;
00135     default:
00136         icon = "unk.png";
00137     }
00138 
00139     Common::JSONObject item;
00140     item.setVal("name", new Common::JSONValue(name));
00141     item.setVal("path", new Common::JSONValue(path));
00142     item.setVal("isDirectory", new Common::JSONValue(isDirectory));
00143     item.setVal("size", new Common::JSONValue(size));
00144     item.setVal("icon", new Common::JSONValue(icon));
00145     responseItemsList.push_back(new Common::JSONValue(item));
00146 }
00147 
00149 
00150 void ListAjaxHandler::handle(Client &client) {
00151     Common::String path = client.queryParameter("path");
00152     Common::JSONValue jsonResponse = listDirectory(path);
00153     Common::String response = jsonResponse.stringify(true);
00154     LocalWebserver::setClientGetHandler(client, response);
00155 }
00156 
00157 } // End of namespace Networking


Generated on Sat May 25 2019 05:00:48 for ResidualVM by doxygen 1.7.1
curved edge   curved edge