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 //______________________________________________________________________________________________________________
30 //	my own include
31 //______________________________________________________________________________________________________________
32 
33 #include "framecontrol.hxx"
34 
35 //______________________________________________________________________________________________________________
36 //	includes of other projects
37 //______________________________________________________________________________________________________________
38 #include <com/sun/star/frame/XDispatchProvider.hpp>
39 #include <com/sun/star/util/XURLTransformer.hpp>
40 #include <com/sun/star/frame/XDispatch.hpp>
41 #include <com/sun/star/frame/FrameSearchFlag.hpp>
42 #include <com/sun/star/frame/FrameSearchFlag.hpp>
43 #include <com/sun/star/beans/PropertyAttribute.hpp>
44 #include <cppuhelper/typeprovider.hxx>
45 #include <vos/diagnose.hxx>
46 
47 //______________________________________________________________________________________________________________
48 //	include of my own project
49 //______________________________________________________________________________________________________________
50 
51 //______________________________________________________________________________________________________________
52 //	namespaces
53 //______________________________________________________________________________________________________________
54 
55 using namespace	::rtl					;
56 using namespace	::osl					;
57 using namespace	::cppu					;
58 using namespace	::com::sun::star::uno	;
59 using namespace	::com::sun::star::lang	;
60 using namespace	::com::sun::star::beans	;
61 using namespace	::com::sun::star::awt	;
62 using namespace	::com::sun::star::frame	;
63 using namespace	::com::sun::star::util	;
64 
65 namespace unocontrols{
66 
67 //______________________________________________________________________________________________________________
68 //	construct/destruct
69 //______________________________________________________________________________________________________________
70 
71 FrameControl::FrameControl( const Reference< XMultiServiceFactory >& xFactory )
72 	: BaseControl					( xFactory																				)
73 	, OBroadcastHelper				( m_aMutex																				)
74 	, OPropertySetHelper			( *SAL_STATIC_CAST( OBroadcastHelper *, this )	)
75 	, m_aInterfaceContainer			( m_aMutex																				)
76 	, m_aConnectionPointContainer	( m_aMutex																				)
77 {
78 }
79 
80 FrameControl::~FrameControl()
81 {
82 }
83 
84 //____________________________________________________________________________________________________________
85 //	XInterface
86 //____________________________________________________________________________________________________________
87 
88 Any SAL_CALL FrameControl::queryInterface( const Type& rType ) throw( RuntimeException )
89 {
90 	// Attention:
91 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
92 	Any aReturn ;
93 	Reference< XInterface > xDel = BaseControl::impl_getDelegator();
94 	if ( xDel.is() )
95 	{
96 		// If an delegator exist, forward question to his queryInterface.
97 		// Delegator will ask his own queryAggregation!
98 		aReturn = xDel->queryInterface( rType );
99 	}
100 	else
101 	{
102 		// If an delegator unknown, forward question to own queryAggregation.
103 		aReturn = queryAggregation( rType );
104 	}
105 
106 	return aReturn ;
107 }
108 
109 //____________________________________________________________________________________________________________
110 //	XInterface
111 //____________________________________________________________________________________________________________
112 
113 void SAL_CALL FrameControl::acquire() throw()
114 {
115 	// Attention:
116 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
117 
118 	// Forward to baseclass
119 	BaseControl::acquire();
120 }
121 
122 //____________________________________________________________________________________________________________
123 //	XInterface
124 //____________________________________________________________________________________________________________
125 
126 void SAL_CALL FrameControl::release() throw()
127 {
128 	// Attention:
129 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
130 
131 	// Forward to baseclass
132 	BaseControl::release();
133 }
134 
135 //____________________________________________________________________________________________________________
136 //	XTypeProvider
137 //____________________________________________________________________________________________________________
138 
139 Sequence< Type > SAL_CALL FrameControl::getTypes() throw( RuntimeException )
140 {
141 	// Optimize this method !
142 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
143 	// For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
144 	static OTypeCollection* pTypeCollection = NULL ;
145 
146 	if ( pTypeCollection == NULL )
147 	{
148 		// Ready for multithreading; get global mutex for first call of this method only! see before
149 		MutexGuard aGuard( Mutex::getGlobalMutex() );
150 
151 		// Control these pointer again ... it can be, that another instance will be faster then these!
152 		if ( pTypeCollection == NULL )
153 		{
154 			// Create a static typecollection ...
155 			static OTypeCollection aTypeCollection	(	::getCppuType(( const Reference< XControlModel				>*)NULL )	,
156 												  		::getCppuType(( const Reference< XControlContainer			>*)NULL )	,
157 												  		::getCppuType(( const Reference< XConnectionPointContainer	>*)NULL )	,
158 														BaseControl::getTypes()
159 													);
160 			// ... and set his address to static pointer!
161 			pTypeCollection = &aTypeCollection ;
162 		}
163 	}
164 
165 	return pTypeCollection->getTypes();
166 }
167 
168 //____________________________________________________________________________________________________________
169 //	XAggregation
170 //____________________________________________________________________________________________________________
171 
172 Any SAL_CALL FrameControl::queryAggregation( const Type& aType ) throw( RuntimeException )
173 {
174 	// Ask for my own supported interfaces ...
175 	// Attention: XTypeProvider and XInterface are supported by OComponentHelper!
176 	Any aReturn	( ::cppu::queryInterface(	aType												,
177 									   		static_cast< XControlModel*				> ( this )	,
178 									   		static_cast< XConnectionPointContainer*	> ( this )
179 										)
180 				);
181 
182 	// If searched interface not supported by this class ...
183 	if ( aReturn.hasValue() == sal_False )
184 	{
185 		// ... ask baseclasses.
186 		aReturn = OPropertySetHelper::queryInterface( aType );
187 		if ( aReturn.hasValue() == sal_False )
188 		{
189 			aReturn = BaseControl::queryAggregation( aType );
190 		}
191 	}
192 
193 	return aReturn ;
194 }
195 
196 //____________________________________________________________________________________________________________
197 //	XControl
198 //____________________________________________________________________________________________________________
199 
200 void SAL_CALL FrameControl::createPeer(	const	Reference< XToolkit >&		xToolkit	,
201 										const	Reference< XWindowPeer >&	xParentPeer	) throw( RuntimeException )
202 {
203 	BaseControl::createPeer( xToolkit, xParentPeer );
204 	if ( impl_getPeerWindow().is() )
205 	{
206 		if( m_sComponentURL.getLength() > 0 )
207 		{
208 			impl_createFrame( getPeer(), m_sComponentURL, m_seqLoaderArguments );
209 		}
210 	}
211 }
212 
213 //____________________________________________________________________________________________________________
214 //	XControl
215 //____________________________________________________________________________________________________________
216 
217 sal_Bool SAL_CALL FrameControl::setModel( const Reference< XControlModel >& /*xModel*/ ) throw( RuntimeException )
218 {
219 	// We have no model.
220 	return sal_False ;
221 }
222 
223 //____________________________________________________________________________________________________________
224 //	XControl
225 //____________________________________________________________________________________________________________
226 
227 Reference< XControlModel > SAL_CALL FrameControl::getModel() throw( RuntimeException )
228 {
229 	// We have no model.
230 	return Reference< XControlModel >();
231 }
232 
233 //____________________________________________________________________________________________________________
234 //	XControl
235 //____________________________________________________________________________________________________________
236 
237 void SAL_CALL FrameControl::dispose() throw( RuntimeException )
238 {
239     impl_deleteFrame();
240     BaseControl::dispose();
241 }
242 
243 //____________________________________________________________________________________________________________
244 //	XView
245 //____________________________________________________________________________________________________________
246 
247 sal_Bool SAL_CALL FrameControl::setGraphics( const Reference< XGraphics >& /*xDevice*/ ) throw( RuntimeException )
248 {
249 	// it is not possible to print this control
250 	return sal_False ;
251 }
252 
253 //____________________________________________________________________________________________________________
254 //	XView
255 //____________________________________________________________________________________________________________
256 
257 Reference< XGraphics > SAL_CALL FrameControl::getGraphics() throw( RuntimeException )
258 {
259 	// when its not posible to set graphics ! then its possible to return null
260 	return Reference< XGraphics >();
261 }
262 
263 //____________________________________________________________________________________________________________
264 //	XConnectionPointContainer
265 //____________________________________________________________________________________________________________
266 
267 Sequence< Type > SAL_CALL FrameControl::getConnectionPointTypes() throw( RuntimeException )
268 {
269 	// Forwarded to helper class
270 	return m_aConnectionPointContainer.getConnectionPointTypes();
271 }
272 
273 //____________________________________________________________________________________________________________
274 //	XConnectionPointContainer
275 //____________________________________________________________________________________________________________
276 
277 Reference< XConnectionPoint > SAL_CALL FrameControl::queryConnectionPoint( const Type& aType ) throw( RuntimeException )
278 {
279 	// Forwarded to helper class
280 	return m_aConnectionPointContainer.queryConnectionPoint( aType );
281 }
282 
283 //____________________________________________________________________________________________________________
284 //	XConnectionPointContainer
285 //____________________________________________________________________________________________________________
286 
287 void SAL_CALL FrameControl::advise(	const	Type&						aType		,
288 									const	Reference< XInterface >&	xListener	) throw( RuntimeException )
289 {
290 	// Forwarded to helper class
291 	m_aConnectionPointContainer.advise( aType, xListener );
292 }
293 
294 //____________________________________________________________________________________________________________
295 //	XConnectionPointContainer
296 //____________________________________________________________________________________________________________
297 
298 void SAL_CALL FrameControl::unadvise(	const	Type&						aType		,
299 										const	Reference< XInterface >&	xListener	) throw( RuntimeException )
300 {
301 	// Forwarded to helper class
302 	m_aConnectionPointContainer.unadvise( aType, xListener );
303 }
304 
305 //____________________________________________________________________________________________________________
306 //	impl but public method to register service
307 //____________________________________________________________________________________________________________
308 
309 const Sequence< OUString > FrameControl::impl_getStaticSupportedServiceNames()
310 {
311 	MutexGuard aGuard( Mutex::getGlobalMutex() );
312     Sequence< OUString > seqServiceNames( 1 );
313     seqServiceNames.getArray() [0] = OUString::createFromAscii( SERVICENAME_FRAMECONTROL );
314     return seqServiceNames ;
315 }
316 
317 //____________________________________________________________________________________________________________
318 //	impl but public method to register service
319 //____________________________________________________________________________________________________________
320 
321 const OUString FrameControl::impl_getStaticImplementationName()
322 {
323 	return OUString::createFromAscii( IMPLEMENTATIONNAME_FRAMECONTROL );
324 }
325 
326 //____________________________________________________________________________________________________________
327 //	OPropertySetHelper
328 //____________________________________________________________________________________________________________
329 
330 sal_Bool FrameControl::convertFastPropertyValue(		Any&		rConvertedValue	,
331 														Any&		rOldValue		,
332 														sal_Int32	nHandle			,
333 												const	Any&		rValue			) throw( IllegalArgumentException )
334 {
335 	sal_Bool bReturn = sal_False ;
336 	switch (nHandle)
337 	{
338 		case PROPERTYHANDLE_COMPONENTURL		:		rConvertedValue	  = rValue					;
339 														rOldValue		<<= m_sComponentURL			;
340 														bReturn			  = sal_True				;
341 														break ;
342 
343 		case PROPERTYHANDLE_LOADERARGUMENTS		:		rConvertedValue	  = rValue					;
344 														rOldValue		<<= m_seqLoaderArguments	;
345 														bReturn			  = sal_True				;
346 														break ;
347 	}
348 
349 	if ( bReturn == sal_False )
350 	{
351 		throw IllegalArgumentException();
352 	}
353 
354 	return bReturn ;
355 }
356 
357 //____________________________________________________________________________________________________________
358 //	OPropertySetHelper
359 //____________________________________________________________________________________________________________
360 
361 void FrameControl::setFastPropertyValue_NoBroadcast(			sal_Int32	nHandle	,
362 														const	Any&		rValue	)
363                                                         throw ( ::com::sun::star::uno::Exception )
364 {
365 	// this method only set the value
366 	MutexGuard	aGuard (m_aMutex) ;
367 	switch (nHandle)
368 	{
369 		case PROPERTYHANDLE_COMPONENTURL		:		rValue >>= m_sComponentURL ;
370 														if (getPeer().is())
371 														{
372 															impl_createFrame ( getPeer(), m_sComponentURL, m_seqLoaderArguments ) ;
373 														}
374 														break ;
375 
376 		case PROPERTYHANDLE_LOADERARGUMENTS		:		rValue >>= m_seqLoaderArguments ;
377 														break ;
378 
379 		default	:										VOS_ENSHURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
380 	}
381 }
382 
383 //____________________________________________________________________________________________________________
384 //	OPropertySetHelper
385 //____________________________________________________________________________________________________________
386 
387 void FrameControl::getFastPropertyValue(	Any&		rRet	,
388 											sal_Int32	nHandle	) const
389 {
390 	MutexGuard	aGuard ( Mutex::getGlobalMutex() ) ;
391 
392 	switch (nHandle)
393 	{
394 		case PROPERTYHANDLE_COMPONENTURL	:		rRet <<= m_sComponentURL ;
395 													break ;
396 
397 		case PROPERTYHANDLE_LOADERARGUMENTS	:		rRet <<= m_seqLoaderArguments ;
398 													break ;
399 
400 		case PROPERTYHANDLE_FRAME			:		rRet <<= m_xFrame ;
401 							   						break ;
402 
403 		default	:			                        VOS_ENSHURE ( nHandle == -1, ERRORTEXT_VOSENSHURE ) ;
404 	}
405 }
406 
407 //____________________________________________________________________________________________________________
408 //	OPropertySetHelper
409 //____________________________________________________________________________________________________________
410 
411 IPropertyArrayHelper& FrameControl::getInfoHelper()
412 {
413 	// Create a table that map names to index values.
414 	static OPropertyArrayHelper* pInfo ;
415 
416 	if (!pInfo)
417 	{
418 		// global method must be guarded
419 		MutexGuard	aGuard ( Mutex::getGlobalMutex() ) ;
420 
421 		if (!pInfo)
422 		{
423 			pInfo = new OPropertyArrayHelper( impl_getStaticPropertyDescriptor(), sal_True );
424 		}
425 	}
426 
427 	return *pInfo ;
428 }
429 /*
430 //--------------------------------------------------------------------------------------------------
431 // start OConnectionPointContainerHelper
432 //--------------------------------------------------------------------------------------------------
433 Uik* FrameControl::getConnectionPointUiks ( sal_Int32* pCount ) const
434 {
435 	static Uik szUiks[] =
436 	{
437 		((XEventListener*)NULL)->getSmartUik  (),
438 		::getCppuType((const Reference< XPropertyChangeListener >*)0),
439 		::getCppuType((const Reference< XVetoableChangeListener >*)0),
440 		::getCppuType((const Reference< XPropertiesChangeListener >*)0)
441 	} ;
442 
443 	*pCount = 4 ;
444 
445 	return szUiks ;
446 }
447 //--------------------------------------------------------------------------------------------------
448 // end OConnectionPointContainerHelper
449 //--------------------------------------------------------------------------------------------------
450 */
451 
452 //____________________________________________________________________________________________________________
453 //	OPropertySetHelper
454 //____________________________________________________________________________________________________________
455 
456 Reference< XPropertySetInfo > SAL_CALL FrameControl::getPropertySetInfo() throw( RuntimeException )
457 {
458 	// Optimize this method !
459 	// We initialize a static variable only one time. And we don't must use a mutex at every call!
460 	// For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
461 	static Reference< XPropertySetInfo >* pInfo = (Reference< XPropertySetInfo >*)0 ;
462 	if ( pInfo == (Reference< XPropertySetInfo >*)0 )
463 	{
464 		// Ready for multithreading
465 		MutexGuard aGuard ( Mutex::getGlobalMutex () ) ;
466 		// Control this pointer again, another instance can be faster then these!
467 		if ( pInfo == (Reference< XPropertySetInfo >*)0 )
468 		{
469 			// Create structure of propertysetinfo for baseclass "OPropertySetHelper".
470 			// (Use method "getInfoHelper()".)
471 			static Reference< XPropertySetInfo > xInfo ( createPropertySetInfo ( getInfoHelper () ) ) ;
472 			pInfo = &xInfo ;
473 		}
474 	}
475 	return ( *pInfo ) ;
476 }
477 
478 //____________________________________________________________________________________________________________
479 //	BaseControl
480 //____________________________________________________________________________________________________________
481 
482 WindowDescriptor* FrameControl::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
483 {
484 	WindowDescriptor* pDescriptor	= new WindowDescriptor	;
485 
486 	pDescriptor->Type				= WindowClass_CONTAINER	;
487 	pDescriptor->ParentIndex		= -1					;
488 	pDescriptor->Parent				= xParentPeer			;
489 	pDescriptor->Bounds				= getPosSize ()			;
490 	pDescriptor->WindowAttributes	= 0						;
491 
492 	return pDescriptor ;
493 }
494 
495 //____________________________________________________________________________________________________________
496 //	private method
497 //____________________________________________________________________________________________________________
498 
499 void FrameControl::impl_createFrame(	const	Reference< XWindowPeer >&	xPeer		,
500 										const	OUString&					rURL		,
501 										const	Sequence< PropertyValue >&	rArguments	)
502 {
503 	Reference< XFrame > 	xOldFrame	;
504 	Reference< XFrame > 	xNewFrame	;
505 
506 	{
507 		MutexGuard	aGuard ( m_aMutex ) ;
508 		xOldFrame = m_xFrame ;
509 	}
510 
511 	xNewFrame = Reference< XFrame >  ( impl_getMultiServiceFactory()->createInstance ( OUString::createFromAscii( "com.sun.star.frame.Frame" ) ), UNO_QUERY ) ;
512 	Reference< XDispatchProvider >  xDSP ( xNewFrame, UNO_QUERY ) ;
513 
514 	if (xDSP.is())
515 	{
516 		Reference< XWindow >  xWP ( xPeer, UNO_QUERY ) ;
517 		xNewFrame->initialize ( xWP ) ;
518 
519 		//	option
520 		//xFrame->setName( "WhatYouWant" );
521 
522 		Reference< XURLTransformer >  xTrans ( impl_getMultiServiceFactory()->createInstance ( OUString::createFromAscii( "com.sun.star.util.URLTransformer" ) ), UNO_QUERY ) ;
523 		if(xTrans.is())
524 		{
525 			// load file
526 			URL	aURL ;
527 
528 			aURL.Complete = rURL ;
529 			xTrans->parseStrict( aURL ) ;
530 
531 			Reference< XDispatch >  xDisp = xDSP->queryDispatch ( aURL, OUString (), FrameSearchFlag::SELF ) ;
532 			if (xDisp.is())
533 			{
534 				xDisp->dispatch ( aURL, rArguments ) ;
535 			}
536 		}
537 	}
538 
539 	// set the frame
540 	{
541 		MutexGuard aGuard ( m_aMutex ) ;
542 		m_xFrame = xNewFrame ;
543 	}
544 
545 	// notify the listeners
546 	sal_Int32	nFrameId = PROPERTYHANDLE_FRAME ;
547 	Any	aNewFrame ( &xNewFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
548 	Any	aOldFrame ( &xOldFrame, ::getCppuType((const Reference< XFrame >*)0) ) ;
549 
550 	fire ( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False ) ;
551 
552 	if (xOldFrame.is())
553 	{
554 		xOldFrame->dispose () ;
555 	}
556 }
557 
558 //____________________________________________________________________________________________________________
559 //	private method
560 //____________________________________________________________________________________________________________
561 
562 void FrameControl::impl_deleteFrame()
563 {
564 	Reference< XFrame >  xOldFrame;
565 	Reference< XFrame >  xNullFrame;
566 
567 	{
568         // do not dispose the frame in this guarded section (deadlock?)
569         MutexGuard aGuard( m_aMutex );
570         xOldFrame = m_xFrame;
571         m_xFrame = Reference< XFrame > ();
572 	}
573 
574 	// notify the listeners
575     sal_Int32 nFrameId = PROPERTYHANDLE_FRAME;
576 	Any aNewFrame( &xNullFrame, ::getCppuType((const Reference< XFrame >*)0) );
577 	Any aOldFrame( &xOldFrame, ::getCppuType((const Reference< XFrame >*)0) );
578 	fire( &nFrameId, &aNewFrame, &aOldFrame, 1, sal_False );
579 
580 	// dispose the frame
581 	if( xOldFrame.is() )
582 		xOldFrame->dispose();
583 }
584 
585 //____________________________________________________________________________________________________________
586 //	private method
587 //____________________________________________________________________________________________________________
588 
589 const Sequence< Property > FrameControl::impl_getStaticPropertyDescriptor()
590 {
591 	// All Properties of this implementation. The array must be sorted!
592 	static const Property pPropertys[PROPERTY_COUNT] =
593 	{
594 		Property( OUString::createFromAscii( PROPERTYNAME_COMPONENTURL		), PROPERTYHANDLE_COMPONENTURL		, ::getCppuType((const OUString*)0)					, PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED	),
595 		Property( OUString::createFromAscii( PROPERTYNAME_FRAME				), PROPERTYHANDLE_FRAME				, ::getCppuType((const Reference< XFrame >*)0)		, PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT	),
596 		Property( OUString::createFromAscii( PROPERTYNAME_LOADERARGUMENTS	), PROPERTYHANDLE_LOADERARGUMENTS	, ::getCppuType((const Sequence< PropertyValue >*)0), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED	)
597 	};
598 
599 	static const Sequence< Property > seqPropertys( pPropertys, PROPERTY_COUNT );
600 
601 	return seqPropertys ;
602 }
603 
604 }	// namespace unocontrols
605