Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uifactory / factoryconfiguration.cxx
blob047a6b6edb682215e8dfde17d74af2f81eed6ad1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <uifactory/factoryconfiguration.hxx>
25 #include <services.h>
27 #include <helper/mischelper.hxx>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/configuration/theDefaultProvider.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/container/XContainer.hpp>
34 #include <comphelper/propertysequence.hxx>
35 #include <utility>
37 // Defines
39 using namespace com::sun::star;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::beans;
43 using namespace com::sun::star::container;
45 // Namespace
47 namespace framework
49 static OUString getHashKeyFromStrings(
50 std::u16string_view aCommandURL, std::u16string_view aModuleName )
52 return OUString::Concat(aCommandURL) + "-" + aModuleName;
55 // XInterface, XTypeProvider
57 ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( const Reference< XComponentContext >& rxContext, OUString _sRoot ) :
58 m_aPropCommand( "Command" ),
59 m_aPropModule( "Module" ),
60 m_aPropController( "Controller" ),
61 m_aPropValue( "Value" ),
62 m_sRoot(std::move(_sRoot)),
63 m_bConfigAccessInitialized( false )
65 m_xConfigProvider = configuration::theDefaultProvider::get( rxContext );
68 ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
70 std::unique_lock g(m_mutex);
72 Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
73 if ( xContainer.is() )
74 xContainer->removeContainerListener(m_xConfigAccessListener);
77 OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( std::u16string_view rCommandURL, std::u16string_view rModule ) const
79 std::unique_lock g(m_mutex);
80 MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
82 if ( pIter != m_aMenuControllerMap.end() )
83 return pIter->second.m_aImplementationName;
84 else if ( !rModule.empty() )
86 // Try to detect if we have a generic popup menu controller
87 pIter = m_aMenuControllerMap.find(
88 getHashKeyFromStrings( rCommandURL, std::u16string_view() ));
90 if ( pIter != m_aMenuControllerMap.end() )
91 return pIter->second.m_aImplementationName;
94 return OUString();
96 OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( std::u16string_view rCommandURL, std::u16string_view rModule ) const
98 std::unique_lock g(m_mutex);
100 MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
102 if ( pIter != m_aMenuControllerMap.end() )
103 return pIter->second.m_aValue;
104 else if ( !rModule.empty() )
106 // Try to detect if we have a generic popup menu controller
107 pIter = m_aMenuControllerMap.find(
108 getHashKeyFromStrings( rCommandURL, std::u16string_view() ));
110 if ( pIter != m_aMenuControllerMap.end() )
111 return pIter->second.m_aValue;
114 return OUString();
117 void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
118 std::u16string_view rCommandURL,
119 std::u16string_view rModule,
120 const OUString& rServiceSpecifier )
122 std::unique_lock g(m_mutex);
124 OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
125 m_aMenuControllerMap.emplace( aHashKey,ControllerInfo(rServiceSpecifier,OUString()) );
128 void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
129 std::u16string_view rCommandURL,
130 std::u16string_view rModule )
132 std::unique_lock g(m_mutex);
134 OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
135 m_aMenuControllerMap.erase( aHashKey );
138 // container.XContainerListener
139 void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent )
141 OUString aCommand;
142 OUString aModule;
143 OUString aService;
144 OUString aValue;
146 std::unique_lock g(m_mutex);
148 if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
150 // Create hash key from command and module as they are together a primary key to
151 // the UNO service that implements the popup menu controller.
152 OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
153 ControllerInfo& rControllerInfo = m_aMenuControllerMap[ aHashKey ];
154 rControllerInfo.m_aImplementationName = aService;
155 rControllerInfo.m_aValue = aValue;
159 void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent )
161 OUString aCommand;
162 OUString aModule;
163 OUString aService;
164 OUString aValue;
166 std::unique_lock g(m_mutex);
168 if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
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 OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
173 m_aMenuControllerMap.erase( aHashKey );
177 void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent )
179 elementInserted(aEvent);
182 // lang.XEventListener
183 void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& )
185 // remove our reference to the config access
186 std::unique_lock g(m_mutex);
187 m_xConfigAccess.clear();
190 void ConfigurationAccess_ControllerFactory::readConfigurationData()
192 // SAFE
193 std::unique_lock aLock( m_mutex );
195 if ( !m_bConfigAccessInitialized )
197 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
199 {"nodepath", uno::Any(m_sRoot)}
200 }));
203 m_xConfigAccess.set( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
205 catch ( const WrappedTargetException& )
209 m_bConfigAccessInitialized = true;
212 if ( !m_xConfigAccess.is() )
213 return;
215 // Read and update configuration data
216 updateConfigurationDataImpl();
218 uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
219 // UNSAFE
220 aLock.unlock();
222 if ( xContainer.is() )
224 m_xConfigAccessListener = new WeakContainerListener(this);
225 xContainer->addContainerListener(m_xConfigAccessListener);
229 void ConfigurationAccess_ControllerFactory::updateConfigurationDataImpl()
231 const Sequence< OUString > aPopupMenuControllers = m_xConfigAccess->getElementNames();
233 OUString aCommand;
234 OUString aModule;
235 OUString aService;
236 OUString aHashKey;
237 OUString aValue;
239 m_aMenuControllerMap.clear();
240 for ( OUString const & name : aPopupMenuControllers )
244 if ( impl_getElementProps( m_xConfigAccess->getByName( name ), aCommand, aModule, aService,aValue ))
246 // Create hash key from command and module as they are together a primary key to
247 // the UNO service that implements the popup menu controller.
248 aHashKey = getHashKeyFromStrings( aCommand, aModule );
249 m_aMenuControllerMap.emplace( aHashKey, ControllerInfo(aService,aValue) );
252 catch ( const NoSuchElementException& )
255 catch ( const WrappedTargetException& )
261 bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, OUString& aCommand, OUString& aModule, OUString& aServiceSpecifier,OUString& aValue ) const
263 Reference< XPropertySet > xPropertySet;
264 aElement >>= xPropertySet;
266 if ( !xPropertySet.is() )
267 return true;
271 xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
272 xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
273 xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
274 xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
276 catch ( const css::beans::UnknownPropertyException& )
278 return false;
280 catch ( const css::lang::WrappedTargetException& )
282 return false;
285 return true;
287 } // namespace framework
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */