LCOV - code coverage report
Current view: top level - sfx2/source/doc - sfxmodelfactory.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 35 44 79.5 %
Date: 2015-06-13 12:38:46 Functions: 9 12 75.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             : #include <sfx2/sfxmodelfactory.hxx>
      21             : 
      22             : #include <com/sun/star/lang/XServiceInfo.hpp>
      23             : #include <com/sun/star/beans/NamedValue.hpp>
      24             : #include <com/sun/star/lang/XInitialization.hpp>
      25             : 
      26             : #include <comphelper/namedvaluecollection.hxx>
      27             : #include <cppuhelper/implbase2.hxx>
      28             : #include <cppuhelper/supportsservice.hxx>
      29             : 
      30             : #include <osl/diagnose.h>
      31             : 
      32             : #include <algorithm>
      33             : #include <functional>
      34             : 
      35             : 
      36             : namespace sfx2
      37             : {
      38             : 
      39             : 
      40             :     using ::com::sun::star::uno::Reference;
      41             :     using ::com::sun::star::uno::XInterface;
      42             :     using ::com::sun::star::uno::UNO_QUERY;
      43             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      44             :     using ::com::sun::star::uno::UNO_SET_THROW;
      45             :     using ::com::sun::star::uno::Exception;
      46             :     using ::com::sun::star::uno::RuntimeException;
      47             :     using ::com::sun::star::uno::Any;
      48             :     using ::com::sun::star::uno::makeAny;
      49             :     using ::com::sun::star::lang::XMultiServiceFactory;
      50             :     using ::com::sun::star::uno::Sequence;
      51             :     using ::com::sun::star::lang::XSingleServiceFactory;
      52             :     using ::com::sun::star::lang::XServiceInfo;
      53             :     using ::com::sun::star::beans::NamedValue;
      54             :     using ::com::sun::star::beans::PropertyValue;
      55             :     using ::com::sun::star::lang::XInitialization;
      56             : 
      57             : 
      58             :     //= SfxModelFactory - declaration
      59             : 
      60             :     typedef ::cppu::WeakImplHelper2 <   XSingleServiceFactory
      61             :                                     ,   XServiceInfo
      62             :                                     >   SfxModelFactory_Base;
      63             :     /** implements a XSingleServiceFactory which can be used to created instances
      64             :         of classes derived from SfxBaseModel
      65             : 
      66             :         In opposite to the default implementations from module cppuhelper, this
      67             :         factory evaluates certain creation arguments (passed to createInstanceWithArguments)
      68             :         and passes them to the factory function of the derived class.
      69             :     */
      70             :     class SfxModelFactory : public SfxModelFactory_Base
      71             :     {
      72             :     public:
      73             :         SfxModelFactory(
      74             :             const Reference< XMultiServiceFactory >& _rxServiceFactory,
      75             :             const OUString& _rImplementationName,
      76             :             const SfxModelFactoryFunc _pComponentFactoryFunc,
      77             :             const Sequence< OUString >& _rServiceNames
      78             :         );
      79             : 
      80             :         // XSingleServiceFactory
      81             :         virtual Reference< XInterface > SAL_CALL createInstance(  ) throw (Exception, RuntimeException, std::exception) SAL_OVERRIDE;
      82             :         virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception) SAL_OVERRIDE;
      83             : 
      84             :         // XServiceInfo
      85             :         virtual OUString SAL_CALL getImplementationName(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      86             :         virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      87             :         virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      88             : 
      89             :     protected:
      90             :         virtual ~SfxModelFactory();
      91             : 
      92             :     private:
      93             :         Reference< XInterface > impl_createInstance( const SfxModelFlags _nCreationFlags ) const;
      94             : 
      95             :     private:
      96             :         const Reference< XMultiServiceFactory >     m_xServiceFactory;
      97             :         const OUString                       m_sImplementationName;
      98             :         const Sequence< OUString >           m_aServiceNames;
      99             :         const SfxModelFactoryFunc                   m_pComponentFactoryFunc;
     100             :     };
     101             : 
     102             : 
     103             :     //= SfxModelFactory - implementation
     104             : 
     105             : 
     106         145 :     SfxModelFactory::SfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
     107             :             const OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
     108             :             const Sequence< OUString >& _rServiceNames )
     109             :         :m_xServiceFactory( _rxServiceFactory )
     110             :         ,m_sImplementationName( _rImplementationName )
     111             :         ,m_aServiceNames( _rServiceNames )
     112         145 :         ,m_pComponentFactoryFunc( _pComponentFactoryFunc )
     113             :     {
     114         145 :     }
     115             : 
     116             : 
     117         288 :     SfxModelFactory::~SfxModelFactory()
     118             :     {
     119         288 :     }
     120             : 
     121             : 
     122        4033 :     Reference< XInterface > SfxModelFactory::impl_createInstance( const SfxModelFlags _nCreationFlags ) const
     123             :     {
     124        4033 :         return (*m_pComponentFactoryFunc)( m_xServiceFactory, _nCreationFlags );
     125             :     }
     126             : 
     127             : 
     128        3740 :     Reference< XInterface > SAL_CALL SfxModelFactory::createInstance(  ) throw (Exception, RuntimeException, std::exception)
     129             :     {
     130        3740 :         return createInstanceWithArguments( Sequence< Any >() );
     131             :     }
     132             : 
     133             : 
     134             :     namespace
     135             :     {
     136             :         struct IsSpecialArgument : public ::std::unary_function< Any, bool >
     137             :         {
     138         877 :             static bool isSpecialArgumentName( const OUString& _rValueName )
     139             :             {
     140         877 :                 return  _rValueName == "EmbeddedObject" || _rValueName == "EmbeddedScriptSupport" || _rValueName == "DocumentRecoverySupport";
     141             :             }
     142             : 
     143         877 :             bool operator()( const Any& _rArgument ) const
     144             :             {
     145         877 :                 NamedValue aNamedValue;
     146         877 :                 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
     147           1 :                     return true;
     148        1752 :                 PropertyValue aPropertyValue;
     149         876 :                 if ( ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name ) )
     150         876 :                     return true;
     151         877 :                 return false;
     152             :             }
     153             :         };
     154             :     }
     155             : 
     156             : 
     157        4033 :     Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
     158             :     {
     159        4033 :         ::comphelper::NamedValueCollection aArgs( _rArguments );
     160        4033 :         const bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", sal_False );
     161        4033 :         const bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", sal_True );
     162        4033 :         const bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", sal_True );
     163             : 
     164             :         SfxModelFlags nCreationFlags =
     165             :                 ( bEmbeddedObject ? SfxModelFlags::EMBEDDED_OBJECT : SfxModelFlags::NONE )
     166             :             |   ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
     167        4033 :             |   ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
     168             : 
     169        4033 :         Reference< XInterface > xInstance( impl_createInstance( nCreationFlags ) );
     170             : 
     171             :         // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
     172             :         // the object with the given arguments, stripped by the three special ones
     173        8066 :         Sequence< Any > aStrippedArguments( _rArguments.getLength() );
     174        4033 :         Any* pStrippedArgs = aStrippedArguments.getArray();
     175             :         Any* pStrippedArgsEnd = ::std::remove_copy_if(
     176             :             _rArguments.getConstArray(),
     177        4033 :             _rArguments.getConstArray() + _rArguments.getLength(),
     178             :             pStrippedArgs,
     179             :             IsSpecialArgument()
     180        4033 :         );
     181        4033 :         aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
     182             : 
     183        4033 :         if ( aStrippedArguments.getLength() )
     184             :         {
     185           0 :             Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
     186             :             OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
     187           0 :             if ( xModelInit.is() )
     188           0 :                 xModelInit->initialize( aStrippedArguments );
     189             :         }
     190             : 
     191        8066 :         return xInstance;
     192             :     }
     193             : 
     194           0 :     OUString SAL_CALL SfxModelFactory::getImplementationName(  ) throw (RuntimeException, std::exception)
     195             :     {
     196           0 :         return m_sImplementationName;
     197             :     }
     198             : 
     199           0 :     sal_Bool SAL_CALL SfxModelFactory::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
     200             :     {
     201           0 :         return cppu::supportsService(this, _rServiceName);
     202             :     }
     203             : 
     204           0 :     Sequence< OUString > SAL_CALL SfxModelFactory::getSupportedServiceNames(  ) throw (RuntimeException, std::exception)
     205             :     {
     206           0 :         return m_aServiceNames;
     207             :     }
     208             : 
     209         145 :     Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
     210             :             const OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
     211             :             const Sequence< OUString >& _rServiceNames )
     212             :     {
     213         145 :         return new SfxModelFactory( _rxServiceFactory, _rImplementationName, _pComponentFactoryFunc, _rServiceNames );
     214             :     }
     215             : } // namespace sfx2
     216             : 
     217             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11