LibreOffice Module framework (master) 1
factoryconfiguration.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
25#include <services.h>
26
27#include <helper/mischelper.hxx>
28
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>
33
35#include <utility>
36
37// Defines
38
39using namespace com::sun::star;
40using namespace com::sun::star::uno;
41using namespace com::sun::star::lang;
42using namespace com::sun::star::beans;
43using namespace com::sun::star::container;
44
45// Namespace
46
47namespace framework
48{
49static OUString getHashKeyFromStrings(
50 std::u16string_view aCommandURL, std::u16string_view aModuleName )
51{
52 return OUString::Concat(aCommandURL) + "-" + aModuleName;
53}
54
55// XInterface, XTypeProvider
56
58 m_aPropCommand( "Command" ),
59 m_aPropModule( "Module" ),
60 m_aPropController( "Controller" ),
61 m_aPropValue( "Value" ),
62 m_sRoot(std::move(_sRoot)),
64{
65 m_xConfigProvider = configuration::theDefaultProvider::get( rxContext );
66}
67
69{
70 std::unique_lock g(m_mutex);
71
72 Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
73 if ( xContainer.is() )
74 xContainer->removeContainerListener(m_xConfigAccessListener);
75}
76
77OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( std::u16string_view rCommandURL, std::u16string_view rModule ) const
78{
79 std::unique_lock g(m_mutex);
80 MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
81
82 if ( pIter != m_aMenuControllerMap.end() )
83 return pIter->second.m_aImplementationName;
84 else if ( !rModule.empty() )
85 {
86 // Try to detect if we have a generic popup menu controller
87 pIter = m_aMenuControllerMap.find(
88 getHashKeyFromStrings( rCommandURL, std::u16string_view() ));
89
90 if ( pIter != m_aMenuControllerMap.end() )
91 return pIter->second.m_aImplementationName;
92 }
93
94 return OUString();
95}
96OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( std::u16string_view rCommandURL, std::u16string_view rModule ) const
97{
98 std::unique_lock g(m_mutex);
99
100 MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
101
102 if ( pIter != m_aMenuControllerMap.end() )
103 return pIter->second.m_aValue;
104 else if ( !rModule.empty() )
105 {
106 // Try to detect if we have a generic popup menu controller
107 pIter = m_aMenuControllerMap.find(
108 getHashKeyFromStrings( rCommandURL, std::u16string_view() ));
109
110 if ( pIter != m_aMenuControllerMap.end() )
111 return pIter->second.m_aValue;
112 }
113
114 return OUString();
115}
116
118 std::u16string_view rCommandURL,
119 std::u16string_view rModule,
120 const OUString& rServiceSpecifier )
121{
122 std::unique_lock g(m_mutex);
123
124 OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
125 m_aMenuControllerMap.emplace( aHashKey,ControllerInfo(rServiceSpecifier,OUString()) );
126}
127
129 std::u16string_view rCommandURL,
130 std::u16string_view rModule )
131{
132 std::unique_lock g(m_mutex);
133
134 OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
135 m_aMenuControllerMap.erase( aHashKey );
136}
137
138// container.XContainerListener
139void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent )
140{
141 OUString aCommand;
142 OUString aModule;
143 OUString aService;
144 OUString aValue;
145
146 std::unique_lock g(m_mutex);
147
148 if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
149 {
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;
156 }
157}
158
159void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent )
160{
161 OUString aCommand;
162 OUString aModule;
163 OUString aService;
164 OUString aValue;
165
166 std::unique_lock g(m_mutex);
167
168 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 OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
173 m_aMenuControllerMap.erase( aHashKey );
174 }
175}
176
177void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent )
178{
180}
181
182// lang.XEventListener
183void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& )
184{
185 // remove our reference to the config access
186 std::unique_lock g(m_mutex);
187 m_xConfigAccess.clear();
188}
189
191{
192 // SAFE
193 std::unique_lock aLock( m_mutex );
194
196 {
197 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
198 {
199 {"nodepath", uno::Any(m_sRoot)}
200 }));
201 try
202 {
203 m_xConfigAccess.set( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
204 }
205 catch ( const WrappedTargetException& )
206 {
207 }
208
210 }
211
212 if ( !m_xConfigAccess.is() )
213 return;
214
215 // Read and update configuration data
217
218 uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
219 // UNSAFE
220 aLock.unlock();
221
222 if ( xContainer.is() )
223 {
225 xContainer->addContainerListener(m_xConfigAccessListener);
226 }
227}
228
230{
231 const Sequence< OUString > aPopupMenuControllers = m_xConfigAccess->getElementNames();
232
233 OUString aCommand;
234 OUString aModule;
235 OUString aService;
236 OUString aHashKey;
237 OUString aValue;
238
239 m_aMenuControllerMap.clear();
240 for ( OUString const & name : aPopupMenuControllers )
241 {
242 try
243 {
244 if ( impl_getElementProps( m_xConfigAccess->getByName( name ), aCommand, aModule, aService,aValue ))
245 {
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) );
250 }
251 }
252 catch ( const NoSuchElementException& )
253 {
254 }
255 catch ( const WrappedTargetException& )
256 {
257 }
258 }
259}
260
261bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, OUString& aCommand, OUString& aModule, OUString& aServiceSpecifier,OUString& aValue ) const
262{
263 Reference< XPropertySet > xPropertySet;
264 aElement >>= xPropertySet;
265
266 if ( !xPropertySet.is() )
267 return true;
268
269 try
270 {
271 xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
272 xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
273 xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
274 xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
275 }
276 catch ( const css::beans::UnknownPropertyException& )
277 {
278 return false;
279 }
280 catch ( const css::lang::WrappedTargetException& )
281 {
282 return false;
283 }
284
285 return true;
286}
287} // namespace framework
288
289/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
virtual void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
ConfigurationAccess_ControllerFactory(const css::uno::Reference< css::uno::XComponentContext > &rxContext, OUString _sRoot)
css::uno::Reference< css::lang::XMultiServiceFactory > m_xConfigProvider
void removeServiceFromCommandModule(std::u16string_view rCommandURL, std::u16string_view rModule)
css::uno::Reference< css::container::XNameAccess > m_xConfigAccess
OUString getServiceFromCommandModule(std::u16string_view rCommandURL, std::u16string_view rModule) const
void addServiceToCommandModule(std::u16string_view rCommandURL, std::u16string_view rModule, const OUString &rServiceSpecifier)
css::uno::Reference< css::container::XContainerListener > m_xConfigAccessListener
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
OUString getValueFromCommandModule(std::u16string_view rCommandURL, std::u16string_view rModule) const
virtual void SAL_CALL elementReplaced(const css::container::ContainerEvent &Event) override
virtual void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
bool impl_getElementProps(const css::uno::Any &aElement, OUString &aCommand, OUString &aModule, OUString &aServiceSpecifier, OUString &aValue) const
const char * name
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
constexpr OUStringLiteral SERVICENAME_CFGREADACCESS
Definition: services.h:30
static OUString getHashKeyFromStrings(std::u16string_view aCommandURL, std::u16string_view aModuleName)
OUString aCommand
bool m_bConfigAccessInitialized