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

Generated by: LCOV version 1.10