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 <officecfg/Office/Common.hxx>
21 #include <rtl/random.h>
22 #include <sal/log.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/frame.hxx>
25 #include <sfx2/sfxsids.hrc>
26 #include <sot/storage.hxx>
27 #include <svl/itemset.hxx>
28 #include <svl/stritem.hxx>
29 #include <xecontent.hxx>
30 #include <xeescher.hxx>
31 #include <xeformula.hxx>
32 #include <xehelper.hxx>
35 #include <xepivot.hxx>
36 #include <xestyle.hxx>
38 #include <xepivotxml.hxx>
39 #include <xedbdata.hxx>
40 #include <xlcontent.hxx>
44 #include <excrecds.hxx>
45 #include <tabprotection.hxx>
46 #include <document.hxx>
49 #include <formulabase.hxx>
50 #include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 using namespace ::com::sun::star
;
55 // Global data ================================================================
57 XclExpRootData::XclExpRootData( XclBiff eBiff
, SfxMedium
& rMedium
,
58 const rtl::Reference
<SotStorage
>& xRootStrg
, ScDocument
& rDoc
, rtl_TextEncoding eTextEnc
) :
59 XclRootData( eBiff
, rMedium
, xRootStrg
, rDoc
, eTextEnc
, true )
61 mbRelUrl
= mrMedium
.IsRemote()
62 ? officecfg::Office::Common::Save::URL::Internet::get()
63 : officecfg::Office::Common::Save::URL::FileSystem::get();
64 maStringBuf
.setLength(0);
67 XclExpRootData::~XclExpRootData()
71 XclExpRoot::XclExpRoot( XclExpRootData
& rExpRootData
) :
72 XclRoot( rExpRootData
),
73 mrExpData( rExpRootData
)
77 XclExpTabInfo
& XclExpRoot::GetTabInfo() const
79 OSL_ENSURE( mrExpData
.mxTabInfo
, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
80 return *mrExpData
.mxTabInfo
;
83 XclExpAddressConverter
& XclExpRoot::GetAddressConverter() const
85 OSL_ENSURE( mrExpData
.mxAddrConv
, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
86 return *mrExpData
.mxAddrConv
;
89 XclExpFormulaCompiler
& XclExpRoot::GetFormulaCompiler() const
91 OSL_ENSURE( mrExpData
.mxFmlaComp
, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
92 return *mrExpData
.mxFmlaComp
;
95 XclExpProgressBar
& XclExpRoot::GetProgressBar() const
97 OSL_ENSURE( mrExpData
.mxProgress
, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
98 return *mrExpData
.mxProgress
;
101 XclExpSst
& XclExpRoot::GetSst() const
103 OSL_ENSURE( mrExpData
.mxSst
, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
104 return *mrExpData
.mxSst
;
107 XclExpPalette
& XclExpRoot::GetPalette() const
109 OSL_ENSURE( mrExpData
.mxPalette
, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
110 return *mrExpData
.mxPalette
;
113 XclExpFontBuffer
& XclExpRoot::GetFontBuffer() const
115 OSL_ENSURE( mrExpData
.mxFontBfr
, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
116 return *mrExpData
.mxFontBfr
;
119 XclExpNumFmtBuffer
& XclExpRoot::GetNumFmtBuffer() const
121 OSL_ENSURE( mrExpData
.mxNumFmtBfr
, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
122 return *mrExpData
.mxNumFmtBfr
;
125 XclExpXFBuffer
& XclExpRoot::GetXFBuffer() const
127 OSL_ENSURE( mrExpData
.mxXFBfr
, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
128 return *mrExpData
.mxXFBfr
;
131 XclExpLinkManager
& XclExpRoot::GetGlobalLinkManager() const
133 OSL_ENSURE( mrExpData
.mxGlobLinkMgr
, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
134 return *mrExpData
.mxGlobLinkMgr
;
137 XclExpLinkManager
& XclExpRoot::GetLocalLinkManager() const
139 OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
140 return *GetLocalLinkMgrRef();
143 XclExpNameManager
& XclExpRoot::GetNameManager() const
145 OSL_ENSURE( mrExpData
.mxNameMgr
, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
146 return *mrExpData
.mxNameMgr
;
149 XclExpObjectManager
& XclExpRoot::GetObjectManager() const
151 OSL_ENSURE( mrExpData
.mxObjMgr
, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
152 return *mrExpData
.mxObjMgr
;
155 XclExpFilterManager
& XclExpRoot::GetFilterManager() const
157 OSL_ENSURE( mrExpData
.mxFilterMgr
, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
158 return *mrExpData
.mxFilterMgr
;
161 XclExpDxfs
& XclExpRoot::GetDxfs() const
163 OSL_ENSURE( mrExpData
.mxDxfs
, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
164 return *mrExpData
.mxDxfs
;
167 XclExpPivotTableManager
& XclExpRoot::GetPivotTableManager() const
169 OSL_ENSURE( mrExpData
.mxPTableMgr
, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
170 return *mrExpData
.mxPTableMgr
;
173 XclExpXmlPivotTableManager
& XclExpRoot::GetXmlPivotTableManager()
175 assert(mrExpData
.mxXmlPTableMgr
);
176 return *mrExpData
.mxXmlPTableMgr
;
179 XclExpTablesManager
& XclExpRoot::GetTablesManager()
181 assert(mrExpData
.mxTablesMgr
);
182 return *mrExpData
.mxTablesMgr
;
185 void XclExpRoot::InitializeConvert()
187 mrExpData
.mxTabInfo
= std::make_shared
<XclExpTabInfo
>( GetRoot() );
188 mrExpData
.mxAddrConv
= std::make_shared
<XclExpAddressConverter
>( GetRoot() );
189 mrExpData
.mxFmlaComp
= std::make_shared
<XclExpFormulaCompiler
>( GetRoot() );
190 mrExpData
.mxProgress
= std::make_shared
<XclExpProgressBar
>( GetRoot() );
192 GetProgressBar().Initialize();
195 void XclExpRoot::InitializeGlobals()
197 SetCurrScTab( SCTAB_GLOBAL
);
199 if( GetBiff() >= EXC_BIFF5
)
201 mrExpData
.mxPalette
= new XclExpPalette( GetRoot() );
202 mrExpData
.mxFontBfr
= new XclExpFontBuffer( GetRoot() );
203 mrExpData
.mxNumFmtBfr
= new XclExpNumFmtBuffer( GetRoot() );
204 mrExpData
.mxXFBfr
= new XclExpXFBuffer( GetRoot() );
205 mrExpData
.mxGlobLinkMgr
= new XclExpLinkManager( GetRoot() );
206 mrExpData
.mxNameMgr
= new XclExpNameManager( GetRoot() );
209 if( GetBiff() == EXC_BIFF8
)
211 mrExpData
.mxSst
= new XclExpSst();
212 mrExpData
.mxObjMgr
= std::make_shared
<XclExpObjectManager
>( GetRoot() );
213 mrExpData
.mxFilterMgr
= std::make_shared
<XclExpFilterManager
>( GetRoot() );
214 mrExpData
.mxPTableMgr
= std::make_shared
<XclExpPivotTableManager
>( GetRoot() );
215 // BIFF8: only one link manager for all sheets
216 mrExpData
.mxLocLinkMgr
= mrExpData
.mxGlobLinkMgr
;
217 mrExpData
.mxDxfs
= new XclExpDxfs( GetRoot() );
220 if( GetOutput() == EXC_OUTPUT_XML_2007
)
222 mrExpData
.mxXmlPTableMgr
= std::make_shared
<XclExpXmlPivotTableManager
>(GetRoot());
223 mrExpData
.mxTablesMgr
= std::make_shared
<XclExpTablesManager
>(GetRoot());
227 ScDocument
& rDoc
= GetDoc();
228 // Pass the model factory to OpCodeProvider, not the process
229 // service factory, otherwise a FormulaOpCodeMapperObj would be
230 // instantiated instead of a ScFormulaOpCodeMapperObj and the
231 // ScCompiler virtuals not be called! Which would be the case with
232 // the current (2013-01-24) rDoc.GetServiceManager()
233 const ScDocShell
* pShell
= rDoc
.GetDocumentShell();
236 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
239 uno::Reference
< lang::XComponent
> xComponent
= pShell
->GetModel();
240 if (!xComponent
.is())
242 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
245 uno::Reference
< lang::XMultiServiceFactory
> xModelFactory( xComponent
, uno::UNO_QUERY
);
246 oox::xls::OpCodeProvider
aOpCodeProvider(xModelFactory
, false);
247 // Compiler mocks about non-matching ctor or conversion from
248 // Sequence<...> to Sequence<const ...> if directly created or passed,
249 // conversion through Any works around.
250 uno::Any
aAny( aOpCodeProvider
.getOoxParserMap());
251 uno::Sequence
< const sheet::FormulaOpCodeMapEntry
> aOpCodeMapping
;
252 if (!(aAny
>>= aOpCodeMapping
))
254 SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
257 ScCompiler
aCompiler( rDoc
, ScAddress(), 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
= new XclExpLinkManager( GetRoot() );
276 void XclExpRoot::InitializeSave()
278 GetPalette().Finalize();
279 GetXFBuffer().Finalize();
280 GetDxfs().Finalize();
283 XclExpRecordRef
XclExpRoot::CreateRecord( sal_uInt16 nRecId
) const
285 XclExpRecordRef xRec
;
288 case EXC_ID_PALETTE
: xRec
= mrExpData
.mxPalette
; break;
289 case EXC_ID_FONTLIST
: xRec
= mrExpData
.mxFontBfr
; break;
290 case EXC_ID_FORMATLIST
: xRec
= mrExpData
.mxNumFmtBfr
; break;
291 case EXC_ID_XFLIST
: xRec
= mrExpData
.mxXFBfr
; break;
292 case EXC_ID_SST
: xRec
= mrExpData
.mxSst
; break;
293 case EXC_ID_EXTERNSHEET
: xRec
= GetLocalLinkMgrRef(); break;
294 case EXC_ID_NAME
: xRec
= mrExpData
.mxNameMgr
; break;
295 case EXC_ID_DXFS
: xRec
= mrExpData
.mxDxfs
; break;
297 OSL_ENSURE( xRec
, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
301 bool XclExpRoot::IsDocumentEncrypted() const
303 // We need to encrypt the content when the document structure is protected.
304 const ScDocProtection
* pDocProt
= GetDoc().GetDocProtection();
305 if (pDocProt
&& pDocProt
->isProtected() && pDocProt
->isOptionEnabled(ScDocProtection::STRUCTURE
))
308 // Whether password is entered directly into the save dialog.
309 return GetEncryptionData().hasElements();
312 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateEncryptionData( std::u16string_view aPass
)
314 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
316 if ( !aPass
.empty() && aPass
.size() < 16 )
318 sal_uInt8 pnDocId
[16];
319 if (rtl_random_getBytes(nullptr, pnDocId
, 16) != rtl_Random_E_None
)
321 throw uno::RuntimeException(u
"rtl_random_getBytes failed"_ustr
);
324 sal_uInt16 pnPasswd
[16] = {};
325 for( size_t nChar
= 0; nChar
< aPass
.size(); ++nChar
)
326 pnPasswd
[nChar
] = aPass
[nChar
];
328 ::msfilter::MSCodec_Std97 aCodec
;
329 aCodec
.InitKey( pnPasswd
, pnDocId
);
330 aEncryptionData
= aCodec
.GetEncryptionData();
333 return aEncryptionData
;
336 uno::Sequence
< beans::NamedValue
> XclExpRoot::GetEncryptionData() const
338 uno::Sequence
< beans::NamedValue
> aEncryptionData
;
339 const SfxUnoAnyItem
* pEncryptionDataItem
= GetMedium().GetItemSet().GetItem(SID_ENCRYPTIONDATA
, false);
340 if ( pEncryptionDataItem
)
341 pEncryptionDataItem
->GetValue() >>= aEncryptionData
;
344 // try to get the encryption data from the password
345 const SfxStringItem
* pPasswordItem
= GetMedium().GetItemSet().GetItem(SID_PASSWORD
, false);
346 if ( pPasswordItem
&& !pPasswordItem
->GetValue().isEmpty() )
347 aEncryptionData
= GenerateEncryptionData( pPasswordItem
->GetValue() );
350 return aEncryptionData
;
353 uno::Sequence
< beans::NamedValue
> XclExpRoot::GenerateDefaultEncryptionData()
355 return GenerateEncryptionData( GetDefaultPassword() );
358 XclExpRootData::XclExpLinkMgrRef
const & XclExpRoot::GetLocalLinkMgrRef() const
360 return IsInGlobals() ? mrExpData
.mxGlobLinkMgr
: mrExpData
.mxLocLinkMgr
;
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */