Line data Source code
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/util/Date.hpp>
24 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <unotools/mediadescriptor.hxx>
27 : #include <oox/core/filterbase.hxx>
28 : #include <oox/helper/attributelist.hxx>
29 : #include <oox/helper/propertyset.hxx>
30 : #include <oox/core/xmlfilterbase.hxx>
31 : #include <oox/token/properties.hxx>
32 : #include "biffinputstream.hxx"
33 : #include "unitconverter.hxx"
34 :
35 : namespace oox {
36 : namespace xls {
37 :
38 : using namespace ::com::sun::star::beans;
39 : using namespace ::com::sun::star::sheet;
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::util;
42 :
43 : namespace {
44 :
45 : const sal_uInt32 BIFF12_WORKBOOKPR_DATE1904 = 0x00000001;
46 : const sal_uInt32 BIFF12_WORKBOOKPR_STRIPEXT = 0x00000080;
47 :
48 : const sal_uInt16 BIFF12_CALCPR_A1 = 0x0002;
49 : const sal_uInt16 BIFF12_CALCPR_ITERATE = 0x0004;
50 : const sal_uInt16 BIFF12_CALCPR_FULLPRECISION = 0x0008;
51 : const sal_uInt16 BIFF12_CALCPR_CALCCOMPLETED = 0x0010;
52 : const sal_uInt16 BIFF12_CALCPR_CALCONSAVE = 0x0020;
53 : const sal_uInt16 BIFF12_CALCPR_CONCURRENT = 0x0040;
54 : const sal_uInt16 BIFF12_CALCPR_MANUALPROC = 0x0080;
55 :
56 : // no predefined constants for show objects mode
57 : const sal_Int16 API_SHOWMODE_SHOW = 0; /// Show drawing objects.
58 : const sal_Int16 API_SHOWMODE_HIDE = 1; /// Hide drawing objects.
59 : const sal_Int16 API_SHOWMODE_PLACEHOLDER = 2; /// Show placeholders for drawing objects.
60 :
61 : } // namespace
62 :
63 145 : FileSharingModel::FileSharingModel() :
64 : mnPasswordHash( 0 ),
65 145 : mbRecommendReadOnly( false )
66 : {
67 145 : }
68 :
69 145 : WorkbookSettingsModel::WorkbookSettingsModel() :
70 : mnShowObjectMode( XML_all ),
71 : mnUpdateLinksMode( XML_userSet ),
72 : mnDefaultThemeVer( -1 ),
73 : mbDateMode1904( false ),
74 : mbDateCompatibility ( false ),
75 145 : mbSaveExtLinkValues( true )
76 : {
77 145 : }
78 :
79 2 : void WorkbookSettingsModel::setBiffObjectMode( sal_uInt16 nObjMode )
80 : {
81 : static const sal_Int32 spnObjModes[] = { XML_all, XML_placeholders, XML_none };
82 2 : mnShowObjectMode = STATIC_ARRAY_SELECT( spnObjModes, nObjMode, XML_all );
83 2 : }
84 :
85 145 : CalcSettingsModel::CalcSettingsModel() :
86 : mfIterateDelta( 0.001 ),
87 : mnCalcId( -1 ),
88 : mnRefMode( XML_A1 ),
89 : mnCalcMode( XML_auto ),
90 : mnIterateCount( 100 ),
91 : mnProcCount( -1 ),
92 : mbCalcOnSave( true ),
93 : mbCalcCompleted( true ),
94 : mbFullPrecision( true ),
95 : mbIterate( false ),
96 : mbConcurrent( true ),
97 145 : mbUseNlr( false )
98 : {
99 145 : }
100 :
101 145 : WorkbookSettings::WorkbookSettings( const WorkbookHelper& rHelper ) :
102 145 : WorkbookHelper( rHelper )
103 : {
104 145 : }
105 :
106 0 : void WorkbookSettings::importFileSharing( const AttributeList& rAttribs )
107 : {
108 0 : maFileSharing.maUserName = rAttribs.getXString( XML_userName, OUString() );
109 0 : maFileSharing.mnPasswordHash = oox::core::CodecHelper::getPasswordHash( rAttribs, XML_reservationPassword );
110 0 : maFileSharing.mbRecommendReadOnly = rAttribs.getBool( XML_readOnlyRecommended, false );
111 0 : }
112 :
113 143 : void WorkbookSettings::importWorkbookPr( const AttributeList& rAttribs )
114 : {
115 143 : maBookSettings.maCodeName = rAttribs.getString( XML_codeName, OUString() );
116 143 : maBookSettings.mnShowObjectMode = rAttribs.getToken( XML_showObjects, XML_all );
117 143 : maBookSettings.mnUpdateLinksMode = rAttribs.getToken( XML_updateLinks, XML_userSet );
118 143 : maBookSettings.mnDefaultThemeVer = rAttribs.getInteger( XML_defaultThemeVersion, -1 );
119 143 : maBookSettings.mbSaveExtLinkValues = rAttribs.getBool( XML_saveExternalLinkValues, true );
120 143 : setDateMode( rAttribs.getBool( XML_date1904, false ), rAttribs.getBool( XML_dateCompatibility, true ) );
121 143 : }
122 :
123 143 : void WorkbookSettings::importCalcPr( const AttributeList& rAttribs )
124 : {
125 143 : maCalcSettings.mfIterateDelta = rAttribs.getDouble( XML_iterateDelta, 0.0001 );
126 143 : maCalcSettings.mnCalcId = rAttribs.getInteger( XML_calcId, -1 );
127 143 : maCalcSettings.mnRefMode = rAttribs.getToken( XML_refMode, XML_A1 );
128 143 : maCalcSettings.mnCalcMode = rAttribs.getToken( XML_calcMode, XML_auto );
129 143 : maCalcSettings.mnIterateCount = rAttribs.getInteger( XML_iterateCount, 100 );
130 143 : maCalcSettings.mnProcCount = rAttribs.getInteger( XML_concurrentManualCount, -1 );
131 143 : maCalcSettings.mbCalcOnSave = rAttribs.getBool( XML_calcOnSave, true );
132 143 : maCalcSettings.mbCalcCompleted = rAttribs.getBool( XML_calcCompleted, true );
133 143 : maCalcSettings.mbFullPrecision = rAttribs.getBool( XML_fullPrecision, true );
134 143 : maCalcSettings.mbIterate = rAttribs.getBool( XML_iterate, false );
135 143 : maCalcSettings.mbConcurrent = rAttribs.getBool( XML_concurrentCalc, true );
136 143 : }
137 :
138 0 : void WorkbookSettings::importFileSharing( SequenceInputStream& rStrm )
139 : {
140 0 : maFileSharing.mbRecommendReadOnly = rStrm.readuInt16() != 0;
141 0 : maFileSharing.mnPasswordHash = rStrm.readuInt16();
142 0 : rStrm >> maFileSharing.maUserName;
143 0 : }
144 :
145 2 : void WorkbookSettings::importWorkbookPr( SequenceInputStream& rStrm )
146 : {
147 : sal_uInt32 nFlags;
148 2 : nFlags = rStrm.readuInt32();
149 2 : maBookSettings.mnDefaultThemeVer = rStrm.readInt32();
150 2 : rStrm >> maBookSettings.maCodeName;
151 2 : maBookSettings.setBiffObjectMode( extractValue< sal_uInt16 >( nFlags, 13, 2 ) );
152 : // set flag means: strip external link values
153 2 : maBookSettings.mbSaveExtLinkValues = !getFlag( nFlags, BIFF12_WORKBOOKPR_STRIPEXT );
154 2 : setDateMode( getFlag( nFlags, BIFF12_WORKBOOKPR_DATE1904 ) );
155 2 : }
156 :
157 2 : void WorkbookSettings::importCalcPr( SequenceInputStream& rStrm )
158 : {
159 : sal_Int32 nCalcMode, nProcCount;
160 : sal_uInt16 nFlags;
161 2 : maCalcSettings.mnCalcId = rStrm.readInt32();
162 2 : nCalcMode = rStrm.readInt32();
163 2 : maCalcSettings.mnIterateCount = rStrm.readInt32();
164 2 : maCalcSettings.mfIterateDelta = rStrm.readDouble();
165 2 : nProcCount = rStrm.readInt32();
166 2 : nFlags = rStrm.readuInt16();
167 :
168 : static const sal_Int32 spnCalcModes[] = { XML_manual, XML_auto, XML_autoNoTable };
169 2 : maCalcSettings.mnRefMode = getFlagValue( nFlags, BIFF12_CALCPR_A1, XML_A1, XML_R1C1 );
170 2 : maCalcSettings.mnCalcMode = STATIC_ARRAY_SELECT( spnCalcModes, nCalcMode, XML_auto );
171 2 : maCalcSettings.mnProcCount = getFlagValue< sal_Int32 >( nFlags, BIFF12_CALCPR_MANUALPROC, nProcCount, -1 );
172 2 : maCalcSettings.mbCalcOnSave = getFlag( nFlags, BIFF12_CALCPR_CALCONSAVE );
173 2 : maCalcSettings.mbCalcCompleted = getFlag( nFlags, BIFF12_CALCPR_CALCCOMPLETED );
174 2 : maCalcSettings.mbFullPrecision = getFlag( nFlags, BIFF12_CALCPR_FULLPRECISION );
175 2 : maCalcSettings.mbIterate = getFlag( nFlags, BIFF12_CALCPR_ITERATE );
176 2 : maCalcSettings.mbConcurrent = getFlag( nFlags, BIFF12_CALCPR_CONCURRENT );
177 2 : }
178 :
179 145 : void WorkbookSettings::finalizeImport()
180 : {
181 : // default settings
182 145 : PropertySet aPropSet( getDocument() );
183 145 : switch( getFilterType() )
184 : {
185 : case FILTER_OOXML:
186 : case FILTER_BIFF:
187 145 : aPropSet.setProperty( PROP_IgnoreCase, true ); // always in Excel
188 145 : aPropSet.setProperty( PROP_RegularExpressions, false ); // not supported in Excel
189 145 : break;
190 : case FILTER_UNKNOWN:
191 0 : break;
192 : }
193 :
194 : // write protection
195 145 : if( maFileSharing.mbRecommendReadOnly || (maFileSharing.mnPasswordHash != 0) ) try
196 : {
197 0 : getBaseFilter().getMediaDescriptor()[ "ReadOnly" ] <<= true;
198 :
199 0 : Reference< XPropertySet > xDocumentSettings( getBaseFilter().getModelFactory()->createInstance(
200 0 : "com.sun.star.document.Settings" ), UNO_QUERY_THROW );
201 0 : PropertySet aSettingsProp( xDocumentSettings );
202 0 : if( maFileSharing.mbRecommendReadOnly )
203 0 : aSettingsProp.setProperty( PROP_LoadReadonly, true );
204 : // if( maFileSharing.mnPasswordHash != 0 )
205 : // aSettingsProp.setProperty( PROP_ModifyPasswordHash, static_cast< sal_Int32 >( maFileSharing.mnPasswordHash ) );
206 : }
207 0 : catch( Exception& )
208 : {
209 : }
210 :
211 : // calculation settings
212 145 : css::util::Date aNullDate = getNullDate();
213 :
214 145 : aPropSet.setProperty( PROP_NullDate, aNullDate );
215 145 : aPropSet.setProperty( PROP_IsIterationEnabled, maCalcSettings.mbIterate );
216 145 : aPropSet.setProperty( PROP_IterationCount, maCalcSettings.mnIterateCount );
217 145 : aPropSet.setProperty( PROP_IterationEpsilon, maCalcSettings.mfIterateDelta );
218 145 : aPropSet.setProperty( PROP_CalcAsShown, !maCalcSettings.mbFullPrecision );
219 145 : aPropSet.setProperty( PROP_LookUpLabels, maCalcSettings.mbUseNlr );
220 :
221 145 : Reference< XNumberFormatsSupplier > xNumFmtsSupp( getDocument(), UNO_QUERY );
222 145 : if( xNumFmtsSupp.is() )
223 : {
224 145 : PropertySet aNumFmtProp( xNumFmtsSupp->getNumberFormatSettings() );
225 145 : aNumFmtProp.setProperty( PROP_NullDate, aNullDate );
226 : }
227 :
228 290 : Reference< XCalculatable > xCalculatable( getDocument(), UNO_QUERY );
229 145 : if( xCalculatable.is() )
230 145 : xCalculatable->enableAutomaticCalculation( (maCalcSettings.mnCalcMode == XML_auto) || (maCalcSettings.mnCalcMode == XML_autoNoTable) );
231 :
232 : // VBA code name
233 290 : aPropSet.setProperty( PROP_CodeName, maBookSettings.maCodeName );
234 145 : }
235 :
236 145 : sal_Int16 WorkbookSettings::getApiShowObjectMode() const
237 : {
238 145 : switch( maBookSettings.mnShowObjectMode )
239 : {
240 145 : case XML_all: return API_SHOWMODE_SHOW;
241 0 : case XML_none: return API_SHOWMODE_HIDE;
242 : // #i80528# placeholders not supported anymore, but this is handled internally in Calc
243 0 : case XML_placeholders: return API_SHOWMODE_PLACEHOLDER;
244 : }
245 0 : return API_SHOWMODE_SHOW;
246 : }
247 :
248 290 : css::util::Date WorkbookSettings::getNullDate() const
249 : {
250 290 : static const css::util::Date saDate1900 ( 30, 12, 1899 );
251 290 : static const css::util::Date saDate1904 ( 1, 1, 1904 );
252 290 : static const css::util::Date saDateBackCompatibility1900( 31, 12, 1899 );
253 :
254 290 : if( getOoxFilter().getVersion() == oox::core::ISOIEC_29500_2008 )
255 : {
256 150 : if( !maBookSettings.mbDateCompatibility )
257 18 : return saDate1900;
258 :
259 : return maBookSettings.mbDateMode1904 ? saDate1904 :
260 132 : saDateBackCompatibility1900;
261 : }
262 :
263 140 : return maBookSettings.mbDateMode1904 ? saDate1904 : saDate1900;
264 : }
265 :
266 145 : void WorkbookSettings::setDateMode( bool bDateMode1904, bool bDateCompatibility )
267 : {
268 145 : maBookSettings.mbDateMode1904 = bDateMode1904;
269 145 : maBookSettings.mbDateCompatibility = bDateCompatibility;
270 :
271 145 : getUnitConverter().finalizeNullDate( getNullDate() );
272 145 : }
273 :
274 : } // namespace xls
275 30 : } // namespace oox
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|