update credits
[LibreOffice.git] / framework / source / uiconfiguration / globalsettings.cxx
blob2f0eb7175054c94269baed6df268a816fef97253
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 "uiconfiguration/globalsettings.hxx"
21 #include <threadhelp/resetableguard.hxx>
22 #include "services.h"
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 //_________________________________________________________________________________________________________________
38 // Defines
39 //_________________________________________________________________________________________________________________
41 using namespace ::com::sun::star;
44 //_________________________________________________________________________________________________________________
45 // Namespace
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";
55 namespace framework
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
67 public:
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
74 // XComponent
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);
79 // XEventListener
80 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException);
82 // settings access
83 sal_Bool HasStatesInfo( GlobalSettings::UIElementType eElementType );
84 sal_Bool GetStateInfo( GlobalSettings::UIElementType eElementType, GlobalSettings::StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue );
86 private:
87 sal_Bool impl_initConfigAccess();
89 sal_Bool m_bDisposed : 1,
90 m_bConfigRead : 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 //*****************************************************************************************************************
102 // XInterface
103 //*****************************************************************************************************************
104 DEFINE_XINTERFACE_2 ( GlobalSettings_Access ,
105 OWeakObject ,
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 ) :
111 ThreadHelpBase(),
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()
127 // XComponent
128 void SAL_CALL GlobalSettings_Access::dispose()
129 throw ( css::uno::RuntimeException )
131 // SAFE
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)
148 // XEventListener
149 void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
150 throw (css::uno::RuntimeException)
152 // SAFE
153 ResetableGuard aLock( m_aLock );
154 m_xConfigAccess.clear();
157 // settings access
158 sal_Bool GlobalSettings_Access::HasStatesInfo( GlobalSettings::UIElementType eElementType )
160 ResetableGuard aLock( m_aLock );
161 if ( eElementType == GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW )
162 return sal_False;
163 else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
164 return sal_False;
166 if ( m_bDisposed )
167 return sal_False;
169 if ( !m_bConfigRead )
171 m_bConfigRead = sal_True;
172 impl_initConfigAccess();
175 if ( m_xConfigAccess.is() )
179 css::uno::Any a;
180 sal_Bool bValue = sal_Bool();
181 a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
182 if ( a >>= bValue )
183 return bValue;
185 catch ( const css::container::NoSuchElementException& )
188 catch ( const css::uno::Exception& )
193 return sal_False;
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 )
200 return sal_False;
201 else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
202 return sal_False;
204 if ( m_bDisposed )
205 return sal_False;
207 if ( !m_bConfigRead )
209 m_bConfigRead = sal_True;
210 impl_initConfigAccess();
213 if ( m_xConfigAccess.is() )
217 css::uno::Any a;
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 );
227 aValue = a;
228 return sal_True;
231 catch ( const css::container::NoSuchElementException& )
234 catch ( const css::uno::Exception& )
239 return sal_False;
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& )
282 return sal_False;
285 //*****************************************************************************************************************
286 // global class
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()
309 // settings access
310 sal_Bool GlobalSettings::HasStatesInfo( UIElementType eElementType )
312 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
314 if ( pSettings )
315 return pSettings->HasStatesInfo( eElementType );
316 else
317 return sal_False;
320 sal_Bool GlobalSettings::GetStateInfo( UIElementType eElementType, StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue )
322 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
324 if ( pSettings )
325 return pSettings->GetStateInfo( eElementType, eStateInfo, aValue );
326 else
327 return sal_False;
330 } // namespace framework
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */