LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/filter/ftools - fapihelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 130 173 75.1 %
Date: 2013-07-09 Functions: 26 30 86.7 %
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 "fapihelper.hxx"
      21             : 
      22             : #include <algorithm>
      23             : #include <com/sun/star/lang/XServiceName.hpp>
      24             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      25             : #include <com/sun/star/beans/XPropertyState.hpp>
      26             : #include <com/sun/star/beans/XPropertySetOption.hpp>
      27             : #include <comphelper/docpasswordhelper.hxx>
      28             : #include <comphelper/processfactory.hxx>
      29             : #include <tools/urlobj.hxx>
      30             : #include <rtl/strbuf.hxx>
      31             : #include <sfx2/objsh.hxx>
      32             : #include <sfx2/docfile.hxx>
      33             : #include <sfx2/request.hxx>
      34             : #include <sfx2/frame.hxx>
      35             : #include <sfx2/sfxsids.hrc>
      36             : #include <svl/stritem.hxx>
      37             : #include <svl/itemset.hxx>
      38             : #include "miscuno.hxx"
      39             : 
      40             : using ::com::sun::star::uno::Any;
      41             : using ::com::sun::star::uno::Reference;
      42             : using ::com::sun::star::uno::Sequence;
      43             : using ::com::sun::star::uno::Exception;
      44             : using ::com::sun::star::uno::UNO_QUERY;
      45             : using ::com::sun::star::uno::UNO_QUERY_THROW;
      46             : using ::com::sun::star::uno::UNO_SET_THROW;
      47             : using ::com::sun::star::uno::TypeClass_BOOLEAN;
      48             : using ::com::sun::star::uno::XInterface;
      49             : using ::com::sun::star::beans::XPropertySet;
      50             : using ::com::sun::star::beans::XPropertyState;
      51             : using ::com::sun::star::lang::XServiceName;
      52             : using ::com::sun::star::lang::XMultiServiceFactory;
      53             : using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER;
      54             : 
      55             : using namespace ::com::sun::star;
      56             : 
      57             : // Static helper functions ====================================================
      58             : 
      59           4 : OUString ScfApiHelper::GetServiceName( const Reference< XInterface >& xInt )
      60             : {
      61           4 :     OUString aService;
      62           8 :     Reference< XServiceName > xServiceName( xInt, UNO_QUERY );
      63           4 :     if( xServiceName.is() )
      64           4 :         aService = xServiceName->getServiceName();
      65           8 :     return aService;
      66             : }
      67             : 
      68         103 : Reference< XMultiServiceFactory > ScfApiHelper::GetServiceFactory( SfxObjectShell* pShell )
      69             : {
      70         103 :     Reference< XMultiServiceFactory > xFactory;
      71         103 :     if( pShell )
      72         103 :         xFactory.set( pShell->GetModel(), UNO_QUERY );
      73         103 :     return xFactory;
      74             : }
      75             : 
      76         345 : Reference< XInterface > ScfApiHelper::CreateInstance(
      77             :         const Reference< XMultiServiceFactory >& xFactory, const OUString& rServiceName )
      78             : {
      79         345 :     Reference< XInterface > xInt;
      80         345 :     if( xFactory.is() )
      81             :     {
      82             :         try
      83             :         {
      84         345 :             xInt = xFactory->createInstance( rServiceName );
      85             :         }
      86           0 :         catch( Exception& )
      87             :         {
      88             :             OSL_FAIL( "ScfApiHelper::CreateInstance - cannot create instance" );
      89             :         }
      90             :     }
      91         345 :     return xInt;
      92             : }
      93             : 
      94         103 : Reference< XInterface > ScfApiHelper::CreateInstance( SfxObjectShell* pShell, const OUString& rServiceName )
      95             : {
      96         103 :     return CreateInstance( GetServiceFactory( pShell ), rServiceName );
      97             : }
      98             : 
      99         242 : Reference< XInterface > ScfApiHelper::CreateInstance( const OUString& rServiceName )
     100             : {
     101         242 :     return CreateInstance( ::comphelper::getProcessServiceFactory(), rServiceName );
     102             : }
     103             : 
     104           0 : uno::Sequence< beans::NamedValue > ScfApiHelper::QueryEncryptionDataForMedium( SfxMedium& rMedium,
     105             :         ::comphelper::IDocPasswordVerifier& rVerifier, const ::std::vector< OUString >* pDefaultPasswords )
     106             : {
     107           0 :     uno::Sequence< beans::NamedValue > aEncryptionData;
     108           0 :     SFX_ITEMSET_ARG( rMedium.GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false);
     109           0 :     if ( pEncryptionDataItem )
     110           0 :         pEncryptionDataItem->GetValue() >>= aEncryptionData;
     111             : 
     112           0 :     OUString aPassword;
     113           0 :     SFX_ITEMSET_ARG( rMedium.GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false);
     114           0 :     if ( pPasswordItem )
     115           0 :         aPassword = pPasswordItem->GetValue();
     116             : 
     117           0 :     OUString aDocName = INetURLObject( rMedium.GetOrigURL() ).GetName( INetURLObject::DECODE_WITH_CHARSET );
     118             : 
     119           0 :     bool bIsDefaultPassword = false;
     120           0 :     aEncryptionData = ::comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
     121             :         rVerifier, aEncryptionData, aPassword, rMedium.GetInteractionHandler(), aDocName,
     122           0 :         ::comphelper::DocPasswordRequestType_MS, pDefaultPasswords, &bIsDefaultPassword );
     123             : 
     124           0 :     rMedium.GetItemSet()->ClearItem( SID_PASSWORD );
     125           0 :     rMedium.GetItemSet()->ClearItem( SID_ENCRYPTIONDATA );
     126             : 
     127           0 :     if( !bIsDefaultPassword && (aEncryptionData.getLength() > 0) )
     128           0 :         rMedium.GetItemSet()->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
     129             : 
     130           0 :     return aEncryptionData;
     131             : }
     132             : 
     133             : // Property sets ==============================================================
     134             : 
     135        2090 : ScfPropertySet::~ScfPropertySet()
     136             : {
     137        1045 :     Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
     138        1045 :     if (xPropSetOpt.is())
     139             :         // Turn the property value change notification back on when finished.
     140          99 :         xPropSetOpt->enableChangeListenerNotification(true);
     141        1045 : }
     142             : 
     143        1045 : void ScfPropertySet::Set( Reference< XPropertySet > xPropSet )
     144             : {
     145        1045 :     mxPropSet = xPropSet;
     146        1045 :     mxMultiPropSet.set( mxPropSet, UNO_QUERY );
     147        1045 :     Reference<beans::XPropertySetOption> xPropSetOpt(mxPropSet, UNO_QUERY);
     148        1045 :     if (xPropSetOpt.is())
     149             :         // We don't want to broadcast property value changes during import to
     150             :         // improve performance.
     151          99 :         xPropSetOpt->enableChangeListenerNotification(false);
     152        1045 : }
     153             : 
     154           0 : OUString ScfPropertySet::GetServiceName() const
     155             : {
     156           0 :     return ScfApiHelper::GetServiceName( mxPropSet );
     157             : }
     158             : 
     159             : // Get properties -------------------------------------------------------------
     160             : 
     161           0 : bool ScfPropertySet::HasProperty( const OUString& rPropName ) const
     162             : {
     163           0 :     bool bHasProp = false;
     164             :     try
     165             :     {
     166           0 :         Reference< XPropertyState > xPropState( mxPropSet, UNO_QUERY_THROW );
     167           0 :         bHasProp = xPropState->getPropertyState( rPropName ) == ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
     168             :     }
     169           0 :     catch( Exception& )
     170             :     {
     171             :     }
     172           0 :     return bHasProp;
     173             : }
     174             : 
     175         148 : bool ScfPropertySet::GetAnyProperty( Any& rValue, const OUString& rPropName ) const
     176             : {
     177         148 :     bool bHasValue = false;
     178             :     try
     179             :     {
     180         148 :         if( mxPropSet.is() )
     181             :         {
     182         146 :             rValue = mxPropSet->getPropertyValue( rPropName );
     183         146 :             bHasValue = true;
     184             :         }
     185             :     }
     186           0 :     catch( Exception& )
     187             :     {
     188             :     }
     189         148 :     return bHasValue;
     190             : }
     191             : 
     192          60 : bool ScfPropertySet::GetBoolProperty( const OUString& rPropName ) const
     193             : {
     194          60 :     Any aAny;
     195          60 :     return GetAnyProperty( aAny, rPropName ) && ScUnoHelpFunctions::GetBoolFromAny( aAny );
     196             : }
     197             : 
     198           0 : OUString ScfPropertySet::GetStringProperty( const OUString& rPropName ) const
     199             : {
     200           0 :     OUString aOUString;
     201           0 :     GetProperty( aOUString, rPropName );
     202           0 :     return aOUString;
     203             : }
     204             : 
     205           2 : bool ScfPropertySet::GetColorProperty( Color& rColor, const OUString& rPropName ) const
     206             : {
     207           2 :     sal_Int32 nApiColor = 0;
     208           2 :     bool bRet = GetProperty( nApiColor, rPropName );
     209           2 :     rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
     210           2 :     return bRet;
     211             : }
     212             : 
     213          52 : void ScfPropertySet::GetProperties( Sequence< Any >& rValues, const Sequence< OUString >& rPropNames ) const
     214             : {
     215             :     try
     216             :     {
     217             :         OSL_ENSURE( mxMultiPropSet.is(), "ScfPropertySet::GetProperties - multi property set not available" );
     218          52 :         if( mxMultiPropSet.is() )   // first try the XMultiPropertySet
     219             :         {
     220          50 :             rValues = mxMultiPropSet->getPropertyValues( rPropNames );
     221             :         }
     222           2 :         else if( mxPropSet.is() )
     223             :         {
     224           2 :             sal_Int32 nLen = rPropNames.getLength();
     225           2 :             const OUString* pPropName = rPropNames.getConstArray();
     226           2 :             const OUString* pPropNameEnd = pPropName + nLen;
     227           2 :             rValues.realloc( nLen );
     228           2 :             Any* pValue = rValues.getArray();
     229          12 :             for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
     230          10 :                 *pValue = mxPropSet->getPropertyValue( *pPropName );
     231             :         }
     232             :     }
     233           0 :     catch( Exception& )
     234             :     {
     235             :     }
     236          52 : }
     237             : 
     238             : // Set properties -------------------------------------------------------------
     239             : 
     240        2073 : void ScfPropertySet::SetAnyProperty( const OUString& rPropName, const Any& rValue )
     241             : {
     242             :     try
     243             :     {
     244        2073 :         if( mxPropSet.is() )
     245        2073 :             mxPropSet->setPropertyValue( rPropName, rValue );
     246             :     }
     247           0 :     catch (const Exception&)
     248             :     {
     249             :         OSL_FAIL(OStringBuffer(RTL_CONSTASCII_STRINGPARAM(
     250             :                 "ScfPropertySet::SetAnyProperty - cannot set property \""))
     251             :                 .append(OUStringToOString(rPropName,
     252             :                     RTL_TEXTENCODING_ASCII_US))
     253             :                 .append('"').getStr());
     254             :     }
     255        2073 : }
     256             : 
     257        1300 : void ScfPropertySet::SetProperties( const Sequence< OUString >& rPropNames, const Sequence< Any >& rValues )
     258             : {
     259             :     OSL_ENSURE( rPropNames.getLength() == rValues.getLength(), "ScfPropertySet::SetProperties - length of sequences different" );
     260             :     try
     261             :     {
     262        1300 :         if( mxMultiPropSet.is() )   // first try the XMultiPropertySet
     263             :         {
     264        1300 :             mxMultiPropSet->setPropertyValues( rPropNames, rValues );
     265             :         }
     266           0 :         else if( mxPropSet.is() )
     267             :         {
     268             :             OSL_FAIL( "ScfPropertySet::SetProperties - multi property set not available" );
     269           0 :             const OUString* pPropName = rPropNames.getConstArray();
     270           0 :             const OUString* pPropNameEnd = pPropName + rPropNames.getLength();
     271           0 :             const Any* pValue = rValues.getConstArray();
     272           0 :             for( ; pPropName != pPropNameEnd; ++pPropName, ++pValue )
     273           0 :                 mxPropSet->setPropertyValue( *pPropName, *pValue );
     274             :         }
     275             :     }
     276           0 :     catch( Exception& )
     277             :     {
     278             :         OSL_FAIL( "ScfPropertySet::SetAnyProperty - cannot set multiple properties" );
     279             :     }
     280        1300 : }
     281             : 
     282             : // ============================================================================
     283             : 
     284        1045 : ScfPropSetHelper::ScfPropSetHelper( const sal_Char* const* ppcPropNames ) :
     285        1045 :     mnNextIdx( 0 )
     286             : {
     287             :     OSL_ENSURE( ppcPropNames, "ScfPropSetHelper::ScfPropSetHelper - no strings found" );
     288             : 
     289             :     // create OUStrings from ASCII property names
     290             :     typedef ::std::pair< OUString, size_t >     IndexedOUString;
     291             :     typedef ::std::vector< IndexedOUString >    IndexedOUStringVec;
     292        1045 :     IndexedOUStringVec aPropNameVec;
     293        5060 :     for( size_t nVecIdx = 0; *ppcPropNames; ++ppcPropNames, ++nVecIdx )
     294             :     {
     295        4015 :         OUString aPropName = OUString::createFromAscii( *ppcPropNames );
     296        4015 :         aPropNameVec.push_back( IndexedOUString( aPropName, nVecIdx ) );
     297        4015 :     }
     298             : 
     299             :     // sorts the pairs, which will be sorted by first component (the property name)
     300        1045 :     ::std::sort( aPropNameVec.begin(), aPropNameVec.end() );
     301             : 
     302             :     // resize member sequences
     303        1045 :     size_t nSize = aPropNameVec.size();
     304        1045 :     maNameSeq.realloc( static_cast< sal_Int32 >( nSize ) );
     305        1045 :     maValueSeq.realloc( static_cast< sal_Int32 >( nSize ) );
     306        1045 :     maNameOrder.resize( nSize );
     307             : 
     308             :     // fill the property name sequence and store original sort order
     309        1045 :     sal_Int32 nSeqIdx = 0;
     310        6105 :     for( IndexedOUStringVec::const_iterator aIt = aPropNameVec.begin(),
     311        1045 :             aEnd = aPropNameVec.end(); aIt != aEnd; ++aIt, ++nSeqIdx )
     312             :     {
     313        4015 :         maNameSeq[ nSeqIdx ] = aIt->first;
     314        4015 :         maNameOrder[ aIt->second ] = nSeqIdx;
     315        1045 :     }
     316        1045 : }
     317             : 
     318             : // read properties ------------------------------------------------------------
     319             : 
     320          52 : void ScfPropSetHelper::ReadFromPropertySet( const ScfPropertySet& rPropSet )
     321             : {
     322          52 :     rPropSet.GetProperties( maValueSeq, maNameSeq );
     323          52 :     mnNextIdx = 0;
     324          52 : }
     325             : 
     326          28 : bool ScfPropSetHelper::ReadValue( Any& rAny )
     327             : {
     328          28 :     Any* pAny = GetNextAny();
     329          28 :     if( pAny )
     330          28 :         rAny = *pAny;
     331          28 :     return pAny != 0;
     332             : }
     333             : 
     334           6 : bool ScfPropSetHelper::ReadValue( String& rString )
     335             : {
     336           6 :     OUString aOUString;
     337           6 :     bool bRet = ReadValue( aOUString );
     338           6 :     rString = aOUString;
     339           6 :     return bRet;
     340             : }
     341             : 
     342          28 : bool ScfPropSetHelper::ReadValue( Color& rColor )
     343             : {
     344          28 :     sal_Int32 nApiColor(0);
     345          28 :     bool bRet = ReadValue( nApiColor );
     346          28 :     rColor = ScfApiHelper::ConvertFromApiColor( nApiColor );
     347          28 :     return bRet;
     348             : }
     349             : 
     350          12 : bool ScfPropSetHelper::ReadValue( bool& rbValue )
     351             : {
     352          12 :     Any aAny;
     353          12 :     bool bRet = ReadValue( aAny );
     354          12 :     rbValue = ScUnoHelpFunctions::GetBoolFromAny( aAny );
     355          12 :     return bRet;
     356             : }
     357             : 
     358             : // write properties -----------------------------------------------------------
     359             : 
     360        1300 : void ScfPropSetHelper::InitializeWrite( bool bClearAllAnys )
     361             : {
     362        1300 :     mnNextIdx = 0;
     363        1300 :     if( bClearAllAnys )
     364           0 :         for( sal_Int32 nIdx = 0, nLen = maValueSeq.getLength(); nIdx < nLen; ++nIdx )
     365           0 :             maValueSeq[ nIdx ].clear();
     366        1300 : }
     367             : 
     368         299 : void ScfPropSetHelper::WriteValue( const Any& rAny )
     369             : {
     370         299 :     if( Any* pAny = GetNextAny() )
     371         299 :         *pAny = rAny;
     372         299 : }
     373             : 
     374         362 : void ScfPropSetHelper::WriteValue( const bool& rbValue )
     375             : {
     376         362 :     if( Any* pAny = GetNextAny() )
     377         362 :         ScUnoHelpFunctions::SetBoolInAny( *pAny, rbValue );
     378         362 : }
     379             : 
     380        1300 : void ScfPropSetHelper::WriteToPropertySet( ScfPropertySet& rPropSet ) const
     381             : {
     382        1300 :     rPropSet.SetProperties( maNameSeq, maValueSeq );
     383        1300 : }
     384             : 
     385             : // private --------------------------------------------------------------------
     386             : 
     387        5414 : Any* ScfPropSetHelper::GetNextAny()
     388             : {
     389             :     OSL_ENSURE( mnNextIdx < maNameOrder.size(), "ScfPropSetHelper::GetNextAny - sequence overflow" );
     390        5414 :     Any* pAny = 0;
     391        5414 :     if( mnNextIdx < maNameOrder.size() )
     392        5414 :         pAny = &maValueSeq[ maNameOrder[ mnNextIdx++ ] ];
     393        5414 :     return pAny;
     394          15 : }
     395             : 
     396             : // ============================================================================
     397             : 
     398             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10