tdf#137810 sw: fix SwXNumberingRules setting CharStyleName

During ODF import, due to removal of the pDocShell parameter, this hits

            else
                rCharStyleName = sCharFormatName;

while setting the "CharStyleName" property and later
GetNumberingRuleByIndex() prefers m_sNewCharStyleNames over the
format set in the SwCharFormat??

Also, "BulletFontName" has a similar problem; otoh "HeadingStyleName"
only makes sense on chapter numbering.

The m_pDoc and m_pDocShell members are such a WTF.

(regression from ae0e4a6ba9be2fa99ac2be8e20157806e36209b2)

Change-Id: I9d4d4cd7aeb7e6e29221d53facaff213fd4e35a5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115495
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
diff --git a/sw/inc/unosett.hxx b/sw/inc/unosett.hxx
index 92094ff..d5351a9 100644
--- a/sw/inc/unosett.hxx
+++ b/sw/inc/unosett.hxx
@@ -151,16 +151,16 @@ private:
    OUString                    m_sNewCharStyleNames[MAXLEVEL];
    OUString                    m_sNewBulletFontNames[MAXLEVEL];
    OUString                    m_sCreatedNumRuleName; //connects to a numbering in SwDoc
    SwDoc*                      m_pDoc;
    SwDoc*                      m_pDoc; // Only if *not* used as chapter numbering.
    SwDocShell*                 m_pDocShell; // Only if used as chapter numbering.
    SwNumRule*                  m_pNumRule;
    const SfxItemPropertySet*   m_pPropertySet;
    bool                        m_bOwnNumRuleCreated;
protected:
    SwXNumberingRules(SwDocShell& rDocSh);  // chapter numbering
    virtual ~SwXNumberingRules() override;

public:
    SwXNumberingRules(SwDocShell& rDocSh);  // chapter numbering
    SwXNumberingRules(const SwNumRule& rRule, SwDoc* doc = nullptr); // NumRule for paragraphs, numbering styles
    SwXNumberingRules(SwDoc& rDoc); //create a new instance

@@ -222,6 +222,7 @@ public:
            OUString *const pHeadingStyleName,
            OUString *const pParagraphStyleName,
            SwDoc *const pDoc,
            SwDocShell *const pDocShell,
            css::uno::Sequence<css::beans::PropertyValue> const& rProperties);

};
diff --git a/sw/qa/extras/unowriter/unowriter.cxx b/sw/qa/extras/unowriter/unowriter.cxx
index f0ed93e..dbee7d4 100644
--- a/sw/qa/extras/unowriter/unowriter.cxx
+++ b/sw/qa/extras/unowriter/unowriter.cxx
@@ -959,6 +959,39 @@ CPPUNIT_TEST_FIXTURE(SwUnoWriter, testImageCommentAtChar)
    }
}

CPPUNIT_TEST_FIXTURE(SwUnoWriter, testChapterNumberingCharStyle)
{
    loadURL("private:factory/swriter", nullptr);

    uno::Reference<lang::XMultiServiceFactory> xDoc(mxComponent, uno::UNO_QUERY);
    uno::Reference<beans::XPropertySet> xStyle(
        xDoc->createInstance("com.sun.star.style.CharacterStyle"), uno::UNO_QUERY);
    uno::Reference<container::XNamed> xStyleN(xStyle, uno::UNO_QUERY);
    xStyle->setPropertyValue("CharColor", uno::makeAny(sal_Int32(0x00FF0000)));
    uno::Reference<style::XStyleFamiliesSupplier> xSFS(mxComponent, uno::UNO_QUERY);
    uno::Reference<container::XNameContainer> xStyles(
        xSFS->getStyleFamilies()->getByName("CharacterStyles"), uno::UNO_QUERY);
    xStyles->insertByName("red", uno::makeAny(xStyle));

    uno::Reference<text::XChapterNumberingSupplier> xCNS(mxComponent, uno::UNO_QUERY);
    uno::Reference<container::XIndexReplace> xOutline(xCNS->getChapterNumberingRules());
    {
        comphelper::SequenceAsHashMap hashMap(xOutline->getByIndex(0));
        hashMap["CharStyleName"] <<= OUString("red");
        uno::Sequence<beans::PropertyValue> props;
        hashMap >> props;
        xOutline->replaceByIndex(0, uno::makeAny(props));
    }
    // now rename the style
    xStyleN->setName("reddishred");
    {
        comphelper::SequenceAsHashMap hashMap(xOutline->getByIndex(0));

        // tdf#137810 this failed, was old value "red"
        CPPUNIT_ASSERT_EQUAL(OUString("reddishred"), hashMap["CharStyleName"].get<OUString>());
    }
}

