diff options
author | Jan Holesovsky <kendy@collabora.com> | 2017-03-23 21:27:18 +0100 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2017-03-24 16:25:03 +0100 |
commit | c68a71026341b586e594048b42d18e2a94ad52d2 (patch) | |
tree | dd968df2a7bfc77cca4fc05aad6363fbb658e25b | |
parent | d8b6c5ef9093347ce19e561d28530685a5ce10e1 (diff) |
lok: Allow setting of the language during load.
Change-Id: I9dbb62950e639376c26122ceb9fcec2982b3ca82
-rw-r--r-- | desktop/CppunitTest_desktop_app.mk | 1 | ||||
-rw-r--r-- | desktop/inc/lib/init.hxx | 5 | ||||
-rw-r--r-- | desktop/qa/desktop_lib/test_desktop_lib.cxx | 30 | ||||
-rw-r--r-- | desktop/source/lib/init.cxx | 60 | ||||
-rw-r--r-- | include/LibreOfficeKit/LibreOfficeKit.hxx | 3 |
5 files changed, 98 insertions, 1 deletions
diff --git a/desktop/CppunitTest_desktop_app.mk b/desktop/CppunitTest_desktop_app.mk index b809b6a7e8c6..1e7e3e0813c5 100644 --- a/desktop/CppunitTest_desktop_app.mk +++ b/desktop/CppunitTest_desktop_app.mk @@ -34,6 +34,7 @@ $(eval $(call gb_CppunitTest_use_libraries,desktop_app, \ salhelper \ sb \ sfx \ + sofficeapp \ svl \ svx \ svxcore \ diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx index bfe9954c3308..2dd4b51c8942 100644 --- a/desktop/inc/lib/init.hxx +++ b/desktop/inc/lib/init.hxx @@ -98,6 +98,11 @@ namespace desktop { return (mOptionalFeatures & feature) != 0; } }; + + /// Helper function to extract the value from parameters delimited by + /// comma, like: Name1=Value1,Name2=Value2,Name3=Value3. + /// @param rOptions When exctacted, the Param=Value is removed from it. + DESKTOP_DLLPUBLIC OUString extractParameter(OUString& aOptions, const OUString& rName); } #endif diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx index 0f2d41c7f03f..20958267a291 100644 --- a/desktop/qa/desktop_lib/test_desktop_lib.cxx +++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx @@ -113,6 +113,7 @@ public: void testCommentsImpress(); void testCommentsCallbacksWriter(); void testRunMacro(); + void testExtractParameter(); CPPUNIT_TEST_SUITE(DesktopLOKTest); CPPUNIT_TEST(testGetStyles); @@ -153,6 +154,7 @@ public: CPPUNIT_TEST(testCommentsImpress); CPPUNIT_TEST(testCommentsCallbacksWriter); CPPUNIT_TEST(testRunMacro); + CPPUNIT_TEST(testExtractParameter); CPPUNIT_TEST_SUITE_END(); uno::Reference<lang::XComponent> mxComponent; @@ -2160,6 +2162,34 @@ void DesktopLOKTest::testRunMacro() CPPUNIT_ASSERT(!bNonExistentMacro); } +void DesktopLOKTest::testExtractParameter() +{ + OUString aOptions("Language=de-DE"); + OUString aValue = extractParameter(aOptions, "Language"); + CPPUNIT_ASSERT_EQUAL(OUString("de-DE"), aValue); + CPPUNIT_ASSERT_EQUAL(OUString(), aOptions); + + aOptions = "Language=en-US,Something"; + aValue = extractParameter(aOptions, "Language"); + CPPUNIT_ASSERT_EQUAL(OUString("en-US"), aValue); + CPPUNIT_ASSERT_EQUAL(OUString("Something"), aOptions); + + aOptions = "SomethingElse,Language=cs-CZ"; + aValue = extractParameter(aOptions, "Language"); + CPPUNIT_ASSERT_EQUAL(OUString("cs-CZ"), aValue); + CPPUNIT_ASSERT_EQUAL(OUString("SomethingElse"), aOptions); + + aOptions = "Something1,Language=hu-HU,Something2"; + aValue = extractParameter(aOptions, "Language"); + CPPUNIT_ASSERT_EQUAL(OUString("hu-HU"), aValue); + CPPUNIT_ASSERT_EQUAL(OUString("Something1,Something2"), aOptions); + + aOptions = "Something1,Something2=blah,Something3"; + aValue = extractParameter(aOptions, "Language"); + CPPUNIT_ASSERT_EQUAL(OUString(), aValue); + CPPUNIT_ASSERT_EQUAL(OUString("Something1,Something2=blah,Something3"), aOptions); +} + CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 3500d2b7673e..9fcea7a1c5a5 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -472,6 +472,50 @@ int lcl_getViewId(const std::string& payload) } // end anonymous namespace +// Could be anonymous in principle, but for the unit testing purposes, we +// declare it in init.hxx. +OUString desktop::extractParameter(OUString& rOptions, const OUString& rName) +{ + OUString aValue; + + OUString aNameEquals(rName + "="); + OUString aCommaNameEquals("," + rName + "="); + + int nIndex = -1; + if (rOptions.startsWith(aNameEquals)) + { + size_t nLen = aNameEquals.getLength(); + int nComma = rOptions.indexOf(",", nLen); + if (nComma >= 0) + { + aValue = rOptions.copy(nLen, nComma - nLen); + rOptions = rOptions.copy(nComma + 1); + } + else + { + aValue = rOptions.copy(nLen); + rOptions.clear(); + } + } + else if ((nIndex = rOptions.indexOf(aCommaNameEquals)) >= 0) + { + size_t nLen = aCommaNameEquals.getLength(); + int nComma = rOptions.indexOf(",", nIndex + nLen); + if (nComma >= 0) + { + aValue = rOptions.copy(nIndex + nLen, nComma - nIndex - nLen); + rOptions = rOptions.copy(0, nIndex) + rOptions.copy(nComma); + } + else + { + aValue = rOptions.copy(nIndex + nLen); + rOptions = rOptions.copy(0, nIndex); + } + } + + return aValue; +} + extern "C" { @@ -1164,10 +1208,24 @@ static LibreOfficeKitDocument* lo_documentLoadWithOptions(LibreOfficeKit* pThis, try { + // 'Language=...' is an option that LOK consumes by itself, and does + // not pass it as a parameter to the filter + OUString aOptions = getUString(pOptions); + OUString aLanguage = extractParameter(aOptions, "Language"); + + if (!aLanguage.isEmpty()) + { + // use with care - it sets it for the entire core, not just the + // document + SvtSysLocaleOptions aSysLocaleOptions; + aSysLocaleOptions.SetLocaleConfigString(aLanguage); + aSysLocaleOptions.SetUILocaleConfigString(aLanguage); + } + uno::Sequence<css::beans::PropertyValue> aFilterOptions(2); aFilterOptions[0] = css::beans::PropertyValue( OUString("FilterOptions"), 0, - uno::makeAny(OUString::createFromAscii(pOptions)), + uno::makeAny(aOptions), beans::PropertyState_DIRECT_VALUE); rtl::Reference<LOKInteractionHandler> const pInteraction( diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx index 12c042886f97..72fb7c2b3882 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.hxx +++ b/include/LibreOfficeKit/LibreOfficeKit.hxx @@ -487,6 +487,9 @@ public: * * @param pUrl the URL of the document to load * @param pFilterOptions options for the import filter, e.g. SkipImages. + * Another useful FilterOption is "Language=...". It is consumed + * by the documentLoad() itself, and when provided, LibreOfficeKit + * switches the language accordingly first. * @since pFilterOptions argument added in LibreOffice 5.0 */ inline Document* documentLoad(const char* pUrl, const char* pFilterOptions = NULL) |