xref: /trunk/main/dbaccess/source/core/dataaccess/databasedocument.cxx (revision 3ce6c2dd3860afd353ceca3849779821e9b9f6f5)
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 )
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(  )
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::bind( ::std::equal_to< Type >(), ::std::placeholders::_1, 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::bind( ::std::equal_to< Type >(), ::std::placeholders::_1, XScriptInvocationContext::static_type() )
292             ) - pStripTo
293         );
294     }
295 
296     return aTypes;
297 }
298 //------------------------------------------------------------------------------
getImplementationId()299 Sequence< sal_Int8 > SAL_CALL ODatabaseDocument::getImplementationId(  )
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(  )
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 )
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()
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 accessible 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 )
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 )
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 )
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(  )
757 {
758     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
759     return m_pImpl->getURL();
760 }
761 
762 // -----------------------------------------------------------------------------
getArgs()763 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getArgs(  )
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 )
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 )
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(  )
843 {
844     DocumentGuard aGuard( *this );
845 
846     ++m_pImpl->m_nControllerLockCount;
847 }
848 
849 // -----------------------------------------------------------------------------
unlockControllers()850 void SAL_CALL ODatabaseDocument::unlockControllers(  )
851 {
852     DocumentGuard aGuard( *this );
853 
854     --m_pImpl->m_nControllerLockCount;
855 }
856 
857 // -----------------------------------------------------------------------------
hasControllersLocked()858 sal_Bool SAL_CALL ODatabaseDocument::hasControllersLocked(  )
859 {
860     DocumentGuard aGuard( *this );
861 
862     return m_pImpl->m_nControllerLockCount != 0;
863 }
864 
865 // -----------------------------------------------------------------------------
getCurrentController()866 Reference< XController > SAL_CALL ODatabaseDocument::getCurrentController()
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 )
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(  )
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(  )
919 {
920     return getLocation().getLength() > 0;
921 }
922 // -----------------------------------------------------------------------------
getLocation()923 ::rtl::OUString SAL_CALL ODatabaseDocument::getLocation(  )
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 document was possibly recovered from (which would be getDocFileLocation)
929 }
930 // -----------------------------------------------------------------------------
isReadonly()931 sal_Bool SAL_CALL ODatabaseDocument::isReadonly(  )
932 {
933     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
934     return m_pImpl->m_bDocumentReadOnly;
935 }
936 // -----------------------------------------------------------------------------
store()937 void SAL_CALL ODatabaseDocument::store(  )
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 )
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 noticeable
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 )
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 )
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 )
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 )
1250 {
1251     DocumentGuard aGuard( *this );
1252     m_aModifyListeners.removeInterface(_xListener);
1253 }
1254 
1255 // -----------------------------------------------------------------------------
1256 // XModifiable
isModified()1257 sal_Bool SAL_CALL ODatabaseDocument::isModified(  )
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 )
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 )
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 )
1305 {
1306     m_aEventNotifier.removeLegacyEventListener( _Listener );
1307 }
1308 
1309 // -----------------------------------------------------------------------------
addDocumentEventListener(const Reference<XDocumentEventListener> & _Listener)1310 void SAL_CALL ODatabaseDocument::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
1311 {
1312     m_aEventNotifier.addDocumentEventListener( _Listener );
1313 }
1314 
1315 // -----------------------------------------------------------------------------
removeDocumentEventListener(const Reference<XDocumentEventListener> & _Listener)1316 void SAL_CALL ODatabaseDocument::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
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 )
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(  )
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*/ )
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*/ )
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
1441 {
1442     typedef Reference< XController > argument_type;
1443     typedef void result_type;
operator ()dbaccess::DisposeControllerFrame1444     void operator()( const Reference< XController >& _rxController ) const
1445     {
1446         try
1447         {
1448             if ( !_rxController.is() )
1449                 return;
1450 
1451             Reference< XFrame > xFrame( _rxController->getFrame() );
1452             ::comphelper::disposeComponent( xFrame );
1453         }
1454         catch( const Exception& )
1455         {
1456             DBG_UNHANDLED_EXCEPTION();
1457         }
1458     };
1459 };
1460 
1461 // -----------------------------------------------------------------------------
impl_disposeControllerFrames_nothrow()1462 void ODatabaseDocument::impl_disposeControllerFrames_nothrow()
1463 {
1464     Controllers aCopy;
1465     aCopy.swap( m_aControllers );   // ensure m_aControllers is empty afterwards
1466     ::std::for_each( aCopy.begin(), aCopy.end(), DisposeControllerFrame() );
1467 }
1468 
1469 // -----------------------------------------------------------------------------
close(sal_Bool _bDeliverOwnership)1470 void SAL_CALL ODatabaseDocument::close( sal_Bool _bDeliverOwnership )
1471 {
1472     // nearly everything below can/must be done without our mutex locked, the below is just for
1473     // the checks for being disposed and the like
1474     // SYNCHRONIZED ->
1475     {
1476         DocumentGuard aGuard( *this );
1477         m_bClosing = true;
1478     }
1479     // <- SYNCHRONIZED
1480 
1481     try
1482     {
1483         // allow listeners to veto
1484         lang::EventObject aEvent( *this );
1485         m_aCloseListener.forEach< XCloseListener >(
1486             boost::bind( &XCloseListener::queryClosing, _1, boost::cref( aEvent ), boost::cref( _bDeliverOwnership ) ) );
1487 
1488         // notify that we're going to unload
1489         m_aEventNotifier.notifyDocumentEvent( "OnPrepareUnload" );
1490 
1491         impl_closeControllerFrames_nolck_throw( _bDeliverOwnership );
1492 
1493         m_aCloseListener.notifyEach( &XCloseListener::notifyClosing, (const lang::EventObject&)aEvent );
1494 
1495         dispose();
1496     }
1497     catch ( const Exception& )
1498     {
1499         ::osl::MutexGuard aGuard( m_aMutex );
1500         m_bClosing = false;
1501         throw;
1502     }
1503 
1504     // SYNCHRONIZED ->
1505     ::osl::MutexGuard aGuard( m_aMutex );
1506     m_bClosing = false;
1507     // <- SYNCHRONIZED
1508 }
1509 // -----------------------------------------------------------------------------
addCloseListener(const Reference<::com::sun::star::util::XCloseListener> & Listener)1510 void SAL_CALL ODatabaseDocument::addCloseListener( const Reference< ::com::sun::star::util::XCloseListener >& Listener )
1511 {
1512     DocumentGuard aGuard( *this );
1513     m_aCloseListener.addInterface(Listener);
1514 }
1515 // -----------------------------------------------------------------------------
removeCloseListener(const Reference<::com::sun::star::util::XCloseListener> & Listener)1516 void SAL_CALL ODatabaseDocument::removeCloseListener( const Reference< ::com::sun::star::util::XCloseListener >& Listener )
1517 {
1518     DocumentGuard aGuard( *this );
1519     m_aCloseListener.removeInterface(Listener);
1520 }
1521 // -----------------------------------------------------------------------------
getFormDocuments()1522 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getFormDocuments(  )
1523 {
1524     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1525     return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_FORM );
1526 }
1527 // -----------------------------------------------------------------------------
getReportDocuments()1528 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getReportDocuments(  )
1529 {
1530     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1531     return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_REPORT );
1532 }
1533 
1534 // -----------------------------------------------------------------------------
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) const1535 void ODatabaseDocument::WriteThroughComponent( const Reference< XComponent >& xComponent, const sal_Char* pStreamName,
1536     const sal_Char* pServiceName, const Sequence< Any >& _rArguments, const Sequence< PropertyValue >& rMediaDesc,
1537     const Reference<XStorage>& _xStorageToSaveTo ) const
1538 {
1539     OSL_ENSURE( pStreamName, "Need stream name!" );
1540     OSL_ENSURE( pServiceName, "Need service name!" );
1541 
1542     // open stream
1543     ::rtl::OUString sStreamName = ::rtl::OUString::createFromAscii( pStreamName );
1544     Reference< XStream > xStream = _xStorageToSaveTo->openStreamElement( sStreamName, ElementModes::READWRITE | ElementModes::TRUNCATE );
1545     if ( !xStream.is() )
1546         return;
1547 
1548     Reference< XOutputStream > xOutputStream( xStream->getOutputStream() );
1549     OSL_ENSURE( xOutputStream.is(), "Can't create output stream in package!" );
1550     if ( !xOutputStream.is() )
1551         return;
1552 
1553     Reference< XSeekable > xSeek( xOutputStream, UNO_QUERY );
1554     if ( xSeek.is() )
1555         xSeek->seek(0);
1556 
1557     Reference< XPropertySet > xStreamProp( xOutputStream, UNO_QUERY_THROW );
1558     xStreamProp->setPropertyValue( INFO_MEDIATYPE, makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "text/xml" ) ) ) );
1559     xStreamProp->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) ), makeAny( (sal_Bool)sal_True ) );
1560 
1561     // write the stuff
1562     WriteThroughComponent( xOutputStream, xComponent, pServiceName, _rArguments, rMediaDesc );
1563 }
1564 
WriteThroughComponent(const Reference<XOutputStream> & xOutputStream,const Reference<XComponent> & xComponent,const sal_Char * pServiceName,const Sequence<Any> & _rArguments,const Sequence<PropertyValue> & rMediaDesc) const1565 void ODatabaseDocument::WriteThroughComponent( const Reference< XOutputStream >& xOutputStream,
1566     const Reference< XComponent >& xComponent, const sal_Char* pServiceName, const Sequence< Any >& _rArguments,
1567     const Sequence< PropertyValue >& rMediaDesc ) const
1568 {
1569     OSL_ENSURE( xOutputStream.is(), "I really need an output stream!" );
1570     OSL_ENSURE( xComponent.is(), "Need component!" );
1571     OSL_ENSURE( NULL != pServiceName, "Need component name!" );
1572 
1573     // get component
1574     Reference< XActiveDataSource > xSaxWriter;
1575     OSL_VERIFY( m_pImpl->m_aContext.createComponent( "com.sun.star.xml.sax.Writer", xSaxWriter ) );
1576     if ( !xSaxWriter.is() )
1577         return;
1578 
1579     // connect XML writer to output stream
1580     xSaxWriter->setOutputStream( xOutputStream );
1581 
1582     // prepare arguments (prepend doc handler to given arguments)
1583     Reference< XDocumentHandler > xDocHandler( xSaxWriter,UNO_QUERY);
1584     Sequence<Any> aArgs( 1 + _rArguments.getLength() );
1585     aArgs[0] <<= xDocHandler;
1586     for ( sal_Int32 i = 0; i < _rArguments.getLength(); ++i )
1587         aArgs[ i+1 ] = _rArguments[i];
1588 
1589     // get filter component
1590     Reference< XExporter > xExporter;
1591     OSL_VERIFY( m_pImpl->m_aContext.createComponentWithArguments( pServiceName, aArgs, xExporter ) );
1592     if ( !xExporter.is() )
1593         return;
1594 
1595     // connect model and filter
1596     xExporter->setSourceDocument( xComponent );
1597 
1598     // filter
1599     Reference< XFilter > xFilter( xExporter, UNO_QUERY_THROW );
1600     xFilter->filter( rMediaDesc );
1601 }
1602 
1603 // -----------------------------------------------------------------------------
impl_writeStorage_throw(const Reference<XStorage> & _rxTargetStorage,const::comphelper::NamedValueCollection & _rMediaDescriptor) const1604 void ODatabaseDocument::impl_writeStorage_throw( const Reference< XStorage >& _rxTargetStorage, const ::comphelper::NamedValueCollection& _rMediaDescriptor ) const
1605 {
1606     // extract status indicator
1607     Sequence< Any > aDelegatorArguments;
1608     lcl_extractStatusIndicator( _rMediaDescriptor, aDelegatorArguments );
1609 
1610     /** property map for export info set */
1611     comphelper::PropertyMapEntry aExportInfoMap[] =
1612     {
1613         { MAP_LEN( "BaseURI"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
1614         { MAP_LEN( "StreamName"), 0,&::getCppuType( (::rtl::OUString *)0 ),beans::PropertyAttribute::MAYBEVOID, 0 },
1615         { MAP_LEN( "UsePrettyPrinting" ), 0, &::getCppuType((sal_Bool*)0), beans::PropertyAttribute::MAYBEVOID, 0},
1616         { NULL, 0, 0, NULL, 0, 0 }
1617     };
1618     uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
1619 
1620     SvtSaveOptions aSaveOpt;
1621     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting")), uno::makeAny(aSaveOpt.IsPrettyPrinting()));
1622     if ( aSaveOpt.IsSaveRelFSys() )
1623         xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BaseURI")), uno::makeAny(_rMediaDescriptor.getOrDefault("URL",::rtl::OUString())));
1624 
1625     ::rtl::OUString aVersion;
1626     SvtSaveOptions::ODFDefaultVersion nDefVersion = aSaveOpt.GetODFDefaultVersion();
1627 
1628     // older versions can not have this property set, it exists only starting from ODF1.2
1629     if ( nDefVersion >= SvtSaveOptions::ODFVER_014 )
1630         aVersion = ODFVER_014_TEXT;
1631     else if ( nDefVersion >= SvtSaveOptions::ODFVER_013 )
1632         aVersion = ODFVER_013_TEXT;
1633     else if ( nDefVersion >= SvtSaveOptions::ODFVER_012 )
1634         aVersion = ODFVER_012_TEXT;
1635 
1636     if ( aVersion.getLength() )
1637     {
1638         try
1639         {
1640             xInfoSet->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Version" )), uno::makeAny( aVersion ) );
1641         }
1642         catch( uno::Exception& )
1643         {
1644         }
1645     }
1646 
1647     sal_Int32 nArgsLen = aDelegatorArguments.getLength();
1648     aDelegatorArguments.realloc(nArgsLen+1);
1649     aDelegatorArguments[nArgsLen++] <<= xInfoSet;
1650 
1651     Reference< XPropertySet > xProp( _rxTargetStorage, UNO_QUERY_THROW );
1652     xProp->setPropertyValue( INFO_MEDIATYPE, makeAny( (rtl::OUString)MIMETYPE_OASIS_OPENDOCUMENT_DATABASE ) );
1653     // manifest:version is taken from the storage's Version property
1654     if ( aVersion.getLength() )
1655     {
1656         try
1657         {
1658             xProp->setPropertyValue( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Version" )), uno::makeAny( aVersion ) );
1659         }
1660         catch( uno::Exception& )
1661         {
1662         }
1663     }
1664 
1665     Reference< XComponent > xComponent( *const_cast< ODatabaseDocument* >( this ), UNO_QUERY_THROW );
1666 
1667     Sequence< PropertyValue > aMediaDescriptor;
1668     _rMediaDescriptor >>= aMediaDescriptor;
1669 
1670     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamName")), uno::makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("settings.xml"))));
1671     WriteThroughComponent( xComponent, "settings.xml", "com.sun.star.comp.sdb.XMLSettingsExporter",
1672         aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1673 
1674     xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamName")), uno::makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml"))));
1675     WriteThroughComponent( xComponent, "content.xml", "com.sun.star.comp.sdb.DBExportFilter",
1676         aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1677 
1678     m_pImpl->storeLibraryContainersTo( _rxTargetStorage );
1679 }
1680 
1681 // -----------------------------------------------------------------------------
getUIConfigurationManager()1682 Reference< XUIConfigurationManager > SAL_CALL ODatabaseDocument::getUIConfigurationManager(  )
1683 {
1684     DocumentGuard aGuard( *this );
1685 
1686     if ( !m_xUIConfigurationManager.is() )
1687     {
1688         m_pImpl->m_aContext.createComponent( "com.sun.star.ui.UIConfigurationManager", m_xUIConfigurationManager );
1689         Reference< XUIConfigurationStorage > xUIConfigStorage( m_xUIConfigurationManager, UNO_QUERY );
1690         if ( xUIConfigStorage.is() )
1691         {
1692             rtl::OUString aUIConfigFolderName( RTL_CONSTASCII_USTRINGPARAM( "Configurations2" ));
1693             Reference< XStorage > xConfigStorage;
1694 
1695             // First try to open with READWRITE and then READ
1696             xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READWRITE );
1697             if ( xConfigStorage.is() )
1698             {
1699                 rtl::OUString aUIConfigMediaType( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.xml.ui.configuration" ));
1700                 rtl::OUString aMediaType;
1701                 Reference< XPropertySet > xPropSet( xConfigStorage, UNO_QUERY );
1702                 Any a = xPropSet->getPropertyValue( INFO_MEDIATYPE );
1703                 if ( !( a >>= aMediaType ) || ( aMediaType.getLength() == 0 ))
1704                 {
1705                     a <<= aUIConfigMediaType;
1706                     xPropSet->setPropertyValue( INFO_MEDIATYPE, a );
1707                 }
1708             }
1709             else
1710                 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READ );
1711 
1712             // initialize ui configuration manager with document substorage
1713             xUIConfigStorage->setStorage( xConfigStorage );
1714         }
1715     }
1716 
1717     return m_xUIConfigurationManager;
1718 }
1719 // -----------------------------------------------------------------------------
getDocumentSubStorage(const::rtl::OUString & aStorageName,sal_Int32 nMode)1720 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentSubStorage( const ::rtl::OUString& aStorageName, sal_Int32 nMode )
1721 {
1722     DocumentGuard aGuard( *this );
1723 
1724     Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1725     return xStorageAccess->getDocumentSubStorage( aStorageName, nMode );
1726 }
1727 // -----------------------------------------------------------------------------
getDocumentSubStoragesNames()1728 Sequence< ::rtl::OUString > SAL_CALL ODatabaseDocument::getDocumentSubStoragesNames(  )
1729 {
1730     Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1731     return xStorageAccess->getDocumentSubStoragesNames();
1732 }
1733 
1734 //------------------------------------------------------------------------------
impl_notifyStorageChange_nolck_nothrow(const Reference<XStorage> & _rxNewRootStorage)1735 void ODatabaseDocument::impl_notifyStorageChange_nolck_nothrow( const Reference< XStorage >& _rxNewRootStorage )
1736 {
1737     Reference< XInterface > xMe( *const_cast< ODatabaseDocument* >( this ) );
1738 
1739     m_aStorageListeners.forEach< XStorageChangeListener >(
1740         boost::bind( &XStorageChangeListener::notifyStorageChange, _1, boost::cref( xMe ), boost::cref( _rxNewRootStorage ) ) );
1741 }
1742 
1743 //------------------------------------------------------------------------------
disposing()1744 void ODatabaseDocument::disposing()
1745 {
1746     OSL_TRACE( "DD: disp: %p: %p", this, m_pImpl.get() );
1747     if ( !m_pImpl.is() )
1748     {
1749         // this means that we're already disposed
1750         DBG_ASSERT( ODatabaseDocument_OfficeDocument::rBHelper.bDisposed, "ODatabaseDocument::disposing: no impl anymore, but not yet disposed!" );
1751         return;
1752     }
1753 
1754     if ( impl_isInitialized() )
1755         m_aEventNotifier.notifyDocumentEvent( "OnUnload" );
1756 
1757     Reference< XModel > xHoldAlive( this );
1758 
1759     m_aEventNotifier.disposing();
1760 
1761     lang::EventObject aDisposeEvent(static_cast<XWeak*>(this));
1762     m_aModifyListeners.disposeAndClear( aDisposeEvent );
1763     m_aCloseListener.disposeAndClear( aDisposeEvent );
1764     m_aStorageListeners.disposeAndClear( aDisposeEvent );
1765 
1766     // this is the list of objects which we currently hold as member. Upon resetting
1767     // those members, we can (potentially) release the last reference to them, in which
1768     // case they will be deleted - if they're C++ implementations, that is :).
1769     // Some of those implementations are offending enough to require the SolarMutex, which
1770     // means we should not release the last reference while our own mutex is locked ...
1771     ::std::list< Reference< XInterface > > aKeepAlive;
1772 
1773     // SYNCHRONIZED ->
1774     ::osl::ClearableMutexGuard aGuard( m_aMutex );
1775 
1776     DBG_ASSERT( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1777         // normally, nobody should explicitly dispose, but only XCloseable::close the document. And upon
1778         // closing, our controllers are closed, too
1779 
1780     aKeepAlive.push_back( m_xUIConfigurationManager );
1781     m_xUIConfigurationManager = NULL;
1782 
1783     clearObjectContainer( m_xForms );
1784     clearObjectContainer( m_xReports );
1785 
1786     // reset the macro mode: in case the our impl struct stays alive (e.g. because our DataSource
1787     // object still exists), and somebody subsequently re-opens the document, we want to have
1788     // the security warning, again.
1789     m_pImpl->resetMacroExecutionMode();
1790 
1791     // similar argueing for our ViewMonitor
1792     m_aViewMonitor.reset();
1793 
1794     // tell our Impl to forget us
1795     m_pImpl->modelIsDisposing( impl_isInitialized(), ODatabaseModelImpl::ResetModelAccess() );
1796 
1797     // now, at the latest, the controller array should be empty. Controllers are
1798     // expected to listen for our disposal, and disconnect then
1799     DBG_ASSERT( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1800     impl_disposeControllerFrames_nothrow();
1801 
1802     aKeepAlive.push_back( m_xModuleManager );
1803     m_xModuleManager.clear();
1804 
1805     aKeepAlive.push_back( m_xTitleHelper );
1806     m_xTitleHelper.clear();
1807 
1808     m_pImpl.clear();
1809 
1810     aGuard.clear();
1811     // <- SYNCHRONIZED
1812 
1813     aKeepAlive.clear();
1814 }
1815 // -----------------------------------------------------------------------------
1816 // XComponent
dispose()1817 void SAL_CALL ODatabaseDocument::dispose(  )
1818 {
1819     ::cppu::WeakComponentImplHelperBase::dispose();
1820 }
1821 // -----------------------------------------------------------------------------
addEventListener(const Reference<lang::XEventListener> & _xListener)1822 void SAL_CALL ODatabaseDocument::addEventListener( const Reference< lang::XEventListener >& _xListener )
1823 {
1824     ::cppu::WeakComponentImplHelperBase::addEventListener( _xListener );
1825 }
1826 // -----------------------------------------------------------------------------
removeEventListener(const Reference<lang::XEventListener> & _xListener)1827 void SAL_CALL ODatabaseDocument::removeEventListener( const Reference< lang::XEventListener >& _xListener )
1828 {
1829     ::cppu::WeakComponentImplHelperBase::removeEventListener( _xListener );
1830 }
1831 // XServiceInfo
1832 //------------------------------------------------------------------------------
getImplementationName()1833 rtl::OUString ODatabaseDocument::getImplementationName(  )
1834 {
1835     return getImplementationName_static();
1836 }
1837 
1838 //------------------------------------------------------------------------------
getImplementationName_static()1839 rtl::OUString ODatabaseDocument::getImplementationName_static(  )
1840 {
1841     return rtl::OUString::createFromAscii("com.sun.star.comp.dba.ODatabaseDocument");
1842 }
1843 
1844 //------------------------------------------------------------------------------
getSupportedServiceNames()1845 Sequence< ::rtl::OUString > ODatabaseDocument::getSupportedServiceNames(  )
1846 {
1847     return getSupportedServiceNames_static();
1848 }
1849 
1850 //------------------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)1851 Reference< XInterface > ODatabaseDocument::Create( const Reference< XComponentContext >& _rxContext )
1852 {
1853     ::comphelper::ComponentContext aContext( _rxContext );
1854     Reference< XUnoTunnel > xDBContextTunnel( aContext.createComponent( (::rtl::OUString)SERVICE_SDB_DATABASECONTEXT ), UNO_QUERY_THROW );
1855     ODatabaseContext* pContext = reinterpret_cast< ODatabaseContext* >( xDBContextTunnel->getSomething( ODatabaseContext::getUnoTunnelImplementationId() ) );
1856 
1857     ::rtl::Reference<ODatabaseModelImpl> pImpl( new ODatabaseModelImpl( aContext.getLegacyServiceFactory(), *pContext ) );
1858     Reference< XModel > xModel( pImpl->createNewModel_deliverOwnership( false ) );
1859     return xModel.get();
1860 }
1861 
1862 //------------------------------------------------------------------------------
getSupportedServiceNames_static()1863 Sequence< ::rtl::OUString > ODatabaseDocument::getSupportedServiceNames_static(  )
1864 {
1865     Sequence< ::rtl::OUString > aSNS( 2 );
1866     aSNS[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.OfficeDatabaseDocument"));
1867     aSNS[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.OfficeDocument"));
1868     return aSNS;
1869 }
1870 
1871 //------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)1872 sal_Bool ODatabaseDocument::supportsService( const ::rtl::OUString& _rServiceName )
1873 {
1874     return ::comphelper::findValue(getSupportedServiceNames(), _rServiceName, sal_True).getLength() != 0;
1875 }
1876 // -----------------------------------------------------------------------------
getDataSource()1877 Reference< XDataSource > SAL_CALL ODatabaseDocument::getDataSource()
1878 {
1879     DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1880     return m_pImpl->getOrCreateDataSource();
1881 }
1882 
1883 // -----------------------------------------------------------------------------
loadFromStorage(const Reference<XStorage> &,const Sequence<PropertyValue> &)1884 void SAL_CALL ODatabaseDocument::loadFromStorage( const Reference< XStorage >& /*xStorage*/, const Sequence< PropertyValue >& /*aMediaDescriptor*/ )
1885 {
1886     DocumentGuard aGuard( *this );
1887 
1888     throw Exception(
1889         DBACORE_RESSTRING( RID_STR_NO_EMBEDDING ),
1890         *this
1891     );
1892 }
1893 
1894 // -----------------------------------------------------------------------------
storeToStorage(const Reference<XStorage> & _rxStorage,const Sequence<PropertyValue> & _rMediaDescriptor)1895 void SAL_CALL ODatabaseDocument::storeToStorage( const Reference< XStorage >& _rxStorage, const Sequence< PropertyValue >& _rMediaDescriptor )
1896 {
1897     DocumentGuard aGuard( *this );
1898     impl_storeToStorage_throw( _rxStorage, _rMediaDescriptor, aGuard );
1899 }
1900 
1901 // -----------------------------------------------------------------------------
switchToStorage(const Reference<XStorage> & _rxNewRootStorage)1902 void SAL_CALL ODatabaseDocument::switchToStorage( const Reference< XStorage >& _rxNewRootStorage )
1903 {
1904     DocumentGuard aGuard( *this );
1905 
1906     Reference< XStorage > xNewRootStorage( m_pImpl->switchToStorage( _rxNewRootStorage ) );
1907 
1908     aGuard.clear();
1909     impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1910 }
1911 
1912 // -----------------------------------------------------------------------------
getDocumentStorage()1913 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentStorage(  )
1914 {
1915     DocumentGuard aGuard( *this );
1916     return m_pImpl->getOrCreateRootStorage();
1917 }
1918 
1919 // -----------------------------------------------------------------------------
addStorageChangeListener(const Reference<XStorageChangeListener> & _Listener)1920 void SAL_CALL ODatabaseDocument::addStorageChangeListener( const Reference< XStorageChangeListener >& _Listener )
1921 {
1922     DocumentGuard aGuard( *this );
1923     m_aStorageListeners.addInterface( _Listener );
1924 }
1925 
1926 // -----------------------------------------------------------------------------
removeStorageChangeListener(const Reference<XStorageChangeListener> & _Listener)1927 void SAL_CALL ODatabaseDocument::removeStorageChangeListener( const Reference< XStorageChangeListener >& _Listener )
1928 {
1929     DocumentGuard aGuard( *this );
1930     m_aStorageListeners.addInterface( _Listener );
1931 }
1932 
1933 // -----------------------------------------------------------------------------
getBasicLibraries()1934 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getBasicLibraries()
1935 {
1936     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1937     return m_pImpl->getLibraryContainer( true );
1938 }
1939 
1940 // -----------------------------------------------------------------------------
getDialogLibraries()1941 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getDialogLibraries()
1942 {
1943     DocumentGuard aGuard( *this );
1944     return m_pImpl->getLibraryContainer( false );
1945 }
1946 
1947 // -----------------------------------------------------------------------------
getAllowMacroExecution()1948 ::sal_Bool SAL_CALL ODatabaseDocument::getAllowMacroExecution()
1949 {
1950     DocumentGuard aGuard( *this );
1951     return m_pImpl->adjustMacroMode_AutoReject();
1952 }
1953 
1954 // -----------------------------------------------------------------------------
getScriptContainer()1955 Reference< XEmbeddedScripts > SAL_CALL ODatabaseDocument::getScriptContainer()
1956 {
1957     DocumentGuard aGuard( *this );
1958     return this;
1959 }
1960 
1961 // -----------------------------------------------------------------------------
getScriptProvider()1962 Reference< provider::XScriptProvider > SAL_CALL ODatabaseDocument::getScriptProvider(  )
1963 {
1964     DocumentGuard aGuard( *this );
1965 
1966     Reference< XScriptProvider > xScriptProvider( m_xScriptProvider );
1967     if ( !xScriptProvider.is() )
1968     {
1969         Reference < XScriptProviderFactory > xFactory(
1970             m_pImpl->m_aContext.getSingleton( "com.sun.star.script.provider.theMasterScriptProviderFactory" ), UNO_QUERY_THROW );
1971 
1972         Any aScriptProviderContext;
1973         if ( m_bAllowDocumentScripting )
1974             aScriptProviderContext <<= Reference< XModel >( this );
1975 
1976         xScriptProvider.set( xFactory->createScriptProvider( aScriptProviderContext ), UNO_SET_THROW );
1977         m_xScriptProvider = xScriptProvider;
1978     }
1979 
1980     return xScriptProvider;
1981 }
1982 
1983 // -----------------------------------------------------------------------------
getEvents()1984 Reference< XNameReplace > SAL_CALL ODatabaseDocument::getEvents(  )
1985 {
1986     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1987     return m_pEventContainer;
1988 }
1989 
1990 // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject & Source)1991 void SAL_CALL ODatabaseDocument::disposing( const ::com::sun::star::lang::EventObject& Source )
1992 {
1993     if ( m_pImpl.is() )
1994         m_pImpl->disposing(Source);
1995 }
1996 
1997 //------------------------------------------------------------------
getThis() const1998 Reference< XInterface > ODatabaseDocument::getThis() const
1999 {
2000     return *const_cast< ODatabaseDocument* >( this );
2001 }
2002 // -----------------------------------------------------------------------------
2003 struct CreateAny
2004 {
2005     typedef Reference<XController> argument_type;
2006     typedef Any result_type;
operator ()dbaccess::CreateAny2007     Any operator() (const Reference<XController>& lhs) const
2008     {
2009         return makeAny(lhs);
2010     }
2011 };
2012 
2013 // XModel2
getControllers()2014 Reference< XEnumeration > SAL_CALL ODatabaseDocument::getControllers(  )
2015 {
2016     DocumentGuard aGuard( *this );
2017     uno::Sequence< Any> aController( m_aControllers.size() );
2018     ::std::transform( m_aControllers.begin(), m_aControllers.end(), aController.getArray(), CreateAny() );
2019     return new ::comphelper::OAnyEnumeration(aController);
2020 }
2021 // -----------------------------------------------------------------------------
getAvailableViewControllerNames()2022 Sequence< ::rtl::OUString > SAL_CALL ODatabaseDocument::getAvailableViewControllerNames(  )
2023 {
2024     Sequence< ::rtl::OUString > aNames(1);
2025     aNames[0] = SERVICE_SDB_APPLICATIONCONTROLLER;
2026     return aNames;
2027 }
2028 // -----------------------------------------------------------------------------
createDefaultViewController(const Reference<XFrame> & _Frame)2029 Reference< XController2 > SAL_CALL ODatabaseDocument::createDefaultViewController( const Reference< XFrame >& _Frame )
2030 {
2031     return createViewController(
2032         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Default" ) ),
2033         Sequence< PropertyValue >(),
2034         _Frame
2035     );
2036 }
2037 
2038 // -----------------------------------------------------------------------------
createViewController(const::rtl::OUString & _ViewName,const Sequence<PropertyValue> & _Arguments,const Reference<XFrame> & _Frame)2039 Reference< XController2 > SAL_CALL ODatabaseDocument::createViewController( const ::rtl::OUString& _ViewName, const Sequence< PropertyValue >& _Arguments, const Reference< XFrame >& _Frame )
2040 {
2041     if ( !_ViewName.equalsAscii( "Default" ) && !_ViewName.equalsAscii( "Preview" ) )
2042         throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
2043     if ( !_Frame.is() )
2044         throw IllegalArgumentException( ::rtl::OUString(), *this, 3 );
2045 
2046     DocumentGuard aGuard( *this );
2047     ::comphelper::ComponentContext aContext( m_pImpl->m_aContext );
2048     aGuard.clear();
2049 
2050     Reference< XController2 > xController;
2051     aContext.createComponent( "org.openoffice.comp.dbu.OApplicationController", xController );
2052 
2053     ::comphelper::NamedValueCollection aInitArgs( _Arguments );
2054     aInitArgs.put( "Frame", _Frame );
2055     if ( _ViewName.equalsAscii( "Preview" ) )
2056         aInitArgs.put( "Preview", sal_Bool( sal_True ) );
2057     Reference< XInitialization > xInitController( xController, UNO_QUERY_THROW );
2058     xInitController->initialize( aInitArgs.getWrappedPropertyValues() );
2059 
2060     return xController;
2061 }
2062 
2063 // -----------------------------------------------------------------------------
2064 //=============================================================================
impl_getTitleHelper_throw()2065 Reference< XTitle > ODatabaseDocument::impl_getTitleHelper_throw()
2066 {
2067     if ( ! m_xTitleHelper.is ())
2068     {
2069         Reference< XUntitledNumbers > xDesktop(
2070             m_pImpl->m_aContext.createComponent( "com.sun.star.frame.Desktop" ),
2071             UNO_QUERY_THROW );
2072         uno::Reference< frame::XModel > xThis   (getThis(), uno::UNO_QUERY_THROW);
2073 
2074         ::framework::TitleHelper* pHelper = new ::framework::TitleHelper(m_pImpl->m_aContext.getLegacyServiceFactory());
2075         m_xTitleHelper.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2076         pHelper->setOwner                   (xThis   );
2077         pHelper->connectWithUntitledNumbers (xDesktop);
2078     }
2079 
2080     return m_xTitleHelper;
2081 }
2082 
2083 //=============================================================================
impl_getUntitledHelper_throw(const uno::Reference<uno::XInterface> & _xComponent)2084 uno::Reference< frame::XUntitledNumbers > ODatabaseDocument::impl_getUntitledHelper_throw(const uno::Reference< uno::XInterface >& _xComponent)
2085 {
2086     if ( !m_xModuleManager.is() )
2087         m_xModuleManager.set( m_pImpl->m_aContext.createComponent( "com.sun.star.frame.ModuleManager" ), UNO_QUERY_THROW );
2088 
2089     ::rtl::OUString sModuleId;
2090     try
2091     {
2092         sModuleId = m_xModuleManager->identify( _xComponent );
2093     }
2094     catch(uno::Exception)
2095     {
2096         // ni
2097     }
2098     uno::Reference< frame::XUntitledNumbers > xNumberedControllers;
2099 
2100     TNumberedController::iterator aFind = m_aNumberedControllers.find(sModuleId);
2101     if ( aFind == m_aNumberedControllers.end() )
2102     {
2103         uno::Reference< frame::XModel > xThis(static_cast< frame::XModel* >(this), uno::UNO_QUERY_THROW);
2104         ::comphelper::NumberedCollection* pHelper = new ::comphelper::NumberedCollection();
2105         xNumberedControllers.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2106 
2107         pHelper->setOwner          (xThis);
2108         //pHelper->setUntitledPrefix (::rtl::OUString::createFromAscii(" : "));
2109 
2110         m_aNumberedControllers.insert(TNumberedController::value_type(sModuleId,xNumberedControllers));
2111     }
2112     else
2113         xNumberedControllers = aFind->second;
2114 
2115     return xNumberedControllers;
2116 }
2117 
2118 //=============================================================================
2119 // css.frame.XTitle
getTitle()2120 ::rtl::OUString SAL_CALL ODatabaseDocument::getTitle()
2121 {
2122     // SYNCHRONIZED ->
2123     DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2124     return impl_getTitleHelper_throw()->getTitle();
2125 }
2126 
2127 //=============================================================================
2128 // css.frame.XTitle
setTitle(const::rtl::OUString & sTitle)2129 void SAL_CALL ODatabaseDocument::setTitle( const ::rtl::OUString& sTitle )
2130 {
2131     // SYNCHRONIZED ->
2132     DocumentGuard aGuard( *this );
2133     impl_getTitleHelper_throw()->setTitle( sTitle );
2134     m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
2135     // <- SYNCHRONIZED
2136 }
2137 
2138 //=============================================================================
2139 // css.frame.XTitleChangeBroadcaster
addTitleChangeListener(const uno::Reference<frame::XTitleChangeListener> & xListener)2140 void SAL_CALL ODatabaseDocument::addTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2141 {
2142     // SYNCHRONIZED ->
2143     DocumentGuard aGuard( *this );
2144 
2145     uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2146     xBroadcaster->addTitleChangeListener( xListener );
2147 }
2148 
2149 //=============================================================================
2150 // css.frame.XTitleChangeBroadcaster
removeTitleChangeListener(const uno::Reference<frame::XTitleChangeListener> & xListener)2151 void SAL_CALL ODatabaseDocument::removeTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2152 {
2153     // SYNCHRONIZED ->
2154     DocumentGuard aGuard( *this );
2155 
2156     uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2157     xBroadcaster->removeTitleChangeListener( xListener );
2158 }
2159 
2160 //=============================================================================
2161 // css.frame.XUntitledNumbers
leaseNumber(const uno::Reference<uno::XInterface> & xComponent)2162 ::sal_Int32 SAL_CALL ODatabaseDocument::leaseNumber( const uno::Reference< uno::XInterface >& xComponent )
2163 {
2164     DocumentGuard aGuard( *this );
2165     return impl_getUntitledHelper_throw(xComponent)->leaseNumber (xComponent);
2166 }
2167 
2168 //=============================================================================
2169 // css.frame.XUntitledNumbers
releaseNumber(::sal_Int32 nNumber)2170 void SAL_CALL ODatabaseDocument::releaseNumber( ::sal_Int32 nNumber )
2171 {
2172     DocumentGuard aGuard( *this );
2173     impl_getUntitledHelper_throw()->releaseNumber (nNumber);
2174 }
2175 
2176 //=============================================================================
2177 // css.frame.XUntitledNumbers
releaseNumberForComponent(const uno::Reference<uno::XInterface> & xComponent)2178 void SAL_CALL ODatabaseDocument::releaseNumberForComponent( const uno::Reference< uno::XInterface >& xComponent )
2179 {
2180     DocumentGuard aGuard( *this );
2181     impl_getUntitledHelper_throw(xComponent)->releaseNumberForComponent (xComponent);
2182 }
2183 
2184 //=============================================================================
2185 // css.frame.XUntitledNumbers
getUntitledPrefix()2186 ::rtl::OUString SAL_CALL ODatabaseDocument::getUntitledPrefix()
2187 {
2188     return ::rtl::OUString();/*RTL_CONSTASCII_USTRINGPARAM(" : "));*/
2189 }
2190 
2191 //------------------------------------------------------------------
2192 //........................................................................
2193 }   // namespace dbaccess
2194 //........................................................................
2195