Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uiconfiguration / globalsettings.cxx
blob0883cc8af197fdf89f59bc1d6547a9328dbd53f8
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/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/lang/XEventListener.hpp>
28 #include <rtl/ref.hxx>
29 #include <comphelper/propertysequence.hxx>
30 #include <cppuhelper/implbase.hxx>
31 #include <mutex>
32 #include <utility>
34 // Defines
36 using namespace ::com::sun::star;
38 // Namespace
40 namespace framework
43 // Configuration access class for WindowState supplier implementation
45 namespace {
47 class GlobalSettings_Access : public ::cppu::WeakImplHelper<
48 css::lang::XComponent,
49 css::lang::XEventListener>
51 public:
52 explicit GlobalSettings_Access( css::uno::Reference< css::uno::XComponentContext > xContext );
54 // XComponent
55 virtual void SAL_CALL dispose() override;
56 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override;
57 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
59 // XEventListener
60 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
62 // settings access
63 bool HasToolbarStatesInfo();
64 bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue );
66 private:
67 void impl_initConfigAccess();
69 std::mutex m_mutex;
70 bool m_bDisposed : 1,
71 m_bConfigRead : 1;
72 OUString m_aNodeRefStates;
73 OUString m_aPropStatesEnabled;
74 OUString m_aPropLocked;
75 OUString m_aPropDocked;
76 css::uno::Reference< css::container::XNameAccess > m_xConfigAccess;
77 css::uno::Reference< css::uno::XComponentContext> m_xContext;
82 GlobalSettings_Access::GlobalSettings_Access( css::uno::Reference< css::uno::XComponentContext > xContext ) :
83 m_bDisposed( false ),
84 m_bConfigRead( false ),
85 m_aNodeRefStates( "States" ),
86 m_aPropStatesEnabled( "StatesEnabled" ),
87 m_aPropLocked( "Locked" ),
88 m_aPropDocked( "Docked" ),
89 m_xContext(std::move( xContext ))
93 // XComponent
94 void SAL_CALL GlobalSettings_Access::dispose()
96 std::unique_lock g(m_mutex);
97 m_xConfigAccess.clear();
98 m_bDisposed = true;
101 void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
105 void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
109 // XEventListener
110 void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
112 std::unique_lock g(m_mutex);
113 m_xConfigAccess.clear();
116 // settings access
117 bool GlobalSettings_Access::HasToolbarStatesInfo()
119 std::unique_lock g(m_mutex);
121 if ( m_bDisposed )
122 return false;
124 if ( !m_bConfigRead )
126 m_bConfigRead = true;
127 impl_initConfigAccess();
130 if ( m_xConfigAccess.is() )
134 css::uno::Any a;
135 bool bValue;
136 a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
137 if ( a >>= bValue )
138 return bValue;
140 catch ( const css::container::NoSuchElementException& )
143 catch ( const css::uno::Exception& )
148 return false;
151 bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue )
153 std::unique_lock g(m_mutex);
155 if ( m_bDisposed )
156 return false;
158 if ( !m_bConfigRead )
160 m_bConfigRead = true;
161 impl_initConfigAccess();
164 if ( !m_xConfigAccess.is() )
165 return false;
169 css::uno::Any a = m_xConfigAccess->getByName( m_aNodeRefStates );
170 css::uno::Reference< css::container::XNameAccess > xNameAccess;
171 if ( a >>= xNameAccess )
173 if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED )
174 a = xNameAccess->getByName( m_aPropLocked );
175 else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED )
176 a = xNameAccess->getByName( m_aPropDocked );
178 aValue = a;
179 return true;
182 catch ( const css::container::NoSuchElementException& )
185 catch ( const css::uno::Exception& )
189 return false;
192 void GlobalSettings_Access::impl_initConfigAccess()
196 if ( m_xContext.is() )
198 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
199 css::configuration::theDefaultProvider::get( m_xContext );
201 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
203 {"nodepath", uno::Any(OUString("/org.openoffice.Office.UI.GlobalSettings/Toolbars"))}
204 }));
205 m_xConfigAccess.set(xConfigProvider->createInstanceWithArguments(
206 SERVICENAME_CFGREADACCESS, aArgs ),
207 css::uno::UNO_QUERY );
209 css::uno::Reference< css::lang::XComponent >(
210 xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener(
211 css::uno::Reference< css::lang::XEventListener >(this));
214 catch ( const css::lang::WrappedTargetException& )
217 catch ( const css::uno::Exception& )
222 // global class
224 static GlobalSettings_Access* GetGlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
226 static rtl::Reference<GlobalSettings_Access> pStaticSettings = new GlobalSettings_Access( rxContext );
227 return pStaticSettings.get();
230 GlobalSettings::GlobalSettings( css::uno::Reference< css::uno::XComponentContext > xContext ) :
231 m_xContext(std::move( xContext ))
235 GlobalSettings::~GlobalSettings()
239 // settings access
240 bool GlobalSettings::HasToolbarStatesInfo() const
242 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
244 if ( pSettings )
245 return pSettings->HasToolbarStatesInfo();
246 else
247 return false;
250 bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo, css::uno::Any& aValue )
252 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
254 if ( pSettings )
255 return pSettings->GetToolbarStateInfo( eStateInfo, aValue );
256 else
257 return false;
260 } // namespace framework
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */