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 <sal/log.hxx>
22 #include <sfx2/docfile.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 <xecontent.hxx>
29 #include <xeescher.hxx>
30 #include <xeformula.hxx>
31 #include <xehelper.hxx>
34 #include <xepivot.hxx>
35 #include <xestyle.hxx>
37 #include <xepivotxml.hxx>
38 #include <xedbdata.hxx>
39 #include <xlcontent.hxx>
43 #include <excrecds.hxx>
44 #include <tabprotection.hxx>
45 #include <document.hxx>
47 #include <formulabase.hxx>
48 #include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 using namespace ::com::sun::star
;
53 // Global data ================================================================
55 XclExpRootData::XclExpRootData( XclBiff eBiff
, SfxMedium
& rMedium
,
56 const 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
.setLength(0);
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 oox::xls::OpCodeProvider
aOpCodeProvider(xModelFactory
, false);
244 // Compiler mocks about non-matching ctor or conversion from
245 // Sequence<...> to Sequence<const ...> if directly created or passed,
246 // conversion through Any works around.
247 uno::Any
aAny( aOpCodeProvider
.getOoxParserMap());
248 uno::Sequence
< const sheet::FormulaOpCodeMapEntry
> aOpCodeMapping
;
249 if (!(aAny
>>= aOpCodeMapping
))
251 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
254 ScCompiler
aCompiler( &rDoc
, ScAddress(), rDoc
.GetGrammar());
255 mrExpData
.mxOpCodeMap
= formula::FormulaCompiler::CreateOpCodeMap( aOpCodeMapping
, true);
259 GetXFBuffer().Initialize();
260 GetNameManager().Initialize();
263 void XclExpRoot::InitializeTable( SCTAB nScTab
)
265 SetCurrScTab( nScTab
);
266 if( GetBiff() == EXC_BIFF5
)
268 // local link manager per sheet
269 mrExpData
.mxLocLinkMgr
.reset( new XclExpLinkManager( GetRoot() ) );
273 void XclExpRoot::InitializeSave()
275 GetPalette().Finalize();
276 GetXFBuffer().Finalize();
279 XclExpRecordRef
XclExpRoot::CreateRecord( sal_uInt16 nRecId
) const
281 XclExpRecordRef xRec
;
284 case EXC_ID_PALETTE
: xRec
= mrExpData
.mxPalette
; break;
285 case EXC_ID_FONTLIST
: xRec
= mrExpData
.mxFontBfr
; break;
286 case EXC_ID_FORMATLIST
: xRec
= mrExpData
.mxNumFmtBfr
; break;
287 case EXC_ID_XFLIST
: xRec
= mrExpData
.mxXFBfr
; break;
288 case EXC_ID_SST
: xRec
= mrExpData
.mxSst
; break;
289 case EXC_ID_EXTERNSHEET
: xRec
= GetLocalLinkMgrRef(); break;
290 case EXC_ID_NAME
: xRec
= mrExpData
.mxNameMgr
; break;
291 case EXC_ID_DXFS
: xRec
= mrExpData
.mxDxfs
; break;
293 OSL_ENSURE( xRec
, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
297 bool XclExpRoot::IsDocumentEncrypted() const
299 // We need to encrypt the content when the document structure is protected.
300 const ScDocProtection
* pDocProt
= GetDoc().GetDocProtection();
301 if (pDocProt
&& pDocProt
->isProtected() && pDocProt
->isOptionEnabled(ScDocProtection::STRUCTURE
))
304 // Whether password is entered directly into the save dialog.
305 return GetEncryptionData().hasElements();
308 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateEncryptionData( const OUString
& aPass
)
310 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
312 if ( !aPass
.isEmpty() && aPass
.getLength() < 16 )
314 rtlRandomPool aRandomPool
= rtl_random_createPool ();
315 sal_uInt8 pnDocId
[16];
316 rtl_random_getBytes( aRandomPool
, pnDocId
, 16 );
318 rtl_random_destroyPool( aRandomPool
);
320 sal_uInt16 pnPasswd
[16] = {};
321 for( sal_Int32 nChar
= 0; nChar
< aPass
.getLength(); ++nChar
)
322 pnPasswd
[nChar
] = aPass
[nChar
];
324 ::msfilter::MSCodec_Std97 aCodec
;
325 aCodec
.InitKey( pnPasswd
, pnDocId
);
326 aEncryptionData
= aCodec
.GetEncryptionData();
329 return aEncryptionData
;
332 uno::Sequence
< beans::NamedValue
> XclExpRoot::GetEncryptionData() const
334 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
335 const SfxUnoAnyItem
* pEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(GetMedium().GetItemSet(), SID_ENCRYPTIONDATA
, false);
336 if ( pEncryptionDataItem
)
337 pEncryptionDataItem
->GetValue() >>= aEncryptionData
;
340 // try to get the encryption data from the password
341 const SfxStringItem
* pPasswordItem
= SfxItemSet::GetItem
<SfxStringItem
>(GetMedium().GetItemSet(), SID_PASSWORD
, false);
342 if ( pPasswordItem
&& !pPasswordItem
->GetValue().isEmpty() )
343 aEncryptionData
= GenerateEncryptionData( pPasswordItem
->GetValue() );
346 return aEncryptionData
;
349 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateDefaultEncryptionData()
351 return GenerateEncryptionData( GetDefaultPassword() );
354 XclExpRootData::XclExpLinkMgrRef
const & XclExpRoot::GetLocalLinkMgrRef() const
356 return IsInGlobals() ? mrExpData
.mxGlobLinkMgr
: mrExpData
.mxLocLinkMgr
;
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */