1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unostorageholder.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sot.hxx"
33 #include <com/sun/star/uno/Reference.hxx>
34 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/lang/XComponent.hpp>
37 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <comphelper/processfactory.hxx>
41 #include "unostorageholder.hxx"
42 #include <storinfo.hxx>
45 using namespace ::com::sun::star
;
47 UNOStorageHolder::UNOStorageHolder( SotStorage
& aParentStorage
,
49 uno::Reference
< embed::XStorage
> xStorage
,
50 ::utl::TempFile
* pTempFile
)
51 : m_pParentStorage( &aParentStorage
)
52 , m_rSotStorage( &aStorage
)
53 , m_xStorage( xStorage
)
54 , m_pTempFile( pTempFile
)
56 OSL_ENSURE( m_xStorage
.is() && m_pTempFile
, "Wrong initialization!\n" );
57 if ( !m_xStorage
.is() || !m_pTempFile
)
58 throw uno::RuntimeException();
60 uno::Reference
< embed::XTransactionBroadcaster
> xTrBroadcast( m_xStorage
, uno::UNO_QUERY
);
61 if ( !xTrBroadcast
.is() )
62 throw uno::RuntimeException();
64 xTrBroadcast
->addTransactionListener( (embed::XTransactionListener
*)this );
67 void UNOStorageHolder::InternalDispose()
69 uno::Reference
< embed::XTransactionBroadcaster
> xTrBroadcast( m_xStorage
, uno::UNO_QUERY
);
70 if ( xTrBroadcast
.is() )
71 xTrBroadcast
->removeTransactionListener( (embed::XTransactionListener
*)this );
73 uno::Reference
< lang::XComponent
> xComponent( m_xStorage
, uno::UNO_QUERY
);
74 if ( xComponent
.is() )
75 xComponent
->dispose();
76 m_xStorage
= uno::Reference
< embed::XStorage
>();
78 if ( m_pParentStorage
)
79 m_pParentStorage
= NULL
;
87 if ( m_rSotStorage
.Is() )
91 String
UNOStorageHolder::GetStorageName()
93 if ( m_rSotStorage
.Is() )
94 return m_rSotStorage
->GetName();
99 void SAL_CALL
UNOStorageHolder::preCommit( const lang::EventObject
& /*aEvent*/ )
100 throw ( uno::Exception
,
101 uno::RuntimeException
)
106 void SAL_CALL
UNOStorageHolder::commited( const lang::EventObject
& /*aEvent*/ )
107 throw ( uno::RuntimeException
)
109 ::utl::TempFile aTmpStorFile
;
110 if ( !aTmpStorFile
.GetURL().Len() )
111 throw uno::RuntimeException();
113 uno::Reference
< lang::XSingleServiceFactory
> xStorageFactory(
114 ::comphelper::getProcessServiceFactory()->createInstance(
115 ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
118 OSL_ENSURE( xStorageFactory
.is(), "Can't create storage factory!\n" );
119 if ( !xStorageFactory
.is() )
120 throw uno::RuntimeException();
122 uno::Sequence
< uno::Any
> aArg( 2 );
123 aArg
[0] <<= ::rtl::OUString( aTmpStorFile
.GetURL() );
124 aArg
[1] <<= embed::ElementModes::READWRITE
;
125 uno::Reference
< embed::XStorage
> xTempStorage( xStorageFactory
->createInstanceWithArguments( aArg
), uno::UNO_QUERY
);
127 OSL_ENSURE( xTempStorage
.is(), "Can't open storage!\n" );
128 if ( !xTempStorage
.is() )
129 throw uno::RuntimeException();
131 m_xStorage
->copyToStorage( xTempStorage
);
133 uno::Reference
< lang::XComponent
> xComp( xTempStorage
, uno::UNO_QUERY
);
135 throw uno::RuntimeException();
139 SotStorageRef rTempStorage
= new SotStorage( TRUE
, aTmpStorFile
.GetURL(), STREAM_WRITE
, STORAGE_TRANSACTED
);
140 if ( !rTempStorage
.Is() || rTempStorage
->GetError() != ERRCODE_NONE
)
141 throw uno::RuntimeException();
143 SvStorageInfoList aSubStorInfoList
;
144 m_rSotStorage
->FillInfoList( &aSubStorInfoList
);
145 for ( sal_uInt32 nInd
= 0; nInd
< aSubStorInfoList
.Count(); nInd
++ )
147 m_rSotStorage
->Remove( aSubStorInfoList
[nInd
].GetName() );
148 if ( m_rSotStorage
->GetError() )
150 m_rSotStorage
->ResetError();
151 throw uno::RuntimeException();
155 rTempStorage
->CopyTo( m_rSotStorage
);
157 // CopyTo does not transport unknown media type
158 // just workaround it
160 if ( rTempStorage
->GetProperty( ::rtl::OUString::createFromAscii( "MediaType" ), aMediaType
) )
161 m_rSotStorage
->SetProperty( ::rtl::OUString::createFromAscii( "MediaType" ), aMediaType
);
163 m_rSotStorage
->Commit();
166 void SAL_CALL
UNOStorageHolder::preRevert( const lang::EventObject
& /*aEvent*/ )
167 throw ( uno::Exception
,
168 uno::RuntimeException
)
173 void SAL_CALL
UNOStorageHolder::reverted( const lang::EventObject
& /*aEvent*/ )
174 throw ( uno::RuntimeException
)
176 // do nothing, since reverting of the duplicate storage just means
177 // not to copy changes done for it to the original storage
180 void SAL_CALL
UNOStorageHolder::disposing( const lang::EventObject
& /*Source*/ )
181 throw ( uno::RuntimeException
)
189 if ( m_rSotStorage
.Is() )
190 m_rSotStorage
= NULL
;
192 if ( m_pParentStorage
)
194 SotStorage
* pTmp
= m_pParentStorage
;
195 m_pParentStorage
= NULL
;
196 pTmp
->RemoveUNOStorageHolder( this ); // this statement can lead to destruction of the holder