LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - addruno.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 96 150 64.0 %
Date: 2014-04-11 Functions: 9 17 52.9 %
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 <com/sun/star/table/CellAddress.hpp>
      21             : #include <com/sun/star/table/CellRangeAddress.hpp>
      22             : #include <cppuhelper/supportsservice.hxx>
      23             : 
      24             : #include <svl/itemprop.hxx>
      25             : #include <vcl/svapp.hxx>
      26             : 
      27             : #include "docsh.hxx"
      28             : #include "unonames.hxx"
      29             : #include "miscuno.hxx"
      30             : #include "convuno.hxx"
      31             : #include "addruno.hxx"
      32             : 
      33             : using namespace com::sun::star;
      34             : 
      35           8 : ScAddressConversionObj::ScAddressConversionObj(ScDocShell* pDocSh, bool _bIsRange) :
      36             :     pDocShell( pDocSh ),
      37             :     nRefSheet( 0 ),
      38           8 :     bIsRange( _bIsRange )
      39             : {
      40           8 :     pDocShell->GetDocument()->AddUnoObject(*this);
      41           8 : }
      42             : 
      43          24 : ScAddressConversionObj::~ScAddressConversionObj()
      44             : {
      45           8 :     if (pDocShell)
      46           6 :         pDocShell->GetDocument()->RemoveUnoObject(*this);
      47          16 : }
      48             : 
      49         142 : void ScAddressConversionObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
      50             : {
      51         284 :     if ( rHint.ISA( SfxSimpleHint ) &&
      52         142 :             ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
      53             :     {
      54           2 :         pDocShell = NULL;       // invalid
      55             :     }
      56         142 : }
      57             : 
      58           2 : bool ScAddressConversionObj::ParseUIString( const OUString& rUIString, ::formula::FormulaGrammar::AddressConvention eConv )
      59             : {
      60           2 :     if (!pDocShell)
      61           0 :         return false;
      62             : 
      63           2 :     ScDocument* pDoc = pDocShell->GetDocument();
      64           2 :     bool bSuccess = false;
      65           2 :     if ( bIsRange )
      66             :     {
      67           2 :         sal_uInt16 nResult = aRange.ParseAny( rUIString, pDoc, eConv );
      68           2 :         if ( nResult & SCA_VALID )
      69             :         {
      70           2 :             if ( ( nResult & SCA_TAB_3D ) == 0 )
      71           1 :                 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
      72           2 :             if ( ( nResult & SCA_TAB2_3D ) == 0 )
      73           1 :                 aRange.aEnd.SetTab( aRange.aStart.Tab() );
      74             :             // different sheets are not supported in CellRangeAddress
      75           2 :             if ( aRange.aStart.Tab() == aRange.aEnd.Tab() )
      76           2 :                 bSuccess = true;
      77             :         }
      78             :     }
      79             :     else
      80             :     {
      81           0 :         sal_uInt16 nResult = aRange.aStart.Parse( rUIString, pDoc, eConv );
      82           0 :         if ( nResult & SCA_VALID )
      83             :         {
      84           0 :             if ( ( nResult & SCA_TAB_3D ) == 0 )
      85           0 :                 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
      86           0 :             bSuccess = true;
      87             :         }
      88             :     }
      89           2 :     return bSuccess;
      90             : }
      91             : 
      92             : // XPropertySet
      93             : 
      94           1 : uno::Reference<beans::XPropertySetInfo> SAL_CALL ScAddressConversionObj::getPropertySetInfo()
      95             :                                                         throw(uno::RuntimeException, std::exception)
      96             : {
      97           1 :     SolarMutexGuard aGuard;
      98             : 
      99           1 :     if ( bIsRange )
     100             :     {
     101             :         static const SfxItemPropertyMapEntry aPropertyMap[] =
     102             :         {
     103           1 :             { OUString(SC_UNONAME_ADDRESS),  0,  getCppuType((table::CellRangeAddress*)0), 0, 0 },
     104           1 :             { OUString(SC_UNONAME_PERSREPR), 0,  getCppuType((OUString*)0),    0, 0 },
     105           1 :             { OUString(SC_UNONAME_XLA1REPR), 0,  getCppuType((OUString*)0),    0, 0 },
     106           1 :             { OUString(SC_UNONAME_REFSHEET), 0,  getCppuType((sal_Int32*)0),        0, 0 },
     107           1 :             { OUString(SC_UNONAME_UIREPR),   0,  getCppuType((OUString*)0),    0, 0 },
     108           1 :             { OUString(SC_UNONAME_XLA1REPR), 0,  getCppuType((OUString*)0),    0, 0 },
     109             :             { OUString(), 0, css::uno::Type(), 0, 0 }
     110           8 :         };
     111           1 :         static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
     112           1 :         return aRef;
     113             :     }
     114             :     else
     115             :     {
     116             :         static const SfxItemPropertyMapEntry aPropertyMap[] =
     117             :         {
     118           0 :             { OUString(SC_UNONAME_ADDRESS),  0,  getCppuType((table::CellAddress*)0), 0, 0 },
     119           0 :             { OUString(SC_UNONAME_PERSREPR), 0,  getCppuType((OUString*)0),    0, 0 },
     120           0 :             { OUString(SC_UNONAME_XLA1REPR), 0,  getCppuType((OUString*)0),    0, 0 },
     121           0 :             { OUString(SC_UNONAME_REFSHEET), 0,  getCppuType((sal_Int32*)0),        0, 0 },
     122           0 :             { OUString(SC_UNONAME_UIREPR),   0,  getCppuType((OUString*)0),    0, 0 },
     123           0 :             { OUString(SC_UNONAME_XLA1REPR), 0,  getCppuType((OUString*)0),    0, 0 },
     124             :             { OUString(), 0, css::uno::Type(), 0, 0 }
     125           0 :         };
     126           0 :         static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
     127           0 :         return aRef;
     128           1 :     }
     129             : }
     130             : 
     131           4 : void SAL_CALL ScAddressConversionObj::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
     132             :                 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
     133             :                         lang::IllegalArgumentException, lang::WrappedTargetException,
     134             :                         uno::RuntimeException, std::exception)
     135             : {
     136           4 :     if ( !pDocShell )
     137           0 :         throw uno::RuntimeException();
     138             : 
     139           4 :     bool bSuccess = false;
     140           4 :     OUString aNameStr(aPropertyName);
     141           4 :     if ( aNameStr.equalsAscii( SC_UNONAME_ADDRESS ) )
     142             :     {
     143             :         //  read the cell/range address from API struct
     144           1 :         if ( bIsRange )
     145             :         {
     146           1 :             table::CellRangeAddress aRangeAddress;
     147           1 :             if ( aValue >>= aRangeAddress )
     148             :             {
     149           1 :                 ScUnoConversion::FillScRange( aRange, aRangeAddress );
     150           1 :                 bSuccess = true;
     151             :             }
     152             :         }
     153             :         else
     154             :         {
     155           0 :             table::CellAddress aCellAddress;
     156           0 :             if ( aValue >>= aCellAddress )
     157             :             {
     158           0 :                 ScUnoConversion::FillScAddress( aRange.aStart, aCellAddress );
     159           0 :                 bSuccess = true;
     160             :             }
     161             :         }
     162             :     }
     163           3 :     else if ( aNameStr.equalsAscii( SC_UNONAME_REFSHEET ) )
     164             :     {
     165             :         //  set the reference sheet
     166           1 :         sal_Int32 nIntVal = 0;
     167           1 :         if ( aValue >>= nIntVal )
     168             :         {
     169           1 :             nRefSheet = nIntVal;
     170           1 :             bSuccess = true;
     171             :         }
     172             :     }
     173           2 :     else if ( aNameStr.equalsAscii( SC_UNONAME_UIREPR ) )
     174             :     {
     175             :         //  parse the UI representation string
     176           0 :         OUString sRepresentation;
     177           0 :         if (aValue >>= sRepresentation)
     178             :         {
     179           0 :             OUString aUIString = sRepresentation;
     180           0 :             bSuccess = ParseUIString( aUIString );
     181           0 :         }
     182             :     }
     183           2 :     else if ( aNameStr.equalsAscii( SC_UNONAME_PERSREPR ) || aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) )
     184             :     {
     185           2 :         ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) ?
     186           2 :             ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
     187             : 
     188             :         //  parse the file format string
     189           2 :         OUString sRepresentation;
     190           2 :         if (aValue >>= sRepresentation)
     191             :         {
     192           2 :             OUString aUIString(sRepresentation);
     193             : 
     194             :             //  cell or range: strip a single "." at the start
     195           2 :             if ( aUIString[0]== (sal_Unicode) '.' )
     196           0 :                 aUIString = aUIString.copy( 1 );
     197             : 
     198           2 :             if ( bIsRange )
     199             :             {
     200             :                 //  range: also strip a "." after the last colon
     201           2 :                 sal_Int32 nColon = OUString(aUIString).lastIndexOf( (sal_Unicode) ':' );
     202           4 :                 if ( nColon >= 0 && nColon < aUIString.getLength() - 1 &&
     203           2 :                      aUIString[nColon+1] == '.' )
     204           0 :                     aUIString = aUIString.replaceAt( nColon+1, 1, "" );
     205             :             }
     206             : 
     207             :             //  parse the rest like a UI string
     208           2 :             bSuccess = ParseUIString( aUIString, eConv );
     209           2 :         }
     210             :     }
     211             :     else
     212           0 :         throw beans::UnknownPropertyException();
     213             : 
     214           4 :     if ( !bSuccess )
     215           0 :         throw lang::IllegalArgumentException();
     216           4 : }
     217             : 
     218           3 : uno::Any SAL_CALL ScAddressConversionObj::getPropertyValue( const OUString& aPropertyName )
     219             :                 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
     220             :                         uno::RuntimeException, std::exception)
     221             : {
     222           3 :     if ( !pDocShell )
     223           0 :         throw uno::RuntimeException();
     224             : 
     225           3 :     ScDocument* pDoc = pDocShell->GetDocument();
     226           3 :     uno::Any aRet;
     227             : 
     228           6 :     OUString aNameStr(aPropertyName);
     229           3 :     if ( aNameStr.equalsAscii( SC_UNONAME_ADDRESS ) )
     230             :     {
     231           2 :         if ( bIsRange )
     232             :         {
     233           2 :             table::CellRangeAddress aRangeAddress;
     234           2 :             ScUnoConversion::FillApiRange( aRangeAddress, aRange );
     235           2 :             aRet <<= aRangeAddress;
     236             :         }
     237             :         else
     238             :         {
     239           0 :             table::CellAddress aCellAddress;
     240           0 :             ScUnoConversion::FillApiAddress( aCellAddress, aRange.aStart );
     241           0 :             aRet <<= aCellAddress;
     242             :         }
     243             :     }
     244           1 :     else if ( aNameStr.equalsAscii( SC_UNONAME_REFSHEET ) )
     245             :     {
     246           0 :         aRet <<= nRefSheet;
     247             :     }
     248           1 :     else if ( aNameStr.equalsAscii( SC_UNONAME_UIREPR ) )
     249             :     {
     250             :         //  generate UI representation string - include sheet only if different from ref sheet
     251           0 :         OUString aFormatStr;
     252           0 :         sal_uInt16 nFlags = SCA_VALID;
     253           0 :         if ( aRange.aStart.Tab() != nRefSheet )
     254           0 :             nFlags |= SCA_TAB_3D;
     255           0 :         if ( bIsRange )
     256           0 :             aFormatStr = aRange.Format(nFlags, pDoc);
     257             :         else
     258           0 :             aFormatStr = aRange.aStart.Format(nFlags, pDoc);
     259           0 :         aRet <<= aFormatStr;
     260             :     }
     261           1 :     else if ( aNameStr.equalsAscii( SC_UNONAME_PERSREPR ) || aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) )
     262             :     {
     263           1 :         ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) ?
     264           1 :             ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
     265             : 
     266             :         //  generate file format string - always include sheet
     267           1 :         OUString aFormatStr(aRange.aStart.Format(SCA_VALID | SCA_TAB_3D, pDoc, eConv));
     268           1 :         if ( bIsRange )
     269             :         {
     270             :             //  manually concatenate range so both parts always have the sheet name
     271           1 :             aFormatStr += ":";
     272           1 :             sal_uInt16 nFlags = SCA_VALID;
     273           1 :             if( eConv != ::formula::FormulaGrammar::CONV_XL_A1 )
     274           1 :                 nFlags |= SCA_TAB_3D;
     275           1 :             OUString aSecond(aRange.aEnd.Format(nFlags, pDoc, eConv));
     276           1 :             aFormatStr += aSecond ;
     277             :         }
     278           1 :         aRet <<= OUString( aFormatStr );
     279             :     }
     280             :     else
     281           0 :         throw beans::UnknownPropertyException();
     282             : 
     283           6 :     return aRet;
     284             : }
     285             : 
     286           0 : SC_IMPL_DUMMY_PROPERTY_LISTENER( ScAddressConversionObj )
     287             : 
     288             : // lang::XServiceInfo
     289             : 
     290           0 : OUString SAL_CALL ScAddressConversionObj::getImplementationName() throw(uno::RuntimeException, std::exception)
     291             : {
     292           0 :     return OUString("ScAddressConversionObj" );
     293             : }
     294             : 
     295           0 : sal_Bool SAL_CALL ScAddressConversionObj::supportsService( const OUString& rServiceName )
     296             :                                                     throw(uno::RuntimeException, std::exception)
     297             : {
     298           0 :     return cppu::supportsService(this, rServiceName);
     299             : }
     300             : 
     301           0 : uno::Sequence<OUString> SAL_CALL ScAddressConversionObj::getSupportedServiceNames()
     302             :                                                     throw(uno::RuntimeException, std::exception)
     303             : {
     304           0 :     uno::Sequence<OUString> aRet(1);
     305           0 :     OUString* pArray = aRet.getArray();
     306           0 :     pArray[0] = bIsRange ? OUString(SC_SERVICENAME_RANGEADDRESS)
     307           0 :                          : OUString(SC_SERVICENAME_CELLADDRESS);
     308           0 :     return aRet;
     309             : }
     310             : 
     311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10