Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / uiconfiguration / globalsettings.cxx
blob02d6032093f7c2f9d7ae4de8ffc3459b9bb4c223
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 <services.h>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/configuration/theDefaultProvider.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/container/XContainer.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/lang/XEventListener.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/instance.hxx>
34 #include <comphelper/propertysequence.hxx>
35 #include <cppuhelper/implbase.hxx>
37 // Defines
39 using namespace ::com::sun::star;
41 // Namespace
43 namespace framework
46 // Configuration access class for WindowState supplier implementation
48 class GlobalSettings_Access : public ::cppu::WeakImplHelper<
49 css::lang::XComponent,
50 css::lang::XEventListener>
52 public:
53 explicit GlobalSettings_Access( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
55 // XComponent
56 virtual void SAL_CALL dispose() override;
57 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override;
58 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
60 // XEventListener
61 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
63 // settings access
64 bool HasToolbarStatesInfo();
65 bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue );
67 private:
68 void impl_initConfigAccess();
70 osl::Mutex m_mutex;
71 bool m_bDisposed : 1,
72 m_bConfigRead : 1;
73 OUString m_aNodeRefStates;
74 OUString m_aPropStatesEnabled;
75 OUString m_aPropLocked;
76 OUString m_aPropDocked;
77 css::uno::Reference< css::container::XNameAccess > m_xConfigAccess;
78 css::uno::Reference< css::uno::XComponentContext> m_xContext;
81 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
82 m_bDisposed( false ),
83 m_bConfigRead( false ),
84 m_aNodeRefStates( "States" ),
85 m_aPropStatesEnabled( "StatesEnabled" ),
86 m_aPropLocked( "Locked" ),
87 m_aPropDocked( "Docked" ),
88 m_xContext( rxContext )
92 // XComponent
93 void SAL_CALL GlobalSettings_Access::dispose()
95 osl::MutexGuard g(m_mutex);
96 m_xConfigAccess.clear();
97 m_bDisposed = true;
100 void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
104 void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
108 // XEventListener
109 void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
111 osl::MutexGuard g(m_mutex);
112 m_xConfigAccess.clear();
115 // settings access
116 bool GlobalSettings_Access::HasToolbarStatesInfo()
118 osl::MutexGuard g(m_mutex);
120 if ( m_bDisposed )
121 return false;
123 if ( !m_bConfigRead )
125 m_bConfigRead = true;
126 impl_initConfigAccess();
129 if ( m_xConfigAccess.is() )
133 css::uno::Any a;
134 bool bValue;
135 a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
136 if ( a >>= bValue )
137 return bValue;
139 catch ( const css::container::NoSuchElementException& )
142 catch ( const css::uno::Exception& )
147 return false;
150 bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue )
152 osl::MutexGuard g(m_mutex);
154 if ( m_bDisposed )
155 return false;
157 if ( !m_bConfigRead )
159 m_bConfigRead = true;
160 impl_initConfigAccess();
163 if ( m_xConfigAccess.is() )
167 css::uno::Any a = m_xConfigAccess->getByName( m_aNodeRefStates );
168 css::uno::Reference< css::container::XNameAccess > xNameAccess;
169 if ( a >>= xNameAccess )
171 if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED )
172 a = xNameAccess->getByName( m_aPropLocked );
173 else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED )
174 a = xNameAccess->getByName( m_aPropDocked );
176 aValue = a;
177 return true;
180 catch ( const css::container::NoSuchElementException& )
183 catch ( const css::uno::Exception& )
188 return false;
191 void GlobalSettings_Access::impl_initConfigAccess()
195 if ( m_xContext.is() )
197 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
198 css::configuration::theDefaultProvider::get( m_xContext );
200 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
202 {"nodepath", uno::Any(OUString("/org.openoffice.Office.UI.GlobalSettings/Toolbars"))}
203 }));
204 m_xConfigAccess.set(xConfigProvider->createInstanceWithArguments(
205 SERVICENAME_CFGREADACCESS, aArgs ),
206 css::uno::UNO_QUERY );
208 css::uno::Reference< css::lang::XComponent >(
209 xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener(
210 css::uno::Reference< css::lang::XEventListener >(
211 static_cast< cppu::OWeakObject* >( this ),
212 css::uno::UNO_QUERY ));
215 catch ( const css::lang::WrappedTargetException& )
218 catch ( const css::uno::Exception& )
223 // global class
225 struct mutexGlobalSettings : public rtl::Static< osl::Mutex, mutexGlobalSettings > {};
226 static GlobalSettings_Access* pStaticSettings = nullptr;
228 static GlobalSettings_Access* GetGlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
230 osl::MutexGuard aGuard(mutexGlobalSettings::get());
231 if ( !pStaticSettings )
232 pStaticSettings = new GlobalSettings_Access( rxContext );
233 return pStaticSettings;
236 GlobalSettings::GlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
237 m_xContext( rxContext )
241 GlobalSettings::~GlobalSettings()
245 // settings access
246 bool GlobalSettings::HasToolbarStatesInfo() const
248 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
250 if ( pSettings )
251 return pSettings->HasToolbarStatesInfo();
252 else
253 return false;
256 bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo, css::uno::Any& aValue )
258 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
260 if ( pSettings )
261 return pSettings->GetToolbarStateInfo( eStateInfo, aValue );
262 else
263 return false;
266 } // namespace framework
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */