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