Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/LibreOffice/sfx2/source/doc/   (Office von Apache Version 25.8.3.2©)  Datei vom 5.10.2025 mit Größe 161 kB image not shown  

Quelle  objstor.cxx   Sprache: C

 
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <config_features.h>

#include <sal/config.h>
#include <sal/log.hxx>

#include <cassert>

#include <svl/eitem.hxx>
#include <svl/stritem.hxx>
#include <svl/intitem.hxx>
#include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/frame/XModule.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XImporter.hpp>
#include <com/sun/star/document/XExporter.hpp>
#include <com/sun/star/packages/zip/ZipIOException.hpp>
#include <com/sun/star/task/XInteractionHandler.hpp>
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
#include <com/sun/star/document/MacroExecMode.hpp>
#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/EmbedStates.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>
#include <com/sun/star/embed/XEmbedPersist.hpp>
#include <com/sun/star/embed/XOptimizedStorage.hpp>
#include <com/sun/star/embed/XEncryptionProtectedStorage.hpp>
#include <com/sun/star/io/WrongFormatException.hpp>
#include <com/sun/star/io/XTruncate.hpp>
#include <com/sun/star/util/XModifiable.hpp>
#include <com/sun/star/util/RevisionTag.hpp>
#include <com/sun/star/security/DocumentDigitalSignatures.hpp>
#include <com/sun/star/text/XTextRange.hpp>
#include <com/sun/star/xml/crypto/CipherID.hpp>
#include <com/sun/star/xml/crypto/DigestID.hpp>
#include <com/sun/star/xml/crypto/KDFID.hpp>

#include <com/sun/star/document/XDocumentProperties.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <comphelper/fileformat.h>
#include <comphelper/processfactory.hxx>
#include <svtools/langtab.hxx>
#include <svtools/sfxecode.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/streamwrap.hxx>

#include <unotools/saveopt.hxx>
#include <unotools/useroptions.hxx>
#include <unotools/securityoptions.hxx>
#include <tools/urlobj.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <unotools/ucbhelper.hxx>
#include <unotools/tempfile.hxx>
#include <unotools/docinfohelper.hxx>
#include <unotools/mediadescriptor.hxx>
#include <ucbhelper/content.hxx>
#include <sot/storage.hxx>
#include <sot/exchange.hxx>
#include <sot/formats.hxx>
#include <comphelper/storagehelper.hxx>
#include <comphelper/documentconstants.hxx>
#include <comphelper/string.hxx>
#include <vcl/errinf.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <basic/modsizeexceeded.hxx>
#include <officecfg/Office/Common.hxx>
#include <osl/file.hxx>
#include <comphelper/scopeguard.hxx>
#include <comphelper/lok.hxx>
#include <i18nlangtag/languagetag.hxx>

#include <sfx2/signaturestate.hxx>
#include <sfx2/app.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/fcontnr.hxx>
#include <sfx2/docfilt.hxx>
#include <sfx2/docfac.hxx>
#include <appopen.hxx>
#include <objshimp.hxx>
#include <sfx2/lokhelper.hxx>
#include <sfx2/strings.hrc>
#include <sfx2/sfxsids.hrc>
#include <sfx2/dispatch.hxx>
#include <sfx2/sfxuno.hxx>
#include <sfx2/event.hxx>
#include <sfx2/infobar.hxx>
#include <fltoptint.hxx>
#include <sfx2/viewfrm.hxx>
#include "graphhelp.hxx"
#include <appbaslib.hxx>
#include <guisaveas.hxx>
#include "objstor.hxx"
#include "exoticfileloadexception.hxx"
#include <unicode/ucsdet.h>
#include <o3tl/string_view.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::task;
using namespace ::com::sun::star::document;
using namespace ::cppu;


static css::uno::Any getODFVersionAny(SvtSaveOptions::ODFSaneDefaultVersion v)
{
    if (v >= SvtSaveOptions::ODFSaneDefaultVersion::ODFSVER_014)
        return css::uno::Any(ODFVER_014_TEXT);
    else if (v >= SvtSaveOptions::ODFSaneDefaultVersion::ODFSVER_013)
        return css::uno::Any(ODFVER_013_TEXT);
    else
        return css::uno::Any(ODFVER_012_TEXT);
}


void impl_addToModelCollection(const css::uno::Reference< css::frame::XModel >& xModel)
{
    if (!xModel.is())
        return;

    const css::uno::Reference< css::uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
    css::uno::Reference< css::frame::XGlobalEventBroadcaster > xModelCollection =
        css::frame::theGlobalEventBroadcaster::get(xContext);
    try
    {
        xModelCollection->insert(css::uno::Any(xModel));
    }
    catch ( uno::Exception& )
    {
        SAL_WARN( "sfx.doc""The document seems to be in the collection already!" );
    }
}


bool SfxObjectShell::Save()
{
    SaveChildren();
    return true;
}


bool SfxObjectShell::SaveAs( SfxMedium& rMedium )
{
    return SaveAsChildren( rMedium );
}


bool SfxObjectShell::QuerySlotExecutable( sal_uInt16 /*nSlotId*/ )
{
    return true;
}

