tdf#126700 allow replacing the default documents

Per default, a document opened by a user action will always open
in a new frame. For tdf#83722, this behaviour was extended to
documents created from templates.

But this currently also affects the default factory templates, if
these are replaced by a config setting with a real template, which
was not intentional.

So this patch introduces a new MediaDescriptor property, which
allows to mark a document as replaceable and automatically sets
it for factory default documents. If this property is set to true,
a document just acts as a placeholder while it's unmodified. I.e.
the next opened document from its frame will close and replace it.

For this backport the documentation in MediaDescriptor.idl is
dropped, so people won't rely on this as a feature before 7.0.

Change-Id: I45ffa8709f7cdda949fac78f3b363f120f0c4a03
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88257
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens <Thorsten.Behrens@CIB.de>
(cherry picked from commit 61e1e0413296928d929f99c0f006c6cbbcf4ac40)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88537
(cherry picked from commit d6188f8c3803490f75fbd1931a0bd6f821c4d700)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/88594
diff --git a/framework/source/loadenv/loadenv.cxx b/framework/source/loadenv/loadenv.cxx
index 0fb25aa..ee6cd716 100644
--- a/framework/source/loadenv/loadenv.cxx
+++ b/framework/source/loadenv/loadenv.cxx
@@ -1089,10 +1089,11 @@ bool LoadEnv::impl_loadContent()
    bool bHidden    = m_lMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_HIDDEN(), false);
    bool bMinimized = m_lMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_MINIMIZED(), false);
    bool bPreview   = m_lMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_PREVIEW(), false);
    css::uno::Reference< css::task::XStatusIndicator > xProgress  = m_lMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STATUSINDICATOR(), css::uno::Reference< css::task::XStatusIndicator >());

    if (!bHidden && !bMinimized && !bPreview)
    {
        css::uno::Reference<css::task::XStatusIndicator> xProgress = m_lMediaDescriptor.getUnpackedValueOrDefault(
            utl::MediaDescriptor::PROP_STATUSINDICATOR(), css::uno::Reference<css::task::XStatusIndicator>());
        if (!xProgress.is())
        {
            // Note: it's an optional interface!
@@ -1515,12 +1516,9 @@ css::uno::Reference< css::frame::XFrame > LoadEnv::impl_searchRecycleTarget()
    if (xOldDoc.is())
    {
        utl::MediaDescriptor lOldDocDescriptor(xModel->getArgs());
        bool bFromTemplate = lOldDocDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_ASTEMPLATE() , false);
        OUString sReferrer = lOldDocDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_REFERRER(), OUString());

        // tdf#83722: valid but unmodified document, either from template
        // or opened by the user (via File > New, referrer is set to private:user)
        if (bFromTemplate || (sReferrer == "private:user"))
        // replaceable document
        if (!lOldDocDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_REPLACEABLE(), false))
            return css::uno::Reference< css::frame::XFrame >();

        bReactivateOldControllerOnError = xOldDoc->suspend(true);
diff --git a/include/sfx2/sfxsids.hrc b/include/sfx2/sfxsids.hrc
index ca2349b..00ae92b 100644
--- a/include/sfx2/sfxsids.hrc
+++ b/include/sfx2/sfxsids.hrc
@@ -262,8 +262,8 @@ class SvxSearchItem;
#define SID_IS_REDACT_MODE                  (SID_SFX_START + 1733)
#define SID_REDACTION_STYLE                 (SID_SFX_START + 1734)
#define SID_DIALOG_PARENT                   (SID_SFX_START + 1735)
#define SID_REPLACEABLE                     (SID_SFX_START + 1739)

//      SID_SFX_free_START                  (SID_SFX_START + 1736)
//      SID_SFX_free_END                    (SID_SFX_START + 3999)

#define SID_OPEN_NEW_VIEW                   (SID_SFX_START + 520)
diff --git a/include/unotools/mediadescriptor.hxx b/include/unotools/mediadescriptor.hxx
index b568bd7..a37f544 100644
--- a/include/unotools/mediadescriptor.hxx
+++ b/include/unotools/mediadescriptor.hxx
@@ -84,6 +84,7 @@ class UNOTOOLS_DLLPUBLIC MediaDescriptor : public comphelper::SequenceAsHashMap
        static const OUString& PROP_PREVIEW();
        static const OUString& PROP_READONLY();
        static const OUString& PROP_REFERRER();
        static const OUString& PROP_REPLACEABLE();
        static const OUString& PROP_SALVAGEDFILE();
        static const OUString& PROP_STATUSINDICATOR();
        static const OUString& PROP_STREAM();
diff --git a/sfx2/source/appl/appuno.cxx b/sfx2/source/appl/appuno.cxx
index f272a93..68c9765 100644
--- a/sfx2/source/appl/appuno.cxx
+++ b/sfx2/source/appl/appuno.cxx
@@ -169,6 +169,7 @@ static char const sFailOnWarning[] = "FailOnWarning";
static char const sDocumentService[] = "DocumentService";
static char const sFilterProvider[] = "FilterProvider";
static char const sImageFilter[] = "ImageFilter";
static char const sReplaceable[] = "Replaceable";

