LCOV - code coverage report
Current view: top level - framework/source/uifactory - factoryconfiguration.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 81 130 62.3 %
Date: 2015-06-13 12:38:46 Functions: 10 15 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "uifactory/factoryconfiguration.hxx"
      21             : #include "services.h"
      22             : 
      23             : #include "helper/mischelper.hxx"
      24             : 
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <com/sun/star/beans/XPropertySet.hpp>
      27             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      28             : #include <com/sun/star/container/XNameAccess.hpp>
      29             : #include <com/sun/star/container/XNameContainer.hpp>
      30             : #include <com/sun/star/container/XContainer.hpp>
      31             : 
      32             : #include <rtl/ustrbuf.hxx>
      33             : #include <cppuhelper/weak.hxx>
      34             : 
      35             : //  Defines
      36             : 
      37             : using namespace com::sun::star;
      38             : using namespace com::sun::star::uno;
      39             : using namespace com::sun::star::lang;
      40             : using namespace com::sun::star::beans;
      41             : using namespace com::sun::star::container;
      42             : 
      43             : //  Namespace
      44             : 
      45             : namespace framework
      46             : {
      47      393850 : OUString getHashKeyFromStrings( const OUString& aCommandURL, const OUString& aModuleName )
      48             : {
      49      393850 :     OUStringBuffer aKey( aCommandURL );
      50      393850 :     aKey.appendAscii( "-" );
      51      393850 :     aKey.append( aModuleName );
      52      393850 :     return aKey.makeStringAndClear();
      53             : }
      54             : 
      55             : //  XInterface, XTypeProvider
      56             : 
      57         320 : ConfigurationAccess_ControllerFactory::ConfigurationAccess_ControllerFactory( const Reference< XComponentContext >& rxContext, const OUString& _sRoot,bool _bAskValue ) :
      58             :     m_aPropCommand( "Command" ),
      59             :     m_aPropModule( "Module" ),
      60             :     m_aPropController( "Controller" ),
      61             :     m_aPropValue( "Value" ),
      62             :     m_sRoot(_sRoot),
      63             :     m_bConfigAccessInitialized( false ),
      64         320 :     m_bAskValue(_bAskValue)
      65             : {
      66         320 :     m_xConfigProvider = configuration::theDefaultProvider::get( rxContext );
      67         320 : }
      68             : 
      69         948 : ConfigurationAccess_ControllerFactory::~ConfigurationAccess_ControllerFactory()
      70             : {
      71         316 :     osl::MutexGuard g(m_mutex);
      72             : 
      73         632 :     Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
      74         316 :     if ( xContainer.is() )
      75         471 :         xContainer->removeContainerListener(m_xConfigAccessListener);
      76         632 : }
      77             : 
      78      308684 : OUString ConfigurationAccess_ControllerFactory::getServiceFromCommandModule( const OUString& rCommandURL, const OUString& rModule ) const
      79             : {
      80      308684 :     osl::MutexGuard g(m_mutex);
      81      308684 :     MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
      82             : 
      83      308684 :     if ( pIter != m_aMenuControllerMap.end() )
      84        5668 :         return pIter->second.m_aImplementationName;
      85      303016 :     else if ( !rModule.isEmpty() )
      86             :     {
      87             :         // Try to detect if we have a generic popup menu controller
      88       82255 :         pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, OUString() ));
      89             : 
      90       82255 :         if ( pIter != m_aMenuControllerMap.end() )
      91        8115 :             return pIter->second.m_aImplementationName;
      92             :     }
      93             : 
      94      294901 :     return OUString();
      95             : }
      96           0 : OUString ConfigurationAccess_ControllerFactory::getValueFromCommandModule( const OUString& rCommandURL, const OUString& rModule ) const
      97             : {
      98           0 :     osl::MutexGuard g(m_mutex);
      99             : 
     100           0 :     MenuControllerMap::const_iterator pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, rModule ));
     101             : 
     102           0 :     if ( pIter != m_aMenuControllerMap.end() )
     103           0 :         return pIter->second.m_aValue;
     104           0 :     else if ( !rModule.isEmpty() )
     105             :     {
     106             :         // Try to detect if we have a generic popup menu controller
     107           0 :         pIter = m_aMenuControllerMap.find( getHashKeyFromStrings( rCommandURL, OUString() ));
     108             : 
     109           0 :         if ( pIter != m_aMenuControllerMap.end() )
     110           0 :             return pIter->second.m_aValue;
     111             :     }
     112             : 
     113           0 :     return OUString();
     114             : }
     115             : 
     116           2 : void ConfigurationAccess_ControllerFactory::addServiceToCommandModule(
     117             :     const OUString& rCommandURL,
     118             :     const OUString& rModule,
     119             :     const OUString& rServiceSpecifier )
     120             : {
     121           2 :     osl::MutexGuard g(m_mutex);
     122             : 
     123           4 :     OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
     124           4 :     m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey,ControllerInfo(rServiceSpecifier,OUString()) ));
     125           2 : }
     126             : 
     127           1 : void ConfigurationAccess_ControllerFactory::removeServiceFromCommandModule(
     128             :     const OUString& rCommandURL,
     129             :     const OUString& rModule )
     130             : {
     131           1 :     osl::MutexGuard g(m_mutex);
     132             : 
     133           2 :     OUString aHashKey = getHashKeyFromStrings( rCommandURL, rModule );
     134           2 :     m_aMenuControllerMap.erase( aHashKey );
     135           1 : }
     136             : 
     137             : // container.XContainerListener
     138           0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementInserted( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception)
     139             : {
     140           0 :     OUString   aCommand;
     141           0 :     OUString   aModule;
     142           0 :     OUString   aService;
     143           0 :     OUString   aValue;
     144             : 
     145           0 :     osl::MutexGuard g(m_mutex);
     146             : 
     147           0 :     if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
     148             :     {
     149             :         // Create hash key from command and module as they are together a primary key to
     150             :         // the UNO service that implements the popup menu controller.
     151           0 :         OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
     152           0 :         ControllerInfo& rControllerInfo = m_aMenuControllerMap[ aHashKey ];
     153           0 :         rControllerInfo.m_aImplementationName = aService;
     154           0 :         rControllerInfo.m_aValue = aValue;
     155           0 :     }
     156           0 : }
     157             : 
     158           0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementRemoved ( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception)
     159             : {
     160           0 :     OUString   aCommand;
     161           0 :     OUString   aModule;
     162           0 :     OUString   aService;
     163           0 :     OUString   aValue;
     164             : 
     165           0 :     osl::MutexGuard g(m_mutex);
     166             : 
     167           0 :     if ( impl_getElementProps( aEvent.Element, aCommand, aModule, aService, aValue ))
     168             :     {
     169             :         // Create hash key from command and module as they are together a primary key to
     170             :         // the UNO service that implements the popup menu controller.
     171           0 :         OUString aHashKey( getHashKeyFromStrings( aCommand, aModule ));
     172           0 :         m_aMenuControllerMap.erase( aHashKey );
     173           0 :     }
     174           0 : }
     175             : 
     176           0 : void SAL_CALL ConfigurationAccess_ControllerFactory::elementReplaced( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception)
     177             : {
     178           0 :     elementInserted(aEvent);
     179           0 : }
     180             : 
     181             : // lang.XEventListener
     182           0 : void SAL_CALL ConfigurationAccess_ControllerFactory::disposing( const EventObject& ) throw(RuntimeException, std::exception)
     183             : {
     184             :     // remove our reference to the config access
     185           0 :     osl::MutexGuard g(m_mutex);
     186           0 :     m_xConfigAccess.clear();
     187           0 : }
     188             : 
     189         159 : void ConfigurationAccess_ControllerFactory::readConfigurationData()
     190             : {
     191             :     // SAFE
     192         159 :     osl::ClearableMutexGuard aLock( m_mutex );
     193             : 
     194         159 :     if ( !m_bConfigAccessInitialized )
     195             :     {
     196         159 :         Sequence< Any > aArgs( 1 );
     197         318 :         PropertyValue   aPropValue;
     198             : 
     199         159 :         aPropValue.Name  = "nodepath";
     200         159 :         aPropValue.Value <<= m_sRoot;
     201         159 :         aArgs[0] <<= aPropValue;
     202             : 
     203             :         try
     204             :         {
     205         159 :             m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS,aArgs ), UNO_QUERY );
     206             :         }
     207           0 :         catch ( const WrappedTargetException& )
     208             :         {
     209             :         }
     210             : 
     211         318 :         m_bConfigAccessInitialized = true;
     212             :     }
     213             : 
     214         159 :     if ( m_xConfigAccess.is() )
     215             :     {
     216             :         // Read and update configuration data
     217         159 :         updateConfigurationData();
     218             : 
     219         159 :         uno::Reference< container::XContainer > xContainer( m_xConfigAccess, uno::UNO_QUERY );
     220             :         // UNSAFE
     221         159 :         aLock.clear();
     222             : 
     223         159 :         if ( xContainer.is() )
     224             :         {
     225         159 :             m_xConfigAccessListener = new WeakContainerListener(this);
     226         159 :             xContainer->addContainerListener(m_xConfigAccessListener);
     227         159 :         }
     228         159 :     }
     229         159 : }
     230             : 
     231         159 : void ConfigurationAccess_ControllerFactory::updateConfigurationData()
     232             : {
     233         159 :     osl::MutexGuard g(m_mutex);
     234         159 :     if ( m_xConfigAccess.is() )
     235             :     {
     236         159 :         Sequence< OUString >   aPopupMenuControllers = m_xConfigAccess->getElementNames();
     237             : 
     238         318 :         OUString aCommand;
     239         318 :         OUString aModule;
     240         318 :         OUString aService;
     241         318 :         OUString aHashKey;
     242         318 :         OUString aValue;
     243             : 
     244         159 :         m_aMenuControllerMap.clear();
     245        3067 :         for ( sal_Int32 i = 0; i < aPopupMenuControllers.getLength(); i++ )
     246             :         {
     247             :             try
     248             :             {
     249        2908 :                 if ( impl_getElementProps( m_xConfigAccess->getByName( aPopupMenuControllers[i] ), aCommand, aModule, aService,aValue ))
     250             :                 {
     251             :                     // Create hash key from command and module as they are together a primary key to
     252             :                     // the UNO service that implements the popup menu controller.
     253        2908 :                     aHashKey = getHashKeyFromStrings( aCommand, aModule );
     254        2908 :                     m_aMenuControllerMap.insert( MenuControllerMap::value_type( aHashKey, ControllerInfo(aService,aValue) ));
     255             :                 }
     256             :             }
     257           0 :             catch ( const NoSuchElementException& )
     258             :             {
     259             :             }
     260           0 :             catch ( const WrappedTargetException& )
     261             :             {
     262             :             }
     263         159 :         }
     264         159 :     }
     265         159 : }
     266             : 
     267        2908 : bool ConfigurationAccess_ControllerFactory::impl_getElementProps( const Any& aElement, OUString& aCommand, OUString& aModule, OUString& aServiceSpecifier,OUString& aValue  ) const
     268             : {
     269        2908 :     Reference< XPropertySet > xPropertySet;
     270        2908 :     aElement >>= xPropertySet;
     271             : 
     272        2908 :     if ( xPropertySet.is() )
     273             :     {
     274             :         try
     275             :         {
     276        2908 :             xPropertySet->getPropertyValue( m_aPropCommand ) >>= aCommand;
     277        2908 :             xPropertySet->getPropertyValue( m_aPropModule ) >>= aModule;
     278        2908 :             xPropertySet->getPropertyValue( m_aPropController ) >>= aServiceSpecifier;
     279        2908 :             if ( m_bAskValue )
     280           0 :                 xPropertySet->getPropertyValue( m_aPropValue ) >>= aValue;
     281             :         }
     282           0 :         catch ( const com::sun::star::beans::UnknownPropertyException& )
     283             :         {
     284           0 :             return false;
     285             :         }
     286           0 :         catch ( const com::sun::star::lang::WrappedTargetException& )
     287             :         {
     288           0 :             return false;
     289             :         }
     290             :     }
     291             : 
     292        2908 :     return true;
     293             : }
     294             : } // namespace framework
     295             : 
     296             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11