From 61ea11e51ceeaec7bf197a076cf40d96bd65ad07 Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Sun, 20 Oct 2019 10:06:20 -0400 Subject: [PATCH] lok: convert JSON to StringMap in sent DialogEvent function The StringMap type is used to the class UIObject methods to get state and execute actions. So the idea is the loleaflet client side send a raw JSON string: { ctrl: "id_control", cmd: "SELECT", ID: "item_id", ... // more parameters } Then it is transformed with a simple JSON to StringMap, finally it is dispatched to execute the actions. Change-Id: Icd628598fe46ae28b4afa3ca17ac75797c1b9308 Reviewed-on: https://gerrit.libreoffice.org/81313 Tested-by: Jenkins Reviewed-by: Henry Castro --- desktop/source/lib/init.cxx | 74 +++++++++++++++---------------------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 6cb4303d4439..3884dbc98882 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -413,6 +413,25 @@ std::vector desktop::jsonToPropertyValuesVector(const char return aArguments; } + +static StringMap jsonToStringMap(const char* pJSON) +{ + StringMap aArgs; + if (pJSON && pJSON[0] != '\0') + { + std::stringstream aStream(pJSON); + boost::property_tree::ptree aTree; + boost::property_tree::read_json(aStream, aTree); + + for (const auto& rPair : aTree) + { + aArgs[OUString::fromUtf8(rPair.first.c_str())] = OUString::fromUtf8(rPair.second.get_value(".").c_str()); + } + } + return aArgs; +} + + static boost::property_tree::ptree unoAnyToPropertyTree(const uno::Any& anyItem) { boost::property_tree::ptree aTree; @@ -3272,34 +3291,14 @@ static void doc_sendDialogEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nWin { SolarMutexGuard aGuard; - char* pCopy = strdup(pArguments); - if (!pCopy) - { - SetLastExceptionMsg("String copying error."); - return; - } - - char* pIdChar = strtok(pCopy, " "); - char* pOptionalEventType = strtok(nullptr, " "); - char* pOptionalData = strtok(nullptr, " "); - - if (!pIdChar) - { - SetLastExceptionMsg("Error parsing the command."); - free(pCopy); - return; - } - - OUString sId = OUString::createFromAscii(pIdChar); - + StringMap aMap(jsonToStringMap(pArguments)); VclPtr pWindow = vcl::Window::FindLOKWindow(nWindowId); if (!pWindow) { SetLastExceptionMsg("Document doesn't support dialog rendering, or window not found."); - free(pCopy); return; } - else + else if (aMap.find("id") != aMap.end()) { const OUString sClickAction("CLICK"); const OUString sSelectAction("SELECT"); @@ -3311,40 +3310,29 @@ static void doc_sendDialogEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nWin try { WindowUIObject aUIObject(pWindow); - std::unique_ptr pUIWindow(aUIObject.get_child(sId)); + std::unique_ptr pUIWindow(aUIObject.get_child(aMap["id"])); if (pUIWindow) { bool bIsClickAction = false; - StringMap aMap; - if (pOptionalEventType) { - if (strcmp(pOptionalEventType, "selected") == 0 && pOptionalData) + if (aMap.find("cmd") != aMap.end()) { + if (aMap["cmd"] == "selected") { - char* pPos = strtok(pOptionalData, ";"); - char* pText = strtok(nullptr, ";"); - - if (!pPos || !pText) - { - SetLastExceptionMsg("Error parsing the command."); - free(pCopy); - return; - } - - aMap["POS"] = OUString::createFromAscii(pPos); - aMap["TEXT"] = OUString::createFromAscii(pText); + aMap["POS"] = aMap["data"]; + aMap["TEXT"] = aMap["data"]; pUIWindow->execute(sSelectAction, aMap); } - else if (strcmp(pOptionalEventType, "plus") == 0) + else if (aMap["cmd"] == "plus") { pUIWindow->execute(sUpAction, aMap); } - else if (strcmp(pOptionalEventType, "minus") == 0) + else if (aMap["cmd"] == "minus") { pUIWindow->execute(sDownAction, aMap); } - else if (pOptionalData) + else if (aMap["cmd"] == "set") { - aMap["TEXT"] = OUString::createFromAscii(pOptionalData); + aMap["TEXT"] = aMap["data"]; pUIWindow->execute(sClearAction, aMap); pUIWindow->execute(sTypeAction, aMap); @@ -3364,8 +3352,6 @@ static void doc_sendDialogEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nWin pWindow->Hide(); pWindow->Show(); } - - free(pCopy); } static void doc_postUnoCommand(LibreOfficeKitDocument* pThis, const char* pCommand, const char* pArguments, bool bNotifyWhenFinished)