namespace sfx2 {

bool UseODFWholesomeEncryption(SvtSaveOptions::ODFSaneDefaultVersion const nODFVersion)
{
    return nODFVersion == SvtSaveOptions::ODFSVER_LATEST_EXTENDED;
}

// namespace sfx2

bool GetEncryptionData_Impl( const SfxItemSet* pSet, uno::Sequence< beans::NamedValue >&&nbsp;o_rEncryptionData )
{
    bool bResult = false;
    if ( pSet )
    {
        const SfxUnoAnyItem* pEncryptionDataItem = SfxItemSet::GetItem<SfxUnoAnyItem>(pSet, SID_ENCRYPTIONDATA, false);
        if ( pEncryptionDataItem )
        {
            pEncryptionDataItem->GetValue() >>= o_rEncryptionData;
            bResult = true;
        }
        else
        {
            const SfxStringItem* pPasswordItem = SfxItemSet::GetItem<SfxStringItem>(pSet, SID_PASSWORD, false);
            if ( pPasswordItem )
            {
                o_rEncryptionData = ::comphelper::OStorageHelper::CreatePackageEncryptionData( pPasswordItem->GetValue() );
                bResult = true;
            }
        }
    }

    return bResult;
}


bool SfxObjectShell::PutURLContentsToVersionStream_Impl(
                                            const OUString& aURL,
                                            const uno::Reference< embed::XStorage >& xDocStorage,
                                            const OUString& aStreamName )
{
    bool bResult = false;
    try
    {
        uno::Reference< embed::XStorage > xVersion = xDocStorage->openStorageElement(
                                                        u"Versions"_ustr,
                                                        embed::ElementModes::READWRITE );

        DBG_ASSERT( xVersion.is(),
                "The method must throw an exception if the storage can not be opened!" );
        if ( !xVersion.is() )
            throw uno::RuntimeException();

        uno::Reference< io::XStream > xVerStream = xVersion->openStreamElement(
                                                                aStreamName,
                                                                embed::ElementModes::READWRITE );
        DBG_ASSERT( xVerStream.is(), "The method must throw an exception if the storage can not be opened!" );
        if ( !xVerStream.is() )
            throw uno::RuntimeException();

        uno::Reference< io::XOutputStream > xOutStream = xVerStream->getOutputStream();
        uno::Reference< io::XTruncate > xTrunc( xOutStream, uno::UNO_QUERY_THROW );

        uno::Reference< io::XInputStream > xTmpInStream =
            ::comphelper::OStorageHelper::GetInputStreamFromURL(
                aURL, comphelper::getProcessComponentContext() );
        assert( xTmpInStream.is() );

        xTrunc->truncate();
        ::comphelper::OStorageHelper::CopyInputToOutput( xTmpInStream, xOutStream );
        xOutStream->closeOutput();

        uno::Reference< embed::XTransactedObject > xTransact( xVersion, uno::UNO_QUERY );
        DBG_ASSERT( xTransact.is(), "The storage must implement XTransacted interface!\n" );
        if ( xTransact.is() )
            xTransact->commit();

        bResult = true;
    }
    catch( uno::Exception& )
    {
        // TODO/LATER: handle the error depending on exception
        SetError(ERRCODE_IO_GENERAL);
    }

    return bResult;
}


OUString SfxObjectShell::CreateTempCopyOfStorage_Impl( const uno::Reference< embed::XStorage >& xStorage )
{
    OUString aTempURL = ::utl::CreateTempURL();

    DBG_ASSERT( !aTempURL.isEmpty(), "Can't create a temporary file!\n" );
    if ( !aTempURL.isEmpty() )
    {
        try
        {
            uno::Reference< embed::XStorage > xTempStorage =
                ::comphelper::OStorageHelper::GetStorageFromURL( aTempURL, embed::ElementModes::READWRITE );

            // the password will be transferred from the xStorage to xTempStorage by storage implementation
            xStorage->copyToStorage( xTempStorage );

            // the temporary storage was committed by the previous method and it will die by refcount
        }
        catch ( uno::Exception& )
        {
            SAL_WARN( "sfx.doc""Creation of a storage copy is failed!" );
            ::utl::UCBContentHelper::Kill( aTempURL );

            aTempURL.clear();

            // TODO/LATER: may need error code setting based on exception
            SetError(ERRCODE_IO_GENERAL);
        }
    }

    return aTempURL;
}


SvGlobalName const & SfxObjectShell::GetClassName() const
{
    return GetFactory().GetClassId();
}


void SfxObjectShell::SetupStorage( const uno::Reference< embed::XStorage >& xStorage,
                                   sal_Int32 nVersion, bool bTemplate ) const
{
    uno::Reference< beans::XPropertySet > xProps( xStorage, uno::UNO_QUERY );

    if ( !xProps.is() )
        return;

    SotClipboardFormatId nClipFormat = SotClipboardFormatId::NONE;

    SvGlobalName aName;
    OUString aFullTypeName;
    FillClass( &aName, &nClipFormat, &aFullTypeName, nVersion, bTemplate );

    if ( nClipFormat == SotClipboardFormatId::NONE )
        return;

    // basic doesn't have a ClipFormat
    // without MediaType the storage is not really usable, but currently the BasicIDE still
    // is an SfxObjectShell and so we can't take this as an error
    datatransfer::DataFlavor aDataFlavor;
    SotExchange::GetFormatDataFlavor( nClipFormat, aDataFlavor );
    if ( aDataFlavor.MimeType.isEmpty() )
        return;

    try
    {
        xProps->setPropertyValue(u"MediaType"_ustr, uno::Any( aDataFlavor.MimeType ) );
    }
    catch( uno::Exception& )
    {
        const_cast<SfxObjectShell*>( this )->SetError(ERRCODE_IO_GENERAL);
    }

    SvtSaveOptions::ODFSaneDefaultVersion nDefVersion = SvtSaveOptions::ODFSVER_014;
    if (!comphelper::IsFuzzing())
    {
        nDefVersion = GetODFSaneDefaultVersion();
    }

    // the default values, that should be used for ODF1.1 and older formats
    uno::Sequence< beans::NamedValue > aEncryptionAlgs
    {
        { u"StartKeyGenerationAlgorithm"_ustr, css::uno::Any(xml::crypto::DigestID::SHA1) },
        { u"EncryptionAlgorithm"_ustr, css::uno::Any(xml::crypto::CipherID::BLOWFISH_CFB_8) },
        { u"ChecksumAlgorithm"_ustr, css::uno::Any(xml::crypto::DigestID::SHA1_1K) },
        { u"KeyDerivationFunction"_ustr, css::uno::Any(xml::crypto::KDFID::PBKDF2) },
    };

    if (nDefVersion >= SvtSaveOptions::ODFSVER_012)
    {
        try
        {
            // older versions can not have this property set, it exists only starting from ODF1.2
            xProps->setPropertyValue(u"Version"_ustr, getODFVersionAny(nDefVersion));
        }
        catch( uno::Exception& )
        {
        }

        auto pEncryptionAlgs = aEncryptionAlgs.getArray();
        pEncryptionAlgs[0].Value <<= xml::crypto::DigestID::SHA256;
        if (::sfx2::UseODFWholesomeEncryption(nDefVersion))
        {
            pEncryptionAlgs[1].Value <<= xml::crypto::CipherID::AES_GCM_W3C;
            pEncryptionAlgs[2].Value.clear();
            if (!getenv("LO_ARGON2_DISABLE"))
            {
                pEncryptionAlgs[3].Value <<= xml::crypto::KDFID::Argon2id;
            }
        }
        else
        {
            pEncryptionAlgs[1].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
            pEncryptionAlgs[2].Value <<= xml::crypto::DigestID::SHA256_1K;
        }
    }

    try
    {
        // set the encryption algorithms accordingly;
        // the setting does not trigger encryption,
        // it just provides the format for the case that contents should be encrypted
        uno::Reference< embed::XEncryptionProtectedStorage > xEncr( xStorage, uno::UNO_QUERY_THROW );
        xEncr->setEncryptionAlgorithms( aEncryptionAlgs );
    }
    catch( uno::Exception& )
    {
        const_cast<SfxObjectShell*>( this )->SetError(ERRCODE_IO_GENERAL);
    }
}


void SfxObjectShell::PrepareSecondTryLoad_Impl()
{
    // only for internal use
    pImpl->m_xDocStorage.clear();
    pImpl->mxObjectContainer.reset();
    pImpl->nDocumentSignatureState = SignatureState::UNKNOWN;
    pImpl->nScriptingSignatureState = SignatureState::UNKNOWN;
    pImpl->m_bIsInit = false;
    ResetError();
}


bool SfxObjectShell::GeneralInit_Impl( const uno::Reference< embed::XStorage >& xStorage,
                                            bool bTypeMustBeSetAlready )
{
    if ( pImpl->m_bIsInit )
        return false;

    pImpl->m_bIsInit = true;
    if ( xStorage.is() )
    {
        // no notification is required the storage is set the first time
        pImpl->m_xDocStorage = xStorage;

        try {
            uno::Reference < beans::XPropertySet > xPropSet( xStorage, uno::UNO_QUERY_THROW );
            Any a = xPropSet->getPropertyValue(u"MediaType"_ustr);
            OUString aMediaType;
            if ( !(a>>=aMediaType) || aMediaType.isEmpty() )
            {
                if ( bTypeMustBeSetAlready )
                {
                    SetError(ERRCODE_IO_BROKENPACKAGE);
                    return false;
                }

                SetupStorage( xStorage, SOFFICE_FILEFORMAT_CURRENT, false );
            }
        }
        catch ( uno::Exception& )
        {
            SAL_WARN( "sfx.doc""Can't check storage's mediatype!" );
        }
    }
    else
        pImpl->m_bCreateTempStor = true;

    return true;
}


bool SfxObjectShell::InitNew( const uno::Reference< embed::XStorage >& xStorage )
{
    return GeneralInit_Impl( xStorage, false );
}


bool SfxObjectShell::Load( SfxMedium& rMedium )
{
    return GeneralInit_Impl(rMedium.GetStorage(), true);
}

void SfxObjectShell::DoInitUnitTest()
{
    pMedium = new SfxMedium;
}

bool SfxObjectShell::DoInitNew()
/*  [Description]

    This from SvPersist inherited virtual method is called to initialize
    the SfxObjectShell instance from a storage (PStore! = 0) or (PStore == 0)

    Like with all Do...-methods there is a from a control, the actual
    implementation is done by the virtual method in which also the
    InitNew(SvStorate *) from the SfxObjectShell-Subclass is implemented.

    For pStore == 0 the SfxObjectShell-instance is connected to an empty
    SfxMedium, otherwise a SfxMedium, which refers to the SotStorage
    passed as a parameter.

    The object is only initialized correctly after InitNew() or Load().

    [Return value]
    true            The object has been initialized.
    false           The object could not be initialized
*/


{
    ModifyBlocker_Impl aBlock( this );
    pMedium = new SfxMedium;

    pMedium->CanDisposeStorage_Impl( true );

    if ( InitNew( nullptr ) )
    {
        // empty documents always get their macros from the user, so there is no reason to restrict access
        pImpl->aMacroMode.allowMacroExecution();
        if ( SfxObjectCreateMode::EMBEDDED == eCreateMode )
            SetTitle(SfxResId(STR_NONAME));

        uno::Reference< frame::XModel >  xModel = GetModel();
        if ( xModel.is() )
        {
            SfxItemSet &rSet = GetMedium()->GetItemSet();
            uno::Sequence< beans::PropertyValue > aArgs;
            TransformItems( SID_OPENDOC, rSet, aArgs );
            sal_Int32 nLength = aArgs.getLength();
            aArgs.realloc( nLength + 1 );
            auto pArgs = aArgs.getArray();
            pArgs[nLength].Name = "Title";
            pArgs[nLength].Value <<= GetTitle( SFX_TITLE_DETECT );
            xModel->attachResource( OUString(), aArgs );
            if (!comphelper::IsFuzzing())
                impl_addToModelCollection(xModel);
        }

        SetInitialized_Impl( true );
        return true;
    }

    return false;
}

bool SfxObjectShell::ImportFromGeneratedStream_Impl(
                    const uno::Reference< io::XStream >& xStream,
                    const uno::Sequence< beans::PropertyValue >& rMediaDescr )
{
    if ( !xStream.is() )
        return false;

    if ( pMedium && pMedium->HasStorage_Impl() )
        pMedium->CloseStorage();

    bool bResult = false;

    try
    {
        uno::Reference< embed::XStorage > xStorage =
            ::comphelper::OStorageHelper::GetStorageFromStream( xStream );

        if ( !xStorage.is() )
            throw uno::RuntimeException();

        if ( !pMedium )
            pMedium = new SfxMedium( xStorage, OUString() );
        else
            pMedium->SetStorage_Impl( xStorage );

        SfxAllItemSet aSet( SfxGetpApp()->GetPool() );
        TransformParameters( SID_OPENDOC, rMediaDescr, aSet );
        pMedium->GetItemSet().Put( aSet );
        pMedium->CanDisposeStorage_Impl( false );
        uno::Reference<text::XTextRange> xInsertTextRange;
        for (const auto& rProp : rMediaDescr)
        {
            if (rProp.Name == "TextInsertModeRange")
            {
                rProp.Value >>= xInsertTextRange;
            }
        }

        if (xInsertTextRange.is())
        {
            bResult = InsertGeneratedStream(*pMedium, xInsertTextRange);
        }
        else
        {

            // allow the subfilter to reinit the model
            if ( pImpl->m_bIsInit )
                pImpl->m_bIsInit = false;

            if ( LoadOwnFormat( *pMedium ) )
            {
                bHasName = true;
                if ( !IsReadOnly() && IsLoadReadonly() )
                    SetReadOnlyUI();

                bResult = true;
                OSL_ENSURE( pImpl->m_xDocStorage == xStorage, "Wrong storage is used!" );
            }
        }

        // now the medium can be disconnected from the storage
        // the medium is not allowed to dispose the storage so CloseStorage() can be used
        pMedium->CloseStorage();
    }
    catch( uno::Exception& )
    {
    }

    return bResult;
}


bool SfxObjectShell::DoLoad( SfxMedium *pMed )
{
    ModifyBlocker_Impl aBlock( this );

    pMedium = pMed;
    pMedium->CanDisposeStorage_Impl( true );

    bool bOk = false;
    std::shared_ptr<const SfxFilter> pFilter = pMed->GetFilter();
    SfxItemSet& rSet = pMedium->GetItemSet();
    if( pImpl->nEventId == SfxEventHintId::NONE )
    {
        const SfxBoolItem* pTemplateItem = rSet.GetItem(SID_TEMPLATE, false);
        SetActivateEvent_Impl(
            ( pTemplateItem && pTemplateItem->GetValue() )
            ? SfxEventHintId::CreateDoc : SfxEventHintId::OpenDoc );
    }

    const SfxStringItem* pBaseItem = rSet.GetItem(SID_BASEURL, false);
    OUString aBaseURL;
    const SfxStringItem* pSalvageItem = rSet.GetItem(SID_DOC_SALVAGE, false);
    if( pBaseItem )
        aBaseURL = pBaseItem->GetValue();
    else
    {
        if ( pSalvageItem )
        {
            osl::FileBase::getFileURLFromSystemPath( pMed->GetPhysicalName(), aBaseURL );
        }
        else
            aBaseURL = pMed->GetBaseURL();
    }
    pMed->GetItemSet().Put( SfxStringItem( SID_DOC_BASEURL, aBaseURL ) );

    pImpl->nLoadedFlags = SfxLoadedFlags::NONE;
    pImpl->bModelInitialized = false;

    if (pFilter && !pFilter->IsEnabled())
    {
        SetError( ERRCODE_IO_FILTERDISABLED );
    }

    if ( pFilter && pFilter->IsExoticFormat() && !QueryAllowExoticFormat_Impl( getInteractionHandler(), aBaseURL, pMed->GetFilter()->GetUIName() ) )
    {
        SetError( ERRCODE_IO_ABORT );
    }

    // initialize static language table so language-related extensions are learned before the document loads
    (void)SvtLanguageTable::GetLanguageEntryCount();

    //TODO/LATER: make a clear strategy how to handle "UsesStorage" etc.
    bool bOwnStorageFormat = IsOwnStorageFormat( *pMedium );
    bool bHasStorage = IsPackageStorageFormat_Impl( *pMedium );
    if ( pMedium->GetFilter() )
    {
        ErrCode nError = HandleFilter( pMedium, this );
        if ( nError != ERRCODE_NONE )
            SetError(nError);

        if (pMedium->GetFilter()->GetFilterFlags() & SfxFilterFlags::STARTPRESENTATION)
            rSet.Put(SfxUInt16Item(SID_DOC_STARTPRESENTATION, 1));
    }

    EnableSetModified( false );

    // tdf#53614 - don't try to lock file after cancelling the import process
    if (GetErrorIgnoreWarning() != ERRCODE_ABORT)
        pMedium->LockOrigFileOnDemand( truefalse );
    if ( GetErrorIgnoreWarning() == ERRCODE_NONE && bOwnStorageFormat && ( !pFilter || !( pFilter->GetFilterFlags() & SfxFilterFlags::STARONEFILTER ) ) )
    {
        uno::Reference< embed::XStorage > xStorage;
        if ( pMedium->GetErrorIgnoreWarning() == ERRCODE_NONE )
            xStorage = pMedium->GetStorage();

        if( xStorage.is() && pMedium->GetLastStorageCreationState() == ERRCODE_NONE )
        {
            DBG_ASSERT( pFilter, "No filter for storage found!" );

            try
            {
                bool bWarnMediaTypeFallback = false;

                // treat the package as broken if the mediatype was retrieved as a fallback
                uno::Reference< beans::XPropertySet > xStorProps( xStorage, uno::UNO_QUERY_THROW );
                xStorProps->getPropertyValue(u"MediaTypeFallbackUsed"_ustr)
                                                                    >>= bWarnMediaTypeFallback;

                if (pMedium->IsRepairPackage())
                {
                    // the macros in repaired documents should be disabled
                    pMedium->GetItemSet().Put( SfxUInt16Item( SID_MACROEXECMODE, document::MacroExecMode::NEVER_EXECUTE ) );

                    // the mediatype was retrieved by using fallback solution but this is a repairing mode
                    // so it is acceptable to open the document if there is no contents that required manifest.xml
                    bWarnMediaTypeFallback = false;
                }

                if (bWarnMediaTypeFallback || !xStorage->getElementNames().hasElements())
                    SetError(ERRCODE_IO_BROKENPACKAGE);
            }
            catch( uno::Exception& )
            {
                // TODO/LATER: may need error code setting based on exception
                SetError(ERRCODE_IO_GENERAL);
            }

            // Load
            if ( !GetErrorIgnoreWarning() )
            {
                pImpl->nLoadedFlags = SfxLoadedFlags::NONE;
                pImpl->bModelInitialized = false;
                bOk = xStorage.is() && LoadOwnFormat( *pMed );
                if ( bOk )
                {
                    // the document loaded from template has no name
                    const SfxBoolItem* pTemplateItem = rSet.GetItem(SID_TEMPLATE, false);
                    if ( !pTemplateItem || !pTemplateItem->GetValue() )
                        bHasName = true;
                }
                else
                    SetError(ERRCODE_ABORT);
            }
        }
        else
            SetError(pMed->GetLastStorageCreationState());
    }
    else if ( GetErrorIgnoreWarning() == ERRCODE_NONE && InitNew(nullptr) )
    {
        // set name before ConvertFrom, so that GetSbxObject() already works
        bHasName = true;
        SetName( SfxResId(STR_NONAME) );

        if( !bHasStorage )
            pMedium->GetInStream();
        else
            pMedium->GetStorage();

        if ( GetErrorIgnoreWarning() == ERRCODE_NONE )
        {
            // Experimental PDF importing using PDFium. This is currently enabled for LOK only and
            // we handle it not via XmlFilterAdaptor but a new SdPdfFilter.
#if !HAVE_FEATURE_POPPLER
            constexpr bool bUsePdfium = true;
#else
            const bool bUsePdfium
                = comphelper::LibreOfficeKit::isActive() || getenv("LO_IMPORT_USE_PDFIUM");
#endif
            const bool bPdfiumImport
                = bUsePdfium && pMedium->GetFilter()
                  && (pMedium->GetFilter()->GetFilterName() == "draw_pdf_import");

            pImpl->nLoadedFlags = SfxLoadedFlags::NONE;
            pImpl->bModelInitialized = false;
            if (pMedium->GetFilter()
                && (pMedium->GetFilter()->GetFilterFlags() & SfxFilterFlags::STARONEFILTER)
                && !bPdfiumImport)
            {
                uno::Reference < beans::XPropertySet > xSet( GetModel(), uno::UNO_QUERY );
                static constexpr OUString sLockUpdates(u"LockUpdates"_ustr);
                bool bSetProperty = true;
                try
                {
                    xSet->setPropertyValue( sLockUpdates, Any( true ) );
                }
                catch(const beans::UnknownPropertyException& )
                {
                    bSetProperty = false;
                }
                bOk = ImportFrom(*pMedium, nullptr);
                if(bSetProperty)
                {
                    try
                    {
                        xSet->setPropertyValue( sLockUpdates, Any( false ) );
                    }
                    catch(const beans::UnknownPropertyException& )
                    {}
                }
                UpdateLinks();
                FinishedLoading();
            }
            else
            {
                if (tools::isEmptyFileUrl(pMedium->GetName()))
                {
                    // The import filter would fail with empty input.
                    bOk = true;
                }
                else
                {
                    bOk = ConvertFrom(*pMedium);
                }
                InitOwnModel_Impl();
            }
        }
    }

    if ( bOk )
    {
        if ( IsReadOnlyMedium() || IsLoadReadonly() )
            SetReadOnlyUI();

        try
        {
            ::ucbhelper::Content aContent( pMedium->GetName(), utl::UCBContentHelper::getDefaultCommandEnvironment(), comphelper::getProcessComponentContext() );
            css::uno::Reference < XPropertySetInfo > xProps = aContent.getProperties();
            if ( xProps.is() )
            {
                static constexpr OUString aAuthor( u"Author"_ustr );
                static constexpr OUString aKeywords( u"Keywords"_ustr );
                static constexpr OUString aSubject( u"Subject"_ustr );
                Any aAny;
                OUString aValue;
                uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
                    GetModel(), uno::UNO_QUERY_THROW);
                uno::Reference<document::XDocumentProperties> xDocProps
                    = xDPS->getDocumentProperties();
                if ( xProps->hasPropertyByName( aAuthor ) )
                {
                    aAny = aContent.getPropertyValue( aAuthor );
                    if ( aAny >>= aValue )
                        xDocProps->setAuthor(aValue);
                }
                if ( xProps->hasPropertyByName( aKeywords ) )
                {
                    aAny = aContent.getPropertyValue( aKeywords );
                    if ( aAny >>= aValue )
                        xDocProps->setKeywords(
                          ::comphelper::string::convertCommaSeparated(aValue));
;
                }
                if ( xProps->hasPropertyByName( aSubject ) )
                {
                    aAny = aContent.getPropertyValue( aSubject );
                    if ( aAny >>= aValue ) {
                        xDocProps->setSubject(aValue);
                    }
                }
            }
        }
        catch( Exception& )
        {
        }

        // If not loaded asynchronously call FinishedLoading
        if ( !( pImpl->nLoadedFlags & SfxLoadedFlags::MAINDOCUMENT ) &&
              ( !pMedium->GetFilter() || pMedium->GetFilter()->UsesStorage() )
            )
            FinishedLoading( SfxLoadedFlags::MAINDOCUMENT );

        Broadcast( SfxHint(SfxHintId::NameChanged) );

        if ( SfxObjectCreateMode::EMBEDDED != eCreateMode )
        {
            const SfxBoolItem* pAsTempItem = rSet.GetItem(SID_TEMPLATE, false);
            const SfxBoolItem* pPreviewItem = rSet.GetItem(SID_PREVIEW, false);
            const SfxBoolItem* pHiddenItem = rSet.GetItem(SID_HIDDEN, false);
            if( !pMedium->GetOrigURL().isEmpty()
            && !( pAsTempItem && pAsTempItem->GetValue() )
            && !( pPreviewItem && pPreviewItem->GetValue() )
            && !( pHiddenItem && pHiddenItem->GetValue() ) )
            {
                AddToRecentlyUsedList();
            }
        }

        const SfxBoolItem* pDdeReconnectItem = rSet.GetItem(SID_DDE_RECONNECT_ONLOAD, false);

        bool bReconnectDde = true// by default, we try to auto-connect DDE connections.
        if (pDdeReconnectItem)
            bReconnectDde = pDdeReconnectItem->GetValue();

        if (bReconnectDde)
            ReconnectDdeLinks(*this);
    }

