LCOV - code coverage report
Current view: top level - forms/source/helper - commanddescriptionprovider.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 21 25 84.0 %
Date: 2014-04-11 Functions: 6 6 100.0 %
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             : 
      21             : #include "commanddescriptionprovider.hxx"
      22             : 
      23             : #include <com/sun/star/container/XNameAccess.hpp>
      24             : #include <com/sun/star/frame/ModuleManager.hpp>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <com/sun/star/frame/theUICommandDescription.hpp>
      27             : 
      28             : #include <comphelper/namedvaluecollection.hxx>
      29             : #include <tools/diagnose_ex.h>
      30             : 
      31             : 
      32             : namespace frm
      33             : {
      34             : 
      35             : 
      36             :     using ::com::sun::star::uno::Reference;
      37             :     using ::com::sun::star::uno::XInterface;
      38             :     using ::com::sun::star::uno::UNO_QUERY;
      39             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      40             :     using ::com::sun::star::uno::UNO_SET_THROW;
      41             :     using ::com::sun::star::uno::Exception;
      42             :     using ::com::sun::star::uno::RuntimeException;
      43             :     using ::com::sun::star::uno::Any;
      44             :     using ::com::sun::star::uno::makeAny;
      45             :     using ::com::sun::star::uno::Sequence;
      46             :     using ::com::sun::star::uno::Type;
      47             :     using ::com::sun::star::uno::XComponentContext;
      48             :     using ::com::sun::star::frame::XModel;
      49             :     using ::com::sun::star::container::XNameAccess;
      50             :     using ::com::sun::star::frame::ModuleManager;
      51             :     using ::com::sun::star::frame::XModuleManager2;
      52             :     using ::com::sun::star::beans::PropertyValue;
      53             :     using ::com::sun::star::frame::theUICommandDescription;
      54             : 
      55             : 
      56             :     //= DefaultCommandDescriptionProvider
      57             : 
      58             :     class DefaultCommandDescriptionProvider : public ICommandDescriptionProvider
      59             :     {
      60             :     public:
      61          18 :         DefaultCommandDescriptionProvider( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
      62          18 :         {
      63          18 :             impl_init_nothrow( _rxContext, _rxDocument );
      64          18 :         }
      65             : 
      66          36 :         virtual ~DefaultCommandDescriptionProvider()
      67          18 :         {
      68          36 :         }
      69             : 
      70             :         // ICommandDescriptionProvider
      71             :         virtual OUString getCommandDescription( const OUString& _rCommandURL ) const SAL_OVERRIDE;
      72             : 
      73             :     private:
      74             :         void    impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument );
      75             : 
      76             :     private:
      77             :         Reference< XNameAccess >    m_xCommandAccess;
      78             :     };
      79             : 
      80             : 
      81             : 
      82          18 :     void DefaultCommandDescriptionProvider::impl_init_nothrow( const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
      83             :     {
      84             :         OSL_ENSURE( _rxDocument.is(), "DefaultCommandDescriptionProvider::impl_init_nothrow: no document => no command descriptions!" );
      85          18 :         if ( !_rxDocument.is() )
      86          18 :             return;
      87             : 
      88             :         try
      89             :         {
      90          18 :             Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rxContext) );
      91          36 :             OUString sModuleID = xModuleManager->identify( _rxDocument );
      92             : 
      93          36 :             Reference< XNameAccess > xUICommandDescriptions( theUICommandDescription::get(_rxContext) );
      94          36 :             m_xCommandAccess.set( xUICommandDescriptions->getByName( sModuleID ), UNO_QUERY_THROW );
      95             :         }
      96           0 :         catch( const Exception& )
      97             :         {
      98             :             DBG_UNHANDLED_EXCEPTION();
      99             :         }
     100             :     }
     101             : 
     102             : 
     103         342 :     OUString DefaultCommandDescriptionProvider::getCommandDescription( const OUString& _rCommandURL ) const
     104             :     {
     105         342 :         if ( !m_xCommandAccess.is() )
     106           0 :             return OUString();
     107             : 
     108             :         try
     109             :         {
     110         342 :             ::comphelper::NamedValueCollection aCommandProperties( m_xCommandAccess->getByName( _rCommandURL ) );
     111         342 :             return aCommandProperties.getOrDefault( "Name", OUString() );
     112             :         }
     113           0 :         catch( const Exception& )
     114             :         {
     115             :             DBG_UNHANDLED_EXCEPTION();
     116             :         }
     117             : 
     118           0 :         return OUString();
     119             :     }
     120             : 
     121             : 
     122          18 :     PCommandDescriptionProvider createDocumentCommandDescriptionProvider(
     123             :         const Reference<XComponentContext>& _rxContext, const Reference< XModel >& _rxDocument )
     124             :     {
     125          18 :         PCommandDescriptionProvider pDescriptionProvider( new DefaultCommandDescriptionProvider( _rxContext, _rxDocument ) );
     126          18 :         return pDescriptionProvider;
     127             :     }
     128             : 
     129             : 
     130             : } // namespace frm
     131             : 
     132             : 
     133             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10