tdf#103347 PPTX import: fix duplicated slide name

PPTX import uses title text as slide names, but this resulted
duplicated slide names in the case of repeating title
text, which forbidden by UNO API:

== GenericDrawPage.idl ==

    /** Gets or sets the name of this page.

        <p>Duplicated page names inside a document are not allowed.
     */
    [optional] interface com::sun::star::container::XNamed;

Now the import code skips the duplicated title text,
resulting default numbered slide names instead of the
duplicated title names.

Note: it seems, this hasn't fixed the jumping slide selection,
e.g. sometimes pressing Shift-F5 still presents the first slide
instead of the actual one.

Change-Id: I98511c3c9a59598ea113e7387db5202d7f8a7cd4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114810
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/oox/source/ppt/pptshape.cxx b/oox/source/ppt/pptshape.cxx
index 62a070e..2f346cc 100644
--- a/oox/source/ppt/pptshape.cxx
+++ b/oox/source/ppt/pptshape.cxx
@@ -26,6 +26,8 @@
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/drawing/XDrawPage.hpp>
#include <com/sun/star/drawing/XDrawPages.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/drawing/XShapes.hpp>
#include <com/sun/star/text/XText.hpp>
#include <basegfx/matrix/b2dhommatrix.hxx>
@@ -363,14 +365,29 @@ void PPTShape::addShape(
                setMasterTextListStyle( aMasterTextListStyle );

            Reference< XShape > xShape( createAndInsert( rFilterBase, sServiceName, pTheme, rxShapes, bClearText, bool(mpPlaceholder), aTransformation, getFillProperties() ) );
            // if exists and not duplicated, try to use the title text as slide name to help its re-use on UI
            if (!rSlidePersist.isMasterPage() && rSlidePersist.getPage().is() && mnSubType == XML_title)
             {
            {
                try
                {
                    OUString aTitleText;
                    Reference<XTextRange> xText(xShape, UNO_QUERY_THROW);
                    aTitleText = xText->getString();
                    if (!aTitleText.isEmpty() && (aTitleText.getLength() < 64))    // just a magic value, but we don't want to set slide names which are too long
                    Reference<drawing::XDrawPagesSupplier> xDPS(rFilterBase.getModel(), uno::UNO_QUERY_THROW);
                    Reference<drawing::XDrawPages> xDrawPages(xDPS->getDrawPages(), uno::UNO_SET_THROW);
                    sal_uInt32 nMaxPages = xDrawPages->getCount();
                    bool bUseTitleAsSlideName = !aTitleText.isEmpty() &&
                          // just a magic value, but we don't want to set slide names which are too long
                          aTitleText.getLength() < 64;
                    // check duplicated title name
                    for (sal_uInt32 nPage = 0; bUseTitleAsSlideName && nPage < nMaxPages; ++nPage)
                    {
                        Reference<XDrawPage> xDrawPage(xDrawPages->getByIndex(nPage), uno::UNO_QUERY);
                        Reference<container::XNamed> xNamed(xDrawPage, UNO_QUERY_THROW);
                        if ( xNamed->getName() == aTitleText )
                            bUseTitleAsSlideName = false;
                    }
                    if ( bUseTitleAsSlideName )
                    {
                        Reference<container::XNamed> xName(rSlidePersist.getPage(), UNO_QUERY_THROW);
                        xName->setName(aTitleText);
diff --git a/sd/qa/unit/data/pptx/tdf103347.pptx b/sd/qa/unit/data/pptx/tdf103347.pptx
new file mode 100755
index 0000000..1207851
--- /dev/null
+++ b/sd/qa/unit/data/pptx/tdf103347.pptx
Binary files differ
diff --git a/sd/qa/unit/import-tests.cxx b/sd/qa/unit/import-tests.cxx
index bbdc8b5..bf539df 100644
--- a/sd/qa/unit/import-tests.cxx
+++ b/sd/qa/unit/import-tests.cxx
@@ -85,6 +85,7 @@
#include <com/sun/star/text/GraphicCrop.hpp>
#include <com/sun/star/text/XTextCursor.hpp>
#include <com/sun/star/xml/dom/XDocument.hpp>
#include <com/sun/star/container/XNamed.hpp>

#include <stlpool.hxx>
#include <comphelper/processfactory.hxx>
@@ -233,6 +234,7 @@ public:
    void testTdf106638();
    void testTdf113198();
    void testTdf49856();
    void testTdf103347();

    CPPUNIT_TEST_SUITE(SdImportTest);

@@ -349,6 +351,7 @@ public:
    CPPUNIT_TEST(testMirroredGraphic);
    CPPUNIT_TEST(testGreysScaleGraphic);
    CPPUNIT_TEST(testTdf134210CropPosition);
    CPPUNIT_TEST(testTdf103347);

    CPPUNIT_TEST_SUITE_END();
};
@@ -3443,6 +3446,28 @@ void SdImportTest::testGreysScaleGraphic()
    xDocShRef->DoClose();
}

void SdImportTest::testTdf103347()
{
    sd::DrawDocShellRef xDocShRef
        = loadURL(m_directories.getURLFromSrc(u"/sd/qa/unit/data/pptx/tdf103347.pptx"), PPTX);
    uno::Reference<drawing::XDrawPagesSupplier> xDoc(xDocShRef->GetDoc()->getUnoModel(),
                                                     uno::UNO_QUERY_THROW);

    uno::Reference<drawing::XDrawPage> xPage1(xDoc->getDrawPages()->getByIndex(0), uno::UNO_QUERY);
    uno::Reference<container::XNamed> xNamed1(xPage1, uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("Hello"), xNamed1->getName());

    uno::Reference<drawing::XDrawPage> xPage2(xDoc->getDrawPages()->getByIndex(1), uno::UNO_QUERY);
    uno::Reference<container::XNamed> xNamed2(xPage2, uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("page2"), xNamed2->getName());

    uno::Reference<drawing::XDrawPage> xPage3(xDoc->getDrawPages()->getByIndex(2), uno::UNO_QUERY);
    uno::Reference<container::XNamed> xNamed3(xPage3, uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("page3"), xNamed3->getName());

    xDocShRef->DoClose();
}

CPPUNIT_TEST_SUITE_REGISTRATION(SdImportTest);

CPPUNIT_PLUGIN_IMPLEMENT();