    return bOk;
}

bool SfxObjectShell::DoLoadExternal( SfxMedium *pMed )
{
    pMedium = pMed;
    return LoadExternal(*pMedium);
}

const ::std::unordered_map<std::string, rtl_TextEncoding>  mapCharSets =
                            {{"UTF-8", RTL_TEXTENCODING_UTF8},
                            {"UTF-16BE", RTL_TEXTENCODING_UCS2},
                            {"UTF-16LE", RTL_TEXTENCODING_UCS2},
                            {"UTF-32BE", RTL_TEXTENCODING_UCS4},
                            {"UTF-32LE", RTL_TEXTENCODING_UCS4},
                            {"Shift_JIS", RTL_TEXTENCODING_SHIFT_JIS},
                            {"ISO-2022-JP", RTL_TEXTENCODING_ISO_2022_JP},
                            {"ISO-2022-CN", RTL_TEXTENCODING_ISO_2022_CN},
                            {"ISO-2022-KR", RTL_TEXTENCODING_ISO_2022_KR},
                            {"GB18030", RTL_TEXTENCODING_GB_18030},
                            {"Big5", RTL_TEXTENCODING_BIG5},
                            {"EUC-JP", RTL_TEXTENCODING_EUC_JP},
                            {"EUC-KR", RTL_TEXTENCODING_EUC_KR},
                            {"ISO-8859-1", RTL_TEXTENCODING_ISO_8859_1},
                            {"ISO-8859-2", RTL_TEXTENCODING_ISO_8859_2},
                            {"ISO-8859-5", RTL_TEXTENCODING_ISO_8859_5},
                            {"ISO-8859-6", RTL_TEXTENCODING_ISO_8859_6},
                            {"ISO-8859-7", RTL_TEXTENCODING_ISO_8859_7},
                            {"ISO-8859-8", RTL_TEXTENCODING_ISO_8859_8},
                            {"ISO-8859-9", RTL_TEXTENCODING_ISO_8859_9},
                            {"windows-1250", RTL_TEXTENCODING_MS_1250},
                            {"windows-1251", RTL_TEXTENCODING_MS_1251},
                            {"windows-1252", RTL_TEXTENCODING_MS_1252},
                            {"windows-1253", RTL_TEXTENCODING_MS_1253},
                            {"windows-1254", RTL_TEXTENCODING_MS_1254},
                            {"windows-1255", RTL_TEXTENCODING_MS_1255},
                            {"windows-1256", RTL_TEXTENCODING_MS_1256},
                            {"KOI8-R", RTL_TEXTENCODING_KOI8_R}};

