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