1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationAccess_ControllerFactory.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include "uifactory/factoryconfiguration.hxx"
38 #include <threadhelp/resetableguard.hxx>
41 //_________________________________________________________________________________________________________________
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/container/XNameAccess.hpp>
47 #include <com/sun/star/container/XNameContainer.hpp>
48 #include <com/sun/star/container/XContainer.hpp>
50 //_________________________________________________________________________________________________________________
51 // includes of other projects
52 //_________________________________________________________________________________________________________________
53 #include <rtl/ustrbuf.hxx>
54 #include <cppuhelper/weak.hxx>
55 #include <rtl/logfile.hxx>
57 //_________________________________________________________________________________________________________________
59 //_________________________________________________________________________________________________________________
61 using namespace com::sun::star
;
62 using namespace com::sun::star::uno
;
63 using namespace com::sun::star::lang
;
64 using namespace com::sun::star::beans
;
65 using namespace com::sun::star::container
;
66 using namespace ::com::sun::star::frame
;
68 //_________________________________________________________________________________________________________________
70 //_________________________________________________________________________________________________________________
75 rtl::OUString
getHashKeyFromStrings( const rtl::OUString
& aCommandURL
, const rtl::OUString
& aModuleName
)
77 rtl::OUStringBuffer
aKey( aCommandURL
);
78 aKey
.appendAscii( "-" );
79 aKey
.append( aModuleName
);
80 return aKey
.makeStringAndClear();
83 //*****************************************************************************************************************
84 // XInterface, XTypeProvider
85 //*****************************************************************************************************************
86 ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( Reference
< XMultiServiceFactory
>& rServiceManager
,const ::rtl::OUString
& _sRoot
,bool _bAskValue
) :
88 m_aPropCommand( RTL_CONSTASCII_USTRINGPARAM( "Command" )),
89 m_aPropModule( RTL_CONSTASCII_USTRINGPARAM( "Module" )),
90 m_aPropController( RTL_CONSTASCII_USTRINGPARAM( "Controller" )),
91 m_aPropValue( RTL_CONSTASCII_USTRINGPARAM( "Value" )),
93 m_xServiceManager( rServiceManager
),
94 m_bConfigAccessInitialized( sal_False
),
95 m_bAskValue(_bAskValue
)
97 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory" );
98 m_xConfigProvider
= Reference
< XMultiServiceFactory
>( rServiceManager
->createInstance( SERVICENAME_CFGPROVIDER
),UNO_QUERY
);
101 ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
104 ResetableGuard
aLock( m_aLock
);
106 Reference
< XContainer
> xContainer( m_xConfigAccess
, UNO_QUERY
);
107 if ( xContainer
.is() )
108 xContainer
->removeContainerListener( this );
111 rtl::OUString
ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const rtl::OUString
& rCommandURL
, const rtl::OUString
& rModule
) const
113 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getServiceFromCommandModule" );
115 ResetableGuard
aLock( m_aLock
);
116 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
118 if ( pIter
!= m_aMenuControllerMap
.end() )
119 return pIter
->second
.m_aImplementationName
;
120 else if ( rModule
.getLength() )
122 // Try to detect if we have a generic popup menu controller
123 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rtl::OUString() ));
125 if ( pIter
!= m_aMenuControllerMap
.end() )
126 return pIter
->second
.m_aImplementationName
;
129 return rtl::OUString();
131 rtl::OUString
ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const rtl::OUString
& rCommandURL
, const rtl::OUString
& rModule
) const
133 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::getValueFromCommandModule" );
135 ResetableGuard
aLock( m_aLock
);
137 MenuControllerMap::const_iterator pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rModule
));
139 if ( pIter
!= m_aMenuControllerMap
.end() )
140 return pIter
->second
.m_aValue
;
141 else if ( rModule
.getLength() )
143 // Try to detect if we have a generic popup menu controller
144 pIter
= m_aMenuControllerMap
.find( getHashKeyFromStrings( rCommandURL
, rtl::OUString() ));
146 if ( pIter
!= m_aMenuControllerMap
.end() )
147 return pIter
->second
.m_aValue
;
150 return rtl::OUString();
154 void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
155 const rtl::OUString
& rCommandURL
,
156 const rtl::OUString
& rModule
,
157 const rtl::OUString
& rServiceSpecifier
)
159 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::addServiceToCommandModule" );
161 ResetableGuard
aLock( m_aLock
);
163 rtl::OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
164 m_aMenuControllerMap
.insert( MenuControllerMap::value_type( aHashKey
,ControllerInfo(rServiceSpecifier
,::rtl::OUString()) ));
167 void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
168 const rtl::OUString
& rCommandURL
,
169 const rtl::OUString
& rModule
)
171 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule" );
173 ResetableGuard
aLock( m_aLock
);
175 rtl::OUString aHashKey
= getHashKeyFromStrings( rCommandURL
, rModule
);
176 m_aMenuControllerMap
.erase( aHashKey
);
179 // container.XContainerListener
180 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent
& aEvent
) throw(RuntimeException
)
182 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementInserted" );
183 rtl::OUString aCommand
;
184 rtl::OUString aModule
;
185 rtl::OUString aService
;
186 rtl::OUString aValue
;
189 ResetableGuard
aLock( m_aLock
);
191 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
193 // Create hash key from command and module as they are together a primary key to
194 // the UNO service that implements the popup menu controller.
195 rtl::OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
196 ControllerInfo
& rControllerInfo
= m_aMenuControllerMap
[ aHashKey
];
197 rControllerInfo
.m_aImplementationName
= aService
;
198 rControllerInfo
.m_aValue
= aValue
;
202 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent
& aEvent
) throw(RuntimeException
)
204 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementRemoved" );
205 rtl::OUString aCommand
;
206 rtl::OUString aModule
;
207 rtl::OUString aService
;
208 rtl::OUString aValue
;
211 ResetableGuard
aLock( m_aLock
);
213 if ( impl_getElementProps( aEvent
.Element
, aCommand
, aModule
, aService
, aValue
))
215 // Create hash key from command and module as they are together a primary key to
216 // the UNO service that implements the popup menu controller.
217 rtl::OUString
aHashKey( getHashKeyFromStrings( aCommand
, aModule
));
218 m_aMenuControllerMap
.erase( aHashKey
);
222 void SAL_CALL
ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent
& aEvent
) throw(RuntimeException
)
224 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::elementReplaced" );
225 elementInserted(aEvent
);
228 // lang.XEventListener
229 void SAL_CALL
ConfigurationAccess_ControllerFactory::disposing( const EventObject
& ) throw(RuntimeException
)
231 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::disposing" );
233 // remove our reference to the config access
234 ResetableGuard
aLock( m_aLock
);
235 m_xConfigAccess
.clear();
238 void ConfigurationAccess_ControllerFactory::readConfigurationData()
240 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::readConfigurationData" );
242 ResetableGuard
aLock( m_aLock
);
244 if ( !m_bConfigAccessInitialized
)
246 Sequence
< Any
> aArgs( 1 );
247 PropertyValue aPropValue
;
249 aPropValue
.Name
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ));
250 aPropValue
.Value
<<= m_sRoot
;
251 aArgs
[0] <<= aPropValue
;
255 m_xConfigAccess
= Reference
< XNameAccess
>( m_xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGREADACCESS
,aArgs
), UNO_QUERY
);
257 catch ( WrappedTargetException
& )
261 m_bConfigAccessInitialized
= sal_True
;
264 if ( m_xConfigAccess
.is() )
266 // Read and update configuration data
267 updateConfigurationData();
269 uno::Reference
< container::XContainer
> xContainer( m_xConfigAccess
, uno::UNO_QUERY
);
273 if ( xContainer
.is() )
274 xContainer
->addContainerListener( this );
278 void ConfigurationAccess_ControllerFactory::updateConfigurationData()
280 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::updateConfigurationData" );
282 ResetableGuard
aLock( m_aLock
);
283 if ( m_xConfigAccess
.is() )
285 Sequence
< rtl::OUString
> aPopupMenuControllers
= m_xConfigAccess
->getElementNames();
287 rtl::OUString aCommand
;
288 rtl::OUString aModule
;
289 rtl::OUString aService
;
290 rtl::OUString aHashKey
;
291 rtl::OUString aValue
;
293 m_aMenuControllerMap
.clear();
294 for ( sal_Int32 i
= 0; i
< aPopupMenuControllers
.getLength(); i
++ )
298 if ( impl_getElementProps( m_xConfigAccess
->getByName( aPopupMenuControllers
[i
] ), aCommand
, aModule
, aService
,aValue
))
300 // Create hash key from command and module as they are together a primary key to
301 // the UNO service that implements the popup menu controller.
302 aHashKey
= getHashKeyFromStrings( aCommand
, aModule
);
303 m_aMenuControllerMap
.insert( MenuControllerMap::value_type( aHashKey
, ControllerInfo(aService
,aValue
) ));
306 catch ( NoSuchElementException
& )
309 catch ( WrappedTargetException
& )
316 sal_Bool
ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any
& aElement
, rtl::OUString
& aCommand
, rtl::OUString
& aModule
, rtl::OUString
& aServiceSpecifier
,rtl::OUString
& aValue
) const
318 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger
, "framework", "Ocke.Janssen@sun.com", "ConfigurationAccess_ControllerFactory::impl_getElementProps" );
319 Reference
< XPropertySet
> xPropertySet
;
320 aElement
>>= xPropertySet
;
322 if ( xPropertySet
.is() )
326 xPropertySet
->getPropertyValue( m_aPropCommand
) >>= aCommand
;
327 xPropertySet
->getPropertyValue( m_aPropModule
) >>= aModule
;
328 xPropertySet
->getPropertyValue( m_aPropController
) >>= aServiceSpecifier
;
330 xPropertySet
->getPropertyValue( m_aPropValue
) >>= aValue
;
332 catch ( com::sun::star::beans::UnknownPropertyException
& )
336 catch ( com::sun::star::lang::WrappedTargetException
& )
344 } // namespace framework