void SfxObjectShell::DetectCharSet(SvStream& stream, rtl_TextEncoding& eCharSet, SvStreamEndian &endian)
{
    constexpr size_t buffsize = 4096;
    sal_Int8 bytes[buffsize] = { 0 };
    sal_uInt64 nInitPos = stream.Tell();
    sal_Int32 nRead = stream.ReadBytes(bytes, buffsize);

    stream.Seek(nInitPos);
    eCharSet = RTL_TEXTENCODING_DONTKNOW;

    if (!nRead)
        return;

    UErrorCode uerr = U_ZERO_ERROR;
    UCharsetDetector* ucd = ucsdet_open(&uerr);
    if (!U_SUCCESS(uerr))
        return;

    const UCharsetMatch* match = nullptr;
    const char* pEncodingName = nullptr;
    ucsdet_setText(ucd, reinterpret_cast<const char*>(bytes), nRead, &uerr);
    if (U_SUCCESS(uerr))
        match = ucsdet_detect(ucd, &uerr);

    if (U_SUCCESS(uerr))
        pEncodingName = ucsdet_getName(match, &uerr);

    if (U_SUCCESS(uerr) && pEncodingName)
    {
        const auto it = mapCharSets.find(pEncodingName);
        if (it != mapCharSets.end())
            eCharSet = it->second;

        if (eCharSet == RTL_TEXTENCODING_UNICODE && !strcmp("UTF-16LE", pEncodingName))
            endian = SvStreamEndian::LITTLE;
        else if (eCharSet == RTL_TEXTENCODING_UNICODE && !strcmp("UTF-16BE", pEncodingName))
            endian = SvStreamEndian::BIG;
    }

    ucsdet_close(ucd);
}

void SfxObjectShell::DetectCsvSeparators(SvStream& stream, rtl_TextEncoding eCharSet, OUString& separators, sal_Unicode cStringDelimiter)
{
    OUString sLine;
    std::vector<std::unordered_map<sal_Unicode, sal_uInt32>> aLinesCharsCount;
    std::unordered_map<sal_Unicode, sal_uInt32> aCharsCount;
    std::unordered_map<sal_Unicode, std::pair<sal_uInt32, sal_uInt32>> aStats;
    constexpr sal_uInt32 nTimeout = 500; // Timeout for detection in ms
    sal_uInt32 nLinesCount = 0;
    OUString sInitSeps;
    OUString sCommonSeps = u",\t;:| \\/"_ustr;//Sorted by importance
    std::unordered_set<sal_Unicode> usetCommonSeps;
    bool bIsDelimiter = false;
    // The below two are needed to handle a "not perfect" structure.
    sal_uInt32 nMaxLinesSameChar = 0;
    sal_uInt32 nMinDiffs = 0xFFFFFFFF;
    sal_uInt64 nInitPos = stream.Tell();
    sal_uInt64 nStartTime = tools::Time::GetSystemTicks();

    if (!cStringDelimiter)
        cStringDelimiter = '\"';

    for (sal_Int32 nComSepIdx = sCommonSeps.getLength() - 1; nComSepIdx >= 0; nComSepIdx --)
        usetCommonSeps.insert(sCommonSeps[nComSepIdx]);
    aLinesCharsCount.reserve(128);
    separators = "";

    stream.StartReadingUnicodeText(eCharSet);
    while (stream.ReadUniOrByteStringLine(sLine, eCharSet) && (tools::Time::GetSystemTicks() - nStartTime < nTimeout))
    {
        if (sLine.isEmpty())
            continue;

        if (!nLinesCount)
        {
            if (sLine.getLength() == 5 && sLine.startsWithIgnoreAsciiCase("sep="))
            {
                separators += OUStringChar(sLine[4]);
                break;
            }
            else if (sLine.getLength() == 7 && sLine[6] == '"' && sLine.startsWithIgnoreAsciiCase("\"sep="))
            {
                separators += OUStringChar(sLine[5]);
                break;
            }
        }

        // Count the occurrences of each character within the line.
        // Skip strings.
        const sal_Unicode *pEnd = sLine.getStr() + sLine.getLength();
        for (const sal_Unicode *p = sLine.getStr(); p < pEnd; p++)
        {
            if (*p == cStringDelimiter)
            {
                bIsDelimiter = !bIsDelimiter;
                continue;
            }
            if (bIsDelimiter)
                continue;

            // If restricted only to common separators then skip the rest
            if (usetCommonSeps.find(*p) == usetCommonSeps.end())
                continue;

            auto it_elem = aCharsCount.find(*p);
            if (it_elem == aCharsCount.cend())
                aCharsCount.insert(std::pair<sal_uInt32, sal_uInt32>(*p, 1));
            else
                it_elem->second ++;
        }

        if (bIsDelimiter)
            continue;

        nLinesCount ++;

        // For each character count the lines that contain it and different number of occurrences.
        // And the global maximum for the first statistic.
        for (auto aCurLineChar=aCharsCount.cbegin(); aCurLineChar != aCharsCount.cend(); aCurLineChar++)
        {
            auto aCurStats = aStats.find(aCurLineChar->first);
            if (aCurStats == aStats.cend())
                aCurStats = aStats.insert(std::pair<sal_Unicode, std::pair<sal_uInt32, sal_uInt32>>(aCurLineChar->first, std::pair<sal_uInt32, sal_uInt32>(1, 1))).first;
            else
            {
                aCurStats->second.first ++;// Increment number of lines that contain the current character

                std::vector<std::unordered_map<sal_Unicode, sal_uInt32>>::const_iterator aPrevLineChar;
                for (aPrevLineChar=aLinesCharsCount.cbegin(); aPrevLineChar != aLinesCharsCount.cend(); aPrevLineChar++)
                {
                    auto aPrevStats = aPrevLineChar->find(aCurLineChar->first);
                    if (aPrevStats != aPrevLineChar->cend() && aPrevStats->second == aCurLineChar->second)
                        break;
                }
                if (aPrevLineChar == aLinesCharsCount.cend())
                    aCurStats->second.second ++;// Increment number of different number of occurrences.
            }

            // Update the maximum of number of lines that contain the same character. This is a global value.
            if (nMaxLinesSameChar < aCurStats->second.first)
                nMaxLinesSameChar = aCurStats->second.first;
        }

        aLinesCharsCount.emplace_back();
        aLinesCharsCount[aLinesCharsCount.size() - 1].swap(aCharsCount);
    }

    SAL_INFO("sfx.doc""" << nLinesCount << " lines processed in " << tools::Time::GetSystemTicks() - nStartTime << " ms while detecting separator.");

    // Compute the global minimum of different number of occurrences.
    // But only for characters which occur in a maximum number of lines (previously computed).
    for (auto it=aStats.cbegin(); it != aStats.cend(); it++)
        if (it->second.first == nMaxLinesSameChar && nMinDiffs > it->second.second)
            nMinDiffs = it->second.second;

    // Compute the initial list of separators: those with the maximum lines of occurrence and
    // the minimum of different number of occurrences.
    for (auto it=aStats.cbegin(); it != aStats.cend(); it++)
        if (it->second.first == nMaxLinesSameChar && it->second.second == nMinDiffs)
            sInitSeps += OUStringChar(it->first);

    // If forced to most common or there are multiple separators then pick up only the most common by importance.
    sal_Int32 nInitSepIdx;
    sal_Int32 nComSepIdx;
    for (nComSepIdx = 0; nComSepIdx < sCommonSeps.getLength(); nComSepIdx++)
    {
        sal_Unicode c = sCommonSeps[nComSepIdx];
        for (nInitSepIdx = sInitSeps.getLength() - 1; nInitSepIdx >= 0; nInitSepIdx --)
        {
            if (c == sInitSeps[nInitSepIdx])
            {
                separators += OUStringChar(c);
                break;
            }
        }

    }

    stream.Seek(nInitPos);
}

