Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "framecontrol.hxx"
21 :
22 : #include <com/sun/star/beans/PropertyAttribute.hpp>
23 : #include <com/sun/star/frame/Frame.hpp>
24 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
25 : #include <com/sun/star/frame/XDispatchProvider.hpp>
26 : #include <com/sun/star/frame/XDispatch.hpp>
27 : #include <com/sun/star/util/URLTransformer.hpp>
28 : #include <com/sun/star/util/XURLTransformer.hpp>
29 : #include <comphelper/processfactory.hxx>
30 : #include <cppuhelper/typeprovider.hxx>
31 : #include <osl/diagnose.h>
32 :
33 : // namespaces
34 :
35 : using namespace ::osl;
36 : using namespace ::cppu;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star::lang;
39 : using namespace ::com::sun::star::beans;
40 : using namespace ::com::sun::star::awt;
41 : using namespace ::com::sun::star::frame;
42 : using namespace ::com::sun::star::util;
43 :
44 : namespace unocontrols{
45 :
46 : // construct/destruct
47 :
48 0 : FrameControl::FrameControl( const Reference< XComponentContext >& rxContext)
49 : : BaseControl ( rxContext )
50 : , OBroadcastHelper ( m_aMutex )
51 : , OPropertySetHelper ( *(static_cast< OBroadcastHelper * >(this)) )
52 : , m_aInterfaceContainer ( m_aMutex )
53 0 : , m_aConnectionPointContainer ( m_aMutex )
54 : {
55 0 : }
56 :
57 0 : FrameControl::~FrameControl()
58 : {
59 0 : }
60 :
61 : // XInterface
62 :
63 0 : Any SAL_CALL FrameControl::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
64 : {
65 : // Attention:
66 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
67 0 : Any aReturn;
68 0 : Reference< XInterface > xDel = BaseControl::impl_getDelegator();
69 0 : if ( xDel.is() )
70 : {
71 : // If an delegator exist, forward question to his queryInterface.
72 : // Delegator will ask his own queryAggregation!
73 0 : aReturn = xDel->queryInterface( rType );
74 : }
75 : else
76 : {
77 : // If an delegator unknown, forward question to own queryAggregation.
78 0 : aReturn = queryAggregation( rType );
79 : }
80 :
81 0 : return aReturn;
82 : }
83 :
84 : // XInterface
85 :
86 0 : void SAL_CALL FrameControl::acquire() throw()
87 : {
88 : // Attention:
89 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
90 :
91 : // Forward to baseclass
92 0 : BaseControl::acquire();
93 0 : }
94 :
95 : // XInterface
96 :
97 0 : void SAL_CALL FrameControl::release() throw()
98 : {
99 : // Attention:
100 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
101 :
102 : // Forward to baseclass
103 0 : BaseControl::release();
104 0 : }
105 :
106 : // XTypeProvider
107 :
108 0 : Sequence< Type > SAL_CALL FrameControl::getTypes() throw( RuntimeException, std::exception )
109 : {
110 : // Optimize this method !
111 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
112 : // For the first call; pTypeCollection is NULL - for the second call pTypeCollection is different from NULL!
113 : static OTypeCollection* pTypeCollection = NULL;
114 :
115 0 : if ( pTypeCollection == NULL )
116 : {
117 : // Ready for multithreading; get global mutex for first call of this method only! see before
118 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
119 :
120 : // Control these pointer again ... it can be, that another instance will be faster then these!
121 0 : if ( pTypeCollection == NULL )
122 : {
123 : // Create a static typecollection ...
124 0 : static OTypeCollection aTypeCollection ( cppu::UnoType<XControlModel>::get(),
125 0 : cppu::UnoType<XControlContainer>::get(),
126 0 : cppu::UnoType<XConnectionPointContainer>::get(),
127 : BaseControl::getTypes()
128 0 : );
129 : // ... and set his address to static pointer!
130 0 : pTypeCollection = &aTypeCollection;
131 0 : }
132 : }
133 :
134 0 : return pTypeCollection->getTypes();
135 : }
136 :
137 : // XAggregation
138 :
139 0 : Any SAL_CALL FrameControl::queryAggregation( const Type& aType ) throw( RuntimeException, std::exception )
140 : {
141 : // Ask for my own supported interfaces ...
142 : // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
143 : Any aReturn ( ::cppu::queryInterface( aType ,
144 : static_cast< XControlModel* > ( this ) ,
145 : static_cast< XConnectionPointContainer* > ( this )
146 : )
147 0 : );
148 :
149 : // If searched interface not supported by this class ...
150 0 : if ( !aReturn.hasValue() )
151 : {
152 : // ... ask baseclasses.
153 0 : aReturn = OPropertySetHelper::queryInterface( aType );
154 0 : if ( !aReturn.hasValue() )
155 : {
156 0 : aReturn = BaseControl::queryAggregation( aType );
157 : }
158 : }
159 :
160 0 : return aReturn;
161 : }
162 :
163 : // XControl
164 :
165 0 : void SAL_CALL FrameControl::createPeer( const Reference< XToolkit >& xToolkit ,
166 : const Reference< XWindowPeer >& xParentPeer ) throw( RuntimeException, std::exception )
167 : {
168 0 : BaseControl::createPeer( xToolkit, xParentPeer );
169 0 : if ( impl_getPeerWindow().is() )
170 : {
171 0 : if( !m_sComponentURL.isEmpty() )
172 : {
173 0 : impl_createFrame( getPeer(), m_sComponentURL, m_seqLoaderArguments );
174 : }
175 : }
176 0 : }
177 :
178 : // XControl
179 :
180 0 : sal_Bool SAL_CALL FrameControl::setModel( const Reference< XControlModel >& /*xModel*/ ) throw( RuntimeException, std::exception )
181 : {
182 : // We have no model.
183 0 : return false;
184 : }
185 :
186 : // XControl
187 :
188 0 : Reference< XControlModel > SAL_CALL FrameControl::getModel() throw( RuntimeException, std::exception )
189 : {
190 : // We have no model.
191 0 : return Reference< XControlModel >();
192 : }
193 :
194 : // XControl
195 :
196 0 : void SAL_CALL FrameControl::dispose() throw( RuntimeException, std::exception )
197 : {
198 0 : impl_deleteFrame();
199 0 : BaseControl::dispose();
200 0 : }
201 :
202 : // XView
203 :
204 0 : sal_Bool SAL_CALL FrameControl::setGraphics( const Reference< XGraphics >& /*xDevice*/ ) throw( RuntimeException, std::exception )
205 : {
206 : // it is not possible to print this control
207 0 : return false;
208 : }
209 :
210 : // XView
211 :
212 0 : Reference< XGraphics > SAL_CALL FrameControl::getGraphics() throw( RuntimeException, std::exception )
213 : {
214 : // when its not posible to set graphics ! then its possible to return null
215 0 : return Reference< XGraphics >();
216 : }
217 :
218 : // XConnectionPointContainer
219 :
220 0 : Sequence< Type > SAL_CALL FrameControl::getConnectionPointTypes() throw( RuntimeException, std::exception )
221 : {
222 : // Forwarded to helper class
223 0 : return m_aConnectionPointContainer.getConnectionPointTypes();
224 : }
225 :
226 : // XConnectionPointContainer
227 :
228 0 : Reference< XConnectionPoint > SAL_CALL FrameControl::queryConnectionPoint( const Type& aType ) throw( RuntimeException, std::exception )
229 : {
230 : // Forwarded to helper class
231 0 : return m_aConnectionPointContainer.queryConnectionPoint( aType );
232 : }
233 :
234 : // XConnectionPointContainer
235 :
236 0 : void SAL_CALL FrameControl::advise( const Type& aType ,
237 : const Reference< XInterface >& xListener ) throw( RuntimeException, std::exception )
238 : {
239 : // Forwarded to helper class
240 0 : m_aConnectionPointContainer.advise( aType, xListener );
241 0 : }
242 :
243 : // XConnectionPointContainer
244 :
245 0 : void SAL_CALL FrameControl::unadvise( const Type& aType ,
246 : const Reference< XInterface >& xListener ) throw( RuntimeException, std::exception )
247 : {
248 : // Forwarded to helper class
249 0 : m_aConnectionPointContainer.unadvise( aType, xListener );
250 0 : }
251 :
252 : // impl but public method to register service
253 :
254 0 : const Sequence< OUString > FrameControl::impl_getStaticSupportedServiceNames()
255 : {
256 0 : MutexGuard aGuard( Mutex::getGlobalMutex() );
257 0 : Sequence< OUString > seqServiceNames( 1 );
258 0 : seqServiceNames.getArray() [0] = SERVICENAME_FRAMECONTROL;
259 0 : return seqServiceNames;
260 : }
261 :
262 : // impl but public method to register service
263 :
264 0 : const OUString FrameControl::impl_getStaticImplementationName()
265 : {
266 0 : return OUString(IMPLEMENTATIONNAME_FRAMECONTROL);
267 : }
268 :
269 : // OPropertySetHelper
270 :
271 0 : sal_Bool FrameControl::convertFastPropertyValue( Any& rConvertedValue ,
272 : Any& rOldValue ,
273 : sal_Int32 nHandle ,
274 : const Any& rValue ) throw( IllegalArgumentException )
275 : {
276 0 : bool bReturn = false;
277 0 : switch (nHandle)
278 : {
279 0 : case PROPERTYHANDLE_COMPONENTURL : rConvertedValue = rValue;
280 0 : rOldValue <<= m_sComponentURL;
281 0 : bReturn = true;
282 0 : break;
283 :
284 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rConvertedValue = rValue;
285 0 : rOldValue <<= m_seqLoaderArguments;
286 0 : bReturn = true;
287 0 : break;
288 : }
289 :
290 0 : if ( !bReturn )
291 : {
292 0 : throw IllegalArgumentException();
293 : }
294 :
295 0 : return bReturn;
296 : }
297 :
298 : // OPropertySetHelper
299 :
300 0 : void FrameControl::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
301 : const Any& rValue )
302 : throw ( ::com::sun::star::uno::Exception, std::exception )
303 : {
304 : // this method only set the value
305 0 : MutexGuard aGuard (m_aMutex);
306 0 : switch (nHandle)
307 : {
308 0 : case PROPERTYHANDLE_COMPONENTURL : rValue >>= m_sComponentURL;
309 0 : if (getPeer().is())
310 : {
311 0 : impl_createFrame ( getPeer(), m_sComponentURL, m_seqLoaderArguments );
312 : }
313 0 : break;
314 :
315 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rValue >>= m_seqLoaderArguments;
316 0 : break;
317 :
318 : default : OSL_ENSURE ( nHandle == -1, "This is an invalid property handle." );
319 0 : }
320 0 : }
321 :
322 : // OPropertySetHelper
323 :
324 0 : void FrameControl::getFastPropertyValue( Any& rRet ,
325 : sal_Int32 nHandle ) const
326 : {
327 0 : MutexGuard aGuard ( Mutex::getGlobalMutex() );
328 :
329 0 : switch (nHandle)
330 : {
331 0 : case PROPERTYHANDLE_COMPONENTURL : rRet <<= m_sComponentURL;
332 0 : break;
333 :
334 0 : case PROPERTYHANDLE_LOADERARGUMENTS : rRet <<= m_seqLoaderArguments;
335 0 : break;
336 :
337 0 : case PROPERTYHANDLE_FRAME : rRet <<= m_xFrame;
338 0 : break;
339 :
340 : default : OSL_ENSURE ( nHandle == -1, "This is an invalid property handle." );
341 0 : }
342 0 : }
343 :
344 : // OPropertySetHelper
345 :
346 0 : IPropertyArrayHelper& FrameControl::getInfoHelper()
347 : {
348 : // Create a table that map names to index values.
349 : static OPropertyArrayHelper* pInfo;
350 :
351 0 : if (!pInfo)
352 : {
353 : // global method must be guarded
354 0 : MutexGuard aGuard ( Mutex::getGlobalMutex() );
355 :
356 0 : if (!pInfo)
357 : {
358 0 : pInfo = new OPropertyArrayHelper( impl_getStaticPropertyDescriptor(), true );
359 0 : }
360 : }
361 :
362 0 : return *pInfo;
363 : }
364 :
365 : // OPropertySetHelper
366 :
367 0 : Reference< XPropertySetInfo > SAL_CALL FrameControl::getPropertySetInfo() throw( RuntimeException, std::exception )
368 : {
369 : // Optimize this method !
370 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
371 : // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
372 : static Reference< XPropertySetInfo >* pInfo = (Reference< XPropertySetInfo >*)0;
373 0 : if ( pInfo == (Reference< XPropertySetInfo >*)0 )
374 : {
375 : // Ready for multithreading
376 0 : MutexGuard aGuard ( Mutex::getGlobalMutex () );
377 : // Control this pointer again, another instance can be faster then these!
378 0 : if ( pInfo == (Reference< XPropertySetInfo >*)0 )
379 : {
380 : // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
381 : // (Use method "getInfoHelper()".)
382 0 : static Reference< XPropertySetInfo > xInfo ( createPropertySetInfo ( getInfoHelper () ) );
383 0 : pInfo = &xInfo;
384 0 : }
385 : }
386 0 : return ( *pInfo );
387 : }
388 :
389 : // BaseControl
390 :
391 0 : WindowDescriptor* FrameControl::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
392 : {
393 0 : WindowDescriptor* pDescriptor = new WindowDescriptor;
394 :
395 0 : pDescriptor->Type = WindowClass_CONTAINER;
396 0 : pDescriptor->ParentIndex = -1;
397 0 : pDescriptor->Parent = xParentPeer;
398 0 : pDescriptor->Bounds = getPosSize ();
399 0 : pDescriptor->WindowAttributes = 0;
400 :
401 0 : return pDescriptor;
402 : }
403 :
404 : // private method
405 :
406 0 : void FrameControl::impl_createFrame( const Reference< XWindowPeer >& xPeer ,
407 : const OUString& rURL ,
408 : const Sequence< PropertyValue >& rArguments )
409 : {
410 0 : Reference< XFrame2 > xOldFrame;
411 0 : Reference< XFrame2 > xNewFrame;
412 :
413 : {
414 0 : MutexGuard aGuard ( m_aMutex );
415 0 : xOldFrame = m_xFrame;
416 : }
417 :
418 0 : xNewFrame = Frame::create( impl_getComponentContext() );
419 :
420 0 : Reference< XWindow > xWP ( xPeer, UNO_QUERY );
421 0 : xNewFrame->initialize ( xWP );
422 :
423 : // option
424 : //xFrame->setName( "WhatYouWant" );
425 :
426 0 : Reference< XURLTransformer > xTrans = URLTransformer::create( impl_getComponentContext() );
427 : // load file
428 0 : URL aURL;
429 0 : aURL.Complete = rURL;
430 0 : xTrans->parseStrict( aURL );
431 :
432 0 : Reference< XDispatch > xDisp = xNewFrame->queryDispatch ( aURL, OUString (), FrameSearchFlag::SELF );
433 0 : if (xDisp.is())
434 : {
435 0 : xDisp->dispatch ( aURL, rArguments );
436 : }
437 :
438 : // set the frame
439 : {
440 0 : MutexGuard aGuard ( m_aMutex );
441 0 : m_xFrame = xNewFrame;
442 : }
443 :
444 : // notify the listeners
445 0 : sal_Int32 nFrameId = PROPERTYHANDLE_FRAME;
446 0 : Any aNewFrame ( &xNewFrame, cppu::UnoType<XFrame>::get());
447 0 : Any aOldFrame ( &xOldFrame, cppu::UnoType<XFrame>::get());
448 :
449 0 : fire ( &nFrameId, &aNewFrame, &aOldFrame, 1, false );
450 :
451 0 : if (xOldFrame.is())
452 : {
453 0 : xOldFrame->dispose ();
454 0 : }
455 0 : }
456 :
457 : // private method
458 :
459 0 : void FrameControl::impl_deleteFrame()
460 : {
461 0 : Reference< XFrame2 > xOldFrame;
462 0 : Reference< XFrame2 > xNullFrame;
463 :
464 : {
465 : // do not dispose the frame in this guarded section (deadlock?)
466 0 : MutexGuard aGuard( m_aMutex );
467 0 : xOldFrame = m_xFrame;
468 0 : m_xFrame = Reference< XFrame2 > ();
469 : }
470 :
471 : // notify the listeners
472 0 : sal_Int32 nFrameId = PROPERTYHANDLE_FRAME;
473 0 : Any aNewFrame( &xNullFrame, cppu::UnoType<XFrame2>::get());
474 0 : Any aOldFrame( &xOldFrame, cppu::UnoType<XFrame2>::get());
475 0 : fire( &nFrameId, &aNewFrame, &aOldFrame, 1, false );
476 :
477 : // dispose the frame
478 0 : if( xOldFrame.is() )
479 0 : xOldFrame->dispose();
480 0 : }
481 :
482 : // private method
483 :
484 0 : const Sequence< Property > FrameControl::impl_getStaticPropertyDescriptor()
485 : {
486 : // All Properties of this implementation. The array must be sorted!
487 : static const Property pPropertys[PROPERTY_COUNT] =
488 : {
489 0 : Property( PROPERTYNAME_COMPONENTURL, PROPERTYHANDLE_COMPONENTURL, cppu::UnoType<OUString>::get(), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
490 0 : Property( PROPERTYNAME_FRAME, PROPERTYHANDLE_FRAME, cppu::UnoType<XFrame>::get(), PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT ),
491 0 : Property( PROPERTYNAME_LOADERARGUMENTS, PROPERTYHANDLE_LOADERARGUMENTS, ::getCppuType((const Sequence< PropertyValue >*)0), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED )
492 0 : };
493 :
494 0 : static const Sequence< Property > seqPropertys( pPropertys, PROPERTY_COUNT );
495 :
496 0 : return seqPropertys;
497 : }
498 :
499 : } // namespace unocontrols
500 :
501 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|