Line data Source code
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/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>
31 :
32 : #include <rtl/ustrbuf.hxx>
33 : #include <rtl/instance.hxx>
34 : #include <cppuhelper/implbase2.hxx>
35 :
36 : // Defines
37 :
38 : using namespace ::com::sun::star;
39 :
40 : // Namespace
41 :
42 : static const char GLOBALSETTINGS_ROOT_ACCESS[] = "/org.openoffice.Office.UI.GlobalSettings/Toolbars";
43 :
44 : static const char GLOBALSETTINGS_NODEREF_STATES[] = "States";
45 : static const char GLOBALSETTINGS_PROPERTY_LOCKED[] = "Locked";
46 : static const char GLOBALSETTINGS_PROPERTY_DOCKED[] = "Docked";
47 : static const char GLOBALSETTINGS_PROPERTY_STATESENABLED[] = "StatesEnabled";
48 :
49 : namespace framework
50 : {
51 :
52 : // Configuration access class for WindowState supplier implementation
53 :
54 : class GlobalSettings_Access : public ::cppu::WeakImplHelper2<
55 : ::com::sun::star::lang::XComponent,
56 : ::com::sun::star::lang::XEventListener>
57 : {
58 : public:
59 : GlobalSettings_Access( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext );
60 : virtual ~GlobalSettings_Access();
61 :
62 : // XComponent
63 : virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
64 : virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 :
67 : // XEventListener
68 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
69 :
70 : // settings access
71 : bool HasStatesInfo( GlobalSettings::UIElementType eElementType );
72 : bool GetStateInfo( GlobalSettings::UIElementType eElementType, GlobalSettings::StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue );
73 :
74 : private:
75 : bool impl_initConfigAccess();
76 :
77 : osl::Mutex m_mutex;
78 : bool m_bDisposed : 1,
79 : m_bConfigRead : 1;
80 : OUString m_aNodeRefStates;
81 : OUString m_aPropStatesEnabled;
82 : OUString m_aPropLocked;
83 : OUString m_aPropDocked;
84 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > m_xConfigAccess;
85 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> m_xContext;
86 : };
87 :
88 162 : GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
89 : m_bDisposed( false ),
90 : m_bConfigRead( false ),
91 : m_aNodeRefStates( GLOBALSETTINGS_NODEREF_STATES ),
92 : m_aPropStatesEnabled( GLOBALSETTINGS_PROPERTY_STATESENABLED ),
93 : m_aPropLocked( GLOBALSETTINGS_PROPERTY_LOCKED ),
94 : m_aPropDocked( GLOBALSETTINGS_PROPERTY_DOCKED ),
95 162 : m_xContext( rxContext )
96 : {
97 162 : }
98 :
99 316 : GlobalSettings_Access::~GlobalSettings_Access()
100 : {
101 316 : }
102 :
103 : // XComponent
104 0 : void SAL_CALL GlobalSettings_Access::dispose()
105 : throw ( css::uno::RuntimeException, std::exception )
106 : {
107 0 : osl::MutexGuard g(m_mutex);
108 0 : m_xConfigAccess.clear();
109 0 : m_bDisposed = true;
110 0 : }
111 :
112 0 : void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
113 : throw (css::uno::RuntimeException, std::exception)
114 : {
115 0 : }
116 :
117 0 : void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
118 : throw (css::uno::RuntimeException, std::exception)
119 : {
120 0 : }
121 :
122 : // XEventListener
123 158 : void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
124 : throw (css::uno::RuntimeException, std::exception)
125 : {
126 158 : osl::MutexGuard g(m_mutex);
127 158 : m_xConfigAccess.clear();
128 158 : }
129 :
130 : // settings access
131 11050 : bool GlobalSettings_Access::HasStatesInfo( GlobalSettings::UIElementType eElementType )
132 : {
133 11050 : osl::MutexGuard g(m_mutex);
134 11050 : if ( eElementType == GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW )
135 0 : return false;
136 11050 : else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
137 0 : return false;
138 :
139 11050 : if ( m_bDisposed )
140 0 : return false;
141 :
142 11050 : if ( !m_bConfigRead )
143 : {
144 162 : m_bConfigRead = true;
145 162 : impl_initConfigAccess();
146 : }
147 :
148 11050 : if ( m_xConfigAccess.is() )
149 : {
150 : try
151 : {
152 11050 : css::uno::Any a;
153 : bool bValue;
154 11050 : a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
155 11050 : if ( a >>= bValue )
156 0 : return bValue;
157 : }
158 0 : catch ( const css::container::NoSuchElementException& )
159 : {
160 : }
161 0 : catch ( const css::uno::Exception& )
162 : {
163 : }
164 : }
165 :
166 11050 : return false;
167 : }
168 :
169 0 : bool GlobalSettings_Access::GetStateInfo( GlobalSettings::UIElementType eElementType, GlobalSettings::StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue )
170 : {
171 0 : osl::MutexGuard g(m_mutex);
172 0 : if ( eElementType == GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW )
173 0 : return false;
174 0 : else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
175 0 : return false;
176 :
177 0 : if ( m_bDisposed )
178 0 : return false;
179 :
180 0 : if ( !m_bConfigRead )
181 : {
182 0 : m_bConfigRead = true;
183 0 : impl_initConfigAccess();
184 : }
185 :
186 0 : if ( m_xConfigAccess.is() )
187 : {
188 : try
189 : {
190 0 : css::uno::Any a;
191 0 : a = m_xConfigAccess->getByName( m_aNodeRefStates );
192 0 : css::uno::Reference< css::container::XNameAccess > xNameAccess;
193 0 : if ( a >>= xNameAccess )
194 : {
195 0 : if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED )
196 0 : a = xNameAccess->getByName( m_aPropLocked );
197 0 : else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED )
198 0 : a = xNameAccess->getByName( m_aPropDocked );
199 :
200 0 : aValue = a;
201 0 : return true;
202 0 : }
203 : }
204 0 : catch ( const css::container::NoSuchElementException& )
205 : {
206 : }
207 0 : catch ( const css::uno::Exception& )
208 : {
209 : }
210 : }
211 :
212 0 : return false;
213 : }
214 :
215 162 : bool GlobalSettings_Access::impl_initConfigAccess()
216 : {
217 162 : css::uno::Sequence< css::uno::Any > aArgs( 2 );
218 324 : css::beans::PropertyValue aPropValue;
219 :
220 : try
221 : {
222 162 : if ( m_xContext.is() )
223 : {
224 : css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
225 162 : css::configuration::theDefaultProvider::get( m_xContext );
226 :
227 162 : aPropValue.Name = "nodepath";
228 162 : aPropValue.Value = css::uno::makeAny( OUString( GLOBALSETTINGS_ROOT_ACCESS ));
229 162 : aArgs[0] = css::uno::makeAny( aPropValue );
230 162 : aPropValue.Name = "lazywrite";
231 162 : aPropValue.Value = css::uno::makeAny( sal_True );
232 162 : aArgs[1] = css::uno::makeAny( aPropValue );
233 :
234 324 : m_xConfigAccess = css::uno::Reference< css::container::XNameAccess >(
235 162 : xConfigProvider->createInstanceWithArguments(
236 162 : SERVICENAME_CFGREADACCESS, aArgs ),
237 162 : css::uno::UNO_QUERY );
238 :
239 : css::uno::Reference< css::lang::XComponent >(
240 324 : xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener(
241 : css::uno::Reference< css::lang::XEventListener >(
242 : static_cast< cppu::OWeakObject* >( this ),
243 324 : css::uno::UNO_QUERY ));
244 : }
245 :
246 162 : return m_xConfigAccess.is();
247 : }
248 0 : catch ( const css::lang::WrappedTargetException& )
249 : {
250 : }
251 0 : catch ( const css::uno::Exception& )
252 : {
253 : }
254 :
255 162 : return false;
256 : }
257 :
258 : // global class
259 :
260 : struct mutexGlobalSettings : public rtl::Static< osl::Mutex, mutexGlobalSettings > {};
261 : static GlobalSettings_Access* pStaticSettings = 0;
262 :
263 11050 : static GlobalSettings_Access* GetGlobalSettings( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext )
264 : {
265 11050 : osl::MutexGuard aGuard(mutexGlobalSettings::get());
266 11050 : if ( !pStaticSettings )
267 162 : pStaticSettings = new GlobalSettings_Access( rxContext );
268 11050 : return pStaticSettings;
269 : }
270 :
271 11050 : GlobalSettings::GlobalSettings( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext ) :
272 11050 : m_xContext( rxContext )
273 : {
274 11050 : }
275 :
276 10968 : GlobalSettings::~GlobalSettings()
277 : {
278 10968 : }
279 :
280 : // settings access
281 11050 : bool GlobalSettings::HasStatesInfo( UIElementType eElementType )
282 : {
283 11050 : GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
284 :
285 11050 : if ( pSettings )
286 11050 : return pSettings->HasStatesInfo( eElementType );
287 : else
288 0 : return false;
289 : }
290 :
291 0 : bool GlobalSettings::GetStateInfo( UIElementType eElementType, StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue )
292 : {
293 0 : GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
294 :
295 0 : if ( pSettings )
296 0 : return pSettings->GetStateInfo( eElementType, eStateInfo, aValue );
297 : else
298 0 : return false;
299 : }
300 :
301 : } // namespace framework
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|