LCOV - code coverage report
Current view: top level - vcl/source/filter - FilterConfigItem.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 61 206 29.6 %
Date: 2014-04-11 Functions: 8 16 50.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 <vcl/FilterConfigItem.hxx>
      21             : 
      22             : #include <unotools/configmgr.hxx>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <comphelper/string.hxx>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      27             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      28             : #include <com/sun/star/util/XChangesBatch.hpp>
      29             : #include <com/sun/star/beans/XPropertySetInfo.hpp>
      30             : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
      31             : 
      32             : using namespace ::com::sun::star::lang      ;   // XMultiServiceFactory
      33             : using namespace ::com::sun::star::beans     ;   // PropertyValue
      34             : using namespace ::com::sun::star::uno       ;   // Reference
      35             : using namespace ::com::sun::star::util      ;   // XChangesBatch
      36             : using namespace ::com::sun::star::awt       ;   // Size
      37             : using namespace ::com::sun::star::container ;
      38             : using namespace ::com::sun::star::configuration;
      39             : using namespace ::com::sun::star::task      ;   // XStatusIndicator
      40             : 
      41           0 : static bool ImpIsTreeAvailable( Reference< XMultiServiceFactory >& rXCfgProv, const OUString& rTree )
      42             : {
      43           0 :     bool bAvailable = !rTree.isEmpty();
      44           0 :     if ( bAvailable )
      45             :     {
      46             :         using comphelper::string::getTokenCount;
      47             :         using comphelper::string::getToken;
      48             : 
      49           0 :         sal_Int32 nTokenCount = getTokenCount(rTree, '/');
      50           0 :         sal_Int32 i = 0;
      51             : 
      52           0 :         if ( rTree[0] == '/' )
      53           0 :             ++i;
      54           0 :         if ( rTree.endsWith("/") )
      55           0 :             --nTokenCount;
      56             : 
      57           0 :         Any aAny;
      58           0 :         aAny <<= getToken(rTree, i++, '/');
      59             : 
      60             :         // creation arguments: nodepath
      61           0 :         PropertyValue aPathArgument;
      62           0 :         aPathArgument.Name = "nodepath";
      63           0 :         aPathArgument.Value = aAny;
      64             : 
      65           0 :         Sequence< Any > aArguments( 1 );
      66           0 :         aArguments[ 0 ] <<= aPathArgument;
      67             : 
      68           0 :         Reference< XInterface > xReadAccess;
      69             :         try
      70             :         {
      71           0 :             xReadAccess = rXCfgProv->createInstanceWithArguments(
      72             :                 OUString( "com.sun.star.configuration.ConfigurationAccess" ),
      73           0 :                     aArguments );
      74             :         }
      75           0 :         catch (const ::com::sun::star::uno::Exception&)
      76             :         {
      77           0 :             bAvailable = false;
      78             :         }
      79           0 :         if ( xReadAccess.is() )
      80             :         {
      81           0 :             for ( ; bAvailable && ( i < nTokenCount ); i++ )
      82             :             {
      83             :                 Reference< XHierarchicalNameAccess > xHierarchicalNameAccess
      84           0 :                     ( xReadAccess, UNO_QUERY );
      85             : 
      86           0 :                 if ( !xHierarchicalNameAccess.is() )
      87           0 :                     bAvailable = false;
      88             :                 else
      89             :                 {
      90           0 :                     OUString aNode( getToken(rTree, i, '/') );
      91           0 :                     if ( !xHierarchicalNameAccess->hasByHierarchicalName( aNode ) )
      92           0 :                         bAvailable = false;
      93             :                     else
      94             :                     {
      95           0 :                         Any a( xHierarchicalNameAccess->getByHierarchicalName( aNode ) );
      96           0 :                         bAvailable = (a >>= xReadAccess);
      97           0 :                     }
      98             :                 }
      99           0 :             }
     100           0 :         }
     101             :     }
     102           0 :     return bAvailable;
     103             : }
     104             : 
     105           0 : void FilterConfigItem::ImpInitTree( const OUString& rSubTree )
     106             : {
     107           0 :     bModified = false;
     108             : 
     109           0 :     Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() );
     110             : 
     111           0 :     Reference< XMultiServiceFactory > xCfgProv = theDefaultProvider::get( xContext );
     112             : 
     113           0 :     OUString sTree = "/org.openoffice." + rSubTree;
     114           0 :     if ( ImpIsTreeAvailable(xCfgProv, sTree) )
     115             :     {
     116           0 :         Any aAny;
     117             :         // creation arguments: nodepath
     118           0 :         PropertyValue aPathArgument;
     119           0 :         aAny <<= sTree;
     120           0 :         aPathArgument.Name = "nodepath";
     121           0 :         aPathArgument.Value = aAny;
     122             : 
     123             :         // creation arguments: commit mode
     124           0 :         PropertyValue aModeArgument;
     125           0 :         bool bAsyncron = true;
     126           0 :         aAny <<= bAsyncron;
     127           0 :         aModeArgument.Name = "lazywrite";
     128           0 :         aModeArgument.Value = aAny;
     129             : 
     130           0 :         Sequence< Any > aArguments( 2 );
     131           0 :         aArguments[ 0 ] <<= aPathArgument;
     132           0 :         aArguments[ 1 ] <<= aModeArgument;
     133             : 
     134             :         try
     135             :         {
     136           0 :             xUpdatableView = xCfgProv->createInstanceWithArguments(
     137             :                 OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ),
     138           0 :                     aArguments );
     139           0 :             if ( xUpdatableView.is() )
     140           0 :                 xPropSet = Reference< XPropertySet >( xUpdatableView, UNO_QUERY );
     141             :         }
     142           0 :         catch ( ::com::sun::star::uno::Exception& )
     143             :         {
     144             :             OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not access configuration Key" );
     145           0 :         }
     146           0 :     }
     147           0 : }
     148             : 
     149           0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree )
     150             : {
     151           0 :     ImpInitTree( rSubTree );
     152           0 : }
     153             : 
     154         139 : FilterConfigItem::FilterConfigItem( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
     155         139 :     : bModified(false)
     156             : {
     157         139 :     if ( pFilterData )
     158          14 :         aFilterData = *pFilterData;
     159         139 : }
     160             : 
     161           0 : FilterConfigItem::FilterConfigItem( const OUString& rSubTree,
     162           0 :     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >* pFilterData )
     163             : {
     164           0 :     ImpInitTree( rSubTree );
     165             : 
     166           0 :     if ( pFilterData )
     167           0 :         aFilterData = *pFilterData;
     168           0 : };
     169             : 
     170         278 : FilterConfigItem::~FilterConfigItem()
     171             : {
     172         139 :     if ( xUpdatableView.is() )
     173             :     {
     174           0 :         if ( xPropSet.is() && bModified )
     175             :         {
     176           0 :             Reference< XChangesBatch > xUpdateControl( xUpdatableView, UNO_QUERY );
     177           0 :             if ( xUpdateControl.is() )
     178             :             {
     179             :                 try
     180             :                 {
     181           0 :                     xUpdateControl->commitChanges();
     182             :                 }
     183           0 :                 catch ( ::com::sun::star::uno::Exception& )
     184             :                 {
     185             :                     OSL_FAIL( "FilterConfigItem::FilterConfigItem - Could not update configuration data" );
     186             :                 }
     187           0 :             }
     188             :         }
     189             :     }
     190         139 : }
     191             : 
     192         455 : bool FilterConfigItem::ImplGetPropertyValue( Any& rAny, const Reference< XPropertySet >& rXPropSet, const OUString& rString, bool bTestPropertyAvailability )
     193             : {
     194         455 :     bool bRetValue = true;
     195             : 
     196         455 :     if ( rXPropSet.is() )
     197             :     {
     198           0 :         if ( bTestPropertyAvailability )
     199             :         {
     200           0 :             bRetValue = false;
     201             :             try
     202             :             {
     203             :                 Reference< XPropertySetInfo >
     204           0 :                     aXPropSetInfo( rXPropSet->getPropertySetInfo() );
     205           0 :                 if ( aXPropSetInfo.is() )
     206           0 :                     bRetValue = aXPropSetInfo->hasPropertyByName( rString );
     207             :             }
     208           0 :             catch( ::com::sun::star::uno::Exception& )
     209             :             {
     210             : 
     211             :             }
     212             :         }
     213           0 :         if ( bRetValue )
     214             :         {
     215             :             try
     216             :             {
     217           0 :                 rAny = rXPropSet->getPropertyValue( rString );
     218           0 :                 if ( !rAny.hasValue() )
     219           0 :                     bRetValue = false;
     220             :             }
     221           0 :             catch( ::com::sun::star::uno::Exception& )
     222             :             {
     223           0 :                 bRetValue = false;
     224             :             }
     225             :         }
     226             :     }
     227             :     else
     228         455 :         bRetValue = false;
     229         455 :     return bRetValue;
     230             : }
     231             : 
     232             : // if property is available it returns a pointer,
     233             : // otherwise the result is null
     234         476 : PropertyValue* FilterConfigItem::GetPropertyValue( Sequence< PropertyValue >& rPropSeq, const OUString& rName )
     235             : {
     236         476 :     PropertyValue* pPropValue = NULL;
     237             : 
     238             :     sal_Int32 i, nCount;
     239        1321 :     for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
     240             :     {
     241         866 :         if ( rPropSeq[ i ].Name == rName )
     242             :         {
     243          21 :             pPropValue = &rPropSeq[ i ];
     244          21 :             break;
     245             :         }
     246             :     }
     247         476 :     return pPropValue;
     248             : }
     249             : 
     250             : /* if PropertySequence already includes a PropertyValue using the same name, the
     251             :     corresponding PropertyValue is replaced, otherwise the given PropertyValue
     252             :     will be appended */
     253             : 
     254         476 : bool FilterConfigItem::WritePropertyValue( Sequence< PropertyValue >& rPropSeq, const PropertyValue& rPropValue )
     255             : {
     256         476 :     bool bRet = false;
     257         476 :     if ( !rPropValue.Name.isEmpty() )
     258             :     {
     259             :         sal_Int32 i, nCount;
     260        1321 :         for ( i = 0, nCount = rPropSeq.getLength(); i < nCount; i++ )
     261             :         {
     262         866 :             if ( rPropSeq[ i ].Name == rPropValue.Name )
     263          21 :                 break;
     264             :         }
     265         476 :         if ( i == nCount )
     266         455 :             rPropSeq.realloc( ++nCount );
     267             : 
     268         476 :         rPropSeq[ i ] = rPropValue;
     269             : 
     270         476 :         bRet = true;
     271             :     }
     272         476 :     return bRet;
     273             : }
     274             : 
     275           1 : bool FilterConfigItem::ReadBool( const OUString& rKey, bool bDefault )
     276             : {
     277           1 :     Any aAny;
     278           1 :     bool bRetValue = bDefault;
     279           1 :     PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
     280           1 :     if ( pPropVal )
     281             :     {
     282           0 :         pPropVal->Value >>= bRetValue;
     283             :     }
     284           1 :     else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
     285             :     {
     286           0 :         aAny >>= bRetValue;
     287             :     }
     288           2 :     PropertyValue aBool;
     289           1 :     aBool.Name = rKey;
     290           1 :     aBool.Value <<= bRetValue;
     291           1 :     WritePropertyValue( aFilterData, aBool );
     292           2 :     return bRetValue;
     293             : }
     294             : 
     295         475 : sal_Int32 FilterConfigItem::ReadInt32( const OUString& rKey, sal_Int32 nDefault )
     296             : {
     297         475 :     Any aAny;
     298         475 :     sal_Int32 nRetValue = nDefault;
     299         475 :     PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
     300         475 :     if ( pPropVal )
     301             :     {
     302          21 :         pPropVal->Value >>= nRetValue;
     303             :     }
     304         454 :     else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
     305             :     {
     306           0 :         aAny >>= nRetValue;
     307             :     }
     308         950 :     PropertyValue aInt32;
     309         475 :     aInt32.Name = rKey;
     310         475 :     aInt32.Value <<= nRetValue;
     311         475 :     WritePropertyValue( aFilterData, aInt32 );
     312         950 :     return nRetValue;
     313             : }
     314             : 
     315           0 : OUString FilterConfigItem::ReadString( const OUString& rKey, const OUString& rDefault )
     316             : {
     317           0 :     Any aAny;
     318           0 :     OUString aRetValue( rDefault );
     319           0 :     PropertyValue* pPropVal = GetPropertyValue( aFilterData, rKey );
     320           0 :     if ( pPropVal )
     321             :     {
     322           0 :         pPropVal->Value >>= aRetValue;
     323             :     }
     324           0 :     else if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
     325             :     {
     326           0 :         aAny >>= aRetValue;
     327             :     }
     328           0 :     PropertyValue aString;
     329           0 :     aString.Name = rKey;
     330           0 :     aString.Value <<= aRetValue;
     331           0 :     WritePropertyValue( aFilterData, aString );
     332           0 :     return aRetValue;
     333             : }
     334             : 
     335           0 : void FilterConfigItem::WriteBool( const OUString& rKey, bool bNewValue )
     336             : {
     337           0 :     PropertyValue aBool;
     338           0 :     aBool.Name = rKey;
     339           0 :     aBool.Value <<= bNewValue;
     340           0 :     WritePropertyValue( aFilterData, aBool );
     341             : 
     342           0 :     if ( xPropSet.is() )
     343             :     {
     344           0 :         Any aAny;
     345           0 :         if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
     346             :         {
     347           0 :             bool bOldValue(true);
     348           0 :             if ( aAny >>= bOldValue )
     349             :             {
     350           0 :                 if ( bOldValue != bNewValue )
     351             :                 {
     352           0 :                     aAny <<= bNewValue;
     353             :                     try
     354             :                     {
     355           0 :                         xPropSet->setPropertyValue( rKey, aAny );
     356           0 :                         bModified = true;
     357             :                     }
     358           0 :                     catch ( ::com::sun::star::uno::Exception& )
     359             :                     {
     360             :                         OSL_FAIL( "FilterConfigItem::WriteBool - could not set PropertyValue" );
     361             :                     }
     362             :                 }
     363             :             }
     364           0 :         }
     365           0 :     }
     366           0 : }
     367             : 
     368           0 : void FilterConfigItem::WriteInt32( const OUString& rKey, sal_Int32 nNewValue )
     369             : {
     370           0 :     PropertyValue aInt32;
     371           0 :     aInt32.Name = rKey;
     372           0 :     aInt32.Value <<= nNewValue;
     373           0 :     WritePropertyValue( aFilterData, aInt32 );
     374             : 
     375           0 :     if ( xPropSet.is() )
     376             :     {
     377           0 :         Any aAny;
     378             : 
     379           0 :         if ( ImplGetPropertyValue( aAny, xPropSet, rKey, true ) )
     380             :         {
     381           0 :             sal_Int32 nOldValue = 0;
     382           0 :             if ( aAny >>= nOldValue )
     383             :             {
     384           0 :                 if ( nOldValue != nNewValue )
     385             :                 {
     386           0 :                     aAny <<= nNewValue;
     387             :                     try
     388             :                     {
     389           0 :                         xPropSet->setPropertyValue( rKey, aAny );
     390           0 :                         bModified = true;
     391             :                     }
     392           0 :                     catch ( ::com::sun::star::uno::Exception& )
     393             :                     {
     394             :                         OSL_FAIL( "FilterConfigItem::WriteInt32 - could not set PropertyValue" );
     395             :                     }
     396             :                 }
     397             :             }
     398           0 :         }
     399           0 :     }
     400           0 : }
     401             : 
     402           0 : Sequence< PropertyValue > FilterConfigItem::GetFilterData() const
     403             : {
     404           0 :     return aFilterData;
     405             : }
     406             : 
     407          31 : Reference< XStatusIndicator > FilterConfigItem::GetStatusIndicator() const
     408             : {
     409          31 :     Reference< XStatusIndicator > xStatusIndicator;
     410          62 :     const OUString sStatusIndicator( "StatusIndicator" );
     411             : 
     412          31 :     sal_Int32 i, nCount = aFilterData.getLength();
     413         124 :     for ( i = 0; i < nCount; i++ )
     414             :     {
     415          93 :         if ( aFilterData[ i ].Name == sStatusIndicator )
     416             :         {
     417           0 :             aFilterData[ i ].Value >>= xStatusIndicator;
     418           0 :             break;
     419             :         }
     420             :     }
     421          62 :     return xStatusIndicator;
     422             : }
     423             : 
     424             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10