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 
30 #include <oox/core/binarycodec.hxx>
31 #include <oox/core/filterbase.hxx>
36 #include <oox/token/properties.hxx>
37 #include <oox/token/tokens.hxx>
38 #include <unitconverter.hxx>
39 #include <biffhelper.hxx>
40 
41 namespace oox::xls {
42 
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sheet;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::util;
47 
48 namespace {
49 
50 const sal_uInt32 BIFF12_WORKBOOKPR_DATE1904 = 0x00000001;
51 const sal_uInt32 BIFF12_WORKBOOKPR_STRIPEXT = 0x00000080;
52 
53 const sal_uInt16 BIFF12_CALCPR_A1 = 0x0002;
54 const sal_uInt16 BIFF12_CALCPR_ITERATE = 0x0004;
55 const sal_uInt16 BIFF12_CALCPR_FULLPRECISION = 0x0008;
56 const sal_uInt16 BIFF12_CALCPR_CALCCOMPLETED = 0x0010;
57 const sal_uInt16 BIFF12_CALCPR_CALCONSAVE = 0x0020;
58 const sal_uInt16 BIFF12_CALCPR_CONCURRENT = 0x0040;
59 const sal_uInt16 BIFF12_CALCPR_MANUALPROC = 0x0080;
60 
61 // no predefined constants for show objects mode
62 const sal_Int16 API_SHOWMODE_SHOW = 0;
63 const sal_Int16 API_SHOWMODE_HIDE = 1;
64 const 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 
85 void WorkbookSettingsModel::setBiffObjectMode( sal_uInt16 nObjMode )
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 {
151  rStrm >> maFileSharing.maUserName;
152 }
153 
155 {
156  sal_uInt32 nFlags;
157  nFlags = rStrm.readuInt32();
159  rStrm >> maBookSettings.maCodeName;
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;
171  nCalcMode = rStrm.readInt32();
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 
226  if( maFileSharing.mnPasswordHash != 0 )
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 
270 css::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 
288 void 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: */
Helper class to provide access to global workbook data.
OUString maAlgorithmName
User who added the write protection password.
bool mbConcurrent
WorkbookSettings(const WorkbookHelper &rHelper)
CalcSettingsModel()
True = concurrent calculation enabled.
OptValue< bool > getBool(sal_Int32 nAttrToken) const
sal_Int16 getApiShowObjectMode() const
Returns the show objects mode (considered a view setting in Calc).
UnitConverter & getUnitConverter() const
Returns the measurement unit converter.
bool mbRecommendReadOnly
Hash value of the write protection password. (unrelated to the above)
OptValue< OUString > getXString(sal_Int32 nAttrToken) const
void importWorkbookPr(const AttributeList &rAttribs)
Imports the workbookPr element containing global workbook settings.
bool mbCalcCompleted
True = always recalculate formulas before save.
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
bool mbConcurrent
True = allow circular references.
void setBiffObjectMode(sal_uInt16 nObjMode)
Sets BIFF object visibility mode.
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
CalcSettingsModel maCalcSettings
sal_uInt32 mnSpinCount
Salt value to be prepended to the password, base-64 encoded.
void importCalcPr(const AttributeList &rAttribs)
Imports the calcPr element containing workbook calculation settings.
OptValue< double > getDouble(sal_Int32 nAttrToken) const
void setDateMode(bool bDateMode1904, bool bDateCompatibility=true)
Updates date mode and unit converter nulldate.
sal_uInt32 readuInt32()
#define STATIC_ARRAY_SELECT(array, index, def)
OptValue< OUString > getString(sal_Int32 nAttrToken) const
bool mbIterate
True = use full precision on calculation.
OOX_DLLPUBLIC sal_uInt16 getPasswordHash(const AttributeList &rAttribs, sal_Int32 nElement)
utl::MediaDescriptor & getMediaDescriptor() const
sal_uInt16 readuInt16()
::oox::core::FilterBase & getBaseFilter() const
Returns the base filter object (base class of all filters).
OUString maHashValue
Algorithm name, "SHA-512", "SHA-1", ...
FileSharingModel()
True = recommend read-only mode on opening.
void finalizeNullDate(const css::util::Date &rNullDate)
Updates internal nulldate for date/serial conversion.
sal_Int16 mnCalcMode
OptValue< sal_uInt32 > getUnsigned(sal_Int32 nAttrToken) const
bool getFlag(Type nBitField, Type nMask)
sal_uInt16 mnPasswordHash
Spin count, iterations to run algorithm.
bool mbDateCompatibility
True = null date is 1904-01-01.
WorkbookSettingsModel()
True = save cached cell values for external links.
WorkbookSettingsModel maBookSettings
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
sal_Int32 mnDefaultThemeVer
Specifies how external links are updated.
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
::oox::core::XmlFilterBase & getOoxFilter() const
Returns the base OOXML/BIFF12 filter object.
css::util::Date const & getNullDate() const
Returns the nulldate of this workbook.
sal_Int32 mnIterateCount
Automatic or manual recalculation.
sal_Int32 mnRefMode
Calculation engine identifier.
OUString maSaltValue
Hash value computed by the algorithm, base-64 encoded.
sal_Int32 mnShowObjectMode
VBA codename for the workbook.
sal_Int32 mnCalcId
Minimum change in circular references.
ReturnType getFlagValue(Type nBitField, Type nMask, ReturnType nSet, ReturnType nUnset)
sal_Int32 mnUpdateLinksMode
Specifies how objects are shown.
bool mbFullPrecision
True = formulas have been recalculated before save.
XML_none
void importFileSharing(const AttributeList &rAttribs)
Imports the fileSharing element containing write protection settings.
bool mbSaveExtLinkValues
False = null date is 1899-12-30.
sal_Int32 mnProcCount
Number of iterations in circular references.
sal_Int32 mnCalcMode
Cell reference mode: A1 or R1C1.
void finalizeImport()
Converts the imported workbook settings.
bool mbCalcOnSave
Number of processors for concurrent calculation.
bool setProperty(sal_Int32 nPropId, const Type &rValue)
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const
bool m_bDetectedRangeSegmentation false
bool mbDateMode1904
Default theme version.