LibreOffice Module sc (master) 1
workbooksettings.cxx
Go to the documentation of this file.
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/sheet/XSpreadsheetDocument.hpp>
24#include <com/sun/star/util/Date.hpp>
25#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27
36#include <oox/token/properties.hxx>
37#include <oox/token/tokens.hxx>
38#include <unitconverter.hxx>
39#include <biffhelper.hxx>
40
41namespace oox::xls {
42
43using namespace ::com::sun::star::beans;
44using namespace ::com::sun::star::sheet;
45using namespace ::com::sun::star::uno;
46using namespace ::com::sun::star::util;
47
48namespace {
49
50const sal_uInt32 BIFF12_WORKBOOKPR_DATE1904 = 0x00000001;
51const sal_uInt32 BIFF12_WORKBOOKPR_STRIPEXT = 0x00000080;
52
53const sal_uInt16 BIFF12_CALCPR_A1 = 0x0002;
54const sal_uInt16 BIFF12_CALCPR_ITERATE = 0x0004;
55const sal_uInt16 BIFF12_CALCPR_FULLPRECISION = 0x0008;
56const sal_uInt16 BIFF12_CALCPR_CALCCOMPLETED = 0x0010;
57const sal_uInt16 BIFF12_CALCPR_CALCONSAVE = 0x0020;
58const sal_uInt16 BIFF12_CALCPR_CONCURRENT = 0x0040;
59const sal_uInt16 BIFF12_CALCPR_MANUALPROC = 0x0080;
60
61// no predefined constants for show objects mode
62const sal_Int16 API_SHOWMODE_SHOW = 0;
63const sal_Int16 API_SHOWMODE_HIDE = 1;
64const sal_Int16 API_SHOWMODE_PLACEHOLDER = 2;
65
66} // namespace
67
69 mnSpinCount( 0 ),
70 mnPasswordHash( 0 ),
71 mbRecommendReadOnly( false )
72{
73}
74
76 mnShowObjectMode( XML_all ),
77 mnUpdateLinksMode( XML_userSet ),
78 mnDefaultThemeVer( -1 ),
79 mbDateMode1904( false ),
80 mbDateCompatibility ( false ),
81 mbSaveExtLinkValues( true )
82{
83}
84
86{
87 static const sal_Int32 spnObjModes[] = { XML_all, XML_placeholders, XML_none };
88 mnShowObjectMode = STATIC_ARRAY_SELECT( spnObjModes, nObjMode, XML_all );
89}
90
92 mfIterateDelta( 0.001 ),
93 mnCalcId( -1 ),
94 mnRefMode( XML_A1 ),
95 mnCalcMode( XML_auto ),
96 mnIterateCount( 100 ),
97 mnProcCount( -1 ),
98 mbCalcOnSave( true ),
99 mbCalcCompleted( true ),
100 mbFullPrecision( true ),
101 mbIterate( false ),
102 mbConcurrent( true )
103{
104}
105
107 WorkbookHelper( rHelper )
108{
109}
110
112{
113 maFileSharing.maUserName = rAttribs.getXString( XML_userName, OUString() );
114 maFileSharing.maAlgorithmName = rAttribs.getString( XML_algorithmName, OUString());
115 maFileSharing.maHashValue = rAttribs.getString( XML_hashValue, OUString());
116 maFileSharing.maSaltValue = rAttribs.getString( XML_saltValue, OUString());
117 maFileSharing.mnSpinCount = rAttribs.getUnsigned( XML_spinCount, 0);
118 maFileSharing.mnPasswordHash = oox::core::CodecHelper::getPasswordHash( rAttribs, XML_reservationPassword );
119 maFileSharing.mbRecommendReadOnly = rAttribs.getBool( XML_readOnlyRecommended, false );
120}
121
123{
124 maBookSettings.maCodeName = rAttribs.getString( XML_codeName, OUString() );
125 maBookSettings.mnShowObjectMode = rAttribs.getToken( XML_showObjects, XML_all );
126 maBookSettings.mnUpdateLinksMode = rAttribs.getToken( XML_updateLinks, XML_userSet );
127 maBookSettings.mnDefaultThemeVer = rAttribs.getInteger( XML_defaultThemeVersion, -1 );
128 maBookSettings.mbSaveExtLinkValues = rAttribs.getBool( XML_saveExternalLinkValues, true );
129 setDateMode( rAttribs.getBool( XML_date1904, false ), rAttribs.getBool( XML_dateCompatibility, true ) );
130}
131
133{
134 maCalcSettings.mfIterateDelta = rAttribs.getDouble( XML_iterateDelta, 0.0001 );
135 maCalcSettings.mnCalcId = rAttribs.getInteger( XML_calcId, -1 );
136 maCalcSettings.mnRefMode = rAttribs.getToken( XML_refMode, XML_A1 );
137 maCalcSettings.mnCalcMode = rAttribs.getToken( XML_calcMode, XML_auto );
138 maCalcSettings.mnIterateCount = rAttribs.getInteger( XML_iterateCount, 100 );
139 maCalcSettings.mnProcCount = rAttribs.getInteger( XML_concurrentManualCount, -1 );
140 maCalcSettings.mbCalcOnSave = rAttribs.getBool( XML_calcOnSave, true );
141 maCalcSettings.mbCalcCompleted = rAttribs.getBool( XML_calcCompleted, true );
142 maCalcSettings.mbFullPrecision = rAttribs.getBool( XML_fullPrecision, true );
143 maCalcSettings.mbIterate = rAttribs.getBool( XML_iterate, false );
144 maCalcSettings.mbConcurrent = rAttribs.getBool( XML_concurrentCalc, true );
145}
146
148{
149 maFileSharing.mbRecommendReadOnly = rStrm.readuInt16() != 0;
150 maFileSharing.mnPasswordHash = rStrm.readuInt16();
152}
153
155{
156 sal_uInt32 nFlags;
157 nFlags = rStrm.readuInt32();
160 maBookSettings.setBiffObjectMode( extractValue< sal_uInt16 >( nFlags, 13, 2 ) );
161 // set flag means: strip external link values
162 maBookSettings.mbSaveExtLinkValues = !getFlag( nFlags, BIFF12_WORKBOOKPR_STRIPEXT );
163 setDateMode( getFlag( nFlags, BIFF12_WORKBOOKPR_DATE1904 ) );
164}
165
167{
168 sal_Int32 nCalcMode, nProcCount;
169 sal_uInt16 nFlags;
170 maCalcSettings.mnCalcId = rStrm.readInt32();
171 nCalcMode = rStrm.readInt32();
172 maCalcSettings.mnIterateCount = rStrm.readInt32();
173 maCalcSettings.mfIterateDelta = rStrm.readDouble();
174 nProcCount = rStrm.readInt32();
175 nFlags = rStrm.readuInt16();
176
177 static const sal_Int32 spnCalcModes[] = { XML_manual, XML_auto, XML_autoNoTable };
178 maCalcSettings.mnRefMode = getFlagValue( nFlags, BIFF12_CALCPR_A1, XML_A1, XML_R1C1 );
179 maCalcSettings.mnCalcMode = STATIC_ARRAY_SELECT( spnCalcModes, nCalcMode, XML_auto );
180 maCalcSettings.mnProcCount = getFlagValue< sal_Int32 >( nFlags, BIFF12_CALCPR_MANUALPROC, nProcCount, -1 );
181 maCalcSettings.mbCalcOnSave = getFlag( nFlags, BIFF12_CALCPR_CALCONSAVE );
182 maCalcSettings.mbCalcCompleted = getFlag( nFlags, BIFF12_CALCPR_CALCCOMPLETED );
183 maCalcSettings.mbFullPrecision = getFlag( nFlags, BIFF12_CALCPR_FULLPRECISION );
184 maCalcSettings.mbIterate = getFlag( nFlags, BIFF12_CALCPR_ITERATE );
185 maCalcSettings.mbConcurrent = getFlag( nFlags, BIFF12_CALCPR_CONCURRENT );
186}
187
189{
190 // default settings
191 PropertySet aPropSet( getDocument() );
192 aPropSet.setProperty( PROP_IgnoreCase, true ); // always in Excel
193 aPropSet.setProperty( PROP_RegularExpressions, false ); // not supported in Excel
194 aPropSet.setProperty( PROP_Wildcards, true ); // always in Excel
195
196 // write protection
198 !maFileSharing.maHashValue.isEmpty()) try
199 {
200 getBaseFilter().getMediaDescriptor()[ "ReadOnly" ] <<= true;
201
202 Reference< XPropertySet > xDocumentSettings( getBaseFilter().getModelFactory()->createInstance(
203 "com.sun.star.document.Settings" ), UNO_QUERY_THROW );
204 PropertySet aSettingsProp( xDocumentSettings );
205
206 /* TODO: not setting read-only if only mnPasswordHash ('password'
207 * attribute) is present looks a bit silly, any reason for that?
208 * 'readOnlyRecommended' is defined as "indicates on open, whether the
209 * application alerts the user that the file be marked as read-only",
210 * which sounds silly in itself and seems not to be present if the
211 * 'password' attribute isn't present, but... */
213 aSettingsProp.setProperty( PROP_LoadReadonly, true );
214
215 if (!maFileSharing.maHashValue.isEmpty())
216 {
217 Sequence<PropertyValue> aResult{
222 };
223 aSettingsProp.setProperty(PROP_ModifyPasswordInfo, aResult);
224 }
225
227 aSettingsProp.setProperty( PROP_ModifyPasswordHash, static_cast< sal_Int32 >( maFileSharing.mnPasswordHash ) );
228 }
229 catch( Exception& )
230 {
231 }
232
233 // calculation settings
234 css::util::Date aNullDate = getNullDate();
235
236 aPropSet.setProperty( PROP_NullDate, aNullDate );
237 aPropSet.setProperty( PROP_IsIterationEnabled, maCalcSettings.mbIterate );
238 aPropSet.setProperty( PROP_IterationCount, maCalcSettings.mnIterateCount );
239 aPropSet.setProperty( PROP_IterationEpsilon, maCalcSettings.mfIterateDelta );
240 aPropSet.setProperty( PROP_CalcAsShown, !maCalcSettings.mbFullPrecision );
241 aPropSet.setProperty( PROP_LookUpLabels, false );
242
243 Reference< XNumberFormatsSupplier > xNumFmtsSupp( getDocument(), UNO_QUERY );
244 if( xNumFmtsSupp.is() )
245 {
246 PropertySet aNumFmtProp( xNumFmtsSupp->getNumberFormatSettings() );
247 aNumFmtProp.setProperty( PROP_NullDate, aNullDate );
248 }
249
250 Reference< XCalculatable > xCalculatable( getDocument(), UNO_QUERY );
251 if( xCalculatable.is() )
252 xCalculatable->enableAutomaticCalculation( (maCalcSettings.mnCalcMode == XML_auto) || (maCalcSettings.mnCalcMode == XML_autoNoTable) );
253
254 // VBA code name
255 aPropSet.setProperty( PROP_CodeName, maBookSettings.maCodeName );
256}
257
259{
261 {
262 case XML_all: return API_SHOWMODE_SHOW;
263 case XML_none: return API_SHOWMODE_HIDE;
264 // #i80528# placeholders not supported anymore, but this is handled internally in Calc
265 case XML_placeholders: return API_SHOWMODE_PLACEHOLDER;
266 }
267 return API_SHOWMODE_SHOW;
268}
269
270css::util::Date const & WorkbookSettings::getNullDate() const
271{
272 static const css::util::Date saDate1900 ( 30, 12, 1899 );
273 static const css::util::Date saDate1904 ( 1, 1, 1904 );
274 static const css::util::Date saDateBackCompatibility1900( 31, 12, 1899 );
275
276 if( getOoxFilter().getVersion() == oox::core::ISOIEC_29500_2008 )
277 {
279 return saDate1900;
280
281 return maBookSettings.mbDateMode1904 ? saDate1904 :
282 saDateBackCompatibility1900;
283 }
284
285 return maBookSettings.mbDateMode1904 ? saDate1904 : saDate1900;
286}
287
288void WorkbookSettings::setDateMode( bool bDateMode1904, bool bDateCompatibility )
289{
290 maBookSettings.mbDateMode1904 = bDateMode1904;
291 maBookSettings.mbDateCompatibility = bDateCompatibility;
292
294}
295
296} // namespace oox::xls
297
298/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
sal_Int16 mnCalcMode
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
std::optional< sal_uInt32 > getUnsigned(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::optional< OUString > getString(sal_Int32 nAttrToken) const
std::optional< bool > getBool(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
std::optional< double > getDouble(sal_Int32 nAttrToken) const
bool setProperty(sal_Int32 nPropId, const Type &rValue)
utl::MediaDescriptor & getMediaDescriptor() const
void finalizeNullDate(const css::util::Date &rNullDate)
Updates internal nulldate for date/serial conversion.
Helper class to provide access to global workbook data.
::oox::core::XmlFilterBase & getOoxFilter() const
Returns the base OOXML/BIFF12 filter object.
UnitConverter & getUnitConverter() const
Returns the measurement unit converter.
::oox::core::FilterBase & getBaseFilter() const
Returns the base filter object (base class of all filters).
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
sal_Int16 getApiShowObjectMode() const
Returns the show objects mode (considered a view setting in Calc).
WorkbookSettingsModel maBookSettings
void finalizeImport()
Converts the imported workbook settings.
CalcSettingsModel maCalcSettings
void importCalcPr(const AttributeList &rAttribs)
Imports the calcPr element containing workbook calculation settings.
void setDateMode(bool bDateMode1904, bool bDateCompatibility=true)
Updates date mode and unit converter nulldate.
WorkbookSettings(const WorkbookHelper &rHelper)
void importFileSharing(const AttributeList &rAttribs)
Imports the fileSharing element containing write protection settings.
css::util::Date const & getNullDate() const
Returns the nulldate of this workbook.
void importWorkbookPr(const AttributeList &rAttribs)
Imports the workbookPr element containing global workbook settings.
#define STATIC_ARRAY_SELECT(array, index, def)
@ Exception
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
void SvStream & rStrm
OOX_DLLPUBLIC sal_uInt16 getPasswordHash(const AttributeList &rAttribs, sal_Int32 nElement)
ReturnType getFlagValue(Type nBitField, Type nMask, ReturnType nSet, ReturnType nUnset)
bool getFlag(Type nBitField, Type nMask)
XML_none
sal_Int32 mnCalcId
Minimum change in circular references.
bool mbCalcOnSave
Number of processors for concurrent calculation.
sal_Int32 mnRefMode
Calculation engine identifier.
sal_Int32 mnProcCount
Number of iterations in circular references.
sal_Int32 mnIterateCount
Automatic or manual recalculation.
bool mbConcurrent
True = allow circular references.
bool mbCalcCompleted
True = always recalculate formulas before save.
bool mbIterate
True = use full precision on calculation.
CalcSettingsModel()
True = concurrent calculation enabled.
sal_Int32 mnCalcMode
Cell reference mode: A1 or R1C1.
bool mbFullPrecision
True = formulas have been recalculated before save.
OUString maAlgorithmName
User who added the write protection password.
OUString maSaltValue
Hash value computed by the algorithm, base-64 encoded.
sal_uInt32 mnSpinCount
Salt value to be prepended to the password, base-64 encoded.
OUString maHashValue
Algorithm name, "SHA-512", "SHA-1", ...
sal_uInt16 mnPasswordHash
Spin count, iterations to run algorithm.
bool mbRecommendReadOnly
Hash value of the write protection password. (unrelated to the above)
FileSharingModel()
True = recommend read-only mode on opening.
void setBiffObjectMode(sal_uInt16 nObjMode)
Sets BIFF object visibility mode.
sal_Int32 mnDefaultThemeVer
Specifies how external links are updated.
bool mbDateCompatibility
True = null date is 1904-01-01.
WorkbookSettingsModel()
True = save cached cell values for external links.
sal_Int32 mnUpdateLinksMode
Specifies how objects are shown.
sal_Int32 mnShowObjectMode
VBA codename for the workbook.
bool mbDateMode1904
Default theme version.
bool mbSaveExtLinkValues
False = null date is 1899-12-30.
bool mbConcurrent