xref: /trunk/main/framework/source/uifactory/uicontrollerfactory.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #include <uifactory/uicontrollerfactory.hxx>
28 #include <uifactory/factoryconfiguration.hxx>
29 #include <threadhelp/resetableguard.hxx>
30 #include "services.h"
31 
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/container/XContainer.hpp>
37 
38 #include <rtl/ustrbuf.hxx>
39 #include <cppuhelper/weak.hxx>
40 
41 using namespace com::sun::star::uno;
42 using namespace com::sun::star::lang;
43 using namespace com::sun::star::beans;
44 using namespace com::sun::star::container;
45 using namespace ::com::sun::star::frame;
46 
47 namespace framework
48 {
49 
UIControllerFactory(const Reference<XMultiServiceFactory> & xServiceManager,const rtl::OUString & rConfigurationNode)50 UIControllerFactory::UIControllerFactory(
51     const Reference< XMultiServiceFactory >& xServiceManager,
52     const rtl::OUString &rConfigurationNode )
53     : ThreadHelpBase()
54     , m_bConfigRead( sal_False )
55     , m_xServiceManager( xServiceManager )
56     , m_pConfigAccess()
57 {
58     rtl::OUStringBuffer aBuffer;
59     aBuffer.appendAscii(
60         RTL_CONSTASCII_STRINGPARAM(
61             "/org.openoffice.Office.UI.Controller/Registered/" ) );
62     aBuffer.append( rConfigurationNode );
63     m_pConfigAccess = new ConfigurationAccess_ControllerFactory(
64         m_xServiceManager, aBuffer.makeStringAndClear() );
65     m_pConfigAccess->acquire();
66 }
67 
~UIControllerFactory()68 UIControllerFactory::~UIControllerFactory()
69 {
70     ResetableGuard aLock( m_aLock );
71 
72     // reduce reference count
73     m_pConfigAccess->release();
74 }
75 
76 // XMultiComponentFactory
createInstanceWithContext(const::rtl::OUString & aServiceSpecifier,const Reference<XComponentContext> &)77 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithContext(
78     const ::rtl::OUString& aServiceSpecifier,
79     const Reference< XComponentContext >& )
80 {
81     // SAFE
82     ResetableGuard aLock( m_aLock );
83 
84     if ( !m_bConfigRead )
85     {
86         m_bConfigRead = sal_True;
87         m_pConfigAccess->readConfigurationData();
88     }
89 
90     rtl::OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( aServiceSpecifier, rtl::OUString() );
91     if ( aServiceName.getLength() > 0 )
92         return m_xServiceManager->createInstance( aServiceName );
93     else
94         return Reference< XInterface >();
95     // SAFE
96 }
97 
createInstanceWithArgumentsAndContext(const::rtl::OUString & ServiceSpecifier,const Sequence<Any> & Arguments,const Reference<XComponentContext> &)98 Reference< XInterface > SAL_CALL UIControllerFactory::createInstanceWithArgumentsAndContext(
99     const ::rtl::OUString&                  ServiceSpecifier,
100     const Sequence< Any >&                  Arguments,
101     const Reference< XComponentContext >& )
102 {
103     const rtl::OUString aPropModuleName( RTL_CONSTASCII_USTRINGPARAM( "ModuleIdentifier" ));
104     const rtl::OUString aPropValueName( RTL_CONSTASCII_USTRINGPARAM( "Value" ));
105 
106     rtl::OUString   aPropName;
107     PropertyValue   aPropValue;
108 
109     // Retrieve the optional module name form the Arguments sequence. It is used as a part of
110     // the hash map key to support different controller implementation for the same URL but different
111     // module!!
112     for ( int i = 0; i < Arguments.getLength(); i++ )
113     {
114         if (( Arguments[i] >>= aPropValue ) && ( aPropValue.Name.equals( aPropModuleName )))
115         {
116             aPropValue.Value >>= aPropName;
117             break;
118         }
119     }
120 
121     Sequence< Any > aNewArgs( Arguments );
122 
123     sal_Int32 nAppendIndex = aNewArgs.getLength();
124     bool bHasValue = m_pConfigAccess->hasValue();
125     aNewArgs.realloc( aNewArgs.getLength() + (bHasValue ? 2 : 1) );
126 
127     // Append the command URL to the Arguments sequence so that one controller can be
128     // used for more than one command URL.
129     aPropValue.Name     = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CommandURL" ));
130     aPropValue.Value  <<= ServiceSpecifier;
131     aNewArgs[nAppendIndex] <<= aPropValue;
132 
133     if ( bHasValue )
134     {
135         // Append the optional value argument. It's an empty string if no additional info
136         // is provided to the controller.
137         rtl::OUString aValue = m_pConfigAccess->getValueFromCommandModule( ServiceSpecifier, aPropName );
138         aPropValue.Name = aPropValueName;
139         aPropValue.Value <<= aValue;
140         aNewArgs[nAppendIndex+1] <<= aPropValue;
141     }
142 
143     {
144         // SAFE
145         ResetableGuard aLock( m_aLock );
146 
147         if ( !m_bConfigRead )
148         {
149             m_bConfigRead = sal_True;
150             m_pConfigAccess->readConfigurationData();
151         }
152 
153         rtl::OUString aServiceName = m_pConfigAccess->getServiceFromCommandModule( ServiceSpecifier, aPropName );
154         Reference< XMultiServiceFactory > xServiceManager( m_xServiceManager );
155 
156         aLock.unlock();
157         // SAFE
158 
159         if ( aServiceName.getLength() > 0 )
160             return xServiceManager->createInstanceWithArguments( aServiceName, aNewArgs );
161         else
162             return Reference< XInterface >();
163     }
164 }
165 
getAvailableServiceNames()166 Sequence< ::rtl::OUString > SAL_CALL UIControllerFactory::getAvailableServiceNames()
167 {
168     return Sequence< ::rtl::OUString >();
169 }
170 
171 // XUIControllerRegistration
hasController(const::rtl::OUString & aCommandURL,const rtl::OUString & aModuleName)172 sal_Bool SAL_CALL UIControllerFactory::hasController(
173     const ::rtl::OUString& aCommandURL,
174     const rtl::OUString& aModuleName )
175 {
176     ResetableGuard aLock( m_aLock );
177 
178     if ( !m_bConfigRead )
179     {
180         m_bConfigRead = sal_True;
181         m_pConfigAccess->readConfigurationData();
182     }
183 
184     return ( m_pConfigAccess->getServiceFromCommandModule( aCommandURL, aModuleName ).getLength() > 0 );
185 }
186 
registerController(const::rtl::OUString & aCommandURL,const::rtl::OUString & aModuleName,const::rtl::OUString & aControllerImplementationName)187 void SAL_CALL UIControllerFactory::registerController(
188     const ::rtl::OUString& aCommandURL,
189     const ::rtl::OUString& aModuleName,
190     const ::rtl::OUString& aControllerImplementationName )
191 {
192     // SAFE
193     ResetableGuard aLock( m_aLock );
194 
195     if ( !m_bConfigRead )
196     {
197         m_bConfigRead = sal_True;
198         m_pConfigAccess->readConfigurationData();
199     }
200 
201     m_pConfigAccess->addServiceToCommandModule( aCommandURL, aModuleName, aControllerImplementationName );
202     // SAFE
203 }
204 
deregisterController(const::rtl::OUString & aCommandURL,const rtl::OUString & aModuleName)205 void SAL_CALL UIControllerFactory::deregisterController(
206     const ::rtl::OUString& aCommandURL,
207     const rtl::OUString& aModuleName )
208 {
209     // SAFE
210     ResetableGuard aLock( m_aLock );
211 
212     if ( !m_bConfigRead )
213     {
214         m_bConfigRead = sal_True;
215         m_pConfigAccess->readConfigurationData();
216     }
217 
218     m_pConfigAccess->removeServiceFromCommandModule( aCommandURL, aModuleName );
219     // SAFE
220 }
221 
222 
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(PopupMenuControllerFactory,::cppu::OWeakObject,SERVICENAME_POPUPMENUCONTROLLERFACTORY,IMPLEMENTATIONNAME_POPUPMENUCONTROLLERFACTORY)223 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   PopupMenuControllerFactory                      ,
224                                             ::cppu::OWeakObject                             ,
225                                             SERVICENAME_POPUPMENUCONTROLLERFACTORY          ,
226                                             IMPLEMENTATIONNAME_POPUPMENUCONTROLLERFACTORY
227                                         )
228 
229 DEFINE_INIT_SERVICE                     (   PopupMenuControllerFactory, {} )
230 
231 PopupMenuControllerFactory::PopupMenuControllerFactory( const Reference< XMultiServiceFactory >& xServiceManager ) :
232     UIControllerFactory(
233         xServiceManager,
234         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PopupMenu" )) )
235 {
236 }
237 
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(ToolbarControllerFactory,::cppu::OWeakObject,SERVICENAME_TOOLBARCONTROLLERFACTORY,IMPLEMENTATIONNAME_TOOLBARCONTROLLERFACTORY)238 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   ToolbarControllerFactory                     ,
239                                             ::cppu::OWeakObject                             ,
240                                             SERVICENAME_TOOLBARCONTROLLERFACTORY            ,
241                                             IMPLEMENTATIONNAME_TOOLBARCONTROLLERFACTORY
242                                         )
243 
244 DEFINE_INIT_SERVICE                     (   ToolbarControllerFactory, {} )
245 
246 ToolbarControllerFactory::ToolbarControllerFactory( const Reference< XMultiServiceFactory >& xServiceManager ) :
247     UIControllerFactory(
248         xServiceManager,
249         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ToolBar" )))
250 {
251 }
252 
DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(StatusbarControllerFactory,::cppu::OWeakObject,SERVICENAME_STATUSBARCONTROLLERFACTORY,IMPLEMENTATIONNAME_STATUSBARCONTROLLERFACTORY)253 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   StatusbarControllerFactory                      ,
254                                             ::cppu::OWeakObject                             ,
255                                             SERVICENAME_STATUSBARCONTROLLERFACTORY          ,
256                                             IMPLEMENTATIONNAME_STATUSBARCONTROLLERFACTORY
257                                         )
258 
259 DEFINE_INIT_SERVICE                     (   StatusbarControllerFactory, {} )
260 
261 StatusbarControllerFactory::StatusbarControllerFactory( const Reference< XMultiServiceFactory >& xServiceManager ) :
262     UIControllerFactory(
263         xServiceManager,
264         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StatusBar" )) )
265 {
266 }
267 
268 } // namespace framework
269