From 8ae0efd7d8888ae6f3444e1633d8a6afc92519ea Mon Sep 17 00:00:00 2001 From: "Frank Schoenheit [fs]" Date: Wed, 27 Jan 2010 10:53:30 +0100 Subject: autorecovery: when creating a new document, also respect the RecoveryStorage parameter --- embeddedobj/source/commonembedding/persistence.cxx | 180 +++++++++++---------- embeddedobj/source/inc/commonembobj.hxx | 21 ++- 2 files changed, 110 insertions(+), 91 deletions(-) (limited to 'embeddedobj/source') diff --git a/embeddedobj/source/commonembedding/persistence.cxx b/embeddedobj/source/commonembedding/persistence.cxx index e9aa0edd0ec8..55170093c43e 100644 --- a/embeddedobj/source/commonembedding/persistence.cxx +++ b/embeddedobj/source/commonembedding/persistence.cxx @@ -67,6 +67,8 @@ #include +#include + #define USE_STORAGEBASED_DOCUMENT using namespace ::com::sun::star; @@ -198,6 +200,22 @@ uno::Reference< io::XInputStream > createTempInpStreamFromStor( } +//------------------------------------------------------ +static void TransferMediaType( const uno::Reference< embed::XStorage >& i_rSource, const uno::Reference< embed::XStorage >& i_rTarget ) +{ + try + { + const uno::Reference< beans::XPropertySet > xSourceProps( i_rSource, uno::UNO_QUERY_THROW ); + const uno::Reference< beans::XPropertySet > xTargetProps( i_rTarget, uno::UNO_QUERY_THROW ); + const ::rtl::OUString sMediaTypePropName( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ); + xTargetProps->setPropertyValue( sMediaTypePropName, xSourceProps->getPropertyValue( sMediaTypePropName ) ); + } + catch( const uno::Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } +} + //------------------------------------------------------ static uno::Reference< util::XCloseable > CreateDocument( const uno::Reference< lang::XMultiServiceFactory >& _rxFactory, const ::rtl::OUString& _rDocumentServiceName, bool _bEmbeddedScriptSupport, const bool i_bDocumentRecoverySupport ) @@ -300,6 +318,23 @@ void OCommonEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::X SwitchOwnPersistence( xNewParentStorage, xNewOwnStorage, aNewName ); } +//------------------------------------------------------ +void OCommonEmbeddedObject::EmbedAndReparentDoc_Impl( const uno::Reference< util::XCloseable >& i_rxDocument ) const +{ + SetDocToEmbedded( uno::Reference< frame::XModel >( i_rxDocument, uno::UNO_QUERY ), m_aModuleName ); + + try + { + uno::Reference < container::XChild > xChild( i_rxDocument, uno::UNO_QUERY ); + if ( xChild.is() ) + xChild->setParent( m_xParent ); + } + catch( const lang::NoSupportException & ) + { + OSL_ENSURE( false, "OCommonEmbeddedObject::EmbedAndReparentDoc: cannot set parent at document!" ); + } +} + //------------------------------------------------------ uno::Reference< util::XCloseable > OCommonEmbeddedObject::InitNewDocument_Impl() { @@ -314,22 +349,31 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::InitNewDocument_Impl() try { // set the document mode to embedded as the first action on document!!! - SetDocToEmbedded( xModel, m_aModuleName ); + EmbedAndReparentDoc_Impl( xDocument ); - try + // if we have a storage to recover the document from, do not use initNew, but instead load from that storage + bool bInitNew = true; + if ( m_xRecoveryStorage.is() ) { - uno::Reference < container::XChild > xChild( xDocument, uno::UNO_QUERY ); - if ( xChild.is() ) - xChild->setParent( m_xParent ); + uno::Reference< document::XStorageBasedDocument > xDoc( xLoadable, uno::UNO_QUERY ); + OSL_ENSURE( xDoc.is(), "OCommonEmbeddedObject::InitNewDocument_Impl: cannot recover from a storage when the document is not storage based!" ); + if ( xDoc.is() ) + { + ::comphelper::NamedValueCollection aLoadArgs; + FillDefaultLoadArgs_Impl( m_xRecoveryStorage, aLoadArgs ); + + xDoc->loadFromStorage( m_xRecoveryStorage, aLoadArgs.getPropertyValues() ); + SwitchDocToStorage_Impl( xDoc, m_xObjectStorage ); + bInitNew = false; + } } - catch( const lang::NoSupportException & ) + + if ( bInitNew ) { - OSL_ENSURE( false, "Cannot set parent at document" ); + // init document as a new + xLoadable->initNew(); + xModel->attachResource( xModel->getURL(), m_aDocMediaDescriptor ); } - - // init document as a new - xLoadable->initNew(); - xModel->attachResource( xModel->getURL(),m_aDocMediaDescriptor); } catch( uno::Exception& ) { @@ -384,18 +428,7 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadLink_Impl() try { // the document is not really an embedded one, it is a link - SetDocToEmbedded( uno::Reference < frame::XModel >( xDocument, uno::UNO_QUERY ), m_aModuleName ); - - try - { - uno::Reference < container::XChild > xChild( xDocument, uno::UNO_QUERY ); - if ( xChild.is() ) - xChild->setParent( m_xParent ); - } - catch( const lang::NoSupportException & ) - { - OSL_ENSURE( false, "Cannot set parent at document" ); - } + EmbedAndReparentDoc_Impl( xDocument ); // load the document xLoadable->load( aArgs ); @@ -436,7 +469,7 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadLink_Impl() } //------------------------------------------------------ -::rtl::OUString OCommonEmbeddedObject::GetFilterName( sal_Int32 nVersion ) +::rtl::OUString OCommonEmbeddedObject::GetFilterName( sal_Int32 nVersion ) const { ::rtl::OUString aFilterName = GetPresetFilterName(); if ( !aFilterName.getLength() ) @@ -451,10 +484,28 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadLink_Impl() return aFilterName; } +//------------------------------------------------------ +void OCommonEmbeddedObject::FillDefaultLoadArgs_Impl( const uno::Reference< embed::XStorage >& i_rxStorage, + ::comphelper::NamedValueCollection& o_rLoadArgs ) const +{ + o_rLoadArgs.put( "DocumentBaseURL", GetBaseURL_Impl() ); + o_rLoadArgs.put( "HierarchicalDocumentName", m_aEntryName ); + o_rLoadArgs.put( "ReadOnly", m_bReadOnly ); + + ::rtl::OUString aFilterName = GetFilterName( ::comphelper::OStorageHelper::GetXStorageFormat( i_rxStorage ) ); + OSL_ENSURE( aFilterName.getLength(), "OCommonEmbeddedObject::FillDefaultLoadArgs_Impl: Wrong document service name!" ); + if ( !aFilterName.getLength() ) + throw io::IOException(); // TODO: error message/code + + o_rLoadArgs.put( "FilterName", aFilterName ); +} + //------------------------------------------------------ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorage_Impl() { - OSL_ENSURE( m_xObjectStorage.is(), "The storage can not be empty!" ); + ENSURE_OR_THROW( m_xObjectStorage.is(), "no object storage" ); + + const uno::Reference< embed::XStorage > xSourceStorage( m_xRecoveryStorage.is() ? m_xRecoveryStorage : m_xObjectStorage ); uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xFactory, GetDocumentServiceName(), m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) ); @@ -477,28 +528,13 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorag if ( !xDoc.is() && !xLoadable.is() ) ///BUG: This should be || instead of && ? throw uno::RuntimeException(); - ::rtl::OUString aFilterName = GetFilterName( ::comphelper::OStorageHelper::GetXStorageFormat( m_xObjectStorage ) ); - - OSL_ENSURE( aFilterName.getLength(), "Wrong document service name!" ); - if ( !aFilterName.getLength() ) - throw io::IOException(); - - sal_Int32 nLen = xDoc.is() ? 4 : 6; - uno::Sequence< beans::PropertyValue > aArgs( nLen ); - - aArgs[0].Name = ::rtl::OUString::createFromAscii( "DocumentBaseURL" ); - aArgs[0].Value <<= GetBaseURL_Impl(); - aArgs[1].Name = ::rtl::OUString::createFromAscii( "HierarchicalDocumentName" ); - aArgs[1].Value <<= m_aEntryName; - aArgs[2].Name = ::rtl::OUString::createFromAscii( "ReadOnly" ); - aArgs[2].Value <<= m_bReadOnly; - aArgs[3].Name = ::rtl::OUString::createFromAscii( "FilterName" ); - aArgs[3].Value <<= aFilterName; + ::comphelper::NamedValueCollection aLoadArgs; + FillDefaultLoadArgs_Impl( xSourceStorage, aLoadArgs ); uno::Reference< io::XInputStream > xTempInpStream; if ( !xDoc.is() ) { - xTempInpStream = createTempInpStreamFromStor( m_xObjectStorage, m_xFactory ); + xTempInpStream = createTempInpStreamFromStor( xSourceStorage, m_xFactory ); if ( !xTempInpStream.is() ) throw uno::RuntimeException(); @@ -516,50 +552,27 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorag OSL_ENSURE( aTempFileURL.getLength(), "Coudn't retrieve temporary file URL!\n" ); - aArgs[4].Name = ::rtl::OUString::createFromAscii( "URL" ); - aArgs[4].Value <<= aTempFileURL; // ::rtl::OUString::createFromAscii( "private:stream" ); - aArgs[5].Name = ::rtl::OUString::createFromAscii( "InputStream" ); - aArgs[5].Value <<= xTempInpStream; + aLoadArgs.put( "URL", aTempFileURL ); + aLoadArgs.put( "InputStream", xTempInpStream ); } - // aArgs[4].Name = ::rtl::OUString::createFromAscii( "AsTemplate" ); - // aArgs[4].Value <<= sal_True; + // aLoadArgs.put( "AsTemplate", sal_True ); - aArgs.realloc( m_aDocMediaDescriptor.getLength() + nLen ); - for ( sal_Int32 nInd = 0; nInd < m_aDocMediaDescriptor.getLength(); nInd++ ) - { - aArgs[nInd+nLen].Name = m_aDocMediaDescriptor[nInd].Name; - aArgs[nInd+nLen].Value = m_aDocMediaDescriptor[nInd].Value; - } + aLoadArgs.merge( m_aDocMediaDescriptor, true ); try { // set the document mode to embedded as the first step!!! - SetDocToEmbedded( uno::Reference < frame::XModel >( xDocument, uno::UNO_QUERY ), m_aModuleName ); - - try - { - uno::Reference < container::XChild > xChild( xDocument, uno::UNO_QUERY ); - if ( xChild.is() ) - xChild->setParent( m_xParent ); - } - catch( const lang::NoSupportException & ) - { - OSL_ENSURE( false, "Cannot set parent at document" ); - } + EmbedAndReparentDoc_Impl( xDocument ); if ( xDoc.is() ) { - if ( m_xRecoveryStorage.is() ) - { - xDoc->loadFromStorage( m_xRecoveryStorage, aArgs ); + xDoc->loadFromStorage( xSourceStorage, aLoadArgs.getPropertyValues() ); + if ( xSourceStorage != m_xObjectStorage ) SwitchDocToStorage_Impl( xDoc, m_xObjectStorage ); - } - else - xDoc->loadFromStorage( m_xObjectStorage, aArgs ); } else - xLoadable->load( aArgs ); + xLoadable->load( aLoadArgs.getPropertyValues() ); } catch( uno::Exception& ) { @@ -572,6 +585,7 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorag } catch( uno::Exception& ) { + DBG_UNHANDLED_EXCEPTION(); } } @@ -661,7 +675,7 @@ void OCommonEmbeddedObject::SaveObject_Impl() } //------------------------------------------------------ -::rtl::OUString OCommonEmbeddedObject::GetBaseURL_Impl() +::rtl::OUString OCommonEmbeddedObject::GetBaseURL_Impl() const { ::rtl::OUString aBaseURL; sal_Int32 nInd = 0; @@ -824,18 +838,7 @@ uno::Reference< util::XCloseable > OCommonEmbeddedObject::CreateDocFromMediaDesc try { // set the document mode to embedded as the first action on the document!!! - SetDocToEmbedded( uno::Reference < frame::XModel >( xDocument, uno::UNO_QUERY ), m_aModuleName ); - - try - { - uno::Reference < container::XChild > xChild( xDocument, uno::UNO_QUERY ); - if ( xChild.is() ) - xChild->setParent( m_xParent ); - } - catch( const lang::NoSupportException & ) - { - OSL_ENSURE( false, "Cannot set parent at document" ); - } + EmbedAndReparentDoc_Impl( xDocument ); xLoadable->load( addAsTemplate( aMedDescr ) ); } @@ -1117,6 +1120,9 @@ void SAL_CALL OCommonEmbeddedObject::setPersistentEntry( } else if ( nEntryConnectionMode == embed::EntryInitModes::TRUNCATE_INIT ) { + if ( m_xRecoveryStorage.is() ) + TransferMediaType( m_xRecoveryStorage, m_xObjectStorage ); + // TODO: m_pDocHolder->SetComponent( InitNewDocument_Impl(), m_bReadOnly ); diff --git a/embeddedobj/source/inc/commonembobj.hxx b/embeddedobj/source/inc/commonembobj.hxx index 2ba0e5cd4aca..f718151b6aa1 100644 --- a/embeddedobj/source/inc/commonembobj.hxx +++ b/embeddedobj/source/inc/commonembobj.hxx @@ -75,6 +75,10 @@ namespace cppu { class OMultiTypeInterfaceContainerHelper; } +namespace comphelper { + class NamedValueCollection; +} + #define NUM_SUPPORTED_STATES 5 // #define NUM_SUPPORTED_VERBS 5 @@ -183,8 +187,8 @@ private: const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xNewParentStorage, const ::rtl::OUString& aNewName ); - ::rtl::OUString GetDocumentServiceName() { return m_aDocServiceName; } - ::rtl::OUString GetPresetFilterName() { return m_aPresetFilterName; } + ::rtl::OUString GetDocumentServiceName() const { return m_aDocServiceName; } + ::rtl::OUString GetPresetFilterName() const { return m_aPresetFilterName; } ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > StoreDocumentToTempStream_Impl( sal_Int32 nStorageFormat, @@ -201,7 +205,7 @@ private: ::com::sun::star::uno::Sequence< sal_Int32 > GetIntermediateStatesSequence_Impl( sal_Int32 nNewState ); - ::rtl::OUString GetFilterName( sal_Int32 nVersion ); + ::rtl::OUString GetFilterName( sal_Int32 nVersion ) const; ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable > LoadDocumentFromStorage_Impl(); ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable > LoadLink_Impl(); @@ -218,12 +222,21 @@ private: const ::com::sun::star::uno::Reference< ::com::sun::star::document::XStorageBasedDocument >& xDoc, const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage ); + void FillDefaultLoadArgs_Impl( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rxStorage, + ::comphelper::NamedValueCollection& o_rLoadArgs + ) const; + + void EmbedAndReparentDoc_Impl( + const ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable >& i_rxDocument + ) const; + ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable > CreateDocFromMediaDescr_Impl( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aMedDescr ); ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloseable > CreateTempDocFromLink_Impl(); - ::rtl::OUString GetBaseURL_Impl(); + ::rtl::OUString GetBaseURL_Impl() const; ::rtl::OUString GetBaseURLFrom_Impl( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lArguments, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lObjArgs ); -- cgit