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 <rtl/random.h>
21 #include <sfx2/docfile.hxx>
22 #include <sfx2/request.hxx>
23 #include <sfx2/frame.hxx>
24 #include <sfx2/sfxsids.hrc>
25 #include <unotools/saveopt.hxx>
26 #include <svl/itemset.hxx>
27 #include <svl/stritem.hxx>
28 #include <svl/intitem.hxx>
29 #include <svl/eitem.hxx>
30 #include "xecontent.hxx"
31 #include "xltracer.hxx"
32 #include "xeescher.hxx"
33 #include "xeformula.hxx"
34 #include "xehelper.hxx"
37 #include "xepivot.hxx"
38 #include "xestyle.hxx"
40 #include <xepivotxml.hxx>
41 #include "xedbdata.hxx"
43 #include "excrecds.hxx"
44 #include "tabprotection.hxx"
45 #include "document.hxx"
46 #include "scextopt.hxx"
48 #include "formulabase.hxx"
49 #include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
51 using namespace ::com::sun::star
;
53 // Global data ================================================================
55 XclExpRootData::XclExpRootData( XclBiff eBiff
, SfxMedium
& rMedium
,
56 tools::SvRef
<SotStorage
> xRootStrg
, ScDocument
& rDoc
, rtl_TextEncoding eTextEnc
) :
57 XclRootData( eBiff
, rMedium
, xRootStrg
, rDoc
, eTextEnc
, true )
59 SvtSaveOptions aSaveOpt
;
60 mbRelUrl
= mrMedium
.IsRemote() ? aSaveOpt
.IsSaveRelINet() : aSaveOpt
.IsSaveRelFSys();
61 maStringBuf
= OStringBuffer();
64 XclExpRootData::~XclExpRootData()
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 XclExpXmlPivotTableManager
& XclExpRoot::GetXmlPivotTableManager()
172 assert(mrExpData
.mxXmlPTableMgr
);
173 return *mrExpData
.mxXmlPTableMgr
;
176 XclExpTablesManager
& XclExpRoot::GetTablesManager()
178 assert(mrExpData
.mxTablesMgr
);
179 return *mrExpData
.mxTablesMgr
;
182 void XclExpRoot::InitializeConvert()
184 mrExpData
.mxTabInfo
.reset( new XclExpTabInfo( GetRoot() ) );
185 mrExpData
.mxAddrConv
.reset( new XclExpAddressConverter( GetRoot() ) );
186 mrExpData
.mxFmlaComp
.reset( new XclExpFormulaCompiler( GetRoot() ) );
187 mrExpData
.mxProgress
.reset( new XclExpProgressBar( GetRoot() ) );
189 GetProgressBar().Initialize();
192 void XclExpRoot::InitializeGlobals()
194 SetCurrScTab( SCTAB_GLOBAL
);
196 if( GetBiff() >= EXC_BIFF5
)
198 mrExpData
.mxPalette
.reset( new XclExpPalette( GetRoot() ) );
199 mrExpData
.mxFontBfr
.reset( new XclExpFontBuffer( GetRoot() ) );
200 mrExpData
.mxNumFmtBfr
.reset( new XclExpNumFmtBuffer( GetRoot() ) );
201 mrExpData
.mxXFBfr
.reset( new XclExpXFBuffer( GetRoot() ) );
202 mrExpData
.mxGlobLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
203 mrExpData
.mxNameMgr
.reset( new XclExpNameManager( GetRoot() ) );
206 if( GetBiff() == EXC_BIFF8
)
208 mrExpData
.mxSst
.reset( new XclExpSst
);
209 mrExpData
.mxObjMgr
.reset( new XclExpObjectManager( GetRoot() ) );
210 mrExpData
.mxFilterMgr
.reset( new XclExpFilterManager( GetRoot() ) );
211 mrExpData
.mxPTableMgr
.reset( new XclExpPivotTableManager( GetRoot() ) );
212 // BIFF8: only one link manager for all sheets
213 mrExpData
.mxLocLinkMgr
= mrExpData
.mxGlobLinkMgr
;
214 mrExpData
.mxDxfs
.reset( new XclExpDxfs( GetRoot() ) );
217 if( GetOutput() == EXC_OUTPUT_XML_2007
)
219 mrExpData
.mxXmlPTableMgr
.reset(new XclExpXmlPivotTableManager(GetRoot()));
220 mrExpData
.mxTablesMgr
.reset(new XclExpTablesManager(GetRoot()));
224 ScDocument
& rDoc
= GetDoc();
225 // Pass the model factory to OpCodeProvider, not the process
226 // service factory, otherwise a FormulaOpCodeMapperObj would be
227 // instantiated instead of a ScFormulaOpCodeMapperObj and the
228 // ScCompiler virtuals not be called! Which would be the case with
229 // the current (2013-01-24) rDoc.GetServiceManager()
230 const SfxObjectShell
* pShell
= rDoc
.GetDocumentShell();
233 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
236 uno::Reference
< lang::XComponent
> xComponent( pShell
->GetModel(), uno::UNO_QUERY
);
237 if (!xComponent
.is())
239 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
242 uno::Reference
< lang::XMultiServiceFactory
> xModelFactory( xComponent
, uno::UNO_QUERY
);
243 // OOXML is also BIFF8 function-wise
244 oox::xls::OpCodeProvider
aOpCodeProvider( xModelFactory
,
245 oox::xls::FILTER_OOXML
, oox::xls::BIFF8
, false);
246 // Compiler mocks about non-matching ctor or conversion from
247 // Sequence<...> to Sequence<const ...> if directly created or passed,
248 // conversion through Any works around.
249 uno::Any
aAny( aOpCodeProvider
.getOoxParserMap());
250 uno::Sequence
< const sheet::FormulaOpCodeMapEntry
> aOpCodeMapping
;
251 if (!(aAny
>>= aOpCodeMapping
))
253 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
256 ScCompiler
aCompiler( &rDoc
, ScAddress());
257 aCompiler
.SetGrammar( rDoc
.GetGrammar());
258 mrExpData
.mxOpCodeMap
= formula::FormulaCompiler::CreateOpCodeMap( aOpCodeMapping
, true);
262 GetXFBuffer().Initialize();
263 GetNameManager().Initialize();
266 void XclExpRoot::InitializeTable( SCTAB nScTab
)
268 SetCurrScTab( nScTab
);
269 if( GetBiff() == EXC_BIFF5
)
271 // local link manager per sheet
272 mrExpData
.mxLocLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
276 void XclExpRoot::InitializeSave()
278 GetPalette().Finalize();
279 GetXFBuffer().Finalize();
282 XclExpRecordRef
XclExpRoot::CreateRecord( sal_uInt16 nRecId
) const
284 XclExpRecordRef xRec
;
287 case EXC_ID_PALETTE
: xRec
= mrExpData
.mxPalette
; break;
288 case EXC_ID_FONTLIST
: xRec
= mrExpData
.mxFontBfr
; break;
289 case EXC_ID_FORMATLIST
: xRec
= mrExpData
.mxNumFmtBfr
; break;
290 case EXC_ID_XFLIST
: xRec
= mrExpData
.mxXFBfr
; break;
291 case EXC_ID_SST
: xRec
= mrExpData
.mxSst
; break;
292 case EXC_ID_EXTERNSHEET
: xRec
= GetLocalLinkMgrRef(); break;
293 case EXC_ID_NAME
: xRec
= mrExpData
.mxNameMgr
; break;
294 case EXC_ID_DXFS
: xRec
= mrExpData
.mxDxfs
; break;
296 OSL_ENSURE( xRec
, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
300 bool XclExpRoot::IsDocumentEncrypted() const
302 // We need to encrypt the content when the document structure is protected.
303 const ScDocProtection
* pDocProt
= GetDoc().GetDocProtection();
304 if (pDocProt
&& pDocProt
->isProtected() && pDocProt
->isOptionEnabled(ScDocProtection::STRUCTURE
))
307 if ( GetEncryptionData().getLength() > 0 )
308 // Password is entered directly into the save dialog.
314 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateEncryptionData( const OUString
& aPass
)
316 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
318 if ( !aPass
.isEmpty() && aPass
.getLength() < 16 )
321 osl_getSystemTime( &aTime
);
322 rtlRandomPool aRandomPool
= rtl_random_createPool ();
323 rtl_random_addBytes ( aRandomPool
, &aTime
, 8 );
325 sal_uInt8 pnDocId
[16];
326 rtl_random_getBytes( aRandomPool
, pnDocId
, 16 );
328 rtl_random_destroyPool( aRandomPool
);
330 sal_uInt16 pnPasswd
[16];
331 memset( pnPasswd
, 0, sizeof( pnPasswd
) );
332 for( sal_Int32 nChar
= 0; nChar
< aPass
.getLength(); ++nChar
)
333 pnPasswd
[nChar
] = aPass
[nChar
];
335 ::msfilter::MSCodec_Std97 aCodec
;
336 aCodec
.InitKey( pnPasswd
, pnDocId
);
337 aEncryptionData
= aCodec
.GetEncryptionData();
340 return aEncryptionData
;
343 uno::Sequence
< beans::NamedValue
> XclExpRoot::GetEncryptionData() const
345 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
346 const SfxUnoAnyItem
* pEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(GetMedium().GetItemSet(), SID_ENCRYPTIONDATA
, false);
347 if ( pEncryptionDataItem
)
348 pEncryptionDataItem
->GetValue() >>= aEncryptionData
;
351 // try to get the encryption data from the password
352 const SfxStringItem
* pPasswordItem
= SfxItemSet::GetItem
<SfxStringItem
>(GetMedium().GetItemSet(), SID_PASSWORD
, false);
353 if ( pPasswordItem
&& !pPasswordItem
->GetValue().isEmpty() )
354 aEncryptionData
= GenerateEncryptionData( pPasswordItem
->GetValue() );
357 return aEncryptionData
;
360 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateDefaultEncryptionData() const
362 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
363 if ( !GetDefaultPassword().isEmpty() )
364 aEncryptionData
= GenerateEncryptionData( GetDefaultPassword() );
366 return aEncryptionData
;
369 XclExpRootData::XclExpLinkMgrRef
XclExpRoot::GetLocalLinkMgrRef() const
371 return IsInGlobals() ? mrExpData
.mxGlobLinkMgr
: mrExpData
.mxLocLinkMgr
;
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */