1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/numformat.hxx>
23 #include <svl/zforlist.hxx>
24 #include <svl/sharedstringpool.hxx>
25 #include <sal/log.hxx>
26 #include <chgviset.hxx>
27 #include <formulacell.hxx>
28 #include <chgtrack.hxx>
29 #include <xihelper.hxx>
31 #include <externalrefmgr.hxx>
32 #include <document.hxx>
33 #include <documentimport.hxx>
34 #include <excdefs.hxx>
37 XclImpChangeTrack::XclImpChangeTrack( const XclImpRoot
& rRoot
, const XclImpStream
& rBookStrm
) :
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 rtl::Reference
<SotStorageStream
> xUserStrm
= OpenStream( EXC_STREAM_USERNAMES
);
51 xInStrm
= OpenStream( EXC_STREAM_REVLOG
);
55 sal_uInt64
const nStreamLen
= xInStrm
->TellEnd();
56 if( (xInStrm
->GetErrorCode() == ERRCODE_NONE
) && (nStreamLen
!= STREAM_SEEK_TO_END
) )
58 xInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
59 pStrm
.reset( new XclImpStream( *xInStrm
, GetRoot() ) );
60 pStrm
->CopyDecrypterFrom( rBookStrm
);
61 pChangeTrack
.reset(new ScChangeTrack( GetDoc() ));
63 sOldUsername
= pChangeTrack
->GetUser();
64 pChangeTrack
->SetUseFixDateTime( true );
70 XclImpChangeTrack::~XclImpChangeTrack()
76 void XclImpChangeTrack::DoAcceptRejectAction( ScChangeAction
* pAction
)
78 if( !pAction
) return;
79 switch( aRecHeader
.nAccept
)
82 pChangeTrack
->Accept( pAction
);
89 void XclImpChangeTrack::DoAcceptRejectAction( sal_uInt32 nFirst
, sal_uInt32 nLast
)
91 for( sal_uInt32 nIndex
= nFirst
; nIndex
<= nLast
; nIndex
++ )
92 DoAcceptRejectAction( pChangeTrack
->GetAction( nIndex
) );
95 void XclImpChangeTrack::DoInsertRange( const ScRange
& rRange
, bool bEndOfList
)
97 sal_uInt32 nFirst
= pChangeTrack
->GetActionMax() + 1;
98 pChangeTrack
->AppendInsert(rRange
, bEndOfList
);
99 sal_uInt32 nLast
= pChangeTrack
->GetActionMax();
100 DoAcceptRejectAction( nFirst
, nLast
);
103 void XclImpChangeTrack::DoDeleteRange( const ScRange
& rRange
)
105 sal_uLong nFirst
, nLast
;
106 pChangeTrack
->AppendDeleteRange( rRange
, &GetDoc() , nFirst
, nLast
);
107 DoAcceptRejectAction( nFirst
, nLast
);
110 SCTAB
XclImpChangeTrack::ReadTabNum()
112 return static_cast<SCTAB
>(GetTabInfo().GetCurrentIndex(
113 pStrm
->ReaduInt16(), nTabIdCount
));
116 void XclImpChangeTrack::ReadDateTime( DateTime
& rDateTime
)
119 sal_uInt8 nMonth
, nDay
, nHour
, nMin
, nSec
;
121 nYear
= pStrm
->ReaduInt16();
122 nMonth
= pStrm
->ReaduInt8();
123 nDay
= pStrm
->ReaduInt8();
124 nHour
= pStrm
->ReaduInt8();
125 nMin
= pStrm
->ReaduInt8();
126 nSec
= pStrm
->ReaduInt8();
128 rDateTime
.SetYear( nYear
);
129 rDateTime
.SetMonth( nMonth
);
130 rDateTime
.SetDay( nDay
);
131 rDateTime
.SetHour( nHour
);
132 rDateTime
.SetMin( nMin
);
133 rDateTime
.SetSec( nSec
);
134 rDateTime
.SetNanoSec( 0 );
137 bool XclImpChangeTrack::CheckRecord( sal_uInt16 nOpCode
)
139 if( (nOpCode
!= EXC_CHTR_OP_UNKNOWN
) && (aRecHeader
.nOpCode
!= nOpCode
) )
141 OSL_FAIL( "XclImpChangeTrack::CheckRecord - unknown action" );
144 return aRecHeader
.nIndex
!= 0;
147 void XclImpChangeTrack::Read3DTabRefInfo( SCTAB
& rFirstTab
, SCTAB
& rLastTab
, ExcelToSc8::ExternalTabInfo
& rExtInfo
)
149 if( LookAtuInt8() == 0x01 )
151 rExtInfo
.mbExternal
= false;
152 // internal ref - read tab num and return sc tab num (position in TABID list)
154 rFirstTab
= static_cast< SCTAB
>( GetTabInfo().GetCurrentIndex( pStrm
->ReaduInt16(), nTabIdCount
) );
155 sal_uInt8 nFillByte
= pStrm
->ReaduInt8();
156 rLastTab
= (nFillByte
== 0x00) ?
157 static_cast< SCTAB
>( GetTabInfo().GetCurrentIndex( pStrm
->ReaduInt16(), nTabIdCount
) ) : rFirstTab
;
161 // external ref - read doc and tab name and find sc tab num
163 OUString
aEncUrl( pStrm
->ReadUniString() );
166 XclImpUrlHelper::DecodeUrl( aUrl
, bSelf
, GetRoot(), aEncUrl
);
168 // - sheet name, always separated from URL
169 rExtInfo
.maTabName
= pStrm
->ReadUniString();
172 rExtInfo
.mbExternal
= true;
173 ScExternalRefManager
* pRefMgr
= GetDoc().GetExternalRefManager();
174 pRefMgr
->convertToAbsName(aUrl
);
175 rExtInfo
.mnFileId
= pRefMgr
->getExternalFileId(aUrl
);
176 rFirstTab
= rLastTab
= 0;
180 void XclImpChangeTrack::ReadFormula( std::unique_ptr
<ScTokenArray
>& rpTokenArray
, const ScAddress
& rPosition
)
182 sal_uInt16 nFmlSize
= pStrm
->ReaduInt16();
184 // create a memory stream and copy the formula to be able to read simultaneously
185 // the formula and the additional 3D tab ref data following the formula
186 // here we have to simulate an Excel record to be able to use an XclImpStream...
187 // 2do: remove the stream member from formula converter and add it as a parameter
188 // to the Convert() routine (to prevent the construction/destruction of the
189 // converter in each formula)
190 SvMemoryStream aMemStrm
;
191 aMemStrm
.WriteUInt16( 0x0001 ).WriteUInt16( nFmlSize
);
192 size_t nRead
= pStrm
->CopyToStream( aMemStrm
, nFmlSize
);
194 // survive reading invalid streams!
195 // if we can't read as many bytes as required just don't use them and
196 // assume that this part is broken
197 if(nRead
!= nFmlSize
)
199 rpTokenArray
= nullptr;
204 XclImpStream
aFmlaStrm( aMemStrm
, GetRoot() );
205 aFmlaStrm
.StartNextRecord();
206 XclImpChTrFmlConverter
aFmlConv( GetRoot(), *this );
208 // read the formula, 3D tab refs from extended data
209 std::unique_ptr
<ScTokenArray
> pArray
;
210 aFmlConv
.Reset( rPosition
);
211 bool bOK
= (aFmlConv
.Convert( pArray
, aFmlaStrm
, nFmlSize
, false ) == ConvErr::OK
); // JEG : Check This
212 rpTokenArray
= (bOK
&& pArray
) ? std::move( pArray
) : nullptr;
216 void XclImpChangeTrack::ReadCell(
217 ScCellValue
& rCell
, sal_uInt32
& rFormat
, sal_uInt16 nFlags
, const ScAddress
& rPosition
)
221 switch( nFlags
& EXC_CHTR_TYPE_MASK
)
223 case EXC_CHTR_TYPE_EMPTY
:
225 case EXC_CHTR_TYPE_RK
:
227 double fValue
= XclTools::GetDoubleFromRK( pStrm
->ReadInt32() );
228 if( pStrm
->IsValid() )
234 case EXC_CHTR_TYPE_DOUBLE
:
236 double fValue
= pStrm
->ReadDouble();
237 if( pStrm
->IsValid() )
243 case EXC_CHTR_TYPE_STRING
:
245 OUString sString
= pStrm
->ReadUniString();
246 if( pStrm
->IsValid() )
248 rCell
.set(GetDoc().GetSharedStringPool().intern(sString
));
252 case EXC_CHTR_TYPE_BOOL
:
254 double fValue
= static_cast<double>(pStrm
->ReaduInt16() != 0);
255 if( pStrm
->IsValid() )
258 rFormat
= GetFormatter().GetStandardFormat( SvNumFormatType::LOGICAL
, ScGlobal::eLnge
);
262 case EXC_CHTR_TYPE_FORMULA
:
264 std::unique_ptr
<ScTokenArray
> pTokenArray
;
265 ReadFormula( pTokenArray
, rPosition
);
266 if( pStrm
->IsValid() && pTokenArray
)
268 rCell
.set(new ScFormulaCell(GetDoc(), rPosition
, std::move(pTokenArray
)));
273 OSL_FAIL( "XclImpChangeTrack::ReadCell - unknown data type" );
277 void XclImpChangeTrack::ReadChTrInsert()
279 *pStrm
>> aRecHeader
;
280 if( !CheckRecord( EXC_CHTR_OP_UNKNOWN
) )
283 if( (aRecHeader
.nOpCode
!= EXC_CHTR_OP_INSROW
) &&
284 (aRecHeader
.nOpCode
!= EXC_CHTR_OP_INSCOL
) &&
285 (aRecHeader
.nOpCode
!= EXC_CHTR_OP_DELROW
) &&
286 (aRecHeader
.nOpCode
!= EXC_CHTR_OP_DELCOL
) )
288 OSL_FAIL( "XclImpChangeTrack::ReadChTrInsert - unknown action" );
293 aRange
.aStart
.SetTab( ReadTabNum() );
294 aRange
.aEnd
.SetTab( aRange
.aStart
.Tab() );
295 sal_uInt16 nFlags
= pStrm
->ReaduInt16();
296 bool bEndOfList
= (nFlags
& 0x0001); // row auto-inserted at the bottom.
297 Read2DRange( aRange
);
299 if( aRecHeader
.nOpCode
& EXC_CHTR_OP_COLFLAG
)
300 aRange
.aEnd
.SetRow( GetDocImport().getDoc().MaxRow() );
302 aRange
.aEnd
.SetCol( GetDocImport().getDoc().MaxCol() );
304 bool bValid
= pStrm
->IsValid();
305 if( FoundNestedMode() )
310 if( aRecHeader
.nOpCode
& EXC_CHTR_OP_DELFLAG
)
311 DoDeleteRange( aRange
);
313 DoInsertRange(aRange
, bEndOfList
);
317 void XclImpChangeTrack::ReadChTrInfo()
319 pStrm
->DisableDecryption();
321 OUString
sUsername( pStrm
->ReadUniString() );
322 if( !pStrm
->IsValid() ) return;
324 if( !sUsername
.isEmpty() )
325 pChangeTrack
->SetUser( sUsername
);
327 if( !pStrm
->IsValid() ) return;
329 DateTime
aDateTime( DateTime::EMPTY
);
330 ReadDateTime( aDateTime
);
331 if( pStrm
->IsValid() )
332 pChangeTrack
->SetFixDateTimeLocal( aDateTime
);
335 void XclImpChangeTrack::ReadChTrCellContent()
337 *pStrm
>> aRecHeader
;
338 if( !CheckRecord( EXC_CHTR_OP_CELL
) )
342 SCTAB nTab
= ReadTabNum();
343 aPosition
.SetTab( nTab
);
344 sal_uInt16 nValueType
;
345 nValueType
= pStrm
->ReaduInt16();
346 sal_uInt16 nOldValueType
= (nValueType
>> 3) & EXC_CHTR_TYPE_MASK
;
347 sal_uInt16 nNewValueType
= nValueType
& EXC_CHTR_TYPE_MASK
;
349 Read2DAddress( aPosition
);
351 nOldSize
= pStrm
->ReaduInt16();
352 SAL_WARN_IF( (nOldSize
== 0) != (nOldValueType
== EXC_CHTR_TYPE_EMPTY
),
354 "XclImpChangeTrack::ReadChTrCellContent - old value mismatch" );
356 switch( nValueType
& EXC_CHTR_TYPE_FORMATMASK
)
359 case 0x1100: pStrm
->Ignore( 16 ); break;
360 case 0x1300: pStrm
->Ignore( 8 ); break;
361 default: OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - unknown format info" );
364 ScCellValue aOldCell
;
365 ScCellValue aNewCell
;
366 sal_uInt32 nOldFormat
;
367 sal_uInt32 nNewFormat
;
368 ReadCell(aOldCell
, nOldFormat
, nOldValueType
, aPosition
);
369 ReadCell(aNewCell
, nNewFormat
, nNewValueType
, aPosition
);
370 if( !pStrm
->IsValid() || (pStrm
->GetRecLeft() > 0) )
372 OSL_FAIL( "XclImpChangeTrack::ReadChTrCellContent - bytes left, action ignored" );
378 ScChangeActionContent
* pNewAction
=
379 pChangeTrack
->AppendContentOnTheFly(aPosition
, aOldCell
, aNewCell
, nOldFormat
, nNewFormat
);
380 DoAcceptRejectAction( pNewAction
);
384 void XclImpChangeTrack::ReadChTrTabId()
386 if( nTabIdCount
== 0 ) // read only 1st time, otherwise calculated by <ReadChTrInsertTab()>
387 nTabIdCount
= static_cast< sal_uInt16
>( pStrm
->GetRecLeft() >> 1 );
390 void XclImpChangeTrack::ReadChTrMoveRange()
392 *pStrm
>> aRecHeader
;
393 if( !CheckRecord( EXC_CHTR_OP_MOVE
) )
396 ScRange aSourceRange
;
398 aDestRange
.aStart
.SetTab( ReadTabNum() );
399 aDestRange
.aEnd
.SetTab( aDestRange
.aStart
.Tab() );
400 Read2DRange( aSourceRange
);
401 Read2DRange( aDestRange
);
402 aSourceRange
.aStart
.SetTab( ReadTabNum() );
403 aSourceRange
.aEnd
.SetTab( aSourceRange
.aStart
.Tab() );
405 bool bValid
= pStrm
->IsValid();
406 if( FoundNestedMode() )
411 pChangeTrack
->AppendMove( aSourceRange
, aDestRange
, nullptr );
412 DoAcceptRejectAction( pChangeTrack
->GetLast() );
416 void XclImpChangeTrack::ReadChTrInsertTab()
418 *pStrm
>> aRecHeader
;
419 if( CheckRecord( EXC_CHTR_OP_INSTAB
) )
421 SCTAB nTab
= ReadTabNum();
422 if( pStrm
->IsValid() )
425 DoInsertRange(ScRange(0, 0, nTab
, GetDocImport().getDoc().MaxCol(), GetDocImport().getDoc().MaxRow(), nTab
), false);
430 void XclImpChangeTrack::InitNestedMode()
432 OSL_ENSURE( eNestedMode
== nmBase
, "XclImpChangeTrack::InitNestedMode - unexpected nested mode" );
433 if( eNestedMode
== nmBase
)
434 eNestedMode
= nmFound
;
437 void XclImpChangeTrack::ReadNestedRecords()
439 OSL_ENSURE( eNestedMode
== nmFound
, "XclImpChangeTrack::StartNestedMode - missing nested mode" );
440 if( eNestedMode
== nmFound
)
442 eNestedMode
= nmNested
;
447 bool XclImpChangeTrack::EndNestedMode()
449 OSL_ENSURE( eNestedMode
!= nmBase
, "XclImpChangeTrack::EndNestedMode - missing nested mode" );
450 bool bReturn
= (eNestedMode
== nmNested
);
451 eNestedMode
= nmBase
;
455 void XclImpChangeTrack::ReadRecords()
457 bool bExitLoop
= false;
459 while( !bExitLoop
&& !bGlobExit
&& pStrm
->StartNextRecord() )
461 switch( pStrm
->GetRecId() )
463 case 0x000A: bGlobExit
= true; break;
464 case 0x0137: ReadChTrInsert(); break;
465 case 0x0138: ReadChTrInfo(); break;
466 case 0x013B: ReadChTrCellContent(); break;
467 case 0x013D: ReadChTrTabId(); break;
468 case 0x0140: ReadChTrMoveRange(); break;
469 case 0x014D: ReadChTrInsertTab(); break;
471 case 0x0150: InitNestedMode(); break;
473 case 0x0151: bExitLoop
= EndNestedMode(); break;
478 void XclImpChangeTrack::Apply()
482 pChangeTrack
->SetUser( sOldUsername
);
483 pChangeTrack
->SetUseFixDateTime( false );
485 GetDoc().SetChangeTrack( std::move(pChangeTrack
) );
487 ScChangeViewSettings aSettings
;
488 aSettings
.SetShowChanges( true );
489 GetDoc().SetChangeViewSettings( aSettings
);
494 XclImpChTrFmlConverter::XclImpChTrFmlConverter(
495 XclImpRoot
& rRoot
, XclImpChangeTrack
& rXclChTr
) :
497 rChangeTrack( rXclChTr
) {}
499 XclImpChTrFmlConverter::~XclImpChTrFmlConverter()
503 // virtual, called from ExcToSc8::Convert()
504 bool XclImpChTrFmlConverter::Read3DTabReference( sal_uInt16
/*nIxti*/, SCTAB
& rFirstTab
, SCTAB
& rLastTab
,
505 ExternalTabInfo
& rExtInfo
)
507 rChangeTrack
.Read3DTabRefInfo( rFirstTab
, rLastTab
, rExtInfo
);
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */