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 0 : OUString getHashKeyFromStrings( const OUString& aCommandURL, const OUString& aModuleName )
48 : {
49 0 : OUStringBuffer aKey( aCommandURL );
50 0 : aKey.appendAscii( "-" );
51 0 : aKey.append( aModuleName );
52 0 : return aKey.makeStringAndClear();
53 : }
54 :
55 : // XInterface, XTypeProvider
56 :
57 0 : 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 0 : m_bAskValue(_bAskValue)
65 : {
66 0 : m_xConfigProvider = configuration::theDefaultProvider::get( rxContext );
67 0 : }
68 :
69 0 : ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
70 : {
71 0 : osl::MutexGuard g(m_mutex);
72 :
73 0 : Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
74 0 : if ( xContainer.is() )
75 0 : xContainer->removeContainerListener(m_xConfigAccessListener);
76 0 : }
77 :
78 0 : OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const OUString& rCommandURL, const OUString& rModule ) const
79 : {
80 0 : osl::MutexGuard g(m_mutex);
81 0 : MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
82 :
83 0 : if ( pIter != m_aMenuControllerMap.end() )
84 0 : return pIter->second.m_aImplementationName;
85 0 : else if ( !rModule.isEmpty() )
86 : {
87 : // Try to detect if we have a generic popup menu controller
88 0 : pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, OUString() ));
89 :
90 0 : if ( pIter != m_aMenuControllerMap.end() )
91 0 : return pIter->second.m_aImplementationName;
92 : }
93 :
94 0 : 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 0 : void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
117 : const OUString& rCommandURL,
118 : const OUString& rModule,
119 : const OUString& rServiceSpecifier )
120 : {
121 0 : osl::MutexGuard g(m_mutex);
122 :
123 0 : OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
124 0 : m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey,ControllerInfo(rServiceSpecifier,OUString()) ));
125 0 : }
126 :
127 0 : void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
128 : const OUString& rCommandURL,
129 : const OUString& rModule )
130 : {
131 0 : osl::MutexGuard g(m_mutex);
132 :
133 0 : OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
134 0 : m_aMenuControllerMap.erase( aHashKey );
135 0 : }
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 0 : void ConfigurationAccess_ControllerFactory::readConfigurationData()
190 : {
191 : // SAFE
192 0 : osl::ClearableMutexGuard aLock( m_mutex );
193 :
194 0 : if ( !m_bConfigAccessInitialized )
195 : {
196 0 : Sequence< Any > aArgs( 1 );
197 0 : PropertyValue aPropValue;
198 :
199 0 : aPropValue.Name = "nodepath";
200 0 : aPropValue.Value <<= m_sRoot;
201 0 : aArgs[0] <<= aPropValue;
202 :
203 : try
204 : {
205 0 : m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
206 : }
207 0 : catch ( const WrappedTargetException& )
208 : {
209 : }
210 :
211 0 : m_bConfigAccessInitialized = true;
212 : }
213 :
214 0 : if ( m_xConfigAccess.is() )
215 : {
216 : // Read and update configuration data
217 0 : updateConfigurationData();
218 :
219 0 : uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
220 : // UNSAFE
221 0 : aLock.clear();
222 :
223 0 : if ( xContainer.is() )
224 : {
225 0 : m_xConfigAccessListener = new WeakContainerListener(this);
226 0 : xContainer->addContainerListener(m_xConfigAccessListener);
227 0 : }
228 0 : }
229 0 : }
230 :
231 0 : void ConfigurationAccess_ControllerFactory::updateConfigurationData()
232 : {
233 0 : osl::MutexGuard g(m_mutex);
234 0 : if ( m_xConfigAccess.is() )
235 : {
236 0 : Sequence< OUString > aPopupMenuControllers = m_xConfigAccess->getElementNames();
237 :
238 0 : OUString aCommand;
239 0 : OUString aModule;
240 0 : OUString aService;
241 0 : OUString aHashKey;
242 0 : OUString aValue;
243 :
244 0 : m_aMenuControllerMap.clear();
245 0 : for ( sal_Int32 i = 0; i < aPopupMenuControllers.getLength(); i++ )
246 : {
247 : try
248 : {
249 0 : 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 0 : aHashKey = getHashKeyFromStrings( aCommand, aModule );
254 0 : 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 0 : }
264 0 : }
265 0 : }
266 :
267 0 : bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, OUString& aCommand, OUString& aModule, OUString& aServiceSpecifier,OUString& aValue ) const
268 : {
269 0 : Reference< XPropertySet > xPropertySet;
270 0 : aElement >>= xPropertySet;
271 :
272 0 : if ( xPropertySet.is() )
273 : {
274 : try
275 : {
276 0 : xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
277 0 : xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
278 0 : xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
279 0 : 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 0 : return true;
293 : }
294 : } // namespace framework
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|