LibreOffice Module dbaccess (master) 1
ModelImpl.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include "ContentHelper.hxx"
23#include "documentevents.hxx"
24
25#include <com/sun/star/beans/PropertyValue.hpp>
26#include <com/sun/star/beans/XPropertyBag.hpp>
27#include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
28#include <com/sun/star/embed/XStorage.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/lang/XSingleServiceFactory.hpp>
31#include <com/sun/star/script/XStorageBasedLibraryContainer.hpp>
32#include <com/sun/star/sdbc/XDataSource.hpp>
33#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
34
37#include <vcl/svapp.hxx>
38#include <sfx2/docmacromode.hxx>
41#include <rtl/ref.hxx>
42#include <o3tl/enumarray.hxx>
43
44namespace comphelper
45{
46 class NamedValueCollection;
47}
48
49namespace dbaccess
50{
51
52typedef std::vector< css::uno::WeakReference< css::sdbc::XConnection > > OWeakConnectionArray;
53
55{
56 // note: the canonic member order would be AsciiName / DefaultValue, but
57 // this crashes on unxlngi6.pro, since there's a bug which somehow results in
58 // getDefaultDataSourceSettings returning corrupted Any instances then.
59 css::uno::Any DefaultValue;
60 const char* AsciiName;
61 const css::uno::Type& ValueType;
62
64 :DefaultValue( )
65 ,AsciiName( nullptr )
66 ,ValueType( ::cppu::UnoType<void>::get() )
67 {
68 }
69
70 AsciiPropertyValue( const char* _pAsciiName, const css::uno::Any& _rDefaultValue )
71 :DefaultValue( _rDefaultValue )
72 ,AsciiName( _pAsciiName )
73 ,ValueType( _rDefaultValue.getValueType() )
74 {
75 OSL_ENSURE( ValueType.getTypeClass() != css::uno::TypeClass_VOID,
76 "AsciiPropertyValue::AsciiPropertyValue: NULL values not allowed here, use the other CTOR for this!" );
77 }
78 AsciiPropertyValue( const char* _pAsciiName, const css::uno::Type& _rValeType )
80 ,AsciiName( _pAsciiName )
81 ,ValueType( _rValeType )
82 {
83 OSL_ENSURE( ValueType.getTypeClass() != css::uno::TypeClass_VOID,
84 "AsciiPropertyValue::AsciiPropertyValue: VOID property values not supported!" );
85 }
86};
87
88class ODatabaseContext;
89class OSharedConnectionManager;
90
91// ODatabaseModelImpl
92typedef ::utl::SharedUNOComponent< css::embed::XStorage > SharedStorage;
93
97
100{
101public:
102
103 enum class ObjectType
104 {
105 Form = 0,
106 Report = 1,
107 Query = 2,
108 Table = 3,
109 LAST = Table
110 };
111
112 enum class EmbeddedMacros
113 {
114 // the database document (storage) itself contains macros
115 DocumentWide,
116 // there are sub document( storage)s containing macros
117 SubDocument,
118 // there are no known macro( storage)s
119 NONE
120 };
121
122private:
123 css::uno::WeakReference< css::frame::XModel > m_xModel;
124 css::uno::WeakReference< css::sdbc::XDataSource > m_xDataSource;
125
130
131 css::uno::Reference< css::script::XStorageBasedLibraryContainer > m_xBasicLibraries;
132 css::uno::Reference< css::script::XStorageBasedLibraryContainer > m_xDialogLibraries;
133
138
142
143 oslInterlockedCount m_refCount;
144
146 ::std::optional< EmbeddedMacros > m_aEmbeddedMacros;
147
150
153
163
165
166public:
168 const css::uno::Reference< css::uno::XComponentContext > m_aContext;
169
170public:
171 css::uno::WeakReference< css::container::XNameAccess > m_xCommandDefinitions;
172 css::uno::WeakReference< css::container::XNameAccess > m_xTableDefinitions;
173
174 css::uno::Reference< css::util::XNumberFormatsSupplier >
177 OUString m_sName; // transient, our creator has to tell us the title
178 OUString m_sUser;
179 OUString m_aPassword; // transient !
181 css::uno::Sequence< css::beans::PropertyValue>
184 bool m_bReadOnly : 1;
187 bool m_bModified : 1;
190 css::uno::Reference< css::beans::XPropertyBag >
192 css::uno::Sequence< OUString > m_aTableFilter;
193 css::uno::Sequence< OUString > m_aTableTypeFilter;
195 css::uno::Reference< css::lang::XEventListener >
197 css::uno::Reference<css::awt::XWindow>
200
201 void reset();
202
205 bool isEmbeddedDatabase() const { return m_sConnectURL.startsWith("sdbc:embedded:"); }
206
216 bool commitEmbeddedStorage( bool _bPreventRootCommits = false );
217
219 void commitStorages();
220
222 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
223 ODatabaseContext& _pDBContext
224 );
225 virtual ~ODatabaseModelImpl();
226
228 OUString _sRegistrationName,
229 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
230 ODatabaseContext& _rDBContext
231 );
232
233 // XEventListener
235 void disposing( const css::lang::EventObject& Source );
236
237 void setModified( bool bModified );
238
239 void dispose();
240
241 const OUString& getURL() const { return m_sDocumentURL; }
242 const OUString& getDocFileLocation() const { return m_sDocFileLocation; }
243
244 css::uno::Reference< css::embed::XStorage >
245 getStorage( const ObjectType _eType );
246
247// helper
248 const css::uno::Reference< css::util::XNumberFormatsSupplier >&
250
253
254 const ::comphelper::NamedValueCollection&
256
257 void setResource(
258 const OUString& _rURL,
259 const css::uno::Sequence< css::beans::PropertyValue >& _rArgs
260 );
262 const OUString& i_rLoadedFrom
263 );
264
265 static ::comphelper::NamedValueCollection
266 stripLoadArguments( const ::comphelper::NamedValueCollection& _rArguments );
267
268// other stuff
269
270 // disposes all elements in m_aStorages, and clears it
271 void disposeStorages();
272
274 css::uno::Reference< css::lang::XSingleServiceFactory >
275 createStorageFactory() const;
276
278 void commitRootStorage();
279
282 const css::uno::Reference< css::embed::XStorage >& _rxStorage
283 );
284
285 void clearConnections();
286
287 css::uno::Reference< css::embed::XStorage > const & getOrCreateRootStorage();
288 css::uno::Reference< css::embed::XStorage > const & getRootStorage() const { return m_xDocumentStorage.getTyped(); }
290
293 css::uno::Reference< css::sdbc::XDataSource> getOrCreateDataSource();
294
297 css::uno::Reference< css::frame::XModel > getModel_noCreate() const;
298
307 css::uno::Reference< css::frame::XModel > createNewModel_deliverOwnership();
308
309 struct ResetModelAccess { friend class ODatabaseDocument; private: ResetModelAccess() { } };
310
315 void modelIsDisposing( const bool _wasInitialized, ResetModelAccess );
316
318
321
322 css::uno::Reference< css::document::XDocumentSubStorageSupplier >
324
325 void acquire();
326
327 void release();
328
331
335
338 static OUString
340
343 static bool objectHasMacros(
344 const css::uno::Reference< css::embed::XStorage >& _rxContainerStorage,
345 const OUString& _rPersistentName
346 );
347
351
356
367
372
381 css::uno::Reference< css::script::XStorageBasedLibraryContainer >
382 getLibraryContainer( bool _bScript );
383
386 void storeLibraryContainersTo( const css::uno::Reference< css::embed::XStorage >& _rxToRootStorage );
387
398 css::uno::Reference< css::embed::XStorage >
400 const css::uno::Reference< css::embed::XStorage >& _rxNewRootStorage
401 );
402
405 sal_Int16 getImposedMacroExecMode() const
406 {
408 }
409 void setImposedMacroExecMode( const sal_Int16 _nMacroMode )
410 {
411 m_nImposedMacroExecMode = _nMacroMode;
412 }
413
414public:
415 // IMacroDocumentAccess overridables
416 virtual sal_Int16 getCurrentMacroExecMode() const override;
417 virtual void setCurrentMacroExecMode( sal_uInt16 ) override;
418 virtual OUString getDocumentLocation() const override;
419 virtual bool documentStorageHasMacros() const override;
420 virtual bool macroCallsSeenWhileLoading() const override;
421 virtual css::uno::Reference< css::document::XEmbeddedScripts > getEmbeddedDocumentScripts() const override;
423 virtual bool hasTrustedScriptingSignature( bool bAllowUIToAddAuthor ) override;
424
425 // IModifiableDocument
426 virtual void storageIsModified() override;
427
428 // don't use directly, use the ModifyLock class instead
431 bool isModifyLocked() const { return m_bModificationLock; }
432
434
435private:
437 css::uno::Reference< css::embed::XStorage > const &
438 impl_switchToStorage_throw( const css::uno::Reference< css::embed::XStorage >& _rxNewRootStorage );
439
444 const OUString& i_rDocumentURL
445 );
446
447};
448
452{
453protected:
455 ::osl::Mutex m_aMutex; // only use this to init WeakComponentImplHelper
456
457protected:
459 virtual ~ModelDependentComponent();
460
463 virtual css::uno::Reference< css::uno::XInterface > getThis() const = 0;
464
465 ::osl::Mutex& getMutex()
466 {
467 return m_aMutex;
468 }
469
470public:
472 void checkDisposed() const
473 {
474 if ( !m_pImpl.is() )
475 throw css::lang::DisposedException( "Component is already disposed.", getThis() );
476 }
477
479 {
480 m_pImpl->lockModify();
481 }
482
484 {
485 m_pImpl->unlockModify();
486 }
487};
488
490{
491public:
492 explicit ModifyLock( ModelDependentComponent& _component )
493 :m_rComponent( _component )
494 {
496 }
497
499 {
501 }
502
503private:
505};
506
513{
514private:
515 // to avoid deadlocks, lock SolarMutex
517
518public:
527 explicit ModelMethodGuard( const ModelDependentComponent& _component )
528 {
529 _component.checkDisposed();
530 }
531
532 void clear()
533 {
534 // note: this only releases *once* so may still be locked
535 m_SolarGuard.clear();
536 }
537
538 void reset()
539 {
540 m_SolarGuard.reset();
541 }
542};
543
544} // namespace dbaccess
545
546/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
a small base class for UNO components whose functionality depends on an ODatabaseModelImpl
Definition: ModelImpl.hxx:452
void checkDisposed() const
checks whether the component is already disposed, throws a DisposedException if so
Definition: ModelImpl.hxx:472
::rtl::Reference< ODatabaseModelImpl > m_pImpl
Definition: ModelImpl.hxx:454
virtual css::uno::Reference< css::uno::XInterface > getThis() const =0
returns the component itself
ModelDependentComponent(::rtl::Reference< ODatabaseModelImpl > _model)
Definition: ModelImpl.cxx:1423
a guard for public methods of objects dependent on an ODatabaseModelImpl instance
Definition: ModelImpl.hxx:513
ModelMethodGuard(const ModelDependentComponent &_component)
constructs the guard
Definition: ModelImpl.hxx:527
SolarMutexResettableGuard m_SolarGuard
Definition: ModelImpl.hxx:516
ModelDependentComponent & m_rComponent
Definition: ModelImpl.hxx:504
ModifyLock(ModelDependentComponent &_component)
Definition: ModelImpl.hxx:492
TContentPtr & getObjectContainer(const ObjectType _eType)
retrieves the requested container of objects (forms/reports/tables/queries)
Definition: ModelImpl.cxx:1106
void setResource(const OUString &_rURL, const css::uno::Sequence< css::beans::PropertyValue > &_rArgs)
Definition: ModelImpl.cxx:702
void setImposedMacroExecMode(const sal_Int16 _nMacroMode)
Definition: ModelImpl.hxx:409
virtual bool hasTrustedScriptingSignature(bool bAllowUIToAddAuthor) override
Definition: ModelImpl.cxx:1363
bool m_bDocumentInitialized
true if and only if a database document existed previously (though meanwhile disposed),...
Definition: ModelImpl.hxx:152
SharedStorage m_xDocumentStorage
Definition: ModelImpl.hxx:134
virtual void storageIsModified() override
Definition: ModelImpl.cxx:1418
static const AsciiPropertyValue * getDefaultDataSourceSettings()
returns all known data source settings, including their default values
Definition: ModelImpl.cxx:1033
const css::uno::Reference< css::uno::XComponentContext > m_aContext
Definition: ModelImpl.hxx:168
ODatabaseModelImpl(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, ODatabaseContext &_pDBContext)
::comphelper::NamedValueCollection stripLoadArguments(const ::comphelper::NamedValueCollection &_rArguments)
Definition: ModelImpl.cxx:726
OUString m_sDocFileLocation
the URL the document was loaded from
Definition: ModelImpl.hxx:141
virtual bool macroCallsSeenWhileLoading() const override
Definition: ModelImpl.cxx:1348
css::uno::WeakReference< css::sdbc::XDataSource > m_xDataSource
Definition: ModelImpl.hxx:124
o3tl::enumarray< ObjectType, TContentPtr > m_aContainer
Definition: ModelImpl.hxx:127
css::uno::Reference< css::awt::XWindow > m_xDialogParent
Definition: ModelImpl.hxx:198
bool commitEmbeddedStorage(bool _bPreventRootCommits=false)
stores the embedded storage ("database")
Definition: ModelImpl.cxx:831
const css::uno::Reference< css::util::XNumberFormatsSupplier > & getNumberFormatsSupplier()
Definition: ModelImpl.cxx:684
css::uno::Sequence< OUString > m_aTableFilter
Definition: ModelImpl.hxx:192
const ::comphelper::NamedValueCollection & getMediaDescriptor() const
Definition: ModelImpl.hxx:255
virtual sal_Int16 getCurrentMacroExecMode() const override
Definition: ModelImpl.cxx:1290
css::uno::Reference< css::document::XDocumentSubStorageSupplier > getDocumentSubStorageSupplier()
Definition: ModelImpl.cxx:826
::comphelper::NamedValueCollection m_aMediaDescriptor
Definition: ModelImpl.hxx:139
css::uno::WeakReference< css::container::XNameAccess > m_xTableDefinitions
Definition: ModelImpl.hxx:172
css::uno::Reference< css::embed::XStorage > const & getRootStorage() const
Definition: ModelImpl.hxx:288
css::uno::Sequence< OUString > m_aTableTypeFilter
Definition: ModelImpl.hxx:193
css::uno::Reference< css::embed::XStorage > const & getOrCreateRootStorage()
Definition: ModelImpl.cxx:755
void modelIsDisposing(const bool _wasInitialized, ResetModelAccess)
resets the model to NULL
Definition: ModelImpl.cxx:813
rtl::Reference< DocumentStorageAccess > m_pStorageAccess
Definition: ModelImpl.hxx:126
css::uno::Reference< css::lang::XEventListener > m_xSharedConnectionManager
Definition: ModelImpl.hxx:196
css::uno::Reference< css::script::XStorageBasedLibraryContainer > m_xDialogLibraries
Definition: ModelImpl.hxx:132
::sfx2::DocumentMacroMode m_aMacroMode
Definition: ModelImpl.hxx:128
ODatabaseContext & m_rDBContext
Definition: ModelImpl.hxx:136
sal_Int16 getImposedMacroExecMode() const
returns the macro mode imposed by an external instance, which passed it to attachResource
Definition: ModelImpl.hxx:405
css::uno::Reference< css::beans::XPropertyBag > m_xSettings
Definition: ModelImpl.hxx:191
SignatureState m_nScriptingSignatureState
Definition: ModelImpl.hxx:164
::std::optional< EmbeddedMacros > m_aEmbeddedMacros
do we have any object (forms/reports) which contains macros?
Definition: ModelImpl.hxx:146
void storeLibraryContainersTo(const css::uno::Reference< css::embed::XStorage > &_rxToRootStorage)
lets our library containers store themself into the given root storage
Definition: ModelImpl.cxx:1171
void impl_switchToLogicalURL(const OUString &i_rDocumentURL)
switches to the given document URL, which denotes the logical URL of the document,...
Definition: ModelImpl.cxx:1252
css::uno::Reference< css::embed::XStorage > getStorage(const ObjectType _eType)
Definition: ModelImpl.cxx:1027
const OUString & getDocFileLocation() const
Definition: ModelImpl.hxx:242
css::uno::Sequence< css::beans::PropertyValue > m_aLayoutInformation
Definition: ModelImpl.hxx:182
OUString m_sDocumentURL
the URL which the document should report as its URL
Definition: ModelImpl.hxx:162
void resetMacroExecutionMode()
resets our macro execute mode, so next time the checkMacrosOnLoading is called, it will behave as if ...
Definition: ModelImpl.cxx:1131
::rtl::Reference< ::sfx2::DocumentStorageModifyListener > m_pStorageModifyListener
Definition: ModelImpl.hxx:135
OWeakConnectionArray m_aConnections
Definition: ModelImpl.hxx:167
virtual SignatureState getScriptingSignatureState() override
Definition: ModelImpl.cxx:1358
weld::Window * GetFrameWeld()
Definition: datasource.cxx:580
void commitRootStorage()
commits our storage
Definition: ModelImpl.cxx:747
ODatabaseModelImpl(OUString _sRegistrationName, const css::uno::Reference< css::uno::XComponentContext > &_rxContext, ODatabaseContext &_rDBContext)
bool m_bModificationLock
true if setting the Modified flag of the document is currently locked
Definition: ModelImpl.hxx:149
bool commitStorageIfWriteable_ignoreErrors(const css::uno::Reference< css::embed::XStorage > &_rxStorage)
commits a given storage if it's not readonly, ignoring (but asserting) all errors
Definition: ModelImpl.cxx:836
DocumentStorageAccess * getDocumentStorageAccess()
Definition: ModelImpl.cxx:804
const OUString & getURL() const
Definition: ModelImpl.hxx:241
void setModified(bool bModified)
Definition: ModelImpl.cxx:933
css::uno::Reference< css::script::XStorageBasedLibraryContainer > getLibraryContainer(bool _bScript)
ensures that ->m_xBasicLibraries resp.
Definition: ModelImpl.cxx:1136
void disposing(const css::lang::EventObject &Source)
Definition: ModelImpl.cxx:574
css::uno::WeakReference< css::frame::XModel > m_xModel
Definition: ModelImpl.hxx:123
void setDocFileLocation(const OUString &i_rLoadedFrom)
Definition: ModelImpl.cxx:696
OSharedConnectionManager * m_pSharedConnectionManager
Definition: ModelImpl.hxx:194
virtual css::uno::Reference< css::document::XEmbeddedScripts > getEmbeddedDocumentScripts() const override
Definition: ModelImpl.cxx:1353
css::uno::Reference< css::embed::XStorage > const & impl_switchToStorage_throw(const css::uno::Reference< css::embed::XStorage > &_rxNewRootStorage)
Definition: ModelImpl.cxx:1231
bool hadInitializedDocument() const
Definition: ModelImpl.hxx:317
bool isEmbeddedDatabase() const
determines whether the database document has an embedded data storage
Definition: ModelImpl.hxx:205
DocumentEventsData m_aDocumentEvents
Definition: ModelImpl.hxx:137
css::uno::WeakReference< css::container::XNameAccess > m_xCommandDefinitions
Definition: ModelImpl.hxx:171
void commitStorages()
commits all sub storages
Definition: ModelImpl.cxx:1022
css::uno::Reference< css::lang::XSingleServiceFactory > createStorageFactory() const
creates a ->css::embed::StorageFactory
Definition: ModelImpl.cxx:742
css::uno::Reference< css::sdbc::XDataSource > getOrCreateDataSource()
returns the data source.
Definition: ModelImpl.cxx:952
bool adjustMacroMode_AutoReject()
adjusts our document's macro execution mode, without using any UI, assuming the user would reject exe...
Definition: ModelImpl.cxx:1119
oslInterlockedCount m_refCount
Definition: ModelImpl.hxx:143
virtual bool documentStorageHasMacros() const override
Definition: ModelImpl.cxx:1342
virtual OUString getDocumentLocation() const override
Definition: ModelImpl.cxx:1309
bool checkMacrosOnLoading()
checks our document's macro execution mode, using the interaction handler as supplied with our load a...
Definition: ModelImpl.cxx:1124
virtual void setCurrentMacroExecMode(sal_uInt16) override
Definition: ModelImpl.cxx:1304
css::uno::Reference< css::frame::XModel > getModel_noCreate() const
returns the model, if there already exists one
Definition: ModelImpl.cxx:963
css::uno::Reference< css::embed::XStorage > switchToStorage(const css::uno::Reference< css::embed::XStorage > &_rxNewRootStorage)
rebases the document to the given storage
Definition: ModelImpl.cxx:1180
DocumentEventsData & getDocumentEvents()
Definition: ModelImpl.hxx:252
css::uno::Reference< css::script::XStorageBasedLibraryContainer > m_xBasicLibraries
Definition: ModelImpl.hxx:131
EmbeddedMacros determineEmbeddedMacros()
determines which kind of embedded macros are present in the document
Definition: ModelImpl.cxx:1320
css::uno::Reference< css::util::XNumberFormatsSupplier > m_xNumberFormatsSupplier
Definition: ModelImpl.hxx:175
css::uno::Reference< css::frame::XModel > createNewModel_deliverOwnership()
returns a new ->ODatabaseDocument
Definition: ModelImpl.cxx:968
static OUString getObjectContainerStorageName(const ObjectType _eType)
returns the name of the storage which is used to stored objects of the given type,...
Definition: ModelImpl.cxx:1285
static bool objectHasMacros(const css::uno::Reference< css::embed::XStorage > &_rxContainerStorage, const OUString &_rPersistentName)
determines whether a given object storage contains macros
Definition: ModelImpl.cxx:539
const css::uno::Reference< INTERFACE > & getTyped() const
NONE
std::shared_ptr< OContentHelper_Impl > TContentPtr
std::vector< css::uno::WeakReference< css::sdbc::XConnection > > OWeakConnectionArray
Definition: ModelImpl.hxx:52
std::map< OUString, css::uno::Sequence< css::beans::PropertyValue > > DocumentEventsData
::utl::SharedUNOComponent< css::embed::XStorage > SharedStorage
Definition: ModelImpl.hxx:89
ObjectType
ValueType
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
SignatureState
const css::uno::Type & ValueType
Definition: ModelImpl.hxx:61
AsciiPropertyValue(const char *_pAsciiName, const css::uno::Any &_rDefaultValue)
Definition: ModelImpl.hxx:70
AsciiPropertyValue(const char *_pAsciiName, const css::uno::Type &_rValeType)
Definition: ModelImpl.hxx:78