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