sw/qa/uibase/uno/uno.cxx | 28 ++++++++++++++++++++++++++ sw/source/uibase/uno/loktxdoc.cxx | 40 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-)
New commits: commit ca4f51aad22809d94ecb9e7e8f53227171dea79f Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Mon Jan 9 13:53:35 2023 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Wed Jan 11 12:46:15 2023 +0000 sw, lok: implement a getCommandValues(Sections) There was no LOK API to get a list of all sections where the name matches a certain prefix. This is useful in case the API client wants to know what previously inserted sections were deleted by the user as part of deleting text content. Add a new getCommandValues(".uno:Sections") that returns the names of matching sections. Do not return the section 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 bibliography items with sections. (cherry picked from commit 2ddd41b420cea7f1b988f0b8acbca564b2811382) Change-Id: If4f02d2a27f2328020934b319d30561aeaaf6612 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145279 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Justin Luth <jl...@mail.com> diff --git a/sw/qa/uibase/uno/uno.cxx b/sw/qa/uibase/uno/uno.cxx index 26e912fcfb2b..300278717664 100644 --- a/sw/qa/uibase/uno/uno.cxx +++ b/sw/qa/uibase/uno/uno.cxx @@ -308,6 +308,34 @@ CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetTextFormField) field.get<std::string>("command")); } +CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetSections) +{ + // Given a document with a section: + createSwDoc(); + uno::Sequence<css::beans::PropertyValue> aArgs = { + comphelper::makePropertyValue( + "RegionName", uno::Any(OUString("ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDRfiit6mXBc"))), + comphelper::makePropertyValue("Content", uno::Any(OUString("<p>aaa</p><p>bbb</p>"))), + }; + dispatchCommand(mxComponent, ".uno:InsertSection", aArgs); + + // When asking for a list of section names: + tools::JsonWriter aJsonWriter; + OString aCommand(".uno:Sections?namePrefix=ZOTERO_BIBL"); + auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get()); + pXTextDocument->getCommandValues(aJsonWriter, aCommand); + + // Make sure we find our just inserted section: + 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 (sections) + // i.e. the returned JSON was an empty object. + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aTree.get_child("sections").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 d1b31238dff8..30722fb6af69 100644 --- a/sw/source/uibase/uno/loktxdoc.cxx +++ b/sw/source/uibase/uno/loktxdoc.cxx @@ -292,13 +292,44 @@ void GetFields(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell, rJsonWriter.put("name", pRefMark->GetRefName()); } } + +/// Implements getCommandValues(".uno:Sections"). +/// +/// Parameters: +/// +/// - namePrefix: field name prefix to not return all sections +void GetSections(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; + } + } + + SwDoc* pDoc = pDocShell->GetDoc(); + tools::ScopedJsonWriterArray aBookmarks = rJsonWriter.startArray("sections"); + for (const auto& pSection : pDoc->GetSections()) + { + if (!pSection->GetName().startsWith(aNamePrefix)) + { + continue; + } + + tools::ScopedJsonWriterStruct aProperty = rJsonWriter.startStruct(); + rJsonWriter.put("name", pSection->GetName()); + } +} } bool SwXTextDocument::supportsCommandValues(const OUString& rCommand) { static const std::initializer_list<std::u16string_view> vForward - = { u"TextFormFields", u"TextFormField", u"SetDocumentProperties", u"Bookmarks", - u"Fields" }; + = { u"TextFormFields", u"TextFormField", u"SetDocumentProperties", + u"Bookmarks", u"Fields", u"Sections" }; return std::find(vForward.begin(), vForward.end(), rCommand) != vForward.end(); } @@ -312,6 +343,7 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& rJsonWriter, const OSt static constexpr OStringLiteral aSetDocumentProperties(".uno:SetDocumentProperties"); static constexpr OStringLiteral aBookmarks(".uno:Bookmarks"); static constexpr OStringLiteral aFields(".uno:Fields"); + static constexpr OStringLiteral aSections(".uno:Sections"); INetURLObject aParser(OUString::fromUtf8(rCommand)); OUString aArguments = aParser.GetParam(); @@ -355,6 +387,10 @@ void SwXTextDocument::getCommandValues(tools::JsonWriter& rJsonWriter, const OSt { GetFields(rJsonWriter, m_pDocShell, aMap); } + else if (o3tl::starts_with(rCommand, aSections)) + { + GetSections(rJsonWriter, m_pDocShell, aMap); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */