Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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"
35 : #include "xelink.hxx"
36 : #include "xename.hxx"
37 : #include "xepivot.hxx"
38 : #include "xestyle.hxx"
39 : #include "xeroot.hxx"
40 : #include <xepivotxml.hxx>
41 :
42 : #include "excrecds.hxx"
43 : #include "tabprotection.hxx"
44 : #include "document.hxx"
45 : #include "scextopt.hxx"
46 :
47 : #include "formulabase.hxx"
48 : #include <com/sun/star/sheet/FormulaOpCodeMapEntry.hpp>
49 :
50 : using namespace ::com::sun::star;
51 :
52 : // Global data ================================================================
53 :
54 64 : XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
55 : SotStorageRef xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc ) :
56 64 : XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
57 : {
58 64 : SvtSaveOptions aSaveOpt;
59 64 : mbRelUrl = mrMedium.IsRemote() ? aSaveOpt.IsSaveRelINet() : aSaveOpt.IsSaveRelFSys();
60 64 : }
61 :
62 64 : XclExpRootData::~XclExpRootData()
63 : {
64 64 : }
65 :
66 64 : XclExpRoot::XclExpRoot( XclExpRootData& rExpRootData ) :
67 : XclRoot( rExpRootData ),
68 64 : mrExpData( rExpRootData )
69 : {
70 64 : }
71 :
72 2782 : XclExpTabInfo& XclExpRoot::GetTabInfo() const
73 : {
74 : OSL_ENSURE( mrExpData.mxTabInfo, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
75 2782 : return *mrExpData.mxTabInfo;
76 : }
77 :
78 632 : XclExpAddressConverter& XclExpRoot::GetAddressConverter() const
79 : {
80 : OSL_ENSURE( mrExpData.mxAddrConv, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
81 632 : return *mrExpData.mxAddrConv;
82 : }
83 :
84 5366 : XclExpFormulaCompiler& XclExpRoot::GetFormulaCompiler() const
85 : {
86 : OSL_ENSURE( mrExpData.mxFmlaComp, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
87 5366 : return *mrExpData.mxFmlaComp;
88 : }
89 :
90 3048 : XclExpProgressBar& XclExpRoot::GetProgressBar() const
91 : {
92 : OSL_ENSURE( mrExpData.mxProgress, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
93 3048 : return *mrExpData.mxProgress;
94 : }
95 :
96 820 : XclExpSst& XclExpRoot::GetSst() const
97 : {
98 : OSL_ENSURE( mrExpData.mxSst, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
99 820 : return *mrExpData.mxSst;
100 : }
101 :
102 5364 : XclExpPalette& XclExpRoot::GetPalette() const
103 : {
104 : OSL_ENSURE( mrExpData.mxPalette, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
105 5364 : return *mrExpData.mxPalette;
106 : }
107 :
108 1852 : XclExpFontBuffer& XclExpRoot::GetFontBuffer() const
109 : {
110 : OSL_ENSURE( mrExpData.mxFontBfr, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
111 1852 : return *mrExpData.mxFontBfr;
112 : }
113 :
114 2680 : XclExpNumFmtBuffer& XclExpRoot::GetNumFmtBuffer() const
115 : {
116 : OSL_ENSURE( mrExpData.mxNumFmtBfr, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
117 2680 : return *mrExpData.mxNumFmtBfr;
118 : }
119 :
120 199240 : XclExpXFBuffer& XclExpRoot::GetXFBuffer() const
121 : {
122 : OSL_ENSURE( mrExpData.mxXFBfr, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
123 199240 : return *mrExpData.mxXFBfr;
124 : }
125 :
126 0 : XclExpLinkManager& XclExpRoot::GetGlobalLinkManager() const
127 : {
128 : OSL_ENSURE( mrExpData.mxGlobLinkMgr, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
129 0 : return *mrExpData.mxGlobLinkMgr;
130 : }
131 :
132 1006 : XclExpLinkManager& XclExpRoot::GetLocalLinkManager() const
133 : {
134 : OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
135 1006 : return *GetLocalLinkMgrRef();
136 : }
137 :
138 540 : XclExpNameManager& XclExpRoot::GetNameManager() const
139 : {
140 : OSL_ENSURE( mrExpData.mxNameMgr, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
141 540 : return *mrExpData.mxNameMgr;
142 : }
143 :
144 388 : XclExpObjectManager& XclExpRoot::GetObjectManager() const
145 : {
146 : OSL_ENSURE( mrExpData.mxObjMgr, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
147 388 : return *mrExpData.mxObjMgr;
148 : }
149 :
150 322 : XclExpFilterManager& XclExpRoot::GetFilterManager() const
151 : {
152 : OSL_ENSURE( mrExpData.mxFilterMgr, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
153 322 : return *mrExpData.mxFilterMgr;
154 : }
155 :
156 20 : XclExpDxfs& XclExpRoot::GetDxfs() const
157 : {
158 : OSL_ENSURE( mrExpData.mxDxfs, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
159 20 : return *mrExpData.mxDxfs;
160 : }
161 :
162 102 : XclExpPivotTableManager& XclExpRoot::GetPivotTableManager() const
163 : {
164 : OSL_ENSURE( mrExpData.mxPTableMgr, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
165 102 : return *mrExpData.mxPTableMgr;
166 : }
167 :
168 150 : XclExpXmlPivotTableManager& XclExpRoot::GetXmlPivotTableManager()
169 : {
170 : assert(mrExpData.mxXmlPTableMgr);
171 150 : return *mrExpData.mxXmlPTableMgr;
172 : }
173 :
174 64 : void XclExpRoot::InitializeConvert()
175 : {
176 64 : mrExpData.mxTabInfo.reset( new XclExpTabInfo( GetRoot() ) );
177 64 : mrExpData.mxAddrConv.reset( new XclExpAddressConverter( GetRoot() ) );
178 64 : mrExpData.mxFmlaComp.reset( new XclExpFormulaCompiler( GetRoot() ) );
179 64 : mrExpData.mxProgress.reset( new XclExpProgressBar( GetRoot() ) );
180 :
181 64 : GetProgressBar().Initialize();
182 64 : }
183 :
184 64 : void XclExpRoot::InitializeGlobals()
185 : {
186 64 : SetCurrScTab( SCTAB_GLOBAL );
187 :
188 64 : if( GetBiff() >= EXC_BIFF5 )
189 : {
190 64 : mrExpData.mxPalette.reset( new XclExpPalette( GetRoot() ) );
191 64 : mrExpData.mxFontBfr.reset( new XclExpFontBuffer( GetRoot() ) );
192 64 : mrExpData.mxNumFmtBfr.reset( new XclExpNumFmtBuffer( GetRoot() ) );
193 64 : mrExpData.mxXFBfr.reset( new XclExpXFBuffer( GetRoot() ) );
194 64 : mrExpData.mxGlobLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
195 64 : mrExpData.mxNameMgr.reset( new XclExpNameManager( GetRoot() ) );
196 : }
197 :
198 64 : if( GetBiff() == EXC_BIFF8 )
199 : {
200 64 : mrExpData.mxSst.reset( new XclExpSst );
201 64 : mrExpData.mxObjMgr.reset( new XclExpObjectManager( GetRoot() ) );
202 64 : mrExpData.mxFilterMgr.reset( new XclExpFilterManager( GetRoot() ) );
203 64 : mrExpData.mxPTableMgr.reset( new XclExpPivotTableManager( GetRoot() ) );
204 : // BIFF8: only one link manager for all sheets
205 64 : mrExpData.mxLocLinkMgr = mrExpData.mxGlobLinkMgr;
206 64 : mrExpData.mxDxfs.reset( new XclExpDxfs( GetRoot() ) );
207 : }
208 :
209 64 : if( GetOutput() == EXC_OUTPUT_XML_2007 )
210 : {
211 38 : mrExpData.mxXmlPTableMgr.reset(new XclExpXmlPivotTableManager(GetRoot()));
212 :
213 : do
214 : {
215 38 : ScDocument& rDoc = GetDoc();
216 : // Pass the model factory to OpCodeProvider, not the process
217 : // service factory, otherwise a FormulaOpCodeMapperObj would be
218 : // instantiated 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 38 : const SfxObjectShell* pShell = rDoc.GetDocumentShell();
222 38 : if (!pShell)
223 : {
224 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
225 0 : break;
226 : }
227 38 : uno::Reference< lang::XComponent > xComponent( pShell->GetModel(), uno::UNO_QUERY);
228 38 : if (!xComponent.is())
229 : {
230 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
231 0 : break;
232 : }
233 76 : uno::Reference< lang::XMultiServiceFactory > xModelFactory( xComponent, uno::UNO_QUERY);
234 : // OOXML is also BIFF8 function-wise
235 : oox::xls::OpCodeProvider aOpCodeProvider( xModelFactory,
236 76 : 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 76 : uno::Any aAny( aOpCodeProvider.getOoxParserMap());
241 76 : uno::Sequence< const sheet::FormulaOpCodeMapEntry > aOpCodeMapping;
242 38 : if (!(aAny >>= aOpCodeMapping))
243 : {
244 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
245 0 : break;
246 : }
247 76 : ScCompiler aCompiler( &rDoc, ScAddress());
248 38 : aCompiler.SetGrammar( rDoc.GetGrammar());
249 76 : mrExpData.mxOpCodeMap = aCompiler.CreateOpCodeMap( aOpCodeMapping, true);
250 : } while(false);
251 : }
252 :
253 64 : GetXFBuffer().Initialize();
254 64 : GetNameManager().Initialize();
255 64 : }
256 :
257 124 : void XclExpRoot::InitializeTable( SCTAB nScTab )
258 : {
259 124 : SetCurrScTab( nScTab );
260 124 : if( GetBiff() == EXC_BIFF5 )
261 : {
262 : // local link manager per sheet
263 0 : mrExpData.mxLocLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
264 : }
265 124 : }
266 :
267 64 : void XclExpRoot::InitializeSave()
268 : {
269 64 : GetPalette().Finalize();
270 64 : GetXFBuffer().Finalize();
271 64 : }
272 :
273 486 : XclExpRecordRef XclExpRoot::CreateRecord( sal_uInt16 nRecId ) const
274 : {
275 486 : XclExpRecordRef xRec;
276 486 : switch( nRecId )
277 : {
278 64 : case EXC_ID_PALETTE: xRec = mrExpData.mxPalette; break;
279 64 : case EXC_ID_FONTLIST: xRec = mrExpData.mxFontBfr; break;
280 64 : case EXC_ID_FORMATLIST: xRec = mrExpData.mxNumFmtBfr; break;
281 64 : case EXC_ID_XFLIST: xRec = mrExpData.mxXFBfr; break;
282 64 : case EXC_ID_SST: xRec = mrExpData.mxSst; break;
283 64 : case EXC_ID_EXTERNSHEET: xRec = GetLocalLinkMgrRef(); break;
284 64 : case EXC_ID_NAME: xRec = mrExpData.mxNameMgr; break;
285 38 : case EXC_ID_DXFS: xRec = mrExpData.mxDxfs; break;
286 : }
287 : OSL_ENSURE( xRec, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
288 486 : return xRec;
289 : }
290 :
291 26 : bool XclExpRoot::IsDocumentEncrypted() const
292 : {
293 : // We need to encrypt the content when the document structure is protected.
294 26 : const ScDocProtection* pDocProt = GetDoc().GetDocProtection();
295 26 : if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE))
296 0 : return true;
297 :
298 26 : if ( GetEncryptionData().getLength() > 0 )
299 : // Password is entered directly into the save dialog.
300 0 : return true;
301 :
302 26 : return false;
303 : }
304 :
305 0 : uno::Sequence< beans::NamedValue > XclExpRoot::GenerateEncryptionData( const OUString& aPass ) const
306 : {
307 0 : uno::Sequence< beans::NamedValue > aEncryptionData;
308 :
309 0 : if ( !aPass.isEmpty() && aPass.getLength() < 16 )
310 : {
311 : TimeValue aTime;
312 0 : osl_getSystemTime( &aTime );
313 0 : rtlRandomPool aRandomPool = rtl_random_createPool ();
314 0 : rtl_random_addBytes ( aRandomPool, &aTime, 8 );
315 :
316 : sal_uInt8 pnDocId[16];
317 0 : rtl_random_getBytes( aRandomPool, pnDocId, 16 );
318 :
319 0 : rtl_random_destroyPool( aRandomPool );
320 :
321 : sal_uInt16 pnPasswd[16];
322 0 : memset( pnPasswd, 0, sizeof( pnPasswd ) );
323 0 : for( sal_Int32 nChar = 0; nChar < aPass.getLength(); ++nChar )
324 0 : pnPasswd[nChar] = aPass[nChar];
325 :
326 0 : ::msfilter::MSCodec_Std97 aCodec;
327 0 : aCodec.InitKey( pnPasswd, pnDocId );
328 0 : aEncryptionData = aCodec.GetEncryptionData();
329 : }
330 :
331 0 : return aEncryptionData;
332 : }
333 :
334 26 : uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const
335 : {
336 26 : uno::Sequence< beans::NamedValue > aEncryptionData;
337 26 : SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false );
338 26 : if ( pEncryptionDataItem )
339 0 : pEncryptionDataItem->GetValue() >>= aEncryptionData;
340 : else
341 : {
342 : // try to get the encryption data from the password
343 26 : SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false );
344 26 : if ( pPasswordItem && !pPasswordItem->GetValue().isEmpty() )
345 0 : aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() );
346 : }
347 :
348 26 : return aEncryptionData;
349 : }
350 :
351 0 : uno::Sequence< beans::NamedValue > XclExpRoot::GenerateDefaultEncryptionData() const
352 : {
353 0 : uno::Sequence< beans::NamedValue > aEncryptionData;
354 0 : if ( !GetDefaultPassword().isEmpty() )
355 0 : aEncryptionData = GenerateEncryptionData( GetDefaultPassword() );
356 :
357 0 : return aEncryptionData;
358 : }
359 :
360 1070 : XclExpRootData::XclExpLinkMgrRef XclExpRoot::GetLocalLinkMgrRef() const
361 : {
362 1070 : return IsInGlobals() ? mrExpData.mxGlobLinkMgr : mrExpData.mxLocLinkMgr;
363 48 : }
364 :
365 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|