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