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 "workbooksettings.hxx"
21 :
22 : #include <com/sun/star/sheet/XCalculatable.hpp>
23 : #include <com/sun/star/util/Date.hpp>
24 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
25 : #include <comphelper/mediadescriptor.hxx>
26 : #include "oox/core/filterbase.hxx"
27 : #include "oox/helper/attributelist.hxx"
28 : #include "oox/helper/propertyset.hxx"
29 : #include "oox/core/xmlfilterbase.hxx"
30 : #include "oox/token/properties.hxx"
31 : #include "biffinputstream.hxx"
32 : #include "unitconverter.hxx"
33 :
34 : namespace oox {
35 : namespace xls {
36 :
37 : // ============================================================================
38 :
39 : using namespace ::com::sun::star::beans;
40 : using namespace ::com::sun::star::sheet;
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::util;
43 :
44 : using ::comphelper::MediaDescriptor;
45 : using ::oox::core::CodecHelper;
46 :
47 : // ============================================================================
48 :
49 : namespace {
50 :
51 : const sal_uInt32 BIFF12_WORKBOOKPR_DATE1904 = 0x00000001;
52 : const sal_uInt32 BIFF12_WORKBOOKPR_STRIPEXT = 0x00000080;
53 :
54 : const sal_uInt16 BIFF12_CALCPR_A1 = 0x0002;
55 : const sal_uInt16 BIFF12_CALCPR_ITERATE = 0x0004;
56 : const sal_uInt16 BIFF12_CALCPR_FULLPRECISION = 0x0008;
57 : const sal_uInt16 BIFF12_CALCPR_CALCCOMPLETED = 0x0010;
58 : const sal_uInt16 BIFF12_CALCPR_CALCONSAVE = 0x0020;
59 : const sal_uInt16 BIFF12_CALCPR_CONCURRENT = 0x0040;
60 : const sal_uInt16 BIFF12_CALCPR_MANUALPROC = 0x0080;
61 :
62 : // no predefined constants for show objects mode
63 : const sal_Int16 API_SHOWMODE_SHOW = 0; /// Show drawing objects.
64 : const sal_Int16 API_SHOWMODE_HIDE = 1; /// Hide drawing objects.
65 : const sal_Int16 API_SHOWMODE_PLACEHOLDER = 2; /// Show placeholders for drawing objects.
66 :
67 : } // namespace
68 :
69 : // ============================================================================
70 :
71 21 : FileSharingModel::FileSharingModel() :
72 : mnPasswordHash( 0 ),
73 21 : mbRecommendReadOnly( false )
74 : {
75 21 : }
76 :
77 : // ============================================================================
78 :
79 21 : WorkbookSettingsModel::WorkbookSettingsModel() :
80 : mnShowObjectMode( XML_all ),
81 : mnUpdateLinksMode( XML_userSet ),
82 : mnDefaultThemeVer( -1 ),
83 : mbDateMode1904( false ),
84 21 : mbSaveExtLinkValues( true )
85 : {
86 21 : }
87 :
88 0 : void WorkbookSettingsModel::setBiffObjectMode( sal_uInt16 nObjMode )
89 : {
90 : static const sal_Int32 spnObjModes[] = { XML_all, XML_placeholders, XML_none };
91 0 : mnShowObjectMode = STATIC_ARRAY_SELECT( spnObjModes, nObjMode, XML_all );
92 0 : }
93 :
94 : // ============================================================================
95 :
96 21 : CalcSettingsModel::CalcSettingsModel() :
97 : mfIterateDelta( 0.001 ),
98 : mnCalcId( -1 ),
99 : mnRefMode( XML_A1 ),
100 : mnCalcMode( XML_auto ),
101 : mnIterateCount( 100 ),
102 : mnProcCount( -1 ),
103 : mbCalcOnSave( true ),
104 : mbCalcCompleted( true ),
105 : mbFullPrecision( true ),
106 : mbIterate( false ),
107 : mbConcurrent( true ),
108 21 : mbUseNlr( false )
109 : {
110 21 : }
111 :
112 : // ============================================================================
113 :
114 21 : WorkbookSettings::WorkbookSettings( const WorkbookHelper& rHelper ) :
115 21 : WorkbookHelper( rHelper )
116 : {
117 21 : }
118 :
119 0 : void WorkbookSettings::importFileSharing( const AttributeList& rAttribs )
120 : {
121 0 : maFileSharing.maUserName = rAttribs.getXString( XML_userName, OUString() );
122 0 : maFileSharing.mnPasswordHash = CodecHelper::getPasswordHash( rAttribs, XML_reservationPassword );
123 0 : maFileSharing.mbRecommendReadOnly = rAttribs.getBool( XML_readOnlyRecommended, false );
124 0 : }
125 :
126 21 : void WorkbookSettings::importWorkbookPr( const AttributeList& rAttribs )
127 : {
128 21 : maBookSettings.maCodeName = rAttribs.getString( XML_codeName, OUString() );
129 21 : maBookSettings.mnShowObjectMode = rAttribs.getToken( XML_showObjects, XML_all );
130 21 : maBookSettings.mnUpdateLinksMode = rAttribs.getToken( XML_updateLinks, XML_userSet );
131 21 : maBookSettings.mnDefaultThemeVer = rAttribs.getInteger( XML_defaultThemeVersion, -1 );
132 21 : maBookSettings.mbSaveExtLinkValues = rAttribs.getBool( XML_saveExternalLinkValues, true );
133 21 : setDateMode( rAttribs.getBool( XML_date1904, false ), rAttribs.getBool( XML_dateCompatibility, true ) );
134 21 : }
135 :
136 21 : void WorkbookSettings::importCalcPr( const AttributeList& rAttribs )
137 : {
138 21 : maCalcSettings.mfIterateDelta = rAttribs.getDouble( XML_iterateDelta, 0.0001 );
139 21 : maCalcSettings.mnCalcId = rAttribs.getInteger( XML_calcId, -1 );
140 21 : maCalcSettings.mnRefMode = rAttribs.getToken( XML_refMode, XML_A1 );
141 21 : maCalcSettings.mnCalcMode = rAttribs.getToken( XML_calcMode, XML_auto );
142 21 : maCalcSettings.mnIterateCount = rAttribs.getInteger( XML_iterateCount, 100 );
143 21 : maCalcSettings.mnProcCount = rAttribs.getInteger( XML_concurrentManualCount, -1 );
144 21 : maCalcSettings.mbCalcOnSave = rAttribs.getBool( XML_calcOnSave, true );
145 21 : maCalcSettings.mbCalcCompleted = rAttribs.getBool( XML_calcCompleted, true );
146 21 : maCalcSettings.mbFullPrecision = rAttribs.getBool( XML_fullPrecision, true );
147 21 : maCalcSettings.mbIterate = rAttribs.getBool( XML_iterate, false );
148 21 : maCalcSettings.mbConcurrent = rAttribs.getBool( XML_concurrentCalc, true );
149 21 : }
150 :
151 0 : void WorkbookSettings::importFileSharing( SequenceInputStream& rStrm )
152 : {
153 0 : maFileSharing.mbRecommendReadOnly = rStrm.readuInt16() != 0;
154 0 : rStrm >> maFileSharing.mnPasswordHash >> maFileSharing.maUserName;
155 0 : }
156 :
157 0 : void WorkbookSettings::importWorkbookPr( SequenceInputStream& rStrm )
158 : {
159 : sal_uInt32 nFlags;
160 0 : rStrm >> nFlags >> maBookSettings.mnDefaultThemeVer >> maBookSettings.maCodeName;
161 0 : maBookSettings.setBiffObjectMode( extractValue< sal_uInt16 >( nFlags, 13, 2 ) );
162 : // set flag means: strip external link values
163 0 : maBookSettings.mbSaveExtLinkValues = !getFlag( nFlags, BIFF12_WORKBOOKPR_STRIPEXT );
164 0 : setDateMode( getFlag( nFlags, BIFF12_WORKBOOKPR_DATE1904 ) );
165 0 : }
166 :
167 0 : void WorkbookSettings::importCalcPr( SequenceInputStream& rStrm )
168 : {
169 : sal_Int32 nCalcMode, nProcCount;
170 : sal_uInt16 nFlags;
171 0 : rStrm >> maCalcSettings.mnCalcId >> nCalcMode >> maCalcSettings.mnIterateCount >> maCalcSettings.mfIterateDelta >> nProcCount >> nFlags;
172 :
173 : static const sal_Int32 spnCalcModes[] = { XML_manual, XML_auto, XML_autoNoTable };
174 0 : maCalcSettings.mnRefMode = getFlagValue( nFlags, BIFF12_CALCPR_A1, XML_A1, XML_R1C1 );
175 0 : maCalcSettings.mnCalcMode = STATIC_ARRAY_SELECT( spnCalcModes, nCalcMode, XML_auto );
176 0 : maCalcSettings.mnProcCount = getFlagValue< sal_Int32 >( nFlags, BIFF12_CALCPR_MANUALPROC, nProcCount, -1 );
177 0 : maCalcSettings.mbCalcOnSave = getFlag( nFlags, BIFF12_CALCPR_CALCONSAVE );
178 0 : maCalcSettings.mbCalcCompleted = getFlag( nFlags, BIFF12_CALCPR_CALCCOMPLETED );
179 0 : maCalcSettings.mbFullPrecision = getFlag( nFlags, BIFF12_CALCPR_FULLPRECISION );
180 0 : maCalcSettings.mbIterate = getFlag( nFlags, BIFF12_CALCPR_ITERATE );
181 0 : maCalcSettings.mbConcurrent = getFlag( nFlags, BIFF12_CALCPR_CONCURRENT );
182 0 : }
183 :
184 21 : void WorkbookSettings::finalizeImport()
185 : {
186 : // default settings
187 21 : PropertySet aPropSet( getDocument() );
188 21 : switch( getFilterType() )
189 : {
190 : case FILTER_OOXML:
191 : case FILTER_BIFF:
192 21 : aPropSet.setProperty( PROP_IgnoreCase, true ); // always in Excel
193 21 : aPropSet.setProperty( PROP_RegularExpressions, false ); // not supported in Excel
194 21 : break;
195 : case FILTER_UNKNOWN:
196 0 : break;
197 : }
198 :
199 : // write protection
200 21 : if( maFileSharing.mbRecommendReadOnly || (maFileSharing.mnPasswordHash != 0) ) try
201 : {
202 0 : getBaseFilter().getMediaDescriptor()[ "ReadOnly" ] <<= true;
203 :
204 0 : Reference< XPropertySet > xDocumentSettings( getBaseFilter().getModelFactory()->createInstance(
205 0 : "com.sun.star.document.Settings" ), UNO_QUERY_THROW );
206 0 : PropertySet aSettingsProp( xDocumentSettings );
207 0 : if( maFileSharing.mbRecommendReadOnly )
208 0 : aSettingsProp.setProperty( PROP_LoadReadonly, true );
209 : // if( maFileSharing.mnPasswordHash != 0 )
210 : // aSettingsProp.setProperty( PROP_ModifyPasswordHash, static_cast< sal_Int32 >( maFileSharing.mnPasswordHash ) );
211 : }
212 0 : catch( Exception& )
213 : {
214 : }
215 :
216 : // calculation settings
217 21 : Date aNullDate = getNullDate();
218 :
219 21 : aPropSet.setProperty( PROP_NullDate, aNullDate );
220 21 : aPropSet.setProperty( PROP_IsIterationEnabled, maCalcSettings.mbIterate );
221 21 : aPropSet.setProperty( PROP_IterationCount, maCalcSettings.mnIterateCount );
222 21 : aPropSet.setProperty( PROP_IterationEpsilon, maCalcSettings.mfIterateDelta );
223 21 : aPropSet.setProperty( PROP_CalcAsShown, !maCalcSettings.mbFullPrecision );
224 21 : aPropSet.setProperty( PROP_LookUpLabels, maCalcSettings.mbUseNlr );
225 :
226 21 : Reference< XNumberFormatsSupplier > xNumFmtsSupp( getDocument(), UNO_QUERY );
227 21 : if( xNumFmtsSupp.is() )
228 : {
229 21 : PropertySet aNumFmtProp( xNumFmtsSupp->getNumberFormatSettings() );
230 21 : aNumFmtProp.setProperty( PROP_NullDate, aNullDate );
231 : }
232 :
233 42 : Reference< XCalculatable > xCalculatable( getDocument(), UNO_QUERY );
234 21 : if( xCalculatable.is() )
235 21 : xCalculatable->enableAutomaticCalculation( (maCalcSettings.mnCalcMode == XML_auto) || (maCalcSettings.mnCalcMode == XML_autoNoTable) );
236 :
237 : // VBA code name
238 42 : aPropSet.setProperty( PROP_CodeName, maBookSettings.maCodeName );
239 21 : }
240 :
241 21 : sal_Int16 WorkbookSettings::getApiShowObjectMode() const
242 : {
243 21 : switch( maBookSettings.mnShowObjectMode )
244 : {
245 21 : case XML_all: return API_SHOWMODE_SHOW;
246 0 : case XML_none: return API_SHOWMODE_HIDE;
247 : // #i80528# placeholders not supported anymore, but this is handled internally in Calc
248 0 : case XML_placeholders: return API_SHOWMODE_PLACEHOLDER;
249 : }
250 0 : return API_SHOWMODE_SHOW;
251 : }
252 :
253 42 : Date WorkbookSettings::getNullDate() const
254 : {
255 42 : static const Date saDate1900 ( 30, 12, 1899 );
256 42 : static const Date saDate1904 ( 1, 1, 1904 );
257 42 : static const Date saDateBackCompatibility1900( 31, 12, 1899 );
258 :
259 42 : if( getOoxFilter().getVersion() == oox::core::ISOIEC_29500_2008 )
260 : {
261 0 : if( !maBookSettings.mbDateCompatibility )
262 0 : return saDate1900;
263 :
264 : return maBookSettings.mbDateMode1904 ? saDate1904 :
265 0 : saDateBackCompatibility1900;
266 : }
267 :
268 42 : return maBookSettings.mbDateMode1904 ? saDate1904 : saDate1900;
269 : }
270 :
271 21 : void WorkbookSettings::setDateMode( bool bDateMode1904, bool bDateCompatibility )
272 : {
273 21 : maBookSettings.mbDateMode1904 = bDateMode1904;
274 21 : maBookSettings.mbDateCompatibility = bDateCompatibility;
275 :
276 21 : getUnitConverter().finalizeNullDate( getNullDate() );
277 21 : }
278 :
279 : // ============================================================================
280 :
281 : } // namespace xls
282 15 : } // namespace oox
283 :
284 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|