1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: workbooksettings.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/xls/workbooksettings.hxx"
32 #include <com/sun/star/util/Date.hpp>
33 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
34 #include <com/sun/star/sheet/XCalculatable.hpp>
35 #include <comphelper/mediadescriptor.hxx>
36 #include "properties.hxx"
37 #include "oox/helper/attributelist.hxx"
38 #include "oox/helper/propertyset.hxx"
39 #include "oox/helper/recordinputstream.hxx"
40 #include "oox/core/filterbase.hxx"
41 #include "oox/xls/biffinputstream.hxx"
42 #include "oox/xls/unitconverter.hxx"
44 using ::rtl::OUString
;
45 using ::com::sun::star::uno::Reference
;
46 using ::com::sun::star::uno::UNO_QUERY
;
47 using ::com::sun::star::util::Date
;
48 using ::com::sun::star::util::XNumberFormatsSupplier
;
49 using ::com::sun::star::sheet::XCalculatable
;
50 using ::oox::core::CodecHelper
;
55 // ============================================================================
59 const sal_uInt32 OOBIN_WORKBOOKPR_DATE1904
= 0x00000001;
60 const sal_uInt32 OOBIN_WORKBOOKPR_STRIPEXT
= 0x00000080;
62 const sal_uInt16 OOBIN_CALCPR_A1
= 0x0002;
63 const sal_uInt16 OOBIN_CALCPR_ITERATE
= 0x0004;
64 const sal_uInt16 OOBIN_CALCPR_FULLPRECISION
= 0x0008;
65 const sal_uInt16 OOBIN_CALCPR_CALCCOMPLETED
= 0x0010;
66 const sal_uInt16 OOBIN_CALCPR_CALCONSAVE
= 0x0020;
67 const sal_uInt16 OOBIN_CALCPR_CONCURRENT
= 0x0040;
68 const sal_uInt16 OOBIN_CALCPR_MANUALPROC
= 0x0080;
70 // no predefined constants for show objects mode
71 const sal_Int16 API_SHOWMODE_SHOW
= 0; /// Show drawing objects.
72 const sal_Int16 API_SHOWMODE_HIDE
= 1; /// Hide drawing objects.
73 const sal_Int16 API_SHOWMODE_PLACEHOLDER
= 2; /// Show placeholders for drawing objects.
77 // ============================================================================
79 FileSharingModel::FileSharingModel() :
81 mbRecommendReadOnly( false )
85 // ============================================================================
87 WorkbookSettingsModel::WorkbookSettingsModel() :
88 mnShowObjectMode( XML_all
),
89 mnUpdateLinksMode( XML_userSet
),
90 mnDefaultThemeVer( -1 ),
91 mbDateMode1904( false ),
92 mbSaveExtLinkValues( true )
96 void WorkbookSettingsModel::setBinObjectMode( sal_uInt16 nObjMode
)
98 static const sal_Int32 spnObjModes
[] = { XML_all
, XML_placeholders
, XML_none
};
99 mnShowObjectMode
= STATIC_ARRAY_SELECT( spnObjModes
, nObjMode
, XML_all
);
102 // ============================================================================
104 CalcSettingsModel::CalcSettingsModel() :
105 mfIterateDelta( 0.001 ),
108 mnCalcMode( XML_auto
),
109 mnIterateCount( 100 ),
111 mbCalcOnSave( true ),
112 mbCalcCompleted( true ),
113 mbFullPrecision( true ),
115 mbConcurrent( true ),
120 // ============================================================================
122 WorkbookSettings::WorkbookSettings( const WorkbookHelper
& rHelper
) :
123 WorkbookHelper( rHelper
)
127 void WorkbookSettings::importFileSharing( const AttributeList
& rAttribs
)
129 maFileSharing
.maUserName
= rAttribs
.getXString( XML_userName
, OUString() );
130 maFileSharing
.mnPasswordHash
= CodecHelper::getPasswordHash( rAttribs
, XML_reservationPassword
);
131 maFileSharing
.mbRecommendReadOnly
= rAttribs
.getBool( XML_readOnlyRecommended
, false );
134 void WorkbookSettings::importWorkbookPr( const AttributeList
& rAttribs
)
136 maBookSettings
.maCodeName
= rAttribs
.getString( XML_codePage
, OUString() );
137 maBookSettings
.mnShowObjectMode
= rAttribs
.getToken( XML_showObjects
, XML_all
);
138 maBookSettings
.mnUpdateLinksMode
= rAttribs
.getToken( XML_updateLinks
, XML_userSet
);
139 maBookSettings
.mnDefaultThemeVer
= rAttribs
.getInteger( XML_defaultThemeVersion
, -1 );
140 maBookSettings
.mbSaveExtLinkValues
= rAttribs
.getBool( XML_saveExternalLinkValues
, true );
141 setDateMode( rAttribs
.getBool( XML_date1904
, false ) );
144 void WorkbookSettings::importCalcPr( const AttributeList
& rAttribs
)
146 maCalcSettings
.mfIterateDelta
= rAttribs
.getDouble( XML_iterateDelta
, 0.0001 );
147 maCalcSettings
.mnCalcId
= rAttribs
.getInteger( XML_calcId
, -1 );
148 maCalcSettings
.mnRefMode
= rAttribs
.getToken( XML_refMode
, XML_A1
);
149 maCalcSettings
.mnCalcMode
= rAttribs
.getToken( XML_calcMode
, XML_auto
);
150 maCalcSettings
.mnIterateCount
= rAttribs
.getInteger( XML_iterateCount
, 100 );
151 maCalcSettings
.mnProcCount
= rAttribs
.getInteger( XML_concurrentManualCount
, -1 );
152 maCalcSettings
.mbCalcOnSave
= rAttribs
.getBool( XML_calcOnSave
, true );
153 maCalcSettings
.mbCalcCompleted
= rAttribs
.getBool( XML_calcCompleted
, true );
154 maCalcSettings
.mbFullPrecision
= rAttribs
.getBool( XML_fullPrecision
, true );
155 maCalcSettings
.mbIterate
= rAttribs
.getBool( XML_iterate
, false );
156 maCalcSettings
.mbConcurrent
= rAttribs
.getBool( XML_concurrentCalc
, true );
159 void WorkbookSettings::importFileSharing( RecordInputStream
& rStrm
)
161 maFileSharing
.mbRecommendReadOnly
= rStrm
.readuInt16() != 0;
162 rStrm
>> maFileSharing
.mnPasswordHash
>> maFileSharing
.maUserName
;
165 void WorkbookSettings::importWorkbookPr( RecordInputStream
& rStrm
)
168 rStrm
>> nFlags
>> maBookSettings
.mnDefaultThemeVer
>> maBookSettings
.maCodeName
;
169 maBookSettings
.setBinObjectMode( extractValue
< sal_uInt16
>( nFlags
, 13, 2 ) );
170 // set flag means: strip external link values
171 maBookSettings
.mbSaveExtLinkValues
= !getFlag( nFlags
, OOBIN_WORKBOOKPR_STRIPEXT
);
172 setDateMode( getFlag( nFlags
, OOBIN_WORKBOOKPR_DATE1904
) );
175 void WorkbookSettings::importCalcPr( RecordInputStream
& rStrm
)
177 sal_Int32 nCalcMode
, nProcCount
;
179 rStrm
>> maCalcSettings
.mnCalcId
>> nCalcMode
>> maCalcSettings
.mnIterateCount
>> maCalcSettings
.mfIterateDelta
>> nProcCount
>> nFlags
;
181 static const sal_Int32 spnCalcModes
[] = { XML_manual
, XML_auto
, XML_autoNoTable
};
182 maCalcSettings
.mnRefMode
= getFlagValue( nFlags
, OOBIN_CALCPR_A1
, XML_A1
, XML_R1C1
);
183 maCalcSettings
.mnCalcMode
= STATIC_ARRAY_SELECT( spnCalcModes
, nCalcMode
, XML_auto
);
184 maCalcSettings
.mnProcCount
= getFlagValue
< sal_Int32
>( nFlags
, OOBIN_CALCPR_MANUALPROC
, nProcCount
, -1 );
185 maCalcSettings
.mbCalcOnSave
= getFlag( nFlags
, OOBIN_CALCPR_CALCONSAVE
);
186 maCalcSettings
.mbCalcCompleted
= getFlag( nFlags
, OOBIN_CALCPR_CALCCOMPLETED
);
187 maCalcSettings
.mbFullPrecision
= getFlag( nFlags
, OOBIN_CALCPR_FULLPRECISION
);
188 maCalcSettings
.mbIterate
= getFlag( nFlags
, OOBIN_CALCPR_ITERATE
);
189 maCalcSettings
.mbConcurrent
= getFlag( nFlags
, OOBIN_CALCPR_CONCURRENT
);
192 void WorkbookSettings::setSaveExtLinkValues( bool bSaveExtLinks
)
194 maBookSettings
.mbSaveExtLinkValues
= bSaveExtLinks
;
197 void WorkbookSettings::importFileSharing( BiffInputStream
& rStrm
)
199 maFileSharing
.mbRecommendReadOnly
= rStrm
.readuInt16() != 0;
200 rStrm
>> maFileSharing
.mnPasswordHash
;
201 if( getBiff() == BIFF8
)
203 sal_uInt16 nStrLen
= rStrm
.readuInt16();
204 // there is no string flags field if string is empty
206 maFileSharing
.maUserName
= rStrm
.readUniStringBody( nStrLen
);
210 maFileSharing
.maUserName
= rStrm
.readByteStringUC( false, getTextEncoding() );
214 void WorkbookSettings::importBookBool( BiffInputStream
& rStrm
)
216 // value of 0 means save external values, value of 1 means strip external values
217 maBookSettings
.mbSaveExtLinkValues
= rStrm
.readuInt16() == 0;
220 void WorkbookSettings::importCalcCount( BiffInputStream
& rStrm
)
222 maCalcSettings
.mnIterateCount
= rStrm
.readuInt16();
225 void WorkbookSettings::importCalcMode( BiffInputStream
& rStrm
)
227 sal_Int16 nCalcMode
= rStrm
.readInt16() + 1;
228 static const sal_Int32 spnCalcModes
[] = { XML_autoNoTable
, XML_manual
, XML_auto
};
229 maCalcSettings
.mnCalcMode
= STATIC_ARRAY_SELECT( spnCalcModes
, nCalcMode
, XML_auto
);
232 void WorkbookSettings::importCodeName( BiffInputStream
& rStrm
)
234 maBookSettings
.maCodeName
= rStrm
.readUniString();
237 void WorkbookSettings::importDateMode( BiffInputStream
& rStrm
)
239 setDateMode( rStrm
.readuInt16() != 0 );
242 void WorkbookSettings::importDelta( BiffInputStream
& rStrm
)
244 rStrm
>> maCalcSettings
.mfIterateDelta
;
247 void WorkbookSettings::importHideObj( BiffInputStream
& rStrm
)
249 maBookSettings
.setBinObjectMode( rStrm
.readuInt16() );
252 void WorkbookSettings::importIteration( BiffInputStream
& rStrm
)
254 maCalcSettings
.mbIterate
= rStrm
.readuInt16() != 0;
257 void WorkbookSettings::importPrecision( BiffInputStream
& rStrm
)
259 maCalcSettings
.mbFullPrecision
= rStrm
.readuInt16() != 0;
262 void WorkbookSettings::importRefMode( BiffInputStream
& rStrm
)
264 maCalcSettings
.mnRefMode
= (rStrm
.readuInt16() == 0) ? XML_R1C1
: XML_A1
;
267 void WorkbookSettings::importSaveRecalc( BiffInputStream
& rStrm
)
269 maCalcSettings
.mbCalcOnSave
= rStrm
.readuInt16() != 0;
272 void WorkbookSettings::importUncalced( BiffInputStream
& )
274 // existence of this record indicates incomplete recalc
275 maCalcSettings
.mbCalcCompleted
= false;
278 void WorkbookSettings::importUsesElfs( BiffInputStream
& rStrm
)
280 maCalcSettings
.mbUseNlr
= rStrm
.readuInt16() != 0;
283 void WorkbookSettings::finalizeImport()
286 PropertySet
aPropSet( getDocument() );
287 switch( getFilterType() )
291 aPropSet
.setProperty( PROP_IgnoreCase
, true ); // always in Excel
292 aPropSet
.setProperty( PROP_RegularExpressions
, false ); // not supported in Excel
299 if( maFileSharing
.mbRecommendReadOnly
|| (maFileSharing
.mnPasswordHash
!= 0) )
300 getBaseFilter().getMediaDescriptor()[ CREATE_OUSTRING( "ReadOnly" ) ] <<= true;
302 // calculation settings
303 Date aNullDate
= getNullDate();
305 aPropSet
.setProperty( PROP_NullDate
, aNullDate
);
306 aPropSet
.setProperty( PROP_IsIterationEnabled
, maCalcSettings
.mbIterate
);
307 aPropSet
.setProperty( PROP_IterationCount
, maCalcSettings
.mnIterateCount
);
308 aPropSet
.setProperty( PROP_IterationEpsilon
, maCalcSettings
.mfIterateDelta
);
309 aPropSet
.setProperty( PROP_CalcAsShown
, !maCalcSettings
.mbFullPrecision
);
310 aPropSet
.setProperty( PROP_LookUpLabels
, maCalcSettings
.mbUseNlr
);
312 Reference
< XNumberFormatsSupplier
> xNumFmtsSupp( getDocument(), UNO_QUERY
);
313 if( xNumFmtsSupp
.is() )
315 PropertySet
aNumFmtProp( xNumFmtsSupp
->getNumberFormatSettings() );
316 aNumFmtProp
.setProperty( PROP_NullDate
, aNullDate
);
319 Reference
< XCalculatable
> xCalculatable( getDocument(), UNO_QUERY
);
320 if( xCalculatable
.is() )
321 xCalculatable
->enableAutomaticCalculation( (maCalcSettings
.mnCalcMode
== XML_auto
) || (maCalcSettings
.mnCalcMode
== XML_autoNoTable
) );
324 sal_Int16
WorkbookSettings::getApiShowObjectMode() const
326 switch( maBookSettings
.mnShowObjectMode
)
328 case XML_all
: return API_SHOWMODE_SHOW
;
329 case XML_none
: return API_SHOWMODE_HIDE
;
330 // #i80528# placeholders not supported anymore, but this is handled internally in Calc
331 case XML_placeholders
: return API_SHOWMODE_PLACEHOLDER
;
333 return API_SHOWMODE_SHOW
;
336 Date
WorkbookSettings::getNullDate() const
338 static const Date
saDate1900( 30, 12, 1899 ), saDate1904( 1, 1, 1904 );
339 return maBookSettings
.mbDateMode1904
? saDate1904
: saDate1900
;
342 void WorkbookSettings::setDateMode( bool bDateMode1904
)
344 maBookSettings
.mbDateMode1904
= bDateMode1904
;
345 getUnitConverter().finalizeNullDate( getNullDate() );
348 // ============================================================================