LCOV - code coverage report
Current view: top level - libreoffice/framework/source/uifactory - addonstoolboxfactory.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 68 77 88.3 %
Date: 2012-12-27 Functions: 11 14 78.6 %
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         129 : DEFINE_XSERVICEINFO_ONEINSTANCESERVICE  (   AddonsToolBoxFactory                            ,
      55             :                                             ::cppu::OWeakObject                             ,
      56             :                                             SERVICENAME_TOOLBARFACTORY                      ,
      57             :                                             IMPLEMENTATIONNAME_ADDONSTOOLBARFACTORY
      58             :                                         )
      59             : 
      60           8 : DEFINE_INIT_SERVICE                     (   AddonsToolBoxFactory, {} )
      61             : 
      62           8 : AddonsToolBoxFactory::AddonsToolBoxFactory(
      63             :     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
      64           8 :     ThreadHelpBase( &Application::GetSolarMutex() )
      65             :     , m_xServiceManager( xServiceManager )
      66           8 :     , m_xModuleManager( ModuleManager::create( comphelper::getComponentContext(xServiceManager) ) )
      67             : {
      68           8 : }
      69             : 
      70          16 : AddonsToolBoxFactory::~AddonsToolBoxFactory()
      71             : {
      72          16 : }
      73             : 
      74         236 : static sal_Bool IsCorrectContext( const ::rtl::OUString& rModuleIdentifier, const rtl::OUString& aContextList )
      75             : {
      76         236 :     if ( aContextList.isEmpty() )
      77           0 :         return sal_True;
      78             : 
      79         236 :     if ( !rModuleIdentifier.isEmpty() )
      80             :     {
      81         236 :         sal_Int32 nIndex = aContextList.indexOf( rModuleIdentifier );
      82         236 :         return ( nIndex >= 0 );
      83             :     }
      84             : 
      85           0 :     return sal_False;
      86             : }
      87             : 
      88         236 : sal_Bool AddonsToolBoxFactory::hasButtonsInContext(
      89             :     const Sequence< Sequence< PropertyValue > >& rPropSeqSeq,
      90             :     const Reference< XFrame >& rFrame )
      91             : {
      92         236 :     ::rtl::OUString aModuleIdentifier;
      93             :     try
      94             :     {
      95         236 :         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         236 :     for ( sal_uInt32 i = 0; i < (sal_uInt32)rPropSeqSeq.getLength(); i++ )
     108             :     {
     109         236 :         sal_Bool    bIsButton( sal_True );
     110         236 :         sal_Bool    bIsCorrectContext( sal_False );
     111         236 :         sal_uInt32  nPropChecked( 0 );
     112             : 
     113         236 :         const Sequence< PropertyValue >& rPropSeq = rPropSeqSeq[i];
     114        1180 :         for ( sal_uInt32 j = 0; j < (sal_uInt32)rPropSeq.getLength(); j++ )
     115             :         {
     116        1180 :             if ( rPropSeq[j].Name.equalsAsciiL( "Context", 7 ))
     117             :             {
     118         236 :                 ::rtl::OUString aContextList;
     119         236 :                 if ( rPropSeq[j].Value >>= aContextList )
     120         236 :                     bIsCorrectContext = IsCorrectContext( aModuleIdentifier, aContextList );
     121         236 :                 nPropChecked++;
     122             :             }
     123         944 :             else if ( rPropSeq[j].Name.equalsAsciiL( "URL", 3 ))
     124             :             {
     125         236 :                 ::rtl::OUString aURL;
     126         236 :                 rPropSeq[j].Value >>= aURL;
     127         236 :                 bIsButton = !aURL.equalsAsciiL( "private:separator", 17 );
     128         236 :                 nPropChecked++;
     129             :             }
     130             : 
     131        1180 :             if ( nPropChecked == 2 )
     132         236 :                 break;
     133             :         }
     134             : 
     135         236 :         if ( bIsButton && bIsCorrectContext )
     136         236 :             return sal_True;
     137             :     }
     138             : 
     139           0 :     return sal_False;
     140             : }
     141             : 
     142             : // XUIElementFactory
     143         472 : Reference< XUIElement > SAL_CALL AddonsToolBoxFactory::createUIElement(
     144             :     const ::rtl::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         472 :     ResetableGuard aLock( m_aLock );
     152             : 
     153         472 :     Sequence< Sequence< PropertyValue > >   aConfigData;
     154         472 :     Reference< XFrame >                     xFrame;
     155         472 :     rtl::OUString                           aResourceURL( ResourceURL );
     156             : 
     157        1416 :     for ( sal_Int32 n = 0; n < Args.getLength(); n++ )
     158             :     {
     159         944 :         if ( Args[n].Name == "ConfigurationData" )
     160         472 :             Args[n].Value >>= aConfigData;
     161         472 :         else if ( Args[n].Name == "Frame" )
     162         472 :             Args[n].Value >>= xFrame;
     163           0 :         else if ( Args[n].Name == "ResourceURL" )
     164           0 :             Args[n].Value >>= aResourceURL;
     165             :     }
     166             : 
     167         472 :     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         472 :     Reference< ::com::sun::star::ui::XUIElement > xToolBar;
     172         944 :     if ( xFrame.is() &&
     173         236 :          ( aConfigData.getLength()> 0 ) &&
     174         236 :          hasButtonsInContext( aConfigData, xFrame ))
     175             :     {
     176         236 :         PropertyValue aPropValue;
     177         236 :         Sequence< Any > aPropSeq( 3 );
     178         236 :         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
     179         236 :         aPropValue.Value <<= xFrame;
     180         236 :         aPropSeq[0] <<= aPropValue;
     181         236 :         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationData" ));
     182         236 :         aPropValue.Value <<= aConfigData;
     183         236 :         aPropSeq[1] <<= aPropValue;
     184         236 :         aPropValue.Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
     185         236 :         aPropValue.Value <<= aResourceURL;
     186         236 :         aPropSeq[2] <<= aPropValue;
     187             : 
     188         236 :         SolarMutexGuard aGuard;
     189         236 :         AddonsToolBarWrapper* pToolBarWrapper = new AddonsToolBarWrapper( m_xServiceManager );
     190         236 :         xToolBar = Reference< ::com::sun::star::ui::XUIElement >( (OWeakObject *)pToolBarWrapper, UNO_QUERY );
     191         236 :         Reference< XInitialization > xInit( xToolBar, UNO_QUERY );
     192         236 :         xInit->initialize( aPropSeq );
     193             :     }
     194             : 
     195         472 :     return xToolBar;
     196             : }
     197             : 
     198             : }
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10