1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
62 //*****************************************************************************************************************
63 // XInterface, XTypeProvider
64 //*****************************************************************************************************************
65 DEFINE_XINTERFACE_10 ( UIConfigElementWrapperBase
,
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 ( )
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()
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
);
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
;
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
,
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
;
198 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
199 bReturn
= PropHelper::willPropertyBeChanged(
200 com::sun::star::uno::makeAny(m_bConfigListener
),
206 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
207 bReturn
= PropHelper::willPropertyBeChanged(
208 com::sun::star::uno::makeAny(m_xConfigSource
),
214 case UIELEMENT_PROPHANDLE_FRAME
:
216 Reference
< XFrame
> xFrame( m_xWeakFrame
);
217 bReturn
= PropHelper::willPropertyBeChanged(
218 com::sun::star::uno::makeAny(xFrame
),
225 case UIELEMENT_PROPHANDLE_PERSISTENT
:
226 bReturn
= PropHelper::willPropertyBeChanged(
227 com::sun::star::uno::makeAny(m_bPersistent
),
233 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
234 bReturn
= PropHelper::willPropertyBeChanged(
235 com::sun::star::uno::makeAny(m_aResourceURL
),
241 case UIELEMENT_PROPHANDLE_TYPE
:
242 bReturn
= PropHelper::willPropertyBeChanged(
243 com::sun::star::uno::makeAny(m_nType
),
249 case UIELEMENT_PROPHANDLE_XMENUBAR
:
250 bReturn
= PropHelper::willPropertyBeChanged(
251 com::sun::star::uno::makeAny(m_xMenuBar
),
257 case UIELEMENT_PROPHANDLE_NOCLOSE
:
258 bReturn
= PropHelper::willPropertyBeChanged(
259 com::sun::star::uno::makeAny(m_bNoClose
),
266 // Return state of operation.
270 void SAL_CALL
UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
,
271 const com::sun::star::uno::Any
& aValue
) throw( com::sun::star::uno::Exception
)
275 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
277 bool bBool( m_bConfigListener
);
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
& )
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
;
322 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
323 aValue
>>= m_xConfigSource
;
325 case UIELEMENT_PROPHANDLE_FRAME
:
327 Reference
< XFrame
> xFrame
;
330 m_xWeakFrame
= xFrame
;
333 case UIELEMENT_PROPHANDLE_PERSISTENT
:
335 sal_Bool
bBool( m_bPersistent
);
337 m_bPersistent
= bBool
;
340 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
341 aValue
>>= m_aResourceURL
;
343 case UIELEMENT_PROPHANDLE_TYPE
:
346 case UIELEMENT_PROPHANDLE_XMENUBAR
:
347 aValue
>>= m_xMenuBar
;
349 case UIELEMENT_PROPHANDLE_NOCLOSE
:
351 sal_Bool
bBool( m_bNoClose
);
359 void SAL_CALL
UIConfigElementWrapperBase::getFastPropertyValue( com::sun::star::uno::Any
& aValue
,
360 sal_Int32 nHandle
) const
364 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
365 aValue
<<= m_bConfigListener
;
367 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
368 aValue
<<= m_xConfigSource
;
370 case UIELEMENT_PROPHANDLE_FRAME
:
372 Reference
< XFrame
> xFrame( m_xWeakFrame
);
376 case UIELEMENT_PROPHANDLE_PERSISTENT
:
377 aValue
<<= m_bPersistent
;
379 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
380 aValue
<<= m_aResourceURL
;
382 case UIELEMENT_PROPHANDLE_TYPE
:
385 case UIELEMENT_PROPHANDLE_XMENUBAR
:
386 aValue
<<= m_xMenuBar
;
388 case UIELEMENT_PROPHANDLE_NOCLOSE
:
389 aValue
<<= m_bNoClose
;
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
;
429 // Ready for multithreading
430 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() ) ;
431 // Control this pointer again, another instance can be faster then these!
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() ) );
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.
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
);
479 m_xConfigData
= Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( new ConstItemContainer( xSettings
) ), UNO_QUERY
);
481 m_xConfigData
= xSettings
;
483 if ( m_xConfigSource
.is() && m_bPersistent
)
485 OUString
aResourceURL( m_aResourceURL
);
486 Reference
< XUIConfigurationManager
> xUICfgMgr( m_xConfigSource
);
492 xUICfgMgr
->replaceSettings( aResourceURL
, m_xConfigData
);
494 catch( const NoSuchElementException
& )
498 else if ( !m_bPersistent
)
500 // Transient menubar => Fill menubar with new data
505 void UIConfigElementWrapperBase::impl_fillNewData()
508 Reference
< XIndexAccess
> SAL_CALL
UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable
) throw ( RuntimeException
)
510 ResetableGuard
aLock( m_aLock
);
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
);
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
);
540 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */