LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/uifactory - addonstoolboxfactory.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 25 79 31.6 %
Date: 2013-07-09 Functions: 11 16 68.8 %
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/addonstoolboxfactory.hxx>
      21             : 
      22             : #include <uielement/addonstoolbarwrapper.hxx>
      23             : #include <threadhelp/resetableguard.hxx>
      24             : 
      25             : #include <com/sun/star/util/XURLTransformer.hpp>
      26             : #include <com/sun/star/frame/ModuleManager.hpp>
      27             : #include <com/sun/star/frame/XFrame.hpp>
      28             : #include <com/sun/star/frame/XModel.hpp>
      29             : #include <com/sun/star/lang/XInitialization.hpp>
      30             : #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
      31             : 
      32             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      33             : 
      34             : #include <vcl/svapp.hxx>
      35             : #include <rtl/ustrbuf.hxx>
      36             : 
      37             : //_________________________________________________________________________________________________________________
      38             : //  Defines
      39             : //_________________________________________________________________________________________________________________
      40             : 
      41             : using namespace com::sun::star::uno;
      42             : using namespace com::sun::star::lang;
      43             : using namespace com::sun::star::frame;
      44             : using namespace com::sun::star::beans;
      45             : using namespace com::sun::star::util;
      46             : using namespace ::com::sun::star::ui;
      47             : 
      48             : namespace framework
      49             : {
      50             : 
      51             : //*****************************************************************************************************************
      52             : //  XInterface, XTypeProvider, XServiceInfo
      53             : //*****************************************************************************************************************
      54        1027 : PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( AddonsToolBoxFactory,
      55             :                                       ::cppu::OWeakObject,
      56             :                                       SERVICENAME_TOOLBARFACTORY,
      57             :                                       IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY )
      58          60 : PRIVATE_DEFINE_ONEINSTANCEFACTORY( AddonsToolBoxFactory )
      59             : 
      60          60 : DEFINE_INIT_SERVICE                     (   AddonsToolBoxFactory, {} )
      61             : 
      62          60 : AddonsToolBoxFactory::AddonsToolBoxFactory(
      63             :     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
      64          60 :     ThreadHelpBase( &Application::GetSolarMutex() )
      65             :     , m_xContext( xContext )
      66          60 :     , m_xModuleManager( ModuleManager::create( xContext ) )
      67             : {
      68          60 : }
      69             : 
      70         118 : AddonsToolBoxFactory::~AddonsToolBoxFactory()
      71             : {
      72         118 : }
      73             : 
      74           0 : static sal_Bool IsCorrectContext( const OUString& rModuleIdentifier, const OUString& aContextList )
      75             : {
      76           0 :     if ( aContextList.isEmpty() )
      77           0 :         return sal_True;
      78             : 
      79           0 :     if ( !rModuleIdentifier.isEmpty() )
      80             :     {
      81           0 :         sal_Int32 nIndex = aContextList.indexOf( rModuleIdentifier );
      82           0 :         return ( nIndex >= 0 );
      83             :     }
      84             : 
      85           0 :     return sal_False;
      86             : }
      87             : 
      88           0 : sal_Bool AddonsToolBoxFactory::hasButtonsInContext(
      89             :     const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
      90             :     const Reference< XFrame >& rFrame )
      91             : {
      92           0 :     OUString aModuleIdentifier;
      93             :     try
      94             :     {
      95           0 :         aModuleIdentifier = m_xModuleManager->identify( rFrame );
      96             :     }
      97           0 :     catch ( const RuntimeException& )
      98             :     {
      99           0 :         throw;
     100             :     }
     101           0 :     catch ( const Exception& )
     102             :     {
     103             :     }
     104             : 
     105             :     // Check before we create a toolbar that we have at least one button in
     106             :     // the current frame context.
     107           0 :     for ( sal_uInt32 i = 0; i < (sal_uInt32)rPropSeqSeq.getLength(); i++ )
     108             :     {
     109           0 :         sal_Bool    bIsButton( sal_True );
     110           0 :         sal_Bool    bIsCorrectContext( sal_False );
     111           0 :         sal_uInt32  nPropChecked( 0 );
     112             : 
     113           0 :         const Sequence< PropertyValue >& rPropSeq = rPropSeqSeq[i];
     114           0 :         for ( sal_uInt32 j = 0; j < (sal_uInt32)rPropSeq.getLength(); j++ )
     115             :         {
     116           0 :             if ( rPropSeq[j].Name.equalsAsciiL( "Context", 7 ))
     117             :             {
     118           0 :                 OUString aContextList;
     119           0 :                 if ( rPropSeq[j].Value >>= aContextList )
     120           0 :                     bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
     121           0 :                 nPropChecked++;
     122             :             }
     123           0 :             else if ( rPropSeq[j].Name.equalsAsciiL( "URL", 3 ))
     124             :             {
     125           0 :                 OUString aURL;
     126           0 :                 rPropSeq[j].Value >>= aURL;
     127           0 :                 bIsButton = !aURL.equalsAsciiL( "private:separator", 17 );
     128           0 :                 nPropChecked++;
     129             :             }
     130             : 
     131           0 :             if ( nPropChecked == 2 )
     132           0 :                 break;
     133             :         }
     134             : 
     135           0 :         if ( bIsButton && bIsCorrectContext )
     136           0 :             return sal_True;
     137             :     }
     138             : 
     139           0 :     return sal_False;
     140             : }
     141             : 
     142             : // XUIElementFactory
     143        2209 : Reference< XUIElement > SAL_CALL AddonsToolBoxFactory::createUIElement(
     144             :     const OUString& ResourceURL,
     145             :     const Sequence< PropertyValue >& Args )
     146             : throw ( ::com::sun::star::container::NoSuchElementException,
     147             :         ::com::sun::star::lang::IllegalArgumentException,
     148             :         ::com::sun::star::uno::RuntimeException )
     149             : {
     150             :     // SAFE
     151        2209 :     ResetableGuard aLock( m_aLock );
     152             : 
     153        4418 :     Sequence< Sequence< PropertyValue > >   aConfigData;
     154        4418 :     Reference< XFrame >                     xFrame;
     155        4418 :     OUString                           aResourceURL( ResourceURL );
     156             : 
     157        6627 :     for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
     158             :     {
     159        4418 :         if ( Args[n].Name == "ConfigurationData" )
     160        2209 :             Args[n].Value >>= aConfigData;
     161        2209 :         else if ( Args[n].Name == "Frame" )
     162        2209 :             Args[n].Value >>= xFrame;
     163           0 :         else if ( Args[n].Name == "ResourceURL" )
     164           0 :             Args[n].Value >>= aResourceURL;
     165             :     }
     166             : 
     167        2209 :     if ( aResourceURL.indexOf( "private:resource/toolbar/addon_" ) != 0 )
     168           0 :         throw IllegalArgumentException();
     169             : 
     170             :     // Identify frame and determine module identifier to look for context based buttons
     171        2209 :     Reference< ::com::sun::star::ui::XUIElement > xToolBar;
     172        5525 :     if ( xFrame.is() &&
     173        2209 :          ( aConfigData.getLength()> 0 ) &&
     174           0 :          hasButtonsInContext( aConfigData, xFrame ))
     175             :     {
     176           0 :         PropertyValue aPropValue;
     177           0 :         Sequence< Any > aPropSeq( 3 );
     178           0 :         aPropValue.Name = OUString( "Frame" );
     179           0 :         aPropValue.Value <<= xFrame;
     180           0 :         aPropSeq[0] <<= aPropValue;
     181           0 :         aPropValue.Name = OUString( "ConfigurationData" );
     182           0 :         aPropValue.Value <<= aConfigData;
     183           0 :         aPropSeq[1] <<= aPropValue;
     184           0 :         aPropValue.Name = OUString( "ResourceURL" );
     185           0 :         aPropValue.Value <<= aResourceURL;
     186           0 :         aPropSeq[2] <<= aPropValue;
     187             : 
     188           0 :         SolarMutexGuard aGuard;
     189           0 :         AddonsToolBarWrapper* pToolBarWrapper = new AddonsToolBarWrapper( m_xContext );
     190           0 :         xToolBar = Reference< ::com::sun::star::ui::XUIElement >( (OWeakObject *)pToolBarWrapper, UNO_QUERY );
     191           0 :         Reference< XInitialization > xInit( xToolBar, UNO_QUERY );
     192           0 :         xInit->initialize( aPropSeq );
     193             :     }
     194             : 
     195        4418 :     return xToolBar;
     196             : }
     197             : 
     198         402 : }
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10