LibreOffice Module framework (master) 1
uicontrollerfactory.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
26#include <com/sun/star/beans/PropertyValue.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/frame/XUIControllerFactory.hpp>
29
30#include <rtl/ref.hxx>
33
34using namespace css::uno;
35using namespace css::lang;
36using namespace css::beans;
37using namespace css::container;
38using namespace css::frame;
39using namespace framework;
40
41namespace {
42
44 css::lang::XServiceInfo,
45 css::frame::XUIControllerFactory > UIControllerFactory_BASE;
46
47class UIControllerFactory : public UIControllerFactory_BASE
48{
49public:
50 virtual ~UIControllerFactory() override;
51
52 // XMultiComponentFactory
53 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext( const OUString& aServiceSpecifier, const css::uno::Reference< css::uno::XComponentContext >& Context ) override;
54 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;
55 virtual css::uno::Sequence< OUString > SAL_CALL getAvailableServiceNames() override;
56
57 // XUIControllerRegistration
58 virtual sal_Bool SAL_CALL hasController( const OUString& aCommandURL, const OUString& aModuleName ) override;
59 virtual void SAL_CALL registerController( const OUString& aCommandURL, const OUString& aModuleName, const OUString& aControllerImplementationName ) override;
60 virtual void SAL_CALL deregisterController( const OUString& aCommandURL, const OUString& aModuleName ) override;
61
62protected:
63 UIControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext, std::u16string_view rUINode );
64 bool m_bConfigRead;
65 css::uno::Reference< css::uno::XComponentContext > m_xContext;
67
68private:
69 virtual void disposing(std::unique_lock<std::mutex>&) final override;
70};
71
72UIControllerFactory::UIControllerFactory(
73 const Reference< XComponentContext >& xContext,
74 std::u16string_view rConfigurationNode )
75 : m_bConfigRead( false )
76 , m_xContext( xContext )
77{
79 OUString::Concat("/org.openoffice.Office.UI.Controller/Registered/")
80 + rConfigurationNode);
81}
82
83UIControllerFactory::~UIControllerFactory()
84{
85 std::unique_lock g(m_aMutex);
86 disposing(g);
87}
88
89void UIControllerFactory::disposing(std::unique_lock<std::mutex>&)
90{
91 m_pConfigAccess.clear();
92}
93
94// XMultiComponentFactory
95Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
96 const OUString& aServiceSpecifier,
97 const Reference< XComponentContext >& )
98{
99 // SAFE
100 std::unique_lock g(m_aMutex);
101
102 if ( !m_bConfigRead )
103 {
104 m_bConfigRead = true;
105 m_pConfigAccess->readConfigurationData();
106 }
107
108 OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, std::u16string_view() );
109 if ( !aServiceName.isEmpty() )
110 return m_xContext->getServiceManager()->createInstanceWithContext( aServiceName, m_xContext );
111 else
112 return Reference< XInterface >();
113 // SAFE
114}
115
116Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithArgumentsAndContext(
117 const OUString& ServiceSpecifier,
118 const Sequence< Any >& Arguments,
119 const Reference< XComponentContext >& )
120{
121 static constexpr OUStringLiteral aPropModuleName( u"ModuleIdentifier" );
122
123 OUString aPropName;
124 PropertyValue aPropValue;
125
126 // Retrieve the optional module name from the Arguments sequence. It is used as a part of
127 // the hash map key to support different controller implementation for the same URL but different
128 // module!!
129 for ( Any const & arg : Arguments )
130 {
131 if (( arg >>= aPropValue ) && ( aPropValue.Name == aPropModuleName ))
132 {
133 aPropValue.Value >>= aPropName;
134 break;
135 }
136 }
137
138 Sequence< Any > aNewArgs( Arguments );
139
140 sal_Int32 nAppendIndex = aNewArgs.getLength();
141 aNewArgs.realloc( aNewArgs.getLength() + 2 );
142 auto pNewArgs = aNewArgs.getArray();
143
144 // Append the command URL to the Arguments sequence so that one controller can be
145 // used for more than one command URL.
146 aPropValue.Name = "CommandURL";
147 aPropValue.Value <<= ServiceSpecifier;
148 pNewArgs[nAppendIndex] <<= aPropValue;
149
150 // Append the optional value argument. It's an empty string if no additional info
151 // is provided to the controller.
152 OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
153 aPropValue.Name = "Value";
154 aPropValue.Value <<= aValue;
155 pNewArgs[nAppendIndex+1] <<= aPropValue;
156
157 {
158 OUString aServiceName;
159 { // SAFE
160 std::unique_lock g(m_aMutex);
161
162 if ( !m_bConfigRead )
163 {
164 m_bConfigRead = true;
165 m_pConfigAccess->readConfigurationData();
166 }
167
168 aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
169 } // SAFE
170
171 if ( !aServiceName.isEmpty() )
172 return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, m_xContext );
173 else
174 return Reference< XInterface >();
175 }
176}
177
178Sequence< OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
179{
180 return Sequence< OUString >();
181}
182
183// XUIControllerRegistration
184sal_Bool SAL_CALL UIControllerFactory::hasController(
185 const OUString& aCommandURL,
186 const OUString& aModuleName )
187{
188 std::unique_lock g(m_aMutex);
189
190 if ( !m_bConfigRead )
191 {
192 m_bConfigRead = true;
193 m_pConfigAccess->readConfigurationData();
194 }
195
196 return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
197}
198
199void SAL_CALL UIControllerFactory::registerController(
200 const OUString& aCommandURL,
201 const OUString& aModuleName,
202 const OUString& aControllerImplementationName )
203{
204 // SAFE
205 std::unique_lock g(m_aMutex);
206
207 if ( !m_bConfigRead )
208 {
209 m_bConfigRead = true;
210 m_pConfigAccess->readConfigurationData();
211 }
212
213 m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
214 // SAFE
215}
216
217void SAL_CALL UIControllerFactory::deregisterController(
218 const OUString& aCommandURL,
219 const OUString& aModuleName )
220{
221 // SAFE
222 std::unique_lock g(m_aMutex);
223
224 if ( !m_bConfigRead )
225 {
226 m_bConfigRead = true;
227 m_pConfigAccess->readConfigurationData();
228 }
229
230 m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
231 // SAFE
232}
233
234class PopupMenuControllerFactory : public UIControllerFactory
235{
236public:
237 explicit PopupMenuControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
238
239 virtual OUString SAL_CALL getImplementationName() override
240 {
241 return "com.sun.star.comp.framework.PopupMenuControllerFactory";
242 }
243
244 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
245 {
246 return cppu::supportsService(this, ServiceName);
247 }
248
249 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
250 {
251 return {"com.sun.star.frame.PopupMenuControllerFactory"};
252 }
253
254};
255
256PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
257 UIControllerFactory( xContext, u"PopupMenu" )
258{
259}
260
261class ToolbarControllerFactory : public UIControllerFactory
262{
263public:
264 explicit ToolbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
265
266 virtual OUString SAL_CALL getImplementationName() override
267 {
268 return "com.sun.star.comp.framework.ToolBarControllerFactory";
269 }
270
271 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
272 {
273 return cppu::supportsService(this, ServiceName);
274 }
275
276 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
277 {
278 return {"com.sun.star.frame.ToolbarControllerFactory"};
279 }
280
281};
282
283ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
284 UIControllerFactory( xContext, u"ToolBar" )
285{
286}
287
288class StatusbarControllerFactory : public UIControllerFactory
289{
290public:
291 explicit StatusbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
292
293 virtual OUString SAL_CALL getImplementationName() override
294 {
295 return "com.sun.star.comp.framework.StatusBarControllerFactory";
296 }
297
298 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
299 {
300 return cppu::supportsService(this, ServiceName);
301 }
302
303 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
304 {
305 return {"com.sun.star.frame.StatusbarControllerFactory"};
306 }
307
308};
309
310StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
311 UIControllerFactory( xContext, u"StatusBar" )
312{
313}
314
315}
316
317extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
319 css::uno::XComponentContext *context,
320 css::uno::Sequence<css::uno::Any> const &)
321{
322 return cppu::acquire(new PopupMenuControllerFactory(context));
323}
324
325extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
327 css::uno::XComponentContext *context,
328 css::uno::Sequence<css::uno::Any> const &)
329{
330 return cppu::acquire(new ToolbarControllerFactory(context));
331}
332
333extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
335 css::uno::XComponentContext *context,
336 css::uno::Sequence<css::uno::Any> const &)
337{
338 return cppu::acquire(new StatusbarControllerFactory(context));
339}
340
341/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float u
bool m_bConfigRead
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OUString aPropName
unsigned char sal_Bool
std::mutex m_aMutex
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_StatusBarControllerFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_PopupMenuControllerFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_ToolBarControllerFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)