tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / dbaccess / source / core / dataaccess / databasedocument.cxx
blobdcacb7355bda34404df4380d52c8e077ca4db536
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 <strings.hrc>
22 #include "databasedocument.hxx"
23 #include "documenteventexecutor.hxx"
24 #include <databasecontext.hxx>
25 #include "documentcontainer.hxx"
26 #include "commandcontainer.hxx"
27 #include <sdbcoretools.hxx>
28 #include <strings.hxx>
29 #include <recovery/dbdocrecovery.hxx>
31 #include <officecfg/Office/Common.hxx>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/document/XExporter.hpp>
34 #include <com/sun/star/document/XFilter.hpp>
35 #include <com/sun/star/document/XImporter.hpp>
36 #include <com/sun/star/document/XGraphicStorageHandler.hpp>
37 #include <com/sun/star/document/GraphicStorageHandler.hpp>
38 #include <com/sun/star/frame/Desktop.hpp>
39 #include <com/sun/star/frame/ModuleManager.hpp>
40 #include <com/sun/star/io/IOException.hpp>
41 #include <com/sun/star/io/XSeekable.hpp>
42 #include <com/sun/star/io/XOutputStream.hpp>
43 #include <com/sun/star/io/XTruncate.hpp>
44 #include <com/sun/star/lang/NoSupportException.hpp>
45 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
46 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
47 #include <com/sun/star/sdb/DatabaseContext.hpp>
48 #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp>
49 #include <com/sun/star/task/XStatusIndicator.hpp>
50 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
51 #include <com/sun/star/ui/UIConfigurationManager.hpp>
52 #include <com/sun/star/util/CloseVetoException.hpp>
53 #include <com/sun/star/view/XSelectionSupplier.hpp>
54 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
55 #include <com/sun/star/xml/sax/Writer.hpp>
57 #include <com/sun/star/script/XStorageBasedLibraryContainer.hpp>
58 #include <com/sun/star/awt/XControl.hpp>
59 #include <com/sun/star/awt/DialogProvider.hpp>
61 #include <comphelper/documentconstants.hxx>
62 #include <comphelper/enumhelper.hxx>
63 #include <comphelper/genericpropertyset.hxx>
64 #include <comphelper/namedvaluecollection.hxx>
65 #include <comphelper/numberedcollection.hxx>
66 #include <comphelper/servicehelper.hxx>
67 #include <comphelper/storagehelper.hxx>
68 #include <comphelper/propertysetinfo.hxx>
69 #include <comphelper/types.hxx>
71 #include <connectivity/dbtools.hxx>
73 #include <cppuhelper/exc_hlp.hxx>
74 #include <cppuhelper/supportsservice.hxx>
75 #include <framework/titlehelper.hxx>
76 #include <unotools/saveopt.hxx>
77 #include <comphelper/diagnose_ex.hxx>
78 #include <osl/diagnose.h>
80 #include <vcl/GraphicObject.hxx>
81 #include <tools/urlobj.hxx>
83 using namespace ::com::sun::star::uno;
84 using namespace ::com::sun::star::beans;
85 using namespace ::com::sun::star::frame;
86 using namespace ::com::sun::star::lang;
87 using namespace ::com::sun::star::container;
88 using namespace ::com::sun::star::document;
89 using namespace ::com::sun::star::io;
90 using namespace ::com::sun::star::util;
91 using namespace ::com::sun::star::embed;
92 using namespace ::com::sun::star::task;
93 using namespace ::com::sun::star::view;
94 using namespace ::com::sun::star::sdb;
95 using namespace ::com::sun::star::sdbc;
96 using namespace ::com::sun::star;
97 using namespace ::com::sun::star::xml::sax;
98 using namespace ::com::sun::star::script;
99 using namespace ::com::sun::star::script::provider;
100 using namespace ::com::sun::star::ui;
101 using namespace ::cppu;
103 namespace dbaccess
106 // ViewMonitor
108 bool ViewMonitor::onControllerConnected( const Reference< XController >& _rxController )
110 bool bFirstControllerEver = !m_bEverHadController;
111 m_bEverHadController = true;
113 m_xLastConnectedController = _rxController;
114 m_bLastIsFirstEverController = bFirstControllerEver;
116 return bFirstControllerEver;
119 bool ViewMonitor::onSetCurrentController( const Reference< XController >& _rxController )
121 // we interpret this as "loading the document (including UI) is finished",
122 // if and only if this is the controller which was last connected, and it was the
123 // first controller ever connected
124 bool bLoadFinished = ( _rxController == m_xLastConnectedController ) && m_bLastIsFirstEverController;
126 // notify the respective events
127 if ( bLoadFinished )
128 m_rEventNotifier.notifyDocumentEventAsync( m_bIsNewDocument ? u"OnNew"_ustr : u"OnLoad"_ustr, nullptr, Any() );
130 return bLoadFinished;
134 ODatabaseDocument::ODatabaseDocument(const ::rtl::Reference<ODatabaseModelImpl>& _pImpl )
135 :ModelDependentComponent( _pImpl )
136 ,ODatabaseDocument_OfficeDocument( getMutex() )
137 ,m_aModifyListeners( getMutex() )
138 ,m_aCloseListener( getMutex() )
139 ,m_aStorageListeners( getMutex() )
140 ,m_pEventContainer( new DocumentEvents( *this, getMutex(), _pImpl->getDocumentEvents() ) )
141 ,m_aEventNotifier( *this, getMutex() )
142 ,m_aViewMonitor( m_aEventNotifier )
143 ,m_eInitState( NotInitialized )
144 ,m_bClosing( false )
145 ,m_bAllowDocumentScripting( false )
146 ,m_bHasBeenRecovered( false )
147 ,m_bEmbedded(false)
149 osl_atomic_increment( &m_refCount );
151 impl_reparent_nothrow( m_xForms );
152 impl_reparent_nothrow( m_xReports );
153 if (auto xTableDef = m_pImpl->m_xTableDefinitions.get())
154 xTableDef->setParent(*this);
155 impl_reparent_nothrow( m_pImpl->m_xCommandDefinitions );
157 m_pEventExecutor = new DocumentEventExecutor( m_pImpl->m_aContext, this );
159 osl_atomic_decrement( &m_refCount );
161 // if there previously was a document instance for the same Impl which was already initialized,
162 // then consider ourself initialized, too.
163 // #i94840#
164 if ( !m_pImpl->hadInitializedDocument() )
165 return;
167 // Note we set our init-state to "Initializing", not "Initialized". We're created from inside the ModelImpl,
168 // which is expected to call attachResource in case there was a previous incarnation of the document,
169 // so we can properly finish our initialization then.
170 impl_setInitializing();
172 if ( !m_pImpl->getURL().isEmpty() )
174 // if the previous incarnation of the DatabaseDocument already had a URL, then creating this incarnation
175 // here is effectively loading the document.
176 // #i105505#
177 m_aViewMonitor.onLoadedDocument();
181 ODatabaseDocument::~ODatabaseDocument()
183 if ( !ODatabaseDocument_OfficeDocument::rBHelper.bInDispose && !ODatabaseDocument_OfficeDocument::rBHelper.bDisposed )
185 acquire();
186 dispose();
190 Any SAL_CALL ODatabaseDocument::queryInterface( const Type& _rType )
192 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
193 // which already contains macros. In this case, the database document itself is not
194 // allowed to contain macros, too.
195 if ( !m_bAllowDocumentScripting
196 && ( _rType.equals( cppu::UnoType<XEmbeddedScripts>::get() )
197 || _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() )
200 return Any();
202 return ODatabaseDocument_OfficeDocument::queryInterface(_rType);
205 Sequence< Type > SAL_CALL ODatabaseDocument::getTypes( )
207 Sequence< Type > aTypes = ODatabaseDocument_OfficeDocument::getTypes();
209 // strip XEmbeddedScripts and XScriptInvocationContext if we have any form/report
210 // which already contains macros. In this case, the database document itself is not
211 // allowed to contain macros, too.
212 if ( !m_bAllowDocumentScripting )
214 auto [begin, end] = asNonConstRange(aTypes);
215 auto newEnd = std::remove_if( begin, end,
216 [](const Type& t)
217 { return t == cppu::UnoType<XEmbeddedScripts>::get() ||
218 t == cppu::UnoType<XScriptInvocationContext>::get();} );
219 aTypes.realloc( std::distance(begin, newEnd) );
222 return aTypes;
225 Sequence< sal_Int8 > SAL_CALL ODatabaseDocument::getImplementationId( )
227 return css::uno::Sequence<sal_Int8>();
230 // local functions
231 namespace
233 Reference< XStatusIndicator > lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments )
235 Reference< XStatusIndicator > xStatusIndicator;
236 return _rArguments.getOrDefault( u"StatusIndicator"_ustr, xStatusIndicator );
239 void lcl_triggerStatusIndicator_throw( const ::comphelper::NamedValueCollection& _rArguments, DocumentGuard& _rGuard, const bool _bStart )
241 Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
242 if ( !xStatusIndicator.is() )
243 return;
245 _rGuard.clear();
248 if ( _bStart )
249 xStatusIndicator->start( OUString(), sal_Int32(1000000) );
250 else
251 xStatusIndicator->end();
253 catch( const Exception& )
255 DBG_UNHANDLED_EXCEPTION("dbaccess");
257 _rGuard.reset();
258 // note that |reset| can throw a DisposedException
261 void lcl_extractStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Sequence< Any >& _rCallArgs )
263 Reference< XStatusIndicator > xStatusIndicator( lcl_extractStatusIndicator( _rArguments ) );
264 if ( !xStatusIndicator.is() )
265 return;
267 sal_Int32 nLength = _rCallArgs.getLength();
268 _rCallArgs.realloc( nLength + 1 );
269 _rCallArgs.getArray()[ nLength ] <<= xStatusIndicator;
272 void lcl_extractAndStartStatusIndicator( const ::comphelper::NamedValueCollection& _rArguments, Reference< XStatusIndicator >& _rxStatusIndicator,
273 Sequence< Any >& _rCallArgs )
275 _rxStatusIndicator = lcl_extractStatusIndicator( _rArguments );
276 if ( !_rxStatusIndicator.is() )
277 return;
281 _rxStatusIndicator->start( OUString(), sal_Int32(1000000) );
283 sal_Int32 nLength = _rCallArgs.getLength();
284 _rCallArgs.realloc( nLength + 1 );
285 _rCallArgs.getArray()[ nLength ] <<= _rxStatusIndicator;
287 catch( const Exception& )
289 DBG_UNHANDLED_EXCEPTION("dbaccess");
293 Sequence< PropertyValue > lcl_appendFileNameToDescriptor( const ::comphelper::NamedValueCollection& _rDescriptor, const OUString& _rURL )
295 if ( _rURL.isEmpty() )
296 return _rDescriptor.getPropertyValues();
298 ::comphelper::NamedValueCollection aMutableDescriptor( _rDescriptor );
299 aMutableDescriptor.put( u"FileName"_ustr, _rURL );
300 aMutableDescriptor.put( u"URL"_ustr, _rURL );
301 return aMutableDescriptor.getPropertyValues();
305 constexpr OUString sPictures = u"Pictures"_ustr;
307 // base documents seem to have a different behaviour to other documents, the
308 // root storage contents at least seem to be re-used over different saves, thus if there is a
309 // top level Picture directory it is never cleared.
310 // If we delete the 'Pictures' directory then the dialog library storage which does store
311 // any embed images will not work properly. ( this is due to the fact it will
312 // try to load the dialog which will try and access the embed images, if those images are not cached in
313 // memory it will try to read them from the Picture directory which is now gone, so... we have to use this
314 // inglorious hack below which basically will
316 // a) create a temp storage
318 // b) introspect any dialogs for any embed graphics and grab the associate URL(s)
320 // c) populate the temp storage with the associated embed images ( will be stored in a 'Pictures' folder )
322 // d) delete the 'Picture' element from the root storage
324 // e) copy the Pictures element of the temp storage to the root storage
326 // this assumes that we don't use the Pictures folder in the root of the base
327 // document for anything, I believe this is a valid assumption ( as much as
328 // I could check anyway )
330 /// @throws RuntimeException
331 static void lcl_uglyHackToStoreDialogeEmbedImages( const Reference< XStorageBasedLibraryContainer >& xDlgCont, const Reference< XStorage >& xStorage, const Reference< XModel >& rxModel, const Reference<XComponentContext >& rxContext )
333 const Sequence< OUString > sLibraries = xDlgCont->getElementNames();
334 Reference< XStorage > xTmpPic = xStorage->openStorageElement( u"tempPictures"_ustr, ElementModes::READWRITE );
336 std::vector<uno::Reference<graphic::XGraphic>> vxGraphicList;
337 for ( OUString const & sLibrary : sLibraries )
339 xDlgCont->loadLibrary( sLibrary );
340 Reference< XNameContainer > xLib;
341 xDlgCont->getByName( sLibrary ) >>= xLib;
342 if ( xLib.is() )
344 Sequence< OUString > sDialogs = xLib->getElementNames();
345 sal_Int32 nDialogs( sDialogs.getLength() );
346 for ( sal_Int32 j=0; j < nDialogs; ++j )
348 Reference < awt::XDialogProvider > xDlgPrv = awt::DialogProvider::createWithModel(rxContext, rxModel);
349 OUString sDialogUrl =
350 "vnd.sun.star.script:" + sLibrary + "." + sDialogs[j] + "?location=document";
352 Reference< css::awt::XControl > xDialog( xDlgPrv->createDialog( sDialogUrl ), UNO_QUERY );
353 Reference< XInterface > xModel( xDialog->getModel() );
354 vcl::graphic::SearchForGraphics(xModel, vxGraphicList);
358 // if we have any image urls, make sure we copy the associated images into tempPictures
359 if (!vxGraphicList.empty())
361 // Export the images to the storage
362 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
363 xGraphicStorageHandler.set(GraphicStorageHandler::createWithStorage(rxContext, xTmpPic));
364 if (xGraphicStorageHandler.is())
366 for (uno::Reference<graphic::XGraphic> const & rxGraphic : vxGraphicList)
368 xGraphicStorageHandler->saveGraphic(rxGraphic);
371 // delete old 'Pictures' storage and copy the contents of tempPictures into xStorage
372 xStorage->removeElement( sPictures );
373 xTmpPic->copyElementTo( sPictures, xStorage, sPictures );
375 else
377 // clean up an existing Pictures dir
378 if ( xStorage->isStorageElement( sPictures ) )
379 xStorage->removeElement( sPictures );
383 void ODatabaseDocument::impl_setInitialized()
385 m_eInitState = Initialized;
387 // start event notifications
388 m_aEventNotifier.onDocumentInitialized();
391 void ODatabaseDocument::impl_reset_nothrow()
395 m_pImpl->clearConnections();
396 m_pImpl->disposeStorages();
397 m_pImpl->resetRootStorage();
399 clearObjectContainer( m_xForms );
400 clearObjectContainer( m_xReports );
401 clearObjectContainer( m_pImpl->m_xTableDefinitions );
402 clearObjectContainer( m_pImpl->m_xCommandDefinitions );
404 m_eInitState = NotInitialized;
406 m_pImpl->reset();
408 catch(const Exception&)
410 DBG_UNHANDLED_EXCEPTION("dbaccess");
412 m_pImpl->m_bDocumentReadOnly = false;
415 namespace
417 /** property map for import/export info set */
418 comphelper::PropertyMapEntry const aExportInfoMap[] =
420 { u"BaseURI"_ustr, 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0 },
421 { u"StreamName"_ustr, 0, ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0 },
422 { u"UsePrettyPrinting"_ustr, 0, ::cppu::UnoType<sal_Bool>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
423 { u"TargetStorage"_ustr, 0, cppu::UnoType<embed::XStorage>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
424 { u"StreamRelPath"_ustr, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
428 void ODatabaseDocument::impl_import_nolck_throw( const Reference< XComponentContext >& _rContext, const Reference< XInterface >& _rxTargetComponent,
429 const ::comphelper::NamedValueCollection& _rResource )
431 Sequence< Any > aFilterCreationArgs;
432 Reference< XStatusIndicator > xStatusIndicator;
433 lcl_extractAndStartStatusIndicator( _rResource, xStatusIndicator, aFilterCreationArgs );
435 uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
436 OUString sBaseURI = _rResource.getOrDefault(u"BaseURI"_ustr, OUString());
437 if (sBaseURI.isEmpty())
438 sBaseURI = _rResource.getOrDefault(u"URL"_ustr,OUString());
439 assert(!sBaseURI.isEmpty()); // needed for relative URLs
440 xInfoSet->setPropertyValue(u"BaseURI"_ustr, uno::Any(sBaseURI));
441 xInfoSet->setPropertyValue(u"StreamName"_ustr, uno::Any(u"content.xml"_ustr));
443 const sal_Int32 nCount = aFilterCreationArgs.getLength();
444 aFilterCreationArgs.realloc(nCount + 1);
445 aFilterCreationArgs.getArray()[nCount] <<= xInfoSet;
447 Reference< XImporter > xImporter(
448 _rContext->getServiceManager()->createInstanceWithArgumentsAndContext(u"com.sun.star.comp.sdb.DBFilter"_ustr, aFilterCreationArgs, _rContext),
449 UNO_QUERY_THROW );
451 Reference< XComponent > xComponent( _rxTargetComponent, UNO_QUERY_THROW );
452 xImporter->setTargetDocument( xComponent );
454 Reference< XFilter > xFilter( xImporter, UNO_QUERY_THROW );
455 Sequence< PropertyValue > aFilterArgs( ODatabaseModelImpl::stripLoadArguments( _rResource ).getPropertyValues() );
456 xFilter->filter( aFilterArgs );
458 if ( xStatusIndicator.is() )
459 xStatusIndicator->end();
462 void SAL_CALL ODatabaseDocument::initNew( )
464 // SYNCHRONIZED ->
465 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
467 impl_reset_nothrow();
469 impl_setInitializing();
471 // create a temporary storage
472 Reference< XStorage > xTempStor( ::comphelper::OStorageHelper::GetTemporaryStorage( m_pImpl->m_aContext ) );
474 // store therein
475 impl_storeToStorage_throw( xTempStor, Sequence< PropertyValue >(), aGuard );
477 // let the impl know we're now based on this storage
478 m_pImpl->switchToStorage( xTempStor );
480 // for the newly created document, allow document-wide scripting
481 m_bAllowDocumentScripting = true;
483 impl_setInitialized();
485 m_aEventNotifier.notifyDocumentEventAsync( u"OnTitleChanged"_ustr, nullptr, Any() );
487 impl_setModified_nothrow( false, aGuard );
488 // <- SYNCHRONIZED
490 m_aEventNotifier.notifyDocumentEvent( u"OnCreate"_ustr, nullptr, Any() );
492 impl_notifyStorageChange_nolck_nothrow( xTempStor );
495 void SAL_CALL ODatabaseDocument::load( const Sequence< PropertyValue >& Arguments )
497 // SYNCHRONIZED ->
498 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
500 impl_reset_nothrow();
502 ::comphelper::NamedValueCollection aResource( Arguments );
503 if ( aResource.has( u"FileName"_ustr ) && !aResource.has( u"URL"_ustr ) )
504 // FileName is the compatibility name for URL, so we might have clients passing
505 // a FileName only. However, some of our code works with the URL only, so ensure
506 // we have one.
507 aResource.put( u"URL"_ustr, aResource.get( u"FileName"_ustr ) );
508 if ( aResource.has( u"URL"_ustr ) && !aResource.has( u"FileName"_ustr ) )
509 // similar ... just in case there is legacy code which expects a FileName only
510 aResource.put( u"FileName"_ustr, aResource.get( u"URL"_ustr ) );
512 // now that somebody (perhaps) told us a macro execution mode, remember it as
513 // ImposedMacroExecMode
514 m_pImpl->setImposedMacroExecMode(
515 aResource.getOrDefault( u"MacroExecutionMode"_ustr, m_pImpl->getImposedMacroExecMode() ) );
517 impl_setInitializing();
520 aGuard.clear();
521 impl_import_nolck_throw( m_pImpl->m_aContext, *this, aResource );
522 aGuard.reset();
524 catch( const Exception& )
526 impl_reset_nothrow();
527 throw;
529 // tell our view monitor that the document has been loaded - this way it will fire the proper
530 // event (OnLoad instead of OnCreate) later on
531 m_aViewMonitor.onLoadedDocument();
533 // note that we do *not* call impl_setInitialized() here: The initialization is only complete
534 // when the XModel::attachResource has been called, not sooner.
535 // however, in case of embedding, XModel::attachResource is already called.
536 if (m_bEmbedded)
537 impl_setInitialized();
539 impl_setModified_nothrow( false, aGuard );
540 // <- SYNCHRONIZED
543 namespace
545 bool lcl_hasAnyModifiedSubComponent_throw( const Reference< XController >& i_rController )
547 Reference< css::sdb::application::XDatabaseDocumentUI > xDatabaseUI( i_rController, UNO_QUERY_THROW );
549 const Sequence< Reference< XComponent > > aComponents( xDatabaseUI->getSubComponents() );
551 bool isAnyModified = false;
552 for ( auto const & xComponent : aComponents )
554 Reference< XModifiable > xModify( xComponent, UNO_QUERY );
555 if ( xModify.is() )
557 isAnyModified = xModify->isModified();
558 continue;
561 // TODO: clarify: anything else to care for? Both the sub components with and without model
562 // should support the XModifiable interface, so I think nothing more is needed here.
563 OSL_FAIL( "lcl_hasAnyModifiedSubComponent_throw: anything left to do here?" );
566 return isAnyModified;
570 sal_Bool SAL_CALL ODatabaseDocument::wasModifiedSinceLastSave()
572 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
574 // The implementation here is somewhat sloppy, in that it returns whether *any* part of the whole
575 // database document, including opened sub components, is modified. This is more than what is requested:
576 // We need to return <TRUE/> if the doc itself, or any of the opened sub components, has been modified
577 // since the last call to any of the save* methods, or since the document has been loaded/created.
578 // However, the API definition explicitly allows to be that sloppy ...
580 if ( isModified() )
581 return true;
583 // auto recovery is an "UI feature", it is to restore the UI the user knows. Thus,
584 // we ask our connected controllers, not simply our existing form/report definitions.
585 // (There is some information which even cannot be obtained without asking the controller.
586 // For instance, newly created, but not yet saved, forms/reports are accessible via the
587 // controller only, but not via the model.)
591 for (auto const& controller : m_aControllers)
593 if ( lcl_hasAnyModifiedSubComponent_throw(controller) )
594 return true;
597 catch( const Exception& )
599 DBG_UNHANDLED_EXCEPTION("dbaccess");
602 return false;
605 void SAL_CALL ODatabaseDocument::storeToRecoveryFile( const OUString& i_TargetLocation, const Sequence< PropertyValue >& i_MediaDescriptor )
607 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
608 ModifyLock aLock( *this );
612 // create a storage for the target location
613 Reference< XStorage > xTargetStorage( impl_createStorageFor_throw( i_TargetLocation ) );
615 // first store the document as a whole into this storage
616 impl_storeToStorage_throw( xTargetStorage, i_MediaDescriptor, aGuard );
618 // save the sub components which need saving
619 DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext);
620 aDocRecovery.saveModifiedSubComponents( xTargetStorage, m_aControllers );
622 // commit the root storage
623 tools::stor::commitStorageIfWriteable( xTargetStorage );
625 catch( const IOException& )
627 throw;
629 catch( const RuntimeException& )
631 throw;
633 catch( const WrappedTargetException& )
635 throw;
637 catch( const Exception& )
639 Any aError = ::cppu::getCaughtException();
640 throw WrappedTargetException( OUString(), *this, aError );
644 void SAL_CALL ODatabaseDocument::recoverFromFile( const OUString& i_SourceLocation, const OUString& i_SalvagedFile, const Sequence< PropertyValue >& i_MediaDescriptor )
648 DocumentGuard aGuard( *this, DocumentGuard::InitMethod );
650 if ( i_SourceLocation.isEmpty() )
651 throw IllegalArgumentException( OUString(), *this, 1 );
654 // load the document itself, by simply delegating to our "load" method
656 // our load implementation expects the SalvagedFile and URL to be in the media descriptor
657 ::comphelper::NamedValueCollection aMediaDescriptor( i_MediaDescriptor );
658 aMediaDescriptor.put( u"SalvagedFile"_ustr, i_SalvagedFile );
659 aMediaDescriptor.put( u"URL"_ustr, i_SourceLocation );
661 aGuard.clear(); // (load has an own guarding scheme)
662 load( aMediaDescriptor.getPropertyValues() );
663 aGuard.reset();
665 // Without a controller, we are unable to recover the sub components, as they're always tied to a controller.
666 // So, everything else is done when the first controller is connected.
667 m_bHasBeenRecovered = true;
669 // tell the impl that we've been loaded from the given location
670 m_pImpl->setDocFileLocation( i_SourceLocation );
672 // by definition (of XDocumentRecovery), we're responsible for delivering a fully-initialized document,
673 // which includes an attachResource call.
674 const OUString sLogicalDocumentURL( i_SalvagedFile.isEmpty() ? i_SourceLocation : i_SalvagedFile );
675 impl_attachResource( sLogicalDocumentURL, aMediaDescriptor.getPropertyValues(), aGuard );
676 // <- SYNCHRONIZED
678 catch( const IOException& )
680 throw;
682 catch( const RuntimeException& )
684 throw;
686 catch( const WrappedTargetException& )
688 throw;
690 catch( const Exception& )
692 Any aError = ::cppu::getCaughtException();
693 throw WrappedTargetException( OUString(), *this, aError );
697 // XModel
698 sal_Bool SAL_CALL ODatabaseDocument::attachResource( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments )
700 if (_rURL.isEmpty() && _rArguments.getLength() == 1 && _rArguments[0].Name == "SetEmbedded")
702 m_bEmbedded = true;
703 return true;
706 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
707 bool bRet = false;
710 bRet = impl_attachResource( _rURL, _rArguments, aGuard );
712 catch( const RuntimeException& )
714 throw;
716 catch( const Exception& )
718 Any aError = ::cppu::getCaughtException();
719 throw WrappedTargetRuntimeException( OUString(), *this, aError );
721 return bRet;
724 bool ODatabaseDocument::impl_attachResource( const OUString& i_rLogicalDocumentURL,
725 const Sequence< PropertyValue >& i_rMediaDescriptor, DocumentGuard& _rDocGuard )
727 if (i_rLogicalDocumentURL == getURL())
729 ::comphelper::NamedValueCollection aArgs(i_rMediaDescriptor);
731 // this misuse of attachresource is a hack of the Basic importer code
732 // repurposing existing interfaces for uses it probably wasn't intended
733 // for
735 // we do not support macro signatures, so we can ignore that request
736 aArgs.remove(u"BreakMacroSignature"_ustr);
738 bool bMacroEventRead = false;
739 if ((aArgs.get( u"MacroEventRead"_ustr ) >>= bMacroEventRead) && bMacroEventRead)
740 m_pImpl->m_bMacroCallsSeenWhileLoading = true;
741 aArgs.remove( u"MacroEventRead"_ustr );
743 if (aArgs.empty())
744 return false;
747 // if no URL has been provided, the caller was lazy enough to not call our getURL - which is not allowed anymore,
748 // now since getURL and getLocation both return the same, so calling one of those should be simple.
749 OUString sDocumentURL( i_rLogicalDocumentURL );
750 OSL_ENSURE( !sDocumentURL.isEmpty(), "ODatabaseDocument::impl_attachResource: invalid URL!" );
751 if ( sDocumentURL.isEmpty() )
752 sDocumentURL = getURL();
754 m_pImpl->setResource( sDocumentURL, i_rMediaDescriptor );
756 if ( impl_isInitializing() )
757 { // this means we've just been loaded, and this is the attachResource call which follows
758 // the load call.
759 impl_setInitialized();
761 // determine whether the document as a whole, or sub documents, have macros. Especially the latter
762 // controls the availability of our XEmbeddedScripts and XScriptInvocationContext interfaces, and we
763 // should know this before anybody actually uses the object.
764 m_bAllowDocumentScripting = ( m_pImpl->determineEmbeddedMacros() != ODatabaseModelImpl::EmbeddedMacros::SubDocument );
766 _rDocGuard.clear();
767 // <- SYNCHRONIZED
768 m_aEventNotifier.notifyDocumentEvent( u"OnLoadFinished"_ustr, nullptr, Any() );
771 return true;
774 OUString SAL_CALL ODatabaseDocument::getURL( )
776 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
777 return m_pImpl->getURL();
780 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getArgs( )
782 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
783 return m_pImpl->getMediaDescriptor().getPropertyValues();
786 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getArgs2( const ::css::uno::Sequence< ::rtl::OUString >& requestedArgs )
788 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
789 std::vector<PropertyValue> aRet;
790 for (const auto & rArgName : requestedArgs)
791 aRet.push_back(PropertyValue(rArgName, 0, m_pImpl->getMediaDescriptor().get(rArgName), PropertyState_DIRECT_VALUE));
792 return comphelper::containerToSequence(aRet);
795 void SAL_CALL ODatabaseDocument::setArgs(const Sequence<beans::PropertyValue>& /* aArgs */)
797 throw NoSupportException();
800 void SAL_CALL ODatabaseDocument::connectController( const Reference< XController >& _xController )
802 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
804 #if OSL_DEBUG_LEVEL > 0
805 for (auto const& controller : m_aControllers)
807 OSL_ENSURE( controller != _xController, "ODatabaseDocument::connectController: this controller is already connected!" );
809 #endif
811 m_aControllers.push_back( _xController );
813 m_aEventNotifier.notifyDocumentEventAsync( u"OnViewCreated"_ustr, Reference< XController2 >( _xController, UNO_QUERY ), Any() );
815 bool bFirstControllerEver = m_aViewMonitor.onControllerConnected( _xController );
816 if ( !bFirstControllerEver )
817 return;
819 // check/adjust our macro mode.
820 m_pImpl->checkMacrosOnLoading();
823 void SAL_CALL ODatabaseDocument::disconnectController( const Reference< XController >& _xController )
825 bool bNotifyViewClosed = false;
826 bool bLastControllerGone = false;
827 bool bIsClosing = false;
829 // SYNCHRONIZED ->
831 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
833 Controllers::iterator pos = std::find( m_aControllers.begin(), m_aControllers.end(), _xController );
834 OSL_ENSURE( pos != m_aControllers.end(), "ODatabaseDocument::disconnectController: don't know this controller!" );
835 if ( pos != m_aControllers.end() )
837 m_aControllers.erase( pos );
838 bNotifyViewClosed = true;
841 if ( m_xCurrentController == _xController )
842 m_xCurrentController = nullptr;
844 bLastControllerGone = m_aControllers.empty();
845 bIsClosing = m_bClosing;
847 // <- SYNCHRONIZED
849 if ( bNotifyViewClosed )
850 m_aEventNotifier.notifyDocumentEvent( u"OnViewClosed"_ustr, Reference< XController2 >( _xController, UNO_QUERY ), Any() );
852 if ( !bLastControllerGone || bIsClosing )
853 return;
855 // if this was the last view, close the document as a whole
856 // #i51157#
859 close( true );
861 catch( const CloseVetoException& )
863 // okay, somebody vetoed and took ownership
867 void SAL_CALL ODatabaseDocument::lockControllers( )
869 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
871 ++m_pImpl->m_nControllerLockCount;
874 void SAL_CALL ODatabaseDocument::unlockControllers( )
876 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
878 --m_pImpl->m_nControllerLockCount;
881 sal_Bool SAL_CALL ODatabaseDocument::hasControllersLocked( )
883 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
885 return m_pImpl->m_nControllerLockCount != 0;
888 Reference< XController > SAL_CALL ODatabaseDocument::getCurrentController()
890 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
892 return m_xCurrentController.is() ? m_xCurrentController : ( m_aControllers.empty() ? Reference< XController >() : *m_aControllers.begin() );
895 void SAL_CALL ODatabaseDocument::setCurrentController( const Reference< XController >& _xController )
897 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
899 m_xCurrentController = _xController;
901 if ( !m_aViewMonitor.onSetCurrentController( _xController ) )
902 return;
904 // check if there are sub components to recover from our document storage
905 bool bAttemptRecovery = m_bHasBeenRecovered;
906 if ( !bAttemptRecovery && m_pImpl->getMediaDescriptor().has( u"ForceRecovery"_ustr ) )
907 // do not use getOrDefault, it will throw for invalid types, which is not desired here
908 m_pImpl->getMediaDescriptor().get( u"ForceRecovery"_ustr ) >>= bAttemptRecovery;
910 if ( !bAttemptRecovery )
911 return;
915 DatabaseDocumentRecovery aDocRecovery( m_pImpl->m_aContext );
916 aDocRecovery.recoverSubDocuments( m_pImpl->getRootStorage(), _xController );
918 catch( const Exception& )
920 DBG_UNHANDLED_EXCEPTION("dbaccess");
924 Reference< XInterface > SAL_CALL ODatabaseDocument::getCurrentSelection( )
926 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
928 Reference< XInterface > xRet;
929 Reference< XSelectionSupplier > xDocView( getCurrentController(), UNO_QUERY );
930 if ( xDocView.is() )
931 xRet.set(xDocView->getSelection(),UNO_QUERY);
933 return xRet;
936 // XStorable
937 sal_Bool SAL_CALL ODatabaseDocument::hasLocation( )
939 return !getLocation().isEmpty();
942 OUString SAL_CALL ODatabaseDocument::getLocation( )
944 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
945 return m_pImpl->getURL();
946 // both XStorable::getLocation and XModel::getURL have to return the URL of the document, *not*
947 // the location of the file which the document was possibly recovered from (which would be getDocFileLocation)
950 sal_Bool SAL_CALL ODatabaseDocument::isReadonly( )
952 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
953 return m_pImpl->m_bDocumentReadOnly;
956 void SAL_CALL ODatabaseDocument::store( )
958 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
960 OUString sDocumentURL( m_pImpl->getURL() );
961 if ( !sDocumentURL.isEmpty() )
963 if ( m_pImpl->getDocFileLocation() == m_pImpl->getURL() )
964 if ( m_pImpl->m_bDocumentReadOnly )
965 throw IOException();
967 impl_storeAs_throw( m_pImpl->getURL(), m_pImpl->getMediaDescriptor(), SAVE, aGuard );
968 return;
971 // if we have no URL, but did survive the DocumentGuard above, then we've been inited via XLoadable::initNew,
972 // i.e. we're based on a temporary storage
973 OSL_ENSURE( m_pImpl->getDocFileLocation().isEmpty(), "ODatabaseDocument::store: unexpected URL inconsistency!" );
977 impl_storeToStorage_throw( m_pImpl->getRootStorage(), m_pImpl->getMediaDescriptor().getPropertyValues(), aGuard );
979 catch( const Exception& )
981 Any aError = ::cppu::getCaughtException();
982 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
983 || aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
986 // allowed to leave
987 throw;
989 impl_throwIOExceptionCausedBySave_throw( aError, {} );
993 void ODatabaseDocument::impl_throwIOExceptionCausedBySave_throw( const Any& i_rError, std::u16string_view i_rTargetURL ) const
995 OUString sErrorMessage = extractExceptionMessage( m_pImpl->m_aContext, i_rError );
996 sErrorMessage = ResourceManager::loadString(
997 RID_STR_ERROR_WHILE_SAVING,
998 u"$location$", i_rTargetURL,
999 u"$message$", sErrorMessage
1001 throw IOException( sErrorMessage, *const_cast< ODatabaseDocument* >( this ) );
1004 void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comphelper::NamedValueCollection& _rArguments,
1005 const StoreType _eType, DocumentGuard& _rGuard )
1007 OSL_PRECOND( ( _eType == SAVE ) || ( _eType == SAVE_AS ),
1008 "ODatabaseDocument::impl_storeAs_throw: you introduced a new type which cannot be handled here!" );
1010 // if we're in the process of initializing the document (which effectively means it is an implicit
1011 // initialization triggered in storeAsURL), the we do not notify events, since to an observer, the SaveAs
1012 // should not be noticeable
1013 bool bIsInitializationProcess = impl_isInitializing();
1015 if ( !bIsInitializationProcess )
1017 _rGuard.clear();
1018 m_aEventNotifier.notifyDocumentEvent( _eType == SAVE ? u"OnSave"_ustr : u"OnSaveAs"_ustr, nullptr, Any( _rURL ) );
1019 _rGuard.reset();
1022 Reference< XStorage > xNewRootStorage;
1023 // will be non-NULL if our storage changed
1027 ModifyLock aLock( *this );
1028 // ignore all changes of our "modified" state during storing
1030 bool bLocationChanged = ( _rURL != m_pImpl->getDocFileLocation() );
1031 if ( bLocationChanged )
1033 // create storage for target URL
1034 uno::Reference<embed::XStorage> xTargetStorage(
1035 impl_GetStorageOrCreateFor_throw(_rArguments, _rURL));
1037 if ( m_pImpl->isEmbeddedDatabase() )
1038 m_pImpl->clearConnections();
1040 // commit everything
1041 m_pImpl->commitEmbeddedStorage();
1042 m_pImpl->commitStorages();
1044 // copy own storage to target storage
1045 Reference< XStorage > xCurrentStorage( m_pImpl->getRootStorage() );
1046 if ( xCurrentStorage.is() )
1047 xCurrentStorage->copyToStorage( xTargetStorage );
1049 m_pImpl->disposeStorages();
1051 // each and every document definition obtained via m_xForms and m_xReports depends
1052 // on the sub storages which we just disposed. So, dispose the forms/reports collections, too.
1053 // This ensures that they're re-created when needed.
1054 clearObjectContainer( m_xForms );
1055 clearObjectContainer( m_xReports );
1057 xNewRootStorage = m_pImpl->switchToStorage( xTargetStorage );
1059 m_pImpl->m_bDocumentReadOnly = false;
1062 // store to current storage
1063 Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_SET_THROW );
1064 Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) );
1065 impl_storeToStorage_throw( xCurrentStorage, aMediaDescriptor, _rGuard );
1067 // success - tell our impl
1068 m_pImpl->setDocFileLocation( _rURL );
1069 m_pImpl->setResource( _rURL, aMediaDescriptor );
1071 // if we are in an initialization process, then this is finished, now that we stored the document
1072 if ( bIsInitializationProcess )
1073 impl_setInitialized();
1075 catch( const IOException& )
1077 if ( !bIsInitializationProcess )
1078 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? u"OnSaveFailed"_ustr : u"OnSaveAsFailed"_ustr, nullptr, Any( _rURL ) );
1079 throw;
1081 catch( const RuntimeException& )
1083 if ( !bIsInitializationProcess )
1084 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? u"OnSaveFailed"_ustr : u"OnSaveAsFailed"_ustr, nullptr, Any( _rURL ) );
1085 throw;
1087 catch( const Exception& )
1089 Any aError = ::cppu::getCaughtException();
1091 // notify the failure
1092 if ( !bIsInitializationProcess )
1093 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? u"OnSaveFailed"_ustr : u"OnSaveAsFailed"_ustr, nullptr, Any( _rURL ) );
1095 impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1098 // notify the document event
1099 if ( !bIsInitializationProcess )
1100 m_aEventNotifier.notifyDocumentEventAsync( _eType == SAVE ? u"OnSaveDone"_ustr : u"OnSaveAsDone"_ustr, nullptr, Any( _rURL ) );
1102 // reset our "modified" flag, and clear the guard
1103 impl_setModified_nothrow( false, _rGuard );
1104 // <- SYNCHRONIZED
1106 // notify storage listeners
1107 if ( xNewRootStorage.is() )
1108 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1111 Reference< XStorage > ODatabaseDocument::impl_createStorageFor_throw( const OUString& _rURL ) const
1113 Reference< ucb::XSimpleFileAccess3 > xTempAccess(ucb::SimpleFileAccess::create(m_pImpl->m_aContext));
1114 Reference< io::XStream > xStream = xTempAccess->openFileReadWrite( _rURL );
1115 Reference< io::XTruncate > xTruncate(xStream,UNO_QUERY);
1116 if ( xTruncate.is() )
1118 xTruncate->truncate();
1120 Sequence<Any> aParam{ Any(xStream), Any(ElementModes::READWRITE | ElementModes::TRUNCATE) };
1122 Reference< XSingleServiceFactory > xStorageFactory( m_pImpl->createStorageFactory(), UNO_SET_THROW );
1123 return Reference< XStorage >( xStorageFactory->createInstanceWithArguments( aParam ), UNO_QUERY_THROW );
1126 css::uno::Reference<css::embed::XStorage> ODatabaseDocument::impl_GetStorageOrCreateFor_throw(
1127 const ::comphelper::NamedValueCollection& _rArguments, const OUString& _rURL) const
1129 // Try to get the storage from arguments, then create storage for target URL
1130 uno::Reference<embed::XStorage> xTargetStorage;
1131 _rArguments.get(u"TargetStorage"_ustr) >>= xTargetStorage;
1132 if (!xTargetStorage.is())
1133 xTargetStorage = impl_createStorageFor_throw(_rURL);
1135 // In case we got a StreamRelPath, then xTargetStorage should reference that sub-storage.
1136 OUString sStreamRelPath = _rArguments.getOrDefault(u"StreamRelPath"_ustr, OUString());
1137 if (!sStreamRelPath.isEmpty())
1138 xTargetStorage
1139 = xTargetStorage->openStorageElement(sStreamRelPath, embed::ElementModes::READWRITE);
1141 return xTargetStorage;
1144 void SAL_CALL ODatabaseDocument::storeAsURL( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments )
1146 // SYNCHRONIZED ->
1147 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1149 // Normally, a document initialization is done via XLoadable::load or XLoadable::initNew. For convenience
1150 // reasons, and to not break existing API clients, it's allowed to call storeAsURL without having initialized
1151 // the document, in which case the initialization will be done implicitly.
1152 bool bImplicitInitialization = !impl_isInitialized();
1153 // implicit initialization while another initialization is just running is not possible
1154 if ( bImplicitInitialization && impl_isInitializing() )
1155 throw RuntimeException();
1157 if ( bImplicitInitialization )
1158 impl_setInitializing();
1162 impl_storeAs_throw( _rURL, _rArguments, SAVE_AS, aGuard );
1163 // <- SYNCHRONIZED
1165 // impl_storeAs_throw cleared the lock on our mutex, but the below lines need this lock
1166 // SYNCHRONIZED ->
1167 aGuard.reset();
1169 // our title might have changed, potentially at least
1170 // Sadly, we cannot check this: Calling getTitle here and now would not deliver
1171 // an up-to-date result, as the call is delegated to our TitleHelper instance, which itself
1172 // updates its title only if it gets the OnSaveAsDone event (which was sent asynchronously
1173 // by impl_storeAs_throw). So, we simply notify always, and also asynchronously
1174 m_aEventNotifier.notifyDocumentEventAsync( u"OnTitleChanged"_ustr, nullptr, Any() );
1176 catch( const Exception& )
1178 impl_reset_nothrow();
1179 throw;
1182 if ( bImplicitInitialization )
1183 m_bAllowDocumentScripting = true;
1185 aGuard.clear();
1186 // <- SYNCHRONIZED
1188 if ( bImplicitInitialization )
1189 m_aEventNotifier.notifyDocumentEvent( u"OnCreate"_ustr, nullptr, Any() );
1192 void ODatabaseDocument::impl_storeToStorage_throw( const Reference< XStorage >& _rxTargetStorage, const Sequence< PropertyValue >& _rMediaDescriptor,
1193 DocumentGuard& _rDocGuard ) const
1195 if ( !_rxTargetStorage.is() )
1196 throw IllegalArgumentException( OUString(), *const_cast< ODatabaseDocument* >( this ), 1 );
1198 if ( !m_pImpl.is() )
1199 throw DisposedException( OUString(), *const_cast< ODatabaseDocument* >( this ) );
1203 // commit everything
1204 m_pImpl->commitEmbeddedStorage();
1205 m_pImpl->commitStorages();
1207 // copy own storage to target storage
1208 if ( impl_isInitialized() )
1210 Reference< XStorage > xCurrentStorage = m_pImpl->getOrCreateRootStorage();
1211 // Root storage may be empty in case of embedding.
1212 if ( xCurrentStorage.is() && xCurrentStorage != _rxTargetStorage )
1213 xCurrentStorage->copyToStorage( _rxTargetStorage );
1216 // write into target storage
1217 ::comphelper::NamedValueCollection aWriteArgs( _rMediaDescriptor );
1218 lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, true );
1219 impl_writeStorage_throw( _rxTargetStorage, aWriteArgs );
1220 lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, false );
1222 // commit target storage
1223 m_pImpl->commitStorageIfWriteable_ignoreErrors(_rxTargetStorage);
1225 catch( const IOException& ) { throw; }
1226 catch( const RuntimeException& ) { throw; }
1227 catch ( const Exception& e )
1229 throw IOException( e.Message, *const_cast< ODatabaseDocument* >( this ) );
1233 void SAL_CALL ODatabaseDocument::storeToURL( const OUString& _rURL, const Sequence< PropertyValue >& _rArguments )
1235 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1236 ModifyLock aLock( *this );
1239 aGuard.clear();
1240 m_aEventNotifier.notifyDocumentEvent( u"OnSaveTo"_ustr, nullptr, Any( _rURL ) );
1241 aGuard.reset();
1246 const ::comphelper::NamedValueCollection aArguments(_rArguments);
1247 // create storage for target URL
1248 Reference<XStorage> xTargetStorage(impl_GetStorageOrCreateFor_throw(aArguments, _rURL));
1250 // extend media descriptor with URL
1251 Sequence<PropertyValue> aMediaDescriptor(lcl_appendFileNameToDescriptor(aArguments, _rURL));
1253 // store to this storage
1254 impl_storeToStorage_throw( xTargetStorage, aMediaDescriptor, aGuard );
1256 catch( const Exception& )
1258 Any aError = ::cppu::getCaughtException();
1259 m_aEventNotifier.notifyDocumentEventAsync( u"OnSaveToFailed"_ustr, nullptr, aError );
1261 if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() )
1262 || aError.isExtractableTo( ::cppu::UnoType< RuntimeException >::get() )
1265 // allowed to leave
1266 throw;
1269 impl_throwIOExceptionCausedBySave_throw( aError, _rURL );
1272 m_aEventNotifier.notifyDocumentEventAsync( u"OnSaveToDone"_ustr, nullptr, Any( _rURL ) );
1275 // XModifyBroadcaster
1276 void SAL_CALL ODatabaseDocument::addModifyListener( const Reference< XModifyListener >& _xListener )
1278 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1279 m_aModifyListeners.addInterface(_xListener);
1282 void SAL_CALL ODatabaseDocument::removeModifyListener( const Reference< XModifyListener >& _xListener )
1284 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1285 m_aModifyListeners.removeInterface(_xListener);
1288 // XModifiable
1289 sal_Bool SAL_CALL ODatabaseDocument::isModified( )
1291 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1293 return m_pImpl->m_bModified;
1296 void SAL_CALL ODatabaseDocument::setModified( sal_Bool _bModified )
1298 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1299 if ( impl_isInitialized() )
1300 impl_setModified_nothrow( _bModified, aGuard );
1301 // it's allowed to call setModified without the document being initialized already. In this case,
1302 // we simply ignore the call - when the initialization is finished, the respective code will set
1303 // a proper "modified" flag
1306 void ODatabaseDocument::impl_setModified_nothrow( bool _bModified, DocumentGuard& _rGuard )
1308 // SYNCHRONIZED ->
1309 bool bModifiedChanged = ( m_pImpl->m_bModified != _bModified ) && ( !m_pImpl->isModifyLocked() );
1311 if ( bModifiedChanged )
1313 m_pImpl->m_bModified = _bModified;
1314 m_aEventNotifier.notifyDocumentEventAsync( u"OnModifyChanged"_ustr, nullptr, Any() );
1316 _rGuard.clear();
1317 // <- SYNCHRONIZED
1319 if ( bModifiedChanged )
1321 lang::EventObject aEvent( *this );
1322 m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
1326 // css::document::XEventBroadcaster
1327 void SAL_CALL ODatabaseDocument::addEventListener(const uno::Reference< document::XEventListener >& Listener )
1329 m_aEventNotifier.addLegacyEventListener( Listener );
1332 void SAL_CALL ODatabaseDocument::removeEventListener( const uno::Reference< document::XEventListener >& Listener )
1334 m_aEventNotifier.removeLegacyEventListener( Listener );
1337 void SAL_CALL ODatabaseDocument::addDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
1339 m_aEventNotifier.addDocumentEventListener( Listener );
1342 void SAL_CALL ODatabaseDocument::removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
1344 m_aEventNotifier.removeDocumentEventListener( Listener );
1347 void SAL_CALL ODatabaseDocument::notifyDocumentEvent( const OUString& EventName, const Reference< XController2 >& ViewController, const Any& Supplement )
1349 if ( EventName.isEmpty() )
1350 throw IllegalArgumentException( OUString(), *this, 1 );
1352 // SYNCHRONIZED ->
1353 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1355 if ( !DocumentEvents::needsSynchronousNotification( EventName ) )
1357 m_aEventNotifier.notifyDocumentEventAsync( EventName, ViewController, Supplement );
1358 return;
1360 aGuard.clear();
1361 // <- SYNCHRONIZED
1363 m_aEventNotifier.notifyDocumentEvent( EventName, ViewController, Supplement );
1366 Sequence< PropertyValue > SAL_CALL ODatabaseDocument::getPrinter( )
1368 OSL_FAIL( "ODatabaseDocument::getPrinter: not supported!" );
1369 return Sequence< PropertyValue >();
1372 void SAL_CALL ODatabaseDocument::setPrinter( const Sequence< PropertyValue >& /*aPrinter*/ )
1374 OSL_FAIL( "ODatabaseDocument::setPrinter: not supported!" );
1377 void SAL_CALL ODatabaseDocument::print( const Sequence< PropertyValue >& /*xOptions*/ )
1379 OSL_FAIL( "ODatabaseDocument::print: not supported!" );
1382 void ODatabaseDocument::impl_reparent_nothrow( const WeakReference< XNameAccess >& _rxContainer )
1384 Reference< XChild > xChild( _rxContainer.get(), UNO_QUERY );
1385 if ( xChild.is() )
1386 xChild->setParent( *this );
1389 void ODatabaseDocument::clearObjectContainer( WeakReference< XNameAccess >& _rxContainer)
1391 Reference< XNameAccess > xContainer = _rxContainer;
1392 ::comphelper::disposeComponent( xContainer );
1394 Reference< XChild > xChild( _rxContainer.get(),UNO_QUERY );
1395 if ( xChild.is() )
1396 xChild->setParent( nullptr );
1397 _rxContainer.clear();
1400 void ODatabaseDocument::clearObjectContainer( unotools::WeakReference< OCommandContainer >& _rxContainer)
1402 rtl::Reference< OCommandContainer > xContainer = _rxContainer;
1403 if ( xContainer.is() )
1405 xContainer->dispose();
1406 xContainer->setParent( nullptr );
1408 _rxContainer.clear();
1411 Reference< XNameAccess > ODatabaseDocument::impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType _eType )
1413 if ( ( _eType != ODatabaseModelImpl::ObjectType::Form ) && ( _eType != ODatabaseModelImpl::ObjectType::Report ) )
1414 throw IllegalArgumentException();
1416 bool bFormsContainer = _eType == ODatabaseModelImpl::ObjectType::Form;
1418 WeakReference< XNameAccess >& rContainerRef( bFormsContainer ? m_xForms : m_xReports );
1419 Reference< XNameAccess > xContainer = rContainerRef;
1420 if ( !xContainer.is() )
1422 Any aValue;
1423 css::uno::Reference< css::uno::XInterface > xMy(*this);
1424 if ( dbtools::getDataSourceSetting(xMy,bFormsContainer ? "Forms" : "Reports",aValue) )
1426 OUString sSupportService;
1427 aValue >>= sSupportService;
1428 if ( !sSupportService.isEmpty() )
1430 Sequence<Any> aArgs{ Any(NamedValue(u"DatabaseDocument"_ustr,Any(xMy))) };
1431 xContainer.set(
1432 m_pImpl->m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(sSupportService, aArgs, m_pImpl->m_aContext),
1433 UNO_QUERY);
1434 rContainerRef = xContainer;
1437 if ( !xContainer.is() )
1439 TContentPtr& rContainerData( m_pImpl->getObjectContainer( _eType ) );
1440 rContainerRef = xContainer = new ODocumentContainer( m_pImpl->m_aContext, *this, rContainerData, bFormsContainer );
1442 impl_reparent_nothrow( xContainer );
1444 return xContainer;
1447 void ODatabaseDocument::impl_closeControllerFrames_nolck_throw( bool _bDeliverOwnership )
1449 Controllers aCopy = m_aControllers;
1451 for (auto const& elem : aCopy)
1453 if ( !elem.is() )
1454 continue;
1458 Reference< XCloseable> xFrame( elem->getFrame(), UNO_QUERY );
1459 if ( xFrame.is() )
1460 xFrame->close( _bDeliverOwnership );
1462 catch( const CloseVetoException& ) { throw; }
1463 catch( const Exception& )
1465 DBG_UNHANDLED_EXCEPTION("dbaccess");
1470 void ODatabaseDocument::impl_disposeControllerFrames_nothrow()
1472 Controllers aCopy;
1473 aCopy.swap( m_aControllers ); // ensure m_aControllers is empty afterwards
1474 for( const auto& rController : aCopy )
1478 if( rController.is() )
1480 Reference< XFrame > xFrame( rController->getFrame() );
1481 ::comphelper::disposeComponent( xFrame );
1484 catch( const Exception& )
1486 DBG_UNHANDLED_EXCEPTION("dbaccess");
1491 void SAL_CALL ODatabaseDocument::close(sal_Bool bDeliverOwnership)
1493 // nearly everything below can/must be done without our mutex locked, the below is just for
1494 // the checks for being disposed and the like
1495 // SYNCHRONIZED ->
1497 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1498 assert (!m_bClosing);
1499 m_bClosing = true;
1501 // <- SYNCHRONIZED
1505 // allow listeners to veto
1506 lang::EventObject aEvent( *this );
1507 m_aCloseListener.forEach(
1508 [&aEvent, &bDeliverOwnership] (uno::Reference<XCloseListener> const& xListener) {
1509 return xListener->queryClosing(aEvent, bDeliverOwnership);
1512 // notify that we're going to unload
1513 m_aEventNotifier.notifyDocumentEvent( u"OnPrepareUnload"_ustr, nullptr, Any() );
1515 impl_closeControllerFrames_nolck_throw( bDeliverOwnership );
1517 m_aCloseListener.notifyEach( &XCloseListener::notifyClosing, const_cast<const lang::EventObject&>(aEvent) );
1519 dispose();
1521 catch ( const Exception& )
1523 SolarMutexGuard g;
1524 m_bClosing = false;
1525 throw;
1528 // SYNCHRONIZED ->
1529 SolarMutexGuard g;
1530 m_bClosing = false;
1531 // <- SYNCHRONIZED
1534 void SAL_CALL ODatabaseDocument::addCloseListener( const Reference< css::util::XCloseListener >& Listener )
1536 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1537 m_aCloseListener.addInterface(Listener);
1540 void SAL_CALL ODatabaseDocument::removeCloseListener( const Reference< css::util::XCloseListener >& Listener )
1542 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1543 m_aCloseListener.removeInterface(Listener);
1546 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getFormDocuments( )
1548 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1549 return impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType::Form );
1552 Reference< XNameAccess > SAL_CALL ODatabaseDocument::getReportDocuments( )
1554 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1555 return impl_getDocumentContainer_throw( ODatabaseModelImpl::ObjectType::Report );
1558 void ODatabaseDocument::WriteThroughComponent( const Reference< XComponent >& xComponent, const OUString& sStreamName,
1559 const OUString & rServiceName, const Sequence< Any >& _rArguments, const Sequence< PropertyValue >& rMediaDesc,
1560 const Reference<XStorage>& _xStorageToSaveTo ) const
1562 // open stream
1563 Reference< XStream > xStream = _xStorageToSaveTo->openStreamElement( sStreamName, ElementModes::READWRITE | ElementModes::TRUNCATE );
1564 if ( !xStream.is() )
1565 return;
1567 Reference< XOutputStream > xOutputStream( xStream->getOutputStream() );
1568 OSL_ENSURE( xOutputStream.is(), "Can't create output stream in package!" );
1569 if ( !xOutputStream.is() )
1570 return;
1572 Reference< XSeekable > xSeek( xOutputStream, UNO_QUERY );
1573 if ( xSeek.is() )
1574 xSeek->seek(0);
1576 Reference< XPropertySet > xStreamProp( xOutputStream, UNO_QUERY_THROW );
1577 xStreamProp->setPropertyValue( INFO_MEDIATYPE, Any( u"text/xml"_ustr ) );
1578 xStreamProp->setPropertyValue( u"Compressed"_ustr, Any( true ) );
1580 // write the stuff
1581 WriteThroughComponent( xOutputStream, xComponent, rServiceName, _rArguments, rMediaDesc );
1584 void ODatabaseDocument::WriteThroughComponent( const Reference< XOutputStream >& xOutputStream,
1585 const Reference< XComponent >& xComponent, const OUString& rServiceName, const Sequence< Any >& _rArguments,
1586 const Sequence< PropertyValue >& rMediaDesc ) const
1588 OSL_ENSURE( xOutputStream.is(), "I really need an output stream!" );
1589 OSL_ENSURE( xComponent.is(), "Need component!" );
1591 // get component
1592 Reference< XWriter > xSaxWriter = xml::sax::Writer::create( m_pImpl->m_aContext );
1594 // connect XML writer to output stream
1595 xSaxWriter->setOutputStream( xOutputStream );
1597 // prepare arguments (prepend doc handler to given arguments)
1598 Sequence<Any> aArgs( 1 + _rArguments.getLength() );
1599 auto pArgs = aArgs.getArray();
1600 pArgs[0] <<= xSaxWriter;
1601 for ( sal_Int32 i = 0; i < _rArguments.getLength(); ++i )
1602 pArgs[ i+1 ] = _rArguments[i];
1604 // get filter component
1605 Reference< XExporter > xExporter( m_pImpl->m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(rServiceName, aArgs, m_pImpl->m_aContext), UNO_QUERY_THROW );
1607 // connect model and filter
1608 xExporter->setSourceDocument( xComponent );
1610 // filter
1611 Reference< XFilter > xFilter( xExporter, UNO_QUERY_THROW );
1612 xFilter->filter( rMediaDesc );
1615 void ODatabaseDocument::impl_writeStorage_throw( const Reference< XStorage >& _rxTargetStorage, const ::comphelper::NamedValueCollection& _rMediaDescriptor ) const
1617 // extract status indicator
1618 Sequence< Any > aDelegatorArguments;
1619 lcl_extractStatusIndicator( _rMediaDescriptor, aDelegatorArguments );
1621 uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
1623 xInfoSet->setPropertyValue(u"UsePrettyPrinting"_ustr, uno::Any(officecfg::Office::Common::Save::Document::PrettyPrinting::get()));
1624 if ( officecfg::Office::Common::Save::URL::FileSystem::get() )
1626 OUString sBaseURI = _rMediaDescriptor.getOrDefault(u"BaseURI"_ustr, OUString());
1627 if (sBaseURI.isEmpty())
1628 sBaseURI = _rMediaDescriptor.getOrDefault(u"URL"_ustr,OUString());
1629 xInfoSet->setPropertyValue(u"BaseURI"_ustr, uno::Any(sBaseURI));
1632 // Set TargetStorage, so it doesn't have to be re-constructed based on possibly empty URL.
1633 xInfoSet->setPropertyValue(u"TargetStorage"_ustr, uno::Any(m_pImpl->getRootStorage()));
1635 // Set StreamRelPath, in case this document is an embedded one.
1636 OUString sStreamRelPath;
1637 OUString sURL = _rMediaDescriptor.getOrDefault(u"URL"_ustr, OUString());
1638 if (sURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:"))
1640 // In this case the host contains the real path, and the path is the embedded stream name.
1641 INetURLObject aURL(sURL);
1642 sStreamRelPath = aURL.GetURLPath(INetURLObject::DecodeMechanism::WithCharset);
1643 if (sStreamRelPath.startsWith("/"))
1644 sStreamRelPath = sStreamRelPath.copy(1);
1646 if (!sStreamRelPath.isEmpty())
1647 xInfoSet->setPropertyValue(u"StreamRelPath"_ustr, uno::Any(sStreamRelPath));
1649 sal_Int32 nArgsLen = aDelegatorArguments.getLength();
1650 aDelegatorArguments.realloc(nArgsLen+1);
1651 aDelegatorArguments.getArray()[nArgsLen++] <<= xInfoSet;
1653 Reference< XPropertySet > xProp( _rxTargetStorage, UNO_QUERY_THROW );
1654 xProp->setPropertyValue( INFO_MEDIATYPE, Any( MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII ) );
1656 OUString aVersion;
1657 SvtSaveOptions::ODFSaneDefaultVersion const nDefVersion =
1658 GetODFSaneDefaultVersion();
1659 // older versions can not have this property set,
1660 // it exists only starting from ODF1.2
1661 if (nDefVersion >= SvtSaveOptions::ODFSVER_014)
1663 aVersion = ODFVER_014_TEXT;
1665 else if (nDefVersion >= SvtSaveOptions::ODFSVER_013)
1667 aVersion = ODFVER_013_TEXT;
1669 else if (nDefVersion >= SvtSaveOptions::ODFSVER_012)
1671 aVersion = ODFVER_012_TEXT;
1674 if (!aVersion.isEmpty())
1678 xProp->setPropertyValue(u"Version"_ustr , uno::Any(aVersion));
1680 catch (const uno::Exception&)
1682 TOOLS_WARN_EXCEPTION("dbaccess", "exception setting Version");
1686 Reference< XComponent > xComponent( *const_cast< ODatabaseDocument* >( this ), UNO_QUERY_THROW );
1688 Sequence< PropertyValue > aMediaDescriptor;
1689 _rMediaDescriptor >>= aMediaDescriptor;
1691 xInfoSet->setPropertyValue(u"StreamName"_ustr, uno::Any(u"settings.xml"_ustr));
1692 WriteThroughComponent( xComponent, u"settings.xml"_ustr, u"com.sun.star.comp.sdb.XMLSettingsExporter"_ustr,
1693 aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1695 xInfoSet->setPropertyValue(u"StreamName"_ustr, uno::Any(u"content.xml"_ustr));
1696 WriteThroughComponent( xComponent, u"content.xml"_ustr, u"com.sun.star.comp.sdb.DBExportFilter"_ustr,
1697 aDelegatorArguments, aMediaDescriptor, _rxTargetStorage );
1699 if ( _rxTargetStorage->hasByName ( sPictures ) )
1703 // Delete any previously existing Pictures folder and regenerate
1704 // any needed content if needed
1705 Reference< XStorageBasedLibraryContainer > xDlgs = m_pImpl->getLibraryContainer( false );
1706 if ( xDlgs.is() )
1708 Reference< XModel > xModel(const_cast< ODatabaseDocument*>(this));
1709 lcl_uglyHackToStoreDialogeEmbedImages( m_pImpl->getLibraryContainer(false), _rxTargetStorage, xModel, m_pImpl->m_aContext );
1712 catch ( const Exception& )
1714 DBG_UNHANDLED_EXCEPTION("dbaccess");
1717 m_pImpl->storeLibraryContainersTo( _rxTargetStorage );
1720 Reference< XUIConfigurationManager > SAL_CALL ODatabaseDocument::getUIConfigurationManager( )
1722 return Reference< XUIConfigurationManager >( getUIConfigurationManager2(), UNO_QUERY_THROW );
1725 Reference< XUIConfigurationManager2 > const & ODatabaseDocument::getUIConfigurationManager2( )
1727 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1729 if ( !m_xUIConfigurationManager.is() )
1731 m_xUIConfigurationManager = UIConfigurationManager::create( m_pImpl->m_aContext );
1733 OUString aUIConfigFolderName( u"Configurations2"_ustr );
1735 // First try to open with READWRITE and then READ
1736 Reference< XStorage > xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READWRITE );
1737 if ( xConfigStorage.is() )
1739 OUString aMediaType;
1740 Reference< XPropertySet > xPropSet( xConfigStorage, UNO_QUERY );
1741 Any a = xPropSet->getPropertyValue( INFO_MEDIATYPE );
1742 if ( !( a >>= aMediaType ) || aMediaType.isEmpty() )
1744 a <<= u"application/vnd.sun.xml.ui.configuration"_ustr;
1745 xPropSet->setPropertyValue( INFO_MEDIATYPE, a );
1748 else
1749 xConfigStorage = getDocumentSubStorage( aUIConfigFolderName, ElementModes::READ );
1751 // initialize ui configuration manager with document substorage
1752 m_xUIConfigurationManager->setStorage( xConfigStorage );
1755 return m_xUIConfigurationManager;
1758 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentSubStorage( const OUString& aStorageName, sal_Int32 nMode )
1760 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1762 Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1763 return xStorageAccess->getDocumentSubStorage( aStorageName, nMode );
1766 Sequence< OUString > SAL_CALL ODatabaseDocument::getDocumentSubStoragesNames( )
1768 Reference< XDocumentSubStorageSupplier > xStorageAccess( m_pImpl->getDocumentSubStorageSupplier() );
1769 return xStorageAccess->getDocumentSubStoragesNames();
1772 void ODatabaseDocument::impl_notifyStorageChange_nolck_nothrow( const Reference< XStorage >& xNewRootStorage )
1774 Reference< XInterface > xMe( *this );
1776 m_aStorageListeners.forEach(
1777 [&xMe, &xNewRootStorage] (uno::Reference<XStorageChangeListener> const& xListener) {
1778 return xListener->notifyStorageChange(xMe, xNewRootStorage);
1782 void ODatabaseDocument::disposing()
1784 if ( !m_pImpl.is() )
1786 // this means that we're already disposed
1787 OSL_ENSURE( ODatabaseDocument_OfficeDocument::rBHelper.bDisposed, "ODatabaseDocument::disposing: no impl anymore, but not yet disposed!" );
1788 return;
1791 if ( impl_isInitialized() )
1792 m_aEventNotifier.notifyDocumentEvent( u"OnUnload"_ustr, nullptr, Any() );
1794 Reference< XModel > xHoldAlive( this );
1796 m_aEventNotifier.disposing();
1798 lang::EventObject aDisposeEvent(static_cast<XWeak*>(this));
1799 m_aModifyListeners.disposeAndClear( aDisposeEvent );
1800 m_aCloseListener.disposeAndClear( aDisposeEvent );
1801 m_aStorageListeners.disposeAndClear( aDisposeEvent );
1803 // this is the list of objects which we currently hold as member. Upon resetting
1804 // those members, we can (potentially) release the last reference to them, in which
1805 // case they will be deleted - if they're C++ implementations, that is :).
1806 // Some of those implementations are offending enough to require the SolarMutex, which
1807 // means we should not release the last reference while our own mutex is locked ...
1808 std::vector< Reference< XInterface > > aKeepAlive;
1810 // SYNCHRONIZED ->
1812 SolarMutexGuard aGuard;
1814 OSL_ENSURE(m_aControllers.empty(),
1815 "ODatabaseDocument::disposing: there still are controllers!");
1816 // normally, nobody should explicitly dispose, but only XCloseable::close
1817 // the document. And upon closing, our controllers are closed, too
1820 uno::Reference<uno::XInterface> xUIInterface(m_xUIConfigurationManager);
1821 aKeepAlive.push_back(xUIInterface);
1823 m_xUIConfigurationManager = nullptr;
1825 clearObjectContainer(m_xForms);
1826 clearObjectContainer(m_xReports);
1828 // reset the macro mode: in case the our impl struct stays alive (e.g. because our DataSource
1829 // object still exists), and somebody subsequently re-opens the document, we want to have
1830 // the security warning, again.
1831 m_pImpl->resetMacroExecutionMode();
1833 // similar arguing for our ViewMonitor
1834 m_aViewMonitor.reset();
1836 // tell our Impl to forget us
1837 m_pImpl->modelIsDisposing(impl_isInitialized(), ODatabaseModelImpl::ResetModelAccess());
1839 // now, at the latest, the controller array should be empty. Controllers are
1840 // expected to listen for our disposal, and disconnect then
1841 OSL_ENSURE(m_aControllers.empty(),
1842 "ODatabaseDocument::disposing: there still are controllers!");
1843 impl_disposeControllerFrames_nothrow();
1846 uno::Reference<uno::XInterface> xModuleInterface(m_xModuleManager);
1847 aKeepAlive.push_back(xModuleInterface);
1849 m_xModuleManager.clear();
1852 uno::Reference<uno::XInterface> xTitleInterface(cppu::getXWeak(m_xTitleHelper.get()));
1853 aKeepAlive.push_back(xTitleInterface);
1855 m_xTitleHelper.clear();
1857 m_pImpl.clear();
1859 // <- SYNCHRONIZED
1861 aKeepAlive.clear();
1864 // XComponent
1865 void SAL_CALL ODatabaseDocument::dispose( )
1867 ::cppu::WeakComponentImplHelperBase::dispose();
1868 m_xTitleHelper.clear();
1869 m_xModuleManager.clear();
1870 m_pEventExecutor.clear();
1871 m_xCurrentController.clear();
1872 m_xUIConfigurationManager.clear();
1875 void SAL_CALL ODatabaseDocument::addEventListener( const Reference< lang::XEventListener >& _xListener )
1877 ::cppu::WeakComponentImplHelperBase::addEventListener( _xListener );
1880 void SAL_CALL ODatabaseDocument::removeEventListener( const Reference< lang::XEventListener >& _xListener )
1882 ::cppu::WeakComponentImplHelperBase::removeEventListener( _xListener );
1885 // XServiceInfo
1886 OUString ODatabaseDocument::getImplementationName()
1888 return u"com.sun.star.comp.dba.ODatabaseDocument"_ustr;
1891 Sequence< OUString > ODatabaseDocument::getSupportedServiceNames()
1893 return { u"com.sun.star.sdb.OfficeDatabaseDocument"_ustr, u"com.sun.star.document.OfficeDocument"_ustr };
1896 sal_Bool ODatabaseDocument::supportsService( const OUString& _rServiceName )
1898 return cppu::supportsService(this, _rServiceName);
1901 Reference< XDataSource > SAL_CALL ODatabaseDocument::getDataSource()
1903 DocumentGuard aGuard( *this, DocumentGuard::MethodWithoutInit );
1904 return m_pImpl->getOrCreateDataSource();
1907 namespace
1909 /// Property map for embedded import info set.
1910 comphelper::PropertyMapEntry const aEmbeddedImportInfoMap[] =
1912 {u"StreamRelPath"_ustr, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1913 {u"StreamName"_ustr, 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1914 {u"SourceStorage"_ustr, 0, cppu::UnoType<embed::XStorage>::get(), beans::PropertyAttribute::MAYBEVOID, 0},
1918 void SAL_CALL ODatabaseDocument::loadFromStorage(const Reference<XStorage>& xStorage, const Sequence<PropertyValue>& rMediaDescriptor)
1920 DocumentGuard aGuard(*this, DocumentGuard::InitMethod);
1922 uno::Reference<beans::XPropertySet> xInfoSet(comphelper::GenericPropertySet_CreateInstance(new comphelper::PropertySetInfo(aEmbeddedImportInfoMap)));
1923 xInfoSet->setPropertyValue(u"StreamRelPath"_ustr, uno::Any(comphelper::NamedValueCollection::getOrDefault(rMediaDescriptor, u"HierarchicalDocumentName", OUString())));
1924 xInfoSet->setPropertyValue(u"StreamName"_ustr, uno::Any(u"content.xml"_ustr));
1925 xInfoSet->setPropertyValue(u"SourceStorage"_ustr, uno::Any(xStorage));
1927 uno::Sequence<uno::Any> aFilterCreationArgs{ Any(xInfoSet) };
1929 uno::Reference<document::XImporter> xImporter(m_pImpl->m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(u"com.sun.star.comp.sdb.DBFilter"_ustr, aFilterCreationArgs, m_pImpl->m_aContext), uno::UNO_QUERY_THROW);
1931 uno::Reference<lang::XComponent> xComponent(*this, uno::UNO_QUERY_THROW);
1932 xImporter->setTargetDocument(xComponent);
1934 uno::Reference<document::XFilter> xFilter(xImporter, uno::UNO_QUERY_THROW);
1935 uno::Sequence<beans::PropertyValue> aFilterArgs;
1936 xFilter->filter(aFilterArgs);
1938 // In case of embedding, XModel::attachResource is already called.
1939 if (m_bEmbedded)
1940 impl_setInitialized();
1942 impl_setModified_nothrow(false, aGuard);
1945 void SAL_CALL ODatabaseDocument::storeToStorage( const Reference< XStorage >& _rxStorage, const Sequence< PropertyValue >& _rMediaDescriptor )
1947 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1948 impl_storeToStorage_throw( _rxStorage, _rMediaDescriptor, aGuard );
1951 void SAL_CALL ODatabaseDocument::switchToStorage( const Reference< XStorage >& _rxNewRootStorage )
1953 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1955 Reference< XStorage > xNewRootStorage( m_pImpl->switchToStorage( _rxNewRootStorage ) );
1957 aGuard.clear();
1958 impl_notifyStorageChange_nolck_nothrow( xNewRootStorage );
1961 Reference< XStorage > SAL_CALL ODatabaseDocument::getDocumentStorage( )
1963 DocumentGuard aGuard(*this, DocumentGuard::MethodUsedDuringInit);
1964 return m_pImpl->getOrCreateRootStorage();
1967 void SAL_CALL ODatabaseDocument::addStorageChangeListener( const Reference< XStorageChangeListener >& Listener )
1969 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1970 m_aStorageListeners.addInterface( Listener );
1973 void SAL_CALL ODatabaseDocument::removeStorageChangeListener( const Reference< XStorageChangeListener >& Listener )
1975 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1976 m_aStorageListeners.removeInterface( Listener );
1979 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getBasicLibraries()
1981 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
1982 return m_pImpl->getLibraryContainer( true );
1985 Reference< XStorageBasedLibraryContainer > SAL_CALL ODatabaseDocument::getDialogLibraries()
1987 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1988 return m_pImpl->getLibraryContainer( false );
1991 sal_Bool SAL_CALL ODatabaseDocument::getAllowMacroExecution()
1993 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
1994 return m_pImpl->adjustMacroMode_AutoReject();
1997 Reference< XEmbeddedScripts > SAL_CALL ODatabaseDocument::getScriptContainer()
1999 return this;
2002 Reference< provider::XScriptProvider > SAL_CALL ODatabaseDocument::getScriptProvider( )
2004 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2006 Reference< XScriptProvider > xScriptProvider( m_xScriptProvider );
2007 if ( !xScriptProvider.is() )
2009 Reference < XScriptProviderFactory > xFactory =
2010 theMasterScriptProviderFactory::get( m_pImpl->m_aContext );
2012 Any aScriptProviderContext;
2013 if ( m_bAllowDocumentScripting )
2014 aScriptProviderContext <<= Reference< XModel >( this );
2016 xScriptProvider.set( xFactory->createScriptProvider( aScriptProviderContext ), UNO_SET_THROW );
2017 m_xScriptProvider = xScriptProvider;
2020 return xScriptProvider;
2023 Reference< XNameReplace > SAL_CALL ODatabaseDocument::getEvents( )
2025 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2026 return m_pEventContainer.get();
2029 Reference< XInterface > ODatabaseDocument::getThis() const
2031 return *const_cast< ODatabaseDocument* >( this );
2034 namespace {
2036 struct CreateAny
2038 Any operator() (const Reference<XController>& lhs) const
2040 return Any(lhs);
2046 // XModel2
2047 Reference< XEnumeration > SAL_CALL ODatabaseDocument::getControllers( )
2049 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2050 uno::Sequence< Any> aController( m_aControllers.size() );
2051 std::transform( m_aControllers.begin(), m_aControllers.end(), aController.getArray(), CreateAny() );
2052 return new ::comphelper::OAnyEnumeration(aController);
2055 Sequence< OUString > SAL_CALL ODatabaseDocument::getAvailableViewControllerNames( )
2057 Sequence< OUString > aNames { SERVICE_SDB_APPLICATIONCONTROLLER };
2058 return aNames;
2061 Reference< XController2 > SAL_CALL ODatabaseDocument::createDefaultViewController( const Reference< XFrame >& Frame )
2063 return createViewController( u"Default"_ustr, Sequence< PropertyValue >(), Frame);
2066 Reference< XController2 > SAL_CALL ODatabaseDocument::createViewController( const OUString& ViewName, const Sequence< PropertyValue >& Arguments, const Reference< XFrame >& Frame )
2068 if ( ViewName != "Default" && ViewName != "Preview" )
2069 throw IllegalArgumentException( OUString(), *this, 1 );
2070 if ( !Frame.is() )
2071 throw IllegalArgumentException( OUString(), *this, 3 );
2073 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2074 aGuard.clear();
2076 Reference< XController2 > xController(
2077 m_pImpl->m_aContext->getServiceManager()->createInstanceWithContext(u"org.openoffice.comp.dbu.OApplicationController"_ustr, m_pImpl->m_aContext),
2078 UNO_QUERY_THROW );
2080 ::comphelper::NamedValueCollection aInitArgs( Arguments );
2081 aInitArgs.put( u"Frame"_ustr, Frame );
2082 if ( ViewName == "Preview" )
2083 aInitArgs.put( u"Preview"_ustr, true );
2084 Reference< XInitialization > xInitController( xController, UNO_QUERY_THROW );
2085 xInitController->initialize( aInitArgs.getWrappedPropertyValues() );
2087 return xController;
2090 rtl::Reference< ::framework::TitleHelper > const & ODatabaseDocument::impl_getTitleHelper_throw()
2092 if ( ! m_xTitleHelper.is ())
2094 Reference< XUntitledNumbers > xDesktop(Desktop::create(m_pImpl->m_aContext), uno::UNO_QUERY_THROW);
2095 Reference< frame::XModel > xThis (getThis(), uno::UNO_QUERY_THROW);
2097 m_xTitleHelper = new ::framework::TitleHelper(m_pImpl->m_aContext, xThis, xDesktop);
2100 return m_xTitleHelper;
2103 uno::Reference< frame::XUntitledNumbers > ODatabaseDocument::impl_getUntitledHelper_throw(const uno::Reference< uno::XInterface >& _xComponent)
2105 if ( !m_xModuleManager.is() )
2106 m_xModuleManager.set( ModuleManager::create(m_pImpl->m_aContext) );
2108 OUString sModuleId;
2109 if (_xComponent.is())
2110 sModuleId = m_xModuleManager->identify(_xComponent);
2112 uno::Reference< frame::XUntitledNumbers > xNumberedControllers;
2114 TNumberedController::const_iterator aFind = m_aNumberedControllers.find(sModuleId);
2115 if ( aFind == m_aNumberedControllers.end() )
2117 rtl::Reference<::comphelper::NumberedCollection> pHelper = new ::comphelper::NumberedCollection();
2118 xNumberedControllers = pHelper;
2119 pHelper->setOwner(uno::Reference< frame::XModel >(this));
2121 m_aNumberedControllers.emplace( sModuleId,xNumberedControllers );
2123 else
2124 xNumberedControllers = aFind->second;
2126 return xNumberedControllers;
2129 // css.frame.XTitle
2130 OUString SAL_CALL ODatabaseDocument::getTitle()
2132 // SYNCHRONIZED ->
2133 DocumentGuard aGuard( *this, DocumentGuard::MethodUsedDuringInit );
2134 return impl_getTitleHelper_throw()->getTitle();
2137 // css.frame.XTitle
2138 void SAL_CALL ODatabaseDocument::setTitle( const OUString& sTitle )
2140 // SYNCHRONIZED ->
2141 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2142 impl_getTitleHelper_throw()->setTitle( sTitle );
2143 m_aEventNotifier.notifyDocumentEventAsync( u"OnTitleChanged"_ustr, nullptr, Any() );
2144 // <- SYNCHRONIZED
2147 // css.frame.XTitleChangeBroadcaster
2148 void SAL_CALL ODatabaseDocument::addTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2150 // SYNCHRONIZED ->
2151 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2153 rtl::Reference< ::framework::TitleHelper > xBroadcaster( impl_getTitleHelper_throw() );
2154 xBroadcaster->addTitleChangeListener( xListener );
2157 // css.frame.XTitleChangeBroadcaster
2158 void SAL_CALL ODatabaseDocument::removeTitleChangeListener( const uno::Reference< frame::XTitleChangeListener >& xListener )
2160 // SYNCHRONIZED ->
2161 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2163 rtl::Reference< ::framework::TitleHelper > xBroadcaster( impl_getTitleHelper_throw() );
2164 xBroadcaster->removeTitleChangeListener( xListener );
2167 // css.frame.XUntitledNumbers
2168 ::sal_Int32 SAL_CALL ODatabaseDocument::leaseNumber( const uno::Reference< uno::XInterface >& xComponent )
2170 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2171 return impl_getUntitledHelper_throw(xComponent)->leaseNumber (xComponent);
2174 // css.frame.XUntitledNumbers
2175 void SAL_CALL ODatabaseDocument::releaseNumber( ::sal_Int32 nNumber )
2177 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2178 impl_getUntitledHelper_throw()->releaseNumber (nNumber);
2181 // css.frame.XUntitledNumbers
2182 void SAL_CALL ODatabaseDocument::releaseNumberForComponent( const uno::Reference< uno::XInterface >& xComponent )
2184 DocumentGuard aGuard(*this, DocumentGuard::DefaultMethod);
2185 impl_getUntitledHelper_throw(xComponent)->releaseNumberForComponent (xComponent);
2188 // css.frame.XUntitledNumbers
2189 OUString SAL_CALL ODatabaseDocument::getUntitledPrefix()
2191 return OUString();
2194 } // namespace dbaccess
2196 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
2197 com_sun_star_comp_dba_ODatabaseDocument(css::uno::XComponentContext* context,
2198 css::uno::Sequence<css::uno::Any> const &)
2200 Reference<XInterface> xDBContextTunnel(DatabaseContext::create(context), UNO_QUERY_THROW);
2201 rtl::Reference<dbaccess::ODatabaseContext> pContext
2202 = dynamic_cast<dbaccess::ODatabaseContext*>(xDBContextTunnel.get());
2203 assert(pContext);
2205 rtl::Reference pImpl(
2206 new dbaccess::ODatabaseModelImpl(context, *pContext));
2207 rtl::Reference<dbaccess::ODatabaseDocument> inst(pImpl->createNewModel_deliverOwnership());
2208 inst->acquire();
2209 return cppu::getXWeak(inst.get());
2212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */