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 .
21 #include <rtl/random.h>
22 #include <sfx2/docfile.hxx>
23 #include <sfx2/request.hxx>
24 #include <sfx2/frame.hxx>
25 #include <sfx2/sfxsids.hrc>
26 #include <unotools/saveopt.hxx>
27 #include <svl/itemset.hxx>
28 #include <svl/stritem.hxx>
29 #include <svl/intitem.hxx>
30 #include <svl/eitem.hxx>
31 #include "xecontent.hxx"
32 #include "xltracer.hxx"
33 #include "xeescher.hxx"
34 #include "xeformula.hxx"
35 #include "xehelper.hxx"
38 #include "xepivot.hxx"
39 #include "xestyle.hxx"
42 #include "excrecds.hxx" // for filter manager
43 #include "tabprotection.hxx"
44 #include "document.hxx"
45 #include "scextopt.hxx"
47 #include "formulabase.hxx"
48 #include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
50 using namespace ::com::sun::star
;
52 // Global data ================================================================
54 XclExpRootData::XclExpRootData( XclBiff eBiff
, SfxMedium
& rMedium
,
55 SotStorageRef xRootStrg
, ScDocument
& rDoc
, rtl_TextEncoding eTextEnc
) :
56 XclRootData( eBiff
, rMedium
, xRootStrg
, rDoc
, eTextEnc
, true )
58 SvtSaveOptions aSaveOpt
;
59 mbRelUrl
= mrMedium
.IsRemote() ? aSaveOpt
.IsSaveRelINet() : aSaveOpt
.IsSaveRelFSys();
62 XclExpRootData::~XclExpRootData()
66 // ----------------------------------------------------------------------------
68 XclExpRoot::XclExpRoot( XclExpRootData
& rExpRootData
) :
69 XclRoot( rExpRootData
),
70 mrExpData( rExpRootData
)
74 XclExpTabInfo
& XclExpRoot::GetTabInfo() const
76 OSL_ENSURE( mrExpData
.mxTabInfo
, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
77 return *mrExpData
.mxTabInfo
;
80 XclExpAddressConverter
& XclExpRoot::GetAddressConverter() const
82 OSL_ENSURE( mrExpData
.mxAddrConv
, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
83 return *mrExpData
.mxAddrConv
;
86 XclExpFormulaCompiler
& XclExpRoot::GetFormulaCompiler() const
88 OSL_ENSURE( mrExpData
.mxFmlaComp
, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
89 return *mrExpData
.mxFmlaComp
;
92 XclExpProgressBar
& XclExpRoot::GetProgressBar() const
94 OSL_ENSURE( mrExpData
.mxProgress
, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
95 return *mrExpData
.mxProgress
;
98 XclExpSst
& XclExpRoot::GetSst() const
100 OSL_ENSURE( mrExpData
.mxSst
, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
101 return *mrExpData
.mxSst
;
104 XclExpPalette
& XclExpRoot::GetPalette() const
106 OSL_ENSURE( mrExpData
.mxPalette
, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
107 return *mrExpData
.mxPalette
;
110 XclExpFontBuffer
& XclExpRoot::GetFontBuffer() const
112 OSL_ENSURE( mrExpData
.mxFontBfr
, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
113 return *mrExpData
.mxFontBfr
;
116 XclExpNumFmtBuffer
& XclExpRoot::GetNumFmtBuffer() const
118 OSL_ENSURE( mrExpData
.mxNumFmtBfr
, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
119 return *mrExpData
.mxNumFmtBfr
;
122 XclExpXFBuffer
& XclExpRoot::GetXFBuffer() const
124 OSL_ENSURE( mrExpData
.mxXFBfr
, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
125 return *mrExpData
.mxXFBfr
;
128 XclExpLinkManager
& XclExpRoot::GetGlobalLinkManager() const
130 OSL_ENSURE( mrExpData
.mxGlobLinkMgr
, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
131 return *mrExpData
.mxGlobLinkMgr
;
134 XclExpLinkManager
& XclExpRoot::GetLocalLinkManager() const
136 OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
137 return *GetLocalLinkMgrRef();
140 XclExpNameManager
& XclExpRoot::GetNameManager() const
142 OSL_ENSURE( mrExpData
.mxNameMgr
, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
143 return *mrExpData
.mxNameMgr
;
146 XclExpObjectManager
& XclExpRoot::GetObjectManager() const
148 OSL_ENSURE( mrExpData
.mxObjMgr
, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
149 return *mrExpData
.mxObjMgr
;
152 XclExpFilterManager
& XclExpRoot::GetFilterManager() const
154 OSL_ENSURE( mrExpData
.mxFilterMgr
, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
155 return *mrExpData
.mxFilterMgr
;
158 XclExpDxfs
& XclExpRoot::GetDxfs() const
160 OSL_ENSURE( mrExpData
.mxDxfs
, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
161 return *mrExpData
.mxDxfs
;
164 XclExpPivotTableManager
& XclExpRoot::GetPivotTableManager() const
166 OSL_ENSURE( mrExpData
.mxPTableMgr
, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
167 return *mrExpData
.mxPTableMgr
;
170 ScCompiler::OpCodeMapPtr
XclExpRoot::GetOpCodeMap() const
172 OSL_ENSURE( mrExpData
.mxOpCodeMap
, "XclExpRoot::GetOpCodeMap - missing object (wrong BIFF?)" );
173 return mrExpData
.mxOpCodeMap
;
176 void XclExpRoot::InitializeConvert()
178 mrExpData
.mxTabInfo
.reset( new XclExpTabInfo( GetRoot() ) );
179 mrExpData
.mxAddrConv
.reset( new XclExpAddressConverter( GetRoot() ) );
180 mrExpData
.mxFmlaComp
.reset( new XclExpFormulaCompiler( GetRoot() ) );
181 mrExpData
.mxProgress
.reset( new XclExpProgressBar( GetRoot() ) );
183 GetProgressBar().Initialize();
186 void XclExpRoot::InitializeGlobals()
188 SetCurrScTab( SCTAB_GLOBAL
);
190 if( GetBiff() >= EXC_BIFF5
)
192 mrExpData
.mxPalette
.reset( new XclExpPalette( GetRoot() ) );
193 mrExpData
.mxFontBfr
.reset( new XclExpFontBuffer( GetRoot() ) );
194 mrExpData
.mxNumFmtBfr
.reset( new XclExpNumFmtBuffer( GetRoot() ) );
195 mrExpData
.mxXFBfr
.reset( new XclExpXFBuffer( GetRoot() ) );
196 mrExpData
.mxGlobLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
197 mrExpData
.mxNameMgr
.reset( new XclExpNameManager( GetRoot() ) );
200 if( GetBiff() == EXC_BIFF8
)
202 mrExpData
.mxSst
.reset( new XclExpSst
);
203 mrExpData
.mxObjMgr
.reset( new XclExpObjectManager( GetRoot() ) );
204 mrExpData
.mxFilterMgr
.reset( new XclExpFilterManager( GetRoot() ) );
205 mrExpData
.mxPTableMgr
.reset( new XclExpPivotTableManager( GetRoot() ) );
206 // BIFF8: only one link manager for all sheets
207 mrExpData
.mxLocLinkMgr
= mrExpData
.mxGlobLinkMgr
;
208 mrExpData
.mxDxfs
.reset( new XclExpDxfs( GetRoot() ) );
211 if( GetOutput() == EXC_OUTPUT_XML_2007
)
215 ScDocument
& rDoc
= GetDoc();
216 // Pass the model factory to OpCodeProvider, not the process
217 // service factory, otherwise a FormulaOpCodeMapperObj would be
218 // instanciated intead of a ScFormulaOpCodeMapperObj and the
219 // ScCompiler virtuals not be called! Which would be the case with
220 // the current (2013-01-24) rDoc.GetServiceManager()
221 const SfxObjectShell
* pShell
= rDoc
.GetDocumentShell();
224 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
227 uno::Reference
< lang::XComponent
> xComponent( pShell
->GetModel(), uno::UNO_QUERY
);
228 if (!xComponent
.is())
230 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
233 uno::Reference
< lang::XMultiServiceFactory
> xModelFactory( xComponent
, uno::UNO_QUERY
);
234 // OOXML is also BIFF8 function-wise
235 oox::xls::OpCodeProvider
aOpCodeProvider( xModelFactory
,
236 oox::xls::FILTER_OOXML
, oox::xls::BIFF8
, false, true);
237 // Compiler mocks about non-matching ctor or conversion from
238 // Sequence<...> to Sequence<const ...> if directly created or passed,
239 // conversion through Any works around.
240 uno::Any
aAny( aOpCodeProvider
.getOoxParserMap());
241 uno::Sequence
< const sheet::FormulaOpCodeMapEntry
> aOpCodeMapping
;
242 if (!(aAny
>>= aOpCodeMapping
))
244 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
247 ScCompiler
aCompiler( &rDoc
, ScAddress());
248 aCompiler
.SetGrammar( rDoc
.GetGrammar());
249 mrExpData
.mxOpCodeMap
= aCompiler
.CreateOpCodeMap( aOpCodeMapping
, true);
253 GetXFBuffer().Initialize();
254 GetNameManager().Initialize();
257 void XclExpRoot::InitializeTable( SCTAB nScTab
)
259 SetCurrScTab( nScTab
);
260 if( GetBiff() == EXC_BIFF5
)
262 // local link manager per sheet
263 mrExpData
.mxLocLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
267 void XclExpRoot::InitializeSave()
269 GetPalette().Finalize();
270 GetXFBuffer().Finalize();
273 XclExpRecordRef
XclExpRoot::CreateRecord( sal_uInt16 nRecId
) const
275 XclExpRecordRef xRec
;
278 case EXC_ID_PALETTE
: xRec
= mrExpData
.mxPalette
; break;
279 case EXC_ID_FONTLIST
: xRec
= mrExpData
.mxFontBfr
; break;
280 case EXC_ID_FORMATLIST
: xRec
= mrExpData
.mxNumFmtBfr
; break;
281 case EXC_ID_XFLIST
: xRec
= mrExpData
.mxXFBfr
; break;
282 case EXC_ID_SST
: xRec
= mrExpData
.mxSst
; break;
283 case EXC_ID_EXTERNSHEET
: xRec
= GetLocalLinkMgrRef(); break;
284 case EXC_ID_NAME
: xRec
= mrExpData
.mxNameMgr
; break;
285 case EXC_ID_DXFS
: xRec
= mrExpData
.mxDxfs
; break;
287 OSL_ENSURE( xRec
, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
291 bool XclExpRoot::IsDocumentEncrypted() const
293 // We need to encrypt the content when the document structure is protected.
294 const ScDocProtection
* pDocProt
= GetDoc().GetDocProtection();
295 if (pDocProt
&& pDocProt
->isProtected() && pDocProt
->isOptionEnabled(ScDocProtection::STRUCTURE
))
298 if ( GetEncryptionData().getLength() > 0 )
299 // Password is entered directly into the save dialog.
305 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateEncryptionData( const OUString
& aPass
) const
307 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
309 if ( !aPass
.isEmpty() && aPass
.getLength() < 16 )
312 osl_getSystemTime( &aTime
);
313 rtlRandomPool aRandomPool
= rtl_random_createPool ();
314 rtl_random_addBytes ( aRandomPool
, &aTime
, 8 );
316 sal_uInt8 pnDocId
[16];
317 rtl_random_getBytes( aRandomPool
, pnDocId
, 16 );
319 rtl_random_destroyPool( aRandomPool
);
321 sal_uInt16 pnPasswd
[16];
322 memset( pnPasswd
, 0, sizeof( pnPasswd
) );
323 for (xub_StrLen nChar
= 0; nChar
< aPass
.getLength(); ++nChar
)
324 pnPasswd
[nChar
] = aPass
.getStr()[nChar
];
326 ::msfilter::MSCodec_Std97 aCodec
;
327 aCodec
.InitKey( pnPasswd
, pnDocId
);
328 aEncryptionData
= aCodec
.GetEncryptionData();
331 return aEncryptionData
;
334 uno::Sequence
< beans::NamedValue
> XclExpRoot::GetEncryptionData() const
336 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
337 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem
, SfxUnoAnyItem
, SID_ENCRYPTIONDATA
, false );
338 if ( pEncryptionDataItem
)
339 pEncryptionDataItem
->GetValue() >>= aEncryptionData
;
342 // try to get the encryption data from the password
343 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem
, SfxStringItem
, SID_PASSWORD
, false );
344 if ( pPasswordItem
&& !pPasswordItem
->GetValue().isEmpty() )
345 aEncryptionData
= GenerateEncryptionData( pPasswordItem
->GetValue() );
348 return aEncryptionData
;
351 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateDefaultEncryptionData() const
353 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
354 if ( GetDefaultPassword().Len() > 0 )
355 aEncryptionData
= GenerateEncryptionData( GetDefaultPassword() );
357 return aEncryptionData
;
360 XclExpRootData::XclExpLinkMgrRef
XclExpRoot::GetLocalLinkMgrRef() const
362 return IsInGlobals() ? mrExpData
.mxGlobLinkMgr
: mrExpData
.mxLocLinkMgr
;
365 // ============================================================================
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */