fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / xcl97 / XclImpChangeTrack.cxx
blob417aa3f807203c8760ab0acea2e5dd239d1ab518
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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"
32 // class XclImpChangeTrack
34 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 eNestedMode( nmBase )
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 tools::SvRef<SotStorageStream> xUserStrm = OpenStream( EXC_STREAM_USERNAMES );
48 if( !xUserStrm.Is() )
49 return;
51 xInStrm = OpenStream( EXC_STREAM_REVLOG );
52 if( xInStrm.Is() )
54 xInStrm->Seek( STREAM_SEEK_TO_END );
55 sal_uInt64 const nStreamLen = xInStrm->Tell();
56 if( (xInStrm->GetErrorCode() == ERRCODE_NONE) && (nStreamLen != STREAM_SEEK_TO_END) )
58 xInStrm->Seek( STREAM_SEEK_TO_BEGIN );
59 pStrm = new XclImpStream( *xInStrm, GetRoot() );
60 pStrm->CopyDecrypterFrom( rBookStrm );
61 pChangeTrack = new ScChangeTrack( GetDocPtr() );
63 sOldUsername = pChangeTrack->GetUser();
64 pChangeTrack->SetUseFixDateTime( true );
66 ReadRecords();
71 XclImpChangeTrack::~XclImpChangeTrack()
73 delete pChangeTrack;
74 delete pStrm;
77 void XclImpChangeTrack::DoAcceptRejectAction( ScChangeAction* pAction )
79 if( !pAction ) return;
80 switch( aRecHeader.nAccept )
82 case EXC_CHTR_ACCEPT:
83 pChangeTrack->Accept( pAction );
84 break;
85 case EXC_CHTR_REJECT:
86 break;
90 void XclImpChangeTrack::DoAcceptRejectAction( sal_uInt32 nFirst, sal_uInt32 nLast )
92 for( sal_uInt32 nIndex = nFirst; nIndex <= nLast; nIndex++ )
93 DoAcceptRejectAction( pChangeTrack->GetAction( nIndex ) );
96 void XclImpChangeTrack::DoInsertRange( const ScRange& rRange, bool bEndOfList )
98 sal_uInt32 nFirst = pChangeTrack->GetActionMax() + 1;
99 pChangeTrack->AppendInsert(rRange, bEndOfList);
100 sal_uInt32 nLast = pChangeTrack->GetActionMax();
101 DoAcceptRejectAction( nFirst, nLast );
104 void XclImpChangeTrack::DoDeleteRange( const ScRange& rRange )
106 sal_uLong nFirst, nLast;
107 pChangeTrack->AppendDeleteRange( rRange, NULL, nFirst, nLast );
108 DoAcceptRejectAction( nFirst, nLast );
111 SCTAB XclImpChangeTrack::ReadTabNum()
113 return static_cast<SCTAB>(GetTabInfo().GetCurrentIndex(
114 pStrm->ReaduInt16(), nTabIdCount ));
117 void XclImpChangeTrack::ReadDateTime( DateTime& rDateTime )
119 sal_uInt16 nYear;
120 sal_uInt8 nMonth, nDay, nHour, nMin, nSec;
122 nYear = pStrm->ReaduInt16();
123 nMonth = pStrm->ReaduInt8();
124 nDay = pStrm->ReaduInt8();
125 nHour = pStrm->ReaduInt8();
126 nMin = pStrm->ReaduInt8();
127 nSec = pStrm->ReaduInt8();
129 rDateTime.SetYear( nYear );
130 rDateTime.SetMonth( nMonth );
131 rDateTime.SetDay( nDay );
132 rDateTime.SetHour( nHour );
133 rDateTime.SetMin( nMin );
134 rDateTime.SetSec( nSec );
135 rDateTime.SetNanoSec( 0 );
138 bool XclImpChangeTrack::CheckRecord( sal_uInt16 nOpCode )
140 if( (nOpCode != EXC_CHTR_OP_UNKNOWN) && (aRecHeader.nOpCode != nOpCode) )
142 OSL_FAIL( "XclImpChangeTrack::CheckRecord - unknown action" );
143 return false;
145 return aRecHeader.nIndex != 0;
148 bool XclImpChangeTrack::Read3DTabRefInfo( SCTAB& rFirstTab, SCTAB& rLastTab, ExcelToSc8::ExternalTabInfo& rExtInfo )
150 if( LookAtuInt8() == 0x01 )
152 rExtInfo.mbExternal = false;
153 // internal ref - read tab num and return sc tab num (position in TABID list)
154 pStrm->Ignore( 3 );
155 rFirstTab = static_cast< SCTAB >( GetTabInfo().GetCurrentIndex( pStrm->ReaduInt16(), nTabIdCount ) );
156 sal_uInt8 nFillByte = pStrm->ReaduInt8();
157 rLastTab = (nFillByte == 0x00) ?
158 static_cast< SCTAB >( GetTabInfo().GetCurrentIndex( pStrm->ReaduInt16(), nTabIdCount ) ) : rFirstTab;
160 else
162 // external ref - read doc and tab name and find sc tab num
163 // - URL
164 OUString aEncUrl( pStrm->ReadUniString() );
165 OUString aUrl;
166 bool bSelf;
167 XclImpUrlHelper::DecodeUrl( aUrl, bSelf, GetRoot(), aEncUrl );
168 pStrm->Ignore( 1 );
169 // - sheet name, always separated from URL
170 OUString aTabName( pStrm->ReadUniString() );
171 pStrm->Ignore( 1 );
173 rExtInfo.mbExternal = true;
174 ScExternalRefManager* pRefMgr = GetDoc().GetExternalRefManager();
175 pRefMgr->convertToAbsName(aUrl);
176 rExtInfo.mnFileId = pRefMgr->getExternalFileId(aUrl);
177 rExtInfo.maTabName = aTabName;
178 rFirstTab = rLastTab = 0;
180 return true;
183 void XclImpChangeTrack::ReadFormula( ScTokenArray*& rpTokenArray, const ScAddress& rPosition )
185 sal_uInt16 nFmlSize = pStrm->ReaduInt16();
187 // create a memory stream and copy the formula to be able to read simultaneously
188 // the formula and the additional 3D tab ref data following the formula
189 // here we have to simulate an Excel record to be able to use an XclImpStream...
190 // 2do: remove the stream member from formula converter and add it as a parameter
191 // to the Convert() routine (to prevent the construction/destruction of the
192 // converter in each formula)
193 SvMemoryStream aMemStrm;
194 aMemStrm.WriteUInt16( 0x0001 ).WriteUInt16( nFmlSize );
195 size_t nRead = pStrm->CopyToStream( aMemStrm, nFmlSize );
197 // survive reading invalid streams!
198 // if we can't read as many bytes as required just don't use them and
199 // assume that this part is broken
200 if(nRead != nFmlSize)
202 rpTokenArray = NULL;
203 pStrm->Ignore(1);
204 return;
207 XclImpStream aFmlaStrm( aMemStrm, GetRoot() );
208 aFmlaStrm.StartNextRecord();
209 XclImpChTrFmlConverter aFmlConv( GetRoot(), *this );
211 // read the formula, 3D tab refs from extended data
212 const ScTokenArray* pArray = NULL;
213 aFmlConv.Reset( rPosition );
214 bool bOK = (aFmlConv.Convert( pArray, aFmlaStrm, nFmlSize, false, FT_CellFormula) == ConvOK); // JEG : Check This
215 rpTokenArray = (bOK && pArray) ? new ScTokenArray( *pArray ) : NULL;
216 pStrm->Ignore( 1 );
219 void XclImpChangeTrack::ReadCell(
220 ScCellValue& rCell, sal_uInt32& rFormat, sal_uInt16 nFlags, const ScAddress& rPosition )
222 rCell.clear();
223 rFormat = 0;
224 switch( nFlags & EXC_CHTR_TYPE_MASK )
226 case EXC_CHTR_TYPE_EMPTY:
227 break;
228 case EXC_CHTR_TYPE_RK:
230 double fValue = ReadRK();
231 if( pStrm->IsValid() )
233 rCell.meType = CELLTYPE_VALUE;
234 rCell.mfValue = fValue;
237 break;
238 case EXC_CHTR_TYPE_DOUBLE:
240 double fValue = pStrm->ReadDouble();
241 if( pStrm->IsValid() )
243 rCell.meType = CELLTYPE_VALUE;
244 rCell.mfValue = fValue;
247 break;
248 case EXC_CHTR_TYPE_STRING:
250 OUString sString = pStrm->ReadUniString();
251 if( pStrm->IsValid() )
253 rCell.meType = CELLTYPE_STRING;
254 rCell.mpString = new svl::SharedString(GetDoc().GetSharedStringPool().intern(sString));
257 break;
258 case EXC_CHTR_TYPE_BOOL:
260 double fValue = (double) ReadBool();
261 if( pStrm->IsValid() )
263 rCell.meType = CELLTYPE_VALUE;
264 rCell.mfValue = fValue;
265 rFormat = GetFormatter().GetStandardFormat( css::util::NumberFormat::LOGICAL, ScGlobal::eLnge );
268 break;
269 case EXC_CHTR_TYPE_FORMULA:
271 ScTokenArray* pTokenArray = NULL;
272 ReadFormula( pTokenArray, rPosition );
273 if( pStrm->IsValid() && pTokenArray )
275 rCell.meType = CELLTYPE_FORMULA;
276 rCell.mpFormula = new ScFormulaCell(GetDocPtr(), rPosition, *pTokenArray);
278 delete pTokenArray;
280 break;
281 default:
282 OSL_FAIL( "XclImpChangeTrack::ReadCell - unknown data type" );
286 void XclImpChangeTrack::ReadChTrInsert()
288 *pStrm >> aRecHeader;
289 if( CheckRecord( EXC_CHTR_OP_UNKNOWN ) )
291 if( (aRecHeader.nOpCode != EXC_CHTR_OP_INSROW) &&
292 (aRecHeader.nOpCode != EXC_CHTR_OP_INSCOL) &&
293 (aRecHeader.nOpCode != EXC_CHTR_OP_DELROW) &&
294 (aRecHeader.nOpCode != EXC_CHTR_OP_DELCOL) )
296 OSL_FAIL( "XclImpChangeTrack::ReadChTrInsert - unknown action" );
297 return;
300 ScRange aRange;
301 aRange.aStart.SetTab( ReadTabNum() );
302 aRange.aEnd.SetTab( aRange.aStart.Tab() );
303 sal_uInt16 nFlags = pStrm->ReaduInt16();
304 bool bEndOfList = (nFlags & 0x0001); // row auto-inserted at the bottom.
305 Read2DRange( aRange );
307 if( aRecHeader.nOpCode & EXC_CHTR_OP_COLFLAG )
308 aRange.aEnd.SetRow( MAXROW );
309 else
310 aRange.aEnd.SetCol( MAXCOL );
312 bool bValid = pStrm->IsValid();
313 if( FoundNestedMode() )
314 ReadNestedRecords();
316 if( bValid )
318 if( aRecHeader.nOpCode & EXC_CHTR_OP_DELFLAG )
319 DoDeleteRange( aRange );
320 else
321 DoInsertRange(aRange, bEndOfList);
326 void XclImpChangeTrack::ReadChTrInfo()
328 pStrm->DisableDecryption();
329 pStrm->Ignore( 32 );
330 OUString sUsername( pStrm->ReadUniString() );
331 if( !pStrm->IsValid() ) return;
333 if( !sUsername.isEmpty() )
334 pChangeTrack->SetUser( sUsername );
335 pStrm->Seek( 148 );
336 if( !pStrm->IsValid() ) return;
338 DateTime aDateTime( DateTime::EMPTY );
339 ReadDateTime( aDateTime );
340 if( pStrm->IsValid() )
341 pChangeTrack->SetFixDateTimeLocal( aDateTime );
344 void XclImpChangeTrack::ReadChTrCellContent()
346 *pStrm >> aRecHeader;
347 if( CheckRecord( EXC_CHTR_OP_CELL ) )
349 ScAddress aPosition;
350 SCTAB nTab = ReadTabNum();
351 aPosition.SetTab( nTab );
352 sal_uInt16 nValueType;
353 nValueType = pStrm->ReaduInt16();
354 sal_uInt16 nOldValueType = (nValueType >> 3) & EXC_CHTR_TYPE_MASK;
355 sal_uInt16 nNewValueType = nValueType & EXC_CHTR_TYPE_MASK;
356 pStrm->Ignore( 2 );
357 Read2DAddress( aPosition );
358 sal_uInt16 nOldSize;
359 nOldSize = pStrm->ReaduInt16();
360 SAL_WARN_IF( (nOldSize == 0) != (nOldValueType == EXC_CHTR_TYPE_EMPTY),
361 "sc.xcl97",
362 "XclImpChangeTrack::ReadChTrCellContent - old value mismatch" );
363 pStrm->Ignore( 4 );
364 switch( nValueType & EXC_CHTR_TYPE_FORMATMASK )
366 case 0x0000: break;
367 case 0x1100: pStrm->Ignore( 16 ); break;
368 case 0x1300: pStrm->Ignore( 8 ); break;
369 default: OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - unknown format info" );
372 ScCellValue aOldCell;
373 ScCellValue aNewCell;
374 sal_uInt32 nOldFormat;
375 sal_uInt32 nNewFormat;
376 ReadCell(aOldCell, nOldFormat, nOldValueType, aPosition);
377 ReadCell(aNewCell, nNewFormat, nNewValueType, aPosition);
378 if( !pStrm->IsValid() || (pStrm->GetRecLeft() > 0) )
380 OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - bytes left, action ignored" );
381 aOldCell.clear();
382 aNewCell.clear();
384 else
386 ScChangeActionContent* pNewAction =
387 pChangeTrack->AppendContentOnTheFly(aPosition, aOldCell, aNewCell, nOldFormat, nNewFormat);
388 DoAcceptRejectAction( pNewAction );
393 void XclImpChangeTrack::ReadChTrTabId()
395 if( nTabIdCount == 0 ) // read only 1st time, otherwise calculated by <ReadChTrInsertTab()>
396 nTabIdCount = static_cast< sal_uInt16 >( pStrm->GetRecLeft() >> 1 );
399 void XclImpChangeTrack::ReadChTrMoveRange()
401 *pStrm >> aRecHeader;
402 if( CheckRecord( EXC_CHTR_OP_MOVE ) )
404 ScRange aSourceRange;
405 ScRange aDestRange;
406 aDestRange.aStart.SetTab( ReadTabNum() );
407 aDestRange.aEnd.SetTab( aDestRange.aStart.Tab() );
408 Read2DRange( aSourceRange );
409 Read2DRange( aDestRange );
410 aSourceRange.aStart.SetTab( ReadTabNum() );
411 aSourceRange.aEnd.SetTab( aSourceRange.aStart.Tab() );
413 bool bValid = pStrm->IsValid();
414 if( FoundNestedMode() )
415 ReadNestedRecords();
417 if( bValid )
419 pChangeTrack->AppendMove( aSourceRange, aDestRange, NULL );
420 DoAcceptRejectAction( pChangeTrack->GetLast() );
425 void XclImpChangeTrack::ReadChTrInsertTab()
427 *pStrm >> aRecHeader;
428 if( CheckRecord( EXC_CHTR_OP_INSTAB ) )
430 SCTAB nTab = ReadTabNum();
431 if( pStrm->IsValid() )
433 nTabIdCount++;
434 DoInsertRange(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab), false);
439 void XclImpChangeTrack::InitNestedMode()
441 OSL_ENSURE( eNestedMode == nmBase, "XclImpChangeTrack::InitNestedMode - unexpected nested mode" );
442 if( eNestedMode == nmBase )
443 eNestedMode = nmFound;
446 void XclImpChangeTrack::ReadNestedRecords()
448 OSL_ENSURE( eNestedMode == nmFound, "XclImpChangeTrack::StartNestedMode - missing nested mode" );
449 if( eNestedMode == nmFound )
451 eNestedMode = nmNested;
452 ReadRecords();
456 bool XclImpChangeTrack::EndNestedMode()
458 OSL_ENSURE( eNestedMode != nmBase, "XclImpChangeTrack::EndNestedMode - missing nested mode" );
459 bool bReturn = (eNestedMode == nmNested);
460 eNestedMode = nmBase;
461 return bReturn;
464 void XclImpChangeTrack::ReadRecords()
466 bool bExitLoop = false;
468 while( !bExitLoop && !bGlobExit && pStrm->StartNextRecord() )
470 switch( pStrm->GetRecId() )
472 case 0x000A: bGlobExit = true; break;
473 case 0x0137: ReadChTrInsert(); break;
474 case 0x0138: ReadChTrInfo(); break;
475 case 0x013B: ReadChTrCellContent(); break;
476 case 0x013D: ReadChTrTabId(); break;
477 case 0x0140: ReadChTrMoveRange(); break;
478 case 0x014D: ReadChTrInsertTab(); break;
479 case 0x014E:
480 case 0x0150: InitNestedMode(); break;
481 case 0x014F:
482 case 0x0151: bExitLoop = EndNestedMode(); break;
487 void XclImpChangeTrack::Apply()
489 if( pChangeTrack )
491 pChangeTrack->SetUser( sOldUsername );
492 pChangeTrack->SetUseFixDateTime( false );
494 GetDoc().SetChangeTrack( pChangeTrack );
495 pChangeTrack = NULL;
497 ScChangeViewSettings aSettings;
498 aSettings.SetShowChanges( true );
499 GetDoc().SetChangeViewSettings( aSettings );
503 // class XclImpChTrFmlConverter
505 XclImpChTrFmlConverter::XclImpChTrFmlConverter(
506 XclImpRoot& rRoot, XclImpChangeTrack& rXclChTr ) :
507 ExcelToSc8( rRoot ),
508 rChangeTrack( rXclChTr ) {}
510 XclImpChTrFmlConverter::~XclImpChTrFmlConverter()
514 // virtual, called from ExcToSc8::Convert()
515 bool XclImpChTrFmlConverter::Read3DTabReference( sal_uInt16 /*nIxti*/, SCTAB& rFirstTab, SCTAB& rLastTab,
516 ExternalTabInfo& rExtInfo )
518 return rChangeTrack.Read3DTabRefInfo( rFirstTab, rLastTab, rExtInfo );
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */