LCOV - code coverage report
Current view: top level - forms/source/helper - commandimageprovider.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 38 42 90.5 %
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 "commandimageprovider.hxx"
      22             : 
      23             : #include <com/sun/star/ui/XImageManager.hpp>
      24             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      25             : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
      26             : #include <com/sun/star/frame/ModuleManager.hpp>
      27             : #include <com/sun/star/ui/ImageType.hpp>
      28             : 
      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::ui::XImageManager;
      50             :     using ::com::sun::star::ui::XUIConfigurationManagerSupplier;
      51             :     using ::com::sun::star::ui::XUIConfigurationManager;
      52             :     using ::com::sun::star::ui::XModuleUIConfigurationManagerSupplier;
      53             :     using ::com::sun::star::ui::theModuleUIConfigurationManagerSupplier;
      54             :     using ::com::sun::star::frame::ModuleManager;
      55             :     using ::com::sun::star::frame::XModuleManager2;
      56             :     using ::com::sun::star::graphic::XGraphic;
      57             : 
      58             :     namespace ImageType = ::com::sun::star::ui::ImageType;
      59             : 
      60             : 
      61             :     //= DocumentCommandImageProvider
      62             : 
      63             :     class DocumentCommandImageProvider : public ICommandImageProvider
      64             :     {
      65             :     public:
      66          18 :         DocumentCommandImageProvider( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
      67          18 :         {
      68          18 :             impl_init_nothrow( _rContext, _rxDocument );
      69          18 :         }
      70          36 :         virtual ~DocumentCommandImageProvider()
      71          18 :         {
      72          36 :         }
      73             : 
      74             :         // ICommandImageProvider
      75             :         virtual CommandImages getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const SAL_OVERRIDE;
      76             : 
      77             :     private:
      78             :         void    impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument );
      79             : 
      80             :     private:
      81             :         Reference< XImageManager >    m_xDocumentImageManager;
      82             :         Reference< XImageManager >    m_xModuleImageManager;
      83             :     };
      84             : 
      85             : 
      86          18 :     void DocumentCommandImageProvider::impl_init_nothrow( const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
      87             :     {
      88             :         OSL_ENSURE( _rxDocument.is(), "DocumentCommandImageProvider::impl_init_nothrow: no document => no images!" );
      89          18 :         if ( !_rxDocument.is() )
      90          18 :             return;
      91             : 
      92             :         // obtain the image manager of the document
      93             :         try
      94             :         {
      95          18 :             Reference< XUIConfigurationManagerSupplier > xSuppUIConfig( _rxDocument, UNO_QUERY_THROW );
      96          36 :             Reference< XUIConfigurationManager > xUIConfig( xSuppUIConfig->getUIConfigurationManager(), UNO_QUERY );
      97          36 :             m_xDocumentImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
      98             :         }
      99           0 :         catch( const Exception& )
     100             :         {
     101             :             DBG_UNHANDLED_EXCEPTION();
     102             :         }
     103             : 
     104             :         // obtain the image manager or the module
     105             :         try
     106             :         {
     107          18 :             Reference< XModuleManager2 > xModuleManager( ModuleManager::create(_rContext) );
     108          36 :             OUString sModuleID = xModuleManager->identify( _rxDocument );
     109             : 
     110             :             Reference< XModuleUIConfigurationManagerSupplier > xSuppUIConfig(
     111          36 :                 theModuleUIConfigurationManagerSupplier::get(_rContext) );
     112             :             Reference< XUIConfigurationManager > xUIConfig(
     113          36 :                 xSuppUIConfig->getUIConfigurationManager( sModuleID ), UNO_SET_THROW );
     114          36 :             m_xModuleImageManager.set( xUIConfig->getImageManager(), UNO_QUERY_THROW );
     115             :         }
     116           0 :         catch( const Exception& )
     117             :         {
     118             :             DBG_UNHANDLED_EXCEPTION();
     119             :         }
     120             :     }
     121             : 
     122             : 
     123          45 :     CommandImages DocumentCommandImageProvider::getCommandImages( const CommandURLs& _rCommandURLs, const bool _bLarge ) const
     124             :     {
     125          45 :         const size_t nCommandCount = _rCommandURLs.getLength();
     126          45 :         CommandImages aImages( nCommandCount );
     127             :         try
     128             :         {
     129             :             const sal_Int16 nImageType = ImageType::COLOR_NORMAL
     130          45 :                                        +  ( _bLarge ? ImageType::SIZE_LARGE : ImageType::SIZE_DEFAULT );
     131             : 
     132          45 :             Sequence< Reference< XGraphic > > aDocImages( nCommandCount );
     133          90 :             Sequence< Reference< XGraphic > > aModImages( nCommandCount );
     134             : 
     135             :             // first try the document image manager
     136          45 :             if ( m_xDocumentImageManager.is() )
     137          45 :                 aDocImages = m_xDocumentImageManager->getImages( nImageType, _rCommandURLs );
     138             : 
     139             :             // then the module's image manager
     140          45 :             if ( m_xModuleImageManager.is() )
     141          45 :                 aModImages = m_xModuleImageManager->getImages( nImageType, _rCommandURLs );
     142             : 
     143          45 :             ENSURE_OR_THROW( (size_t)aDocImages.getLength() == nCommandCount, "illegal array size returned by getImages (document image manager)" );
     144          45 :             ENSURE_OR_THROW( (size_t)aModImages.getLength() == nCommandCount, "illegal array size returned by getImages (module image manager)" );
     145             : 
     146         900 :             for ( size_t i=0; i<nCommandCount; ++i )
     147             :             {
     148         855 :                 if ( aDocImages[i].is() )
     149           0 :                     aImages[i] = Image( aDocImages[i] );
     150             :                 else
     151         855 :                     aImages[i] = Image( aModImages[i] );
     152          45 :             }
     153             :         }
     154           0 :         catch( const Exception& )
     155             :         {
     156             :             DBG_UNHANDLED_EXCEPTION();
     157             :         }
     158          45 :         return aImages;
     159             :     }
     160             : 
     161             : 
     162          18 :     PCommandImageProvider createDocumentCommandImageProvider(
     163             :         const Reference<XComponentContext>& _rContext, const Reference< XModel >& _rxDocument )
     164             :     {
     165          18 :         PCommandImageProvider pImageProvider( new DocumentCommandImageProvider( _rContext, _rxDocument ) );
     166          18 :         return pImageProvider;
     167             :     }
     168             : 
     169             : 
     170             : } // namespace frm
     171             : 
     172             : 
     173             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10