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/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/lang/XEventListener.hpp>
28 #include <rtl/ref.hxx>
29 #include <comphelper/propertysequence.hxx>
30 #include <cppuhelper/implbase.hxx>
36 using namespace ::com::sun::star
;
43 // Configuration access class for WindowState supplier implementation
47 class GlobalSettings_Access
: public ::cppu::WeakImplHelper
<
48 css::lang::XComponent
,
49 css::lang::XEventListener
>
52 explicit GlobalSettings_Access( css::uno::Reference
< css::uno::XComponentContext
> xContext
);
55 virtual void SAL_CALL
dispose() override
;
56 virtual void SAL_CALL
addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& xListener
) override
;
57 virtual void SAL_CALL
removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& aListener
) override
;
60 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
63 bool HasToolbarStatesInfo();
64 bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo
, css::uno::Any
& aValue
);
67 void impl_initConfigAccess();
72 OUString m_aNodeRefStates
;
73 OUString m_aPropStatesEnabled
;
74 OUString m_aPropLocked
;
75 OUString m_aPropDocked
;
76 css::uno::Reference
< css::container::XNameAccess
> m_xConfigAccess
;
77 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
82 GlobalSettings_Access::GlobalSettings_Access( css::uno::Reference
< css::uno::XComponentContext
> xContext
) :
84 m_bConfigRead( false ),
85 m_aNodeRefStates( "States" ),
86 m_aPropStatesEnabled( "StatesEnabled" ),
87 m_aPropLocked( "Locked" ),
88 m_aPropDocked( "Docked" ),
89 m_xContext(std::move( xContext
))
94 void SAL_CALL
GlobalSettings_Access::dispose()
96 std::unique_lock
g(m_mutex
);
97 m_xConfigAccess
.clear();
101 void SAL_CALL
GlobalSettings_Access::addEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
105 void SAL_CALL
GlobalSettings_Access::removeEventListener( const css::uno::Reference
< css::lang::XEventListener
>& )
110 void SAL_CALL
GlobalSettings_Access::disposing( const css::lang::EventObject
& )
112 std::unique_lock
g(m_mutex
);
113 m_xConfigAccess
.clear();
117 bool GlobalSettings_Access::HasToolbarStatesInfo()
119 std::unique_lock
g(m_mutex
);
124 if ( !m_bConfigRead
)
126 m_bConfigRead
= true;
127 impl_initConfigAccess();
130 if ( m_xConfigAccess
.is() )
136 a
= m_xConfigAccess
->getByName( m_aPropStatesEnabled
);
140 catch ( const css::container::NoSuchElementException
& )
143 catch ( const css::uno::Exception
& )
151 bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo
, css::uno::Any
& aValue
)
153 std::unique_lock
g(m_mutex
);
158 if ( !m_bConfigRead
)
160 m_bConfigRead
= true;
161 impl_initConfigAccess();
164 if ( !m_xConfigAccess
.is() )
169 css::uno::Any a
= m_xConfigAccess
->getByName( m_aNodeRefStates
);
170 css::uno::Reference
< css::container::XNameAccess
> xNameAccess
;
171 if ( a
>>= xNameAccess
)
173 if ( eStateInfo
== GlobalSettings::STATEINFO_LOCKED
)
174 a
= xNameAccess
->getByName( m_aPropLocked
);
175 else if ( eStateInfo
== GlobalSettings::STATEINFO_DOCKED
)
176 a
= xNameAccess
->getByName( m_aPropDocked
);
182 catch ( const css::container::NoSuchElementException
& )
185 catch ( const css::uno::Exception
& )
192 void GlobalSettings_Access::impl_initConfigAccess()
196 if ( m_xContext
.is() )
198 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider
=
199 css::configuration::theDefaultProvider::get( m_xContext
);
201 uno::Sequence
<uno::Any
> aArgs(comphelper::InitAnyPropertySequence(
203 {"nodepath", uno::Any(OUString("/org.openoffice.Office.UI.GlobalSettings/Toolbars"))}
205 m_xConfigAccess
.set(xConfigProvider
->createInstanceWithArguments(
206 SERVICENAME_CFGREADACCESS
, aArgs
),
207 css::uno::UNO_QUERY
);
209 css::uno::Reference
< css::lang::XComponent
>(
210 xConfigProvider
, css::uno::UNO_QUERY_THROW
)->addEventListener(
211 css::uno::Reference
< css::lang::XEventListener
>(this));
214 catch ( const css::lang::WrappedTargetException
& )
217 catch ( const css::uno::Exception
& )
224 static GlobalSettings_Access
* GetGlobalSettings( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
)
226 static rtl::Reference
<GlobalSettings_Access
> pStaticSettings
= new GlobalSettings_Access( rxContext
);
227 return pStaticSettings
.get();
230 GlobalSettings::GlobalSettings( css::uno::Reference
< css::uno::XComponentContext
> xContext
) :
231 m_xContext(std::move( xContext
))
235 GlobalSettings::~GlobalSettings()
240 bool GlobalSettings::HasToolbarStatesInfo() const
242 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
245 return pSettings
->HasToolbarStatesInfo();
250 bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo
, css::uno::Any
& aValue
)
252 GlobalSettings_Access
* pSettings( GetGlobalSettings( m_xContext
));
255 return pSettings
->GetToolbarStateInfo( eStateInfo
, aValue
);
260 } // namespace framework
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */