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/uicontrollerfactory.hxx>
21 : #include <uifactory/factoryconfiguration.hxx>
22 : #include <threadhelp/resetableguard.hxx>
23 : #include "services.h"
24 :
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <com/sun/star/container/XNameAccess.hpp>
28 : #include <com/sun/star/container/XNameContainer.hpp>
29 : #include <com/sun/star/container/XContainer.hpp>
30 :
31 : #include <rtl/ustrbuf.hxx>
32 : #include <cppuhelper/weak.hxx>
33 :
34 : using namespace com::sun::star::uno;
35 : using namespace com::sun::star::lang;
36 : using namespace com::sun::star::beans;
37 : using namespace com::sun::star::container;
38 : using namespace ::com::sun::star::frame;
39 :
40 : namespace framework
41 : {
42 :
43 171 : UIControllerFactory::UIControllerFactory(
44 : const Reference< XComponentContext >& xContext,
45 : const rtl::OUString &rConfigurationNode )
46 : : ThreadHelpBase()
47 : , m_bConfigRead( sal_False )
48 : , m_xContext( xContext )
49 171 : , m_pConfigAccess()
50 : {
51 171 : rtl::OUStringBuffer aBuffer;
52 : aBuffer.appendAscii(
53 : RTL_CONSTASCII_STRINGPARAM(
54 171 : "/org.openoffice.Office.UI.Controller/Registered/" ) );
55 171 : aBuffer.append( rConfigurationNode );
56 171 : m_pConfigAccess = new ConfigurationAccess_ControllerFactory( m_xContext, aBuffer.makeStringAndClear() );
57 171 : m_pConfigAccess->acquire();
58 171 : }
59 :
60 330 : UIControllerFactory::~UIControllerFactory()
61 : {
62 165 : ResetableGuard aLock( m_aLock );
63 :
64 : // reduce reference count
65 165 : m_pConfigAccess->release();
66 165 : }
67 :
68 : // XMultiComponentFactory
69 0 : Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
70 : const OUString& aServiceSpecifier,
71 : const Reference< XComponentContext >& )
72 : throw (Exception, RuntimeException)
73 : {
74 : // SAFE
75 0 : ResetableGuard aLock( m_aLock );
76 :
77 0 : if ( !m_bConfigRead )
78 : {
79 0 : m_bConfigRead = sal_True;
80 0 : m_pConfigAccess->readConfigurationData();
81 : }
82 :
83 0 : OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, OUString() );
84 0 : if ( !aServiceName.isEmpty() )
85 0 : return m_xContext->getServiceManager()->createInstanceWithContext( aServiceName, m_xContext );
86 : else
87 0 : return Reference< XInterface >();
88 : // SAFE
89 : }
90 :
91 2439 : Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithArgumentsAndContext(
92 : const OUString& ServiceSpecifier,
93 : const Sequence< Any >& Arguments,
94 : const Reference< XComponentContext >& )
95 : throw (Exception, RuntimeException)
96 : {
97 2439 : const OUString aPropModuleName( "ModuleIdentifier" );
98 4878 : const OUString aPropValueName( "Value" );
99 :
100 4878 : OUString aPropName;
101 4878 : PropertyValue aPropValue;
102 :
103 : // Retrieve the optional module name form the Arguments sequence. It is used as a part of
104 : // the hash map key to support different controller implementation for the same URL but different
105 : // module!!
106 3457 : for ( int i = 0; i < Arguments.getLength(); i++ )
107 : {
108 3457 : if (( Arguments[i] >>= aPropValue ) && ( aPropValue.Name.equals( aPropModuleName )))
109 : {
110 2439 : aPropValue.Value >>= aPropName;
111 2439 : break;
112 : }
113 : }
114 :
115 4878 : Sequence< Any > aNewArgs( Arguments );
116 :
117 2439 : sal_Int32 nAppendIndex = aNewArgs.getLength();
118 2439 : bool bHasValue = m_pConfigAccess->hasValue();
119 2439 : aNewArgs.realloc( aNewArgs.getLength() + (bHasValue ? 2 : 1) );
120 :
121 : // Append the command URL to the Arguments sequence so that one controller can be
122 : // used for more than one command URL.
123 2439 : aPropValue.Name = OUString( "CommandURL" );
124 2439 : aPropValue.Value <<= ServiceSpecifier;
125 2439 : aNewArgs[nAppendIndex] <<= aPropValue;
126 :
127 2439 : if ( bHasValue )
128 : {
129 : // Append the optional value argument. It's an empty string if no additional info
130 : // is provided to the controller.
131 0 : OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
132 0 : aPropValue.Name = aPropValueName;
133 0 : aPropValue.Value <<= aValue;
134 0 : aNewArgs[nAppendIndex+1] <<= aPropValue;
135 : }
136 :
137 : {
138 : // SAFE
139 2439 : ResetableGuard aLock( m_aLock );
140 :
141 2439 : if ( !m_bConfigRead )
142 : {
143 0 : m_bConfigRead = sal_True;
144 0 : m_pConfigAccess->readConfigurationData();
145 : }
146 :
147 4878 : OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
148 4878 : Reference< XComponentContext > xContext( m_xContext );
149 :
150 2439 : aLock.unlock();
151 : // SAFE
152 :
153 2439 : if ( !aServiceName.isEmpty() )
154 2439 : return xContext->getServiceManager()->createInstanceWithArgumentsAndContext( aServiceName, aNewArgs, xContext );
155 : else
156 2439 : return Reference< XInterface >();
157 2439 : }
158 : }
159 :
160 0 : Sequence< OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
161 : throw (RuntimeException)
162 : {
163 0 : return Sequence< OUString >();
164 : }
165 :
166 : // XUIControllerRegistration
167 209453 : sal_Bool SAL_CALL UIControllerFactory::hasController(
168 : const OUString& aCommandURL,
169 : const OUString& aModuleName )
170 : throw (::com::sun::star::uno::RuntimeException)
171 : {
172 209453 : ResetableGuard aLock( m_aLock );
173 :
174 209453 : if ( !m_bConfigRead )
175 : {
176 90 : m_bConfigRead = sal_True;
177 90 : m_pConfigAccess->readConfigurationData();
178 : }
179 :
180 209453 : return ( !m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).isEmpty() );
181 : }
182 :
183 2 : void SAL_CALL UIControllerFactory::registerController(
184 : const OUString& aCommandURL,
185 : const OUString& aModuleName,
186 : const OUString& aControllerImplementationName )
187 : throw (RuntimeException)
188 : {
189 : // SAFE
190 2 : ResetableGuard aLock( m_aLock );
191 :
192 2 : if ( !m_bConfigRead )
193 : {
194 0 : m_bConfigRead = sal_True;
195 0 : m_pConfigAccess->readConfigurationData();
196 : }
197 :
198 2 : m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
199 : // SAFE
200 2 : }
201 :
202 1 : void SAL_CALL UIControllerFactory::deregisterController(
203 : const OUString& aCommandURL,
204 : const OUString& aModuleName )
205 : throw (RuntimeException)
206 : {
207 : // SAFE
208 1 : ResetableGuard aLock( m_aLock );
209 :
210 1 : if ( !m_bConfigRead )
211 : {
212 0 : m_bConfigRead = sal_True;
213 0 : m_pConfigAccess->readConfigurationData();
214 : }
215 :
216 1 : m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
217 : // SAFE
218 1 : }
219 :
220 :
221 1670 : DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( PopupMenuControllerFactory ,
222 : ::cppu::OWeakObject ,
223 : SERVICENAME_POPUPMENUCONTROLLERFACTORY ,
224 : IMPLEMENTATIONNAME_POPUPMENUCONTROLLERFACTORY
225 : )
226 :
227 57 : DEFINE_INIT_SERVICE ( PopupMenuControllerFactory, {} )
228 :
229 57 : PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XComponentContext >& xContext ) :
230 : UIControllerFactory(
231 : xContext,
232 57 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PopupMenu" )) )
233 : {
234 57 : }
235 :
236 954 : DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( ToolbarControllerFactory ,
237 : ::cppu::OWeakObject ,
238 : SERVICENAME_TOOLBARCONTROLLERFACTORY ,
239 : IMPLEMENTATIONNAME_TOOLBARCONTROLLERFACTORY
240 : )
241 :
242 57 : DEFINE_INIT_SERVICE ( ToolbarControllerFactory, {} )
243 :
244 57 : ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XComponentContext >& xContext ) :
245 : UIControllerFactory(
246 : xContext,
247 57 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ToolBar" )))
248 : {
249 57 : }
250 :
251 727 : DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( StatusbarControllerFactory ,
252 : ::cppu::OWeakObject ,
253 : SERVICENAME_STATUSBARCONTROLLERFACTORY ,
254 : IMPLEMENTATIONNAME_STATUSBARCONTROLLERFACTORY
255 : )
256 :
257 57 : DEFINE_INIT_SERVICE ( StatusbarControllerFactory, {} )
258 :
259 57 : StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XComponentContext >& xContext ) :
260 : UIControllerFactory(
261 : xContext,
262 57 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StatusBar" )) )
263 : {
264 57 : }
265 :
266 : } // namespace framework
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|