xref: /core/sc/source/filter/oox/workbooksettings.cxx (revision 000aaf1f8c6b3520a604327445a41ca6b8f568dc)
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 
28 #include <comphelper/propertyvalue.hxx>
29 #include <unotools/mediadescriptor.hxx>
30 #include <oox/core/binarycodec.hxx>
31 #include <oox/core/filterbase.hxx>
32 #include <oox/helper/binaryinputstream.hxx>
33 #include <oox/helper/attributelist.hxx>
34 #include <oox/helper/propertyset.hxx>
35 #include <oox/core/xmlfilterbase.hxx>
36 #include <oox/token/properties.hxx>
37 #include <oox/token/tokens.hxx>
38 #include <unitconverter.hxx>
39 #include <biffhelper.hxx>
40 #include <docuno.hxx>
41 
42 namespace oox::xls {
43 
44 using namespace ::com::sun::star::beans;
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;        /// Show drawing objects.
63 const sal_Int16 API_SHOWMODE_HIDE               = 1;        /// Hide drawing objects.
64 const sal_Int16 API_SHOWMODE_PLACEHOLDER        = 2;        /// Show placeholders for drawing objects.
65 
66 } // namespace
67 
FileSharingModel()68 FileSharingModel::FileSharingModel() :
69     mnSpinCount( 0 ),
70     mnPasswordHash( 0 ),
71     mbRecommendReadOnly( false )
72 {
73 }
74 
WorkbookSettingsModel()75 WorkbookSettingsModel::WorkbookSettingsModel() :
76     mnShowObjectMode( XML_all ),
77     mnUpdateLinksMode( XML_userSet ),
78     mnDefaultThemeVer( -1 ),
79     mbDateMode1904( false ),
80     mbDateCompatibility ( false ),
81     mbSaveExtLinkValues( true )
82 {
83 }
84 
setBiffObjectMode(sal_uInt16 nObjMode)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 
CalcSettingsModel()91 CalcSettingsModel::CalcSettingsModel() :
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 
WorkbookSettings(const WorkbookHelper & rHelper)106 WorkbookSettings::WorkbookSettings( const WorkbookHelper& rHelper ) :
107     WorkbookHelper( rHelper )
108 {
109 }
110 
importFileSharing(const AttributeList & rAttribs)111 void WorkbookSettings::importFileSharing( const AttributeList& rAttribs )
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 
importWorkbookPr(const AttributeList & rAttribs)122 void WorkbookSettings::importWorkbookPr( const AttributeList& rAttribs )
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 
importCalcPr(const AttributeList & rAttribs)132 void WorkbookSettings::importCalcPr( const AttributeList& rAttribs )
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 
importFileSharing(SequenceInputStream & rStrm)147 void WorkbookSettings::importFileSharing( SequenceInputStream& rStrm )
148 {
149     maFileSharing.mbRecommendReadOnly = rStrm.readuInt16() != 0;
150     maFileSharing.mnPasswordHash = rStrm.readuInt16();
151     rStrm >> maFileSharing.maUserName;
152 }
153 
importWorkbookPr(SequenceInputStream & rStrm)154 void WorkbookSettings::importWorkbookPr( SequenceInputStream& rStrm )
155 {
156     sal_uInt32 nFlags;
157     nFlags = rStrm.readuInt32();
158     maBookSettings.mnDefaultThemeVer = rStrm.readInt32();
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 
importCalcPr(SequenceInputStream & rStrm)166 void WorkbookSettings::importCalcPr( SequenceInputStream& rStrm )
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 
finalizeImport()188 void WorkbookSettings::finalizeImport()
189 {
190     // default settings
191     PropertySet aPropSet(( Reference< css::beans::XPropertySet >(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
197     if (maFileSharing.mbRecommendReadOnly || (maFileSharing.mnPasswordHash != 0) ||
198             !maFileSharing.maHashValue.isEmpty()) try
199     {
200         getBaseFilter().getMediaDescriptor()[ u"ReadOnly"_ustr ] <<= true;
201 
202         Reference< XPropertySet > xDocumentSettings( getBaseFilter().getModelFactory()->createInstance(
203             u"com.sun.star.document.Settings"_ustr ), 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... */
212         if (maFileSharing.mbRecommendReadOnly || !maFileSharing.maHashValue.isEmpty())
213             aSettingsProp.setProperty( PROP_LoadReadonly, true );
214 
215         if (!maFileSharing.maHashValue.isEmpty())
216         {
217             Sequence<PropertyValue> aResult{
218                 comphelper::makePropertyValue(u"algorithm-name"_ustr, maFileSharing.maAlgorithmName),
219                 comphelper::makePropertyValue(u"salt"_ustr, maFileSharing.maSaltValue),
220                 comphelper::makePropertyValue(u"iteration-count"_ustr, maFileSharing.mnSpinCount),
221                 comphelper::makePropertyValue(u"hash"_ustr, maFileSharing.maHashValue)
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( static_cast<cppu::OWeakObject*>(getDocument().get()), UNO_QUERY );
244     if( xNumFmtsSupp.is() )
245     {
246         PropertySet aNumFmtProp( xNumFmtsSupp->getNumberFormatSettings() );
247         aNumFmtProp.setProperty( PROP_NullDate, aNullDate );
248     }
249 
250     rtl::Reference< ScModelObj > xCalculatable( getDocument() );
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 
getApiShowObjectMode() const258 sal_Int16 WorkbookSettings::getApiShowObjectMode() const
259 {
260     switch( maBookSettings.mnShowObjectMode )
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 
getNullDate() const270 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     {
278         if( !maBookSettings.mbDateCompatibility )
279             return saDate1900;
280 
281         return maBookSettings.mbDateMode1904 ? saDate1904 :
282                                                saDateBackCompatibility1900;
283     }
284 
285     return maBookSettings.mbDateMode1904 ? saDate1904 : saDate1900;
286 }
287 
setDateMode(bool bDateMode1904,bool bDateCompatibility)288 void WorkbookSettings::setDateMode( bool bDateMode1904, bool bDateCompatibility )
289 {
290     maBookSettings.mbDateMode1904      = bDateMode1904;
291     maBookSettings.mbDateCompatibility = bDateCompatibility;
292 
293     getUnitConverter().finalizeNullDate( getNullDate() );
294 }
295 
296 } // namespace oox::xls
297 
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
299