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