1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "core_resource.hxx"
28 #include "core_resource.hrc"
29 #include "datasource.hxx"
30 #include "databasedocument.hxx"
31 #include "dbastrings.hrc"
32 #include "module_dba.hxx"
33 #include "documenteventexecutor.hxx"
34 #include "databasecontext.hxx"
35 #include "documentcontainer.hxx"
36 #include "sdbcoretools.hxx"
37 #include "recovery/dbdocrecovery.hxx"
38 
39 /** === begin UNO includes === **/
40 #include <com/sun/star/beans/Optional.hpp>
41 #include <com/sun/star/document/XExporter.hpp>
42 #include <com/sun/star/document/XFilter.hpp>
43 #include <com/sun/star/document/XImporter.hpp>
44 #include <com/sun/star/embed/EntryInitModes.hpp>
45 #include <com/sun/star/embed/XEmbedPersist.hpp>
46 #include <com/sun/star/embed/XTransactedObject.hpp>
47 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
48 #include <com/sun/star/io/XActiveDataSource.hpp>
49 #include <com/sun/star/io/XSeekable.hpp>
50 #include <com/sun/star/io/XOutputStream.hpp>
51 #include <com/sun/star/io/XTruncate.hpp>
52 #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
53 #include <com/sun/star/task/ErrorCodeIOException.hpp>
54 #include <com/sun/star/task/XStatusIndicator.hpp>
55 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
56 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
57 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
58 #include <com/sun/star/view/XSelectionSupplier.hpp>
59 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
60 #include <com/sun/star/ucb/XContent.hpp>
61 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
62 /** === end UNO includes === **/
63 
64 #include <comphelper/documentconstants.hxx>
65 #include <comphelper/enumhelper.hxx>
66 #include <comphelper/genericpropertyset.hxx>
67 #include <comphelper/interaction.hxx>
68 #include <comphelper/mediadescriptor.hxx>
69 #include <comphelper/namedvaluecollection.hxx>
70 #include <comphelper/numberedcollection.hxx>
71 #include <comphelper/property.hxx>
72 #include <comphelper/storagehelper.hxx>
73 #include <comphelper/genericpropertyset.hxx>
74 #include <comphelper/property.hxx>
75 
76 #include <connectivity/dbtools.hxx>
77 
78 #include <cppuhelper/exc_hlp.hxx>
79 #include <framework/titlehelper.hxx>
80 #include <unotools/saveopt.hxx>
81 #include <tools/debug.hxx>
82 #include <tools/diagnose_ex.h>
83 #include <tools/errcode.hxx>
84 #include <tools/urlobj.hxx>
85 
86 #include <boost/bind.hpp>
87 
88 #include <algorithm>
89 #include <functional>
90 #include <list>
91 
92 #define MAP_LEN(x) x, sizeof(x) - 1
93 
94 #define MAP_LEN(x) x, sizeof(x) - 1
95 
96 using namespace ::com::sun::star::uno;
97 using namespace ::com::sun::star::beans;
98 using namespace ::com::sun::star::frame;
99 using namespace ::com::sun::star::lang;
100 using namespace ::com::sun::star::container;
101 using namespace ::com::sun::star::document;
102 using namespace ::com::sun::star::io;
103 using namespace ::com::sun::star::util;
104 using namespace ::com::sun::star::embed;
105 using namespace ::com::sun::star::task;
106 using namespace ::com::sun::star::view;
107 using namespace ::com::sun::star::sdbc;
108 using namespace ::com::sun::star;
109 using namespace ::com::sun::star::xml::sax;
110 using namespace ::com::sun::star::script;
111 using namespace ::com::sun::star::script::provider;
112 using namespace ::com::sun::star::ui;
113 using namespace ::cppu;
114 using namespace ::osl;
115 
116 using ::com::sun::star::awt::XWindow;
117 using ::com::sun::star::ucb::XContent;
118 using ::com::sun::star::sdb::application::XDatabaseDocumentUI;
119 
120 //........................................................................
121 namespace dbaccess
122 {
123 //........................................................................
124 
125 //============================================================
126 //= ViewMonitor
127 //============================================================
128 //--------------------------------------------------------------------------
onControllerConnected(const Reference<XController> & _rxController)129 bool ViewMonitor::onControllerConnected( const Reference< XController >& _rxController )
130 {
131     bool bFirstControllerEver = ( m_bEverHadController == false );
132     m_bEverHadController = true;
133 
134     m_xLastConnectedController = _rxController;
135     m_bLastIsFirstEverController = bFirstControllerEver;
136 
137     return bFirstControllerEver;
138 }
139 
140 //--------------------------------------------------------------------------
onSetCurrentController(const Reference<XController> & _rxController)141 bool ViewMonitor::onSetCurrentController( const Reference< XController >& _rxController )
142 {
143     // we interpret this as "loading the document (including UI) is finished",
144     // if and only if this is the controller which was last connected, and it was the
145     // first controller ever connected
146     bool bLoadFinished = ( _rxController == m_xLastConnectedController ) && m_bLastIsFirstEverController;
147 
148     // notify the respective events
149     if ( bLoadFinished )
150         m_rEventNotifier.notifyDocumentEventAsync( m_bIsNewDocument ? "OnNew" : "OnLoad" );
151 
152     return bLoadFinished;
153 }
154 
155 //============================================================
156 //= ODatabaseDocument
157 //============================================================
DBG_NAME(ODatabaseDocument)158 DBG_NAME(ODatabaseDocument)
159 //--------------------------------------------------------------------------
160 extern "C" void SAL_CALL createRegistryInfo_ODatabaseDocument()
161 {
162 	static ::dba::OAutoRegistration< ODatabaseDocument > aAutoRegistration;
163 }
164 
165 //--------------------------------------------------------------------------
ODatabaseDocument(const::rtl::Reference<ODatabaseModelImpl> & _pImpl)166 ODatabaseDocument::ODatabaseDocument(const ::rtl::Reference<ODatabaseModelImpl>& _pImpl	)
167             :ModelDependentComponent( _pImpl )
168             ,ODatabaseDocument_OfficeDocument( getMutex() )
169             ,m_aModifyListeners( getMutex() )
170 			,m_aCloseListener( getMutex() )
171             ,m_aStorageListeners( getMutex() )
172             ,m_pEventContainer( new DocumentEvents( *this, getMutex(), _pImpl->getDocumentEvents() ) )
173             ,m_pEventExecutor( NULL )   // initialized below, ref-count-protected
174             ,m_aEventNotifier( *this, getMutex() )
175             ,m_aViewMonitor( m_aEventNotifier )
176             ,m_eInitState( NotInitialized )
177             ,m_bClosing( false )
178             ,m_bAllowDocumentScripting( false )
179             ,m_bHasBeenRecovered( false )
180 {
181 	DBG_CTOR(ODatabaseDocument,NULL);
182     OSL_TRACE( "DD: ctor: %p: %p", this, m_pImpl.get() );
183 
184     osl_incrementInterlockedCount( &m_refCount );
185     {
186         impl_reparent_nothrow( m_xForms );
187         impl_reparent_nothrow( m_xReports );
188         impl_reparent_nothrow( m_pImpl->m_xTableDefinitions );
189         impl_reparent_nothrow( m_pImpl->m_xCommandDefinitions );
190 
191         m_pEventExecutor = new DocumentEventExecutor( m_pImpl->m_aContext, this );
192     }
193     osl_decrementInterlockedCount( &m_refCount );
194 
195     // if there previously was a document instance for the same Impl which was already initialized,
196     // then consider ourself initialized, too.
197     // #i94840#
198     if ( m_pImpl->hadInitializedDocument() )
199     {
200         // Note we set our init-state to "Initializing", not "Initialized". We're created from inside the ModelImpl,
201         // which is expected to call attachResource in case there was a previous incarnation of the document,
202         // so we can properly finish our initialization then.
203         impl_setInitializing();
204 
205         if ( m_pImpl->getURL().getLength() )
206         {
207             // if the previous incarnation of the DatabaseDocument already had an URL, then creating this incarnation
208             // here is effectively loading the document.
209             // #i105505# / 2009-10-01 / frank.schoenheit@sun.com
210             m_aViewMonitor.onLoadedDocument();
211         }
212     }
213 }
214 
215 //--------------------------------------------------------------------------
~ODatabaseDocument()216 ODatabaseDocument::~ODatabaseDocument()
217 {
218     OSL_TRACE( "DD: dtor: %p: %p", this, m_pImpl.get() );
219 	DBG_DTOR(ODatabaseDocument,NULL);
220 	if ( !ODatabaseDocument_OfficeDocument::rBHelper.bInDispose && !ODatabaseDocument_OfficeDocument::rBHelper.bDisposed )
221 	{
222 		acquire();
223 		dispose();
224 	}
225 
226     delete m_pEventContainer, m_pEventContainer = NULL;
227 }
228 // -----------------------------------------------------------------------------
queryInterface(const Type & _rType)229 Any SAL_CALL ODatabaseDocument::queryInterface( const Type& _rType ) throw (RuntimeException)
230 {
231     // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
232     // which already contains macros. In this case, the database document itself is not
233     // allowed to contain macros, too.
234     if  (   !m_bAllowDocumentScripting
235         &&  (   _rType.equals( XEmbeddedScripts::static_type() )
236             ||  _rType.equals( XScriptInvocationContext::static_type() )
237             )
238         )
239         return Any();
240 
241 	Any aReturn = ODatabaseDocument_OfficeDocument::queryInterface(_rType);
242 	if (!aReturn.hasValue())
243 		aReturn = ODatabaseDocument_Title::queryInterface(_rType);
244 	return aReturn;
245 }
246 //------------------------------------------------------------------------------
acquire()247 void SAL_CALL ODatabaseDocument::acquire(  ) throw ()
248 {
249 	ODatabaseDocument_OfficeDocument::acquire();
250 }
251 
252 //------------------------------------------------------------------------------
release()253 void SAL_CALL ODatabaseDocument::release(  ) throw ()
254 {
255 	ODatabaseDocument_OfficeDocument::release();
256 }
257 //------------------------------------------------------------------------------
getTypes()258 Sequence< Type > SAL_CALL ODatabaseDocument::getTypes(	) throw (RuntimeException)
259 {
260 	Sequence< Type > aTypes = ::comphelper::concatSequences(
261 		ODatabaseDocument_OfficeDocument::getTypes(),
262 		ODatabaseDocument_Title::getTypes()
263 	);
264 
265     // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
266     // which already contains macros. In this case, the database document itself is not
267     // allowed to contain macros, too.
268     if ( !m_bAllowDocumentScripting )
269     {
270         Sequence< Type > aStrippedTypes( aTypes.getLength() );
271         Type* pStripTo( aStrippedTypes.getArray() );
272 
273         // strip XEmbeddedScripts, and immediately re-assign to aTypes
274         aTypes = Sequence< Type >(
275             pStripTo,
276             ::std::remove_copy_if(
277                 aTypes.getConstArray(),
278                 aTypes.getConstArray() + aTypes.getLength(),
279                 pStripTo,
280                 ::std::bind2nd( ::std::equal_to< Type >(), XEmbeddedScripts::static_type() )
281             ) - pStripTo
282         );
283 
284         // strip XScriptInvocationContext, and immediately re-assign to aTypes
285         aTypes = Sequence< Type >(
286             pStripTo,
287             ::std::remove_copy_if(
288                 aTypes.getConstArray(),
289                 aTypes.getConstArray() + aTypes.getLength(),
290                 pStripTo,
291                 ::std::bind2nd( ::std::equal_to< Type >(), XScriptInvocationContext::static_type() )
292             ) - pStripTo
293         );
294     }
295 
296     return aTypes;
297 }
298 //------------------------------------------------------------------------------
getImplementationId()299 Sequence< sal_Int8 > SAL_CALL ODatabaseDocument::getImplementationId(  ) throw (RuntimeException)
300 {
301 	static ::cppu::OImplementationId * pId = 0;
302 	if (! pId)
303 	{
304 		::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
305 		if (! pId)
306 		{
307 			static ::cppu::OImplementationId aId;
308 			pId = &aId;
309 		}
310 	}
311 	return pId->getImplementationId();
312 }
313 
314 // -----------------------------------------------------------------------------
315 // local functions
316 // -----------------------------------------------------------------------------
317 namespace
318 {
319     // -----------------------------------------------------------------------------
lcl_extractStatusIndicator(const::comphelper::NamedValueCollection & _rArguments)320     Reference< XStatusIndicator > lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments )
321     {
322         Reference< XStatusIndicator > xStatusIndicator;
323         return _rArguments.getOrDefault( "StatusIndicator", xStatusIndicator );
324     }
325 
326     // -----------------------------------------------------------------------------
lcl_triggerStatusIndicator_throw(const::comphelper::NamedValueCollection & _rArguments,DocumentGuard & _rGuard,const bool _bStart)327     static void lcl_triggerStatusIndicator_throw( const ::comphelper::NamedValueCollection& _rArguments, DocumentGuard& _rGuard, const bool _bStart )
328     {
329         Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
330         if ( !xStatusIndicator.is() )
331             return;
332 
333         _rGuard.clear();
334         try
335         {
336             if ( _bStart )
337                 xStatusIndicator->start( ::rtl::OUString(), (sal_Int32)1000000 );
338             else
339                 xStatusIndicator->end();
340         }
341         catch( const Exception& )
342         {
343         	DBG_UNHANDLED_EXCEPTION();
344         }
345         _rGuard.reset();
346             // note that |reset| can throw a DisposedException
347     }
348 
349     // -----------------------------------------------------------------------------
lcl_extractStatusIndicator(const::comphelper::NamedValueCollection & _rArguments,Sequence<Any> & _rCallArgs)350     static void lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Sequence< Any >& _rCallArgs )
351     {
352         Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
353         if ( !xStatusIndicator.is() )
354             return;
355 
356         sal_Int32 nLength = _rCallArgs.getLength();
357         _rCallArgs.realloc( nLength + 1 );
358         _rCallArgs[ nLength ] <<= xStatusIndicator;
359     }
360 
361     // -----------------------------------------------------------------------------
lcl_extractAndStartStatusIndicator(const::comphelper::NamedValueCollection & _rArguments,Reference<XStatusIndicator> & _rxStatusIndicator,Sequence<Any> & _rCallArgs)362     static void lcl_extractAndStartStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Reference< XStatusIndicator >& _rxStatusIndicator,
363         Sequence< Any >& _rCallArgs )
364     {
365         _rxStatusIndicator = lcl_extractStatusIndicator( _rArguments );
366         if ( !_rxStatusIndicator.is() )
367             return;
368 
369         try
370         {
371 	        _rxStatusIndicator->start( ::rtl::OUString(), (sal_Int32)1000000 );
372 
373             sal_Int32 nLength = _rCallArgs.getLength();
374             _rCallArgs.realloc( nLength + 1 );
375             _rCallArgs[ nLength ] <<= _rxStatusIndicator;
376         }
377         catch( const Exception& )
378         {
379             DBG_UNHANDLED_EXCEPTION();
380         }
381     }
382 
383     // -----------------------------------------------------------------------------
lcl_appendFileNameToDescriptor(const::comphelper::NamedValueCollection & _rDescriptor,const::rtl::OUString _rURL)384     static Sequence< PropertyValue > lcl_appendFileNameToDescriptor( const ::comphelper::NamedValueCollection& _rDescriptor, const ::rtl::OUString _rURL )
385     {
386         ::comphelper::NamedValueCollection aMutableDescriptor( _rDescriptor );
387         if ( _rURL.getLength() )
388         {
389             aMutableDescriptor.put( "FileName", _rURL );
390             aMutableDescriptor.put( "URL", _rURL );
391         }
392         return aMutableDescriptor.getPropertyValues();
393     }
394 }
395 
396 // -----------------------------------------------------------------------------
impl_setInitialized()397 void ODatabaseDocument::impl_setInitialized()
398 {
399     m_eInitState = Initialized;
400 
401     // start event notifications
402     m_aEventNotifier.onDocumentInitialized();
403 }
404 
405 // -----------------------------------------------------------------------------
impl_reset_nothrow()406 void ODatabaseDocument::impl_reset_nothrow()
407 {
408     try
409 	{
410 		m_pImpl->clearConnections();
411         m_pImpl->disposeStorages();
412         m_pImpl->resetRootStroage();
413 
414         clearObjectContainer( m_xForms );
415 		clearObjectContainer( m_xReports );
416 		clearObjectContainer( m_pImpl->m_xTableDefinitions );
417 		clearObjectContainer( m_pImpl->m_xCommandDefinitions );
418 
419         m_eInitState = NotInitialized;
420 
421 		m_pImpl->reset();
422 	}
423 	catch(const Exception&)
424 	{
425         DBG_UNHANDLED_EXCEPTION();
426 	}
427 	m_pImpl->m_bDocumentReadOnly = sal_False;
428 }
429 
430 // -----------------------------------------------------------------------------
impl_import_nolck_throw(const::comphelper::ComponentContext _rContext,const Reference<XInterface> & _rxTargetComponent,const::comphelper::NamedValueCollection & _rResource)431 void ODatabaseDocument::impl_import_nolck_throw( const ::comphelper::ComponentContext _rContext, const Reference< XInterface >& _rxTargetComponent,
432                                                  const ::comphelper::NamedValueCollection& _rResource )
433 {
434 	Sequence< Any > aFilterCreationArgs;
435     Reference< XStatusIndicator > xStatusIndicator;
436     lcl_extractAndStartStatusIndicator( _rResource, xStatusIndicator, aFilterCreationArgs );
437 
438     /** property map for import info set */
439 	comphelper::PropertyMapEntry aExportInfoMap[] =
440  	{
441         { MAP_LEN( "BaseURI"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
442         { MAP_LEN( "StreamName"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
443   		{ NULL, 0, 0, NULL, 0, 0 }
444  	};
445  	uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
446     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BaseURI")), uno::makeAny(_rResource.getOrDefault("URL",::rtl::OUString())));
447     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamName")), uno::makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml"))));
448 
449     const sal_Int32 nCount = aFilterCreationArgs.getLength();
450     aFilterCreationArgs.realloc(nCount + 1);
451     aFilterCreationArgs[nCount] <<= xInfoSet;
452 
453     Reference< XImporter > xImporter(
454         _rContext.createComponentWithArguments( "com.sun.star.comp.sdb.DBFilter", aFilterCreationArgs ),
455         UNO_QUERY_THROW );
456 
457 	Reference< XComponent > xComponent( _rxTargetComponent, UNO_QUERY_THROW );
458 	xImporter->setTargetDocument( xComponent );
459 
460     Reference< XFilter > xFilter( xImporter, UNO_QUERY_THROW );
461     Sequence< PropertyValue > aFilterArgs( ODatabaseModelImpl::stripLoadArguments( _rResource ).getPropertyValues() );
462 	xFilter->filter( aFilterArgs );
463 
464     if ( xStatusIndicator.is() )
465         xStatusIndicator->end();
466 }
467 
468 // -----------------------------------------------------------------------------
initNew()469 void SAL_CALL ODatabaseDocument::initNew(  ) throw (DoubleInitializationException, IOException, Exception, RuntimeException)
470 {
471     // SYNCHRONIZED ->
472     DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
473 
474     impl_reset_nothrow();
475 
476     impl_setInitializing();
477 
478     // create a temporary storage
479     Reference< XStorage > xTempStor( ::comphelper::OStorageHelper::GetTemporaryStorage(
480         m_pImpl->m_aContext.getLegacyServiceFactory() ) );
481 
482     // store therein
483     impl_storeToStorage_throw( xTempStor, Sequence< PropertyValue >(), aGuard );
484 
485     // let the impl know we're now based on this storage
486     m_pImpl->switchToStorage( xTempStor );
487 
488     // for the newly created document, allow document-wide scripting
489     m_bAllowDocumentScripting = true;
490 
491     impl_setInitialized();
492 
493     m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
494 
495     impl_setModified_nothrow( sal_False, aGuard );
496     // <- SYNCHRONIZED
497 
498     m_aEventNotifier.notifyDocumentEvent( "OnCreate" );
499 
500     impl_notifyStorageChange_nolck_nothrow( xTempStor );
501 }
502 
503 // -----------------------------------------------------------------------------
load(const Sequence<PropertyValue> & _Arguments)504 void SAL_CALL ODatabaseDocument::load( const Sequence< PropertyValue >& _Arguments ) throw (DoubleInitializationException, IOException, Exception, RuntimeException)
505 {
506     // SYNCHRONIZED ->
507     DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
508 
509     impl_reset_nothrow();
510 
511     ::comphelper::NamedValueCollection aResource( _Arguments );
512     if ( aResource.has( "FileName" ) && !aResource.has( "URL" ) )
513         // FileName is the compatibility name for URL, so we might have clients passing
514         // a FileName only. However, some of our code works with the URL only, so ensure
515         // we have one.
516         aResource.put( "URL", aResource.get( "FileName" ) );
517     if ( aResource.has( "URL" ) && !aResource.has( "FileName" ) )
518         // similar ... just in case there is legacy code which expects a FileName only
519         aResource.put( "FileName", aResource.get( "URL" ) );
520 
521     // now that somebody (perhaps) told us an macro execution mode, remember it as
522     // ImposedMacroExecMode
523     m_pImpl->setImposedMacroExecMode(
524         aResource.getOrDefault( "MacroExecutionMode", m_pImpl->getImposedMacroExecMode() ) );
525 
526     impl_setInitializing();
527     try
528     {
529         aGuard.clear();
530         impl_import_nolck_throw( m_pImpl->m_aContext, *this, aResource );
531         aGuard.reset();
532     }
533     catch( const Exception& )
534     {
535         impl_reset_nothrow();
536         throw;
537     }
538     // tell our view monitor that the document has been loaded - this way it will fire the proper
539     // event (OnLoad instead of OnCreate) later on
540     m_aViewMonitor.onLoadedDocument();
541 
542     // note that we do *not* call impl_setInitialized() here: The initialization is only complete
543     // when the XModel::attachResource has been called, not sooner.
544 
545     impl_setModified_nothrow( sal_False, aGuard );
546     // <- SYNCHRONIZED
547 }
548 
549 // -----------------------------------------------------------------------------
550 namespace
551 {
552     // .........................................................................
lcl_hasAnyModifiedSubComponent_throw(const Reference<XController> & i_rController)553     bool lcl_hasAnyModifiedSubComponent_throw( const Reference< XController >& i_rController )
554     {
555         Reference< XDatabaseDocumentUI > xDatabaseUI( i_rController, UNO_QUERY_THROW );
556 
557         Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
558         const Reference< XComponent >* component = aComponents.getConstArray();
559         const Reference< XComponent >* componentsEnd = aComponents.getConstArray() + aComponents.getLength();
560 
561         bool isAnyModified = false;
562         for ( ; component != componentsEnd; ++component )
563         {
564             Reference< XModifiable > xModify( *component, UNO_QUERY );
565             if ( xModify.is() )
566             {
567                 isAnyModified = xModify->isModified();
568                 continue;
569             }
570 
571             // TODO: clarify: anything else to care for? Both the sub componbents with and without model
572             // should support the XModifiable interface, so I think nothing more is needed here.
573             OSL_ENSURE( false, "lcl_hasAnyModifiedSubComponent_throw: anything left to do here?" );
574         }
575 
576         return isAnyModified;
577     }
578 }
579 
580 // -----------------------------------------------------------------------------
wasModifiedSinceLastSave()581 ::sal_Bool SAL_CALL ODatabaseDocument::wasModifiedSinceLastSave() throw ( RuntimeException )
582 {
583     DocumentGuard aGuard( *this );
584 
585     // The implementation here is somewhat sloppy, in that it returns whether *any* part of the whole
586     // database document, including opened sub components, is modified. This is more than what is requested:
587     // We need to return <TRUE/> if the doc itself, or any of the opened sub components, has been modified
588     // since the last call to any of the save* methods, or since the document has been loaded/created.
589     // However, the API definition explicitly allows to be that sloppy ...
590 
591     if ( isModified() )
592         return sal_True;
593 
594     // auto recovery is an "UI feature", it is to restore the UI the user knows. Thus,
595     // we ask our connected controllers, not simply our existing form/report definitions.
596     // (There is some information which even cannot be obtained without asking the controller.
597     // For instance, newly created, but not yet saved, forms/reports are acessible via the
598     // controller only, but not via the model.)
599 
600     try
601     {
602         for (   Controllers::const_iterator ctrl = m_aControllers.begin();
603                 ctrl != m_aControllers.end();
604                 ++ctrl
605             )
606         {
607             if ( lcl_hasAnyModifiedSubComponent_throw( *ctrl ) )
608                 return sal_True;
609         }
610     }
611     catch( const Exception& )
612     {
613     	DBG_UNHANDLED_EXCEPTION();
614     }
615 
616     return sal_False;
617 }
618 
619 // -----------------------------------------------------------------------------
storeToRecoveryFile(const::rtl::OUString & i_TargetLocation,const Sequence<PropertyValue> & i_MediaDescriptor)620 void SAL_CALL ODatabaseDocument::storeToRecoveryFile( const ::rtl::OUString& i_TargetLocation, const Sequence< PropertyValue >& i_MediaDescriptor ) throw ( RuntimeException, IOException, WrappedTargetException )
621 {
622     DocumentGuard aGuard( *this );
623     ModifyLock aLock( *this );
624 
625     try
626     {
627         // create a storage for the target location
628         Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( i_TargetLocation ) );
629 
630         // first store the document as a whole into this storage
631         impl_storeToStorage_throw( xTargetStorage, i_MediaDescriptor, aGuard );
632 
633         // save the sub components which need saving
634         DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext);
635         aDocRecovery.saveModifiedSubComponents( xTargetStorage, m_aControllers );
636 
637         // commit the root storage
638         tools::stor::commitStorageIfWriteable( xTargetStorage );
639     }
640     catch( const Exception& )
641     {
642         Any aError = ::cppu::getCaughtException();
643         if  (   aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
644             ||  aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
645             ||  aError.isExtractableTo( ::cppu::UnoType< WrappedTargetException >::get() )
646             )
647         {
648             // allowed to leave
649             throw;
650         }
651 
652         throw WrappedTargetException( ::rtl::OUString(), *this, aError );
653     }
654 }
655 
656 // -----------------------------------------------------------------------------
recoverFromFile(const::rtl::OUString & i_SourceLocation,const::rtl::OUString & i_SalvagedFile,const Sequence<PropertyValue> & i_MediaDescriptor)657 void SAL_CALL ODatabaseDocument::recoverFromFile( const ::rtl::OUString& i_SourceLocation, const ::rtl::OUString& i_SalvagedFile, const Sequence< PropertyValue >& i_MediaDescriptor ) throw ( RuntimeException, IOException, WrappedTargetException )
658 {
659     DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
660 
661     if ( i_SourceLocation.getLength() == 0 )
662         throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
663 
664     try
665     {
666         // load the document itself, by simply delegating to our "load" method
667 
668         // our load implementation expects the SalvagedFile and URL to be in the media descriptor
669         ::comphelper::NamedValueCollection aMediaDescriptor( i_MediaDescriptor );
670         aMediaDescriptor.put( "SalvagedFile", i_SalvagedFile );
671         aMediaDescriptor.put( "URL", i_SourceLocation );
672 
673         aGuard.clear(); // (load has an own guarding scheme)
674         load( aMediaDescriptor.getPropertyValues() );
675 
676         // Without a controller, we are unable to recover the sub components, as they're always tied to a controller.
677         // So, everything else is done when the first controller is connected.
678         m_bHasBeenRecovered = true;
679 
680         // tell the impl that we've been loaded from the given location
681         m_pImpl->setDocFileLocation( i_SourceLocation );
682 
683         // by definition (of XDocumentRecovery), we're responsible for delivering a fully-initialized document,
684         // which includes an attachResource call.
685         const ::rtl::OUString sLogicalDocumentURL( i_SalvagedFile.getLength() ? i_SalvagedFile : i_SourceLocation );
686         impl_attachResource( sLogicalDocumentURL, aMediaDescriptor.getPropertyValues(), aGuard );
687         // <- SYNCHRONIZED
688     }
689     catch( const Exception& )
690     {
691         Any aError = ::cppu::getCaughtException();
692         if  (   aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
693             ||  aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
694             ||  aError.isExtractableTo( ::cppu::UnoType< WrappedTargetException >::get() )
695             )
696         {
697             // allowed to leave
698             throw;
699         }
700 
701         throw WrappedTargetException( ::rtl::OUString(), *this, aError );
702     }
703 }
704 
705 // -----------------------------------------------------------------------------
706 // XModel
attachResource(const::rtl::OUString & _rURL,const Sequence<PropertyValue> & _rArguments)707 sal_Bool SAL_CALL ODatabaseDocument::attachResource( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (RuntimeException)
708 {
709     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
710     return impl_attachResource( _rURL, _rArguments, aGuard );
711 }
712 
713 // -----------------------------------------------------------------------------
impl_attachResource(const::rtl::OUString & i_rLogicalDocumentURL,const Sequence<PropertyValue> & i_rMediaDescriptor,DocumentGuard & _rDocGuard)714 sal_Bool ODatabaseDocument::impl_attachResource( const ::rtl::OUString& i_rLogicalDocumentURL,
715             const Sequence< PropertyValue >& i_rMediaDescriptor, DocumentGuard& _rDocGuard )
716 {
717     if  (   ( i_rLogicalDocumentURL == getURL() )
718         &&  ( i_rMediaDescriptor.getLength() == 1 )
719         &&  ( i_rMediaDescriptor[0].Name.compareToAscii( "BreakMacroSignature" ) == 0 )
720         )
721     {
722         // this is a BAD hack of the Basic importer code ... there should be a dedicated API for this,
723         // not this bad mis-using of existing interfaces
724         return sal_False;
725             // (we do not support macro signatures, so we can ignore this call)
726     }
727 
728     // if no URL has been provided, the caller was lazy enough to not call our getURL - which is not allowed anymore,
729     // now since getURL and getLocation both return the same, so calling one of those should be simple.
730     ::rtl::OUString sDocumentURL( i_rLogicalDocumentURL );
731     OSL_ENSURE( sDocumentURL.getLength(), "ODatabaseDocument::impl_attachResource: invalid URL!" );
732     if ( !sDocumentURL.getLength() )
733         sDocumentURL = getURL();
734 
735     m_pImpl->setResource( sDocumentURL, i_rMediaDescriptor );
736 
737     if ( impl_isInitializing() )
738     {   // this means we've just been loaded, and this is the attachResource call which follows
739         // the load call.
740         impl_setInitialized();
741 
742         // determine whether the document as a whole, or sub documents, have macros. Especially the latter
743         // controls the availability of our XEmbeddedScripts and XScriptInvocationContext interfaces, and we
744         // should know this before anybody actually uses the object.
745         m_bAllowDocumentScripting = ( m_pImpl->determineEmbeddedMacros() != ODatabaseModelImpl::eSubDocumentMacros );
746 
747         _rDocGuard.clear();
748         // <- SYNCHRONIZED
749         m_aEventNotifier.notifyDocumentEvent( "OnLoadFinished" );
750     }
751 
752     return sal_True;
753 }
754 
755 // -----------------------------------------------------------------------------
getURL()756 ::rtl::OUString SAL_CALL ODatabaseDocument::getURL(  ) throw (RuntimeException)
757 {
758     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
759     return m_pImpl->getURL();
760 }
761 
762 // -----------------------------------------------------------------------------
getArgs()763 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getArgs(  ) throw (RuntimeException)
764 {
765     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
766     return m_pImpl->getMediaDescriptor().getPropertyValues();
767 }
768 
769 // -----------------------------------------------------------------------------
connectController(const Reference<XController> & _xController)770 void SAL_CALL ODatabaseDocument::connectController( const Reference< XController >& _xController ) throw (RuntimeException)
771 {
772     DocumentGuard aGuard( *this );
773 
774 #if OSL_DEBUG_LEVEL > 0
775     for (   Controllers::const_iterator controller = m_aControllers.begin();
776             controller != m_aControllers.end();
777             ++controller
778         )
779     {
780         OSL_ENSURE( *controller != _xController, "ODatabaseDocument::connectController: this controller is already connected!" );
781     }
782 #endif
783 
784     m_aControllers.push_back( _xController );
785 
786     m_aEventNotifier.notifyDocumentEventAsync( "OnViewCreated", Reference< XController2 >( _xController, UNO_QUERY ) );
787 
788     bool bFirstControllerEver = m_aViewMonitor.onControllerConnected( _xController );
789     if ( !bFirstControllerEver )
790         return;
791 
792     // check/adjust our macro mode.
793     m_pImpl->checkMacrosOnLoading();
794 }
795 
796 // -----------------------------------------------------------------------------
disconnectController(const Reference<XController> & _xController)797 void SAL_CALL ODatabaseDocument::disconnectController( const Reference< XController >& _xController ) throw (RuntimeException)
798 {
799     bool bNotifyViewClosed = false;
800     bool bLastControllerGone = false;
801     bool bIsClosing = false;
802 
803     // SYNCHRONIZED ->
804     {
805         DocumentGuard aGuard( *this );
806 
807         Controllers::iterator pos = ::std::find( m_aControllers.begin(), m_aControllers.end(), _xController );
808         OSL_ENSURE( pos != m_aControllers.end(), "ODatabaseDocument::disconnectController: don't know this controller!" );
809         if ( pos != m_aControllers.end() )
810         {
811             m_aControllers.erase( pos );
812             bNotifyViewClosed = true;
813         }
814 
815 	    if ( m_xCurrentController == _xController )
816 		    m_xCurrentController = NULL;
817 
818         bLastControllerGone = m_aControllers.empty();
819         bIsClosing = m_bClosing;
820     }
821     // <- SYNCHRONIZED
822 
823     if ( bNotifyViewClosed )
824         m_aEventNotifier.notifyDocumentEvent( "OnViewClosed", Reference< XController2 >( _xController, UNO_QUERY ) );
825 
826     if ( bLastControllerGone && !bIsClosing )
827     {
828         // if this was the last view, close the document as a whole
829         // #i51157# / 2006-03-16 / frank.schoenheit@sun.com
830         try
831         {
832             close( sal_True );
833         }
834         catch( const CloseVetoException& )
835         {
836             // okay, somebody vetoed and took ownership
837         }
838     }
839 }
840 
841 // -----------------------------------------------------------------------------
lockControllers()842 void SAL_CALL ODatabaseDocument::lockControllers(  ) throw (RuntimeException)
843 {
844     DocumentGuard aGuard( *this );
845 
846     ++m_pImpl->m_nControllerLockCount;
847 }
848 
849 // -----------------------------------------------------------------------------
unlockControllers()850 void SAL_CALL ODatabaseDocument::unlockControllers(  ) throw (RuntimeException)
851 {
852     DocumentGuard aGuard( *this );
853 
854 	--m_pImpl->m_nControllerLockCount;
855 }
856 
857 // -----------------------------------------------------------------------------
hasControllersLocked()858 sal_Bool SAL_CALL ODatabaseDocument::hasControllersLocked(  ) throw (RuntimeException)
859 {
860     DocumentGuard aGuard( *this );
861 
862 	return m_pImpl->m_nControllerLockCount != 0;
863 }
864 
865 // -----------------------------------------------------------------------------
getCurrentController()866 Reference< XController > SAL_CALL ODatabaseDocument::getCurrentController() throw (RuntimeException)
867 {
868     DocumentGuard aGuard( *this );
869 
870 	return m_xCurrentController.is() ? m_xCurrentController : ( m_aControllers.empty() ? Reference< XController >() : *m_aControllers.begin() );
871 }
872 
873 // -----------------------------------------------------------------------------
setCurrentController(const Reference<XController> & _xController)874 void SAL_CALL ODatabaseDocument::setCurrentController( const Reference< XController >& _xController ) throw (NoSuchElementException, RuntimeException)
875 {
876     DocumentGuard aGuard( *this );
877 
878 	m_xCurrentController = _xController;
879 
880     if ( !m_aViewMonitor.onSetCurrentController( _xController ) )
881         return;
882 
883     // check if there are sub components to recover from our document storage
884     bool bAttemptRecovery = m_bHasBeenRecovered;
885     if ( !bAttemptRecovery && m_pImpl->getMediaDescriptor().has( "ForceRecovery" ) )
886         // do not use getOrDefault, it will throw for invalid types, which is not desired here
887         m_pImpl->getMediaDescriptor().get( "ForceRecovery" ) >>= bAttemptRecovery;
888 
889     if ( !bAttemptRecovery )
890         return;
891 
892     try
893     {
894         DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext );
895         aDocRecovery.recoverSubDocuments( m_pImpl->getRootStorage(), _xController );
896     }
897     catch( const Exception& )
898     {
899     	DBG_UNHANDLED_EXCEPTION();
900     }
901 }
902 
903 // -----------------------------------------------------------------------------
getCurrentSelection()904 Reference< XInterface > SAL_CALL ODatabaseDocument::getCurrentSelection(  ) throw (RuntimeException)
905 {
906     DocumentGuard aGuard( *this );
907 
908 	Reference< XInterface > xRet;
909 	Reference< XSelectionSupplier >  xDocView( getCurrentController(), UNO_QUERY );
910 	if ( xDocView.is() )
911 		xRet.set(xDocView->getSelection(),UNO_QUERY);
912 
913 	return xRet;
914 }
915 // -----------------------------------------------------------------------------
916 
917 // XStorable
hasLocation()918 sal_Bool SAL_CALL ODatabaseDocument::hasLocation(  ) throw (RuntimeException)
919 {
920     return getLocation().getLength() > 0;
921 }
922 // -----------------------------------------------------------------------------
getLocation()923 ::rtl::OUString SAL_CALL ODatabaseDocument::getLocation(  ) throw (RuntimeException)
924 {
925     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
926     return m_pImpl->getURL();
927         // both XStorable::getLocation and XModel::getURL have to return the URL of the document, *not*
928         // the location of the file which the docunment was possibly recovered from (which would be getDocFileLocation)
929 }
930 // -----------------------------------------------------------------------------
isReadonly()931 sal_Bool SAL_CALL ODatabaseDocument::isReadonly(  ) throw (RuntimeException)
932 {
933     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
934 	return m_pImpl->m_bDocumentReadOnly;
935 }
936 // -----------------------------------------------------------------------------
store()937 void SAL_CALL ODatabaseDocument::store(  ) throw (IOException, RuntimeException)
938 {
939     DocumentGuard aGuard( *this );
940 
941     ::rtl::OUString sDocumentURL( m_pImpl->getURL() );
942     if ( sDocumentURL.getLength() )
943     {
944         if ( m_pImpl->getDocFileLocation() == m_pImpl->getURL() )
945             if ( m_pImpl->m_bDocumentReadOnly )
946                 throw IOException();
947 
948         impl_storeAs_throw( m_pImpl->getURL(), m_pImpl->getMediaDescriptor(), SAVE, aGuard );
949         return;
950     }
951 
952     // if we have no URL, but did survive the DocumentGuard above, then we've been inited via XLoadable::initNew,
953     // i.e. we're based on a temporary storage
954     OSL_ENSURE( m_pImpl->getDocFileLocation().getLength() == 0, "ODatabaseDocument::store: unexpected URL inconsistency!" );
955 
956     try
957     {
958         impl_storeToStorage_throw( m_pImpl->getRootStorage(), m_pImpl->getMediaDescriptor().getPropertyValues(), aGuard );
959     }
960     catch( const Exception& )
961     {
962         Any aError = ::cppu::getCaughtException();
963         if  (   aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
964             ||  aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
965             )
966         {
967             // allowed to leave
968             throw;
969         }
970         impl_throwIOExceptionCausedBySave_throw( aError, ::rtl::OUString() );
971     }
972 }
973 
974 // -----------------------------------------------------------------------------
impl_throwIOExceptionCausedBySave_throw(const Any & i_rError,const::rtl::OUString & i_rTargetURL) const975 void ODatabaseDocument::impl_throwIOExceptionCausedBySave_throw( const Any& i_rError, const ::rtl::OUString& i_rTargetURL ) const
976 {
977     ::rtl::OUString sErrorMessage = extractExceptionMessage( m_pImpl->m_aContext, i_rError );
978     sErrorMessage = ResourceManager::loadString(
979         RID_STR_ERROR_WHILE_SAVING,
980         "$location$", i_rTargetURL,
981         "$message$", sErrorMessage
982     );
983     throw IOException( sErrorMessage, *const_cast< ODatabaseDocument* >( this ) );
984 }
985 
986 // -----------------------------------------------------------------------------
impl_storeAs_throw(const::rtl::OUString & _rURL,const::comphelper::NamedValueCollection & _rArguments,const StoreType _eType,DocumentGuard & _rGuard)987 void ODatabaseDocument::impl_storeAs_throw( const ::rtl::OUString& _rURL, const ::comphelper::NamedValueCollection& _rArguments,
988     const StoreType _eType, DocumentGuard& _rGuard ) throw ( IOException, RuntimeException )
989 {
990     OSL_PRECOND( ( _eType == SAVE ) || ( _eType == SAVE_AS ),
991         "ODatabaseDocument::impl_storeAs_throw: you introduced a new type which cannot be handled here!" );
992 
993     // if we're in the process of initializing the document (which effectively means it is an implicit
994     // initialization triggered in storeAsURL), the we do not notify events, since to an observer, the SaveAs
995     // should not be noticable
996     bool bIsInitializationProcess = impl_isInitializing();
997 
998     if ( !bIsInitializationProcess )
999     {
1000         _rGuard.clear();
1001     	m_aEventNotifier.notifyDocumentEvent( _eType == SAVE ? "OnSave" : "OnSaveAs", NULL, makeAny( _rURL ) );
1002         _rGuard.reset();
1003     }
1004 
1005     Reference< XStorage > xNewRootStorage;
1006         // will be non-NULL if our storage changed
1007 
1008     try
1009     {
1010         ModifyLock aLock( *this );
1011             // ignore all changes of our "modified" state during storing
1012 
1013         sal_Bool bLocationChanged = ( _rURL != m_pImpl->getDocFileLocation() );
1014 	    if ( bLocationChanged )
1015 	    {
1016             // create storage for target URL
1017             Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( _rURL ) );
1018 
1019             if ( m_pImpl->isEmbeddedDatabase() )
1020                 m_pImpl->clearConnections();
1021 
1022             // commit everything
1023             m_pImpl->commitEmbeddedStorage();
1024             m_pImpl->commitStorages();
1025 
1026             // copy own storage to target storage
1027             Reference< XStorage > xCurrentStorage( m_pImpl->getRootStorage() );
1028             if ( xCurrentStorage.is() )
1029                 xCurrentStorage->copyToStorage( xTargetStorage );
1030 
1031             m_pImpl->disposeStorages();
1032 
1033             // each and every document definition obtained via m_xForms and m_xReports depends
1034             // on the sub storages which we just disposed. So, dispose the forms/reports collections, too.
1035             // This ensures that they're re-created when needed.
1036             clearObjectContainer( m_xForms );
1037             clearObjectContainer( m_xReports );
1038 
1039             xNewRootStorage = m_pImpl->switchToStorage( xTargetStorage );
1040 
1041             m_pImpl->m_bDocumentReadOnly = sal_False;
1042 	    }
1043 
1044         // store to current storage
1045         Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_QUERY_THROW );
1046         Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) );
1047         impl_storeToStorage_throw( xCurrentStorage, aMediaDescriptor, _rGuard );
1048 
1049         // success - tell our impl
1050         m_pImpl->setDocFileLocation( _rURL );
1051         m_pImpl->setResource( _rURL, aMediaDescriptor );
1052 
1053         // if we are in an initialization process, then this is finished, now that we stored the document
1054         if ( bIsInitializationProcess )
1055             impl_setInitialized();
1056     }
1057     catch( const Exception& )
1058     {
1059         Any aError = ::cppu::getCaughtException();
1060 
1061         // notify the failure
1062         if ( !bIsInitializationProcess )
1063             m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveFailed" : "OnSaveAsFailed", NULL, makeAny( _rURL ) );
1064 
1065         if  (   aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
1066             ||  aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
1067             )
1068         {
1069             // allowed to leave
1070             throw;
1071         }
1072 
1073         impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1074     }
1075 
1076     // notify the document event
1077     if ( !bIsInitializationProcess )
1078         m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveDone" : "OnSaveAsDone", NULL, makeAny( _rURL ) );
1079 
1080     // reset our "modified" flag, and clear the guard
1081     impl_setModified_nothrow( sal_False, _rGuard );
1082     // <- SYNCHRONIZED
1083 
1084     // notify storage listeners
1085     if ( xNewRootStorage.is() )
1086         impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1087 }
1088 
1089 // -----------------------------------------------------------------------------
impl_createStorageFor_throw(const::rtl::OUString & _rURL) const1090 Reference< XStorage > ODatabaseDocument::impl_createStorageFor_throw( const ::rtl::OUString& _rURL ) const
1091 {
1092 	Reference < ::com::sun::star::ucb::XSimpleFileAccess > xTempAccess;
1093 	m_pImpl->m_aContext.createComponent( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ,xTempAccess);
1094 	Reference< io::XStream > xStream = xTempAccess->openFileReadWrite( _rURL );
1095 	Reference< io::XTruncate > xTruncate(xStream,UNO_QUERY);
1096 	if ( xTruncate.is() )
1097 	{
1098 		xTruncate->truncate();
1099 	}
1100     Sequence<Any> aParam(2);
1101 	aParam[0] <<= xStream;
1102 	aParam[1] <<= ElementModes::READWRITE | ElementModes::TRUNCATE;
1103 
1104     Reference< XSingleServiceFactory > xStorageFactory( m_pImpl->createStorageFactory(), UNO_SET_THROW );
1105 	return Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aParam ), UNO_QUERY_THROW );
1106 }
1107 
1108 // -----------------------------------------------------------------------------
storeAsURL(const::rtl::OUString & _rURL,const Sequence<PropertyValue> & _rArguments)1109 void SAL_CALL ODatabaseDocument::storeAsURL( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (IOException, RuntimeException)
1110 {
1111     // SYNCHRONIZED ->
1112     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1113 
1114     // Normally, a document initialization is done via XLoadable::load or XLoadable::initNew. For convenience
1115     // reasons, and to not break existing API clients, it's allowed to call storeAsURL without having initialized
1116     // the document, in which case the initialization will be done implicitly.
1117     bool bImplicitInitialization = !impl_isInitialized();
1118     // implicit initialization while another initialization is just running is not possible
1119     if ( bImplicitInitialization && impl_isInitializing() )
1120         throw DoubleInitializationException();
1121 
1122     if ( bImplicitInitialization )
1123         impl_setInitializing();
1124 
1125     try
1126     {
1127         impl_storeAs_throw( _rURL, _rArguments, SAVE_AS, aGuard );
1128         // <- SYNCHRONIZED
1129 
1130         // impl_storeAs_throw cleared the lock on our mutex, but the below lines need this lock
1131         // SYNCHRONIZED ->
1132         aGuard.reset();
1133 
1134         // our title might have changed, potentially at least
1135         // Sadly, we cannot check this: Calling getTitle here and now would not deliver
1136         // an up-to-date result, as the call is delegated to our TitleHelper instance, which itself
1137         // updates its title only if it gets the OnSaveAsDone event (which was sent asynchronously
1138         // by impl_storeAs_throw). So, we simply notify always, and also asynchronously
1139         m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
1140     }
1141     catch( const Exception& )
1142     {
1143         impl_reset_nothrow();
1144         throw;
1145     }
1146 
1147     if ( bImplicitInitialization )
1148         m_bAllowDocumentScripting = true;
1149 
1150     aGuard.clear();
1151     // <- SYNCHRONIZED
1152 
1153     if ( bImplicitInitialization )
1154         m_aEventNotifier.notifyDocumentEvent( "OnCreate" );
1155 }
1156 
1157 // -----------------------------------------------------------------------------
impl_storeToStorage_throw(const Reference<XStorage> & _rxTargetStorage,const Sequence<PropertyValue> & _rMediaDescriptor,DocumentGuard & _rDocGuard) const1158 void ODatabaseDocument::impl_storeToStorage_throw( const Reference< XStorage >& _rxTargetStorage, const Sequence< PropertyValue >& _rMediaDescriptor,
1159                                                    DocumentGuard& _rDocGuard ) const
1160 {
1161     if ( !_rxTargetStorage.is() )
1162         throw IllegalArgumentException( ::rtl::OUString(), *const_cast< ODatabaseDocument* >( this ), 1 );
1163 
1164     if ( !m_pImpl.is() )
1165         throw DisposedException( ::rtl::OUString(), *const_cast< ODatabaseDocument* >( this ) );
1166 
1167 	try
1168 	{
1169         // commit everything
1170 	    m_pImpl->commitEmbeddedStorage();
1171         m_pImpl->commitStorages();
1172 
1173         // copy own storage to target storage
1174         if ( impl_isInitialized() )
1175         {
1176             Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_QUERY_THROW );
1177             if ( xCurrentStorage != _rxTargetStorage )
1178     	        xCurrentStorage->copyToStorage( _rxTargetStorage );
1179         }
1180 
1181         // write into target storage
1182 	    ::comphelper::NamedValueCollection aWriteArgs( _rMediaDescriptor );
1183         lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, true );
1184         impl_writeStorage_throw( _rxTargetStorage, aWriteArgs );
1185         lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, false );
1186 
1187         // commit target storage
1188         OSL_VERIFY( tools::stor::commitStorageIfWriteable( _rxTargetStorage ) );
1189 	}
1190     catch( const IOException& ) { throw; }
1191     catch( const RuntimeException& ) { throw; }
1192 	catch ( const Exception& e )
1193 	{
1194 		throw IOException( e.Message, *const_cast< ODatabaseDocument* >( this ) );
1195 	}
1196 }
1197 
1198 // -----------------------------------------------------------------------------
storeToURL(const::rtl::OUString & _rURL,const Sequence<PropertyValue> & _rArguments)1199 void SAL_CALL ODatabaseDocument::storeToURL( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (IOException, RuntimeException)
1200 {
1201     DocumentGuard aGuard( *this );
1202     ModifyLock aLock( *this );
1203 
1204     {
1205         aGuard.clear();
1206         m_aEventNotifier.notifyDocumentEvent( "OnSaveTo", NULL, makeAny( _rURL ) );
1207         aGuard.reset();
1208     }
1209 
1210     try
1211     {
1212         // create storage for target URL
1213         Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( _rURL ) );
1214 
1215         // extend media descriptor with URL
1216         Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) );
1217 
1218         // store to this storage
1219         impl_storeToStorage_throw( xTargetStorage, aMediaDescriptor, aGuard );
1220     }
1221     catch( const Exception& )
1222     {
1223         Any aError = ::cppu::getCaughtException();
1224         m_aEventNotifier.notifyDocumentEventAsync( "OnSaveToFailed", NULL, aError );
1225 
1226         if  (   aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
1227             ||  aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
1228             )
1229         {
1230             // allowed to leave
1231             throw;
1232         }
1233 
1234         impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1235     }
1236 
1237     m_aEventNotifier.notifyDocumentEventAsync( "OnSaveToDone", NULL, makeAny( _rURL ) );
1238 }
1239 
1240 // -----------------------------------------------------------------------------
1241 // XModifyBroadcaster
addModifyListener(const Reference<XModifyListener> & _xListener)1242 void SAL_CALL ODatabaseDocument::addModifyListener( const Reference< XModifyListener >& _xListener ) throw (RuntimeException)
1243 {
1244     DocumentGuard aGuard( *this );
1245 	m_aModifyListeners.addInterface(_xListener);
1246 }
1247 
1248 // -----------------------------------------------------------------------------
removeModifyListener(const Reference<XModifyListener> & _xListener)1249 void SAL_CALL ODatabaseDocument::removeModifyListener( const Reference< XModifyListener >& _xListener ) throw (RuntimeException)
1250 {
1251     DocumentGuard aGuard( *this );
1252 	m_aModifyListeners.removeInterface(_xListener);
1253 }
1254 
1255 // -----------------------------------------------------------------------------
1256 // XModifiable
isModified()1257 sal_Bool SAL_CALL ODatabaseDocument::isModified(  ) throw (RuntimeException)
1258 {
1259     DocumentGuard aGuard( *this );
1260 
1261 	return m_pImpl->m_bModified;
1262 }
1263 
1264 // -----------------------------------------------------------------------------
setModified(sal_Bool _bModified)1265 void SAL_CALL ODatabaseDocument::setModified( sal_Bool _bModified ) throw (PropertyVetoException, RuntimeException)
1266 {
1267     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1268     if ( impl_isInitialized() )
1269         impl_setModified_nothrow( _bModified, aGuard );
1270     // it's allowed to call setModified without the document being initialized already. In this case,
1271     // we simply ignore the call - when the initialization is finished, the respective code will set
1272     // a proper "modified" flag
1273 }
1274 
1275 // -----------------------------------------------------------------------------
impl_setModified_nothrow(sal_Bool _bModified,DocumentGuard & _rGuard)1276 void ODatabaseDocument::impl_setModified_nothrow( sal_Bool _bModified, DocumentGuard& _rGuard )
1277 {
1278     // SYNCHRONIZED ->
1279     bool bModifiedChanged = ( m_pImpl->m_bModified != _bModified ) && ( !m_pImpl->isModifyLocked() );
1280 
1281     if ( bModifiedChanged )
1282     {
1283         m_pImpl->m_bModified = _bModified;
1284         m_aEventNotifier.notifyDocumentEventAsync( "OnModifyChanged" );
1285     }
1286     _rGuard.clear();
1287     // <- SYNCHRONIZED
1288 
1289     if ( bModifiedChanged )
1290     {
1291         lang::EventObject aEvent( *this );
1292         m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
1293     }
1294 }
1295 
1296 // -----------------------------------------------------------------------------
1297 // ::com::sun::star::document::XEventBroadcaster
addEventListener(const uno::Reference<document::XEventListener> & _Listener)1298 void SAL_CALL ODatabaseDocument::addEventListener(const uno::Reference< document::XEventListener >& _Listener ) throw (uno::RuntimeException)
1299 {
1300     m_aEventNotifier.addLegacyEventListener( _Listener );
1301 }
1302 
1303 // -----------------------------------------------------------------------------
removeEventListener(const uno::Reference<document::XEventListener> & _Listener)1304 void SAL_CALL ODatabaseDocument::removeEventListener( const uno::Reference< document::XEventListener >& _Listener ) throw (uno::RuntimeException)
1305 {
1306     m_aEventNotifier.removeLegacyEventListener( _Listener );
1307 }
1308 
1309 // -----------------------------------------------------------------------------
addDocumentEventListener(const Reference<XDocumentEventListener> & _Listener)1310 void SAL_CALL ODatabaseDocument::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener ) throw (RuntimeException)
1311 {
1312     m_aEventNotifier.addDocumentEventListener( _Listener );
1313 }
1314 
1315 // -----------------------------------------------------------------------------
removeDocumentEventListener(const Reference<XDocumentEventListener> & _Listener)1316 void SAL_CALL ODatabaseDocument::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener ) throw (RuntimeException)
1317 {
1318     m_aEventNotifier.removeDocumentEventListener( _Listener );
1319 }
1320 
1321 // -----------------------------------------------------------------------------
notifyDocumentEvent(const::rtl::OUString & _EventName,const Reference<XController2> & _ViewController,const Any & _Supplement)1322 void SAL_CALL ODatabaseDocument::notifyDocumentEvent( const ::rtl::OUString& _EventName, const Reference< XController2 >& _ViewController, const Any& _Supplement ) throw (IllegalArgumentException, NoSupportException, RuntimeException)
1323 {
1324     if ( !_EventName.getLength() )
1325         throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
1326 
1327     // SYNCHRONIZED ->
1328     DocumentGuard aGuard( *this );
1329 
1330     if ( !DocumentEvents::needsSynchronousNotification( _EventName ) )
1331     {
1332         m_aEventNotifier.notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
1333         return;
1334     }
1335     aGuard.clear();
1336     // <- SYNCHRONIZED
1337 
1338     m_aEventNotifier.notifyDocumentEvent( _EventName, _ViewController, _Supplement );
1339 }
1340 
1341 // -----------------------------------------------------------------------------
getPrinter()1342 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getPrinter(  ) throw (RuntimeException)
1343 {
1344     DBG_ERROR( "ODatabaseDocument::getPrinter: not supported!" );
1345 	return Sequence< PropertyValue >();
1346 }
1347 
1348 // -----------------------------------------------------------------------------
setPrinter(const Sequence<PropertyValue> &)1349 void SAL_CALL ODatabaseDocument::setPrinter( const Sequence< PropertyValue >& /*aPrinter*/ ) throw (IllegalArgumentException, RuntimeException)
1350 {
1351     DBG_ERROR( "ODatabaseDocument::setPrinter: not supported!" );
1352 }
1353 
1354 // -----------------------------------------------------------------------------
print(const Sequence<PropertyValue> &)1355 void SAL_CALL ODatabaseDocument::print( const Sequence< PropertyValue >& /*xOptions*/ ) throw (IllegalArgumentException, RuntimeException)
1356 {
1357     DBG_ERROR( "ODatabaseDocument::print: not supported!" );
1358 }
1359 
1360 // -----------------------------------------------------------------------------
impl_reparent_nothrow(const WeakReference<XNameAccess> & _rxContainer)1361 void ODatabaseDocument::impl_reparent_nothrow( const WeakReference< XNameAccess >& _rxContainer )
1362 {
1363 	Reference< XChild > xChild( _rxContainer.get(), UNO_QUERY );
1364 	if  ( xChild.is() )
1365 		xChild->setParent( *this );
1366 }
1367 // -----------------------------------------------------------------------------
clearObjectContainer(WeakReference<XNameAccess> & _rxContainer)1368 void ODatabaseDocument::clearObjectContainer( WeakReference< XNameAccess >& _rxContainer)
1369 {
1370     Reference< XNameAccess > xContainer = _rxContainer;
1371     ::comphelper::disposeComponent( xContainer );
1372 
1373 	Reference< XChild > xChild( _rxContainer.get(),UNO_QUERY );
1374 	if ( xChild.is() )
1375 		xChild->setParent( NULL );
1376     _rxContainer = Reference< XNameAccess >();
1377 }
1378 // -----------------------------------------------------------------------------
impl_getDocumentContainer_throw(ODatabaseModelImpl::ObjectType _eType)1379 Reference< XNameAccess > ODatabaseDocument::impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType _eType )
1380 {
1381     if ( ( _eType != ODatabaseModelImpl::E_FORM ) && ( _eType != ODatabaseModelImpl::E_REPORT ) )
1382         throw IllegalArgumentException();
1383 
1384     bool bFormsContainer = _eType == ODatabaseModelImpl::E_FORM;
1385 
1386     WeakReference< XNameAccess >& rContainerRef( bFormsContainer ? m_xForms : m_xReports );
1387     Reference< XNameAccess > xContainer = rContainerRef;
1388 	if ( !xContainer.is() )
1389 	{
1390         Any aValue;
1391         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > xMy(*this);
1392         if ( dbtools::getDataSourceSetting(xMy,bFormsContainer ? "Forms" : "Reports",aValue) )
1393         {
1394             ::rtl::OUString sSupportService;
1395             aValue >>= sSupportService;
1396             if ( sSupportService.getLength() )
1397             {
1398                 Sequence<Any> aArgs(1);
1399                 aArgs[0] <<= NamedValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DatabaseDocument")),makeAny(xMy));
1400                 xContainer.set(m_pImpl->m_aContext.createComponentWithArguments(sSupportService,aArgs),UNO_QUERY);
1401                 rContainerRef = xContainer;
1402             }
1403         }
1404         if ( !xContainer.is() )
1405         {
1406             TContentPtr& rContainerData( m_pImpl->getObjectContainer( _eType ) );
1407             rContainerRef = xContainer = new ODocumentContainer( m_pImpl->m_aContext.getLegacyServiceFactory(), *this, rContainerData, bFormsContainer );
1408         }
1409         impl_reparent_nothrow( xContainer );
1410 	}
1411 	return xContainer;
1412 }
1413 
1414 // -----------------------------------------------------------------------------
impl_closeControllerFrames_nolck_throw(sal_Bool _bDeliverOwnership)1415 void ODatabaseDocument::impl_closeControllerFrames_nolck_throw( sal_Bool _bDeliverOwnership )
1416 {
1417     Controllers aCopy = m_aControllers;
1418 
1419     Controllers::iterator aEnd = aCopy.end();
1420 	for ( Controllers::iterator aIter = aCopy.begin(); aIter != aEnd ; ++aIter )
1421 	{
1422 		if ( !aIter->is() )
1423             continue;
1424 
1425         try
1426         {
1427 			Reference< XCloseable> xFrame( (*aIter)->getFrame(), UNO_QUERY );
1428 			if ( xFrame.is() )
1429 				xFrame->close( _bDeliverOwnership );
1430         }
1431         catch( const CloseVetoException& ) { throw; }
1432         catch( const Exception& )
1433         {
1434             DBG_UNHANDLED_EXCEPTION();
1435         }
1436 	}
1437 }
1438 
1439 // -----------------------------------------------------------------------------
1440 struct DisposeControllerFrame : public ::std::unary_function< Reference< XController >, void >
1441 {
operator ()dbaccess::DisposeControllerFrame1442     void operator()( const Reference< XController >& _rxController ) const
1443     {
1444         try
1445         {
1446             if ( !_rxController.is() )
1447                 return;
1448 
1449             Reference< XFrame > xFrame( _rxController->getFrame() );
1450             ::comphelper::disposeComponent( xFrame );
1451         }
1452         catch( const Exception& )
1453         {
1454         	DBG_UNHANDLED_EXCEPTION();
1455         }
1456     };
1457 };
1458 
1459 // -----------------------------------------------------------------------------
impl_disposeControllerFrames_nothrow()1460 void ODatabaseDocument::impl_disposeControllerFrames_nothrow()
1461 {
1462     Controllers aCopy;
1463     aCopy.swap( m_aControllers );   // ensure m_aControllers is empty afterwards
1464     ::std::for_each( aCopy.begin(), aCopy.end(), DisposeControllerFrame() );
1465 }
1466 
1467 // -----------------------------------------------------------------------------
close(sal_Bool _bDeliverOwnership)1468 void SAL_CALL ODatabaseDocument::close( sal_Bool _bDeliverOwnership ) throw (CloseVetoException, RuntimeException)
1469 {
1470     // nearly everything below can/must be done without our mutex locked, the below is just for
1471     // the checks for being disposed and the like
1472     // SYNCHRONIZED ->
1473     {
1474         DocumentGuard aGuard( *this );
1475         m_bClosing = true;
1476     }
1477     // <- SYNCHRONIZED
1478 
1479     try
1480     {
1481         // allow listeners to veto
1482         lang::EventObject aEvent( *this );
1483         m_aCloseListener.forEach< XCloseListener >(
1484             boost::bind( &XCloseListener::queryClosing, _1, boost::cref( aEvent ), boost::cref( _bDeliverOwnership ) ) );
1485 
1486         // notify that we're going to unload
1487 	    m_aEventNotifier.notifyDocumentEvent( "OnPrepareUnload" );
1488 
1489         impl_closeControllerFrames_nolck_throw( _bDeliverOwnership );
1490 
1491         m_aCloseListener.notifyEach( &XCloseListener::notifyClosing, (const lang::EventObject&)aEvent );
1492 
1493         dispose();
1494     }
1495     catch ( const Exception& )
1496     {
1497         ::osl::MutexGuard aGuard( m_aMutex );
1498         m_bClosing = false;
1499         throw;
1500     }
1501 
1502     // SYNCHRONIZED ->
1503     ::osl::MutexGuard aGuard( m_aMutex );
1504     m_bClosing = false;
1505     // <- SYNCHRONIZED
1506 }
1507 // -----------------------------------------------------------------------------
addCloseListener(const Reference<::com::sun::star::util::XCloseListener> & Listener)1508 void SAL_CALL ODatabaseDocument::addCloseListener( const Reference< ::com::sun::star::util::XCloseListener >& Listener ) throw (RuntimeException)
1509 {
1510     DocumentGuard aGuard( *this );
1511 	m_aCloseListener.addInterface(Listener);
1512 }
1513 // -----------------------------------------------------------------------------
removeCloseListener(const Reference<::com::sun::star::util::XCloseListener> & Listener)1514 void SAL_CALL ODatabaseDocument::removeCloseListener( const Reference< ::com::sun::star::util::XCloseListener >& Listener ) throw (RuntimeException)
1515 {
1516     DocumentGuard aGuard( *this );
1517     m_aCloseListener.removeInterface(Listener);
1518 }
1519 // -----------------------------------------------------------------------------
getFormDocuments()1520 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getFormDocuments(  ) throw (RuntimeException)
1521 {
1522     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1523     return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_FORM );
1524 }
1525 // -----------------------------------------------------------------------------
getReportDocuments()1526 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getReportDocuments(  ) throw (RuntimeException)
1527 {
1528     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1529     return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_REPORT );
1530 }
1531 
1532 // -----------------------------------------------------------------------------
WriteThroughComponent(const Reference<XComponent> & xComponent,const sal_Char * pStreamName,const sal_Char * pServiceName,const Sequence<Any> & _rArguments,const Sequence<PropertyValue> & rMediaDesc,const Reference<XStorage> & _xStorageToSaveTo) const1533 void ODatabaseDocument::WriteThroughComponent( const Reference< XComponent >& xComponent, const sal_Char* pStreamName,
1534 	const sal_Char* pServiceName, const Sequence< Any >& _rArguments, const Sequence< PropertyValue >& rMediaDesc,
1535 	const Reference<XStorage>& _xStorageToSaveTo ) const
1536 {
1537 	OSL_ENSURE( pStreamName, "Need stream name!" );
1538 	OSL_ENSURE( pServiceName, "Need service name!" );
1539 
1540     // open stream
1541     ::rtl::OUString sStreamName = ::rtl::OUString::createFromAscii( pStreamName );
1542     Reference< XStream > xStream = _xStorageToSaveTo->openStreamElement( sStreamName, ElementModes::READWRITE | ElementModes::TRUNCATE );
1543     if ( !xStream.is() )
1544         return;
1545 
1546     Reference< XOutputStream > xOutputStream( xStream->getOutputStream() );
1547     OSL_ENSURE( xOutputStream.is(), "Can't create output stream in package!" );
1548     if ( !xOutputStream.is() )
1549         return;
1550 
1551     Reference< XSeekable > xSeek( xOutputStream, UNO_QUERY );
1552     if ( xSeek.is() )
1553         xSeek->seek(0);
1554 
1555     Reference< XPropertySet > xStreamProp( xOutputStream, UNO_QUERY_THROW );
1556     xStreamProp->setPropertyValue( INFO_MEDIATYPE, makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "text/xml" ) ) ) );
1557     xStreamProp->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ), makeAny( (sal_Bool)sal_True ) );
1558 
1559 	// write the stuff
1560 	WriteThroughComponent( xOutputStream, xComponent, pServiceName, _rArguments, rMediaDesc );
1561 }
1562 
WriteThroughComponent(const Reference<XOutputStream> & xOutputStream,const Reference<XComponent> & xComponent,const sal_Char * pServiceName,const Sequence<Any> & _rArguments,const Sequence<PropertyValue> & rMediaDesc) const1563 void ODatabaseDocument::WriteThroughComponent( const Reference< XOutputStream >& xOutputStream,
1564     const Reference< XComponent >& xComponent, const sal_Char* pServiceName, const Sequence< Any >& _rArguments,
1565 	const Sequence< PropertyValue >& rMediaDesc ) const
1566 {
1567 	OSL_ENSURE( xOutputStream.is(), "I really need an output stream!" );
1568 	OSL_ENSURE( xComponent.is(), "Need component!" );
1569 	OSL_ENSURE( NULL != pServiceName, "Need component name!" );
1570 
1571 	// get component
1572 	Reference< XActiveDataSource > xSaxWriter;
1573     OSL_VERIFY( m_pImpl->m_aContext.createComponent( "com.sun.star.xml.sax.Writer", xSaxWriter ) );
1574 	if ( !xSaxWriter.is() )
1575 		return;
1576 
1577 	// connect XML writer to output stream
1578 	xSaxWriter->setOutputStream( xOutputStream );
1579 
1580 	// prepare arguments (prepend doc handler to given arguments)
1581 	Reference< XDocumentHandler > xDocHandler( xSaxWriter,UNO_QUERY);
1582 	Sequence<Any> aArgs( 1 + _rArguments.getLength() );
1583 	aArgs[0] <<= xDocHandler;
1584 	for ( sal_Int32 i = 0; i < _rArguments.getLength(); ++i )
1585 		aArgs[ i+1 ] = _rArguments[i];
1586 
1587 	// get filter component
1588 	Reference< XExporter > xExporter;
1589     OSL_VERIFY( m_pImpl->m_aContext.createComponentWithArguments( pServiceName, aArgs, xExporter ) );
1590 	if ( !xExporter.is() )
1591 		return;
1592 
1593 	// connect model and filter
1594 	xExporter->setSourceDocument( xComponent );
1595 
1596 	// filter
1597     Reference< XFilter > xFilter( xExporter, UNO_QUERY_THROW );
1598 	xFilter->filter( rMediaDesc );
1599 }
1600 
1601 // -----------------------------------------------------------------------------
impl_writeStorage_throw(const Reference<XStorage> & _rxTargetStorage,const::comphelper::NamedValueCollection & _rMediaDescriptor) const1602 void ODatabaseDocument::impl_writeStorage_throw( const Reference< XStorage >& _rxTargetStorage, const ::comphelper::NamedValueCollection& _rMediaDescriptor ) const
1603 {
1604 	// extract status indicator
1605     Sequence< Any > aDelegatorArguments;
1606     lcl_extractStatusIndicator( _rMediaDescriptor, aDelegatorArguments );
1607 
1608     /** property map for export info set */
1609 	comphelper::PropertyMapEntry aExportInfoMap[] =
1610 	{
1611         { MAP_LEN( "BaseURI"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
1612         { MAP_LEN( "StreamName"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
1613 		{ MAP_LEN( "UsePrettyPrinting" ), 0, &::getCppuType((sal_Bool*)0), beans::PropertyAttribute::MAYBEVOID, 0},
1614 		{ NULL, 0, 0, NULL, 0, 0 }
1615 	};
1616 	uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
1617 
1618     SvtSaveOptions aSaveOpt;
1619     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting")), uno::makeAny(aSaveOpt.IsPrettyPrinting()));
1620     if ( aSaveOpt.IsSaveRelFSys() )
1621         xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BaseURI")), uno::makeAny(_rMediaDescriptor.getOrDefault("URL",::rtl::OUString())));
1622 
1623     ::rtl::OUString aVersion;
1624     SvtSaveOptions::ODFDefaultVersion nDefVersion = aSaveOpt.GetODFDefaultVersion();
1625 
1626     // older versions can not have this property set, it exists only starting from ODF1.2
1627     if ( nDefVersion >= SvtSaveOptions::ODFVER_012 )
1628         aVersion = ODFVER_012_TEXT;
1629 
1630     if ( aVersion.getLength() )
1631     {
1632         try
1633         {
1634             xInfoSet->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Version" )), uno::makeAny( aVersion ) );
1635         }
1636         catch( uno::Exception& )
1637         {
1638         }
1639     }
1640 
1641     sal_Int32 nArgsLen = aDelegatorArguments.getLength();
1642     aDelegatorArguments.realloc(nArgsLen+1);
1643     aDelegatorArguments[nArgsLen++] <<= xInfoSet;
1644 
1645 	Reference< XPropertySet > xProp( _rxTargetStorage, UNO_QUERY_THROW );
1646     xProp->setPropertyValue( INFO_MEDIATYPE, makeAny( (rtl::OUString)MIMETYPE_OASIS_OPENDOCUMENT_DATABASE ) );
1647 
1648 	Reference< XComponent > xComponent( *const_cast< ODatabaseDocument* >( this ), UNO_QUERY_THROW );
1649 
1650     Sequence< PropertyValue > aMediaDescriptor;
1651     _rMediaDescriptor >>= aMediaDescriptor;
1652 
1653     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamName")), uno::makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("settings.xml"))));
1654     WriteThroughComponent( xComponent, "settings.xml", "com.sun.star.comp.sdb.XMLSettingsExporter",
1655         aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1656 
1657     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamName")), uno::makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml"))));
1658     WriteThroughComponent( xComponent, "content.xml", "com.sun.star.comp.sdb.DBExportFilter",
1659         aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1660 
1661     m_pImpl->storeLibraryContainersTo( _rxTargetStorage );
1662 }
1663 
1664 // -----------------------------------------------------------------------------
getUIConfigurationManager()1665 Reference< XUIConfigurationManager > SAL_CALL ODatabaseDocument::getUIConfigurationManager(  ) throw (RuntimeException)
1666 {
1667     DocumentGuard aGuard( *this );
1668 
1669 	if ( !m_xUIConfigurationManager.is() )
1670     {
1671         m_pImpl->m_aContext.createComponent( "com.sun.star.ui.UIConfigurationManager", m_xUIConfigurationManager );
1672         Reference< XUIConfigurationStorage > xUIConfigStorage( m_xUIConfigurationManager, UNO_QUERY );
1673         if ( xUIConfigStorage.is() )
1674         {
1675             rtl::OUString aUIConfigFolderName( RTL_CONSTASCII_USTRINGPARAM( "Configurations2" ));
1676             Reference< XStorage > xConfigStorage;
1677 
1678             // First try to open with READWRITE and then READ
1679             xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READWRITE );
1680             if ( xConfigStorage.is() )
1681             {
1682                 rtl::OUString aUIConfigMediaType( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.xml.ui.configuration" ));
1683                 rtl::OUString aMediaType;
1684 				Reference< XPropertySet > xPropSet( xConfigStorage, UNO_QUERY );
1685                 Any a = xPropSet->getPropertyValue( INFO_MEDIATYPE );
1686                 if ( !( a >>= aMediaType ) || ( aMediaType.getLength() == 0 ))
1687                 {
1688                     a <<= aUIConfigMediaType;
1689                     xPropSet->setPropertyValue( INFO_MEDIATYPE, a );
1690                 }
1691             }
1692             else
1693                 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READ );
1694 
1695             // initialize ui configuration manager with document substorage
1696             xUIConfigStorage->setStorage( xConfigStorage );
1697         }
1698     }
1699 
1700     return m_xUIConfigurationManager;
1701 }
1702 // -----------------------------------------------------------------------------
getDocumentSubStorage(const::rtl::OUString & aStorageName,sal_Int32 nMode)1703 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentSubStorage( const ::rtl::OUString& aStorageName, sal_Int32 nMode ) throw (RuntimeException)
1704 {
1705     DocumentGuard aGuard( *this );
1706 
1707     Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1708     return xStorageAccess->getDocumentSubStorage( aStorageName, nMode );
1709 }
1710 // -----------------------------------------------------------------------------
getDocumentSubStoragesNames()1711 Sequence< ::rtl::OUString > SAL_CALL ODatabaseDocument::getDocumentSubStoragesNames(  ) throw (::com::sun::star::io::IOException, RuntimeException)
1712 {
1713     Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1714     return xStorageAccess->getDocumentSubStoragesNames();
1715 }
1716 
1717 //------------------------------------------------------------------------------
impl_notifyStorageChange_nolck_nothrow(const Reference<XStorage> & _rxNewRootStorage)1718 void ODatabaseDocument::impl_notifyStorageChange_nolck_nothrow( const Reference< XStorage >& _rxNewRootStorage )
1719 {
1720     Reference< XInterface > xMe( *const_cast< ODatabaseDocument* >( this ) );
1721 
1722     m_aStorageListeners.forEach< XStorageChangeListener >(
1723         boost::bind( &XStorageChangeListener::notifyStorageChange, _1, boost::cref( xMe ), boost::cref( _rxNewRootStorage ) ) );
1724 }
1725 
1726 //------------------------------------------------------------------------------
disposing()1727 void ODatabaseDocument::disposing()
1728 {
1729     OSL_TRACE( "DD: disp: %p: %p", this, m_pImpl.get() );
1730     if ( !m_pImpl.is() )
1731     {
1732         // this means that we're already disposed
1733         DBG_ASSERT( ODatabaseDocument_OfficeDocument::rBHelper.bDisposed, "ODatabaseDocument::disposing: no impl anymore, but not yet disposed!" );
1734         return;
1735     }
1736 
1737     if ( impl_isInitialized() )
1738 	    m_aEventNotifier.notifyDocumentEvent( "OnUnload" );
1739 
1740     Reference< XModel > xHoldAlive( this );
1741 
1742     m_aEventNotifier.disposing();
1743 
1744     lang::EventObject aDisposeEvent(static_cast<XWeak*>(this));
1745     m_aModifyListeners.disposeAndClear( aDisposeEvent );
1746     m_aCloseListener.disposeAndClear( aDisposeEvent );
1747     m_aStorageListeners.disposeAndClear( aDisposeEvent );
1748 
1749     // this is the list of objects which we currently hold as member. Upon resetting
1750     // those members, we can (potentially) release the last reference to them, in which
1751     // case they will be deleted - if they're C++ implementations, that is :).
1752     // Some of those implementations are offending enough to require the SolarMutex, which
1753     // means we should not release the last reference while our own mutex is locked ...
1754     ::std::list< Reference< XInterface > > aKeepAlive;
1755 
1756     // SYNCHRONIZED ->
1757     ::osl::ClearableMutexGuard aGuard( m_aMutex );
1758 
1759     DBG_ASSERT( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1760         // normally, nobody should explicitly dispose, but only XCloseable::close the document. And upon
1761         // closing, our controllers are closed, too
1762 
1763     aKeepAlive.push_back( m_xUIConfigurationManager );
1764     m_xUIConfigurationManager = NULL;
1765 
1766     clearObjectContainer( m_xForms );
1767 	clearObjectContainer( m_xReports );
1768 
1769     // reset the macro mode: in case the our impl struct stays alive (e.g. because our DataSource
1770     // object still exists), and somebody subsequently re-opens the document, we want to have
1771     // the security warning, again.
1772     m_pImpl->resetMacroExecutionMode();
1773 
1774     // similar argueing for our ViewMonitor
1775     m_aViewMonitor.reset();
1776 
1777     // tell our Impl to forget us
1778     m_pImpl->modelIsDisposing( impl_isInitialized(), ODatabaseModelImpl::ResetModelAccess() );
1779 
1780     // now, at the latest, the controller array should be empty. Controllers are
1781     // expected to listen for our disposal, and disconnect then
1782     DBG_ASSERT( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1783     impl_disposeControllerFrames_nothrow();
1784 
1785     aKeepAlive.push_back( m_xModuleManager );
1786     m_xModuleManager.clear();
1787 
1788     aKeepAlive.push_back( m_xTitleHelper );
1789     m_xTitleHelper.clear();
1790 
1791 	m_pImpl.clear();
1792 
1793     aGuard.clear();
1794     // <- SYNCHRONIZED
1795 
1796     aKeepAlive.clear();
1797 }
1798 // -----------------------------------------------------------------------------
1799 // XComponent
dispose()1800 void SAL_CALL ODatabaseDocument::dispose(  ) throw (RuntimeException)
1801 {
1802     ::cppu::WeakComponentImplHelperBase::dispose();
1803 }
1804 // -----------------------------------------------------------------------------
addEventListener(const Reference<lang::XEventListener> & _xListener)1805 void SAL_CALL ODatabaseDocument::addEventListener( const Reference< lang::XEventListener >& _xListener ) throw (RuntimeException)
1806 {
1807 	::cppu::WeakComponentImplHelperBase::addEventListener( _xListener );
1808 }
1809 // -----------------------------------------------------------------------------
removeEventListener(const Reference<lang::XEventListener> & _xListener)1810 void SAL_CALL ODatabaseDocument::removeEventListener( const Reference< lang::XEventListener >& _xListener ) throw (RuntimeException)
1811 {
1812 	::cppu::WeakComponentImplHelperBase::removeEventListener( _xListener );
1813 }
1814 // XServiceInfo
1815 //------------------------------------------------------------------------------
getImplementationName()1816 rtl::OUString ODatabaseDocument::getImplementationName(  ) throw(RuntimeException)
1817 {
1818 	return getImplementationName_static();
1819 }
1820 
1821 //------------------------------------------------------------------------------
getImplementationName_static()1822 rtl::OUString ODatabaseDocument::getImplementationName_static(  ) throw(RuntimeException)
1823 {
1824 	return rtl::OUString::createFromAscii("com.sun.star.comp.dba.ODatabaseDocument");
1825 }
1826 
1827 //------------------------------------------------------------------------------
getSupportedServiceNames()1828 Sequence< ::rtl::OUString > ODatabaseDocument::getSupportedServiceNames(  ) throw (RuntimeException)
1829 {
1830 	return getSupportedServiceNames_static();
1831 }
1832 
1833 //------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)1834 Reference< XInterface > ODatabaseDocument::Create( const Reference< XComponentContext >& _rxContext )
1835 {
1836     ::comphelper::ComponentContext aContext( _rxContext );
1837     Reference< XUnoTunnel > xDBContextTunnel( aContext.createComponent( (::rtl::OUString)SERVICE_SDB_DATABASECONTEXT ), UNO_QUERY_THROW );
1838     ODatabaseContext* pContext = reinterpret_cast< ODatabaseContext* >( xDBContextTunnel->getSomething( ODatabaseContext::getUnoTunnelImplementationId() ) );
1839 
1840 	::rtl::Reference<ODatabaseModelImpl> pImpl( new ODatabaseModelImpl( aContext.getLegacyServiceFactory(), *pContext ) );
1841     Reference< XModel > xModel( pImpl->createNewModel_deliverOwnership( false ) );
1842     return xModel.get();
1843 }
1844 
1845 //------------------------------------------------------------------------------
getSupportedServiceNames_static()1846 Sequence< ::rtl::OUString > ODatabaseDocument::getSupportedServiceNames_static(  ) throw (RuntimeException)
1847 {
1848 	Sequence< ::rtl::OUString > aSNS( 2 );
1849 	aSNS[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument"));
1850 	aSNS[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.OfficeDocument"));
1851 	return aSNS;
1852 }
1853 
1854 //------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)1855 sal_Bool ODatabaseDocument::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
1856 {
1857 	return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
1858 }
1859 // -----------------------------------------------------------------------------
getDataSource()1860 Reference< XDataSource > SAL_CALL ODatabaseDocument::getDataSource() throw (RuntimeException)
1861 {
1862     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1863     return m_pImpl->getOrCreateDataSource();
1864 }
1865 
1866 // -----------------------------------------------------------------------------
loadFromStorage(const Reference<XStorage> &,const Sequence<PropertyValue> &)1867 void SAL_CALL ODatabaseDocument::loadFromStorage( const Reference< XStorage >& /*xStorage*/, const Sequence< PropertyValue >& /*aMediaDescriptor*/ ) throw (IllegalArgumentException, DoubleInitializationException, IOException, Exception, RuntimeException)
1868 {
1869     DocumentGuard aGuard( *this );
1870 
1871     throw Exception(
1872         DBACORE_RESSTRING( RID_STR_NO_EMBEDDING ),
1873         *this
1874     );
1875 }
1876 
1877 // -----------------------------------------------------------------------------
storeToStorage(const Reference<XStorage> & _rxStorage,const Sequence<PropertyValue> & _rMediaDescriptor)1878 void SAL_CALL ODatabaseDocument::storeToStorage( const Reference< XStorage >& _rxStorage, const Sequence< PropertyValue >& _rMediaDescriptor ) throw (IllegalArgumentException, IOException, Exception, RuntimeException)
1879 {
1880     DocumentGuard aGuard( *this );
1881     impl_storeToStorage_throw( _rxStorage, _rMediaDescriptor, aGuard );
1882 }
1883 
1884 // -----------------------------------------------------------------------------
switchToStorage(const Reference<XStorage> & _rxNewRootStorage)1885 void SAL_CALL ODatabaseDocument::switchToStorage( const Reference< XStorage >& _rxNewRootStorage ) throw (IllegalArgumentException, IOException, Exception, RuntimeException)
1886 {
1887     DocumentGuard aGuard( *this );
1888 
1889     Reference< XStorage > xNewRootStorage( m_pImpl->switchToStorage( _rxNewRootStorage ) );
1890 
1891     aGuard.clear();
1892     impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1893 }
1894 
1895 // -----------------------------------------------------------------------------
getDocumentStorage()1896 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentStorage(  ) throw (IOException, Exception, RuntimeException)
1897 {
1898     DocumentGuard aGuard( *this );
1899     return m_pImpl->getOrCreateRootStorage();
1900 }
1901 
1902 // -----------------------------------------------------------------------------
addStorageChangeListener(const Reference<XStorageChangeListener> & _Listener)1903 void SAL_CALL ODatabaseDocument::addStorageChangeListener( const Reference< XStorageChangeListener >& _Listener ) throw (RuntimeException)
1904 {
1905     DocumentGuard aGuard( *this );
1906     m_aStorageListeners.addInterface( _Listener );
1907 }
1908 
1909 // -----------------------------------------------------------------------------
removeStorageChangeListener(const Reference<XStorageChangeListener> & _Listener)1910 void SAL_CALL ODatabaseDocument::removeStorageChangeListener( const Reference< XStorageChangeListener >& _Listener ) throw (RuntimeException)
1911 {
1912     DocumentGuard aGuard( *this );
1913     m_aStorageListeners.addInterface( _Listener );
1914 }
1915 
1916 // -----------------------------------------------------------------------------
getBasicLibraries()1917 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getBasicLibraries() throw (RuntimeException)
1918 {
1919     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1920     return m_pImpl->getLibraryContainer( true );
1921 }
1922 
1923 // -----------------------------------------------------------------------------
getDialogLibraries()1924 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getDialogLibraries() throw (RuntimeException)
1925 {
1926     DocumentGuard aGuard( *this );
1927     return m_pImpl->getLibraryContainer( false );
1928 }
1929 
1930 // -----------------------------------------------------------------------------
getAllowMacroExecution()1931 ::sal_Bool SAL_CALL ODatabaseDocument::getAllowMacroExecution() throw (RuntimeException)
1932 {
1933     DocumentGuard aGuard( *this );
1934     return m_pImpl->adjustMacroMode_AutoReject();
1935 }
1936 
1937 // -----------------------------------------------------------------------------
getScriptContainer()1938 Reference< XEmbeddedScripts > SAL_CALL ODatabaseDocument::getScriptContainer() throw (RuntimeException)
1939 {
1940     DocumentGuard aGuard( *this );
1941     return this;
1942 }
1943 
1944 // -----------------------------------------------------------------------------
getScriptProvider()1945 Reference< provider::XScriptProvider > SAL_CALL ODatabaseDocument::getScriptProvider(  ) throw (RuntimeException)
1946 {
1947     DocumentGuard aGuard( *this );
1948 
1949     Reference< XScriptProvider > xScriptProvider( m_xScriptProvider );
1950     if ( !xScriptProvider.is() )
1951     {
1952         Reference < XScriptProviderFactory > xFactory(
1953             m_pImpl->m_aContext.getSingleton( "com.sun.star.script.provider.theMasterScriptProviderFactory" ), UNO_QUERY_THROW );
1954 
1955         Any aScriptProviderContext;
1956         if ( m_bAllowDocumentScripting )
1957             aScriptProviderContext <<= Reference< XModel >( this );
1958 
1959         xScriptProvider.set( xFactory->createScriptProvider( aScriptProviderContext ), UNO_SET_THROW );
1960         m_xScriptProvider = xScriptProvider;
1961     }
1962 
1963     return xScriptProvider;
1964 }
1965 
1966 // -----------------------------------------------------------------------------
getEvents()1967 Reference< XNameReplace > SAL_CALL ODatabaseDocument::getEvents(  ) throw (RuntimeException)
1968 {
1969     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1970     return m_pEventContainer;
1971 }
1972 
1973 // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject & Source)1974 void SAL_CALL ODatabaseDocument::disposing( const ::com::sun::star::lang::EventObject& Source ) throw(RuntimeException)
1975 {
1976 	if ( m_pImpl.is() )
1977 		m_pImpl->disposing(Source);
1978 }
1979 
1980 //------------------------------------------------------------------
getThis() const1981 Reference< XInterface > ODatabaseDocument::getThis() const
1982 {
1983     return *const_cast< ODatabaseDocument* >( this );
1984 }
1985 // -----------------------------------------------------------------------------
1986 struct CreateAny : public ::std::unary_function< Reference<XController>, Any>
1987 {
operator ()dbaccess::CreateAny1988     Any operator() (const Reference<XController>& lhs) const
1989     {
1990         return makeAny(lhs);
1991     }
1992 };
1993 
1994 // XModel2
getControllers()1995 Reference< XEnumeration > SAL_CALL ODatabaseDocument::getControllers(  ) throw (RuntimeException)
1996 {
1997     DocumentGuard aGuard( *this );
1998     uno::Sequence< Any> aController( m_aControllers.size() );
1999     ::std::transform( m_aControllers.begin(), m_aControllers.end(), aController.getArray(), CreateAny() );
2000     return new ::comphelper::OAnyEnumeration(aController);
2001 }
2002 // -----------------------------------------------------------------------------
getAvailableViewControllerNames()2003 Sequence< ::rtl::OUString > SAL_CALL ODatabaseDocument::getAvailableViewControllerNames(  ) throw (RuntimeException)
2004 {
2005     Sequence< ::rtl::OUString > aNames(1);
2006     aNames[0] = SERVICE_SDB_APPLICATIONCONTROLLER;
2007     return aNames;
2008 }
2009 // -----------------------------------------------------------------------------
createDefaultViewController(const Reference<XFrame> & _Frame)2010 Reference< XController2 > SAL_CALL ODatabaseDocument::createDefaultViewController( const Reference< XFrame >& _Frame ) throw (IllegalArgumentException, Exception, RuntimeException)
2011 {
2012     return createViewController(
2013         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Default" ) ),
2014         Sequence< PropertyValue >(),
2015         _Frame
2016     );
2017 }
2018 
2019 // -----------------------------------------------------------------------------
createViewController(const::rtl::OUString & _ViewName,const Sequence<PropertyValue> & _Arguments,const Reference<XFrame> & _Frame)2020 Reference< XController2 > SAL_CALL ODatabaseDocument::createViewController( const ::rtl::OUString& _ViewName, const Sequence< PropertyValue >& _Arguments, const Reference< XFrame >& _Frame ) throw (IllegalArgumentException, Exception, RuntimeException)
2021 {
2022     if ( !_ViewName.equalsAscii( "Default" ) && !_ViewName.equalsAscii( "Preview" ) )
2023         throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
2024     if ( !_Frame.is() )
2025         throw IllegalArgumentException( ::rtl::OUString(), *this, 3 );
2026 
2027     DocumentGuard aGuard( *this );
2028     ::comphelper::ComponentContext aContext( m_pImpl->m_aContext );
2029     aGuard.clear();
2030 
2031     Reference< XController2 > xController;
2032     aContext.createComponent( "org.openoffice.comp.dbu.OApplicationController", xController );
2033 
2034     ::comphelper::NamedValueCollection aInitArgs( _Arguments );
2035     aInitArgs.put( "Frame", _Frame );
2036     if ( _ViewName.equalsAscii( "Preview" ) )
2037         aInitArgs.put( "Preview", sal_Bool( sal_True ) );
2038     Reference< XInitialization > xInitController( xController, UNO_QUERY_THROW );
2039     xInitController->initialize( aInitArgs.getWrappedPropertyValues() );
2040 
2041     return xController;
2042 }
2043 
2044 // -----------------------------------------------------------------------------
2045 //=============================================================================
impl_getTitleHelper_throw()2046 Reference< XTitle > ODatabaseDocument::impl_getTitleHelper_throw()
2047 {
2048     if ( ! m_xTitleHelper.is ())
2049     {
2050 		Reference< XUntitledNumbers > xDesktop(
2051             m_pImpl->m_aContext.createComponent( "com.sun.star.frame.Desktop" ),
2052             UNO_QUERY_THROW );
2053         uno::Reference< frame::XModel > xThis   (getThis(), uno::UNO_QUERY_THROW);
2054 
2055         ::framework::TitleHelper* pHelper = new ::framework::TitleHelper(m_pImpl->m_aContext.getLegacyServiceFactory());
2056         m_xTitleHelper.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2057         pHelper->setOwner                   (xThis   );
2058         pHelper->connectWithUntitledNumbers (xDesktop);
2059     }
2060 
2061     return m_xTitleHelper;
2062 }
2063 
2064 //=============================================================================
impl_getUntitledHelper_throw(const uno::Reference<uno::XInterface> & _xComponent)2065 uno::Reference< frame::XUntitledNumbers > ODatabaseDocument::impl_getUntitledHelper_throw(const uno::Reference< uno::XInterface >& _xComponent)
2066 {
2067     if ( !m_xModuleManager.is() )
2068 		m_xModuleManager.set( m_pImpl->m_aContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
2069 
2070 	::rtl::OUString sModuleId;
2071 	try
2072 	{
2073 		sModuleId = m_xModuleManager->identify( _xComponent );
2074 	}
2075 	catch(uno::Exception)
2076 	{
2077 		// ni
2078 	}
2079     uno::Reference< frame::XUntitledNumbers > xNumberedControllers;
2080 
2081     TNumberedController::iterator aFind = m_aNumberedControllers.find(sModuleId);
2082     if ( aFind == m_aNumberedControllers.end() )
2083     {
2084         uno::Reference< frame::XModel > xThis(static_cast< frame::XModel* >(this), uno::UNO_QUERY_THROW);
2085         ::comphelper::NumberedCollection* pHelper = new ::comphelper::NumberedCollection();
2086         xNumberedControllers.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2087 
2088         pHelper->setOwner          (xThis);
2089         //pHelper->setUntitledPrefix (::rtl::OUString::createFromAscii(" : "));
2090 
2091         m_aNumberedControllers.insert(TNumberedController::value_type(sModuleId,xNumberedControllers));
2092     }
2093     else
2094         xNumberedControllers = aFind->second;
2095 
2096     return xNumberedControllers;
2097 }
2098 
2099 //=============================================================================
2100 // css.frame.XTitle
getTitle()2101 ::rtl::OUString SAL_CALL ODatabaseDocument::getTitle()
2102     throw (uno::RuntimeException)
2103 {
2104     // SYNCHRONIZED ->
2105     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2106     return impl_getTitleHelper_throw()->getTitle();
2107 }
2108 
2109 //=============================================================================
2110 // css.frame.XTitle
setTitle(const::rtl::OUString & sTitle)2111 void SAL_CALL ODatabaseDocument::setTitle( const ::rtl::OUString& sTitle )
2112     throw (uno::RuntimeException)
2113 {
2114     // SYNCHRONIZED ->
2115     DocumentGuard aGuard( *this );
2116     impl_getTitleHelper_throw()->setTitle( sTitle );
2117     m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
2118     // <- SYNCHRONIZED
2119 }
2120 
2121 //=============================================================================
2122 // css.frame.XTitleChangeBroadcaster
addTitleChangeListener(const uno::Reference<frame::XTitleChangeListener> & xListener)2123 void SAL_CALL ODatabaseDocument::addTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2124     throw (uno::RuntimeException)
2125 {
2126     // SYNCHRONIZED ->
2127     DocumentGuard aGuard( *this );
2128 
2129     uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2130     xBroadcaster->addTitleChangeListener( xListener );
2131 }
2132 
2133 //=============================================================================
2134 // css.frame.XTitleChangeBroadcaster
removeTitleChangeListener(const uno::Reference<frame::XTitleChangeListener> & xListener)2135 void SAL_CALL ODatabaseDocument::removeTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2136     throw (uno::RuntimeException)
2137 {
2138     // SYNCHRONIZED ->
2139     DocumentGuard aGuard( *this );
2140 
2141     uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2142     xBroadcaster->removeTitleChangeListener( xListener );
2143 }
2144 
2145 //=============================================================================
2146 // css.frame.XUntitledNumbers
leaseNumber(const uno::Reference<uno::XInterface> & xComponent)2147 ::sal_Int32 SAL_CALL ODatabaseDocument::leaseNumber( const uno::Reference< uno::XInterface >& xComponent )
2148     throw (lang::IllegalArgumentException,
2149            uno::RuntimeException         )
2150 {
2151     DocumentGuard aGuard( *this );
2152     return impl_getUntitledHelper_throw(xComponent)->leaseNumber (xComponent);
2153 }
2154 
2155 //=============================================================================
2156 // css.frame.XUntitledNumbers
releaseNumber(::sal_Int32 nNumber)2157 void SAL_CALL ODatabaseDocument::releaseNumber( ::sal_Int32 nNumber )
2158     throw (lang::IllegalArgumentException,
2159            uno::RuntimeException         )
2160 {
2161     DocumentGuard aGuard( *this );
2162     impl_getUntitledHelper_throw()->releaseNumber (nNumber);
2163 }
2164 
2165 //=============================================================================
2166 // css.frame.XUntitledNumbers
releaseNumberForComponent(const uno::Reference<uno::XInterface> & xComponent)2167 void SAL_CALL ODatabaseDocument::releaseNumberForComponent( const uno::Reference< uno::XInterface >& xComponent )
2168     throw (lang::IllegalArgumentException,
2169            uno::RuntimeException         )
2170 {
2171     DocumentGuard aGuard( *this );
2172     impl_getUntitledHelper_throw(xComponent)->releaseNumberForComponent (xComponent);
2173 }
2174 
2175 //=============================================================================
2176 // css.frame.XUntitledNumbers
getUntitledPrefix()2177 ::rtl::OUString SAL_CALL ODatabaseDocument::getUntitledPrefix()    throw (uno::RuntimeException)
2178 {
2179     return ::rtl::OUString();/*RTL_CONSTASCII_USTRINGPARAM(" : "));*/
2180 }
2181 
2182 //------------------------------------------------------------------
2183 //........................................................................
2184 }	// namespace dbaccess
2185 //........................................................................
2186 
2187 
2188