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