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