From ad049ffa1d3a22fac4864cae972690a1e46993c9 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 31 May 2016 09:11:52 +0200 Subject: tdf#77349 RTF import: automatically generate names for images if needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The DOC/ODT import can call SwDoc::SetAllUniqueFlyNames() at the end of the process to assign unique names to fly frames which lack a name. Add a similar (but much simpler) feature to the domain mapper to avoid empty image names in the DOCX/RTF import result, so it's easier to click on the items in Writer's navigator. (cherry picked from commit 526ed1f7dbd9150734edcb03727d49e1b1306f56) Change-Id: I432fc741f8d75d735e1dfe88daba50ba0797042d Reviewed-on: https://gerrit.libreoffice.org/25825 Tested-by: Jenkins Reviewed-by: Caolán McNamara Tested-by: Caolán McNamara --- sw/qa/extras/rtfimport/data/tdf77349.rtf | 3 +++ sw/qa/extras/rtfimport/rtfimport.cxx | 7 +++++++ writerfilter/source/dmapper/DomainMapper.cxx | 7 +++++++ writerfilter/source/dmapper/DomainMapper.hxx | 3 +++ writerfilter/source/dmapper/GraphicHelpers.cxx | 24 ++++++++++++++++++++++++ writerfilter/source/dmapper/GraphicHelpers.hxx | 11 +++++++++++ writerfilter/source/dmapper/GraphicImport.cxx | 11 ++++++----- 7 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 sw/qa/extras/rtfimport/data/tdf77349.rtf diff --git a/sw/qa/extras/rtfimport/data/tdf77349.rtf b/sw/qa/extras/rtfimport/data/tdf77349.rtf new file mode 100644 index 000000000000..1451b367823a --- /dev/null +++ b/sw/qa/extras/rtfimport/data/tdf77349.rtf @@ -0,0 +1,3 @@ +{\rtf1 +{\pict \pngblip \picw-64 \pich-1061137057 \picwgoal0 \pichgoal0 47494638396110001000d5ff00000000ffffffc0c0c0555f00ffffaafcfcfcf6f6f6eaeaeae6e6e6e4e4e4e3e3e3c2c2c2c1c1c1bcbcbcb5b5b5b3b3b3b0b0b0adadada5a5a5a2a2a2a1a1a19f9f9f9494948a8a8a8888888686867b7b7b6c6c6c5c5c5c4e4e4e4b4b4b4747474646463d3d3d3c3c3c2e2e2e2525251b1b1b18181810101009090906060603030300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021f90401000002002c0000000010001000000684408170482c0a06c8a4728924389f506833b281302a8e6b164b18103024c52111504cca67332102e0042e9a40d9319f8300a343c1200f54e47f7e2a00001e0b0a7d0d728a010d838400261a7c0d94947784252700127e9d159f6c8411140019080ea7a9a85f842122281612b1b3b25d6b1f29291d0fbbbdbc5d5e51c34e4cc64a46c94341003b} +\par } diff --git a/sw/qa/extras/rtfimport/rtfimport.cxx b/sw/qa/extras/rtfimport/rtfimport.cxx index 66b4c0410ff4..5fa67e93a2cd 100644 --- a/sw/qa/extras/rtfimport/rtfimport.cxx +++ b/sw/qa/extras/rtfimport/rtfimport.cxx @@ -2588,6 +2588,13 @@ DECLARE_RTFIMPORT_TEST(testTdf74795, "tdf74795.rtf") CPPUNIT_ASSERT_EQUAL(static_cast(0), getProperty(xCell, "LeftBorderDistance")); } +DECLARE_RTFIMPORT_TEST(testTdf77349, "tdf77349.rtf") +{ + uno::Reference xImage(getShape(1), uno::UNO_QUERY); + // This was empty: imported image wasn't named automatically. + CPPUNIT_ASSERT_EQUAL(OUString("graphic1"), xImage->getName()); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerfilter/source/dmapper/DomainMapper.cxx b/writerfilter/source/dmapper/DomainMapper.cxx index 840e0e40f3f2..80206df9244d 100644 --- a/writerfilter/source/dmapper/DomainMapper.cxx +++ b/writerfilter/source/dmapper/DomainMapper.cxx @@ -3562,6 +3562,13 @@ GraphicZOrderHelper* DomainMapper::graphicZOrderHelper() return zOrderHelper.get(); } +GraphicNamingHelper& DomainMapper::GetGraphicNamingHelper() +{ + if (m_pGraphicNamingHelper.get() == nullptr) + m_pGraphicNamingHelper.reset(new GraphicNamingHelper()); + return *m_pGraphicNamingHelper; +} + uno::Reference DomainMapper::PopPendingShape() { return m_pImpl->PopPendingShape(); diff --git a/writerfilter/source/dmapper/DomainMapper.hxx b/writerfilter/source/dmapper/DomainMapper.hxx index ddf1ac304509..305ec874f20b 100644 --- a/writerfilter/source/dmapper/DomainMapper.hxx +++ b/writerfilter/source/dmapper/DomainMapper.hxx @@ -62,6 +62,7 @@ class DomainMapper_Impl; class ListsManager; class StyleSheetTable; class GraphicZOrderHelper; +class GraphicNamingHelper; // different context types require different sprm handling (e.g. names) enum SprmType @@ -107,6 +108,7 @@ public: OUString getOrCreateCharStyle( PropertyValueVector_t& rCharProperties, bool bAlwaysCreate ); std::shared_ptr< StyleSheetTable > GetStyleSheetTable( ); GraphicZOrderHelper* graphicZOrderHelper(); + GraphicNamingHelper& GetGraphicNamingHelper(); /// Return the first from the pending (not inserted to the document) shapes, if there are any. css::uno::Reference PopPendingShape(); @@ -169,6 +171,7 @@ private: static sal_Unicode getFillCharFromValue(const sal_Int32 nIntValue); bool mbIsSplitPara; std::unique_ptr< GraphicZOrderHelper > zOrderHelper; + std::unique_ptr m_pGraphicNamingHelper; }; } // namespace dmapper diff --git a/writerfilter/source/dmapper/GraphicHelpers.cxx b/writerfilter/source/dmapper/GraphicHelpers.cxx index 7e21f0f2dfb1..3d410b2e809a 100644 --- a/writerfilter/source/dmapper/GraphicHelpers.cxx +++ b/writerfilter/source/dmapper/GraphicHelpers.cxx @@ -29,8 +29,12 @@ #include #include +#include +#include +#include #include +#include namespace writerfilter { namespace dmapper { @@ -284,6 +288,26 @@ sal_Int32 GraphicZOrderHelper::findZOrder( sal_Int32 relativeHeight, bool bOldSt return 0; // this should not(?) happen } +GraphicNamingHelper::GraphicNamingHelper() + : m_nCounter(0) +{ +} + +OUString GraphicNamingHelper::NameGraphic(const OUString& rTemplate) +{ + OUString aRet = rTemplate; + + if (aRet.isEmpty()) + { + // Empty template: then auto-generate a unique name. + std::unique_ptr pResMgr(ResMgr::CreateResMgr("svx", Application::GetSettings().GetUILanguageTag())); + OUString aPrefix(ResId(STR_ObjNameSingulGRAF, *pResMgr).toString()); + aRet += aPrefix + OUString::number(++m_nCounter); + } + + return aRet; +} + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/writerfilter/source/dmapper/GraphicHelpers.hxx b/writerfilter/source/dmapper/GraphicHelpers.hxx index 95c33b416c6e..ba16d46d4bc0 100644 --- a/writerfilter/source/dmapper/GraphicHelpers.hxx +++ b/writerfilter/source/dmapper/GraphicHelpers.hxx @@ -66,6 +66,17 @@ public: }; typedef std::shared_ptr WrapHandlerPtr; +/// Keeps track of the next available unique automatic name. +class GraphicNamingHelper +{ + int m_nCounter; + +public: + GraphicNamingHelper(); + /// Name a graphic based on rTemplate. + OUString NameGraphic(const OUString& rTemplate); +}; + } } #endif diff --git a/writerfilter/source/dmapper/GraphicImport.cxx b/writerfilter/source/dmapper/GraphicImport.cxx index 735687d7d3ee..034ccafa9350 100644 --- a/writerfilter/source/dmapper/GraphicImport.cxx +++ b/writerfilter/source/dmapper/GraphicImport.cxx @@ -388,11 +388,12 @@ public: { try { - if( !sName.isEmpty() ) - { - uno::Reference< container::XNamed > xNamed( xGraphicObjectProperties, uno::UNO_QUERY_THROW ); - xNamed->setName( sName ); - } + // Ask the graphic naming helper to find out the name for this + // object: It's around till the end of the import, so it remembers + // what's the first free name. + uno::Reference< container::XNamed > xNamed( xGraphicObjectProperties, uno::UNO_QUERY_THROW ); + xNamed->setName(rDomainMapper.GetGraphicNamingHelper().NameGraphic(sName)); + xGraphicObjectProperties->setPropertyValue(getPropertyName( PROP_DESCRIPTION ), uno::makeAny( sAlternativeText )); xGraphicObjectProperties->setPropertyValue(getPropertyName( PROP_TITLE ), -- cgit