tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / framework / source / uifactory / uicontrollerfactory.cxx
blobe935ae7a9daf7694514f7e7ce3c3bddb2c7cbc7f
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>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/frame/XUIControllerFactory.hpp>
30 #include <rtl/ref.hxx>
31 #include <comphelper/compbase.hxx>
32 #include <cppuhelper/supportsservice.hxx>
34 using namespace css::uno;
35 using namespace css::lang;
36 using namespace css::beans;
37 using namespace css::frame;
38 using namespace framework;
40 namespace {
42 typedef comphelper::WeakComponentImplHelper<
43 css::lang::XServiceInfo,
44 css::frame::XUIControllerFactory > UIControllerFactory_BASE;
46 class UIControllerFactory : public UIControllerFactory_BASE
48 public:
49 virtual ~UIControllerFactory() override;
51 // XMultiComponentFactory
52 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( const OUString& aServiceSpecifier, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
53 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const OUString& ServiceSpecifier, const css::uno::Sequence< css::uno::Any >& Arguments, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
54 virtual css::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames() override;
56 // XUIControllerRegistration
57 virtual sal_Bool SAL_CALL hasController( const OUString& aCommandURL, const OUString& aModuleName ) override;
58 virtual void SAL_CALL registerController( const OUString& aCommandURL, const OUString& aModuleName, const OUString& aControllerImplementationName ) override;
59 virtual void SAL_CALL deregisterController( const OUString& aCommandURL, const OUString& aModuleName ) override;
61 protected:
62 UIControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext, std::u16string_view rUINode );
63 bool m_bConfigRead;
64 css::uno::Reference< css::uno::XComponentContext > m_xContext;
65 rtl::Reference<ConfigurationAccess_ControllerFactory> m_pConfigAccess;
67 private:
68 virtual void disposing(std::unique_lock<std::mutex>&) final override;
71 UIControllerFactory::UIControllerFactory(
72 const Reference< XComponentContext >& xContext,
73 std::u16string_view rConfigurationNode )
74 : m_bConfigRead( false )
75 , m_xContext( xContext )
77 m_pConfigAccess = new ConfigurationAccess_ControllerFactory(m_xContext,
78 OUString::Concat("/org.openoffice.Office.UI.Controller/Registered/")
79 + rConfigurationNode);
82 UIControllerFactory::~UIControllerFactory()
84 std::unique_lock g(m_aMutex);
85 disposing(g);
88 void UIControllerFactory::disposing(std::unique_lock<std::mutex>&)
90 m_pConfigAccess.clear();
93 // XMultiComponentFactory
94 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
95 const OUString& aServiceSpecifier,
96 const Reference< XComponentContext >& )
98 // SAFE
99 std::unique_lock g(m_aMutex);
101 if ( !m_bConfigRead )
103 m_bConfigRead = true;
104 m_pConfigAccess->readConfigurationData();
107 OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, std::u16string_view() );
108 if ( !aServiceName.isEmpty() )
109 return m_xContext->getServiceManager()->createInstanceWithContext( aServiceName, m_xContext );
110 else
111 return Reference< XInterface >();
112 // SAFE
115 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithArgumentsAndContext(
116 const OUString& ServiceSpecifier,
117 const Sequence< Any >& Arguments,
118 const Reference< XComponentContext >& )
120 static constexpr OUStringLiteral aPropModuleName( u"ModuleIdentifier" );
122 OUString aPropName;
123 PropertyValue aPropValue;
125 // Retrieve the optional module name from the Arguments sequence. It is used as a part of
126 // the hash map key to support different controller implementation for the same URL but different
127 // module!!
128 for ( Any const & arg : Arguments )
130 if (( arg >>= aPropValue ) && ( aPropValue.Name == aPropModuleName ))
132 aPropValue.Value >>= aPropName;
133 break;
137 Sequence< Any > aNewArgs( Arguments );
139 sal_Int32 nAppendIndex = aNewArgs.getLength();
140 aNewArgs.realloc( aNewArgs.getLength() + 2 );
141 auto pNewArgs = aNewArgs.getArray();
143 // Append the command URL to the Arguments sequence so that one controller can be
144 // used for more than one command URL.
145 aPropValue.Name = "CommandURL";
146 aPropValue.Value <<= ServiceSpecifier;
147 pNewArgs[nAppendIndex] <<= aPropValue;
149 // Append the optional value argument. It's an empty string if no additional info
150 // is provided to the controller.
151 OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
152 aPropValue.Name = "Value";
153 aPropValue.Value <<= aValue;
154 pNewArgs[nAppendIndex+1] <<= aPropValue;
157 OUString aServiceName;
158 { // SAFE
159 std::unique_lock g(m_aMutex);
161 if ( !m_bConfigRead )
163 m_bConfigRead = true;
164 m_pConfigAccess->readConfigurationData();
167 aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
168 } // SAFE
170 if ( !aServiceName.isEmpty() )
171 return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, m_xContext );
172 else
173 return Reference< XInterface >();
177 Sequence< OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
179 return Sequence< OUString >();
182 // XUIControllerRegistration
183 sal_Bool SAL_CALL UIControllerFactory::hasController(
184 const OUString& aCommandURL,
185 const OUString& aModuleName )
187 std::unique_lock g(m_aMutex);
189 if ( !m_bConfigRead )
191 m_bConfigRead = true;
192 m_pConfigAccess->readConfigurationData();
195 return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
198 void SAL_CALL UIControllerFactory::registerController(
199 const OUString& aCommandURL,
200 const OUString& aModuleName,
201 const OUString& aControllerImplementationName )
203 // SAFE
204 std::unique_lock g(m_aMutex);
206 if ( !m_bConfigRead )
208 m_bConfigRead = true;
209 m_pConfigAccess->readConfigurationData();
212 m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
213 // SAFE
216 void SAL_CALL UIControllerFactory::deregisterController(
217 const OUString& aCommandURL,
218 const OUString& aModuleName )
220 // SAFE
221 std::unique_lock g(m_aMutex);
223 if ( !m_bConfigRead )
225 m_bConfigRead = true;
226 m_pConfigAccess->readConfigurationData();
229 m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
230 // SAFE
233 class PopupMenuControllerFactory : public UIControllerFactory
235 public:
236 explicit PopupMenuControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
238 virtual OUString SAL_CALL getImplementationName() override
240 return u"com.sun.star.comp.framework.PopupMenuControllerFactory"_ustr;
243 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
245 return cppu::supportsService(this, ServiceName);
248 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
250 return {u"com.sun.star.frame.PopupMenuControllerFactory"_ustr};
255 PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
256 UIControllerFactory( xContext, u"PopupMenu" )
260 class ToolbarControllerFactory : public UIControllerFactory
262 public:
263 explicit ToolbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
265 virtual OUString SAL_CALL getImplementationName() override
267 return u"com.sun.star.comp.framework.ToolBarControllerFactory"_ustr;
270 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
272 return cppu::supportsService(this, ServiceName);
275 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
277 return {u"com.sun.star.frame.ToolbarControllerFactory"_ustr};
282 ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
283 UIControllerFactory( xContext, u"ToolBar" )
287 class StatusbarControllerFactory : public UIControllerFactory
289 public:
290 explicit StatusbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
292 virtual OUString SAL_CALL getImplementationName() override
294 return u"com.sun.star.comp.framework.StatusBarControllerFactory"_ustr;
297 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
299 return cppu::supportsService(this, ServiceName);
302 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
304 return {u"com.sun.star.frame.StatusbarControllerFactory"_ustr};
309 StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
310 UIControllerFactory( xContext, u"StatusBar" )
316 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
317 com_sun_star_comp_framework_PopupMenuControllerFactory_get_implementation(
318 css::uno::XComponentContext *context,
319 css::uno::Sequence<css::uno::Any> const &)
321 return cppu::acquire(new PopupMenuControllerFactory(context));
324 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
325 com_sun_star_comp_framework_ToolBarControllerFactory_get_implementation(
326 css::uno::XComponentContext *context,
327 css::uno::Sequence<css::uno::Any> const &)
329 return cppu::acquire(new ToolbarControllerFactory(context));
332 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
333 com_sun_star_comp_framework_StatusBarControllerFactory_get_implementation(
334 css::uno::XComponentContext *context,
335 css::uno::Sequence<css::uno::Any> const &)
337 return cppu::acquire(new StatusbarControllerFactory(context));
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */