1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uiconfiguration/globalsettings.hxx>
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>
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/instance.hxx>
34 #include <comphelper/propertysequence.hxx>
35 #include <cppuhelper/implbase.hxx>
39 using namespace ::com::sun::star
;
46 // Configuration access class for WindowState supplier implementation
48 class GlobalSettings_Access
: public ::cppu::WeakImplHelper
<
49 css::lang::XComponent
,
50 css::lang::XEventListener
>
53 explicit GlobalSettings_Access( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
);
56 virtual void SAL_CALL
dispose() override
;
57 virtual void SAL_CALL
addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& xListener
) override
;
58 virtual void SAL_CALL
removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& aListener
) override
;
61 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
64 bool HasToolbarStatesInfo();
65 bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo
, css::uno::Any
& aValue
);
68 void impl_initConfigAccess();
73 OUString m_aNodeRefStates
;
74 OUString m_aPropStatesEnabled
;
75 OUString m_aPropLocked
;
76 OUString m_aPropDocked
;
77 css::uno::Reference
< css::container::XNameAccess
> m_xConfigAccess
;
78 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
81 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
) :
83 m_bConfigRead( false ),
84 m_aNodeRefStates( "States" ),
85 m_aPropStatesEnabled( "StatesEnabled" ),
86 m_aPropLocked( "Locked" ),
87 m_aPropDocked( "Docked" ),
88 m_xContext( rxContext
)
93 void SAL_CALL
GlobalSettings_Access::dispose()
95 osl::MutexGuard
g(m_mutex
);
96 m_xConfigAccess
.clear();
100 void SAL_CALL
GlobalSettings_Access::addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
104 void SAL_CALL
GlobalSettings_Access::removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
109 void SAL_CALL
GlobalSettings_Access::disposing( const css::lang::EventObject
& )
111 osl::MutexGuard
g(m_mutex
);
112 m_xConfigAccess
.clear();
116 bool GlobalSettings_Access::HasToolbarStatesInfo()
118 osl::MutexGuard
g(m_mutex
);
123 if ( !m_bConfigRead
)
125 m_bConfigRead
= true;
126 impl_initConfigAccess();
129 if ( m_xConfigAccess
.is() )
135 a
= m_xConfigAccess
->getByName( m_aPropStatesEnabled
);
139 catch ( const css::container::NoSuchElementException
& )
142 catch ( const css::uno::Exception
& )
150 bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo
, css::uno::Any
& aValue
)
152 osl::MutexGuard
g(m_mutex
);
157 if ( !m_bConfigRead
)
159 m_bConfigRead
= true;
160 impl_initConfigAccess();
163 if ( m_xConfigAccess
.is() )
167 css::uno::Any a
= m_xConfigAccess
->getByName( m_aNodeRefStates
);
168 css::uno::Reference
< css::container::XNameAccess
> xNameAccess
;
169 if ( a
>>= xNameAccess
)
171 if ( eStateInfo
== GlobalSettings::STATEINFO_LOCKED
)
172 a
= xNameAccess
->getByName( m_aPropLocked
);
173 else if ( eStateInfo
== GlobalSettings::STATEINFO_DOCKED
)
174 a
= xNameAccess
->getByName( m_aPropDocked
);
180 catch ( const css::container::NoSuchElementException
& )
183 catch ( const css::uno::Exception
& )
191 void GlobalSettings_Access::impl_initConfigAccess()
195 if ( m_xContext
.is() )
197 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider
=
198 css::configuration::theDefaultProvider::get( m_xContext
);
200 uno::Sequence
<uno::Any
> aArgs(comphelper::InitAnyPropertySequence(
202 {"nodepath", uno::Any(OUString("/org.openoffice.Office.UI.GlobalSettings/Toolbars"))}
204 m_xConfigAccess
.set(xConfigProvider
->createInstanceWithArguments(
205 SERVICENAME_CFGREADACCESS
, aArgs
),
206 css::uno::UNO_QUERY
);
208 css::uno::Reference
< css::lang::XComponent
>(
209 xConfigProvider
, css::uno::UNO_QUERY_THROW
)->addEventListener(
210 css::uno::Reference
< css::lang::XEventListener
>(
211 static_cast< cppu::OWeakObject
* >( this ),
212 css::uno::UNO_QUERY
));
215 catch ( const css::lang::WrappedTargetException
& )
218 catch ( const css::uno::Exception
& )
225 struct mutexGlobalSettings
: public rtl::Static
< osl::Mutex
, mutexGlobalSettings
> {};
226 static GlobalSettings_Access
* pStaticSettings
= nullptr;
228 static GlobalSettings_Access
* GetGlobalSettings( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
230 osl::MutexGuard
aGuard(mutexGlobalSettings::get());
231 if ( !pStaticSettings
)
232 pStaticSettings
= new GlobalSettings_Access( rxContext
);
233 return pStaticSettings
;
236 GlobalSettings::GlobalSettings( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
) :
237 m_xContext( rxContext
)
241 GlobalSettings::~GlobalSettings()
246 bool GlobalSettings::HasToolbarStatesInfo() const
248 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
251 return pSettings
->HasToolbarStatesInfo();
256 bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo
, css::uno::Any
& aValue
)
258 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
261 return pSettings
->GetToolbarStateInfo( eStateInfo
, aValue
);
266 } // namespace framework
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */