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