LibreOffice Module framework (master) 1
globalsettings.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <services.h>
22
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>
27
28#include <rtl/ref.hxx>
31#include <mutex>
32#include <utility>
33
34// Defines
35
36using namespace ::com::sun::star;
37
38// Namespace
39
40namespace framework
41{
42
43// Configuration access class for WindowState supplier implementation
44
45namespace {
46
47class GlobalSettings_Access : public ::cppu::WeakImplHelper<
48 css::lang::XComponent,
49 css::lang::XEventListener>
50{
51 public:
52 explicit GlobalSettings_Access( css::uno::Reference< css::uno::XComponentContext > xContext );
53
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;
58
59 // XEventListener
60 virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
61
62 // settings access
63 bool HasToolbarStatesInfo();
64 bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue );
65
66 private:
67 void impl_initConfigAccess();
68
69 std::mutex m_mutex;
70 bool m_bDisposed : 1,
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;
78};
79
80}
81
82GlobalSettings_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 ))
90{
91}
92
93// XComponent
94void SAL_CALL GlobalSettings_Access::dispose()
95{
96 std::unique_lock g(m_mutex);
97 m_xConfigAccess.clear();
98 m_bDisposed = true;
99}
100
101void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
102{
103}
104
105void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
106{
107}
108
109// XEventListener
110void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
111{
112 std::unique_lock g(m_mutex);
113 m_xConfigAccess.clear();
114}
115
116// settings access
117bool GlobalSettings_Access::HasToolbarStatesInfo()
118{
119 std::unique_lock g(m_mutex);
120
121 if ( m_bDisposed )
122 return false;
123
124 if ( !m_bConfigRead )
125 {
126 m_bConfigRead = true;
127 impl_initConfigAccess();
128 }
129
130 if ( m_xConfigAccess.is() )
131 {
132 try
133 {
134 css::uno::Any a;
135 bool bValue;
136 a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
137 if ( a >>= bValue )
138 return bValue;
139 }
140 catch ( const css::container::NoSuchElementException& )
141 {
142 }
143 catch ( const css::uno::Exception& )
144 {
145 }
146 }
147
148 return false;
149}
150
151bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue )
152{
153 std::unique_lock g(m_mutex);
154
155 if ( m_bDisposed )
156 return false;
157
158 if ( !m_bConfigRead )
159 {
160 m_bConfigRead = true;
161 impl_initConfigAccess();
162 }
163
164 if ( !m_xConfigAccess.is() )
165 return false;
166
167 try
168 {
169 css::uno::Any a = m_xConfigAccess->getByName( m_aNodeRefStates );
170 css::uno::Reference< css::container::XNameAccess > xNameAccess;
171 if ( a >>= xNameAccess )
172 {
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 );
177
178 aValue = a;
179 return true;
180 }
181 }
182 catch ( const css::container::NoSuchElementException& )
183 {
184 }
185 catch ( const css::uno::Exception& )
186 {
187 }
188
189 return false;
190}
191
192void GlobalSettings_Access::impl_initConfigAccess()
193{
194 try
195 {
196 if ( m_xContext.is() )
197 {
198 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
199 css::configuration::theDefaultProvider::get( m_xContext );
200
201 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
202 {
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 );
208
209 css::uno::Reference< css::lang::XComponent >(
210 xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener(
211 css::uno::Reference< css::lang::XEventListener >(this));
212 }
213 }
214 catch ( const css::lang::WrappedTargetException& )
215 {
216 }
217 catch ( const css::uno::Exception& )
218 {
219 }
220}
221
222// global class
223
224static GlobalSettings_Access* GetGlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
225{
226 static rtl::Reference<GlobalSettings_Access> pStaticSettings = new GlobalSettings_Access( rxContext );
227 return pStaticSettings.get();
228}
229
230GlobalSettings::GlobalSettings( css::uno::Reference< css::uno::XComponentContext > xContext ) :
231 m_xContext(std::move( xContext ))
232{
233}
234
236{
237}
238
239// settings access
241{
242 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
243
244 if ( pSettings )
245 return pSettings->HasToolbarStatesInfo();
246 else
247 return false;
248}
249
250bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo, css::uno::Any& aValue )
251{
252 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
253
254 if ( pSettings )
255 return pSettings->GetToolbarStateInfo( eStateInfo, aValue );
256 else
257 return false;
258}
259
260} // namespace framework
261
262/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool GetToolbarStateInfo(StateInfo eStateInfo, css::uno::Any &aValue)
css::uno::Reference< css::uno::XComponentContext > m_xContext
OUString m_aPropLocked
css::uno::Reference< css::container::XNameAccess > m_xConfigAccess
bool m_bConfigRead
OUString m_aNodeRefStates
std::mutex m_mutex
OUString m_aPropDocked
css::uno::Reference< css::uno::XComponentContext > m_xContext
OUString m_aPropStatesEnabled
bool m_bDisposed
uno_Any a
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
static GlobalSettings_Access * GetGlobalSettings(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
void dispose()