1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "commandcontainer.hxx"
28cdf0e10cSrcweir #include "connection.hxx"
29cdf0e10cSrcweir #include "core_resource.hrc"
30cdf0e10cSrcweir #include "core_resource.hxx"
31cdf0e10cSrcweir #include "databasecontext.hxx"
32cdf0e10cSrcweir #include "databasedocument.hxx"
33cdf0e10cSrcweir #include "datasource.hxx"
34cdf0e10cSrcweir #include "dbastrings.hrc"
35cdf0e10cSrcweir #include "ModelImpl.hxx"
36cdf0e10cSrcweir #include "userinformation.hxx"
37cdf0e10cSrcweir #include "sdbcoretools.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /** === begin UNO includes === **/
40cdf0e10cSrcweir #include <com/sun/star/container/XSet.hpp>
41cdf0e10cSrcweir #include <com/sun/star/document/MacroExecMode.hpp>
42cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
43cdf0e10cSrcweir #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
44cdf0e10cSrcweir #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
45cdf0e10cSrcweir #include <com/sun/star/script/DocumentScriptLibraryContainer.hpp>
46cdf0e10cSrcweir #include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
47cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
48cdf0e10cSrcweir #include <com/sun/star/form/XLoadable.hpp>
49cdf0e10cSrcweir /** === end UNO includes === **/
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <comphelper/interaction.hxx>
52cdf0e10cSrcweir #include <comphelper/mediadescriptor.hxx>
53cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
54cdf0e10cSrcweir #include <comphelper/sequence.hxx>
55cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
56cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
57cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
58cdf0e10cSrcweir #include <rtl/digest.h>
59cdf0e10cSrcweir #include <sfx2/signaturestate.hxx>
60cdf0e10cSrcweir #include <tools/debug.hxx>
61cdf0e10cSrcweir #include <tools/diagnose_ex.h>
62cdf0e10cSrcweir #include <tools/errcode.hxx>
63cdf0e10cSrcweir #include <tools/urlobj.hxx>
64cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #include <algorithm>
67cdf0e10cSrcweir 
68cdf0e10cSrcweir using namespace ::com::sun::star::document;
69cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
70cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
71cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
72cdf0e10cSrcweir using namespace ::com::sun::star::beans;
73cdf0e10cSrcweir using namespace ::com::sun::star::uno;
74cdf0e10cSrcweir using namespace ::com::sun::star::lang;
75cdf0e10cSrcweir using namespace ::com::sun::star::embed;
76cdf0e10cSrcweir using namespace ::com::sun::star::container;
77cdf0e10cSrcweir using namespace ::com::sun::star::util;
78cdf0e10cSrcweir using namespace ::com::sun::star::io;
79cdf0e10cSrcweir using namespace ::com::sun::star::task;
80cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
81cdf0e10cSrcweir using namespace ::com::sun::star::frame;
82cdf0e10cSrcweir using namespace ::com::sun::star::view;
83cdf0e10cSrcweir using namespace ::com::sun::star::task;
84cdf0e10cSrcweir using namespace ::com::sun::star::reflection;
85cdf0e10cSrcweir using namespace ::com::sun::star::script;
86cdf0e10cSrcweir using namespace ::cppu;
87cdf0e10cSrcweir using namespace ::osl;
88cdf0e10cSrcweir using namespace ::vos;
89cdf0e10cSrcweir using namespace ::dbtools;
90cdf0e10cSrcweir using namespace ::comphelper;
91cdf0e10cSrcweir namespace css = ::com::sun::star;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //........................................................................
94cdf0e10cSrcweir namespace dbaccess
95cdf0e10cSrcweir {
96cdf0e10cSrcweir //........................................................................
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //============================================================
99cdf0e10cSrcweir //= VosMutexFacade
100cdf0e10cSrcweir //============================================================
101cdf0e10cSrcweir //------------------------------------------------------------------------
VosMutexFacade(::osl::Mutex & _rMutex)102cdf0e10cSrcweir VosMutexFacade::VosMutexFacade( ::osl::Mutex& _rMutex )
103cdf0e10cSrcweir     :m_rMutex( _rMutex )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir //------------------------------------------------------------------------
acquire()108cdf0e10cSrcweir void SAL_CALL VosMutexFacade::acquire()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir     m_rMutex.acquire();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //------------------------------------------------------------------------
tryToAcquire()114cdf0e10cSrcweir sal_Bool SAL_CALL VosMutexFacade::tryToAcquire()
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     return m_rMutex.tryToAcquire();
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir //------------------------------------------------------------------------
release()120cdf0e10cSrcweir void SAL_CALL VosMutexFacade::release()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     m_rMutex.release();
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir //============================================================
126cdf0e10cSrcweir //= DocumentStorageAccess
127cdf0e10cSrcweir //============================================================
128cdf0e10cSrcweir DBG_NAME( DocumentStorageAccess )
129cdf0e10cSrcweir class DocumentStorageAccess : public ::cppu::WeakImplHelper2<   XDocumentSubStorageSupplier
130cdf0e10cSrcweir                                                             ,   XTransactionListener >
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     typedef ::std::map< ::rtl::OUString, Reference< XStorage > >    NamedStorages;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     ::osl::Mutex        m_aMutex;
135cdf0e10cSrcweir     /// all sub storages which we ever gave to the outer world
136cdf0e10cSrcweir     NamedStorages       m_aExposedStorages;
137cdf0e10cSrcweir     ODatabaseModelImpl* m_pModelImplementation;
138cdf0e10cSrcweir     bool                m_bPropagateCommitToRoot;
139cdf0e10cSrcweir     bool                m_bDisposingSubStorages;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir public:
DocumentStorageAccess(ODatabaseModelImpl & _rModelImplementation)142cdf0e10cSrcweir     DocumentStorageAccess( ODatabaseModelImpl& _rModelImplementation )
143cdf0e10cSrcweir         :m_pModelImplementation( &_rModelImplementation )
144cdf0e10cSrcweir         ,m_bPropagateCommitToRoot( true )
145cdf0e10cSrcweir         ,m_bDisposingSubStorages( false )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         DBG_CTOR( DocumentStorageAccess, NULL );
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir protected:
~DocumentStorageAccess()151cdf0e10cSrcweir     ~DocumentStorageAccess()
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         DBG_DTOR( DocumentStorageAccess, NULL );
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir public:
157cdf0e10cSrcweir     void dispose();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     // XDocumentSubStorageSupplier
160cdf0e10cSrcweir     virtual Reference< XStorage > SAL_CALL getDocumentSubStorage( const ::rtl::OUString& aStorageName, ::sal_Int32 _nMode ) throw (RuntimeException);
161cdf0e10cSrcweir     virtual Sequence< ::rtl::OUString > SAL_CALL getDocumentSubStoragesNames(  ) throw (IOException, RuntimeException);
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // XTransactionListener
164cdf0e10cSrcweir     virtual void SAL_CALL preCommit( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
165cdf0e10cSrcweir     virtual void SAL_CALL commited( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException);
166cdf0e10cSrcweir     virtual void SAL_CALL preRevert( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
167cdf0e10cSrcweir     virtual void SAL_CALL reverted( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException);
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // XEventListener
170cdf0e10cSrcweir     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     /// disposes all storages managed by this instance
173cdf0e10cSrcweir     void disposeStorages();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     /// disposes all known sub storages
176cdf0e10cSrcweir     void commitStorages() SAL_THROW(( IOException, RuntimeException ));
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     /// commits the dedicated "database" storage
179cdf0e10cSrcweir     bool commitEmbeddedStorage( bool _bPreventRootCommits );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir private:
182cdf0e10cSrcweir     /** opens the sub storage with the given name, in the given mode
183cdf0e10cSrcweir     */
184cdf0e10cSrcweir     Reference< XStorage > impl_openSubStorage_nothrow( const ::rtl::OUString& _rStorageName, sal_Int32 _nMode );
185cdf0e10cSrcweir 
impl_suspendCommitPropagation()186cdf0e10cSrcweir     void impl_suspendCommitPropagation()
187cdf0e10cSrcweir     {
188cdf0e10cSrcweir         OSL_ENSURE( m_bPropagateCommitToRoot, "DocumentStorageAccess::impl_suspendCommitPropagation: already suspended" );
189cdf0e10cSrcweir         m_bPropagateCommitToRoot = false;
190cdf0e10cSrcweir     }
impl_resumeCommitPropagation()191cdf0e10cSrcweir     void impl_resumeCommitPropagation()
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         OSL_ENSURE( !m_bPropagateCommitToRoot, "DocumentStorageAccess::impl_resumeCommitPropagation: not suspended" );
194cdf0e10cSrcweir         m_bPropagateCommitToRoot = true;
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir };
198cdf0e10cSrcweir 
199cdf0e10cSrcweir //--------------------------------------------------------------------------
dispose()200cdf0e10cSrcweir void DocumentStorageAccess::dispose()
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     for (   NamedStorages::iterator loop = m_aExposedStorages.begin();
205cdf0e10cSrcweir             loop != m_aExposedStorages.end();
206cdf0e10cSrcweir             ++loop
207cdf0e10cSrcweir         )
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         try
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             Reference< XTransactionBroadcaster > xBroadcaster( loop->second, UNO_QUERY );
212cdf0e10cSrcweir 	        if ( xBroadcaster.is() )
213cdf0e10cSrcweir 		        xBroadcaster->removeTransactionListener( this );
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir         catch( const Exception& )
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     m_aExposedStorages.clear();
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     m_pModelImplementation = NULL;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //--------------------------------------------------------------------------
impl_openSubStorage_nothrow(const::rtl::OUString & _rStorageName,sal_Int32 _nDesiredMode)227cdf0e10cSrcweir Reference< XStorage > DocumentStorageAccess::impl_openSubStorage_nothrow( const ::rtl::OUString& _rStorageName, sal_Int32 _nDesiredMode )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	OSL_ENSURE( _rStorageName.getLength(),"ODatabaseModelImpl::impl_openSubStorage_nothrow: Invalid storage name!" );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     Reference< XStorage > xStorage;
232cdf0e10cSrcweir     try
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 	    Reference< XStorage > xRootStorage( m_pModelImplementation->getOrCreateRootStorage() );
235cdf0e10cSrcweir         if ( xRootStorage.is() )
236cdf0e10cSrcweir 	    {
237cdf0e10cSrcweir             sal_Int32 nRealMode = m_pModelImplementation->m_bDocumentReadOnly ? ElementModes::READ : _nDesiredMode;
238cdf0e10cSrcweir             if ( nRealMode == ElementModes::READ )
239cdf0e10cSrcweir             {
240cdf0e10cSrcweir                 Reference< XNameAccess > xSubStorageNames( xRootStorage, UNO_QUERY );
241cdf0e10cSrcweir                 if ( xSubStorageNames.is() && !xSubStorageNames->hasByName( _rStorageName ) )
242cdf0e10cSrcweir                     return xStorage;
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 			xStorage = xRootStorage->openStorageElement( _rStorageName, nRealMode );
246cdf0e10cSrcweir 
247cdf0e10cSrcweir             Reference< XTransactionBroadcaster > xBroad( xStorage, UNO_QUERY );
248cdf0e10cSrcweir             if ( xBroad.is() )
249cdf0e10cSrcweir                 xBroad->addTransactionListener( this );
250cdf0e10cSrcweir 		}
251cdf0e10cSrcweir 	}
252cdf0e10cSrcweir 	catch( const Exception& )
253cdf0e10cSrcweir 	{
254cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	return xStorage;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir //--------------------------------------------------------------------------
disposeStorages()261cdf0e10cSrcweir void DocumentStorageAccess::disposeStorages()
262cdf0e10cSrcweir {
263cdf0e10cSrcweir     m_bDisposingSubStorages = true;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	NamedStorages::iterator aEnd = m_aExposedStorages.end();
266cdf0e10cSrcweir 	for (   NamedStorages::iterator aIter = m_aExposedStorages.begin();
267cdf0e10cSrcweir             aIter != aEnd ;
268cdf0e10cSrcweir             ++aIter
269cdf0e10cSrcweir         )
270cdf0e10cSrcweir 	{
271cdf0e10cSrcweir         try
272cdf0e10cSrcweir         {
273cdf0e10cSrcweir 		    ::comphelper::disposeComponent( aIter->second );
274cdf0e10cSrcweir         }
275cdf0e10cSrcweir         catch( const Exception& )
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir 	}
280cdf0e10cSrcweir 	m_aExposedStorages.clear();
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     m_bDisposingSubStorages = false;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir //--------------------------------------------------------------------------
commitStorages()286cdf0e10cSrcweir void DocumentStorageAccess::commitStorages() SAL_THROW(( IOException, RuntimeException ))
287cdf0e10cSrcweir {
288cdf0e10cSrcweir     try
289cdf0e10cSrcweir     {
290cdf0e10cSrcweir         for (   NamedStorages::const_iterator aIter = m_aExposedStorages.begin();
291cdf0e10cSrcweir                 aIter != m_aExposedStorages.end();
292cdf0e10cSrcweir                 ++aIter
293cdf0e10cSrcweir             )
294cdf0e10cSrcweir         {
295cdf0e10cSrcweir             tools::stor::commitStorageIfWriteable( aIter->second );
296cdf0e10cSrcweir         }
297cdf0e10cSrcweir     }
298cdf0e10cSrcweir     catch(const WrappedTargetException&)
299cdf0e10cSrcweir     {
300cdf0e10cSrcweir         // WrappedTargetException not allowed to leave
301cdf0e10cSrcweir         throw IOException();
302cdf0e10cSrcweir     }
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir //--------------------------------------------------------------------------
commitEmbeddedStorage(bool _bPreventRootCommits)306cdf0e10cSrcweir bool DocumentStorageAccess::commitEmbeddedStorage( bool _bPreventRootCommits )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir 	if ( _bPreventRootCommits )
309cdf0e10cSrcweir         impl_suspendCommitPropagation();
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     bool bSuccess = false;
312cdf0e10cSrcweir 	try
313cdf0e10cSrcweir 	{
314cdf0e10cSrcweir 		NamedStorages::const_iterator pos = m_aExposedStorages.find( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "database" ) ) );
315cdf0e10cSrcweir 		if ( pos != m_aExposedStorages.end() )
316cdf0e10cSrcweir 			bSuccess = tools::stor::commitStorageIfWriteable( pos->second );
317cdf0e10cSrcweir 	}
318cdf0e10cSrcweir 	catch( Exception& )
319cdf0e10cSrcweir 	{
320cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
321cdf0e10cSrcweir 	}
322cdf0e10cSrcweir 
323cdf0e10cSrcweir     if ( _bPreventRootCommits )
324cdf0e10cSrcweir         impl_resumeCommitPropagation();
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     return bSuccess;
327cdf0e10cSrcweir 
328cdf0e10cSrcweir }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir //--------------------------------------------------------------------------
getDocumentSubStorage(const::rtl::OUString & aStorageName,::sal_Int32 _nDesiredMode)331cdf0e10cSrcweir Reference< XStorage > SAL_CALL DocumentStorageAccess::getDocumentSubStorage( const ::rtl::OUString& aStorageName, ::sal_Int32 _nDesiredMode ) throw (RuntimeException)
332cdf0e10cSrcweir {
333cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
334cdf0e10cSrcweir     NamedStorages::iterator pos = m_aExposedStorages.find( aStorageName );
335cdf0e10cSrcweir     if ( pos == m_aExposedStorages.end() )
336cdf0e10cSrcweir     {
337cdf0e10cSrcweir 	    Reference< XStorage > xResult = impl_openSubStorage_nothrow( aStorageName, _nDesiredMode );
338cdf0e10cSrcweir         pos = m_aExposedStorages.insert( NamedStorages::value_type( aStorageName, xResult ) ).first;
339cdf0e10cSrcweir     }
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     return pos->second;
342cdf0e10cSrcweir }
343cdf0e10cSrcweir 
344cdf0e10cSrcweir //--------------------------------------------------------------------------
getDocumentSubStoragesNames()345cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL DocumentStorageAccess::getDocumentSubStoragesNames(  ) throw (IOException, RuntimeException)
346cdf0e10cSrcweir {
347cdf0e10cSrcweir     Reference< XStorage > xRootStor( m_pModelImplementation->getRootStorage() );
348cdf0e10cSrcweir     if ( !xRootStor.is() )
349cdf0e10cSrcweir         return Sequence< ::rtl::OUString >();
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aNames;
352cdf0e10cSrcweir 
353cdf0e10cSrcweir     Reference< XNameAccess > xNames( xRootStor, UNO_QUERY_THROW );
354cdf0e10cSrcweir     Sequence< ::rtl::OUString > aElementNames( xNames->getElementNames() );
355cdf0e10cSrcweir     for ( sal_Int32 i=0; i<aElementNames.getLength(); ++i )
356cdf0e10cSrcweir     {
357cdf0e10cSrcweir         if ( xRootStor->isStorageElement( aElementNames[i] ) )
358cdf0e10cSrcweir             aNames.push_back( aElementNames[i] );
359cdf0e10cSrcweir     }
360cdf0e10cSrcweir     return aNames.empty()
361cdf0e10cSrcweir         ?  Sequence< ::rtl::OUString >()
362cdf0e10cSrcweir         :  Sequence< ::rtl::OUString >( &aNames[0], aNames.size() );
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir //--------------------------------------------------------------------------
preCommit(const css::lang::EventObject &)366cdf0e10cSrcweir void SAL_CALL DocumentStorageAccess::preCommit( const css::lang::EventObject& /*aEvent*/ ) throw (Exception, RuntimeException)
367cdf0e10cSrcweir {
368cdf0e10cSrcweir 	// not interested in
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir //--------------------------------------------------------------------------
commited(const css::lang::EventObject & aEvent)372cdf0e10cSrcweir void SAL_CALL DocumentStorageAccess::commited( const css::lang::EventObject& aEvent ) throw (RuntimeException)
373cdf0e10cSrcweir {
374cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     if ( m_pModelImplementation )
377cdf0e10cSrcweir         m_pModelImplementation->setModified( sal_True );
378cdf0e10cSrcweir 
379cdf0e10cSrcweir     if ( m_pModelImplementation && m_bPropagateCommitToRoot )
380cdf0e10cSrcweir     {
381cdf0e10cSrcweir         Reference< XStorage > xStorage( aEvent.Source, UNO_QUERY );
382cdf0e10cSrcweir 
383cdf0e10cSrcweir         // check if this is the dedicated "database" sub storage
384cdf0e10cSrcweir         NamedStorages::const_iterator pos = m_aExposedStorages.find( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "database" ) ) );
385cdf0e10cSrcweir 	    if  (   ( pos != m_aExposedStorages.end() )
386cdf0e10cSrcweir             &&  ( pos->second == xStorage )
387cdf0e10cSrcweir             )
388cdf0e10cSrcweir 	    {
389cdf0e10cSrcweir             // if so, also commit the root storage
390cdf0e10cSrcweir             m_pModelImplementation->commitRootStorage();
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir }
394cdf0e10cSrcweir 
395cdf0e10cSrcweir //--------------------------------------------------------------------------
preRevert(const css::lang::EventObject &)396cdf0e10cSrcweir void SAL_CALL DocumentStorageAccess::preRevert( const css::lang::EventObject& /*aEvent*/ ) throw (Exception, RuntimeException)
397cdf0e10cSrcweir {
398cdf0e10cSrcweir 	// not interested in
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir //--------------------------------------------------------------------------
reverted(const css::lang::EventObject &)402cdf0e10cSrcweir void SAL_CALL DocumentStorageAccess::reverted( const css::lang::EventObject& /*aEvent*/ ) throw (RuntimeException)
403cdf0e10cSrcweir {
404cdf0e10cSrcweir 	// not interested in
405cdf0e10cSrcweir }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir //--------------------------------------------------------------------------
disposing(const css::lang::EventObject & Source)408cdf0e10cSrcweir void SAL_CALL DocumentStorageAccess::disposing( const css::lang::EventObject& Source ) throw ( RuntimeException )
409cdf0e10cSrcweir {
410cdf0e10cSrcweir     OSL_ENSURE( Reference< XStorage >( Source.Source, UNO_QUERY ).is(), "DocumentStorageAccess::disposing: No storage? What's this?" );
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     if ( m_bDisposingSubStorages )
413cdf0e10cSrcweir         return;
414cdf0e10cSrcweir 
415cdf0e10cSrcweir     for (   NamedStorages::iterator find = m_aExposedStorages.begin();
416cdf0e10cSrcweir             find != m_aExposedStorages.end();
417cdf0e10cSrcweir             ++find
418cdf0e10cSrcweir         )
419cdf0e10cSrcweir         if ( find->second == Source.Source )
420cdf0e10cSrcweir         {
421cdf0e10cSrcweir             m_aExposedStorages.erase( find );
422cdf0e10cSrcweir             break;
423cdf0e10cSrcweir         }
424cdf0e10cSrcweir }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir //============================================================
427cdf0e10cSrcweir //= ODatabaseModelImpl
428cdf0e10cSrcweir //============================================================
DBG_NAME(ODatabaseModelImpl)429cdf0e10cSrcweir DBG_NAME(ODatabaseModelImpl)
430cdf0e10cSrcweir //--------------------------------------------------------------------------
431cdf0e10cSrcweir ODatabaseModelImpl::ODatabaseModelImpl( const Reference< XMultiServiceFactory >& _rxFactory, ODatabaseContext& _rDBContext )
432cdf0e10cSrcweir             :m_xModel()
433cdf0e10cSrcweir             ,m_xDataSource()
434cdf0e10cSrcweir             ,m_pStorageAccess( NULL )
435cdf0e10cSrcweir             ,m_aMutex()
436cdf0e10cSrcweir             ,m_aMutexFacade( m_aMutex )
437cdf0e10cSrcweir             ,m_aContainer(4)
438cdf0e10cSrcweir             ,m_aMacroMode( *this )
439cdf0e10cSrcweir 			,m_nImposedMacroExecMode( MacroExecMode::NEVER_EXECUTE )
440cdf0e10cSrcweir 			,m_pDBContext( &_rDBContext )
441cdf0e10cSrcweir             ,m_refCount(0)
442cdf0e10cSrcweir             ,m_aEmbeddedMacros()
443cdf0e10cSrcweir             ,m_bModificationLock( false )
444cdf0e10cSrcweir             ,m_bDocumentInitialized( false )
445cdf0e10cSrcweir 			,m_aContext( _rxFactory )
446cdf0e10cSrcweir 			,m_nLoginTimeout(0)
447cdf0e10cSrcweir             ,m_bReadOnly(sal_False)
448cdf0e10cSrcweir 			,m_bPasswordRequired(sal_False)
449cdf0e10cSrcweir 			,m_bSuppressVersionColumns(sal_True)
450cdf0e10cSrcweir 			,m_bModified(sal_False)
451cdf0e10cSrcweir 			,m_bDocumentReadOnly(sal_False)
452cdf0e10cSrcweir 			,m_pSharedConnectionManager(NULL)
453cdf0e10cSrcweir 			,m_nControllerLockCount(0)
454cdf0e10cSrcweir {
455cdf0e10cSrcweir 	// some kind of default
456cdf0e10cSrcweir 	DBG_CTOR(ODatabaseModelImpl,NULL);
457cdf0e10cSrcweir 	m_sConnectURL = ::rtl::OUString::createFromAscii("jdbc:");
458cdf0e10cSrcweir 	m_aTableFilter.realloc(1);
459cdf0e10cSrcweir 	m_aTableFilter[0] = ::rtl::OUString::createFromAscii("%");
460cdf0e10cSrcweir 	impl_construct_nothrow();
461cdf0e10cSrcweir }
462cdf0e10cSrcweir 
463cdf0e10cSrcweir //--------------------------------------------------------------------------
ODatabaseModelImpl(const::rtl::OUString & _rRegistrationName,const Reference<XMultiServiceFactory> & _rxFactory,ODatabaseContext & _rDBContext)464cdf0e10cSrcweir ODatabaseModelImpl::ODatabaseModelImpl(
465cdf0e10cSrcweir 					const ::rtl::OUString& _rRegistrationName,
466cdf0e10cSrcweir 					const Reference< XMultiServiceFactory >& _rxFactory,
467cdf0e10cSrcweir 					ODatabaseContext& _rDBContext
468cdf0e10cSrcweir 					)
469cdf0e10cSrcweir             :m_xModel()
470cdf0e10cSrcweir             ,m_xDataSource()
471cdf0e10cSrcweir             ,m_pStorageAccess( NULL )
472cdf0e10cSrcweir             ,m_aMutex()
473cdf0e10cSrcweir             ,m_aMutexFacade( m_aMutex )
474cdf0e10cSrcweir             ,m_aContainer(4)
475cdf0e10cSrcweir             ,m_aMacroMode( *this )
476cdf0e10cSrcweir 			,m_nImposedMacroExecMode( MacroExecMode::NEVER_EXECUTE )
477cdf0e10cSrcweir 			,m_pDBContext( &_rDBContext )
478cdf0e10cSrcweir             ,m_refCount(0)
479cdf0e10cSrcweir             ,m_aEmbeddedMacros()
480cdf0e10cSrcweir             ,m_bModificationLock( false )
481cdf0e10cSrcweir             ,m_bDocumentInitialized( false )
482cdf0e10cSrcweir 			,m_aContext( _rxFactory )
483cdf0e10cSrcweir             ,m_sName(_rRegistrationName)
484cdf0e10cSrcweir 			,m_nLoginTimeout(0)
485cdf0e10cSrcweir 			,m_bReadOnly(sal_False)
486cdf0e10cSrcweir 			,m_bPasswordRequired(sal_False)
487cdf0e10cSrcweir 			,m_bSuppressVersionColumns(sal_True)
488cdf0e10cSrcweir 			,m_bModified(sal_False)
489cdf0e10cSrcweir 			,m_bDocumentReadOnly(sal_False)
490cdf0e10cSrcweir 			,m_pSharedConnectionManager(NULL)
491cdf0e10cSrcweir 			,m_nControllerLockCount(0)
492cdf0e10cSrcweir {
493cdf0e10cSrcweir 	DBG_CTOR(ODatabaseModelImpl,NULL);
494cdf0e10cSrcweir 	impl_construct_nothrow();
495cdf0e10cSrcweir }
496cdf0e10cSrcweir 
497cdf0e10cSrcweir //--------------------------------------------------------------------------
~ODatabaseModelImpl()498cdf0e10cSrcweir ODatabaseModelImpl::~ODatabaseModelImpl()
499cdf0e10cSrcweir {
500cdf0e10cSrcweir 	DBG_DTOR(ODatabaseModelImpl,NULL);
501cdf0e10cSrcweir }
502cdf0e10cSrcweir 
503cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_construct_nothrow()504cdf0e10cSrcweir void ODatabaseModelImpl::impl_construct_nothrow()
505cdf0e10cSrcweir {
506cdf0e10cSrcweir     // create the property bag to hold the settings (also known as "Info" property)
507cdf0e10cSrcweir     try
508cdf0e10cSrcweir     {
509cdf0e10cSrcweir         // the set of property value types in the bag is limited:
510cdf0e10cSrcweir         Sequence< Type > aAllowedTypes(6);
511cdf0e10cSrcweir         Type* pAllowedType = aAllowedTypes.getArray();
512cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< sal_Bool* >( NULL ) );
513cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< double* >( NULL ) );
514cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
515cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< sal_Int32* >( NULL ) );
516cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< sal_Int16* >( NULL ) );
517cdf0e10cSrcweir         *pAllowedType++ = ::getCppuType( static_cast< Sequence< Any >* >( NULL ) );
518cdf0e10cSrcweir 
519cdf0e10cSrcweir         Sequence< Any > aInitArgs( 2 );
520cdf0e10cSrcweir         aInitArgs[0] <<= NamedValue(
521cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AutomaticAddition" ) ),
522cdf0e10cSrcweir             makeAny( (sal_Bool)sal_True )
523cdf0e10cSrcweir         );
524cdf0e10cSrcweir         aInitArgs[1] <<= NamedValue(
525cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AllowedTypes" ) ),
526cdf0e10cSrcweir             makeAny( aAllowedTypes )
527cdf0e10cSrcweir         );
528cdf0e10cSrcweir 
529cdf0e10cSrcweir         m_xSettings.set( m_aContext.createComponentWithArguments( "com.sun.star.beans.PropertyBag", aInitArgs ), UNO_QUERY_THROW );
530cdf0e10cSrcweir 
531cdf0e10cSrcweir         // insert the default settings
532cdf0e10cSrcweir         Reference< XPropertyContainer > xContainer( m_xSettings, UNO_QUERY_THROW );
533cdf0e10cSrcweir         Reference< XSet > xSettingsSet( m_xSettings, UNO_QUERY_THROW );
534cdf0e10cSrcweir         const AsciiPropertyValue* pSettings = getDefaultDataSourceSettings();
535cdf0e10cSrcweir         for ( ; pSettings->AsciiName; ++pSettings )
536cdf0e10cSrcweir         {
537cdf0e10cSrcweir             if ( !pSettings->DefaultValue.hasValue() )
538cdf0e10cSrcweir             {
539cdf0e10cSrcweir                 Property aProperty(
540cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii( pSettings->AsciiName ),
541cdf0e10cSrcweir                     -1,
542cdf0e10cSrcweir                     pSettings->ValueType,
543cdf0e10cSrcweir                     PropertyAttribute::BOUND | PropertyAttribute::MAYBEDEFAULT | PropertyAttribute::MAYBEVOID
544cdf0e10cSrcweir                 );
545cdf0e10cSrcweir                 xSettingsSet->insert( makeAny( aProperty ) );
546cdf0e10cSrcweir             }
547cdf0e10cSrcweir             else
548cdf0e10cSrcweir             {
549cdf0e10cSrcweir                 xContainer->addProperty(
550cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii( pSettings->AsciiName ),
551cdf0e10cSrcweir                     PropertyAttribute::BOUND | PropertyAttribute::MAYBEDEFAULT,
552cdf0e10cSrcweir                     pSettings->DefaultValue
553cdf0e10cSrcweir                 );
554cdf0e10cSrcweir             }
555cdf0e10cSrcweir         }
556cdf0e10cSrcweir     }
557cdf0e10cSrcweir     catch( const Exception& )
558cdf0e10cSrcweir     {
559cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir     m_pDBContext->appendAtTerminateListener(*this);
562cdf0e10cSrcweir }
563cdf0e10cSrcweir 
564cdf0e10cSrcweir // -----------------------------------------------------------------------------
565cdf0e10cSrcweir namespace
566cdf0e10cSrcweir {
567cdf0e10cSrcweir     // .........................................................................
lcl_getContainerStorageName_throw(ODatabaseModelImpl::ObjectType _eType)568cdf0e10cSrcweir     ::rtl::OUString lcl_getContainerStorageName_throw( ODatabaseModelImpl::ObjectType _eType )
569cdf0e10cSrcweir     {
570cdf0e10cSrcweir         const sal_Char* pAsciiName( NULL );
571cdf0e10cSrcweir         switch ( _eType )
572cdf0e10cSrcweir         {
573cdf0e10cSrcweir         case ODatabaseModelImpl::E_FORM:   pAsciiName = "forms"; break;
574cdf0e10cSrcweir         case ODatabaseModelImpl::E_REPORT: pAsciiName = "reports"; break;
575cdf0e10cSrcweir         case ODatabaseModelImpl::E_QUERY:  pAsciiName = "queries"; break;
576cdf0e10cSrcweir         case ODatabaseModelImpl::E_TABLE:  pAsciiName = "tables"; break;
577cdf0e10cSrcweir         default:
578cdf0e10cSrcweir             throw RuntimeException();
579cdf0e10cSrcweir         }
580cdf0e10cSrcweir         return ::rtl::OUString::createFromAscii( pAsciiName );
581cdf0e10cSrcweir     }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir     // .........................................................................
lcl_hasObjectWithMacros_throw(const ODefinitionContainer_Impl & _rObjectDefinitions,const Reference<XStorage> & _rxContainerStorage)584cdf0e10cSrcweir     bool lcl_hasObjectWithMacros_throw( const ODefinitionContainer_Impl& _rObjectDefinitions, const Reference< XStorage >& _rxContainerStorage )
585cdf0e10cSrcweir     {
586cdf0e10cSrcweir         bool bSomeDocHasMacros = false;
587cdf0e10cSrcweir 
588cdf0e10cSrcweir         for (   ODefinitionContainer_Impl::const_iterator object = _rObjectDefinitions.begin();
589cdf0e10cSrcweir                 ( object != _rObjectDefinitions.end() ) && !bSomeDocHasMacros;
590cdf0e10cSrcweir                 ++object
591cdf0e10cSrcweir             )
592cdf0e10cSrcweir         {
593cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
594cdf0e10cSrcweir             const ::rtl::OUString& rName( object->first ); (void)rName;
595cdf0e10cSrcweir #endif
596cdf0e10cSrcweir 
597cdf0e10cSrcweir             const TContentPtr& rDefinition( object->second );
598cdf0e10cSrcweir             const ::rtl::OUString& rPersistentName( rDefinition->m_aProps.sPersistentName );
599cdf0e10cSrcweir 
600cdf0e10cSrcweir             if ( !rPersistentName.getLength() )
601cdf0e10cSrcweir             {   // it's a logical sub folder used to organize the real objects
602cdf0e10cSrcweir                 const ODefinitionContainer_Impl& rSubFoldersObjectDefinitions( dynamic_cast< const ODefinitionContainer_Impl& >( *rDefinition.get() ) );
603cdf0e10cSrcweir                 bSomeDocHasMacros = lcl_hasObjectWithMacros_throw( rSubFoldersObjectDefinitions, _rxContainerStorage );
604cdf0e10cSrcweir                 continue;
605cdf0e10cSrcweir             }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir             bSomeDocHasMacros = ODatabaseModelImpl::objectHasMacros( _rxContainerStorage, rPersistentName );
608cdf0e10cSrcweir         }
609cdf0e10cSrcweir         return bSomeDocHasMacros;
610cdf0e10cSrcweir     }
611cdf0e10cSrcweir 
612cdf0e10cSrcweir     // .........................................................................
lcl_hasObjectsWithMacros_nothrow(ODatabaseModelImpl & _rModel,const ODatabaseModelImpl::ObjectType _eType)613cdf0e10cSrcweir     bool lcl_hasObjectsWithMacros_nothrow( ODatabaseModelImpl& _rModel, const ODatabaseModelImpl::ObjectType _eType )
614cdf0e10cSrcweir     {
615cdf0e10cSrcweir         bool bSomeDocHasMacros = false;
616cdf0e10cSrcweir 
617cdf0e10cSrcweir         const OContentHelper_Impl& rContainerData( *_rModel.getObjectContainer( _eType ).get() );
618cdf0e10cSrcweir         const ODefinitionContainer_Impl& rObjectDefinitions = dynamic_cast< const ODefinitionContainer_Impl& >( rContainerData );
619cdf0e10cSrcweir 
620cdf0e10cSrcweir         try
621cdf0e10cSrcweir         {
622cdf0e10cSrcweir             Reference< XStorage > xContainerStorage( _rModel.getStorage( _eType, ElementModes::READWRITE ) );
623cdf0e10cSrcweir             // note the READWRITE here: If the storage already existed before, then the OpenMode will
624cdf0e10cSrcweir             // be ignored, anyway.
625cdf0e10cSrcweir             // If the storage did not yet exist, then it will be created. If the database document
626cdf0e10cSrcweir             // is read-only, the OpenMode will be automatically downgraded to READ. Otherwise,
627cdf0e10cSrcweir             // the storage will in fact be created as READWRITE. While this is not strictly necessary
628cdf0e10cSrcweir             // for this particular use case here, it is required since the storage is *cached*, and
629cdf0e10cSrcweir             // later use cases will need the READWRITE mode.
630cdf0e10cSrcweir 
631cdf0e10cSrcweir             if ( xContainerStorage.is() )
632cdf0e10cSrcweir                 bSomeDocHasMacros = lcl_hasObjectWithMacros_throw( rObjectDefinitions, xContainerStorage );
633cdf0e10cSrcweir         }
634cdf0e10cSrcweir         catch( const Exception& )
635cdf0e10cSrcweir         {
636cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
637cdf0e10cSrcweir             // be on the safe side: If we can't reliably determine whether there are macros,
638cdf0e10cSrcweir             // assume there actually are. Better this way, than the other way round.
639cdf0e10cSrcweir             bSomeDocHasMacros = true;
640cdf0e10cSrcweir         }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir         return bSomeDocHasMacros;
643cdf0e10cSrcweir     }
644cdf0e10cSrcweir }
645cdf0e10cSrcweir 
646cdf0e10cSrcweir // -----------------------------------------------------------------------------
objectHasMacros(const Reference<XStorage> & _rxContainerStorage,const::rtl::OUString & _rPersistentName)647cdf0e10cSrcweir bool ODatabaseModelImpl::objectHasMacros( const Reference< XStorage >& _rxContainerStorage, const ::rtl::OUString& _rPersistentName )
648cdf0e10cSrcweir {
649cdf0e10cSrcweir     OSL_PRECOND( _rxContainerStorage.is(), "ODatabaseModelImpl::objectHasMacros: this will crash!" );
650cdf0e10cSrcweir 
651cdf0e10cSrcweir     bool bHasMacros = true;
652cdf0e10cSrcweir     try
653cdf0e10cSrcweir     {
654cdf0e10cSrcweir         if ( !_rxContainerStorage->hasByName( _rPersistentName ) )
655cdf0e10cSrcweir             return false;
656cdf0e10cSrcweir 
657cdf0e10cSrcweir         Reference< XStorage > xObjectStor( _rxContainerStorage->openStorageElement(
658cdf0e10cSrcweir             _rPersistentName, ElementModes::READ ) );
659cdf0e10cSrcweir 
660cdf0e10cSrcweir         bHasMacros = ::sfx2::DocumentMacroMode::storageHasMacros( xObjectStor );
661cdf0e10cSrcweir     }
662cdf0e10cSrcweir     catch( const Exception& )
663cdf0e10cSrcweir     {
664cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
665cdf0e10cSrcweir     }
666cdf0e10cSrcweir     return bHasMacros;
667cdf0e10cSrcweir }
668cdf0e10cSrcweir 
669cdf0e10cSrcweir // -----------------------------------------------------------------------------
reset()670cdf0e10cSrcweir void ODatabaseModelImpl::reset()
671cdf0e10cSrcweir {
672cdf0e10cSrcweir 	m_bReadOnly = sal_False;
673cdf0e10cSrcweir     ::std::vector< TContentPtr > aEmptyContainers( 4 );
674cdf0e10cSrcweir     m_aContainer.swap( aEmptyContainers );
675cdf0e10cSrcweir 
676cdf0e10cSrcweir     if ( m_pStorageAccess )
677cdf0e10cSrcweir     {
678cdf0e10cSrcweir         m_pStorageAccess->dispose();
679cdf0e10cSrcweir         m_pStorageAccess->release();
680cdf0e10cSrcweir         m_pStorageAccess = NULL;
681cdf0e10cSrcweir     }
682cdf0e10cSrcweir }
683cdf0e10cSrcweir // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject & Source)684cdf0e10cSrcweir void SAL_CALL ODatabaseModelImpl::disposing( const ::com::sun::star::lang::EventObject& Source ) throw(RuntimeException)
685cdf0e10cSrcweir {
686cdf0e10cSrcweir 	Reference<XConnection> xCon(Source.Source,UNO_QUERY);
687cdf0e10cSrcweir 	if ( xCon.is() )
688cdf0e10cSrcweir 	{
689cdf0e10cSrcweir 		bool bStore = false;
690cdf0e10cSrcweir 		OWeakConnectionArray::iterator aEnd = m_aConnections.end();
691cdf0e10cSrcweir 		for (OWeakConnectionArray::iterator i = m_aConnections.begin(); aEnd != i; ++i)
692cdf0e10cSrcweir 		{
693cdf0e10cSrcweir 			if ( xCon == i->get() )
694cdf0e10cSrcweir 			{
695cdf0e10cSrcweir 				*i = OWeakConnection();
696cdf0e10cSrcweir                 bStore = true;
697cdf0e10cSrcweir                 break;
698cdf0e10cSrcweir 			}
699cdf0e10cSrcweir 		}
700cdf0e10cSrcweir 
701cdf0e10cSrcweir 		if ( bStore )
702cdf0e10cSrcweir             commitRootStorage();
703cdf0e10cSrcweir 	}
704cdf0e10cSrcweir 	else
705cdf0e10cSrcweir 	{
706cdf0e10cSrcweir         OSL_ENSURE( false, "ODatabaseModelImpl::disposing: where does this come from?" );
707cdf0e10cSrcweir 	}
708cdf0e10cSrcweir }
709cdf0e10cSrcweir //------------------------------------------------------------------------------
clearConnections()710cdf0e10cSrcweir void ODatabaseModelImpl::clearConnections()
711cdf0e10cSrcweir {
712cdf0e10cSrcweir     OWeakConnectionArray aConnections;
713cdf0e10cSrcweir     aConnections.swap( m_aConnections );
714cdf0e10cSrcweir 
715cdf0e10cSrcweir 	Reference< XConnection > xConn;
716cdf0e10cSrcweir     OWeakConnectionArray::iterator aEnd = aConnections.end();
717cdf0e10cSrcweir 	for ( OWeakConnectionArray::iterator i = aConnections.begin(); aEnd != i; ++i )
718cdf0e10cSrcweir 	{
719cdf0e10cSrcweir 		xConn = *i;
720cdf0e10cSrcweir 		if ( xConn.is() )
721cdf0e10cSrcweir 		{
722cdf0e10cSrcweir 			try
723cdf0e10cSrcweir 			{
724cdf0e10cSrcweir 				xConn->close();
725cdf0e10cSrcweir 			}
726cdf0e10cSrcweir 			catch(const Exception&)
727cdf0e10cSrcweir 			{
728cdf0e10cSrcweir 				DBG_UNHANDLED_EXCEPTION();
729cdf0e10cSrcweir 			}
730cdf0e10cSrcweir 		}
731cdf0e10cSrcweir 	}
732cdf0e10cSrcweir 
733cdf0e10cSrcweir     m_pSharedConnectionManager = NULL;
734cdf0e10cSrcweir 	m_xSharedConnectionManager = NULL;
735cdf0e10cSrcweir }
736cdf0e10cSrcweir //------------------------------------------------------------------------------
dispose()737cdf0e10cSrcweir void ODatabaseModelImpl::dispose()
738cdf0e10cSrcweir {
739cdf0e10cSrcweir     // dispose the data source and the model
740cdf0e10cSrcweir     try
741cdf0e10cSrcweir     {
742cdf0e10cSrcweir         Reference< XDataSource > xDS( m_xDataSource );
743cdf0e10cSrcweir 		::comphelper::disposeComponent( xDS );
744cdf0e10cSrcweir 
745cdf0e10cSrcweir         Reference< XModel > xModel( m_xModel );
746cdf0e10cSrcweir         ::comphelper::disposeComponent( xModel );
747cdf0e10cSrcweir     }
748cdf0e10cSrcweir     catch( const Exception& )
749cdf0e10cSrcweir     {
750cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
751cdf0e10cSrcweir     }
752cdf0e10cSrcweir 	m_xDataSource = WeakReference<XDataSource>();
753cdf0e10cSrcweir     m_xModel = WeakReference< XModel >();
754cdf0e10cSrcweir 
755cdf0e10cSrcweir 	::std::vector<TContentPtr>::iterator aIter = m_aContainer.begin();
756cdf0e10cSrcweir 	::std::vector<TContentPtr>::iterator aEnd = m_aContainer.end();
757cdf0e10cSrcweir 	for (;aIter != aEnd ; ++aIter)
758cdf0e10cSrcweir 	{
759cdf0e10cSrcweir 		if ( aIter->get() )
760cdf0e10cSrcweir 			(*aIter)->m_pDataSource = NULL;
761cdf0e10cSrcweir 	}
762cdf0e10cSrcweir 	m_aContainer.clear();
763cdf0e10cSrcweir 
764cdf0e10cSrcweir 	clearConnections();
765cdf0e10cSrcweir 
766cdf0e10cSrcweir 	m_xNumberFormatsSupplier = NULL;
767cdf0e10cSrcweir 
768cdf0e10cSrcweir     try
769cdf0e10cSrcweir 	{
770cdf0e10cSrcweir         sal_Bool bCouldStore = commitEmbeddedStorage( true );
771cdf0e10cSrcweir             // "true" means that committing the embedded storage should not trigger committing the root
772cdf0e10cSrcweir             // storage. This is because we are going to commit the root storage ourself, anyway
773cdf0e10cSrcweir         disposeStorages();
774cdf0e10cSrcweir         if ( bCouldStore )
775cdf0e10cSrcweir             commitRootStorage();
776cdf0e10cSrcweir 
777cdf0e10cSrcweir         impl_switchToStorage_throw( NULL );
778cdf0e10cSrcweir 	}
779cdf0e10cSrcweir 	catch( const Exception& )
780cdf0e10cSrcweir 	{
781cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
782cdf0e10cSrcweir 	}
783cdf0e10cSrcweir 
784cdf0e10cSrcweir     if ( m_pStorageAccess )
785cdf0e10cSrcweir     {
786cdf0e10cSrcweir         m_pStorageAccess->dispose();
787cdf0e10cSrcweir         m_pStorageAccess->release();
788cdf0e10cSrcweir         m_pStorageAccess = NULL;
789cdf0e10cSrcweir     }
790cdf0e10cSrcweir }
791cdf0e10cSrcweir // -----------------------------------------------------------------------------
getNumberFormatsSupplier()792cdf0e10cSrcweir const Reference< XNumberFormatsSupplier > & ODatabaseModelImpl::getNumberFormatsSupplier()
793cdf0e10cSrcweir {
794cdf0e10cSrcweir 	if (!m_xNumberFormatsSupplier.is())
795cdf0e10cSrcweir 	{
796cdf0e10cSrcweir 		// the arguments : the locale of the current user
797cdf0e10cSrcweir 		UserInformation aUserInfo;
798cdf0e10cSrcweir 		Sequence< Any > aArguments(1);
799cdf0e10cSrcweir 		aArguments.getArray()[0] <<= aUserInfo.getUserLanguage();
800cdf0e10cSrcweir 
801cdf0e10cSrcweir         m_xNumberFormatsSupplier.set(
802cdf0e10cSrcweir             m_aContext.createComponentWithArguments( "com.sun.star.util.NumberFormatsSupplier", aArguments ), UNO_QUERY_THROW );
803cdf0e10cSrcweir 		DBG_ASSERT(m_xNumberFormatsSupplier.is(), "ODatabaseModelImpl::getNumberFormatsSupplier : could not instantiate the formats supplier !");
804cdf0e10cSrcweir 	}
805cdf0e10cSrcweir 	return m_xNumberFormatsSupplier;
806cdf0e10cSrcweir }
807cdf0e10cSrcweir 
808cdf0e10cSrcweir // -----------------------------------------------------------------------------
setDocFileLocation(const::rtl::OUString & i_rLoadedFrom)809cdf0e10cSrcweir void ODatabaseModelImpl::setDocFileLocation( const ::rtl::OUString& i_rLoadedFrom )
810cdf0e10cSrcweir {
811cdf0e10cSrcweir     ENSURE_OR_THROW( i_rLoadedFrom.getLength(), "invalid URL" );
812cdf0e10cSrcweir     m_sDocFileLocation = i_rLoadedFrom;
813cdf0e10cSrcweir }
814cdf0e10cSrcweir 
815cdf0e10cSrcweir // -----------------------------------------------------------------------------
setResource(const::rtl::OUString & i_rDocumentURL,const Sequence<PropertyValue> & _rArgs)816cdf0e10cSrcweir void ODatabaseModelImpl::setResource( const ::rtl::OUString& i_rDocumentURL, const Sequence< PropertyValue >& _rArgs )
817cdf0e10cSrcweir {
818cdf0e10cSrcweir     ENSURE_OR_THROW( i_rDocumentURL.getLength(), "invalid URL" );
819cdf0e10cSrcweir 
820cdf0e10cSrcweir     ::comphelper::NamedValueCollection aMediaDescriptor( _rArgs );
821cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
822cdf0e10cSrcweir     if ( aMediaDescriptor.has( "SalvagedFile" ) )
823cdf0e10cSrcweir     {
824cdf0e10cSrcweir         ::rtl::OUString sSalvagedFile( aMediaDescriptor.getOrDefault( "SalvagedFile", ::rtl::OUString() ) );
825cdf0e10cSrcweir         // If SalvagedFile is an empty string, this indicates "the document is being recovered, but i_rDocumentURL already
826cdf0e10cSrcweir         // is the real document URL, not the temporary document location"
827cdf0e10cSrcweir         if ( !sSalvagedFile.getLength() )
828cdf0e10cSrcweir             sSalvagedFile = i_rDocumentURL;
829cdf0e10cSrcweir 
830cdf0e10cSrcweir         OSL_ENSURE( sSalvagedFile == i_rDocumentURL, "ODatabaseModelImpl::setResource: inconsistency!" );
831cdf0e10cSrcweir             // nowadays, setResource should only be called with the logical URL of the document
832cdf0e10cSrcweir     }
833cdf0e10cSrcweir #endif
834cdf0e10cSrcweir 
835cdf0e10cSrcweir     m_aMediaDescriptor = stripLoadArguments( aMediaDescriptor );
836cdf0e10cSrcweir 
837cdf0e10cSrcweir     impl_switchToLogicalURL( i_rDocumentURL );
838cdf0e10cSrcweir }
839cdf0e10cSrcweir 
840cdf0e10cSrcweir // -----------------------------------------------------------------------------
stripLoadArguments(const::comphelper::NamedValueCollection & _rArguments)841cdf0e10cSrcweir ::comphelper::NamedValueCollection ODatabaseModelImpl::stripLoadArguments( const ::comphelper::NamedValueCollection& _rArguments )
842cdf0e10cSrcweir {
843cdf0e10cSrcweir     OSL_ENSURE( !_rArguments.has( "Model" ), "ODatabaseModelImpl::stripLoadArguments: this is suspicious (1)!" );
844cdf0e10cSrcweir     OSL_ENSURE( !_rArguments.has( "ViewName" ), "ODatabaseModelImpl::stripLoadArguments: this is suspicious (2)!" );
845cdf0e10cSrcweir 
846cdf0e10cSrcweir     ::comphelper::NamedValueCollection aMutableArgs( _rArguments );
847cdf0e10cSrcweir     aMutableArgs.remove( "Model" );
848cdf0e10cSrcweir     aMutableArgs.remove( "ViewName" );
849cdf0e10cSrcweir     return aMutableArgs;
850cdf0e10cSrcweir }
851cdf0e10cSrcweir 
852cdf0e10cSrcweir // -----------------------------------------------------------------------------
disposeStorages()853cdf0e10cSrcweir void ODatabaseModelImpl::disposeStorages() SAL_THROW(())
854cdf0e10cSrcweir {
855cdf0e10cSrcweir     getDocumentStorageAccess()->disposeStorages();
856cdf0e10cSrcweir }
857cdf0e10cSrcweir 
858cdf0e10cSrcweir // -----------------------------------------------------------------------------
createStorageFactory() const859cdf0e10cSrcweir Reference< XSingleServiceFactory > ODatabaseModelImpl::createStorageFactory() const
860cdf0e10cSrcweir {
861cdf0e10cSrcweir     return Reference< XSingleServiceFactory >( m_aContext.createComponent( "com.sun.star.embed.StorageFactory" ), UNO_QUERY_THROW );
862cdf0e10cSrcweir }
863cdf0e10cSrcweir // -----------------------------------------------------------------------------
commitRootStorage()864cdf0e10cSrcweir void ODatabaseModelImpl::commitRootStorage()
865cdf0e10cSrcweir {
866cdf0e10cSrcweir     Reference< XStorage > xStorage( getOrCreateRootStorage() );
867cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
868cdf0e10cSrcweir     bool bSuccess =
869cdf0e10cSrcweir #endif
870cdf0e10cSrcweir     commitStorageIfWriteable_ignoreErrors( xStorage );
871cdf0e10cSrcweir     OSL_ENSURE( bSuccess || !xStorage.is(),
872cdf0e10cSrcweir         "ODatabaseModelImpl::commitRootStorage: could commit the storage!" );
873cdf0e10cSrcweir }
874cdf0e10cSrcweir // -----------------------------------------------------------------------------
getOrCreateRootStorage()875cdf0e10cSrcweir Reference< XStorage > ODatabaseModelImpl::getOrCreateRootStorage()
876cdf0e10cSrcweir {
877cdf0e10cSrcweir 	if ( !m_xDocumentStorage.is() )
878cdf0e10cSrcweir 	{
879cdf0e10cSrcweir 		Reference< XSingleServiceFactory> xStorageFactory = createStorageFactory();
880cdf0e10cSrcweir         if ( xStorageFactory.is() )
881cdf0e10cSrcweir 		{
882cdf0e10cSrcweir             Any aSource;
883cdf0e10cSrcweir             aSource = m_aMediaDescriptor.get( "Stream" );
884cdf0e10cSrcweir             if ( !aSource.hasValue() )
885cdf0e10cSrcweir                 aSource = m_aMediaDescriptor.get( "InputStream" );
886cdf0e10cSrcweir             if ( !aSource.hasValue() && m_sDocFileLocation.getLength() )
887cdf0e10cSrcweir                 aSource <<= m_sDocFileLocation;
888cdf0e10cSrcweir             // TODO: shouldn't we also check URL?
889cdf0e10cSrcweir 
890cdf0e10cSrcweir             OSL_ENSURE( aSource.hasValue(), "ODatabaseModelImpl::getOrCreateRootStorage: no source to create the storage from!" );
891cdf0e10cSrcweir 
892cdf0e10cSrcweir             if ( aSource.hasValue() )
893cdf0e10cSrcweir             {
894cdf0e10cSrcweir 			    Sequence< Any > aStorageCreationArgs(2);
895cdf0e10cSrcweir                 aStorageCreationArgs[0] = aSource;
896cdf0e10cSrcweir 			    aStorageCreationArgs[1] <<= ElementModes::READWRITE;
897cdf0e10cSrcweir 
898cdf0e10cSrcweir                 Reference< XStorage > xDocumentStorage;
899cdf0e10cSrcweir 			    try
900cdf0e10cSrcweir 			    {
901cdf0e10cSrcweir 				    xDocumentStorage.set( xStorageFactory->createInstanceWithArguments( aStorageCreationArgs ), UNO_QUERY_THROW );
902cdf0e10cSrcweir 			    }
903cdf0e10cSrcweir 			    catch( const Exception& )
904cdf0e10cSrcweir 			    {
905cdf0e10cSrcweir 				    m_bDocumentReadOnly = sal_True;
906cdf0e10cSrcweir 				    aStorageCreationArgs[1] <<= ElementModes::READ;
907cdf0e10cSrcweir 				    try
908cdf0e10cSrcweir 				    {
909cdf0e10cSrcweir 					    xDocumentStorage.set( xStorageFactory->createInstanceWithArguments( aStorageCreationArgs ), UNO_QUERY_THROW );
910cdf0e10cSrcweir 				    }
911cdf0e10cSrcweir 				    catch( const Exception& )
912cdf0e10cSrcweir 				    {
913cdf0e10cSrcweir                         DBG_UNHANDLED_EXCEPTION();
914cdf0e10cSrcweir 				    }
915cdf0e10cSrcweir 			    }
916cdf0e10cSrcweir 
917cdf0e10cSrcweir                 impl_switchToStorage_throw( xDocumentStorage );
918cdf0e10cSrcweir             }
919cdf0e10cSrcweir 		}
920cdf0e10cSrcweir 	}
921cdf0e10cSrcweir 	return m_xDocumentStorage.getTyped();
922cdf0e10cSrcweir }
923cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDocumentStorageAccess()924cdf0e10cSrcweir DocumentStorageAccess* ODatabaseModelImpl::getDocumentStorageAccess()
925cdf0e10cSrcweir {
926cdf0e10cSrcweir     if ( !m_pStorageAccess )
927cdf0e10cSrcweir     {
928cdf0e10cSrcweir         m_pStorageAccess = new DocumentStorageAccess( *this );
929cdf0e10cSrcweir         m_pStorageAccess->acquire();
930cdf0e10cSrcweir     }
931cdf0e10cSrcweir     return m_pStorageAccess;
932cdf0e10cSrcweir }
933cdf0e10cSrcweir 
934cdf0e10cSrcweir // -----------------------------------------------------------------------------
modelIsDisposing(const bool _wasInitialized,ResetModelAccess)935cdf0e10cSrcweir void ODatabaseModelImpl::modelIsDisposing( const bool _wasInitialized, ResetModelAccess )
936cdf0e10cSrcweir {
937cdf0e10cSrcweir     m_xModel = Reference< XModel >();
938cdf0e10cSrcweir 
939cdf0e10cSrcweir     // Basic libraries and Dialog libraries are a model facet, though held at this impl class.
940cdf0e10cSrcweir     // They automatically dispose themself when the model they belong to is being disposed.
941cdf0e10cSrcweir     // So, to not be tempted to do anything with them, again, we reset them.
942cdf0e10cSrcweir     m_xBasicLibraries.clear();
943cdf0e10cSrcweir     m_xDialogLibraries.clear();
944cdf0e10cSrcweir 
945cdf0e10cSrcweir     m_bDocumentInitialized = _wasInitialized;
946cdf0e10cSrcweir }
947cdf0e10cSrcweir 
948cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDocumentSubStorageSupplier()949cdf0e10cSrcweir Reference< XDocumentSubStorageSupplier > ODatabaseModelImpl::getDocumentSubStorageSupplier()
950cdf0e10cSrcweir {
951cdf0e10cSrcweir     return getDocumentStorageAccess();
952cdf0e10cSrcweir }
953cdf0e10cSrcweir 
954cdf0e10cSrcweir // -----------------------------------------------------------------------------
commitEmbeddedStorage(bool _bPreventRootCommits)955cdf0e10cSrcweir bool ODatabaseModelImpl::commitEmbeddedStorage( bool _bPreventRootCommits )
956cdf0e10cSrcweir {
957cdf0e10cSrcweir     return getDocumentStorageAccess()->commitEmbeddedStorage( _bPreventRootCommits );
958cdf0e10cSrcweir }
959cdf0e10cSrcweir 
960cdf0e10cSrcweir // -----------------------------------------------------------------------------
commitStorageIfWriteable_ignoreErrors(const Reference<XStorage> & _rxStorage)961cdf0e10cSrcweir bool ODatabaseModelImpl::commitStorageIfWriteable_ignoreErrors( const Reference< XStorage >& _rxStorage ) SAL_THROW(())
962cdf0e10cSrcweir {
963cdf0e10cSrcweir     bool bSuccess = false;
964cdf0e10cSrcweir     try
965cdf0e10cSrcweir     {
966cdf0e10cSrcweir         bSuccess = tools::stor::commitStorageIfWriteable( _rxStorage );
967cdf0e10cSrcweir     }
968cdf0e10cSrcweir     catch( const Exception& )
969cdf0e10cSrcweir     {
970cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
971cdf0e10cSrcweir     }
972cdf0e10cSrcweir     return bSuccess;
973cdf0e10cSrcweir }
974cdf0e10cSrcweir // -----------------------------------------------------------------------------
setModified(sal_Bool _bModified)975cdf0e10cSrcweir void ODatabaseModelImpl::setModified( sal_Bool _bModified )
976cdf0e10cSrcweir {
977cdf0e10cSrcweir     if ( isModifyLocked() )
978cdf0e10cSrcweir         return;
979cdf0e10cSrcweir 
980cdf0e10cSrcweir     try
981cdf0e10cSrcweir     {
982cdf0e10cSrcweir         Reference< XModifiable > xModi( m_xModel.get(), UNO_QUERY );
983cdf0e10cSrcweir         if ( xModi.is() )
984cdf0e10cSrcweir             xModi->setModified( _bModified );
985cdf0e10cSrcweir         else
986cdf0e10cSrcweir             m_bModified = _bModified;
987cdf0e10cSrcweir     }
988cdf0e10cSrcweir     catch( const Exception& )
989cdf0e10cSrcweir     {
990cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
991cdf0e10cSrcweir     }
992cdf0e10cSrcweir }
993cdf0e10cSrcweir 
994cdf0e10cSrcweir // -----------------------------------------------------------------------------
getOrCreateDataSource()995cdf0e10cSrcweir Reference<XDataSource> ODatabaseModelImpl::getOrCreateDataSource()
996cdf0e10cSrcweir {
997cdf0e10cSrcweir 	Reference<XDataSource> xDs = m_xDataSource;
998cdf0e10cSrcweir 	if ( !xDs.is() )
999cdf0e10cSrcweir 	{
1000cdf0e10cSrcweir 		xDs = new ODatabaseSource(this);
1001cdf0e10cSrcweir         m_xDataSource = xDs;
1002cdf0e10cSrcweir 	}
1003cdf0e10cSrcweir 	return xDs;
1004cdf0e10cSrcweir }
1005cdf0e10cSrcweir // -----------------------------------------------------------------------------
getModel_noCreate() const1006cdf0e10cSrcweir Reference< XModel> ODatabaseModelImpl::getModel_noCreate() const
1007cdf0e10cSrcweir {
1008cdf0e10cSrcweir     return m_xModel;
1009cdf0e10cSrcweir }
1010cdf0e10cSrcweir // -----------------------------------------------------------------------------
createNewModel_deliverOwnership(bool _bInitialize)1011cdf0e10cSrcweir Reference< XModel > ODatabaseModelImpl::createNewModel_deliverOwnership( bool _bInitialize )
1012cdf0e10cSrcweir {
1013cdf0e10cSrcweir     Reference< XModel > xModel( m_xModel );
1014cdf0e10cSrcweir     OSL_PRECOND( !xModel.is(), "ODatabaseModelImpl::createNewModel_deliverOwnership: not to be called if there already is a model!" );
1015cdf0e10cSrcweir 	if ( !xModel.is() )
1016cdf0e10cSrcweir     {
1017cdf0e10cSrcweir         bool bHadModelBefore = m_bDocumentInitialized;
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir         xModel = ODatabaseDocument::createDatabaseDocument( this, ODatabaseDocument::FactoryAccess() );
1020cdf0e10cSrcweir         m_xModel = xModel;
1021cdf0e10cSrcweir 
1022cdf0e10cSrcweir         try
1023cdf0e10cSrcweir         {
1024cdf0e10cSrcweir             Reference< XSet > xModelCollection;
1025cdf0e10cSrcweir             if ( m_aContext.createComponent( "com.sun.star.frame.GlobalEventBroadcaster", xModelCollection ) )
1026cdf0e10cSrcweir                 xModelCollection->insert( makeAny( xModel ) );
1027cdf0e10cSrcweir         }
1028cdf0e10cSrcweir         catch( const Exception& )
1029cdf0e10cSrcweir         {
1030cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
1031cdf0e10cSrcweir         }
1032cdf0e10cSrcweir 
1033cdf0e10cSrcweir         if ( bHadModelBefore )
1034cdf0e10cSrcweir         {
1035cdf0e10cSrcweir             // do an attachResources
1036cdf0e10cSrcweir             // In case the document is loaded regularly, this is not necessary, as our loader will do it.
1037cdf0e10cSrcweir             // However, in case that the document is implicitly created by asking the data source for the document,
1038cdf0e10cSrcweir             // then nobody would call the doc's attachResource. So, we do it here, to ensure it's in a proper
1039cdf0e10cSrcweir             // state, fires all events, and so on.
1040cdf0e10cSrcweir             // #i105505# / 2009-10-02 / frank.schoenheit@sun.com
1041cdf0e10cSrcweir             xModel->attachResource( xModel->getURL(), m_aMediaDescriptor.getPropertyValues() );
1042cdf0e10cSrcweir         }
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir         if ( _bInitialize )
1045cdf0e10cSrcweir         {
1046cdf0e10cSrcweir             try
1047cdf0e10cSrcweir             {
1048cdf0e10cSrcweir                 Reference< XLoadable > xLoad( xModel, UNO_QUERY_THROW );
1049cdf0e10cSrcweir                 xLoad->initNew();
1050cdf0e10cSrcweir             }
1051cdf0e10cSrcweir             catch( RuntimeException& ) { throw; }
1052cdf0e10cSrcweir             catch( const Exception& )
1053cdf0e10cSrcweir             {
1054cdf0e10cSrcweir         	    DBG_UNHANDLED_EXCEPTION();
1055cdf0e10cSrcweir             }
1056cdf0e10cSrcweir         }
1057cdf0e10cSrcweir     }
1058cdf0e10cSrcweir 	return xModel;
1059cdf0e10cSrcweir }
1060cdf0e10cSrcweir // -----------------------------------------------------------------------------
acquire()1061cdf0e10cSrcweir oslInterlockedCount SAL_CALL ODatabaseModelImpl::acquire()
1062cdf0e10cSrcweir {
1063cdf0e10cSrcweir 	return osl_incrementInterlockedCount(&m_refCount);
1064cdf0e10cSrcweir }
1065cdf0e10cSrcweir // -----------------------------------------------------------------------------
release()1066cdf0e10cSrcweir oslInterlockedCount SAL_CALL ODatabaseModelImpl::release()
1067cdf0e10cSrcweir {
1068cdf0e10cSrcweir 	if ( osl_decrementInterlockedCount(&m_refCount) == 0 )
1069cdf0e10cSrcweir 	{
1070cdf0e10cSrcweir         acquire();  // prevent multiple releases
1071cdf0e10cSrcweir         m_pDBContext->removeFromTerminateListener(*this);
1072cdf0e10cSrcweir         dispose();
1073cdf0e10cSrcweir         m_pDBContext->storeTransientProperties(*this);
1074cdf0e10cSrcweir         revokeDataSource();
1075cdf0e10cSrcweir 		delete this;
1076cdf0e10cSrcweir 		return 0;
1077cdf0e10cSrcweir 	}
1078cdf0e10cSrcweir 	return m_refCount;
1079cdf0e10cSrcweir }
1080cdf0e10cSrcweir // -----------------------------------------------------------------------------
commitStorages()1081cdf0e10cSrcweir void ODatabaseModelImpl::commitStorages() SAL_THROW(( IOException, RuntimeException ))
1082cdf0e10cSrcweir {
1083cdf0e10cSrcweir     getDocumentStorageAccess()->commitStorages();
1084cdf0e10cSrcweir }
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir // -----------------------------------------------------------------------------
getStorage(const ObjectType _eType,const sal_Int32 _nDesiredMode)1087cdf0e10cSrcweir Reference< XStorage > ODatabaseModelImpl::getStorage( const ObjectType _eType, const sal_Int32 _nDesiredMode )
1088cdf0e10cSrcweir {
1089cdf0e10cSrcweir     return getDocumentStorageAccess()->getDocumentSubStorage( getObjectContainerStorageName( _eType ), _nDesiredMode );
1090cdf0e10cSrcweir }
1091cdf0e10cSrcweir 
1092cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDefaultDataSourceSettings()1093cdf0e10cSrcweir const AsciiPropertyValue* ODatabaseModelImpl::getDefaultDataSourceSettings()
1094cdf0e10cSrcweir {
1095cdf0e10cSrcweir     static const AsciiPropertyValue aKnownSettings[] =
1096cdf0e10cSrcweir     {
1097cdf0e10cSrcweir         // known JDBC settings
1098cdf0e10cSrcweir         AsciiPropertyValue( "JavaDriverClass",            makeAny( ::rtl::OUString() ) ),
1099cdf0e10cSrcweir         AsciiPropertyValue( "JavaDriverClassPath",       makeAny( ::rtl::OUString() ) ),
1100cdf0e10cSrcweir         AsciiPropertyValue( "IgnoreCurrency",             makeAny( (sal_Bool)sal_False ) ),
1101cdf0e10cSrcweir         // known settings for file-based drivers
1102cdf0e10cSrcweir         AsciiPropertyValue( "Extension",                  makeAny( ::rtl::OUString() ) ),
1103cdf0e10cSrcweir         AsciiPropertyValue( "CharSet",                    makeAny( ::rtl::OUString() ) ),
1104cdf0e10cSrcweir         AsciiPropertyValue( "HeaderLine",                 makeAny( (sal_Bool)sal_True ) ),
1105cdf0e10cSrcweir         AsciiPropertyValue( "FieldDelimiter",             makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "," ) ) ) ),
1106cdf0e10cSrcweir         AsciiPropertyValue( "StringDelimiter",            makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\"" ) ) ) ),
1107cdf0e10cSrcweir         AsciiPropertyValue( "DecimalDelimiter",           makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) ) ),
1108cdf0e10cSrcweir         AsciiPropertyValue( "ThousandDelimiter",          makeAny( ::rtl::OUString() ) ),
1109cdf0e10cSrcweir         AsciiPropertyValue( "ShowDeleted",                makeAny( (sal_Bool)sal_False ) ),
1110cdf0e10cSrcweir         // known ODBC settings
1111cdf0e10cSrcweir         AsciiPropertyValue( "SystemDriverSettings",       makeAny( ::rtl::OUString() ) ),
1112cdf0e10cSrcweir         AsciiPropertyValue( "UseCatalog",                 makeAny( (sal_Bool)sal_False ) ),
1113cdf0e10cSrcweir         AsciiPropertyValue( "TypeInfoSettings",           makeAny( Sequence< Any >()) ),
1114cdf0e10cSrcweir         // settings related to auto increment handling
1115cdf0e10cSrcweir         AsciiPropertyValue( "AutoIncrementCreation",      makeAny( ::rtl::OUString() ) ),
1116cdf0e10cSrcweir         AsciiPropertyValue( "AutoRetrievingStatement",    makeAny( ::rtl::OUString() ) ),
1117cdf0e10cSrcweir         AsciiPropertyValue( "IsAutoRetrievingEnabled",    makeAny( (sal_Bool)sal_False ) ),
1118cdf0e10cSrcweir         // known Adabas D driver setting
1119cdf0e10cSrcweir         AsciiPropertyValue( "ShutdownDatabase",           makeAny( (sal_Bool)sal_False ) ),
1120cdf0e10cSrcweir         AsciiPropertyValue( "DataCacheSizeIncrement",     makeAny( (sal_Int32)20 ) ),
1121cdf0e10cSrcweir         AsciiPropertyValue( "DataCacheSize",              makeAny( (sal_Int32)20 ) ),
1122cdf0e10cSrcweir         AsciiPropertyValue( "ControlUser",                makeAny( ::rtl::OUString() ) ),
1123cdf0e10cSrcweir         AsciiPropertyValue( "ControlPassword",            makeAny( ::rtl::OUString() ) ),
1124cdf0e10cSrcweir         // known LDAP driver settings
1125cdf0e10cSrcweir         AsciiPropertyValue( "HostName",                   makeAny( ::rtl::OUString() ) ),
1126cdf0e10cSrcweir         AsciiPropertyValue( "PortNumber",                 makeAny( (sal_Int32)389 ) ),
1127cdf0e10cSrcweir         AsciiPropertyValue( "BaseDN",                     makeAny( ::rtl::OUString() ) ),
1128cdf0e10cSrcweir         AsciiPropertyValue( "MaxRowCount",                makeAny( (sal_Int32)100 ) ),
1129cdf0e10cSrcweir         // known MySQLNative driver settings
1130cdf0e10cSrcweir         AsciiPropertyValue( "LocalSocket",                makeAny( ::rtl::OUString() ) ),
1131cdf0e10cSrcweir         AsciiPropertyValue( "NamedPipe",                  makeAny( ::rtl::OUString() ) ),
1132cdf0e10cSrcweir         // misc known driver settings
1133cdf0e10cSrcweir         AsciiPropertyValue( "ParameterNameSubstitution",  makeAny( (sal_Bool)sal_False ) ),
1134cdf0e10cSrcweir         AsciiPropertyValue( "AddIndexAppendix",           makeAny( (sal_Bool)sal_True ) ),
1135cdf0e10cSrcweir         AsciiPropertyValue( "IgnoreDriverPrivileges",     makeAny( (sal_Bool)sal_True ) ),
1136cdf0e10cSrcweir         AsciiPropertyValue( "ImplicitCatalogRestriction", ::cppu::UnoType< ::rtl::OUString >::get() ),
1137cdf0e10cSrcweir         AsciiPropertyValue( "ImplicitSchemaRestriction",  ::cppu::UnoType< ::rtl::OUString >::get() ),
1138cdf0e10cSrcweir         AsciiPropertyValue( "PrimaryKeySupport",          ::cppu::UnoType< sal_Bool >::get() ),
1139cdf0e10cSrcweir         AsciiPropertyValue( "ShowColumnDescription",      makeAny( (sal_Bool)sal_False ) ),
1140cdf0e10cSrcweir         // known SDB level settings
1141cdf0e10cSrcweir         AsciiPropertyValue( "NoNameLengthLimit",          makeAny( (sal_Bool)sal_False ) ),
1142cdf0e10cSrcweir         AsciiPropertyValue( "AppendTableAliasName",       makeAny( (sal_Bool)sal_False ) ),
1143cdf0e10cSrcweir         AsciiPropertyValue( "GenerateASBeforeCorrelationName",  makeAny( (sal_Bool)sal_True ) ),
1144cdf0e10cSrcweir         AsciiPropertyValue( "ColumnAliasInOrderBy",       makeAny( (sal_Bool)sal_True ) ),
1145cdf0e10cSrcweir         AsciiPropertyValue( "EnableSQL92Check",           makeAny( (sal_Bool)sal_False ) ),
1146cdf0e10cSrcweir         AsciiPropertyValue( "BooleanComparisonMode",      makeAny( BooleanComparisonMode::EQUAL_INTEGER ) ),
1147cdf0e10cSrcweir         AsciiPropertyValue( "TableTypeFilterMode",        makeAny( (sal_Int32)3 ) ),
1148cdf0e10cSrcweir         AsciiPropertyValue( "RespectDriverResultSetType", makeAny( (sal_Bool)sal_False ) ),
1149cdf0e10cSrcweir         AsciiPropertyValue( "UseSchemaInSelect",          makeAny( (sal_Bool)sal_True ) ),
1150cdf0e10cSrcweir         AsciiPropertyValue( "UseCatalogInSelect",         makeAny( (sal_Bool)sal_True ) ),
1151cdf0e10cSrcweir         AsciiPropertyValue( "EnableOuterJoinEscape",      makeAny( (sal_Bool)sal_True ) ),
1152cdf0e10cSrcweir         AsciiPropertyValue( "PreferDosLikeLineEnds",      makeAny( (sal_Bool)sal_False ) ),
1153cdf0e10cSrcweir         AsciiPropertyValue( "FormsCheckRequiredFields",   makeAny( (sal_Bool)sal_True ) ),
1154cdf0e10cSrcweir         AsciiPropertyValue( "EscapeDateTime",             makeAny( (sal_Bool)sal_True ) ),
1155cdf0e10cSrcweir 
1156cdf0e10cSrcweir         // known services to handle database tasks
1157cdf0e10cSrcweir         AsciiPropertyValue( "TableAlterationServiceName", makeAny( ::rtl::OUString() ) ),
1158cdf0e10cSrcweir         AsciiPropertyValue( "TableRenameServiceName",     makeAny( ::rtl::OUString() ) ),
1159cdf0e10cSrcweir         AsciiPropertyValue( "ViewAlterationServiceName",  makeAny( ::rtl::OUString() ) ),
1160cdf0e10cSrcweir         AsciiPropertyValue( "ViewAccessServiceName",      makeAny( ::rtl::OUString() ) ),
1161cdf0e10cSrcweir         AsciiPropertyValue( "CommandDefinitions",         makeAny( ::rtl::OUString() ) ),
1162cdf0e10cSrcweir         AsciiPropertyValue( "Forms",                      makeAny( ::rtl::OUString() ) ),
1163cdf0e10cSrcweir         AsciiPropertyValue( "Reports",                    makeAny( ::rtl::OUString() ) ),
1164cdf0e10cSrcweir         AsciiPropertyValue( "KeyAlterationServiceName",   makeAny( ::rtl::OUString() ) ),
1165cdf0e10cSrcweir         AsciiPropertyValue( "IndexAlterationServiceName", makeAny( ::rtl::OUString() ) ),
1166cdf0e10cSrcweir 
1167cdf0e10cSrcweir         AsciiPropertyValue()
1168cdf0e10cSrcweir     };
1169cdf0e10cSrcweir     return aKnownSettings;
1170cdf0e10cSrcweir }
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir // -----------------------------------------------------------------------------
getObjectContainer(ObjectType _eType)1173cdf0e10cSrcweir TContentPtr& ODatabaseModelImpl::getObjectContainer( ObjectType _eType )
1174cdf0e10cSrcweir {
1175cdf0e10cSrcweir     OSL_PRECOND( _eType >= E_FORM && _eType <= E_TABLE, "ODatabaseModelImpl::getObjectContainer: illegal index!" );
1176cdf0e10cSrcweir     TContentPtr& rContentPtr = m_aContainer[ _eType ];
1177cdf0e10cSrcweir 
1178cdf0e10cSrcweir     if ( !rContentPtr.get() )
1179cdf0e10cSrcweir 	{
1180cdf0e10cSrcweir 		rContentPtr = TContentPtr( new ODefinitionContainer_Impl );
1181cdf0e10cSrcweir 		rContentPtr->m_pDataSource = this;
1182cdf0e10cSrcweir 		rContentPtr->m_aProps.aTitle = lcl_getContainerStorageName_throw( _eType );
1183cdf0e10cSrcweir 	}
1184cdf0e10cSrcweir     return rContentPtr;
1185cdf0e10cSrcweir }
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir // -----------------------------------------------------------------------------
revokeDataSource() const1188cdf0e10cSrcweir void ODatabaseModelImpl::revokeDataSource() const
1189cdf0e10cSrcweir {
1190cdf0e10cSrcweir     if ( m_pDBContext && m_sDocumentURL.getLength() )
1191cdf0e10cSrcweir         m_pDBContext->revokeDatabaseDocument( *this );
1192cdf0e10cSrcweir }
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir // -----------------------------------------------------------------------------
adjustMacroMode_AutoReject()1195cdf0e10cSrcweir bool ODatabaseModelImpl::adjustMacroMode_AutoReject()
1196cdf0e10cSrcweir {
1197cdf0e10cSrcweir     return m_aMacroMode.adjustMacroMode( NULL );
1198cdf0e10cSrcweir }
1199cdf0e10cSrcweir 
1200cdf0e10cSrcweir // -----------------------------------------------------------------------------
checkMacrosOnLoading()1201cdf0e10cSrcweir bool ODatabaseModelImpl::checkMacrosOnLoading()
1202cdf0e10cSrcweir {
1203cdf0e10cSrcweir     Reference< XInteractionHandler > xInteraction;
1204cdf0e10cSrcweir     xInteraction = m_aMediaDescriptor.getOrDefault( "InteractionHandler", xInteraction );
1205cdf0e10cSrcweir     return m_aMacroMode.checkMacrosOnLoading( xInteraction );
1206cdf0e10cSrcweir }
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir // -----------------------------------------------------------------------------
resetMacroExecutionMode()1209cdf0e10cSrcweir void ODatabaseModelImpl::resetMacroExecutionMode()
1210cdf0e10cSrcweir {
1211cdf0e10cSrcweir     m_aMacroMode = ::sfx2::DocumentMacroMode( *this );
1212cdf0e10cSrcweir }
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir // -----------------------------------------------------------------------------
getLibraryContainer(bool _bScript)1215cdf0e10cSrcweir Reference< XStorageBasedLibraryContainer > ODatabaseModelImpl::getLibraryContainer( bool _bScript )
1216cdf0e10cSrcweir {
1217cdf0e10cSrcweir     Reference< XStorageBasedLibraryContainer >& rxContainer( _bScript ? m_xBasicLibraries : m_xDialogLibraries );
1218cdf0e10cSrcweir     if ( rxContainer.is() )
1219cdf0e10cSrcweir         return rxContainer;
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir     Reference< XStorageBasedDocument > xDocument( getModel_noCreate(), UNO_QUERY_THROW );
1222cdf0e10cSrcweir         // this is only to be called if there already exists a document model - in fact, it is
1223cdf0e10cSrcweir         // to be called by the document model only
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir     try
1226cdf0e10cSrcweir     {
1227cdf0e10cSrcweir         Reference< XStorageBasedLibraryContainer > (*Factory)( const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&)
1228cdf0e10cSrcweir             = _bScript ? &DocumentScriptLibraryContainer::create : &DocumentDialogLibraryContainer::create;
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir         rxContainer.set(
1231cdf0e10cSrcweir             (*Factory)( m_aContext.getUNOContext(), xDocument ),
1232cdf0e10cSrcweir             UNO_QUERY_THROW
1233cdf0e10cSrcweir         );
1234cdf0e10cSrcweir     }
1235cdf0e10cSrcweir     catch( const RuntimeException& )
1236cdf0e10cSrcweir     {
1237cdf0e10cSrcweir         throw;
1238cdf0e10cSrcweir     }
1239cdf0e10cSrcweir     catch( const Exception& )
1240cdf0e10cSrcweir     {
1241cdf0e10cSrcweir         throw WrappedTargetRuntimeException(
1242cdf0e10cSrcweir             ::rtl::OUString(),
1243cdf0e10cSrcweir             xDocument,
1244cdf0e10cSrcweir             ::cppu::getCaughtException()
1245cdf0e10cSrcweir         );
1246cdf0e10cSrcweir     }
1247cdf0e10cSrcweir     return rxContainer;
1248cdf0e10cSrcweir }
1249cdf0e10cSrcweir 
1250cdf0e10cSrcweir // -----------------------------------------------------------------------------
storeLibraryContainersTo(const Reference<XStorage> & _rxToRootStorage)1251cdf0e10cSrcweir void ODatabaseModelImpl::storeLibraryContainersTo( const Reference< XStorage >& _rxToRootStorage )
1252cdf0e10cSrcweir {
1253cdf0e10cSrcweir     if ( m_xBasicLibraries.is() )
1254cdf0e10cSrcweir         m_xBasicLibraries->storeLibrariesToStorage( _rxToRootStorage );
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir     if ( m_xDialogLibraries.is() )
1257cdf0e10cSrcweir         m_xDialogLibraries->storeLibrariesToStorage( _rxToRootStorage );
1258cdf0e10cSrcweir }
1259cdf0e10cSrcweir 
1260cdf0e10cSrcweir // -----------------------------------------------------------------------------
switchToStorage(const Reference<XStorage> & _rxNewRootStorage)1261cdf0e10cSrcweir Reference< XStorage > ODatabaseModelImpl::switchToStorage( const Reference< XStorage >& _rxNewRootStorage )
1262cdf0e10cSrcweir {
1263cdf0e10cSrcweir     if ( !_rxNewRootStorage.is() )
1264cdf0e10cSrcweir         throw IllegalArgumentException();
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir     return impl_switchToStorage_throw( _rxNewRootStorage );
1267cdf0e10cSrcweir }
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir // -----------------------------------------------------------------------------
1270cdf0e10cSrcweir namespace
1271cdf0e10cSrcweir {
lcl_modifyListening(::sfx2::IModifiableDocument & _rDocument,const Reference<XStorage> & _rxStorage,::rtl::Reference<::sfx2::DocumentStorageModifyListener> & _inout_rListener,::vos::IMutex & _rMutex,bool _bListen)1272cdf0e10cSrcweir     void lcl_modifyListening( ::sfx2::IModifiableDocument& _rDocument,
1273cdf0e10cSrcweir         const Reference< XStorage >& _rxStorage, ::rtl::Reference< ::sfx2::DocumentStorageModifyListener >& _inout_rListener,
1274cdf0e10cSrcweir         ::vos::IMutex& _rMutex, bool _bListen )
1275cdf0e10cSrcweir     {
1276cdf0e10cSrcweir         Reference< XModifiable > xModify( _rxStorage, UNO_QUERY );
1277cdf0e10cSrcweir         OSL_ENSURE( xModify.is() || !_rxStorage.is(), "lcl_modifyListening: storage can't notify us!" );
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir         if ( xModify.is() && !_bListen && _inout_rListener.is() )
1280cdf0e10cSrcweir         {
1281cdf0e10cSrcweir             xModify->removeModifyListener( _inout_rListener.get() );
1282cdf0e10cSrcweir         }
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir         if ( _inout_rListener.is() )
1285cdf0e10cSrcweir         {
1286cdf0e10cSrcweir             _inout_rListener->dispose();
1287cdf0e10cSrcweir             _inout_rListener = NULL;
1288cdf0e10cSrcweir         }
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir         if ( xModify.is() && _bListen )
1291cdf0e10cSrcweir         {
1292cdf0e10cSrcweir             _inout_rListener = new ::sfx2::DocumentStorageModifyListener( _rDocument, _rMutex );
1293cdf0e10cSrcweir             xModify->addModifyListener( _inout_rListener.get() );
1294cdf0e10cSrcweir         }
1295cdf0e10cSrcweir     }
1296cdf0e10cSrcweir }
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir // -----------------------------------------------------------------------------
1299cdf0e10cSrcweir namespace
1300cdf0e10cSrcweir {
lcl_rebaseScriptStorage_throw(const Reference<XStorageBasedLibraryContainer> & _rxContainer,const Reference<XStorage> & _rxNewRootStorage)1301cdf0e10cSrcweir     static void lcl_rebaseScriptStorage_throw( const Reference< XStorageBasedLibraryContainer >& _rxContainer,
1302cdf0e10cSrcweir         const Reference< XStorage >& _rxNewRootStorage )
1303cdf0e10cSrcweir     {
1304cdf0e10cSrcweir         if ( _rxContainer.is() )
1305cdf0e10cSrcweir         {
1306cdf0e10cSrcweir             if ( _rxNewRootStorage.is() )
1307cdf0e10cSrcweir                 _rxContainer->setRootStorage( _rxNewRootStorage );
1308cdf0e10cSrcweir //            else
1309cdf0e10cSrcweir                    // TODO: what to do here? dispose the container?
1310cdf0e10cSrcweir         }
1311cdf0e10cSrcweir     }
1312cdf0e10cSrcweir }
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_switchToStorage_throw(const Reference<XStorage> & _rxNewRootStorage)1315cdf0e10cSrcweir Reference< XStorage > ODatabaseModelImpl::impl_switchToStorage_throw( const Reference< XStorage >& _rxNewRootStorage )
1316cdf0e10cSrcweir {
1317cdf0e10cSrcweir     // stop listening for modifications at the old storage
1318cdf0e10cSrcweir     lcl_modifyListening( *this, m_xDocumentStorage.getTyped(), m_pStorageModifyListener, m_aMutexFacade, false );
1319cdf0e10cSrcweir 
1320cdf0e10cSrcweir     // set new storage
1321cdf0e10cSrcweir     m_xDocumentStorage.reset( _rxNewRootStorage, SharedStorage::TakeOwnership );
1322cdf0e10cSrcweir 
1323cdf0e10cSrcweir     // start listening for modifications
1324cdf0e10cSrcweir     lcl_modifyListening( *this, m_xDocumentStorage.getTyped(), m_pStorageModifyListener, m_aMutexFacade, true );
1325cdf0e10cSrcweir 
1326cdf0e10cSrcweir     // forward new storage to Basic and Dialog library containers
1327cdf0e10cSrcweir     lcl_rebaseScriptStorage_throw( m_xBasicLibraries, m_xDocumentStorage.getTyped() );
1328cdf0e10cSrcweir     lcl_rebaseScriptStorage_throw( m_xDialogLibraries, m_xDocumentStorage.getTyped() );
1329cdf0e10cSrcweir 
1330cdf0e10cSrcweir     m_bReadOnly = !tools::stor::storageIsWritable_nothrow( m_xDocumentStorage.getTyped() );
1331cdf0e10cSrcweir     // TODO: our data source, if it exists, must broadcast the change of its ReadOnly property
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir     return m_xDocumentStorage.getTyped();
1334cdf0e10cSrcweir }
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir // -----------------------------------------------------------------------------
impl_switchToLogicalURL(const::rtl::OUString & i_rDocumentURL)1337cdf0e10cSrcweir void ODatabaseModelImpl::impl_switchToLogicalURL( const ::rtl::OUString& i_rDocumentURL )
1338cdf0e10cSrcweir {
1339cdf0e10cSrcweir     if ( i_rDocumentURL == m_sDocumentURL )
1340cdf0e10cSrcweir         return;
1341cdf0e10cSrcweir 
1342cdf0e10cSrcweir     const ::rtl::OUString sOldURL( m_sDocumentURL );
1343cdf0e10cSrcweir     // update our name, if necessary
1344cdf0e10cSrcweir     if  (   ( m_sName == m_sDocumentURL )   // our name is our old URL
1345cdf0e10cSrcweir         ||  ( !m_sName.getLength() )        // we do not have a name, yet (i.e. are not registered at the database context)
1346cdf0e10cSrcweir         )
1347cdf0e10cSrcweir     {
1348cdf0e10cSrcweir 	    INetURLObject aURL( i_rDocumentURL );
1349cdf0e10cSrcweir 	    if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
1350cdf0e10cSrcweir         {
1351cdf0e10cSrcweir 		    m_sName = i_rDocumentURL;
1352cdf0e10cSrcweir             // TODO: our data source must broadcast the change of the Name property
1353cdf0e10cSrcweir         }
1354cdf0e10cSrcweir     }
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir     // remember URL
1357cdf0e10cSrcweir     m_sDocumentURL = i_rDocumentURL;
1358cdf0e10cSrcweir 
1359cdf0e10cSrcweir     // update our location, if necessary
1360cdf0e10cSrcweir     if  ( m_sDocFileLocation.getLength() == 0 )
1361cdf0e10cSrcweir         m_sDocFileLocation = m_sDocumentURL;
1362cdf0e10cSrcweir 
1363cdf0e10cSrcweir     // register at the database context, or change registration
1364cdf0e10cSrcweir     if ( m_pDBContext )
1365cdf0e10cSrcweir     {
1366cdf0e10cSrcweir 	    if ( sOldURL.getLength() )
1367cdf0e10cSrcweir             m_pDBContext->databaseDocumentURLChange( sOldURL, m_sDocumentURL );
1368cdf0e10cSrcweir         else
1369cdf0e10cSrcweir             m_pDBContext->registerDatabaseDocument( *this );
1370cdf0e10cSrcweir     }
1371cdf0e10cSrcweir }
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir // -----------------------------------------------------------------------------
getObjectContainerStorageName(const ObjectType _eType)1374cdf0e10cSrcweir ::rtl::OUString ODatabaseModelImpl::getObjectContainerStorageName( const ObjectType _eType )
1375cdf0e10cSrcweir {
1376cdf0e10cSrcweir     return lcl_getContainerStorageName_throw( _eType );
1377cdf0e10cSrcweir }
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir // -----------------------------------------------------------------------------
getCurrentMacroExecMode() const1380cdf0e10cSrcweir sal_Int16 ODatabaseModelImpl::getCurrentMacroExecMode() const
1381cdf0e10cSrcweir {
1382cdf0e10cSrcweir     sal_Int16 nCurrentMode = MacroExecMode::NEVER_EXECUTE;
1383cdf0e10cSrcweir     try
1384cdf0e10cSrcweir     {
1385cdf0e10cSrcweir         nCurrentMode = m_aMediaDescriptor.getOrDefault( "MacroExecutionMode", nCurrentMode );
1386cdf0e10cSrcweir     }
1387cdf0e10cSrcweir     catch( const Exception& )
1388cdf0e10cSrcweir     {
1389cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
1390cdf0e10cSrcweir     }
1391cdf0e10cSrcweir     return nCurrentMode;
1392cdf0e10cSrcweir }
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir // -----------------------------------------------------------------------------
setCurrentMacroExecMode(sal_uInt16 nMacroMode)1395cdf0e10cSrcweir sal_Bool ODatabaseModelImpl::setCurrentMacroExecMode( sal_uInt16 nMacroMode )
1396cdf0e10cSrcweir {
1397cdf0e10cSrcweir     m_aMediaDescriptor.put( "MacroExecutionMode", nMacroMode );
1398cdf0e10cSrcweir     return sal_True;
1399cdf0e10cSrcweir }
1400cdf0e10cSrcweir 
1401cdf0e10cSrcweir // -----------------------------------------------------------------------------
getDocumentLocation() const1402cdf0e10cSrcweir ::rtl::OUString ODatabaseModelImpl::getDocumentLocation() const
1403cdf0e10cSrcweir {
1404cdf0e10cSrcweir     return getURL();
1405cdf0e10cSrcweir     // formerly, we returned getDocFileLocation here, which is the location of the file from which we
1406cdf0e10cSrcweir     // recovered the "real" document.
1407cdf0e10cSrcweir     // However, during CWS autorecovery evolving, we clarified (with MAV/MT) the role of XModel::getURL and
1408cdf0e10cSrcweir     // XStorable::getLocation. In this course, we agreed that for a macro security check, the *document URL*
1409cdf0e10cSrcweir     // (not the recovery file URL) is to be used: The recovery file lies in the backup folder, and by definition,
1410cdf0e10cSrcweir     // this folder is considered to be secure. So, the document URL needs to be used to decide about the security.
1411cdf0e10cSrcweir }
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir // -----------------------------------------------------------------------------
getZipStorageToSign()1414cdf0e10cSrcweir Reference< XStorage > ODatabaseModelImpl::getZipStorageToSign()
1415cdf0e10cSrcweir {
1416cdf0e10cSrcweir     // we do not support signing the scripting storages, so we're allowed to
1417cdf0e10cSrcweir     // return <NULL/> here.
1418cdf0e10cSrcweir     return Reference< XStorage >();
1419cdf0e10cSrcweir }
1420cdf0e10cSrcweir 
1421cdf0e10cSrcweir // -----------------------------------------------------------------------------
determineEmbeddedMacros()1422cdf0e10cSrcweir ODatabaseModelImpl::EmbeddedMacros ODatabaseModelImpl::determineEmbeddedMacros()
1423cdf0e10cSrcweir {
1424cdf0e10cSrcweir     if ( !m_aEmbeddedMacros )
1425cdf0e10cSrcweir     {
1426cdf0e10cSrcweir         if ( ::sfx2::DocumentMacroMode::storageHasMacros( const_cast< ODatabaseModelImpl* >( this )->getOrCreateRootStorage() ) )
1427cdf0e10cSrcweir         {
1428cdf0e10cSrcweir             m_aEmbeddedMacros.reset( eDocumentWideMacros );
1429cdf0e10cSrcweir         }
1430cdf0e10cSrcweir         else if (   lcl_hasObjectsWithMacros_nothrow( const_cast< ODatabaseModelImpl& >( *this ), E_FORM )
1431cdf0e10cSrcweir                 ||  lcl_hasObjectsWithMacros_nothrow( const_cast< ODatabaseModelImpl& >( *this ), E_REPORT )
1432cdf0e10cSrcweir                 )
1433cdf0e10cSrcweir         {
1434cdf0e10cSrcweir             m_aEmbeddedMacros.reset( eSubDocumentMacros );
1435cdf0e10cSrcweir         }
1436cdf0e10cSrcweir         else
1437cdf0e10cSrcweir         {
1438cdf0e10cSrcweir             m_aEmbeddedMacros.reset( eNoMacros );
1439cdf0e10cSrcweir         }
1440cdf0e10cSrcweir     }
1441cdf0e10cSrcweir     return *m_aEmbeddedMacros;
1442cdf0e10cSrcweir }
1443cdf0e10cSrcweir 
1444cdf0e10cSrcweir // -----------------------------------------------------------------------------
documentStorageHasMacros() const1445cdf0e10cSrcweir sal_Bool ODatabaseModelImpl::documentStorageHasMacros() const
1446cdf0e10cSrcweir {
1447cdf0e10cSrcweir     const_cast< ODatabaseModelImpl* >( this )->determineEmbeddedMacros();
1448cdf0e10cSrcweir     return ( *m_aEmbeddedMacros != eNoMacros );
1449cdf0e10cSrcweir }
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir // -----------------------------------------------------------------------------
getEmbeddedDocumentScripts() const1452cdf0e10cSrcweir Reference< XEmbeddedScripts > ODatabaseModelImpl::getEmbeddedDocumentScripts() const
1453cdf0e10cSrcweir {
1454cdf0e10cSrcweir     return Reference< XEmbeddedScripts >( getModel_noCreate(), UNO_QUERY );
1455cdf0e10cSrcweir }
1456cdf0e10cSrcweir 
1457cdf0e10cSrcweir // -----------------------------------------------------------------------------
getScriptingSignatureState()1458cdf0e10cSrcweir sal_Int16 ODatabaseModelImpl::getScriptingSignatureState()
1459cdf0e10cSrcweir {
1460cdf0e10cSrcweir     // no support for signatures at the moment
1461cdf0e10cSrcweir     return SIGNATURESTATE_NOSIGNATURES;
1462cdf0e10cSrcweir }
1463cdf0e10cSrcweir 
1464cdf0e10cSrcweir // -----------------------------------------------------------------------------
hasTrustedScriptingSignature(sal_Bool)1465cdf0e10cSrcweir sal_Bool ODatabaseModelImpl::hasTrustedScriptingSignature( sal_Bool /*bAllowUIToAddAuthor*/ )
1466cdf0e10cSrcweir {
1467cdf0e10cSrcweir     // no support for signatures at the moment
1468cdf0e10cSrcweir     return sal_False;
1469cdf0e10cSrcweir }
1470cdf0e10cSrcweir 
1471cdf0e10cSrcweir // -----------------------------------------------------------------------------
showBrokenSignatureWarning(const Reference<XInteractionHandler> &) const1472cdf0e10cSrcweir void ODatabaseModelImpl::showBrokenSignatureWarning( const Reference< XInteractionHandler >& /*_rxInteraction*/ ) const
1473cdf0e10cSrcweir {
1474cdf0e10cSrcweir     OSL_ENSURE( false, "ODatabaseModelImpl::showBrokenSignatureWarning: signatures can't be broken - we do not support them!" );
1475cdf0e10cSrcweir }
1476cdf0e10cSrcweir 
1477cdf0e10cSrcweir // -----------------------------------------------------------------------------
storageIsModified()1478cdf0e10cSrcweir void ODatabaseModelImpl::storageIsModified()
1479cdf0e10cSrcweir {
1480cdf0e10cSrcweir     setModified( sal_True );
1481cdf0e10cSrcweir }
1482cdf0e10cSrcweir 
1483cdf0e10cSrcweir // -----------------------------------------------------------------------------
ModelDependentComponent(const::rtl::Reference<ODatabaseModelImpl> & _model)1484cdf0e10cSrcweir ModelDependentComponent::ModelDependentComponent( const ::rtl::Reference< ODatabaseModelImpl >& _model )
1485cdf0e10cSrcweir     :m_pImpl( _model )
1486cdf0e10cSrcweir     ,m_aMutex( _model->getSharedMutex() )
1487cdf0e10cSrcweir {
1488cdf0e10cSrcweir }
1489cdf0e10cSrcweir 
1490cdf0e10cSrcweir // -----------------------------------------------------------------------------
~ModelDependentComponent()1491cdf0e10cSrcweir ModelDependentComponent::~ModelDependentComponent()
1492cdf0e10cSrcweir {
1493cdf0e10cSrcweir }
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir //........................................................................
1496cdf0e10cSrcweir }	// namespace dbaccess
1497cdf0e10cSrcweir //........................................................................
1498cdf0e10cSrcweir 
1499