diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-12-07 09:35:15 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-12-07 12:30:52 +0000 |
commit | e0bf2712aa9e240748534e3a7498d41c8eeeb9d7 (patch) | |
tree | 795541f4de9d3dc5e7aaa641ca35e2d4f57bf6e2 | |
parent | 36a554e1f1c4e924a2cbc2df5a4f164fa31e3d6b (diff) |
sw, lok: implement a getCommandValues(Bookmarks)
There was no LOK API to get a list of all bookmarks where the name
matches a certain prefix.
This is useful in case the API client wants to know what previously
inserted bookmarks were deleted by the user as part of deleting text
content.
Add a new getCommandValues(".uno:Bookmarks") that returns the names of
matching bookmarks. Do not return the bookmark text, assuming that would
be updated by the API client anyway.
In practice this is needed by Zotero in case it wants to model its
citations with bookmarks.
Change-Id: I42a544c3c64496519eec6826b58a310ec86dee74
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143764
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | desktop/source/lib/init.cxx | 3 | ||||
-rw-r--r-- | sw/qa/uibase/uno/uno.cxx | 40 | ||||
-rw-r--r-- | sw/source/uibase/uno/loktxdoc.cxx | 41 |
3 files changed, 81 insertions, 3 deletions
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 734f76f76362..c777bc7bb731 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -5704,7 +5704,8 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCo static constexpr OStringLiteral aFontSubset(".uno:FontSubset&name="); static const std::initializer_list<std::u16string_view> vForward = { u"TextFormFields", - u"SetDocumentProperties" + u"SetDocumentProperties", + u"Bookmarks" }; if (!strcmp(pCommand, ".uno:LanguageStatus")) diff --git a/sw/qa/uibase/uno/uno.cxx b/sw/qa/uibase/uno/uno.cxx index 8e6b06453d86..e0806672e6af 100644 --- a/sw/qa/uibase/uno/uno.cxx +++ b/sw/qa/uibase/uno/uno.cxx @@ -236,6 +236,46 @@ CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetDocumentProperties) aTree.get_child("userDefinedProperties").count("")); } +CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetBookmarks) +{ + // Given a document with 3 bookmarks: 2 zotero references and a zotero bibliography: + createSwDoc(); + { + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BREF_1"))), + }; + dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs); + } + { + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BREF_2"))), + }; + dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs); + } + { + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BIBL"))), + }; + dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs); + } + + // When getting the reference bookmarks: + tools::JsonWriter aJsonWriter; + std::string_view aCommand(".uno:Bookmarks?namePrefix=ZOTERO_BREF_"); + auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get()); + pXTextDocument->getCommandValues(aJsonWriter, aCommand); + + // Then make sure we get the 2 references but not the bibliography: + std::unique_ptr<char[], o3tl::free_delete> pJSON(aJsonWriter.extractData()); + std::stringstream aStream(pJSON.get()); + boost::property_tree::ptree aTree; + boost::property_tree::read_json(aStream, aTree); + // Without the accompanying fix in place, this test would have failed with: + // - No such node (bookmarks) + // i.e. the returned JSON was just empty. + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aTree.get_child("bookmarks").count("")); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/uibase/uno/loktxdoc.cxx b/sw/source/uibase/uno/loktxdoc.cxx index cf58f1ca4a7b..5381fd5c1dfe 100644 --- a/sw/source/uibase/uno/loktxdoc.cxx +++ b/sw/source/uibase/uno/loktxdoc.cxx @@ -99,7 +99,7 @@ void GetTextFormFields(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell, /// /// Parameters: /// -/// - namePrefix: field name prefix not not return all user-defined properties +/// - namePrefix: field name prefix to not return all user-defined properties void GetDocumentProperties(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell, const std::map<OUString, OUString>& rArguments) { @@ -138,6 +138,38 @@ void GetDocumentProperties(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell rJsonWriter.put("value", aValue); } } + +/// Implements getCommandValues(".uno:Bookmarks"). +/// +/// Parameters: +/// +/// - namePrefix: bookmark name prefix to not return all bookmarks +void GetBookmarks(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell, + const std::map<OUString, OUString>& rArguments) +{ + OUString aNamePrefix; + { + auto it = rArguments.find("namePrefix"); + if (it != rArguments.end()) + { + aNamePrefix = it->second; + } + } + + IDocumentMarkAccess& rIDMA = *pDocShell->GetDoc()->getIDocumentMarkAccess(); + tools::ScopedJsonWriterArray aBookmarks = rJsonWriter.startArray("bookmarks"); + for (auto it = rIDMA.getBookmarksBegin(); it != rIDMA.getBookmarksEnd(); ++it) + { + sw::mark::IMark* pMark = *it; + if (!pMark->GetName().startsWith(aNamePrefix)) + { + continue; + } + + tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct(); + rJsonWriter.put("name", pMark->GetName()); + } +} } void SwXTextDocument::getCommandValues(tools::JsonWriter& rJsonWriter, std::string_view rCommand) @@ -146,6 +178,7 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& rJsonWriter, std::stri static constexpr OStringLiteral aTextFormFields(".uno:TextFormFields"); static constexpr OStringLiteral aSetDocumentProperties(".uno:SetDocumentProperties"); + static constexpr OStringLiteral aBookmarks(".uno:Bookmarks"); INetURLObject aParser(OUString::fromUtf8(rCommand)); OUString aArguments = aParser.GetParam(); @@ -173,10 +206,14 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& rJsonWriter, std::stri { GetTextFormFields(rJsonWriter, m_pDocShell, aMap); } - if (o3tl::starts_with(rCommand, aSetDocumentProperties)) + else if (o3tl::starts_with(rCommand, aSetDocumentProperties)) { GetDocumentProperties(rJsonWriter, m_pDocShell, aMap); } + else if (o3tl::starts_with(rCommand, aBookmarks)) + { + GetBookmarks(rJsonWriter, m_pDocShell, aMap); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |