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