build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / dataaccess / databasedocument.cxx
blob2cd295656df9e6ceaf30f553cd9e28482e45263e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "documenteventexecutor.hxx"
26 #include "databasecontext.hxx"
27 #include "documentcontainer.hxx"
28 #include "sdbcoretools.hxx"
29 #include "recovery/dbdocrecovery.hxx"
31 #include <com/sun/star/beans/Optional.hpp>
32 #include <com/sun/star/document/XExporter.hpp>
33 #include <com/sun/star/document/XFilter.hpp>
34 #include <com/sun/star/document/XImporter.hpp>
35 #include <com/sun/star/document/GraphicObjectResolver.hpp>
36 #include <com/sun/star/embed/EntryInitModes.hpp>
37 #include <com/sun/star/embed/XEmbedPersist.hpp>
38 #include <com/sun/star/embed/XTransactedObject.hpp>
39 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
40 #include <com/sun/star/frame/Desktop.hpp>
41 #include <com/sun/star/frame/ModuleManager.hpp>
42 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #include <com/sun/star/io/XSeekable.hpp>
44 #include <com/sun/star/io/XOutputStream.hpp>
45 #include <com/sun/star/io/XTruncate.hpp>
46 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
47 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
48 #include <com/sun/star/sdb/DatabaseContext.hpp>
49 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
50 #include <com/sun/star/task/XStatusIndicator.hpp>
51 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
52 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
53 #include <com/sun/star/ui/UIConfigurationManager.hpp>
54 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
55 #include <com/sun/star/view/XSelectionSupplier.hpp>
56 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
57 #include <com/sun/star/xml/sax/Writer.hpp>
59 #include <com/sun/star/script/XStorageBasedLibraryContainer.hpp>
60 #include <com/sun/star/awt/XControl.hpp>
61 #include <com/sun/star/awt/DialogProvider.hpp>
62 #include <com/sun/star/document/XGraphicObjectResolver.hpp>
64 #include <comphelper/documentconstants.hxx>
65 #include <comphelper/enumhelper.hxx>
66 #include <comphelper/genericpropertyset.hxx>
67 #include <comphelper/interaction.hxx>
68 #include <comphelper/namedvaluecollection.hxx>
69 #include <comphelper/numberedcollection.hxx>
70 #include <comphelper/property.hxx>
71 #include <comphelper/storagehelper.hxx>
73 #include <connectivity/dbtools.hxx>
75 #include <cppuhelper/exc_hlp.hxx>
76 #include <cppuhelper/supportsservice.hxx>
77 #include <framework/titlehelper.hxx>
78 #include <unotools/saveopt.hxx>
79 #include <tools/debug.hxx>
80 #include <tools/diagnose_ex.h>
81 #include <osl/diagnose.h>
82 #include <tools/errcode.hxx>
84 #include <functional>
85 #include <list>
87 #include <svtools/grfmgr.hxx>
88 #include <tools/urlobj.hxx>
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::beans;
92 using namespace ::com::sun::star::frame;
93 using namespace ::com::sun::star::lang;
94 using namespace ::com::sun::star::container;
95 using namespace ::com::sun::star::document;
96 using namespace ::com::sun::star::io;
97 using namespace ::com::sun::star::util;
98 using namespace ::com::sun::star::embed;
99 using namespace ::com::sun::star::task;
100 using namespace ::com::sun::star::view;
101 using namespace ::com::sun::star::sdb;
102 using namespace ::com::sun::star::sdbc;
103 using namespace ::com::sun::star;
104 using namespace ::com::sun::star::xml::sax;
105 using namespace ::com::sun::star::script;
106 using namespace ::com::sun::star::script::provider;
107 using namespace ::com::sun::star::ui;
108 using namespace ::cppu;
110 namespace dbaccess
113 // ViewMonitor
115 bool ViewMonitor::onControllerConnected( const Reference< XController >& _rxController )
117 bool bFirstControllerEver = !m_bEverHadController;
118 m_bEverHadController = true;
120 m_xLastConnectedController = _rxController;
121 m_bLastIsFirstEverController = bFirstControllerEver;
123 return bFirstControllerEver;
126 bool ViewMonitor::onSetCurrentController( const Reference< XController >& _rxController )
128 // we interpret this as "loading the document (including UI) is finished",
129 // if and only if this is the controller which was last connected, and it was the
130 // first controller ever connected
131 bool bLoadFinished = ( _rxController == m_xLastConnectedController ) && m_bLastIsFirstEverController;
133 // notify the respective events
134 if ( bLoadFinished )
135 m_rEventNotifier.notifyDocumentEventAsync( m_bIsNewDocument ? "OnNew" : "OnLoad" );
137 return bLoadFinished;
141 ODatabaseDocument::ODatabaseDocument(const ::rtl::Reference<ODatabaseModelImpl>& _pImpl )
142 :ModelDependentComponent( _pImpl )
143 ,ODatabaseDocument_OfficeDocument( getMutex() )
144 ,m_aModifyListeners( getMutex() )
145 ,m_aCloseListener( getMutex() )
146 ,m_aStorageListeners( getMutex() )
147 ,m_pEventContainer( new DocumentEvents( *this, getMutex(), _pImpl->getDocumentEvents() ) )
148 ,m_pEventExecutor( nullptr ) // initialized below, ref-count-protected
149 ,m_aEventNotifier( *this, getMutex() )
150 ,m_aViewMonitor( m_aEventNotifier )
151 ,m_eInitState( NotInitialized )
152 ,m_bClosing( false )
153 ,m_bAllowDocumentScripting( false )
154 ,m_bHasBeenRecovered( false )
155 ,m_bEmbedded(false)
157 OSL_TRACE( "DD: ctor: %p: %p", this, m_pImpl.get() );
159 osl_atomic_increment( &m_refCount );
161 impl_reparent_nothrow( m_xForms );
162 impl_reparent_nothrow( m_xReports );
163 impl_reparent_nothrow( m_pImpl->m_xTableDefinitions );
164 impl_reparent_nothrow( m_pImpl->m_xCommandDefinitions );
166 m_pEventExecutor = new DocumentEventExecutor( m_pImpl->m_aContext, this );
168 osl_atomic_decrement( &m_refCount );
170 // if there previously was a document instance for the same Impl which was already initialized,
171 // then consider ourself initialized, too.
172 // #i94840#
173 if ( m_pImpl->hadInitializedDocument() )
175 // Note we set our init-state to "Initializing", not "Initialized". We're created from inside the ModelImpl,
176 // which is expected to call attachResource in case there was a previous incarnation of the document,
177 // so we can properly finish our initialization then.
178 impl_setInitializing();
180 if ( !m_pImpl->getURL().isEmpty() )
182 // if the previous incarnation of the DatabaseDocument already had an URL, then creating this incarnation
183 // here is effectively loading the document.
184 // #i105505#
185 m_aViewMonitor.onLoadedDocument();
190 ODatabaseDocument::~ODatabaseDocument()
192 OSL_TRACE( "DD: dtor: %p: %p", this, m_pImpl.get() );
193 if ( !ODatabaseDocument_OfficeDocument::rBHelper.bInDispose && !ODatabaseDocument_OfficeDocument::rBHelper.bDisposed )
195 acquire();
196 dispose();
199 delete m_pEventContainer;
200 m_pEventContainer = nullptr;
203 Any SAL_CALL ODatabaseDocument::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
205 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
206 // which already contains macros. In this case, the database document itself is not
207 // allowed to contain macros, too.
208 if ( !m_bAllowDocumentScripting
209 && ( _rType.equals( cppu::UnoType<XEmbeddedScripts>::get() )
210 || _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() )
213 return Any();
215 Any aReturn = ODatabaseDocument_OfficeDocument::queryInterface(_rType);
216 if (!aReturn.hasValue())
217 aReturn = ODatabaseDocument_Title::queryInterface(_rType);
218 return aReturn;
221 void SAL_CALL ODatabaseDocument::acquire( ) throw ()
223 ODatabaseDocument_OfficeDocument::acquire();
226 void SAL_CALL ODatabaseDocument::release( ) throw ()
228 ODatabaseDocument_OfficeDocument::release();
231 Sequence< Type > SAL_CALL ODatabaseDocument::getTypes( ) throw (RuntimeException, std::exception)
233 Sequence< Type > aTypes = ::comphelper::concatSequences(
234 ODatabaseDocument_OfficeDocument::getTypes(),
235 ODatabaseDocument_Title::getTypes()
238 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
239 // which already contains macros. In this case, the database document itself is not
240 // allowed to contain macros, too.
241 if ( !m_bAllowDocumentScripting )
243 Sequence< Type > aStrippedTypes( aTypes.getLength() );
244 Type* pStripTo( aStrippedTypes.getArray() );
246 // strip XEmbeddedScripts, and immediately re-assign to aTypes
247 aTypes = Sequence< Type >(
248 pStripTo,
249 ::std::remove_copy_if(
250 aTypes.getConstArray(),
251 aTypes.getConstArray() + aTypes.getLength(),
252 pStripTo,
253 ::std::bind2nd( ::std::equal_to< Type >(), cppu::UnoType<XEmbeddedScripts>::get() )
254 ) - pStripTo
257 // strip XScriptInvocationContext, and immediately re-assign to aTypes
258 aTypes = Sequence< Type >(
259 pStripTo,
260 ::std::remove_copy_if(
261 aTypes.getConstArray(),
262 aTypes.getConstArray() + aTypes.getLength(),
263 pStripTo,
264 ::std::bind2nd( ::std::equal_to< Type >(), cppu::UnoType<XScriptInvocationContext>::get() )
265 ) - pStripTo
269 return aTypes;
272 Sequence< sal_Int8 > SAL_CALL ODatabaseDocument::getImplementationId( ) throw (RuntimeException, std::exception)
274 return css::uno::Sequence<sal_Int8>();
277 // local functions
278 namespace
280 Reference< XStatusIndicator > lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments )
282 Reference< XStatusIndicator > xStatusIndicator;
283 return _rArguments.getOrDefault( "StatusIndicator", xStatusIndicator );
286 void lcl_triggerStatusIndicator_throw( const ::comphelper::NamedValueCollection& _rArguments, DocumentGuard& _rGuard, const bool _bStart )
288 Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
289 if ( !xStatusIndicator.is() )
290 return;
292 _rGuard.clear();
295 if ( _bStart )
296 xStatusIndicator->start( OUString(), (sal_Int32)1000000 );
297 else
298 xStatusIndicator->end();
300 catch( const Exception& )
302 DBG_UNHANDLED_EXCEPTION();
304 _rGuard.reset();
305 // note that |reset| can throw a DisposedException
308 void lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Sequence< Any >& _rCallArgs )
310 Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
311 if ( !xStatusIndicator.is() )
312 return;
314 sal_Int32 nLength = _rCallArgs.getLength();
315 _rCallArgs.realloc( nLength + 1 );
316 _rCallArgs[ nLength ] <<= xStatusIndicator;
319 void lcl_extractAndStartStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Reference< XStatusIndicator >& _rxStatusIndicator,
320 Sequence< Any >& _rCallArgs )
322 _rxStatusIndicator = lcl_extractStatusIndicator( _rArguments );
323 if ( !_rxStatusIndicator.is() )
324 return;
328 _rxStatusIndicator->start( OUString(), (sal_Int32)1000000 );
330 sal_Int32 nLength = _rCallArgs.getLength();
331 _rCallArgs.realloc( nLength + 1 );
332 _rCallArgs[ nLength ] <<= _rxStatusIndicator;
334 catch( const Exception& )
336 DBG_UNHANDLED_EXCEPTION();
340 Sequence< PropertyValue > lcl_appendFileNameToDescriptor( const ::comphelper::NamedValueCollection& _rDescriptor, const OUString& _rURL )
342 ::comphelper::NamedValueCollection aMutableDescriptor( _rDescriptor );
343 if ( !_rURL.isEmpty() )
345 aMutableDescriptor.put( "FileName", _rURL );
346 aMutableDescriptor.put( "URL", _rURL );
348 return aMutableDescriptor.getPropertyValues();
352 static const char sPictures[] = "Pictures";
354 // base documents seem to have a different behaviour to other documents, the
355 // root storage contents at least seem to be re-used over different saves, thus if there is a
356 // top level Picture directory it is never cleared.
357 // If we delete the 'Pictures' directory then the dialog library storage which does store
358 // any embed images will not work properly. ( this is due to the fact it will
359 // try to load the dialog which will try and access the embed images, if those images are not cached in
360 // memory it will try to read them from the Picture directory which is now gone, so... we have to use this
361 // inglorious hack below which basically will
363 // a) create a temp storage
365 // b) introspect any dialogs for any embed graphics and grab the associate URL(s)
367 // c) populate the temp storage with the associated embed images ( will be stored in a 'Pictures' folder )
369 // d) delete the 'Picture' element from the root storage
371 // e) copy the Pictures element of the temp storage to the root storage
373 // this assumes that we don't use the Pictures folder in the root of the base
374 // document for anything, I believe this is a valid assumption ( as much as
375 // I could check anyway )
377 /// @throws RuntimeException
378 void lcl_uglyHackToStoreDialogeEmbedImages( const Reference< XStorageBasedLibraryContainer >& xDlgCont, const Reference< XStorage >& xStorage, const Reference< XModel >& rxModel, const Reference<XComponentContext >& rxContext ) throw ( RuntimeException )
380 Sequence< OUString > sLibraries = xDlgCont->getElementNames();
381 Reference< XStorage > xTmpPic = xStorage->openStorageElement( "tempPictures", ElementModes::READWRITE );
383 std::vector< OUString > vEmbedImgUrls;
384 for ( sal_Int32 i=0; i < sLibraries.getLength(); ++i )
386 OUString sLibrary( sLibraries[ i ] );
387 xDlgCont->loadLibrary( sLibrary );
388 Reference< XNameContainer > xLib;
389 xDlgCont->getByName( sLibrary ) >>= xLib;
390 if ( xLib.is() )
392 Sequence< OUString > sDialogs = xLib->getElementNames();
393 sal_Int32 nDialogs( sDialogs.getLength() );
394 for ( sal_Int32 j=0; j < nDialogs; ++j )
396 Reference < awt::XDialogProvider > xDlgPrv = awt::DialogProvider::createWithModel(rxContext, rxModel);
397 OUString sDialogUrl = "vnd.sun.star.script:";
398 sDialogUrl = sDialogUrl.concat( sLibraries[ i ] ).concat( "." ).concat ( sDialogs[ j ] ).concat( "?location=document" );
400 Reference< css::awt::XControl > xDialog( xDlgPrv->createDialog( sDialogUrl ), UNO_QUERY );
401 Reference< XInterface > xModel( xDialog->getModel() );
402 GraphicObject::InspectForGraphicObjectImageURL( xModel, vEmbedImgUrls );
406 // if we have any image urls, make sure we copy the associated images into tempPictures
407 if ( !vEmbedImgUrls.empty() )
409 // Export the images to the storage
410 Reference< XGraphicObjectResolver > xGraphicResolver = GraphicObjectResolver::createWithStorage(rxContext, xTmpPic);
411 std::vector< OUString >::const_iterator it = vEmbedImgUrls.begin();
412 std::vector< OUString >::const_iterator it_end = vEmbedImgUrls.end();
413 if ( xGraphicResolver.is() )
415 for ( sal_Int32 count = 0; it != it_end; ++it, ++count )
416 xGraphicResolver->resolveGraphicObjectURL( *it );
419 // delete old 'Pictures' storage and copy the contents of tempPictures into xStorage
420 xStorage->removeElement( sPictures );
421 xTmpPic->copyElementTo( sPictures, xStorage, sPictures );
423 else
425 // clean up an existing Pictures dir
426 if ( xStorage->isStorageElement( sPictures ) )
427 xStorage->removeElement( sPictures );
431 void ODatabaseDocument::impl_setInitialized()
433 m_eInitState = Initialized;
435 // start event notifications
436 m_aEventNotifier.onDocumentInitialized();
439 void ODatabaseDocument::impl_reset_nothrow()
443 m_pImpl->clearConnections();
444 m_pImpl->disposeStorages();
445 m_pImpl->resetRootStorage();
447 clearObjectContainer( m_xForms );
448 clearObjectContainer( m_xReports );
449 clearObjectContainer( m_pImpl->m_xTableDefinitions );
450 clearObjectContainer( m_pImpl->m_xCommandDefinitions );
452 m_eInitState = NotInitialized;
454 m_pImpl->reset();
456 catch(const Exception&)
458 DBG_UNHANDLED_EXCEPTION();
460 m_pImpl->m_bDocumentReadOnly = false;
463 namespace
465 /** property map for import/export info set */
466 comphelper::PropertyMapEntry const aExportInfoMap[] =
468 { OUString("BaseURI"), 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0 },
469 { OUString("StreamName"), 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0 },
470 { OUString("UsePrettyPrinting"), 0, ::cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
471 {OUString("TargetStorage"), 0, cppu::UnoType<embed::XStorage>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
472 {OUString("StreamRelPath"), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
473 { OUString(), 0, css::uno::Type(), 0, 0 }
477 void ODatabaseDocument::impl_import_nolck_throw( const Reference< XComponentContext >& _rContext, const Reference< XInterface >& _rxTargetComponent,
478 const ::comphelper::NamedValueCollection& _rResource )
480 Sequence< Any > aFilterCreationArgs;
481 Reference< XStatusIndicator > xStatusIndicator;
482 lcl_extractAndStartStatusIndicator( _rResource, xStatusIndicator, aFilterCreationArgs );
484 uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
485 OUString sBaseURI = _rResource.getOrDefault("BaseURI", OUString());
486 if (sBaseURI.isEmpty())
487 sBaseURI = _rResource.getOrDefault("URL",OUString());
488 assert(!sBaseURI.isEmpty()); // needed for relative URLs
489 xInfoSet->setPropertyValue("BaseURI", uno::makeAny(sBaseURI));
490 xInfoSet->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
492 const sal_Int32 nCount = aFilterCreationArgs.getLength();
493 aFilterCreationArgs.realloc(nCount + 1);
494 aFilterCreationArgs[nCount] <<= xInfoSet;
496 Reference< XImporter > xImporter(
497 _rContext->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.comp.sdb.DBFilter", aFilterCreationArgs, _rContext),
498 UNO_QUERY_THROW );
500 Reference< XComponent > xComponent( _rxTargetComponent, UNO_QUERY_THROW );
501 xImporter->setTargetDocument( xComponent );
503 Reference< XFilter > xFilter( xImporter, UNO_QUERY_THROW );
504 Sequence< PropertyValue > aFilterArgs( ODatabaseModelImpl::stripLoadArguments( _rResource ).getPropertyValues() );
505 xFilter->filter( aFilterArgs );
507 if ( xStatusIndicator.is() )
508 xStatusIndicator->end();
511 void SAL_CALL ODatabaseDocument::initNew( ) throw (DoubleInitializationException, IOException, Exception, RuntimeException, std::exception)
513 // SYNCHRONIZED ->
514 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
516 impl_reset_nothrow();
518 impl_setInitializing();
520 // create a temporary storage
521 Reference< XStorage > xTempStor( ::comphelper::OStorageHelper::GetTemporaryStorage( m_pImpl->m_aContext ) );
523 // store therein
524 impl_storeToStorage_throw( xTempStor, Sequence< PropertyValue >(), aGuard );
526 // let the impl know we're now based on this storage
527 m_pImpl->switchToStorage( xTempStor );
529 // for the newly created document, allow document-wide scripting
530 m_bAllowDocumentScripting = true;
532 impl_setInitialized();
534 m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
536 impl_setModified_nothrow( false, aGuard );
537 // <- SYNCHRONIZED
539 m_aEventNotifier.notifyDocumentEvent( "OnCreate" );
541 impl_notifyStorageChange_nolck_nothrow( xTempStor );
544 void SAL_CALL ODatabaseDocument::load( const Sequence< PropertyValue >& Arguments ) throw (DoubleInitializationException, IOException, Exception, RuntimeException, std::exception)
546 // SYNCHRONIZED ->
547 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
549 impl_reset_nothrow();
551 ::comphelper::NamedValueCollection aResource( Arguments );
552 if ( aResource.has( "FileName" ) && !aResource.has( "URL" ) )
553 // FileName is the compatibility name for URL, so we might have clients passing
554 // a FileName only. However, some of our code works with the URL only, so ensure
555 // we have one.
556 aResource.put( "URL", aResource.get( "FileName" ) );
557 if ( aResource.has( "URL" ) && !aResource.has( "FileName" ) )
558 // similar ... just in case there is legacy code which expects a FileName only
559 aResource.put( "FileName", aResource.get( "URL" ) );
561 // now that somebody (perhaps) told us an macro execution mode, remember it as
562 // ImposedMacroExecMode
563 m_pImpl->setImposedMacroExecMode(
564 aResource.getOrDefault( "MacroExecutionMode", m_pImpl->getImposedMacroExecMode() ) );
566 impl_setInitializing();
569 aGuard.clear();
570 impl_import_nolck_throw( m_pImpl->m_aContext, *this, aResource );
571 aGuard.reset();
573 catch( const Exception& )
575 impl_reset_nothrow();
576 throw;
578 // tell our view monitor that the document has been loaded - this way it will fire the proper
579 // event (OnLoad instead of OnCreate) later on
580 m_aViewMonitor.onLoadedDocument();
582 // note that we do *not* call impl_setInitialized() here: The initialization is only complete
583 // when the XModel::attachResource has been called, not sooner.
584 // however, in case of embedding, XModel::attachResource is already called.
585 if (m_bEmbedded)
586 impl_setInitialized();
588 impl_setModified_nothrow( false, aGuard );
589 // <- SYNCHRONIZED
592 namespace
594 bool lcl_hasAnyModifiedSubComponent_throw( const Reference< XController >& i_rController )
596 Reference< css::sdb::application::XDatabaseDocumentUI > xDatabaseUI( i_rController, UNO_QUERY_THROW );
598 Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
599 const Reference< XComponent >* component = aComponents.getConstArray();
600 const Reference< XComponent >* componentsEnd = aComponents.getConstArray() + aComponents.getLength();
602 bool isAnyModified = false;
603 for ( ; component != componentsEnd; ++component )
605 Reference< XModifiable > xModify( *component, UNO_QUERY );
606 if ( xModify.is() )
608 isAnyModified = xModify->isModified();
609 continue;
612 // TODO: clarify: anything else to care for? Both the sub components with and without model
613 // should support the XModifiable interface, so I think nothing more is needed here.
614 OSL_FAIL( "lcl_hasAnyModifiedSubComponent_throw: anything left to do here?" );
617 return isAnyModified;
621 sal_Bool SAL_CALL ODatabaseDocument::wasModifiedSinceLastSave() throw ( RuntimeException, std::exception )
623 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
625 // The implementation here is somewhat sloppy, in that it returns whether *any* part of the whole
626 // database document, including opened sub components, is modified. This is more than what is requested:
627 // We need to return <TRUE/> if the doc itself, or any of the opened sub components, has been modified
628 // since the last call to any of the save* methods, or since the document has been loaded/created.
629 // However, the API definition explicitly allows to be that sloppy ...
631 if ( isModified() )
632 return true;
634 // auto recovery is an "UI feature", it is to restore the UI the user knows. Thus,
635 // we ask our connected controllers, not simply our existing form/report definitions.
636 // (There is some information which even cannot be obtained without asking the controller.
637 // For instance, newly created, but not yet saved, forms/reports are accessible via the
638 // controller only, but not via the model.)
642 for ( Controllers::const_iterator ctrl = m_aControllers.begin();
643 ctrl != m_aControllers.end();
644 ++ctrl
647 if ( lcl_hasAnyModifiedSubComponent_throw( *ctrl ) )
648 return true;
651 catch( const Exception& )
653 DBG_UNHANDLED_EXCEPTION();
656 return false;
659 void SAL_CALL ODatabaseDocument::storeToRecoveryFile( const OUString& i_TargetLocation, const Sequence< PropertyValue >& i_MediaDescriptor ) throw ( RuntimeException, IOException, WrappedTargetException, std::exception )
661 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
662 ModifyLock aLock( *this );
666 // create a storage for the target location
667 Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( i_TargetLocation ) );
669 // first store the document as a whole into this storage
670 impl_storeToStorage_throw( xTargetStorage, i_MediaDescriptor, aGuard );
672 // save the sub components which need saving
673 DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext);
674 aDocRecovery.saveModifiedSubComponents( xTargetStorage, m_aControllers );
676 // commit the root storage
677 tools::stor::commitStorageIfWriteable( xTargetStorage );
679 catch( const Exception& )
681 Any aError = ::cppu::getCaughtException();
682 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
683 || aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
684 || aError.isExtractableTo( ::cppu::UnoType< WrappedTargetException >::get() )
687 // allowed to leave
688 throw;
691 throw WrappedTargetException( OUString(), *this, aError );
695 void SAL_CALL ODatabaseDocument::recoverFromFile( const OUString& i_SourceLocation, const OUString& i_SalvagedFile, const Sequence< PropertyValue >& i_MediaDescriptor ) throw ( RuntimeException, IOException, WrappedTargetException, std::exception )
699 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
701 if ( i_SourceLocation.isEmpty() )
702 throw IllegalArgumentException( OUString(), *this, 1 );
705 // load the document itself, by simply delegating to our "load" method
707 // our load implementation expects the SalvagedFile and URL to be in the media descriptor
708 ::comphelper::NamedValueCollection aMediaDescriptor( i_MediaDescriptor );
709 aMediaDescriptor.put( "SalvagedFile", i_SalvagedFile );
710 aMediaDescriptor.put( "URL", i_SourceLocation );
712 aGuard.clear(); // (load has an own guarding scheme)
713 load( aMediaDescriptor.getPropertyValues() );
715 // Without a controller, we are unable to recover the sub components, as they're always tied to a controller.
716 // So, everything else is done when the first controller is connected.
717 m_bHasBeenRecovered = true;
719 // tell the impl that we've been loaded from the given location
720 m_pImpl->setDocFileLocation( i_SourceLocation );
722 // by definition (of XDocumentRecovery), we're responsible for delivering a fully-initialized document,
723 // which includes an attachResource call.
724 const OUString sLogicalDocumentURL( i_SalvagedFile.isEmpty() ? i_SourceLocation : i_SalvagedFile );
725 impl_attachResource( sLogicalDocumentURL, aMediaDescriptor.getPropertyValues(), aGuard );
726 // <- SYNCHRONIZED
728 catch( const IOException& )
730 throw;
732 catch( const RuntimeException& )
734 throw;
736 catch( const WrappedTargetException& )
738 throw;
740 catch( const Exception& )
742 Any aError = ::cppu::getCaughtException();
743 throw WrappedTargetException( OUString(), *this, aError );
747 // XModel
748 sal_Bool SAL_CALL ODatabaseDocument::attachResource( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (RuntimeException, std::exception)
750 if (_rURL.isEmpty() && _rArguments.getLength() == 1 && _rArguments[0].Name == "SetEmbedded")
752 m_bEmbedded = true;
753 return true;
756 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
757 bool bRet = false;
760 bRet = impl_attachResource( _rURL, _rArguments, aGuard );
762 catch( const RuntimeException& )
764 throw;
766 catch( const Exception& )
768 Any aError = ::cppu::getCaughtException();
769 throw WrappedTargetRuntimeException( OUString(), *this, aError );
771 return bRet;
774 bool ODatabaseDocument::impl_attachResource( const OUString& i_rLogicalDocumentURL,
775 const Sequence< PropertyValue >& i_rMediaDescriptor, DocumentGuard& _rDocGuard )
777 if ( ( i_rLogicalDocumentURL == getURL() )
778 && ( i_rMediaDescriptor.getLength() == 1 )
779 && ( i_rMediaDescriptor[0].Name == "BreakMacroSignature" )
782 // this is a BAD hack of the Basic importer code ... there should be a dedicated API for this,
783 // not this bad mis-using of existing interfaces
784 return false;
785 // (we do not support macro signatures, so we can ignore this call)
788 // if no URL has been provided, the caller was lazy enough to not call our getURL - which is not allowed anymore,
789 // now since getURL and getLocation both return the same, so calling one of those should be simple.
790 OUString sDocumentURL( i_rLogicalDocumentURL );
791 OSL_ENSURE( !sDocumentURL.isEmpty(), "ODatabaseDocument::impl_attachResource: invalid URL!" );
792 if ( sDocumentURL.isEmpty() )
793 sDocumentURL = getURL();
795 m_pImpl->setResource( sDocumentURL, i_rMediaDescriptor );
797 if ( impl_isInitializing() )
798 { // this means we've just been loaded, and this is the attachResource call which follows
799 // the load call.
800 impl_setInitialized();
802 // determine whether the document as a whole, or sub documents, have macros. Especially the latter
803 // controls the availability of our XEmbeddedScripts and XScriptInvocationContext interfaces, and we
804 // should know this before anybody actually uses the object.
805 m_bAllowDocumentScripting = ( m_pImpl->determineEmbeddedMacros() != ODatabaseModelImpl::eSubDocumentMacros );
807 _rDocGuard.clear();
808 // <- SYNCHRONIZED
809 m_aEventNotifier.notifyDocumentEvent( "OnLoadFinished" );
812 return true;
815 OUString SAL_CALL ODatabaseDocument::getURL( ) throw (RuntimeException, std::exception)
817 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
818 return m_pImpl->getURL();
821 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getArgs( ) throw (RuntimeException, std::exception)
823 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
824 return m_pImpl->getMediaDescriptor().getPropertyValues();
827 void SAL_CALL ODatabaseDocument::connectController( const Reference< XController >& _xController ) throw (RuntimeException, std::exception)
829 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
831 #if OSL_DEBUG_LEVEL > 0
832 for ( Controllers::const_iterator controller = m_aControllers.begin();
833 controller != m_aControllers.end();
834 ++controller
837 OSL_ENSURE( *controller != _xController, "ODatabaseDocument::connectController: this controller is already connected!" );
839 #endif
841 m_aControllers.push_back( _xController );
843 m_aEventNotifier.notifyDocumentEventAsync( "OnViewCreated", Reference< XController2 >( _xController, UNO_QUERY ) );
845 bool bFirstControllerEver = m_aViewMonitor.onControllerConnected( _xController );
846 if ( !bFirstControllerEver )
847 return;
849 // check/adjust our macro mode.
850 m_pImpl->checkMacrosOnLoading();
853 void SAL_CALL ODatabaseDocument::disconnectController( const Reference< XController >& _xController ) throw (RuntimeException, std::exception)
855 bool bNotifyViewClosed = false;
856 bool bLastControllerGone = false;
857 bool bIsClosing = false;
859 // SYNCHRONIZED ->
861 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
863 Controllers::iterator pos = ::std::find( m_aControllers.begin(), m_aControllers.end(), _xController );
864 OSL_ENSURE( pos != m_aControllers.end(), "ODatabaseDocument::disconnectController: don't know this controller!" );
865 if ( pos != m_aControllers.end() )
867 m_aControllers.erase( pos );
868 bNotifyViewClosed = true;
871 if ( m_xCurrentController == _xController )
872 m_xCurrentController = nullptr;
874 bLastControllerGone = m_aControllers.empty();
875 bIsClosing = m_bClosing;
877 // <- SYNCHRONIZED
879 if ( bNotifyViewClosed )
880 m_aEventNotifier.notifyDocumentEvent( "OnViewClosed", Reference< XController2 >( _xController, UNO_QUERY ) );
882 if ( bLastControllerGone && !bIsClosing )
884 // if this was the last view, close the document as a whole
885 // #i51157#
888 close( true );
890 catch( const CloseVetoException& )
892 // okay, somebody vetoed and took ownership
897 void SAL_CALL ODatabaseDocument::lockControllers( ) throw (RuntimeException, std::exception)
899 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
901 ++m_pImpl->m_nControllerLockCount;
904 void SAL_CALL ODatabaseDocument::unlockControllers( ) throw (RuntimeException, std::exception)
906 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
908 --m_pImpl->m_nControllerLockCount;
911 sal_Bool SAL_CALL ODatabaseDocument::hasControllersLocked( ) throw (RuntimeException, std::exception)
913 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
915 return m_pImpl->m_nControllerLockCount != 0;
918 Reference< XController > SAL_CALL ODatabaseDocument::getCurrentController() throw (RuntimeException, std::exception)
920 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
922 return m_xCurrentController.is() ? m_xCurrentController : ( m_aControllers.empty() ? Reference< XController >() : *m_aControllers.begin() );
925 void SAL_CALL ODatabaseDocument::setCurrentController( const Reference< XController >& _xController ) throw (NoSuchElementException, RuntimeException, std::exception)
927 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
929 m_xCurrentController = _xController;
931 if ( !m_aViewMonitor.onSetCurrentController( _xController ) )
932 return;
934 // check if there are sub components to recover from our document storage
935 bool bAttemptRecovery = m_bHasBeenRecovered;
936 if ( !bAttemptRecovery && m_pImpl->getMediaDescriptor().has( "ForceRecovery" ) )
937 // do not use getOrDefault, it will throw for invalid types, which is not desired here
938 m_pImpl->getMediaDescriptor().get( "ForceRecovery" ) >>= bAttemptRecovery;
940 if ( !bAttemptRecovery )
941 return;
945 DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext );
946 aDocRecovery.recoverSubDocuments( m_pImpl->getRootStorage(), _xController );
948 catch( const Exception& )
950 DBG_UNHANDLED_EXCEPTION();
954 Reference< XInterface > SAL_CALL ODatabaseDocument::getCurrentSelection( ) throw (RuntimeException, std::exception)
956 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
958 Reference< XInterface > xRet;
959 Reference< XSelectionSupplier > xDocView( getCurrentController(), UNO_QUERY );
960 if ( xDocView.is() )
961 xRet.set(xDocView->getSelection(),UNO_QUERY);
963 return xRet;
966 // XStorable
967 sal_Bool SAL_CALL ODatabaseDocument::hasLocation( ) throw (RuntimeException, std::exception)
969 return !getLocation().isEmpty();
972 OUString SAL_CALL ODatabaseDocument::getLocation( ) throw (RuntimeException, std::exception)
974 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
975 return m_pImpl->getURL();
976 // both XStorable::getLocation and XModel::getURL have to return the URL of the document, *not*
977 // the location of the file which the document was possibly recovered from (which would be getDocFileLocation)
980 sal_Bool SAL_CALL ODatabaseDocument::isReadonly( ) throw (RuntimeException, std::exception)
982 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
983 return m_pImpl->m_bDocumentReadOnly;
986 void SAL_CALL ODatabaseDocument::store( ) throw (IOException, RuntimeException, std::exception)
988 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
990 OUString sDocumentURL( m_pImpl->getURL() );
991 if ( !sDocumentURL.isEmpty() )
993 if ( m_pImpl->getDocFileLocation() == m_pImpl->getURL() )
994 if ( m_pImpl->m_bDocumentReadOnly )
995 throw IOException();
997 impl_storeAs_throw( m_pImpl->getURL(), m_pImpl->getMediaDescriptor(), SAVE, aGuard );
998 return;
1001 // if we have no URL, but did survive the DocumentGuard above, then we've been inited via XLoadable::initNew,
1002 // i.e. we're based on a temporary storage
1003 OSL_ENSURE( m_pImpl->getDocFileLocation().isEmpty(), "ODatabaseDocument::store: unexpected URL inconsistency!" );
1007 impl_storeToStorage_throw( m_pImpl->getRootStorage(), m_pImpl->getMediaDescriptor().getPropertyValues(), aGuard );
1009 catch( const Exception& )
1011 Any aError = ::cppu::getCaughtException();
1012 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
1013 || aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
1016 // allowed to leave
1017 throw;
1019 impl_throwIOExceptionCausedBySave_throw( aError, OUString() );
1023 void ODatabaseDocument::impl_throwIOExceptionCausedBySave_throw( const Any& i_rError, const OUString& i_rTargetURL ) const
1025 OUString sErrorMessage = extractExceptionMessage( m_pImpl->m_aContext, i_rError );
1026 sErrorMessage = ResourceManager::loadString(
1027 RID_STR_ERROR_WHILE_SAVING,
1028 "$location$", i_rTargetURL,
1029 "$message$", sErrorMessage
1031 throw IOException( sErrorMessage, *const_cast< ODatabaseDocument* >( this ) );
1034 void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comphelper::NamedValueCollection& _rArguments,
1035 const StoreType _eType, DocumentGuard& _rGuard ) throw (IOException, RuntimeException, std::exception)
1037 OSL_PRECOND( ( _eType == SAVE ) || ( _eType == SAVE_AS ),
1038 "ODatabaseDocument::impl_storeAs_throw: you introduced a new type which cannot be handled here!" );
1040 // if we're in the process of initializing the document (which effectively means it is an implicit
1041 // initialization triggered in storeAsURL), the we do not notify events, since to an observer, the SaveAs
1042 // should not be noticeable
1043 bool bIsInitializationProcess = impl_isInitializing();
1045 if ( !bIsInitializationProcess )
1047 _rGuard.clear();
1048 m_aEventNotifier.notifyDocumentEvent( _eType == SAVE ? "OnSave" : "OnSaveAs", nullptr, makeAny( _rURL ) );
1049 _rGuard.reset();
1052 Reference< XStorage > xNewRootStorage;
1053 // will be non-NULL if our storage changed
1057 ModifyLock aLock( *this );
1058 // ignore all changes of our "modified" state during storing
1060 bool bLocationChanged = ( _rURL != m_pImpl->getDocFileLocation() );
1061 if ( bLocationChanged )
1063 // create storage for target URL
1064 uno::Reference<embed::XStorage> xTargetStorage;
1065 _rArguments.get("TargetStorage") >>= xTargetStorage;
1066 if (!xTargetStorage.is())
1067 xTargetStorage = impl_createStorageFor_throw(_rURL);
1069 // In case we got a StreamRelPath, then xTargetStorage should reference that sub-storage.
1070 OUString sStreamRelPath = _rArguments.getOrDefault("StreamRelPath", OUString());
1071 if (!sStreamRelPath.isEmpty())
1072 xTargetStorage = xTargetStorage->openStorageElement(sStreamRelPath, embed::ElementModes::READWRITE);
1074 if ( m_pImpl->isEmbeddedDatabase() )
1075 m_pImpl->clearConnections();
1077 // commit everything
1078 m_pImpl->commitEmbeddedStorage();
1079 m_pImpl->commitStorages();
1081 // copy own storage to target storage
1082 Reference< XStorage > xCurrentStorage( m_pImpl->getRootStorage() );
1083 if ( xCurrentStorage.is() )
1084 xCurrentStorage->copyToStorage( xTargetStorage );
1086 m_pImpl->disposeStorages();
1088 // each and every document definition obtained via m_xForms and m_xReports depends
1089 // on the sub storages which we just disposed. So, dispose the forms/reports collections, too.
1090 // This ensures that they're re-created when needed.
1091 clearObjectContainer( m_xForms );
1092 clearObjectContainer( m_xReports );
1094 xNewRootStorage = m_pImpl->switchToStorage( xTargetStorage );
1096 m_pImpl->m_bDocumentReadOnly = false;
1099 // store to current storage
1100 Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_QUERY_THROW );
1101 Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) );
1102 impl_storeToStorage_throw( xCurrentStorage, aMediaDescriptor, _rGuard );
1104 // success - tell our impl
1105 m_pImpl->setDocFileLocation( _rURL );
1106 m_pImpl->setResource( _rURL, aMediaDescriptor );
1108 // if we are in an initialization process, then this is finished, now that we stored the document
1109 if ( bIsInitializationProcess )
1110 impl_setInitialized();
1112 catch( const IOException& )
1114 if ( !bIsInitializationProcess )
1115 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveFailed" : "OnSaveAsFailed", nullptr, makeAny( _rURL ) );
1116 throw;
1118 catch( const RuntimeException& )
1120 if ( !bIsInitializationProcess )
1121 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveFailed" : "OnSaveAsFailed", nullptr, makeAny( _rURL ) );
1122 throw;
1124 catch( const Exception& )
1126 Any aError = ::cppu::getCaughtException();
1128 // notify the failure
1129 if ( !bIsInitializationProcess )
1130 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveFailed" : "OnSaveAsFailed", nullptr, makeAny( _rURL ) );
1132 impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1135 // notify the document event
1136 if ( !bIsInitializationProcess )
1137 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? "OnSaveDone" : "OnSaveAsDone", nullptr, makeAny( _rURL ) );
1139 // reset our "modified" flag, and clear the guard
1140 impl_setModified_nothrow( false, _rGuard );
1141 // <- SYNCHRONIZED
1143 // notify storage listeners
1144 if ( xNewRootStorage.is() )
1145 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1148 Reference< XStorage > ODatabaseDocument::impl_createStorageFor_throw( const OUString& _rURL ) const
1150 Reference< ucb::XSimpleFileAccess3 > xTempAccess(ucb::SimpleFileAccess::create(m_pImpl->m_aContext));
1151 Reference< io::XStream > xStream = xTempAccess->openFileReadWrite( _rURL );
1152 Reference< io::XTruncate > xTruncate(xStream,UNO_QUERY);
1153 if ( xTruncate.is() )
1155 xTruncate->truncate();
1157 Sequence<Any> aParam(2);
1158 aParam[0] <<= xStream;
1159 aParam[1] <<= ElementModes::READWRITE | ElementModes::TRUNCATE;
1161 Reference< XSingleServiceFactory > xStorageFactory( m_pImpl->createStorageFactory(), UNO_SET_THROW );
1162 return Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aParam ), UNO_QUERY_THROW );
1165 void SAL_CALL ODatabaseDocument::storeAsURL( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (IOException, RuntimeException, std::exception)
1167 // SYNCHRONIZED ->
1168 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1170 // Normally, a document initialization is done via XLoadable::load or XLoadable::initNew. For convenience
1171 // reasons, and to not break existing API clients, it's allowed to call storeAsURL without having initialized
1172 // the document, in which case the initialization will be done implicitly.
1173 bool bImplicitInitialization = !impl_isInitialized();
1174 // implicit initialization while another initialization is just running is not possible
1175 if ( bImplicitInitialization && impl_isInitializing() )
1176 throw RuntimeException();
1178 if ( bImplicitInitialization )
1179 impl_setInitializing();
1183 impl_storeAs_throw( _rURL, _rArguments, SAVE_AS, aGuard );
1184 // <- SYNCHRONIZED
1186 // impl_storeAs_throw cleared the lock on our mutex, but the below lines need this lock
1187 // SYNCHRONIZED ->
1188 aGuard.reset();
1190 // our title might have changed, potentially at least
1191 // Sadly, we cannot check this: Calling getTitle here and now would not deliver
1192 // an up-to-date result, as the call is delegated to our TitleHelper instance, which itself
1193 // updates its title only if it gets the OnSaveAsDone event (which was sent asynchronously
1194 // by impl_storeAs_throw). So, we simply notify always, and also asynchronously
1195 m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
1197 catch( const Exception& )
1199 impl_reset_nothrow();
1200 throw;
1203 if ( bImplicitInitialization )
1204 m_bAllowDocumentScripting = true;
1206 aGuard.clear();
1207 // <- SYNCHRONIZED
1209 if ( bImplicitInitialization )
1210 m_aEventNotifier.notifyDocumentEvent( "OnCreate" );
1213 void ODatabaseDocument::impl_storeToStorage_throw( const Reference< XStorage >& _rxTargetStorage, const Sequence< PropertyValue >& _rMediaDescriptor,
1214 DocumentGuard& _rDocGuard ) const
1216 if ( !_rxTargetStorage.is() )
1217 throw IllegalArgumentException( OUString(), *const_cast< ODatabaseDocument* >( this ), 1 );
1219 if ( !m_pImpl.is() )
1220 throw DisposedException( OUString(), *const_cast< ODatabaseDocument* >( this ) );
1224 // commit everything
1225 m_pImpl->commitEmbeddedStorage();
1226 m_pImpl->commitStorages();
1228 // copy own storage to target storage
1229 if ( impl_isInitialized() )
1231 Reference< XStorage > xCurrentStorage = m_pImpl->getOrCreateRootStorage();
1232 // Root storage may be empty in case of embedding.
1233 if ( xCurrentStorage.is() && xCurrentStorage != _rxTargetStorage )
1234 xCurrentStorage->copyToStorage( _rxTargetStorage );
1237 // write into target storage
1238 ::comphelper::NamedValueCollection aWriteArgs( _rMediaDescriptor );
1239 lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, true );
1240 impl_writeStorage_throw( _rxTargetStorage, aWriteArgs );
1241 lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, false );
1243 // commit target storage
1244 OSL_VERIFY( tools::stor::commitStorageIfWriteable( _rxTargetStorage ) );
1246 catch( const IOException& ) { throw; }
1247 catch( const RuntimeException& ) { throw; }
1248 catch ( const Exception& e )
1250 throw IOException( e.Message, *const_cast< ODatabaseDocument* >( this ) );
1254 void SAL_CALL ODatabaseDocument::storeToURL( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments ) throw (IOException, RuntimeException, std::exception)
1256 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1257 ModifyLock aLock( *this );
1260 aGuard.clear();
1261 m_aEventNotifier.notifyDocumentEvent( "OnSaveTo", nullptr, makeAny( _rURL ) );
1262 aGuard.reset();
1267 // create storage for target URL
1268 Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( _rURL ) );
1270 // extend media descriptor with URL
1271 Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) );
1273 // store to this storage
1274 impl_storeToStorage_throw( xTargetStorage, aMediaDescriptor, aGuard );
1276 catch( const Exception& )
1278 Any aError = ::cppu::getCaughtException();
1279 m_aEventNotifier.notifyDocumentEventAsync( "OnSaveToFailed", nullptr, aError );
1281 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
1282 || aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
1285 // allowed to leave
1286 throw;
1289 impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1292 m_aEventNotifier.notifyDocumentEventAsync( "OnSaveToDone", nullptr, makeAny( _rURL ) );
1295 // XModifyBroadcaster
1296 void SAL_CALL ODatabaseDocument::addModifyListener( const Reference< XModifyListener >& _xListener ) throw (RuntimeException, std::exception)
1298 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1299 m_aModifyListeners.addInterface(_xListener);
1302 void SAL_CALL ODatabaseDocument::removeModifyListener( const Reference< XModifyListener >& _xListener ) throw (RuntimeException, std::exception)
1304 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1305 m_aModifyListeners.removeInterface(_xListener);
1308 // XModifiable
1309 sal_Bool SAL_CALL ODatabaseDocument::isModified( ) throw (RuntimeException, std::exception)
1311 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1313 return m_pImpl->m_bModified;
1316 void SAL_CALL ODatabaseDocument::setModified( sal_Bool _bModified ) throw (PropertyVetoException, RuntimeException, std::exception)
1318 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1319 if ( impl_isInitialized() )
1320 impl_setModified_nothrow( _bModified, aGuard );
1321 // it's allowed to call setModified without the document being initialized already. In this case,
1322 // we simply ignore the call - when the initialization is finished, the respective code will set
1323 // a proper "modified" flag
1326 void ODatabaseDocument::impl_setModified_nothrow( bool _bModified, DocumentGuard& _rGuard )
1328 // SYNCHRONIZED ->
1329 bool bModifiedChanged = ( m_pImpl->m_bModified != _bModified ) && ( !m_pImpl->isModifyLocked() );
1331 if ( bModifiedChanged )
1333 m_pImpl->m_bModified = _bModified;
1334 m_aEventNotifier.notifyDocumentEventAsync( "OnModifyChanged" );
1336 _rGuard.clear();
1337 // <- SYNCHRONIZED
1339 if ( bModifiedChanged )
1341 lang::EventObject aEvent( *this );
1342 m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
1346 // css::document::XEventBroadcaster
1347 void SAL_CALL ODatabaseDocument::addEventListener(const uno::Reference< document::XEventListener >& Listener ) throw (uno::RuntimeException, std::exception)
1349 m_aEventNotifier.addLegacyEventListener( Listener );
1352 void SAL_CALL ODatabaseDocument::removeEventListener( const uno::Reference< document::XEventListener >& Listener ) throw (uno::RuntimeException, std::exception)
1354 m_aEventNotifier.removeLegacyEventListener( Listener );
1357 void SAL_CALL ODatabaseDocument::addDocumentEventListener( const Reference< XDocumentEventListener >& Listener ) throw (RuntimeException, std::exception)
1359 m_aEventNotifier.addDocumentEventListener( Listener );
1362 void SAL_CALL ODatabaseDocument::removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener ) throw (RuntimeException, std::exception)
1364 m_aEventNotifier.removeDocumentEventListener( Listener );
1367 void SAL_CALL ODatabaseDocument::notifyDocumentEvent( const OUString& EventName, const Reference< XController2 >& ViewController, const Any& Supplement ) throw (IllegalArgumentException, NoSupportException, RuntimeException, std::exception)
1369 if ( EventName.isEmpty() )
1370 throw IllegalArgumentException( OUString(), *this, 1 );
1372 // SYNCHRONIZED ->
1373 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1375 if ( !DocumentEvents::needsSynchronousNotification( EventName ) )
1377 m_aEventNotifier.notifyDocumentEventAsync( EventName, ViewController, Supplement );
1378 return;
1380 aGuard.clear();
1381 // <- SYNCHRONIZED
1383 m_aEventNotifier.notifyDocumentEvent( EventName, ViewController, Supplement );
1386 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getPrinter( ) throw (RuntimeException, std::exception)
1388 OSL_FAIL( "ODatabaseDocument::getPrinter: not supported!" );
1389 return Sequence< PropertyValue >();
1392 void SAL_CALL ODatabaseDocument::setPrinter( const Sequence< PropertyValue >& /*aPrinter*/ ) throw (IllegalArgumentException, RuntimeException, std::exception)
1394 OSL_FAIL( "ODatabaseDocument::setPrinter: not supported!" );
1397 void SAL_CALL ODatabaseDocument::print( const Sequence< PropertyValue >& /*xOptions*/ ) throw (IllegalArgumentException, RuntimeException, std::exception)
1399 OSL_FAIL( "ODatabaseDocument::print: not supported!" );
1402 void ODatabaseDocument::impl_reparent_nothrow( const WeakReference< XNameAccess >& _rxContainer )
1404 Reference< XChild > xChild( _rxContainer.get(), UNO_QUERY );
1405 if ( xChild.is() )
1406 xChild->setParent( *this );
1409 void ODatabaseDocument::clearObjectContainer( WeakReference< XNameAccess >& _rxContainer)
1411 Reference< XNameAccess > xContainer = _rxContainer;
1412 ::comphelper::disposeComponent( xContainer );
1414 Reference< XChild > xChild( _rxContainer.get(),UNO_QUERY );
1415 if ( xChild.is() )
1416 xChild->setParent( nullptr );
1417 _rxContainer.clear();
1420 Reference< XNameAccess > ODatabaseDocument::impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType _eType )
1422 if ( ( _eType != ODatabaseModelImpl::E_FORM ) && ( _eType != ODatabaseModelImpl::E_REPORT ) )
1423 throw IllegalArgumentException();
1425 bool bFormsContainer = _eType == ODatabaseModelImpl::E_FORM;
1427 WeakReference< XNameAccess >& rContainerRef( bFormsContainer ? m_xForms : m_xReports );
1428 Reference< XNameAccess > xContainer = rContainerRef;
1429 if ( !xContainer.is() )
1431 Any aValue;
1432 css::uno::Reference< css::uno::XInterface > xMy(*this);
1433 if ( dbtools::getDataSourceSetting(xMy,bFormsContainer ? "Forms" : "Reports",aValue) )
1435 OUString sSupportService;
1436 aValue >>= sSupportService;
1437 if ( !sSupportService.isEmpty() )
1439 Sequence<Any> aArgs(1);
1440 aArgs[0] <<= NamedValue("DatabaseDocument",makeAny(xMy));
1441 xContainer.set(
1442 m_pImpl->m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(sSupportService, aArgs, m_pImpl->m_aContext),
1443 UNO_QUERY);
1444 rContainerRef = xContainer;
1447 if ( !xContainer.is() )
1449 TContentPtr& rContainerData( m_pImpl->getObjectContainer( _eType ) );
1450 rContainerRef = xContainer = new ODocumentContainer( m_pImpl->m_aContext, *this, rContainerData, bFormsContainer );
1452 impl_reparent_nothrow( xContainer );
1454 return xContainer;
1457 void ODatabaseDocument::impl_closeControllerFrames_nolck_throw( bool _bDeliverOwnership )
1459 Controllers aCopy = m_aControllers;
1461 Controllers::const_iterator aEnd = aCopy.end();
1462 for ( Controllers::const_iterator aIter = aCopy.begin(); aIter != aEnd ; ++aIter )
1464 if ( !aIter->is() )
1465 continue;
1469 Reference< XCloseable> xFrame( (*aIter)->getFrame(), UNO_QUERY );
1470 if ( xFrame.is() )
1471 xFrame->close( _bDeliverOwnership );
1473 catch( const CloseVetoException& ) { throw; }
1474 catch( const Exception& )
1476 DBG_UNHANDLED_EXCEPTION();
1481 void ODatabaseDocument::impl_disposeControllerFrames_nothrow()
1483 Controllers aCopy;
1484 aCopy.swap( m_aControllers ); // ensure m_aControllers is empty afterwards
1485 for( const auto& rController : aCopy )
1489 if( rController.is() )
1491 Reference< XFrame > xFrame( rController->getFrame() );
1492 ::comphelper::disposeComponent( xFrame );
1495 catch( const Exception& )
1497 DBG_UNHANDLED_EXCEPTION();
1502 void SAL_CALL ODatabaseDocument::close(sal_Bool bDeliverOwnership)
1503 throw (CloseVetoException, RuntimeException, std::exception)
1505 // nearly everything below can/must be done without our mutex locked, the below is just for
1506 // the checks for being disposed and the like
1507 // SYNCHRONIZED ->
1509 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1510 assert (!m_bClosing);
1511 m_bClosing = true;
1513 // <- SYNCHRONIZED
1517 // allow listeners to veto
1518 lang::EventObject aEvent( *this );
1519 m_aCloseListener.forEach< XCloseListener >(
1520 [&aEvent, &bDeliverOwnership] (uno::Reference<XCloseListener> const& xListener) {
1521 return xListener->queryClosing(aEvent, bDeliverOwnership);
1524 // notify that we're going to unload
1525 m_aEventNotifier.notifyDocumentEvent( "OnPrepareUnload" );
1527 impl_closeControllerFrames_nolck_throw( bDeliverOwnership );
1529 m_aCloseListener.notifyEach( &XCloseListener::notifyClosing, (const lang::EventObject&)aEvent );
1531 dispose();
1533 catch ( const Exception& )
1535 ::osl::MutexGuard aGuard( m_aMutex );
1536 m_bClosing = false;
1537 throw;
1540 // SYNCHRONIZED ->
1541 ::osl::MutexGuard aGuard( m_aMutex );
1542 m_bClosing = false;
1543 // <- SYNCHRONIZED
1546 void SAL_CALL ODatabaseDocument::addCloseListener( const Reference< css::util::XCloseListener >& Listener ) throw (RuntimeException, std::exception)
1548 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1549 m_aCloseListener.addInterface(Listener);
1552 void SAL_CALL ODatabaseDocument::removeCloseListener( const Reference< css::util::XCloseListener >& Listener ) throw (RuntimeException, std::exception)
1554 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1555 m_aCloseListener.removeInterface(Listener);
1558 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getFormDocuments( ) throw (RuntimeException, std::exception)
1560 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1561 return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_FORM );
1564 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getReportDocuments( ) throw (RuntimeException, std::exception)
1566 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1567 return impl_getDocumentContainer_throw( ODatabaseModelImpl::E_REPORT );
1570 void ODatabaseDocument::WriteThroughComponent( const Reference< XComponent >& xComponent, const sal_Char* pStreamName,
1571 const sal_Char* pServiceName, const Sequence< Any >& _rArguments, const Sequence< PropertyValue >& rMediaDesc,
1572 const Reference<XStorage>& _xStorageToSaveTo ) const
1574 OSL_ENSURE( pStreamName, "Need stream name!" );
1575 OSL_ENSURE( pServiceName, "Need service name!" );
1577 // open stream
1578 OUString sStreamName = OUString::createFromAscii( pStreamName );
1579 Reference< XStream > xStream = _xStorageToSaveTo->openStreamElement( sStreamName, ElementModes::READWRITE | ElementModes::TRUNCATE );
1580 if ( !xStream.is() )
1581 return;
1583 Reference< XOutputStream > xOutputStream( xStream->getOutputStream() );
1584 OSL_ENSURE( xOutputStream.is(), "Can't create output stream in package!" );
1585 if ( !xOutputStream.is() )
1586 return;
1588 Reference< XSeekable > xSeek( xOutputStream, UNO_QUERY );
1589 if ( xSeek.is() )
1590 xSeek->seek(0);
1592 Reference< XPropertySet > xStreamProp( xOutputStream, UNO_QUERY_THROW );
1593 xStreamProp->setPropertyValue( INFO_MEDIATYPE, makeAny( OUString( "text/xml" ) ) );
1594 xStreamProp->setPropertyValue( "Compressed", makeAny( true ) );
1596 // write the stuff
1597 WriteThroughComponent( xOutputStream, xComponent, pServiceName, _rArguments, rMediaDesc );
1600 void ODatabaseDocument::WriteThroughComponent( const Reference< XOutputStream >& xOutputStream,
1601 const Reference< XComponent >& xComponent, const sal_Char* pServiceName, const Sequence< Any >& _rArguments,
1602 const Sequence< PropertyValue >& rMediaDesc ) const
1604 OSL_ENSURE( xOutputStream.is(), "I really need an output stream!" );
1605 OSL_ENSURE( xComponent.is(), "Need component!" );
1606 OSL_ENSURE( nullptr != pServiceName, "Need component name!" );
1608 // get component
1609 Reference< XWriter > xSaxWriter = xml::sax::Writer::create( m_pImpl->m_aContext );
1611 // connect XML writer to output stream
1612 xSaxWriter->setOutputStream( xOutputStream );
1614 // prepare arguments (prepend doc handler to given arguments)
1615 Reference< XDocumentHandler > xDocHandler( xSaxWriter,UNO_QUERY);
1616 Sequence<Any> aArgs( 1 + _rArguments.getLength() );
1617 aArgs[0] <<= xDocHandler;
1618 for ( sal_Int32 i = 0; i < _rArguments.getLength(); ++i )
1619 aArgs[ i+1 ] = _rArguments[i];
1621 // get filter component
1622 Reference< XExporter > xExporter( m_pImpl->m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(OUString::createFromAscii(pServiceName), aArgs, m_pImpl->m_aContext), UNO_QUERY_THROW );
1624 // connect model and filter
1625 xExporter->setSourceDocument( xComponent );
1627 // filter
1628 Reference< XFilter > xFilter( xExporter, UNO_QUERY_THROW );
1629 xFilter->filter( rMediaDesc );
1632 void ODatabaseDocument::impl_writeStorage_throw( const Reference< XStorage >& _rxTargetStorage, const ::comphelper::NamedValueCollection& _rMediaDescriptor ) const
1634 // extract status indicator
1635 Sequence< Any > aDelegatorArguments;
1636 lcl_extractStatusIndicator( _rMediaDescriptor, aDelegatorArguments );
1638 uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
1640 SvtSaveOptions aSaveOpt;
1641 xInfoSet->setPropertyValue("UsePrettyPrinting", uno::makeAny(aSaveOpt.IsPrettyPrinting()));
1642 if ( aSaveOpt.IsSaveRelFSys() )
1644 OUString sBaseURI = _rMediaDescriptor.getOrDefault("BaseURI", OUString());
1645 if (sBaseURI.isEmpty())
1646 sBaseURI = _rMediaDescriptor.getOrDefault("URL",OUString());
1647 xInfoSet->setPropertyValue("BaseURI", uno::makeAny(sBaseURI));
1650 // Set TargetStorage, so it doesn't have to be re-constructed based on possibly empty URL.
1651 xInfoSet->setPropertyValue("TargetStorage", uno::makeAny(m_pImpl->getRootStorage()));
1653 // Set StreamRelPath, in case this document is an embedded one.
1654 OUString sStreamRelPath;
1655 OUString sURL = _rMediaDescriptor.getOrDefault("URL", OUString());
1656 if (sURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:"))
1658 // In this case the host contains the real path, and the path is the embedded stream name.
1659 INetURLObject aURL(sURL);
1660 sStreamRelPath = aURL.GetURLPath(INetURLObject::DecodeMechanism::WithCharset);
1661 if (sStreamRelPath.startsWith("/"))
1662 sStreamRelPath = sStreamRelPath.copy(1);
1664 if (!sStreamRelPath.isEmpty())
1665 xInfoSet->setPropertyValue("StreamRelPath", uno::makeAny(sStreamRelPath));
1667 sal_Int32 nArgsLen = aDelegatorArguments.getLength();
1668 aDelegatorArguments.realloc(nArgsLen+1);
1669 aDelegatorArguments[nArgsLen++] <<= xInfoSet;
1671 Reference< XPropertySet > xProp( _rxTargetStorage, UNO_QUERY_THROW );
1672 xProp->setPropertyValue( INFO_MEDIATYPE, makeAny( OUString(MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII) ) );
1674 OUString aVersion;
1675 SvtSaveOptions::ODFDefaultVersion const nDefVersion =
1676 aSaveOpt.GetODFDefaultVersion();
1677 // older versions can not have this property set,
1678 // it exists only starting from ODF1.2
1679 if (nDefVersion >= SvtSaveOptions::ODFVER_012)
1680 aVersion = ODFVER_012_TEXT;
1682 if (!aVersion.isEmpty())
1686 xProp->setPropertyValue("Version" , uno::makeAny(aVersion));
1688 catch (const uno::Exception& e)
1690 SAL_WARN("dbaccess", "exception setting Version: " << e.Message);
1694 Reference< XComponent > xComponent( *const_cast< ODatabaseDocument* >( this ), UNO_QUERY_THROW );
1696 Sequence< PropertyValue > aMediaDescriptor;
1697 _rMediaDescriptor >>= aMediaDescriptor;
1699 xInfoSet->setPropertyValue("StreamName", uno::makeAny(OUString("settings.xml")));
1700 WriteThroughComponent( xComponent, "settings.xml", "com.sun.star.comp.sdb.XMLSettingsExporter",
1701 aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1703 xInfoSet->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
1704 WriteThroughComponent( xComponent, "content.xml", "com.sun.star.comp.sdb.DBExportFilter",
1705 aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1707 if ( _rxTargetStorage->hasByName ( sPictures ) )
1711 // Delete any previously existing Pictures folder and regenerate
1712 // any needed content if needed
1713 Reference< XStorageBasedLibraryContainer > xDlgs = m_pImpl->getLibraryContainer( false );
1714 if ( xDlgs.is() )
1716 Reference< XModel > xModel(const_cast< ODatabaseDocument*>(this));
1717 lcl_uglyHackToStoreDialogeEmbedImages( m_pImpl->getLibraryContainer(false), _rxTargetStorage, xModel, m_pImpl->m_aContext );
1720 catch ( const Exception& )
1722 DBG_UNHANDLED_EXCEPTION();
1725 m_pImpl->storeLibraryContainersTo( _rxTargetStorage );
1728 Reference< XUIConfigurationManager > SAL_CALL ODatabaseDocument::getUIConfigurationManager( ) throw (RuntimeException, std::exception)
1730 return Reference< XUIConfigurationManager >( getUIConfigurationManager2(), UNO_QUERY_THROW );
1733 Reference< XUIConfigurationManager2 > const & ODatabaseDocument::getUIConfigurationManager2( ) throw (RuntimeException)
1735 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1737 if ( !m_xUIConfigurationManager.is() )
1739 m_xUIConfigurationManager = UIConfigurationManager::create( m_pImpl->m_aContext );
1741 OUString aUIConfigFolderName( "Configurations2" );
1742 Reference< XStorage > xConfigStorage;
1744 // First try to open with READWRITE and then READ
1745 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READWRITE );
1746 if ( xConfigStorage.is() )
1748 OUString aUIConfigMediaType( "application/vnd.sun.xml.ui.configuration" );
1749 OUString aMediaType;
1750 Reference< XPropertySet > xPropSet( xConfigStorage, UNO_QUERY );
1751 Any a = xPropSet->getPropertyValue( INFO_MEDIATYPE );
1752 if ( !( a >>= aMediaType ) || aMediaType.isEmpty() )
1754 a <<= aUIConfigMediaType;
1755 xPropSet->setPropertyValue( INFO_MEDIATYPE, a );
1758 else
1759 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READ );
1761 // initialize ui configuration manager with document substorage
1762 m_xUIConfigurationManager->setStorage( xConfigStorage );
1765 return m_xUIConfigurationManager;
1768 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentSubStorage( const OUString& aStorageName, sal_Int32 nMode ) throw (RuntimeException, std::exception)
1770 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1772 Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1773 return xStorageAccess->getDocumentSubStorage( aStorageName, nMode );
1776 Sequence< OUString > SAL_CALL ODatabaseDocument::getDocumentSubStoragesNames( ) throw (css::io::IOException, RuntimeException, std::exception)
1778 Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1779 return xStorageAccess->getDocumentSubStoragesNames();
1782 void ODatabaseDocument::impl_notifyStorageChange_nolck_nothrow( const Reference< XStorage >& xNewRootStorage )
1784 Reference< XInterface > xMe( *this );
1786 m_aStorageListeners.forEach< XStorageChangeListener >(
1787 [&xMe, &xNewRootStorage] (uno::Reference<XStorageChangeListener> const& xListener) {
1788 return xListener->notifyStorageChange(xMe, xNewRootStorage);
1792 void ODatabaseDocument::disposing()
1794 OSL_TRACE( "DD: disp: %p: %p", this, m_pImpl.get() );
1795 if ( !m_pImpl.is() )
1797 // this means that we're already disposed
1798 OSL_ENSURE( ODatabaseDocument_OfficeDocument::rBHelper.bDisposed, "ODatabaseDocument::disposing: no impl anymore, but not yet disposed!" );
1799 return;
1802 if ( impl_isInitialized() )
1803 m_aEventNotifier.notifyDocumentEvent( "OnUnload" );
1805 Reference< XModel > xHoldAlive( this );
1807 m_aEventNotifier.disposing();
1809 lang::EventObject aDisposeEvent(static_cast<XWeak*>(this));
1810 m_aModifyListeners.disposeAndClear( aDisposeEvent );
1811 m_aCloseListener.disposeAndClear( aDisposeEvent );
1812 m_aStorageListeners.disposeAndClear( aDisposeEvent );
1814 // this is the list of objects which we currently hold as member. Upon resetting
1815 // those members, we can (potentially) release the last reference to them, in which
1816 // case they will be deleted - if they're C++ implementations, that is :).
1817 // Some of those implementations are offending enough to require the SolarMutex, which
1818 // means we should not release the last reference while our own mutex is locked ...
1819 ::std::list< Reference< XInterface > > aKeepAlive;
1821 // SYNCHRONIZED ->
1822 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1824 OSL_ENSURE( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1825 // normally, nobody should explicitly dispose, but only XCloseable::close
1826 // the document. And upon closing, our controllers are closed, too
1829 uno::Reference<uno::XInterface> xUIInterface( m_xUIConfigurationManager );
1830 aKeepAlive.push_back( xUIInterface );
1832 m_xUIConfigurationManager = nullptr;
1834 clearObjectContainer( m_xForms );
1835 clearObjectContainer( m_xReports );
1837 // reset the macro mode: in case the our impl struct stays alive (e.g. because our DataSource
1838 // object still exists), and somebody subsequently re-opens the document, we want to have
1839 // the security warning, again.
1840 m_pImpl->resetMacroExecutionMode();
1842 // similar arguing for our ViewMonitor
1843 m_aViewMonitor.reset();
1845 // tell our Impl to forget us
1846 m_pImpl->modelIsDisposing( impl_isInitialized(), ODatabaseModelImpl::ResetModelAccess() );
1848 // now, at the latest, the controller array should be empty. Controllers are
1849 // expected to listen for our disposal, and disconnect then
1850 OSL_ENSURE( m_aControllers.empty(), "ODatabaseDocument::disposing: there still are controllers!" );
1851 impl_disposeControllerFrames_nothrow();
1854 uno::Reference<uno::XInterface> xModuleInterface( m_xModuleManager );
1855 aKeepAlive.push_back( xModuleInterface );
1857 m_xModuleManager.clear();
1860 uno::Reference<uno::XInterface> xTitleInterface( m_xTitleHelper );
1861 aKeepAlive.push_back( xTitleInterface );
1863 m_xTitleHelper.clear();
1865 m_pImpl.clear();
1867 aGuard.clear();
1868 // <- SYNCHRONIZED
1870 aKeepAlive.clear();
1873 // XComponent
1874 void SAL_CALL ODatabaseDocument::dispose( ) throw (RuntimeException, std::exception)
1876 ::cppu::WeakComponentImplHelperBase::dispose();
1879 void SAL_CALL ODatabaseDocument::addEventListener( const Reference< lang::XEventListener >& _xListener ) throw (RuntimeException, std::exception)
1881 ::cppu::WeakComponentImplHelperBase::addEventListener( _xListener );
1884 void SAL_CALL ODatabaseDocument::removeEventListener( const Reference< lang::XEventListener >& _xListener ) throw (RuntimeException, std::exception)
1886 ::cppu::WeakComponentImplHelperBase::removeEventListener( _xListener );
1889 // XServiceInfo
1890 OUString ODatabaseDocument::getImplementationName() throw(RuntimeException, std::exception)
1892 return OUString("com.sun.star.comp.dba.ODatabaseDocument");
1895 Sequence< OUString > ODatabaseDocument::getSupportedServiceNames() throw (RuntimeException, std::exception)
1897 return { "com.sun.star.sdb.OfficeDatabaseDocument", "com.sun.star.document.OfficeDocument" };
1900 sal_Bool ODatabaseDocument::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
1902 return cppu::supportsService(this, _rServiceName);
1905 Reference< XDataSource > SAL_CALL ODatabaseDocument::getDataSource() throw (RuntimeException, std::exception)
1907 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1908 return m_pImpl->getOrCreateDataSource();
1911 namespace
1913 /// Property map for embedded import info set.
1914 comphelper::PropertyMapEntry const aEmbeddedImportInfoMap[] =
1916 {OUString("StreamRelPath"), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1917 {OUString("StreamName"), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1918 {OUString("SourceStorage"), 0, cppu::UnoType<embed::XStorage>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1919 {OUString(), 0, css::uno::Type(), 0, 0}
1923 void SAL_CALL ODatabaseDocument::loadFromStorage(const Reference<XStorage>& xStorage, const Sequence<PropertyValue>& rMediaDescriptor) throw (IllegalArgumentException, DoubleInitializationException, IOException, Exception, RuntimeException, std::exception)
1925 DocumentGuard aGuard(*this, DocumentGuard::InitMethod);
1927 uno::Reference<beans::XPropertySet> xInfoSet(comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aEmbeddedImportInfoMap)));
1928 comphelper::NamedValueCollection aDescriptor(rMediaDescriptor);
1929 xInfoSet->setPropertyValue("StreamRelPath", uno::makeAny(aDescriptor.getOrDefault("HierarchicalDocumentName", OUString())));
1930 xInfoSet->setPropertyValue("StreamName", uno::makeAny(OUString("content.xml")));
1931 xInfoSet->setPropertyValue("SourceStorage", uno::makeAny(xStorage));
1933 uno::Sequence<uno::Any> aFilterCreationArgs(1);
1934 aFilterCreationArgs[0] <<= xInfoSet;
1936 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);
1938 uno::Reference<lang::XComponent> xComponent(*this, uno::UNO_QUERY_THROW);
1939 xImporter->setTargetDocument(xComponent);
1941 uno::Reference<document::XFilter> xFilter(xImporter, uno::UNO_QUERY_THROW);
1942 uno::Sequence<beans::PropertyValue> aFilterArgs;
1943 xFilter->filter(aFilterArgs);
1945 // In case of embedding, XModel::attachResource is already called.
1946 if (m_bEmbedded)
1947 impl_setInitialized();
1949 impl_setModified_nothrow(false, aGuard);
1952 void SAL_CALL ODatabaseDocument::storeToStorage( const Reference< XStorage >& _rxStorage, const Sequence< PropertyValue >& _rMediaDescriptor ) throw (IllegalArgumentException, IOException, Exception, RuntimeException, std::exception)
1954 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1955 impl_storeToStorage_throw( _rxStorage, _rMediaDescriptor, aGuard );
1958 void SAL_CALL ODatabaseDocument::switchToStorage( const Reference< XStorage >& _rxNewRootStorage ) throw (IllegalArgumentException, IOException, Exception, RuntimeException, std::exception)
1960 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1962 Reference< XStorage > xNewRootStorage( m_pImpl->switchToStorage( _rxNewRootStorage ) );
1964 aGuard.clear();
1965 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1968 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentStorage( ) throw (IOException, Exception, RuntimeException, std::exception)
1970 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1971 return m_pImpl->getOrCreateRootStorage();
1974 void SAL_CALL ODatabaseDocument::addStorageChangeListener( const Reference< XStorageChangeListener >& Listener ) throw (RuntimeException, std::exception)
1976 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1977 m_aStorageListeners.addInterface( Listener );
1980 void SAL_CALL ODatabaseDocument::removeStorageChangeListener( const Reference< XStorageChangeListener >& Listener ) throw (RuntimeException, std::exception)
1982 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1983 m_aStorageListeners.addInterface( Listener );
1986 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getBasicLibraries() throw (RuntimeException, std::exception)
1988 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1989 return m_pImpl->getLibraryContainer( true );
1992 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getDialogLibraries() throw (RuntimeException, std::exception)
1994 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1995 return m_pImpl->getLibraryContainer( false );
1998 sal_Bool SAL_CALL ODatabaseDocument::getAllowMacroExecution() throw (RuntimeException, std::exception)
2000 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2001 return m_pImpl->adjustMacroMode_AutoReject();
2004 Reference< XEmbeddedScripts > SAL_CALL ODatabaseDocument::getScriptContainer() throw (RuntimeException, std::exception)
2006 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2007 return this;
2010 Reference< provider::XScriptProvider > SAL_CALL ODatabaseDocument::getScriptProvider( ) throw (RuntimeException, std::exception)
2012 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2014 Reference< XScriptProvider > xScriptProvider( m_xScriptProvider );
2015 if ( !xScriptProvider.is() )
2017 Reference < XScriptProviderFactory > xFactory =
2018 theMasterScriptProviderFactory::get( m_pImpl->m_aContext );
2020 Any aScriptProviderContext;
2021 if ( m_bAllowDocumentScripting )
2022 aScriptProviderContext <<= Reference< XModel >( this );
2024 xScriptProvider.set( xFactory->createScriptProvider( aScriptProviderContext ), UNO_SET_THROW );
2025 m_xScriptProvider = xScriptProvider;
2028 return xScriptProvider;
2031 Reference< XNameReplace > SAL_CALL ODatabaseDocument::getEvents( ) throw (RuntimeException, std::exception)
2033 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2034 return m_pEventContainer;
2037 Reference< XInterface > ODatabaseDocument::getThis() const
2039 return *const_cast< ODatabaseDocument* >( this );
2042 struct CreateAny : public ::std::unary_function< Reference<XController>, Any>
2044 Any operator() (const Reference<XController>& lhs) const
2046 return makeAny(lhs);
2050 // XModel2
2051 Reference< XEnumeration > SAL_CALL ODatabaseDocument::getControllers( ) throw (RuntimeException, std::exception)
2053 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2054 uno::Sequence< Any> aController( m_aControllers.size() );
2055 ::std::transform( m_aControllers.begin(), m_aControllers.end(), aController.getArray(), CreateAny() );
2056 return new ::comphelper::OAnyEnumeration(aController);
2059 Sequence< OUString > SAL_CALL ODatabaseDocument::getAvailableViewControllerNames( ) throw (RuntimeException, std::exception)
2061 Sequence< OUString > aNames { SERVICE_SDB_APPLICATIONCONTROLLER };
2062 return aNames;
2065 Reference< XController2 > SAL_CALL ODatabaseDocument::createDefaultViewController( const Reference< XFrame >& Frame ) throw (IllegalArgumentException, Exception, RuntimeException, std::exception)
2067 return createViewController( "Default", Sequence< PropertyValue >(), Frame);
2070 Reference< XController2 > SAL_CALL ODatabaseDocument::createViewController( const OUString& ViewName, const Sequence< PropertyValue >& Arguments, const Reference< XFrame >& Frame ) throw (IllegalArgumentException, Exception, RuntimeException, std::exception)
2072 if ( ViewName != "Default" && ViewName != "Preview" )
2073 throw IllegalArgumentException( OUString(), *this, 1 );
2074 if ( !Frame.is() )
2075 throw IllegalArgumentException( OUString(), *this, 3 );
2077 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2078 aGuard.clear();
2080 Reference< XController2 > xController(
2081 m_pImpl->m_aContext->getServiceManager()->createInstanceWithContext("org.openoffice.comp.dbu.OApplicationController", m_pImpl->m_aContext),
2082 UNO_QUERY_THROW );
2084 ::comphelper::NamedValueCollection aInitArgs( Arguments );
2085 aInitArgs.put( "Frame", Frame );
2086 if ( ViewName == "Preview" )
2087 aInitArgs.put( "Preview", true );
2088 Reference< XInitialization > xInitController( xController, UNO_QUERY_THROW );
2089 xInitController->initialize( aInitArgs.getWrappedPropertyValues() );
2091 return xController;
2094 Reference< XTitle > const & ODatabaseDocument::impl_getTitleHelper_throw()
2096 if ( ! m_xTitleHelper.is ())
2098 Reference< XUntitledNumbers > xDesktop(Desktop::create(m_pImpl->m_aContext), uno::UNO_QUERY_THROW);
2099 Reference< frame::XModel > xThis (getThis(), uno::UNO_QUERY_THROW);
2101 ::framework::TitleHelper* pHelper = new ::framework::TitleHelper(m_pImpl->m_aContext);
2102 m_xTitleHelper.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2103 pHelper->setOwner (xThis );
2104 pHelper->connectWithUntitledNumbers (xDesktop);
2107 return m_xTitleHelper;
2110 uno::Reference< frame::XUntitledNumbers > ODatabaseDocument::impl_getUntitledHelper_throw(const uno::Reference< uno::XInterface >& _xComponent)
2112 if ( !m_xModuleManager.is() )
2113 m_xModuleManager.set( ModuleManager::create(m_pImpl->m_aContext) );
2115 OUString sModuleId;
2118 sModuleId = m_xModuleManager->identify( _xComponent );
2120 catch(const uno::Exception&)
2123 uno::Reference< frame::XUntitledNumbers > xNumberedControllers;
2125 TNumberedController::const_iterator aFind = m_aNumberedControllers.find(sModuleId);
2126 if ( aFind == m_aNumberedControllers.end() )
2128 uno::Reference< frame::XModel > xThis(static_cast< frame::XModel* >(this), uno::UNO_QUERY_THROW);
2129 ::comphelper::NumberedCollection* pHelper = new ::comphelper::NumberedCollection();
2130 xNumberedControllers.set(static_cast< ::cppu::OWeakObject* >(pHelper), uno::UNO_QUERY_THROW);
2132 pHelper->setOwner (xThis);
2134 m_aNumberedControllers.insert(TNumberedController::value_type(sModuleId,xNumberedControllers));
2136 else
2137 xNumberedControllers = aFind->second;
2139 return xNumberedControllers;
2142 // css.frame.XTitle
2143 OUString SAL_CALL ODatabaseDocument::getTitle()
2144 throw (uno::RuntimeException, std::exception)
2146 // SYNCHRONIZED ->
2147 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2148 return impl_getTitleHelper_throw()->getTitle();
2151 // css.frame.XTitle
2152 void SAL_CALL ODatabaseDocument::setTitle( const OUString& sTitle )
2153 throw (uno::RuntimeException, std::exception)
2155 // SYNCHRONIZED ->
2156 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2157 impl_getTitleHelper_throw()->setTitle( sTitle );
2158 m_aEventNotifier.notifyDocumentEventAsync( "OnTitleChanged" );
2159 // <- SYNCHRONIZED
2162 // css.frame.XTitleChangeBroadcaster
2163 void SAL_CALL ODatabaseDocument::addTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2164 throw (uno::RuntimeException, std::exception)
2166 // SYNCHRONIZED ->
2167 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2169 uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2170 xBroadcaster->addTitleChangeListener( xListener );
2173 // css.frame.XTitleChangeBroadcaster
2174 void SAL_CALL ODatabaseDocument::removeTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2175 throw (uno::RuntimeException, std::exception)
2177 // SYNCHRONIZED ->
2178 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2180 uno::Reference< frame::XTitleChangeBroadcaster > xBroadcaster( impl_getTitleHelper_throw(), uno::UNO_QUERY_THROW );
2181 xBroadcaster->removeTitleChangeListener( xListener );
2184 // css.frame.XUntitledNumbers
2185 ::sal_Int32 SAL_CALL ODatabaseDocument::leaseNumber( const uno::Reference< uno::XInterface >& xComponent )
2186 throw (lang::IllegalArgumentException,
2187 uno::RuntimeException, std::exception )
2189 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2190 return impl_getUntitledHelper_throw(xComponent)->leaseNumber (xComponent);
2193 // css.frame.XUntitledNumbers
2194 void SAL_CALL ODatabaseDocument::releaseNumber( ::sal_Int32 nNumber )
2195 throw (lang::IllegalArgumentException,
2196 uno::RuntimeException, std::exception )
2198 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2199 impl_getUntitledHelper_throw()->releaseNumber (nNumber);
2202 // css.frame.XUntitledNumbers
2203 void SAL_CALL ODatabaseDocument::releaseNumberForComponent( const uno::Reference< uno::XInterface >& xComponent )
2204 throw (lang::IllegalArgumentException,
2205 uno::RuntimeException, std::exception )
2207 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2208 impl_getUntitledHelper_throw(xComponent)->releaseNumberForComponent (xComponent);
2211 // css.frame.XUntitledNumbers
2212 OUString SAL_CALL ODatabaseDocument::getUntitledPrefix() throw (uno::RuntimeException, std::exception)
2214 return OUString();
2217 } // namespace dbaccess
2219 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* SAL_CALL
2220 com_sun_star_comp_dba_ODatabaseDocument(css::uno::XComponentContext* context,
2221 css::uno::Sequence<css::uno::Any> const &)
2223 Reference<XUnoTunnel> xDBContextTunnel(DatabaseContext::create(context), UNO_QUERY_THROW);
2224 dbaccess::ODatabaseContext* pContext = reinterpret_cast<dbaccess::ODatabaseContext*>(
2225 xDBContextTunnel->getSomething(
2226 dbaccess::ODatabaseContext::getUnoTunnelImplementationId()));
2228 rtl::Reference<dbaccess::ODatabaseModelImpl> pImpl(
2229 new dbaccess::ODatabaseModelImpl(context, *pContext));
2230 css::uno::Reference<XInterface> inst(pImpl->createNewModel_deliverOwnership());
2231 inst->acquire();
2232 return inst.get();
2235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */