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 "uiconfiguration/globalsettings.hxx"
21 #include <threadhelp/resetableguard.hxx>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/configuration/theDefaultProvider.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <com/sun/star/container/XContainer.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XEventListener.hpp>
33 #include <rtl/ustrbuf.hxx>
34 #include <rtl/instance.hxx>
35 #include <cppuhelper/weak.hxx>
37 //_________________________________________________________________________________________________________________
39 //_________________________________________________________________________________________________________________
41 using namespace ::com::sun::star
;
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
48 static const char GLOBALSETTINGS_ROOT_ACCESS
[] = "/org.openoffice.Office.UI.GlobalSettings/Toolbars";
50 static const char GLOBALSETTINGS_NODEREF_STATES
[] = "States";
51 static const char GLOBALSETTINGS_PROPERTY_LOCKED
[] = "Locked";
52 static const char GLOBALSETTINGS_PROPERTY_DOCKED
[] = "Docked";
53 static const char GLOBALSETTINGS_PROPERTY_STATESENABLED
[] = "StatesEnabled";
58 //*****************************************************************************************************************
59 // Configuration access class for WindowState supplier implementation
60 //*****************************************************************************************************************
62 class GlobalSettings_Access
: public ::com::sun::star::lang::XComponent
,
63 public ::com::sun::star::lang::XEventListener
,
64 private ThreadHelpBase
, // Struct for right initalization of mutex member! Must be first of baseclasses.
65 public ::cppu::OWeakObject
68 GlobalSettings_Access( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
);
69 virtual ~GlobalSettings_Access();
71 // XInterface, XTypeProvider, XServiceInfo
72 FWK_DECLARE_XINTERFACE
75 virtual void SAL_CALL
dispose() throw (::com::sun::star::uno::RuntimeException
);
76 virtual void SAL_CALL
addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
);
77 virtual void SAL_CALL
removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& aListener
) throw (::com::sun::star::uno::RuntimeException
);
80 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
);
83 sal_Bool
HasStatesInfo( GlobalSettings::UIElementType eElementType
);
84 sal_Bool
GetStateInfo( GlobalSettings::UIElementType eElementType
, GlobalSettings::StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
);
87 sal_Bool
impl_initConfigAccess();
89 sal_Bool m_bDisposed
: 1,
91 OUString m_aConfigSettingsAccess
;
92 OUString m_aNodeRefStates
;
93 OUString m_aPropStatesEnabled
;
94 OUString m_aPropLocked
;
95 OUString m_aPropDocked
;
96 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xConfigAccess
;
97 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
101 //*****************************************************************************************************************
103 //*****************************************************************************************************************
104 DEFINE_XINTERFACE_2 ( GlobalSettings_Access
,
106 DIRECT_INTERFACE ( css::lang::XComponent
),
107 DIRECT_INTERFACE ( css::lang::XEventListener
)
110 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
) :
112 m_bDisposed( sal_False
),
113 m_bConfigRead( sal_False
),
114 m_aConfigSettingsAccess( GLOBALSETTINGS_ROOT_ACCESS
),
115 m_aNodeRefStates( GLOBALSETTINGS_NODEREF_STATES
),
116 m_aPropStatesEnabled( GLOBALSETTINGS_PROPERTY_STATESENABLED
),
117 m_aPropLocked( GLOBALSETTINGS_PROPERTY_LOCKED
),
118 m_aPropDocked( GLOBALSETTINGS_PROPERTY_DOCKED
),
119 m_xContext( rxContext
)
123 GlobalSettings_Access::~GlobalSettings_Access()
128 void SAL_CALL
GlobalSettings_Access::dispose()
129 throw ( css::uno::RuntimeException
)
132 ResetableGuard
aLock( m_aLock
);
134 m_xConfigAccess
.clear();
135 m_bDisposed
= sal_True
;
138 void SAL_CALL
GlobalSettings_Access::addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
139 throw (css::uno::RuntimeException
)
143 void SAL_CALL
GlobalSettings_Access::removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
144 throw (css::uno::RuntimeException
)
149 void SAL_CALL
GlobalSettings_Access::disposing( const css::lang::EventObject
& )
150 throw (css::uno::RuntimeException
)
153 ResetableGuard
aLock( m_aLock
);
154 m_xConfigAccess
.clear();
158 sal_Bool
GlobalSettings_Access::HasStatesInfo( GlobalSettings::UIElementType eElementType
)
160 ResetableGuard
aLock( m_aLock
);
161 if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW
)
163 else if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_STATUSBAR
)
169 if ( !m_bConfigRead
)
171 m_bConfigRead
= sal_True
;
172 impl_initConfigAccess();
175 if ( m_xConfigAccess
.is() )
180 sal_Bool bValue
= sal_Bool();
181 a
= m_xConfigAccess
->getByName( m_aPropStatesEnabled
);
185 catch ( const css::container::NoSuchElementException
& )
188 catch ( const css::uno::Exception
& )
196 sal_Bool
GlobalSettings_Access::GetStateInfo( GlobalSettings::UIElementType eElementType
, GlobalSettings::StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
)
198 ResetableGuard
aLock( m_aLock
);
199 if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW
)
201 else if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_STATUSBAR
)
207 if ( !m_bConfigRead
)
209 m_bConfigRead
= sal_True
;
210 impl_initConfigAccess();
213 if ( m_xConfigAccess
.is() )
218 a
= m_xConfigAccess
->getByName( m_aNodeRefStates
);
219 css::uno::Reference
< css::container::XNameAccess
> xNameAccess
;
220 if ( a
>>= xNameAccess
)
222 if ( eStateInfo
== GlobalSettings::STATEINFO_LOCKED
)
223 a
= xNameAccess
->getByName( m_aPropLocked
);
224 else if ( eStateInfo
== GlobalSettings::STATEINFO_DOCKED
)
225 a
= xNameAccess
->getByName( m_aPropDocked
);
231 catch ( const css::container::NoSuchElementException
& )
234 catch ( const css::uno::Exception
& )
242 sal_Bool
GlobalSettings_Access::impl_initConfigAccess()
244 css::uno::Sequence
< css::uno::Any
> aArgs( 2 );
245 css::beans::PropertyValue aPropValue
;
249 if ( m_xContext
.is() )
251 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider
=
252 css::configuration::theDefaultProvider::get( m_xContext
);
254 aPropValue
.Name
= OUString( "nodepath" );
255 aPropValue
.Value
= css::uno::makeAny( OUString( GLOBALSETTINGS_ROOT_ACCESS
));
256 aArgs
[0] = css::uno::makeAny( aPropValue
);
257 aPropValue
.Name
= OUString( "lazywrite" );
258 aPropValue
.Value
= css::uno::makeAny( sal_True
);
259 aArgs
[1] = css::uno::makeAny( aPropValue
);
261 m_xConfigAccess
= css::uno::Reference
< css::container::XNameAccess
>(
262 xConfigProvider
->createInstanceWithArguments(
263 SERVICENAME_CFGREADACCESS
, aArgs
),
264 css::uno::UNO_QUERY
);
266 css::uno::Reference
< css::lang::XComponent
>(
267 xConfigProvider
, css::uno::UNO_QUERY_THROW
)->addEventListener(
268 css::uno::Reference
< css::lang::XEventListener
>(
269 static_cast< cppu::OWeakObject
* >( this ),
270 css::uno::UNO_QUERY
));
273 return m_xConfigAccess
.is();
275 catch ( const css::lang::WrappedTargetException
& )
278 catch ( const css::uno::Exception
& )
285 //*****************************************************************************************************************
287 //*****************************************************************************************************************
289 struct mutexGlobalSettings
: public rtl::Static
< osl::Mutex
, mutexGlobalSettings
> {};
290 static GlobalSettings_Access
* pStaticSettings
= 0;
292 static GlobalSettings_Access
* GetGlobalSettings( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
)
294 osl::MutexGuard
aGuard(mutexGlobalSettings::get());
295 if ( !pStaticSettings
)
296 pStaticSettings
= new GlobalSettings_Access( rxContext
);
297 return pStaticSettings
;
300 GlobalSettings::GlobalSettings( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& rxContext
) :
301 m_xContext( rxContext
)
305 GlobalSettings::~GlobalSettings()
310 sal_Bool
GlobalSettings::HasStatesInfo( UIElementType eElementType
)
312 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
315 return pSettings
->HasStatesInfo( eElementType
);
320 sal_Bool
GlobalSettings::GetStateInfo( UIElementType eElementType
, StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
)
322 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
325 return pSettings
->GetStateInfo( eElementType
, eStateInfo
, aValue
);
330 } // namespace framework
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */