Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "uifactory/factoryconfiguration.hxx"
30 : : #include <threadhelp/resetableguard.hxx>
31 : : #include "services.h"
32 : :
33 : : #include "helper/mischelper.hxx"
34 : :
35 : : #include <com/sun/star/beans/PropertyValue.hpp>
36 : : #include <com/sun/star/beans/XPropertySet.hpp>
37 : : #include <com/sun/star/container/XNameAccess.hpp>
38 : : #include <com/sun/star/container/XNameContainer.hpp>
39 : : #include <com/sun/star/container/XContainer.hpp>
40 : :
41 : : #include <rtl/ustrbuf.hxx>
42 : : #include <cppuhelper/weak.hxx>
43 : : #include <rtl/logfile.hxx>
44 : :
45 : : //_________________________________________________________________________________________________________________
46 : : // Defines
47 : : //_________________________________________________________________________________________________________________
48 : : using namespace com::sun::star;
49 : : using namespace com::sun::star::uno;
50 : : using namespace com::sun::star::lang;
51 : : using namespace com::sun::star::beans;
52 : : using namespace com::sun::star::container;
53 : : using namespace ::com::sun::star::frame;
54 : :
55 : : //_________________________________________________________________________________________________________________
56 : : // Namespace
57 : : //_________________________________________________________________________________________________________________
58 : :
59 : : namespace framework
60 : : {
61 : 469194 : rtl::OUString getHashKeyFromStrings( const rtl::OUString& aCommandURL, const rtl::OUString& aModuleName )
62 : : {
63 [ + - ]: 469194 : rtl::OUStringBuffer aKey( aCommandURL );
64 [ + - ]: 469194 : aKey.appendAscii( "-" );
65 [ + - ]: 469194 : aKey.append( aModuleName );
66 [ + - ]: 469194 : return aKey.makeStringAndClear();
67 : : }
68 : :
69 : : //*****************************************************************************************************************
70 : : // XInterface, XTypeProvider
71 : : //*****************************************************************************************************************
72 : 333 : ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( Reference< XMultiServiceFactory >& rServiceManager,const ::rtl::OUString& _sRoot,bool _bAskValue ) :
73 : : ThreadHelpBase(),
74 : : m_aPropCommand( RTL_CONSTASCII_USTRINGPARAM( "Command" )),
75 : : m_aPropModule( RTL_CONSTASCII_USTRINGPARAM( "Module" )),
76 : : m_aPropController( RTL_CONSTASCII_USTRINGPARAM( "Controller" )),
77 : : m_aPropValue( RTL_CONSTASCII_USTRINGPARAM( "Value" )),
78 : : m_sRoot(_sRoot),
79 : : m_xServiceManager( rServiceManager ),
80 : : m_bConfigAccessInitialized( sal_False ),
81 [ + - ][ + - ]: 333 : m_bAskValue(_bAskValue)
[ + - ][ + - ]
[ + - ][ + - ]
82 : : {
83 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory" );
84 [ + - ][ + - ]: 333 : m_xConfigProvider = Reference< XMultiServiceFactory >( rServiceManager->createInstance( SERVICENAME_CFGPROVIDER),UNO_QUERY );
[ + - ][ + - ]
[ + - ]
85 : 333 : }
86 : :
87 [ + - ][ + - ]: 303 : ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
88 : : {
89 : : // SAFE
90 [ + - ]: 303 : ResetableGuard aLock( m_aLock );
91 : :
92 [ + - ]: 303 : Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
93 [ + + ]: 303 : if ( xContainer.is() )
94 [ + - ][ + - ]: 303 : xContainer->removeContainerListener(m_xConfigAccessListener);
[ + - ]
95 [ - + ]: 606 : }
96 : :
97 : 378850 : rtl::OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
98 : : {
99 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getServiceFromCommandModule" );
100 : : // SAFE
101 [ + - ]: 378850 : ResetableGuard aLock( m_aLock );
102 [ + - ][ + - ]: 378850 : MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
103 : :
104 [ + + ][ + - ]: 378850 : if ( pIter != m_aMenuControllerMap.end() )
105 [ + - ]: 11376 : return pIter->second.m_aImplementationName;
106 [ + + ]: 367474 : else if ( !rModule.isEmpty() )
107 : : {
108 : : // Try to detect if we have a generic popup menu controller
109 [ + - ][ + - ]: 85570 : pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
110 : :
111 [ + + ][ + - ]: 85570 : if ( pIter != m_aMenuControllerMap.end() )
112 [ + - ]: 3456 : return pIter->second.m_aImplementationName;
113 : : }
114 : :
115 [ + - ]: 378850 : return rtl::OUString();
116 : : }
117 : 764 : rtl::OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
118 : : {
119 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getValueFromCommandModule" );
120 : : // SAFE
121 [ + - ]: 764 : ResetableGuard aLock( m_aLock );
122 : :
123 [ + - ][ + - ]: 764 : MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
124 : :
125 [ - + ][ + - ]: 764 : if ( pIter != m_aMenuControllerMap.end() )
126 [ # # ]: 0 : return pIter->second.m_aValue;
127 [ + - ]: 764 : else if ( !rModule.isEmpty() )
128 : : {
129 : : // Try to detect if we have a generic popup menu controller
130 [ + - ][ + - ]: 764 : pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
131 : :
132 [ + - ][ + - ]: 764 : if ( pIter != m_aMenuControllerMap.end() )
133 [ + - ]: 764 : return pIter->second.m_aValue;
134 : : }
135 : :
136 [ + - ]: 764 : return rtl::OUString();
137 : : }
138 : :
139 : :
140 : 4 : void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
141 : : const rtl::OUString& rCommandURL,
142 : : const rtl::OUString& rModule,
143 : : const rtl::OUString& rServiceSpecifier )
144 : : {
145 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::addServiceToCommandModule" );
146 : : // SAFE
147 [ + - ]: 4 : ResetableGuard aLock( m_aLock );
148 : :
149 [ + - ]: 4 : rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
150 [ + - ][ + - ]: 4 : m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey,ControllerInfo(rServiceSpecifier,::rtl::OUString()) ));
151 : 4 : }
152 : :
153 : 2 : void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
154 : : const rtl::OUString& rCommandURL,
155 : : const rtl::OUString& rModule )
156 : : {
157 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule" );
158 : : // SAFE
159 [ + - ]: 2 : ResetableGuard aLock( m_aLock );
160 : :
161 [ + - ]: 2 : rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
162 [ + - ][ + - ]: 2 : m_aMenuControllerMap.erase( aHashKey );
163 : 2 : }
164 : :
165 : : // container.XContainerListener
166 : 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent ) throw(RuntimeException)
167 : : {
168 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementInserted" );
169 : 0 : rtl::OUString aCommand;
170 : 0 : rtl::OUString aModule;
171 : 0 : rtl::OUString aService;
172 : 0 : rtl::OUString aValue;
173 : :
174 : : // SAFE
175 [ # # ]: 0 : ResetableGuard aLock( m_aLock );
176 : :
177 [ # # ][ # # ]: 0 : if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
178 : : {
179 : : // Create hash key from command and module as they are together a primary key to
180 : : // the UNO service that implements the popup menu controller.
181 [ # # ]: 0 : rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
182 [ # # ]: 0 : ControllerInfo& rControllerInfo = m_aMenuControllerMap[ aHashKey ];
183 : 0 : rControllerInfo.m_aImplementationName = aService;
184 : 0 : rControllerInfo.m_aValue = aValue;
185 [ # # ]: 0 : }
186 : 0 : }
187 : :
188 : 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent ) throw(RuntimeException)
189 : : {
190 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementRemoved" );
191 : 0 : rtl::OUString aCommand;
192 : 0 : rtl::OUString aModule;
193 : 0 : rtl::OUString aService;
194 : 0 : rtl::OUString aValue;
195 : :
196 : : // SAFE
197 [ # # ]: 0 : ResetableGuard aLock( m_aLock );
198 : :
199 [ # # ][ # # ]: 0 : if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
200 : : {
201 : : // Create hash key from command and module as they are together a primary key to
202 : : // the UNO service that implements the popup menu controller.
203 [ # # ]: 0 : rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
204 [ # # ]: 0 : m_aMenuControllerMap.erase( aHashKey );
205 [ # # ]: 0 : }
206 : 0 : }
207 : :
208 : 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent ) throw(RuntimeException)
209 : : {
210 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementReplaced" );
211 : 0 : elementInserted(aEvent);
212 : 0 : }
213 : :
214 : : // lang.XEventListener
215 : 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& ) throw(RuntimeException)
216 : : {
217 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::disposing" );
218 : : // SAFE
219 : : // remove our reference to the config access
220 [ # # ]: 0 : ResetableGuard aLock( m_aLock );
221 [ # # ]: 0 : m_xConfigAccess.clear();
222 : 0 : }
223 : :
224 : 180 : void ConfigurationAccess_ControllerFactory::readConfigurationData()
225 : : {
226 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::readConfigurationData" );
227 : : // SAFE
228 [ + - ]: 180 : ResetableGuard aLock( m_aLock );
229 : :
230 [ + - ]: 180 : if ( !m_bConfigAccessInitialized )
231 : : {
232 [ + - ]: 180 : Sequence< Any > aArgs( 1 );
233 : 180 : PropertyValue aPropValue;
234 : :
235 [ + - ]: 180 : aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
236 [ + - ]: 180 : aPropValue.Value <<= m_sRoot;
237 [ + - ][ + - ]: 180 : aArgs[0] <<= aPropValue;
238 : :
239 : : try
240 : : {
241 [ + - ][ + - ]: 180 : m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
[ + - ][ + - ]
[ + - ][ # # ]
242 : : }
243 [ # # ]: 0 : catch ( const WrappedTargetException& )
244 : : {
245 : : }
246 : :
247 [ + - ]: 180 : m_bConfigAccessInitialized = sal_True;
248 : : }
249 : :
250 [ + - ]: 180 : if ( m_xConfigAccess.is() )
251 : : {
252 : : // Read and update configuration data
253 [ + - ]: 180 : updateConfigurationData();
254 : :
255 [ + - ]: 180 : uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
256 : : // UNSAFE
257 [ + - ]: 180 : aLock.unlock();
258 : :
259 [ + - ]: 180 : if ( xContainer.is() )
260 : : {
261 [ + - ][ + - ]: 180 : m_xConfigAccessListener = new WeakContainerListener(this);
[ + - ][ + - ]
262 [ + - ][ + - ]: 180 : xContainer->addContainerListener(m_xConfigAccessListener);
263 : 180 : }
264 [ + - ]: 180 : }
265 : 180 : }
266 : :
267 : 180 : void ConfigurationAccess_ControllerFactory::updateConfigurationData()
268 : : {
269 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::updateConfigurationData" );
270 : : // SAFE
271 [ + - ]: 180 : ResetableGuard aLock( m_aLock );
272 [ + - ]: 180 : if ( m_xConfigAccess.is() )
273 : : {
274 [ + - ][ + - ]: 180 : Sequence< rtl::OUString > aPopupMenuControllers = m_xConfigAccess->getElementNames();
275 : :
276 : 180 : rtl::OUString aCommand;
277 : 180 : rtl::OUString aModule;
278 : 180 : rtl::OUString aService;
279 : 180 : rtl::OUString aHashKey;
280 : 180 : rtl::OUString aValue;
281 : :
282 [ + - ]: 180 : m_aMenuControllerMap.clear();
283 [ + + ]: 3420 : for ( sal_Int32 i = 0; i < aPopupMenuControllers.getLength(); i++ )
284 : : {
285 : : try
286 : : {
287 [ + - ][ + - ]: 3240 : if ( impl_getElementProps( m_xConfigAccess->getByName( aPopupMenuControllers[i] ), aCommand, aModule, aService,aValue ))
[ + - ][ + - ]
[ + - ]
288 : : {
289 : : // Create hash key from command and module as they are together a primary key to
290 : : // the UNO service that implements the popup menu controller.
291 [ + - ]: 3240 : aHashKey = getHashKeyFromStrings( aCommand, aModule );
292 [ # # # ]: 3240 : m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey, ControllerInfo(aService,aValue) ));
[ + - ]
293 : : }
294 : : }
295 [ # # ]: 0 : catch ( const NoSuchElementException& )
296 : : {
297 : : }
298 [ # # ]: 0 : catch ( const WrappedTargetException& )
299 : : {
300 : : }
301 [ + - ]: 180 : }
302 [ + - ]: 180 : }
303 : 180 : }
304 : :
305 : 3240 : sal_Bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, rtl::OUString& aCommand, rtl::OUString& aModule, rtl::OUString& aServiceSpecifier,rtl::OUString& aValue ) const
306 : : {
307 : : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::impl_getElementProps" );
308 : 3240 : Reference< XPropertySet > xPropertySet;
309 [ + - ]: 3240 : aElement >>= xPropertySet;
310 : :
311 [ + - ]: 3240 : if ( xPropertySet.is() )
312 : : {
313 : : try
314 : : {
315 [ + - ][ + - ]: 3240 : xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
316 [ + - ][ + - ]: 3240 : xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
317 [ + - ][ + - ]: 3240 : xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
318 [ + + ]: 3240 : if ( m_bAskValue )
319 [ + - ][ + - ]: 540 : xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
320 : : }
321 [ # # ]: 0 : catch ( const com::sun::star::beans::UnknownPropertyException& )
322 : : {
323 : 0 : return sal_False;
324 : : }
325 [ # # ]: 0 : catch ( const com::sun::star::lang::WrappedTargetException& )
326 : : {
327 : 0 : return sal_False;
328 : : }
329 : : }
330 : :
331 [ # # # ]: 3240 : return sal_True;
332 : : }
333 : : } // namespace framework
334 : :
335 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|