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 <uielement/constitemcontainer.hxx>
24 #include <uielement/rootitemcontainer.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/beans/PropertyValue.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/ui/XUIConfiguration.hpp>
31 #include <vcl/svapp.hxx>
32 #include <comphelper/sequence.hxx>
34 const int UIELEMENT_PROPHANDLE_CONFIGSOURCE
= 1;
35 const int UIELEMENT_PROPHANDLE_FRAME
= 2;
36 const int UIELEMENT_PROPHANDLE_PERSISTENT
= 3;
37 const int UIELEMENT_PROPHANDLE_RESOURCEURL
= 4;
38 const int UIELEMENT_PROPHANDLE_TYPE
= 5;
39 const int UIELEMENT_PROPHANDLE_XMENUBAR
= 6;
40 const int UIELEMENT_PROPHANDLE_CONFIGLISTENER
= 7;
41 const int UIELEMENT_PROPHANDLE_NOCLOSE
= 8;
42 const int UIELEMENT_PROPCOUNT
= 8;
43 const char UIELEMENT_PROPNAME_CONFIGLISTENER
[] = "ConfigListener";
44 const char UIELEMENT_PROPNAME_CONFIGSOURCE
[] = "ConfigurationSource";
45 const char UIELEMENT_PROPNAME_FRAME
[] = "Frame";
46 const char UIELEMENT_PROPNAME_PERSISTENT
[] = "Persistent";
47 const char UIELEMENT_PROPNAME_RESOURCEURL
[] = "ResourceURL";
48 const char UIELEMENT_PROPNAME_TYPE
[] = "Type";
49 const char UIELEMENT_PROPNAME_XMENUBAR
[] = "XMenuBar";
50 const char UIELEMENT_PROPNAME_NOCLOSE
[] = "NoClose";
51 using namespace com::sun::star::beans
;
52 using namespace com::sun::star::uno
;
53 using namespace com::sun::star::frame
;
54 using namespace com::sun::star::lang
;
55 using namespace com::sun::star::container
;
56 using namespace ::com::sun::star::ui
;
61 UIConfigElementWrapperBase::UIConfigElementWrapperBase( sal_Int16 nType
)
62 : ::cppu::OBroadcastHelperVar
< ::cppu::OMultiTypeInterfaceContainerHelper
, ::cppu::OMultiTypeInterfaceContainerHelper::keyType
>( m_aMutex
)
63 , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper
* >(this)) )
65 , m_bPersistent ( true )
66 , m_bInitialized ( false )
67 , m_bConfigListener ( false )
68 , m_bConfigListening ( false )
69 , m_bDisposed ( false )
70 , m_bNoClose ( false )
71 , m_aListenerContainer ( m_aMutex
)
75 UIConfigElementWrapperBase::~UIConfigElementWrapperBase()
79 Any SAL_CALL
UIConfigElementWrapperBase::queryInterface( const Type
& _rType
) throw(RuntimeException
, std::exception
)
81 Any aRet
= UIConfigElementWrapperBase_BASE::queryInterface( _rType
);
82 if ( !aRet
.hasValue() )
83 aRet
= OPropertySetHelper::queryInterface( _rType
);
87 Sequence
< Type
> SAL_CALL
UIConfigElementWrapperBase::getTypes( ) throw(RuntimeException
, std::exception
)
89 return comphelper::concatSequences(
90 UIConfigElementWrapperBase_BASE::getTypes(),
91 ::cppu::OPropertySetHelper::getTypes()
96 void SAL_CALL
UIConfigElementWrapperBase::addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
98 m_aListenerContainer
.addInterface( cppu::UnoType
<css::lang::XEventListener
>::get(), xListener
);
101 void SAL_CALL
UIConfigElementWrapperBase::removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& aListener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
103 m_aListenerContainer
.removeInterface( cppu::UnoType
<css::lang::XEventListener
>::get(), aListener
);
107 void SAL_CALL
UIConfigElementWrapperBase::disposing( const EventObject
& )
108 throw( RuntimeException
, std::exception
)
111 m_xConfigSource
.clear();
114 void SAL_CALL
UIConfigElementWrapperBase::initialize( const Sequence
< Any
>& aArguments
)
115 throw ( Exception
, RuntimeException
, std::exception
)
119 if ( !m_bInitialized
)
121 for ( sal_Int32 n
= 0; n
< aArguments
.getLength(); n
++ )
123 PropertyValue aPropValue
;
124 if ( aArguments
[n
] >>= aPropValue
)
126 if ( aPropValue
.Name
== UIELEMENT_PROPNAME_CONFIGSOURCE
)
127 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGSOURCE
, aPropValue
.Value
);
128 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_FRAME
)
129 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_FRAME
, aPropValue
.Value
);
130 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_PERSISTENT
)
131 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_PERSISTENT
, aPropValue
.Value
);
132 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_RESOURCEURL
)
133 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_RESOURCEURL
, aPropValue
.Value
);
134 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_TYPE
)
135 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_TYPE
, aPropValue
.Value
);
136 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_CONFIGLISTENER
)
137 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGLISTENER
, aPropValue
.Value
);
138 else if ( aPropValue
.Name
== UIELEMENT_PROPNAME_NOCLOSE
)
139 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_NOCLOSE
, aPropValue
.Value
);
143 m_bInitialized
= true;
148 void SAL_CALL
UIConfigElementWrapperBase::update() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
150 // can be implemented by derived class
153 void SAL_CALL
UIConfigElementWrapperBase::elementInserted( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
155 // can be implemented by derived class
158 void SAL_CALL
UIConfigElementWrapperBase::elementRemoved( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
160 // can be implemented by derived class
163 void SAL_CALL
UIConfigElementWrapperBase::elementReplaced( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
165 // can be implemented by derived class
168 // XPropertySet helper
169 sal_Bool SAL_CALL
UIConfigElementWrapperBase::convertFastPropertyValue( Any
& aConvertedValue
,
172 const Any
& aValue
) throw( com::sun::star::lang::IllegalArgumentException
)
174 // Initialize state with sal_False !!!
175 // (Handle can be invalid)
176 bool bReturn
= false;
180 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
181 bReturn
= PropHelper::willPropertyBeChanged(
182 com::sun::star::uno::makeAny(m_bConfigListener
),
188 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
189 bReturn
= PropHelper::willPropertyBeChanged(
190 com::sun::star::uno::makeAny(m_xConfigSource
),
196 case UIELEMENT_PROPHANDLE_FRAME
:
198 Reference
< XFrame
> xFrame( m_xWeakFrame
);
199 bReturn
= PropHelper::willPropertyBeChanged(
200 com::sun::star::uno::makeAny(xFrame
),
207 case UIELEMENT_PROPHANDLE_PERSISTENT
:
208 bReturn
= PropHelper::willPropertyBeChanged(
209 com::sun::star::uno::makeAny(m_bPersistent
),
215 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
216 bReturn
= PropHelper::willPropertyBeChanged(
217 com::sun::star::uno::makeAny(m_aResourceURL
),
223 case UIELEMENT_PROPHANDLE_TYPE
:
224 bReturn
= PropHelper::willPropertyBeChanged(
225 com::sun::star::uno::makeAny(m_nType
),
231 case UIELEMENT_PROPHANDLE_XMENUBAR
:
232 bReturn
= PropHelper::willPropertyBeChanged(
233 com::sun::star::uno::makeAny(m_xMenuBar
),
239 case UIELEMENT_PROPHANDLE_NOCLOSE
:
240 bReturn
= PropHelper::willPropertyBeChanged(
241 com::sun::star::uno::makeAny(m_bNoClose
),
248 // Return state of operation.
252 void SAL_CALL
UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
,
253 const com::sun::star::uno::Any
& aValue
) throw( com::sun::star::uno::Exception
, std::exception
)
257 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
259 bool bBool( m_bConfigListener
);
261 if ( m_bConfigListener
!= bBool
)
263 if ( m_bConfigListening
)
265 if ( m_xConfigSource
.is() && !bBool
)
269 Reference
< XUIConfiguration
> xUIConfig( m_xConfigSource
, UNO_QUERY
);
270 if ( xUIConfig
.is() )
272 xUIConfig
->removeConfigurationListener( Reference
< XUIConfigurationListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
));
273 m_bConfigListening
= false;
276 catch ( const Exception
& )
283 if ( m_xConfigSource
.is() && bBool
)
287 Reference
< XUIConfiguration
> xUIConfig( m_xConfigSource
, UNO_QUERY
);
288 if ( xUIConfig
.is() )
290 xUIConfig
->addConfigurationListener( Reference
< XUIConfigurationListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
));
291 m_bConfigListening
= true;
294 catch ( const Exception
& )
300 m_bConfigListener
= bBool
;
304 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
305 aValue
>>= m_xConfigSource
;
307 case UIELEMENT_PROPHANDLE_FRAME
:
309 Reference
< XFrame
> xFrame
;
312 m_xWeakFrame
= xFrame
;
315 case UIELEMENT_PROPHANDLE_PERSISTENT
:
317 bool bBool( m_bPersistent
);
319 m_bPersistent
= bBool
;
322 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
323 aValue
>>= m_aResourceURL
;
325 case UIELEMENT_PROPHANDLE_TYPE
:
328 case UIELEMENT_PROPHANDLE_XMENUBAR
:
329 aValue
>>= m_xMenuBar
;
331 case UIELEMENT_PROPHANDLE_NOCLOSE
:
333 bool bBool( m_bNoClose
);
341 void SAL_CALL
UIConfigElementWrapperBase::getFastPropertyValue( com::sun::star::uno::Any
& aValue
,
342 sal_Int32 nHandle
) const
346 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
347 aValue
<<= m_bConfigListener
;
349 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
350 aValue
<<= m_xConfigSource
;
352 case UIELEMENT_PROPHANDLE_FRAME
:
354 Reference
< XFrame
> xFrame( m_xWeakFrame
);
358 case UIELEMENT_PROPHANDLE_PERSISTENT
:
359 aValue
<<= m_bPersistent
;
361 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
362 aValue
<<= m_aResourceURL
;
364 case UIELEMENT_PROPHANDLE_TYPE
:
367 case UIELEMENT_PROPHANDLE_XMENUBAR
:
368 aValue
<<= m_xMenuBar
;
370 case UIELEMENT_PROPHANDLE_NOCLOSE
:
371 aValue
<<= m_bNoClose
;
376 ::cppu::IPropertyArrayHelper
& SAL_CALL
UIConfigElementWrapperBase::getInfoHelper()
378 // Optimize this method !
379 // We initialize a static variable only one time. And we don't must use a mutex at every call!
380 // For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
381 static ::cppu::OPropertyArrayHelper
* pInfoHelper
= NULL
;
383 if( pInfoHelper
== NULL
)
385 // Ready for multithreading
386 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() );
388 // Control this pointer again, another instance can be faster then these!
389 if( pInfoHelper
== NULL
)
391 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
392 // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
393 // "sal_True" say: Table is sorted by name.
394 static ::cppu::OPropertyArrayHelper
aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True
);
395 pInfoHelper
= &aInfoHelper
;
399 return(*pInfoHelper
);
402 com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
> SAL_CALL
UIConfigElementWrapperBase::getPropertySetInfo() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
404 // Optimize this method !
405 // We initialize a static variable only one time. And we don't must use a mutex at every call!
406 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
407 static com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
>* pInfo
= NULL
;
411 // Ready for multithreading
412 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() );
413 // Control this pointer again, another instance can be faster then these!
416 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
417 // (Use method "getInfoHelper()".)
418 static com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
426 const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> UIConfigElementWrapperBase::impl_getStaticPropertyDescriptor()
428 // Create property array to initialize sequence!
429 // Table of all predefined properties of this class. Its used from OPropertySetHelper-class!
430 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
431 // It's necessary for methods of OPropertySetHelper.
433 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
435 const com::sun::star::beans::Property pProperties
[] =
437 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_CONFIGLISTENER
), UIELEMENT_PROPHANDLE_CONFIGLISTENER
, cppu::UnoType
<sal_Bool
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
438 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_CONFIGSOURCE
), UIELEMENT_PROPHANDLE_CONFIGSOURCE
, cppu::UnoType
<com::sun::star::ui::XUIConfigurationManager
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
439 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_FRAME
), UIELEMENT_PROPHANDLE_FRAME
, cppu::UnoType
<com::sun::star::frame::XFrame
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
440 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_NOCLOSE
), UIELEMENT_PROPHANDLE_NOCLOSE
, cppu::UnoType
<sal_Bool
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
441 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_PERSISTENT
), UIELEMENT_PROPHANDLE_PERSISTENT
, cppu::UnoType
<sal_Bool
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
442 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_RESOURCEURL
), UIELEMENT_PROPHANDLE_RESOURCEURL
, cppu::UnoType
<OUString
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
443 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_TYPE
), UIELEMENT_PROPHANDLE_TYPE
, cppu::UnoType
<OUString
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
444 com::sun::star::beans::Property( OUString(UIELEMENT_PROPNAME_XMENUBAR
), UIELEMENT_PROPHANDLE_XMENUBAR
, cppu::UnoType
<com::sun::star::awt::XMenuBar
>::get(), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
)
446 // Use it to initialize sequence!
447 const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> lPropertyDescriptor( pProperties
, UIELEMENT_PROPCOUNT
);
448 // Return "PropertyDescriptor"
449 return lPropertyDescriptor
;
451 void SAL_CALL
UIConfigElementWrapperBase::setSettings( const Reference
< XIndexAccess
>& xSettings
) throw ( RuntimeException
, std::exception
)
453 SolarMutexClearableGuard aLock
;
455 if ( xSettings
.is() )
457 // Create a copy of the data if the container is not const
458 Reference
< XIndexReplace
> xReplace( xSettings
, UNO_QUERY
);
460 m_xConfigData
= Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( new ConstItemContainer( xSettings
) ), UNO_QUERY
);
462 m_xConfigData
= xSettings
;
464 if ( m_xConfigSource
.is() && m_bPersistent
)
466 OUString
aResourceURL( m_aResourceURL
);
467 Reference
< XUIConfigurationManager
> xUICfgMgr( m_xConfigSource
);
473 xUICfgMgr
->replaceSettings( aResourceURL
, m_xConfigData
);
475 catch( const NoSuchElementException
& )
479 else if ( !m_bPersistent
)
481 // Transient menubar => Fill menubar with new data
486 void UIConfigElementWrapperBase::impl_fillNewData()
489 Reference
< XIndexAccess
> SAL_CALL
UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable
) throw ( RuntimeException
, std::exception
)
494 return Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( new RootItemContainer( m_xConfigData
) ), UNO_QUERY
);
496 return m_xConfigData
;
499 Reference
< XFrame
> SAL_CALL
UIConfigElementWrapperBase::getFrame() throw (RuntimeException
, std::exception
)
502 Reference
< XFrame
> xFrame( m_xWeakFrame
);
506 OUString SAL_CALL
UIConfigElementWrapperBase::getResourceURL() throw (RuntimeException
, std::exception
)
509 return m_aResourceURL
;
512 ::sal_Int16 SAL_CALL
UIConfigElementWrapperBase::getType() throw (RuntimeException
, std::exception
)
520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */