LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/oox - excelvbaproject.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 34 2.9 %
Date: 2012-12-27 Functions: 2 7 28.6 %
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 "excelvbaproject.hxx"
      21             : 
      22             : #include <list>
      23             : #include <set>
      24             : #include <com/sun/star/container/XEnumeration.hpp>
      25             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      26             : #include <com/sun/star/document/XEventsSupplier.hpp>
      27             : #include <com/sun/star/frame/XModel.hpp>
      28             : #include <com/sun/star/script/ModuleType.hpp>
      29             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      30             : #include <rtl/ustrbuf.hxx>
      31             : #include "oox/helper/helper.hxx"
      32             : #include "oox/helper/propertyset.hxx"
      33             : #include "oox/token/properties.hxx"
      34             : 
      35             : namespace oox {
      36             : namespace xls {
      37             : 
      38             : // ============================================================================
      39             : 
      40             : using namespace ::com::sun::star::container;
      41             : using namespace ::com::sun::star::document;
      42             : using namespace ::com::sun::star::frame;
      43             : using namespace ::com::sun::star::lang;
      44             : using namespace ::com::sun::star::script;
      45             : using namespace ::com::sun::star::sheet;
      46             : using namespace ::com::sun::star::uno;
      47             : 
      48             : using ::rtl::OUString;
      49             : using ::rtl::OUStringBuffer;
      50             : 
      51             : // ============================================================================
      52             : 
      53           0 : ExcelVbaProject::ExcelVbaProject( const Reference< XComponentContext >& rxContext, const Reference< XSpreadsheetDocument >& rxDocument ) :
      54             :     ::oox::ole::VbaProject( rxContext, Reference< XModel >( rxDocument, UNO_QUERY ), "Calc" ),
      55           0 :     mxDocument( rxDocument )
      56             : {
      57           0 : }
      58             : 
      59             : // protected ------------------------------------------------------------------
      60             : 
      61             : namespace {
      62             : 
      63           0 : struct SheetCodeNameInfo
      64             : {
      65             :     PropertySet         maSheetProps;       /// Property set of the sheet without codename.
      66             :     OUString            maPrefix;           /// Prefix for the codename to be generated.
      67             : 
      68           0 :     inline explicit     SheetCodeNameInfo( PropertySet& rSheetProps, const OUString& rPrefix ) :
      69           0 :                             maSheetProps( rSheetProps ), maPrefix( rPrefix ) {}
      70             : };
      71             : 
      72             : typedef ::std::set< OUString >              CodeNameSet;
      73             : typedef ::std::list< SheetCodeNameInfo >    SheetCodeNameInfoList;
      74             : 
      75             : } // namespace
      76             : 
      77           0 : void ExcelVbaProject::prepareImport()
      78             : {
      79             :     /*  Check if the sheets have imported codenames. Generate new unused
      80             :         codenames if not. */
      81           0 :     if( mxDocument.is() ) try
      82             :     {
      83             :         // collect existing codenames (do not use them when creating new codenames)
      84           0 :         CodeNameSet aUsedCodeNames;
      85             : 
      86             :         // collect sheets without codenames
      87           0 :         SheetCodeNameInfoList aCodeNameInfos;
      88             : 
      89             :         // iterate over all imported sheets
      90           0 :         Reference< XEnumerationAccess > xSheetsEA( mxDocument->getSheets(), UNO_QUERY_THROW );
      91           0 :         Reference< XEnumeration > xSheetsEnum( xSheetsEA->createEnumeration(), UNO_SET_THROW );
      92             :         // own try/catch for every sheet
      93           0 :         while( xSheetsEnum->hasMoreElements() ) try
      94             :         {
      95           0 :             PropertySet aSheetProp( xSheetsEnum->nextElement() );
      96           0 :             OUString aCodeName;
      97           0 :             aSheetProp.getProperty( aCodeName, PROP_CodeName );
      98           0 :             if( !aCodeName.isEmpty() )
      99             :             {
     100           0 :                 aUsedCodeNames.insert( aCodeName );
     101             :             }
     102             :             else
     103             :             {
     104             :                 // TODO: once we have chart sheets we need a switch/case on sheet type ('SheetNNN' vs. 'ChartNNN')
     105           0 :                 aCodeNameInfos.push_back( SheetCodeNameInfo( aSheetProp, "Sheet" ) );
     106           0 :             }
     107             :         }
     108           0 :         catch( Exception& )
     109             :         {
     110             :         }
     111             : 
     112             :         // create new codenames if sheets do not have one
     113           0 :         for( SheetCodeNameInfoList::iterator aIt = aCodeNameInfos.begin(), aEnd = aCodeNameInfos.end(); aIt != aEnd; ++aIt )
     114             :         {
     115             :             // search for an unused codename
     116           0 :             sal_Int32 nCounter = 1;
     117           0 :             OUString aCodeName;
     118           0 :             do
     119             :             {
     120           0 :                 aCodeName = OUStringBuffer( aIt->maPrefix ).append( nCounter++ ).makeStringAndClear();
     121             :             }
     122           0 :             while( aUsedCodeNames.count( aCodeName ) > 0 );
     123           0 :             aUsedCodeNames.insert( aCodeName );
     124             : 
     125             :             // set codename at sheet
     126           0 :             aIt->maSheetProps.setProperty( PROP_CodeName, aCodeName );
     127             : 
     128             :             // tell base class to create a dummy module
     129           0 :             addDummyModule( aCodeName, ModuleType::DOCUMENT );
     130           0 :         }
     131             :     }
     132           0 :     catch( Exception& )
     133             :     {
     134             :     }
     135           0 : }
     136             : 
     137             : // ============================================================================
     138             : 
     139             : } // namespace xls
     140           9 : } // namespace oox
     141             : 
     142             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10