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 :
21 : #include <rtl/random.h>
22 : #include <sfx2/docfile.hxx>
23 : #include <sfx2/request.hxx>
24 : #include <sfx2/frame.hxx>
25 : #include <sfx2/sfxsids.hrc>
26 : #include <unotools/saveopt.hxx>
27 : #include <svl/itemset.hxx>
28 : #include <svl/stritem.hxx>
29 : #include <svl/intitem.hxx>
30 : #include <svl/eitem.hxx>
31 : #include "xecontent.hxx"
32 : #include "xltracer.hxx"
33 : #include "xeescher.hxx"
34 : #include "xeformula.hxx"
35 : #include "xehelper.hxx"
36 : #include "xelink.hxx"
37 : #include "xename.hxx"
38 : #include "xepivot.hxx"
39 : #include "xestyle.hxx"
40 : #include "xeroot.hxx"
41 :
42 : #include "excrecds.hxx" // for filter manager
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 9 : XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
55 : SotStorageRef xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc ) :
56 9 : XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
57 : {
58 9 : SvtSaveOptions aSaveOpt;
59 9 : mbRelUrl = mrMedium.IsRemote() ? aSaveOpt.IsSaveRelINet() : aSaveOpt.IsSaveRelFSys();
60 9 : }
61 :
62 9 : XclExpRootData::~XclExpRootData()
63 : {
64 9 : }
65 :
66 : // ----------------------------------------------------------------------------
67 :
68 9 : XclExpRoot::XclExpRoot( XclExpRootData& rExpRootData ) :
69 : XclRoot( rExpRootData ),
70 9 : mrExpData( rExpRootData )
71 : {
72 9 : }
73 :
74 160 : XclExpTabInfo& XclExpRoot::GetTabInfo() const
75 : {
76 : OSL_ENSURE( mrExpData.mxTabInfo, "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
77 160 : return *mrExpData.mxTabInfo;
78 : }
79 :
80 75 : XclExpAddressConverter& XclExpRoot::GetAddressConverter() const
81 : {
82 : OSL_ENSURE( mrExpData.mxAddrConv, "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
83 75 : return *mrExpData.mxAddrConv;
84 : }
85 :
86 14 : XclExpFormulaCompiler& XclExpRoot::GetFormulaCompiler() const
87 : {
88 : OSL_ENSURE( mrExpData.mxFmlaComp, "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
89 14 : return *mrExpData.mxFmlaComp;
90 : }
91 :
92 259 : XclExpProgressBar& XclExpRoot::GetProgressBar() const
93 : {
94 : OSL_ENSURE( mrExpData.mxProgress, "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
95 259 : return *mrExpData.mxProgress;
96 : }
97 :
98 12 : XclExpSst& XclExpRoot::GetSst() const
99 : {
100 : OSL_ENSURE( mrExpData.mxSst, "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
101 12 : return *mrExpData.mxSst;
102 : }
103 :
104 600 : XclExpPalette& XclExpRoot::GetPalette() const
105 : {
106 : OSL_ENSURE( mrExpData.mxPalette, "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
107 600 : return *mrExpData.mxPalette;
108 : }
109 :
110 124 : XclExpFontBuffer& XclExpRoot::GetFontBuffer() const
111 : {
112 : OSL_ENSURE( mrExpData.mxFontBfr, "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
113 124 : return *mrExpData.mxFontBfr;
114 : }
115 :
116 38 : XclExpNumFmtBuffer& XclExpRoot::GetNumFmtBuffer() const
117 : {
118 : OSL_ENSURE( mrExpData.mxNumFmtBfr, "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
119 38 : return *mrExpData.mxNumFmtBfr;
120 : }
121 :
122 22112 : XclExpXFBuffer& XclExpRoot::GetXFBuffer() const
123 : {
124 : OSL_ENSURE( mrExpData.mxXFBfr, "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
125 22112 : return *mrExpData.mxXFBfr;
126 : }
127 :
128 0 : XclExpLinkManager& XclExpRoot::GetGlobalLinkManager() const
129 : {
130 : OSL_ENSURE( mrExpData.mxGlobLinkMgr, "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
131 0 : return *mrExpData.mxGlobLinkMgr;
132 : }
133 :
134 14 : XclExpLinkManager& XclExpRoot::GetLocalLinkManager() const
135 : {
136 : OSL_ENSURE( GetLocalLinkMgrRef(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
137 14 : return *GetLocalLinkMgrRef();
138 : }
139 :
140 23 : XclExpNameManager& XclExpRoot::GetNameManager() const
141 : {
142 : OSL_ENSURE( mrExpData.mxNameMgr, "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
143 23 : return *mrExpData.mxNameMgr;
144 : }
145 :
146 46 : XclExpObjectManager& XclExpRoot::GetObjectManager() const
147 : {
148 : OSL_ENSURE( mrExpData.mxObjMgr, "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
149 46 : return *mrExpData.mxObjMgr;
150 : }
151 :
152 37 : XclExpFilterManager& XclExpRoot::GetFilterManager() const
153 : {
154 : OSL_ENSURE( mrExpData.mxFilterMgr, "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
155 37 : return *mrExpData.mxFilterMgr;
156 : }
157 :
158 8 : XclExpDxfs& XclExpRoot::GetDxfs() const
159 : {
160 : OSL_ENSURE( mrExpData.mxDxfs, "XclExpRoot::GetDxfs - missing object ( wrong BIFF?)" );
161 8 : return *mrExpData.mxDxfs;
162 : }
163 :
164 32 : XclExpPivotTableManager& XclExpRoot::GetPivotTableManager() const
165 : {
166 : OSL_ENSURE( mrExpData.mxPTableMgr, "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
167 32 : return *mrExpData.mxPTableMgr;
168 : }
169 :
170 6 : ScCompiler::OpCodeMapPtr XclExpRoot::GetOpCodeMap() const
171 : {
172 : OSL_ENSURE( mrExpData.mxOpCodeMap, "XclExpRoot::GetOpCodeMap - missing object (wrong BIFF?)" );
173 6 : return mrExpData.mxOpCodeMap;
174 : }
175 :
176 9 : void XclExpRoot::InitializeConvert()
177 : {
178 9 : mrExpData.mxTabInfo.reset( new XclExpTabInfo( GetRoot() ) );
179 9 : mrExpData.mxAddrConv.reset( new XclExpAddressConverter( GetRoot() ) );
180 9 : mrExpData.mxFmlaComp.reset( new XclExpFormulaCompiler( GetRoot() ) );
181 9 : mrExpData.mxProgress.reset( new XclExpProgressBar( GetRoot() ) );
182 :
183 9 : GetProgressBar().Initialize();
184 9 : }
185 :
186 9 : void XclExpRoot::InitializeGlobals()
187 : {
188 9 : SetCurrScTab( SCTAB_GLOBAL );
189 :
190 9 : if( GetBiff() >= EXC_BIFF5 )
191 : {
192 9 : mrExpData.mxPalette.reset( new XclExpPalette( GetRoot() ) );
193 9 : mrExpData.mxFontBfr.reset( new XclExpFontBuffer( GetRoot() ) );
194 9 : mrExpData.mxNumFmtBfr.reset( new XclExpNumFmtBuffer( GetRoot() ) );
195 9 : mrExpData.mxXFBfr.reset( new XclExpXFBuffer( GetRoot() ) );
196 9 : mrExpData.mxGlobLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
197 9 : mrExpData.mxNameMgr.reset( new XclExpNameManager( GetRoot() ) );
198 : }
199 :
200 9 : if( GetBiff() == EXC_BIFF8 )
201 : {
202 9 : mrExpData.mxSst.reset( new XclExpSst );
203 9 : mrExpData.mxObjMgr.reset( new XclExpObjectManager( GetRoot() ) );
204 9 : mrExpData.mxFilterMgr.reset( new XclExpFilterManager( GetRoot() ) );
205 9 : mrExpData.mxPTableMgr.reset( new XclExpPivotTableManager( GetRoot() ) );
206 : // BIFF8: only one link manager for all sheets
207 9 : mrExpData.mxLocLinkMgr = mrExpData.mxGlobLinkMgr;
208 9 : mrExpData.mxDxfs.reset( new XclExpDxfs( GetRoot() ) );
209 : }
210 :
211 9 : if( GetOutput() == EXC_OUTPUT_XML_2007 )
212 : {
213 : do
214 : {
215 6 : ScDocument& rDoc = GetDoc();
216 : // Pass the model factory to OpCodeProvider, not the process
217 : // service factory, otherwise a FormulaOpCodeMapperObj would be
218 : // instanciated 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 6 : const SfxObjectShell* pShell = rDoc.GetDocumentShell();
222 6 : if (!pShell)
223 : {
224 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no object shell");
225 0 : break;
226 : }
227 6 : uno::Reference< lang::XComponent > xComponent( pShell->GetModel(), uno::UNO_QUERY);
228 6 : if (!xComponent.is())
229 : {
230 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no component");
231 0 : break;
232 : }
233 12 : uno::Reference< lang::XMultiServiceFactory > xModelFactory( xComponent, uno::UNO_QUERY);
234 : // OOXML is also BIFF8 function-wise
235 : oox::xls::OpCodeProvider aOpCodeProvider( xModelFactory,
236 12 : 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 12 : uno::Any aAny( aOpCodeProvider.getOoxParserMap());
241 12 : uno::Sequence< const sheet::FormulaOpCodeMapEntry > aOpCodeMapping;
242 6 : if (!(aAny >>= aOpCodeMapping))
243 : {
244 : SAL_WARN( "sc", "XclExpRoot::InitializeGlobals - no OpCodeMap");
245 0 : break;
246 : }
247 12 : ScCompiler aCompiler( &rDoc, ScAddress());
248 6 : aCompiler.SetGrammar( rDoc.GetGrammar());
249 12 : mrExpData.mxOpCodeMap = aCompiler.CreateOpCodeMap( aOpCodeMapping, true);
250 : } while(0);
251 : }
252 :
253 9 : GetXFBuffer().Initialize();
254 9 : GetNameManager().Initialize();
255 9 : }
256 :
257 14 : void XclExpRoot::InitializeTable( SCTAB nScTab )
258 : {
259 14 : SetCurrScTab( nScTab );
260 14 : if( GetBiff() == EXC_BIFF5 )
261 : {
262 : // local link manager per sheet
263 0 : mrExpData.mxLocLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
264 : }
265 14 : }
266 :
267 9 : void XclExpRoot::InitializeSave()
268 : {
269 9 : GetPalette().Finalize();
270 9 : GetXFBuffer().Finalize();
271 9 : }
272 :
273 69 : XclExpRecordRef XclExpRoot::CreateRecord( sal_uInt16 nRecId ) const
274 : {
275 69 : XclExpRecordRef xRec;
276 69 : switch( nRecId )
277 : {
278 9 : case EXC_ID_PALETTE: xRec = mrExpData.mxPalette; break;
279 9 : case EXC_ID_FONTLIST: xRec = mrExpData.mxFontBfr; break;
280 9 : case EXC_ID_FORMATLIST: xRec = mrExpData.mxNumFmtBfr; break;
281 9 : case EXC_ID_XFLIST: xRec = mrExpData.mxXFBfr; break;
282 9 : case EXC_ID_SST: xRec = mrExpData.mxSst; break;
283 9 : case EXC_ID_EXTERNSHEET: xRec = GetLocalLinkMgrRef(); break;
284 9 : case EXC_ID_NAME: xRec = mrExpData.mxNameMgr; break;
285 6 : case EXC_ID_DXFS: xRec = mrExpData.mxDxfs; break;
286 : }
287 : OSL_ENSURE( xRec, "XclExpRoot::CreateRecord - unknown record ID or missing object" );
288 69 : return xRec;
289 : }
290 :
291 9 : bool XclExpRoot::IsDocumentEncrypted() const
292 : {
293 : // We need to encrypt the content when the document structure is protected.
294 9 : const ScDocProtection* pDocProt = GetDoc().GetDocProtection();
295 9 : if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE))
296 0 : return true;
297 :
298 9 : if ( GetEncryptionData().getLength() > 0 )
299 : // Password is entered directly into the save dialog.
300 0 : return true;
301 :
302 9 : 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 (xub_StrLen nChar = 0; nChar < aPass.getLength(); ++nChar )
324 0 : pnPasswd[nChar] = aPass.getStr()[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 9 : uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const
335 : {
336 9 : uno::Sequence< beans::NamedValue > aEncryptionData;
337 9 : SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, false );
338 9 : if ( pEncryptionDataItem )
339 0 : pEncryptionDataItem->GetValue() >>= aEncryptionData;
340 : else
341 : {
342 : // try to get the encryption data from the password
343 9 : SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, false );
344 9 : if ( pPasswordItem && !pPasswordItem->GetValue().isEmpty() )
345 0 : aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() );
346 : }
347 :
348 9 : 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().Len() > 0 )
355 0 : aEncryptionData = GenerateEncryptionData( GetDefaultPassword() );
356 :
357 0 : return aEncryptionData;
358 : }
359 :
360 23 : XclExpRootData::XclExpLinkMgrRef XclExpRoot::GetLocalLinkMgrRef() const
361 : {
362 23 : return IsInGlobals() ? mrExpData.mxGlobLinkMgr : mrExpData.mxLocLinkMgr;
363 15 : }
364 :
365 : // ============================================================================
366 :
367 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|