void SfxObjectShell::DetectCsvFilterOptions(SvStream& stream, OUString& aFilterOptions)
{
    rtl_TextEncoding eCharSet = RTL_TEXTENCODING_DONTKNOW;
    std::u16string_view aSeps;
    std::u16string_view aDelimiter;
    std::u16string_view aCharSet;
    std::u16string_view aRest;
    OUString aOrigFilterOpts = aFilterOptions;
    bool bDelimiter = false, bCharSet = false, bRest = false// This indicates the presence of the token even if empty ;)

    if (aFilterOptions.isEmpty())
        return;
    const std::u16string_view aDetect = u"DETECT";
    sal_Int32 nPos = 0;

    // Get first three tokens as they are the only tokens that affect detection.
    aSeps = o3tl::getToken(aOrigFilterOpts, 0, ',', nPos);
    bDelimiter = (nPos >= 0);
    if (bDelimiter)
        aDelimiter = o3tl::getToken(aOrigFilterOpts, 0, ',', nPos);
    bCharSet = (nPos >= 0);
    if (bCharSet)
        aCharSet = o3tl::getToken(aOrigFilterOpts, 0, ',', nPos);
    bRest = (nPos >= 0);
    if (bRest)
        aRest = std::basic_string_view<sal_Unicode>(aOrigFilterOpts.getStr() + nPos, aOrigFilterOpts.getLength() - nPos);

    // Detect charset
    if (aCharSet == aDetect)
    {
        SvStreamEndian endian;
        DetectCharSet(stream, eCharSet, endian);
        if (eCharSet == RTL_TEXTENCODING_UNICODE)
            stream.SetEndian(endian);
    }
    else if (!aCharSet.empty())
        eCharSet = o3tl::toInt32(aCharSet);


    //Detect separators
    if (aSeps == aDetect)
    {
        aFilterOptions = "";
        OUString separators;
        DetectCsvSeparators(stream, eCharSet, separators, static_cast<sal_Unicode>(o3tl::toInt32(aDelimiter)));

        sal_Int32 nLen = separators.getLength();
        for (sal_Int32 nSep = 0; nSep < nLen; nSep ++)
        {
            if (nSep)
                aFilterOptions += "/";
            aFilterOptions += OUString::number(separators[nSep]);
        }
    }
    else
        // For now keep the provided values.
        aFilterOptions = aSeps;

    OUStringChar cComma = u',';
    if (bDelimiter)
        aFilterOptions += cComma + aDelimiter;
    if (bCharSet)
        aFilterOptions += cComma + (aCharSet == aDetect ? OUString::number(eCharSet) : aCharSet);
    if (bRest)
        aFilterOptions += cComma + aRest;
}

void SfxObjectShell::DetectFilterOptions(SfxMedium* pMedium)
{
    std::shared_ptr<const SfxFilter> pFilter = pMedium->GetFilter();
    SfxItemSet& rSet = pMedium->GetItemSet();
    const SfxStringItem* pOptions = rSet.GetItem(SID_FILE_FILTEROPTIONS, false);

    // Skip if filter options are missing
    if (!pFilter || !pOptions)
        return;

    if (pFilter->GetName() == "Text - txt - csv (StarCalc)")
    {
        css::uno::Reference< css::io::XInputStream > xInputStream = pMedium->GetInputStream();
        if (!xInputStream.is())
            return;
        std::unique_ptr<SvStream> pInStream = utl::UcbStreamHelper::CreateStream(xInputStream);
        if (!pInStream)
            return;

        OUString aFilterOptions = pOptions->GetValue();
        DetectCsvFilterOptions(*pInStream, aFilterOptions);
        rSet.Put(SfxStringItem(SID_FILE_FILTEROPTIONS, aFilterOptions));
    }
}

ErrCode SfxObjectShell::HandleFilter( SfxMedium* pMedium, SfxObjectShell const * pDoc )
{
    ErrCode nError = ERRCODE_NONE;
    SfxItemSet& rSet = pMedium->GetItemSet();
    const SfxStringItem* pOptions = rSet.GetItem(SID_FILE_FILTEROPTIONS, false);
    const SfxUnoAnyItem* pData = rSet.GetItem(SID_FILTER_DATA, false);
    const bool bTiledRendering = comphelper::LibreOfficeKit::isActive();

    // Process earlier as the input could contain express detection instructions.
    // This is relevant for "automatic" use case. For interactive use case the
    // FilterOptions should not be detected here (the detection is done before entering
    // interactive state). For now this is focused on CSV files.
    DetectFilterOptions(pMedium);

    if ( !pData && (bTiledRendering || !pOptions) )
    {
        css::uno::Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
        css::uno::Reference< XNameAccess > xFilterCFG;
        if( xServiceManager.is() )
        {
            xFilterCFG.set( xServiceManager->createInstance(u"com.sun.star.document.FilterFactory"_ustr),
                            UNO_QUERY );
        }

        if( xFilterCFG.is() )
        {
            try {
                bool bAbort = false;
                std::shared_ptr<const SfxFilter> pFilter = pMedium->GetFilter();
                Sequence < PropertyValue > aProps;
                Any aAny = xFilterCFG->getByName( pFilter->GetName() );
                if ( aAny >>= aProps )
                {
                    auto pProp = std::find_if(std::cbegin(aProps), std::cend(aProps),
                        [](const PropertyValue& rProp) { return rProp.Name == "UIComponent"; });
                    if (pProp != std::cend(aProps))
                    {
                        OUString aServiceName;
                        pProp->Value >>= aServiceName;
                        if( !aServiceName.isEmpty() )
                        {
                            css::uno::Reference< XInteractionHandler > rHandler = pMedium->GetInteractionHandler();
                            if( rHandler.is() )
                            {
                                // we need some properties in the media descriptor, so we have to make sure that they are in
                                Any aStreamAny;
                                aStreamAny <<= pMedium->GetInputStream();
                                if ( rSet.GetItemState( SID_INPUTSTREAM ) < SfxItemState::SET )
                                    rSet.Put( SfxUnoAnyItem( SID_INPUTSTREAM, aStreamAny ) );
                                if ( rSet.GetItemState( SID_FILE_NAME ) < SfxItemState::SET )
                                    rSet.Put( SfxStringItem( SID_FILE_NAME, pMedium->GetName() ) );
                                if ( rSet.GetItemState( SID_FILTER_NAME ) < SfxItemState::SET )
                                    rSet.Put( SfxStringItem( SID_FILTER_NAME, pFilter->GetName() ) );

                                Sequence< PropertyValue > rProperties;
                                TransformItems( SID_OPENDOC, rSet, rProperties );
                                rtl::Reference<RequestFilterOptions> pFORequest = new RequestFilterOptions( pDoc->GetModel(), rProperties );

                                rHandler->handle( pFORequest );

                                if ( !pFORequest->isAbort() )
                                {
                                        SfxAllItemSet aNewParams( pDoc->GetPool() );
                                        TransformParameters( SID_OPENDOC,
                                                        pFORequest->getFilterOptions(),
                                                        aNewParams );

                                        const SfxStringItem* pFilterOptions = aNewParams.GetItem<SfxStringItem>(SID_FILE_FILTEROPTIONS, false);
                                        if ( pFilterOptions )
                                            rSet.Put( *pFilterOptions );

                                        const SfxUnoAnyItem* pFilterData = aNewParams.GetItem<SfxUnoAnyItem>(SID_FILTER_DATA, false);
                                        if ( pFilterData )
                                            rSet.Put( *pFilterData );
                                }
                                else
                                    bAbort = true;
                            }
                        }
                    }
                }

                if( bAbort )
                {
                    // filter options were not entered
                    nError = ERRCODE_ABORT;
                }
            }
            catch( NoSuchElementException& )
            {
                // the filter name is unknown
                nError = ERRCODE_IO_INVALIDPARAMETER;
            }
            catch( Exception& )
            {
                nError = ERRCODE_ABORT;
            }
        }
    }

    return nError;
}


