LCOV - code coverage report
Current view: top level - sc/source/filter/excel - xicontent.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 387 698 55.4 %
Date: 2014-11-03 Functions: 43 62 69.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 "xicontent.hxx"
      21             : #include <sfx2/objsh.hxx>
      22             : #include <sfx2/docfile.hxx>
      23             : #include <tools/urlobj.hxx>
      24             : #include <editeng/editeng.hxx>
      25             : #include <editeng/editobj.hxx>
      26             : #include <sfx2/linkmgr.hxx>
      27             : #include <svl/itemset.hxx>
      28             : #include "scitems.hxx"
      29             : #include <editeng/eeitem.hxx>
      30             : #include <svl/intitem.hxx>
      31             : #include <svl/stritem.hxx>
      32             : #include <editeng/flditem.hxx>
      33             : #include <editeng/fhgtitem.hxx>
      34             : #include <editeng/wghtitem.hxx>
      35             : #include <editeng/udlnitem.hxx>
      36             : #include <editeng/postitem.hxx>
      37             : #include <editeng/colritem.hxx>
      38             : #include <editeng/crossedoutitem.hxx>
      39             : #include "stringutil.hxx"
      40             : #include "document.hxx"
      41             : #include "editutil.hxx"
      42             : #include "formulacell.hxx"
      43             : #include "validat.hxx"
      44             : #include "patattr.hxx"
      45             : #include "docpool.hxx"
      46             : #include "rangenam.hxx"
      47             : #include "arealink.hxx"
      48             : #include "stlsheet.hxx"
      49             : #include "scextopt.hxx"
      50             : #include "xlformula.hxx"
      51             : #include "xltracer.hxx"
      52             : #include "xistream.hxx"
      53             : #include "xihelper.hxx"
      54             : #include "xistyle.hxx"
      55             : #include "xiescher.hxx"
      56             : #include "xiname.hxx"
      57             : 
      58             : #include "excform.hxx"
      59             : #include "tabprotection.hxx"
      60             : #include "documentimport.hxx"
      61             : 
      62             : #include <memory>
      63             : #include <utility>
      64             : 
      65             : using ::com::sun::star::uno::Sequence;
      66             : using ::std::unique_ptr;
      67             : 
      68             : // Shared string table ========================================================
      69             : 
      70         142 : XclImpSst::XclImpSst( const XclImpRoot& rRoot ) :
      71         142 :     XclImpRoot( rRoot )
      72             : {
      73         142 : }
      74             : 
      75         122 : void XclImpSst::ReadSst( XclImpStream& rStrm )
      76             : {
      77         122 :     rStrm.Ignore( 4 );
      78         122 :     sal_uInt32 nStrCount(0);
      79         122 :     rStrm >> nStrCount;
      80         122 :     maStrings.clear();
      81         122 :     maStrings.reserve( static_cast< size_t >( nStrCount ) );
      82        4500 :     while( (nStrCount > 0) && rStrm.IsValid() )
      83             :     {
      84        4256 :         XclImpString aString;
      85        4256 :         aString.Read( rStrm );
      86        4256 :         maStrings.push_back( aString );
      87        4256 :         --nStrCount;
      88        4256 :     }
      89         122 : }
      90             : 
      91       21798 : const XclImpString* XclImpSst::GetString( sal_uInt32 nSstIndex ) const
      92             : {
      93       21798 :     return (nSstIndex < maStrings.size()) ? &maStrings[ nSstIndex ] : 0;
      94             : }
      95             : 
      96             : // Hyperlinks =================================================================
      97             : 
      98             : namespace {
      99             : 
     100             : /** Reads character array and stores it into rString.
     101             :     @param nChars  Number of following characters (not byte count!).
     102             :     @param b16Bit  true = 16-bit characters, false = 8-bit characters. */
     103          16 : void lclAppendString32( OUString& rString, XclImpStream& rStrm, sal_uInt32 nChars, bool b16Bit )
     104             : {
     105          16 :     sal_uInt16 nReadChars = ulimit_cast< sal_uInt16 >( nChars );
     106          16 :     rString += rStrm.ReadRawUniString( nReadChars, b16Bit );
     107             :     // ignore remaining chars
     108          16 :     sal_Size nIgnore = nChars - nReadChars;
     109          16 :     if( b16Bit )
     110          16 :         nIgnore *= 2;
     111          16 :     rStrm.Ignore( nIgnore );
     112          16 : }
     113             : 
     114             : /** Reads 32-bit string length and the character array and stores it into rString.
     115             :     @param b16Bit  true = 16-bit characters, false = 8-bit characters. */
     116           0 : void lclAppendString32( OUString& rString, XclImpStream& rStrm, bool b16Bit )
     117             : {
     118           0 :     lclAppendString32( rString, rStrm, rStrm.ReaduInt32(), b16Bit );
     119           0 : }
     120             : 
     121             : /** Reads 32-bit string length and ignores following character array.
     122             :     @param b16Bit  true = 16-bit characters, false = 8-bit characters. */
     123          14 : void lclIgnoreString32( XclImpStream& rStrm, bool b16Bit )
     124             : {
     125          14 :     sal_uInt32 nChars(0);
     126          14 :     rStrm >> nChars;
     127          14 :     if( b16Bit )
     128          14 :         nChars *= 2;
     129          14 :     rStrm.Ignore( nChars );
     130          14 : }
     131             : 
     132             : /** Converts a path to an absolute path.
     133             :     @param rPath  The source path. The resulting path is returned here.
     134             :     @param nLevel  Number of parent directories to add in front of the path. */
     135           0 : void lclGetAbsPath( OUString& rPath, sal_uInt16 nLevel, SfxObjectShell* pDocShell )
     136             : {
     137           0 :     OUStringBuffer aTmpStr;
     138           0 :     while( nLevel )
     139             :     {
     140           0 :         aTmpStr.append( "../" );
     141           0 :         --nLevel;
     142             :     }
     143           0 :     aTmpStr.append( rPath );
     144             : 
     145           0 :     if( pDocShell )
     146             :     {
     147           0 :         bool bWasAbs = false;
     148           0 :         rPath = pDocShell->GetMedium()->GetURLObject().smartRel2Abs( aTmpStr.makeStringAndClear(), bWasAbs ).GetMainURL( INetURLObject::NO_DECODE );
     149             :         // full path as stored in SvxURLField must be encoded
     150             :     }
     151             :     else
     152           0 :         rPath = aTmpStr.makeStringAndClear();
     153           0 : }
     154             : 
     155             : /** Inserts the URL into a text cell. Does not modify value or formula cells. */
     156          16 : void lclInsertUrl( XclImpRoot& rRoot, const OUString& rUrl, SCCOL nScCol, SCROW nScRow, SCTAB nScTab )
     157             : {
     158          16 :     ScDocumentImport& rDoc = rRoot.GetDocImport();
     159          16 :     ScAddress aScPos( nScCol, nScRow, nScTab );
     160          16 :     CellType eCellType = rDoc.getDoc().GetCellType(aScPos);
     161          16 :     switch( eCellType )
     162             :     {
     163             :         // #i54261# hyperlinks in string cells
     164             :         case CELLTYPE_STRING:
     165             :         case CELLTYPE_EDIT:
     166             :         {
     167          16 :             OUString aDisplText = rDoc.getDoc().GetString(nScCol, nScRow, nScTab);
     168          16 :             if (aDisplText.isEmpty())
     169           0 :                 aDisplText = rUrl;
     170             : 
     171          16 :             ScEditEngineDefaulter& rEE = rRoot.GetEditEngine();
     172          32 :             SvxURLField aUrlField( rUrl, aDisplText, SVXURLFORMAT_APPDEFAULT );
     173             : 
     174          16 :             const EditTextObject* pEditObj = rDoc.getDoc().GetEditText(aScPos);
     175          16 :             if( pEditObj )
     176             :             {
     177           0 :                 rEE.SetText( *pEditObj );
     178           0 :                 rEE.QuickInsertField( SvxFieldItem( aUrlField, EE_FEATURE_FIELD ), ESelection( 0, 0, EE_PARA_ALL, 0 ) );
     179             :             }
     180             :             else
     181             :             {
     182          16 :                 rEE.SetText( EMPTY_OUSTRING );
     183          16 :                 rEE.QuickInsertField( SvxFieldItem( aUrlField, EE_FEATURE_FIELD ), ESelection() );
     184          16 :                 if( const ScPatternAttr* pPattern = rDoc.getDoc().GetPattern( aScPos.Col(), aScPos.Row(), nScTab ) )
     185             :                 {
     186          16 :                     SfxItemSet aItemSet( rEE.GetEmptyItemSet() );
     187          16 :                     pPattern->FillEditItemSet( &aItemSet );
     188          16 :                     rEE.QuickSetAttribs( aItemSet, ESelection( 0, 0, EE_PARA_ALL, 0 ) );
     189             :                 }
     190             :             }
     191             : 
     192             :             // The cell will own the text object instance.
     193          32 :             rDoc.setEditCell(aScPos, rEE.CreateTextObject());
     194             :         }
     195          16 :         break;
     196             : 
     197             :         default:
     198             :         // Handle other cell types e.g. formulas ( and ? ) that have associated
     199             :         // hyperlinks.
     200             :         // Ideally all hyperlinks should be treated  as below. For the moment,
     201             :         // given the current absence of ods support lets just handle what we
     202             :         // previously didn't handle the new way.
     203             :         // Unfortunately we won't be able to preserve such hyperlinks when
     204             :         // saving to ods. Note: when we are able to save such hyperlinks to ods
     205             :         // we should handle *all* imported hyperlinks as below ( e.g. as cell
     206             :         // attribute ) for better interoperability.
     207             :         {
     208           0 :             SfxStringItem aItem( ATTR_HYPERLINK, rUrl );
     209           0 :             rDoc.getDoc().ApplyAttr(nScCol, nScRow, nScTab, aItem);
     210           0 :             break;
     211             :         }
     212             :     }
     213          16 : }
     214             : 
     215             : } // namespace
     216             : 
     217          16 : void XclImpHyperlink::ReadHlink( XclImpStream& rStrm )
     218             : {
     219          16 :     XclRange aXclRange( ScAddress::UNINITIALIZED );
     220          16 :     rStrm >> aXclRange;
     221             :     // #i80006# Excel silently ignores invalid hi-byte of column index (TODO: everywhere?)
     222          16 :     aXclRange.maFirst.mnCol &= 0xFF;
     223          16 :     aXclRange.maLast.mnCol &= 0xFF;
     224          16 :     OUString aString = ReadEmbeddedData( rStrm );
     225          16 :     if ( !aString.isEmpty() )
     226          16 :         rStrm.GetRoot().GetXFRangeBuffer().SetHyperlink( aXclRange, aString );
     227          16 : }
     228             : 
     229          16 : OUString XclImpHyperlink::ReadEmbeddedData( XclImpStream& rStrm )
     230             : {
     231          16 :     const XclImpRoot& rRoot = rStrm.GetRoot();
     232          16 :     SfxObjectShell* pDocShell = rRoot.GetDocShell();
     233             : 
     234             :     OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
     235             : 
     236          16 :     XclGuid aGuid;
     237          16 :     rStrm >> aGuid;
     238          16 :     rStrm.Ignore( 4 );
     239          16 :     sal_uInt32 nFlags(0);
     240          16 :     rStrm >> nFlags;
     241             : 
     242             :     OSL_ENSURE( aGuid == XclTools::maGuidStdLink, "XclImpHyperlink::ReadEmbeddedData - unknown header GUID" );
     243             : 
     244          16 :     ::std::unique_ptr< OUString > xLongName;    // link / file name
     245          32 :     ::std::unique_ptr< OUString > xShortName;   // 8.3-representation of file name
     246          32 :     ::std::unique_ptr< OUString > xTextMark;    // text mark
     247             : 
     248             :     // description (ignore)
     249          16 :     if( ::get_flag( nFlags, EXC_HLINK_DESCR ) )
     250          14 :         lclIgnoreString32( rStrm, true );
     251             :     // target frame (ignore) !! DESCR/FRAME - is this the right order? (never seen them together)
     252          16 :     if( ::get_flag( nFlags, EXC_HLINK_FRAME ) )
     253           0 :         lclIgnoreString32( rStrm, true );
     254             : 
     255             :     // URL fields are zero-terminated - do not let the stream replace them
     256             :     // in the lclAppendString32() with the '?' character.
     257          16 :     rStrm.SetNulSubstChar( '\0' );
     258             : 
     259             :     // UNC path
     260          16 :     if( ::get_flag( nFlags, EXC_HLINK_UNC ) )
     261             :     {
     262           0 :         xLongName.reset( new OUString );
     263           0 :         lclAppendString32( *xLongName, rStrm, true );
     264           0 :         lclGetAbsPath( *xLongName, 0, pDocShell );
     265             :     }
     266             :     // file link or URL
     267          16 :     else if( ::get_flag( nFlags, EXC_HLINK_BODY ) )
     268             :     {
     269          16 :         rStrm >> aGuid;
     270             : 
     271          16 :         if( aGuid == XclTools::maGuidFileMoniker )
     272             :         {
     273           0 :             sal_uInt16 nLevel = 0; // counter for level to climb down in path
     274           0 :             rStrm >> nLevel;
     275           0 :             xShortName.reset( new OUString );
     276           0 :             lclAppendString32( *xShortName, rStrm, false );
     277           0 :             rStrm.Ignore( 24 );
     278             : 
     279           0 :             sal_uInt32 nStrLen = 0;
     280           0 :             rStrm >> nStrLen;
     281           0 :             if( nStrLen )
     282             :             {
     283           0 :                 nStrLen = 0;
     284           0 :                 rStrm >> nStrLen;
     285           0 :                 nStrLen /= 2;       // it's byte count here...
     286           0 :                 rStrm.Ignore( 2 );
     287           0 :                 xLongName.reset( new OUString );
     288           0 :                 lclAppendString32( *xLongName, rStrm, nStrLen, true );
     289           0 :                 lclGetAbsPath( *xLongName, nLevel, pDocShell );
     290             :             }
     291             :             else
     292           0 :                 lclGetAbsPath( *xShortName, nLevel, pDocShell );
     293             :         }
     294          16 :         else if( aGuid == XclTools::maGuidUrlMoniker )
     295             :         {
     296          16 :             sal_uInt32 nStrLen(0);
     297          16 :             rStrm >> nStrLen;
     298          16 :             nStrLen /= 2;       // it's byte count here...
     299          16 :             xLongName.reset( new OUString );
     300          16 :             lclAppendString32( *xLongName, rStrm, nStrLen, true );
     301          16 :             if( !::get_flag( nFlags, EXC_HLINK_ABS ) )
     302           0 :                 lclGetAbsPath( *xLongName, 0, pDocShell );
     303             :         }
     304             :         else
     305             :         {
     306             :             OSL_FAIL( "XclImpHyperlink::ReadEmbeddedData - unknown content GUID" );
     307             :         }
     308             :     }
     309             : 
     310             :     // text mark
     311          16 :     if( ::get_flag( nFlags, EXC_HLINK_MARK ) )
     312             :     {
     313           0 :         xTextMark.reset( new OUString );
     314           0 :         lclAppendString32( *xTextMark, rStrm, true );
     315             :     }
     316             : 
     317          16 :     rStrm.SetNulSubstChar();    // back to default
     318             : 
     319             :     OSL_ENSURE( rStrm.GetRecLeft() == 0, "XclImpHyperlink::ReadEmbeddedData - record size mismatch" );
     320             : 
     321          16 :     if( !xLongName.get() && xShortName.get() )
     322           0 :         xLongName = std::move(xShortName);
     323          16 :     else if( !xLongName.get() && xTextMark.get() )
     324           0 :         xLongName.reset( new OUString );
     325             : 
     326          16 :     if( xLongName.get() )
     327             :     {
     328          16 :         if( xTextMark.get() )
     329             :         {
     330           0 :             if( xLongName->isEmpty() )
     331           0 :                 xTextMark.reset( new OUString( xTextMark->replace( '!', '.' ) ) );
     332           0 :             xLongName.reset( new OUString( *xLongName + "#" ) );
     333           0 :             xLongName.reset( new OUString( *xLongName + *xTextMark  ) );
     334             :         }
     335          16 :         return( *xLongName );
     336             :     }
     337          16 :     return( OUString() );
     338             : }
     339             : 
     340          16 : void XclImpHyperlink::ConvertToValidTabName(OUString& rUrl)
     341             : {
     342          16 :     sal_Int32 n = rUrl.getLength();
     343          16 :     if (n < 4)
     344             :         // Needs at least 4 characters.
     345          16 :         return;
     346             : 
     347          16 :     if (rUrl[0] != '#')
     348             :         // the 1st character must be '#'.
     349          16 :         return;
     350             : 
     351           0 :     OUString aNewUrl('#'), aTabName;
     352             : 
     353           0 :     bool bInQuote = false;
     354           0 :     bool bQuoteTabName = false;
     355           0 :     for( sal_Int32 i = 1; i < n; ++i )
     356             :     {
     357           0 :         sal_Unicode c = rUrl[i];
     358           0 :         if (c == '\'')
     359             :         {
     360           0 :             if (bInQuote && i+1 < n && rUrl[i+1] == '\'')
     361             :             {
     362             :                 // Two consecutive single quotes ('') signify a single literal
     363             :                 // quite.  When this occurs, the whole table name needs to be
     364             :                 // quoted.
     365           0 :                 bQuoteTabName = true;
     366           0 :                 aTabName += OUString(c);
     367           0 :                 aTabName += OUString(c);
     368           0 :                 ++i;
     369           0 :                 continue;
     370             :             }
     371             : 
     372           0 :             bInQuote = !bInQuote;
     373           0 :             if (!bInQuote && !aTabName.isEmpty())
     374             :             {
     375           0 :                 if (bQuoteTabName)
     376           0 :                     aNewUrl += OUString('\'');
     377           0 :                 aNewUrl += aTabName;
     378           0 :                 if (bQuoteTabName)
     379           0 :                     aNewUrl += OUString('\'');
     380             :             }
     381             :         }
     382           0 :         else if (bInQuote)
     383           0 :             aTabName += OUString(c);
     384             :         else
     385           0 :             aNewUrl += OUString(c);
     386             :     }
     387             : 
     388           0 :     if (bInQuote)
     389             :         // It should be outside the quotes!
     390           0 :         return;
     391             : 
     392             :     // All is good.  Pass the new URL.
     393           0 :     rUrl = aNewUrl;
     394             : }
     395             : 
     396          16 : void XclImpHyperlink::InsertUrl( XclImpRoot& rRoot, const XclRange& rXclRange, const OUString& rUrl )
     397             : {
     398          16 :     OUString aUrl(rUrl);
     399          16 :     ConvertToValidTabName(aUrl);
     400             : 
     401          16 :     SCTAB nScTab = rRoot.GetCurrScTab();
     402          16 :     ScRange aScRange( ScAddress::UNINITIALIZED );
     403          16 :     if( rRoot.GetAddressConverter().ConvertRange( aScRange, rXclRange, nScTab, nScTab, true ) )
     404             :     {
     405             :         SCCOL nScCol1, nScCol2;
     406             :         SCROW nScRow1, nScRow2;
     407          16 :         aScRange.GetVars( nScCol1, nScRow1, nScTab, nScCol2, nScRow2, nScTab );
     408          32 :         for( SCCOL nScCol = nScCol1; nScCol <= nScCol2; ++nScCol )
     409          32 :             for( SCROW nScRow = nScRow1; nScRow <= nScRow2; ++nScRow )
     410          16 :                 lclInsertUrl( rRoot, aUrl, nScCol, nScRow, nScTab );
     411          16 :     }
     412          16 : }
     413             : 
     414             : // Label ranges ===============================================================
     415             : 
     416           0 : void XclImpLabelranges::ReadLabelranges( XclImpStream& rStrm )
     417             : {
     418           0 :     const XclImpRoot& rRoot = rStrm.GetRoot();
     419             :     OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
     420             : 
     421           0 :     ScDocument& rDoc = rRoot.GetDoc();
     422           0 :     SCTAB nScTab = rRoot.GetCurrScTab();
     423           0 :     XclImpAddressConverter& rAddrConv = rRoot.GetAddressConverter();
     424           0 :     ScRangePairListRef xLabelRangesRef;
     425           0 :     const ScRange* pScRange = 0;
     426             : 
     427           0 :     XclRangeList aRowXclRanges, aColXclRanges;
     428           0 :     rStrm >> aRowXclRanges >> aColXclRanges;
     429             : 
     430             :     // row label ranges
     431           0 :     ScRangeList aRowScRanges;
     432           0 :     rAddrConv.ConvertRangeList( aRowScRanges, aRowXclRanges, nScTab, false );
     433           0 :     xLabelRangesRef = rDoc.GetRowNameRangesRef();
     434           0 :     for ( size_t i = 0, nRanges = aRowScRanges.size(); i < nRanges; ++i )
     435             :     {
     436           0 :         pScRange = aRowScRanges[ i ];
     437           0 :         ScRange aDataRange( *pScRange );
     438           0 :         if( aDataRange.aEnd.Col() < MAXCOL )
     439             :         {
     440           0 :             aDataRange.aStart.SetCol( aDataRange.aEnd.Col() + 1 );
     441           0 :             aDataRange.aEnd.SetCol( MAXCOL );
     442             :         }
     443           0 :         else if( aDataRange.aStart.Col() > 0 )
     444             :         {
     445           0 :             aDataRange.aEnd.SetCol( aDataRange.aStart.Col() - 1 );
     446           0 :             aDataRange.aStart.SetCol( 0 );
     447             :         }
     448           0 :         xLabelRangesRef->Append( ScRangePair( *pScRange, aDataRange ) );
     449             :     }
     450             : 
     451             :     // column label ranges
     452           0 :     ScRangeList aColScRanges;
     453           0 :     rAddrConv.ConvertRangeList( aColScRanges, aColXclRanges, nScTab, false );
     454           0 :     xLabelRangesRef = rDoc.GetColNameRangesRef();
     455             : 
     456           0 :     for ( size_t i = 0, nRanges = aColScRanges.size(); i < nRanges; ++i )
     457             :     {
     458           0 :         pScRange = aColScRanges[ i ];
     459           0 :         ScRange aDataRange( *pScRange );
     460           0 :         if( aDataRange.aEnd.Row() < MAXROW )
     461             :         {
     462           0 :             aDataRange.aStart.SetRow( aDataRange.aEnd.Row() + 1 );
     463           0 :             aDataRange.aEnd.SetRow( MAXROW );
     464             :         }
     465           0 :         else if( aDataRange.aStart.Row() > 0 )
     466             :         {
     467           0 :             aDataRange.aEnd.SetRow( aDataRange.aStart.Row() - 1 );
     468           0 :             aDataRange.aStart.SetRow( 0 );
     469             :         }
     470           0 :         xLabelRangesRef->Append( ScRangePair( *pScRange, aDataRange ) );
     471           0 :     }
     472           0 : }
     473             : 
     474             : // Conditional formatting =====================================================
     475             : 
     476           4 : XclImpCondFormat::XclImpCondFormat( const XclImpRoot& rRoot, sal_uInt32 nFormatIndex ) :
     477             :     XclImpRoot( rRoot ),
     478             :     mnFormatIndex( nFormatIndex ),
     479             :     mnCondCount( 0 ),
     480           4 :     mnCondIndex( 0 )
     481             : {
     482           4 : }
     483             : 
     484           8 : XclImpCondFormat::~XclImpCondFormat()
     485             : {
     486           8 : }
     487             : 
     488           4 : void XclImpCondFormat::ReadCondfmt( XclImpStream& rStrm )
     489             : {
     490             :     OSL_ENSURE( !mnCondCount, "XclImpCondFormat::ReadCondfmt - already initialized" );
     491           4 :     XclRangeList aXclRanges;
     492           4 :     rStrm >> mnCondCount;
     493           4 :     rStrm.Ignore( 10 );
     494           4 :     rStrm >> aXclRanges;
     495           4 :     GetAddressConverter().ConvertRangeList( maRanges, aXclRanges, GetCurrScTab(), true );
     496           4 : }
     497             : 
     498           4 : void XclImpCondFormat::ReadCF( XclImpStream& rStrm )
     499             : {
     500           4 :     if( mnCondIndex >= mnCondCount )
     501             :     {
     502             :         OSL_FAIL( "XclImpCondFormat::ReadCF - CF without leading CONDFMT" );
     503           0 :         return;
     504             :     }
     505             : 
     506             :     // entire conditional format outside of valid range?
     507           4 :     if( maRanges.empty() )
     508           0 :         return;
     509             : 
     510           4 :     sal_uInt8 nType(0), nOperator(0);
     511           4 :     sal_uInt16 nFmlaSize1(0), nFmlaSize2(0);
     512           4 :     sal_uInt32 nFlags(0);
     513           4 :     sal_uInt16 nFlagsExtended(0);
     514             : 
     515           4 :     rStrm >> nType >> nOperator >> nFmlaSize1 >> nFmlaSize2 >> nFlags >> nFlagsExtended;
     516             : 
     517             :     // *** mode and comparison operator ***
     518             : 
     519           4 :     ScConditionMode eMode = SC_COND_NONE;
     520           4 :     switch( nType )
     521             :     {
     522             :         case EXC_CF_TYPE_CELL:
     523             :         {
     524           4 :             switch( nOperator )
     525             :             {
     526           2 :                 case EXC_CF_CMP_BETWEEN:        eMode = SC_COND_BETWEEN;    break;
     527           0 :                 case EXC_CF_CMP_NOT_BETWEEN:    eMode = SC_COND_NOTBETWEEN; break;
     528           0 :                 case EXC_CF_CMP_EQUAL:          eMode = SC_COND_EQUAL;      break;
     529           0 :                 case EXC_CF_CMP_NOT_EQUAL:      eMode = SC_COND_NOTEQUAL;   break;
     530           2 :                 case EXC_CF_CMP_GREATER:        eMode = SC_COND_GREATER;    break;
     531           0 :                 case EXC_CF_CMP_LESS:           eMode = SC_COND_LESS;       break;
     532           0 :                 case EXC_CF_CMP_GREATER_EQUAL:  eMode = SC_COND_EQGREATER;  break;
     533           0 :                 case EXC_CF_CMP_LESS_EQUAL:     eMode = SC_COND_EQLESS;     break;
     534             :                 default:
     535             :                     OSL_TRACE( "XclImpCondFormat::ReadCF - unknown CF comparison 0x%02hX", nOperator );
     536             :             }
     537             :         }
     538           4 :         break;
     539             : 
     540             :         case EXC_CF_TYPE_FMLA:
     541           0 :             eMode = SC_COND_DIRECT;
     542           0 :         break;
     543             : 
     544             :         default:
     545             :             OSL_TRACE( "XclImpCondFormat::ReadCF - unknown CF mode 0x%02hX", nType );
     546           0 :             return;
     547             :     }
     548             : 
     549             :     // *** create style sheet ***
     550             : 
     551           4 :     OUString aStyleName( XclTools::GetCondFormatStyleName( GetCurrScTab(), mnFormatIndex, mnCondIndex ) );
     552           4 :     SfxItemSet& rStyleItemSet = ScfTools::MakeCellStyleSheet( GetStyleSheetPool(), aStyleName, true ).GetItemSet();
     553             : 
     554           4 :     const XclImpPalette& rPalette = GetPalette();
     555             : 
     556             :     // number format
     557             : 
     558           4 :     if( get_flag( nFlags, EXC_CF_BLOCK_NUMFMT ) )
     559             :     {
     560           0 :         XclImpNumFmtBuffer& rNumFmtBuffer = GetRoot().GetNumFmtBuffer();
     561           0 :         bool bIFmt = get_flag( nFlags, EXC_CF_IFMT_USER );
     562           0 :         sal_uInt16 nFormat = rNumFmtBuffer.ReadCFFormat( rStrm, bIFmt );
     563           0 :         rNumFmtBuffer.FillToItemSet( rStyleItemSet, nFormat );
     564             :     }
     565             : 
     566             :     // *** font block ***
     567             : 
     568           4 :     if( ::get_flag( nFlags, EXC_CF_BLOCK_FONT ) )
     569             :     {
     570           0 :         XclImpFont aFont( GetRoot() );
     571           0 :         aFont.ReadCFFontBlock( rStrm );
     572           0 :         aFont.FillToItemSet( rStyleItemSet, EXC_FONTITEM_CELL );
     573             :     }
     574             : 
     575             :     // alignment
     576           4 :     if( get_flag( nFlags, EXC_CF_BLOCK_ALIGNMENT ) )
     577             :     {
     578           0 :         XclImpCellAlign aAlign;
     579           0 :         sal_uInt16 nAlign(0);
     580           0 :         sal_uInt16 nAlignMisc(0);
     581           0 :         rStrm >> nAlign >> nAlignMisc;
     582           0 :         aAlign.FillFromCF( nAlign, nAlignMisc );
     583           0 :         aAlign.FillToItemSet( rStyleItemSet, NULL );
     584           0 :         rStrm.Ignore(4);
     585             :     }
     586             : 
     587             :     // *** border block ***
     588             : 
     589           4 :     if( ::get_flag( nFlags, EXC_CF_BLOCK_BORDER ) )
     590             :     {
     591           0 :         sal_uInt16 nLineStyle(0);
     592           0 :         sal_uInt32 nLineColor(0);
     593           0 :         rStrm >> nLineStyle >> nLineColor;
     594           0 :         rStrm.Ignore( 2 );
     595             : 
     596           0 :         XclImpCellBorder aBorder;
     597           0 :         aBorder.FillFromCF8( nLineStyle, nLineColor, nFlags );
     598           0 :         aBorder.FillToItemSet( rStyleItemSet, rPalette );
     599             :     }
     600             : 
     601             :     // *** pattern block ***
     602             : 
     603           4 :     if( ::get_flag( nFlags, EXC_CF_BLOCK_AREA ) )
     604             :     {
     605           0 :         sal_uInt16 nPattern(0), nColor(0);
     606           0 :         rStrm >> nPattern >> nColor;
     607             : 
     608           0 :         XclImpCellArea aArea;
     609           0 :         aArea.FillFromCF8( nPattern, nColor, nFlags );
     610           0 :         aArea.FillToItemSet( rStyleItemSet, rPalette );
     611             :     }
     612             : 
     613           4 :     if( get_flag( nFlags, EXC_CF_BLOCK_PROTECTION ) )
     614             :     {
     615             :         sal_uInt16 nCellProt;
     616           0 :         rStrm >> nCellProt;
     617           0 :         XclImpCellProt aCellProt;
     618           0 :         aCellProt.FillFromXF3(nCellProt);
     619           0 :         aCellProt.FillToItemSet( rStyleItemSet );
     620             :     }
     621             : 
     622             :     // *** formulas ***
     623             : 
     624           4 :     const ScAddress& rPos = maRanges.front()->aStart;    // assured above that maRanges is not empty
     625           4 :     ExcelToSc& rFmlaConv = GetOldFmlaConverter();
     626             : 
     627           8 :     ::std::unique_ptr< ScTokenArray > xTokArr1;
     628           4 :     if( nFmlaSize1 > 0 )
     629             :     {
     630           4 :         const ScTokenArray* pTokArr = 0;
     631           4 :         rFmlaConv.Reset( rPos );
     632           4 :         rFmlaConv.Convert( pTokArr, rStrm, nFmlaSize1, false, FT_CondFormat );
     633             :         // formula converter owns pTokArr -> create a copy of the token array
     634           4 :         if( pTokArr )
     635           4 :             xTokArr1.reset( pTokArr->Clone() );
     636             :     }
     637             : 
     638           8 :     ::std::unique_ptr< ScTokenArray > pTokArr2;
     639           4 :     if( nFmlaSize2 > 0 )
     640             :     {
     641           2 :         const ScTokenArray* pTokArr = 0;
     642           2 :         rFmlaConv.Reset( rPos );
     643           2 :         rFmlaConv.Convert( pTokArr, rStrm, nFmlaSize2, false, FT_CondFormat );
     644             :         // formula converter owns pTokArr -> create a copy of the token array
     645           2 :         if( pTokArr )
     646           2 :             pTokArr2.reset( pTokArr->Clone() );
     647             :     }
     648             : 
     649             :     // *** create the Calc conditional formatting ***
     650             : 
     651           4 :     if( !mxScCondFmt.get() )
     652             :     {
     653           4 :         sal_uLong nKey = 0;
     654           4 :         mxScCondFmt.reset( new ScConditionalFormat( nKey, GetDocPtr() ) );
     655           4 :         if(maRanges.size() > 1)
     656           4 :             maRanges.Join(*maRanges[0], true);
     657           4 :         mxScCondFmt->AddRange(maRanges);
     658             :     }
     659             : 
     660           4 :     ScCondFormatEntry* pEntry = new ScCondFormatEntry( eMode, xTokArr1.get(), pTokArr2.get(), GetDocPtr(), rPos, aStyleName );
     661           4 :     mxScCondFmt->AddEntry( pEntry );
     662           8 :     ++mnCondIndex;
     663             : }
     664             : 
     665           4 : void XclImpCondFormat::Apply()
     666             : {
     667           4 :     if( mxScCondFmt.get() )
     668             :     {
     669           4 :         ScDocument& rDoc = GetDoc();
     670             : 
     671           4 :         SCTAB nTab = maRanges.front()->aStart.Tab();
     672           4 :         sal_uLong nKey = rDoc.AddCondFormat( mxScCondFmt->Clone(), nTab );
     673             : 
     674           4 :         rDoc.AddCondFormatData( maRanges, nTab, nKey );
     675             :     }
     676           4 : }
     677             : 
     678         142 : XclImpCondFormatManager::XclImpCondFormatManager( const XclImpRoot& rRoot ) :
     679         142 :     XclImpRoot( rRoot )
     680             : {
     681         142 : }
     682             : 
     683           4 : void XclImpCondFormatManager::ReadCondfmt( XclImpStream& rStrm )
     684             : {
     685           4 :     XclImpCondFormat* pFmt = new XclImpCondFormat( GetRoot(), maCondFmtList.size() );
     686           4 :     pFmt->ReadCondfmt( rStrm );
     687           4 :     maCondFmtList.push_back( pFmt );
     688           4 : }
     689             : 
     690           4 : void XclImpCondFormatManager::ReadCF( XclImpStream& rStrm )
     691             : {
     692             :     OSL_ENSURE( !maCondFmtList.empty(), "XclImpCondFormatManager::ReadCF - CF without leading CONDFMT" );
     693           4 :     if( !maCondFmtList.empty() )
     694           4 :         maCondFmtList.back().ReadCF( rStrm );
     695           4 : }
     696             : 
     697         412 : void XclImpCondFormatManager::Apply()
     698             : {
     699         416 :     for( XclImpCondFmtList::iterator itFmt = maCondFmtList.begin(); itFmt != maCondFmtList.end(); ++itFmt )
     700           4 :         itFmt->Apply();
     701         412 :     maCondFmtList.clear();
     702         412 : }
     703             : 
     704             : // Data Validation ============================================================
     705             : 
     706           6 : XclImpValidationManager::DVItem::DVItem( const ScRangeList& rRanges, const ScValidationData& rValidData ) :
     707           6 :     maRanges(rRanges), maValidData(rValidData) {}
     708             : 
     709         142 : XclImpValidationManager::XclImpValidationManager( const XclImpRoot& rRoot ) :
     710         142 :     XclImpRoot( rRoot )
     711             : {
     712         142 : }
     713             : 
     714          10 : void XclImpValidationManager::ReadDval( XclImpStream& rStrm )
     715             : {
     716          10 :     const XclImpRoot& rRoot = rStrm.GetRoot();
     717             :     OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
     718             : 
     719          10 :     sal_uInt32 nObjId(0);
     720          10 :     rStrm.Ignore( 10 );
     721          10 :     rStrm >> nObjId;
     722          10 :     if( nObjId != EXC_DVAL_NOOBJ )
     723             :     {
     724             :         OSL_ENSURE( nObjId <= 0xFFFF, "XclImpValidation::ReadDval - invalid object ID" );
     725           2 :         rRoot.GetCurrSheetDrawing().SetSkipObj( static_cast< sal_uInt16 >( nObjId ) );
     726             :     }
     727          10 : }
     728             : 
     729           6 : void XclImpValidationManager::ReadDV( XclImpStream& rStrm )
     730             : {
     731           6 :     const XclImpRoot& rRoot = rStrm.GetRoot();
     732             :     OSL_ENSURE_BIFF( rRoot.GetBiff() == EXC_BIFF8 );
     733             : 
     734           6 :     ScDocument& rDoc = rRoot.GetDoc();
     735           6 :     SCTAB nScTab = rRoot.GetCurrScTab();
     736           6 :     ExcelToSc& rFmlaConv = rRoot.GetOldFmlaConverter();
     737             : 
     738             :     // flags
     739           6 :     sal_uInt32 nFlags(0);
     740           6 :     rStrm >> nFlags;
     741             : 
     742             :     // message strings
     743             :     /*  Empty strings are single NUL characters in Excel (string length is 1).
     744             :         -> Do not let the stream replace them with '?' characters. */
     745           6 :     rStrm.SetNulSubstChar( '\0' );
     746           6 :     OUString aPromptTitle(   rStrm.ReadUniString() );
     747          12 :     OUString aErrorTitle(    rStrm.ReadUniString() );
     748          12 :     OUString aPromptMessage( rStrm.ReadUniString() );
     749          12 :     OUString aErrorMessage(  rStrm.ReadUniString() );
     750           6 :     rStrm.SetNulSubstChar();    // back to default
     751             : 
     752             :     // formula(s)
     753           6 :     if ( rStrm.GetRecLeft() <= 8 )
     754             :         // Not enough bytes left in the record.  Bail out.
     755           0 :         return;
     756             : 
     757             :     // first formula
     758             :     // string list is single tStr token with NUL separators -> replace them with LF
     759           6 :     rStrm.SetNulSubstChar( '\n' );
     760          12 :     ::std::unique_ptr< ScTokenArray > xTokArr1;
     761             : 
     762           6 :     sal_uInt16 nLen = 0;
     763           6 :     rStrm >> nLen;
     764           6 :     rStrm.Ignore( 2 );
     765           6 :     if( nLen > 0 )
     766             :     {
     767           2 :         const ScTokenArray* pTokArr = 0;
     768           2 :         rFmlaConv.Reset();
     769           2 :             rFmlaConv.Convert( pTokArr, rStrm, nLen, false, FT_CondFormat );
     770             :         // formula converter owns pTokArr -> create a copy of the token array
     771           2 :         if( pTokArr )
     772           2 :             xTokArr1.reset( pTokArr->Clone() );
     773             :     }
     774           6 :     rStrm.SetNulSubstChar();    // back to default
     775             : 
     776             :     // second formula
     777          12 :     ::std::unique_ptr< ScTokenArray > xTokArr2;
     778             : 
     779           6 :     nLen = 0;
     780           6 :     rStrm >> nLen;
     781           6 :     rStrm.Ignore( 2 );
     782           6 :     if( nLen > 0 )
     783             :     {
     784           0 :         const ScTokenArray* pTokArr = 0;
     785           0 :         rFmlaConv.Reset();
     786           0 :             rFmlaConv.Convert( pTokArr, rStrm, nLen, false, FT_CondFormat );
     787             :         // formula converter owns pTokArr -> create a copy of the token array
     788           0 :         if( pTokArr )
     789           0 :             xTokArr2.reset( pTokArr->Clone() );
     790             :     }
     791             : 
     792             :     // read all cell ranges
     793          12 :     XclRangeList aXclRanges;
     794           6 :     rStrm >> aXclRanges;
     795             : 
     796             :     // convert to Calc range list
     797          12 :     ScRangeList aScRanges;
     798           6 :     rRoot.GetAddressConverter().ConvertRangeList( aScRanges, aXclRanges, nScTab, true );
     799             : 
     800             :     // only continue if there are valid ranges
     801           6 :     if ( aScRanges.empty() )
     802           0 :         return;
     803             : 
     804           6 :     bool bIsValid = true;   // valid settings in flags field
     805             : 
     806           6 :     ScValidationMode eValMode = SC_VALID_ANY;
     807           6 :     switch( nFlags & EXC_DV_MODE_MASK )
     808             :     {
     809           4 :         case EXC_DV_MODE_ANY:       eValMode = SC_VALID_ANY;        break;
     810           0 :         case EXC_DV_MODE_WHOLE:     eValMode = SC_VALID_WHOLE;      break;
     811           0 :         case EXC_DV_MODE_DECIMAL:   eValMode = SC_VALID_DECIMAL;    break;
     812           2 :         case EXC_DV_MODE_LIST:      eValMode = SC_VALID_LIST;       break;
     813           0 :         case EXC_DV_MODE_DATE:      eValMode = SC_VALID_DATE;       break;
     814           0 :         case EXC_DV_MODE_TIME:      eValMode = SC_VALID_TIME;       break;
     815           0 :         case EXC_DV_MODE_TEXTLEN:   eValMode = SC_VALID_TEXTLEN;    break;
     816           0 :         case EXC_DV_MODE_CUSTOM:    eValMode = SC_VALID_CUSTOM;     break;
     817           0 :         default:                    bIsValid = false;
     818             :     }
     819           6 :     rRoot.GetTracer().TraceDVType(eValMode == SC_VALID_CUSTOM);
     820             : 
     821           6 :     ScConditionMode eCondMode = SC_COND_BETWEEN;
     822           6 :     switch( nFlags & EXC_DV_COND_MASK )
     823             :     {
     824           6 :         case EXC_DV_COND_BETWEEN:   eCondMode = SC_COND_BETWEEN;    break;
     825           0 :         case EXC_DV_COND_NOTBETWEEN:eCondMode = SC_COND_NOTBETWEEN; break;
     826           0 :         case EXC_DV_COND_EQUAL:     eCondMode = SC_COND_EQUAL;      break;
     827           0 :         case EXC_DV_COND_NOTEQUAL:  eCondMode = SC_COND_NOTEQUAL;   break;
     828           0 :         case EXC_DV_COND_GREATER:   eCondMode = SC_COND_GREATER;    break;
     829           0 :         case EXC_DV_COND_LESS:      eCondMode = SC_COND_LESS;       break;
     830           0 :         case EXC_DV_COND_EQGREATER: eCondMode = SC_COND_EQGREATER;  break;
     831           0 :         case EXC_DV_COND_EQLESS:    eCondMode = SC_COND_EQLESS;     break;
     832           0 :         default:                    bIsValid = false;
     833             :     }
     834             : 
     835           6 :     if ( !bIsValid )
     836             :         // No valid validation found.  Bail out.
     837           0 :         return;
     838             : 
     839             :     // first range for base address for relative references
     840           6 :     const ScRange& rScRange = *aScRanges.front();    // aScRanges is not empty
     841             : 
     842             :     // process string list of a list validity (convert to list of string tokens)
     843           6 :     if( xTokArr1.get() && (eValMode == SC_VALID_LIST) && ::get_flag( nFlags, EXC_DV_STRINGLIST ) )
     844           0 :         XclTokenArrayHelper::ConvertStringToList(*xTokArr1, rDoc.GetSharedStringPool(), '\n', true);
     845             : 
     846             :     maDVItems.push_back(
     847           6 :         new DVItem(aScRanges, ScValidationData(eValMode, eCondMode, xTokArr1.get(), xTokArr2.get(), &rDoc, rScRange.aStart)));
     848           6 :     DVItem& rItem = maDVItems.back();
     849             : 
     850           6 :     rItem.maValidData.SetIgnoreBlank( ::get_flag( nFlags, EXC_DV_IGNOREBLANK ) );
     851           6 :     rItem.maValidData.SetListType( ::get_flagvalue( nFlags, EXC_DV_SUPPRESSDROPDOWN, css::sheet::TableValidationVisibility::INVISIBLE, css::sheet::TableValidationVisibility::UNSORTED ) );
     852             : 
     853             :     // *** prompt box ***
     854           6 :     if( !aPromptTitle.isEmpty() || !aPromptMessage.isEmpty() )
     855             :     {
     856             :         // set any text stored in the record
     857           0 :         rItem.maValidData.SetInput( aPromptTitle, aPromptMessage );
     858           0 :         if( !::get_flag( nFlags, EXC_DV_SHOWPROMPT ) )
     859           0 :             rItem.maValidData.ResetInput();
     860             :     }
     861             : 
     862             :     // *** error box ***
     863           6 :     ScValidErrorStyle eErrStyle = SC_VALERR_STOP;
     864           6 :     switch( nFlags & EXC_DV_ERROR_MASK )
     865             :     {
     866           0 :         case EXC_DV_ERROR_WARNING:  eErrStyle = SC_VALERR_WARNING;  break;
     867           0 :         case EXC_DV_ERROR_INFO:     eErrStyle = SC_VALERR_INFO;     break;
     868             :     }
     869             :     // set texts and error style
     870           6 :     rItem.maValidData.SetError( aErrorTitle, aErrorMessage, eErrStyle );
     871           6 :     if( !::get_flag( nFlags, EXC_DV_SHOWERROR ) )
     872          10 :         rItem.maValidData.ResetError();
     873             : }
     874             : 
     875         412 : void XclImpValidationManager::Apply()
     876             : {
     877         412 :     ScDocument& rDoc = GetRoot().GetDoc();
     878         412 :     DVItemList::iterator itr = maDVItems.begin(), itrEnd = maDVItems.end();
     879         418 :     for (; itr != itrEnd; ++itr)
     880             :     {
     881           6 :         DVItem& rItem = *itr;
     882             :         // set the handle ID
     883           6 :         sal_uLong nHandle = rDoc.AddValidationEntry( rItem.maValidData );
     884           6 :         ScPatternAttr aPattern( rDoc.GetPool() );
     885           6 :         aPattern.GetItemSet().Put( SfxUInt32Item( ATTR_VALIDDATA, nHandle ) );
     886             : 
     887             :         // apply all ranges
     888          16 :         for ( size_t i = 0, nRanges = rItem.maRanges.size(); i < nRanges; ++i )
     889             :         {
     890          10 :             const ScRange* pScRange = rItem.maRanges[ i ];
     891          10 :             rDoc.ApplyPatternAreaTab( pScRange->aStart.Col(), pScRange->aStart.Row(),
     892          20 :                 pScRange->aEnd.Col(), pScRange->aEnd.Row(), pScRange->aStart.Tab(), aPattern );
     893             :         }
     894           6 :     }
     895         412 :     maDVItems.clear();
     896         412 : }
     897             : 
     898             : // Web queries ================================================================
     899             : 
     900           0 : XclImpWebQuery::XclImpWebQuery( const ScRange& rDestRange ) :
     901             :     maDestRange( rDestRange ),
     902             :     meMode( xlWQUnknown ),
     903           0 :     mnRefresh( 0 )
     904             : {
     905           0 : }
     906             : 
     907           0 : void XclImpWebQuery::ReadParamqry( XclImpStream& rStrm )
     908             : {
     909           0 :     sal_uInt16 nFlags = rStrm.ReaduInt16();
     910           0 :     sal_uInt16 nType = ::extract_value< sal_uInt16 >( nFlags, 0, 3 );
     911           0 :     if( (nType == EXC_PQRYTYPE_WEBQUERY) && ::get_flag( nFlags, EXC_PQRY_WEBQUERY ) )
     912             :     {
     913           0 :         if( ::get_flag( nFlags, EXC_PQRY_TABLES ) )
     914             :         {
     915           0 :             meMode = xlWQAllTables;
     916           0 :             maTables = ScfTools::GetHTMLTablesName();
     917             :         }
     918             :         else
     919             :         {
     920           0 :             meMode = xlWQDocument;
     921           0 :             maTables = ScfTools::GetHTMLDocName();
     922             :         }
     923             :     }
     924           0 : }
     925             : 
     926           0 : void XclImpWebQuery::ReadWqstring( XclImpStream& rStrm )
     927             : {
     928           0 :     maURL = rStrm.ReadUniString();
     929           0 : }
     930             : 
     931           0 : void XclImpWebQuery::ReadWqsettings( XclImpStream& rStrm )
     932             : {
     933           0 :     rStrm.Ignore( 10 );
     934           0 :     sal_uInt16 nFlags(0);
     935           0 :     rStrm >> nFlags;
     936           0 :     rStrm.Ignore( 10 );
     937           0 :     rStrm >> mnRefresh;
     938             : 
     939           0 :     if( ::get_flag( nFlags, EXC_WQSETT_SPECTABLES ) && (meMode == xlWQAllTables) )
     940           0 :         meMode = xlWQSpecTables;
     941           0 : }
     942             : 
     943           0 : void XclImpWebQuery::ReadWqtables( XclImpStream& rStrm )
     944             : {
     945           0 :     if( meMode == xlWQSpecTables )
     946             :     {
     947           0 :         rStrm.Ignore( 4 );
     948           0 :         OUString aTables( rStrm.ReadUniString() );
     949             : 
     950           0 :         const sal_Unicode cSep = ';';
     951           0 :         OUString aQuotedPairs( "\"\"" );
     952           0 :         sal_Int32 nTokenCnt = ScStringUtil::GetQuotedTokenCount( aTables, aQuotedPairs, ',' );
     953           0 :         maTables = "";
     954           0 :         sal_Int32 nStringIx = 0;
     955           0 :         for( sal_Int32 nToken = 0; nToken < nTokenCnt; ++nToken )
     956             :         {
     957           0 :             OUString aToken( ScStringUtil::GetQuotedToken( aTables, 0, aQuotedPairs, ',', nStringIx ) );
     958           0 :             sal_Int32 nTabNum = CharClass::isAsciiNumeric( aToken ) ? aToken.toInt32() : 0;
     959           0 :             if( nTabNum > 0 )
     960           0 :                 maTables = ScGlobal::addToken( maTables, ScfTools::GetNameFromHTMLIndex( static_cast< sal_uInt32 >( nTabNum ) ), cSep );
     961             :             else
     962             :             {
     963           0 :                 ScGlobal::EraseQuotes( aToken, '"', false );
     964           0 :                 if( !aToken.isEmpty() )
     965           0 :                     maTables = ScGlobal::addToken( maTables, ScfTools::GetNameFromHTMLName( aToken ), cSep );
     966             :             }
     967           0 :         }
     968             :     }
     969           0 : }
     970             : 
     971           0 : void XclImpWebQuery::Apply( ScDocument& rDoc, const OUString& rFilterName )
     972             : {
     973           0 :     if( !maURL.isEmpty() && (meMode != xlWQUnknown) && rDoc.GetDocumentShell() )
     974             :     {
     975             :         ScAreaLink* pLink = new ScAreaLink( rDoc.GetDocumentShell(),
     976           0 :             maURL, rFilterName, EMPTY_OUSTRING, maTables, maDestRange, mnRefresh * 60UL );
     977             :         rDoc.GetLinkManager()->InsertFileLink( *pLink, OBJECT_CLIENT_FILE,
     978           0 :             maURL, &rFilterName, &maTables );
     979             :     }
     980           0 : }
     981             : 
     982         142 : XclImpWebQueryBuffer::XclImpWebQueryBuffer( const XclImpRoot& rRoot ) :
     983         142 :     XclImpRoot( rRoot )
     984             : {
     985         142 : }
     986             : 
     987           0 : void XclImpWebQueryBuffer::ReadQsi( XclImpStream& rStrm )
     988             : {
     989           0 :     if( GetBiff() == EXC_BIFF8 )
     990             :     {
     991           0 :         rStrm.Ignore( 10 );
     992           0 :         OUString aXclName( rStrm.ReadUniString() );
     993             : 
     994             :         // #i64794# Excel replaces spaces with underscores
     995           0 :         aXclName = aXclName.replaceAll( " ", "_" );
     996             : 
     997             :         // find the defined name used in Calc
     998           0 :         if( const XclImpName* pName = GetNameManager().FindName( aXclName, GetCurrScTab() ) )
     999             :         {
    1000           0 :             if( const ScRangeData* pRangeData = pName->GetScRangeData() )
    1001             :             {
    1002           0 :                 ScRange aRange;
    1003           0 :                 if( pRangeData->IsReference( aRange ) )
    1004           0 :                     maWQList.push_back( new XclImpWebQuery( aRange ) );
    1005             :             }
    1006           0 :         }
    1007             :     }
    1008             :     else
    1009             :     {
    1010             :         DBG_ERROR_BIFF();
    1011             :     }
    1012           0 : }
    1013             : 
    1014           0 : void XclImpWebQueryBuffer::ReadParamqry( XclImpStream& rStrm )
    1015             : {
    1016           0 :     if (!maWQList.empty())
    1017           0 :         maWQList.back().ReadParamqry( rStrm );
    1018           0 : }
    1019             : 
    1020           0 : void XclImpWebQueryBuffer::ReadWqstring( XclImpStream& rStrm )
    1021             : {
    1022           0 :     if (!maWQList.empty())
    1023           0 :         maWQList.back().ReadWqstring( rStrm );
    1024           0 : }
    1025             : 
    1026           0 : void XclImpWebQueryBuffer::ReadWqsettings( XclImpStream& rStrm )
    1027             : {
    1028           0 :     if (!maWQList.empty())
    1029           0 :         maWQList.back().ReadWqsettings( rStrm );
    1030           0 : }
    1031             : 
    1032           0 : void XclImpWebQueryBuffer::ReadWqtables( XclImpStream& rStrm )
    1033             : {
    1034           0 :     if (!maWQList.empty())
    1035           0 :         maWQList.back().ReadWqtables( rStrm );
    1036           0 : }
    1037             : 
    1038         142 : void XclImpWebQueryBuffer::Apply()
    1039             : {
    1040         142 :     ScDocument& rDoc = GetDoc();
    1041         142 :     OUString aFilterName( EXC_WEBQRY_FILTER );
    1042         142 :     for( XclImpWebQueryList::iterator itQuery = maWQList.begin(); itQuery != maWQList.end(); ++itQuery )
    1043         142 :         itQuery->Apply( rDoc, aFilterName );
    1044         142 : }
    1045             : 
    1046             : // Decryption =================================================================
    1047             : 
    1048             : namespace {
    1049             : 
    1050           0 : XclImpDecrypterRef lclReadFilepass5( XclImpStream& rStrm )
    1051             : {
    1052           0 :     XclImpDecrypterRef xDecr;
    1053             :     OSL_ENSURE( rStrm.GetRecLeft() == 4, "lclReadFilepass5 - wrong record size" );
    1054           0 :     if( rStrm.GetRecLeft() == 4 )
    1055             :     {
    1056           0 :         sal_uInt16 nKey(0), nHash(0);
    1057           0 :         rStrm >> nKey >> nHash;
    1058           0 :         xDecr.reset( new XclImpBiff5Decrypter( nKey, nHash ) );
    1059             :     }
    1060           0 :     return xDecr;
    1061             : }
    1062             : 
    1063           0 : XclImpDecrypterRef lclReadFilepass8_Standard( XclImpStream& rStrm )
    1064             : {
    1065           0 :     XclImpDecrypterRef xDecr;
    1066             :     OSL_ENSURE( rStrm.GetRecLeft() == 48, "lclReadFilepass8 - wrong record size" );
    1067           0 :     if( rStrm.GetRecLeft() == 48 )
    1068             :     {
    1069             :         sal_uInt8 pnSalt[ 16 ];
    1070             :         sal_uInt8 pnVerifier[ 16 ];
    1071             :         sal_uInt8 pnVerifierHash[ 16 ];
    1072           0 :         rStrm.Read( pnSalt, 16 );
    1073           0 :         rStrm.Read( pnVerifier, 16 );
    1074           0 :         rStrm.Read( pnVerifierHash, 16 );
    1075           0 :         xDecr.reset( new XclImpBiff8Decrypter( pnSalt, pnVerifier, pnVerifierHash ) );
    1076             :     }
    1077           0 :     return xDecr;
    1078             : }
    1079             : 
    1080           0 : XclImpDecrypterRef lclReadFilepass8_Strong( XclImpStream& /*rStrm*/ )
    1081             : {
    1082             :     // not supported
    1083           0 :     return XclImpDecrypterRef();
    1084             : }
    1085             : 
    1086           0 : XclImpDecrypterRef lclReadFilepass8( XclImpStream& rStrm )
    1087             : {
    1088           0 :     XclImpDecrypterRef xDecr;
    1089             : 
    1090           0 :     sal_uInt16 nMode(0);
    1091           0 :     rStrm >> nMode;
    1092           0 :     switch( nMode )
    1093             :     {
    1094             :         case EXC_FILEPASS_BIFF5:
    1095           0 :             xDecr = lclReadFilepass5( rStrm );
    1096           0 :         break;
    1097             : 
    1098             :         case EXC_FILEPASS_BIFF8:
    1099             :         {
    1100           0 :             rStrm.Ignore( 2 );
    1101           0 :             sal_uInt16 nSubMode(0);
    1102           0 :             rStrm >> nSubMode;
    1103           0 :             switch( nSubMode )
    1104             :             {
    1105             :                 case EXC_FILEPASS_BIFF8_STD:
    1106           0 :                     xDecr = lclReadFilepass8_Standard( rStrm );
    1107           0 :                 break;
    1108             :                 case EXC_FILEPASS_BIFF8_STRONG:
    1109           0 :                     xDecr = lclReadFilepass8_Strong( rStrm );
    1110           0 :                 break;
    1111             :                 default:
    1112             :                     OSL_FAIL( "lclReadFilepass8 - unknown BIFF8 encryption sub mode" );
    1113             :             }
    1114             :         }
    1115           0 :         break;
    1116             : 
    1117             :         default:
    1118             :             OSL_FAIL( "lclReadFilepass8 - unknown encryption mode" );
    1119             :     }
    1120             : 
    1121           0 :     return xDecr;
    1122             : }
    1123             : 
    1124             : } // namespace
    1125             : 
    1126           0 : ErrCode XclImpDecryptHelper::ReadFilepass( XclImpStream& rStrm )
    1127             : {
    1128           0 :     XclImpDecrypterRef xDecr;
    1129           0 :     rStrm.DisableDecryption();
    1130             : 
    1131             :     // read the FILEPASS record and create a new decrypter object
    1132           0 :     switch( rStrm.GetRoot().GetBiff() )
    1133             :     {
    1134             :         case EXC_BIFF2:
    1135             :         case EXC_BIFF3:
    1136             :         case EXC_BIFF4:
    1137           0 :         case EXC_BIFF5: xDecr = lclReadFilepass5( rStrm );  break;
    1138           0 :         case EXC_BIFF8: xDecr = lclReadFilepass8( rStrm );  break;
    1139             :         default:        DBG_ERROR_BIFF();
    1140             :     };
    1141             : 
    1142             :     // set decrypter at import stream
    1143           0 :     rStrm.SetDecrypter( xDecr );
    1144             : 
    1145             :     // request and verify a password (decrypter implements IDocPasswordVerifier)
    1146           0 :     if( xDecr )
    1147           0 :         rStrm.GetRoot().RequestEncryptionData( *xDecr );
    1148             : 
    1149             :     // return error code (success, wrong password, etc.)
    1150           0 :     return xDecr ? xDecr->GetError() : EXC_ENCR_ERROR_UNSUPP_CRYPT;
    1151             : }
    1152             : 
    1153             : // Document protection ========================================================
    1154             : 
    1155         142 : XclImpDocProtectBuffer::XclImpDocProtectBuffer( const XclImpRoot& rRoot ) :
    1156             :     XclImpRoot( rRoot ),
    1157             :     mnPassHash(0x0000),
    1158             :     mbDocProtect(false),
    1159         142 :     mbWinProtect(false)
    1160             : {
    1161         142 : }
    1162             : 
    1163          96 : void XclImpDocProtectBuffer::ReadDocProtect( XclImpStream& rStrm )
    1164             : {
    1165          96 :     mbDocProtect = rStrm.ReaduInt16() ? true : false;
    1166          96 : }
    1167             : 
    1168          96 : void XclImpDocProtectBuffer::ReadWinProtect( XclImpStream& rStrm )
    1169             : {
    1170          96 :     mbWinProtect = rStrm.ReaduInt16() ? true : false;
    1171          96 : }
    1172             : 
    1173          96 : void XclImpDocProtectBuffer::ReadPasswordHash( XclImpStream& rStrm )
    1174             : {
    1175          96 :     rStrm.EnableDecryption();
    1176          96 :     mnPassHash = rStrm.ReaduInt16();
    1177          96 : }
    1178             : 
    1179         142 : void XclImpDocProtectBuffer::Apply() const
    1180             : {
    1181         142 :     if (!mbDocProtect && !mbWinProtect)
    1182             :         // Excel requires either the structure or windows protection is set.
    1183             :         // If neither is set then the document is not protected at all.
    1184         282 :         return;
    1185             : 
    1186           2 :     unique_ptr<ScDocProtection> pProtect(new ScDocProtection);
    1187           2 :     pProtect->setProtected(true);
    1188             : 
    1189           2 :     if (mnPassHash)
    1190             :     {
    1191             :         // 16-bit password pash.
    1192           0 :         Sequence<sal_Int8> aPass(2);
    1193           0 :         aPass[0] = (mnPassHash >> 8) & 0xFF;
    1194           0 :         aPass[1] = mnPassHash & 0xFF;
    1195           0 :         pProtect->setPasswordHash(aPass, PASSHASH_XL);
    1196             :     }
    1197             : 
    1198             :     // document protection options
    1199           2 :     pProtect->setOption(ScDocProtection::STRUCTURE, mbDocProtect);
    1200           2 :     pProtect->setOption(ScDocProtection::WINDOWS,   mbWinProtect);
    1201             : 
    1202           2 :     GetDoc().SetDocProtection(pProtect.get());
    1203             : }
    1204             : 
    1205             : // Sheet Protection ===========================================================
    1206             : 
    1207         222 : XclImpSheetProtectBuffer::Sheet::Sheet() :
    1208             :     mbProtected(false),
    1209             :     mnPasswordHash(0x0000),
    1210         222 :     mnOptions(0x4400)
    1211             : {
    1212         222 : }
    1213             : 
    1214         444 : XclImpSheetProtectBuffer::Sheet::Sheet(const Sheet& r) :
    1215             :     mbProtected(r.mbProtected),
    1216             :     mnPasswordHash(r.mnPasswordHash),
    1217         444 :     mnOptions(r.mnOptions)
    1218             : {
    1219         444 : }
    1220             : 
    1221         142 : XclImpSheetProtectBuffer::XclImpSheetProtectBuffer( const XclImpRoot& rRoot ) :
    1222         142 :     XclImpRoot( rRoot )
    1223             : {
    1224         142 : }
    1225             : 
    1226          26 : void XclImpSheetProtectBuffer::ReadProtect( XclImpStream& rStrm, SCTAB nTab )
    1227             : {
    1228          26 :     if ( rStrm.ReaduInt16() )
    1229             :     {
    1230          24 :         Sheet* pSheet = GetSheetItem(nTab);
    1231          24 :         if (pSheet)
    1232          24 :             pSheet->mbProtected = true;
    1233             :     }
    1234          26 : }
    1235             : 
    1236         204 : void XclImpSheetProtectBuffer::ReadOptions( XclImpStream& rStrm, SCTAB nTab )
    1237             : {
    1238             :     // The flag size specifies the size of bytes that follows that stores
    1239             :     // feature data.  If -1 it depends on the feature type imported earlier.
    1240             :     // For enhanced protection data, the size is always 4.  For the most xls
    1241             :     // documents out there this value is almost always -1.
    1242         204 :     sal_Int32 nFlagSize(0);
    1243         204 :     rStrm >> nFlagSize;
    1244         204 :     if (nFlagSize != -1)
    1245         204 :         return;
    1246             : 
    1247             :     // There are actually 4 bytes to read, but the upper 2 bytes currently
    1248             :     // don't store any bits.
    1249         204 :     sal_uInt16 nOptions(0);
    1250         204 :     rStrm >> nOptions;
    1251             : 
    1252         204 :     Sheet* pSheet = GetSheetItem(nTab);
    1253         204 :     if (pSheet)
    1254         204 :         pSheet->mnOptions = nOptions;
    1255             : }
    1256             : 
    1257          10 : void XclImpSheetProtectBuffer::AppendEnhancedProtection( const ScEnhancedProtection & rProt, SCTAB nTab )
    1258             : {
    1259          10 :     Sheet* pSheet = GetSheetItem(nTab);
    1260          10 :     if (pSheet)
    1261          10 :         pSheet->maEnhancedProtections.push_back( rProt);
    1262          10 : }
    1263             : 
    1264           2 : void XclImpSheetProtectBuffer::ReadPasswordHash( XclImpStream& rStrm, SCTAB nTab )
    1265             : {
    1266           2 :     sal_uInt16 nHash(0);
    1267           2 :     rStrm >> nHash;
    1268           2 :     Sheet* pSheet = GetSheetItem(nTab);
    1269           2 :     if (pSheet)
    1270           2 :         pSheet->mnPasswordHash = nHash;
    1271           2 : }
    1272             : 
    1273         142 : void XclImpSheetProtectBuffer::Apply() const
    1274             : {
    1275         364 :     for (ProtectedSheetMap::const_iterator itr = maProtectedSheets.begin(), itrEnd = maProtectedSheets.end();
    1276             :          itr != itrEnd; ++itr)
    1277             :     {
    1278         222 :         if (!itr->second.mbProtected)
    1279             :             // This sheet is (for whatever reason) not protected.
    1280         198 :             continue;
    1281             : 
    1282          24 :         unique_ptr<ScTableProtection> pProtect(new ScTableProtection);
    1283          24 :         pProtect->setProtected(true);
    1284             : 
    1285             :         // 16-bit hash password
    1286          24 :         const sal_uInt16 nHash = itr->second.mnPasswordHash;
    1287          24 :         if (nHash)
    1288             :         {
    1289           0 :             Sequence<sal_Int8> aPass(2);
    1290           0 :             aPass[0] = (nHash >> 8) & 0xFF;
    1291           0 :             aPass[1] = nHash & 0xFF;
    1292           0 :             pProtect->setPasswordHash(aPass, PASSHASH_XL);
    1293             :         }
    1294             : 
    1295             :         // sheet protection options
    1296          24 :         const sal_uInt16 nOptions = itr->second.mnOptions;
    1297          24 :         pProtect->setOption( ScTableProtection::OBJECTS,               (nOptions & 0x0001) );
    1298          24 :         pProtect->setOption( ScTableProtection::SCENARIOS,             (nOptions & 0x0002) );
    1299          24 :         pProtect->setOption( ScTableProtection::FORMAT_CELLS,          (nOptions & 0x0004) );
    1300          24 :         pProtect->setOption( ScTableProtection::FORMAT_COLUMNS,        (nOptions & 0x0008) );
    1301          24 :         pProtect->setOption( ScTableProtection::FORMAT_ROWS,           (nOptions & 0x0010) );
    1302          24 :         pProtect->setOption( ScTableProtection::INSERT_COLUMNS,        (nOptions & 0x0020) );
    1303          24 :         pProtect->setOption( ScTableProtection::INSERT_ROWS,           (nOptions & 0x0040) );
    1304          24 :         pProtect->setOption( ScTableProtection::INSERT_HYPERLINKS,     (nOptions & 0x0080) );
    1305          24 :         pProtect->setOption( ScTableProtection::DELETE_COLUMNS,        (nOptions & 0x0100) );
    1306          24 :         pProtect->setOption( ScTableProtection::DELETE_ROWS,           (nOptions & 0x0200) );
    1307          24 :         pProtect->setOption( ScTableProtection::SELECT_LOCKED_CELLS,   (nOptions & 0x0400) );
    1308          24 :         pProtect->setOption( ScTableProtection::SORT,                  (nOptions & 0x0800) );
    1309          24 :         pProtect->setOption( ScTableProtection::AUTOFILTER,            (nOptions & 0x1000) );
    1310          24 :         pProtect->setOption( ScTableProtection::PIVOT_TABLES,          (nOptions & 0x2000) );
    1311          24 :         pProtect->setOption( ScTableProtection::SELECT_UNLOCKED_CELLS, (nOptions & 0x4000) );
    1312             : 
    1313             :         // Enhanced protection containing editable ranges and permissions.
    1314          24 :         pProtect->setEnhancedProtection( itr->second.maEnhancedProtections);
    1315             : 
    1316             :         // all done.  now commit.
    1317          24 :         GetDoc().SetTabProtection(itr->first, pProtect.get());
    1318          24 :     }
    1319         142 : }
    1320             : 
    1321         240 : XclImpSheetProtectBuffer::Sheet* XclImpSheetProtectBuffer::GetSheetItem( SCTAB nTab )
    1322             : {
    1323         240 :     ProtectedSheetMap::iterator itr = maProtectedSheets.find(nTab);
    1324         240 :     if (itr == maProtectedSheets.end())
    1325             :     {
    1326             :         // new sheet
    1327         222 :         if ( !maProtectedSheets.insert( ProtectedSheetMap::value_type(nTab, Sheet()) ).second )
    1328           0 :             return NULL;
    1329             : 
    1330         222 :         itr = maProtectedSheets.find(nTab);
    1331             :     }
    1332             : 
    1333         240 :     return &itr->second;
    1334          48 : }
    1335             : 
    1336             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10