1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/config.h>
27 #include <cppuhelper/implbase7.hxx>
28 #include <osl/mutex.hxx>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/container/XContainer.hpp>
31 #include <com/sun/star/container/XEnumerationAccess.hpp>
32 #include <com/sun/star/container/XIndexAccess.hpp>
33 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
34 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
35 #include <com/sun/star/container/XContainerApproveBroadcaster.hpp>
36 #include "ContentHelper.hxx"
37 #include "containerapprove.hxx"
38 #include <comphelper/uno3.hxx>
39 #include <comphelper/interfacecontainer2.hxx>
44 class ODefinitionContainer_Impl
: public OContentHelper_Impl
47 typedef std::map
< OUString
, TContentPtr
> NamedDefinitions
;
48 typedef NamedDefinitions::iterator iterator
;
49 typedef NamedDefinitions::const_iterator const_iterator
;
52 NamedDefinitions m_aDefinitions
;
55 size_t size() const { return m_aDefinitions
.size(); }
57 const_iterator
begin() const { return m_aDefinitions
.begin(); }
58 const_iterator
end() const { return m_aDefinitions
.end(); }
60 const_iterator
find( const OUString
& _rName
) const { return m_aDefinitions
.find( _rName
); }
61 const_iterator
find( const TContentPtr
& _pDefinition
) const;
63 void erase( const OUString
& _rName
) { m_aDefinitions
.erase( _rName
); }
64 void erase( const TContentPtr
& _pDefinition
);
66 void insert( const OUString
& _rName
, const TContentPtr
& _pDefinition
)
68 m_aDefinitions
.emplace( _rName
, _pDefinition
);
72 iterator
find( const TContentPtr
& _pDefinition
);
73 // (for the moment, this is private. Make it public if needed. If really needed.)
76 // ODefinitionContainer - base class of collections of database definition
78 typedef ::cppu::ImplHelper7
< css::container::XIndexAccess
79 , css::container::XNameContainer
80 , css::container::XEnumerationAccess
81 , css::container::XContainer
82 , css::container::XContainerApproveBroadcaster
83 , css::beans::XPropertyChangeListener
84 , css::beans::XVetoableChangeListener
85 > ODefinitionContainer_Base
;
87 class ODefinitionContainer
88 :public OContentHelper
89 ,public ODefinitionContainer_Base
92 typedef std::map
< OUString
, css::uno::WeakReference
< css::ucb::XContent
> > Documents
;
94 enum ContainerOperation
108 PContainerApprove m_pElementApproval
;
111 // we can't just hold a vector of XContentRefs, as after initialization they're all empty
112 // cause we load them only on access
113 std::vector
<Documents::iterator
>
114 m_aDocuments
; // for an efficient index access
115 Documents m_aDocumentMap
; // for an efficient name access
117 ::comphelper::OInterfaceContainerHelper2
119 ::comphelper::OInterfaceContainerHelper2
120 m_aContainerListeners
;
122 bool m_bInPropertyChange
;
126 /** Additionally to our own approvals which new elements must pass, derived classes
127 can specify an additional approval instance here.
129 Every time a new element is inserted into the container (or an element is replaced
130 with a new one), this new element must pass our own internal approval, plus the approval
133 void setElementApproval(const PContainerApprove
& _pElementApproval
) { m_pElementApproval
= _pElementApproval
; }
134 const PContainerApprove
& getElementApproval() const { return m_pElementApproval
; }
137 virtual ~ODefinitionContainer() override
;
139 const ODefinitionContainer_Impl
& getDefinitions() const
141 return dynamic_cast< const ODefinitionContainer_Impl
& >( *m_pImpl
);
144 ODefinitionContainer_Impl
& getDefinitions()
146 return dynamic_cast< ODefinitionContainer_Impl
& >( *m_pImpl
);
149 /** constructs the container.
151 ODefinitionContainer(
152 const css::uno::Reference
< css::uno::XComponentContext
>& _xORB
153 , const css::uno::Reference
< css::uno::XInterface
>& _xParentContainer
154 , const TContentPtr
& _pImpl
155 , bool _bCheckSlash
= true
158 // css::uno::XInterface
159 DECLARE_XINTERFACE( )
161 virtual css::uno::Sequence
<css::uno::Type
> SAL_CALL
getTypes() override
;
162 virtual css::uno::Sequence
<sal_Int8
> SAL_CALL
getImplementationId() override
;
164 // css::lang::XServiceInfo
165 virtual OUString SAL_CALL
getImplementationName( ) override
;
166 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
;
168 // css::container::XElementAccess
169 virtual css::uno::Type SAL_CALL
getElementType( ) override
;
170 virtual sal_Bool SAL_CALL
hasElements( ) override
;
172 // css::container::XEnumerationAccess
173 virtual css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
createEnumeration( ) override
;
175 // css::container::XIndexAccess
176 virtual sal_Int32 SAL_CALL
getCount( ) override
;
177 virtual css::uno::Any SAL_CALL
getByIndex( sal_Int32 _nIndex
) override
;
179 // css::container::XNameContainer
180 virtual void SAL_CALL
insertByName( const OUString
& _rName
, const css::uno::Any
& aElement
) override
;
181 virtual void SAL_CALL
removeByName( const OUString
& _rName
) override
;
183 // css::container::XNameReplace
184 virtual void SAL_CALL
replaceByName( const OUString
& _rName
, const css::uno::Any
& aElement
) override
;
186 // css::container::XNameAccess
187 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
188 virtual css::uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
;
189 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
191 // css::container::XContainer
192 virtual void SAL_CALL
addContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& xListener
) override
;
193 virtual void SAL_CALL
removeContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& xListener
) override
;
195 // XContainerApproveBroadcaster
196 virtual void SAL_CALL
addContainerApproveListener( const css::uno::Reference
< css::container::XContainerApproveListener
>& Listener
) override
;
197 virtual void SAL_CALL
removeContainerApproveListener( const css::uno::Reference
< css::container::XContainerApproveListener
>& Listener
) override
;
199 // css::lang::XEventListener
200 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
202 // XPropertyChangeListener
203 virtual void SAL_CALL
propertyChange( const css::beans::PropertyChangeEvent
& evt
) override
;
204 // XVetoableChangeListener
205 virtual void SAL_CALL
vetoableChange( const css::beans::PropertyChangeEvent
& aEvent
) override
;
209 virtual void SAL_CALL
disposing() override
;
211 /** create an object from its persistent data within the configuration. To be overwritten by derived classes.
212 @param _rName the name the object has within the container
213 @return the newly created object or an empty reference if something went wrong
215 virtual css::uno::Reference
< css::ucb::XContent
> createObject(
216 const OUString
& _rName
) = 0;
218 /** get the object specified by the given name. If desired, the object will be read if not already done so.<BR>
219 @param _rName the object name
220 @param _bReadIfNecessary if sal_True, the object will be created if necessary
221 @return the property set interface of the object. Usually the return value is not NULL, but
222 if so, then the object could not be read from the configuration
223 @throws NoSuchElementException if there is no object with the given name.
226 css::uno::Reference
< css::ucb::XContent
>
227 implGetByName(const OUString
& _rName
, bool _bCreateIfNecessary
);
229 /** quickly checks if there already is an element with a given name. No access to the configuration occurs, i.e.
230 if there is such an object which is not already loaded, it won't be loaded now.
231 @param _rName the object name to check
232 @return sal_True if there already exists such an object
234 virtual bool checkExistence(const OUString
& _rName
);
236 /** append a new object to the container. No plausibility checks are done, e.g. if the object is non-NULL or
237 if the name is already used by another object or anything like this. This method is for derived classes
238 which may support different methods to create and/or append objects, and don't want to deal with the
239 internal structures of this class.<BR>
240 The old component will not be disposed, this is the callers responsibility, too.
241 @param _rName the name of the new object
242 @param _rxNewObject the new object (not surprising, is it ?)
248 const OUString
& _rName
,
249 const css::uno::Reference
< css::ucb::XContent
>& _rxNewObject
252 /** remove all references to an object from the container. No plausibility checks are done, e.g. whether
253 or not there exists an object with the given name. This is the responsibility of the caller.<BR>
254 Additionally the node for the given object will be removed from the registry (including all sub nodes).<BR>
255 The old component will not be disposed, this is the callers responsibility, too.
256 @param _rName the objects name
260 void implRemove(const OUString
& _rName
);
262 /** remove an object in the container. No plausibility checks are done, e.g. whether
263 or not there exists an object with the given name or the object is non-NULL. This is the responsibility of the caller.<BR>
264 Additionally all object-related information within the registry will be deleted. The new object config node,
265 where the caller may want to store the new objects information, is returned.<BR>
266 The old component will not be disposed, this is the callers responsibility, too.
267 @param _rName the objects name
268 @param _rxNewObject the new object
269 @param _rNewObjectNode the configuration node where the new object may be stored
274 const OUString
& _rName
,
275 const css::uno::Reference
< css::ucb::XContent
>& _rxNewObject
278 /** notifies our container/approve listeners
281 ::osl::ResettableMutexGuard
& _rGuard
,
282 const OUString
& _rName
,
283 const css::uno::Reference
< css::ucb::XContent
>& _xNewElement
,
284 const css::uno::Reference
< css::ucb::XContent
>& xOldElement
,
285 ContainerOperation _eOperation
,
289 operator css::uno::Reference
< css::uno::XInterface
> () const
291 return const_cast< XContainer
* >( static_cast< const XContainer
* >( this ) );
295 void addObjectListener(const css::uno::Reference
< css::ucb::XContent
>& _xNewObject
);
296 void removeObjectListener(const css::uno::Reference
< css::ucb::XContent
>& _xNewObject
);
298 /** approve that the object given may be inserted into the container.
299 Should be overridden by derived classes,
300 the default implementation just checks the object to be non-void.
302 @throws IllegalArgumentException
303 if the name or the object are invalid
304 @throws ElementExistException
305 if the object already exists in the container, or another object with the same name
307 @throws WrappedTargetException
308 if another error occurs which prevents insertion of the object into the container
310 void approveNewObject(
311 const OUString
& _sName
,
312 const css::uno::Reference
< css::ucb::XContent
>& _rxObject
315 bool impl_haveAnyListeners_nothrow() const
317 return ( m_aContainerListeners
.getLength() > 0 ) || ( m_aApproveListeners
.getLength() > 0 );
321 } // namespace dbaccess
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */