LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/oox - worksheetbuffer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 60 91 65.9 %
Date: 2012-12-27 Functions: 13 18 72.2 %
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 "worksheetbuffer.hxx"
      21             : 
      22             : #include <com/sun/star/container/XIndexAccess.hpp>
      23             : #include <com/sun/star/container/XNameAccess.hpp>
      24             : #include <com/sun/star/container/XNamed.hpp>
      25             : #include <com/sun/star/sheet/XExternalSheetName.hpp>
      26             : #include <com/sun/star/sheet/XSheetLinkable.hpp>
      27             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      28             : #include <rtl/ustrbuf.hxx>
      29             : #include "oox/core/filterbase.hxx"
      30             : #include "oox/helper/attributelist.hxx"
      31             : #include "oox/helper/containerhelper.hxx"
      32             : #include "oox/helper/propertyset.hxx"
      33             : #include "oox/token/properties.hxx"
      34             : #include "biffinputstream.hxx"
      35             : #include "excelhandlers.hxx"
      36             : 
      37             : namespace oox {
      38             : namespace xls {
      39             : 
      40             : // ============================================================================
      41             : 
      42             : using namespace ::com::sun::star::container;
      43             : using namespace ::com::sun::star::sheet;
      44             : using namespace ::com::sun::star::uno;
      45             : 
      46             : using ::rtl::OUString;
      47             : using ::rtl::OUStringBuffer;
      48             : 
      49             : // ============================================================================
      50             : 
      51          25 : SheetInfoModel::SheetInfoModel() :
      52             :     mnBiffHandle( -1 ),
      53             :     mnSheetId( -1 ),
      54          25 :     mnState( XML_visible )
      55             : {
      56          25 : }
      57             : 
      58             : // ============================================================================
      59             : 
      60          11 : WorksheetBuffer::WorksheetBuffer( const WorkbookHelper& rHelper ) :
      61          11 :     WorkbookHelper( rHelper )
      62             : {
      63          11 : }
      64             : 
      65          25 : void WorksheetBuffer::importSheet( const AttributeList& rAttribs )
      66             : {
      67          25 :     SheetInfoModel aModel;
      68          25 :     aModel.maRelId = rAttribs.getString( R_TOKEN( id ), OUString() );
      69          25 :     aModel.maName = rAttribs.getXString( XML_name, OUString() );
      70          25 :     aModel.mnSheetId = rAttribs.getInteger( XML_sheetId, -1 );
      71          25 :     aModel.mnState = rAttribs.getToken( XML_state, XML_visible );
      72          25 :     insertSheet( aModel );
      73          25 : }
      74             : 
      75           0 : void WorksheetBuffer::importSheet( SequenceInputStream& rStrm )
      76             : {
      77             :     sal_Int32 nState;
      78           0 :     SheetInfoModel aModel;
      79           0 :     rStrm >> nState >> aModel.mnSheetId >> aModel.maRelId >> aModel.maName;
      80             :     static const sal_Int32 spnStates[] = { XML_visible, XML_hidden, XML_veryHidden };
      81           0 :     aModel.mnState = STATIC_ARRAY_SELECT( spnStates, nState, XML_visible );
      82           0 :     insertSheet( aModel );
      83           0 : }
      84             : 
      85           0 : sal_Int16 WorksheetBuffer::insertEmptySheet( const OUString& rPreferredName, bool bVisible )
      86             : {
      87           0 :     return createSheet( rPreferredName, SAL_MAX_INT32, bVisible ).first;
      88             : }
      89             : 
      90          22 : sal_Int32 WorksheetBuffer::getWorksheetCount() const
      91             : {
      92          22 :     return static_cast< sal_Int32 >( maSheetInfos.size() );
      93             : }
      94             : 
      95          25 : OUString WorksheetBuffer::getWorksheetRelId( sal_Int32 nWorksheet ) const
      96             : {
      97          25 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
      98          25 :     return pSheetInfo ? pSheetInfo->maRelId : OUString();
      99             : }
     100             : 
     101          55 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( sal_Int32 nWorksheet ) const
     102             : {
     103          55 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
     104          55 :     return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
     105             : }
     106             : 
     107          36 : OUString WorksheetBuffer::getCalcSheetName( sal_Int32 nWorksheet ) const
     108             : {
     109          36 :     const SheetInfo* pSheetInfo = maSheetInfos.get( nWorksheet ).get();
     110          36 :     return pSheetInfo ? pSheetInfo->maCalcName : OUString();
     111             : }
     112             : 
     113           0 : void WorksheetBuffer::convertSheetNameRef( ::rtl::OUString& sSheetNameRef ) const
     114             : {
     115             :     // convert '#SheetName!A1' to '#SheetName.A1'
     116           0 :     if( !sSheetNameRef.isEmpty() && (sSheetNameRef[ 0 ] == '#') )
     117             :     {
     118           0 :         sal_Int32 nSepPos = sSheetNameRef.lastIndexOf( '!' );
     119           0 :         if( nSepPos > 0 )
     120             :         {
     121             :             // replace the exclamation mark with a period
     122           0 :             sSheetNameRef = sSheetNameRef.replaceAt( nSepPos, 1, OUString( sal_Unicode( '.' ) ) );
     123             :             // #i66592# convert sheet names that have been renamed on import
     124           0 :             OUString aSheetName = sSheetNameRef.copy( 1, nSepPos - 1 );
     125           0 :             OUString aCalcName = getCalcSheetName( aSheetName );
     126           0 :             if( !aCalcName.isEmpty() )
     127           0 :                 sSheetNameRef = sSheetNameRef.replaceAt( 1, nSepPos - 1, aCalcName );
     128             :         }
     129             :     }
     130           0 : }
     131             : 
     132           0 : sal_Int16 WorksheetBuffer::getCalcSheetIndex( const OUString& rWorksheetName ) const
     133             : {
     134           0 :     const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get();
     135           0 :     return pSheetInfo ? pSheetInfo->mnCalcSheet : -1;
     136             : }
     137             : 
     138           0 : OUString WorksheetBuffer::getCalcSheetName( const OUString& rWorksheetName ) const
     139             : {
     140           0 :     if( const SheetInfo* pSheetInfo = maSheetInfosByName.get( rWorksheetName ).get() )
     141             :     {
     142           0 :         bool bIsQuoted = pSheetInfo->maName != rWorksheetName;
     143           0 :         return bIsQuoted ? pSheetInfo->maCalcQuotedName : pSheetInfo->maCalcName;
     144             :     }
     145           0 :     return OUString();
     146             : }
     147             : 
     148             : // private --------------------------------------------------------------------
     149             : 
     150             : namespace {
     151             : 
     152          50 : OUString lclQuoteName( const OUString& rName )
     153             : {
     154          50 :     OUStringBuffer aBuffer( rName );
     155             :     // duplicate all quote characters
     156         350 :     for( sal_Int32 nPos = aBuffer.getLength() - 1; nPos >= 0; --nPos )
     157         300 :         if( aBuffer[nPos] == '\'' )
     158           0 :             aBuffer.insert( nPos, sal_Unicode( '\'' ) );
     159             :     // add outer quotes and return
     160          50 :     return aBuffer.insert( 0, sal_Unicode( '\'' ) ).append( sal_Unicode( '\'' ) ).makeStringAndClear();
     161             : }
     162             : 
     163             : } // namespace
     164             : 
     165          25 : WorksheetBuffer::SheetInfo::SheetInfo( const SheetInfoModel& rModel, sal_Int16 nCalcSheet, const OUString& rCalcName ) :
     166             :     SheetInfoModel( rModel ),
     167             :     maCalcName( rCalcName ),
     168             :     maCalcQuotedName( lclQuoteName( rCalcName ) ),
     169          25 :     mnCalcSheet( nCalcSheet )
     170             : {
     171          25 : }
     172             : 
     173          25 : WorksheetBuffer::IndexNamePair WorksheetBuffer::createSheet( const OUString& rPreferredName, sal_Int32 nSheetPos, bool bVisible )
     174             : {
     175             :     try
     176             :     {
     177          25 :         Reference< XSpreadsheets > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW );
     178          25 :         Reference< XIndexAccess > xSheetsIA( xSheets, UNO_QUERY_THROW );
     179          25 :         Reference< XNameAccess > xSheetsNA( xSheets, UNO_QUERY_THROW );
     180          25 :         sal_Int16 nCalcSheet = -1;
     181          25 :         OUString aSheetName = rPreferredName.isEmpty() ? "Sheet" : rPreferredName;
     182          25 :         PropertySet aPropSet;
     183          25 :         if( nSheetPos < xSheetsIA->getCount() )
     184             :         {
     185          11 :             nCalcSheet = static_cast< sal_Int16 >( nSheetPos );
     186             :             // existing sheet - try to rename
     187          11 :             Reference< XNamed > xSheetName( xSheetsIA->getByIndex( nSheetPos ), UNO_QUERY_THROW );
     188          11 :             if( xSheetName->getName() != aSheetName )
     189             :             {
     190           0 :                 aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' );
     191           0 :                 xSheetName->setName( aSheetName );
     192             :             }
     193          11 :             aPropSet.set( xSheetName );
     194             :         }
     195             :         else
     196             :         {
     197          14 :             nCalcSheet = static_cast< sal_Int16 >( xSheetsIA->getCount() );
     198             :             // new sheet - insert with unused name
     199          14 :             aSheetName = ContainerHelper::getUnusedName( xSheetsNA, aSheetName, ' ' );
     200          14 :             xSheets->insertNewByName( aSheetName, nCalcSheet );
     201          14 :             aPropSet.set( xSheetsIA->getByIndex( nCalcSheet ) );
     202             :         }
     203             : 
     204             :         // sheet properties
     205          25 :         aPropSet.setProperty( PROP_IsVisible, bVisible );
     206             : 
     207             :         // return final sheet index if sheet exists
     208          25 :         return IndexNamePair( nCalcSheet, aSheetName );
     209             :     }
     210           0 :     catch( Exception& )
     211             :     {
     212             :         OSL_FAIL( "WorksheetBuffer::createSheet - cannot insert or rename worksheet" );
     213             :     }
     214           0 :     return IndexNamePair( -1, OUString() );
     215             : }
     216             : 
     217          25 : void WorksheetBuffer::insertSheet( const SheetInfoModel& rModel )
     218             : {
     219          25 :     sal_Int32 nWorksheet = static_cast< sal_Int32 >( maSheetInfos.size() );
     220          25 :     IndexNamePair aIndexName = createSheet( rModel.maName, nWorksheet, rModel.mnState == XML_visible );
     221          25 :     ::boost::shared_ptr< SheetInfo > xSheetInfo( new SheetInfo( rModel, aIndexName.first, aIndexName.second ) );
     222          25 :     maSheetInfos.push_back( xSheetInfo );
     223          25 :     maSheetInfosByName[ rModel.maName ] = xSheetInfo;
     224          25 :     maSheetInfosByName[ lclQuoteName( rModel.maName ) ] = xSheetInfo;
     225          25 : }
     226             : 
     227             : // ============================================================================
     228             : 
     229             : } // namespace xls
     230           9 : } // namespace oox
     231             : 
     232             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10