diff options
Diffstat (limited to 'dbaccess/source/core/recovery')
-rw-r--r-- | dbaccess/source/core/recovery/dbdocrecovery.cxx | 436 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/makefile.mk | 58 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/settingsimport.cxx | 294 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/settingsimport.hxx | 190 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagestream.cxx | 126 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagestream.hxx | 109 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagetextstream.cxx | 130 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagetextstream.hxx | 66 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagexmlstream.cxx | 197 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/storagexmlstream.hxx | 113 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/subcomponentloader.cxx | 200 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/subcomponentloader.hxx | 87 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/subcomponentrecovery.cxx | 702 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/subcomponentrecovery.hxx | 126 | ||||
-rw-r--r-- | dbaccess/source/core/recovery/subcomponents.hxx | 88 |
15 files changed, 2922 insertions, 0 deletions
diff --git a/dbaccess/source/core/recovery/dbdocrecovery.cxx b/dbaccess/source/core/recovery/dbdocrecovery.cxx new file mode 100644 index 000000000000..6f31f17682fd --- /dev/null +++ b/dbaccess/source/core/recovery/dbdocrecovery.cxx @@ -0,0 +1,436 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "recovery/dbdocrecovery.hxx" +#include "sdbcoretools.hxx" +#include "storagetextstream.hxx" +#include "subcomponentrecovery.hxx" +#include "subcomponents.hxx" +#include "dbastrings.hrc" + +/** === begin UNO includes === **/ +#include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp> +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/document/XStorageBasedDocument.hpp> +#include <com/sun/star/io/XTextOutputStream.hpp> +#include <com/sun/star/io/XTextInputStream.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/util/XModifiable.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +/** === end UNO includes === **/ + +#include <comphelper/componentcontext.hxx> +#include <comphelper/namedvaluecollection.hxx> +#include <rtl/ustrbuf.hxx> +#include <tools/diagnose_ex.h> + +#include <algorithm> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::embed::XStorage; + using ::com::sun::star::frame::XController; + using ::com::sun::star::sdb::application::XDatabaseDocumentUI; + using ::com::sun::star::lang::XComponent; + using ::com::sun::star::document::XStorageBasedDocument; + using ::com::sun::star::beans::PropertyValue; + using ::com::sun::star::io::XStream; + using ::com::sun::star::io::XTextOutputStream; + using ::com::sun::star::io::XActiveDataSource; + using ::com::sun::star::io::XTextInputStream; + using ::com::sun::star::io::XActiveDataSink; + using ::com::sun::star::frame::XModel; + using ::com::sun::star::util::XModifiable; + using ::com::sun::star::beans::XPropertySet; + using ::com::sun::star::lang::XMultiServiceFactory; + /** === end UNO using === **/ + + namespace ElementModes = ::com::sun::star::embed::ElementModes; + + //==================================================================== + //= helpers + //==================================================================== + namespace + { + // ......................................................................... + static void lcl_getPersistentRepresentation( const MapStringToCompDesc::value_type& i_rComponentDesc, ::rtl::OUStringBuffer& o_rBuffer ) + { + o_rBuffer.append( i_rComponentDesc.first ); + o_rBuffer.append( sal_Unicode( '=' ) ); + o_rBuffer.append( i_rComponentDesc.second.sName ); + o_rBuffer.append( sal_Unicode( ',' ) ); + o_rBuffer.append( sal_Unicode( i_rComponentDesc.second.bForEditing ? '1' : '0' ) ); + } + + // ......................................................................... + static bool lcl_extractCompDesc( const ::rtl::OUString& i_rIniLine, ::rtl::OUString& o_rStorName, SubComponentDescriptor& o_rCompDesc ) + { + const sal_Int32 nEqualSignPos = i_rIniLine.indexOf( sal_Unicode( '=' ) ); + if ( nEqualSignPos < 1 ) + { + OSL_ENSURE( false, "lcl_extractCompDesc: invalid map file entry - unexpected pos of '='" ); + return false; + } + o_rStorName = i_rIniLine.copy( 0, nEqualSignPos ); + + const sal_Int32 nCommaPos = i_rIniLine.lastIndexOf( sal_Unicode( ',' ) ); + if ( nCommaPos != i_rIniLine.getLength() - 2 ) + { + OSL_ENSURE( false, "lcl_extractCompDesc: invalid map file entry - unexpected pos of ','" ); + return false; + } + o_rCompDesc.sName = i_rIniLine.copy( nEqualSignPos + 1, nCommaPos - nEqualSignPos - 1 ); + o_rCompDesc.bForEditing = ( i_rIniLine.getStr()[ nCommaPos + 1 ] == '1' ); + return true; + } + + // ......................................................................... + static const ::rtl::OUString& lcl_getRecoveryDataSubStorageName() + { + static const ::rtl::OUString s_sRecDataStorName( RTL_CONSTASCII_USTRINGPARAM( "recovery" ) ); + return s_sRecDataStorName; + } + // ......................................................................... + static const ::rtl::OUString& lcl_getObjectMapStreamName() + { + static const ::rtl::OUString s_sObjectMapStreamName( RTL_CONSTASCII_USTRINGPARAM( "storage-component-map.ini" ) ); + return s_sObjectMapStreamName; + } + + // ......................................................................... + static const ::rtl::OUString& lcl_getMapStreamEncodingName() + { + static const ::rtl::OUString s_sMapStreamEncodingName( RTL_CONSTASCII_USTRINGPARAM( "UTF-8" ) ); + return s_sMapStreamEncodingName; + } + + // ......................................................................... + static void lcl_writeObjectMap_throw( const ::comphelper::ComponentContext& i_rContext, const Reference< XStorage >& i_rStorage, + const MapStringToCompDesc& i_mapStorageToCompDesc ) + { + if ( i_mapStorageToCompDesc.empty() ) + // nothing to do + return; + + StorageTextOutputStream aTextOutput( i_rContext, i_rStorage, lcl_getObjectMapStreamName() ); + + aTextOutput.writeLine( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "[storages]" ) ) ); + + for ( MapStringToCompDesc::const_iterator stor = i_mapStorageToCompDesc.begin(); + stor != i_mapStorageToCompDesc.end(); + ++stor + ) + { + ::rtl::OUStringBuffer aLine; + lcl_getPersistentRepresentation( *stor, aLine ); + + aTextOutput.writeLine( aLine.makeStringAndClear() ); + } + + aTextOutput.writeLine(); + } + + // ......................................................................... + static bool lcl_isSectionStart( const ::rtl::OUString& i_rIniLine, ::rtl::OUString& o_rSectionName ) + { + const sal_Int32 nLen = i_rIniLine.getLength(); + if ( ( nLen > 0 ) && ( i_rIniLine.getStr()[0] == '[' ) && ( i_rIniLine.getStr()[ nLen - 1 ] == ']' ) ) + { + o_rSectionName = i_rIniLine.copy( 1, nLen -2 ); + return true; + } + return false; + } + + // ......................................................................... + static void lcl_stripTrailingLineFeed( ::rtl::OUString& io_rLine ) + { + const sal_Int32 nLen = io_rLine.getLength(); + if ( ( nLen > 0 ) && ( io_rLine.getStr()[ nLen - 1 ] == '\n' ) ) + io_rLine = io_rLine.copy( 0, nLen - 1 ); + } + + // ......................................................................... + static void lcl_readObjectMap_throw( const ::comphelper::ComponentContext& i_rContext, const Reference< XStorage >& i_rStorage, + MapStringToCompDesc& o_mapStorageToObjectName ) + { + ENSURE_OR_THROW( i_rStorage.is(), "invalid storage" ); + if ( !i_rStorage->hasByName( lcl_getObjectMapStreamName() ) ) + { // nothing to do, though suspicious + OSL_ENSURE( false, "lcl_readObjectMap_throw: if there's no map file, then there's expected to be no storage, too!" ); + return; + } + + Reference< XStream > xIniStream( i_rStorage->openStreamElement( + lcl_getObjectMapStreamName(), ElementModes::READ ), UNO_SET_THROW ); + + Reference< XTextInputStream > xTextInput( i_rContext.createComponent( "com.sun.star.io.TextInputStream" ), UNO_QUERY_THROW ); + xTextInput->setEncoding( lcl_getMapStreamEncodingName() ); + + Reference< XActiveDataSink > xDataSink( xTextInput, UNO_QUERY_THROW ); + xDataSink->setInputStream( xIniStream->getInputStream() ); + + ::rtl::OUString sCurrentSection; + bool bCurrentSectionIsKnownToBeUnsupported = true; + while ( !xTextInput->isEOF() ) + { + ::rtl::OUString sLine = xTextInput->readLine(); + lcl_stripTrailingLineFeed( sLine ); + + if ( sLine.getLength() == 0 ) + continue; + + if ( lcl_isSectionStart( sLine, sCurrentSection ) ) + { + bCurrentSectionIsKnownToBeUnsupported = false; + continue; + } + + if ( bCurrentSectionIsKnownToBeUnsupported ) + continue; + + // the only section we support so far is "storages" + if ( !sCurrentSection.equalsAscii( "storages" ) ) + { + bCurrentSectionIsKnownToBeUnsupported = true; + continue; + } + + ::rtl::OUString sStorageName; + SubComponentDescriptor aCompDesc; + if ( !lcl_extractCompDesc( sLine, sStorageName, aCompDesc ) ) + continue; + o_mapStorageToObjectName[ sStorageName ] = aCompDesc; + } + } + + // ......................................................................... + static void lcl_markModified( const Reference< XComponent >& i_rSubComponent ) + { + const Reference< XModifiable > xModify( i_rSubComponent, UNO_QUERY ); + if ( !xModify.is() ) + { + OSL_ENSURE( false, "lcl_markModified: unhandled case!" ); + return; + } + + xModify->setModified( sal_True ); + } + } + + //==================================================================== + //= DatabaseDocumentRecovery_Data + //==================================================================== + struct DBACCESS_DLLPRIVATE DatabaseDocumentRecovery_Data + { + const ::comphelper::ComponentContext aContext; + + DatabaseDocumentRecovery_Data( const ::comphelper::ComponentContext& i_rContext ) + :aContext( i_rContext ) + { + } + }; + + //==================================================================== + //= DatabaseDocumentRecovery + //==================================================================== + //-------------------------------------------------------------------- + DatabaseDocumentRecovery::DatabaseDocumentRecovery( const ::comphelper::ComponentContext& i_rContext ) + :m_pData( new DatabaseDocumentRecovery_Data( i_rContext ) ) + { + } + + //-------------------------------------------------------------------- + DatabaseDocumentRecovery::~DatabaseDocumentRecovery() + { + } + + //-------------------------------------------------------------------- + void DatabaseDocumentRecovery::saveModifiedSubComponents( const Reference< XStorage >& i_rTargetStorage, + const ::std::vector< Reference< XController > >& i_rControllers ) + { + ENSURE_OR_THROW( i_rTargetStorage.is(), "invalid document storage" ); + + // create a sub storage for recovery data + if ( i_rTargetStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) ) + i_rTargetStorage->removeElement( lcl_getRecoveryDataSubStorageName() ); + Reference< XStorage > xRecoveryStorage = i_rTargetStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READWRITE ); + + // store recovery data for open sub components of the given controller(s) + if ( !i_rControllers.empty() ) + { + ENSURE_OR_THROW( i_rControllers.size() == 1, "can't handle more than one controller" ); + // At the moment, there can be only one view to a database document. If we ever allow for more than this, + // then we need a concept for sub documents opened from different controllers (i.e. two document views, + // and the user opens the very same form in both views). And depending on this, we need a concept for + // how those are saved to the recovery file. + + MapCompTypeToCompDescs aMapCompDescs; + + for ( ::std::vector< Reference< XController > >::const_iterator ctrl = i_rControllers.begin(); + ctrl != i_rControllers.end(); + ++ctrl + ) + { + Reference< XDatabaseDocumentUI > xDatabaseUI( *ctrl, UNO_QUERY_THROW ); + Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() ); + + const Reference< XComponent >* component = aComponents.getConstArray(); + const Reference< XComponent >* componentEnd = aComponents.getConstArray() + aComponents.getLength(); + for ( ; component != componentEnd; ++component ) + { + SubComponentRecovery aComponentRecovery( m_pData->aContext, xDatabaseUI, *component ); + aComponentRecovery.saveToRecoveryStorage( xRecoveryStorage, aMapCompDescs ); + } + } + + for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin(); + map != aMapCompDescs.end(); + ++map + ) + { + Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement( + SubComponentRecovery::getComponentsStorageName( map->first ), ElementModes::WRITE | ElementModes::NOCREATE ) ); + lcl_writeObjectMap_throw( m_pData->aContext, xComponentsStor, map->second ); + tools::stor::commitStorageIfWriteable( xComponentsStor ); + } + } + + // commit the recovery storage + tools::stor::commitStorageIfWriteable( xRecoveryStorage ); + } + + //-------------------------------------------------------------------- + void DatabaseDocumentRecovery::recoverSubDocuments( const Reference< XStorage >& i_rDocumentStorage, + const Reference< XController >& i_rTargetController ) + { + ENSURE_OR_THROW( i_rDocumentStorage.is(), "illegal document storage" ); + Reference< XDatabaseDocumentUI > xDocumentUI( i_rTargetController, UNO_QUERY_THROW ); + + if ( !i_rDocumentStorage->hasByName( lcl_getRecoveryDataSubStorageName() ) ) + // that's allowed + return; + + // the "recovery" sub storage + Reference< XStorage > xRecoveryStorage = i_rDocumentStorage->openStorageElement( lcl_getRecoveryDataSubStorageName(), ElementModes::READ ); + + // read the map from sub storages to object names + MapCompTypeToCompDescs aMapCompDescs; + SubComponentType aKnownTypes[] = { TABLE, QUERY, FORM, REPORT, RELATION_DESIGN }; + for ( size_t i = 0; i < sizeof( aKnownTypes ) / sizeof( aKnownTypes[0] ); ++i ) + { + if ( !xRecoveryStorage->hasByName( SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ) ) ) + continue; + + Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement( + SubComponentRecovery::getComponentsStorageName( aKnownTypes[i] ), ElementModes::READ ) ); + lcl_readObjectMap_throw( m_pData->aContext, xComponentsStor, aMapCompDescs[ aKnownTypes[i] ] ); + xComponentsStor->dispose(); + } + + // recover all sub components as indicated by the map + for ( MapCompTypeToCompDescs::const_iterator map = aMapCompDescs.begin(); + map != aMapCompDescs.end(); + ++map + ) + { + const SubComponentType eComponentType = map->first; + + // the storage for all components of the current type + Reference< XStorage > xComponentsStor( xRecoveryStorage->openStorageElement( + SubComponentRecovery::getComponentsStorageName( eComponentType ), ElementModes::READ ), UNO_QUERY_THROW ); + + // loop thru all components of this type + for ( MapStringToCompDesc::const_iterator stor = map->second.begin(); + stor != map->second.end(); + ++stor + ) + { + const ::rtl::OUString sComponentName( stor->second.sName ); + if ( !xComponentsStor->hasByName( stor->first ) ) + { + #if OSL_DEBUG_LEVEL > 0 + ::rtl::OStringBuffer message; + message.append( "DatabaseDocumentRecovery::recoverSubDocuments: inconsistent recovery storage: storage '" ); + message.append( ::rtl::OUStringToOString( stor->first, RTL_TEXTENCODING_ASCII_US ) ); + message.append( "' not found in '" ); + message.append( ::rtl::OUStringToOString( SubComponentRecovery::getComponentsStorageName( eComponentType ), RTL_TEXTENCODING_ASCII_US ) ); + message.append( "', but required per map file!" ); + OSL_ENSURE( false, message.makeStringAndClear() ); + #endif + continue; + } + + // the controller needs to have a connection to be able to open sub components + if ( !xDocumentUI->isConnected() ) + xDocumentUI->connect(); + + // recover the single component + Reference< XStorage > xCompStor( xComponentsStor->openStorageElement( stor->first, ElementModes::READ ) ); + SubComponentRecovery aComponentRecovery( m_pData->aContext, xDocumentUI, eComponentType ); + Reference< XComponent > xSubComponent( aComponentRecovery.recoverFromStorage( xCompStor, sComponentName, stor->second.bForEditing ) ); + + // at the moment, we only store, during session save, sub components which are modified. So, set this + // recovered sub component to "modified", too. + lcl_markModified( xSubComponent ); + } + + xComponentsStor->dispose(); + } + + xRecoveryStorage->dispose(); + + // now that we successfully recovered, removed the "recovery" sub storage + try + { + i_rDocumentStorage->removeElement( lcl_getRecoveryDataSubStorageName() ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + +//........................................................................ +} // namespace dbaccess +//........................................................................ diff --git a/dbaccess/source/core/recovery/makefile.mk b/dbaccess/source/core/recovery/makefile.mk new file mode 100644 index 000000000000..c13334bbc9d9 --- /dev/null +++ b/dbaccess/source/core/recovery/makefile.mk @@ -0,0 +1,58 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2008 by Sun Microsystems, Inc. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.10.2.3 $ +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/..$/.. +PRJINC=$(PRJ)$/source +PRJNAME=dbaccess +TARGET=recovery + +ENABLE_EXCEPTIONS=TRUE + +# --- Settings ---------------------------------- + +.INCLUDE : settings.mk +.INCLUDE : $(PRJ)$/util$/dba.pmk + +# --- Files ------------------------------------- + +SLOFILES= \ + $(SLO)$/dbdocrecovery.obj \ + $(SLO)$/subcomponentloader.obj \ + $(SLO)$/storagestream.obj \ + $(SLO)$/storagexmlstream.obj \ + $(SLO)$/storagetextstream.obj \ + $(SLO)$/subcomponentrecovery.obj \ + $(SLO)$/settingsimport.obj + +# --- Targets ---------------------------------- + +.INCLUDE : target.mk + diff --git a/dbaccess/source/core/recovery/settingsimport.cxx b/dbaccess/source/core/recovery/settingsimport.cxx new file mode 100644 index 000000000000..566bf42ae847 --- /dev/null +++ b/dbaccess/source/core/recovery/settingsimport.cxx @@ -0,0 +1,294 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "settingsimport.hxx" + +/** === begin UNO includes === **/ +/** === end UNO includes === **/ + +#include <tools/diagnose_ex.h> +#include <xmloff/xmltoken.hxx> +#include <xmloff/xmluconv.hxx> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::xml::sax::XAttributeList; + /** === end UNO using === **/ + + //==================================================================== + //= SettingsImport + //==================================================================== + //-------------------------------------------------------------------- + SettingsImport::SettingsImport() + :m_refCount( 0 ) + { + } + + //-------------------------------------------------------------------- + SettingsImport::~SettingsImport() + { + } + + //-------------------------------------------------------------------- + oslInterlockedCount SAL_CALL SettingsImport::acquire() + { + return osl_incrementInterlockedCount( &m_refCount ); + } + + //-------------------------------------------------------------------- + oslInterlockedCount SAL_CALL SettingsImport::release() + { + oslInterlockedCount newCount = osl_decrementInterlockedCount( &m_refCount ); + if ( newCount == 0 ) + delete this; + return newCount; + } + + //-------------------------------------------------------------------- + void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes ) + { + // find the name of the setting + if ( i_rAttributes.is() ) + { + m_sItemName = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:name" ) ) ); + m_sItemType = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:type" ) ) ); + } + } + + //-------------------------------------------------------------------- + void SettingsImport::endElement() + { + } + + //-------------------------------------------------------------------- + void SettingsImport::characters( const ::rtl::OUString& i_rCharacters ) + { + m_aCharacters.append( i_rCharacters ); + } + + //-------------------------------------------------------------------- + void SettingsImport::split( const ::rtl::OUString& i_rElementName, ::rtl::OUString& o_rNamespace, ::rtl::OUString& o_rLocalName ) + { + o_rNamespace = ::rtl::OUString(); + o_rLocalName = i_rElementName; + const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' ); + if ( nSeparatorPos > -1 ) + { + o_rNamespace = i_rElementName.copy( 0, nSeparatorPos ); + o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 ); + } + + OSL_ENSURE( o_rNamespace.equalsAscii( "config" ), "SettingsImport::split: unexpected namespace!" ); + // our recovery file is kind of hand-made, so there shouldn't be anything else than "config". + // If there is, then just ignore it ... + } + + //==================================================================== + //= IgnoringSettingsImport + //==================================================================== + //-------------------------------------------------------------------- + ::rtl::Reference< SettingsImport > IgnoringSettingsImport::nextState( const ::rtl::OUString& i_rElementName ) + { + (void)i_rElementName; + return this; + } + + //==================================================================== + //= OfficeSettingsImport + //==================================================================== + //-------------------------------------------------------------------- + OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings ) + :m_rSettings( o_rSettings ) + { + } + + //-------------------------------------------------------------------- + OfficeSettingsImport::~OfficeSettingsImport() + { + } + + //-------------------------------------------------------------------- + ::rtl::Reference< SettingsImport > OfficeSettingsImport::nextState( const ::rtl::OUString& i_rElementName ) + { + // separate the namespace part from the element name + ::rtl::OUString sNamespace; + ::rtl::OUString sLocalName; + split( i_rElementName, sNamespace, sLocalName ); + + if ( sLocalName.equalsAscii( "config-item-set" ) ) + return new ConfigItemSetImport( m_rSettings ); + +#if OSL_DEBUG_LEVEL > 0 + ::rtl::OString sMessage( "unknown (or unsupported at this place) element name '" ); + sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 ); + sMessage += "', ignoring"; + OSL_ENSURE( false, sMessage.getStr() ); +#endif + return new IgnoringSettingsImport; + } + + //==================================================================== + //= ConfigItemImport + //==================================================================== + //-------------------------------------------------------------------- + ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings ) + :m_rSettings( o_rSettings ) + { + } + + //-------------------------------------------------------------------- + ConfigItemImport::~ConfigItemImport() + { + } + + //-------------------------------------------------------------------- + ::rtl::Reference< SettingsImport > ConfigItemImport::nextState( const ::rtl::OUString& i_rElementName ) + { + OSL_ENSURE( false, "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" ); + (void)i_rElementName; + return new IgnoringSettingsImport; + } + + //-------------------------------------------------------------------- + void ConfigItemImport::endElement() + { + SettingsImport::endElement(); + + const ::rtl::OUString sItemName( getItemName() ); + ENSURE_OR_RETURN_VOID( sItemName.getLength(), "no item name -> no item value" ); + Any aValue; + getItemValue( aValue ); + m_rSettings.put( sItemName, aValue ); + } + + //-------------------------------------------------------------------- + void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any& o_rValue ) const + { + o_rValue.clear(); + + // the characters building up th evalue + ::rtl::OUStringBuffer aCharacters( getAccumulatedCharacters() ); + const ::rtl::OUString sValue = aCharacters.makeStringAndClear(); + + const ::rtl::OUString& rItemType( getItemType() ); + ENSURE_OR_RETURN_VOID( rItemType.getLength(), "no item type -> no item value" ); + + if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) ) + { + sal_Int32 nValue(0); + if ( SvXMLUnitConverter::convertNumber( nValue, sValue ) ) + o_rValue <<= nValue; + else + { + OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert an int value!" ); + } + } + else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) ) + { + sal_Bool nValue( sal_False ); + if ( SvXMLUnitConverter::convertBool( nValue, sValue ) ) + o_rValue <<= nValue; + else + { + OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert a boolean value!" ); + } + } + else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) ) + { + o_rValue <<= sValue; + } +#if OSL_DEBUG_LEVEL > 0 + else + { + ::rtl::OString sMessage( "ConfigItemImport::getItemValue: unsupported item type '" ); + sMessage += ::rtl::OUStringToOString( rItemType, RTL_TEXTENCODING_UTF8 ); + sMessage += "', ignoring"; + OSL_ENSURE( false, sMessage.getStr() ); + } +#endif + } + + //==================================================================== + //= ConfigItemSetImport + //==================================================================== + //-------------------------------------------------------------------- + ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings ) + :ConfigItemImport( o_rSettings ) + { + } + + //-------------------------------------------------------------------- + ConfigItemSetImport::~ConfigItemSetImport() + { + } + + //-------------------------------------------------------------------- + ::rtl::Reference< SettingsImport > ConfigItemSetImport::nextState( const ::rtl::OUString& i_rElementName ) + { + // separate the namespace part from the element name + ::rtl::OUString sNamespace; + ::rtl::OUString sLocalName; + split( i_rElementName, sNamespace, sLocalName ); + + if ( sLocalName.equalsAscii( "config-item-set" ) ) + return new ConfigItemSetImport( m_aChildSettings ); + if ( sLocalName.equalsAscii( "config-item" ) ) + return new ConfigItemImport( m_aChildSettings ); + +#if OSL_DEBUG_LEVEL > 0 + ::rtl::OString sMessage( "unknown element name '" ); + sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 ); + sMessage += "', ignoring"; + OSL_ENSURE( false, sMessage.getStr() ); +#endif + return new IgnoringSettingsImport; + } + + //-------------------------------------------------------------------- + void ConfigItemSetImport::getItemValue( Any& o_rValue ) const + { + o_rValue <<= m_aChildSettings.getPropertyValues(); + } + +//........................................................................ +} // namespace dbaccess +//........................................................................ diff --git a/dbaccess/source/core/recovery/settingsimport.hxx b/dbaccess/source/core/recovery/settingsimport.hxx new file mode 100644 index 000000000000..3415092ee93a --- /dev/null +++ b/dbaccess/source/core/recovery/settingsimport.hxx @@ -0,0 +1,190 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef SETTINGSIMPORT_HXX +#define SETTINGSIMPORT_HXX + +/** === begin UNO includes === **/ +#include <com/sun/star/xml/sax/XAttributeList.hpp> +/** === end UNO includes === **/ + +#include <comphelper/namedvaluecollection.hxx> +#include <rtl/ref.hxx> +#include <rtl/ustrbuf.hxx> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + //==================================================================== + //= SettingsImport + //==================================================================== + /** a simplified version of xmloff/DocumentSettingsContext + + It would be nice if the DocumentSettingsContext would not be that tightly interwoven with the SvXMLImport + class, so we could re-use it here ... + */ + class SettingsImport : public ::rtl::IReference + { + public: + SettingsImport(); + + // IReference + virtual oslInterlockedCount SAL_CALL acquire(); + virtual oslInterlockedCount SAL_CALL release(); + + // own overriables + virtual ::rtl::Reference< SettingsImport > nextState( + const ::rtl::OUString& i_rElementName + ) = 0; + virtual void startElement( + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& i_rAttributes + ); + virtual void endElement(); + virtual void characters( const ::rtl::OUString& i_rCharacters ); + + protected: + virtual ~SettingsImport(); + + protected: + static void split( const ::rtl::OUString& i_rElementName, ::rtl::OUString& o_rNamespace, ::rtl::OUString& o_rLocalName ); + + protected: + const ::rtl::OUString& getItemName() const { return m_sItemName; } + const ::rtl::OUString& getItemType() const { return m_sItemType; } + const ::rtl::OUStringBuffer& getAccumulatedCharacters() const { return m_aCharacters; } + + private: + oslInterlockedCount m_refCount; + // value of the config:name attribute, if any + ::rtl::OUString m_sItemName; + // value of the config:type attribute, if any + ::rtl::OUString m_sItemType; + // accumulated characters, if any + ::rtl::OUStringBuffer m_aCharacters; + }; + + //==================================================================== + //= IgnoringSettingsImport + //==================================================================== + class IgnoringSettingsImport : public SettingsImport + { + public: + IgnoringSettingsImport() + { + } + + // SettingsImport overridables + virtual ::rtl::Reference< SettingsImport > nextState( + const ::rtl::OUString& i_rElementName + ); + + private: + ~IgnoringSettingsImport() + { + } + }; + + //==================================================================== + //= OfficeSettingsImport + //==================================================================== + class OfficeSettingsImport : public SettingsImport + { + public: + OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings ); + + // SettingsImport overridables + virtual ::rtl::Reference< SettingsImport > nextState( + const ::rtl::OUString& i_rElementName + ); + + protected: + ~OfficeSettingsImport(); + + private: + // the settings collection to which |this| will contribute a single setting + ::comphelper::NamedValueCollection& m_rSettings; + }; + + //==================================================================== + //= ConfigItemSetImport + //==================================================================== + class ConfigItemImport : public SettingsImport + { + public: + ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings ); + + protected: + ~ConfigItemImport(); + + public: + // SettingsImport overridables + virtual ::rtl::Reference< SettingsImport > nextState( + const ::rtl::OUString& i_rElementName + ); + virtual void endElement(); + + protected: + // own overridables + /// retrieves the value represented by the element + virtual void getItemValue( ::com::sun::star::uno::Any& o_rValue ) const; + + private: + // the settings collection to which |this| will contribute a single setting + ::comphelper::NamedValueCollection& m_rSettings; + }; + + //==================================================================== + //= ConfigItemSetImport + //==================================================================== + class ConfigItemSetImport : public ConfigItemImport + { + public: + ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings ); + + protected: + ~ConfigItemSetImport(); + + public: + // SettingsImport overridables + virtual ::rtl::Reference< SettingsImport > nextState( + const ::rtl::OUString& i_rElementName + ); + + protected: + // ConfigItemImport overridables + virtual void getItemValue( ::com::sun::star::uno::Any& o_rValue ) const; + + private: + /// the settings represented by our child elements + ::comphelper::NamedValueCollection m_aChildSettings; + }; + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // SETTINGSIMPORT_HXX diff --git a/dbaccess/source/core/recovery/storagestream.cxx b/dbaccess/source/core/recovery/storagestream.cxx new file mode 100644 index 000000000000..ec6ff49a6098 --- /dev/null +++ b/dbaccess/source/core/recovery/storagestream.cxx @@ -0,0 +1,126 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "storagestream.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/embed/ElementModes.hpp> +/** === end UNO includes === **/ + +#include <tools/diagnose_ex.h> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::embed::XStorage; + using ::com::sun::star::io::XStream; + /** === end UNO using === **/ + namespace ElementModes = ::com::sun::star::embed::ElementModes; + + //==================================================================== + //= StorageOutputStream + //==================================================================== + //-------------------------------------------------------------------- + StorageOutputStream::StorageOutputStream( const ::comphelper::ComponentContext& i_rContext, + const Reference< XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ) + :m_rContext( i_rContext ) + { + ENSURE_OR_THROW( i_rParentStorage.is(), "illegal stream" ); + + const Reference< XStream > xStream( + i_rParentStorage->openStreamElement( i_rStreamName, ElementModes::READWRITE ), UNO_QUERY_THROW ); + m_xOutputStream.set( xStream->getOutputStream(), UNO_SET_THROW ); + } + + //-------------------------------------------------------------------- + StorageOutputStream::~StorageOutputStream() + { + } + + //-------------------------------------------------------------------- + void StorageOutputStream::close() + { + ENSURE_OR_RETURN_VOID( m_xOutputStream.is(), "already closed" ); + m_xOutputStream->closeOutput(); + m_xOutputStream.clear(); + + // if you add additional functionality here, be aware that there are derived classes which + // (legitimately) do not call this method here. + } + + //==================================================================== + //= StorageInputStream + //==================================================================== + //-------------------------------------------------------------------- + StorageInputStream::StorageInputStream( const ::comphelper::ComponentContext& i_rContext, + const Reference< XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ) + :m_rContext( i_rContext ) + { + ENSURE_OR_THROW( i_rParentStorage.is(), "illegal stream" ); + + const Reference< XStream > xStream( + i_rParentStorage->openStreamElement( i_rStreamName, ElementModes::READ ), UNO_QUERY_THROW ); + m_xInputStream.set( xStream->getInputStream(), UNO_SET_THROW ); + } + + //-------------------------------------------------------------------- + StorageInputStream::~StorageInputStream() + { + } + + //-------------------------------------------------------------------- + void StorageInputStream::close() + { + ENSURE_OR_RETURN_VOID( m_xInputStream.is(), "already closed" ); + m_xInputStream->closeInput(); + m_xInputStream.clear(); + + // if you add additional functionality here, be aware that there are derived classes which + // (legitimately) do not call this method here. + } + +//........................................................................ +} // namespace dbaccess +//........................................................................ diff --git a/dbaccess/source/core/recovery/storagestream.hxx b/dbaccess/source/core/recovery/storagestream.hxx new file mode 100644 index 000000000000..2485cfac24cb --- /dev/null +++ b/dbaccess/source/core/recovery/storagestream.hxx @@ -0,0 +1,109 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef STORAGESTREAM_HXX +#define STORAGESTREAM_HXX + +#include "dbaccessdllapi.h" + +/** === begin UNO includes === **/ +#include <com/sun/star/embed/XStorage.hpp> +/** === end UNO includes === **/ + +namespace comphelper +{ + class ComponentContext; +} + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + //==================================================================== + //= StorageOutputStream + //==================================================================== + /** convenience wrapper around a stream living in a storage + */ + class DBACCESS_DLLPRIVATE StorageOutputStream + { + public: + StorageOutputStream( + const ::comphelper::ComponentContext& i_rContext, + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ); + virtual ~StorageOutputStream(); + + /** simply calls closeOutput on our output stream, override to extend/modify this behavior + */ + virtual void close(); + + protected: + const ::comphelper::ComponentContext& getContext() const { return m_rContext; } + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& + getOutputStream() const { return m_xOutputStream; } + + private: + const ::comphelper::ComponentContext& m_rContext; + ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > + m_xOutputStream; + }; + + //==================================================================== + //= StorageInputStream + //==================================================================== + /** convenience wrapper around a stream living in a storage + */ + class DBACCESS_DLLPRIVATE StorageInputStream + { + public: + StorageInputStream( + const ::comphelper::ComponentContext& i_rContext, + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ); + virtual ~StorageInputStream(); + + /** simply calls closeInput on our input stream, override to extend/modify this behavior + */ + virtual void close(); + + protected: + const ::comphelper::ComponentContext& getContext() const { return m_rContext; } + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& + getInputStream() const { return m_xInputStream; } + + private: + const ::comphelper::ComponentContext& m_rContext; + ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > + m_xInputStream; + }; + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // STORAGESTREAM_HXX diff --git a/dbaccess/source/core/recovery/storagetextstream.cxx b/dbaccess/source/core/recovery/storagetextstream.cxx new file mode 100644 index 000000000000..af1b832b67b3 --- /dev/null +++ b/dbaccess/source/core/recovery/storagetextstream.cxx @@ -0,0 +1,130 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "storagetextstream.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/io/XTextOutputStream.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +/** === end UNO includes === **/ + +#include <comphelper/componentcontext.hxx> +#include <tools/diagnose_ex.h> + +//...................................................................................................................... +namespace dbaccess +{ +//...................................................................................................................... + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::embed::XStorage; + using ::com::sun::star::io::XTextOutputStream; + using ::com::sun::star::io::XActiveDataSource; + /** === end UNO using === **/ + + //================================================================================================================== + //= StorageTextOutputStream_Data + //================================================================================================================== + struct StorageTextOutputStream_Data + { + Reference< XTextOutputStream > xTextOutput; + }; + + //================================================================================================================== + //= helper + //================================================================================================================== + namespace + { + //-------------------------------------------------------------------------------------------------------------- + static const ::rtl::OUString& lcl_getTextStreamEncodingName() + { + static const ::rtl::OUString s_sMapStreamEncodingName( RTL_CONSTASCII_USTRINGPARAM( "UTF-8" ) ); + return s_sMapStreamEncodingName; + } + + //-------------------------------------------------------------------------------------------------------------- + static const ::rtl::OUString& lcl_getLineFeed() + { + static const ::rtl::OUString s_sLineFeed( sal_Unicode( '\n' ) ); + return s_sLineFeed; + } + } + + //================================================================================================================== + //= StorageTextOutputStream + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + StorageTextOutputStream::StorageTextOutputStream( const ::comphelper::ComponentContext& i_rContext, + const Reference< XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ) + :StorageOutputStream( i_rContext, i_rParentStorage, i_rStreamName ) + ,m_pData( new StorageTextOutputStream_Data ) + { + m_pData->xTextOutput.set( i_rContext.createComponent( "com.sun.star.io.TextOutputStream" ), UNO_QUERY_THROW ); + m_pData->xTextOutput->setEncoding( lcl_getTextStreamEncodingName() ); + + Reference< XActiveDataSource > xDataSource( m_pData->xTextOutput, UNO_QUERY_THROW ); + xDataSource->setOutputStream( getOutputStream() ); + } + + //------------------------------------------------------------------------------------------------------------------ + StorageTextOutputStream::~StorageTextOutputStream() + { + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageTextOutputStream::writeLine( const ::rtl::OUString& i_rLine ) + { + ENSURE_OR_RETURN_VOID( m_pData->xTextOutput.is(), "no text output" ); + + m_pData->xTextOutput->writeString( i_rLine ); + m_pData->xTextOutput->writeString( lcl_getLineFeed() ); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageTextOutputStream::writeLine() + { + ENSURE_OR_RETURN_VOID( m_pData->xTextOutput.is(), "no text output" ); + + m_pData->xTextOutput->writeString( lcl_getLineFeed() ); + } + +//...................................................................................................................... +} // namespace dbaccess +//...................................................................................................................... diff --git a/dbaccess/source/core/recovery/storagetextstream.hxx b/dbaccess/source/core/recovery/storagetextstream.hxx new file mode 100644 index 000000000000..08d02008f1a4 --- /dev/null +++ b/dbaccess/source/core/recovery/storagetextstream.hxx @@ -0,0 +1,66 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef STORAGETEXTSTREAM_HXX +#define STORAGETEXTSTREAM_HXX + +#include "storagestream.hxx" + +/** === begin UNO includes === **/ +/** === end UNO includes === **/ + +#include <memory> + +//...................................................................................................................... +namespace dbaccess +{ +//...................................................................................................................... + + //================================================================================================================== + //= StorageTextStream + //================================================================================================================== + struct StorageTextOutputStream_Data; + class DBACCESS_DLLPRIVATE StorageTextOutputStream : public StorageOutputStream + { + public: + StorageTextOutputStream( + const ::comphelper::ComponentContext& i_rContext, + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ); + ~StorageTextOutputStream(); + + void writeLine( const ::rtl::OUString& i_rLine ); + void writeLine(); + + private: + ::std::auto_ptr< StorageTextOutputStream_Data > m_pData; + }; + +//...................................................................................................................... +} // namespace dbaccess +//...................................................................................................................... + +#endif // STORAGETEXTSTREAM_HXX diff --git a/dbaccess/source/core/recovery/storagexmlstream.cxx b/dbaccess/source/core/recovery/storagexmlstream.cxx new file mode 100644 index 000000000000..8405cce75903 --- /dev/null +++ b/dbaccess/source/core/recovery/storagexmlstream.cxx @@ -0,0 +1,197 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "storagexmlstream.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/xml/sax/XParser.hpp> +/** === end UNO includes === **/ + +#include <comphelper/componentcontext.hxx> +#include <cppuhelper/implbase1.hxx> +#include <rtl/ref.hxx> +#include <tools/diagnose_ex.h> +#include <xmloff/attrlist.hxx> + +#include <stack> + +//...................................................................................................................... +namespace dbaccess +{ +//...................................................................................................................... + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::embed::XStorage; + using ::com::sun::star::xml::sax::XDocumentHandler; + using ::com::sun::star::xml::sax::XAttributeList; + using ::com::sun::star::io::XStream; + using ::com::sun::star::io::XOutputStream; + using ::com::sun::star::io::XActiveDataSource; + using ::com::sun::star::xml::sax::XParser; + using ::com::sun::star::xml::sax::InputSource; + /** === end UNO using === **/ + + //================================================================================================================== + //= StorageXMLOutputStream_Data + //================================================================================================================== + struct StorageXMLOutputStream_Data + { + Reference< XDocumentHandler > xHandler; + ::std::stack< ::rtl::OUString > aElements; + ::rtl::Reference< SvXMLAttributeList > xAttributes; + }; + + //================================================================================================================== + //= StorageXMLOutputStream + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + StorageXMLOutputStream::StorageXMLOutputStream( const ::comphelper::ComponentContext& i_rContext, + const Reference< XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName ) + :StorageOutputStream( i_rContext, i_rParentStorage, i_rStreamName ) + ,m_pData( new StorageXMLOutputStream_Data ) + { + const Reference< XActiveDataSource > xSaxWriter( i_rContext.createComponent( "com.sun.star.xml.sax.Writer" ), UNO_QUERY_THROW ); + xSaxWriter->setOutputStream( getOutputStream() ); + + m_pData->xHandler.set( xSaxWriter, UNO_QUERY_THROW ); + m_pData->xHandler->startDocument(); + + m_pData->xAttributes = new SvXMLAttributeList; + } + + //------------------------------------------------------------------------------------------------------------------ + StorageXMLOutputStream::~StorageXMLOutputStream() + { + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::close() + { + ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "illegal document handler" ); + m_pData->xHandler->endDocument(); + // do not call the base class, it would call closeOutput on the output stream, which is already done by + // endDocument + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::addAttribute( const ::rtl::OUString& i_rName, const ::rtl::OUString& i_rValue ) const + { + m_pData->xAttributes->AddAttribute( i_rName, i_rValue ); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::startElement( const ::rtl::OUString& i_rElementName ) const + { + ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); + + m_pData->xHandler->startElement( i_rElementName, m_pData->xAttributes.get() ); + m_pData->xAttributes = new SvXMLAttributeList; + m_pData->aElements.push( i_rElementName ); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::endElement() const + { + ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); + ENSURE_OR_RETURN_VOID( !m_pData->aElements.empty(), "no element on the stack" ); + + const ::rtl::OUString sElementName( m_pData->aElements.top() ); + m_pData->xHandler->endElement( sElementName ); + m_pData->aElements.pop(); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::ignorableWhitespace( const ::rtl::OUString& i_rWhitespace ) const + { + ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); + + m_pData->xHandler->ignorableWhitespace( i_rWhitespace ); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLOutputStream::characters( const ::rtl::OUString& i_rCharacters ) const + { + ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); + + m_pData->xHandler->characters( i_rCharacters ); + } + + //================================================================================================================== + //= StorageXMLInputStream_Data + //================================================================================================================== + struct StorageXMLInputStream_Data + { + Reference< XParser > xParser; + }; + + //================================================================================================================== + //= StorageXMLInputStream + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + StorageXMLInputStream::StorageXMLInputStream( const ::comphelper::ComponentContext& i_rContext, + const Reference< XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName ) + :StorageInputStream( i_rContext, i_rParentStorage, i_rStreamName ) + ,m_pData( new StorageXMLInputStream_Data ) + { + m_pData->xParser.set( i_rContext.createComponent( "com.sun.star.xml.sax.Parser" ), UNO_QUERY_THROW ); + } + + //------------------------------------------------------------------------------------------------------------------ + void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler ) + { + ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" ); + + InputSource aInputSource; + aInputSource.aInputStream = getInputStream(); + + m_pData->xParser->setDocumentHandler( i_rHandler ); + m_pData->xParser->parseStream( aInputSource ); + } + + //------------------------------------------------------------------------------------------------------------------ + StorageXMLInputStream::~StorageXMLInputStream() + { + } + +//...................................................................................................................... +} // namespace dbaccess +//...................................................................................................................... diff --git a/dbaccess/source/core/recovery/storagexmlstream.hxx b/dbaccess/source/core/recovery/storagexmlstream.hxx new file mode 100644 index 000000000000..10a8aaaed5de --- /dev/null +++ b/dbaccess/source/core/recovery/storagexmlstream.hxx @@ -0,0 +1,113 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef STORAGEXMLSTREAM_HXX +#define STORAGEXMLSTREAM_HXX + +#include "storagestream.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/embed/XStorage.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +/** === end UNO includes === **/ + +#include <memory> + +namespace comphelper +{ + class ComponentContext; +} + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + //==================================================================== + //= StorageXMLOutputStream + //==================================================================== + struct StorageXMLOutputStream_Data; + class DBACCESS_DLLPRIVATE StorageXMLOutputStream : public StorageOutputStream + { + public: + StorageXMLOutputStream( + const ::comphelper::ComponentContext& i_rContext, + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ); + ~StorageXMLOutputStream(); + + // StorageOutputStream overridables + virtual void close(); + + void addAttribute( const ::rtl::OUString& i_rName, const ::rtl::OUString& i_rValue ) const; + + void startElement( const ::rtl::OUString& i_rElementName ) const; + void endElement() const; + + void ignorableWhitespace( const ::rtl::OUString& i_rWhitespace ) const; + void characters( const ::rtl::OUString& i_rCharacters ) const; + + private: + StorageXMLOutputStream(); // never implemented + StorageXMLOutputStream( const StorageXMLOutputStream& ); // never implemented + StorageXMLOutputStream& operator=( const StorageXMLOutputStream& ); // never implemented + + private: + ::std::auto_ptr< StorageXMLOutputStream_Data > m_pData; + }; + + //==================================================================== + //= StorageXMLInputStream + //==================================================================== + struct StorageXMLInputStream_Data; + class DBACCESS_DLLPRIVATE StorageXMLInputStream : public StorageInputStream + { + public: + StorageXMLInputStream( + const ::comphelper::ComponentContext& i_rContext, + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rParentStorage, + const ::rtl::OUString& i_rStreamName + ); + ~StorageXMLInputStream(); + + void import( + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler >& i_rHandler + ); + + private: + StorageXMLInputStream(); // never implemented + StorageXMLInputStream( const StorageXMLInputStream& ); // never implemented + StorageXMLInputStream& operator=( const StorageXMLInputStream& ); // never implemented + + private: + ::std::auto_ptr< StorageXMLInputStream_Data > m_pData; + }; + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // STORAGEXMLSTREAM_HXX diff --git a/dbaccess/source/core/recovery/subcomponentloader.cxx b/dbaccess/source/core/recovery/subcomponentloader.cxx new file mode 100644 index 000000000000..5fd120a5009b --- /dev/null +++ b/dbaccess/source/core/recovery/subcomponentloader.cxx @@ -0,0 +1,200 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "subcomponentloader.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/ucb/Command.hpp> +#include <com/sun/star/frame/XController2.hpp> +/** === end UNO includes === **/ + +#include <tools/diagnose_ex.h> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::frame::XController; + using ::com::sun::star::frame::XFrame; + using ::com::sun::star::awt::XWindow; + using ::com::sun::star::awt::WindowEvent; + using ::com::sun::star::lang::EventObject; + using ::com::sun::star::ucb::Command; + using ::com::sun::star::ucb::XCommandProcessor; + using ::com::sun::star::frame::XController2; + using ::com::sun::star::lang::XComponent; + /** === end UNO using === **/ + + //==================================================================== + //= SubComponentLoader + //==================================================================== + struct DBACCESS_DLLPRIVATE SubComponentLoader_Data + { + const Reference< XCommandProcessor > xDocDefCommands; + const Reference< XComponent > xNonDocComponent; + Reference< XWindow > xAppComponentWindow; + + SubComponentLoader_Data( const Reference< XCommandProcessor >& i_rDocumentDefinition ) + :xDocDefCommands( i_rDocumentDefinition, UNO_SET_THROW ) + ,xNonDocComponent() + { + } + + SubComponentLoader_Data( const Reference< XComponent >& i_rNonDocumentComponent ) + :xDocDefCommands() + ,xNonDocComponent( i_rNonDocumentComponent, UNO_SET_THROW ) + { + } + }; + + //==================================================================== + //= helper + //==================================================================== + namespace + { + //................................................................ + void lcl_onWindowShown_nothrow( const SubComponentLoader_Data& i_rData ) + { + try + { + if ( i_rData.xDocDefCommands.is() ) + { + Command aCommandOpen; + aCommandOpen.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "show" ) ); + + const sal_Int32 nCommandIdentifier = i_rData.xDocDefCommands->createCommandIdentifier(); + i_rData.xDocDefCommands->execute( aCommandOpen, nCommandIdentifier, NULL ); + } + else + { + const Reference< XController > xController( i_rData.xNonDocComponent, UNO_QUERY_THROW ); + const Reference< XFrame > xFrame( xController->getFrame(), UNO_SET_THROW ); + const Reference< XWindow > xWindow( xFrame->getContainerWindow(), UNO_SET_THROW ); + xWindow->setVisible( sal_True ); + } + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + } + + //==================================================================== + //= SubComponentLoader + //==================================================================== + //-------------------------------------------------------------------- + SubComponentLoader::SubComponentLoader( const Reference< XController >& i_rApplicationController, + const Reference< XCommandProcessor >& i_rSubDocumentDefinition ) + :m_pData( new SubComponentLoader_Data( i_rSubDocumentDefinition ) ) + { + // add as window listener to the controller's container window, so we get notified when it is shown + Reference< XController2 > xController( i_rApplicationController, UNO_QUERY_THROW ); + m_pData->xAppComponentWindow.set( xController->getComponentWindow(), UNO_SET_THROW ); + + osl_incrementInterlockedCount( &m_refCount ); + { + m_pData->xAppComponentWindow->addWindowListener( this ); + } + osl_decrementInterlockedCount( &m_refCount ); + } + + //-------------------------------------------------------------------- + SubComponentLoader::SubComponentLoader( const Reference< XController >& i_rApplicationController, + const Reference< XComponent >& i_rNonDocumentComponent ) + :m_pData( new SubComponentLoader_Data( i_rNonDocumentComponent ) ) + { + // add as window listener to the controller's container window, so we get notified when it is shown + Reference< XController2 > xController( i_rApplicationController, UNO_QUERY_THROW ); + m_pData->xAppComponentWindow.set( xController->getComponentWindow(), UNO_SET_THROW ); + + osl_incrementInterlockedCount( &m_refCount ); + { + m_pData->xAppComponentWindow->addWindowListener( this ); + } + osl_decrementInterlockedCount( &m_refCount ); + } + + //-------------------------------------------------------------------- + SubComponentLoader::~SubComponentLoader() + { + delete m_pData, m_pData = NULL; + } + + //-------------------------------------------------------------------- + void SAL_CALL SubComponentLoader::windowResized( const WindowEvent& i_rEvent ) throw (RuntimeException) + { + // not interested in + (void)i_rEvent; + } + + //-------------------------------------------------------------------- + void SAL_CALL SubComponentLoader::windowMoved( const WindowEvent& i_rEvent ) throw (RuntimeException) + { + // not interested in + (void)i_rEvent; + } + + //-------------------------------------------------------------------- + void SAL_CALL SubComponentLoader::windowShown( const EventObject& i_rEvent ) throw (RuntimeException) + { + (void)i_rEvent; + + lcl_onWindowShown_nothrow( *m_pData ); + m_pData->xAppComponentWindow->removeWindowListener( this ); + } + + //-------------------------------------------------------------------- + void SAL_CALL SubComponentLoader::windowHidden( const EventObject& i_rEvent ) throw (RuntimeException) + { + // not interested in + (void)i_rEvent; + } + + //-------------------------------------------------------------------- + void SAL_CALL SubComponentLoader::disposing( const EventObject& i_rEvent ) throw (RuntimeException) + { + // not interested in + (void)i_rEvent; + } + +//........................................................................ +} // namespace dbaccess +//........................................................................ diff --git a/dbaccess/source/core/recovery/subcomponentloader.hxx b/dbaccess/source/core/recovery/subcomponentloader.hxx new file mode 100644 index 000000000000..f3bec5b6b39e --- /dev/null +++ b/dbaccess/source/core/recovery/subcomponentloader.hxx @@ -0,0 +1,87 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef SUBCOMPONENTLOADER_HXX +#define SUBCOMPONENTLOADER_HXX + +#include "dbaccessdllapi.h" + +/** === begin UNO includes === **/ +#include <com/sun/star/awt/XWindowListener.hpp> +#include <com/sun/star/frame/XController.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/ucb/XCommandProcessor.hpp> +/** === end UNO includes === **/ + +#include <cppuhelper/implbase1.hxx> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + //==================================================================== + //= SubComponentLoader + //==================================================================== + typedef ::cppu::WeakImplHelper1 < ::com::sun::star::awt::XWindowListener + > SubComponentLoader_Base; + struct SubComponentLoader_Data; + /** is a helper class which loads/opens a given sub component as soon as the main application + window becomes visible. + */ + class DBACCESS_DLLPRIVATE SubComponentLoader : public SubComponentLoader_Base + { + public: + SubComponentLoader( + const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& i_rApplicationController, + const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandProcessor >& i_rSubDocumentDefinition + ); + + SubComponentLoader( + const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController >& i_rApplicationController, + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& i_rNonDocumentComponent + ); + + // XWindowListener + virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException); + + // XEventListener + virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException); + + protected: + virtual ~SubComponentLoader(); + + private: + SubComponentLoader_Data* m_pData; + }; + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // SUBCOMPONENTLOADER_HXX diff --git a/dbaccess/source/core/recovery/subcomponentrecovery.cxx b/dbaccess/source/core/recovery/subcomponentrecovery.cxx new file mode 100644 index 000000000000..0ae69402faeb --- /dev/null +++ b/dbaccess/source/core/recovery/subcomponentrecovery.cxx @@ -0,0 +1,702 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#include "precompiled_dbaccess.hxx" + +#include "subcomponentrecovery.hxx" + +#include "sdbcoretools.hxx" +#include "storagexmlstream.hxx" +#include "subcomponentloader.hxx" +#include "settingsimport.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/embed/ElementModes.hpp> +#include <com/sun/star/frame/XModuleManager.hpp> +#include <com/sun/star/document/XStorageBasedDocument.hpp> +#include <com/sun/star/ucb/XCommandProcessor.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/sdb/XFormDocumentsSupplier.hpp> +#include <com/sun/star/sdb/XReportDocumentsSupplier.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +/** === end UNO includes === **/ + +#include <comphelper/namedvaluecollection.hxx> +#include <connectivity/dbtools.hxx> +#include <tools/diagnose_ex.h> +#include <xmloff/XMLSettingsExportContext.hxx> +#include <xmloff/SettingsExportHelper.hxx> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::lang::XMultiServiceFactory; + using ::com::sun::star::embed::XStorage; + using ::com::sun::star::sdb::application::XDatabaseDocumentUI; + using ::com::sun::star::beans::Pair; + using ::com::sun::star::frame::XModuleManager; + using ::com::sun::star::lang::XComponent; + using ::com::sun::star::frame::XModel; + using ::com::sun::star::frame::XController; + using ::com::sun::star::beans::XPropertySet; + using ::com::sun::star::beans::PropertyValue; + using ::com::sun::star::document::XStorageBasedDocument; + using ::com::sun::star::ucb::XCommandProcessor; + using ::com::sun::star::container::XHierarchicalNameAccess; + using ::com::sun::star::sdb::XFormDocumentsSupplier; + using ::com::sun::star::sdb::XReportDocumentsSupplier; + using ::com::sun::star::xml::sax::SAXException; + using ::com::sun::star::xml::sax::XLocator; + using ::com::sun::star::xml::sax::XDocumentHandler; + using ::com::sun::star::xml::sax::XAttributeList; + /** === end UNO using === **/ + + namespace ElementModes = ::com::sun::star::embed::ElementModes; + + + //==================================================================== + //= helper + //==================================================================== + namespace + { + // ......................................................................... + static const ::rtl::OUString& lcl_getComponentStorageBaseName( const SubComponentType i_eType ) + { + static const ::rtl::OUString s_sFormBaseName( RTL_CONSTASCII_USTRINGPARAM( "form" ) ); + static const ::rtl::OUString s_sReportBaseName( RTL_CONSTASCII_USTRINGPARAM( "report" ) ); + static const ::rtl::OUString s_sTableBaseName( RTL_CONSTASCII_USTRINGPARAM( "table" ) ); + static const ::rtl::OUString s_sQueryBaseName( RTL_CONSTASCII_USTRINGPARAM( "query" ) ); + + switch ( i_eType ) + { + case FORM: + return s_sFormBaseName; + case REPORT: + return s_sReportBaseName; + case TABLE: + return s_sTableBaseName; + case QUERY: + return s_sQueryBaseName; + default: + break; + } + + OSL_ENSURE( false, "lcl_getComponentStorageBaseName: unimplemented case!" ); + static const ::rtl::OUString s_sFallback; + return s_sFallback; + } + + // ......................................................................... + static SubComponentType lcl_databaseObjectToSubComponentType( const sal_Int32 i_nObjectType ) + { + switch ( i_nObjectType ) + { + case DatabaseObject::TABLE: return TABLE; + case DatabaseObject::QUERY: return QUERY; + case DatabaseObject::FORM: return FORM; + case DatabaseObject::REPORT:return REPORT; + default: + break; + } + return UNKNOWN; + } + + // ......................................................................... + static bool lcl_determineReadOnly( const Reference< XComponent >& i_rComponent ) + { + Reference< XModel > xDocument( i_rComponent, UNO_QUERY ); + if ( !xDocument.is() ) + { + Reference< XController > xController( i_rComponent, UNO_QUERY_THROW ); + xDocument = xController->getModel(); + } + + if ( !xDocument.is() ) + return false; + + ::comphelper::NamedValueCollection aDocArgs( xDocument->getArgs() ); + return aDocArgs.getOrDefault( "ReadOnly", false ); + } + + // ......................................................................... + static Reference< XCommandProcessor > lcl_getSubComponentDef_nothrow( const Reference< XDatabaseDocumentUI >& i_rAppUI, + const SubComponentType i_eType, const ::rtl::OUString& i_rName ) + { + Reference< XController > xController( i_rAppUI, UNO_QUERY_THROW ); + ENSURE_OR_RETURN( ( i_eType == FORM ) || ( i_eType == REPORT ), "lcl_getSubComponentDef_nothrow: illegal controller", NULL ); + + Reference< XCommandProcessor > xCommandProcessor; + try + { + Reference< XHierarchicalNameAccess > xDefinitionContainer; + if ( i_eType == FORM ) + { + Reference< XFormDocumentsSupplier > xSuppForms( xController->getModel(), UNO_QUERY_THROW ); + xDefinitionContainer.set( xSuppForms->getFormDocuments(), UNO_QUERY_THROW ); + } + else + { + Reference< XReportDocumentsSupplier > xSuppReports( xController->getModel(), UNO_QUERY_THROW ); + xDefinitionContainer.set( xSuppReports->getReportDocuments(), UNO_QUERY_THROW ); + } + xCommandProcessor.set( xDefinitionContainer->getByHierarchicalName( i_rName ), UNO_QUERY_THROW ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + return xCommandProcessor; + } + + // ......................................................................... + static const ::rtl::OUString& lcl_getSettingsStreamName() + { + static const ::rtl::OUString s_sStatementStreamName( RTL_CONSTASCII_USTRINGPARAM( "settings.xml" ) ); + return s_sStatementStreamName; + } + + // ......................................................................... + static const ::rtl::OUString& lcl_getCurrentQueryDesignName() + { + static const ::rtl::OUString s_sQuerySettingsName( RTL_CONSTASCII_USTRINGPARAM( "ooo:current-query-design" ) ); + return s_sQuerySettingsName; + } + } + + //==================================================================== + //= SettingsExportContext + //==================================================================== + class DBACCESS_DLLPRIVATE SettingsExportContext : public ::xmloff::XMLSettingsExportContext + { + public: + SettingsExportContext( const ::comphelper::ComponentContext& i_rContext, const StorageXMLOutputStream& i_rDelegator ) + :m_rContext( i_rContext ) + ,m_rDelegator( i_rDelegator ) + ,m_aNamespace( ::xmloff::token::GetXMLToken( ::xmloff::token::XML_NP_CONFIG ) ) + { + } + + virtual ~SettingsExportContext() + { + } + + public: + virtual void AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const ::rtl::OUString& i_rValue ); + virtual void AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue ); + virtual void StartElement( enum ::xmloff::token::XMLTokenEnum i_eName, const sal_Bool i_bIgnoreWhitespace ); + virtual void EndElement ( const sal_Bool i_bIgnoreWhitespace ); + virtual void Characters( const ::rtl::OUString& i_rCharacters ); + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > + GetServiceFactory() const; + + private: + ::rtl::OUString impl_prefix( const ::xmloff::token::XMLTokenEnum i_eToken ) + { + ::rtl::OUStringBuffer aQualifiedName( m_aNamespace ); + aQualifiedName.append( sal_Unicode( ':' ) ); + aQualifiedName.append( ::xmloff::token::GetXMLToken( i_eToken ) ); + return aQualifiedName.makeStringAndClear(); + } + + private: + const ::comphelper::ComponentContext& m_rContext; + const StorageXMLOutputStream& m_rDelegator; + const ::rtl::OUStringBuffer m_aNamespace; + }; + + //-------------------------------------------------------------------- + void SettingsExportContext::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, const ::rtl::OUString& i_rValue ) + { + m_rDelegator.addAttribute( impl_prefix( i_eName ), i_rValue ); + } + + //-------------------------------------------------------------------- + void SettingsExportContext::AddAttribute( enum ::xmloff::token::XMLTokenEnum i_eName, enum ::xmloff::token::XMLTokenEnum i_eValue ) + { + m_rDelegator.addAttribute( impl_prefix( i_eName ), ::xmloff::token::GetXMLToken( i_eValue ) ); + } + + //-------------------------------------------------------------------- + void SettingsExportContext::StartElement( enum ::xmloff::token::XMLTokenEnum i_eName, const sal_Bool i_bIgnoreWhitespace ) + { + if ( i_bIgnoreWhitespace ) + m_rDelegator.ignorableWhitespace( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ) ); + + m_rDelegator.startElement( impl_prefix( i_eName ) ); + } + + //-------------------------------------------------------------------- + void SettingsExportContext::EndElement( const sal_Bool i_bIgnoreWhitespace ) + { + if ( i_bIgnoreWhitespace ) + m_rDelegator.ignorableWhitespace( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ) ); + m_rDelegator.endElement(); + } + + //-------------------------------------------------------------------- + void SettingsExportContext::Characters( const ::rtl::OUString& i_rCharacters ) + { + m_rDelegator.characters( i_rCharacters ); + } + + //-------------------------------------------------------------------- + Reference< XMultiServiceFactory > SettingsExportContext::GetServiceFactory() const + { + return m_rContext.getLegacyServiceFactory(); + } + + //================================================================================================================== + //= SettingsDocumentHandler + //================================================================================================================== + typedef ::cppu::WeakImplHelper1 < XDocumentHandler + > SettingsDocumentHandler_Base; + class DBACCESS_DLLPRIVATE SettingsDocumentHandler : public SettingsDocumentHandler_Base + { + public: + SettingsDocumentHandler() + { + } + + protected: + virtual ~SettingsDocumentHandler() + { + } + + public: + // XDocumentHandler + virtual void SAL_CALL startDocument( ) throw (SAXException, RuntimeException); + virtual void SAL_CALL endDocument( ) throw (SAXException, RuntimeException); + virtual void SAL_CALL startElement( const ::rtl::OUString& aName, const Reference< XAttributeList >& xAttribs ) throw (SAXException, RuntimeException); + virtual void SAL_CALL endElement( const ::rtl::OUString& aName ) throw (SAXException, RuntimeException); + virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw (SAXException, RuntimeException); + virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (SAXException, RuntimeException); + virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData ) throw (SAXException, RuntimeException); + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) throw (SAXException, RuntimeException); + + const ::comphelper::NamedValueCollection& getSettings() const { return m_aSettings; } + + private: + ::std::stack< ::rtl::Reference< SettingsImport > > m_aStates; + ::comphelper::NamedValueCollection m_aSettings; + }; + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::startDocument( ) throw (SAXException, RuntimeException) + { + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::endDocument( ) throw (SAXException, RuntimeException) + { + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::startElement( const ::rtl::OUString& i_Name, const Reference< XAttributeList >& i_Attribs ) throw (SAXException, RuntimeException) + { + ::rtl::Reference< SettingsImport > pNewState; + + if ( m_aStates.empty() ) + { + if ( i_Name.equalsAscii( "office:settings" ) ) + { + pNewState = new OfficeSettingsImport( m_aSettings ); + } + else + { + OSL_ENSURE( false, "SettingsDocumentHandler::startElement: invalid settings file!" ); + // Yes, that's not correct. Somebody could, in theory, give us a document which starts with "foo:settings", + // where "foo" is mapped to the proper namespace URL. + // However, there's no need to bother with this. The "recovery" sub storage we're recovering from is + // not part of ODF, so we can impose any format restrictions on it ... + } + } + else + { + ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() ); + pNewState = pCurrentState->nextState( i_Name ); + } + + ENSURE_OR_THROW( pNewState.is(), "no new state - aborting import" ); + pNewState->startElement( i_Attribs ); + + m_aStates.push( pNewState ); + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::endElement( const ::rtl::OUString& i_Name ) throw (SAXException, RuntimeException) + { + ENSURE_OR_THROW( !m_aStates.empty(), "no active element" ); + (void)i_Name; + + ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() ); + pCurrentState->endElement(); + m_aStates.pop(); + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::characters( const ::rtl::OUString& i_Chars ) throw (SAXException, RuntimeException) + { + ENSURE_OR_THROW( !m_aStates.empty(), "no active element" ); + + ::rtl::Reference< SettingsImport > pCurrentState( m_aStates.top() ); + pCurrentState->characters( i_Chars ); + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (SAXException, RuntimeException) + { + // ignore them - that's why they're called "ignorable" + (void)aWhitespaces; + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::processingInstruction( const ::rtl::OUString& i_Target, const ::rtl::OUString& i_Data ) throw (SAXException, RuntimeException) + { + OSL_ENSURE( false, "SettingsDocumentHandler::processingInstruction: unexpected ..." ); + (void)i_Target; + (void)i_Data; + } + + //-------------------------------------------------------------------- + void SAL_CALL SettingsDocumentHandler::setDocumentLocator( const Reference< XLocator >& i_Locator ) throw (SAXException, RuntimeException) + { + (void)i_Locator; + } + + //==================================================================== + //= SubComponentRecovery + //==================================================================== + //-------------------------------------------------------------------- + const ::rtl::OUString SubComponentRecovery::getComponentsStorageName( const SubComponentType i_eType ) + { + static const ::rtl::OUString s_sFormsStorageName( RTL_CONSTASCII_USTRINGPARAM( "forms" ) ); + static const ::rtl::OUString s_sReportsStorageName( RTL_CONSTASCII_USTRINGPARAM( "reports" ) ); + static const ::rtl::OUString s_sTablesStorageName( RTL_CONSTASCII_USTRINGPARAM( "tables" ) ); + static const ::rtl::OUString s_sQueriesStorageName( RTL_CONSTASCII_USTRINGPARAM( "queries" ) ); + static const ::rtl::OUString s_sRelationsStorageName( RTL_CONSTASCII_USTRINGPARAM( "relations" ) ); + + switch ( i_eType ) + { + case FORM: + return s_sFormsStorageName; + case REPORT: + return s_sReportsStorageName; + case TABLE: + return s_sTablesStorageName; + case QUERY: + return s_sQueriesStorageName; + case RELATION_DESIGN: + return s_sRelationsStorageName; + default: + break; + } + + OSL_ENSURE( false, "SubComponentRecovery::getComponentsStorageName: unimplemented case!" ); + static const ::rtl::OUString s_sFallback; + return s_sFallback; + } + + //-------------------------------------------------------------------- + void SubComponentRecovery::saveToRecoveryStorage( const Reference< XStorage >& i_rRecoveryStorage, + MapCompTypeToCompDescs& io_mapCompDescs ) + { + if ( m_eType == UNKNOWN ) + // quite fatal, but has already been reported (as assertion) before + return; + + // open the sub storage for the given kind of components + const ::rtl::OUString& rStorageName( getComponentsStorageName( m_eType ) ); + const Reference< XStorage > xComponentsStorage( i_rRecoveryStorage->openStorageElement( + rStorageName, ElementModes::READWRITE ), UNO_QUERY_THROW ); + + // find a free sub storage name, and create Yet Another Sub Storage + const ::rtl::OUString& rBaseName( lcl_getComponentStorageBaseName( m_eType ) ); + const ::rtl::OUString sStorName = ::dbtools::createUniqueName( xComponentsStorage.get(), rBaseName, true ); + const Reference< XStorage > xObjectStor( xComponentsStorage->openStorageElement( + sStorName, ElementModes::READWRITE ), UNO_QUERY_THROW ); + + switch ( m_eType ) + { + case FORM: + case REPORT: + impl_saveSubDocument_throw( xObjectStor ); + break; + + case QUERY: + impl_saveQueryDesign_throw( xObjectStor ); + break; + + default: + // TODO + OSL_ENSURE( false, "SubComponentRecoverys::saveToRecoveryStorage: unimplemented case!" ); + break; + } + + // commit the storage(s) + tools::stor::commitStorageIfWriteable( xObjectStor ); + tools::stor::commitStorageIfWriteable( xComponentsStorage ); + + // remember the relationship from the component name to the storage name + MapStringToCompDesc& rMapCompDescs = io_mapCompDescs[ m_eType ]; + OSL_ENSURE( rMapCompDescs.find( sStorName ) == rMapCompDescs.end(), + "SubComponentRecoverys::saveToRecoveryStorage: object name already used!" ); + rMapCompDescs[ sStorName ] = m_aCompDesc; + } + + //-------------------------------------------------------------------- + void SubComponentRecovery::impl_identifyComponent_throw() + { + // ask the controller + Pair< sal_Int32, ::rtl::OUString > aComponentIdentity = m_xDocumentUI->identifySubComponent( m_xComponent ); + m_eType = lcl_databaseObjectToSubComponentType( aComponentIdentity.First ); + m_aCompDesc.sName = aComponentIdentity.Second; + + // what the controller didn't give us is the information whether this is in edit mode or not ... + Reference< XModuleManager > xModuleManager( m_rContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW ); + const ::rtl::OUString sModuleIdentifier = xModuleManager->identify( m_xComponent ); + + switch ( m_eType ) + { + case TABLE: + m_aCompDesc.bForEditing = sModuleIdentifier.equalsAscii( "com.sun.star.sdb.TableDesign" ); + break; + + case QUERY: + m_aCompDesc.bForEditing = sModuleIdentifier.equalsAscii( "com.sun.star.sdb.QueryDesign" ); + break; + + case REPORT: + if ( sModuleIdentifier.equalsAscii( "com.sun.star.report.ReportDefinition" ) ) + { + // it's an SRB report desginer + m_aCompDesc.bForEditing = true; + break; + } + // fall through + + case FORM: + m_aCompDesc.bForEditing = !lcl_determineReadOnly( m_xComponent ); + break; + + default: + if ( sModuleIdentifier.equalsAscii( "com.sun.star.sdb.RelationDesign" ) ) + { + m_eType = RELATION_DESIGN; + m_aCompDesc.bForEditing = true; + } + else + { + OSL_ENSURE( false, "SubComponentRecovery::impl_identifyComponent_throw: couldn't classify the given sub component!" ); + } + break; + } + + OSL_POSTCOND( m_eType != UNKNOWN, + "SubComponentRecovery::impl_identifyComponent_throw: couldn't classify the component!" ); + } + + //-------------------------------------------------------------------- + void SubComponentRecovery::impl_saveQueryDesign_throw( const Reference< XStorage >& i_rObjectStorage ) + { + ENSURE_OR_THROW( m_eType == QUERY, "illegal sub component type" ); + ENSURE_OR_THROW( i_rObjectStorage.is(), "illegal storage" ); + + // retrieve the current query design (which might differ from what we can retrieve as ActiveCommand property, since + // the latter is updated only upon successful save of the design) + Reference< XPropertySet > xDesignerProps( m_xComponent, UNO_QUERY_THROW ); + Sequence< PropertyValue > aCurrentQueryDesign; + OSL_VERIFY( xDesignerProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CurrentQueryDesign" ) ) ) >>= aCurrentQueryDesign ); + + // write the query design + StorageXMLOutputStream aDesignOutput( m_rContext, i_rObjectStorage, lcl_getSettingsStreamName() ); + SettingsExportContext aSettingsExportContext( m_rContext, aDesignOutput ); + + const ::rtl::OUString sWhitespace( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ) ) ); + + aDesignOutput.startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "office:settings" ) ) ); + aDesignOutput.ignorableWhitespace( sWhitespace ); + + XMLSettingsExportHelper aSettingsExporter( aSettingsExportContext ); + aSettingsExporter.exportAllSettings( aCurrentQueryDesign, lcl_getCurrentQueryDesignName() ); + + aDesignOutput.ignorableWhitespace( sWhitespace ); + aDesignOutput.endElement(); + aDesignOutput.close(); + } + + //-------------------------------------------------------------------- + void SubComponentRecovery::impl_saveSubDocument_throw( const Reference< XStorage >& i_rObjectStorage ) + { + ENSURE_OR_THROW( ( m_eType == FORM ) || ( m_eType == REPORT ), "illegal sub component type" ); + ENSURE_OR_THROW( i_rObjectStorage.is(), "illegal storage" ); + + // store the document into the storage + Reference< XStorageBasedDocument > xStorageDocument( m_xComponent, UNO_QUERY_THROW ); + xStorageDocument->storeToStorage( i_rObjectStorage, Sequence< PropertyValue >() ); + } + + //-------------------------------------------------------------------- + Reference< XComponent > SubComponentRecovery::impl_recoverSubDocument_throw( const Reference< XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, const bool i_bForEditing ) + { + Reference< XComponent > xSubComponent; + Reference< XCommandProcessor > xDocDefinition; + + ::comphelper::NamedValueCollection aLoadArgs; + aLoadArgs.put( "RecoveryStorage", i_rRecoveryStorage ); + + // load/create the sub component hidden. We'll show it when the main app window is shown. + aLoadArgs.put( "Hidden", true ); + + if ( i_rComponentName.getLength() ) + { + xDocDefinition = lcl_getSubComponentDef_nothrow( m_xDocumentUI, m_eType, i_rComponentName ); + xSubComponent.set( m_xDocumentUI->loadComponentWithArguments( + m_eType, + i_rComponentName, + i_bForEditing, + aLoadArgs.getPropertyValues() + ), + UNO_SET_THROW + ); + } + else + { + Reference< XComponent > xDocDefComponent; + xSubComponent.set( m_xDocumentUI->createComponentWithArguments( + m_eType, + aLoadArgs.getPropertyValues(), + xDocDefComponent + ), + UNO_SET_THROW + ); + + xDocDefinition.set( xDocDefComponent, UNO_QUERY ); + OSL_ENSURE( xDocDefinition.is(), "DatabaseDocumentRecovery::recoverSubDocuments: loaded a form/report, but don't have a document definition?!" ); + } + + if ( xDocDefinition.is() ) + { + Reference< XController > xController( m_xDocumentUI, UNO_QUERY_THROW ); + Reference< XInterface > xLoader( *new SubComponentLoader( xController, xDocDefinition ) ); + (void)xLoader; + } + + return xSubComponent; + } + + //-------------------------------------------------------------------- + Reference< XComponent > SubComponentRecovery::impl_recoverQueryDesign_throw( const Reference< XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, const bool i_bForEditing ) + { + Reference< XComponent > xSubComponent; + + // first read the settings query design settings from the storage + StorageXMLInputStream aDesignInput( m_rContext, i_rRecoveryStorage, lcl_getSettingsStreamName() ); + + ::rtl::Reference< SettingsDocumentHandler > pDocHandler( new SettingsDocumentHandler ); + aDesignInput.import( pDocHandler.get() ); + + const ::comphelper::NamedValueCollection& rSettings( pDocHandler->getSettings() ); + const Any aCurrentQueryDesign = rSettings.get( lcl_getCurrentQueryDesignName() ); +#if OSL_DEBUG_LEVEL > 0 + Sequence< PropertyValue > aQueryDesignLayout; + OSL_VERIFY( aCurrentQueryDesign >>= aQueryDesignLayout ); +#endif + + // then load the query designer + ::comphelper::NamedValueCollection aLoadArgs; + aLoadArgs.put( "CurrentQueryDesign", aCurrentQueryDesign ); + aLoadArgs.put( "Hidden", true ); + + if ( i_rComponentName.getLength() ) + { + xSubComponent.set( m_xDocumentUI->loadComponentWithArguments( + m_eType, + i_rComponentName, + i_bForEditing, + aLoadArgs.getPropertyValues() + ), + UNO_SET_THROW + ); + } + else + { + Reference< XComponent > xDummy; + xSubComponent.set( m_xDocumentUI->createComponentWithArguments( + m_eType, + aLoadArgs.getPropertyValues(), + xDummy + ), + UNO_SET_THROW + ); + } + + Reference< XController > xController( m_xDocumentUI, UNO_QUERY_THROW ); + Reference< XInterface > xLoader( *new SubComponentLoader( xController, xSubComponent ) ); + (void)xLoader; + + return xSubComponent; + } + + //-------------------------------------------------------------------- + Reference< XComponent > SubComponentRecovery::recoverFromStorage( const Reference< XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, const bool i_bForEditing ) + { + Reference< XComponent > xSubComponent; + switch ( m_eType ) + { + case FORM: + case REPORT: + xSubComponent = impl_recoverSubDocument_throw( i_rRecoveryStorage, i_rComponentName, i_bForEditing ); + break; + case QUERY: + xSubComponent = impl_recoverQueryDesign_throw( i_rRecoveryStorage, i_rComponentName, i_bForEditing ); + break; + default: + OSL_ENSURE( false, "SubComponentRecovery::recoverFromStorage: unimplemented case!" ); + break; + } + return xSubComponent; + } + +//........................................................................ +} // namespace dbaccess +//........................................................................ diff --git a/dbaccess/source/core/recovery/subcomponentrecovery.hxx b/dbaccess/source/core/recovery/subcomponentrecovery.hxx new file mode 100644 index 000000000000..2cec262937a8 --- /dev/null +++ b/dbaccess/source/core/recovery/subcomponentrecovery.hxx @@ -0,0 +1,126 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef SUBCOMPONENTRECOVERY_HXX +#define SUBCOMPONENTRECOVERY_HXX + +#include "subcomponents.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp> +#include <com/sun/star/embed/XStorage.hpp> +/** === end UNO includes === **/ + +#include <comphelper/componentcontext.hxx> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + //==================================================================== + //= SubComponentRecovery + //==================================================================== + class DBACCESS_DLLPRIVATE SubComponentRecovery + { + public: + SubComponentRecovery( const ::comphelper::ComponentContext& i_rContext, const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::application::XDatabaseDocumentUI >& i_rController, + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& i_rComponent ) + :m_rContext( i_rContext ) + ,m_xDocumentUI( i_rController, ::com::sun::star::uno::UNO_SET_THROW ) + ,m_xComponent( i_rComponent ) + ,m_eType( UNKNOWN ) + ,m_aCompDesc() + { + impl_identifyComponent_throw(); + } + + SubComponentRecovery( const ::comphelper::ComponentContext& i_rContext, const ::com::sun::star::uno::Reference< ::com::sun::star::sdb::application::XDatabaseDocumentUI >& i_rController, + const SubComponentType i_eType ) + :m_rContext( i_rContext ) + ,m_xDocumentUI( i_rController, ::com::sun::star::uno::UNO_SET_THROW ) + ,m_xComponent() + ,m_eType( i_eType ) + ,m_aCompDesc() + { + } + + // only to be used after being constructed with a component + void saveToRecoveryStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rRecoveryStorage, + MapCompTypeToCompDescs& io_mapCompDescs + ); + + // only to be used after being constructed with a type + ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > + recoverFromStorage( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, + const bool i_bForEditing + ); + + static const ::rtl::OUString getComponentsStorageName( const SubComponentType i_eType ); + + private: + void impl_saveSubDocument_throw( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rObjectStorage + ); + + void impl_saveQueryDesign_throw( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rObjectStorage + ); + + ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > + impl_recoverSubDocument_throw( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, + const bool i_bForEditing + ); + + ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > + impl_recoverQueryDesign_throw( + const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& i_rRecoveryStorage, + const ::rtl::OUString& i_rComponentName, + const bool i_bForEditing + ); + + void impl_identifyComponent_throw(); + + private: + const ::comphelper::ComponentContext& m_rContext; + ::com::sun::star::uno::Reference< ::com::sun::star::sdb::application::XDatabaseDocumentUI > + m_xDocumentUI; + const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > + m_xComponent; + SubComponentType m_eType; + SubComponentDescriptor m_aCompDesc; + }; + + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // SUBCOMPONENTRECOVERY_HXX diff --git a/dbaccess/source/core/recovery/subcomponents.hxx b/dbaccess/source/core/recovery/subcomponents.hxx new file mode 100644 index 000000000000..d1ba0ccb1966 --- /dev/null +++ b/dbaccess/source/core/recovery/subcomponents.hxx @@ -0,0 +1,88 @@ +/************************************************************************* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +* +* Copyright 2009 by Sun Microsystems, Inc. +* +* OpenOffice.org - a multi-platform office productivity suite +* +* This file is part of OpenOffice.org. +* +* OpenOffice.org is free software: you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License version 3 +* only, as published by the Free Software Foundation. +* +* OpenOffice.org is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License version 3 for more details +* (a copy is included in the LICENSE file that accompanied this code). +* +* You should have received a copy of the GNU Lesser General Public License +* version 3 along with OpenOffice.org. If not, see +* <http://www.openoffice.org/license.html> +* for a copy of the LGPLv3 License. +************************************************************************/ + +#ifndef SUBCOMPONENTS_HXX +#define SUBCOMPONENTS_HXX + +#include "dbaccessdllapi.h" + +/** === begin UNO includes === **/ +#include <com/sun/star/sdb/application/DatabaseObject.hpp> +/** === end UNO includes === **/ + +#include <rtl/ustring.hxx> + +#include <hash_map> +#include <map> + +//........................................................................ +namespace dbaccess +{ +//........................................................................ + + namespace DatabaseObject = ::com::sun::star::sdb::application::DatabaseObject; + + // ------------------------------------------------------------------- + enum SubComponentType + { + TABLE = DatabaseObject::TABLE, + QUERY = DatabaseObject::QUERY, + FORM = DatabaseObject::FORM, + REPORT = DatabaseObject::REPORT, + + RELATION_DESIGN = 1000, + + UNKNOWN = 10001 + }; + + // ------------------------------------------------------------------- + struct DBACCESS_DLLPRIVATE SubComponentDescriptor + { + ::rtl::OUString sName; + bool bForEditing; + + SubComponentDescriptor() + :sName() + ,bForEditing( false ) + { + } + + SubComponentDescriptor( const ::rtl::OUString& i_rName, const bool i_bForEditing ) + :sName( i_rName ) + ,bForEditing( i_bForEditing ) + { + } + }; + + // ------------------------------------------------------------------- + typedef ::std::hash_map< ::rtl::OUString, SubComponentDescriptor, ::rtl::OUStringHash > MapStringToCompDesc; + typedef ::std::map< SubComponentType, MapStringToCompDesc > MapCompTypeToCompDescs; + + +//........................................................................ +} // namespace dbaccess +//........................................................................ + +#endif // SUBCOMPONENTS_HXX |