Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "XclImpChangeTrack.hxx"
22 : #include <sot/storage.hxx>
23 : #include <svl/zforlist.hxx>
24 : #include "svl/sharedstringpool.hxx"
25 : #include "chgviset.hxx"
26 : #include "formulacell.hxx"
27 : #include "chgtrack.hxx"
28 : #include "xihelper.hxx"
29 : #include "xilink.hxx"
30 : #include "externalrefmgr.hxx"
31 : #include "document.hxx"
32 :
33 :
34 : // class XclImpChangeTrack
35 :
36 0 : XclImpChangeTrack::XclImpChangeTrack( const XclImpRoot& rRoot, const XclImpStream& rBookStrm ) :
37 : XclImpRoot( rRoot ),
38 : aRecHeader(),
39 : sOldUsername(),
40 : pChangeTrack( NULL ),
41 : pStrm( NULL ),
42 : nTabIdCount( 0 ),
43 : bGlobExit( false ),
44 0 : eNestedMode( nmBase )
45 : {
46 : // Verify that the User Names stream exists before going any further. Excel adds both
47 : // "Revision Log" and "User Names" streams when Change Tracking is active but the Revision log
48 : // remains if Change Tracking is turned off.
49 0 : SotStorageStreamRef xUserStrm = OpenStream( EXC_STREAM_USERNAMES );
50 0 : if( !xUserStrm.Is() )
51 0 : return;
52 :
53 0 : xInStrm = OpenStream( EXC_STREAM_REVLOG );
54 0 : if( xInStrm.Is() )
55 : {
56 0 : xInStrm->Seek( STREAM_SEEK_TO_END );
57 0 : sal_uInt64 const nStreamLen = xInStrm->Tell();
58 0 : if( (xInStrm->GetErrorCode() == ERRCODE_NONE) && (nStreamLen != STREAM_SEEK_TO_END) )
59 : {
60 0 : xInStrm->Seek( STREAM_SEEK_TO_BEGIN );
61 0 : pStrm = new XclImpStream( *xInStrm, GetRoot() );
62 0 : pStrm->CopyDecrypterFrom( rBookStrm );
63 0 : pChangeTrack = new ScChangeTrack( GetDocPtr() );
64 :
65 0 : sOldUsername = pChangeTrack->GetUser();
66 0 : pChangeTrack->SetUseFixDateTime( true );
67 :
68 0 : ReadRecords();
69 : }
70 0 : }
71 : }
72 :
73 0 : XclImpChangeTrack::~XclImpChangeTrack()
74 : {
75 0 : delete pChangeTrack;
76 0 : delete pStrm;
77 0 : }
78 :
79 0 : void XclImpChangeTrack::DoAcceptRejectAction( ScChangeAction* pAction )
80 : {
81 0 : if( !pAction ) return;
82 0 : switch( aRecHeader.nAccept )
83 : {
84 : case EXC_CHTR_ACCEPT:
85 0 : pChangeTrack->Accept( pAction );
86 0 : break;
87 : case EXC_CHTR_REJECT:
88 0 : break;
89 : }
90 : }
91 :
92 0 : void XclImpChangeTrack::DoAcceptRejectAction( sal_uInt32 nFirst, sal_uInt32 nLast )
93 : {
94 0 : for( sal_uInt32 nIndex = nFirst; nIndex <= nLast; nIndex++ )
95 0 : DoAcceptRejectAction( pChangeTrack->GetAction( nIndex ) );
96 0 : }
97 :
98 0 : void XclImpChangeTrack::DoInsertRange( const ScRange& rRange )
99 : {
100 0 : sal_uInt32 nFirst = pChangeTrack->GetActionMax() + 1;
101 0 : pChangeTrack->AppendInsert( rRange );
102 0 : sal_uInt32 nLast = pChangeTrack->GetActionMax();
103 0 : DoAcceptRejectAction( nFirst, nLast );
104 0 : }
105 :
106 0 : void XclImpChangeTrack::DoDeleteRange( const ScRange& rRange )
107 : {
108 : sal_uLong nFirst, nLast;
109 0 : pChangeTrack->AppendDeleteRange( rRange, NULL, nFirst, nLast );
110 0 : DoAcceptRejectAction( nFirst, nLast );
111 0 : }
112 :
113 0 : SCTAB XclImpChangeTrack::ReadTabNum()
114 : {
115 0 : return static_cast<SCTAB>(GetTabInfo().GetCurrentIndex(
116 0 : pStrm->ReaduInt16(), nTabIdCount ));
117 : }
118 :
119 0 : void XclImpChangeTrack::ReadDateTime( DateTime& rDateTime )
120 : {
121 : sal_uInt16 nYear;
122 : sal_uInt8 nMonth, nDay, nHour, nMin, nSec;
123 :
124 0 : *pStrm >> nYear >> nMonth >> nDay >> nHour >> nMin >> nSec;
125 :
126 0 : rDateTime.SetYear( nYear );
127 0 : rDateTime.SetMonth( nMonth );
128 0 : rDateTime.SetDay( nDay );
129 0 : rDateTime.SetHour( nHour );
130 0 : rDateTime.SetMin( nMin );
131 0 : rDateTime.SetSec( nSec );
132 0 : rDateTime.SetNanoSec( 0 );
133 0 : }
134 :
135 0 : bool XclImpChangeTrack::CheckRecord( sal_uInt16 nOpCode )
136 : {
137 0 : if( (nOpCode != EXC_CHTR_OP_UNKNOWN) && (aRecHeader.nOpCode != nOpCode) )
138 : {
139 : OSL_FAIL( "XclImpChangeTrack::CheckRecord - unknown action" );
140 0 : return false;
141 : }
142 0 : return aRecHeader.nIndex != 0;
143 : }
144 :
145 0 : bool XclImpChangeTrack::Read3DTabRefInfo( SCTAB& rFirstTab, SCTAB& rLastTab, ExcelToSc8::ExternalTabInfo& rExtInfo )
146 : {
147 0 : if( LookAtuInt8() == 0x01 )
148 : {
149 0 : rExtInfo.mbExternal = false;
150 : // internal ref - read tab num and return sc tab num (position in TABID list)
151 0 : pStrm->Ignore( 3 );
152 0 : rFirstTab = static_cast< SCTAB >( GetTabInfo().GetCurrentIndex( pStrm->ReaduInt16(), nTabIdCount ) );
153 0 : sal_uInt8 nFillByte = pStrm->ReaduInt8();
154 : rLastTab = (nFillByte == 0x00) ?
155 0 : static_cast< SCTAB >( GetTabInfo().GetCurrentIndex( pStrm->ReaduInt16(), nTabIdCount ) ) : rFirstTab;
156 : }
157 : else
158 : {
159 : // external ref - read doc and tab name and find sc tab num
160 : // - URL
161 0 : OUString aEncUrl( pStrm->ReadUniString() );
162 0 : OUString aUrl;
163 : bool bSelf;
164 0 : XclImpUrlHelper::DecodeUrl( aUrl, bSelf, GetRoot(), aEncUrl );
165 0 : pStrm->Ignore( 1 );
166 : // - sheet name, always separated from URL
167 0 : OUString aTabName( pStrm->ReadUniString() );
168 0 : pStrm->Ignore( 1 );
169 :
170 0 : rExtInfo.mbExternal = true;
171 0 : ScExternalRefManager* pRefMgr = GetDoc().GetExternalRefManager();
172 0 : pRefMgr->convertToAbsName(aUrl);
173 0 : rExtInfo.mnFileId = pRefMgr->getExternalFileId(aUrl);
174 0 : rExtInfo.maTabName = aTabName;
175 0 : rFirstTab = rLastTab = 0;
176 : }
177 0 : return true;
178 : }
179 :
180 0 : void XclImpChangeTrack::ReadFormula( ScTokenArray*& rpTokenArray, const ScAddress& rPosition )
181 : {
182 : sal_uInt16 nFmlSize;
183 0 : *pStrm >> nFmlSize;
184 :
185 : // create a memory stream and copy the formula to be able to read simultaneously
186 : // the formula and the additional 3D tab ref data following the formula
187 : // here we have to simulate an Excel record to be able to use an XclImpStream...
188 : // 2do: remove the stream member from formula converter and add it as a parameter
189 : // to the Convert() routine (to prevent the construction/destruction of the
190 : // converter in each formula)
191 0 : SvMemoryStream aMemStrm;
192 0 : aMemStrm.WriteUInt16( (sal_uInt16) 0x0001 ).WriteUInt16( nFmlSize );
193 0 : size_t nRead = pStrm->CopyToStream( aMemStrm, nFmlSize );
194 :
195 : // survive reading invalid streams!
196 : // if we can't read as many bytes as required just don't use them and
197 : // assume that this part is broken
198 0 : if(nRead != nFmlSize)
199 : {
200 0 : rpTokenArray = NULL;
201 0 : pStrm->Ignore(1);
202 0 : return;
203 : }
204 :
205 0 : XclImpStream aFmlaStrm( aMemStrm, GetRoot() );
206 0 : aFmlaStrm.StartNextRecord();
207 0 : XclImpChTrFmlConverter aFmlConv( GetRoot(), *this );
208 :
209 : // read the formula, 3D tab refs from extended data
210 0 : const ScTokenArray* pArray = NULL;
211 0 : aFmlConv.Reset( rPosition );
212 0 : sal_Bool bOK = (aFmlConv.Convert( pArray, aFmlaStrm, nFmlSize, false, FT_CellFormula) == ConvOK); // JEG : Check This
213 0 : rpTokenArray = (bOK && pArray) ? new ScTokenArray( *pArray ) : NULL;
214 0 : pStrm->Ignore( 1 );
215 : }
216 :
217 0 : void XclImpChangeTrack::ReadCell(
218 : ScCellValue& rCell, sal_uInt32& rFormat, sal_uInt16 nFlags, const ScAddress& rPosition )
219 : {
220 0 : rCell.clear();
221 0 : rFormat = 0;
222 0 : switch( nFlags & EXC_CHTR_TYPE_MASK )
223 : {
224 : case EXC_CHTR_TYPE_EMPTY:
225 0 : break;
226 : case EXC_CHTR_TYPE_RK:
227 : {
228 0 : double fValue = ReadRK();
229 0 : if( pStrm->IsValid() )
230 : {
231 0 : rCell.meType = CELLTYPE_VALUE;
232 0 : rCell.mfValue = fValue;
233 : }
234 : }
235 0 : break;
236 : case EXC_CHTR_TYPE_DOUBLE:
237 : {
238 : double fValue;
239 0 : *pStrm >> fValue;
240 0 : if( pStrm->IsValid() )
241 : {
242 0 : rCell.meType = CELLTYPE_VALUE;
243 0 : rCell.mfValue = fValue;
244 : }
245 : }
246 0 : break;
247 : case EXC_CHTR_TYPE_STRING:
248 : {
249 0 : OUString sString = pStrm->ReadUniString();
250 0 : if( pStrm->IsValid() )
251 : {
252 0 : rCell.meType = CELLTYPE_STRING;
253 0 : rCell.mpString = new svl::SharedString(GetDoc().GetSharedStringPool().intern(sString));
254 0 : }
255 : }
256 0 : break;
257 : case EXC_CHTR_TYPE_BOOL:
258 : {
259 0 : double fValue = (double) ReadBool();
260 0 : if( pStrm->IsValid() )
261 : {
262 0 : rCell.meType = CELLTYPE_VALUE;
263 0 : rCell.mfValue = fValue;
264 0 : rFormat = GetFormatter().GetStandardFormat( NUMBERFORMAT_LOGICAL, ScGlobal::eLnge );
265 : }
266 : }
267 0 : break;
268 : case EXC_CHTR_TYPE_FORMULA:
269 : {
270 0 : ScTokenArray* pTokenArray = NULL;
271 0 : ReadFormula( pTokenArray, rPosition );
272 0 : if( pStrm->IsValid() && pTokenArray )
273 : {
274 0 : rCell.meType = CELLTYPE_FORMULA;
275 0 : rCell.mpFormula = new ScFormulaCell(GetDocPtr(), rPosition, *pTokenArray);
276 : }
277 0 : delete pTokenArray;
278 : }
279 0 : break;
280 : default:
281 : OSL_FAIL( "XclImpChangeTrack::ReadCell - unknown data type" );
282 : }
283 0 : }
284 :
285 0 : void XclImpChangeTrack::ReadChTrInsert()
286 : {
287 0 : *pStrm >> aRecHeader;
288 0 : if( CheckRecord( EXC_CHTR_OP_UNKNOWN ) )
289 : {
290 0 : if( (aRecHeader.nOpCode != EXC_CHTR_OP_INSROW) &&
291 0 : (aRecHeader.nOpCode != EXC_CHTR_OP_INSCOL) &&
292 0 : (aRecHeader.nOpCode != EXC_CHTR_OP_DELROW) &&
293 0 : (aRecHeader.nOpCode != EXC_CHTR_OP_DELCOL) )
294 : {
295 : OSL_FAIL( "XclImpChangeTrack::ReadChTrInsert - unknown action" );
296 0 : return;
297 : }
298 :
299 0 : ScRange aRange;
300 0 : aRange.aStart.SetTab( ReadTabNum() );
301 0 : aRange.aEnd.SetTab( aRange.aStart.Tab() );
302 0 : pStrm->Ignore( 2 );
303 0 : Read2DRange( aRange );
304 :
305 0 : if( aRecHeader.nOpCode & EXC_CHTR_OP_COLFLAG )
306 0 : aRange.aEnd.SetRow( MAXROW );
307 : else
308 0 : aRange.aEnd.SetCol( MAXCOL );
309 :
310 0 : sal_Bool bValid = pStrm->IsValid();
311 0 : if( FoundNestedMode() )
312 0 : ReadNestedRecords();
313 :
314 0 : if( bValid )
315 : {
316 0 : if( aRecHeader.nOpCode & EXC_CHTR_OP_DELFLAG )
317 0 : DoDeleteRange( aRange );
318 : else
319 0 : DoInsertRange( aRange );
320 : }
321 : }
322 : }
323 :
324 0 : void XclImpChangeTrack::ReadChTrInfo()
325 : {
326 0 : pStrm->DisableDecryption();
327 0 : pStrm->Ignore( 32 );
328 0 : OUString sUsername( pStrm->ReadUniString() );
329 0 : if( !pStrm->IsValid() ) return;
330 :
331 0 : if( !sUsername.isEmpty() )
332 0 : pChangeTrack->SetUser( sUsername );
333 0 : pStrm->Seek( 148 );
334 0 : if( !pStrm->IsValid() ) return;
335 :
336 0 : DateTime aDateTime( DateTime::EMPTY );
337 0 : ReadDateTime( aDateTime );
338 0 : if( pStrm->IsValid() )
339 0 : pChangeTrack->SetFixDateTimeLocal( aDateTime );
340 : }
341 :
342 0 : void XclImpChangeTrack::ReadChTrCellContent()
343 : {
344 0 : *pStrm >> aRecHeader;
345 0 : if( CheckRecord( EXC_CHTR_OP_CELL ) )
346 : {
347 0 : ScAddress aPosition;
348 0 : SCTAB nTab = ReadTabNum();
349 0 : aPosition.SetTab( nTab );
350 : sal_uInt16 nValueType;
351 0 : *pStrm >> nValueType;
352 0 : sal_uInt16 nOldValueType = (nValueType >> 3) & EXC_CHTR_TYPE_MASK;
353 0 : sal_uInt16 nNewValueType = nValueType & EXC_CHTR_TYPE_MASK;
354 0 : pStrm->Ignore( 2 );
355 0 : Read2DAddress( aPosition );
356 : sal_uInt16 nOldSize;
357 0 : *pStrm >> nOldSize;
358 : OSL_ENSURE( (nOldSize == 0) == (nOldValueType == EXC_CHTR_TYPE_EMPTY),
359 : "XclImpChangeTrack::ReadChTrCellContent - old value mismatch" );
360 0 : pStrm->Ignore( 4 );
361 0 : switch( nValueType & EXC_CHTR_TYPE_FORMATMASK )
362 : {
363 0 : case 0x0000: break;
364 0 : case 0x1100: pStrm->Ignore( 16 ); break;
365 0 : case 0x1300: pStrm->Ignore( 8 ); break;
366 : default: OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - unknown format info" );
367 : }
368 :
369 0 : ScCellValue aOldCell;
370 0 : ScCellValue aNewCell;
371 : sal_uInt32 nOldFormat;
372 : sal_uInt32 nNewFormat;
373 0 : ReadCell(aOldCell, nOldFormat, nOldValueType, aPosition);
374 0 : ReadCell(aNewCell, nNewFormat, nNewValueType, aPosition);
375 0 : if( !pStrm->IsValid() || (pStrm->GetRecLeft() > 0) )
376 : {
377 : OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - bytes left, action ignored" );
378 0 : aOldCell.clear();
379 0 : aNewCell.clear();
380 : }
381 : else
382 : {
383 : ScChangeActionContent* pNewAction =
384 0 : pChangeTrack->AppendContentOnTheFly(aPosition, aOldCell, aNewCell, nOldFormat, nNewFormat);
385 0 : DoAcceptRejectAction( pNewAction );
386 0 : }
387 : }
388 0 : }
389 :
390 0 : void XclImpChangeTrack::ReadChTrTabId()
391 : {
392 0 : if( nTabIdCount == 0 ) // read only 1st time, otherwise calculated by <ReadChTrInsertTab()>
393 0 : nTabIdCount = static_cast< sal_uInt16 >( pStrm->GetRecLeft() >> 1 );
394 0 : }
395 :
396 0 : void XclImpChangeTrack::ReadChTrMoveRange()
397 : {
398 0 : *pStrm >> aRecHeader;
399 0 : if( CheckRecord( EXC_CHTR_OP_MOVE ) )
400 : {
401 0 : ScRange aSourceRange;
402 0 : ScRange aDestRange;
403 0 : aDestRange.aStart.SetTab( ReadTabNum() );
404 0 : aDestRange.aEnd.SetTab( aDestRange.aStart.Tab() );
405 0 : Read2DRange( aSourceRange );
406 0 : Read2DRange( aDestRange );
407 0 : aSourceRange.aStart.SetTab( ReadTabNum() );
408 0 : aSourceRange.aEnd.SetTab( aSourceRange.aStart.Tab() );
409 :
410 0 : sal_Bool bValid = pStrm->IsValid();
411 0 : if( FoundNestedMode() )
412 0 : ReadNestedRecords();
413 :
414 0 : if( bValid )
415 : {
416 0 : pChangeTrack->AppendMove( aSourceRange, aDestRange, NULL );
417 0 : DoAcceptRejectAction( pChangeTrack->GetLast() );
418 : }
419 : }
420 0 : }
421 :
422 0 : void XclImpChangeTrack::ReadChTrInsertTab()
423 : {
424 0 : *pStrm >> aRecHeader;
425 0 : if( CheckRecord( EXC_CHTR_OP_INSTAB ) )
426 : {
427 0 : SCTAB nTab = ReadTabNum();
428 0 : if( pStrm->IsValid() )
429 : {
430 0 : nTabIdCount++;
431 0 : DoInsertRange( ScRange( 0, 0, nTab, MAXCOL, MAXROW, nTab ) );
432 : }
433 : }
434 0 : }
435 :
436 0 : void XclImpChangeTrack::InitNestedMode()
437 : {
438 : OSL_ENSURE( eNestedMode == nmBase, "XclImpChangeTrack::InitNestedMode - unexpected nested mode" );
439 0 : if( eNestedMode == nmBase )
440 0 : eNestedMode = nmFound;
441 0 : }
442 :
443 0 : void XclImpChangeTrack::ReadNestedRecords()
444 : {
445 : OSL_ENSURE( eNestedMode == nmFound, "XclImpChangeTrack::StartNestedMode - missing nested mode" );
446 0 : if( eNestedMode == nmFound )
447 : {
448 0 : eNestedMode = nmNested;
449 0 : ReadRecords();
450 : }
451 0 : }
452 :
453 0 : bool XclImpChangeTrack::EndNestedMode()
454 : {
455 : OSL_ENSURE( eNestedMode != nmBase, "XclImpChangeTrack::EndNestedMode - missing nested mode" );
456 0 : bool bReturn = (eNestedMode == nmNested);
457 0 : eNestedMode = nmBase;
458 0 : return bReturn;
459 : }
460 :
461 0 : void XclImpChangeTrack::ReadRecords()
462 : {
463 0 : sal_Bool bExitLoop = false;
464 :
465 0 : while( !bExitLoop && !bGlobExit && pStrm->StartNextRecord() )
466 : {
467 0 : switch( pStrm->GetRecId() )
468 : {
469 0 : case 0x000A: bGlobExit = true; break;
470 0 : case 0x0137: ReadChTrInsert(); break;
471 0 : case 0x0138: ReadChTrInfo(); break;
472 0 : case 0x013B: ReadChTrCellContent(); break;
473 0 : case 0x013D: ReadChTrTabId(); break;
474 0 : case 0x0140: ReadChTrMoveRange(); break;
475 0 : case 0x014D: ReadChTrInsertTab(); break;
476 : case 0x014E:
477 0 : case 0x0150: InitNestedMode(); break;
478 : case 0x014F:
479 0 : case 0x0151: bExitLoop = EndNestedMode(); break;
480 : }
481 : }
482 0 : }
483 :
484 0 : void XclImpChangeTrack::Apply()
485 : {
486 0 : if( pChangeTrack )
487 : {
488 0 : pChangeTrack->SetUser( sOldUsername );
489 0 : pChangeTrack->SetUseFixDateTime( false );
490 :
491 0 : GetDoc().SetChangeTrack( pChangeTrack );
492 0 : pChangeTrack = NULL;
493 :
494 0 : ScChangeViewSettings aSettings;
495 0 : aSettings.SetShowChanges( true );
496 0 : GetDoc().SetChangeViewSettings( aSettings );
497 : }
498 0 : }
499 :
500 :
501 : // class XclImpChTrFmlConverter
502 :
503 0 : XclImpChTrFmlConverter::~XclImpChTrFmlConverter()
504 : {
505 0 : }
506 :
507 : // virtual, called from ExcToSc8::Convert()
508 0 : bool XclImpChTrFmlConverter::Read3DTabReference( sal_uInt16 /*nIxti*/, SCTAB& rFirstTab, SCTAB& rLastTab,
509 : ExternalTabInfo& rExtInfo )
510 : {
511 0 : return rChangeTrack.Read3DTabRefInfo( rFirstTab, rLastTab, rExtInfo );
512 0 : }
513 :
514 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|