LCOV - code coverage report
Current view: top level - framework/source/uielement - uicommanddescription.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 227 300 75.7 %
Date: 2015-06-13 12:38:46 Functions: 30 39 76.9 %
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 "uielement/uicommanddescription.hxx"
      21             : 
      22             : #include "properties.h"
      23             : 
      24             : #include "helper/mischelper.hxx"
      25             : 
      26             : #include <com/sun/star/beans/PropertyValue.hpp>
      27             : #include <com/sun/star/beans/XPropertySet.hpp>
      28             : #include <com/sun/star/frame/ModuleManager.hpp>
      29             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      30             : #include <com/sun/star/container/XNameAccess.hpp>
      31             : #include <com/sun/star/container/XNameContainer.hpp>
      32             : #include <com/sun/star/container/XContainer.hpp>
      33             : 
      34             : #include <rtl/ustrbuf.hxx>
      35             : #include <cppuhelper/implbase2.hxx>
      36             : #include <unotools/configmgr.hxx>
      37             : 
      38             : #include <vcl/mnemonic.hxx>
      39             : #include <comphelper/sequence.hxx>
      40             : #include <comphelper/string.hxx>
      41             : 
      42             : using namespace com::sun::star::uno;
      43             : using namespace com::sun::star::lang;
      44             : using namespace com::sun::star::beans;
      45             : using namespace com::sun::star::configuration;
      46             : using namespace com::sun::star::container;
      47             : using namespace ::com::sun::star::frame;
      48             : 
      49             : //  Namespace
      50             : 
      51             : struct ModuleToCommands
      52             : {
      53             :     const char* pModuleId;
      54             :     const char* pCommands;
      55             : };
      56             : 
      57             : static const char CONFIGURATION_ROOT_ACCESS[]           = "/org.openoffice.Office.UI.";
      58             : static const char CONFIGURATION_CMD_ELEMENT_ACCESS[]    = "/UserInterface/Commands";
      59             : static const char CONFIGURATION_POP_ELEMENT_ACCESS[]    = "/UserInterface/Popups";
      60             : static const char CONFIGURATION_PROPERTY_LABEL[]        = "Label";
      61             : static const char CONFIGURATION_PROPERTY_CONTEXT_LABEL[] = "ContextLabel";
      62             : 
      63             : // Property names of the resulting Property Set
      64             : static const char PROPSET_LABEL[]                       = "Label";
      65             : static const char PROPSET_NAME[]                        = "Name";
      66             : static const char PROPSET_POPUP[]                       = "Popup";
      67             : static const char PROPSET_PROPERTIES[]                  = "Properties";
      68             : 
      69             : // Special resource URLs to retrieve additional information
      70             : static const char PRIVATE_RESOURCE_URL[]                = "private:";
      71             : 
      72             : const sal_Int32   COMMAND_PROPERTY_IMAGE                = 1;
      73             : const sal_Int32   COMMAND_PROPERTY_ROTATE               = 2;
      74             : const sal_Int32   COMMAND_PROPERTY_MIRROR               = 4;
      75             : 
      76             : namespace framework
      77             : {
      78             : 
      79             : //  Configuration access class for PopupMenuControllerFactory implementation
      80             : 
      81             : class ConfigurationAccess_UICommand : // Order is necessary for right initialization!
      82             :                                         public  ::cppu::WeakImplHelper2<XNameAccess,XContainerListener>
      83             : {
      84             :     osl::Mutex m_aMutex;
      85             :     public:
      86             :                                   ConfigurationAccess_UICommand( const OUString& aModuleName, const Reference< XNameAccess >& xGenericUICommands, const Reference< XComponentContext >& rxContext );
      87             :         virtual                   ~ConfigurationAccess_UICommand();
      88             : 
      89             :         // XNameAccess
      90             :         virtual ::com::sun::star::uno::Any SAL_CALL getByName( const OUString& aName )
      91             :             throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      92             : 
      93             :         virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getElementNames()
      94             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      95             : 
      96             :         virtual sal_Bool SAL_CALL hasByName( const OUString& aName )
      97             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      98             : 
      99             :         // XElementAccess
     100             :         virtual ::com::sun::star::uno::Type SAL_CALL getElementType()
     101             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     102             : 
     103             :         virtual sal_Bool SAL_CALL hasElements()
     104             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     105             : 
     106             :         // container.XContainerListener
     107             :         virtual void SAL_CALL     elementInserted( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     108             :         virtual void SAL_CALL     elementRemoved ( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     109             :         virtual void SAL_CALL     elementReplaced( const ContainerEvent& aEvent ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     110             : 
     111             :         // lang.XEventListener
     112             :         virtual void SAL_CALL disposing( const EventObject& aEvent ) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     113             : 
     114             :     protected:
     115             :         ::com::sun::star::uno::Any SAL_CALL getByNameImpl( const OUString& aName );
     116             : 
     117      281038 :         struct CmdToInfoMap
     118             :         {
     119       56388 :             CmdToInfoMap() : bPopup( false ),
     120             :                              bCommandNameCreated( false ),
     121       56388 :                              nProperties( 0 ) {}
     122             : 
     123             :             OUString       aLabel;
     124             :             OUString       aContextLabel;
     125             :             OUString       aCommandName;
     126             :             bool                bPopup : 1,
     127             :                                 bCommandNameCreated : 1;
     128             :             sal_Int32           nProperties;
     129             :         };
     130             : 
     131             :         Any                       getSequenceFromCache( const OUString& aCommandURL );
     132             :         Any                       getInfoFromCommand( const OUString& rCommandURL );
     133             :         void                      fillInfoFromResult( CmdToInfoMap& rCmdInfo, const OUString& aLabel );
     134             :         Sequence< OUString > getAllCommands();
     135             :         bool                  fillCache();
     136             :         bool                  addGenericInfoToCache();
     137             :         void                      impl_fill(const Reference< XNameAccess >& _xConfigAccess,bool _bPopup,
     138             :                                                 std::vector< OUString >& aImageCommandVector,
     139             :                                                 std::vector< OUString >& aImageRotateVector,
     140             :                                                 std::vector< OUString >& aImageMirrorVector);
     141             : 
     142             :     private:
     143             :         typedef std::unordered_map< OUString,
     144             :                                     CmdToInfoMap,
     145             :                                     OUStringHash,
     146             :                                     std::equal_to< OUString > > CommandToInfoCache;
     147             : 
     148             :         bool initializeConfigAccess();
     149             : 
     150             :         OUString                     m_aConfigCmdAccess;
     151             :         OUString                     m_aConfigPopupAccess;
     152             :         OUString                     m_aPropUILabel;
     153             :         OUString                     m_aPropUIContextLabel;
     154             :         OUString                     m_aPropLabel;
     155             :         OUString                     m_aPropName;
     156             :         OUString                     m_aPropPopup;
     157             :         OUString                     m_aPropProperties;
     158             :         OUString                     m_aPrivateResourceURL;
     159             :         Reference< XNameAccess >          m_xGenericUICommands;
     160             :         Reference< XMultiServiceFactory > m_xConfigProvider;
     161             :         Reference< XNameAccess >          m_xConfigAccess;
     162             :         Reference< XContainerListener >   m_xConfigListener;
     163             :         Reference< XNameAccess >          m_xConfigAccessPopups;
     164             :         Reference< XContainerListener >   m_xConfigAccessListener;
     165             :         Sequence< OUString >         m_aCommandImageList;
     166             :         Sequence< OUString >         m_aCommandRotateImageList;
     167             :         Sequence< OUString >         m_aCommandMirrorImageList;
     168             :         CommandToInfoCache                m_aCmdInfoCache;
     169             :         bool                          m_bConfigAccessInitialized;
     170             :         bool                          m_bCacheFilled;
     171             :         bool                          m_bGenericDataRetrieved;
     172             : };
     173             : 
     174             : //  XInterface, XTypeProvider
     175             : 
     176         109 : ConfigurationAccess_UICommand::ConfigurationAccess_UICommand( const OUString& aModuleName, const Reference< XNameAccess >& rGenericUICommands, const Reference< XComponentContext>& rxContext ) :
     177             :     m_aConfigCmdAccess( CONFIGURATION_ROOT_ACCESS ),
     178             :     m_aConfigPopupAccess( CONFIGURATION_ROOT_ACCESS ),
     179             :     m_aPropUILabel( CONFIGURATION_PROPERTY_LABEL ),
     180             :     m_aPropUIContextLabel( CONFIGURATION_PROPERTY_CONTEXT_LABEL ),
     181             :     m_aPropLabel( PROPSET_LABEL ),
     182             :     m_aPropName( PROPSET_NAME ),
     183             :     m_aPropPopup( PROPSET_POPUP ),
     184             :     m_aPropProperties( PROPSET_PROPERTIES ),
     185             :     m_aPrivateResourceURL( PRIVATE_RESOURCE_URL ),
     186             :     m_xGenericUICommands( rGenericUICommands ),
     187             :     m_bConfigAccessInitialized( false ),
     188             :     m_bCacheFilled( false ),
     189         109 :     m_bGenericDataRetrieved( false )
     190             : {
     191             :     // Create configuration hierarchical access name
     192         109 :     m_aConfigCmdAccess += aModuleName;
     193         109 :     m_aConfigCmdAccess += CONFIGURATION_CMD_ELEMENT_ACCESS;
     194             : 
     195         109 :     m_xConfigProvider = theDefaultProvider::get( rxContext );
     196             : 
     197         109 :     m_aConfigPopupAccess += aModuleName;
     198         109 :     m_aConfigPopupAccess += CONFIGURATION_POP_ELEMENT_ACCESS;
     199         109 : }
     200             : 
     201         321 : ConfigurationAccess_UICommand::~ConfigurationAccess_UICommand()
     202             : {
     203             :     // SAFE
     204         107 :     osl::MutexGuard g(m_aMutex);
     205         214 :     Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
     206         107 :     if ( xContainer.is() )
     207         104 :         xContainer->removeContainerListener(m_xConfigListener);
     208         107 :     xContainer = Reference< XContainer >( m_xConfigAccessPopups, UNO_QUERY );
     209         107 :     if ( xContainer.is() )
     210         211 :         xContainer->removeContainerListener(m_xConfigAccessListener);
     211         214 : }
     212             : 
     213             : // XNameAccess
     214      382810 : Any SAL_CALL ConfigurationAccess_UICommand::getByNameImpl( const OUString& rCommandURL )
     215             : {
     216             :     static sal_Int32 nRequests  = 0;
     217             : 
     218      382810 :     osl::MutexGuard g(m_aMutex);
     219      382810 :     if ( !m_bConfigAccessInitialized )
     220             :     {
     221         106 :         initializeConfigAccess();
     222         106 :         m_bConfigAccessInitialized = true;
     223         106 :         fillCache();
     224             :     }
     225             : 
     226      382810 :     if ( rCommandURL.startsWith( m_aPrivateResourceURL ) )
     227             :     {
     228             :         // special keys to retrieve information about a set of commands
     229             :         // SAFE
     230        4112 :         addGenericInfoToCache();
     231             : 
     232        4112 :         if ( rCommandURL.equalsIgnoreAsciiCase( UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDIMAGELIST ))
     233         170 :             return makeAny( m_aCommandImageList );
     234        3942 :         else if ( rCommandURL.equalsIgnoreAsciiCase( UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDROTATEIMAGELIST ))
     235        1971 :             return makeAny( m_aCommandRotateImageList );
     236        1971 :         else if ( rCommandURL.equalsIgnoreAsciiCase( UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDMIRRORIMAGELIST ))
     237        1971 :             return makeAny( m_aCommandMirrorImageList );
     238             :         else
     239           0 :             return Any();
     240             :     }
     241             :     else
     242             :     {
     243             :         // SAFE
     244      378698 :         ++nRequests;
     245      378698 :         return getInfoFromCommand( rCommandURL );
     246      382810 :     }
     247             : }
     248             : 
     249      257533 : Any SAL_CALL ConfigurationAccess_UICommand::getByName( const OUString& rCommandURL )
     250             : throw ( NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
     251             : {
     252      257533 :     Any aRet( getByNameImpl( rCommandURL ) );
     253      257533 :     if( !aRet.hasValue() )
     254        2714 :         throw NoSuchElementException();
     255             : 
     256      254819 :     return aRet;
     257             : }
     258             : 
     259           0 : Sequence< OUString > SAL_CALL ConfigurationAccess_UICommand::getElementNames()
     260             : throw ( RuntimeException, std::exception )
     261             : {
     262           0 :     return getAllCommands();
     263             : }
     264             : 
     265      125277 : sal_Bool SAL_CALL ConfigurationAccess_UICommand::hasByName( const OUString& rCommandURL )
     266             : throw (::com::sun::star::uno::RuntimeException, std::exception)
     267             : {
     268      125277 :     return getByNameImpl( rCommandURL ).hasValue();
     269             : }
     270             : 
     271             : // XElementAccess
     272           0 : Type SAL_CALL ConfigurationAccess_UICommand::getElementType()
     273             : throw ( RuntimeException, std::exception )
     274             : {
     275           0 :     return( cppu::UnoType<Sequence< PropertyValue >>::get() );
     276             : }
     277             : 
     278           0 : sal_Bool SAL_CALL ConfigurationAccess_UICommand::hasElements()
     279             : throw ( RuntimeException, std::exception )
     280             : {
     281             :     // There must are global commands!
     282           0 :     return sal_True;
     283             : }
     284             : 
     285        5486 : void ConfigurationAccess_UICommand::fillInfoFromResult( CmdToInfoMap& rCmdInfo, const OUString& aLabel )
     286             : {
     287        5486 :     OUString aStr(aLabel.replaceAll("%PRODUCTNAME", utl::ConfigManager::getProductName()));
     288        5486 :     rCmdInfo.aLabel = aStr;
     289        5486 :     aStr = comphelper::string::stripEnd(aStr, '.'); // Remove "..." from string
     290        5486 :     rCmdInfo.aCommandName = MnemonicGenerator::EraseAllMnemonicChars(aStr);
     291        5486 :     rCmdInfo.bCommandNameCreated = true;
     292        5486 : }
     293             : 
     294      378698 : Any ConfigurationAccess_UICommand::getSequenceFromCache( const OUString& aCommandURL )
     295             : {
     296      378698 :     CommandToInfoCache::iterator pIter = m_aCmdInfoCache.find( aCommandURL );
     297      378698 :     if ( pIter != m_aCmdInfoCache.end() )
     298             :     {
     299      261285 :         if ( !pIter->second.bCommandNameCreated )
     300        5486 :             fillInfoFromResult( pIter->second, pIter->second.aLabel );
     301             : 
     302      261285 :         Sequence< PropertyValue > aPropSeq( 4 );
     303      261285 :         aPropSeq[0].Name  = m_aPropLabel;
     304      783855 :         aPropSeq[0].Value = !pIter->second.aContextLabel.isEmpty() ?
     305      522570 :                 makeAny( pIter->second.aContextLabel ): makeAny( pIter->second.aLabel );
     306      261285 :         aPropSeq[1].Name  = m_aPropName;
     307      261285 :         aPropSeq[1].Value <<= pIter->second.aCommandName;
     308      261285 :         aPropSeq[2].Name  = m_aPropPopup;
     309      261285 :         aPropSeq[2].Value <<= pIter->second.bPopup;
     310      261285 :         aPropSeq[3].Name  = m_aPropProperties;
     311      261285 :         aPropSeq[3].Value <<= pIter->second.nProperties;
     312      261285 :         return makeAny( aPropSeq );
     313             :     }
     314             : 
     315      117413 :     return Any();
     316             : }
     317         212 : void ConfigurationAccess_UICommand::impl_fill(const Reference< XNameAccess >& _xConfigAccess,bool _bPopup,
     318             :                                                 std::vector< OUString >& aImageCommandVector,
     319             :                                                 std::vector< OUString >& aImageRotateVector,
     320             :                                                 std::vector< OUString >& aImageMirrorVector)
     321             : {
     322         212 :     if ( _xConfigAccess.is() )
     323             :     {
     324         212 :         Sequence< OUString> aNameSeq = _xConfigAccess->getElementNames();
     325         212 :         const sal_Int32 nCount = aNameSeq.getLength();
     326       56600 :         for ( sal_Int32 i = 0; i < nCount; i++ )
     327             :         {
     328             :             try
     329             :             {
     330       56388 :                 Reference< XNameAccess > xNameAccess(_xConfigAccess->getByName( aNameSeq[i] ),UNO_QUERY);
     331       56388 :                 if ( xNameAccess.is() )
     332             :                 {
     333       56388 :                     CmdToInfoMap aCmdToInfo;
     334             : 
     335       56388 :                     aCmdToInfo.bPopup = _bPopup;
     336       56388 :                     xNameAccess->getByName( m_aPropUILabel )        >>= aCmdToInfo.aLabel;
     337       56388 :                     xNameAccess->getByName( m_aPropUIContextLabel ) >>= aCmdToInfo.aContextLabel;
     338       56388 :                     xNameAccess->getByName( m_aPropProperties )     >>= aCmdToInfo.nProperties;
     339             : 
     340       56388 :                     m_aCmdInfoCache.insert( CommandToInfoCache::value_type( aNameSeq[i], aCmdToInfo ));
     341             : 
     342       56388 :                     if ( aCmdToInfo.nProperties & COMMAND_PROPERTY_IMAGE )
     343       33545 :                         aImageCommandVector.push_back( aNameSeq[i] );
     344       56388 :                     if ( aCmdToInfo.nProperties & COMMAND_PROPERTY_ROTATE )
     345        1147 :                         aImageRotateVector.push_back( aNameSeq[i] );
     346       56388 :                     if ( aCmdToInfo.nProperties & COMMAND_PROPERTY_MIRROR )
     347         912 :                         aImageMirrorVector.push_back( aNameSeq[i] );
     348       56388 :                 }
     349             :             }
     350           0 :             catch (const com::sun::star::lang::WrappedTargetException&)
     351             :             {
     352             :             }
     353           0 :             catch (const com::sun::star::container::NoSuchElementException&)
     354             :             {
     355             :             }
     356         212 :         }
     357             :     }
     358         212 : }
     359         106 : bool ConfigurationAccess_UICommand::fillCache()
     360             : {
     361             : 
     362         106 :     if ( m_bCacheFilled )
     363           0 :         return true;
     364             : 
     365         106 :     std::vector< OUString > aImageCommandVector;
     366         212 :     std::vector< OUString > aImageRotateVector;
     367         212 :     std::vector< OUString > aImageMirrorVector;
     368             : 
     369         106 :     impl_fill(m_xConfigAccess,false,aImageCommandVector,aImageRotateVector,aImageMirrorVector);
     370         106 :     impl_fill(m_xConfigAccessPopups,true,aImageCommandVector,aImageRotateVector,aImageMirrorVector);
     371             :     // Create cached sequences for fast retrieving
     372         106 :     m_aCommandImageList       = comphelper::containerToSequence( aImageCommandVector );
     373         106 :     m_aCommandRotateImageList = comphelper::containerToSequence( aImageRotateVector );
     374         106 :     m_aCommandMirrorImageList = comphelper::containerToSequence( aImageMirrorVector );
     375             : 
     376         106 :     m_bCacheFilled = true;
     377             : 
     378         212 :     return true;
     379             : }
     380             : 
     381        4112 : bool ConfigurationAccess_UICommand::addGenericInfoToCache()
     382             : {
     383        4112 :     if ( m_xGenericUICommands.is() && !m_bGenericDataRetrieved )
     384             :     {
     385          58 :         Sequence< OUString > aCommandNameSeq;
     386             :         try
     387             :         {
     388         174 :             if ( m_xGenericUICommands->getByName(
     389         116 :                     OUString( UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDROTATEIMAGELIST )) >>= aCommandNameSeq )
     390          58 :                 m_aCommandRotateImageList = comphelper::concatSequences< OUString >( m_aCommandRotateImageList, aCommandNameSeq );
     391             :         }
     392           0 :         catch (const RuntimeException&)
     393             :         {
     394           0 :             throw;
     395             :         }
     396           0 :         catch (const Exception&)
     397             :         {
     398             :         }
     399             : 
     400             :         try
     401             :         {
     402         174 :             if ( m_xGenericUICommands->getByName(
     403         116 :                     OUString( UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDMIRRORIMAGELIST )) >>= aCommandNameSeq )
     404          58 :                 m_aCommandMirrorImageList = comphelper::concatSequences< OUString >( m_aCommandMirrorImageList, aCommandNameSeq );
     405             :         }
     406           0 :         catch (const RuntimeException&)
     407             :         {
     408           0 :             throw;
     409             :         }
     410           0 :         catch (const Exception&)
     411             :         {
     412             :         }
     413             : 
     414          58 :         m_bGenericDataRetrieved = true;
     415             :     }
     416             : 
     417        4112 :     return true;
     418             : }
     419             : 
     420      378698 : Any ConfigurationAccess_UICommand::getInfoFromCommand( const OUString& rCommandURL )
     421             : {
     422      378698 :     Any a;
     423             : 
     424             :     try
     425             :     {
     426      378698 :         a = getSequenceFromCache( rCommandURL );
     427      378698 :         if ( !a.hasValue() )
     428             :         {
     429             :             // First try to ask our global commands configuration access. It also caches maybe
     430             :             // we find the entry in its cache first.
     431      117413 :             if ( m_xGenericUICommands.is() && m_xGenericUICommands->hasByName( rCommandURL ) )
     432             :             {
     433             :                 try
     434             :                 {
     435      109101 :                     return m_xGenericUICommands->getByName( rCommandURL );
     436             :                 }
     437           0 :                 catch (const com::sun::star::lang::WrappedTargetException&)
     438             :                 {
     439             :                 }
     440           0 :                 catch (const com::sun::star::container::NoSuchElementException&)
     441             :                 {
     442             :                 }
     443             :             }
     444             :         }
     445             :     }
     446           0 :     catch (const com::sun::star::container::NoSuchElementException&)
     447             :     {
     448             :     }
     449           0 :     catch (const com::sun::star::lang::WrappedTargetException&)
     450             :     {
     451             :     }
     452             : 
     453      269597 :     return a;
     454             : }
     455             : 
     456           0 : Sequence< OUString > ConfigurationAccess_UICommand::getAllCommands()
     457             : {
     458             :     // SAFE
     459           0 :     osl::MutexGuard g(m_aMutex);
     460             : 
     461           0 :     if ( !m_bConfigAccessInitialized )
     462             :     {
     463           0 :         initializeConfigAccess();
     464           0 :         m_bConfigAccessInitialized = true;
     465           0 :         fillCache();
     466             :     }
     467             : 
     468           0 :     if ( m_xConfigAccess.is() )
     469             :     {
     470           0 :         Reference< XNameAccess > xNameAccess;
     471             : 
     472             :         try
     473             :         {
     474           0 :             Sequence< OUString > aNameSeq = m_xConfigAccess->getElementNames();
     475             : 
     476           0 :             if ( m_xGenericUICommands.is() )
     477             :             {
     478             :                 // Create concat list of supported user interface commands of the module
     479           0 :                 Sequence< OUString > aGenericNameSeq = m_xGenericUICommands->getElementNames();
     480           0 :                 sal_uInt32 nCount1 = aNameSeq.getLength();
     481           0 :                 sal_uInt32 nCount2 = aGenericNameSeq.getLength();
     482             : 
     483           0 :                 aNameSeq.realloc( nCount1 + nCount2 );
     484           0 :                 OUString* pNameSeq = aNameSeq.getArray();
     485           0 :                 const OUString* pGenericSeq = aGenericNameSeq.getConstArray();
     486           0 :                 for ( sal_uInt32 i = 0; i < nCount2; i++ )
     487           0 :                     pNameSeq[nCount1+i] = pGenericSeq[i];
     488             :             }
     489             : 
     490           0 :             return aNameSeq;
     491             :         }
     492           0 :         catch (const com::sun::star::container::NoSuchElementException&)
     493             :         {
     494             :         }
     495           0 :         catch (const com::sun::star::lang::WrappedTargetException&)
     496             :         {
     497           0 :         }
     498             :     }
     499             : 
     500           0 :     return Sequence< OUString >();
     501             : }
     502             : 
     503         106 : bool ConfigurationAccess_UICommand::initializeConfigAccess()
     504             : {
     505         106 :     Sequence< Any > aArgs( 1 );
     506         212 :     PropertyValue   aPropValue;
     507             : 
     508             :     try
     509             :     {
     510         106 :         aPropValue.Name  = "nodepath";
     511         106 :         aPropValue.Value <<= m_aConfigCmdAccess;
     512         106 :         aArgs[0] <<= aPropValue;
     513             : 
     514         318 :         m_xConfigAccess = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(
     515         212 :                     "com.sun.star.configuration.ConfigurationAccess", aArgs ),UNO_QUERY );
     516         106 :         if ( m_xConfigAccess.is() )
     517             :         {
     518             :             // Add as container listener
     519         106 :             Reference< XContainer > xContainer( m_xConfigAccess, UNO_QUERY );
     520         106 :             if ( xContainer.is() )
     521             :             {
     522         106 :                 m_xConfigListener = new WeakContainerListener(this);
     523         106 :                 xContainer->addContainerListener(m_xConfigListener);
     524         106 :             }
     525             :         }
     526             : 
     527         106 :         aPropValue.Value <<= m_aConfigPopupAccess;
     528         106 :         aArgs[0] <<= aPropValue;
     529         318 :         m_xConfigAccessPopups = Reference< XNameAccess >( m_xConfigProvider->createInstanceWithArguments(
     530         212 :                     "com.sun.star.configuration.ConfigurationAccess", aArgs ),UNO_QUERY );
     531         106 :         if ( m_xConfigAccessPopups.is() )
     532             :         {
     533             :             // Add as container listener
     534         106 :             Reference< XContainer > xContainer( m_xConfigAccessPopups, UNO_QUERY );
     535         106 :             if ( xContainer.is() )
     536             :             {
     537         106 :                 m_xConfigAccessListener = new WeakContainerListener(this);
     538         106 :                 xContainer->addContainerListener(m_xConfigAccessListener);
     539         106 :             }
     540             :         }
     541             : 
     542         106 :         return true;
     543             :     }
     544           0 :     catch (const WrappedTargetException&)
     545             :     {
     546             :     }
     547           0 :     catch (const Exception&)
     548             :     {
     549             :     }
     550             : 
     551         106 :     return false;
     552             : }
     553             : 
     554             : // container.XContainerListener
     555           0 : void SAL_CALL ConfigurationAccess_UICommand::elementInserted( const ContainerEvent& ) throw(RuntimeException, std::exception)
     556             : {
     557           0 :     osl::MutexGuard g(m_aMutex);
     558           0 :     m_bCacheFilled = false;
     559           0 :     fillCache();
     560           0 : }
     561             : 
     562           0 : void SAL_CALL ConfigurationAccess_UICommand::elementRemoved( const ContainerEvent& ) throw(RuntimeException, std::exception)
     563             : {
     564           0 :     osl::MutexGuard g(m_aMutex);
     565           0 :     m_bCacheFilled = false;
     566           0 :     fillCache();
     567           0 : }
     568             : 
     569           0 : void SAL_CALL ConfigurationAccess_UICommand::elementReplaced( const ContainerEvent& ) throw(RuntimeException, std::exception)
     570             : {
     571           0 :     osl::MutexGuard g(m_aMutex);
     572           0 :     m_bCacheFilled = false;
     573           0 :     fillCache();
     574           0 : }
     575             : 
     576             : // lang.XEventListener
     577           0 : void SAL_CALL ConfigurationAccess_UICommand::disposing( const EventObject& aEvent ) throw(RuntimeException, std::exception)
     578             : {
     579             :     // SAFE
     580             :     // remove our reference to the config access
     581           0 :     osl::MutexGuard g(m_aMutex);
     582             : 
     583           0 :     Reference< XInterface > xIfac1( aEvent.Source, UNO_QUERY );
     584           0 :     Reference< XInterface > xIfac2( m_xConfigAccess, UNO_QUERY );
     585           0 :     if ( xIfac1 == xIfac2 )
     586           0 :         m_xConfigAccess.clear();
     587             :     else
     588             :     {
     589           0 :         xIfac2 = Reference< XInterface >( m_xConfigAccessPopups, UNO_QUERY );
     590           0 :         if ( xIfac1 == xIfac2 )
     591           0 :             m_xConfigAccessPopups.clear();
     592           0 :     }
     593           0 : }
     594             : 
     595          48 : UICommandDescription::UICommandDescription(const Reference< XComponentContext >& rxContext)
     596             :     : UICommandDescription_BASE(m_aMutex)
     597             :     , m_bConfigRead(false)
     598             :     , m_aPrivateResourceURL(PRIVATE_RESOURCE_URL)
     599          48 :     , m_xContext(rxContext)
     600             : {
     601          48 :     Reference< XNameAccess > xEmpty;
     602          96 :     OUString aGenericUICommand( "GenericCommands" );
     603          48 :     m_xGenericUICommands = new ConfigurationAccess_UICommand( aGenericUICommand, xEmpty, m_xContext );
     604             : 
     605          48 :     impl_fillElements("ooSetupFactoryCommandConfigRef");
     606             : 
     607             :     // insert generic commands
     608          48 :     UICommandsHashMap::iterator pIter = m_aUICommandsHashMap.find( aGenericUICommand );
     609          48 :     if ( pIter != m_aUICommandsHashMap.end() )
     610          48 :         pIter->second = m_xGenericUICommands;
     611          48 : }
     612             : 
     613           2 : UICommandDescription::UICommandDescription(const Reference< XComponentContext >& rxContext, bool)
     614             :     : UICommandDescription_BASE(m_aMutex)
     615             :     , m_bConfigRead(false)
     616           2 :     , m_xContext(rxContext)
     617             : {
     618           2 : }
     619             : 
     620         145 : UICommandDescription::~UICommandDescription()
     621             : {
     622          49 :     osl::MutexGuard g(rBHelper.rMutex);
     623          49 :     m_aModuleToCommandFileMap.clear();
     624          49 :     m_aUICommandsHashMap.clear();
     625          49 :     m_xGenericUICommands.clear();
     626          96 : }
     627          50 : void UICommandDescription::impl_fillElements(const sal_Char* _pName)
     628             : {
     629          50 :     m_xModuleManager.set( ModuleManager::create( m_xContext ) );
     630          50 :     Sequence< OUString > aElementNames = m_xModuleManager->getElementNames();
     631         100 :     Sequence< PropertyValue > aSeq;
     632         100 :     OUString                  aModuleIdentifier;
     633             : 
     634        1100 :     for ( sal_Int32 i = 0; i < aElementNames.getLength(); i++ )
     635             :     {
     636        1050 :         aModuleIdentifier = aElementNames[i];
     637        1050 :         if ( m_xModuleManager->getByName( aModuleIdentifier ) >>= aSeq )
     638             :         {
     639        1050 :             OUString aCommandStr;
     640        6132 :             for ( sal_Int32 y = 0; y < aSeq.getLength(); y++ )
     641             :             {
     642        6132 :                 if ( aSeq[y].Name.equalsAscii(_pName) )
     643             :                 {
     644        1050 :                     aSeq[y].Value >>= aCommandStr;
     645        1050 :                     break;
     646             :                 }
     647             :             }
     648             : 
     649             :             // Create first mapping ModuleIdentifier ==> Command File
     650        1050 :             m_aModuleToCommandFileMap.insert( ModuleToCommandFileMap::value_type( aModuleIdentifier, aCommandStr ));
     651             : 
     652             :             // Create second mapping Command File ==> commands instance
     653        1050 :             UICommandsHashMap::iterator pIter = m_aUICommandsHashMap.find( aCommandStr );
     654        1050 :             if ( pIter == m_aUICommandsHashMap.end() )
     655         434 :                 m_aUICommandsHashMap.insert( UICommandsHashMap::value_type( aCommandStr, Reference< XNameAccess >() ));
     656             :         }
     657          50 :     } // for ( sal_Int32 i = 0; i < aElementNames.getLength(); i++ )
     658          50 : }
     659           0 : Reference< XNameAccess > UICommandDescription::impl_createConfigAccess(const OUString& _sName)
     660             : {
     661           0 :     return new ConfigurationAccess_UICommand( _sName, m_xGenericUICommands, m_xContext );
     662             : }
     663             : 
     664       42529 : Any SAL_CALL UICommandDescription::getByName( const OUString& aName )
     665             : throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
     666             : {
     667       42529 :     Any a;
     668             : 
     669       85058 :     osl::MutexGuard g(rBHelper.rMutex);
     670             : 
     671       42529 :     ModuleToCommandFileMap::const_iterator pM2CIter = m_aModuleToCommandFileMap.find( aName );
     672       42529 :     if ( pM2CIter != m_aModuleToCommandFileMap.end() )
     673             :     {
     674       42483 :         OUString aCommandFile( pM2CIter->second );
     675       42483 :         UICommandsHashMap::iterator pIter = m_aUICommandsHashMap.find( aCommandFile );
     676       42483 :         if ( pIter != m_aUICommandsHashMap.end() )
     677             :         {
     678       42483 :             if ( pIter->second.is() )
     679       42422 :                 a <<= pIter->second;
     680             :             else
     681             :             {
     682          61 :                 Reference< XNameAccess > xUICommands;
     683             :                 ConfigurationAccess_UICommand* pUICommands = new ConfigurationAccess_UICommand( aCommandFile,
     684             :                                                                                                m_xGenericUICommands,
     685          61 :                                                                                                m_xContext );
     686          61 :                 xUICommands = Reference< XNameAccess >( static_cast< cppu::OWeakObject* >( pUICommands ),UNO_QUERY );
     687          61 :                 pIter->second = xUICommands;
     688          61 :                 a <<= xUICommands;
     689             :             }
     690       42483 :         }
     691             :     }
     692          46 :     else if ( !m_aPrivateResourceURL.isEmpty() && aName.startsWith( m_aPrivateResourceURL ) )
     693             :     {
     694             :         // special keys to retrieve information about a set of commands
     695          44 :         return m_xGenericUICommands->getByName( aName );
     696             :     }
     697             :     else
     698             :     {
     699           2 :         throw NoSuchElementException();
     700             :     }
     701             : 
     702       85012 :     return a;
     703             : }
     704             : 
     705           2 : Sequence< OUString > SAL_CALL UICommandDescription::getElementNames()
     706             : throw (::com::sun::star::uno::RuntimeException, std::exception)
     707             : {
     708           2 :     osl::MutexGuard g(rBHelper.rMutex);
     709             : 
     710           2 :     Sequence< OUString > aSeq( m_aModuleToCommandFileMap.size() );
     711             : 
     712           2 :     sal_Int32 n = 0;
     713           2 :     ModuleToCommandFileMap::const_iterator pIter = m_aModuleToCommandFileMap.begin();
     714          47 :     while ( pIter != m_aModuleToCommandFileMap.end() )
     715             :     {
     716          43 :         aSeq[n++] = pIter->first;
     717          43 :         ++pIter;
     718             :     }
     719             : 
     720           2 :     return aSeq;
     721             : }
     722             : 
     723           4 : sal_Bool SAL_CALL UICommandDescription::hasByName( const OUString& aName )
     724             : throw (::com::sun::star::uno::RuntimeException, std::exception)
     725             : {
     726           4 :     osl::MutexGuard g(rBHelper.rMutex);
     727             : 
     728           4 :     ModuleToCommandFileMap::const_iterator pIter = m_aModuleToCommandFileMap.find( aName );
     729           4 :     return ( pIter != m_aModuleToCommandFileMap.end() );
     730             : }
     731             : 
     732             : // XElementAccess
     733           2 : Type SAL_CALL UICommandDescription::getElementType()
     734             : throw (::com::sun::star::uno::RuntimeException, std::exception)
     735             : {
     736           2 :     return( cppu::UnoType<XNameAccess>::get());
     737             : }
     738             : 
     739           2 : sal_Bool SAL_CALL UICommandDescription::hasElements()
     740             : throw (::com::sun::star::uno::RuntimeException, std::exception)
     741             : {
     742             :     // generic UI commands are always available!
     743           2 :     return sal_True;
     744             : }
     745             : 
     746             : } // namespace framework
     747             : 
     748             : namespace {
     749             : 
     750          48 : struct Instance {
     751          48 :     explicit Instance(
     752             :         css::uno::Reference<css::uno::XComponentContext> const & context):
     753             :         instance(static_cast<cppu::OWeakObject *>(
     754          48 :                     new framework::UICommandDescription(context)))
     755             :     {
     756          48 :     }
     757             : 
     758             :     css::uno::Reference<css::uno::XInterface> instance;
     759             : };
     760             : 
     761             : struct Singleton:
     762             :     public rtl::StaticWithArg<
     763             :         Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
     764             : {};
     765             : 
     766             : }
     767             : 
     768             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     769          49 : com_sun_star_comp_framework_UICommandDescription_get_implementation(
     770             :     css::uno::XComponentContext *context,
     771             :     css::uno::Sequence<css::uno::Any> const &)
     772             : {
     773             :     return cppu::acquire(static_cast<cppu::OWeakObject *>(
     774          49 :                 Singleton::get(context).instance.get()));
     775             : }
     776             : 
     777             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11