bool SfxObjectShell::IsOwnStorageFormat(const SfxMedium &rMedium)
{
    return !rMedium.GetFilter() || // Embedded
           ( rMedium.GetFilter()->IsOwnFormat() &&
             rMedium.GetFilter()->UsesStorage() &&
             rMedium.GetFilter()->GetVersion() >= SOFFICE_FILEFORMAT_60 );
}


bool SfxObjectShell::IsPackageStorageFormat_Impl(const SfxMedium &rMedium)
{
    return !rMedium.GetFilter() || // Embedded
           ( rMedium.GetFilter()->UsesStorage() &&
             rMedium.GetFilter()->GetVersion() >= SOFFICE_FILEFORMAT_60 );
}


bool SfxObjectShell::DoSave()
// DoSave is only invoked for OLE. Save your own documents in the SFX through
// DoSave_Impl order to allow for the creation of backups.
// Save in your own format again.
{
    bool bOk = false ;
    {
        ModifyBlocker_Impl aBlock( this );

        pImpl->bIsSaving = true;

        if (IsOwnStorageFormat(*GetMedium()))
        {
            SvtSaveOptions::ODFSaneDefaultVersion nDefVersion = SvtSaveOptions::ODFSVER_013;
            if (!comphelper::IsFuzzing())
            {
                nDefVersion = GetODFSaneDefaultVersion();
            }
            uno::Reference<beans::XPropertySet> const xProps(GetMedium()->GetStorage(), uno::UNO_QUERY);
            assert(xProps.is());
            if (nDefVersion >= SvtSaveOptions::ODFSVER_012) // property exists only since ODF 1.2
            {
                try // tdf#134582 set Version on embedded objects as they
                {   // could have been loaded with a different/old version
                    xProps->setPropertyValue(u"Version"_ustr, getODFVersionAny(nDefVersion));
                }
                catch (uno::Exception&)
                {
                    TOOLS_WARN_EXCEPTION("sfx.doc""SfxObjectShell::DoSave");
                }
            }
        }

        if ( IsPackageStorageFormat_Impl( *GetMedium() ) )
        {
            GetMedium()->GetStorage(); // sets encryption properties if necessary
            if (GetMedium()->GetErrorCode())
            {
                SetError(ERRCODE_IO_GENERAL);
            }
            else
            {
                bOk = true;
            }
#if HAVE_FEATURE_SCRIPTING
            if ( HasBasic() )
            {
                try
                {
                    // The basic and dialogs related contents are still not able to proceed with save operation ( saveTo only )
                    // so since the document storage is locked a workaround has to be used

                    uno::Reference< embed::XStorage > xTmpStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
                    DBG_ASSERT( xTmpStorage.is(), "If a storage can not be created an exception must be thrown!\n" );
                    if ( !xTmpStorage.is() )
                        throw uno::RuntimeException();

                    static constexpr OUString aBasicStorageName( u"Basic"_ustr  );
                    static constexpr OUString aDialogsStorageName( u"Dialogs"_ustr  );
                    if ( GetMedium()->GetStorage()->hasByName( aBasicStorageName ) )
                        GetMedium()->GetStorage()->copyElementTo( aBasicStorageName, xTmpStorage, aBasicStorageName );
                    if ( GetMedium()->GetStorage()->hasByName( aDialogsStorageName ) )
                        GetMedium()->GetStorage()->copyElementTo( aDialogsStorageName, xTmpStorage, aDialogsStorageName );

                    GetBasicManager();

                    // disconnect from the current storage
                    pImpl->aBasicManager.setStorage( xTmpStorage );

                    // store to the current storage
                    pImpl->aBasicManager.storeLibrariesToStorage( GetMedium()->GetStorage() );

                    // connect to the current storage back
                    pImpl->aBasicManager.setStorage( GetMedium()->GetStorage() );
                }
                catch( uno::Exception& )
                {
                    SetError(ERRCODE_IO_GENERAL);
                    bOk = false;
                }
            }
#endif
        }

        if (bOk)
            bOk = Save();

        if (bOk)
            bOk = pMedium->Commit();
    }

    return bOk;
}

namespace
{
class LockUIGuard
{
public:
    LockUIGuard(SfxObjectShell const* pDoc)
        : m_pDoc(pDoc)
    {
        Lock_Impl();
    }
    ~LockUIGuard() { Unlock(); }

    void Unlock()
    {
        if (m_bUnlock)
            Lock_Impl();
    }

private:
    void Lock_Impl()
    {
        SfxViewFrame* pFrame = SfxViewFrame::GetFirst(m_pDoc);
        while (pFrame)
        {
            pFrame->GetDispatcher()->Lock(!m_bUnlock);
            pFrame->Enable(m_bUnlock);
            pFrame = SfxViewFrame::GetNext(*pFrame, m_pDoc);
        }
        m_bUnlock = !m_bUnlock;
    }
    SfxObjectShell const* m_pDoc;
    bool m_bUnlock = false;
};
}

static OUString lcl_strip_template(const OUString &aString)
{
    static constexpr OUString sPostfix(u"_template"_ustr);
    OUString sRes(aString);
    if (sRes.endsWith(sPostfix))
        sRes = sRes.copy(0, sRes.getLength() - sPostfix.getLength());
    return sRes;
}

bool SfxObjectShell::SaveTo_Impl
(
     SfxMedium &rMedium, // Medium, in which it will be stored
     const SfxItemSet* pSet
)

/*  [Description]

    Writes the current contents to the medium rMedium. If the target medium is
    no storage, then saving to a temporary storage, or directly if the medium
    is transacted, if we ourselves have opened it, and if we are a server
    either the container a transacted storage provides or created a
    temporary storage by one self.
*/


