bump product version to 4.1.6.2
[LibreOffice.git] / framework / source / helper / uiconfigelementwrapperbase.cxx
bloba70340ad22d8ec86e842e6edf228d8b0c0d86280
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <helper/uiconfigelementwrapperbase.hxx>
21 #include <general.h>
22 #include <properties.h>
23 #include <threadhelp/resetableguard.hxx>
24 #include <uielement/constitemcontainer.hxx>
25 #include <uielement/rootitemcontainer.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/ui/XUIConfiguration.hpp>
32 #include <vcl/svapp.hxx>
33 #include <rtl/logfile.hxx>
35 const int UIELEMENT_PROPHANDLE_CONFIGSOURCE = 1;
36 const int UIELEMENT_PROPHANDLE_FRAME = 2;
37 const int UIELEMENT_PROPHANDLE_PERSISTENT = 3;
38 const int UIELEMENT_PROPHANDLE_RESOURCEURL = 4;
39 const int UIELEMENT_PROPHANDLE_TYPE = 5;
40 const int UIELEMENT_PROPHANDLE_XMENUBAR = 6;
41 const int UIELEMENT_PROPHANDLE_CONFIGLISTENER = 7;
42 const int UIELEMENT_PROPHANDLE_NOCLOSE = 8;
43 const int UIELEMENT_PROPCOUNT = 8;
44 const char UIELEMENT_PROPNAME_CONFIGLISTENER[] = "ConfigListener";
45 const char UIELEMENT_PROPNAME_CONFIGSOURCE[] = "ConfigurationSource";
46 const char UIELEMENT_PROPNAME_FRAME[] = "Frame";
47 const char UIELEMENT_PROPNAME_PERSISTENT[] = "Persistent";
48 const char UIELEMENT_PROPNAME_RESOURCEURL[] = "ResourceURL";
49 const char UIELEMENT_PROPNAME_TYPE[] = "Type";
50 const char UIELEMENT_PROPNAME_XMENUBAR[] = "XMenuBar";
51 const char UIELEMENT_PROPNAME_NOCLOSE[] = "NoClose";
52 using namespace com::sun::star::beans;
53 using namespace com::sun::star::uno;
54 using namespace com::sun::star::frame;
55 using namespace com::sun::star::lang;
56 using namespace com::sun::star::container;
57 using namespace ::com::sun::star::ui;
59 namespace framework
62 //*****************************************************************************************************************
63 // XInterface, XTypeProvider
64 //*****************************************************************************************************************
65 DEFINE_XINTERFACE_10 ( UIConfigElementWrapperBase ,
66 OWeakObject ,
67 DIRECT_INTERFACE( ::com::sun::star::lang::XTypeProvider ),
68 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElement ),
69 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElementSettings ),
70 DIRECT_INTERFACE( ::com::sun::star::beans::XMultiPropertySet ),
71 DIRECT_INTERFACE( ::com::sun::star::beans::XFastPropertySet ),
72 DIRECT_INTERFACE( ::com::sun::star::beans::XPropertySet ),
73 DIRECT_INTERFACE( ::com::sun::star::lang::XInitialization ),
74 DIRECT_INTERFACE( ::com::sun::star::lang::XComponent ),
75 DIRECT_INTERFACE( ::com::sun::star::util::XUpdatable ),
76 DIRECT_INTERFACE( ::com::sun::star::ui::XUIConfigurationListener )
79 DEFINE_XTYPEPROVIDER_10 ( UIConfigElementWrapperBase ,
80 ::com::sun::star::lang::XTypeProvider ,
81 ::com::sun::star::ui::XUIElement ,
82 ::com::sun::star::ui::XUIElementSettings ,
83 ::com::sun::star::beans::XMultiPropertySet ,
84 ::com::sun::star::beans::XFastPropertySet ,
85 ::com::sun::star::beans::XPropertySet ,
86 ::com::sun::star::lang::XInitialization ,
87 ::com::sun::star::lang::XComponent ,
88 ::com::sun::star::util::XUpdatable ,
89 ::com::sun::star::ui::XUIConfigurationListener
92 UIConfigElementWrapperBase::UIConfigElementWrapperBase( sal_Int16 nType,const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xServiceFactory )
93 : ThreadHelpBase ( &Application::GetSolarMutex() )
94 , ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aLock.getShareableOslMutex() )
95 , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper* >(this)) )
96 , ::cppu::OWeakObject ( )
97 , m_nType ( nType )
98 , m_bPersistent ( sal_True )
99 , m_bInitialized ( sal_False )
100 , m_bConfigListener ( sal_False )
101 , m_bConfigListening ( sal_False )
102 , m_bDisposed ( sal_False )
103 , m_bNoClose ( sal_False )
104 , m_xServiceFactory ( _xServiceFactory )
105 , m_aListenerContainer ( m_aLock.getShareableOslMutex() )
109 UIConfigElementWrapperBase::~UIConfigElementWrapperBase()
113 // XComponent
114 void SAL_CALL UIConfigElementWrapperBase::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException)
116 m_aListenerContainer.addInterface( ::getCppuType( ( const css::uno::Reference< css::lang::XEventListener >* ) NULL ), xListener );
119 void SAL_CALL UIConfigElementWrapperBase::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException)
121 m_aListenerContainer.removeInterface( ::getCppuType( ( const css::uno::Reference< css::lang::XEventListener >* ) NULL ), aListener );
124 // XEventListener
125 void SAL_CALL UIConfigElementWrapperBase::disposing( const EventObject& )
126 throw( RuntimeException )
128 ResetableGuard aLock( m_aLock );
129 m_xConfigSource.clear();
132 void SAL_CALL UIConfigElementWrapperBase::initialize( const Sequence< Any >& aArguments )
133 throw ( Exception, RuntimeException )
135 ResetableGuard aLock( m_aLock );
137 if ( !m_bInitialized )
139 for ( sal_Int32 n = 0; n < aArguments.getLength(); n++ )
141 PropertyValue aPropValue;
142 if ( aArguments[n] >>= aPropValue )
144 if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGSOURCE )
145 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGSOURCE, aPropValue.Value );
146 else if ( aPropValue.Name == UIELEMENT_PROPNAME_FRAME )
147 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_FRAME, aPropValue.Value );
148 else if ( aPropValue.Name == UIELEMENT_PROPNAME_PERSISTENT )
149 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_PERSISTENT, aPropValue.Value );
150 else if ( aPropValue.Name == UIELEMENT_PROPNAME_RESOURCEURL )
151 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_RESOURCEURL, aPropValue.Value );
152 else if ( aPropValue.Name == UIELEMENT_PROPNAME_TYPE )
153 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_TYPE, aPropValue.Value );
154 else if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGLISTENER )
155 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGLISTENER, aPropValue.Value );
156 else if ( aPropValue.Name == UIELEMENT_PROPNAME_NOCLOSE )
157 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_NOCLOSE, aPropValue.Value );
161 m_bInitialized = sal_True;
165 // XUpdatable
166 void SAL_CALL UIConfigElementWrapperBase::update() throw (::com::sun::star::uno::RuntimeException)
168 // can be implemented by derived class
171 void SAL_CALL UIConfigElementWrapperBase::elementInserted( const ::com::sun::star::ui::ConfigurationEvent& ) throw (::com::sun::star::uno::RuntimeException)
173 // can be implemented by derived class
176 void SAL_CALL UIConfigElementWrapperBase::elementRemoved( const ::com::sun::star::ui::ConfigurationEvent& ) throw (::com::sun::star::uno::RuntimeException)
178 // can be implemented by derived class
181 void SAL_CALL UIConfigElementWrapperBase::elementReplaced( const ::com::sun::star::ui::ConfigurationEvent& ) throw (::com::sun::star::uno::RuntimeException)
183 // can be implemented by derived class
186 // XPropertySet helper
187 sal_Bool SAL_CALL UIConfigElementWrapperBase::convertFastPropertyValue( Any& aConvertedValue ,
188 Any& aOldValue ,
189 sal_Int32 nHandle ,
190 const Any& aValue ) throw( com::sun::star::lang::IllegalArgumentException )
192 // Initialize state with sal_False !!!
193 // (Handle can be invalid)
194 sal_Bool bReturn = sal_False;
196 switch( nHandle )
198 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
199 bReturn = PropHelper::willPropertyBeChanged(
200 com::sun::star::uno::makeAny(m_bConfigListener),
201 aValue,
202 aOldValue,
203 aConvertedValue);
204 break;
206 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
207 bReturn = PropHelper::willPropertyBeChanged(
208 com::sun::star::uno::makeAny(m_xConfigSource),
209 aValue,
210 aOldValue,
211 aConvertedValue);
212 break;
214 case UIELEMENT_PROPHANDLE_FRAME:
216 Reference< XFrame > xFrame( m_xWeakFrame );
217 bReturn = PropHelper::willPropertyBeChanged(
218 com::sun::star::uno::makeAny(xFrame),
219 aValue,
220 aOldValue,
221 aConvertedValue);
223 break;
225 case UIELEMENT_PROPHANDLE_PERSISTENT:
226 bReturn = PropHelper::willPropertyBeChanged(
227 com::sun::star::uno::makeAny(m_bPersistent),
228 aValue,
229 aOldValue,
230 aConvertedValue);
231 break;
233 case UIELEMENT_PROPHANDLE_RESOURCEURL:
234 bReturn = PropHelper::willPropertyBeChanged(
235 com::sun::star::uno::makeAny(m_aResourceURL),
236 aValue,
237 aOldValue,
238 aConvertedValue);
239 break;
241 case UIELEMENT_PROPHANDLE_TYPE :
242 bReturn = PropHelper::willPropertyBeChanged(
243 com::sun::star::uno::makeAny(m_nType),
244 aValue,
245 aOldValue,
246 aConvertedValue);
247 break;
249 case UIELEMENT_PROPHANDLE_XMENUBAR :
250 bReturn = PropHelper::willPropertyBeChanged(
251 com::sun::star::uno::makeAny(m_xMenuBar),
252 aValue,
253 aOldValue,
254 aConvertedValue);
255 break;
257 case UIELEMENT_PROPHANDLE_NOCLOSE:
258 bReturn = PropHelper::willPropertyBeChanged(
259 com::sun::star::uno::makeAny(m_bNoClose),
260 aValue,
261 aOldValue,
262 aConvertedValue);
263 break;
266 // Return state of operation.
267 return bReturn ;
270 void SAL_CALL UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
271 const com::sun::star::uno::Any& aValue ) throw( com::sun::star::uno::Exception )
273 switch( nHandle )
275 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
277 bool bBool( m_bConfigListener );
278 aValue >>= bBool;
279 if ( m_bConfigListener != bBool )
281 if ( m_bConfigListening )
283 if ( m_xConfigSource.is() && !bBool )
287 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
288 if ( xUIConfig.is() )
290 xUIConfig->removeConfigurationListener( Reference< XUIConfigurationListener >( static_cast< OWeakObject* >( this ), UNO_QUERY ));
291 m_bConfigListening = sal_False;
294 catch ( const Exception& )
299 else
301 if ( m_xConfigSource.is() && bBool )
305 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
306 if ( xUIConfig.is() )
308 xUIConfig->addConfigurationListener( Reference< XUIConfigurationListener >( static_cast< OWeakObject* >( this ), UNO_QUERY ));
309 m_bConfigListening = sal_True;
312 catch ( const Exception& )
318 m_bConfigListener = bBool;
321 break;
322 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
323 aValue >>= m_xConfigSource;
324 break;
325 case UIELEMENT_PROPHANDLE_FRAME:
327 Reference< XFrame > xFrame;
329 aValue >>= xFrame;
330 m_xWeakFrame = xFrame;
331 break;
333 case UIELEMENT_PROPHANDLE_PERSISTENT:
335 sal_Bool bBool( m_bPersistent );
336 aValue >>= bBool;
337 m_bPersistent = bBool;
338 break;
340 case UIELEMENT_PROPHANDLE_RESOURCEURL:
341 aValue >>= m_aResourceURL;
342 break;
343 case UIELEMENT_PROPHANDLE_TYPE:
344 aValue >>= m_nType;
345 break;
346 case UIELEMENT_PROPHANDLE_XMENUBAR:
347 aValue >>= m_xMenuBar;
348 break;
349 case UIELEMENT_PROPHANDLE_NOCLOSE:
351 sal_Bool bBool( m_bNoClose );
352 aValue >>= bBool;
353 m_bNoClose = bBool;
354 break;
359 void SAL_CALL UIConfigElementWrapperBase::getFastPropertyValue( com::sun::star::uno::Any& aValue ,
360 sal_Int32 nHandle ) const
362 switch( nHandle )
364 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
365 aValue <<= m_bConfigListener;
366 break;
367 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
368 aValue <<= m_xConfigSource;
369 break;
370 case UIELEMENT_PROPHANDLE_FRAME:
372 Reference< XFrame > xFrame( m_xWeakFrame );
373 aValue <<= xFrame;
374 break;
376 case UIELEMENT_PROPHANDLE_PERSISTENT:
377 aValue <<= m_bPersistent;
378 break;
379 case UIELEMENT_PROPHANDLE_RESOURCEURL:
380 aValue <<= m_aResourceURL;
381 break;
382 case UIELEMENT_PROPHANDLE_TYPE:
383 aValue <<= m_nType;
384 break;
385 case UIELEMENT_PROPHANDLE_XMENUBAR:
386 aValue <<= m_xMenuBar;
387 break;
388 case UIELEMENT_PROPHANDLE_NOCLOSE:
389 aValue <<= m_bNoClose;
390 break;
394 ::cppu::IPropertyArrayHelper& SAL_CALL UIConfigElementWrapperBase::getInfoHelper()
396 // Optimize this method !
397 // We initialize a static variable only one time. And we don't must use a mutex at every call!
398 // For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
399 static ::cppu::OPropertyArrayHelper* pInfoHelper = NULL;
401 if( pInfoHelper == NULL )
403 // Ready for multithreading
404 osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
406 // Control this pointer again, another instance can be faster then these!
407 if( pInfoHelper == NULL )
409 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
410 // "impl_getStaticPropertyDescriptor" is a non exported and static funtion, who will define a static propertytable.
411 // "sal_True" say: Table is sorted by name.
412 static ::cppu::OPropertyArrayHelper aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True );
413 pInfoHelper = &aInfoHelper;
417 return(*pInfoHelper);
420 com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo > SAL_CALL UIConfigElementWrapperBase::getPropertySetInfo() throw (::com::sun::star::uno::RuntimeException)
422 // Optimize this method !
423 // We initialize a static variable only one time. And we don't must use a mutex at every call!
424 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
425 static com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo >* pInfo = NULL;
427 if( pInfo == NULL )
429 // Ready for multithreading
430 osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() ) ;
431 // Control this pointer again, another instance can be faster then these!
432 if( pInfo == NULL )
434 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
435 // (Use method "getInfoHelper()".)
436 static com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
437 pInfo = &xInfo;
441 return (*pInfo);
444 const com::sun::star::uno::Sequence< com::sun::star::beans::Property > UIConfigElementWrapperBase::impl_getStaticPropertyDescriptor()
446 // Create property array to initialize sequence!
447 // Table of all predefined properties of this class. Its used from OPropertySetHelper-class!
448 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
449 // It's necessary for methods of OPropertySetHelper.
450 // ATTENTION:
451 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
453 const com::sun::star::beans::Property pProperties[] =
455 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_CONFIGLISTENER), UIELEMENT_PROPHANDLE_CONFIGLISTENER , ::getCppuType((const sal_Bool*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT ),
456 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_CONFIGSOURCE), UIELEMENT_PROPHANDLE_CONFIGSOURCE , ::getCppuType((const Reference< ::com::sun::star::ui::XUIConfigurationManager >*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT ),
457 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_FRAME), UIELEMENT_PROPHANDLE_FRAME , ::getCppuType((const Reference< com::sun::star::frame::XFrame >*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY ),
458 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_NOCLOSE), UIELEMENT_PROPHANDLE_NOCLOSE , ::getCppuType((const sal_Bool*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT ),
459 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_PERSISTENT), UIELEMENT_PROPHANDLE_PERSISTENT , ::getCppuType((const sal_Bool*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT ),
460 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_RESOURCEURL), UIELEMENT_PROPHANDLE_RESOURCEURL , ::getCppuType((const OUString*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY ),
461 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_TYPE), UIELEMENT_PROPHANDLE_TYPE , ::getCppuType((const OUString*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY ),
462 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_XMENUBAR), UIELEMENT_PROPHANDLE_XMENUBAR , ::getCppuType((const Reference< com::sun::star::awt::XMenuBar >*)NULL), com::sun::star::beans::PropertyAttribute::TRANSIENT | com::sun::star::beans::PropertyAttribute::READONLY )
464 // Use it to initialize sequence!
465 const com::sun::star::uno::Sequence< com::sun::star::beans::Property > lPropertyDescriptor( pProperties, UIELEMENT_PROPCOUNT );
466 // Return "PropertyDescriptor"
467 return lPropertyDescriptor;
469 void SAL_CALL UIConfigElementWrapperBase::setSettings( const Reference< XIndexAccess >& xSettings ) throw ( RuntimeException )
471 ResetableGuard aLock( m_aLock );
474 if ( xSettings.is() )
476 // Create a copy of the data if the container is not const
477 Reference< XIndexReplace > xReplace( xSettings, UNO_QUERY );
478 if ( xReplace.is() )
479 m_xConfigData = Reference< XIndexAccess >( static_cast< OWeakObject * >( new ConstItemContainer( xSettings ) ), UNO_QUERY );
480 else
481 m_xConfigData = xSettings;
483 if ( m_xConfigSource.is() && m_bPersistent )
485 OUString aResourceURL( m_aResourceURL );
486 Reference< XUIConfigurationManager > xUICfgMgr( m_xConfigSource );
488 aLock.unlock();
492 xUICfgMgr->replaceSettings( aResourceURL, m_xConfigData );
494 catch( const NoSuchElementException& )
498 else if ( !m_bPersistent )
500 // Transient menubar => Fill menubar with new data
501 impl_fillNewData();
505 void UIConfigElementWrapperBase::impl_fillNewData()
508 Reference< XIndexAccess > SAL_CALL UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable ) throw ( RuntimeException )
510 ResetableGuard aLock( m_aLock );
513 if ( bWriteable )
514 return Reference< XIndexAccess >( static_cast< OWeakObject * >( new RootItemContainer( m_xConfigData ) ), UNO_QUERY );
516 return m_xConfigData;
519 Reference< XFrame > SAL_CALL UIConfigElementWrapperBase::getFrame() throw (RuntimeException)
521 ResetableGuard aLock( m_aLock );
522 Reference< XFrame > xFrame( m_xWeakFrame );
523 return xFrame;
526 OUString SAL_CALL UIConfigElementWrapperBase::getResourceURL() throw (RuntimeException)
528 ResetableGuard aLock( m_aLock );
529 return m_aResourceURL;
532 ::sal_Int16 SAL_CALL UIConfigElementWrapperBase::getType() throw (RuntimeException)
534 ResetableGuard aLock( m_aLock );
535 return m_nType;
540 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */