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 .
20 #ifndef INCLUDED_DBACCESS_SOURCE_CORE_INC_DEFINITIONCONTAINER_HXX
21 #define INCLUDED_DBACCESS_SOURCE_CORE_INC_DEFINITIONCONTAINER_HXX
23 #include <sal/config.h>
28 #include <cppuhelper/interfacecontainer.hxx>
29 #include <cppuhelper/implbase7.hxx>
30 #include <osl/mutex.hxx>
31 #include <com/sun/star/container/XChild.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/container/XContainer.hpp>
34 #include <com/sun/star/container/XEnumerationAccess.hpp>
35 #include <com/sun/star/container/XIndexAccess.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/DisposedException.hpp>
38 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
39 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
40 #include <com/sun/star/container/XContainerApproveBroadcaster.hpp>
41 #include "ContentHelper.hxx"
42 #include "containerapprove.hxx"
43 #include <comphelper/uno3.hxx>
44 #include <rtl/ref.hxx>
45 #include "apitools.hxx"
50 class ODefinitionContainer_Impl
: public OContentHelper_Impl
53 typedef ::std::map
< OUString
, TContentPtr
> NamedDefinitions
;
54 typedef NamedDefinitions::iterator iterator
;
55 typedef NamedDefinitions::const_iterator const_iterator
;
58 NamedDefinitions m_aDefinitions
;
61 inline size_t size() const { return m_aDefinitions
.size(); }
63 inline const_iterator
begin() const { return m_aDefinitions
.begin(); }
64 inline const_iterator
end() const { return m_aDefinitions
.end(); }
66 inline const_iterator
find( const OUString
& _rName
) const { return m_aDefinitions
.find( _rName
); }
67 const_iterator
find( TContentPtr _pDefinition
) const;
69 inline void erase( const OUString
& _rName
) { m_aDefinitions
.erase( _rName
); }
70 void erase( TContentPtr _pDefinition
);
72 inline void insert( const OUString
& _rName
, TContentPtr _pDefinition
)
74 m_aDefinitions
.insert( NamedDefinitions::value_type( _rName
, _pDefinition
) );
78 iterator
find( TContentPtr _pDefinition
);
79 // (for the moment, this is private. Make it public if needed. If really needed.)
82 // ODefinitionContainer - base class of collections of database definition
84 typedef ::cppu::ImplHelper7
< ::com::sun::star::container::XIndexAccess
85 , ::com::sun::star::container::XNameContainer
86 , ::com::sun::star::container::XEnumerationAccess
87 , ::com::sun::star::container::XContainer
88 , ::com::sun::star::container::XContainerApproveBroadcaster
89 , ::com::sun::star::beans::XPropertyChangeListener
90 , ::com::sun::star::beans::XVetoableChangeListener
91 > ODefinitionContainer_Base
;
93 class ODefinitionContainer
94 :public OContentHelper
95 ,public ODefinitionContainer_Base
98 typedef std::map
< OUString
, ::com::sun::star::uno::WeakReference
< ::com::sun::star::ucb::XContent
> > Documents
;
99 typedef std::vector
<Documents::iterator
> DocumentsIndexAccess
;
101 enum ContainerOperation
115 PContainerApprove m_pElementApproval
;
118 // we can't just hold a vector of XContentRefs, as after initialization they're all empty
119 // cause we load them only on access
120 DocumentsIndexAccess m_aDocuments
; // for a efficient index access
121 Documents m_aDocumentMap
; // for a efficient name access
123 ::cppu::OInterfaceContainerHelper
125 ::cppu::OInterfaceContainerHelper
126 m_aContainerListeners
;
128 bool m_bInPropertyChange
;
132 /** Additionally to our own approvals which new elements must pass, derived classes
133 can specifiy an additional approval instance here.
135 Every time a new element is inserted into the container (or an element is replaced
136 with a new one), this new element must pass our own internal approval, plus the approval
139 void setElementApproval( PContainerApprove _pElementApproval
) { m_pElementApproval
= _pElementApproval
; }
140 PContainerApprove
getElementApproval() const { return m_pElementApproval
; }
143 virtual ~ODefinitionContainer();
145 inline const ODefinitionContainer_Impl
& getDefinitions() const
147 return dynamic_cast< const ODefinitionContainer_Impl
& >( *m_pImpl
.get() );
150 inline ODefinitionContainer_Impl
& getDefinitions()
152 return dynamic_cast< ODefinitionContainer_Impl
& >( *m_pImpl
.get() );
155 /** constructs the container.
157 ODefinitionContainer(
158 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _xORB
159 , const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& _xParentContainer
160 , const TContentPtr
& _pImpl
161 , bool _bCheckSlash
= true
164 // ::com::sun::star::uno::XInterface
165 DECLARE_XINTERFACE( )
167 virtual css::uno::Sequence
<css::uno::Type
> SAL_CALL
getTypes()
168 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
169 virtual css::uno::Sequence
<sal_Int8
> SAL_CALL
getImplementationId()
170 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
172 // ::com::sun::star::lang::XServiceInfo
173 virtual OUString SAL_CALL
getImplementationName( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
174 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
176 // ::com::sun::star::container::XElementAccess
177 virtual ::com::sun::star::uno::Type SAL_CALL
getElementType( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
178 virtual sal_Bool SAL_CALL
hasElements( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
180 // ::com::sun::star::container::XEnumerationAccess
181 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XEnumeration
> SAL_CALL
createEnumeration( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
183 // ::com::sun::star::container::XIndexAccess
184 virtual sal_Int32 SAL_CALL
getCount( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
185 virtual ::com::sun::star::uno::Any SAL_CALL
getByIndex( sal_Int32 _nIndex
) throw(::com::sun::star::lang::IndexOutOfBoundsException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
187 // ::com::sun::star::container::XNameContainer
188 virtual void SAL_CALL
insertByName( const OUString
& _rName
, const ::com::sun::star::uno::Any
& aElement
) throw(::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::container::ElementExistException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
189 virtual void SAL_CALL
removeByName( const OUString
& _rName
) throw(::com::sun::star::container::NoSuchElementException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
191 // ::com::sun::star::container::XNameReplace
192 virtual void SAL_CALL
replaceByName( const OUString
& _rName
, const ::com::sun::star::uno::Any
& aElement
) throw(::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::container::NoSuchElementException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
194 // ::com::sun::star::container::XNameAccess
195 virtual ::com::sun::star::uno::Any SAL_CALL
getByName( const OUString
& aName
) throw(::com::sun::star::container::NoSuchElementException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
196 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
197 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
199 // ::com::sun::star::container::XContainer
200 virtual void SAL_CALL
addContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& xListener
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
201 virtual void SAL_CALL
removeContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& xListener
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
203 // XContainerApproveBroadcaster
204 virtual void SAL_CALL
addContainerApproveListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerApproveListener
>& Listener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
205 virtual void SAL_CALL
removeContainerApproveListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerApproveListener
>& Listener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
207 // ::com::sun::star::lang::XEventListener
208 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
210 // XPropertyChangeListener
211 virtual void SAL_CALL
propertyChange( const ::com::sun::star::beans::PropertyChangeEvent
& evt
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
212 // XVetoableChangeListener
213 virtual void SAL_CALL
vetoableChange( const ::com::sun::star::beans::PropertyChangeEvent
& aEvent
) throw (::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
217 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
219 /** create a object from it's persistent data within the configuration. To be overwritten by derived classes.
220 @param _rName the name the object has within the container
221 @return the newly created object or an empty reference if something went wrong
223 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
> createObject(
224 const OUString
& _rName
) = 0;
226 /** get the object specified by the given name. If desired, the object will be read if not already done so.<BR>
227 @param _rName the object name
228 @param _bReadIfNecessary if sal_True, the object will be created if necessary
229 @return the property set interface of the object. Usually the return value is not NULL, but
230 if so, then the object could not be read from the configuration
231 @throws NoSuchElementException if there is no object with the given name.
234 ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>
235 implGetByName(const OUString
& _rName
, bool _bCreateIfNecessary
) throw (::com::sun::star::container::NoSuchElementException
);
237 /** quickly checks if there already is an element with a given name. No access to the configuration occurs, i.e.
238 if there is such an object which is not already loaded, it won't be loaded now.
239 @param _rName the object name to check
240 @return sal_True if there already exists such an object
242 virtual bool checkExistence(const OUString
& _rName
);
244 /** append a new object to the container. No plausibility checks are done, e.g. if the object is non-NULL or
245 if the name is already used by another object or anything like this. This method is for derived classes
246 which may support different methods to create and/or append objects, and don't want to deal with the
247 internal structures of this class.<BR>
248 The old component will not be disposed, this is the callers responsibility, too.
249 @param _rName the name of the new object
250 @param _rxNewObject the new object (not surprising, is it ?)
256 const OUString
& _rName
,
257 const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _rxNewObject
260 /** remove all references to an object from the container. No plausibility checks are done, e.g. whether
261 or not there exists an object with the given name. This is the responsibility of the caller.<BR>
262 Additionally the node for the given object will be removed from the registry (including all sub nodes).<BR>
263 The old component will not be disposed, this is the callers responsibility, too.
264 @param _rName the objects name
268 void implRemove(const OUString
& _rName
);
270 /** remove a object in the container. No plausibility checks are done, e.g. whether
271 or not there exists an object with the given name or the object is non-NULL. This is the responsibility of the caller.<BR>
272 Additionally all object-related information within the registry will be deleted. The new object config node,
273 where the caller may want to store the new objects information, is returned.<BR>
274 The old component will not be disposed, this is the callers responsibility, too.
275 @param _rName the objects name
276 @param _rxNewObject the new object
277 @param _rNewObjectNode the configuration node where the new object may be stored
282 const OUString
& _rName
,
283 const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _rxNewObject
286 /** notifies our container/approve listeners
289 ::osl::ResettableMutexGuard
& _rGuard
,
290 const OUString
& _rName
,
291 const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _xNewElement
,
292 const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& xOldElement
,
293 ContainerOperation _eOperation
,
297 inline SAL_CALL
operator ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> () const
299 return const_cast< XContainer
* >( static_cast< const XContainer
* >( this ) );
303 void addObjectListener(const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _xNewObject
);
304 void removeObjectListener(const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _xNewObject
);
306 /** approve that the object given may be inserted into the container.
307 Should be overridden by derived classes,
308 the default implementation just checks the object to be non-void.
310 @throws IllegalArgumentException
311 if the name or the object are invalid
312 @throws ElementExistException
313 if the object already exists in the container, or another object with the same name
315 @throws WrappedTargetException
316 if another error occurs which prevents insertion of the object into the container
318 void approveNewObject(
319 const OUString
& _sName
,
320 const ::com::sun::star::uno::Reference
< ::com::sun::star::ucb::XContent
>& _rxObject
323 inline bool impl_haveAnyListeners_nothrow() const
325 return ( m_aContainerListeners
.getLength() > 0 ) || ( m_aApproveListeners
.getLength() > 0 );
329 } // namespace dbaccess
331 #endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_DEFINITIONCONTAINER_HXX
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */