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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 #include <classes/actiontriggerpropertyset.hxx>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <cppuhelper/proptypehlp.hxx>
34 #include <cppuhelper/typeprovider.hxx>
35 #include <vcl/svapp.hxx>
36 
37 
38 using namespace cppu;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::awt;
43 
44 //struct SAL_DLLPUBLIC_IMPORT ::cppu::OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType >;
45 
46 // Handles for properties
47 // (PLEASE SORT THIS FIELD, IF YOU ADD NEW PROPERTIES!)
48 // We use an enum to define these handles, to use all numbers from 0 to nn and
49 // if you add someone, you don't must control this!
50 // But don't forget to change values of follow defines, if you do something with this enum!
51 enum EPROPERTIES
52 {
53 	HANDLE_COMMANDURL,
54 	HANDLE_HELPURL,
55 	HANDLE_IMAGE,
56 	HANDLE_SUBCONTAINER,
57 	HANDLE_TEXT,
58 	PROPERTYCOUNT
59 };
60 
61 namespace framework
62 {
63 
64 ActionTriggerPropertySet::ActionTriggerPropertySet( const Reference< XMultiServiceFactory >& /*xServiceManager*/ )
65     : ThreadHelpBase           ( &Application::GetSolarMutex()               )
66     , OBroadcastHelper         ( m_aLock.getShareableOslMutex()              )
67     ,	OPropertySetHelper       ( *SAL_STATIC_CAST( OBroadcastHelper *, this ))
68     , OWeakObject              ()
69     , m_xBitmap                ( 0 )
70     , m_xActionTriggerContainer( 0 )
71 {
72 }
73 
74 ActionTriggerPropertySet::~ActionTriggerPropertySet()
75 {
76 }
77 
78 // XInterface
79 Any SAL_CALL ActionTriggerPropertySet::queryInterface( const Type& aType )
80 throw ( RuntimeException )
81 {
82 	Any a = ::cppu::queryInterface(
83 				aType ,
84 				SAL_STATIC_CAST( XServiceInfo*, this ));
85 
86 	if( a.hasValue() )
87 		return a;
88 	else
89 	{
90 		a = OPropertySetHelper::queryInterface( aType );
91 
92 		if( a.hasValue() )
93 			return a;
94 	}
95 
96 	return OWeakObject::queryInterface( aType );
97 }
98 
99 void SAL_CALL ActionTriggerPropertySet::acquire() throw ()
100 {
101 	OWeakObject::acquire();
102 }
103 
104 void SAL_CALL ActionTriggerPropertySet::release() throw ()
105 {
106 	OWeakObject::release();
107 }
108 
109 
110 // XServiceInfo
111 ::rtl::OUString SAL_CALL ActionTriggerPropertySet::getImplementationName()
112 throw ( RuntimeException )
113 {
114 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATIONNAME_ACTIONTRIGGER ));
115 }
116 
117 sal_Bool SAL_CALL ActionTriggerPropertySet::supportsService( const ::rtl::OUString& ServiceName )
118 throw ( RuntimeException )
119 {
120 	if ( ServiceName.equalsAscii( SERVICENAME_ACTIONTRIGGER ))
121 		return sal_True;
122 
123 	return sal_False;
124 }
125 
126 Sequence< ::rtl::OUString > SAL_CALL ActionTriggerPropertySet::getSupportedServiceNames()
127 throw ( RuntimeException )
128 {
129     Sequence< ::rtl::OUString > seqServiceNames( 1 );
130 	seqServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME_ACTIONTRIGGER ));
131 	return seqServiceNames;
132 }
133 
134 // XTypeProvider
135 Sequence< Type > SAL_CALL ActionTriggerPropertySet::getTypes() throw ( RuntimeException )
136 {
137 	// Optimize this method !
138 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
139 	// For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
140 	static ::cppu::OTypeCollection* pTypeCollection = NULL ;
141 
142 	if ( pTypeCollection == NULL )
143 	{
144 		// Ready for multithreading; get global mutex for first call of this method only! see before
145 		osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
146 
147 		// Control these pointer again ... it can be, that another instance will be faster then these!
148 		if ( pTypeCollection == NULL )
149 		{
150 			// Create a static typecollection ...
151 			static ::cppu::OTypeCollection aTypeCollection(
152 						::getCppuType(( const Reference< XPropertySet			>*)NULL ) ,
153 						::getCppuType(( const Reference< XFastPropertySet		>*)NULL	) ,
154 						::getCppuType(( const Reference< XMultiPropertySet		>*)NULL	) ,
155 						::getCppuType(( const Reference< XServiceInfo			>*)NULL ) ,
156 						::getCppuType(( const Reference< XTypeProvider			>*)NULL ) ) ;
157 
158 			// ... and set his address to static pointer!
159 			pTypeCollection = &aTypeCollection ;
160 		}
161 	}
162 
163 	return pTypeCollection->getTypes() ;
164 }
165 
166 Sequence< sal_Int8 > SAL_CALL ActionTriggerPropertySet::getImplementationId() throw ( RuntimeException )
167 {
168 	// Create one Id for all instances of this class.
169 	// Use ethernet address to do this! (sal_True)
170 
171 	// Optimize this method
172 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
173 	// For the first call; pID is NULL - for the second call pID is different from NULL!
174 	static ::cppu::OImplementationId* pID = NULL ;
175 
176 	if ( pID == NULL )
177 	{
178 		// Ready for multithreading; get global mutex for first call of this method only! see before
179 		osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
180 
181 		// Control these pointer again ... it can be, that another instance will be faster then these!
182 		if ( pID == NULL )
183 		{
184 			// Create a new static ID ...
185 			static ::cppu::OImplementationId aID( sal_False ) ;
186 			// ... and set his address to static pointer!
187 			pID = &aID ;
188 		}
189 	}
190 
191 	return pID->getImplementationId() ;
192 }
193 
194 //---------------------------------------------------------------------------------------------------------
195 //	OPropertySetHelper implementation
196 //---------------------------------------------------------------------------------------------------------
197 
198 sal_Bool SAL_CALL ActionTriggerPropertySet::convertFastPropertyValue(
199 	Any&		aConvertedValue,
200 	Any&		aOldValue,
201 	sal_Int32	nHandle,
202 	const Any&	aValue	)
203 throw( IllegalArgumentException )
204 {
205 	//	Check, if value of property will changed in method "setFastPropertyValue_NoBroadcast()".
206 	//	Return sal_True, if changed - else return sal_False.
207 	//	Attention: Method "impl_tryToChangeProperty()" can throw the IllegalArgumentException !!!
208 	//	Initialize return value with sal_False !!!
209 	//	(Handle can be invalid)
210 	sal_Bool bReturn = sal_False;
211 
212 	switch( nHandle )
213 	{
214 		case HANDLE_COMMANDURL:
215 			bReturn = impl_tryToChangeProperty( m_aCommandURL, aValue, aOldValue, aConvertedValue );
216 			break;
217 
218 		case HANDLE_HELPURL:
219 			bReturn = impl_tryToChangeProperty( m_aHelpURL, aValue, aOldValue, aConvertedValue ) ;
220 			break;
221 
222 		case HANDLE_IMAGE:
223 			bReturn = impl_tryToChangeProperty( m_xBitmap, aValue, aOldValue, aConvertedValue ) ;
224 			break;
225 
226 		case HANDLE_SUBCONTAINER:
227 			bReturn = impl_tryToChangeProperty( m_xActionTriggerContainer, aValue, aOldValue, aConvertedValue );
228 			break;
229 
230 		case HANDLE_TEXT:
231 			bReturn = impl_tryToChangeProperty( m_aText, aValue, aOldValue, aConvertedValue ) ;
232 			break;
233 	}
234 
235 	// Return state of operation.
236 	return bReturn;
237 }
238 
239 
240 void SAL_CALL ActionTriggerPropertySet::setFastPropertyValue_NoBroadcast(
241 	sal_Int32 nHandle, const Any& aValue )
242 throw( Exception )
243 {
244 	::osl::MutexGuard aGuard( LockHelper::getGlobalLock().getShareableOslMutex() );
245 
246 	// Search for right handle ... and try to set property value.
247 	switch( nHandle )
248 	{
249 		case HANDLE_COMMANDURL:
250 			aValue >>= m_aCommandURL;
251 			break;
252 
253 		case HANDLE_HELPURL:
254 			aValue >>= m_aHelpURL;
255 			break;
256 
257 		case HANDLE_IMAGE:
258 			aValue >>= m_xBitmap;
259 			break;
260 
261 		case HANDLE_SUBCONTAINER:
262 			aValue >>= m_xActionTriggerContainer;
263 			break;
264 
265 		case HANDLE_TEXT:
266 			aValue >>= m_aText;
267 			break;
268 	}
269 }
270 
271 void SAL_CALL ActionTriggerPropertySet::getFastPropertyValue(
272 	Any& aValue, sal_Int32 nHandle ) const
273 {
274 	::osl::MutexGuard aGuard( LockHelper::getGlobalLock().getShareableOslMutex() );
275 
276 	// Search for right handle ... and try to get property value.
277 	switch( nHandle )
278 	{
279 		case HANDLE_COMMANDURL:
280 			aValue <<= m_aCommandURL;
281 			break;
282 
283 		case HANDLE_HELPURL:
284 			aValue <<= m_aHelpURL;
285 			break;
286 
287 		case HANDLE_IMAGE:
288 			aValue <<= m_xBitmap;
289 			break;
290 
291 		case HANDLE_SUBCONTAINER:
292 			aValue <<= m_xActionTriggerContainer;
293 			break;
294 
295 		case HANDLE_TEXT:
296 			aValue <<= m_aText;
297 			break;
298 	}
299 }
300 
301 ::cppu::IPropertyArrayHelper& SAL_CALL ActionTriggerPropertySet::getInfoHelper()
302 {
303 	// Optimize this method !
304 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
305 	// For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
306 	static OPropertyArrayHelper* pInfoHelper = NULL;
307 
308 	if( pInfoHelper == NULL )
309 	{
310 		// Ready for multithreading
311         ::osl::MutexGuard aGuard( LockHelper::getGlobalLock().getShareableOslMutex() );
312 		// Control this pointer again, another instance can be faster then these!
313 		if( pInfoHelper == NULL )
314 		{
315 			// Define static member to give structure of properties to baseclass "OPropertySetHelper".
316 			// "impl_getStaticPropertyDescriptor" is a non exported and static funtion, who will define a static propertytable.
317 			// "sal_True" say: Table is sorted by name.
318 			static OPropertyArrayHelper aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True );
319 			pInfoHelper = &aInfoHelper;
320 		}
321 	}
322 
323 	return (*pInfoHelper);
324 }
325 
326 Reference< XPropertySetInfo > SAL_CALL ActionTriggerPropertySet::getPropertySetInfo()
327 throw ( RuntimeException )
328 {
329 	// Optimize this method !
330 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
331 	// For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
332 	static Reference< XPropertySetInfo >* pInfo = NULL ;
333 
334 	if( pInfo == NULL )
335 	{
336 		// Ready for multithreading
337         ::osl::MutexGuard aGuard( LockHelper::getGlobalLock().getShareableOslMutex() );
338 		// Control this pointer again, another instance can be faster then these!
339 		if( pInfo == NULL )
340 		{
341 			// Create structure of propertysetinfo for baseclass "OPropertySetHelper".
342 			// (Use method "getInfoHelper()".)
343 			static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
344 			pInfo = &xInfo;
345 		}
346 	}
347 
348 	return (*pInfo);
349 }
350 
351 const Sequence< Property > ActionTriggerPropertySet::impl_getStaticPropertyDescriptor()
352 {
353 	static const Property pActionTriggerPropertys[] =
354 	{
355 		Property( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CommandURL"	)), HANDLE_COMMANDURL	, ::getCppuType((::rtl::OUString*)0)				, PropertyAttribute::TRANSIENT	),
356 		Property( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "HelpURL"		)), HANDLE_HELPURL		, ::getCppuType((::rtl::OUString*)0)				, PropertyAttribute::TRANSIENT	),
357 		Property( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Image"        )), HANDLE_IMAGE		, ::getCppuType((Reference<XBitmap>*)0)		, PropertyAttribute::TRANSIENT	),
358 		Property( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SubContainer"	)), HANDLE_SUBCONTAINER	, ::getCppuType((::rtl::OUString*)0)				, PropertyAttribute::TRANSIENT	),
359 		Property( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text"       	)), HANDLE_TEXT			, ::getCppuType((Reference<XInterface>*)0)	, PropertyAttribute::TRANSIENT	)
360 	};
361 
362 	// Use it to initialize sequence!
363 	static const Sequence< Property > seqActionTriggerPropertyDescriptor( pActionTriggerPropertys, PROPERTYCOUNT );
364 
365 	// Return static "PropertyDescriptor"
366 	return seqActionTriggerPropertyDescriptor ;
367 }
368 
369 
370 //******************************************************************************************************************************
371 //	private method
372 //******************************************************************************************************************************
373 sal_Bool ActionTriggerPropertySet::impl_tryToChangeProperty(
374 	const	::rtl::OUString&	sCurrentValue	,
375 	const	Any&		aNewValue		,
376 	Any&		aOldValue		,
377 	Any&		aConvertedValue	)
378 throw( IllegalArgumentException )
379 {
380 	// Set default return value if method failed.
381 	sal_Bool bReturn = sal_False;
382 	// Get new value from any.
383 	// IllegalArgumentException() can be thrown!
384 	::rtl::OUString sValue ;
385 	convertPropertyValue( sValue, aNewValue );
386 
387 	// If value change ...
388 	if( sValue != sCurrentValue )
389 	{
390 		// ... set information of change.
391 		aOldValue		<<= sCurrentValue	;
392 		aConvertedValue	<<= sValue			;
393 		// Return OK - "value will be change ..."
394 		bReturn = sal_True;
395 	}
396 	else
397 	{
398 		// ... clear information of return parameter!
399 		aOldValue.clear			() ;
400 		aConvertedValue.clear	() ;
401 		// Return NOTHING - "value will not be change ..."
402 		bReturn = sal_False;
403 	}
404 
405 	return bReturn;
406 }
407 
408 
409 sal_Bool ActionTriggerPropertySet::impl_tryToChangeProperty(
410 	const Reference< XBitmap >	aCurrentValue	,
411 	const Any&					aNewValue		,
412 	Any&						aOldValue		,
413 	Any&						aConvertedValue	)
414 throw( IllegalArgumentException )
415 {
416 	// Set default return value if method failed.
417 	sal_Bool bReturn = sal_False;
418 	// Get new value from any.
419 	// IllegalArgumentException() can be thrown!
420 	Reference< XBitmap > aValue ;
421 	convertPropertyValue( aValue, aNewValue );
422 
423 	// If value change ...
424 	if( aValue != aCurrentValue )
425 	{
426 		// ... set information of change.
427 		aOldValue		<<= aCurrentValue	;
428 		aConvertedValue	<<= aValue			;
429 		// Return OK - "value will be change ..."
430 		bReturn = sal_True;
431 	}
432 	else
433 	{
434 		// ... clear information of return parameter!
435 		aOldValue.clear			() ;
436 		aConvertedValue.clear	() ;
437 		// Return NOTHING - "value will not be change ..."
438 		bReturn = sal_False;
439 	}
440 
441 	return bReturn;
442 }
443 
444 sal_Bool ActionTriggerPropertySet::impl_tryToChangeProperty(
445 	const Reference< XInterface >	aCurrentValue	,
446 	const Any&						aNewValue		,
447 	Any&							aOldValue		,
448 	Any&							aConvertedValue	)
449 throw( IllegalArgumentException )
450 {
451 	// Set default return value if method failed.
452 	sal_Bool bReturn = sal_False;
453 	// Get new value from any.
454 	// IllegalArgumentException() can be thrown!
455 	Reference< XInterface > aValue ;
456 	convertPropertyValue( aValue, aNewValue );
457 
458 	// If value change ...
459 	if( aValue != aCurrentValue )
460 	{
461 		// ... set information of change.
462 		aOldValue		<<= aCurrentValue	;
463 		aConvertedValue	<<= aValue			;
464 		// Return OK - "value will be change ..."
465 		bReturn = sal_True;
466 	}
467 	else
468 	{
469 		// ... clear information of return parameter!
470 		aOldValue.clear			() ;
471 		aConvertedValue.clear	() ;
472 		// Return NOTHING - "value will not be change ..."
473 		bReturn = sal_False;
474 	}
475 
476 	return bReturn;
477 }
478 
479 }
480 
481