static bool isMediaDescriptor( sal_uInt16 nSlotId )
{
@@ -852,6 +853,14 @@ void TransformParameters( sal_uInt16 nSlotId, const uno::Sequence<beans::Propert
                if (bOK)
                    rSet.Put(SfxStringItem(SID_FILTER_PROVIDER, aVal));
            }
            else if (aName == sReplaceable)
            {
                bool bVal = false;
                bool bOK = (rProp.Value >>= bVal);
                DBG_ASSERT(bOK, "invalid type for Replaceable");
                if (bOK)
                    rSet.Put(SfxBoolItem(SID_REPLACEABLE, bVal));
            }
#ifdef DBG_UTIL
            else
                --nFoundArgs;
@@ -1073,6 +1082,8 @@ void TransformItems( sal_uInt16 nSlotId, const SfxItemSet& rSet, uno::Sequence<b
                ++nAdditional;
            if ( rSet.GetItemState( SID_CONVERT_IMAGES ) == SfxItemState::SET )
                nAdditional++;
            if (rSet.GetItemState(SID_REPLACEABLE) == SfxItemState::SET)
                nAdditional++;

            // consider additional arguments
            nProps += nAdditional;
@@ -1230,6 +1241,8 @@ void TransformItems( sal_uInt16 nSlotId, const SfxItemSet& rSet, uno::Sequence<b
                        continue;
                    if ( nId == SID_SUGGESTEDSAVEASNAME )
                        continue;
                    if (nId == SID_REPLACEABLE)
                        continue;
               }

                OStringBuffer aDbg("Unknown item detected: ");
@@ -1625,6 +1638,11 @@ void TransformItems( sal_uInt16 nSlotId, const SfxItemSet& rSet, uno::Sequence<b
            pValue[nActProp].Name = sImageFilter;
            pValue[nActProp++].Value <<= static_cast<const SfxStringItem*>(pItem)->GetValue();
        }
        if (rSet.GetItemState(SID_REPLACEABLE, false, &pItem) == SfxItemState::SET)
        {
            pValue[nActProp].Name = sReplaceable;
            pValue[nActProp++].Value <<= static_cast<const SfxBoolItem*>(pItem)->GetValue();
        }
    }

    rArgs = aSequ;
diff --git a/sfx2/source/doc/sfxbasemodel.cxx b/sfx2/source/doc/sfxbasemodel.cxx
index e281ff9..353161e 100644
--- a/sfx2/source/doc/sfxbasemodel.cxx
+++ b/sfx2/source/doc/sfxbasemodel.cxx
@@ -1068,16 +1068,23 @@ void SAL_CALL SfxBaseModel::setArgs(const Sequence<beans::PropertyValue>& aArgs)
    for (int i = 0; i < aArgs.getLength(); i++)
    {
        OUString sValue;
        aArgs[i].Value >>= sValue;
        bool bValue;

        if (aArgs[i].Name == "SuggestedSaveAsName")
        {
            aArgs[i].Value >>= sValue;
            pMedium->GetItemSet()->Put(SfxStringItem(SID_SUGGESTEDSAVEASNAME, sValue));
        }
        else if (aArgs[i].Name == "SuggestedSaveAsDir")
        {
            aArgs[i].Value >>= sValue;
            pMedium->GetItemSet()->Put(SfxStringItem(SID_SUGGESTEDSAVEASDIR, sValue));
        }
        else if (aArgs[i].Name == "Replaceable")
        {
            aArgs[i].Value >>= bValue;
            pMedium->GetItemSet()->Put(SfxBoolItem(SID_REPLACEABLE, bValue));
        }
        else
        {
            throw lang::IllegalArgumentException("Setting property not supported: " + aArgs[i].Name,
diff --git a/sfx2/source/view/frmload.cxx b/sfx2/source/view/frmload.cxx
index 9d609cd..53d3cf2 100644
--- a/sfx2/source/view/frmload.cxx
+++ b/sfx2/source/view/frmload.cxx
@@ -624,7 +624,9 @@ sal_Bool SAL_CALL SfxFrameLoader_Impl::load( const Sequence< PropertyValue >& rA
    const OUString sURL = aDescriptor.getOrDefault( "URL", OUString() );
    const bool bIsFactoryURL = sURL.startsWith( "private:factory/" );
    bool bInitNewModel = bIsFactoryURL;
    if ( bIsFactoryURL && !bExternalModel )
    const bool bIsDefault = bIsFactoryURL && !bExternalModel;
    aDescriptor.put("Replaceable", bIsDefault);
    if (bIsDefault)
    {
        const OUString sFactory = sURL.copy( sizeof( "private:factory/" ) -1 );
        // special handling for some weird factory URLs a la private:factory/swriter?slot=21053
diff --git a/sw/qa/python/check_xmodel.py b/sw/qa/python/check_xmodel.py
index c5374c0..bea14998 100644
--- a/sw/qa/python/check_xmodel.py
+++ b/sw/qa/python/check_xmodel.py
@@ -32,12 +32,14 @@ class TestXModel(unittest.TestCase):

        p1 = PropertyValue(Name="SuggestedSaveAsName", Value="prettyFileName")
        p2 = PropertyValue(Name="SuggestedSaveAsDir", Value="/my/dir")
        xDoc.setArgs([p1, p2])
        p8 = PropertyValue(Name="Replaceable", Value=True)
        xDoc.setArgs([p1, p2, p8])

        # Make sure that all properties are returned with getArgs()
        args = xDoc.getArgs()
        self.assertTrue(p1 in args)
        self.assertTrue(p2 in args)
        self.assertTrue(p8 in args)

        xDoc.close(True)

diff --git a/unotools/source/misc/mediadescriptor.cxx b/unotools/source/misc/mediadescriptor.cxx
index 1c9aead..22f1835 100644
--- a/unotools/source/misc/mediadescriptor.cxx
+++ b/unotools/source/misc/mediadescriptor.cxx
@@ -222,6 +222,12 @@ const OUString& MediaDescriptor::PROP_REFERRER()
    return sProp;
}

const OUString& MediaDescriptor::PROP_REPLACEABLE()
{
    static const OUString sProp("Replaceable");
    return sProp;
}

const OUString& MediaDescriptor::PROP_STATUSINDICATOR()
{
    static const OUString sProp("StatusIndicator");