Resolves: tdf#161430 reindex the correct style if there are duplicate names

Change-Id: I6d4e96faef3ec6caa038edf7595f91f20d964807
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168479
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/include/svl/IndexedStyleSheets.hxx b/include/svl/IndexedStyleSheets.hxx
index 685b958..1934bc1 100644
--- a/include/svl/IndexedStyleSheets.hxx
+++ b/include/svl/IndexedStyleSheets.hxx
@@ -145,7 +145,8 @@ public:
    void Clear(StyleSheetDisposer& cleanup);

    void Reindex();
    void ReindexOnNameChange(const OUString& rOldName, const OUString& rNewName);
    void ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName,
                             const OUString& rNewName);

    /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found. */
    SfxStyleSheetBase* GetNthStyleSheetThatMatchesPredicate(sal_Int32 n,
diff --git a/include/svl/style.hxx b/include/svl/style.hxx
index 018da0a..522ef24 100644
--- a/include/svl/style.hxx
+++ b/include/svl/style.hxx
@@ -275,7 +275,7 @@ public:
    virtual SfxStyleSheetBase*  Find( const OUString&, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All );

    void                        Reindex();
    void                        ReindexOnNameChange(const OUString& rOldName, const OUString& rNewName);
    void                        ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName, const OUString& rNewName);
    /** Add a style sheet.
     * Not an actual public function. Do not call it from non-subclasses.
     */
diff --git a/svl/source/items/IndexedStyleSheets.cxx b/svl/source/items/IndexedStyleSheets.cxx
index adce9bf1..0db2f20 100644
--- a/svl/source/items/IndexedStyleSheets.cxx
+++ b/svl/source/items/IndexedStyleSheets.cxx
@@ -69,14 +69,18 @@ IndexedStyleSheets::Reindex()
}

void
IndexedStyleSheets::ReindexOnNameChange(const OUString& rOldName, const OUString& rNewName)
IndexedStyleSheets::ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName, const OUString& rNewName)
{
    auto it = mPositionsByName.find(rOldName);
    if (it != mPositionsByName.end())
    std::pair<MapType::const_iterator, MapType::const_iterator> range = mPositionsByName.equal_range(rOldName);
    for (MapType::const_iterator it = range.first; it != range.second; ++it)
    {
        unsigned nPos = it->second;
        mPositionsByName.erase(it);
        mPositionsByName.insert(std::make_pair(rNewName, nPos));
        if (mStyleSheets[it->second].get() == &style)
        {
            unsigned nPos = it->second;
            mPositionsByName.erase(it);
            mPositionsByName.insert(std::make_pair(rNewName, nPos));
            break;
        }
    }
}

diff --git a/svl/source/items/style.cxx b/svl/source/items/style.cxx
index a43e326..3827b7c 100644
--- a/svl/source/items/style.cxx
+++ b/svl/source/items/style.cxx
@@ -178,7 +178,7 @@ bool SfxStyleSheetBase::SetName(const OUString& rName, bool bReIndexNow)
        aFollow = rName;
    aName = rName;
    if (bReIndexNow)
        m_pPool->ReindexOnNameChange(aOldName, rName);
        m_pPool->ReindexOnNameChange(*this, aOldName, rName);

    m_pPool->Broadcast( SfxStyleSheetModifiedHint( aOldName, *this ) );
    return true;
@@ -890,9 +890,9 @@ SfxStyleSheetBasePool::Reindex()
}

void
SfxStyleSheetBasePool::ReindexOnNameChange(const OUString& rOldName, const OUString& rNewName)
SfxStyleSheetBasePool::ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName, const OUString& rNewName)
{
    pImpl->mxIndexedStyleSheets->ReindexOnNameChange(rOldName, rNewName);
    pImpl->mxIndexedStyleSheets->ReindexOnNameChange(style, rOldName, rNewName);
}

const svl::IndexedStyleSheets&