{
    SAL_INFO( "sfx.doc""saving \"" << rMedium.GetName() << "\"" );

    UpdateDocInfoForSave();

    ModifyBlocker_Impl aMod(this);
    // tdf#41063, tdf#135244: prevent jumping to cursor at any temporary modification
    auto aViewGuard(LockAllViews());

    uno::Reference<uno::XComponentContext> const& xContext(
        ::comphelper::getProcessComponentContext());

    std::shared_ptr<const SfxFilter> pFilter = rMedium.GetFilter();
    if ( !pFilter )
    {
        // if no filter was set, use the default filter
        // this should be changed in the feature, it should be an error!
        SAL_WARN( "sfx.doc","No filter set!");
        pFilter = GetFactory().GetFilterContainer()->GetAnyFilter( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT );
        rMedium.SetFilter(pFilter);
    }

    bool bStorageBasedSource = IsPackageStorageFormat_Impl( *pMedium );
    bool bStorageBasedTarget = IsPackageStorageFormat_Impl( rMedium );
    bool bOwnSource = IsOwnStorageFormat( *pMedium );
    bool bOwnTarget = IsOwnStorageFormat( rMedium );

    // Examine target format to determine whether to query if any password
    // protected libraries exceed the size we can handler
    if ( bOwnTarget && !QuerySaveSizeExceededModules_Impl( rMedium.GetInteractionHandler() ) )
    {
        SetError(ERRCODE_IO_ABORT);
        return false;
    }

    SvtSaveOptions::ODFSaneDefaultVersion nVersion(SvtSaveOptions::ODFSVER_LATEST_EXTENDED);
    if (bOwnTarget && !comphelper::IsFuzzing())
    {
        nVersion = GetODFSaneDefaultVersion();
    }

    bool bNeedsDisconnectionOnFail = false;

    bool bStoreToSameLocation = false;

    // the detection whether the script is changed should be done before saving
    bool bTryToPreserveScriptSignature = false;
    // no way to detect whether a filter is oasis format, have to wait for saving process
    bool bNoPreserveForOasis = false;
    if ( bOwnSource && bOwnTarget
      && ( pImpl->nScriptingSignatureState == SignatureState::OK
        || pImpl->nScriptingSignatureState == SignatureState::NOTVALIDATED
        || pImpl->nScriptingSignatureState == SignatureState::INVALID ) )
    {
        // the checking of the library modified state iterates over the libraries, should be done only when required
        // currently the check is commented out since it is broken, we have to check the signature every time we save
        // TODO/LATER: let isAnyContainerModified() work!
        bTryToPreserveScriptSignature = true// !pImpl->pBasicManager->isAnyContainerModified();
        if ( bTryToPreserveScriptSignature )
        {
            // check that the storage format stays the same

            OUString aODFVersion;
            try
            {
                uno::Reference < beans::XPropertySet > xPropSet( GetStorage(), uno::UNO_QUERY );
                if (xPropSet)
                    xPropSet->getPropertyValue(u"Version"_ustr) >>= aODFVersion;
            }
            catch( uno::Exception& )
            {}

            // preserve only if the same filter has been used
            // for templates, strip the _template from the filter name for comparison
            const OUString aMediumFilter = lcl_strip_template(pMedium->GetFilter()->GetFilterName());
            bTryToPreserveScriptSignature = pMedium->GetFilter() && pFilter && aMediumFilter == lcl_strip_template(pFilter->GetFilterName());

            // signatures were specified in ODF 1.2 but were used since much longer.
            // LO will still correctly validate an old style signature on an ODF 1.2
            // document, but technically this is not correct, so this prevents old
            // signatures to be copied over to a version 1.2 document
            bNoPreserveForOasis = (
                                   (0 <= aODFVersion.compareTo(ODFVER_012_TEXT) && nVersion < SvtSaveOptions::ODFSVER_012) ||
                                   (aODFVersion.isEmpty() && nVersion >= SvtSaveOptions::ODFSVER_012)
                                  );
        }
    }

    uno::Reference<io::XStream> xODFDecryptedInnerPackageStream;
    uno::Reference<embed::XStorage> xODFDecryptedInnerPackage;
    uno::Sequence<beans::NamedValue> aEncryptionData;
    if (GetEncryptionData_Impl(&rMedium.GetItemSet(), aEncryptionData))
    {
        assert(aEncryptionData.getLength() != 0);
        if (bOwnTarget && ::sfx2::UseODFWholesomeEncryption(nVersion))
        {
            // when embedded objects are stored here, it should be called from
            // this function for the root document and encryption data was cleared
            assert(GetCreateMode() != SfxObjectCreateMode::EMBEDDED);
            // clear now to store inner package (+ embedded objects) unencrypted
            rMedium.GetItemSet().ClearItem(SID_ENCRYPTIONDATA);
            rMedium.GetItemSet().ClearItem(SID_PASSWORD);
            xODFDecryptedInnerPackageStream.set(
                xContext->getServiceManager()->createInstanceWithContext(
                    u"com.sun.star.comp.MemoryStream"_ustr, xContext),
                UNO_QUERY_THROW);
            xODFDecryptedInnerPackage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream(
                PACKAGE_STORAGE_FORMAT_STRING, xODFDecryptedInnerPackageStream,
                css::embed::ElementModes::WRITE, xContext, false);
            assert(xODFDecryptedInnerPackage.is());
        }
    }

    bool isStreamAndInputStreamCleared(false);
    // use UCB for case sensitive/insensitive file name comparison
    if ( !pMedium->GetName().equalsIgnoreAsciiCase("private:stream")
      && !rMedium.GetName().equalsIgnoreAsciiCase("private:stream")
      && ::utl::UCBContentHelper::EqualURLs( pMedium->GetName(), rMedium.GetName() ) )
    {
        // Do not unlock the file during saving.
        // need to modify this for WebDAV if this method is called outside of
        // the process of saving a file
        pMedium->DisableUnlockWebDAV();
        bStoreToSameLocation = true;

        if ( pMedium->DocNeedsFileDateCheck() )
        {
            rMedium.CheckFileDate( pMedium->GetInitFileDate( false ) );
            if (rMedium.GetErrorCode() == ERRCODE_ABORT)
            {
                // if user cancels the save, exit early to avoid resetting SfxMedium values that
                // would cause an invalid subsequent filedate check
                return false;
            }
        }

        // before we overwrite the original file, we will make a backup if there is a demand for that
        // if the backup is not created here it will be created internally and will be removed in case of successful saving
        const bool bDoBackup = officecfg::Office::Common::Save::Document::CreateBackup::get() && !comphelper::LibreOfficeKit::isActive();
        if ( bDoBackup )
        {
            rMedium.DoBackup_Impl(/*bForceUsingBackupPath=*/false);
            if ( rMedium.GetErrorIgnoreWarning() )
            {
                SetError(rMedium.GetErrorCode());
                rMedium.ResetError();
            }
        }

        if ( bStorageBasedSource && bStorageBasedTarget )
        {
            // The active storage must be switched. The simple saving is not enough.
            // The problem is that the target medium contains target MediaDescriptor.

                // In future the switch of the persistence could be done on stream level:
                // a new wrapper service will be implemented that allows to exchange
                // persistence on the fly. So the real persistence will be set
                // to that stream only after successful commit of the storage.
                // TODO/LATER:
                // create wrapper stream based on the URL
                // create a new storage based on this stream
                // store to this new storage
                // commit the new storage
                // call saveCompleted based with this new storage ( get rid of old storage and "frees" URL )
                // commit the wrapper stream ( the stream will connect the URL only on commit, after that it will hold it )
                // if the last step is failed the stream should stay to be transacted and should be committed on any flush
                // so we can forget the stream in any way and the next storage commit will flush it

            bNeedsDisconnectionOnFail = DisconnectStorage_Impl(
                *pMedium, rMedium );
            if ( bNeedsDisconnectionOnFail
              || ConnectTmpStorage_Impl( pMedium->GetStorage(), pMedium ) )
            {
                pMedium->CloseAndRelease();
                isStreamAndInputStreamCleared = true;

                // TODO/LATER: for now the medium must be closed since it can already contain streams from old medium
                //             in future those streams should not be copied in case a valid target url is provided,
                //             if the url is not provided ( means the document is based on a stream ) this code is not
                //             reachable.
                rMedium.CloseAndRelease();
                rMedium.SetHasEmbeddedObjects(GetEmbeddedObjectContainer().HasEmbeddedObjects());
                if (xODFDecryptedInnerPackageStream.is())
                {
                    assert(!rMedium.GetItemSet().GetItem(SID_STREAM));
                    rMedium.SetInnerStorage_Impl(xODFDecryptedInnerPackage);
                }
                else
                {
                    rMedium.GetOutputStorage();
                }
                rMedium.SetHasEmbeddedObjects(false);
            }
        }
        else if ( !bStorageBasedSource && !bStorageBasedTarget )
        {
            // the source and the target formats are alien
            // just disconnect the stream from the source format
            // so that the target medium can use it

            pMedium->CloseAndRelease();
            rMedium.CloseAndRelease();
            isStreamAndInputStreamCleared = true;
            rMedium.CreateTempFileNoCopy();
            rMedium.GetOutStream();
        }
        else if ( !bStorageBasedSource && bStorageBasedTarget )
        {
            // the source format is an alien one but the target
            // format is an own one so just disconnect the source
            // medium

            pMedium->CloseAndRelease();
            rMedium.CloseAndRelease();
            isStreamAndInputStreamCleared = true;
            if (xODFDecryptedInnerPackageStream.is())
            {
                assert(!rMedium.GetItemSet().GetItem(SID_STREAM));
                rMedium.SetInnerStorage_Impl(xODFDecryptedInnerPackage);
            }
            else
            {
                rMedium.GetOutputStorage();
            }
        }
        else // means if ( bStorageBasedSource && !bStorageBasedTarget )
        {
            // the source format is an own one but the target is
            // an alien format, just connect the source to temporary
            // storage

            bNeedsDisconnectionOnFail = DisconnectStorage_Impl(
                *pMedium, rMedium );
            if ( bNeedsDisconnectionOnFail
              || ConnectTmpStorage_Impl( pMedium->GetStorage(), pMedium ) )
            {
                pMedium->CloseAndRelease();
                rMedium.CloseAndRelease();
                isStreamAndInputStreamCleared = true;
                rMedium.CreateTempFileNoCopy();
                rMedium.GetOutStream();
            }
        }
        pMedium->DisableUnlockWebDAV(false);
    }
    else
    {
        // This is SaveAs or export action, prepare the target medium
        // the alien filters still might write directly to the file, that is of course a bug,
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=95 H=64 G=80

¤ Dauer der Verarbeitung: 0.37 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.