1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: globalsettings.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include "uiconfiguration/globalsettings.hxx"
38 #include <threadhelp/resetableguard.hxx>
41 //_________________________________________________________________________________________________________________
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/container/XNameAccess.hpp>
47 #include <com/sun/star/container/XNameContainer.hpp>
48 #include <com/sun/star/container/XContainer.hpp>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/lang/XEventListener.hpp>
52 //_________________________________________________________________________________________________________________
53 // includes of other projects
54 //_________________________________________________________________________________________________________________
55 #include <rtl/ustrbuf.hxx>
56 #include <rtl/instance.hxx>
57 #include <cppuhelper/weak.hxx>
58 #include <tools/debug.hxx>
60 //_________________________________________________________________________________________________________________
62 //_________________________________________________________________________________________________________________
66 using namespace ::com::sun::star
;
68 //_________________________________________________________________________________________________________________
70 //_________________________________________________________________________________________________________________
73 static const char GLOBALSETTINGS_ROOT_ACCESS
[] = "/org.openoffice.Office.UI.GlobalSettings/Toolbars";
75 static const char GLOBALSETTINGS_NODEREF_STATES
[] = "States";
76 static const char GLOBALSETTINGS_PROPERTY_LOCKED
[] = "Locked";
77 static const char GLOBALSETTINGS_PROPERTY_DOCKED
[] = "Docked";
78 static const char GLOBALSETTINGS_PROPERTY_STATESENABLED
[] = "StatesEnabled";
83 //*****************************************************************************************************************
84 // Configuration access class for WindowState supplier implementation
85 //*****************************************************************************************************************
87 class GlobalSettings_Access
: public ::com::sun::star::lang::XComponent
,
88 public ::com::sun::star::lang::XEventListener
,
89 private ThreadHelpBase
, // Struct for right initalization of mutex member! Must be first of baseclasses.
90 public ::cppu::OWeakObject
93 GlobalSettings_Access( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& xServiceManager
);
94 virtual ~GlobalSettings_Access();
96 // XInterface, XTypeProvider, XServiceInfo
97 FWK_DECLARE_XINTERFACE
100 virtual void SAL_CALL
dispose() throw (::com::sun::star::uno::RuntimeException
);
101 virtual void SAL_CALL
addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
);
102 virtual void SAL_CALL
removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& aListener
) throw (::com::sun::star::uno::RuntimeException
);
105 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
) throw (::com::sun::star::uno::RuntimeException
);
108 sal_Bool
HasStatesInfo( GlobalSettings::UIElementType eElementType
);
109 sal_Bool
GetStateInfo( GlobalSettings::UIElementType eElementType
, GlobalSettings::StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
);
112 sal_Bool
impl_initConfigAccess();
114 sal_Bool m_bDisposed
: 1,
116 rtl::OUString m_aConfigSettingsAccess
;
117 rtl::OUString m_aNodeRefStates
;
118 rtl::OUString m_aPropStatesEnabled
;
119 rtl::OUString m_aPropLocked
;
120 rtl::OUString m_aPropDocked
;
121 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xConfigAccess
;
122 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> m_xServiceManager
;
126 //*****************************************************************************************************************
128 //*****************************************************************************************************************
129 DEFINE_XINTERFACE_2 ( GlobalSettings_Access
,
131 DIRECT_INTERFACE ( css::lang::XComponent
),
132 DIRECT_INTERFACE ( css::lang::XEventListener
)
135 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& rServiceManager
) :
137 m_bDisposed( sal_False
),
138 m_bConfigRead( sal_False
),
139 m_aConfigSettingsAccess( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_ROOT_ACCESS
)),
140 m_aNodeRefStates( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_NODEREF_STATES
)),
141 m_aPropStatesEnabled( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_STATESENABLED
)),
142 m_aPropLocked( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_LOCKED
)),
143 m_aPropDocked( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_PROPERTY_DOCKED
)),
144 m_xServiceManager( rServiceManager
)
148 GlobalSettings_Access::~GlobalSettings_Access()
153 void SAL_CALL
GlobalSettings_Access::dispose()
154 throw ( css::uno::RuntimeException
)
157 ResetableGuard
aLock( m_aLock
);
159 m_xConfigAccess
.clear();
160 m_bDisposed
= sal_True
;
163 void SAL_CALL
GlobalSettings_Access::addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
164 throw (css::uno::RuntimeException
)
168 void SAL_CALL
GlobalSettings_Access::removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
169 throw (css::uno::RuntimeException
)
174 void SAL_CALL
GlobalSettings_Access::disposing( const css::lang::EventObject
& )
175 throw (css::uno::RuntimeException
)
178 ResetableGuard
aLock( m_aLock
);
179 m_xConfigAccess
.clear();
183 sal_Bool
GlobalSettings_Access::HasStatesInfo( GlobalSettings::UIElementType eElementType
)
185 ResetableGuard
aLock( m_aLock
);
186 if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW
)
188 else if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_STATUSBAR
)
194 if ( !m_bConfigRead
)
196 m_bConfigRead
= sal_True
;
197 impl_initConfigAccess();
200 if ( m_xConfigAccess
.is() )
205 sal_Bool bValue
= sal_Bool();
206 a
= m_xConfigAccess
->getByName( m_aPropStatesEnabled
);
210 catch ( css::container::NoSuchElementException
& )
213 catch ( css::uno::Exception
& )
221 sal_Bool
GlobalSettings_Access::GetStateInfo( GlobalSettings::UIElementType eElementType
, GlobalSettings::StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
)
223 ResetableGuard
aLock( m_aLock
);
224 if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_DOCKWINDOW
)
226 else if ( eElementType
== GlobalSettings::UIELEMENT_TYPE_STATUSBAR
)
232 if ( !m_bConfigRead
)
234 m_bConfigRead
= sal_True
;
235 impl_initConfigAccess();
238 if ( m_xConfigAccess
.is() )
243 a
= m_xConfigAccess
->getByName( m_aNodeRefStates
);
244 css::uno::Reference
< css::container::XNameAccess
> xNameAccess
;
245 if ( a
>>= xNameAccess
)
247 if ( eStateInfo
== GlobalSettings::STATEINFO_LOCKED
)
248 a
= xNameAccess
->getByName( m_aPropLocked
);
249 else if ( eStateInfo
== GlobalSettings::STATEINFO_DOCKED
)
250 a
= xNameAccess
->getByName( m_aPropDocked
);
256 catch ( css::container::NoSuchElementException
& )
259 catch ( css::uno::Exception
& )
267 sal_Bool
GlobalSettings_Access::impl_initConfigAccess()
269 css::uno::Sequence
< css::uno::Any
> aArgs( 2 );
270 css::beans::PropertyValue aPropValue
;
274 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider
;
275 if ( m_xServiceManager
.is() )
276 xConfigProvider
= css::uno::Reference
< css::lang::XMultiServiceFactory
>(
277 m_xServiceManager
->createInstance( SERVICENAME_CFGPROVIDER
),
278 css::uno::UNO_QUERY
);
280 if ( xConfigProvider
.is() )
282 aPropValue
.Name
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
283 aPropValue
.Value
= css::uno::makeAny( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( GLOBALSETTINGS_ROOT_ACCESS
)));
284 aArgs
[0] = css::uno::makeAny( aPropValue
);
285 aPropValue
.Name
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "lazywrite" ));
286 aPropValue
.Value
= css::uno::makeAny( sal_True
);
287 aArgs
[1] = css::uno::makeAny( aPropValue
);
289 m_xConfigAccess
= css::uno::Reference
< css::container::XNameAccess
>(
290 xConfigProvider
->createInstanceWithArguments(
291 SERVICENAME_CFGREADACCESS
, aArgs
),
292 css::uno::UNO_QUERY
);
294 css::uno::Reference
< css::lang::XComponent
> xComponent( xConfigProvider
, css::uno::UNO_QUERY
);
295 if ( xComponent
.is() )
296 xComponent
->addEventListener(
297 css::uno::Reference
< css::lang::XEventListener
>(
298 static_cast< cppu::OWeakObject
* >( this ),
299 css::uno::UNO_QUERY
));
302 return m_xConfigAccess
.is();
304 catch ( css::lang::WrappedTargetException
& )
307 catch ( css::uno::Exception
& )
314 //*****************************************************************************************************************
316 //*****************************************************************************************************************
318 struct mutexGlobalSettings
: public rtl::Static
< osl::Mutex
, mutexGlobalSettings
> {};
319 static GlobalSettings_Access
* pStaticSettings
= 0;
321 static GlobalSettings_Access
* GetGlobalSettings( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rSrvMgr
)
323 osl::MutexGuard
aGuard(mutexGlobalSettings::get());
324 if ( !pStaticSettings
)
325 pStaticSettings
= new GlobalSettings_Access( rSrvMgr
);
326 return pStaticSettings
;
329 GlobalSettings::GlobalSettings( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& rSrvMgr
) :
334 GlobalSettings::~GlobalSettings()
339 sal_Bool
GlobalSettings::HasStatesInfo( UIElementType eElementType
)
341 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xSrvMgr
));
344 return pSettings
->HasStatesInfo( eElementType
);
349 sal_Bool
GlobalSettings::GetStateInfo( UIElementType eElementType
, StateInfo eStateInfo
, ::com::sun::star::uno::Any
& aValue
)
351 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xSrvMgr
));
354 return pSettings
->GetStateInfo( eElementType
, eStateInfo
, aValue
);
359 } // namespace framework