LCOV - code coverage report
Current view: top level - libreoffice/sc/source/filter/excel - xeroot.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 101 128 78.9 %
Date: 2012-12-27 Functions: 28 32 87.5 %
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             : 
      21             : #include <rtl/random.h>
      22             : #include <sfx2/docfile.hxx>
      23             : #include <sfx2/request.hxx>
      24             : #include <sfx2/frame.hxx>
      25             : #include <sfx2/sfxsids.hrc>
      26             : #include <unotools/saveopt.hxx>
      27             : #include <svl/itemset.hxx>
      28             : #include <svl/stritem.hxx>
      29             : #include <svl/intitem.hxx>
      30             : #include <svl/eitem.hxx>
      31             : #include "xecontent.hxx"
      32             : #include "xltracer.hxx"
      33             : #include "xeescher.hxx"
      34             : #include "xeformula.hxx"
      35             : #include "xehelper.hxx"
      36             : #include "xelink.hxx"
      37             : #include "xename.hxx"
      38             : #include "xepivot.hxx"
      39             : #include "xestyle.hxx"
      40             : #include "xeroot.hxx"
      41             : 
      42             : #include "excrecds.hxx"  // for filter manager
      43             : #include "tabprotection.hxx"
      44             : #include "document.hxx"
      45             : #include "scextopt.hxx"
      46             : 
      47             : using namespace ::com::sun::star;
      48             : 
      49             : // Global data ================================================================
      50             : 
      51           1 : XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
      52             :         SotStorageRef xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc ) :
      53           1 :     XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
      54             : {
      55           1 :     SvtSaveOptions aSaveOpt;
      56           1 :     mbRelUrl = mrMedium.IsRemote() ? aSaveOpt.IsSaveRelINet() : aSaveOpt.IsSaveRelFSys();
      57           1 : }
      58             : 
      59           1 : XclExpRootData::~XclExpRootData()
      60             : {
      61           1 : }
      62             : 
      63             : // ----------------------------------------------------------------------------
      64             : 
      65           1 : XclExpRoot::XclExpRoot( XclExpRootData& rExpRootData ) :
      66             :     XclRoot( rExpRootData ),
      67           1 :     mrExpData( rExpRootData )
      68             : {
      69           1 : }
      70             : 
      71          15 : XclExpTabInfo& XclExpRoot::GetTabInfo() const
      72             : {
      73             :     OSL_ENSURE( mrExpData.mxTabInfo, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
      74          15 :     return *mrExpData.mxTabInfo;
      75             : }
      76             : 
      77          11 : XclExpAddressConverter& XclExpRoot::GetAddressConverter() const
      78             : {
      79             :     OSL_ENSURE( mrExpData.mxAddrConv, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
      80          11 :     return *mrExpData.mxAddrConv;
      81             : }
      82             : 
      83           8 : XclExpFormulaCompiler& XclExpRoot::GetFormulaCompiler() const
      84             : {
      85             :     OSL_ENSURE( mrExpData.mxFmlaComp, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
      86           8 :     return *mrExpData.mxFmlaComp;
      87             : }
      88             : 
      89          49 : XclExpProgressBar& XclExpRoot::GetProgressBar() const
      90             : {
      91             :     OSL_ENSURE( mrExpData.mxProgress, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
      92          49 :     return *mrExpData.mxProgress;
      93             : }
      94             : 
      95           8 : XclExpSst& XclExpRoot::GetSst() const
      96             : {
      97             :     OSL_ENSURE( mrExpData.mxSst, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
      98           8 :     return *mrExpData.mxSst;
      99             : }
     100             : 
     101         125 : XclExpPalette& XclExpRoot::GetPalette() const
     102             : {
     103             :     OSL_ENSURE( mrExpData.mxPalette, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
     104         125 :     return *mrExpData.mxPalette;
     105             : }
     106             : 
     107          20 : XclExpFontBuffer& XclExpRoot::GetFontBuffer() const
     108             : {
     109             :     OSL_ENSURE( mrExpData.mxFontBfr, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
     110          20 :     return *mrExpData.mxFontBfr;
     111             : }
     112             : 
     113          18 : XclExpNumFmtBuffer& XclExpRoot::GetNumFmtBuffer() const
     114             : {
     115             :     OSL_ENSURE( mrExpData.mxNumFmtBfr, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
     116          18 :     return *mrExpData.mxNumFmtBfr;
     117             : }
     118             : 
     119        2641 : XclExpXFBuffer& XclExpRoot::GetXFBuffer() const
     120             : {
     121             :     OSL_ENSURE( mrExpData.mxXFBfr, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
     122        2641 :     return *mrExpData.mxXFBfr;
     123             : }
     124             : 
     125           0 : XclExpLinkManager& XclExpRoot::GetGlobalLinkManager() const
     126             : {
     127             :     OSL_ENSURE( mrExpData.mxGlobLinkMgr, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
     128           0 :     return *mrExpData.mxGlobLinkMgr;
     129             : }
     130             : 
     131           8 : XclExpLinkManager& XclExpRoot::GetLocalLinkManager() const
     132             : {
     133             :     OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
     134           8 :     return *GetLocalLinkMgrRef();
     135             : }
     136             : 
     137           2 : XclExpNameManager& XclExpRoot::GetNameManager() const
     138             : {
     139             :     OSL_ENSURE( mrExpData.mxNameMgr, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
     140           2 :     return *mrExpData.mxNameMgr;
     141             : }
     142             : 
     143           4 : XclExpObjectManager& XclExpRoot::GetObjectManager() const
     144             : {
     145             :     OSL_ENSURE( mrExpData.mxObjMgr, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
     146           4 :     return *mrExpData.mxObjMgr;
     147             : }
     148             : 
     149           3 : XclExpFilterManager& XclExpRoot::GetFilterManager() const
     150             : {
     151             :     OSL_ENSURE( mrExpData.mxFilterMgr, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
     152           3 :     return *mrExpData.mxFilterMgr;
     153             : }
     154             : 
     155           8 : XclExpDxfs& XclExpRoot::GetDxfs() const
     156             : {
     157             :     OSL_ENSURE( mrExpData.mxDxfs, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
     158           8 :     return *mrExpData.mxDxfs;
     159             : }
     160             : 
     161           3 : XclExpPivotTableManager& XclExpRoot::GetPivotTableManager() const
     162             : {
     163             :     OSL_ENSURE( mrExpData.mxPTableMgr, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
     164           3 :     return *mrExpData.mxPTableMgr;
     165             : }
     166             : 
     167           1 : void XclExpRoot::InitializeConvert()
     168             : {
     169           1 :     mrExpData.mxTabInfo.reset( new XclExpTabInfo( GetRoot() ) );
     170           1 :     mrExpData.mxAddrConv.reset( new XclExpAddressConverter( GetRoot() ) );
     171           1 :     mrExpData.mxFmlaComp.reset( new XclExpFormulaCompiler( GetRoot() ) );
     172           1 :     mrExpData.mxProgress.reset( new XclExpProgressBar( GetRoot() ) );
     173             : 
     174           1 :     GetProgressBar().Initialize();
     175           1 : }
     176             : 
     177           1 : void XclExpRoot::InitializeGlobals()
     178             : {
     179           1 :     SetCurrScTab( SCTAB_GLOBAL );
     180             : 
     181           1 :     if( GetBiff() >= EXC_BIFF5 )
     182             :     {
     183           1 :         mrExpData.mxPalette.reset( new XclExpPalette( GetRoot() ) );
     184           1 :         mrExpData.mxFontBfr.reset( new XclExpFontBuffer( GetRoot() ) );
     185           1 :         mrExpData.mxNumFmtBfr.reset( new XclExpNumFmtBuffer( GetRoot() ) );
     186           1 :         mrExpData.mxXFBfr.reset( new XclExpXFBuffer( GetRoot() ) );
     187           1 :         mrExpData.mxGlobLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
     188           1 :         mrExpData.mxNameMgr.reset( new XclExpNameManager( GetRoot() ) );
     189             :     }
     190             : 
     191           1 :     if( GetBiff() == EXC_BIFF8 )
     192             :     {
     193           1 :         mrExpData.mxSst.reset( new XclExpSst );
     194           1 :         mrExpData.mxObjMgr.reset( new XclExpObjectManager( GetRoot() ) );
     195           1 :         mrExpData.mxFilterMgr.reset( new XclExpFilterManager( GetRoot() ) );
     196           1 :         mrExpData.mxPTableMgr.reset( new XclExpPivotTableManager( GetRoot() ) );
     197             :         // BIFF8: only one link manager for all sheets
     198           1 :         mrExpData.mxLocLinkMgr = mrExpData.mxGlobLinkMgr;
     199           1 :         mrExpData.mxDxfs.reset( new XclExpDxfs( GetRoot() ) );
     200             :     }
     201             : 
     202           1 :     GetXFBuffer().Initialize();
     203           1 :     GetNameManager().Initialize();
     204           1 : }
     205             : 
     206           1 : void XclExpRoot::InitializeTable( SCTAB nScTab )
     207             : {
     208           1 :     SetCurrScTab( nScTab );
     209           1 :     if( GetBiff() == EXC_BIFF5 )
     210             :     {
     211             :         // local link manager per sheet
     212           0 :         mrExpData.mxLocLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
     213             :     }
     214           1 : }
     215             : 
     216           1 : void XclExpRoot::InitializeSave()
     217             : {
     218           1 :     GetPalette().Finalize();
     219           1 :     GetXFBuffer().Finalize();
     220           1 : }
     221             : 
     222           8 : XclExpRecordRef XclExpRoot::CreateRecord( sal_uInt16 nRecId ) const
     223             : {
     224           8 :     XclExpRecordRef xRec;
     225           8 :     switch( nRecId )
     226             :     {
     227           1 :         case EXC_ID_PALETTE:        xRec = mrExpData.mxPalette;     break;
     228           1 :         case EXC_ID_FONTLIST:       xRec = mrExpData.mxFontBfr;     break;
     229           1 :         case EXC_ID_FORMATLIST:     xRec = mrExpData.mxNumFmtBfr;   break;
     230           1 :         case EXC_ID_XFLIST:         xRec = mrExpData.mxXFBfr;       break;
     231           1 :         case EXC_ID_SST:            xRec = mrExpData.mxSst;         break;
     232           1 :         case EXC_ID_EXTERNSHEET:    xRec = GetLocalLinkMgrRef();    break;
     233           1 :         case EXC_ID_NAME:           xRec = mrExpData.mxNameMgr;     break;
     234           1 :         case EXC_ID_DXFS:           xRec = mrExpData.mxDxfs;        break;
     235             :     }
     236             :     OSL_ENSURE( xRec, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
     237           8 :     return xRec;
     238             : }
     239             : 
     240           1 : bool XclExpRoot::IsDocumentEncrypted() const
     241             : {
     242             :     // We need to encrypt the content when the document structure is protected.
     243           1 :     const ScDocProtection* pDocProt = GetDoc().GetDocProtection();
     244           1 :     if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE))
     245           0 :         return true;
     246             : 
     247           1 :     if ( GetEncryptionData().getLength() > 0 )
     248             :         // Password is entered directly into the save dialog.
     249           0 :         return true;
     250             : 
     251           1 :     return false;
     252             : }
     253             : 
     254           0 : uno::Sequence< beans::NamedValue > XclExpRoot::GenerateEncryptionData( const ::rtl::OUString& aPass ) const
     255             : {
     256           0 :     uno::Sequence< beans::NamedValue > aEncryptionData;
     257             : 
     258           0 :     if ( !aPass.isEmpty() && aPass.getLength() < 16 )
     259             :     {
     260             :         TimeValue aTime;
     261           0 :         osl_getSystemTime( &aTime );
     262           0 :         rtlRandomPool aRandomPool = rtl_random_createPool ();
     263           0 :         rtl_random_addBytes ( aRandomPool, &aTime, 8 );
     264             : 
     265             :         sal_uInt8 pnDocId[16];
     266           0 :         rtl_random_getBytes( aRandomPool, pnDocId, 16 );
     267             : 
     268           0 :         rtl_random_destroyPool( aRandomPool );
     269             : 
     270             :         sal_uInt16 pnPasswd[16];
     271           0 :         memset( pnPasswd, 0, sizeof( pnPasswd ) );
     272           0 :         for (xub_StrLen nChar = 0; nChar < aPass.getLength(); ++nChar )
     273           0 :             pnPasswd[nChar] = aPass.getStr()[nChar];
     274             : 
     275           0 :         ::msfilter::MSCodec_Std97 aCodec;
     276           0 :         aCodec.InitKey( pnPasswd, pnDocId );
     277           0 :         aEncryptionData = aCodec.GetEncryptionData();
     278             :     }
     279             : 
     280           0 :     return aEncryptionData;
     281             : }
     282             : 
     283           1 : uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const
     284             : {
     285           1 :     uno::Sequence< beans::NamedValue > aEncryptionData;
     286           1 :     SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false );
     287           1 :     if ( pEncryptionDataItem )
     288           0 :         pEncryptionDataItem->GetValue() >>= aEncryptionData;
     289             :     else
     290             :     {
     291             :         // try to get the encryption data from the password
     292           1 :         SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false );
     293           1 :         if ( pPasswordItem && pPasswordItem->GetValue().Len() )
     294           0 :             aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() );
     295             :     }
     296             : 
     297           1 :     return aEncryptionData;
     298             : }
     299             : 
     300           0 : uno::Sequence< beans::NamedValue > XclExpRoot::GenerateDefaultEncryptionData() const
     301             : {
     302           0 :     uno::Sequence< beans::NamedValue > aEncryptionData;
     303           0 :     if ( GetDefaultPassword().Len() > 0 )
     304           0 :         aEncryptionData = GenerateEncryptionData( GetDefaultPassword() );
     305             : 
     306           0 :     return aEncryptionData;
     307             : }
     308             : 
     309           9 : XclExpRootData::XclExpLinkMgrRef XclExpRoot::GetLocalLinkMgrRef() const
     310             : {
     311           9 :     return IsInGlobals() ? mrExpData.mxGlobLinkMgr : mrExpData.mxLocLinkMgr;
     312           9 : }
     313             : 
     314             : // ============================================================================
     315             : 
     316             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10