1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _FRM_INTERFACE_CONTAINER_HXX_
29 #define _FRM_INTERFACE_CONTAINER_HXX_
30 
31 #include <hash_map>
32 #include <comphelper/stl_types.hxx>
33 #include <comphelper/types.hxx>
34 #include <comphelper/uno3.hxx>
35 #include <com/sun/star/container/XNameReplace.hpp>
36 #include <com/sun/star/container/XIndexReplace.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/lang/XEventListener.hpp>
39 #include <com/sun/star/lang/EventObject.hpp>
40 #include <com/sun/star/container/XEnumerationAccess.hpp>
41 #include <com/sun/star/io/XPersistObject.hpp>
42 #include <com/sun/star/form/XFormComponent.hpp>
43 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
44 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/script/XEventAttacherManager.hpp>
47 #include <com/sun/star/script/ScriptEvent.hpp>
48 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
49 #include <com/sun/star/container/XContainer.hpp>
50 #include <com/sun/star/container/XIndexContainer.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/form/XFormComponent.hpp>
53 #include <com/sun/star/util/XCloneable.hpp>
54 #include <osl/mutex.hxx>
55 #include <cppuhelper/interfacecontainer.hxx>
56 #include <cppuhelper/component.hxx>
57 #include <cppuhelper/implbase8.hxx>
58 
59 using namespace comphelper;
60 
61 //.........................................................................
62 namespace frm
63 {
64 //.........................................................................
65 
66 
67 //==================================================================
68 	struct ElementDescription
69 	{
70 	public:
71 		::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >		xInterface;
72 		::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >	xPropertySet;
73 		::com::sun::star::uno::Reference< ::com::sun::star::container::XChild >		xChild;
74 		::com::sun::star::uno::Any													aElementTypeInterface;
75 
76 	public:
77 		ElementDescription( );
78 		virtual ~ElementDescription();
79 
80 	private:
81 		ElementDescription( const ElementDescription& );			// never implemented
82 		ElementDescription& operator=( const ElementDescription& );	// never implemented
83 	};
84 
85 typedef ::std::vector<InterfaceRef> OInterfaceArray;
86 typedef ::std::hash_multimap< ::rtl::OUString, InterfaceRef, ::comphelper::UStringHash, ::comphelper::UStringEqual> OInterfaceMap;
87 
88 //==================================================================
89 // OInterfaceContainer
90 // implements a container for form components
91 //==================================================================
92 typedef ::cppu::ImplHelper8 <   ::com::sun::star::container::XNameContainer
93                             ,   ::com::sun::star::container::XIndexContainer
94                             ,   ::com::sun::star::container::XContainer
95                             ,   ::com::sun::star::container::XEnumerationAccess
96                             ,   ::com::sun::star::script::XEventAttacherManager
97                             ,   ::com::sun::star::beans::XPropertyChangeListener
98                             ,   ::com::sun::star::io::XPersistObject
99                             ,   ::com::sun::star::util::XCloneable
100                             > OInterfaceContainer_BASE;
101 
102 class OInterfaceContainer :	public OInterfaceContainer_BASE
103 {
104 protected:
105 	::osl::Mutex&							m_rMutex;
106 
107     OInterfaceArray							m_aItems;
108     OInterfaceMap							m_aMap;
109 	::cppu::OInterfaceContainerHelper		m_aContainerListeners;
110 
111     const ::com::sun::star::uno::Type       m_aElementType;
112 
113 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory>		m_xServiceFactory;
114 
115 
116 	// EventManager
117 	::com::sun::star::uno::Reference< ::com::sun::star::script::XEventAttacherManager> 	m_xEventAttacher;
118 
119 public:
120 	OInterfaceContainer(
121 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory>& _rxFactory,
122 		::osl::Mutex& _rMutex,
123 		const ::com::sun::star::uno::Type& _rElementType);
124 
125     OInterfaceContainer( ::osl::Mutex& _rMutex, const OInterfaceContainer& _cloneSource );
126 
127     // late constructor for cloning
128     void clonedFrom( const OInterfaceContainer& _cloneSource );
129 
130 protected:
131     virtual ~OInterfaceContainer();
132 
133 public:
134 // ::com::sun::star::io::XPersistObject
135 	virtual ::rtl::OUString SAL_CALL getServiceName(  ) throw(::com::sun::star::uno::RuntimeException) = 0;
136 	virtual void SAL_CALL write( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectOutputStream >& OutStream ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
137 	virtual void SAL_CALL read( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectInputStream >& InStream ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
138 
139 // ::com::sun::star::lang::XEventListener
140 	virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& _rSource) throw(::com::sun::star::uno::RuntimeException);
141 
142 // ::com::sun::star::beans::XPropertyChangeListener
143 	virtual void SAL_CALL propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw (::com::sun::star::uno::RuntimeException);
144 
145 // ::com::sun::star::container::XElementAccess
146     virtual ::com::sun::star::uno::Type SAL_CALL getElementType() throw(::com::sun::star::uno::RuntimeException) ;
147     virtual sal_Bool SAL_CALL hasElements() throw(::com::sun::star::uno::RuntimeException);
148 
149 // ::com::sun::star::container::XEnumerationAccess
150 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration> SAL_CALL createEnumeration() throw(::com::sun::star::uno::RuntimeException);
151 
152 // ::com::sun::star::container::XNameAccess
153 	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);
154 	virtual StringSequence SAL_CALL getElementNames(  ) throw(::com::sun::star::uno::RuntimeException);
155 	virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw(::com::sun::star::uno::RuntimeException);
156 
157 // ::com::sun::star::container::XNameReplace
158 	virtual void SAL_CALL replaceByName(const ::rtl::OUString& Name, const ::com::sun::star::uno::Any& _rElement) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
159 
160 // ::com::sun::star::container::XNameContainer
161 	virtual void SAL_CALL insertByName(const ::rtl::OUString& Name, const ::com::sun::star::uno::Any& _rElement) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
162 	virtual void SAL_CALL removeByName(const ::rtl::OUString& Name) throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
163 
164 // ::com::sun::star::container::XIndexAccess
165 	virtual sal_Int32 SAL_CALL getCount() throw(::com::sun::star::uno::RuntimeException);
166 	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);
167 
168 // ::com::sun::star::container::XIndexReplace
169 	virtual void SAL_CALL replaceByIndex(sal_Int32 _nIndex, const ::com::sun::star::uno::Any& _rElement) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
170 
171 // ::com::sun::star::container::XIndexContainer
172 	virtual void SAL_CALL insertByIndex(sal_Int32 _nIndex, const ::com::sun::star::uno::Any& Element) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
173 	virtual void SAL_CALL removeByIndex(sal_Int32 _nIndex) throw(::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
174 
175 // ::com::sun::star::container::XContainer
176 	virtual void SAL_CALL addContainerListener(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener>& _rxListener) throw(::com::sun::star::uno::RuntimeException);
177 	virtual void SAL_CALL removeContainerListener(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener>& _rxListener) throw(::com::sun::star::uno::RuntimeException);
178 
179 // ::com::sun::star::script::XEventAttacherManager
180     virtual void SAL_CALL registerScriptEvent( sal_Int32 nIndex, const ::com::sun::star::script::ScriptEventDescriptor& aScriptEvent ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
181     virtual void SAL_CALL registerScriptEvents( sal_Int32 nIndex, const ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor >& aScriptEvents ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
182     virtual void SAL_CALL revokeScriptEvent( sal_Int32 nIndex, const ::rtl::OUString& aListenerType, const ::rtl::OUString& aEventMethod, const ::rtl::OUString& aRemoveListenerParam ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
183     virtual void SAL_CALL revokeScriptEvents( sal_Int32 nIndex ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
184     virtual void SAL_CALL insertEntry( sal_Int32 nIndex ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
185     virtual void SAL_CALL removeEntry( sal_Int32 nIndex ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
186     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::script::ScriptEventDescriptor > SAL_CALL getScriptEvents( sal_Int32 Index ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
187     virtual void SAL_CALL attach( sal_Int32 nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xObject, const ::com::sun::star::uno::Any& aHelper ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::ServiceNotRegisteredException, ::com::sun::star::uno::RuntimeException);
188     virtual void SAL_CALL detach( sal_Int32 nIndex, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xObject ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
189     virtual void SAL_CALL addScriptListener( const ::com::sun::star::uno::Reference< ::com::sun::star::script::XScriptListener >& xListener ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
190     virtual void SAL_CALL removeScriptListener( const ::com::sun::star::uno::Reference< ::com::sun::star::script::XScriptListener >& Listener ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
191 
192 protected:
193 	// helper
194 	virtual void SAL_CALL disposing();
195 	virtual void removeElementsNoEvents(sal_Int32 nIndex);
196 
197 	/** to be overridden if elements which are to be inserted into the container shall be checked
198 
199 		<p>the ElementDescription given can be used to cache information about the object - it will be passed
200 		later on to implInserted/implReplaced.</p>
201 	*/
202 	virtual void approveNewElement(
203 					const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject,
204 					ElementDescription* _pElement
205 				);
206 
207 	virtual ElementDescription* createElementMetaData( );
208 
209 	/** inserts an object into our internal structures
210 
211 		@param _nIndex
212 			the index at which position it should be inserted
213 		@param _bEvents
214 			if <TRUE/>, event knittings will be done
215 		@param _pApprovalResult
216 			must contain the result of an approveNewElement call. Can be <NULL/>, in this case, the approval
217 			is done within implInsert.
218 		@param _bFire
219 			if <TRUE/>, a notification about the insertion will be fired
220 	*/
221 			void implInsert(
222 				sal_Int32 _nIndex,
223 				const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject,
224 				sal_Bool _bEvents /* = sal_True */,
225 				ElementDescription* _pApprovalResult /* = NULL */ ,
226 				sal_Bool _bFire /* = sal_True */
227 			) throw(::com::sun::star::lang::IllegalArgumentException);
228 
229 	// called after the object is inserted, but before the "real listeners" are notified
230 	virtual void implInserted( const ElementDescription* _pElement );
231 	// called after the object is removed, but before the "real listeners" are notified
232 	virtual void implRemoved(const InterfaceRef& _rxObject);
233 
234     /** called after an object was replaced. The default implementation notifies our listeners, after releasing
235         the instance lock.
236     */
237 	virtual void impl_replacedElement(
238                     const ::com::sun::star::container::ContainerEvent& _rEvent,
239                     ::osl::ClearableMutexGuard& _rInstanceLock
240                 );
241 
242 	void SAL_CALL writeEvents(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectOutputStream>& _rxOutStream);
243 	void SAL_CALL readEvents(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XObjectInputStream>& _rxInStream);
244 
245 	/** replace an element, specified by position
246 
247 		@precond <arg>_nIndex</arg> is a valid index
248 		@precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
249 
250 	*/
251 	void implReplaceByIndex(
252 			const sal_Int32 _nIndex,
253 			const ::com::sun::star::uno::Any& _rNewElement,
254 			::osl::ClearableMutexGuard& _rClearBeforeNotify
255 		);
256 
257 	/** removes an element, specified by position
258 
259 		@precond <arg>_nIndex</arg> is a valid index
260 		@precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
261 
262 	*/
263 	void implRemoveByIndex(
264 			const sal_Int32 _nIndex,
265 			::osl::ClearableMutexGuard& _rClearBeforeNotify
266 		);
267 
268 	/** validates the given index
269 		@throws ::com::sun::star::lang::IndexOutOfBoundsException
270 			if the given index does not denote a valid position in our childs array
271 	*/
272 	void implCheckIndex( const sal_Int32 _nIndex ) SAL_THROW( ( ::com::sun::star::lang::IndexOutOfBoundsException ) );
273 
274 private:
275 	// hack for Vba Events
276     void impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex );
277 
278 	// the runtime event format has changed from version SO5.2 to OOo
279 	enum EventFormat
280 	{
281 		efVersionSO5x,
282 		efVersionSO6x
283 	};
284 	void	transformEvents( const EventFormat _eTargetFormat );
285 
286     void    impl_createEventAttacher_nothrow();
287 };
288 
289 //==================================================================
290 //= OFormComponents
291 //==================================================================
292 typedef ::cppu::ImplHelper1< ::com::sun::star::form::XFormComponent> OFormComponents_BASE;
293 typedef ::cppu::OComponentHelper FormComponentsBase;
294 	// else MSVC kills itself on some statements
295 class OFormComponents   :public FormComponentsBase
296 						,public OInterfaceContainer
297 						,public OFormComponents_BASE
298 {
299 protected:
300 	::osl::Mutex				m_aMutex;
301 	::comphelper::InterfaceRef 	m_xParent;
302 
303 public:
304 	OFormComponents(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory>& _rxFactory);
305     OFormComponents( const OFormComponents& _cloneSource );
306 	virtual ~OFormComponents();
307 
308 	DECLARE_UNO3_AGG_DEFAULTS(OFormComponents, FormComponentsBase);
309 
310 	virtual ::com::sun::star::uno::Any SAL_CALL queryAggregation(const ::com::sun::star::uno::Type& _rType) throw(::com::sun::star::uno::RuntimeException);
311     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw(::com::sun::star::uno::RuntimeException);
312 
313 // OComponentHelper
314 	virtual void SAL_CALL disposing();
315 
316 // ::com::sun::star::form::XFormComponent
317 	virtual ::comphelper::InterfaceRef SAL_CALL getParent() throw(::com::sun::star::uno::RuntimeException);
318 	virtual void SAL_CALL setParent(const ::comphelper::InterfaceRef& Parent) throw(::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
319 
320     // XEventListener
321     using OInterfaceContainer::disposing;
322 };
323 //.........................................................................
324 }	// namespace frm
325 //.........................................................................
326 
327 #endif          // _FRM_INTERFACE_CONTAINER_HXX_
328 
329