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>
42 #include "excrecds.hxx"
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 tools::SvRef
<SotStorage
> 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();
60 maStringBuf
= OStringBuffer();
63 XclExpRootData::~XclExpRootData()
67 XclExpRoot::XclExpRoot( XclExpRootData
& rExpRootData
) :
68 XclRoot( rExpRootData
),
69 mrExpData( rExpRootData
)
73 XclExpTabInfo
& XclExpRoot::GetTabInfo() const
75 OSL_ENSURE( mrExpData
.mxTabInfo
, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
76 return *mrExpData
.mxTabInfo
;
79 XclExpAddressConverter
& XclExpRoot::GetAddressConverter() const
81 OSL_ENSURE( mrExpData
.mxAddrConv
, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
82 return *mrExpData
.mxAddrConv
;
85 XclExpFormulaCompiler
& XclExpRoot::GetFormulaCompiler() const
87 OSL_ENSURE( mrExpData
.mxFmlaComp
, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
88 return *mrExpData
.mxFmlaComp
;
91 XclExpProgressBar
& XclExpRoot::GetProgressBar() const
93 OSL_ENSURE( mrExpData
.mxProgress
, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
94 return *mrExpData
.mxProgress
;
97 XclExpSst
& XclExpRoot::GetSst() const
99 OSL_ENSURE( mrExpData
.mxSst
, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
100 return *mrExpData
.mxSst
;
103 XclExpPalette
& XclExpRoot::GetPalette() const
105 OSL_ENSURE( mrExpData
.mxPalette
, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
106 return *mrExpData
.mxPalette
;
109 XclExpFontBuffer
& XclExpRoot::GetFontBuffer() const
111 OSL_ENSURE( mrExpData
.mxFontBfr
, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
112 return *mrExpData
.mxFontBfr
;
115 XclExpNumFmtBuffer
& XclExpRoot::GetNumFmtBuffer() const
117 OSL_ENSURE( mrExpData
.mxNumFmtBfr
, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
118 return *mrExpData
.mxNumFmtBfr
;
121 XclExpXFBuffer
& XclExpRoot::GetXFBuffer() const
123 OSL_ENSURE( mrExpData
.mxXFBfr
, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
124 return *mrExpData
.mxXFBfr
;
127 XclExpLinkManager
& XclExpRoot::GetGlobalLinkManager() const
129 OSL_ENSURE( mrExpData
.mxGlobLinkMgr
, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
130 return *mrExpData
.mxGlobLinkMgr
;
133 XclExpLinkManager
& XclExpRoot::GetLocalLinkManager() const
135 OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
136 return *GetLocalLinkMgrRef();
139 XclExpNameManager
& XclExpRoot::GetNameManager() const
141 OSL_ENSURE( mrExpData
.mxNameMgr
, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
142 return *mrExpData
.mxNameMgr
;
145 XclExpObjectManager
& XclExpRoot::GetObjectManager() const
147 OSL_ENSURE( mrExpData
.mxObjMgr
, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
148 return *mrExpData
.mxObjMgr
;
151 XclExpFilterManager
& XclExpRoot::GetFilterManager() const
153 OSL_ENSURE( mrExpData
.mxFilterMgr
, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
154 return *mrExpData
.mxFilterMgr
;
157 XclExpDxfs
& XclExpRoot::GetDxfs() const
159 OSL_ENSURE( mrExpData
.mxDxfs
, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
160 return *mrExpData
.mxDxfs
;
163 XclExpPivotTableManager
& XclExpRoot::GetPivotTableManager() const
165 OSL_ENSURE( mrExpData
.mxPTableMgr
, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
166 return *mrExpData
.mxPTableMgr
;
169 XclExpXmlPivotTableManager
& XclExpRoot::GetXmlPivotTableManager()
171 assert(mrExpData
.mxXmlPTableMgr
);
172 return *mrExpData
.mxXmlPTableMgr
;
175 void XclExpRoot::InitializeConvert()
177 mrExpData
.mxTabInfo
.reset( new XclExpTabInfo( GetRoot() ) );
178 mrExpData
.mxAddrConv
.reset( new XclExpAddressConverter( GetRoot() ) );
179 mrExpData
.mxFmlaComp
.reset( new XclExpFormulaCompiler( GetRoot() ) );
180 mrExpData
.mxProgress
.reset( new XclExpProgressBar( GetRoot() ) );
182 GetProgressBar().Initialize();
185 void XclExpRoot::InitializeGlobals()
187 SetCurrScTab( SCTAB_GLOBAL
);
189 if( GetBiff() >= EXC_BIFF5
)
191 mrExpData
.mxPalette
.reset( new XclExpPalette( GetRoot() ) );
192 mrExpData
.mxFontBfr
.reset( new XclExpFontBuffer( GetRoot() ) );
193 mrExpData
.mxNumFmtBfr
.reset( new XclExpNumFmtBuffer( GetRoot() ) );
194 mrExpData
.mxXFBfr
.reset( new XclExpXFBuffer( GetRoot() ) );
195 mrExpData
.mxGlobLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
196 mrExpData
.mxNameMgr
.reset( new XclExpNameManager( GetRoot() ) );
199 if( GetBiff() == EXC_BIFF8
)
201 mrExpData
.mxSst
.reset( new XclExpSst
);
202 mrExpData
.mxObjMgr
.reset( new XclExpObjectManager( GetRoot() ) );
203 mrExpData
.mxFilterMgr
.reset( new XclExpFilterManager( GetRoot() ) );
204 mrExpData
.mxPTableMgr
.reset( new XclExpPivotTableManager( GetRoot() ) );
205 // BIFF8: only one link manager for all sheets
206 mrExpData
.mxLocLinkMgr
= mrExpData
.mxGlobLinkMgr
;
207 mrExpData
.mxDxfs
.reset( new XclExpDxfs( GetRoot() ) );
210 if( GetOutput() == EXC_OUTPUT_XML_2007
)
212 mrExpData
.mxXmlPTableMgr
.reset(new XclExpXmlPivotTableManager(GetRoot()));
216 ScDocument
& rDoc
= GetDoc();
217 // Pass the model factory to OpCodeProvider, not the process
218 // service factory, otherwise a FormulaOpCodeMapperObj would be
219 // instantiated instead of a ScFormulaOpCodeMapperObj and the
220 // ScCompiler virtuals not be called! Which would be the case with
221 // the current (2013-01-24) rDoc.GetServiceManager()
222 const SfxObjectShell
* pShell
= rDoc
.GetDocumentShell();
225 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
228 uno::Reference
< lang::XComponent
> xComponent( pShell
->GetModel(), uno::UNO_QUERY
);
229 if (!xComponent
.is())
231 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
234 uno::Reference
< lang::XMultiServiceFactory
> xModelFactory( xComponent
, uno::UNO_QUERY
);
235 // OOXML is also BIFF8 function-wise
236 oox::xls::OpCodeProvider
aOpCodeProvider( xModelFactory
,
237 oox::xls::FILTER_OOXML
, oox::xls::BIFF8
, false, true);
238 // Compiler mocks about non-matching ctor or conversion from
239 // Sequence<...> to Sequence<const ...> if directly created or passed,
240 // conversion through Any works around.
241 uno::Any
aAny( aOpCodeProvider
.getOoxParserMap());
242 uno::Sequence
< const sheet::FormulaOpCodeMapEntry
> aOpCodeMapping
;
243 if (!(aAny
>>= aOpCodeMapping
))
245 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
248 ScCompiler
aCompiler( &rDoc
, ScAddress());
249 aCompiler
.SetGrammar( rDoc
.GetGrammar());
250 mrExpData
.mxOpCodeMap
= formula::FormulaCompiler::CreateOpCodeMap( aOpCodeMapping
, true);
254 GetXFBuffer().Initialize();
255 GetNameManager().Initialize();
258 void XclExpRoot::InitializeTable( SCTAB nScTab
)
260 SetCurrScTab( nScTab
);
261 if( GetBiff() == EXC_BIFF5
)
263 // local link manager per sheet
264 mrExpData
.mxLocLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
268 void XclExpRoot::InitializeSave()
270 GetPalette().Finalize();
271 GetXFBuffer().Finalize();
274 XclExpRecordRef
XclExpRoot::CreateRecord( sal_uInt16 nRecId
) const
276 XclExpRecordRef xRec
;
279 case EXC_ID_PALETTE
: xRec
= mrExpData
.mxPalette
; break;
280 case EXC_ID_FONTLIST
: xRec
= mrExpData
.mxFontBfr
; break;
281 case EXC_ID_FORMATLIST
: xRec
= mrExpData
.mxNumFmtBfr
; break;
282 case EXC_ID_XFLIST
: xRec
= mrExpData
.mxXFBfr
; break;
283 case EXC_ID_SST
: xRec
= mrExpData
.mxSst
; break;
284 case EXC_ID_EXTERNSHEET
: xRec
= GetLocalLinkMgrRef(); break;
285 case EXC_ID_NAME
: xRec
= mrExpData
.mxNameMgr
; break;
286 case EXC_ID_DXFS
: xRec
= mrExpData
.mxDxfs
; break;
288 OSL_ENSURE( xRec
, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
292 bool XclExpRoot::IsDocumentEncrypted() const
294 // We need to encrypt the content when the document structure is protected.
295 const ScDocProtection
* pDocProt
= GetDoc().GetDocProtection();
296 if (pDocProt
&& pDocProt
->isProtected() && pDocProt
->isOptionEnabled(ScDocProtection::STRUCTURE
))
299 if ( GetEncryptionData().getLength() > 0 )
300 // Password is entered directly into the save dialog.
306 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateEncryptionData( const OUString
& aPass
)
308 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
310 if ( !aPass
.isEmpty() && aPass
.getLength() < 16 )
313 osl_getSystemTime( &aTime
);
314 rtlRandomPool aRandomPool
= rtl_random_createPool ();
315 rtl_random_addBytes ( aRandomPool
, &aTime
, 8 );
317 sal_uInt8 pnDocId
[16];
318 rtl_random_getBytes( aRandomPool
, pnDocId
, 16 );
320 rtl_random_destroyPool( aRandomPool
);
322 sal_uInt16 pnPasswd
[16];
323 memset( pnPasswd
, 0, sizeof( pnPasswd
) );
324 for( sal_Int32 nChar
= 0; nChar
< aPass
.getLength(); ++nChar
)
325 pnPasswd
[nChar
] = aPass
[nChar
];
327 ::msfilter::MSCodec_Std97 aCodec
;
328 aCodec
.InitKey( pnPasswd
, pnDocId
);
329 aEncryptionData
= aCodec
.GetEncryptionData();
332 return aEncryptionData
;
335 uno::Sequence
< beans::NamedValue
> XclExpRoot::GetEncryptionData() const
337 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
338 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem
, SfxUnoAnyItem
, SID_ENCRYPTIONDATA
, false );
339 if ( pEncryptionDataItem
)
340 pEncryptionDataItem
->GetValue() >>= aEncryptionData
;
343 // try to get the encryption data from the password
344 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem
, SfxStringItem
, SID_PASSWORD
, false );
345 if ( pPasswordItem
&& !pPasswordItem
->GetValue().isEmpty() )
346 aEncryptionData
= GenerateEncryptionData( pPasswordItem
->GetValue() );
349 return aEncryptionData
;
352 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateDefaultEncryptionData() const
354 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
355 if ( !GetDefaultPassword().isEmpty() )
356 aEncryptionData
= GenerateEncryptionData( GetDefaultPassword() );
358 return aEncryptionData
;
361 XclExpRootData::XclExpLinkMgrRef
XclExpRoot::GetLocalLinkMgrRef() const
363 return IsInGlobals() ? mrExpData
.mxGlobLinkMgr
: mrExpData
.mxLocLinkMgr
;
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */