desktop/source/lib/init.cxx       |    3 +-
 sw/qa/uibase/uno/uno.cxx          |   40 +++++++++++++++++++++++++++++++++++++
 sw/source/uibase/uno/loktxdoc.cxx |   41 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 81 insertions(+), 3 deletions(-)

New commits:
commit 9c4cd504dba88a8542691480404e6d3b67c81f79
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Dec 7 09:35:15 2022 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Thu Dec 8 07:07:46 2022 +0000

    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.
    
    (cherry picked from commit e0bf2712aa9e240748534e3a7498d41c8eeeb9d7)
    
    Change-Id: I42a544c3c64496519eec6826b58a310ec86dee74
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143790
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 1331aac9c69d..f398e23d91b7 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -5727,7 +5727,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 d8bb4dc68fba..1664f18c5dda 100644
--- a/sw/qa/uibase/uno/uno.cxx
+++ b/sw/qa/uibase/uno/uno.cxx
@@ -174,6 +174,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;
+    OString 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 b5716aeb925e..fd6d05eef132 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, const 
OString& rCommand)
@@ -146,6 +178,7 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& 
rJsonWriter, const OSt
 
     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, const OSt
     {
         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: */

Reply via email to