1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "core_resource.hxx"
21 #include "core_resource.hrc"
22 #include "datasource.hxx"
23 #include "databasedocument.hxx"
24 #include "dbastrings.hrc"
25 #include "module_dba.hxx"
26 #include "services.hxx"
27 #include "documenteventexecutor.hxx"
28 #include "databasecontext.hxx"
29 #include "documentcontainer.hxx"
30 #include "sdbcoretools.hxx"
31 #include "recovery/dbdocrecovery.hxx"
33 #include <com/sun/star/beans/Optional.hpp>
34 #include <com/sun/star/document/XExporter.hpp>
35 #include <com/sun/star/document/XFilter.hpp>
36 #include <com/sun/star/document/XImporter.hpp>
37 #include <com/sun/star/document/GraphicObjectResolver.hpp>
38 #include <com/sun/star/embed/EntryInitModes.hpp>
39 #include <com/sun/star/embed/XEmbedPersist.hpp>
40 #include <com/sun/star/embed/XTransactedObject.hpp>
41 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
42 #include <com/sun/star/frame/Desktop.hpp>
43 #include <com/sun/star/frame/ModuleManager.hpp>
44 #include <com/sun/star/io/XActiveDataSource.hpp>
45 #include <com/sun/star/io/XSeekable.hpp>
46 #include <com/sun/star/io/XOutputStream.hpp>
47 #include <com/sun/star/io/XTruncate.hpp>
48 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
49 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
50 #include <com/sun/star/sdb/DatabaseContext.hpp>
51 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
52 #include <com/sun/star/task/XStatusIndicator.hpp>
53 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
54 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
55 #include <com/sun/star/ucb/XContent.hpp>
56 #include <com/sun/star/ui/UIConfigurationManager.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/xml/sax/Writer.hpp>
62 #include <com/sun/star/script/XStorageBasedLibraryContainer.hpp>
63 #include <com/sun/star/awt/XControl.hpp>
64 #include <com/sun/star/awt/DialogProvider.hpp>
65 #include <com/sun/star/document/XGraphicObjectResolver.hpp>
67 #include <comphelper/documentconstants.hxx>
68 #include <comphelper/enumhelper.hxx>
69 #include <comphelper/genericpropertyset.hxx>
70 #include <comphelper/interaction.hxx>
71 #include <comphelper/namedvaluecollection.hxx>
72 #include <comphelper/numberedcollection.hxx>
73 #include <comphelper/property.hxx>
74 #include <comphelper/storagehelper.hxx>
76 #include <connectivity/dbtools.hxx>
78 #include <cppuhelper/exc_hlp.hxx>
79 #include <cppuhelper/supportsservice.hxx>
80 #include <framework/titlehelper.hxx>
81 #include <unotools/saveopt.hxx>
82 #include <tools/debug.hxx>
83 #include <tools/diagnose_ex.h>
84 #include <osl/diagnose.h>
85 #include <tools/errcode.hxx>
87 #include <boost/bind.hpp>
93 #include <svtools/grfmgr.hxx>
95 using namespace ::com::sun::star::uno
;
96 using namespace ::com::sun::star::beans
;
97 using namespace ::com::sun::star::frame
;
98 using namespace ::com::sun::star::lang
;
99 using namespace ::com::sun::star::container
;
100 using namespace ::com::sun::star::document
;
101 using namespace ::com::sun::star::io
;
102 using namespace ::com::sun::star::util
;
103 using namespace ::com::sun::star::embed
;
104 using namespace ::com::sun::star::task
;
105 using namespace ::com::sun::star::view
;
106 using namespace ::com::sun::star::sdb
;
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
;
116 using ::com::sun::star::awt::XWindow
;
117 using ::com::sun::star::ucb::XContent
;
118 using ::com::sun::star::sdb::application::XDatabaseDocumentUI
;
125 bool ViewMonitor::onControllerConnected( const Reference
< XController
>& _rxController
)
127 bool bFirstControllerEver
= !m_bEverHadController
;
128 m_bEverHadController
= true;
130 m_xLastConnectedController
= _rxController
;
131 m_bLastIsFirstEverController
= bFirstControllerEver
;
133 return bFirstControllerEver
;
136 bool ViewMonitor::onSetCurrentController( const Reference
< XController
>& _rxController
)
138 // we interpret this as "loading the document (including UI) is finished",
139 // if and only if this is the controller which was last connected, and it was the
140 // first controller ever connected
141 bool bLoadFinished
= ( _rxController
== m_xLastConnectedController
) && m_bLastIsFirstEverController
;
143 // notify the respective events
145 m_rEventNotifier
.notifyDocumentEventAsync( m_bIsNewDocument
? "OnNew" : "OnLoad" );
147 return bLoadFinished
;
150 } // namespace dbaccess
154 extern "C" void SAL_CALL
createRegistryInfo_ODatabaseDocument()
156 static ::dba::OAutoRegistration
< ::dbaccess::ODatabaseDocument
> aAutoRegistration
;
162 ODatabaseDocument::ODatabaseDocument(const ::rtl::Reference
<ODatabaseModelImpl
>& _pImpl
)
163 :ModelDependentComponent( _pImpl
)
164 ,ODatabaseDocument_OfficeDocument( getMutex() )
165 ,m_aModifyListeners( getMutex() )
166 ,m_aCloseListener( getMutex() )
167 ,m_aStorageListeners( getMutex() )
168 ,m_pEventContainer( new DocumentEvents( *this, getMutex(), _pImpl
->getDocumentEvents() ) )
169 ,m_pEventExecutor( NULL
) // initialized below, ref-count-protected
170 ,m_aEventNotifier( *this, getMutex() )
171 ,m_aViewMonitor( m_aEventNotifier
)
172 ,m_eInitState( NotInitialized
)
174 ,m_bAllowDocumentScripting( false )
175 ,m_bHasBeenRecovered( false )
178 OSL_TRACE( "DD: ctor: %p: %p", this, m_pImpl
.get() );
180 osl_atomic_increment( &m_refCount
);
182 impl_reparent_nothrow( m_xForms
);
183 impl_reparent_nothrow( m_xReports
);
184 impl_reparent_nothrow( m_pImpl
->m_xTableDefinitions
);
185 impl_reparent_nothrow( m_pImpl
->m_xCommandDefinitions
);
187 m_pEventExecutor
= new DocumentEventExecutor( m_pImpl
->m_aContext
, this );
189 osl_atomic_decrement( &m_refCount
);
191 // if there previously was a document instance for the same Impl which was already initialized,
192 // then consider ourself initialized, too.
194 if ( m_pImpl
->hadInitializedDocument() )
196 // Note we set our init-state to "Initializing", not "Initialized". We're created from inside the ModelImpl,
197 // which is expected to call attachResource in case there was a previous incarnation of the document,
198 // so we can properly finish our initialization then.
199 impl_setInitializing();
201 if ( !m_pImpl
->getURL().isEmpty() )
203 // if the previous incarnation of the DatabaseDocument already had an URL, then creating this incarnation
204 // here is effectively loading the document.
206 m_aViewMonitor
.onLoadedDocument();
211 ODatabaseDocument::~ODatabaseDocument()
213 OSL_TRACE( "DD: dtor: %p: %p", this, m_pImpl
.get() );
214 if ( !ODatabaseDocument_OfficeDocument::rBHelper
.bInDispose
&& !ODatabaseDocument_OfficeDocument::rBHelper
.bDisposed
)
220 delete m_pEventContainer
, m_pEventContainer
= NULL
;
223 Any SAL_CALL
ODatabaseDocument::queryInterface( const Type
& _rType
) throw (RuntimeException
, std::exception
)
225 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
226 // which already contains macros. In this case, the database document itself is not
227 // allowed to contain macros, too.
228 if ( !m_bAllowDocumentScripting
229 && ( _rType
.equals( cppu::UnoType
<XEmbeddedScripts
>::get() )
230 || _rType
.equals( cppu::UnoType
<XScriptInvocationContext
>::get() )
235 Any aReturn
= ODatabaseDocument_OfficeDocument::queryInterface(_rType
);
236 if (!aReturn
.hasValue())
237 aReturn
= ODatabaseDocument_Title::queryInterface(_rType
);
241 void SAL_CALL
ODatabaseDocument::acquire( ) throw ()
243 ODatabaseDocument_OfficeDocument::acquire();
246 void SAL_CALL
ODatabaseDocument::release( ) throw ()
248 ODatabaseDocument_OfficeDocument::release();
251 Sequence
< Type
> SAL_CALL
ODatabaseDocument::getTypes( ) throw (RuntimeException
, std::exception
)
253 Sequence
< Type
> aTypes
= ::comphelper::concatSequences(
254 ODatabaseDocument_OfficeDocument::getTypes(),
255 ODatabaseDocument_Title::getTypes()
258 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
259 // which already contains macros. In this case, the database document itself is not
260 // allowed to contain macros, too.
261 if ( !m_bAllowDocumentScripting
)
263 Sequence
< Type
> aStrippedTypes( aTypes
.getLength() );
264 Type
* pStripTo( aStrippedTypes
.getArray() );
266 // strip XEmbeddedScripts, and immediately re-assign to aTypes
267 aTypes
= Sequence
< Type
>(
269 ::std::remove_copy_if(
270 aTypes
.getConstArray(),
271 aTypes
.getConstArray() + aTypes
.getLength(),
273 ::std::bind2nd( ::std::equal_to
< Type
>(), cppu::UnoType
<XEmbeddedScripts
>::get() )
277 // strip XScriptInvocationContext, and immediately re-assign to aTypes
278 aTypes
= Sequence
< Type
>(
280 ::std::remove_copy_if(
281 aTypes
.getConstArray(),
282 aTypes
.getConstArray() + aTypes
.getLength(),
284 ::std::bind2nd( ::std::equal_to
< Type
>(), cppu::UnoType
<XScriptInvocationContext
>::get() )
292 Sequence
< sal_Int8
> SAL_CALL
ODatabaseDocument::getImplementationId( ) throw (RuntimeException
, std::exception
)
294 return css::uno::Sequence
<sal_Int8
>();
300 Reference
< XStatusIndicator
> lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection
& _rArguments
)
302 Reference
< XStatusIndicator
> xStatusIndicator
;
303 return _rArguments
.getOrDefault( "StatusIndicator", xStatusIndicator
);
306 static void lcl_triggerStatusIndicator_throw( const ::comphelper::NamedValueCollection
& _rArguments
, DocumentGuard
& _rGuard
, const bool _bStart
)
308 Reference
< XStatusIndicator
> xStatusIndicator( lcl_extractStatusIndicator( _rArguments
) );
309 if ( !xStatusIndicator
.is() )
316 xStatusIndicator
->start( OUString(), (sal_Int32
)1000000 );
318 xStatusIndicator
->end();
320 catch( const Exception
& )
322 DBG_UNHANDLED_EXCEPTION();
325 // note that |reset| can throw a DisposedException
328 static void lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection
& _rArguments
, Sequence
< Any
>& _rCallArgs
)
330 Reference
< XStatusIndicator
> xStatusIndicator( lcl_extractStatusIndicator( _rArguments
) );
331 if ( !xStatusIndicator
.is() )
334 sal_Int32 nLength
= _rCallArgs
.getLength();
335 _rCallArgs
.realloc( nLength
+ 1 );
336 _rCallArgs
[ nLength
] <<= xStatusIndicator
;
339 static void lcl_extractAndStartStatusIndicator( const ::comphelper::NamedValueCollection
& _rArguments
, Reference
< XStatusIndicator
>& _rxStatusIndicator
,
340 Sequence
< Any
>& _rCallArgs
)
342 _rxStatusIndicator
= lcl_extractStatusIndicator( _rArguments
);
343 if ( !_rxStatusIndicator
.is() )
348 _rxStatusIndicator
->start( OUString(), (sal_Int32
)1000000 );
350 sal_Int32 nLength
= _rCallArgs
.getLength();
351 _rCallArgs
.realloc( nLength
+ 1 );
352 _rCallArgs
[ nLength
] <<= _rxStatusIndicator
;
354 catch( const Exception
& )
356 DBG_UNHANDLED_EXCEPTION();
360 static Sequence
< PropertyValue
> lcl_appendFileNameToDescriptor( const ::comphelper::NamedValueCollection
& _rDescriptor
, const OUString
& _rURL
)
362 ::comphelper::NamedValueCollection
aMutableDescriptor( _rDescriptor
);
363 if ( !_rURL
.isEmpty() )
365 aMutableDescriptor
.put( "FileName", _rURL
);
366 aMutableDescriptor
.put( "URL", _rURL
);
368 return aMutableDescriptor
.getPropertyValues();
372 static const char sPictures
[] = "Pictures";
374 // base documents seem to have a different behaviour to other documents, the
375 // root storage contents at least seem to be re-used over different saves, thus if there is a
376 // top level Picture directory it is never cleared.
377 // If we delete the 'Pictures' directory then the dialog library storage which does store
378 // any embed images will not work properly. ( this is due to the fact it will
379 // try to load the dialog which will try and access the embed images, if those images are not cached in
380 // memory it will try to read them from the Picture directory which is now gone, so... we have to use this
381 // inglorious hack below which basically will
383 // a) create a temp storage
385 // b) introspect any dialogs for any embed graphics and grab the associate URL(s)
387 // c) populate the temp storage with the associated embed images ( will be stored in a 'Pictures' folder )
389 // d) delete the 'Picture' element from the root storage
391 // e) copy the Pictures element of the temp storage to the root storage
393 // this assumes that we don't use the Pictures folder in the root of the base
394 // document for anything, I believe this is a valid assumption ( as much as
395 // I could check anyway )
397 void lcl_uglyHackToStoreDialogeEmbedImages( const Reference
< XStorageBasedLibraryContainer
>& xDlgCont
, const Reference
< XStorage
>& xStorage
, const Reference
< XModel
>& rxModel
, const Reference
<XComponentContext
>& rxContext
) throw ( RuntimeException
)
399 Sequence
< OUString
> sLibraries
= xDlgCont
->getElementNames();
400 Reference
< XStorage
> xTmpPic
= xStorage
->openStorageElement( "tempPictures", ElementModes::READWRITE
);
402 std::vector
< OUString
> vEmbedImgUrls
;
403 for ( sal_Int32 i
=0; i
< sLibraries
.getLength(); ++i
)
405 OUString
sLibrary( sLibraries
[ i
] );
406 xDlgCont
->loadLibrary( sLibrary
);
407 Reference
< XNameContainer
> xLib
;
408 xDlgCont
->getByName( sLibrary
) >>= xLib
;
411 Sequence
< OUString
> sDialogs
= xLib
->getElementNames();
412 sal_Int32
nDialogs( sDialogs
.getLength() );
413 for ( sal_Int32 j
=0; j
< nDialogs
; ++j
)
415 Reference
< awt::XDialogProvider
> xDlgPrv
= awt::DialogProvider::createWithModel(rxContext
, rxModel
);
416 OUString sDialogUrl
= "vnd.sun.star.script:";
417 sDialogUrl
= sDialogUrl
.concat( sLibraries
[ i
] ).concat( "." ).concat ( sDialogs
[ j
] ).concat( "?location=document" );
419 Reference
< ::com::sun::star::awt::XControl
> xDialog( xDlgPrv
->createDialog( sDialogUrl
), UNO_QUERY
);
420 Reference
< XInterface
> xModel( xDialog
->getModel() );
421 GraphicObject::InspectForGraphicObjectImageURL( xModel
, vEmbedImgUrls
);
425 // if we have any image urls, make sure we copy the associated images into tempPictures
426 if ( !vEmbedImgUrls
.empty() )
428 // Export the images to the storage
429 Reference
< XGraphicObjectResolver
> xGraphicResolver
= GraphicObjectResolver::createWithStorage(rxContext
, xTmpPic
);
430 std::vector
< OUString
>::iterator it
= vEmbedImgUrls
.begin();
431 std::vector
< OUString
>::iterator it_end
= vEmbedImgUrls
.end();
432 if ( xGraphicResolver
.is() )
434 for ( sal_Int32 count
= 0; it
!= it_end
; ++it
, ++count
)
435 xGraphicResolver
->resolveGraphicObjectURL( *it
);
438 // delete old 'Pictures' storage and copy the contents of tempPictures into xStorage
439 xStorage
->removeElement( sPictures
);
440 xTmpPic
->copyElementTo( sPictures
, xStorage
, sPictures
);
444 // clean up an existing Pictures dir
445 if ( xStorage
->isStorageElement( sPictures
) )
446 xStorage
->removeElement( sPictures
);
450 void ODatabaseDocument::impl_setInitialized()
452 m_eInitState
= Initialized
;
454 // start event notifications
455 m_aEventNotifier
.onDocumentInitialized();
458 void ODatabaseDocument::impl_reset_nothrow()
462 m_pImpl
->clearConnections();
463 m_pImpl
->disposeStorages();
464 m_pImpl
->resetRootStorage();
466 clearObjectContainer( m_xForms
);
467 clearObjectContainer( m_xReports
);
468 clearObjectContainer( m_pImpl
->m_xTableDefinitions
);
469 clearObjectContainer( m_pImpl
->m_xCommandDefinitions
);
471 m_eInitState
= NotInitialized
;
475 catch(const Exception
&)
477 DBG_UNHANDLED_EXCEPTION();
479 m_pImpl
->m_bDocumentReadOnly
= false;
484 /** property map for import/exmport info set */
485 comphelper::PropertyMapEntry
const aExportInfoMap
[] =
487 { OUString("BaseURI"), 0, ::cppu::UnoType
<OUString
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0 },
488 { OUString("StreamName"), 0, ::cppu::UnoType
<OUString
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0 },
489 { OUString("UsePrettyPrinting"), 0, ::cppu::UnoType
<sal_Bool
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0},
490 { OUString(), 0, css::uno::Type(), 0, 0 }
494 void ODatabaseDocument::impl_import_nolck_throw( const Reference
< XComponentContext
>& _rContext
, const Reference
< XInterface
>& _rxTargetComponent
,
495 const ::comphelper::NamedValueCollection
& _rResource
)
497 Sequence
< Any
> aFilterCreationArgs
;
498 Reference
< XStatusIndicator
> xStatusIndicator
;
499 lcl_extractAndStartStatusIndicator( _rResource
, xStatusIndicator
, aFilterCreationArgs
);
501 uno::Reference
< beans::XPropertySet
> xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap
) ) );
502 xInfoSet
->setPropertyValue("BaseURI", uno::makeAny(_rResource
.getOrDefault("URL",OUString())));
503 xInfoSet
->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
505 const sal_Int32 nCount
= aFilterCreationArgs
.getLength();
506 aFilterCreationArgs
.realloc(nCount
+ 1);
507 aFilterCreationArgs
[nCount
] <<= xInfoSet
;
509 Reference
< XImporter
> xImporter(
510 _rContext
->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.comp.sdb.DBFilter", aFilterCreationArgs
, _rContext
),
513 Reference
< XComponent
> xComponent( _rxTargetComponent
, UNO_QUERY_THROW
);
514 xImporter
->setTargetDocument( xComponent
);
516 Reference
< XFilter
> xFilter( xImporter
, UNO_QUERY_THROW
);
517 Sequence
< PropertyValue
> aFilterArgs( ODatabaseModelImpl::stripLoadArguments( _rResource
).getPropertyValues() );
518 xFilter
->filter( aFilterArgs
);
520 if ( xStatusIndicator
.is() )
521 xStatusIndicator
->end();
524 void SAL_CALL
ODatabaseDocument::initNew( ) throw (DoubleInitializationException
, IOException
, Exception
, RuntimeException
, std::exception
)
527 DocumentGuard
aGuard( *this, DocumentGuard::InitMethod
);
529 impl_reset_nothrow();
531 impl_setInitializing();
533 // create a temporary storage
534 Reference
< XStorage
> xTempStor( ::comphelper::OStorageHelper::GetTemporaryStorage( m_pImpl
->m_aContext
) );
537 impl_storeToStorage_throw( xTempStor
, Sequence
< PropertyValue
>(), aGuard
);
539 // let the impl know we're now based on this storage
540 m_pImpl
->switchToStorage( xTempStor
);
542 // for the newly created document, allow document-wide scripting
543 m_bAllowDocumentScripting
= true;
545 impl_setInitialized();
547 m_aEventNotifier
.notifyDocumentEventAsync( "OnTitleChanged" );
549 impl_setModified_nothrow( false, aGuard
);
552 m_aEventNotifier
.notifyDocumentEvent( "OnCreate" );
554 impl_notifyStorageChange_nolck_nothrow( xTempStor
);
557 void SAL_CALL
ODatabaseDocument::load( const Sequence
< PropertyValue
>& _Arguments
) throw (DoubleInitializationException
, IOException
, Exception
, RuntimeException
, std::exception
)
560 DocumentGuard
aGuard( *this, DocumentGuard::InitMethod
);
562 impl_reset_nothrow();
564 ::comphelper::NamedValueCollection
aResource( _Arguments
);
565 if ( aResource
.has( "FileName" ) && !aResource
.has( "URL" ) )
566 // FileName is the compatibility name for URL, so we might have clients passing
567 // a FileName only. However, some of our code works with the URL only, so ensure
569 aResource
.put( "URL", aResource
.get( "FileName" ) );
570 if ( aResource
.has( "URL" ) && !aResource
.has( "FileName" ) )
571 // similar ... just in case there is legacy code which expects a FileName only
572 aResource
.put( "FileName", aResource
.get( "URL" ) );
574 // now that somebody (perhaps) told us an macro execution mode, remember it as
575 // ImposedMacroExecMode
576 m_pImpl
->setImposedMacroExecMode(
577 aResource
.getOrDefault( "MacroExecutionMode", m_pImpl
->getImposedMacroExecMode() ) );
579 impl_setInitializing();
583 impl_import_nolck_throw( m_pImpl
->m_aContext
, *this, aResource
);
586 catch( const Exception
& )
588 impl_reset_nothrow();
591 // tell our view monitor that the document has been loaded - this way it will fire the proper
592 // event (OnLoad instead of OnCreate) later on
593 m_aViewMonitor
.onLoadedDocument();
595 // note that we do *not* call impl_setInitialized() here: The initialization is only complete
596 // when the XModel::attachResource has been called, not sooner.
597 // however, in case of embedding, XModel::attachResource is already called.
599 impl_setInitialized();
601 impl_setModified_nothrow( false, aGuard
);
607 bool lcl_hasAnyModifiedSubComponent_throw( const Reference
< XController
>& i_rController
)
609 Reference
< XDatabaseDocumentUI
> xDatabaseUI( i_rController
, UNO_QUERY_THROW
);
611 Sequence
< Reference
< XComponent
> > aComponents( xDatabaseUI
->getSubComponents() );
612 const Reference
< XComponent
>* component
= aComponents
.getConstArray();
613 const Reference
< XComponent
>* componentsEnd
= aComponents
.getConstArray() + aComponents
.getLength();
615 bool isAnyModified
= false;
616 for ( ; component
!= componentsEnd
; ++component
)
618 Reference
< XModifiable
> xModify( *component
, UNO_QUERY
);
621 isAnyModified
= xModify
->isModified();
625 // TODO: clarify: anything else to care for? Both the sub componbents with and without model
626 // should support the XModifiable interface, so I think nothing more is needed here.
627 OSL_FAIL( "lcl_hasAnyModifiedSubComponent_throw: anything left to do here?" );
630 return isAnyModified
;
634 sal_Bool SAL_CALL
ODatabaseDocument::wasModifiedSinceLastSave() throw ( RuntimeException
, std::exception
)
636 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
638 // The implementation here is somewhat sloppy, in that it returns whether *any* part of the whole
639 // database document, including opened sub components, is modified. This is more than what is requested:
640 // We need to return <TRUE/> if the doc itself, or any of the opened sub components, has been modified
641 // since the last call to any of the save* methods, or since the document has been loaded/created.
642 // However, the API definition explicitly allows to be that sloppy ...
647 // auto recovery is an "UI feature", it is to restore the UI the user knows. Thus,
648 // we ask our connected controllers, not simply our existing form/report definitions.
649 // (There is some information which even cannot be obtained without asking the controller.
650 // For instance, newly created, but not yet saved, forms/reports are accessible via the
651 // controller only, but not via the model.)
655 for ( Controllers::const_iterator ctrl
= m_aControllers
.begin();
656 ctrl
!= m_aControllers
.end();
660 if ( lcl_hasAnyModifiedSubComponent_throw( *ctrl
) )
664 catch( const Exception
& )
666 DBG_UNHANDLED_EXCEPTION();
672 void SAL_CALL
ODatabaseDocument::storeToRecoveryFile( const OUString
& i_TargetLocation
, const Sequence
< PropertyValue
>& i_MediaDescriptor
) throw ( RuntimeException
, IOException
, WrappedTargetException
, std::exception
)
674 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
675 ModifyLock
aLock( *this );
679 // create a storage for the target location
680 Reference
< XStorage
> xTargetStorage( impl_createStorageFor_throw( i_TargetLocation
) );
682 // first store the document as a whole into this storage
683 impl_storeToStorage_throw( xTargetStorage
, i_MediaDescriptor
, aGuard
);
685 // save the sub components which need saving
686 DatabaseDocumentRecovery
aDocRecovery( m_pImpl
->m_aContext
);
687 aDocRecovery
.saveModifiedSubComponents( xTargetStorage
, m_aControllers
);
689 // commit the root storage
690 tools::stor::commitStorageIfWriteable( xTargetStorage
);
692 catch( const Exception
& )
694 Any aError
= ::cppu::getCaughtException();
695 if ( aError
.isExtractableTo( ::cppu::UnoType
< IOException
>::get() )
696 || aError
.isExtractableTo( ::cppu::UnoType
< RuntimeException
>::get() )
697 || aError
.isExtractableTo( ::cppu::UnoType
< WrappedTargetException
>::get() )
704 throw WrappedTargetException( OUString(), *this, aError
);
708 void SAL_CALL
ODatabaseDocument::recoverFromFile( const OUString
& i_SourceLocation
, const OUString
& i_SalvagedFile
, const Sequence
< PropertyValue
>& i_MediaDescriptor
) throw ( RuntimeException
, IOException
, WrappedTargetException
, std::exception
)
712 DocumentGuard
aGuard( *this, DocumentGuard::InitMethod
);
714 if ( i_SourceLocation
.isEmpty() )
715 throw IllegalArgumentException( OUString(), *this, 1 );
718 // load the document itself, by simply delegating to our "load" method
720 // our load implementation expects the SalvagedFile and URL to be in the media descriptor
721 ::comphelper::NamedValueCollection
aMediaDescriptor( i_MediaDescriptor
);
722 aMediaDescriptor
.put( "SalvagedFile", i_SalvagedFile
);
723 aMediaDescriptor
.put( "URL", i_SourceLocation
);
725 aGuard
.clear(); // (load has an own guarding scheme)
726 load( aMediaDescriptor
.getPropertyValues() );
728 // Without a controller, we are unable to recover the sub components, as they're always tied to a controller.
729 // So, everything else is done when the first controller is connected.
730 m_bHasBeenRecovered
= true;
732 // tell the impl that we've been loaded from the given location
733 m_pImpl
->setDocFileLocation( i_SourceLocation
);
735 // by definition (of XDocumentRecovery), we're responsible for delivering a fully-initialized document,
736 // which includes an attachResource call.
737 const OUString
sLogicalDocumentURL( i_SalvagedFile
.isEmpty() ? i_SourceLocation
: i_SalvagedFile
);
738 impl_attachResource( sLogicalDocumentURL
, aMediaDescriptor
.getPropertyValues(), aGuard
);
741 catch( const IOException
& )
745 catch( const RuntimeException
& )
749 catch( const WrappedTargetException
& )
753 catch( const Exception
& )
755 Any aError
= ::cppu::getCaughtException();
756 throw WrappedTargetException( OUString(), *this, aError
);
761 sal_Bool SAL_CALL
ODatabaseDocument::attachResource( const OUString
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
) throw (RuntimeException
, std::exception
)
763 if (_rURL
.isEmpty() && _rArguments
.getLength() == 1 && _rArguments
[0].Name
== "SetEmbedded")
769 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
773 bRet
= impl_attachResource( _rURL
, _rArguments
, aGuard
);
775 catch( const RuntimeException
& )
779 catch( const Exception
& )
781 Any aError
= ::cppu::getCaughtException();
782 throw WrappedTargetRuntimeException( OUString(), *this, aError
);
787 bool ODatabaseDocument::impl_attachResource( const OUString
& i_rLogicalDocumentURL
,
788 const Sequence
< PropertyValue
>& i_rMediaDescriptor
, DocumentGuard
& _rDocGuard
)
790 if ( ( i_rLogicalDocumentURL
== getURL() )
791 && ( i_rMediaDescriptor
.getLength() == 1 )
792 && ( i_rMediaDescriptor
[0].Name
== "BreakMacroSignature" )
795 // this is a BAD hack of the Basic importer code ... there should be a dedicated API for this,
796 // not this bad mis-using of existing interfaces
798 // (we do not support macro signatures, so we can ignore this call)
801 // if no URL has been provided, the caller was lazy enough to not call our getURL - which is not allowed anymore,
802 // now since getURL and getLocation both return the same, so calling one of those should be simple.
803 OUString
sDocumentURL( i_rLogicalDocumentURL
);
804 OSL_ENSURE( !sDocumentURL
.isEmpty(), "ODatabaseDocument::impl_attachResource: invalid URL!" );
805 if ( sDocumentURL
.isEmpty() )
806 sDocumentURL
= getURL();
808 m_pImpl
->setResource( sDocumentURL
, i_rMediaDescriptor
);
810 if ( impl_isInitializing() )
811 { // this means we've just been loaded, and this is the attachResource call which follows
813 impl_setInitialized();
815 // determine whether the document as a whole, or sub documents, have macros. Especially the latter
816 // controls the availability of our XEmbeddedScripts and XScriptInvocationContext interfaces, and we
817 // should know this before anybody actually uses the object.
818 m_bAllowDocumentScripting
= ( m_pImpl
->determineEmbeddedMacros() != ODatabaseModelImpl::eSubDocumentMacros
);
822 m_aEventNotifier
.notifyDocumentEvent( "OnLoadFinished" );
828 OUString SAL_CALL
ODatabaseDocument::getURL( ) throw (RuntimeException
, std::exception
)
830 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
831 return m_pImpl
->getURL();
834 Sequence
< PropertyValue
> SAL_CALL
ODatabaseDocument::getArgs( ) throw (RuntimeException
, std::exception
)
836 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
837 return m_pImpl
->getMediaDescriptor().getPropertyValues();
840 void SAL_CALL
ODatabaseDocument::connectController( const Reference
< XController
>& _xController
) throw (RuntimeException
, std::exception
)
842 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
844 #if OSL_DEBUG_LEVEL > 0
845 for ( Controllers::const_iterator controller
= m_aControllers
.begin();
846 controller
!= m_aControllers
.end();
850 OSL_ENSURE( *controller
!= _xController
, "ODatabaseDocument::connectController: this controller is already connected!" );
854 m_aControllers
.push_back( _xController
);
856 m_aEventNotifier
.notifyDocumentEventAsync( "OnViewCreated", Reference
< XController2
>( _xController
, UNO_QUERY
) );
858 bool bFirstControllerEver
= m_aViewMonitor
.onControllerConnected( _xController
);
859 if ( !bFirstControllerEver
)
862 // check/adjust our macro mode.
863 m_pImpl
->checkMacrosOnLoading();
866 void SAL_CALL
ODatabaseDocument::disconnectController( const Reference
< XController
>& _xController
) throw (RuntimeException
, std::exception
)
868 bool bNotifyViewClosed
= false;
869 bool bLastControllerGone
= false;
870 bool bIsClosing
= false;
874 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
876 Controllers::iterator pos
= ::std::find( m_aControllers
.begin(), m_aControllers
.end(), _xController
);
877 OSL_ENSURE( pos
!= m_aControllers
.end(), "ODatabaseDocument::disconnectController: don't know this controller!" );
878 if ( pos
!= m_aControllers
.end() )
880 m_aControllers
.erase( pos
);
881 bNotifyViewClosed
= true;
884 if ( m_xCurrentController
== _xController
)
885 m_xCurrentController
= NULL
;
887 bLastControllerGone
= m_aControllers
.empty();
888 bIsClosing
= m_bClosing
;
892 if ( bNotifyViewClosed
)
893 m_aEventNotifier
.notifyDocumentEvent( "OnViewClosed", Reference
< XController2
>( _xController
, UNO_QUERY
) );
895 if ( bLastControllerGone
&& !bIsClosing
)
897 // if this was the last view, close the document as a whole
903 catch( const CloseVetoException
& )
905 // okay, somebody vetoed and took ownership
910 void SAL_CALL
ODatabaseDocument::lockControllers( ) throw (RuntimeException
, std::exception
)
912 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
914 ++m_pImpl
->m_nControllerLockCount
;
917 void SAL_CALL
ODatabaseDocument::unlockControllers( ) throw (RuntimeException
, std::exception
)
919 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
921 --m_pImpl
->m_nControllerLockCount
;
924 sal_Bool SAL_CALL
ODatabaseDocument::hasControllersLocked( ) throw (RuntimeException
, std::exception
)
926 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
928 return m_pImpl
->m_nControllerLockCount
!= 0;
931 Reference
< XController
> SAL_CALL
ODatabaseDocument::getCurrentController() throw (RuntimeException
, std::exception
)
933 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
935 return m_xCurrentController
.is() ? m_xCurrentController
: ( m_aControllers
.empty() ? Reference
< XController
>() : *m_aControllers
.begin() );
938 void SAL_CALL
ODatabaseDocument::setCurrentController( const Reference
< XController
>& _xController
) throw (NoSuchElementException
, RuntimeException
, std::exception
)
940 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
942 m_xCurrentController
= _xController
;
944 if ( !m_aViewMonitor
.onSetCurrentController( _xController
) )
947 // check if there are sub components to recover from our document storage
948 bool bAttemptRecovery
= m_bHasBeenRecovered
;
949 if ( !bAttemptRecovery
&& m_pImpl
->getMediaDescriptor().has( "ForceRecovery" ) )
950 // do not use getOrDefault, it will throw for invalid types, which is not desired here
951 m_pImpl
->getMediaDescriptor().get( "ForceRecovery" ) >>= bAttemptRecovery
;
953 if ( !bAttemptRecovery
)
958 DatabaseDocumentRecovery
aDocRecovery( m_pImpl
->m_aContext
);
959 aDocRecovery
.recoverSubDocuments( m_pImpl
->getRootStorage(), _xController
);
961 catch( const Exception
& )
963 DBG_UNHANDLED_EXCEPTION();
967 Reference
< XInterface
> SAL_CALL
ODatabaseDocument::getCurrentSelection( ) throw (RuntimeException
, std::exception
)
969 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
971 Reference
< XInterface
> xRet
;
972 Reference
< XSelectionSupplier
> xDocView( getCurrentController(), UNO_QUERY
);
974 xRet
.set(xDocView
->getSelection(),UNO_QUERY
);
980 sal_Bool SAL_CALL
ODatabaseDocument::hasLocation( ) throw (RuntimeException
, std::exception
)
982 return !getLocation().isEmpty();
985 OUString SAL_CALL
ODatabaseDocument::getLocation( ) throw (RuntimeException
, std::exception
)
987 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
988 return m_pImpl
->getURL();
989 // both XStorable::getLocation and XModel::getURL have to return the URL of the document, *not*
990 // the location of the file which the docunment was possibly recovered from (which would be getDocFileLocation)
993 sal_Bool SAL_CALL
ODatabaseDocument::isReadonly( ) throw (RuntimeException
, std::exception
)
995 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
996 return m_pImpl
->m_bDocumentReadOnly
;
999 void SAL_CALL
ODatabaseDocument::store( ) throw (IOException
, RuntimeException
, std::exception
)
1001 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1003 OUString
sDocumentURL( m_pImpl
->getURL() );
1004 if ( !sDocumentURL
.isEmpty() )
1006 if ( m_pImpl
->getDocFileLocation() == m_pImpl
->getURL() )
1007 if ( m_pImpl
->m_bDocumentReadOnly
)
1008 throw IOException();
1010 impl_storeAs_throw( m_pImpl
->getURL(), m_pImpl
->getMediaDescriptor(), SAVE
, aGuard
);
1014 // if we have no URL, but did survive the DocumentGuard above, then we've been inited via XLoadable::initNew,
1015 // i.e. we're based on a temporary storage
1016 OSL_ENSURE( m_pImpl
->getDocFileLocation().isEmpty(), "ODatabaseDocument::store: unexpected URL inconsistency!" );
1020 impl_storeToStorage_throw( m_pImpl
->getRootStorage(), m_pImpl
->getMediaDescriptor().getPropertyValues(), aGuard
);
1022 catch( const Exception
& )
1024 Any aError
= ::cppu::getCaughtException();
1025 if ( aError
.isExtractableTo( ::cppu::UnoType
< IOException
>::get() )
1026 || aError
.isExtractableTo( ::cppu::UnoType
< RuntimeException
>::get() )
1032 impl_throwIOExceptionCausedBySave_throw( aError
, OUString() );
1036 void ODatabaseDocument::impl_throwIOExceptionCausedBySave_throw( const Any
& i_rError
, const OUString
& i_rTargetURL
) const
1038 OUString sErrorMessage
= extractExceptionMessage( m_pImpl
->m_aContext
, i_rError
);
1039 sErrorMessage
= ResourceManager::loadString(
1040 RID_STR_ERROR_WHILE_SAVING
,
1041 "$location$", i_rTargetURL
,
1042 "$message$", sErrorMessage
1044 throw IOException( sErrorMessage
, *const_cast< ODatabaseDocument
* >( this ) );
1047 void ODatabaseDocument::impl_storeAs_throw( const OUString
& _rURL
, const ::comphelper::NamedValueCollection
& _rArguments
,
1048 const StoreType _eType
, DocumentGuard
& _rGuard
) throw (IOException
, RuntimeException
, std::exception
)
1050 OSL_PRECOND( ( _eType
== SAVE
) || ( _eType
== SAVE_AS
),
1051 "ODatabaseDocument::impl_storeAs_throw: you introduced a new type which cannot be handled here!" );
1053 // if we're in the process of initializing the document (which effectively means it is an implicit
1054 // initialization triggered in storeAsURL), the we do not notify events, since to an observer, the SaveAs
1055 // should not be noticeable
1056 bool bIsInitializationProcess
= impl_isInitializing();
1058 if ( !bIsInitializationProcess
)
1061 m_aEventNotifier
.notifyDocumentEvent( _eType
== SAVE
? "OnSave" : "OnSaveAs", NULL
, makeAny( _rURL
) );
1065 Reference
< XStorage
> xNewRootStorage
;
1066 // will be non-NULL if our storage changed
1070 ModifyLock
aLock( *this );
1071 // ignore all changes of our "modified" state during storing
1073 bool bLocationChanged
= ( _rURL
!= m_pImpl
->getDocFileLocation() );
1074 if ( bLocationChanged
)
1076 // create storage for target URL
1077 Reference
< XStorage
> xTargetStorage( impl_createStorageFor_throw( _rURL
) );
1079 if ( m_pImpl
->isEmbeddedDatabase() )
1080 m_pImpl
->clearConnections();
1082 // commit everything
1083 m_pImpl
->commitEmbeddedStorage();
1084 m_pImpl
->commitStorages();
1086 // copy own storage to target storage
1087 Reference
< XStorage
> xCurrentStorage( m_pImpl
->getRootStorage() );
1088 if ( xCurrentStorage
.is() )
1089 xCurrentStorage
->copyToStorage( xTargetStorage
);
1091 m_pImpl
->disposeStorages();
1093 // each and every document definition obtained via m_xForms and m_xReports depends
1094 // on the sub storages which we just disposed. So, dispose the forms/reports collections, too.
1095 // This ensures that they're re-created when needed.
1096 clearObjectContainer( m_xForms
);
1097 clearObjectContainer( m_xReports
);
1099 xNewRootStorage
= m_pImpl
->switchToStorage( xTargetStorage
);
1101 m_pImpl
->m_bDocumentReadOnly
= false;
1104 // store to current storage
1105 Reference
< XStorage
> xCurrentStorage( m_pImpl
->getOrCreateRootStorage(), UNO_QUERY_THROW
);
1106 Sequence
< PropertyValue
> aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments
, _rURL
) );
1107 impl_storeToStorage_throw( xCurrentStorage
, aMediaDescriptor
, _rGuard
);
1109 // success - tell our impl
1110 m_pImpl
->setDocFileLocation( _rURL
);
1111 m_pImpl
->setResource( _rURL
, aMediaDescriptor
);
1113 // if we are in an initialization process, then this is finished, now that we stored the document
1114 if ( bIsInitializationProcess
)
1115 impl_setInitialized();
1117 catch( const IOException
& )
1119 if ( !bIsInitializationProcess
)
1120 m_aEventNotifier
.notifyDocumentEventAsync( _eType
== SAVE
? "OnSaveFailed" : "OnSaveAsFailed", NULL
, makeAny( _rURL
) );
1123 catch( const RuntimeException
& )
1125 if ( !bIsInitializationProcess
)
1126 m_aEventNotifier
.notifyDocumentEventAsync( _eType
== SAVE
? "OnSaveFailed" : "OnSaveAsFailed", NULL
, makeAny( _rURL
) );
1129 catch( const Exception
& )
1131 Any aError
= ::cppu::getCaughtException();
1133 // notify the failure
1134 if ( !bIsInitializationProcess
)
1135 m_aEventNotifier
.notifyDocumentEventAsync( _eType
== SAVE
? "OnSaveFailed" : "OnSaveAsFailed", NULL
, makeAny( _rURL
) );
1137 impl_throwIOExceptionCausedBySave_throw( aError
, _rURL
);
1140 // notify the document event
1141 if ( !bIsInitializationProcess
)
1142 m_aEventNotifier
.notifyDocumentEventAsync( _eType
== SAVE
? "OnSaveDone" : "OnSaveAsDone", NULL
, makeAny( _rURL
) );
1144 // reset our "modified" flag, and clear the guard
1145 impl_setModified_nothrow( false, _rGuard
);
1148 // notify storage listeners
1149 if ( xNewRootStorage
.is() )
1150 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage
);
1153 Reference
< XStorage
> ODatabaseDocument::impl_createStorageFor_throw( const OUString
& _rURL
) const
1155 Reference
< ucb::XSimpleFileAccess3
> xTempAccess(ucb::SimpleFileAccess::create(m_pImpl
->m_aContext
));
1156 Reference
< io::XStream
> xStream
= xTempAccess
->openFileReadWrite( _rURL
);
1157 Reference
< io::XTruncate
> xTruncate(xStream
,UNO_QUERY
);
1158 if ( xTruncate
.is() )
1160 xTruncate
->truncate();
1162 Sequence
<Any
> aParam(2);
1163 aParam
[0] <<= xStream
;
1164 aParam
[1] <<= ElementModes::READWRITE
| ElementModes::TRUNCATE
;
1166 Reference
< XSingleServiceFactory
> xStorageFactory( m_pImpl
->createStorageFactory(), UNO_SET_THROW
);
1167 return Reference
< XStorage
>( xStorageFactory
->createInstanceWithArguments( aParam
), UNO_QUERY_THROW
);
1170 void SAL_CALL
ODatabaseDocument::storeAsURL( const OUString
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
) throw (IOException
, RuntimeException
, std::exception
)
1173 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
1175 // Normally, a document initialization is done via XLoadable::load or XLoadable::initNew. For convenience
1176 // reasons, and to not break existing API clients, it's allowed to call storeAsURL without having initialized
1177 // the document, in which case the initialization will be done implicitly.
1178 bool bImplicitInitialization
= !impl_isInitialized();
1179 // implicit initialization while another initialization is just running is not possible
1180 if ( bImplicitInitialization
&& impl_isInitializing() )
1181 throw RuntimeException();
1183 if ( bImplicitInitialization
)
1184 impl_setInitializing();
1188 impl_storeAs_throw( _rURL
, _rArguments
, SAVE_AS
, aGuard
);
1191 // impl_storeAs_throw cleared the lock on our mutex, but the below lines need this lock
1195 // our title might have changed, potentially at least
1196 // Sadly, we cannot check this: Calling getTitle here and now would not deliver
1197 // an up-to-date result, as the call is delegated to our TitleHelper instance, which itself
1198 // updates its title only if it gets the OnSaveAsDone event (which was sent asynchronously
1199 // by impl_storeAs_throw). So, we simply notify always, and also asynchronously
1200 m_aEventNotifier
.notifyDocumentEventAsync( "OnTitleChanged" );
1202 catch( const Exception
& )
1204 impl_reset_nothrow();
1208 if ( bImplicitInitialization
)
1209 m_bAllowDocumentScripting
= true;
1214 if ( bImplicitInitialization
)
1215 m_aEventNotifier
.notifyDocumentEvent( "OnCreate" );
1218 void ODatabaseDocument::impl_storeToStorage_throw( const Reference
< XStorage
>& _rxTargetStorage
, const Sequence
< PropertyValue
>& _rMediaDescriptor
,
1219 DocumentGuard
& _rDocGuard
) const
1221 if ( !_rxTargetStorage
.is() )
1222 throw IllegalArgumentException( OUString(), *const_cast< ODatabaseDocument
* >( this ), 1 );
1224 if ( !m_pImpl
.is() )
1225 throw DisposedException( OUString(), *const_cast< ODatabaseDocument
* >( this ) );
1229 // commit everything
1230 m_pImpl
->commitEmbeddedStorage();
1231 m_pImpl
->commitStorages();
1233 // copy own storage to target storage
1234 if ( impl_isInitialized() )
1236 Reference
< XStorage
> xCurrentStorage
= m_pImpl
->getOrCreateRootStorage();
1237 // Root storage may be empty in case of embedding.
1238 if ( xCurrentStorage
.is() && xCurrentStorage
!= _rxTargetStorage
)
1239 xCurrentStorage
->copyToStorage( _rxTargetStorage
);
1242 // write into target storage
1243 ::comphelper::NamedValueCollection
aWriteArgs( _rMediaDescriptor
);
1244 lcl_triggerStatusIndicator_throw( aWriteArgs
, _rDocGuard
, true );
1245 impl_writeStorage_throw( _rxTargetStorage
, aWriteArgs
);
1246 lcl_triggerStatusIndicator_throw( aWriteArgs
, _rDocGuard
, false );
1248 // commit target storage
1249 OSL_VERIFY( tools::stor::commitStorageIfWriteable( _rxTargetStorage
) );
1251 catch( const IOException
& ) { throw; }
1252 catch( const RuntimeException
& ) { throw; }
1253 catch ( const Exception
& e
)
1255 throw IOException( e
.Message
, *const_cast< ODatabaseDocument
* >( this ) );
1259 void SAL_CALL
ODatabaseDocument::storeToURL( const OUString
& _rURL
, const Sequence
< PropertyValue
>& _rArguments
) throw (IOException
, RuntimeException
, std::exception
)
1261 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1262 ModifyLock
aLock( *this );
1266 m_aEventNotifier
.notifyDocumentEvent( "OnSaveTo", NULL
, makeAny( _rURL
) );
1272 // create storage for target URL
1273 Reference
< XStorage
> xTargetStorage( impl_createStorageFor_throw( _rURL
) );
1275 // extend media descriptor with URL
1276 Sequence
< PropertyValue
> aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments
, _rURL
) );
1278 // store to this storage
1279 impl_storeToStorage_throw( xTargetStorage
, aMediaDescriptor
, aGuard
);
1281 catch( const Exception
& )
1283 Any aError
= ::cppu::getCaughtException();
1284 m_aEventNotifier
.notifyDocumentEventAsync( "OnSaveToFailed", NULL
, aError
);
1286 if ( aError
.isExtractableTo( ::cppu::UnoType
< IOException
>::get() )
1287 || aError
.isExtractableTo( ::cppu::UnoType
< RuntimeException
>::get() )
1294 impl_throwIOExceptionCausedBySave_throw( aError
, _rURL
);
1297 m_aEventNotifier
.notifyDocumentEventAsync( "OnSaveToDone", NULL
, makeAny( _rURL
) );
1300 // XModifyBroadcaster
1301 void SAL_CALL
ODatabaseDocument::addModifyListener( const Reference
< XModifyListener
>& _xListener
) throw (RuntimeException
, std::exception
)
1303 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1304 m_aModifyListeners
.addInterface(_xListener
);
1307 void SAL_CALL
ODatabaseDocument::removeModifyListener( const Reference
< XModifyListener
>& _xListener
) throw (RuntimeException
, std::exception
)
1309 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1310 m_aModifyListeners
.removeInterface(_xListener
);
1314 sal_Bool SAL_CALL
ODatabaseDocument::isModified( ) throw (RuntimeException
, std::exception
)
1316 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1318 return m_pImpl
->m_bModified
;
1321 void SAL_CALL
ODatabaseDocument::setModified( sal_Bool _bModified
) throw (PropertyVetoException
, RuntimeException
, std::exception
)
1323 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
1324 if ( impl_isInitialized() )
1325 impl_setModified_nothrow( _bModified
, aGuard
);
1326 // it's allowed to call setModified without the document being initialized already. In this case,
1327 // we simply ignore the call - when the initialization is finished, the respective code will set
1328 // a proper "modified" flag
1331 void ODatabaseDocument::impl_setModified_nothrow( bool _bModified
, DocumentGuard
& _rGuard
)
1334 bool bModifiedChanged
= ( m_pImpl
->m_bModified
!= _bModified
) && ( !m_pImpl
->isModifyLocked() );
1336 if ( bModifiedChanged
)
1338 m_pImpl
->m_bModified
= _bModified
;
1339 m_aEventNotifier
.notifyDocumentEventAsync( "OnModifyChanged" );
1344 if ( bModifiedChanged
)
1346 lang::EventObject
aEvent( *this );
1347 m_aModifyListeners
.notifyEach( &XModifyListener::modified
, aEvent
);
1351 // ::com::sun::star::document::XEventBroadcaster
1352 void SAL_CALL
ODatabaseDocument::addEventListener(const uno::Reference
< document::XEventListener
>& _Listener
) throw (uno::RuntimeException
, std::exception
)
1354 m_aEventNotifier
.addLegacyEventListener( _Listener
);
1357 void SAL_CALL
ODatabaseDocument::removeEventListener( const uno::Reference
< document::XEventListener
>& _Listener
) throw (uno::RuntimeException
, std::exception
)
1359 m_aEventNotifier
.removeLegacyEventListener( _Listener
);
1362 void SAL_CALL
ODatabaseDocument::addDocumentEventListener( const Reference
< XDocumentEventListener
>& _Listener
) throw (RuntimeException
, std::exception
)
1364 m_aEventNotifier
.addDocumentEventListener( _Listener
);
1367 void SAL_CALL
ODatabaseDocument::removeDocumentEventListener( const Reference
< XDocumentEventListener
>& _Listener
) throw (RuntimeException
, std::exception
)
1369 m_aEventNotifier
.removeDocumentEventListener( _Listener
);
1372 void SAL_CALL
ODatabaseDocument::notifyDocumentEvent( const OUString
& _EventName
, const Reference
< XController2
>& _ViewController
, const Any
& _Supplement
) throw (IllegalArgumentException
, NoSupportException
, RuntimeException
, std::exception
)
1374 if ( _EventName
.isEmpty() )
1375 throw IllegalArgumentException( OUString(), *this, 1 );
1378 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1380 if ( !DocumentEvents::needsSynchronousNotification( _EventName
) )
1382 m_aEventNotifier
.notifyDocumentEventAsync( _EventName
, _ViewController
, _Supplement
);
1388 m_aEventNotifier
.notifyDocumentEvent( _EventName
, _ViewController
, _Supplement
);
1391 Sequence
< PropertyValue
> SAL_CALL
ODatabaseDocument::getPrinter( ) throw (RuntimeException
, std::exception
)
1393 OSL_FAIL( "ODatabaseDocument::getPrinter: not supported!" );
1394 return Sequence
< PropertyValue
>();
1397 void SAL_CALL
ODatabaseDocument::setPrinter( const Sequence
< PropertyValue
>& /*aPrinter*/ ) throw (IllegalArgumentException
, RuntimeException
, std::exception
)
1399 OSL_FAIL( "ODatabaseDocument::setPrinter: not supported!" );
1402 void SAL_CALL
ODatabaseDocument::print( const Sequence
< PropertyValue
>& /*xOptions*/ ) throw (IllegalArgumentException
, RuntimeException
, std::exception
)
1404 OSL_FAIL( "ODatabaseDocument::print: not supported!" );
1407 void ODatabaseDocument::impl_reparent_nothrow( const WeakReference
< XNameAccess
>& _rxContainer
)
1409 Reference
< XChild
> xChild( _rxContainer
.get(), UNO_QUERY
);
1411 xChild
->setParent( *this );
1414 void ODatabaseDocument::clearObjectContainer( WeakReference
< XNameAccess
>& _rxContainer
)
1416 Reference
< XNameAccess
> xContainer
= _rxContainer
;
1417 ::comphelper::disposeComponent( xContainer
);
1419 Reference
< XChild
> xChild( _rxContainer
.get(),UNO_QUERY
);
1421 xChild
->setParent( NULL
);
1422 _rxContainer
.clear();
1425 Reference
< XNameAccess
> ODatabaseDocument::impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType _eType
)
1427 if ( ( _eType
!= ODatabaseModelImpl::E_FORM
) && ( _eType
!= ODatabaseModelImpl::E_REPORT
) )
1428 throw IllegalArgumentException();
1430 bool bFormsContainer
= _eType
== ODatabaseModelImpl::E_FORM
;
1432 WeakReference
< XNameAccess
>& rContainerRef( bFormsContainer
? m_xForms
: m_xReports
);
1433 Reference
< XNameAccess
> xContainer
= rContainerRef
;
1434 if ( !xContainer
.is() )
1437 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> xMy(*this);
1438 if ( dbtools::getDataSourceSetting(xMy
,bFormsContainer
? "Forms" : "Reports",aValue
) )
1440 OUString sSupportService
;
1441 aValue
>>= sSupportService
;
1442 if ( !sSupportService
.isEmpty() )
1444 Sequence
<Any
> aArgs(1);
1445 aArgs
[0] <<= NamedValue("DatabaseDocument",makeAny(xMy
));
1447 m_pImpl
->m_aContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sSupportService
, aArgs
, m_pImpl
->m_aContext
),
1449 rContainerRef
= xContainer
;
1452 if ( !xContainer
.is() )
1454 TContentPtr
& rContainerData( m_pImpl
->getObjectContainer( _eType
) );
1455 rContainerRef
= xContainer
= new ODocumentContainer( m_pImpl
->m_aContext
, *this, rContainerData
, bFormsContainer
);
1457 impl_reparent_nothrow( xContainer
);
1462 void ODatabaseDocument::impl_closeControllerFrames_nolck_throw( bool _bDeliverOwnership
)
1464 Controllers aCopy
= m_aControllers
;
1466 Controllers::iterator aEnd
= aCopy
.end();
1467 for ( Controllers::iterator aIter
= aCopy
.begin(); aIter
!= aEnd
; ++aIter
)
1474 Reference
< XCloseable
> xFrame( (*aIter
)->getFrame(), UNO_QUERY
);
1476 xFrame
->close( _bDeliverOwnership
);
1478 catch( const CloseVetoException
& ) { throw; }
1479 catch( const Exception
& )
1481 DBG_UNHANDLED_EXCEPTION();
1486 struct DisposeControllerFrame
: public ::std::unary_function
< Reference
< XController
>, void >
1488 void operator()( const Reference
< XController
>& _rxController
) const
1492 if ( !_rxController
.is() )
1495 Reference
< XFrame
> xFrame( _rxController
->getFrame() );
1496 ::comphelper::disposeComponent( xFrame
);
1498 catch( const Exception
& )
1500 DBG_UNHANDLED_EXCEPTION();
1505 void ODatabaseDocument::impl_disposeControllerFrames_nothrow()
1508 aCopy
.swap( m_aControllers
); // ensure m_aControllers is empty afterwards
1509 ::std::for_each( aCopy
.begin(), aCopy
.end(), DisposeControllerFrame() );
1512 void SAL_CALL
ODatabaseDocument::close( sal_Bool _bDeliverOwnership
) throw (CloseVetoException
, RuntimeException
, std::exception
)
1514 // nearly everything below can/must be done without our mutex locked, the below is just for
1515 // the checks for being disposed and the like
1518 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1519 assert (!m_bClosing
);
1526 // allow listeners to veto
1527 lang::EventObject
aEvent( *this );
1528 m_aCloseListener
.forEach
< XCloseListener
>(
1529 boost::bind( &XCloseListener::queryClosing
, _1
, boost::cref( aEvent
), boost::cref( _bDeliverOwnership
) ) );
1531 // notify that we're going to unload
1532 m_aEventNotifier
.notifyDocumentEvent( "OnPrepareUnload" );
1534 impl_closeControllerFrames_nolck_throw( _bDeliverOwnership
);
1536 m_aCloseListener
.notifyEach( &XCloseListener::notifyClosing
, (const lang::EventObject
&)aEvent
);
1540 catch ( const Exception
& )
1542 ::osl::MutexGuard
aGuard( m_aMutex
);
1548 ::osl::MutexGuard
aGuard( m_aMutex
);
1553 void SAL_CALL
ODatabaseDocument::addCloseListener( const Reference
< ::com::sun::star::util::XCloseListener
>& Listener
) throw (RuntimeException
, std::exception
)
1555 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1556 m_aCloseListener
.addInterface(Listener
);
1559 void SAL_CALL
ODatabaseDocument::removeCloseListener( const Reference
< ::com::sun::star::util::XCloseListener
>& Listener
) throw (RuntimeException
, std::exception
)
1561 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1562 m_aCloseListener
.removeInterface(Listener
);
1565 Reference
< XNameAccess
> SAL_CALL
ODatabaseDocument::getFormDocuments( ) throw (RuntimeException
, std::exception
)
1567 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
1568 return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_FORM
);
1571 Reference
< XNameAccess
> SAL_CALL
ODatabaseDocument::getReportDocuments( ) throw (RuntimeException
, std::exception
)
1573 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
1574 return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_REPORT
);
1577 void ODatabaseDocument::WriteThroughComponent( const Reference
< XComponent
>& xComponent
, const sal_Char
* pStreamName
,
1578 const sal_Char
* pServiceName
, const Sequence
< Any
>& _rArguments
, const Sequence
< PropertyValue
>& rMediaDesc
,
1579 const Reference
<XStorage
>& _xStorageToSaveTo
) const
1581 OSL_ENSURE( pStreamName
, "Need stream name!" );
1582 OSL_ENSURE( pServiceName
, "Need service name!" );
1585 OUString sStreamName
= OUString::createFromAscii( pStreamName
);
1586 Reference
< XStream
> xStream
= _xStorageToSaveTo
->openStreamElement( sStreamName
, ElementModes::READWRITE
| ElementModes::TRUNCATE
);
1587 if ( !xStream
.is() )
1590 Reference
< XOutputStream
> xOutputStream( xStream
->getOutputStream() );
1591 OSL_ENSURE( xOutputStream
.is(), "Can't create output stream in package!" );
1592 if ( !xOutputStream
.is() )
1595 Reference
< XSeekable
> xSeek( xOutputStream
, UNO_QUERY
);
1599 Reference
< XPropertySet
> xStreamProp( xOutputStream
, UNO_QUERY_THROW
);
1600 xStreamProp
->setPropertyValue( INFO_MEDIATYPE
, makeAny( OUString( "text/xml" ) ) );
1601 xStreamProp
->setPropertyValue( "Compressed", makeAny( true ) );
1604 WriteThroughComponent( xOutputStream
, xComponent
, pServiceName
, _rArguments
, rMediaDesc
);
1607 void ODatabaseDocument::WriteThroughComponent( const Reference
< XOutputStream
>& xOutputStream
,
1608 const Reference
< XComponent
>& xComponent
, const sal_Char
* pServiceName
, const Sequence
< Any
>& _rArguments
,
1609 const Sequence
< PropertyValue
>& rMediaDesc
) const
1611 OSL_ENSURE( xOutputStream
.is(), "I really need an output stream!" );
1612 OSL_ENSURE( xComponent
.is(), "Need component!" );
1613 OSL_ENSURE( NULL
!= pServiceName
, "Need component name!" );
1616 Reference
< XWriter
> xSaxWriter
= xml::sax::Writer::create( m_pImpl
->m_aContext
);
1618 // connect XML writer to output stream
1619 xSaxWriter
->setOutputStream( xOutputStream
);
1621 // prepare arguments (prepend doc handler to given arguments)
1622 Reference
< XDocumentHandler
> xDocHandler( xSaxWriter
,UNO_QUERY
);
1623 Sequence
<Any
> aArgs( 1 + _rArguments
.getLength() );
1624 aArgs
[0] <<= xDocHandler
;
1625 for ( sal_Int32 i
= 0; i
< _rArguments
.getLength(); ++i
)
1626 aArgs
[ i
+1 ] = _rArguments
[i
];
1628 // get filter component
1629 Reference
< XExporter
> xExporter( m_pImpl
->m_aContext
->getServiceManager()->createInstanceWithArgumentsAndContext(OUString::createFromAscii(pServiceName
), aArgs
, m_pImpl
->m_aContext
), UNO_QUERY_THROW
);
1631 // connect model and filter
1632 xExporter
->setSourceDocument( xComponent
);
1635 Reference
< XFilter
> xFilter( xExporter
, UNO_QUERY_THROW
);
1636 xFilter
->filter( rMediaDesc
);
1639 void ODatabaseDocument::impl_writeStorage_throw( const Reference
< XStorage
>& _rxTargetStorage
, const ::comphelper::NamedValueCollection
& _rMediaDescriptor
) const
1641 // extract status indicator
1642 Sequence
< Any
> aDelegatorArguments
;
1643 lcl_extractStatusIndicator( _rMediaDescriptor
, aDelegatorArguments
);
1645 uno::Reference
< beans::XPropertySet
> xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap
) ) );
1647 SvtSaveOptions aSaveOpt
;
1648 xInfoSet
->setPropertyValue("UsePrettyPrinting", uno::makeAny(aSaveOpt
.IsPrettyPrinting()));
1649 if ( aSaveOpt
.IsSaveRelFSys() )
1650 xInfoSet
->setPropertyValue("BaseURI", uno::makeAny(_rMediaDescriptor
.getOrDefault("URL",OUString())));
1652 sal_Int32 nArgsLen
= aDelegatorArguments
.getLength();
1653 aDelegatorArguments
.realloc(nArgsLen
+1);
1654 aDelegatorArguments
[nArgsLen
++] <<= xInfoSet
;
1656 Reference
< XPropertySet
> xProp( _rxTargetStorage
, UNO_QUERY_THROW
);
1657 xProp
->setPropertyValue( INFO_MEDIATYPE
, makeAny( OUString(MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
) ) );
1660 SvtSaveOptions::ODFDefaultVersion
const nDefVersion
=
1661 aSaveOpt
.GetODFDefaultVersion();
1662 // older versions can not have this property set,
1663 // it exists only starting from ODF1.2
1664 if (nDefVersion
>= SvtSaveOptions::ODFVER_012
)
1665 aVersion
= ODFVER_012_TEXT
;
1667 if (!aVersion
.isEmpty())
1671 xProp
->setPropertyValue("Version" , uno::makeAny(aVersion
));
1673 catch (const uno::Exception
& e
)
1675 SAL_WARN("dbaccess", "exception setting Version: " << e
.Message
);
1679 Reference
< XComponent
> xComponent( *const_cast< ODatabaseDocument
* >( this ), UNO_QUERY_THROW
);
1681 Sequence
< PropertyValue
> aMediaDescriptor
;
1682 _rMediaDescriptor
>>= aMediaDescriptor
;
1684 xInfoSet
->setPropertyValue("StreamName", uno::makeAny(OUString("settings.xml")));
1685 WriteThroughComponent( xComponent
, "settings.xml", "com.sun.star.comp.sdb.XMLSettingsExporter",
1686 aDelegatorArguments
, aMediaDescriptor
, _rxTargetStorage
);
1688 xInfoSet
->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
1689 WriteThroughComponent( xComponent
, "content.xml", "com.sun.star.comp.sdb.DBExportFilter",
1690 aDelegatorArguments
, aMediaDescriptor
, _rxTargetStorage
);
1692 if ( _rxTargetStorage
->hasByName ( sPictures
) )
1696 // Delete any previously existing Pictures folder and regenerate
1697 // any needed content if needed
1698 Reference
< XStorageBasedLibraryContainer
> xDlgs
= m_pImpl
->getLibraryContainer( false );
1701 Reference
< XModel
> xModel(const_cast< ODatabaseDocument
*>(this));
1702 lcl_uglyHackToStoreDialogeEmbedImages( m_pImpl
->getLibraryContainer(false), _rxTargetStorage
, xModel
, m_pImpl
->m_aContext
);
1705 catch ( const Exception
& )
1707 DBG_UNHANDLED_EXCEPTION();
1710 m_pImpl
->storeLibraryContainersTo( _rxTargetStorage
);
1713 Reference
< XUIConfigurationManager
> SAL_CALL
ODatabaseDocument::getUIConfigurationManager( ) throw (RuntimeException
, std::exception
)
1715 return Reference
< XUIConfigurationManager
>( getUIConfigurationManager2(), UNO_QUERY_THROW
);
1718 Reference
< XUIConfigurationManager2
> ODatabaseDocument::getUIConfigurationManager2( ) throw (RuntimeException
)
1720 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1722 if ( !m_xUIConfigurationManager
.is() )
1724 m_xUIConfigurationManager
= UIConfigurationManager::create( m_pImpl
->m_aContext
);
1726 OUString
aUIConfigFolderName( "Configurations2" );
1727 Reference
< XStorage
> xConfigStorage
;
1729 // First try to open with READWRITE and then READ
1730 xConfigStorage
= getDocumentSubStorage( aUIConfigFolderName
, ElementModes::READWRITE
);
1731 if ( xConfigStorage
.is() )
1733 OUString
aUIConfigMediaType( "application/vnd.sun.xml.ui.configuration" );
1734 OUString aMediaType
;
1735 Reference
< XPropertySet
> xPropSet( xConfigStorage
, UNO_QUERY
);
1736 Any a
= xPropSet
->getPropertyValue( INFO_MEDIATYPE
);
1737 if ( !( a
>>= aMediaType
) || aMediaType
.isEmpty() )
1739 a
<<= aUIConfigMediaType
;
1740 xPropSet
->setPropertyValue( INFO_MEDIATYPE
, a
);
1744 xConfigStorage
= getDocumentSubStorage( aUIConfigFolderName
, ElementModes::READ
);
1746 // initialize ui configuration manager with document substorage
1747 m_xUIConfigurationManager
->setStorage( xConfigStorage
);
1750 return m_xUIConfigurationManager
;
1753 Reference
< XStorage
> SAL_CALL
ODatabaseDocument::getDocumentSubStorage( const OUString
& aStorageName
, sal_Int32 nMode
) throw (RuntimeException
, std::exception
)
1755 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1757 Reference
< XDocumentSubStorageSupplier
> xStorageAccess( m_pImpl
->getDocumentSubStorageSupplier() );
1758 return xStorageAccess
->getDocumentSubStorage( aStorageName
, nMode
);
1761 Sequence
< OUString
> SAL_CALL
ODatabaseDocument::getDocumentSubStoragesNames( ) throw (::com::sun::star::io::IOException
, RuntimeException
, std::exception
)
1763 Reference
< XDocumentSubStorageSupplier
> xStorageAccess( m_pImpl
->getDocumentSubStorageSupplier() );
1764 return xStorageAccess
->getDocumentSubStoragesNames();
1767 void ODatabaseDocument::impl_notifyStorageChange_nolck_nothrow( const Reference
< XStorage
>& _rxNewRootStorage
)
1769 Reference
< XInterface
> xMe( *const_cast< ODatabaseDocument
* >( this ) );
1771 m_aStorageListeners
.forEach
< XStorageChangeListener
>(
1772 boost::bind( &XStorageChangeListener::notifyStorageChange
, _1
, boost::cref( xMe
), boost::cref( _rxNewRootStorage
) ) );
1775 void ODatabaseDocument::disposing()
1777 OSL_TRACE( "DD: disp: %p: %p", this, m_pImpl
.get() );
1778 if ( !m_pImpl
.is() )
1780 // this means that we're already disposed
1781 OSL_ENSURE( ODatabaseDocument_OfficeDocument::rBHelper
.bDisposed
, "ODatabaseDocument::disposing: no impl anymore, but not yet disposed!" );
1785 if ( impl_isInitialized() )
1786 m_aEventNotifier
.notifyDocumentEvent( "OnUnload" );
1788 Reference
< XModel
> xHoldAlive( this );
1790 m_aEventNotifier
.disposing();
1792 lang::EventObject
aDisposeEvent(static_cast<XWeak
*>(this));
1793 m_aModifyListeners
.disposeAndClear( aDisposeEvent
);
1794 m_aCloseListener
.disposeAndClear( aDisposeEvent
);
1795 m_aStorageListeners
.disposeAndClear( aDisposeEvent
);
1797 // this is the list of objects which we currently hold as member. Upon resetting
1798 // those members, we can (potentially) release the last reference to them, in which
1799 // case they will be deleted - if they're C++ implementations, that is :).
1800 // Some of those implementations are offending enough to require the SolarMutex, which
1801 // means we should not release the last reference while our own mutex is locked ...
1802 ::std::list
< Reference
< XInterface
> > aKeepAlive
;
1805 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
1807 OSL_ENSURE( m_aControllers
.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1808 // normally, nobody should explicitly dispose, but only XCloseable::close
1809 // the document. And upon closing, our controllers are closed, too
1812 uno::Reference
<uno::XInterface
> xUIInterface( m_xUIConfigurationManager
);
1813 aKeepAlive
.push_back( xUIInterface
);
1815 m_xUIConfigurationManager
= NULL
;
1817 clearObjectContainer( m_xForms
);
1818 clearObjectContainer( m_xReports
);
1820 // reset the macro mode: in case the our impl struct stays alive (e.g. because our DataSource
1821 // object still exists), and somebody subsequently re-opens the document, we want to have
1822 // the security warning, again.
1823 m_pImpl
->resetMacroExecutionMode();
1825 // similar arguing for our ViewMonitor
1826 m_aViewMonitor
.reset();
1828 // tell our Impl to forget us
1829 m_pImpl
->modelIsDisposing( impl_isInitialized(), ODatabaseModelImpl::ResetModelAccess() );
1831 // now, at the latest, the controller array should be empty. Controllers are
1832 // expected to listen for our disposal, and disconnect then
1833 OSL_ENSURE( m_aControllers
.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1834 impl_disposeControllerFrames_nothrow();
1837 uno::Reference
<uno::XInterface
> xModuleInterface( m_xModuleManager
);
1838 aKeepAlive
.push_back( xModuleInterface
);
1840 m_xModuleManager
.clear();
1843 uno::Reference
<uno::XInterface
> xTitleInterface( m_xTitleHelper
);
1844 aKeepAlive
.push_back( xTitleInterface
);
1846 m_xTitleHelper
.clear();
1857 void SAL_CALL
ODatabaseDocument::dispose( ) throw (RuntimeException
, std::exception
)
1859 ::cppu::WeakComponentImplHelperBase::dispose();
1862 void SAL_CALL
ODatabaseDocument::addEventListener( const Reference
< lang::XEventListener
>& _xListener
) throw (RuntimeException
, std::exception
)
1864 ::cppu::WeakComponentImplHelperBase::addEventListener( _xListener
);
1867 void SAL_CALL
ODatabaseDocument::removeEventListener( const Reference
< lang::XEventListener
>& _xListener
) throw (RuntimeException
, std::exception
)
1869 ::cppu::WeakComponentImplHelperBase::removeEventListener( _xListener
);
1873 OUString
ODatabaseDocument::getImplementationName( ) throw(RuntimeException
, std::exception
)
1875 return getImplementationName_static();
1878 OUString
ODatabaseDocument::getImplementationName_static( ) throw(RuntimeException
)
1880 return OUString("com.sun.star.comp.dba.ODatabaseDocument");
1883 Sequence
< OUString
> ODatabaseDocument::getSupportedServiceNames( ) throw (RuntimeException
, std::exception
)
1885 return getSupportedServiceNames_static();
1888 Reference
< XInterface
> ODatabaseDocument::Create( const Reference
< XComponentContext
>& _rxContext
)
1890 Reference
< XUnoTunnel
> xDBContextTunnel( DatabaseContext::create(_rxContext
), UNO_QUERY_THROW
);
1891 ODatabaseContext
* pContext
= reinterpret_cast< ODatabaseContext
* >( xDBContextTunnel
->getSomething( ODatabaseContext::getUnoTunnelImplementationId() ) );
1893 ::rtl::Reference
<ODatabaseModelImpl
> pImpl( new ODatabaseModelImpl( _rxContext
, *pContext
) );
1894 Reference
< XModel
> xModel( pImpl
->createNewModel_deliverOwnership( false ) );
1895 return xModel
.get();
1898 Sequence
< OUString
> ODatabaseDocument::getSupportedServiceNames_static( ) throw (RuntimeException
)
1900 Sequence
< OUString
> aSNS( 2 );
1901 aSNS
[0] = "com.sun.star.sdb.OfficeDatabaseDocument";
1902 aSNS
[1] = "com.sun.star.document.OfficeDocument";
1906 sal_Bool
ODatabaseDocument::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
, std::exception
)
1908 return cppu::supportsService(this, _rServiceName
);
1911 Reference
< XDataSource
> SAL_CALL
ODatabaseDocument::getDataSource() throw (RuntimeException
, std::exception
)
1913 DocumentGuard
aGuard( *this, DocumentGuard::MethodWithoutInit
);
1914 return m_pImpl
->getOrCreateDataSource();
1919 /// Property map for embedded import info set.
1920 comphelper::PropertyMapEntry
const aEmbeddedImportInfoMap
[] =
1922 {OUString("StreamRelPath"), 0, cppu::UnoType
<OUString
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0},
1923 {OUString("StreamName"), 0, cppu::UnoType
<OUString
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0},
1924 {OUString("SourceStorage"), 0, cppu::UnoType
<embed::XStorage
>::get(), beans::PropertyAttribute::MAYBEVOID
, 0},
1925 {OUString(), 0, css::uno::Type(), 0, 0}
1929 void SAL_CALL
ODatabaseDocument::loadFromStorage(const Reference
<XStorage
>& xStorage
, const Sequence
<PropertyValue
>& rMediaDescriptor
) throw (IllegalArgumentException
, DoubleInitializationException
, IOException
, Exception
, RuntimeException
, std::exception
)
1931 DocumentGuard
aGuard(*this, DocumentGuard::InitMethod
);
1933 uno::Reference
<beans::XPropertySet
> xInfoSet(comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aEmbeddedImportInfoMap
)));
1934 comphelper::NamedValueCollection
aDescriptor(rMediaDescriptor
);
1935 xInfoSet
->setPropertyValue("StreamRelPath", uno::makeAny(aDescriptor
.getOrDefault("HierarchicalDocumentName", OUString())));
1936 xInfoSet
->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
1937 xInfoSet
->setPropertyValue("SourceStorage", uno::makeAny(xStorage
));
1939 uno::Sequence
<uno::Any
> aFilterCreationArgs(1);
1940 aFilterCreationArgs
[0] <<= xInfoSet
;
1942 uno::Reference
<document::XImporter
> xImporter(m_pImpl
->m_aContext
->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.comp.sdb.DBFilter", aFilterCreationArgs
, m_pImpl
->m_aContext
), uno::UNO_QUERY_THROW
);
1944 uno::Reference
<lang::XComponent
> xComponent(*this, uno::UNO_QUERY_THROW
);
1945 xImporter
->setTargetDocument(xComponent
);
1947 uno::Reference
<document::XFilter
> xFilter(xImporter
, uno::UNO_QUERY_THROW
);
1948 uno::Sequence
<beans::PropertyValue
> aFilterArgs
;
1949 xFilter
->filter(aFilterArgs
);
1951 // In case of embedding, XModel::attachResource is already called.
1953 impl_setInitialized();
1955 impl_setModified_nothrow(false, aGuard
);
1958 void SAL_CALL
ODatabaseDocument::storeToStorage( const Reference
< XStorage
>& _rxStorage
, const Sequence
< PropertyValue
>& _rMediaDescriptor
) throw (IllegalArgumentException
, IOException
, Exception
, RuntimeException
, std::exception
)
1960 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1961 impl_storeToStorage_throw( _rxStorage
, _rMediaDescriptor
, aGuard
);
1964 void SAL_CALL
ODatabaseDocument::switchToStorage( const Reference
< XStorage
>& _rxNewRootStorage
) throw (IllegalArgumentException
, IOException
, Exception
, RuntimeException
, std::exception
)
1966 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1968 Reference
< XStorage
> xNewRootStorage( m_pImpl
->switchToStorage( _rxNewRootStorage
) );
1971 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage
);
1974 Reference
< XStorage
> SAL_CALL
ODatabaseDocument::getDocumentStorage( ) throw (IOException
, Exception
, RuntimeException
, std::exception
)
1976 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1977 return m_pImpl
->getOrCreateRootStorage();
1980 void SAL_CALL
ODatabaseDocument::addStorageChangeListener( const Reference
< XStorageChangeListener
>& _Listener
) throw (RuntimeException
, std::exception
)
1982 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1983 m_aStorageListeners
.addInterface( _Listener
);
1986 void SAL_CALL
ODatabaseDocument::removeStorageChangeListener( const Reference
< XStorageChangeListener
>& _Listener
) throw (RuntimeException
, std::exception
)
1988 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
1989 m_aStorageListeners
.addInterface( _Listener
);
1992 Reference
< XStorageBasedLibraryContainer
> SAL_CALL
ODatabaseDocument::getBasicLibraries() throw (RuntimeException
, std::exception
)
1994 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
1995 return m_pImpl
->getLibraryContainer( true );
1998 Reference
< XStorageBasedLibraryContainer
> SAL_CALL
ODatabaseDocument::getDialogLibraries() throw (RuntimeException
, std::exception
)
2000 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2001 return m_pImpl
->getLibraryContainer( false );
2004 sal_Bool SAL_CALL
ODatabaseDocument::getAllowMacroExecution() throw (RuntimeException
, std::exception
)
2006 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2007 return m_pImpl
->adjustMacroMode_AutoReject();
2010 Reference
< XEmbeddedScripts
> SAL_CALL
ODatabaseDocument::getScriptContainer() throw (RuntimeException
, std::exception
)
2012 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2016 Reference
< provider::XScriptProvider
> SAL_CALL
ODatabaseDocument::getScriptProvider( ) throw (RuntimeException
, std::exception
)
2018 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2020 Reference
< XScriptProvider
> xScriptProvider( m_xScriptProvider
);
2021 if ( !xScriptProvider
.is() )
2023 Reference
< XScriptProviderFactory
> xFactory
=
2024 theMasterScriptProviderFactory::get( m_pImpl
->m_aContext
);
2026 Any aScriptProviderContext
;
2027 if ( m_bAllowDocumentScripting
)
2028 aScriptProviderContext
<<= Reference
< XModel
>( this );
2030 xScriptProvider
.set( xFactory
->createScriptProvider( aScriptProviderContext
), UNO_SET_THROW
);
2031 m_xScriptProvider
= xScriptProvider
;
2034 return xScriptProvider
;
2037 Reference
< XNameReplace
> SAL_CALL
ODatabaseDocument::getEvents( ) throw (RuntimeException
, std::exception
)
2039 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
2040 return m_pEventContainer
;
2043 Reference
< XInterface
> ODatabaseDocument::getThis() const
2045 return *const_cast< ODatabaseDocument
* >( this );
2048 struct CreateAny
: public ::std::unary_function
< Reference
<XController
>, Any
>
2050 Any
operator() (const Reference
<XController
>& lhs
) const
2052 return makeAny(lhs
);
2057 Reference
< XEnumeration
> SAL_CALL
ODatabaseDocument::getControllers( ) throw (RuntimeException
, std::exception
)
2059 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2060 uno::Sequence
< Any
> aController( m_aControllers
.size() );
2061 ::std::transform( m_aControllers
.begin(), m_aControllers
.end(), aController
.getArray(), CreateAny() );
2062 return new ::comphelper::OAnyEnumeration(aController
);
2065 Sequence
< OUString
> SAL_CALL
ODatabaseDocument::getAvailableViewControllerNames( ) throw (RuntimeException
, std::exception
)
2067 Sequence
< OUString
> aNames(1);
2068 aNames
[0] = SERVICE_SDB_APPLICATIONCONTROLLER
;
2072 Reference
< XController2
> SAL_CALL
ODatabaseDocument::createDefaultViewController( const Reference
< XFrame
>& _Frame
) throw (IllegalArgumentException
, Exception
, RuntimeException
, std::exception
)
2074 return createViewController( "Default", Sequence
< PropertyValue
>(), _Frame
);
2077 Reference
< XController2
> SAL_CALL
ODatabaseDocument::createViewController( const OUString
& _ViewName
, const Sequence
< PropertyValue
>& _Arguments
, const Reference
< XFrame
>& _Frame
) throw (IllegalArgumentException
, Exception
, RuntimeException
, std::exception
)
2079 if ( _ViewName
!= "Default" && _ViewName
!= "Preview" )
2080 throw IllegalArgumentException( OUString(), *this, 1 );
2082 throw IllegalArgumentException( OUString(), *this, 3 );
2084 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2087 Reference
< XController2
> xController(
2088 m_pImpl
->m_aContext
->getServiceManager()->createInstanceWithContext("org.openoffice.comp.dbu.OApplicationController", m_pImpl
->m_aContext
),
2091 ::comphelper::NamedValueCollection
aInitArgs( _Arguments
);
2092 aInitArgs
.put( "Frame", _Frame
);
2093 if ( _ViewName
== "Preview" )
2094 aInitArgs
.put( "Preview", true );
2095 Reference
< XInitialization
> xInitController( xController
, UNO_QUERY_THROW
);
2096 xInitController
->initialize( aInitArgs
.getWrappedPropertyValues() );
2101 Reference
< XTitle
> ODatabaseDocument::impl_getTitleHelper_throw()
2103 if ( ! m_xTitleHelper
.is ())
2105 Reference
< XUntitledNumbers
> xDesktop(Desktop::create(m_pImpl
->m_aContext
), uno::UNO_QUERY_THROW
);
2106 Reference
< frame::XModel
> xThis (getThis(), uno::UNO_QUERY_THROW
);
2108 ::framework::TitleHelper
* pHelper
= new ::framework::TitleHelper(m_pImpl
->m_aContext
);
2109 m_xTitleHelper
.set(static_cast< ::cppu::OWeakObject
* >(pHelper
), uno::UNO_QUERY_THROW
);
2110 pHelper
->setOwner (xThis
);
2111 pHelper
->connectWithUntitledNumbers (xDesktop
);
2114 return m_xTitleHelper
;
2117 uno::Reference
< frame::XUntitledNumbers
> ODatabaseDocument::impl_getUntitledHelper_throw(const uno::Reference
< uno::XInterface
>& _xComponent
)
2119 if ( !m_xModuleManager
.is() )
2120 m_xModuleManager
.set( ModuleManager::create(m_pImpl
->m_aContext
) );
2125 sModuleId
= m_xModuleManager
->identify( _xComponent
);
2127 catch(const uno::Exception
&)
2130 uno::Reference
< frame::XUntitledNumbers
> xNumberedControllers
;
2132 TNumberedController::iterator aFind
= m_aNumberedControllers
.find(sModuleId
);
2133 if ( aFind
== m_aNumberedControllers
.end() )
2135 uno::Reference
< frame::XModel
> xThis(static_cast< frame::XModel
* >(this), uno::UNO_QUERY_THROW
);
2136 ::comphelper::NumberedCollection
* pHelper
= new ::comphelper::NumberedCollection();
2137 xNumberedControllers
.set(static_cast< ::cppu::OWeakObject
* >(pHelper
), uno::UNO_QUERY_THROW
);
2139 pHelper
->setOwner (xThis
);
2141 m_aNumberedControllers
.insert(TNumberedController::value_type(sModuleId
,xNumberedControllers
));
2144 xNumberedControllers
= aFind
->second
;
2146 return xNumberedControllers
;
2150 OUString SAL_CALL
ODatabaseDocument::getTitle()
2151 throw (uno::RuntimeException
, std::exception
)
2154 DocumentGuard
aGuard( *this, DocumentGuard::MethodUsedDuringInit
);
2155 return impl_getTitleHelper_throw()->getTitle();
2159 void SAL_CALL
ODatabaseDocument::setTitle( const OUString
& sTitle
)
2160 throw (uno::RuntimeException
, std::exception
)
2163 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2164 impl_getTitleHelper_throw()->setTitle( sTitle
);
2165 m_aEventNotifier
.notifyDocumentEventAsync( "OnTitleChanged" );
2169 // css.frame.XTitleChangeBroadcaster
2170 void SAL_CALL
ODatabaseDocument::addTitleChangeListener( const uno::Reference
< frame::XTitleChangeListener
>& xListener
)
2171 throw (uno::RuntimeException
, std::exception
)
2174 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2176 uno::Reference
< frame::XTitleChangeBroadcaster
> xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW
);
2177 xBroadcaster
->addTitleChangeListener( xListener
);
2180 // css.frame.XTitleChangeBroadcaster
2181 void SAL_CALL
ODatabaseDocument::removeTitleChangeListener( const uno::Reference
< frame::XTitleChangeListener
>& xListener
)
2182 throw (uno::RuntimeException
, std::exception
)
2185 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2187 uno::Reference
< frame::XTitleChangeBroadcaster
> xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW
);
2188 xBroadcaster
->removeTitleChangeListener( xListener
);
2191 // css.frame.XUntitledNumbers
2192 ::sal_Int32 SAL_CALL
ODatabaseDocument::leaseNumber( const uno::Reference
< uno::XInterface
>& xComponent
)
2193 throw (lang::IllegalArgumentException
,
2194 uno::RuntimeException
, std::exception
)
2196 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2197 return impl_getUntitledHelper_throw(xComponent
)->leaseNumber (xComponent
);
2200 // css.frame.XUntitledNumbers
2201 void SAL_CALL
ODatabaseDocument::releaseNumber( ::sal_Int32 nNumber
)
2202 throw (lang::IllegalArgumentException
,
2203 uno::RuntimeException
, std::exception
)
2205 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2206 impl_getUntitledHelper_throw()->releaseNumber (nNumber
);
2209 // css.frame.XUntitledNumbers
2210 void SAL_CALL
ODatabaseDocument::releaseNumberForComponent( const uno::Reference
< uno::XInterface
>& xComponent
)
2211 throw (lang::IllegalArgumentException
,
2212 uno::RuntimeException
, std::exception
)
2214 DocumentGuard
aGuard(*this, DocumentGuard::DefaultMethod
);
2215 impl_getUntitledHelper_throw(xComponent
)->releaseNumberForComponent (xComponent
);
2218 // css.frame.XUntitledNumbers
2219 OUString SAL_CALL
ODatabaseDocument::getUntitledPrefix() throw (uno::RuntimeException
, std::exception
)
2224 } // namespace dbaccess
2226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */