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