1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26
27 //_________________________________________________________________________________________________________________
28 // my own includes
29 //_________________________________________________________________________________________________________________
30 #include "uiconfiguration/globalsettings.hxx"
31 #include <threadhelp/resetableguard.hxx>
32 #include "services.h"
33
34 //_________________________________________________________________________________________________________________
35 // interface includes
36 //_________________________________________________________________________________________________________________
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
40 #include <com/sun/star/container/XNameContainer.hpp>
41 #include <com/sun/star/container/XContainer.hpp>
42 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/lang/XEventListener.hpp>
44
45 //_________________________________________________________________________________________________________________
46 // includes of other projects
47 //_________________________________________________________________________________________________________________
48 #include <rtl/ustrbuf.hxx>
49 #include <rtl/instance.hxx>
50 #include <cppuhelper/weak.hxx>
51 #include <tools/debug.hxx>
52
53 //_________________________________________________________________________________________________________________
54 // Defines
55 //_________________________________________________________________________________________________________________
56 //
57
58 using namespace rtl;
59 using namespace ::com::sun::star;
60
61 //_________________________________________________________________________________________________________________
62 // Namespace
63 //_________________________________________________________________________________________________________________
64 //
65
66 static const char GLOBALSETTINGS_ROOT_ACCESS[] = "/org.openoffice.Office.UI.GlobalSettings/Toolbars";
67
68 static const char GLOBALSETTINGS_NODEREF_STATES[] = "States";
69 static const char GLOBALSETTINGS_PROPERTY_LOCKED[] = "Locked";
70 static const char GLOBALSETTINGS_PROPERTY_DOCKED[] = "Docked";
71 static const char GLOBALSETTINGS_PROPERTY_STATESENABLED[] = "StatesEnabled";
72
73 namespace framework
74 {
75
76 //*****************************************************************************************************************
77 // Configuration access class for WindowState supplier implementation
78 //*****************************************************************************************************************
79
80 class GlobalSettings_Access : public ::com::sun::star::lang::XComponent ,
81 public ::com::sun::star::lang::XEventListener ,
82 private ThreadHelpBase , // Struct for right initialization of mutex member! Must be first of baseclasses.
83 public ::cppu::OWeakObject
84 {
85 public:
86 GlobalSettings_Access( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager );
87 virtual ~GlobalSettings_Access();
88
89 // XInterface, XTypeProvider, XServiceInfo
90 FWK_DECLARE_XINTERFACE
91
92 // XComponent
93 virtual void SAL_CALL dispose();
94 virtual void SAL_CALL addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& xListener );
95 virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener );
96
97 // XEventListener
98 virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source );
99
100 // settings access
101 sal_Bool HasStatesInfo( GlobalSettings::UIElementType eElementType );
102 sal_Bool GetStateInfo( GlobalSettings::UIElementType eElementType, GlobalSettings::StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue );
103
104 private:
105 sal_Bool impl_initConfigAccess();
106
107 sal_Bool m_bDisposed : 1,
108 m_bConfigRead : 1;
109 rtl::OUString m_aConfigSettingsAccess;
110 rtl::OUString m_aNodeRefStates;
111 rtl::OUString m_aPropStatesEnabled;
112 rtl::OUString m_aPropLocked;
113 rtl::OUString m_aPropDocked;
114 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > m_xConfigAccess;
115 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xServiceManager;
116 };
117
118
119 //*****************************************************************************************************************
120 // XInterface
121 //*****************************************************************************************************************
DEFINE_XINTERFACE_2(GlobalSettings_Access,OWeakObject,DIRECT_INTERFACE (css::lang::XComponent),DIRECT_INTERFACE (css::lang::XEventListener))122 DEFINE_XINTERFACE_2 ( GlobalSettings_Access ,
123 OWeakObject ,
124 DIRECT_INTERFACE ( css::lang::XComponent ),
125 DIRECT_INTERFACE ( css::lang::XEventListener )
126 )
127
128 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference< css::lang::XMultiServiceFactory >& rServiceManager ) :
129 ThreadHelpBase(),
130 m_bDisposed( sal_False ),
131 m_bConfigRead( sal_False ),
132 m_aConfigSettingsAccess( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_ROOT_ACCESS )),
133 m_aNodeRefStates( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_NODEREF_STATES )),
134 m_aPropStatesEnabled( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_STATESENABLED )),
135 m_aPropLocked( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_LOCKED )),
136 m_aPropDocked( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_DOCKED )),
137 m_xServiceManager( rServiceManager )
138 {
139 }
140
~GlobalSettings_Access()141 GlobalSettings_Access::~GlobalSettings_Access()
142 {
143 }
144
145 // XComponent
dispose()146 void SAL_CALL GlobalSettings_Access::dispose()
147 {
148 // SAFE
149 ResetableGuard aLock( m_aLock );
150
151 m_xConfigAccess.clear();
152 m_bDisposed = sal_True;
153 }
154
addEventListener(const css::uno::Reference<css::lang::XEventListener> &)155 void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
156 {
157 }
158
removeEventListener(const css::uno::Reference<css::lang::XEventListener> &)159 void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
160 {
161 }
162
163 // XEventListener
disposing(const css::lang::EventObject &)164 void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
165 {
166 // SAFE
167 ResetableGuard aLock( m_aLock );
168 m_xConfigAccess.clear();
169 }
170
171 // settings access
HasStatesInfo(GlobalSettings::UIElementType eElementType)172 sal_Bool GlobalSettings_Access::HasStatesInfo( GlobalSettings::UIElementType eElementType )
173 {
174 ResetableGuard aLock( m_aLock );
175 if ( eElementType == GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW )
176 return sal_False;
177 else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
178 return sal_False;
179
180 if ( m_bDisposed )
181 return sal_False;
182
183 if ( !m_bConfigRead )
184 {
185 m_bConfigRead = sal_True;
186 impl_initConfigAccess();
187 }
188
189 if ( m_xConfigAccess.is() )
190 {
191 try
192 {
193 css::uno::Any a;
194 sal_Bool bValue = sal_Bool();
195 a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
196 if ( a >>= bValue )
197 return bValue;
198 }
199 catch ( css::container::NoSuchElementException& )
200 {
201 }
202 catch ( css::uno::Exception& )
203 {
204 }
205 }
206
207 return sal_False;
208 }
209
GetStateInfo(GlobalSettings::UIElementType eElementType,GlobalSettings::StateInfo eStateInfo,::com::sun::star::uno::Any & aValue)210 sal_Bool GlobalSettings_Access::GetStateInfo( GlobalSettings::UIElementType eElementType, GlobalSettings::StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue )
211 {
212 ResetableGuard aLock( m_aLock );
213 if ( eElementType == GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW )
214 return sal_False;
215 else if ( eElementType == GlobalSettings::UIELEMENT_TYPE_STATUSBAR )
216 return sal_False;
217
218 if ( m_bDisposed )
219 return sal_False;
220
221 if ( !m_bConfigRead )
222 {
223 m_bConfigRead = sal_True;
224 impl_initConfigAccess();
225 }
226
227 if ( m_xConfigAccess.is() )
228 {
229 try
230 {
231 css::uno::Any a;
232 a = m_xConfigAccess->getByName( m_aNodeRefStates );
233 css::uno::Reference< css::container::XNameAccess > xNameAccess;
234 if ( a >>= xNameAccess )
235 {
236 if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED )
237 a = xNameAccess->getByName( m_aPropLocked );
238 else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED )
239 a = xNameAccess->getByName( m_aPropDocked );
240
241 aValue = a;
242 return sal_True;
243 }
244 }
245 catch ( css::container::NoSuchElementException& )
246 {
247 }
248 catch ( css::uno::Exception& )
249 {
250 }
251 }
252
253 return sal_False;
254 }
255
impl_initConfigAccess()256 sal_Bool GlobalSettings_Access::impl_initConfigAccess()
257 {
258 css::uno::Sequence< css::uno::Any > aArgs( 2 );
259 css::beans::PropertyValue aPropValue;
260
261 try
262 {
263 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider;
264 if ( m_xServiceManager.is() )
265 xConfigProvider = css::uno::Reference< css::lang::XMultiServiceFactory >(
266 m_xServiceManager->createInstance( SERVICENAME_CFGPROVIDER ),
267 css::uno::UNO_QUERY );
268
269 if ( xConfigProvider.is() )
270 {
271 aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
272 aPropValue.Value = css::uno::makeAny( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_ROOT_ACCESS )));
273 aArgs[0] = css::uno::makeAny( aPropValue );
274 aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ));
275 aPropValue.Value = css::uno::makeAny( sal_True );
276 aArgs[1] = css::uno::makeAny( aPropValue );
277
278 m_xConfigAccess = css::uno::Reference< css::container::XNameAccess >(
279 xConfigProvider->createInstanceWithArguments(
280 SERVICENAME_CFGREADACCESS, aArgs ),
281 css::uno::UNO_QUERY );
282
283 css::uno::Reference< css::lang::XComponent > xComponent( xConfigProvider, css::uno::UNO_QUERY );
284 if ( xComponent.is() )
285 xComponent->addEventListener(
286 css::uno::Reference< css::lang::XEventListener >(
287 static_cast< cppu::OWeakObject* >( this ),
288 css::uno::UNO_QUERY ));
289 }
290
291 return m_xConfigAccess.is();
292 }
293 catch ( css::lang::WrappedTargetException& )
294 {
295 }
296 catch ( css::uno::Exception& )
297 {
298 }
299
300 return sal_False;
301 }
302
303 //*****************************************************************************************************************
304 // global class
305 //*****************************************************************************************************************
306
307 struct mutexGlobalSettings : public rtl::Static< osl::Mutex, mutexGlobalSettings > {};
308 static GlobalSettings_Access* pStaticSettings = 0;
309
GetGlobalSettings(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rSrvMgr)310 static GlobalSettings_Access* GetGlobalSettings( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rSrvMgr )
311 {
312 osl::MutexGuard aGuard(mutexGlobalSettings::get());
313 if ( !pStaticSettings )
314 pStaticSettings = new GlobalSettings_Access( rSrvMgr );
315 return pStaticSettings;
316 }
317
GlobalSettings(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rSrvMgr)318 GlobalSettings::GlobalSettings( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rSrvMgr ) :
319 m_xSrvMgr( rSrvMgr )
320 {
321 }
322
~GlobalSettings()323 GlobalSettings::~GlobalSettings()
324 {
325 }
326
327 // settings access
HasStatesInfo(UIElementType eElementType)328 sal_Bool GlobalSettings::HasStatesInfo( UIElementType eElementType )
329 {
330 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xSrvMgr ));
331
332 if ( pSettings )
333 return pSettings->HasStatesInfo( eElementType );
334 else
335 return sal_False;
336 }
337
GetStateInfo(UIElementType eElementType,StateInfo eStateInfo,::com::sun::star::uno::Any & aValue)338 sal_Bool GlobalSettings::GetStateInfo( UIElementType eElementType, StateInfo eStateInfo, ::com::sun::star::uno::Any& aValue )
339 {
340 GlobalSettings_Access* pSettings( GetGlobalSettings( m_xSrvMgr ));
341
342 if ( pSettings )
343 return pSettings->GetStateInfo( eElementType, eStateInfo, aValue );
344 else
345 return sal_False;
346 }
347
348 } // namespace framework
349