Line data Source code
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 <uifactory/factoryconfiguration.hxx>
21 :
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>
26 :
27 : #include <rtl/ustrbuf.hxx>
28 : #include <cppuhelper/basemutex.hxx>
29 : #include <cppuhelper/compbase2.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 :
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;
38 :
39 : namespace {
40 :
41 : typedef ::cppu::WeakComponentImplHelper2<
42 : css::lang::XServiceInfo,
43 : css::frame::XUIControllerFactory > UIControllerFactory_BASE;
44 :
45 : class UIControllerFactory : private cppu::BaseMutex,
46 : public UIControllerFactory_BASE
47 : {
48 : public:
49 : virtual ~UIControllerFactory();
50 :
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;
55 :
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;
60 :
61 : protected:
62 : UIControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext, const rtl::OUString &rUINode );
63 : bool m_bConfigRead;
64 : css::uno::Reference< css::uno::XComponentContext > m_xContext;
65 : ConfigurationAccess_ControllerFactory* m_pConfigAccess;
66 :
67 : private:
68 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
69 : };
70 :
71 0 : 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 )
77 0 : , m_pConfigAccess()
78 : {
79 : m_pConfigAccess = new ConfigurationAccess_ControllerFactory(m_xContext,
80 0 : "/org.openoffice.Office.UI.Controller/Registered/" + rConfigurationNode);
81 0 : m_pConfigAccess->acquire();
82 0 : }
83 :
84 0 : UIControllerFactory::~UIControllerFactory()
85 : {
86 0 : disposing();
87 0 : }
88 :
89 0 : void SAL_CALL UIControllerFactory::disposing()
90 : {
91 0 : osl::MutexGuard g(rBHelper.rMutex);
92 0 : if (m_pConfigAccess)
93 : {
94 : // reduce reference count
95 0 : m_pConfigAccess->release();
96 0 : m_pConfigAccess = 0;
97 0 : }
98 0 : }
99 :
100 : // XMultiComponentFactory
101 0 : Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
102 : const OUString& aServiceSpecifier,
103 : const Reference< XComponentContext >& )
104 : throw (Exception, RuntimeException, std::exception)
105 : {
106 : // SAFE
107 0 : osl::MutexGuard g(rBHelper.rMutex);
108 :
109 0 : if ( !m_bConfigRead )
110 : {
111 0 : m_bConfigRead = true;
112 0 : m_pConfigAccess->readConfigurationData();
113 : }
114 :
115 0 : OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, OUString() );
116 0 : if ( !aServiceName.isEmpty() )
117 0 : return m_xContext->getServiceManager()->createInstanceWithContext( aServiceName, m_xContext );
118 : else
119 0 : return Reference< XInterface >();
120 : // SAFE
121 : }
122 :
123 0 : 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)
128 : {
129 0 : const OUString aPropModuleName( "ModuleIdentifier" );
130 0 : const OUString aPropValueName( "Value" );
131 :
132 0 : OUString aPropName;
133 0 : PropertyValue aPropValue;
134 :
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
137 : // module!!
138 0 : for ( int i = 0; i < Arguments.getLength(); i++ )
139 : {
140 0 : if (( Arguments[i] >>= aPropValue ) && ( aPropValue.Name.equals( aPropModuleName )))
141 : {
142 0 : aPropValue.Value >>= aPropName;
143 0 : break;
144 : }
145 : }
146 :
147 0 : Sequence< Any > aNewArgs( Arguments );
148 :
149 0 : sal_Int32 nAppendIndex = aNewArgs.getLength();
150 0 : bool bHasValue = m_pConfigAccess->hasValue();
151 0 : aNewArgs.realloc( aNewArgs.getLength() + (bHasValue ? 2 : 1) );
152 :
153 : // Append the command URL to the Arguments sequence so that one controller can be
154 : // used for more than one command URL.
155 0 : aPropValue.Name = "CommandURL";
156 0 : aPropValue.Value <<= ServiceSpecifier;
157 0 : aNewArgs[nAppendIndex] <<= aPropValue;
158 :
159 0 : if ( bHasValue )
160 : {
161 : // Append the optional value argument. It's an empty string if no additional info
162 : // is provided to the controller.
163 0 : OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
164 0 : aPropValue.Name = aPropValueName;
165 0 : aPropValue.Value <<= aValue;
166 0 : aNewArgs[nAppendIndex+1] <<= aPropValue;
167 : }
168 :
169 : {
170 0 : OUString aServiceName;
171 : { // SAFE
172 0 : osl::MutexGuard g(rBHelper.rMutex);
173 :
174 0 : if ( !m_bConfigRead )
175 : {
176 0 : m_bConfigRead = true;
177 0 : m_pConfigAccess->readConfigurationData();
178 : }
179 :
180 0 : aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
181 : } // SAFE
182 :
183 0 : if ( !aServiceName.isEmpty() )
184 0 : return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, m_xContext );
185 : else
186 0 : return Reference< XInterface >();
187 0 : }
188 : }
189 :
190 0 : Sequence< OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
191 : throw (RuntimeException, std::exception)
192 : {
193 0 : return Sequence< OUString >();
194 : }
195 :
196 : // XUIControllerRegistration
197 0 : sal_Bool SAL_CALL UIControllerFactory::hasController(
198 : const OUString& aCommandURL,
199 : const OUString& aModuleName )
200 : throw (css::uno::RuntimeException, std::exception)
201 : {
202 0 : osl::MutexGuard g(rBHelper.rMutex);
203 :
204 0 : if ( !m_bConfigRead )
205 : {
206 0 : m_bConfigRead = true;
207 0 : m_pConfigAccess->readConfigurationData();
208 : }
209 :
210 0 : return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
211 : }
212 :
213 0 : void SAL_CALL UIControllerFactory::registerController(
214 : const OUString& aCommandURL,
215 : const OUString& aModuleName,
216 : const OUString& aControllerImplementationName )
217 : throw (RuntimeException, std::exception)
218 : {
219 : // SAFE
220 0 : osl::MutexGuard g(rBHelper.rMutex);
221 :
222 0 : if ( !m_bConfigRead )
223 : {
224 0 : m_bConfigRead = true;
225 0 : m_pConfigAccess->readConfigurationData();
226 : }
227 :
228 0 : m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
229 : // SAFE
230 0 : }
231 :
232 0 : void SAL_CALL UIControllerFactory::deregisterController(
233 : const OUString& aCommandURL,
234 : const OUString& aModuleName )
235 : throw (RuntimeException, std::exception)
236 : {
237 : // SAFE
238 0 : osl::MutexGuard g(rBHelper.rMutex);
239 :
240 0 : if ( !m_bConfigRead )
241 : {
242 0 : m_bConfigRead = true;
243 0 : m_pConfigAccess->readConfigurationData();
244 : }
245 :
246 0 : m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
247 : // SAFE
248 0 : }
249 :
250 0 : class PopupMenuControllerFactory : public UIControllerFactory
251 : {
252 : public:
253 : PopupMenuControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
254 :
255 0 : virtual OUString SAL_CALL getImplementationName()
256 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
257 : {
258 0 : return OUString("com.sun.star.comp.framework.PopupMenuControllerFactory");
259 : }
260 :
261 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
262 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
263 : {
264 0 : return cppu::supportsService(this, ServiceName);
265 : }
266 :
267 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
268 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
269 : {
270 0 : css::uno::Sequence< OUString > aSeq(1);
271 0 : aSeq[0] = OUString("com.sun.star.frame.PopupMenuControllerFactory");
272 0 : return aSeq;
273 : }
274 :
275 : };
276 :
277 0 : PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
278 : UIControllerFactory(
279 : xContext,
280 0 : OUString( "PopupMenu") )
281 : {
282 0 : }
283 :
284 0 : struct PopupMenuControllerFactoryInstance {
285 0 : explicit PopupMenuControllerFactoryInstance(
286 : css::uno::Reference<css::uno::XComponentContext> const & context):
287 : instance(static_cast<cppu::OWeakObject *>(
288 0 : new PopupMenuControllerFactory(context)))
289 : {
290 0 : }
291 :
292 : css::uno::Reference<css::uno::XInterface> instance;
293 : };
294 :
295 : struct PopupMenuControllerFactorySingleton:
296 : public rtl::StaticWithArg<
297 : PopupMenuControllerFactoryInstance,
298 : css::uno::Reference<css::uno::XComponentContext>,
299 : PopupMenuControllerFactorySingleton>
300 : {};
301 :
302 0 : class ToolbarControllerFactory : public UIControllerFactory
303 : {
304 : public:
305 : ToolbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
306 :
307 0 : virtual OUString SAL_CALL getImplementationName()
308 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
309 : {
310 0 : return OUString("com.sun.star.comp.framework.ToolBarControllerFactory");
311 : }
312 :
313 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
314 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
315 : {
316 0 : return cppu::supportsService(this, ServiceName);
317 : }
318 :
319 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
320 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
321 : {
322 0 : css::uno::Sequence< OUString > aSeq(1);
323 0 : aSeq[0] = OUString("com.sun.star.frame.ToolbarControllerFactory");
324 0 : return aSeq;
325 : }
326 :
327 : };
328 :
329 0 : ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
330 : UIControllerFactory(
331 : xContext,
332 0 : OUString( "ToolBar" ))
333 : {
334 0 : }
335 :
336 0 : struct ToolbarControllerFactoryInstance {
337 0 : explicit ToolbarControllerFactoryInstance(
338 : css::uno::Reference<css::uno::XComponentContext> const & context):
339 : instance(static_cast<cppu::OWeakObject *>(
340 0 : new ToolbarControllerFactory(context)))
341 : {
342 0 : }
343 :
344 : css::uno::Reference<css::uno::XInterface> instance;
345 : };
346 :
347 : struct ToolbarControllerFactorySingleton:
348 : public rtl::StaticWithArg<
349 : ToolbarControllerFactoryInstance,
350 : css::uno::Reference<css::uno::XComponentContext>,
351 : ToolbarControllerFactorySingleton>
352 : {};
353 :
354 0 : class StatusbarControllerFactory : public UIControllerFactory
355 : {
356 : public:
357 : StatusbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
358 :
359 0 : virtual OUString SAL_CALL getImplementationName()
360 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
361 : {
362 0 : return OUString("com.sun.star.comp.framework.StatusBarControllerFactory");
363 : }
364 :
365 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
366 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
367 : {
368 0 : return cppu::supportsService(this, ServiceName);
369 : }
370 :
371 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
372 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
373 : {
374 0 : css::uno::Sequence< OUString > aSeq(1);
375 0 : aSeq[0] = OUString("com.sun.star.frame.StatusbarControllerFactory");
376 0 : return aSeq;
377 : }
378 :
379 : };
380 :
381 0 : StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
382 : UIControllerFactory(
383 : xContext,
384 0 : OUString( "StatusBar" ) )
385 : {
386 0 : }
387 :
388 0 : struct StatusbarControllerFactoryInstance {
389 0 : explicit StatusbarControllerFactoryInstance(
390 : css::uno::Reference<css::uno::XComponentContext> const & context):
391 : instance(static_cast<cppu::OWeakObject *>(
392 0 : new StatusbarControllerFactory(context)))
393 : {
394 0 : }
395 :
396 : css::uno::Reference<css::uno::XInterface> instance;
397 : };
398 :
399 : struct StatusbarControllerFactorySingleton:
400 : public rtl::StaticWithArg<
401 : StatusbarControllerFactoryInstance,
402 : css::uno::Reference<css::uno::XComponentContext>,
403 : StatusbarControllerFactorySingleton>
404 : {};
405 :
406 : }
407 :
408 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
409 0 : com_sun_star_comp_framework_PopupMenuControllerFactory_get_implementation(
410 : css::uno::XComponentContext *context,
411 : css::uno::Sequence<css::uno::Any> const &)
412 : {
413 : return cppu::acquire(static_cast<cppu::OWeakObject *>(
414 0 : PopupMenuControllerFactorySingleton::get(context).instance.get()));
415 : }
416 :
417 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
418 0 : com_sun_star_comp_framework_ToolBarControllerFactory_get_implementation(
419 : css::uno::XComponentContext *context,
420 : css::uno::Sequence<css::uno::Any> const &)
421 : {
422 : return cppu::acquire(static_cast<cppu::OWeakObject *>(
423 0 : ToolbarControllerFactorySingleton::get(context).instance.get()));
424 : }
425 :
426 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
427 0 : com_sun_star_comp_framework_StatusBarControllerFactory_get_implementation(
428 : css::uno::XComponentContext *context,
429 : css::uno::Sequence<css::uno::Any> const &)
430 : {
431 : return cppu::acquire(static_cast<cppu::OWeakObject *>(
432 0 : StatusbarControllerFactorySingleton::get(context).instance.get()));
433 : }
434 :
435 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|