Branch data Line data Source code
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 : : #ifndef _DBA_CORE_DEFINITIONCONTAINER_HXX_
21 : : #define _DBA_CORE_DEFINITIONCONTAINER_HXX_
22 : :
23 : : #include <cppuhelper/interfacecontainer.hxx>
24 : : #include <cppuhelper/implbase7.hxx>
25 : : #include <comphelper/stl_types.hxx>
26 : : #include <osl/mutex.hxx>
27 : : #include <com/sun/star/container/XChild.hpp>
28 : : #include <com/sun/star/container/XNameContainer.hpp>
29 : : #include <com/sun/star/container/XContainer.hpp>
30 : : #include <com/sun/star/container/XEnumerationAccess.hpp>
31 : : #include <com/sun/star/container/XIndexAccess.hpp>
32 : : #include <com/sun/star/lang/XServiceInfo.hpp>
33 : : #include <com/sun/star/lang/DisposedException.hpp>
34 : : #include <com/sun/star/beans/XPropertyChangeListener.hpp>
35 : : #include <com/sun/star/beans/XVetoableChangeListener.hpp>
36 : : #include <com/sun/star/container/XContainerApproveBroadcaster.hpp>
37 : : #include "ContentHelper.hxx"
38 : : #include "containerapprove.hxx"
39 : : #include <comphelper/uno3.hxx>
40 : : #include <rtl/ref.hxx>
41 : : #include "apitools.hxx"
42 : :
43 : : namespace dbaccess
44 : : {
45 : :
46 [ - + ][ + - ]: 2388 : class ODefinitionContainer_Impl : public OContentHelper_Impl
47 : : {
48 : : public:
49 : : typedef ::std::map< ::rtl::OUString, TContentPtr > NamedDefinitions;
50 : : typedef NamedDefinitions::iterator iterator;
51 : : typedef NamedDefinitions::const_iterator const_iterator;
52 : :
53 : : private:
54 : : NamedDefinitions m_aDefinitions;
55 : :
56 : : public:
57 : 0 : inline size_t size() const { return m_aDefinitions.size(); }
58 : :
59 : 1408 : inline const_iterator begin() const { return m_aDefinitions.begin(); }
60 : 1568 : inline const_iterator end() const { return m_aDefinitions.end(); }
61 : :
62 : 92 : inline const_iterator find( const ::rtl::OUString& _rName ) const { return m_aDefinitions.find( _rName ); }
63 : : const_iterator find( TContentPtr _pDefinition ) const;
64 : :
65 : 0 : inline void erase( const ::rtl::OUString& _rName ) { m_aDefinitions.erase( _rName ); }
66 : : void erase( TContentPtr _pDefinition );
67 : :
68 : 40 : inline void insert( const ::rtl::OUString& _rName, TContentPtr _pDefinition )
69 : : {
70 [ + - ]: 40 : m_aDefinitions.insert( NamedDefinitions::value_type( _rName, _pDefinition ) );
71 : 40 : }
72 : :
73 : : private:
74 : : iterator find( TContentPtr _pDefinition );
75 : : // (for the moment, this is private. Make it public if needed. If really needed.)
76 : : };
77 : :
78 : : //==========================================================================
79 : : //= ODefinitionContainer - base class of collections of database definition
80 : : //= documents
81 : : //==========================================================================
82 : : typedef ::cppu::ImplHelper7 < ::com::sun::star::container::XIndexAccess
83 : : , ::com::sun::star::container::XNameContainer
84 : : , ::com::sun::star::container::XEnumerationAccess
85 : : , ::com::sun::star::container::XContainer
86 : : , ::com::sun::star::container::XContainerApproveBroadcaster
87 : : , ::com::sun::star::beans::XPropertyChangeListener
88 : : , ::com::sun::star::beans::XVetoableChangeListener
89 : : > ODefinitionContainer_Base;
90 : :
91 : : class ODefinitionContainer
92 : : :public OContentHelper
93 : : ,public ODefinitionContainer_Base
94 : : {
95 : : protected:
96 : : DECLARE_STL_USTRINGACCESS_MAP(::com::sun::star::uno::WeakReference< ::com::sun::star::ucb::XContent >, Documents);
97 : : DECLARE_STL_VECTOR(Documents::iterator, DocumentsIndexAccess);
98 : :
99 : : enum ContainerOperation
100 : : {
101 : : E_REPLACED,
102 : : E_REMOVED,
103 : : E_INSERTED
104 : : };
105 : :
106 : : enum ListenerType
107 : : {
108 : : ApproveListeners,
109 : : ContainerListemers
110 : : };
111 : :
112 : : private:
113 : : PContainerApprove m_pElementApproval;
114 : :
115 : : protected:
116 : : // we can't just hold a vector of XContentRefs, as after initialization they're all empty
117 : : // cause we load them only on access
118 : : DocumentsIndexAccess m_aDocuments; // for a efficient index access
119 : : Documents m_aDocumentMap; // for a efficient name access
120 : :
121 : : ::cppu::OInterfaceContainerHelper
122 : : m_aApproveListeners;
123 : : ::cppu::OInterfaceContainerHelper
124 : : m_aContainerListeners;
125 : :
126 : : sal_Bool m_bInPropertyChange;
127 : : bool m_bCheckSlash;
128 : :
129 : : protected:
130 : : /** Additionally to our own approvals which new elements must pass, derived classes
131 : : can specifiy an additional approval instance here.
132 : :
133 : : Every time a new element is inserted into the container (or an element is replaced
134 : : with a new one), this new element must pass our own internal approval, plus the approval
135 : : given here.
136 : : */
137 : 404 : void setElementApproval( PContainerApprove _pElementApproval ) { m_pElementApproval = _pElementApproval; }
138 : 0 : PContainerApprove getElementApproval() const { return m_pElementApproval; }
139 : :
140 : : protected:
141 : : virtual ~ODefinitionContainer();
142 : :
143 : 40 : inline const ODefinitionContainer_Impl& getDefinitions() const
144 : : {
145 [ + - ]: 40 : return dynamic_cast< const ODefinitionContainer_Impl& >( *m_pImpl.get() );
146 : : }
147 : :
148 : 1488 : inline ODefinitionContainer_Impl& getDefinitions()
149 : : {
150 [ + - ]: 1488 : return dynamic_cast< ODefinitionContainer_Impl& >( *m_pImpl.get() );
151 : : }
152 : : public:
153 : : /** constructs the container.
154 : : */
155 : : ODefinitionContainer(
156 : : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB
157 : : , const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xParentContainer
158 : : , const TContentPtr& _pImpl
159 : : , bool _bCheckSlash = true
160 : : );
161 : :
162 : : // ::com::sun::star::uno::XInterface
163 : : DECLARE_XINTERFACE( )
164 : : // com::sun::star::lang::XTypeProvider
165 : : DECLARE_TYPEPROVIDER( );
166 : :
167 : : // ::com::sun::star::lang::XServiceInfo
168 : : virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
169 : : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException);
170 : :
171 : : // ::com::sun::star::container::XElementAccess
172 : : virtual ::com::sun::star::uno::Type SAL_CALL getElementType( ) throw(::com::sun::star::uno::RuntimeException);
173 : : virtual sal_Bool SAL_CALL hasElements( ) throw(::com::sun::star::uno::RuntimeException);
174 : :
175 : : // ::com::sun::star::container::XEnumerationAccess
176 : : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createEnumeration( ) throw(::com::sun::star::uno::RuntimeException);
177 : :
178 : : // ::com::sun::star::container::XIndexAccess
179 : : virtual sal_Int32 SAL_CALL getCount( ) throw(::com::sun::star::uno::RuntimeException);
180 : : 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);
181 : :
182 : : // ::com::sun::star::container::XNameContainer
183 : : virtual void SAL_CALL insertByName( const ::rtl::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);
184 : : virtual void SAL_CALL removeByName( const ::rtl::OUString& _rName ) throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
185 : :
186 : : // ::com::sun::star::container::XNameReplace
187 : : virtual void SAL_CALL replaceByName( const ::rtl::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);
188 : :
189 : : // ::com::sun::star::container::XNameAccess
190 : : virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
191 : : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw(::com::sun::star::uno::RuntimeException);
192 : : virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(::com::sun::star::uno::RuntimeException);
193 : :
194 : : // ::com::sun::star::container::XContainer
195 : : virtual void SAL_CALL addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
196 : : virtual void SAL_CALL removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw(::com::sun::star::uno::RuntimeException);
197 : :
198 : : // XContainerApproveBroadcaster
199 : : virtual void SAL_CALL addContainerApproveListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerApproveListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
200 : : virtual void SAL_CALL removeContainerApproveListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerApproveListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
201 : :
202 : : // ::com::sun::star::lang::XEventListener
203 : : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
204 : :
205 : : // XPropertyChangeListener
206 : : virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException);
207 : : // XVetoableChangeListener
208 : : virtual void SAL_CALL vetoableChange( const ::com::sun::star::beans::PropertyChangeEvent& aEvent ) throw (::com::sun::star::beans::PropertyVetoException, ::com::sun::star::uno::RuntimeException);
209 : :
210 : : protected:
211 : : // helper
212 : : virtual void SAL_CALL disposing();
213 : :
214 : : /** create a object from it's persistent data within the configuration. To be overwritten by derived classes.
215 : : @param _rName the name the object has within the container
216 : : @return the newly created object or an empty reference if somthing went wrong
217 : : */
218 : : virtual ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent > createObject(
219 : : const ::rtl::OUString& _rName) = 0;
220 : :
221 : : /** get the object specified by the given name. If desired, the object will be read if not already done so.<BR>
222 : : @param _rName the object name
223 : : @param _bReadIfNeccessary if sal_True, the object will be created if necessary
224 : : @return the property set interface of the object. Usually the return value is not NULL, but
225 : : if so, then the object could not be read from the configuration
226 : : @throws NoSuchElementException if there is no object with the given name.
227 : : @see createObject
228 : : */
229 : : virtual ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >
230 : : implGetByName(const ::rtl::OUString& _rName, sal_Bool _bCreateIfNecessary) throw (::com::sun::star::container::NoSuchElementException);
231 : :
232 : : /** quickly checks if there already is an element with a given name. No access to the configuration occures, i.e.
233 : : if there is such an object which is not already loaded, it won't be loaded now.
234 : : @param _rName the object name to check
235 : : @return sal_True if there already exists such an object
236 : : */
237 : : virtual sal_Bool checkExistence(const ::rtl::OUString& _rName);
238 : :
239 : : /** append a new object to the container. No plausibility checks are done, e.g. if the object is non-NULL or
240 : : if the name is already used by another object or anything like this. This method is for derived classes
241 : : which may support different methods to create and/or append objects, and don't want to deal with the
242 : : internal structures of this class.<BR>
243 : : The old component will not be disposed, this is the callers responsibility, too.
244 : : @param _rName the name of the new object
245 : : @param _rxNewObject the new object (not surprising, is it ?)
246 : : @see createConfigKey
247 : : @see implReplace
248 : : @see implRemove
249 : : */
250 : : void implAppend(
251 : : const ::rtl::OUString& _rName,
252 : : const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _rxNewObject
253 : : );
254 : :
255 : : /** remove all references to an object from the container. No plausibility checks are done, e.g. whether
256 : : or not there exists an object with the given name. This is the responsibility of the caller.<BR>
257 : : Additionally the node for the given object will be removed from the registry (including all sub nodes).<BR>
258 : : The old component will not be disposed, this is the callers responsibility, too.
259 : : @param _rName the objects name
260 : : @see implReplace
261 : : @see implAppend
262 : : */
263 : : void implRemove(const ::rtl::OUString& _rName);
264 : :
265 : : /** remove a object in the container. No plausibility checks are done, e.g. whether
266 : : or not there exists an object with the given name or the object is non-NULL. This is the responsibility of the caller.<BR>
267 : : Additionally all object-related informations within the registry will be deleted. The new object config node,
268 : : where the caller may want to store the new objects information, is returned.<BR>
269 : : The old component will not be disposed, this is the callers responsibility, too.
270 : : @param _rName the objects name
271 : : @param _rxNewObject the new object
272 : : @param _rNewObjectNode the configuration node where the new object may be stored
273 : : @see implAppend
274 : : @see implRemove
275 : : */
276 : : void implReplace(
277 : : const ::rtl::OUString& _rName,
278 : : const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _rxNewObject
279 : : );
280 : :
281 : : /** notifies our container/approve listeners
282 : : */
283 : : void notifyByName(
284 : : ::osl::ResettableMutexGuard& _rGuard,
285 : : const ::rtl::OUString& _rName,
286 : : const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _xNewElement,
287 : : const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& xOldElement,
288 : : ContainerOperation _eOperation,
289 : : ListenerType _eType
290 : : );
291 : :
292 : 1392 : inline SAL_CALL operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > () const
293 : : {
294 : 1392 : return const_cast< XContainer* >( static_cast< const XContainer* >( this ) );
295 : : }
296 : :
297 : : private:
298 : : void addObjectListener(const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _xNewObject);
299 : : void removeObjectListener(const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _xNewObject);
300 : :
301 : : /** approve that the object given may be inserted into the container. Should be overloaded by derived classes,
302 : : the default implementation just checks the object to be non-void.
303 : :
304 : : @throws IllegalArgumentException
305 : : if the name or the object are invalid
306 : : @throws ElementExistException
307 : : if the object already exists in the container, or another object with the same name
308 : : already exists
309 : : @throws WrappedTargetException
310 : : if another error occures which prevents insertion of the object into the container
311 : : */
312 : : void approveNewObject(
313 : : const ::rtl::OUString& _sName,
314 : : const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContent >& _rxObject
315 : : ) const;
316 : :
317 : 0 : inline bool impl_haveAnyListeners_nothrow() const
318 : : {
319 [ # # ][ # # ]: 0 : return ( m_aContainerListeners.getLength() > 0 ) || ( m_aApproveListeners.getLength() > 0 );
320 : : }
321 : : };
322 : :
323 : : } // namespace dbaccess
324 : :
325 : : #endif // _DBA_CORE_DEFINITIONCONTAINER_HXX_
326 : :
327 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|