1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <uifactory/factoryconfiguration.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/container/XContainer.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/frame/XUIControllerFactory.hpp>
27 #include <rtl/ustrbuf.hxx>
28 #include <cppuhelper/basemutex.hxx>
29 #include <cppuhelper/compbase2.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 using namespace css::uno
;
33 using namespace css::lang
;
34 using namespace css::beans
;
35 using namespace css::container
;
36 using namespace css::frame
;
37 using namespace framework
;
41 typedef ::cppu::WeakComponentImplHelper2
<
42 css::lang::XServiceInfo
,
43 css::frame::XUIControllerFactory
> UIControllerFactory_BASE
;
45 class UIControllerFactory
: private cppu::BaseMutex
,
46 public UIControllerFactory_BASE
49 virtual ~UIControllerFactory();
51 // XMultiComponentFactory
52 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstanceWithContext( const OUString
& aServiceSpecifier
, const css::uno::Reference
< css::uno::XComponentContext
>& Context
) throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) SAL_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
) throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
54 virtual css::uno::Sequence
< OUString
> SAL_CALL
getAvailableServiceNames() throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
56 // XUIControllerRegistration
57 virtual sal_Bool SAL_CALL
hasController( const OUString
& aCommandURL
, const OUString
& aModuleName
) throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
58 virtual void SAL_CALL
registerController( const OUString
& aCommandURL
, const OUString
& aModuleName
, const OUString
& aControllerImplementationName
) throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
59 virtual void SAL_CALL
deregisterController( const OUString
& aCommandURL
, const OUString
& aModuleName
) throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
62 UIControllerFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
, const rtl::OUString
&rUINode
);
64 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
65 ConfigurationAccess_ControllerFactory
* m_pConfigAccess
;
68 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
71 UIControllerFactory::UIControllerFactory(
72 const Reference
< XComponentContext
>& xContext
,
73 const rtl::OUString
&rConfigurationNode
)
74 : UIControllerFactory_BASE(m_aMutex
)
75 , m_bConfigRead( false )
76 , m_xContext( xContext
)
79 m_pConfigAccess
= new ConfigurationAccess_ControllerFactory(m_xContext
,
80 "/org.openoffice.Office.UI.Controller/Registered/" + rConfigurationNode
);
81 m_pConfigAccess
->acquire();
84 UIControllerFactory::~UIControllerFactory()
89 void SAL_CALL
UIControllerFactory::disposing()
91 osl::MutexGuard
g(rBHelper
.rMutex
);
94 // reduce reference count
95 m_pConfigAccess
->release();
100 // XMultiComponentFactory
101 Reference
< XInterface
> SAL_CALL
UIControllerFactory::createInstanceWithContext(
102 const OUString
& aServiceSpecifier
,
103 const Reference
< XComponentContext
>& )
104 throw (Exception
, RuntimeException
, std::exception
)
107 osl::MutexGuard
g(rBHelper
.rMutex
);
109 if ( !m_bConfigRead
)
111 m_bConfigRead
= true;
112 m_pConfigAccess
->readConfigurationData();
115 OUString aServiceName
= m_pConfigAccess
->getServiceFromCommandModule( aServiceSpecifier
, OUString() );
116 if ( !aServiceName
.isEmpty() )
117 return m_xContext
->getServiceManager()->createInstanceWithContext( aServiceName
, m_xContext
);
119 return Reference
< XInterface
>();
123 Reference
< XInterface
> SAL_CALL
UIControllerFactory::createInstanceWithArgumentsAndContext(
124 const OUString
& ServiceSpecifier
,
125 const Sequence
< Any
>& Arguments
,
126 const Reference
< XComponentContext
>& )
127 throw (Exception
, RuntimeException
, std::exception
)
129 const OUString
aPropModuleName( "ModuleIdentifier" );
130 const OUString
aPropValueName( "Value" );
133 PropertyValue aPropValue
;
135 // Retrieve the optional module name form the Arguments sequence. It is used as a part of
136 // the hash map key to support different controller implementation for the same URL but different
138 for ( int i
= 0; i
< Arguments
.getLength(); i
++ )
140 if (( Arguments
[i
] >>= aPropValue
) && ( aPropValue
.Name
.equals( aPropModuleName
)))
142 aPropValue
.Value
>>= aPropName
;
147 Sequence
< Any
> aNewArgs( Arguments
);
149 sal_Int32 nAppendIndex
= aNewArgs
.getLength();
150 bool bHasValue
= m_pConfigAccess
->hasValue();
151 aNewArgs
.realloc( aNewArgs
.getLength() + (bHasValue
? 2 : 1) );
153 // Append the command URL to the Arguments sequence so that one controller can be
154 // used for more than one command URL.
155 aPropValue
.Name
= "CommandURL";
156 aPropValue
.Value
<<= ServiceSpecifier
;
157 aNewArgs
[nAppendIndex
] <<= aPropValue
;
161 // Append the optional value argument. It's an empty string if no additional info
162 // is provided to the controller.
163 OUString aValue
= m_pConfigAccess
->getValueFromCommandModule( ServiceSpecifier
, aPropName
);
164 aPropValue
.Name
= aPropValueName
;
165 aPropValue
.Value
<<= aValue
;
166 aNewArgs
[nAppendIndex
+1] <<= aPropValue
;
170 OUString aServiceName
;
172 osl::MutexGuard
g(rBHelper
.rMutex
);
174 if ( !m_bConfigRead
)
176 m_bConfigRead
= true;
177 m_pConfigAccess
->readConfigurationData();
180 aServiceName
= m_pConfigAccess
->getServiceFromCommandModule( ServiceSpecifier
, aPropName
);
183 if ( !aServiceName
.isEmpty() )
184 return m_xContext
->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName
, aNewArgs
, m_xContext
);
186 return Reference
< XInterface
>();
190 Sequence
< OUString
> SAL_CALL
UIControllerFactory::getAvailableServiceNames()
191 throw (RuntimeException
, std::exception
)
193 return Sequence
< OUString
>();
196 // XUIControllerRegistration
197 sal_Bool SAL_CALL
UIControllerFactory::hasController(
198 const OUString
& aCommandURL
,
199 const OUString
& aModuleName
)
200 throw (css::uno::RuntimeException
, std::exception
)
202 osl::MutexGuard
g(rBHelper
.rMutex
);
204 if ( !m_bConfigRead
)
206 m_bConfigRead
= true;
207 m_pConfigAccess
->readConfigurationData();
210 return ( !m_pConfigAccess
->getServiceFromCommandModule( aCommandURL
, aModuleName
).isEmpty() );
213 void SAL_CALL
UIControllerFactory::registerController(
214 const OUString
& aCommandURL
,
215 const OUString
& aModuleName
,
216 const OUString
& aControllerImplementationName
)
217 throw (RuntimeException
, std::exception
)
220 osl::MutexGuard
g(rBHelper
.rMutex
);
222 if ( !m_bConfigRead
)
224 m_bConfigRead
= true;
225 m_pConfigAccess
->readConfigurationData();
228 m_pConfigAccess
->addServiceToCommandModule( aCommandURL
, aModuleName
, aControllerImplementationName
);
232 void SAL_CALL
UIControllerFactory::deregisterController(
233 const OUString
& aCommandURL
,
234 const OUString
& aModuleName
)
235 throw (RuntimeException
, std::exception
)
238 osl::MutexGuard
g(rBHelper
.rMutex
);
240 if ( !m_bConfigRead
)
242 m_bConfigRead
= true;
243 m_pConfigAccess
->readConfigurationData();
246 m_pConfigAccess
->removeServiceFromCommandModule( aCommandURL
, aModuleName
);
250 class PopupMenuControllerFactory
: public UIControllerFactory
253 PopupMenuControllerFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
255 virtual OUString SAL_CALL
getImplementationName()
256 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
258 return OUString("com.sun.star.comp.framework.PopupMenuControllerFactory");
261 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
262 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
264 return cppu::supportsService(this, ServiceName
);
267 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
268 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
270 css::uno::Sequence
< OUString
> aSeq(1);
271 aSeq
[0] = "com.sun.star.frame.PopupMenuControllerFactory";
277 PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference
< XComponentContext
>& xContext
) :
280 OUString( "PopupMenu") )
284 struct PopupMenuControllerFactoryInstance
{
285 explicit PopupMenuControllerFactoryInstance(
286 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
287 instance(static_cast<cppu::OWeakObject
*>(
288 new PopupMenuControllerFactory(context
)))
292 css::uno::Reference
<css::uno::XInterface
> instance
;
295 struct PopupMenuControllerFactorySingleton
:
296 public rtl::StaticWithArg
<
297 PopupMenuControllerFactoryInstance
,
298 css::uno::Reference
<css::uno::XComponentContext
>,
299 PopupMenuControllerFactorySingleton
>
302 class ToolbarControllerFactory
: public UIControllerFactory
305 ToolbarControllerFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
307 virtual OUString SAL_CALL
getImplementationName()
308 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
310 return OUString("com.sun.star.comp.framework.ToolBarControllerFactory");
313 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
314 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
316 return cppu::supportsService(this, ServiceName
);
319 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
320 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
322 css::uno::Sequence
< OUString
> aSeq(1);
323 aSeq
[0] = "com.sun.star.frame.ToolbarControllerFactory";
329 ToolbarControllerFactory::ToolbarControllerFactory( const Reference
< XComponentContext
>& xContext
) :
332 OUString( "ToolBar" ))
336 struct ToolbarControllerFactoryInstance
{
337 explicit ToolbarControllerFactoryInstance(
338 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
339 instance(static_cast<cppu::OWeakObject
*>(
340 new ToolbarControllerFactory(context
)))
344 css::uno::Reference
<css::uno::XInterface
> instance
;
347 struct ToolbarControllerFactorySingleton
:
348 public rtl::StaticWithArg
<
349 ToolbarControllerFactoryInstance
,
350 css::uno::Reference
<css::uno::XComponentContext
>,
351 ToolbarControllerFactorySingleton
>
354 class StatusbarControllerFactory
: public UIControllerFactory
357 StatusbarControllerFactory( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
);
359 virtual OUString SAL_CALL
getImplementationName()
360 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
362 return OUString("com.sun.star.comp.framework.StatusBarControllerFactory");
365 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
366 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
368 return cppu::supportsService(this, ServiceName
);
371 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
372 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
374 css::uno::Sequence
< OUString
> aSeq(1);
375 aSeq
[0] = "com.sun.star.frame.StatusbarControllerFactory";
381 StatusbarControllerFactory::StatusbarControllerFactory( const Reference
< XComponentContext
>& xContext
) :
384 OUString( "StatusBar" ) )
388 struct StatusbarControllerFactoryInstance
{
389 explicit StatusbarControllerFactoryInstance(
390 css::uno::Reference
<css::uno::XComponentContext
> const & context
):
391 instance(static_cast<cppu::OWeakObject
*>(
392 new StatusbarControllerFactory(context
)))
396 css::uno::Reference
<css::uno::XInterface
> instance
;
399 struct StatusbarControllerFactorySingleton
:
400 public rtl::StaticWithArg
<
401 StatusbarControllerFactoryInstance
,
402 css::uno::Reference
<css::uno::XComponentContext
>,
403 StatusbarControllerFactorySingleton
>
408 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
409 com_sun_star_comp_framework_PopupMenuControllerFactory_get_implementation(
410 css::uno::XComponentContext
*context
,
411 css::uno::Sequence
<css::uno::Any
> const &)
413 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
414 PopupMenuControllerFactorySingleton::get(context
).instance
.get()));
417 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
418 com_sun_star_comp_framework_ToolBarControllerFactory_get_implementation(
419 css::uno::XComponentContext
*context
,
420 css::uno::Sequence
<css::uno::Any
> const &)
422 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
423 ToolbarControllerFactorySingleton::get(context
).instance
.get()));
426 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
427 com_sun_star_comp_framework_StatusBarControllerFactory_get_implementation(
428 css::uno::XComponentContext
*context
,
429 css::uno::Sequence
<css::uno::Any
> const &)
431 return cppu::acquire(static_cast<cppu::OWeakObject
*>(
432 StatusbarControllerFactorySingleton::get(context
).instance
.get()));
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */