/src/libreoffice/framework/source/uiconfiguration/globalsettings.cxx
Line | Count | Source |
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 | | |
20 | | #include <uiconfiguration/globalsettings.hxx> |
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> |
29 | | #include <comphelper/propertysequence.hxx> |
30 | | #include <cppuhelper/implbase.hxx> |
31 | | #include <mutex> |
32 | | #include <utility> |
33 | | |
34 | | // Defines |
35 | | |
36 | | using namespace ::com::sun::star; |
37 | | |
38 | | // Namespace |
39 | | |
40 | | namespace framework |
41 | | { |
42 | | |
43 | | // Configuration access class for WindowState supplier implementation |
44 | | |
45 | | namespace { |
46 | | |
47 | | class 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, |
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; |
78 | | }; |
79 | | |
80 | | } |
81 | | |
82 | | GlobalSettings_Access::GlobalSettings_Access( css::uno::Reference< css::uno::XComponentContext > xContext ) : |
83 | 0 | m_bDisposed( false ), |
84 | 0 | m_bConfigRead( false ), |
85 | 0 | m_aNodeRefStates( u"States"_ustr ), |
86 | 0 | m_aPropStatesEnabled( u"StatesEnabled"_ustr ), |
87 | 0 | m_aPropLocked( u"Locked"_ustr ), |
88 | 0 | m_aPropDocked( u"Docked"_ustr ), |
89 | 0 | m_xContext(std::move( xContext )) |
90 | 0 | { |
91 | 0 | } |
92 | | |
93 | | // XComponent |
94 | | void SAL_CALL GlobalSettings_Access::dispose() |
95 | 0 | { |
96 | 0 | std::unique_lock g(m_mutex); |
97 | 0 | m_xConfigAccess.clear(); |
98 | 0 | m_bDisposed = true; |
99 | 0 | } |
100 | | |
101 | | void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& ) |
102 | 0 | { |
103 | 0 | } |
104 | | |
105 | | void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& ) |
106 | 0 | { |
107 | 0 | } |
108 | | |
109 | | // XEventListener |
110 | | void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& ) |
111 | 0 | { |
112 | 0 | std::unique_lock g(m_mutex); |
113 | 0 | m_xConfigAccess.clear(); |
114 | 0 | } |
115 | | |
116 | | // settings access |
117 | | bool GlobalSettings_Access::HasToolbarStatesInfo() |
118 | 0 | { |
119 | 0 | std::unique_lock g(m_mutex); |
120 | |
|
121 | 0 | if ( m_bDisposed ) |
122 | 0 | return false; |
123 | | |
124 | 0 | if ( !m_bConfigRead ) |
125 | 0 | { |
126 | 0 | m_bConfigRead = true; |
127 | 0 | impl_initConfigAccess(); |
128 | 0 | } |
129 | |
|
130 | 0 | if ( m_xConfigAccess.is() ) |
131 | 0 | { |
132 | 0 | try |
133 | 0 | { |
134 | 0 | css::uno::Any a; |
135 | 0 | bool bValue; |
136 | 0 | a = m_xConfigAccess->getByName( m_aPropStatesEnabled ); |
137 | 0 | if ( a >>= bValue ) |
138 | 0 | return bValue; |
139 | 0 | } |
140 | 0 | catch ( const css::container::NoSuchElementException& ) |
141 | 0 | { |
142 | 0 | } |
143 | 0 | catch ( const css::uno::Exception& ) |
144 | 0 | { |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | 0 | return false; |
149 | 0 | } |
150 | | |
151 | | bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue ) |
152 | 0 | { |
153 | 0 | std::unique_lock g(m_mutex); |
154 | |
|
155 | 0 | if ( m_bDisposed ) |
156 | 0 | return false; |
157 | | |
158 | 0 | if ( !m_bConfigRead ) |
159 | 0 | { |
160 | 0 | m_bConfigRead = true; |
161 | 0 | impl_initConfigAccess(); |
162 | 0 | } |
163 | |
|
164 | 0 | if ( !m_xConfigAccess.is() ) |
165 | 0 | return false; |
166 | | |
167 | 0 | try |
168 | 0 | { |
169 | 0 | css::uno::Any a = m_xConfigAccess->getByName( m_aNodeRefStates ); |
170 | 0 | css::uno::Reference< css::container::XNameAccess > xNameAccess; |
171 | 0 | if ( a >>= xNameAccess ) |
172 | 0 | { |
173 | 0 | if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED ) |
174 | 0 | a = xNameAccess->getByName( m_aPropLocked ); |
175 | 0 | else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED ) |
176 | 0 | a = xNameAccess->getByName( m_aPropDocked ); |
177 | |
|
178 | 0 | aValue = std::move(a); |
179 | 0 | return true; |
180 | 0 | } |
181 | 0 | } |
182 | 0 | catch ( const css::container::NoSuchElementException& ) |
183 | 0 | { |
184 | 0 | } |
185 | 0 | catch ( const css::uno::Exception& ) |
186 | 0 | { |
187 | 0 | } |
188 | | |
189 | 0 | return false; |
190 | 0 | } |
191 | | |
192 | | void GlobalSettings_Access::impl_initConfigAccess() |
193 | 0 | { |
194 | 0 | try |
195 | 0 | { |
196 | 0 | if ( m_xContext.is() ) |
197 | 0 | { |
198 | 0 | css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider = |
199 | 0 | css::configuration::theDefaultProvider::get( m_xContext ); |
200 | |
|
201 | 0 | uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence( |
202 | 0 | { |
203 | 0 | {"nodepath", uno::Any(u"/org.openoffice.Office.UI.GlobalSettings/Toolbars"_ustr)} |
204 | 0 | })); |
205 | 0 | m_xConfigAccess.set(xConfigProvider->createInstanceWithArguments( |
206 | 0 | SERVICENAME_CFGREADACCESS, aArgs ), |
207 | 0 | css::uno::UNO_QUERY ); |
208 | |
|
209 | 0 | css::uno::Reference< css::lang::XComponent >( |
210 | 0 | xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener( |
211 | 0 | css::uno::Reference< css::lang::XEventListener >(this)); |
212 | 0 | } |
213 | 0 | } |
214 | 0 | catch ( const css::lang::WrappedTargetException& ) |
215 | 0 | { |
216 | 0 | } |
217 | 0 | catch ( const css::uno::Exception& ) |
218 | 0 | { |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | // global class |
223 | | |
224 | | static GlobalSettings_Access* GetGlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) |
225 | 0 | { |
226 | 0 | static rtl::Reference<GlobalSettings_Access> pStaticSettings = new GlobalSettings_Access( rxContext ); |
227 | 0 | return pStaticSettings.get(); |
228 | 0 | } |
229 | | |
230 | | GlobalSettings::GlobalSettings( css::uno::Reference< css::uno::XComponentContext > xContext ) : |
231 | 0 | m_xContext(std::move( xContext )) |
232 | 0 | { |
233 | 0 | } |
234 | | |
235 | | GlobalSettings::~GlobalSettings() |
236 | 0 | { |
237 | 0 | } |
238 | | |
239 | | // settings access |
240 | | bool GlobalSettings::HasToolbarStatesInfo() const |
241 | 0 | { |
242 | 0 | GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext )); |
243 | |
|
244 | 0 | if ( pSettings ) |
245 | 0 | return pSettings->HasToolbarStatesInfo(); |
246 | 0 | else |
247 | 0 | return false; |
248 | 0 | } |
249 | | |
250 | | bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo, css::uno::Any& aValue ) |
251 | 0 | { |
252 | 0 | GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext )); |
253 | |
|
254 | 0 | if ( pSettings ) |
255 | 0 | return pSettings->GetToolbarStateInfo( eStateInfo, aValue ); |
256 | 0 | else |
257 | 0 | return false; |
258 | 0 | } |
259 | | |
260 | | } // namespace framework |
261 | | |
262 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |