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