Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / helper / uiconfigelementwrapperbase.cxx
blobf68d4228f828e71198ee9097de705b507ad0c52c
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 <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 char UIELEMENT_PROPNAME_CONFIGLISTENER[] = "ConfigListener";
43 const char UIELEMENT_PROPNAME_CONFIGSOURCE[] = "ConfigurationSource";
44 const char UIELEMENT_PROPNAME_FRAME[] = "Frame";
45 const char UIELEMENT_PROPNAME_PERSISTENT[] = "Persistent";
46 const char UIELEMENT_PROPNAME_RESOURCEURL[] = "ResourceURL";
47 const char UIELEMENT_PROPNAME_TYPE[] = "Type";
48 const char UIELEMENT_PROPNAME_XMENUBAR[] = "XMenuBar";
49 const char UIELEMENT_PROPNAME_NOCLOSE[] = "NoClose";
50 using namespace com::sun::star::beans;
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::frame;
53 using namespace com::sun::star::lang;
54 using namespace com::sun::star::container;
55 using namespace ::com::sun::star::ui;
57 namespace framework
60 UIConfigElementWrapperBase::UIConfigElementWrapperBase( sal_Int16 nType )
61 : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
62 , ::cppu::OPropertySetHelper ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
63 , m_nType ( nType )
64 , m_bPersistent ( true )
65 , m_bInitialized ( false )
66 , m_bConfigListener ( false )
67 , m_bConfigListening ( false )
68 , m_bDisposed ( false )
69 , m_bNoClose ( false )
70 , m_aListenerContainer ( m_aMutex )
74 UIConfigElementWrapperBase::~UIConfigElementWrapperBase()
78 Any SAL_CALL UIConfigElementWrapperBase::queryInterface( const Type& _rType )
80 Any aRet = UIConfigElementWrapperBase_BASE::queryInterface( _rType );
81 if ( !aRet.hasValue() )
82 aRet = OPropertySetHelper::queryInterface( _rType );
83 return aRet;
86 Sequence< Type > SAL_CALL UIConfigElementWrapperBase::getTypes( )
88 return comphelper::concatSequences(
89 UIConfigElementWrapperBase_BASE::getTypes(),
90 ::cppu::OPropertySetHelper::getTypes()
94 // XComponent
95 void SAL_CALL UIConfigElementWrapperBase::addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener )
97 m_aListenerContainer.addInterface( cppu::UnoType<css::lang::XEventListener>::get(), xListener );
100 void SAL_CALL UIConfigElementWrapperBase::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
102 m_aListenerContainer.removeInterface( cppu::UnoType<css::lang::XEventListener>::get(), aListener );
105 // XEventListener
106 void SAL_CALL UIConfigElementWrapperBase::disposing( const EventObject& )
108 SolarMutexGuard g;
109 m_xConfigSource.clear();
112 void SAL_CALL UIConfigElementWrapperBase::initialize( const Sequence< Any >& aArguments )
114 SolarMutexGuard g;
116 if ( !m_bInitialized )
118 for ( sal_Int32 n = 0; n < aArguments.getLength(); n++ )
120 PropertyValue aPropValue;
121 if ( aArguments[n] >>= aPropValue )
123 if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGSOURCE )
124 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGSOURCE, aPropValue.Value );
125 else if ( aPropValue.Name == UIELEMENT_PROPNAME_FRAME )
126 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_FRAME, aPropValue.Value );
127 else if ( aPropValue.Name == UIELEMENT_PROPNAME_PERSISTENT )
128 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_PERSISTENT, aPropValue.Value );
129 else if ( aPropValue.Name == UIELEMENT_PROPNAME_RESOURCEURL )
130 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_RESOURCEURL, aPropValue.Value );
131 else if ( aPropValue.Name == UIELEMENT_PROPNAME_TYPE )
132 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_TYPE, aPropValue.Value );
133 else if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGLISTENER )
134 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGLISTENER, aPropValue.Value );
135 else if ( aPropValue.Name == UIELEMENT_PROPNAME_NOCLOSE )
136 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_NOCLOSE, aPropValue.Value );
140 m_bInitialized = true;
144 // XUpdatable
145 void SAL_CALL UIConfigElementWrapperBase::update()
147 // can be implemented by derived class
150 void SAL_CALL UIConfigElementWrapperBase::elementInserted( const css::ui::ConfigurationEvent& )
152 // can be implemented by derived class
155 void SAL_CALL UIConfigElementWrapperBase::elementRemoved( const css::ui::ConfigurationEvent& )
157 // can be implemented by derived class
160 void SAL_CALL UIConfigElementWrapperBase::elementReplaced( const css::ui::ConfigurationEvent& )
162 // can be implemented by derived class
165 // XPropertySet helper
166 sal_Bool SAL_CALL UIConfigElementWrapperBase::convertFastPropertyValue( Any& aConvertedValue ,
167 Any& aOldValue ,
168 sal_Int32 nHandle ,
169 const Any& aValue )
171 // Initialize state with sal_False !!!
172 // (Handle can be invalid)
173 bool bReturn = false;
175 switch( nHandle )
177 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
178 bReturn = PropHelper::willPropertyBeChanged(
179 css::uno::makeAny(m_bConfigListener),
180 aValue,
181 aOldValue,
182 aConvertedValue);
183 break;
185 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
186 bReturn = PropHelper::willPropertyBeChanged(
187 css::uno::makeAny(m_xConfigSource),
188 aValue,
189 aOldValue,
190 aConvertedValue);
191 break;
193 case UIELEMENT_PROPHANDLE_FRAME:
195 Reference< XFrame > xFrame( m_xWeakFrame );
196 bReturn = PropHelper::willPropertyBeChanged(
197 css::uno::makeAny(xFrame),
198 aValue,
199 aOldValue,
200 aConvertedValue);
202 break;
204 case UIELEMENT_PROPHANDLE_PERSISTENT:
205 bReturn = PropHelper::willPropertyBeChanged(
206 css::uno::makeAny(m_bPersistent),
207 aValue,
208 aOldValue,
209 aConvertedValue);
210 break;
212 case UIELEMENT_PROPHANDLE_RESOURCEURL:
213 bReturn = PropHelper::willPropertyBeChanged(
214 css::uno::makeAny(m_aResourceURL),
215 aValue,
216 aOldValue,
217 aConvertedValue);
218 break;
220 case UIELEMENT_PROPHANDLE_TYPE :
221 bReturn = PropHelper::willPropertyBeChanged(
222 css::uno::makeAny(m_nType),
223 aValue,
224 aOldValue,
225 aConvertedValue);
226 break;
228 case UIELEMENT_PROPHANDLE_XMENUBAR :
229 bReturn = PropHelper::willPropertyBeChanged(
230 css::uno::makeAny(m_xMenuBar),
231 aValue,
232 aOldValue,
233 aConvertedValue);
234 break;
236 case UIELEMENT_PROPHANDLE_NOCLOSE:
237 bReturn = PropHelper::willPropertyBeChanged(
238 css::uno::makeAny(m_bNoClose),
239 aValue,
240 aOldValue,
241 aConvertedValue);
242 break;
245 // Return state of operation.
246 return bReturn;
249 void SAL_CALL UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
250 const css::uno::Any& aValue )
252 switch( nHandle )
254 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
256 bool bBool( m_bConfigListener );
257 aValue >>= bBool;
258 if ( m_bConfigListener != bBool )
260 if ( m_bConfigListening )
262 if ( m_xConfigSource.is() && !bBool )
266 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
267 if ( xUIConfig.is() )
269 xUIConfig->removeConfigurationListener( Reference< XUIConfigurationListener >( static_cast< OWeakObject* >( this ), UNO_QUERY ));
270 m_bConfigListening = false;
273 catch ( const Exception& )
278 else
280 if ( m_xConfigSource.is() && bBool )
284 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
285 if ( xUIConfig.is() )
287 xUIConfig->addConfigurationListener( Reference< XUIConfigurationListener >( static_cast< OWeakObject* >( this ), UNO_QUERY ));
288 m_bConfigListening = true;
291 catch ( const Exception& )
297 m_bConfigListener = bBool;
300 break;
301 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
302 aValue >>= m_xConfigSource;
303 break;
304 case UIELEMENT_PROPHANDLE_FRAME:
306 Reference< XFrame > xFrame;
308 aValue >>= xFrame;
309 m_xWeakFrame = xFrame;
310 break;
312 case UIELEMENT_PROPHANDLE_PERSISTENT:
314 bool bBool( m_bPersistent );
315 aValue >>= bBool;
316 m_bPersistent = bBool;
317 break;
319 case UIELEMENT_PROPHANDLE_RESOURCEURL:
320 aValue >>= m_aResourceURL;
321 break;
322 case UIELEMENT_PROPHANDLE_TYPE:
323 aValue >>= m_nType;
324 break;
325 case UIELEMENT_PROPHANDLE_XMENUBAR:
326 aValue >>= m_xMenuBar;
327 break;
328 case UIELEMENT_PROPHANDLE_NOCLOSE:
330 bool bBool( m_bNoClose );
331 aValue >>= bBool;
332 m_bNoClose = bBool;
333 break;
338 void SAL_CALL UIConfigElementWrapperBase::getFastPropertyValue( css::uno::Any& aValue ,
339 sal_Int32 nHandle ) const
341 switch( nHandle )
343 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
344 aValue <<= m_bConfigListener;
345 break;
346 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
347 aValue <<= m_xConfigSource;
348 break;
349 case UIELEMENT_PROPHANDLE_FRAME:
351 Reference< XFrame > xFrame( m_xWeakFrame );
352 aValue <<= xFrame;
353 break;
355 case UIELEMENT_PROPHANDLE_PERSISTENT:
356 aValue <<= m_bPersistent;
357 break;
358 case UIELEMENT_PROPHANDLE_RESOURCEURL:
359 aValue <<= m_aResourceURL;
360 break;
361 case UIELEMENT_PROPHANDLE_TYPE:
362 aValue <<= m_nType;
363 break;
364 case UIELEMENT_PROPHANDLE_XMENUBAR:
365 aValue <<= m_xMenuBar;
366 break;
367 case UIELEMENT_PROPHANDLE_NOCLOSE:
368 aValue <<= m_bNoClose;
369 break;
373 ::cppu::IPropertyArrayHelper& SAL_CALL UIConfigElementWrapperBase::getInfoHelper()
375 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
376 // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
377 // "true" say: Table is sorted by name.
378 static ::cppu::OPropertyArrayHelper ourInfoHelper( impl_getStaticPropertyDescriptor(), true );
380 return ourInfoHelper;
383 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL UIConfigElementWrapperBase::getPropertySetInfo()
385 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
386 // (Use method "getInfoHelper()".)
387 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
389 return xInfo;
392 css::uno::Sequence< css::beans::Property > UIConfigElementWrapperBase::impl_getStaticPropertyDescriptor()
394 // Create property array to initialize sequence!
395 // Table of all predefined properties of this class. It's used from OPropertySetHelper-class!
396 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
397 // It's necessary for methods of OPropertySetHelper.
398 // ATTENTION:
399 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
401 return
403 css::beans::Property( UIELEMENT_PROPNAME_CONFIGLISTENER, UIELEMENT_PROPHANDLE_CONFIGLISTENER , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
404 css::beans::Property( UIELEMENT_PROPNAME_CONFIGSOURCE, UIELEMENT_PROPHANDLE_CONFIGSOURCE , cppu::UnoType<css::ui::XUIConfigurationManager>::get(), css::beans::PropertyAttribute::TRANSIENT ),
405 css::beans::Property( UIELEMENT_PROPNAME_FRAME, UIELEMENT_PROPHANDLE_FRAME , cppu::UnoType<css::frame::XFrame>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
406 css::beans::Property( UIELEMENT_PROPNAME_NOCLOSE, UIELEMENT_PROPHANDLE_NOCLOSE , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
407 css::beans::Property( UIELEMENT_PROPNAME_PERSISTENT, UIELEMENT_PROPHANDLE_PERSISTENT , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
408 css::beans::Property( UIELEMENT_PROPNAME_RESOURCEURL, UIELEMENT_PROPHANDLE_RESOURCEURL , cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
409 css::beans::Property( UIELEMENT_PROPNAME_TYPE, UIELEMENT_PROPHANDLE_TYPE , cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
410 css::beans::Property( UIELEMENT_PROPNAME_XMENUBAR, UIELEMENT_PROPHANDLE_XMENUBAR , cppu::UnoType<css::awt::XMenuBar>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY )
414 void SAL_CALL UIConfigElementWrapperBase::setSettings( const Reference< XIndexAccess >& xSettings )
416 SolarMutexClearableGuard aLock;
418 if ( xSettings.is() )
420 // Create a copy of the data if the container is not const
421 Reference< XIndexReplace > xReplace( xSettings, UNO_QUERY );
422 if ( xReplace.is() )
423 m_xConfigData.set( static_cast< OWeakObject * >( new ConstItemContainer( xSettings ) ), UNO_QUERY );
424 else
425 m_xConfigData = xSettings;
427 if ( m_xConfigSource.is() && m_bPersistent )
429 OUString aResourceURL( m_aResourceURL );
430 Reference< XUIConfigurationManager > xUICfgMgr( m_xConfigSource );
432 aLock.clear();
436 xUICfgMgr->replaceSettings( aResourceURL, m_xConfigData );
438 catch( const NoSuchElementException& )
442 else if ( !m_bPersistent )
444 // Transient menubar => Fill menubar with new data
445 impl_fillNewData();
449 void UIConfigElementWrapperBase::impl_fillNewData()
452 Reference< XIndexAccess > SAL_CALL UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable )
454 SolarMutexGuard g;
456 if ( bWriteable )
457 return Reference< XIndexAccess >( static_cast< OWeakObject * >( new RootItemContainer( m_xConfigData ) ), UNO_QUERY );
459 return m_xConfigData;
462 Reference< XFrame > SAL_CALL UIConfigElementWrapperBase::getFrame()
464 SolarMutexGuard g;
465 Reference< XFrame > xFrame( m_xWeakFrame );
466 return xFrame;
469 OUString SAL_CALL UIConfigElementWrapperBase::getResourceURL()
471 SolarMutexGuard g;
472 return m_aResourceURL;
475 ::sal_Int16 SAL_CALL UIConfigElementWrapperBase::getType()
477 SolarMutexGuard g;
478 return m_nType;
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */