1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <uiconfiguration/globalsettings.hxx>
21 #include <services.h>
22 
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>
31 
32 #include <rtl/ustrbuf.hxx>
33 #include <rtl/instance.hxx>
34 #include <comphelper/propertysequence.hxx>
35 #include <cppuhelper/implbase.hxx>
36 
37 //  Defines
38 
39 using namespace ::com::sun::star;
40 
41 //  Namespace
42 
43 namespace framework
44 {
45 
46 //  Configuration access class for WindowState supplier implementation
47 
48 class GlobalSettings_Access : public ::cppu::WeakImplHelper<
49                                   css::lang::XComponent,
50                                   css::lang::XEventListener>
51 {
52     public:
53         explicit GlobalSettings_Access( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
54 
55         // XComponent
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;
59 
60         // XEventListener
61         virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
62 
63         // settings access
64         bool HasToolbarStatesInfo();
65         bool GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue );
66 
67     private:
68         void impl_initConfigAccess();
69 
70         osl::Mutex                                                m_mutex;
71         bool                                                      m_bDisposed   : 1,
72                                                                   m_bConfigRead : 1;
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;
79 };
80 
81 GlobalSettings_Access::GlobalSettings_Access( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
82     m_bDisposed( false ),
83     m_bConfigRead( false ),
84     m_aNodeRefStates( "States" ),
85     m_aPropStatesEnabled( "StatesEnabled" ),
86     m_aPropLocked( "Locked" ),
87     m_aPropDocked( "Docked" ),
88     m_xContext( rxContext )
89 {
90 }
91 
92 // XComponent
93 void SAL_CALL GlobalSettings_Access::dispose()
94 {
95     osl::MutexGuard g(m_mutex);
96     m_xConfigAccess.clear();
97     m_bDisposed = true;
98 }
99 
100 void SAL_CALL GlobalSettings_Access::addEventListener( const css::uno::Reference< css::lang::XEventListener >& )
101 {
102 }
103 
104 void SAL_CALL GlobalSettings_Access::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& )
105 {
106 }
107 
108 // XEventListener
109 void SAL_CALL GlobalSettings_Access::disposing( const css::lang::EventObject& )
110 {
111     osl::MutexGuard g(m_mutex);
112     m_xConfigAccess.clear();
113 }
114 
115 // settings access
116 bool GlobalSettings_Access::HasToolbarStatesInfo()
117 {
118     osl::MutexGuard g(m_mutex);
119 
120     if ( m_bDisposed )
121         return false;
122 
123     if ( !m_bConfigRead )
124     {
125         m_bConfigRead = true;
126         impl_initConfigAccess();
127     }
128 
129     if ( m_xConfigAccess.is() )
130     {
131         try
132         {
133             css::uno::Any a;
134             bool      bValue;
135             a = m_xConfigAccess->getByName( m_aPropStatesEnabled );
136             if ( a >>= bValue )
137                 return bValue;
138         }
139         catch ( const css::container::NoSuchElementException& )
140         {
141         }
142         catch ( const css::uno::Exception& )
143         {
144         }
145     }
146 
147     return false;
148 }
149 
150 bool GlobalSettings_Access::GetToolbarStateInfo( GlobalSettings::StateInfo eStateInfo, css::uno::Any& aValue )
151 {
152     osl::MutexGuard g(m_mutex);
153 
154     if ( m_bDisposed )
155         return false;
156 
157     if ( !m_bConfigRead )
158     {
159         m_bConfigRead = true;
160         impl_initConfigAccess();
161     }
162 
163     if ( m_xConfigAccess.is() )
164     {
165         try
166         {
167             css::uno::Any a;
168             a = m_xConfigAccess->getByName( m_aNodeRefStates );
169             css::uno::Reference< css::container::XNameAccess > xNameAccess;
170             if ( a >>= xNameAccess )
171             {
172                 if ( eStateInfo == GlobalSettings::STATEINFO_LOCKED )
173                     a = xNameAccess->getByName( m_aPropLocked );
174                 else if ( eStateInfo == GlobalSettings::STATEINFO_DOCKED )
175                     a = xNameAccess->getByName( m_aPropDocked );
176 
177                 aValue = a;
178                 return true;
179             }
180         }
181         catch ( const css::container::NoSuchElementException& )
182         {
183         }
184         catch ( const css::uno::Exception& )
185         {
186         }
187     }
188 
189     return false;
190 }
191 
192 void GlobalSettings_Access::impl_initConfigAccess()
193 {
194     try
195     {
196         if ( m_xContext.is() )
197         {
198             css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
199                  css::configuration::theDefaultProvider::get( m_xContext );
200 
201             uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
202             {
203                 {"nodepath", uno::Any(OUString("/org.openoffice.Office.UI.GlobalSettings/Toolbars"))}
204             }));
205             m_xConfigAccess.set(xConfigProvider->createInstanceWithArguments(
206                                     SERVICENAME_CFGREADACCESS, aArgs ),
207                                 css::uno::UNO_QUERY );
208 
209             css::uno::Reference< css::lang::XComponent >(
210                 xConfigProvider, css::uno::UNO_QUERY_THROW )->addEventListener(
211                     css::uno::Reference< css::lang::XEventListener >(
212                         static_cast< cppu::OWeakObject* >( this ),
213                         css::uno::UNO_QUERY ));
214         }
215     }
216     catch ( const css::lang::WrappedTargetException& )
217     {
218     }
219     catch ( const css::uno::Exception& )
220     {
221     }
222 }
223 
224 //  global class
225 
226 struct mutexGlobalSettings : public rtl::Static< osl::Mutex, mutexGlobalSettings > {};
227 static GlobalSettings_Access* pStaticSettings = nullptr;
228 
229 static GlobalSettings_Access* GetGlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
230 {
231     osl::MutexGuard aGuard(mutexGlobalSettings::get());
232     if ( !pStaticSettings )
233         pStaticSettings = new GlobalSettings_Access( rxContext );
234     return pStaticSettings;
235 }
236 
237 GlobalSettings::GlobalSettings( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
238     m_xContext( rxContext )
239 {
240 }
241 
242 GlobalSettings::~GlobalSettings()
243 {
244 }
245 
246 // settings access
247 bool GlobalSettings::HasToolbarStatesInfo()
248 {
249     GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
250 
251     if ( pSettings )
252         return pSettings->HasToolbarStatesInfo();
253     else
254         return false;
255 }
256 
257 bool GlobalSettings::GetToolbarStateInfo( StateInfo eStateInfo, css::uno::Any& aValue )
258 {
259     GlobalSettings_Access* pSettings( GetGlobalSettings( m_xContext ));
260 
261     if ( pSettings )
262         return pSettings->GetToolbarStateInfo( eStateInfo, aValue );
263     else
264         return false;
265 }
266 
267 } // namespace framework
268 
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
270