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

Generated by: LCOV version 1.10