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