Line data Source code
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 "uifactory/factoryconfiguration.hxx"
21 : #include <threadhelp/resetableguard.hxx>
22 : #include "services.h"
23 :
24 : #include "helper/mischelper.hxx"
25 :
26 : #include <com/sun/star/beans/PropertyValue.hpp>
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
29 : #include <com/sun/star/container/XNameAccess.hpp>
30 : #include <com/sun/star/container/XNameContainer.hpp>
31 : #include <com/sun/star/container/XContainer.hpp>
32 :
33 : #include <rtl/ustrbuf.hxx>
34 : #include <cppuhelper/weak.hxx>
35 : #include <rtl/logfile.hxx>
36 :
37 : //_________________________________________________________________________________________________________________
38 : // Defines
39 : //_________________________________________________________________________________________________________________
40 : using namespace com::sun::star;
41 : using namespace com::sun::star::uno;
42 : using namespace com::sun::star::lang;
43 : using namespace com::sun::star::beans;
44 : using namespace com::sun::star::container;
45 : using namespace ::com::sun::star::frame;
46 :
47 : //_________________________________________________________________________________________________________________
48 : // Namespace
49 : //_________________________________________________________________________________________________________________
50 :
51 : namespace framework
52 : {
53 2528 : rtl::OUString getHashKeyFromStrings( const rtl::OUString& aCommandURL, const rtl::OUString& aModuleName )
54 : {
55 2528 : rtl::OUStringBuffer aKey( aCommandURL );
56 2528 : aKey.appendAscii( "-" );
57 2528 : aKey.append( aModuleName );
58 2528 : return aKey.makeStringAndClear();
59 : }
60 :
61 : //*****************************************************************************************************************
62 : // XInterface, XTypeProvider
63 : //*****************************************************************************************************************
64 24 : ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( const Reference< XComponentContext >& rxContext, const ::rtl::OUString& _sRoot,bool _bAskValue ) :
65 : ThreadHelpBase(),
66 : m_aPropCommand( RTL_CONSTASCII_USTRINGPARAM( "Command" )),
67 : m_aPropModule( RTL_CONSTASCII_USTRINGPARAM( "Module" )),
68 : m_aPropController( RTL_CONSTASCII_USTRINGPARAM( "Controller" )),
69 : m_aPropValue( RTL_CONSTASCII_USTRINGPARAM( "Value" )),
70 : m_sRoot(_sRoot),
71 : m_bConfigAccessInitialized( sal_False ),
72 24 : m_bAskValue(_bAskValue)
73 : {
74 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory" );
75 24 : m_xConfigProvider = configuration::theDefaultProvider::get( rxContext );
76 24 : }
77 :
78 9 : ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
79 : {
80 : // SAFE
81 3 : ResetableGuard aLock( m_aLock );
82 :
83 3 : Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
84 3 : if ( xContainer.is() )
85 1 : xContainer->removeContainerListener(m_xConfigAccessListener);
86 6 : }
87 :
88 2360 : rtl::OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
89 : {
90 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getServiceFromCommandModule" );
91 : // SAFE
92 2360 : ResetableGuard aLock( m_aLock );
93 2360 : MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
94 :
95 2360 : if ( pIter != m_aMenuControllerMap.end() )
96 0 : return pIter->second.m_aImplementationName;
97 2360 : else if ( !rModule.isEmpty() )
98 : {
99 : // Try to detect if we have a generic popup menu controller
100 0 : pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
101 :
102 0 : if ( pIter != m_aMenuControllerMap.end() )
103 0 : return pIter->second.m_aImplementationName;
104 : }
105 :
106 2360 : return rtl::OUString();
107 : }
108 0 : rtl::OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
109 : {
110 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getValueFromCommandModule" );
111 : // SAFE
112 0 : ResetableGuard aLock( m_aLock );
113 :
114 0 : MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
115 :
116 0 : if ( pIter != m_aMenuControllerMap.end() )
117 0 : return pIter->second.m_aValue;
118 0 : else if ( !rModule.isEmpty() )
119 : {
120 : // Try to detect if we have a generic popup menu controller
121 0 : pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
122 :
123 0 : if ( pIter != m_aMenuControllerMap.end() )
124 0 : return pIter->second.m_aValue;
125 : }
126 :
127 0 : return rtl::OUString();
128 : }
129 :
130 :
131 0 : void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
132 : const rtl::OUString& rCommandURL,
133 : const rtl::OUString& rModule,
134 : const rtl::OUString& rServiceSpecifier )
135 : {
136 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::addServiceToCommandModule" );
137 : // SAFE
138 0 : ResetableGuard aLock( m_aLock );
139 :
140 0 : rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
141 0 : m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey,ControllerInfo(rServiceSpecifier,::rtl::OUString()) ));
142 0 : }
143 :
144 0 : void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
145 : const rtl::OUString& rCommandURL,
146 : const rtl::OUString& rModule )
147 : {
148 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule" );
149 : // SAFE
150 0 : ResetableGuard aLock( m_aLock );
151 :
152 0 : rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
153 0 : m_aMenuControllerMap.erase( aHashKey );
154 0 : }
155 :
156 : // container.XContainerListener
157 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent ) throw(RuntimeException)
158 : {
159 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementInserted" );
160 0 : rtl::OUString aCommand;
161 0 : rtl::OUString aModule;
162 0 : rtl::OUString aService;
163 0 : rtl::OUString aValue;
164 :
165 : // SAFE
166 0 : ResetableGuard aLock( m_aLock );
167 :
168 0 : if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
169 : {
170 : // Create hash key from command and module as they are together a primary key to
171 : // the UNO service that implements the popup menu controller.
172 0 : rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
173 0 : ControllerInfo& rControllerInfo = m_aMenuControllerMap[ aHashKey ];
174 0 : rControllerInfo.m_aImplementationName = aService;
175 0 : rControllerInfo.m_aValue = aValue;
176 0 : }
177 0 : }
178 :
179 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent ) throw(RuntimeException)
180 : {
181 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementRemoved" );
182 0 : rtl::OUString aCommand;
183 0 : rtl::OUString aModule;
184 0 : rtl::OUString aService;
185 0 : rtl::OUString aValue;
186 :
187 : // SAFE
188 0 : ResetableGuard aLock( m_aLock );
189 :
190 0 : if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
191 : {
192 : // Create hash key from command and module as they are together a primary key to
193 : // the UNO service that implements the popup menu controller.
194 0 : rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
195 0 : m_aMenuControllerMap.erase( aHashKey );
196 0 : }
197 0 : }
198 :
199 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent ) throw(RuntimeException)
200 : {
201 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementReplaced" );
202 0 : elementInserted(aEvent);
203 0 : }
204 :
205 : // lang.XEventListener
206 0 : void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& ) throw(RuntimeException)
207 : {
208 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::disposing" );
209 : // SAFE
210 : // remove our reference to the config access
211 0 : ResetableGuard aLock( m_aLock );
212 0 : m_xConfigAccess.clear();
213 0 : }
214 :
215 8 : void ConfigurationAccess_ControllerFactory::readConfigurationData()
216 : {
217 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::readConfigurationData" );
218 : // SAFE
219 8 : ResetableGuard aLock( m_aLock );
220 :
221 8 : if ( !m_bConfigAccessInitialized )
222 : {
223 8 : Sequence< Any > aArgs( 1 );
224 8 : PropertyValue aPropValue;
225 :
226 8 : aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
227 8 : aPropValue.Value <<= m_sRoot;
228 8 : aArgs[0] <<= aPropValue;
229 :
230 : try
231 : {
232 8 : m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
233 : }
234 0 : catch ( const WrappedTargetException& )
235 : {
236 : }
237 :
238 8 : m_bConfigAccessInitialized = sal_True;
239 : }
240 :
241 8 : if ( m_xConfigAccess.is() )
242 : {
243 : // Read and update configuration data
244 8 : updateConfigurationData();
245 :
246 8 : uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
247 : // UNSAFE
248 8 : aLock.unlock();
249 :
250 8 : if ( xContainer.is() )
251 : {
252 8 : m_xConfigAccessListener = new WeakContainerListener(this);
253 8 : xContainer->addContainerListener(m_xConfigAccessListener);
254 8 : }
255 8 : }
256 8 : }
257 :
258 8 : void ConfigurationAccess_ControllerFactory::updateConfigurationData()
259 : {
260 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::updateConfigurationData" );
261 : // SAFE
262 8 : ResetableGuard aLock( m_aLock );
263 8 : if ( m_xConfigAccess.is() )
264 : {
265 8 : Sequence< rtl::OUString > aPopupMenuControllers = m_xConfigAccess->getElementNames();
266 :
267 8 : rtl::OUString aCommand;
268 8 : rtl::OUString aModule;
269 8 : rtl::OUString aService;
270 8 : rtl::OUString aHashKey;
271 8 : rtl::OUString aValue;
272 :
273 8 : m_aMenuControllerMap.clear();
274 176 : for ( sal_Int32 i = 0; i < aPopupMenuControllers.getLength(); i++ )
275 : {
276 : try
277 : {
278 168 : if ( impl_getElementProps( m_xConfigAccess->getByName( aPopupMenuControllers[i] ), aCommand, aModule, aService,aValue ))
279 : {
280 : // Create hash key from command and module as they are together a primary key to
281 : // the UNO service that implements the popup menu controller.
282 168 : aHashKey = getHashKeyFromStrings( aCommand, aModule );
283 168 : m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey, ControllerInfo(aService,aValue) ));
284 : }
285 : }
286 0 : catch ( const NoSuchElementException& )
287 : {
288 : }
289 0 : catch ( const WrappedTargetException& )
290 : {
291 : }
292 8 : }
293 8 : }
294 8 : }
295 :
296 168 : sal_Bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, rtl::OUString& aCommand, rtl::OUString& aModule, rtl::OUString& aServiceSpecifier,rtl::OUString& aValue ) const
297 : {
298 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::impl_getElementProps" );
299 168 : Reference< XPropertySet > xPropertySet;
300 168 : aElement >>= xPropertySet;
301 :
302 168 : if ( xPropertySet.is() )
303 : {
304 : try
305 : {
306 168 : xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
307 168 : xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
308 168 : xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
309 168 : if ( m_bAskValue )
310 0 : xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
311 : }
312 0 : catch ( const com::sun::star::beans::UnknownPropertyException& )
313 : {
314 0 : return sal_False;
315 : }
316 0 : catch ( const com::sun::star::lang::WrappedTargetException& )
317 : {
318 0 : return sal_False;
319 : }
320 : }
321 :
322 168 : return sal_True;
323 : }
324 : } // namespace framework
325 :
326 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|