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 //_________________________________________________________________________________________________________________
28 //	my own includes
29 //_________________________________________________________________________________________________________________
30 #include "uifactory/factoryconfiguration.hxx"
31 #include <threadhelp/resetableguard.hxx>
32 #include "services.h"
33 
34 //_________________________________________________________________________________________________________________
35 //	interface includes
36 //_________________________________________________________________________________________________________________
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
40 #include <com/sun/star/container/XNameContainer.hpp>
41 #include <com/sun/star/container/XContainer.hpp>
42 
43 //_________________________________________________________________________________________________________________
44 //	includes of other projects
45 //_________________________________________________________________________________________________________________
46 #include <rtl/ustrbuf.hxx>
47 #include <cppuhelper/weak.hxx>
48 #include <rtl/logfile.hxx>
49 
50 //_________________________________________________________________________________________________________________
51 //	Defines
52 //_________________________________________________________________________________________________________________
53 //
54 using namespace com::sun::star;
55 using namespace com::sun::star::uno;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star::beans;
58 using namespace com::sun::star::container;
59 
60 //_________________________________________________________________________________________________________________
61 //	Namespace
62 //_________________________________________________________________________________________________________________
63 //
64 
65 namespace framework
66 {
getHashKeyFromStrings(const rtl::OUString & aCommandURL,const rtl::OUString & aModuleName)67 rtl::OUString getHashKeyFromStrings( const rtl::OUString& aCommandURL, const rtl::OUString& aModuleName )
68 {
69     rtl::OUStringBuffer aKey( aCommandURL );
70     aKey.appendAscii( "-" );
71     aKey.append( aModuleName );
72     return aKey.makeStringAndClear();
73 }
74 
75 //*****************************************************************************************************************
76 //	XInterface, XTypeProvider
77 //*****************************************************************************************************************
ConfigurationAccess_ControllerFactory(Reference<XMultiServiceFactory> & rServiceManager,const::rtl::OUString & _sRoot,bool _bAskValue)78 ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( Reference< XMultiServiceFactory >& rServiceManager,const ::rtl::OUString& _sRoot,bool _bAskValue ) :
79     ThreadHelpBase(),
80     m_aPropCommand( RTL_CONSTASCII_USTRINGPARAM( "Command" )),
81     m_aPropModule( RTL_CONSTASCII_USTRINGPARAM( "Module" )),
82     m_aPropController( RTL_CONSTASCII_USTRINGPARAM( "Controller" )),
83     m_aPropValue( RTL_CONSTASCII_USTRINGPARAM( "Value" )),
84     m_sRoot(_sRoot),
85     m_xServiceManager( rServiceManager ),
86     m_bConfigAccessInitialized( sal_False ),
87     m_bAskValue(_bAskValue)
88 {
89     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory" );
90     m_xConfigProvider = Reference< XMultiServiceFactory >( rServiceManager->createInstance( SERVICENAME_CFGPROVIDER),UNO_QUERY );
91 }
92 
~ConfigurationAccess_ControllerFactory()93 ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
94 {
95     // SAFE
96     ResetableGuard aLock( m_aLock );
97 
98     Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
99     if ( xContainer.is() )
100         xContainer->removeContainerListener( this );
101 }
102 
getServiceFromCommandModule(const rtl::OUString & rCommandURL,const rtl::OUString & rModule) const103 rtl::OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
104 {
105     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getServiceFromCommandModule" );
106     // SAFE
107     ResetableGuard aLock( m_aLock );
108     MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
109 
110     if ( pIter != m_aMenuControllerMap.end() )
111         return pIter->second.m_aImplementationName;
112     else if ( rModule.getLength() )
113     {
114         // Try to detect if we have a generic popup menu controller
115         pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
116 
117         if ( pIter != m_aMenuControllerMap.end() )
118             return pIter->second.m_aImplementationName;
119     }
120 
121     return rtl::OUString();
122 }
getValueFromCommandModule(const rtl::OUString & rCommandURL,const rtl::OUString & rModule) const123 rtl::OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const rtl::OUString& rCommandURL, const rtl::OUString& rModule ) const
124 {
125     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getValueFromCommandModule" );
126     // SAFE
127     ResetableGuard aLock( m_aLock );
128 
129     MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
130 
131     if ( pIter != m_aMenuControllerMap.end() )
132         return pIter->second.m_aValue;
133     else if ( rModule.getLength() )
134     {
135         // Try to detect if we have a generic popup menu controller
136         pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rtl::OUString() ));
137 
138         if ( pIter != m_aMenuControllerMap.end() )
139             return pIter->second.m_aValue;
140     }
141 
142     return rtl::OUString();
143 }
144 
145 
addServiceToCommandModule(const rtl::OUString & rCommandURL,const rtl::OUString & rModule,const rtl::OUString & rServiceSpecifier)146 void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
147     const rtl::OUString& rCommandURL,
148     const rtl::OUString& rModule,
149     const rtl::OUString& rServiceSpecifier )
150 {
151     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::addServiceToCommandModule" );
152     // SAFE
153     ResetableGuard aLock( m_aLock );
154 
155     rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
156     m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey,ControllerInfo(rServiceSpecifier,::rtl::OUString()) ));
157 }
158 
removeServiceFromCommandModule(const rtl::OUString & rCommandURL,const rtl::OUString & rModule)159 void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
160     const rtl::OUString& rCommandURL,
161     const rtl::OUString& rModule )
162 {
163     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule" );
164     // SAFE
165     ResetableGuard aLock( m_aLock );
166 
167     rtl::OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
168     m_aMenuControllerMap.erase( aHashKey );
169 }
170 
171 // container.XContainerListener
elementInserted(const ContainerEvent & aEvent)172 void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent ) throw(RuntimeException)
173 {
174     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementInserted" );
175     rtl::OUString   aCommand;
176     rtl::OUString   aModule;
177     rtl::OUString   aService;
178     rtl::OUString   aValue;
179 
180     // SAFE
181     ResetableGuard aLock( m_aLock );
182 
183     if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
184     {
185         // Create hash key from command and module as they are together a primary key to
186         // the UNO service that implements the popup menu controller.
187         rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
188         ControllerInfo& rControllerInfo = m_aMenuControllerMap[ aHashKey ];
189         rControllerInfo.m_aImplementationName = aService;
190         rControllerInfo.m_aValue = aValue;
191     }
192 }
193 
elementRemoved(const ContainerEvent & aEvent)194 void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent ) throw(RuntimeException)
195 {
196     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementRemoved" );
197     rtl::OUString   aCommand;
198     rtl::OUString   aModule;
199     rtl::OUString   aService;
200     rtl::OUString   aValue;
201 
202     // SAFE
203     ResetableGuard aLock( m_aLock );
204 
205     if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
206     {
207         // Create hash key from command and module as they are together a primary key to
208         // the UNO service that implements the popup menu controller.
209         rtl::OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
210         m_aMenuControllerMap.erase( aHashKey );
211     }
212 }
213 
elementReplaced(const ContainerEvent & aEvent)214 void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent ) throw(RuntimeException)
215 {
216     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementReplaced" );
217     elementInserted(aEvent);
218 }
219 
220 // lang.XEventListener
disposing(const EventObject &)221 void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& ) throw(RuntimeException)
222 {
223     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::disposing" );
224     // SAFE
225     // remove our reference to the config access
226     ResetableGuard aLock( m_aLock );
227     m_xConfigAccess.clear();
228 }
229 
readConfigurationData()230 void ConfigurationAccess_ControllerFactory::readConfigurationData()
231 {
232     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::readConfigurationData" );
233     // SAFE
234     ResetableGuard aLock( m_aLock );
235 
236     if ( !m_bConfigAccessInitialized )
237     {
238         Sequence< Any > aArgs( 1 );
239         PropertyValue   aPropValue;
240 
241         aPropValue.Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
242         aPropValue.Value <<= m_sRoot;
243         aArgs[0] <<= aPropValue;
244 
245         try
246         {
247             m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
248         }
249         catch ( WrappedTargetException& )
250         {
251         }
252 
253         m_bConfigAccessInitialized = sal_True;
254     }
255 
256     if ( m_xConfigAccess.is() )
257     {
258         // Read and update configuration data
259         updateConfigurationData();
260 
261         uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
262         // UNSAFE
263         aLock.unlock();
264 
265         if ( xContainer.is() )
266             xContainer->addContainerListener( this );
267     }
268 }
269 
updateConfigurationData()270 void ConfigurationAccess_ControllerFactory::updateConfigurationData()
271 {
272     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::updateConfigurationData" );
273     // SAFE
274     ResetableGuard aLock( m_aLock );
275     if ( m_xConfigAccess.is() )
276     {
277         Sequence< rtl::OUString >   aPopupMenuControllers = m_xConfigAccess->getElementNames();
278 
279         rtl::OUString aCommand;
280         rtl::OUString aModule;
281         rtl::OUString aService;
282         rtl::OUString aHashKey;
283         rtl::OUString aValue;
284 
285         m_aMenuControllerMap.clear();
286         for ( sal_Int32 i = 0; i < aPopupMenuControllers.getLength(); i++ )
287         {
288             try
289             {
290                 if ( impl_getElementProps( m_xConfigAccess->getByName( aPopupMenuControllers[i] ), aCommand, aModule, aService,aValue ))
291                 {
292                     // Create hash key from command and module as they are together a primary key to
293                     // the UNO service that implements the popup menu controller.
294                     aHashKey = getHashKeyFromStrings( aCommand, aModule );
295                     m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey, ControllerInfo(aService,aValue) ));
296                 }
297             }
298             catch ( NoSuchElementException& )
299             {
300             }
301             catch ( WrappedTargetException& )
302             {
303             }
304         }
305     }
306 }
307 
impl_getElementProps(const Any & aElement,rtl::OUString & aCommand,rtl::OUString & aModule,rtl::OUString & aServiceSpecifier,rtl::OUString & aValue) const308 sal_Bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, rtl::OUString& aCommand, rtl::OUString& aModule, rtl::OUString& aServiceSpecifier,rtl::OUString& aValue  ) const
309 {
310     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::impl_getElementProps" );
311     Reference< XPropertySet > xPropertySet;
312     aElement >>= xPropertySet;
313 
314     if ( xPropertySet.is() )
315     {
316         try
317         {
318             xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
319             xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
320             xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
321             if ( m_bAskValue )
322                 xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
323         }
324         catch ( com::sun::star::beans::UnknownPropertyException& )
325         {
326             return sal_False;
327         }
328         catch ( com::sun::star::lang::WrappedTargetException& )
329         {
330             return sal_False;
331         }
332     }
333 
334     return sal_True;
335 }
336 } // namespace framework
337