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 //______________________________________________________________________________________________________________
29 //	my own include
30 //______________________________________________________________________________________________________________
31 
32 #include "OConnectionPointHelper.hxx"
33 
34 //______________________________________________________________________________________________________________
35 //	includes of other projects
36 //______________________________________________________________________________________________________________
37 
38 //______________________________________________________________________________________________________________
39 //	include of my own project
40 //______________________________________________________________________________________________________________
41 #include "OConnectionPointContainerHelper.hxx"
42 
43 //______________________________________________________________________________________________________________
44 //	namespaces
45 //______________________________________________________________________________________________________________
46 
47 using namespace	::rtl					;
48 using namespace	::osl					;
49 using namespace	::cppu					;
50 using namespace	::com::sun::star::uno	;
51 using namespace	::com::sun::star::lang	;
52 
53 namespace unocontrols{
54 
55 //______________________________________________________________________________________________________________
56 //	construct/destruct
57 //______________________________________________________________________________________________________________
58 
59 OConnectionPointHelper::OConnectionPointHelper(	Mutex&								aMutex						,
60 		 										OConnectionPointContainerHelper*	pContainerImplementation	,
61 												UNO3_TYPE				  			aType						)
62 	: m_aSharedMutex				( aMutex					)
63 	, m_oContainerWeakReference		( pContainerImplementation	)
64 	, m_pContainerImplementation	( pContainerImplementation	)
65 	, m_aInterfaceType				( aType						)
66 {
67 }
68 
69 OConnectionPointHelper::~OConnectionPointHelper()
70 {
71 }
72 
73 //____________________________________________________________________________________________________________
74 //	XInterface
75 //____________________________________________________________________________________________________________
76 
77 Any SAL_CALL OConnectionPointHelper::queryInterface( const Type& aType ) throw( RuntimeException )
78 {
79 	// Attention:
80 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
81 
82 	// Ask for my own supported interfaces ...
83 	Any aReturn	( ::cppu::queryInterface(	aType										,
84 									   		static_cast< XConnectionPoint*	> ( this )
85 										)
86 				);
87 
88 	// If searched interface not supported by this class ...
89 	if ( aReturn.hasValue() == sal_False )
90 	{
91 		// ... ask baseclasses.
92 		aReturn = OWeakObject::queryInterface( aType );
93 	}
94 
95 	return aReturn ;
96 }
97 
98 //____________________________________________________________________________________________________________
99 //	XInterface
100 //____________________________________________________________________________________________________________
101 
102 void SAL_CALL OConnectionPointHelper::acquire() throw()
103 {
104 	// Attention:
105 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
106 
107 	// Forward to baseclass
108 	OWeakObject::acquire();
109 }
110 
111 //____________________________________________________________________________________________________________
112 //	XInterface
113 //____________________________________________________________________________________________________________
114 
115 void SAL_CALL OConnectionPointHelper::release() throw()
116 {
117 	// Attention:
118 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
119 
120 	// Forward to baseclass
121 	OWeakObject::release();
122 }
123 
124 //______________________________________________________________________________________________________________
125 //	XConnectionPoint
126 //______________________________________________________________________________________________________________
127 
128 Type SAL_CALL OConnectionPointHelper::getConnectionType() throw( RuntimeException )
129 {
130 	// Ready for multithreading
131 	MutexGuard aGuard( m_aSharedMutex );
132 
133 	// Set default return value, if method failed.
134 	if ( impl_LockContainer() == sal_False )
135 	{
136 		// Container not exist! Its an runtime error.
137 		throw RuntimeException();
138 	}
139 
140 	// If container reference valid, return right type of supported interfaces of THIS connectionpoint.
141 	Type aReturnType = m_aInterfaceType ;
142 	// Don't forget this!
143 	impl_UnlockContainer();
144 
145 	return aReturnType;
146 }
147 
148 //______________________________________________________________________________________________________________
149 //	XConnectionPoint
150 //______________________________________________________________________________________________________________
151 
152 Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer() throw( RuntimeException )
153 {
154 	// Ready for multithreading
155 	MutexGuard aGuard( m_aSharedMutex );
156 	// Convert weakreference to correct uno3-reference and return value. It can be NULL, if container destroyed!
157 	return Reference< XConnectionPointContainer >( m_oContainerWeakReference.get(), UNO_QUERY );
158 }
159 
160 //______________________________________________________________________________________________________________
161 //	XConnectionPoint
162 //______________________________________________________________________________________________________________
163 
164 void SAL_CALL OConnectionPointHelper::advise( const Reference< XInterface >& xListener ) throw(	ListenerExistException		,
165 																								InvalidListenerException	,
166 																								RuntimeException			)
167 {
168 	// Ready for multithreading
169 	MutexGuard aGuard( m_aSharedMutex );
170 
171 	// If type of listener not the same for this special container ...
172 	Any aCheckType = xListener->queryInterface( m_aInterfaceType );
173 	if ( aCheckType.hasValue() )
174 	{
175 		// ... throw an exception.
176 		throw InvalidListenerException();
177 	}
178 
179 	// ListenerExistException is obsolete!?
180 	// Its the same container for XConnectionPointContainer and XConnectionPoint. But only here we must control, if a listener already exist!?
181 	// You can add a listener more then one time at XConnectionPointContainer, but here only one ...
182 
183 	// Operation is permitted only, if reference to container is valid!
184 	if ( impl_LockContainer() == sal_False )
185 	{
186 		// Container not exist! Its an runtime error.
187 		throw RuntimeException();
188 	}
189 	// Forward it to OConnectionPointHelperContainer!
190 	m_pContainerImplementation->advise( m_aInterfaceType, xListener );
191 	// Don't forget this!
192 	impl_UnlockContainer();
193 }
194 
195 //______________________________________________________________________________________________________________
196 //	XConnectionPoint
197 //______________________________________________________________________________________________________________
198 
199 void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener ) throw( RuntimeException )
200 {
201 	// Ready for multithreading
202 	MutexGuard aGuard( m_aSharedMutex );
203 	// Operation is permitted only, if reference to container is valid!
204 	if ( impl_LockContainer() == sal_False )
205 	{
206 		// Container not exist! Its an runtime error.
207 		throw RuntimeException();
208 
209 	}
210 	// Forward it to OConnectionPointHelperContainer!
211 	m_pContainerImplementation->unadvise( m_aInterfaceType, xListener );
212 	// Don't forget this!
213 	impl_UnlockContainer();
214 }
215 
216 //______________________________________________________________________________________________________________
217 //	XConnectionPoint
218 //______________________________________________________________________________________________________________
219 
220 Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections() throw( RuntimeException )
221 {
222 	// Ready for multithreading
223 	MutexGuard aGuard( m_aSharedMutex );
224 	// Operation is permitted only, if reference to container is valid!
225 	if ( impl_LockContainer() == sal_False )
226 	{
227 		// Container not exist! Its an runtime error.
228 		throw RuntimeException();
229 	}
230 	// Set default return value, if method failed.
231 	Sequence< Reference< XInterface > > seqReturnConnections = Sequence< Reference< XInterface > >();
232 	// Get reference to private member of OConnectionPointHelperContainer!
233 	OMultiTypeInterfaceContainerHelper&	aSharedContainer = m_pContainerImplementation->impl_getMultiTypeContainer();
234 	// Get pointer to specialized container which hold all interfaces of searched type.
235 	OInterfaceContainerHelper* pSpecialContainer = aSharedContainer.getContainer( m_aInterfaceType );
236 	// Get elements of searched type, if somelse exist.
237 	if ( pSpecialContainer != NULL )
238 	{
239 		seqReturnConnections = pSpecialContainer->getElements();
240 	}
241 	// Don't forget this!
242 	impl_UnlockContainer();
243 
244 	return seqReturnConnections;
245 }
246 
247 //______________________________________________________________________________________________________________
248 //	private method
249 //______________________________________________________________________________________________________________
250 
251 sal_Bool OConnectionPointHelper::impl_LockContainer()
252 {
253 	// Convert weakreference to hard uno3-reference and return state.
254 	// If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
255 	// Don't forget to "unlock" this reference!
256 	m_xLock = m_oContainerWeakReference.get();
257 	return m_xLock.is();
258 }
259 
260 //______________________________________________________________________________________________________________
261 //	private method
262 //______________________________________________________________________________________________________________
263 
264 void OConnectionPointHelper::impl_UnlockContainer()
265 {
266 	// Free hard uno3-reference to container.
267 	// see also "impl_LockContainer()"
268 	m_xLock = Reference< XInterface >();
269 }
270 
271 }	// namespace unocontrols
272