nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / core / dataaccess / ModelImpl.cxx
blobc24e2376e41a366802e53f346ae6e9c4f3466898
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 <databasecontext.hxx>
21 #include "databasedocument.hxx"
22 #include "datasource.hxx"
23 #include <definitioncontainer.hxx>
24 #include <ModelImpl.hxx>
25 #include <sdbcoretools.hxx>
27 #include <com/sun/star/beans/PropertyBag.hpp>
28 #include <com/sun/star/container/XSet.hpp>
29 #include <com/sun/star/document/MacroExecMode.hpp>
30 #include <com/sun/star/embed/XTransactedObject.hpp>
31 #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
32 #include <com/sun/star/embed/StorageFactory.hpp>
33 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
34 #include <com/sun/star/io/IOException.hpp>
35 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
36 #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
37 #include <com/sun/star/script/DocumentScriptLibraryContainer.hpp>
38 #include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
39 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
40 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
41 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
42 #include <com/sun/star/task/DocumentMacroConfirmationRequest.hpp>
44 #include <cppuhelper/exc_hlp.hxx>
45 #include <cppuhelper/implbase.hxx>
46 #include <comphelper/storagehelper.hxx>
47 #include <comphelper/types.hxx>
48 #include <comphelper/processfactory.hxx>
49 #include <sfx2/docfile.hxx>
50 #include <sfx2/signaturestate.hxx>
51 #include <tools/diagnose_ex.h>
52 #include <osl/file.hxx>
53 #include <osl/diagnose.h>
54 #include <sal/log.hxx>
55 #include <tools/urlobj.hxx>
56 #include <unotools/configmgr.hxx>
57 #include <unotools/tempfile.hxx>
58 #include <i18nlangtag/languagetag.hxx>
60 #include <algorithm>
62 using namespace css;
63 using namespace ::com::sun::star::document;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::sdbcx;
66 using namespace ::com::sun::star::sdb;
67 using namespace ::com::sun::star::beans;
68 using namespace ::com::sun::star::uno;
69 using namespace ::com::sun::star::lang;
70 using namespace ::com::sun::star::embed;
71 using namespace ::com::sun::star::container;
72 using namespace ::com::sun::star::util;
73 using namespace ::com::sun::star::io;
74 using namespace ::com::sun::star::ucb;
75 using namespace ::com::sun::star::frame;
76 using namespace ::com::sun::star::view;
77 using namespace ::com::sun::star::task;
78 using namespace ::com::sun::star::script;
79 using namespace ::cppu;
80 using namespace ::osl;
81 using namespace ::comphelper;
83 namespace dbaccess
86 // DocumentStorageAccess
87 class DocumentStorageAccess : public ::cppu::WeakImplHelper< XDocumentSubStorageSupplier
88 , XTransactionListener >
90 typedef std::map< OUString, Reference< XStorage > > NamedStorages;
92 ::osl::Mutex m_aMutex;
93 /// all sub storages which we ever gave to the outer world
94 NamedStorages m_aExposedStorages;
95 ODatabaseModelImpl* m_pModelImplementation;
96 bool m_bPropagateCommitToRoot;
97 bool m_bDisposingSubStorages;
99 public:
100 explicit DocumentStorageAccess( ODatabaseModelImpl& _rModelImplementation )
101 :m_pModelImplementation( &_rModelImplementation )
102 ,m_bPropagateCommitToRoot( true )
103 ,m_bDisposingSubStorages( false )
107 protected:
108 virtual ~DocumentStorageAccess() override
112 public:
113 void dispose();
115 // XDocumentSubStorageSupplier
116 virtual Reference< XStorage > SAL_CALL getDocumentSubStorage( const OUString& aStorageName, ::sal_Int32 _nMode ) override;
117 virtual Sequence< OUString > SAL_CALL getDocumentSubStoragesNames( ) override;
119 // XTransactionListener
120 virtual void SAL_CALL preCommit( const css::lang::EventObject& aEvent ) override;
121 virtual void SAL_CALL commited( const css::lang::EventObject& aEvent ) override;
122 virtual void SAL_CALL preRevert( const css::lang::EventObject& aEvent ) override;
123 virtual void SAL_CALL reverted( const css::lang::EventObject& aEvent ) override;
125 // XEventListener
126 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
128 /// disposes all storages managed by this instance
129 void disposeStorages();
131 /// disposes all known sub storages
132 void commitStorages();
134 /// commits the dedicated "database" storage
135 bool commitEmbeddedStorage( bool _bPreventRootCommits );
137 private:
138 /** opens the sub storage with the given name, in the given mode
140 Reference< XStorage > impl_openSubStorage_nothrow( const OUString& _rStorageName, sal_Int32 _nMode );
142 void impl_suspendCommitPropagation()
144 OSL_ENSURE( m_bPropagateCommitToRoot, "DocumentStorageAccess::impl_suspendCommitPropagation: already suspended" );
145 m_bPropagateCommitToRoot = false;
147 void impl_resumeCommitPropagation()
149 OSL_ENSURE( !m_bPropagateCommitToRoot, "DocumentStorageAccess::impl_resumeCommitPropagation: not suspended" );
150 m_bPropagateCommitToRoot = true;
155 void DocumentStorageAccess::dispose()
157 ::osl::MutexGuard aGuard( m_aMutex );
159 for (auto const& exposedStorage : m_aExposedStorages)
163 Reference< XTransactionBroadcaster > xBroadcaster(exposedStorage.second, UNO_QUERY);
164 if ( xBroadcaster.is() )
165 xBroadcaster->removeTransactionListener( this );
167 catch( const Exception& )
169 DBG_UNHANDLED_EXCEPTION("dbaccess");
173 m_aExposedStorages.clear();
175 m_pModelImplementation = nullptr;
178 Reference< XStorage > DocumentStorageAccess::impl_openSubStorage_nothrow( const OUString& _rStorageName, sal_Int32 _nDesiredMode )
180 OSL_ENSURE( !_rStorageName.isEmpty(),"ODatabaseModelImpl::impl_openSubStorage_nothrow: Invalid storage name!" );
182 Reference< XStorage > xStorage;
185 Reference< XStorage > xRootStorage( m_pModelImplementation->getOrCreateRootStorage() );
186 if ( xRootStorage.is() )
188 sal_Int32 nRealMode = m_pModelImplementation->m_bDocumentReadOnly ? ElementModes::READ : _nDesiredMode;
189 if ( nRealMode == ElementModes::READ )
191 if ( xRootStorage.is() && !xRootStorage->hasByName( _rStorageName ) )
192 return xStorage;
195 xStorage = xRootStorage->openStorageElement( _rStorageName, nRealMode );
197 Reference< XTransactionBroadcaster > xBroad( xStorage, UNO_QUERY );
198 if ( xBroad.is() )
199 xBroad->addTransactionListener( this );
202 catch( const Exception& )
204 DBG_UNHANDLED_EXCEPTION("dbaccess");
207 return xStorage;
210 void DocumentStorageAccess::disposeStorages()
212 m_bDisposingSubStorages = true;
214 for (auto & exposedStorage : m_aExposedStorages)
218 ::comphelper::disposeComponent( exposedStorage.second );
220 catch( const Exception& )
222 DBG_UNHANDLED_EXCEPTION("dbaccess");
225 m_aExposedStorages.clear();
227 m_bDisposingSubStorages = false;
230 void DocumentStorageAccess::commitStorages()
234 for (auto const& exposedStorage : m_aExposedStorages)
236 tools::stor::commitStorageIfWriteable( exposedStorage.second );
239 catch(const WrappedTargetException&)
241 // WrappedTargetException not allowed to leave
242 throw IOException();
246 bool DocumentStorageAccess::commitEmbeddedStorage( bool _bPreventRootCommits )
248 if ( _bPreventRootCommits )
249 impl_suspendCommitPropagation();
251 bool bSuccess = false;
254 NamedStorages::const_iterator pos = m_aExposedStorages.find( "database" );
255 if ( pos != m_aExposedStorages.end() )
256 bSuccess = tools::stor::commitStorageIfWriteable( pos->second );
258 catch( Exception& )
260 DBG_UNHANDLED_EXCEPTION("dbaccess");
263 if ( _bPreventRootCommits )
264 impl_resumeCommitPropagation();
266 return bSuccess;
270 Reference< XStorage > SAL_CALL DocumentStorageAccess::getDocumentSubStorage( const OUString& aStorageName, ::sal_Int32 _nDesiredMode )
272 ::osl::MutexGuard aGuard( m_aMutex );
273 NamedStorages::const_iterator pos = m_aExposedStorages.find( aStorageName );
274 if ( pos == m_aExposedStorages.end() )
276 Reference< XStorage > xResult = impl_openSubStorage_nothrow( aStorageName, _nDesiredMode );
277 pos = m_aExposedStorages.emplace( aStorageName, xResult ).first;
280 return pos->second;
283 Sequence< OUString > SAL_CALL DocumentStorageAccess::getDocumentSubStoragesNames( )
285 Reference< XStorage > xRootStor( m_pModelImplementation->getRootStorage() );
286 if ( !xRootStor.is() )
287 return Sequence< OUString >();
289 std::vector< OUString > aNames;
291 const Sequence< OUString > aElementNames( xRootStor->getElementNames() );
292 for ( OUString const & name : aElementNames )
294 if ( xRootStor->isStorageElement( name ) )
295 aNames.push_back( name );
297 return aNames.empty()
298 ? Sequence< OUString >()
299 : Sequence< OUString >( aNames.data(), aNames.size() );
302 void SAL_CALL DocumentStorageAccess::preCommit( const css::lang::EventObject& /*aEvent*/ )
304 // not interested in
307 void SAL_CALL DocumentStorageAccess::commited( const css::lang::EventObject& aEvent )
309 ::osl::MutexGuard aGuard( m_aMutex );
311 if ( m_pModelImplementation )
312 m_pModelImplementation->setModified( true );
314 if ( !(m_pModelImplementation && m_bPropagateCommitToRoot) )
315 return;
317 Reference< XStorage > xStorage( aEvent.Source, UNO_QUERY );
319 // check if this is the dedicated "database" sub storage
320 NamedStorages::const_iterator pos = m_aExposedStorages.find( "database" );
321 if ( ( pos != m_aExposedStorages.end() )
322 && ( pos->second == xStorage )
325 // if so, also commit the root storage
326 m_pModelImplementation->commitRootStorage();
330 void SAL_CALL DocumentStorageAccess::preRevert( const css::lang::EventObject& /*aEvent*/ )
332 // not interested in
335 void SAL_CALL DocumentStorageAccess::reverted( const css::lang::EventObject& /*aEvent*/ )
337 // not interested in
340 void SAL_CALL DocumentStorageAccess::disposing( const css::lang::EventObject& Source )
342 OSL_ENSURE( Reference< XStorage >( Source.Source, UNO_QUERY ).is(), "DocumentStorageAccess::disposing: No storage? What's this?" );
344 if ( m_bDisposingSubStorages )
345 return;
347 auto find = std::find_if(m_aExposedStorages.begin(), m_aExposedStorages.end(),
348 [&Source](const NamedStorages::value_type& rEntry) { return rEntry.second == Source.Source; });
349 if (find != m_aExposedStorages.end())
350 m_aExposedStorages.erase( find );
353 // ODatabaseModelImpl
355 ODatabaseModelImpl::ODatabaseModelImpl( const Reference< XComponentContext >& _rxContext, ODatabaseContext& _rDBContext )
356 :m_xModel()
357 ,m_xDataSource()
358 ,m_aContainer(4)
359 ,m_aMacroMode( *this )
360 ,m_nImposedMacroExecMode( MacroExecMode::NEVER_EXECUTE )
361 ,m_rDBContext( _rDBContext )
362 ,m_refCount(0)
363 ,m_aEmbeddedMacros()
364 ,m_bModificationLock( false )
365 ,m_bDocumentInitialized( false )
366 ,m_nScriptingSignatureState(SignatureState::UNKNOWN)
367 ,m_aContext( _rxContext )
368 ,m_nLoginTimeout(0)
369 ,m_bReadOnly(false)
370 ,m_bPasswordRequired(false)
371 ,m_bSuppressVersionColumns(true)
372 ,m_bModified(false)
373 ,m_bDocumentReadOnly(false)
374 ,m_bMacroCallsSeenWhileLoading(false)
375 ,m_pSharedConnectionManager(nullptr)
376 ,m_nControllerLockCount(0)
378 // some kind of default
379 m_sConnectURL = "jdbc:";
380 m_aTableFilter.realloc(1);
381 m_aTableFilter[0] = "%";
382 impl_construct_nothrow();
385 ODatabaseModelImpl::ODatabaseModelImpl(
386 const OUString& _rRegistrationName,
387 const Reference< XComponentContext >& _rxContext,
388 ODatabaseContext& _rDBContext
390 :m_xModel()
391 ,m_xDataSource()
392 ,m_aContainer(4)
393 ,m_aMacroMode( *this )
394 ,m_nImposedMacroExecMode( MacroExecMode::NEVER_EXECUTE )
395 ,m_rDBContext( _rDBContext )
396 ,m_refCount(0)
397 ,m_aEmbeddedMacros()
398 ,m_bModificationLock( false )
399 ,m_bDocumentInitialized( false )
400 ,m_nScriptingSignatureState(SignatureState::UNKNOWN)
401 ,m_aContext( _rxContext )
402 ,m_sName(_rRegistrationName)
403 ,m_nLoginTimeout(0)
404 ,m_bReadOnly(false)
405 ,m_bPasswordRequired(false)
406 ,m_bSuppressVersionColumns(true)
407 ,m_bModified(false)
408 ,m_bDocumentReadOnly(false)
409 ,m_bMacroCallsSeenWhileLoading(false)
410 ,m_pSharedConnectionManager(nullptr)
411 ,m_nControllerLockCount(0)
413 impl_construct_nothrow();
416 ODatabaseModelImpl::~ODatabaseModelImpl()
420 void ODatabaseModelImpl::impl_construct_nothrow()
422 // create the property bag to hold the settings (also known as "Info" property)
425 // the set of property value types in the bag is limited:
426 Sequence< Type > aAllowedTypes({
427 cppu::UnoType<sal_Bool>::get(),
428 cppu::UnoType<double>::get(),
429 cppu::UnoType<OUString>::get(),
430 cppu::UnoType<sal_Int32>::get(),
431 cppu::UnoType<sal_Int16>::get(),
432 cppu::UnoType<Sequence< Any >>::get(),
435 m_xSettings = PropertyBag::createWithTypes( m_aContext, aAllowedTypes, false/*AllowEmptyPropertyName*/, true/*AutomaticAddition*/ );
437 // insert the default settings
438 Reference< XPropertyContainer > xContainer( m_xSettings, UNO_QUERY_THROW );
439 Reference< XSet > xSettingsSet( m_xSettings, UNO_QUERY_THROW );
440 const AsciiPropertyValue* pSettings = getDefaultDataSourceSettings();
441 for ( ; pSettings->AsciiName; ++pSettings )
443 if ( !pSettings->DefaultValue.hasValue() )
445 Property aProperty(
446 OUString::createFromAscii( pSettings->AsciiName ),
448 pSettings->ValueType,
449 PropertyAttribute::BOUND | PropertyAttribute::MAYBEDEFAULT | PropertyAttribute::MAYBEVOID
451 xSettingsSet->insert( makeAny( aProperty ) );
453 else
455 xContainer->addProperty(
456 OUString::createFromAscii( pSettings->AsciiName ),
457 PropertyAttribute::BOUND | PropertyAttribute::MAYBEDEFAULT,
458 pSettings->DefaultValue
463 catch( const Exception& )
465 DBG_UNHANDLED_EXCEPTION("dbaccess");
467 m_rDBContext.appendAtTerminateListener(*this);
470 namespace
472 OUString lcl_getContainerStorageName_throw( ODatabaseModelImpl::ObjectType _eType )
474 const char* pAsciiName( nullptr );
475 switch ( _eType )
477 case ODatabaseModelImpl::E_FORM: pAsciiName = "forms"; break;
478 case ODatabaseModelImpl::E_REPORT: pAsciiName = "reports"; break;
479 case ODatabaseModelImpl::E_QUERY: pAsciiName = "queries"; break;
480 case ODatabaseModelImpl::E_TABLE: pAsciiName = "tables"; break;
481 default:
482 throw RuntimeException();
484 return OUString::createFromAscii( pAsciiName );
487 bool lcl_hasObjectWithMacros_throw( const ODefinitionContainer_Impl& _rObjectDefinitions, const Reference< XStorage >& _rxContainerStorage )
489 bool bSomeDocHasMacros = false;
491 for (auto const& objectDefinition : _rObjectDefinitions)
493 const TContentPtr& rDefinition( objectDefinition.second );
494 const OUString& rPersistentName( rDefinition->m_aProps.sPersistentName );
496 if ( rPersistentName.isEmpty() )
497 { // it's a logical sub folder used to organize the real objects
498 const ODefinitionContainer_Impl& rSubFoldersObjectDefinitions( dynamic_cast< const ODefinitionContainer_Impl& >( *rDefinition ) );
499 bSomeDocHasMacros = lcl_hasObjectWithMacros_throw( rSubFoldersObjectDefinitions, _rxContainerStorage );
500 if (bSomeDocHasMacros)
501 break;
502 continue;
505 bSomeDocHasMacros = ODatabaseModelImpl::objectHasMacros( _rxContainerStorage, rPersistentName );
506 if (bSomeDocHasMacros)
507 break;
509 return bSomeDocHasMacros;
512 bool lcl_hasObjectsWithMacros_nothrow( ODatabaseModelImpl& _rModel, const ODatabaseModelImpl::ObjectType _eType )
514 bool bSomeDocHasMacros = false;
516 const OContentHelper_Impl& rContainerData( *_rModel.getObjectContainer( _eType ) );
517 const ODefinitionContainer_Impl& rObjectDefinitions = dynamic_cast< const ODefinitionContainer_Impl& >( rContainerData );
521 Reference< XStorage > xContainerStorage( _rModel.getStorage( _eType ) );
522 // note the READWRITE here: If the storage already existed before, then the OpenMode will
523 // be ignored, anyway.
524 // If the storage did not yet exist, then it will be created. If the database document
525 // is read-only, the OpenMode will be automatically downgraded to READ. Otherwise,
526 // the storage will in fact be created as READWRITE. While this is not strictly necessary
527 // for this particular use case here, it is required since the storage is *cached*, and
528 // later use cases will need the READWRITE mode.
530 if ( xContainerStorage.is() )
531 bSomeDocHasMacros = lcl_hasObjectWithMacros_throw( rObjectDefinitions, xContainerStorage );
533 catch( const Exception& )
535 DBG_UNHANDLED_EXCEPTION("dbaccess");
536 // be on the safe side: If we can't reliably determine whether there are macros,
537 // assume there actually are. Better this way, than the other way round.
538 bSomeDocHasMacros = true;
541 return bSomeDocHasMacros;
545 bool ODatabaseModelImpl::objectHasMacros( const Reference< XStorage >& _rxContainerStorage, const OUString& _rPersistentName )
547 OSL_PRECOND( _rxContainerStorage.is(), "ODatabaseModelImpl::objectHasMacros: this will crash!" );
549 bool bHasMacros = true;
552 if ( !_rxContainerStorage->hasByName( _rPersistentName ) )
553 return false;
555 Reference< XStorage > xObjectStor( _rxContainerStorage->openStorageElement(
556 _rPersistentName, ElementModes::READ ) );
558 bHasMacros = ::sfx2::DocumentMacroMode::storageHasMacros( xObjectStor );
560 catch( const Exception& )
562 DBG_UNHANDLED_EXCEPTION("dbaccess");
564 return bHasMacros;
567 void ODatabaseModelImpl::reset()
569 m_bReadOnly = false;
570 std::vector< TContentPtr > aEmptyContainers( 4 );
571 m_aContainer.swap( aEmptyContainers );
573 if ( m_pStorageAccess.is() )
575 m_pStorageAccess->dispose();
576 m_pStorageAccess.clear();
580 void ODatabaseModelImpl::disposing( const css::lang::EventObject& Source )
582 Reference<XConnection> xCon(Source.Source,UNO_QUERY);
583 if ( xCon.is() )
585 bool bStore = false;
586 for (OWeakConnectionArray::iterator i = m_aConnections.begin(); i != m_aConnections.end(); )
588 css::uno::Reference< css::sdbc::XConnection > xIterConn ( *i );
589 if ( !xIterConn.is())
591 i = m_aConnections.erase(i);
593 else if ( xCon == xIterConn )
595 *i = css::uno::WeakReference< css::sdbc::XConnection >();
596 bStore = true;
597 break;
598 } else
599 ++i;
602 if ( bStore )
603 commitRootStorage();
605 else
607 OSL_FAIL( "ODatabaseModelImpl::disposing: where does this come from?" );
611 void ODatabaseModelImpl::clearConnections()
613 OWeakConnectionArray aConnections;
614 aConnections.swap( m_aConnections );
616 Reference< XConnection > xConn;
617 for (auto const& connection : aConnections)
619 xConn = connection;
620 if ( xConn.is() )
624 xConn->close();
626 catch(const Exception&)
628 DBG_UNHANDLED_EXCEPTION("dbaccess");
633 m_pSharedConnectionManager = nullptr;
634 m_xSharedConnectionManager = nullptr;
637 void ODatabaseModelImpl::dispose()
639 // dispose the data source and the model
642 Reference< XDataSource > xDS( m_xDataSource );
643 ::comphelper::disposeComponent( xDS );
645 Reference< XModel > xModel( m_xModel );
646 ::comphelper::disposeComponent( xModel );
648 catch( const Exception& )
650 DBG_UNHANDLED_EXCEPTION("dbaccess");
652 m_xDataSource = WeakReference<XDataSource>();
653 m_xModel = WeakReference< XModel >();
655 for (auto const& elem : m_aContainer)
657 if ( elem )
658 elem->m_pDataSource = nullptr;
660 m_aContainer.clear();
662 clearConnections();
664 m_xNumberFormatsSupplier = nullptr;
668 bool bCouldStore = commitEmbeddedStorage( true );
669 // "true" means that committing the embedded storage should not trigger committing the root
670 // storage. This is because we are going to commit the root storage ourself, anyway
671 disposeStorages();
672 if ( bCouldStore )
673 commitRootStorage();
675 impl_switchToStorage_throw( nullptr );
677 catch( const Exception& )
679 DBG_UNHANDLED_EXCEPTION("dbaccess");
682 if ( m_pStorageAccess.is() )
684 m_pStorageAccess->dispose();
685 m_pStorageAccess.clear();
689 const Reference< XNumberFormatsSupplier > & ODatabaseModelImpl::getNumberFormatsSupplier()
691 if (!m_xNumberFormatsSupplier.is())
693 // the arguments : the work locale of the current user
694 Locale aLocale( LanguageTag::convertToLocale( utl::ConfigManager::getWorkLocale(), false));
696 m_xNumberFormatsSupplier.set( NumberFormatsSupplier::createWithLocale( m_aContext, aLocale ) );
698 return m_xNumberFormatsSupplier;
701 void ODatabaseModelImpl::setDocFileLocation( const OUString& i_rLoadedFrom )
703 ENSURE_OR_THROW( !i_rLoadedFrom.isEmpty(), "invalid URL" );
704 m_sDocFileLocation = i_rLoadedFrom;
707 void ODatabaseModelImpl::setResource( const OUString& i_rDocumentURL, const Sequence< PropertyValue >& _rArgs )
709 ENSURE_OR_THROW( !i_rDocumentURL.isEmpty(), "invalid URL" );
711 ::comphelper::NamedValueCollection aMediaDescriptor( _rArgs );
712 #if OSL_DEBUG_LEVEL > 0
713 if ( aMediaDescriptor.has( "SalvagedFile" ) )
715 OUString sSalvagedFile( aMediaDescriptor.getOrDefault( "SalvagedFile", OUString() ) );
716 // If SalvagedFile is an empty string, this indicates "the document is being recovered, but i_rDocumentURL already
717 // is the real document URL, not the temporary document location"
718 if ( sSalvagedFile.isEmpty() )
719 sSalvagedFile = i_rDocumentURL;
721 OSL_ENSURE( sSalvagedFile == i_rDocumentURL, "ODatabaseModelImpl::setResource: inconsistency!" );
722 // nowadays, setResource should only be called with the logical URL of the document
724 #endif
726 m_aMediaDescriptor = stripLoadArguments( aMediaDescriptor );
728 impl_switchToLogicalURL( i_rDocumentURL );
731 ::comphelper::NamedValueCollection ODatabaseModelImpl::stripLoadArguments( const ::comphelper::NamedValueCollection& _rArguments )
733 OSL_ENSURE( !_rArguments.has( "Model" ), "ODatabaseModelImpl::stripLoadArguments: this is suspicious (1)!" );
734 OSL_ENSURE( !_rArguments.has( "ViewName" ), "ODatabaseModelImpl::stripLoadArguments: this is suspicious (2)!" );
736 ::comphelper::NamedValueCollection aMutableArgs( _rArguments );
737 aMutableArgs.remove( "Model" );
738 aMutableArgs.remove( "ViewName" );
739 return aMutableArgs;
742 void ODatabaseModelImpl::disposeStorages()
744 getDocumentStorageAccess()->disposeStorages();
747 Reference< XSingleServiceFactory > ODatabaseModelImpl::createStorageFactory() const
749 return StorageFactory::create( m_aContext );
752 void ODatabaseModelImpl::commitRootStorage()
754 Reference< XStorage > xStorage( getOrCreateRootStorage() );
755 bool bSuccess = commitStorageIfWriteable_ignoreErrors( xStorage );
756 SAL_WARN_IF(!bSuccess && xStorage.is(), "dbaccess",
757 "ODatabaseModelImpl::commitRootStorage: could not commit the storage!");
760 Reference< XStorage > const & ODatabaseModelImpl::getOrCreateRootStorage()
762 if ( !m_xDocumentStorage.is() )
764 Reference< XSingleServiceFactory> xStorageFactory = StorageFactory::create( m_aContext );
765 Any aSource = m_aMediaDescriptor.get( "Stream" );
766 if ( !aSource.hasValue() )
767 aSource = m_aMediaDescriptor.get( "InputStream" );
768 if ( !aSource.hasValue() && !m_sDocFileLocation.isEmpty() )
769 aSource <<= m_sDocFileLocation;
770 // TODO: shouldn't we also check URL?
772 OSL_ENSURE( aSource.hasValue(), "ODatabaseModelImpl::getOrCreateRootStorage: no source to create the storage from!" );
774 if ( aSource.hasValue() )
776 Sequence< Any > aStorageCreationArgs(2);
777 aStorageCreationArgs[0] = aSource;
778 aStorageCreationArgs[1] <<= ElementModes::READWRITE;
780 Reference< XStorage > xDocumentStorage;
781 OUString sURL;
782 aSource >>= sURL;
783 // Don't try to load a meta-URL as-is.
784 if (!sURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:"))
788 xDocumentStorage.set( xStorageFactory->createInstanceWithArguments( aStorageCreationArgs ), UNO_QUERY_THROW );
790 catch( const Exception& )
792 m_bDocumentReadOnly = true;
793 aStorageCreationArgs[1] <<= ElementModes::READ;
796 xDocumentStorage.set( xStorageFactory->createInstanceWithArguments( aStorageCreationArgs ), UNO_QUERY_THROW );
798 catch( const Exception& )
800 DBG_UNHANDLED_EXCEPTION("dbaccess");
805 impl_switchToStorage_throw( xDocumentStorage );
808 return m_xDocumentStorage.getTyped();
811 DocumentStorageAccess* ODatabaseModelImpl::getDocumentStorageAccess()
813 if ( !m_pStorageAccess.is() )
815 m_pStorageAccess = new DocumentStorageAccess( *this );
817 return m_pStorageAccess.get();
820 void ODatabaseModelImpl::modelIsDisposing( const bool _wasInitialized, ResetModelAccess )
822 m_xModel.clear();
824 // Basic libraries and Dialog libraries are a model facet, though held at this impl class.
825 // They automatically dispose themself when the model they belong to is being disposed.
826 // So, to not be tempted to do anything with them, again, we reset them.
827 m_xBasicLibraries.clear();
828 m_xDialogLibraries.clear();
830 m_bDocumentInitialized = _wasInitialized;
833 Reference< XDocumentSubStorageSupplier > ODatabaseModelImpl::getDocumentSubStorageSupplier()
835 return getDocumentStorageAccess();
838 bool ODatabaseModelImpl::commitEmbeddedStorage( bool _bPreventRootCommits )
840 return getDocumentStorageAccess()->commitEmbeddedStorage( _bPreventRootCommits );
843 bool ODatabaseModelImpl::commitStorageIfWriteable_ignoreErrors( const Reference< XStorage >& _rxStorage )
845 bool bTryToPreserveScriptSignature = false;
846 utl::TempFile aTempFile;
847 aTempFile.EnableKillingFile();
848 OUString sTmpFileUrl = aTempFile.GetURL();
849 SignatureState aSignatureState = getScriptingSignatureState();
850 OUString sLocation = getDocFileLocation();
851 bool bIsEmbedded = sLocation.startsWith("vnd.sun.star.pkg:") && sLocation.endsWith("/EmbeddedDatabase");
852 if (!bIsEmbedded && !sLocation.isEmpty()
853 && (aSignatureState == SignatureState::OK || aSignatureState == SignatureState::NOTVALIDATED
854 || aSignatureState == SignatureState::INVALID
855 || aSignatureState == SignatureState::UNKNOWN))
857 bTryToPreserveScriptSignature = true;
858 // We need to first save the file (which removes the macro signature), then add the macro signature again.
859 // For that, we need a temporary copy of the original file.
860 osl::File::RC rc = osl::File::copy(sLocation, sTmpFileUrl);
861 if (rc != osl::FileBase::E_None)
862 throw uno::RuntimeException("Could not create temp file");
865 bool bSuccess = false;
868 bSuccess = tools::stor::commitStorageIfWriteable( _rxStorage );
870 catch( const Exception& )
872 DBG_UNHANDLED_EXCEPTION("dbaccess");
875 // Preserve script signature if the script has not changed
876 if (bTryToPreserveScriptSignature)
878 OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(_rxStorage));
879 uno::Reference<security::XDocumentDigitalSignatures> xDDSigns;
882 xDDSigns = security::DocumentDigitalSignatures::createWithVersion(
883 comphelper::getProcessComponentContext(), aODFVersion);
885 const OUString aScriptSignName
886 = xDDSigns->getScriptingContentSignatureDefaultStreamName();
888 if (!aScriptSignName.isEmpty())
890 Reference<XStorage> xReadOrig
891 = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
892 ZIP_STORAGE_FORMAT_STRING, sTmpFileUrl, ElementModes::READ);
893 if (!xReadOrig.is())
894 throw uno::RuntimeException("Could not read " + sTmpFileUrl);
895 uno::Reference<embed::XStorage> xMetaInf
896 = xReadOrig->openStorageElement("META-INF", embed::ElementModes::READ);
898 uno::Reference<embed::XStorage> xTargetMetaInf
899 = _rxStorage->openStorageElement("META-INF", embed::ElementModes::READWRITE);
900 if (xMetaInf.is() && xTargetMetaInf.is())
902 xMetaInf->copyElementTo(aScriptSignName, xTargetMetaInf, aScriptSignName);
904 uno::Reference<embed::XTransactedObject> xTransact(xTargetMetaInf,
905 uno::UNO_QUERY);
906 if (xTransact.is())
907 xTransact->commit();
909 xTargetMetaInf->dispose();
911 // now check the copied signature
912 uno::Sequence<security::DocumentSignatureInformation> aInfos
913 = xDDSigns->verifyScriptingContentSignatures(
914 _rxStorage, uno::Reference<io::XInputStream>());
915 SignatureState nState = DocumentSignatures::getSignatureState(aInfos);
916 if (nState == SignatureState::OK || nState == SignatureState::NOTVALIDATED
917 || nState == SignatureState::PARTIAL_OK)
919 // commit the ZipStorage from target medium
920 xTransact.set(_rxStorage, uno::UNO_QUERY);
921 if (xTransact.is())
922 xTransact->commit();
924 else
926 SAL_WARN("dbaccess", "An invalid signature was copied!");
931 catch (uno::Exception&)
933 TOOLS_WARN_EXCEPTION("dbaccess", "");
937 return bSuccess;
940 void ODatabaseModelImpl::setModified( bool _bModified )
942 if ( isModifyLocked() )
943 return;
947 Reference< XModifiable > xModi( m_xModel.get(), UNO_QUERY );
948 if ( xModi.is() )
949 xModi->setModified( _bModified );
950 else
951 m_bModified = _bModified;
953 catch( const Exception& )
955 DBG_UNHANDLED_EXCEPTION("dbaccess");
959 Reference<XDataSource> ODatabaseModelImpl::getOrCreateDataSource()
961 Reference<XDataSource> xDs = m_xDataSource;
962 if ( !xDs.is() )
964 xDs = new ODatabaseSource(this);
965 m_xDataSource = xDs;
967 return xDs;
970 Reference< XModel> ODatabaseModelImpl::getModel_noCreate() const
972 return m_xModel;
975 Reference< XModel > ODatabaseModelImpl::createNewModel_deliverOwnership()
977 Reference< XModel > xModel( m_xModel );
978 OSL_PRECOND( !xModel.is(), "ODatabaseModelImpl::createNewModel_deliverOwnership: not to be called if there already is a model!" );
979 if ( !xModel.is() )
981 bool bHadModelBefore = m_bDocumentInitialized;
983 xModel = ODatabaseDocument::createDatabaseDocument( this, ODatabaseDocument::FactoryAccess() );
984 m_xModel = xModel;
988 Reference< XGlobalEventBroadcaster > xModelCollection = theGlobalEventBroadcaster::get( m_aContext );
989 xModelCollection->insert( makeAny( xModel ) );
991 catch( const Exception& )
993 DBG_UNHANDLED_EXCEPTION("dbaccess");
996 if ( bHadModelBefore )
998 // do an attachResources
999 // In case the document is loaded regularly, this is not necessary, as our loader will do it.
1000 // However, in case that the document is implicitly created by asking the data source for the document,
1001 // then nobody would call the doc's attachResource. So, we do it here, to ensure it's in a proper
1002 // state, fires all events, and so on.
1003 // #i105505#
1004 xModel->attachResource( xModel->getURL(), m_aMediaDescriptor.getPropertyValues() );
1007 return xModel;
1010 void ODatabaseModelImpl::acquire()
1012 osl_atomic_increment(&m_refCount);
1015 void ODatabaseModelImpl::release()
1017 if ( osl_atomic_decrement(&m_refCount) == 0 )
1019 acquire(); // prevent multiple releases
1020 m_rDBContext.removeFromTerminateListener(*this);
1021 dispose();
1022 m_rDBContext.storeTransientProperties(*this);
1023 if (!m_sDocumentURL.isEmpty())
1024 m_rDBContext.revokeDatabaseDocument(*this);
1025 delete this;
1029 void ODatabaseModelImpl::commitStorages()
1031 getDocumentStorageAccess()->commitStorages();
1034 Reference< XStorage > ODatabaseModelImpl::getStorage( const ObjectType _eType )
1036 return getDocumentStorageAccess()->getDocumentSubStorage( getObjectContainerStorageName( _eType ),
1037 css::embed::ElementModes::READWRITE );
1040 const AsciiPropertyValue* ODatabaseModelImpl::getDefaultDataSourceSettings()
1042 static const AsciiPropertyValue aKnownSettings[] =
1044 // known JDBC settings
1045 AsciiPropertyValue( "JavaDriverClass", makeAny( OUString() ) ),
1046 AsciiPropertyValue( "JavaDriverClassPath", makeAny( OUString() ) ),
1047 AsciiPropertyValue( "IgnoreCurrency", makeAny( false ) ),
1048 // known settings for file-based drivers
1049 AsciiPropertyValue( "Extension", makeAny( OUString() ) ),
1050 AsciiPropertyValue( "CharSet", makeAny( OUString() ) ),
1051 AsciiPropertyValue( "HeaderLine", makeAny( true ) ),
1052 AsciiPropertyValue( "FieldDelimiter", makeAny( OUString( "," ) ) ),
1053 AsciiPropertyValue( "StringDelimiter", makeAny( OUString( "\"" ) ) ),
1054 AsciiPropertyValue( "DecimalDelimiter", makeAny( OUString( "." ) ) ),
1055 AsciiPropertyValue( "ThousandDelimiter", makeAny( OUString() ) ),
1056 AsciiPropertyValue( "ShowDeleted", makeAny( false ) ),
1057 // known ODBC settings
1058 AsciiPropertyValue( "SystemDriverSettings", makeAny( OUString() ) ),
1059 AsciiPropertyValue( "UseCatalog", makeAny( false ) ),
1060 AsciiPropertyValue( "TypeInfoSettings", makeAny( Sequence< Any >()) ),
1061 // settings related to auto increment handling
1062 AsciiPropertyValue( "AutoIncrementCreation", makeAny( OUString() ) ),
1063 AsciiPropertyValue( "AutoRetrievingStatement", makeAny( OUString() ) ),
1064 AsciiPropertyValue( "IsAutoRetrievingEnabled", makeAny( false ) ),
1065 // known LDAP driver settings
1066 AsciiPropertyValue( "HostName", makeAny( OUString() ) ),
1067 AsciiPropertyValue( "PortNumber", makeAny( sal_Int32(389) ) ),
1068 AsciiPropertyValue( "BaseDN", makeAny( OUString() ) ),
1069 AsciiPropertyValue( "MaxRowCount", makeAny( sal_Int32(100) ) ),
1070 // known MySQLNative driver settings
1071 AsciiPropertyValue( "LocalSocket", makeAny( OUString() ) ),
1072 AsciiPropertyValue( "NamedPipe", makeAny( OUString() ) ),
1073 // misc known driver settings
1074 AsciiPropertyValue( "ParameterNameSubstitution", makeAny( false ) ),
1075 AsciiPropertyValue( "AddIndexAppendix", makeAny( true ) ),
1076 AsciiPropertyValue( "IgnoreDriverPrivileges", makeAny( true ) ),
1077 AsciiPropertyValue( "ImplicitCatalogRestriction", ::cppu::UnoType< OUString >::get() ),
1078 AsciiPropertyValue( "ImplicitSchemaRestriction", ::cppu::UnoType< OUString >::get() ),
1079 AsciiPropertyValue( "PrimaryKeySupport", ::cppu::UnoType< sal_Bool >::get() ),
1080 AsciiPropertyValue( "ShowColumnDescription", makeAny( false ) ),
1081 // known SDB level settings
1082 AsciiPropertyValue( "NoNameLengthLimit", makeAny( false ) ),
1083 AsciiPropertyValue( "AppendTableAliasName", makeAny( false ) ),
1084 AsciiPropertyValue( "GenerateASBeforeCorrelationName", makeAny( false ) ),
1085 AsciiPropertyValue( "ColumnAliasInOrderBy", makeAny( true ) ),
1086 AsciiPropertyValue( "EnableSQL92Check", makeAny( false ) ),
1087 AsciiPropertyValue( "BooleanComparisonMode", makeAny( BooleanComparisonMode::EQUAL_INTEGER ) ),
1088 AsciiPropertyValue( "TableTypeFilterMode", makeAny( sal_Int32(3) ) ),
1089 AsciiPropertyValue( "RespectDriverResultSetType", makeAny( false ) ),
1090 AsciiPropertyValue( "UseSchemaInSelect", makeAny( true ) ),
1091 AsciiPropertyValue( "UseCatalogInSelect", makeAny( true ) ),
1092 AsciiPropertyValue( "EnableOuterJoinEscape", makeAny( true ) ),
1093 AsciiPropertyValue( "PreferDosLikeLineEnds", makeAny( false ) ),
1094 AsciiPropertyValue( "FormsCheckRequiredFields", makeAny( true ) ),
1095 AsciiPropertyValue( "EscapeDateTime", makeAny( true ) ),
1097 // known services to handle database tasks
1098 AsciiPropertyValue( "TableAlterationServiceName", makeAny( OUString() ) ),
1099 AsciiPropertyValue( "TableRenameServiceName", makeAny( OUString() ) ),
1100 AsciiPropertyValue( "ViewAlterationServiceName", makeAny( OUString() ) ),
1101 AsciiPropertyValue( "ViewAccessServiceName", makeAny( OUString() ) ),
1102 AsciiPropertyValue( "CommandDefinitions", makeAny( OUString() ) ),
1103 AsciiPropertyValue( "Forms", makeAny( OUString() ) ),
1104 AsciiPropertyValue( "Reports", makeAny( OUString() ) ),
1105 AsciiPropertyValue( "KeyAlterationServiceName", makeAny( OUString() ) ),
1106 AsciiPropertyValue( "IndexAlterationServiceName", makeAny( OUString() ) ),
1108 AsciiPropertyValue()
1110 return aKnownSettings;
1113 TContentPtr& ODatabaseModelImpl::getObjectContainer( ObjectType _eType )
1115 OSL_PRECOND( _eType >= E_FORM && _eType <= E_TABLE, "ODatabaseModelImpl::getObjectContainer: illegal index!" );
1116 TContentPtr& rContentPtr = m_aContainer[ _eType ];
1118 if ( !rContentPtr )
1120 rContentPtr = std::make_shared<ODefinitionContainer_Impl>();
1121 rContentPtr->m_pDataSource = this;
1122 rContentPtr->m_aProps.aTitle = lcl_getContainerStorageName_throw( _eType );
1124 return rContentPtr;
1127 bool ODatabaseModelImpl::adjustMacroMode_AutoReject()
1129 return m_aMacroMode.adjustMacroMode( nullptr );
1132 bool ODatabaseModelImpl::checkMacrosOnLoading()
1134 Reference< XInteractionHandler > xInteraction;
1135 xInteraction = m_aMediaDescriptor.getOrDefault( "InteractionHandler", xInteraction );
1136 return m_aMacroMode.checkMacrosOnLoading( xInteraction );
1139 void ODatabaseModelImpl::resetMacroExecutionMode()
1141 m_aMacroMode = ::sfx2::DocumentMacroMode( *this );
1144 Reference< XStorageBasedLibraryContainer > ODatabaseModelImpl::getLibraryContainer( bool _bScript )
1146 Reference< XStorageBasedLibraryContainer >& rxContainer( _bScript ? m_xBasicLibraries : m_xDialogLibraries );
1147 if ( rxContainer.is() )
1148 return rxContainer;
1150 Reference< XStorageBasedDocument > xDocument( getModel_noCreate(), UNO_QUERY_THROW );
1151 // this is only to be called if there already exists a document model - in fact, it is
1152 // to be called by the document model only
1156 Reference< XStorageBasedLibraryContainer > (*Factory)( const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&)
1157 = _bScript ? &DocumentScriptLibraryContainer::create : &DocumentDialogLibraryContainer::create;
1159 rxContainer.set(
1160 (*Factory)( m_aContext, xDocument ),
1161 UNO_SET_THROW
1164 catch( const RuntimeException& )
1166 throw;
1168 catch( const Exception& )
1170 throw WrappedTargetRuntimeException(
1171 OUString(),
1172 xDocument,
1173 ::cppu::getCaughtException()
1176 return rxContainer;
1179 void ODatabaseModelImpl::storeLibraryContainersTo( const Reference< XStorage >& _rxToRootStorage )
1181 if ( m_xBasicLibraries.is() )
1182 m_xBasicLibraries->storeLibrariesToStorage( _rxToRootStorage );
1184 if ( m_xDialogLibraries.is() )
1185 m_xDialogLibraries->storeLibrariesToStorage( _rxToRootStorage );
1188 Reference< XStorage > ODatabaseModelImpl::switchToStorage( const Reference< XStorage >& _rxNewRootStorage )
1190 if ( !_rxNewRootStorage.is() )
1191 throw IllegalArgumentException();
1193 return impl_switchToStorage_throw( _rxNewRootStorage );
1196 namespace
1198 void lcl_modifyListening( ::sfx2::IModifiableDocument& _rDocument,
1199 const Reference< XStorage >& _rxStorage, ::rtl::Reference< ::sfx2::DocumentStorageModifyListener >& _inout_rListener,
1200 comphelper::SolarMutex& _rMutex, bool _bListen )
1202 Reference< XModifiable > xModify( _rxStorage, UNO_QUERY );
1203 OSL_ENSURE( xModify.is() || !_rxStorage.is(), "lcl_modifyListening: storage can't notify us!" );
1205 if ( xModify.is() && !_bListen && _inout_rListener.is() )
1207 xModify->removeModifyListener( _inout_rListener.get() );
1210 if ( _inout_rListener.is() )
1212 _inout_rListener->dispose();
1213 _inout_rListener = nullptr;
1216 if ( xModify.is() && _bListen )
1218 _inout_rListener = new ::sfx2::DocumentStorageModifyListener( _rDocument, _rMutex );
1219 xModify->addModifyListener( _inout_rListener.get() );
1224 namespace
1226 void lcl_rebaseScriptStorage_throw( const Reference< XStorageBasedLibraryContainer >& _rxContainer,
1227 const Reference< XStorage >& _rxNewRootStorage )
1229 if ( _rxContainer.is() )
1231 if ( _rxNewRootStorage.is() )
1232 _rxContainer->setRootStorage( _rxNewRootStorage );
1233 // else
1234 // TODO: what to do here? dispose the container?
1239 Reference< XStorage > const & ODatabaseModelImpl::impl_switchToStorage_throw( const Reference< XStorage >& _rxNewRootStorage )
1241 // stop listening for modifications at the old storage
1242 lcl_modifyListening( *this, m_xDocumentStorage.getTyped(), m_pStorageModifyListener, Application::GetSolarMutex(), false );
1244 // set new storage
1245 m_xDocumentStorage.reset( _rxNewRootStorage, SharedStorage::TakeOwnership );
1247 // start listening for modifications
1248 lcl_modifyListening( *this, m_xDocumentStorage.getTyped(), m_pStorageModifyListener, Application::GetSolarMutex(), true );
1250 // forward new storage to Basic and Dialog library containers
1251 lcl_rebaseScriptStorage_throw( m_xBasicLibraries, m_xDocumentStorage.getTyped() );
1252 lcl_rebaseScriptStorage_throw( m_xDialogLibraries, m_xDocumentStorage.getTyped() );
1254 m_bReadOnly = !tools::stor::storageIsWritable_nothrow( m_xDocumentStorage.getTyped() );
1255 // TODO: our data source, if it exists, must broadcast the change of its ReadOnly property
1257 return m_xDocumentStorage.getTyped();
1260 void ODatabaseModelImpl::impl_switchToLogicalURL( const OUString& i_rDocumentURL )
1262 if ( i_rDocumentURL == m_sDocumentURL )
1263 return;
1265 const OUString sOldURL( m_sDocumentURL );
1266 // update our name, if necessary
1267 if ( ( m_sName == m_sDocumentURL ) // our name is our old URL
1268 || ( m_sName.isEmpty() ) // we do not have a name, yet (i.e. are not registered at the database context)
1271 INetURLObject aURL( i_rDocumentURL );
1272 if ( aURL.GetProtocol() != INetProtocol::NotValid )
1274 m_sName = i_rDocumentURL;
1275 // TODO: our data source must broadcast the change of the Name property
1279 // remember URL
1280 m_sDocumentURL = i_rDocumentURL;
1282 // update our location, if necessary
1283 if ( m_sDocFileLocation.isEmpty() )
1284 m_sDocFileLocation = m_sDocumentURL;
1286 // register at the database context, or change registration
1287 if (!sOldURL.isEmpty())
1288 m_rDBContext.databaseDocumentURLChange( sOldURL, m_sDocumentURL );
1289 else
1290 m_rDBContext.registerDatabaseDocument( *this );
1293 OUString ODatabaseModelImpl::getObjectContainerStorageName( const ObjectType _eType )
1295 return lcl_getContainerStorageName_throw( _eType );
1298 sal_Int16 ODatabaseModelImpl::getCurrentMacroExecMode() const
1300 sal_Int16 nCurrentMode = MacroExecMode::NEVER_EXECUTE;
1303 nCurrentMode = m_aMediaDescriptor.getOrDefault( "MacroExecutionMode", nCurrentMode );
1305 catch( const Exception& )
1307 DBG_UNHANDLED_EXCEPTION("dbaccess");
1309 return nCurrentMode;
1312 void ODatabaseModelImpl::setCurrentMacroExecMode( sal_uInt16 nMacroMode )
1314 m_aMediaDescriptor.put( "MacroExecutionMode", nMacroMode );
1317 OUString ODatabaseModelImpl::getDocumentLocation() const
1319 return getURL();
1320 // formerly, we returned getDocFileLocation here, which is the location of the file from which we
1321 // recovered the "real" document.
1322 // However, during CWS autorecovery evolving, we clarified (with MAV/MT) the role of XModel::getURL and
1323 // XStorable::getLocation. In this course, we agreed that for a macro security check, the *document URL*
1324 // (not the recovery file URL) is to be used: The recovery file lies in the backup folder, and by definition,
1325 // this folder is considered to be secure. So, the document URL needs to be used to decide about the security.
1328 ODatabaseModelImpl::EmbeddedMacros ODatabaseModelImpl::determineEmbeddedMacros()
1330 if ( !m_aEmbeddedMacros )
1332 if ( ::sfx2::DocumentMacroMode::storageHasMacros( getOrCreateRootStorage() ) )
1334 m_aEmbeddedMacros = eDocumentWideMacros;
1336 else if ( lcl_hasObjectsWithMacros_nothrow( *this, E_FORM )
1337 || lcl_hasObjectsWithMacros_nothrow( *this, E_REPORT )
1340 m_aEmbeddedMacros = eSubDocumentMacros;
1342 else
1344 m_aEmbeddedMacros = eNoMacros;
1347 return *m_aEmbeddedMacros;
1350 bool ODatabaseModelImpl::documentStorageHasMacros() const
1352 const_cast< ODatabaseModelImpl* >( this )->determineEmbeddedMacros();
1353 return ( *m_aEmbeddedMacros != eNoMacros );
1356 bool ODatabaseModelImpl::macroCallsSeenWhileLoading() const
1358 return m_bMacroCallsSeenWhileLoading;
1361 Reference< XEmbeddedScripts > ODatabaseModelImpl::getEmbeddedDocumentScripts() const
1363 return Reference< XEmbeddedScripts >( getModel_noCreate(), UNO_QUERY );
1366 SignatureState ODatabaseModelImpl::getScriptingSignatureState()
1368 return m_nScriptingSignatureState;
1371 bool ODatabaseModelImpl::hasTrustedScriptingSignature(bool bAllowUIToAddAuthor)
1373 bool bResult = false;
1377 // Don't use m_xDocumentStorage, that somehow has an incomplete storage representation
1378 // which leads to signatures not being found
1379 Reference<XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
1380 ZIP_STORAGE_FORMAT_STRING, m_sDocFileLocation, ElementModes::READ);
1382 OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(getOrCreateRootStorage()));
1383 uno::Reference<security::XDocumentDigitalSignatures> xSigner(
1384 security::DocumentDigitalSignatures::createWithVersion(
1385 comphelper::getProcessComponentContext(), aODFVersion));
1386 uno::Sequence<security::DocumentSignatureInformation> aInfo
1387 = xSigner->verifyScriptingContentSignatures(xStorage,
1388 uno::Reference<io::XInputStream>());
1390 if (!aInfo.hasElements())
1391 return false;
1393 m_nScriptingSignatureState = DocumentSignatures::getSignatureState(aInfo);
1394 if (m_nScriptingSignatureState == SignatureState::OK
1395 || m_nScriptingSignatureState == SignatureState::NOTVALIDATED)
1397 bResult = std::any_of(aInfo.begin(), aInfo.end(),
1398 [&xSigner](const security::DocumentSignatureInformation& rInfo) {
1399 return xSigner->isAuthorTrusted(rInfo.Signer);
1403 if (!bResult && bAllowUIToAddAuthor)
1405 Reference<XInteractionHandler> xInteraction;
1406 xInteraction = m_aMediaDescriptor.getOrDefault("InteractionHandler", xInteraction);
1407 if (xInteraction.is())
1409 task::DocumentMacroConfirmationRequest aRequest;
1410 aRequest.DocumentURL = m_sDocFileLocation;
1411 aRequest.DocumentStorage = xStorage;
1412 aRequest.DocumentSignatureInformation = aInfo;
1413 aRequest.DocumentVersion = aODFVersion;
1414 aRequest.Classification = task::InteractionClassification_QUERY;
1415 bResult = SfxMedium::CallApproveHandler(xInteraction, uno::makeAny(aRequest), true);
1419 catch (uno::Exception&)
1423 return bResult;
1426 void ODatabaseModelImpl::storageIsModified()
1428 setModified( true );
1431 ModelDependentComponent::ModelDependentComponent( const ::rtl::Reference< ODatabaseModelImpl >& _model )
1432 :m_pImpl( _model )
1436 ModelDependentComponent::~ModelDependentComponent()
1440 } // namespace dbaccess
1442 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */