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 320 : 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 320 : , m_pConfigAccess()
78 : {
79 : m_pConfigAccess = new ConfigurationAccess_ControllerFactory(m_xContext,
80 320 : "/org.openoffice.Office.UI.Controller/Registered/" + rConfigurationNode);
81 320 : m_pConfigAccess->acquire();
82 320 : }
83 :
84 596 : UIControllerFactory::~UIControllerFactory()
85 : {
86 298 : disposing();
87 298 : }
88 :
89 614 : void SAL_CALL UIControllerFactory::disposing()
90 : {
91 614 : osl::MutexGuard g(rBHelper.rMutex);
92 614 : if (m_pConfigAccess)
93 : {
94 : // reduce reference count
95 316 : m_pConfigAccess->release();
96 316 : m_pConfigAccess = 0;
97 614 : }
98 614 : }
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 4151 : 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 4151 : const OUString aPropModuleName( "ModuleIdentifier" );
130 8302 : const OUString aPropValueName( "Value" );
131 :
132 8302 : OUString aPropName;
133 8302 : 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 5103 : for ( int i = 0; i < Arguments.getLength(); i++ )
139 : {
140 5103 : if (( Arguments[i] >>= aPropValue ) && ( aPropValue.Name.equals( aPropModuleName )))
141 : {
142 4151 : aPropValue.Value >>= aPropName;
143 4151 : break;
144 : }
145 : }
146 :
147 8302 : Sequence< Any > aNewArgs( Arguments );
148 :
149 4151 : sal_Int32 nAppendIndex = aNewArgs.getLength();
150 4151 : bool bHasValue = m_pConfigAccess->hasValue();
151 4151 : 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 4151 : aPropValue.Name = "CommandURL";
156 4151 : aPropValue.Value <<= ServiceSpecifier;
157 4151 : aNewArgs[nAppendIndex] <<= aPropValue;
158 :
159 4151 : 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 4151 : OUString aServiceName;
171 : { // SAFE
172 4151 : osl::MutexGuard g(rBHelper.rMutex);
173 :
174 4151 : if ( !m_bConfigRead )
175 : {
176 0 : m_bConfigRead = true;
177 0 : m_pConfigAccess->readConfigurationData();
178 : }
179 :
180 4151 : aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
181 : } // SAFE
182 :
183 4151 : if ( !aServiceName.isEmpty() )
184 4151 : return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, m_xContext );
185 : else
186 0 : return Reference< XInterface >();
187 4151 : }
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 304533 : sal_Bool SAL_CALL UIControllerFactory::hasController(
198 : const OUString& aCommandURL,
199 : const OUString& aModuleName )
200 : throw (css::uno::RuntimeException, std::exception)
201 : {
202 304533 : osl::MutexGuard g(rBHelper.rMutex);
203 :
204 304533 : if ( !m_bConfigRead )
205 : {
206 159 : m_bConfigRead = true;
207 159 : m_pConfigAccess->readConfigurationData();
208 : }
209 :
210 304533 : return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
211 : }
212 :
213 2 : 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 2 : osl::MutexGuard g(rBHelper.rMutex);
221 :
222 2 : if ( !m_bConfigRead )
223 : {
224 0 : m_bConfigRead = true;
225 0 : m_pConfigAccess->readConfigurationData();
226 : }
227 :
228 2 : m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
229 : // SAFE
230 2 : }
231 :
232 1 : void SAL_CALL UIControllerFactory::deregisterController(
233 : const OUString& aCommandURL,
234 : const OUString& aModuleName )
235 : throw (RuntimeException, std::exception)
236 : {
237 : // SAFE
238 1 : osl::MutexGuard g(rBHelper.rMutex);
239 :
240 1 : if ( !m_bConfigRead )
241 : {
242 0 : m_bConfigRead = true;
243 0 : m_pConfigAccess->readConfigurationData();
244 : }
245 :
246 1 : m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
247 : // SAFE
248 1 : }
249 :
250 198 : class PopupMenuControllerFactory : public UIControllerFactory
251 : {
252 : public:
253 : PopupMenuControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
254 :
255 1 : virtual OUString SAL_CALL getImplementationName()
256 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
257 : {
258 1 : 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 1 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
268 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
269 : {
270 1 : css::uno::Sequence< OUString > aSeq(1);
271 1 : aSeq[0] = "com.sun.star.frame.PopupMenuControllerFactory";
272 1 : return aSeq;
273 : }
274 :
275 : };
276 :
277 106 : PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
278 : UIControllerFactory(
279 : xContext,
280 106 : OUString( "PopupMenu") )
281 : {
282 106 : }
283 :
284 106 : struct PopupMenuControllerFactoryInstance {
285 106 : explicit PopupMenuControllerFactoryInstance(
286 : css::uno::Reference<css::uno::XComponentContext> const & context):
287 : instance(static_cast<cppu::OWeakObject *>(
288 106 : new PopupMenuControllerFactory(context)))
289 : {
290 106 : }
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 200 : class ToolbarControllerFactory : public UIControllerFactory
303 : {
304 : public:
305 : ToolbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
306 :
307 1 : virtual OUString SAL_CALL getImplementationName()
308 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
309 : {
310 1 : 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 1 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
320 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
321 : {
322 1 : css::uno::Sequence< OUString > aSeq(1);
323 1 : aSeq[0] = "com.sun.star.frame.ToolbarControllerFactory";
324 1 : return aSeq;
325 : }
326 :
327 : };
328 :
329 108 : ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
330 : UIControllerFactory(
331 : xContext,
332 108 : OUString( "ToolBar" ))
333 : {
334 108 : }
335 :
336 108 : struct ToolbarControllerFactoryInstance {
337 108 : explicit ToolbarControllerFactoryInstance(
338 : css::uno::Reference<css::uno::XComponentContext> const & context):
339 : instance(static_cast<cppu::OWeakObject *>(
340 108 : new ToolbarControllerFactory(context)))
341 : {
342 108 : }
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 198 : class StatusbarControllerFactory : public UIControllerFactory
355 : {
356 : public:
357 : StatusbarControllerFactory( const css::uno::Reference< css::uno::XComponentContext >& xContext );
358 :
359 1 : virtual OUString SAL_CALL getImplementationName()
360 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
361 : {
362 1 : 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 1 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
372 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
373 : {
374 1 : css::uno::Sequence< OUString > aSeq(1);
375 1 : aSeq[0] = "com.sun.star.frame.StatusbarControllerFactory";
376 1 : return aSeq;
377 : }
378 :
379 : };
380 :
381 106 : StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
382 : UIControllerFactory(
383 : xContext,
384 106 : OUString( "StatusBar" ) )
385 : {
386 106 : }
387 :
388 106 : struct StatusbarControllerFactoryInstance {
389 106 : explicit StatusbarControllerFactoryInstance(
390 : css::uno::Reference<css::uno::XComponentContext> const & context):
391 : instance(static_cast<cppu::OWeakObject *>(
392 106 : new StatusbarControllerFactory(context)))
393 : {
394 106 : }
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 106 : 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 106 : PopupMenuControllerFactorySingleton::get(context).instance.get()));
415 : }
416 :
417 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
418 108 : 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 108 : ToolbarControllerFactorySingleton::get(context).instance.get()));
424 : }
425 :
426 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
427 107 : 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 107 : StatusbarControllerFactorySingleton::get(context).instance.get()));
433 : }
434 :
435 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|