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