2020-06-03 11:46:42 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
|
|
|
|
#include "FileServer.hpp"
|
|
|
|
|
2020-12-07 02:02:06 -06:00
|
|
|
std::string FileServerRequestHandler::uiDefaultsToJSON(const std::string& uiDefaults, std::string& uiMode)
|
2020-06-03 11:46:42 -05:00
|
|
|
{
|
|
|
|
static std::string previousUIDefaults;
|
|
|
|
static std::string previousJSON("{}");
|
2020-12-07 02:02:06 -06:00
|
|
|
static std::string previousUIMode;
|
2020-06-03 11:46:42 -05:00
|
|
|
|
|
|
|
// early exit if we are serving the same thing
|
|
|
|
if (uiDefaults == previousUIDefaults)
|
2020-12-07 02:02:06 -06:00
|
|
|
{
|
|
|
|
uiMode = previousUIMode;
|
2020-06-03 11:46:42 -05:00
|
|
|
return previousJSON;
|
2020-12-07 02:02:06 -06:00
|
|
|
}
|
2020-06-03 11:46:42 -05:00
|
|
|
|
|
|
|
Poco::JSON::Object json;
|
|
|
|
Poco::JSON::Object textDefs;
|
|
|
|
Poco::JSON::Object spreadsheetDefs;
|
|
|
|
Poco::JSON::Object presentationDefs;
|
|
|
|
|
2020-12-07 02:02:06 -06:00
|
|
|
uiMode = "";
|
2020-06-03 11:46:42 -05:00
|
|
|
StringVector tokens(Util::tokenize(uiDefaults, ';'));
|
|
|
|
for (const auto& token : tokens)
|
|
|
|
{
|
|
|
|
StringVector keyValue(Util::tokenize(tokens.getParam(token), '='));
|
|
|
|
Poco::JSON::Object* currentDef = nullptr;
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
// detect the UIMode or component
|
|
|
|
if (keyValue[0] == "UIMode")
|
|
|
|
{
|
|
|
|
if (keyValue[1] == "classic" || keyValue[1] == "notebookbar")
|
2020-12-07 02:02:06 -06:00
|
|
|
{
|
2020-06-03 11:46:42 -05:00
|
|
|
json.set("uiMode", keyValue[1]);
|
2020-12-07 02:02:06 -06:00
|
|
|
uiMode = keyValue[1];
|
|
|
|
}
|
2020-06-03 11:46:42 -05:00
|
|
|
else
|
2021-02-17 07:50:48 -06:00
|
|
|
LOG_ERR("unknown UIMode value " << keyValue[1]);
|
2020-06-03 11:46:42 -05:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-14 06:38:45 -05:00
|
|
|
else if (keyValue.startsWith(0, "Text"))
|
2020-06-03 11:46:42 -05:00
|
|
|
{
|
|
|
|
currentDef = &textDefs;
|
|
|
|
key = keyValue[0].substr(4);
|
|
|
|
}
|
2021-10-14 06:38:45 -05:00
|
|
|
else if (keyValue.startsWith(0, "Spreadsheet"))
|
2020-06-03 11:46:42 -05:00
|
|
|
{
|
|
|
|
currentDef = &spreadsheetDefs;
|
|
|
|
key = keyValue[0].substr(11);
|
|
|
|
}
|
2021-10-14 06:38:45 -05:00
|
|
|
else if (keyValue.startsWith(0, "Presentation"))
|
2020-06-03 11:46:42 -05:00
|
|
|
{
|
|
|
|
currentDef = &presentationDefs;
|
|
|
|
key = keyValue[0].substr(12);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-17 07:50:48 -06:00
|
|
|
LOG_ERR("unknown UI default's component " << keyValue[0]);
|
2020-06-03 11:46:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(currentDef);
|
|
|
|
|
|
|
|
// detect the actual UI widget we want to hide or show
|
|
|
|
if (key == "Ruler" || key == "Sidebar" || key == "Statusbar")
|
|
|
|
{
|
|
|
|
bool value(true);
|
|
|
|
if (keyValue[1] == "false" || keyValue[1] == "False" || keyValue[1] == "0")
|
|
|
|
value = false;
|
|
|
|
|
|
|
|
currentDef->set("Show" + key, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-17 07:50:48 -06:00
|
|
|
LOG_ERR("unknown UI default " << keyValue[0]);
|
2020-06-03 11:46:42 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textDefs.size() > 0)
|
|
|
|
json.set("text", textDefs);
|
|
|
|
|
|
|
|
if (spreadsheetDefs.size() > 0)
|
|
|
|
json.set("spreadsheet", spreadsheetDefs);
|
|
|
|
|
|
|
|
if (presentationDefs.size() > 0)
|
|
|
|
json.set("presentation", presentationDefs);
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::JSON::Stringifier::stringify(json, oss);
|
|
|
|
|
|
|
|
previousUIDefaults = uiDefaults;
|
|
|
|
previousJSON = oss.str();
|
2020-12-07 02:02:06 -06:00
|
|
|
previousUIMode = uiMode;
|
2020-06-03 11:46:42 -05:00
|
|
|
|
|
|
|
return previousJSON;
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:51:50 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool isValidCss(const std::string& token)
|
|
|
|
{
|
|
|
|
const std::string forbidden = "<>{}&|\\\"^`'$[]";
|
|
|
|
for (auto c: token)
|
|
|
|
{
|
|
|
|
if (c < 0x20 || c >= 0x7F || forbidden.find(c) != std::string::npos)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 06:08:11 -05:00
|
|
|
std::string FileServerRequestHandler::cssVarsToStyle(const std::string& cssVars)
|
|
|
|
{
|
|
|
|
static std::string previousVars;
|
2020-12-03 20:14:30 -06:00
|
|
|
static std::string previousStyle;
|
2020-10-22 06:08:11 -05:00
|
|
|
|
|
|
|
// early exit if we are serving the same thing
|
|
|
|
if (cssVars == previousVars)
|
|
|
|
return previousStyle;
|
|
|
|
|
|
|
|
std::ostringstream styleOSS;
|
|
|
|
styleOSS << "<style>:root {";
|
|
|
|
StringVector tokens(Util::tokenize(cssVars, ';'));
|
|
|
|
for (const auto& token : tokens)
|
|
|
|
{
|
|
|
|
StringVector keyValue(Util::tokenize(tokens.getParam(token), '='));
|
|
|
|
if (keyValue.size() < 2)
|
|
|
|
{
|
2021-02-17 07:50:48 -06:00
|
|
|
LOG_ERR("Skipping the token [" << tokens.getParam(token) << "] since it does not have '='");
|
2020-10-22 06:08:11 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (keyValue.size() > 2)
|
|
|
|
{
|
2021-02-17 07:50:48 -06:00
|
|
|
LOG_ERR("Skipping the token [" << tokens.getParam(token) << "] since it has more than one '=' pair");
|
2020-10-22 06:08:11 -05:00
|
|
|
continue;
|
|
|
|
}
|
2021-06-30 09:51:50 -05:00
|
|
|
|
|
|
|
if (!isValidCss(tokens.getParam(token)))
|
|
|
|
{
|
|
|
|
LOG_WRN("Skipping the token [" << tokens.getParam(token) << "] since it contains forbidden characters");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-06 22:01:18 -06:00
|
|
|
styleOSS << keyValue[0] << ':' << keyValue[1] << ';';
|
2020-10-22 06:08:11 -05:00
|
|
|
}
|
|
|
|
styleOSS << "}</style>";
|
|
|
|
|
|
|
|
previousVars = cssVars;
|
|
|
|
previousStyle = styleOSS.str();
|
|
|
|
|
|
|
|
return previousStyle;
|
|
|
|
}
|
|
|
|
|
2020-06-03 11:46:42 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|