LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/xml - xmlsubti.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 116 128 90.6 %
Date: 2012-12-27 Functions: 17 18 94.4 %
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 "xmlsubti.hxx"
      21             : #include "global.hxx"
      22             : #include "xmlstyli.hxx"
      23             : #include "xmlimprt.hxx"
      24             : #include "document.hxx"
      25             : #include "markdata.hxx"
      26             : #include "XMLConverter.hxx"
      27             : #include "docuno.hxx"
      28             : #include "cellsuno.hxx"
      29             : #include "XMLStylesImportHelper.hxx"
      30             : #include "sheetdata.hxx"
      31             : #include "tabprotection.hxx"
      32             : #include "tokenarray.hxx"
      33             : #include "convuno.hxx"
      34             : #include <svx/svdpage.hxx>
      35             : 
      36             : #include <sax/tools/converter.hxx>
      37             : #include <xmloff/xmltkmap.hxx>
      38             : #include <xmloff/nmspmap.hxx>
      39             : #include <xmloff/xmlerror.hxx>
      40             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      41             : #include <com/sun/star/sheet/XSheetCellRange.hpp>
      42             : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      43             : #include <com/sun/star/sheet/CellInsertMode.hpp>
      44             : #include <com/sun/star/sheet/XCellRangeMovement.hpp>
      45             : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
      46             : #include <com/sun/star/container/XNamed.hpp>
      47             : #include <com/sun/star/util/XProtectable.hpp>
      48             : #include <com/sun/star/sheet/XArrayFormulaRange.hpp>
      49             : 
      50             : #include <memory>
      51             : 
      52             : using ::std::auto_ptr;
      53             : using namespace com::sun::star;
      54             : 
      55             : 
      56         138 : ScXMLTabProtectionData::ScXMLTabProtectionData() :
      57             :     meHash1(PASSHASH_SHA1),
      58             :     meHash2(PASSHASH_UNSPECIFIED),
      59             :     mbProtected(false),
      60             :     mbSelectProtectedCells(true),
      61         138 :     mbSelectUnprotectedCells(true)
      62             : {
      63         138 : }
      64             : 
      65          88 : ScMyTables::ScMyTables(ScXMLImport& rTempImport)
      66             :     : rImport(rTempImport),
      67             :     aFixupOLEs(rTempImport),
      68             :     maCurrentCellPos(ScAddress::INITIALIZE_INVALID),
      69             :     nCurrentColCount(0),
      70             :     nCurrentDrawPage( -1 ),
      71          88 :     nCurrentXShapes( -1 )
      72             : {
      73          88 : }
      74             : 
      75          88 : ScMyTables::~ScMyTables()
      76             : {
      77          88 : }
      78             : 
      79             : namespace {
      80             : 
      81          50 : uno::Reference<sheet::XSpreadsheet> getCurrentSheet(const uno::Reference<frame::XModel>& xModel, SCTAB nSheet)
      82             : {
      83          50 :     uno::Reference<sheet::XSpreadsheet> xSheet;
      84          50 :     uno::Reference<sheet::XSpreadsheetDocument> xSpreadDoc(xModel, uno::UNO_QUERY);
      85          50 :     if (!xSpreadDoc.is())
      86             :         return xSheet;
      87             : 
      88          50 :     uno::Reference <sheet::XSpreadsheets> xSheets(xSpreadDoc->getSheets());
      89          50 :     if (!xSheets.is())
      90             :         return xSheet;
      91             : 
      92          50 :     uno::Reference <container::XIndexAccess> xIndex(xSheets, uno::UNO_QUERY);
      93          50 :     if (!xIndex.is())
      94             :         return xSheet;
      95             : 
      96          50 :     xSheet.set(xIndex->getByIndex(nSheet), uno::UNO_QUERY);
      97          50 :     return xSheet;
      98             : }
      99             : 
     100             : }
     101             : 
     102          50 : void ScMyTables::NewSheet(const rtl::OUString& sTableName, const rtl::OUString& sStyleName,
     103             :                           const ScXMLTabProtectionData& rProtectData)
     104             : {
     105          50 :     if (rImport.GetModel().is())
     106             :     {
     107          50 :         nCurrentColCount = 0;
     108          50 :         sCurrentSheetName = sTableName;
     109             :         //reset cols and rows for new sheet, but increment tab
     110          50 :         maCurrentCellPos.SetCol(-1);
     111          50 :         maCurrentCellPos.SetRow(-1);
     112          50 :         maCurrentCellPos.SetTab(maCurrentCellPos.Tab() + 1);
     113             : 
     114          50 :         maProtectionData = rProtectData;
     115          50 :         ScDocument *pDoc = ScXMLConverter::GetScDocument(rImport.GetModel());
     116             : 
     117             :         // The document contains one sheet when created. So for the first
     118             :         // sheet, we only need to set its name.
     119          50 :         if (maCurrentCellPos.Tab() > 0)
     120          28 :             pDoc->AppendTabOnLoad(sTableName);
     121             :         else
     122          22 :             pDoc->SetTabNameOnLoad(maCurrentCellPos.Tab(), sTableName);
     123             : 
     124          50 :         rImport.SetTableStyle(sStyleName);
     125          50 :         xCurrentSheet = getCurrentSheet(rImport.GetModel(), maCurrentCellPos.Tab());
     126          50 :         if (xCurrentSheet.is())
     127             :         {
     128             :             // We need to set the current cell range here regardless of
     129             :             // presence of style name.
     130          50 :             xCurrentCellRange.set(xCurrentSheet, uno::UNO_QUERY);
     131          50 :             SetTableStyle(sStyleName);
     132             :         }
     133             :     }
     134          50 : }
     135             : 
     136          50 : void ScMyTables::SetTableStyle(const rtl::OUString& sStyleName)
     137             : {
     138             :     //these uno calls are a bit difficult to remove, XMLTableStyleContext::FillPropertySet uses
     139             :     //SvXMLImportPropertyMapper::FillPropertySet
     140          50 :     if ( !sStyleName.isEmpty() )
     141             :     {
     142             :         // #i57869# All table style properties for all sheets are now applied here,
     143             :         // before importing the contents.
     144             :         // This is needed for the background color.
     145             :         // Sheet visibility has special handling in ScDocFunc::SetTableVisible to
     146             :         // allow hiding the first sheet.
     147             :         // RTL layout is only remembered, not actually applied, so the shapes can
     148             :         // be loaded before mirroring.
     149             : 
     150          50 :         if ( xCurrentSheet.is() )
     151             :         {
     152          50 :             xCurrentCellRange.set(xCurrentSheet, uno::UNO_QUERY);
     153          50 :             uno::Reference <beans::XPropertySet> xProperties(xCurrentSheet, uno::UNO_QUERY);
     154          50 :             if ( xProperties.is() )
     155             :             {
     156          50 :                 XMLTableStylesContext *pStyles = (XMLTableStylesContext *)rImport.GetAutoStyles();
     157          50 :                 if ( pStyles )
     158             :                 {
     159             :                     XMLTableStyleContext* pStyle = (XMLTableStyleContext *)pStyles->FindStyleChildContext(
     160          50 :                             XML_STYLE_FAMILY_TABLE_TABLE, sStyleName, true);
     161          50 :                     if ( pStyle )
     162             :                     {
     163          50 :                         pStyle->FillPropertySet(xProperties);
     164             : 
     165          50 :                         ScSheetSaveData* pSheetData = ScModelObj::getImplementation(rImport.GetModel())->GetSheetSaveData();
     166          50 :                         pSheetData->AddTableStyle( sStyleName, ScAddress( 0, 0, maCurrentCellPos.Tab() ) );
     167             :                     }
     168             :                 }
     169          50 :             }
     170             :         }
     171             :     }
     172          50 : }
     173             : 
     174     3147071 : void ScMyTables::AddRow()
     175             : {
     176     3147071 :     maCurrentCellPos.SetRow(maCurrentCellPos.Row() + 1);
     177     3147071 :     maCurrentCellPos.SetCol(-1); //reset columns for new row
     178     3147071 : }
     179             : 
     180        1348 : void ScMyTables::SetRowStyle(const rtl::OUString& rCellStyleName)
     181             : {
     182        1348 :     rImport.GetStylesImportHelper()->SetRowStyle(rCellStyleName);
     183        1348 : }
     184             : 
     185     3150082 : void ScMyTables::AddColumn(bool bIsCovered)
     186             : {
     187     3150082 :     maCurrentCellPos.SetCol( maCurrentCellPos.Col() + 1 );
     188             :     //here only need to set column style if this is the first row and
     189             :     //the cell is not covered.
     190     3150082 :     if(maCurrentCellPos.Row() == 0 && !bIsCovered)
     191         213 :         rImport.GetStylesImportHelper()->InsertCol(maCurrentCellPos.Col(), maCurrentCellPos.Tab(), rImport.GetDocument());
     192     3150082 : }
     193             : 
     194          50 : void ScMyTables::DeleteTable()
     195             : {
     196          50 :     ScXMLImport::MutexGuard aGuard(rImport);
     197             : 
     198          50 :     rImport.GetStylesImportHelper()->SetStylesToRanges();
     199          50 :     rImport.SetStylesToRangesFinished();
     200             : 
     201          50 :     maMatrixRangeList.RemoveAll();
     202             : 
     203          50 :     if (rImport.GetDocument() && maProtectionData.mbProtected)
     204             :     {
     205           0 :         uno::Sequence<sal_Int8> aHash;
     206           0 :         ::sax::Converter::decodeBase64(aHash, maProtectionData.maPassword);
     207             : 
     208             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
     209           0 :         auto_ptr<ScTableProtection> pProtect(new ScTableProtection);
     210             :         SAL_WNODEPRECATED_DECLARATIONS_POP
     211           0 :         pProtect->setProtected(maProtectionData.mbProtected);
     212           0 :         pProtect->setPasswordHash(aHash, maProtectionData.meHash1, maProtectionData.meHash2);
     213           0 :         pProtect->setOption(ScTableProtection::SELECT_LOCKED_CELLS,   maProtectionData.mbSelectProtectedCells);
     214           0 :         pProtect->setOption(ScTableProtection::SELECT_UNLOCKED_CELLS, maProtectionData.mbSelectUnprotectedCells);
     215           0 :         rImport.GetDocument()->SetTabProtection(maCurrentCellPos.Tab(), pProtect.get());
     216          50 :     }
     217          50 : }
     218             : 
     219          81 : void ScMyTables::AddColStyle(const sal_Int32 nRepeat, const rtl::OUString& rCellStyleName)
     220             : {
     221          81 :     rImport.GetStylesImportHelper()->AddColumnStyle(rCellStyleName, nCurrentColCount, nRepeat);
     222          81 :     nCurrentColCount += nRepeat;
     223          81 : }
     224             : 
     225           2 : uno::Reference< drawing::XDrawPage > ScMyTables::GetCurrentXDrawPage()
     226             : {
     227           2 :     if( (maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is() )
     228             :     {
     229           2 :         uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier( xCurrentSheet, uno::UNO_QUERY );
     230           2 :         if( xDrawPageSupplier.is() )
     231           2 :             xDrawPage.set(xDrawPageSupplier->getDrawPage());
     232           2 :         nCurrentDrawPage = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
     233             :     }
     234           2 :     return xDrawPage;
     235             : }
     236             : 
     237           9 : uno::Reference< drawing::XShapes > ScMyTables::GetCurrentXShapes()
     238             : {
     239           9 :     if( (maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is() )
     240             :     {
     241           2 :         xShapes.set(GetCurrentXDrawPage(), uno::UNO_QUERY);
     242           2 :         rImport.GetShapeImport()->startPage(xShapes);
     243           2 :         rImport.GetShapeImport()->pushGroupForSorting ( xShapes );
     244           2 :         nCurrentXShapes = sal::static_int_cast<sal_Int16>(maCurrentCellPos.Tab());
     245           2 :         return xShapes;
     246             :     }
     247             :     else
     248           7 :         return xShapes;
     249             : }
     250             : 
     251          50 : bool ScMyTables::HasDrawPage()
     252             : {
     253          50 :     return !((maCurrentCellPos.Tab() != nCurrentDrawPage) || !xDrawPage.is());
     254             : }
     255             : 
     256           2 : bool ScMyTables::HasXShapes()
     257             : {
     258           2 :     return !((maCurrentCellPos.Tab() != nCurrentXShapes) || !xShapes.is());
     259             : }
     260             : 
     261           0 : void ScMyTables::AddOLE(uno::Reference <drawing::XShape>& rShape,
     262             :       const rtl::OUString &rRangeList)
     263             : {
     264           0 :       aFixupOLEs.AddOLE(rShape, rRangeList);
     265           0 : }
     266             : 
     267          25 : void ScMyTables::AddMatrixRange(
     268             :         const SCCOL nStartColumn, const SCROW nStartRow, const SCCOL nEndColumn, const SCROW nEndRow,
     269             :         const rtl::OUString& rFormula, const rtl::OUString& rFormulaNmsp, const formula::FormulaGrammar::Grammar eGrammar)
     270             : {
     271             :     OSL_ENSURE(nEndRow >= nStartRow, "wrong row order");
     272             :     OSL_ENSURE(nEndColumn >= nStartColumn, "wrong column order");
     273             :     ScRange aScRange(
     274          25 :         nStartColumn, nStartRow, maCurrentCellPos.Tab(),
     275          25 :         nEndColumn, nEndRow, maCurrentCellPos.Tab()
     276          50 :     );
     277             : 
     278          25 :     maMatrixRangeList.Append(aScRange);
     279             : 
     280          25 :     ScDocument* pDoc = rImport.GetDocument();
     281          25 :     ScMarkData aMark;
     282          25 :     aMark.SetMarkArea( aScRange );
     283          25 :     aMark.SelectTable( aScRange.aStart.Tab(), sal_True );
     284          25 :     ScTokenArray* pCode = new ScTokenArray;
     285          25 :     pCode->AddStringXML( rFormula );
     286          25 :     if( (eGrammar == formula::FormulaGrammar::GRAM_EXTERNAL) && !rFormulaNmsp.isEmpty() )
     287           0 :         pCode->AddStringXML( rFormulaNmsp );
     288             :     pDoc->InsertMatrixFormula(
     289          25 :         aScRange.aStart.Col(), aScRange.aStart.Row(),
     290          25 :         aScRange.aEnd.Col(), aScRange.aEnd.Row(),
     291          75 :         aMark, EMPTY_STRING, pCode, eGrammar, false );
     292          25 :     delete pCode;
     293          25 :     pDoc->IncXMLImportedFormulaCount( rFormula.getLength() );
     294          25 : }
     295             : 
     296        1478 : bool ScMyTables::IsPartOfMatrix(const ScAddress& rScAddress) const
     297             : {
     298        1478 :     if (!maMatrixRangeList.empty())
     299        1123 :         return maMatrixRangeList.In(rScAddress);
     300         355 :     return false;
     301             : }
     302             : 
     303             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10