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