tdf#108188, tdf#131190 - Verify that the page style is actually used

Change-Id: I007d26c6fcaf60d2a2378cc00e630da447f95a8a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134022
Tested-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/sc/qa/unit/bugfix-test.cxx b/sc/qa/unit/bugfix-test.cxx
index 3b3cc27..2975b0b 100644
--- a/sc/qa/unit/bugfix-test.cxx
+++ b/sc/qa/unit/bugfix-test.cxx
@@ -29,6 +29,7 @@
#include <svx/svdpage.hxx>
#include <svx/svdomeas.hxx>
#include <userdat.hxx>
#include <stlpool.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
@@ -63,6 +64,7 @@ public:
    void testTdf129789();
    void testTdf130725();
    void testTdf104502_hiddenColsCountedInPageCount();
    void testTdf108188_pagestyle();

    CPPUNIT_TEST_SUITE(ScFiltersTest);
    CPPUNIT_TEST(testTdf137576_Measureline);
@@ -87,6 +89,7 @@ public:
    CPPUNIT_TEST(testTdf129789);
    CPPUNIT_TEST(testTdf130725);
    CPPUNIT_TEST(testTdf104502_hiddenColsCountedInPageCount);
    CPPUNIT_TEST(testTdf108188_pagestyle);
    CPPUNIT_TEST_SUITE_END();

private:
@@ -726,6 +729,24 @@ void ScFiltersTest::testTdf104502_hiddenColsCountedInPageCount()

    xShell->DoClose();
}
void ScFiltersTest::testTdf108188_pagestyle()
{
    ScDocShellRef xDocSh = loadDoc(u"tdf108188_pagestyle.", FORMAT_ODS);
    CPPUNIT_ASSERT(xDocSh);

    // Check if the user defined page style is present
    const OUString aTestPageStyle = "TestPageStyle";
    ScDocument& rDoc = xDocSh->GetDocument();
    CPPUNIT_ASSERT_EQUAL(aTestPageStyle, rDoc.GetPageStyle(0));

    // Without the accompanying fix in place, the page styles are always used
    ScStyleSheetPool* pStylePool = rDoc.GetStyleSheetPool();
    CPPUNIT_ASSERT(pStylePool->Find(aTestPageStyle, SfxStyleFamily::Page)->IsUsed());
    CPPUNIT_ASSERT(!pStylePool->Find("Default", SfxStyleFamily::Page)->IsUsed());

    xDocSh->DoClose();
}


ScFiltersTest::ScFiltersTest()
      : ScBootstrapFixture( "sc/qa/unit/data" )
diff --git a/sc/qa/unit/data/ods/tdf108188_pagestyle.ods b/sc/qa/unit/data/ods/tdf108188_pagestyle.ods
new file mode 100644
index 0000000..d0ecf9f
--- /dev/null
+++ b/sc/qa/unit/data/ods/tdf108188_pagestyle.ods
Binary files differ
diff --git a/sc/source/core/data/stlsheet.cxx b/sc/source/core/data/stlsheet.cxx
index e485b5f..fdf09e7 100644
--- a/sc/source/core/data/stlsheet.cxx
+++ b/sc/source/core/data/stlsheet.cxx
@@ -250,19 +250,32 @@ SfxItemSet& ScStyleSheet::GetItemSet()

bool ScStyleSheet::IsUsed() const
{
    if ( GetFamily() == SfxStyleFamily::Para )
    switch (GetFamily())
    {
        // Always query the document to let it decide if a rescan is necessary,
        // and store the state.
        ScDocument* pDoc = static_cast<ScStyleSheetPool*>(m_pPool)->GetDocument();
        if ( pDoc && pDoc->IsStyleSheetUsed( *this ) )
            eUsage = Usage::USED;
        else
            eUsage = Usage::NOTUSED;
        return eUsage == Usage::USED;
        case SfxStyleFamily::Para:
        {
            // Always query the document to let it decide if a rescan is necessary,
            // and store the state.
            ScDocument* pDoc = static_cast<ScStyleSheetPool*>(m_pPool)->GetDocument();
            if ( pDoc && pDoc->IsStyleSheetUsed( *this ) )
                eUsage = Usage::USED;
            else
                eUsage = Usage::NOTUSED;
            return eUsage == Usage::USED;
        }
        case SfxStyleFamily::Page:
        {
            // tdf#108188 - verify that the page style is actually used
            ScDocument* pDoc = static_cast<ScStyleSheetPool*>(m_pPool)->GetDocument();
            if (pDoc && pDoc->IsPageStyleInUse(GetName(), nullptr))
                eUsage = Usage::USED;
            else
                eUsage = Usage::NOTUSED;
            return eUsage == Usage::USED;
        }
        default:
            return true;
    }
    else
        return true;
}

void ScStyleSheet::Notify( SfxBroadcaster&, const SfxHint& rHint )
diff --git a/sfx2/source/dialog/StyleList.cxx b/sfx2/source/dialog/StyleList.cxx
index 6f8e354..a25050e 100644
--- a/sfx2/source/dialog/StyleList.cxx
+++ b/sfx2/source/dialog/StyleList.cxx
@@ -1321,21 +1321,6 @@ IMPL_LINK_NOARG(StyleList, EnableDelete, void*, void)
            {
                bEnableDelete = true;
            }
            else if (pStyle->GetFamily() == SfxStyleFamily::Page)
            {
                // Hack to allow Calc page styles to be deleted,
                // remove when IsUsed is fixed for Calc page styles.
                SfxViewFrame* pFrame = m_pCurObjShell->GetFrame();
                if (pFrame)
                {
                    uno::Reference<frame::XFrame> xFrame = pFrame->GetFrame().GetFrameInterface();
                    if (vcl::CommandInfoProvider::GetModuleIdentifier(xFrame)
                        == "com.sun.star.sheet.SpreadsheetDocument")
                    {
                        bEnableDelete = true;
                    }
                }
            }
        }
    }
    m_pParentDialog->EnableDel(bEnableDelete, this);