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>
28 #include <oox/core/binarycodec.hxx>
29 #include <oox/core/filterbase.hxx>
34 #include <oox/token/properties.hxx>
35 #include <oox/token/tokens.hxx>
36 #include <unitconverter.hxx>
37 #include <biffhelper.hxx>
38 
39 namespace oox::xls {
40 
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::sheet;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::util;
45 
46 namespace {
47 
48 const sal_uInt32 BIFF12_WORKBOOKPR_DATE1904 = 0x00000001;
49 const sal_uInt32 BIFF12_WORKBOOKPR_STRIPEXT = 0x00000080;
50 
51 const sal_uInt16 BIFF12_CALCPR_A1 = 0x0002;
52 const sal_uInt16 BIFF12_CALCPR_ITERATE = 0x0004;
53 const sal_uInt16 BIFF12_CALCPR_FULLPRECISION = 0x0008;
54 const sal_uInt16 BIFF12_CALCPR_CALCCOMPLETED = 0x0010;
55 const sal_uInt16 BIFF12_CALCPR_CALCONSAVE = 0x0020;
56 const sal_uInt16 BIFF12_CALCPR_CONCURRENT = 0x0040;
57 const sal_uInt16 BIFF12_CALCPR_MANUALPROC = 0x0080;
58 
59 // no predefined constants for show objects mode
60 const sal_Int16 API_SHOWMODE_SHOW = 0;
61 const sal_Int16 API_SHOWMODE_HIDE = 1;
62 const sal_Int16 API_SHOWMODE_PLACEHOLDER = 2;
63 
64 } // namespace
65 
67  mnSpinCount( 0 ),
68  mnPasswordHash( 0 ),
69  mbRecommendReadOnly( false )
70 {
71 }
72 
74  mnShowObjectMode( XML_all ),
75  mnUpdateLinksMode( XML_userSet ),
76  mnDefaultThemeVer( -1 ),
77  mbDateMode1904( false ),
78  mbDateCompatibility ( false ),
79  mbSaveExtLinkValues( true )
80 {
81 }
82 
83 void WorkbookSettingsModel::setBiffObjectMode( sal_uInt16 nObjMode )
84 {
85  static const sal_Int32 spnObjModes[] = { XML_all, XML_placeholders, XML_none };
86  mnShowObjectMode = STATIC_ARRAY_SELECT( spnObjModes, nObjMode, XML_all );
87 }
88 
90  mfIterateDelta( 0.001 ),
91  mnCalcId( -1 ),
92  mnRefMode( XML_A1 ),
93  mnCalcMode( XML_auto ),
94  mnIterateCount( 100 ),
95  mnProcCount( -1 ),
96  mbCalcOnSave( true ),
97  mbCalcCompleted( true ),
98  mbFullPrecision( true ),
99  mbIterate( false ),
100  mbConcurrent( true )
101 {
102 }
103 
105  WorkbookHelper( rHelper )
106 {
107 }
108 
110 {
111  maFileSharing.maUserName = rAttribs.getXString( XML_userName, OUString() );
112  maFileSharing.maAlgorithmName = rAttribs.getString( XML_algorithmName, OUString());
113  maFileSharing.maHashValue = rAttribs.getString( XML_hashValue, OUString());
114  maFileSharing.maSaltValue = rAttribs.getString( XML_saltValue, OUString());
115  maFileSharing.mnSpinCount = rAttribs.getUnsigned( XML_spinCount, 0);
116  maFileSharing.mnPasswordHash = oox::core::CodecHelper::getPasswordHash( rAttribs, XML_reservationPassword );
117  maFileSharing.mbRecommendReadOnly = rAttribs.getBool( XML_readOnlyRecommended, false );
118 }
119 
121 {
122  maBookSettings.maCodeName = rAttribs.getString( XML_codeName, OUString() );
123  maBookSettings.mnShowObjectMode = rAttribs.getToken( XML_showObjects, XML_all );
124  maBookSettings.mnUpdateLinksMode = rAttribs.getToken( XML_updateLinks, XML_userSet );
125  maBookSettings.mnDefaultThemeVer = rAttribs.getInteger( XML_defaultThemeVersion, -1 );
126  maBookSettings.mbSaveExtLinkValues = rAttribs.getBool( XML_saveExternalLinkValues, true );
127  setDateMode( rAttribs.getBool( XML_date1904, false ), rAttribs.getBool( XML_dateCompatibility, true ) );
128 }
129 
131 {
132  maCalcSettings.mfIterateDelta = rAttribs.getDouble( XML_iterateDelta, 0.0001 );
133  maCalcSettings.mnCalcId = rAttribs.getInteger( XML_calcId, -1 );
134  maCalcSettings.mnRefMode = rAttribs.getToken( XML_refMode, XML_A1 );
135  maCalcSettings.mnCalcMode = rAttribs.getToken( XML_calcMode, XML_auto );
136  maCalcSettings.mnIterateCount = rAttribs.getInteger( XML_iterateCount, 100 );
137  maCalcSettings.mnProcCount = rAttribs.getInteger( XML_concurrentManualCount, -1 );
138  maCalcSettings.mbCalcOnSave = rAttribs.getBool( XML_calcOnSave, true );
139  maCalcSettings.mbCalcCompleted = rAttribs.getBool( XML_calcCompleted, true );
140  maCalcSettings.mbFullPrecision = rAttribs.getBool( XML_fullPrecision, true );
141  maCalcSettings.mbIterate = rAttribs.getBool( XML_iterate, false );
142  maCalcSettings.mbConcurrent = rAttribs.getBool( XML_concurrentCalc, true );
143 }
144 
146 {
149  rStrm >> maFileSharing.maUserName;
150 }
151 
153 {
154  sal_uInt32 nFlags;
155  nFlags = rStrm.readuInt32();
157  rStrm >> maBookSettings.maCodeName;
158  maBookSettings.setBiffObjectMode( extractValue< sal_uInt16 >( nFlags, 13, 2 ) );
159  // set flag means: strip external link values
160  maBookSettings.mbSaveExtLinkValues = !getFlag( nFlags, BIFF12_WORKBOOKPR_STRIPEXT );
161  setDateMode( getFlag( nFlags, BIFF12_WORKBOOKPR_DATE1904 ) );
162 }
163 
165 {
166  sal_Int32 nCalcMode, nProcCount;
167  sal_uInt16 nFlags;
169  nCalcMode = rStrm.readInt32();
172  nProcCount = rStrm.readInt32();
173  nFlags = rStrm.readuInt16();
174 
175  static const sal_Int32 spnCalcModes[] = { XML_manual, XML_auto, XML_autoNoTable };
176  maCalcSettings.mnRefMode = getFlagValue( nFlags, BIFF12_CALCPR_A1, XML_A1, XML_R1C1 );
177  maCalcSettings.mnCalcMode = STATIC_ARRAY_SELECT( spnCalcModes, nCalcMode, XML_auto );
178  maCalcSettings.mnProcCount = getFlagValue< sal_Int32 >( nFlags, BIFF12_CALCPR_MANUALPROC, nProcCount, -1 );
179  maCalcSettings.mbCalcOnSave = getFlag( nFlags, BIFF12_CALCPR_CALCONSAVE );
180  maCalcSettings.mbCalcCompleted = getFlag( nFlags, BIFF12_CALCPR_CALCCOMPLETED );
181  maCalcSettings.mbFullPrecision = getFlag( nFlags, BIFF12_CALCPR_FULLPRECISION );
182  maCalcSettings.mbIterate = getFlag( nFlags, BIFF12_CALCPR_ITERATE );
183  maCalcSettings.mbConcurrent = getFlag( nFlags, BIFF12_CALCPR_CONCURRENT );
184 }
185 
187 {
188  // default settings
189  PropertySet aPropSet( getDocument() );
190  aPropSet.setProperty( PROP_IgnoreCase, true ); // always in Excel
191  aPropSet.setProperty( PROP_RegularExpressions, false ); // not supported in Excel
192  aPropSet.setProperty( PROP_Wildcards, true ); // always in Excel
193 
194  // write protection
196  !maFileSharing.maHashValue.isEmpty()) try
197  {
198  getBaseFilter().getMediaDescriptor()[ "ReadOnly" ] <<= true;
199 
200  Reference< XPropertySet > xDocumentSettings( getBaseFilter().getModelFactory()->createInstance(
201  "com.sun.star.document.Settings" ), UNO_QUERY_THROW );
202  PropertySet aSettingsProp( xDocumentSettings );
203 
204  /* TODO: not setting read-only if only mnPasswordHash ('password'
205  * attribute) is present looks a bit silly, any reason for that?
206  * 'readOnlyRecommended' is defined as "indicates on open, whether the
207  * application alerts the user that the file be marked as read-only",
208  * which sounds silly in itself and seems not to be present if the
209  * 'password' attribute isn't present, but... */
211  aSettingsProp.setProperty( PROP_LoadReadonly, true );
212 
213  /* TODO: setting ModifyPasswordHash was commented out with commit
214  * 1a842832cd174d5ccfd832fdb94c93ae42e8eacc of which the filter
215  * fragment parts meanwhile contain PASSWORDTOMODIFY again, also for
216  * calc_OOXML.xcu, but setting the property doesn't raise a dialog. If
217  * it worked, a Sequence<PropertyValue> should be used for
218  * algorithmName,hashValue,... see also
219  * SfxObjectShell::SetModifyPasswordInfo() */
220 // if( maFileSharing.mnPasswordHash != 0 )
221 // aSettingsProp.setProperty( PROP_ModifyPasswordHash, static_cast< sal_Int32 >( maFileSharing.mnPasswordHash ) );
222  }
223  catch( Exception& )
224  {
225  }
226 
227  // calculation settings
228  css::util::Date aNullDate = getNullDate();
229 
230  aPropSet.setProperty( PROP_NullDate, aNullDate );
231  aPropSet.setProperty( PROP_IsIterationEnabled, maCalcSettings.mbIterate );
232  aPropSet.setProperty( PROP_IterationCount, maCalcSettings.mnIterateCount );
233  aPropSet.setProperty( PROP_IterationEpsilon, maCalcSettings.mfIterateDelta );
234  aPropSet.setProperty( PROP_CalcAsShown, !maCalcSettings.mbFullPrecision );
235  aPropSet.setProperty( PROP_LookUpLabels, false );
236 
237  Reference< XNumberFormatsSupplier > xNumFmtsSupp( getDocument(), UNO_QUERY );
238  if( xNumFmtsSupp.is() )
239  {
240  PropertySet aNumFmtProp( xNumFmtsSupp->getNumberFormatSettings() );
241  aNumFmtProp.setProperty( PROP_NullDate, aNullDate );
242  }
243 
244  Reference< XCalculatable > xCalculatable( getDocument(), UNO_QUERY );
245  if( xCalculatable.is() )
246  xCalculatable->enableAutomaticCalculation( (maCalcSettings.mnCalcMode == XML_auto) || (maCalcSettings.mnCalcMode == XML_autoNoTable) );
247 
248  // VBA code name
249  aPropSet.setProperty( PROP_CodeName, maBookSettings.maCodeName );
250 }
251 
253 {
255  {
256  case XML_all: return API_SHOWMODE_SHOW;
257  case XML_none: return API_SHOWMODE_HIDE;
258  // #i80528# placeholders not supported anymore, but this is handled internally in Calc
259  case XML_placeholders: return API_SHOWMODE_PLACEHOLDER;
260  }
261  return API_SHOWMODE_SHOW;
262 }
263 
264 css::util::Date const & WorkbookSettings::getNullDate() const
265 {
266  static const css::util::Date saDate1900 ( 30, 12, 1899 );
267  static const css::util::Date saDate1904 ( 1, 1, 1904 );
268  static const css::util::Date saDateBackCompatibility1900( 31, 12, 1899 );
269 
270  if( getOoxFilter().getVersion() == oox::core::ISOIEC_29500_2008 )
271  {
273  return saDate1900;
274 
275  return maBookSettings.mbDateMode1904 ? saDate1904 :
276  saDateBackCompatibility1900;
277  }
278 
279  return maBookSettings.mbDateMode1904 ? saDate1904 : saDate1900;
280 }
281 
282 void WorkbookSettings::setDateMode( bool bDateMode1904, bool bDateCompatibility )
283 {
284  maBookSettings.mbDateMode1904 = bDateMode1904;
285  maBookSettings.mbDateCompatibility = bDateCompatibility;
286 
288 }
289 
290 } // namespace oox::xls
291 
292 /* 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.
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.
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 mbDateMode1904
Default theme version.