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