CPPUNIT_TEST_FIXTURE(SwUnoWriter, testViewCursorPageStyle)
{
    // Load a document with 2 pages, but a single paragraph.
diff --git a/sw/source/core/unocore/unosett.cxx b/sw/source/core/unocore/unosett.cxx
index 073cee3..af6633e 100644
--- a/sw/source/core/unocore/unosett.cxx
+++ b/sw/source/core/unocore/unosett.cxx
@@ -1140,6 +1140,7 @@ void SwXNumberingRules::replaceByIndex(sal_Int32 nIndex, const uno::Any& rElemen
        SwXNumberingRules::SetNumberingRuleByIndex( aNumRule,
                            *rProperties, nIndex);
        // set character format if needed
        // this code appears to be dead - except when a style is assigned for BITMAP numbering?
        const SwCharFormats* pFormats = m_pDocShell->GetDoc()->GetCharFormats();
        const size_t nChCount = pFormats->size();
        for(sal_uInt16 i = 0; i < MAXLEVEL;i++)
@@ -1486,7 +1487,7 @@ void SwXNumberingRules::SetNumberingRuleByIndex(
    SetPropertiesToNumFormat(aFormat, m_sNewCharStyleNames[nIndex],
        &m_sNewBulletFontNames[nIndex],
        &sHeadingStyleName, &sParagraphStyleName,
        m_pDoc, rProperties);
        m_pDoc, m_pDocShell, rProperties);


    if (m_pDoc && !sParagraphStyleName.isEmpty())
@@ -1533,8 +1534,11 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
        OUString *const pHeadingStyleName,
        OUString *const pParagraphStyleName,
        SwDoc *const pDoc,
        SwDocShell *const pDocShell,
        const uno::Sequence<beans::PropertyValue>& rProperties)
{
    assert(pDoc == nullptr || pDocShell == nullptr); // can't be both ordinary and chapter numbering

    bool bWrongArg = false;
    std::unique_ptr<SvxBrushItem> pSetBrush;
    std::unique_ptr<Size> pSetSize;
@@ -1582,14 +1586,15 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
            rProp.Value >>= uTmp;
            OUString sCharFormatName;
            SwStyleNameMapper::FillUIName( uTmp, sCharFormatName, SwGetPoolIdFromName::ChrFmt );
            SwDoc *const pLocalDoc = pDocShell ? pDocShell->GetDoc() : pDoc;
            if (sCharFormatName == UNO_NAME_CHARACTER_FORMAT_NONE)
            {
                rCharStyleName = aInvalidStyle;
                aFormat.SetCharFormat(nullptr);
            }
            else if(pDoc)
            else if (pLocalDoc)
            {
                const SwCharFormats* pFormats = pDoc->GetCharFormats();
                const SwCharFormats* pFormats = pLocalDoc->GetCharFormats();
                const size_t nChCount = pFormats->size();

                SwCharFormat* pCharFormat = nullptr;
@@ -1608,7 +1613,7 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
                    {

                        SfxStyleSheetBase* pBase;
                        SfxStyleSheetBasePool* pPool = pDoc->GetDocShell()->GetStyleSheetPool();
                        SfxStyleSheetBasePool* pPool = pLocalDoc->GetDocShell()->GetStyleSheetPool();
                        pBase = pPool->Find(sCharFormatName, SfxStyleFamily::Char);
                        if(!pBase)
                            pBase = &pPool->Make(sCharFormatName, SfxStyleFamily::Char);
@@ -1620,7 +1625,7 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
                // If the character format has been found its name should not be in the
                // char style names array
                rCharStyleName.clear();
                }
            }
            else
                rCharStyleName = sCharFormatName;
        }
@@ -1773,8 +1778,8 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
        {
            OUString sBulletFontName;
            rProp.Value >>= sBulletFontName;
            SwDocShell* pLclDocShell = nullptr;
            if( !sBulletFontName.isEmpty() && pDoc && (pLclDocShell = pDoc->GetDocShell()) )
            SwDocShell *const pLclDocShell = pDocShell ? pDocShell : pDoc ? pDoc->GetDocShell() : nullptr;
            if (!sBulletFontName.isEmpty() && pLclDocShell)
            {
                const SvxFontListItem* pFontListItem =
                        static_cast<const SvxFontListItem* >(pLclDocShell
@@ -1873,7 +1878,8 @@ void SwXNumberingRules::SetPropertiesToNumFormat(
            }
            pSetVOrient->PutValue(rProp.Value, MID_VERTORIENT_ORIENT);
        }
        else if (rProp.Name == UNO_NAME_HEADING_STYLE_NAME)
        else if (rProp.Name == UNO_NAME_HEADING_STYLE_NAME
                && pDocShell) // only on chapter numbering
        {
            if (pHeadingStyleName)
            {
diff --git a/sw/source/uibase/config/StoredChapterNumbering.cxx b/sw/source/uibase/config/StoredChapterNumbering.cxx
index 07e6884..4833a6a 100644
--- a/sw/source/uibase/config/StoredChapterNumbering.cxx
+++ b/sw/source/uibase/config/StoredChapterNumbering.cxx
@@ -149,7 +149,7 @@ public:
        SwXNumberingRules::SetPropertiesToNumFormat(
            aNumberFormat,
            charStyleName,
            nullptr, nullptr, nullptr, nullptr,
            nullptr, nullptr, nullptr, nullptr, nullptr,
            props);
        SwNumRulesWithName *const pRules(GetOrCreateRules());
        pRules->SetNumFormat(nIndex, aNumberFormat, charStyleName);