1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26
27 #include <rtl/random.h>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/request.hxx>
30 #include <sfx2/frame.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <unotools/saveopt.hxx>
33 #include <svl/itemset.hxx>
34 #include <svl/stritem.hxx>
35 #include <svl/intitem.hxx>
36 #include <svl/eitem.hxx>
37 #include "xecontent.hxx"
38 #include "xltracer.hxx"
39 #include "xeescher.hxx"
40 #include "xeformula.hxx"
41 #include "xehelper.hxx"
42 #include "xelink.hxx"
43 #include "xename.hxx"
44 #include "xepivot.hxx"
45 #include "xestyle.hxx"
46 #include "xeroot.hxx"
47
48 #include "excrecds.hxx" // for filter manager
49 #include "tabprotection.hxx"
50 #include "document.hxx"
51 #include "scextopt.hxx"
52
53 using namespace ::com::sun::star;
54
55 // Global data ================================================================
56
XclExpRootData(XclBiff eBiff,SfxMedium & rMedium,SotStorageRef xRootStrg,ScDocument & rDoc,rtl_TextEncoding eTextEnc)57 XclExpRootData::XclExpRootData( XclBiff eBiff, SfxMedium& rMedium,
58 SotStorageRef xRootStrg, ScDocument& rDoc, rtl_TextEncoding eTextEnc ) :
59 XclRootData( eBiff, rMedium, xRootStrg, rDoc, eTextEnc, true )
60 {
61 SvtSaveOptions aSaveOpt;
62 mbRelUrl = mrMedium.IsRemote() ? aSaveOpt.IsSaveRelINet() : aSaveOpt.IsSaveRelFSys();
63 }
64
~XclExpRootData()65 XclExpRootData::~XclExpRootData()
66 {
67 }
68
69 // ----------------------------------------------------------------------------
70
XclExpRoot(XclExpRootData & rExpRootData)71 XclExpRoot::XclExpRoot( XclExpRootData& rExpRootData ) :
72 XclRoot( rExpRootData ),
73 mrExpData( rExpRootData )
74 {
75 }
76
GetTabInfo() const77 XclExpTabInfo& XclExpRoot::GetTabInfo() const
78 {
79 DBG_ASSERT( mrExpData.mxTabInfo.is(), "XclExpRoot::GetTabInfo - missing object (wrong BIFF?)" );
80 return *mrExpData.mxTabInfo;
81 }
82
GetAddressConverter() const83 XclExpAddressConverter& XclExpRoot::GetAddressConverter() const
84 {
85 DBG_ASSERT( mrExpData.mxAddrConv.is(), "XclExpRoot::GetAddressConverter - missing object (wrong BIFF?)" );
86 return *mrExpData.mxAddrConv;
87 }
88
GetFormulaCompiler() const89 XclExpFormulaCompiler& XclExpRoot::GetFormulaCompiler() const
90 {
91 DBG_ASSERT( mrExpData.mxFmlaComp.is(), "XclExpRoot::GetFormulaCompiler - missing object (wrong BIFF?)" );
92 return *mrExpData.mxFmlaComp;
93 }
94
GetProgressBar() const95 XclExpProgressBar& XclExpRoot::GetProgressBar() const
96 {
97 DBG_ASSERT( mrExpData.mxProgress.is(), "XclExpRoot::GetProgressBar - missing object (wrong BIFF?)" );
98 return *mrExpData.mxProgress;
99 }
100
GetSst() const101 XclExpSst& XclExpRoot::GetSst() const
102 {
103 DBG_ASSERT( mrExpData.mxSst.is(), "XclExpRoot::GetSst - missing object (wrong BIFF?)" );
104 return *mrExpData.mxSst;
105 }
106
GetPalette() const107 XclExpPalette& XclExpRoot::GetPalette() const
108 {
109 DBG_ASSERT( mrExpData.mxPalette.is(), "XclExpRoot::GetPalette - missing object (wrong BIFF?)" );
110 return *mrExpData.mxPalette;
111 }
112
GetFontBuffer() const113 XclExpFontBuffer& XclExpRoot::GetFontBuffer() const
114 {
115 DBG_ASSERT( mrExpData.mxFontBfr.is(), "XclExpRoot::GetFontBuffer - missing object (wrong BIFF?)" );
116 return *mrExpData.mxFontBfr;
117 }
118
GetNumFmtBuffer() const119 XclExpNumFmtBuffer& XclExpRoot::GetNumFmtBuffer() const
120 {
121 DBG_ASSERT( mrExpData.mxNumFmtBfr.is(), "XclExpRoot::GetNumFmtBuffer - missing object (wrong BIFF?)" );
122 return *mrExpData.mxNumFmtBfr;
123 }
124
GetXFBuffer() const125 XclExpXFBuffer& XclExpRoot::GetXFBuffer() const
126 {
127 DBG_ASSERT( mrExpData.mxXFBfr.is(), "XclExpRoot::GetXFBuffer - missing object (wrong BIFF?)" );
128 return *mrExpData.mxXFBfr;
129 }
130
GetGlobalLinkManager() const131 XclExpLinkManager& XclExpRoot::GetGlobalLinkManager() const
132 {
133 DBG_ASSERT( mrExpData.mxGlobLinkMgr.is(), "XclExpRoot::GetGlobalLinkManager - missing object (wrong BIFF?)" );
134 return *mrExpData.mxGlobLinkMgr;
135 }
136
GetLocalLinkManager() const137 XclExpLinkManager& XclExpRoot::GetLocalLinkManager() const
138 {
139 DBG_ASSERT( GetLocalLinkMgrRef().is(), "XclExpRoot::GetLocalLinkManager - missing object (wrong BIFF?)" );
140 return *GetLocalLinkMgrRef();
141 }
142
GetNameManager() const143 XclExpNameManager& XclExpRoot::GetNameManager() const
144 {
145 DBG_ASSERT( mrExpData.mxNameMgr.is(), "XclExpRoot::GetNameManager - missing object (wrong BIFF?)" );
146 return *mrExpData.mxNameMgr;
147 }
148
GetObjectManager() const149 XclExpObjectManager& XclExpRoot::GetObjectManager() const
150 {
151 DBG_ASSERT( mrExpData.mxObjMgr.is(), "XclExpRoot::GetObjectManager - missing object (wrong BIFF?)" );
152 return *mrExpData.mxObjMgr;
153 }
154
GetFilterManager() const155 XclExpFilterManager& XclExpRoot::GetFilterManager() const
156 {
157 DBG_ASSERT( mrExpData.mxFilterMgr.is(), "XclExpRoot::GetFilterManager - missing object (wrong BIFF?)" );
158 return *mrExpData.mxFilterMgr;
159 }
160
GetPivotTableManager() const161 XclExpPivotTableManager& XclExpRoot::GetPivotTableManager() const
162 {
163 DBG_ASSERT( mrExpData.mxPTableMgr.is(), "XclExpRoot::GetPivotTableManager - missing object (wrong BIFF?)" );
164 return *mrExpData.mxPTableMgr;
165 }
166
InitializeConvert()167 void XclExpRoot::InitializeConvert()
168 {
169 mrExpData.mxTabInfo.reset( new XclExpTabInfo( GetRoot() ) );
170 mrExpData.mxAddrConv.reset( new XclExpAddressConverter( GetRoot() ) );
171 mrExpData.mxFmlaComp.reset( new XclExpFormulaCompiler( GetRoot() ) );
172 mrExpData.mxProgress.reset( new XclExpProgressBar( GetRoot() ) );
173
174 GetProgressBar().Initialize();
175 }
176
InitializeGlobals()177 void XclExpRoot::InitializeGlobals()
178 {
179 SetCurrScTab( SCTAB_GLOBAL );
180
181 if( GetBiff() >= EXC_BIFF5 )
182 {
183 mrExpData.mxPalette.reset( new XclExpPalette( GetRoot() ) );
184 mrExpData.mxFontBfr.reset( new XclExpFontBuffer( GetRoot() ) );
185 mrExpData.mxNumFmtBfr.reset( new XclExpNumFmtBuffer( GetRoot() ) );
186 mrExpData.mxXFBfr.reset( new XclExpXFBuffer( GetRoot() ) );
187 mrExpData.mxGlobLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
188 mrExpData.mxNameMgr.reset( new XclExpNameManager( GetRoot() ) );
189 }
190
191 if( GetBiff() == EXC_BIFF8 )
192 {
193 mrExpData.mxSst.reset( new XclExpSst );
194 mrExpData.mxObjMgr.reset( new XclExpObjectManager( GetRoot() ) );
195 mrExpData.mxFilterMgr.reset( new XclExpFilterManager( GetRoot() ) );
196 mrExpData.mxPTableMgr.reset( new XclExpPivotTableManager( GetRoot() ) );
197 // BIFF8: only one link manager for all sheets
198 mrExpData.mxLocLinkMgr = mrExpData.mxGlobLinkMgr;
199 }
200
201 GetXFBuffer().Initialize();
202 GetNameManager().Initialize();
203 }
204
InitializeTable(SCTAB nScTab)205 void XclExpRoot::InitializeTable( SCTAB nScTab )
206 {
207 SetCurrScTab( nScTab );
208 if( GetBiff() == EXC_BIFF5 )
209 {
210 // local link manager per sheet
211 mrExpData.mxLocLinkMgr.reset( new XclExpLinkManager( GetRoot() ) );
212 }
213 }
214
InitializeSave()215 void XclExpRoot::InitializeSave()
216 {
217 GetPalette().Finalize();
218 GetXFBuffer().Finalize();
219 }
220
CreateRecord(sal_uInt16 nRecId) const221 XclExpRecordRef XclExpRoot::CreateRecord( sal_uInt16 nRecId ) const
222 {
223 XclExpRecordRef xRec;
224 switch( nRecId )
225 {
226 case EXC_ID_PALETTE: xRec = mrExpData.mxPalette; break;
227 case EXC_ID_FONTLIST: xRec = mrExpData.mxFontBfr; break;
228 case EXC_ID_FORMATLIST: xRec = mrExpData.mxNumFmtBfr; break;
229 case EXC_ID_XFLIST: xRec = mrExpData.mxXFBfr; break;
230 case EXC_ID_SST: xRec = mrExpData.mxSst; break;
231 case EXC_ID_EXTERNSHEET: xRec = GetLocalLinkMgrRef(); break;
232 case EXC_ID_NAME: xRec = mrExpData.mxNameMgr; break;
233 }
234 DBG_ASSERT( xRec.is(), "XclExpRoot::CreateRecord - unknown record ID or missing object" );
235 return xRec;
236 }
237
IsDocumentEncrypted() const238 bool XclExpRoot::IsDocumentEncrypted() const
239 {
240 // We need to encrypt the content when the document structure is protected.
241 const ScDocProtection* pDocProt = GetDoc().GetDocProtection();
242 if (pDocProt && pDocProt->isProtected() && pDocProt->isOptionEnabled(ScDocProtection::STRUCTURE))
243 return true;
244
245 if ( GetEncryptionData().getLength() > 0 )
246 // Password is entered directly into the save dialog.
247 return true;
248
249 return false;
250 }
251
GenerateEncryptionData(const::rtl::OUString & aPass) const252 uno::Sequence< beans::NamedValue > XclExpRoot::GenerateEncryptionData( const ::rtl::OUString& aPass ) const
253 {
254 uno::Sequence< beans::NamedValue > aEncryptionData;
255
256 if ( aPass.getLength() > 0 && aPass.getLength() < 16 )
257 {
258 TimeValue aTime;
259 osl_getSystemTime( &aTime );
260 rtlRandomPool aRandomPool = rtl_random_createPool ();
261 rtl_random_addBytes ( aRandomPool, &aTime, 8 );
262
263 sal_uInt8 pnDocId[16];
264 rtl_random_getBytes( aRandomPool, pnDocId, 16 );
265
266 rtl_random_destroyPool( aRandomPool );
267
268 sal_uInt16 pnPasswd[16];
269 memset( pnPasswd, 0, sizeof( pnPasswd ) );
270 for (xub_StrLen nChar = 0; nChar < aPass.getLength(); ++nChar )
271 pnPasswd[nChar] = aPass.getStr()[nChar];
272
273 ::msfilter::MSCodec_Std97 aCodec;
274 aCodec.InitKey( pnPasswd, pnDocId );
275 aEncryptionData = aCodec.GetEncryptionData();
276 }
277
278 return aEncryptionData;
279 }
280
GetEncryptionData() const281 uno::Sequence< beans::NamedValue > XclExpRoot::GetEncryptionData() const
282 {
283 uno::Sequence< beans::NamedValue > aEncryptionData;
284 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pEncryptionDataItem, SfxUnoAnyItem, SID_ENCRYPTIONDATA, sal_False );
285 if ( pEncryptionDataItem )
286 pEncryptionDataItem->GetValue() >>= aEncryptionData;
287 else
288 {
289 // try to get the encryption data from the password
290 SFX_ITEMSET_ARG( GetMedium().GetItemSet(), pPasswordItem, SfxStringItem, SID_PASSWORD, sal_False );
291 if ( pPasswordItem && pPasswordItem->GetValue().Len() )
292 aEncryptionData = GenerateEncryptionData( pPasswordItem->GetValue() );
293 }
294
295 return aEncryptionData;
296 }
297
GenerateDefaultEncryptionData() const298 uno::Sequence< beans::NamedValue > XclExpRoot::GenerateDefaultEncryptionData() const
299 {
300 uno::Sequence< beans::NamedValue > aEncryptionData;
301 if ( GetDefaultPassword().Len() > 0 )
302 aEncryptionData = GenerateEncryptionData( GetDefaultPassword() );
303
304 return aEncryptionData;
305 }
306
GetLocalLinkMgrRef() const307 XclExpRootData::XclExpLinkMgrRef XclExpRoot::GetLocalLinkMgrRef() const
308 {
309 return IsInGlobals() ? mrExpData.mxGlobLinkMgr : mrExpData.mxLocLinkMgr;
310 }
311
312 // ============================================================================
313
314