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 "querytablebuffer.hxx"
21 :
22 : #include <com/sun/star/container/XEnumerationAccess.hpp>
23 : #include <com/sun/star/sheet/XAreaLink.hpp>
24 : #include <com/sun/star/sheet/XAreaLinks.hpp>
25 : #include "oox/core/filterbase.hxx"
26 : #include "oox/helper/attributelist.hxx"
27 : #include "oox/token/properties.hxx"
28 : #include "addressconverter.hxx"
29 : #include "biffinputstream.hxx"
30 : #include "connectionsbuffer.hxx"
31 : #include "defnamesbuffer.hxx"
32 :
33 : namespace oox {
34 : namespace xls {
35 :
36 : // ============================================================================
37 :
38 : using namespace ::com::sun::star::container;
39 : using namespace ::com::sun::star::sheet;
40 : using namespace ::com::sun::star::table;
41 : using namespace ::com::sun::star::uno;
42 :
43 : using ::rtl::OUString;
44 : using ::rtl::OUStringBuffer;
45 :
46 : // ============================================================================
47 :
48 : namespace {
49 :
50 : const sal_uInt32 BIFF12_QUERYTABLE_HEADERS = 0x00000001;
51 : const sal_uInt32 BIFF12_QUERYTABLE_ROWNUMBERS = 0x00000002;
52 : const sal_uInt32 BIFF12_QUERYTABLE_DISABLEREFRESH = 0x00000004;
53 : const sal_uInt32 BIFF12_QUERYTABLE_BACKGROUND = 0x00000008;
54 : const sal_uInt32 BIFF12_QUERYTABLE_FIRSTBACKGROUND = 0x00000010;
55 : const sal_uInt32 BIFF12_QUERYTABLE_REFRESHONLOAD = 0x00000020;
56 : const sal_uInt32 BIFF12_QUERYTABLE_FILLFORMULAS = 0x00000100;
57 : const sal_uInt32 BIFF12_QUERYTABLE_SAVEDATA = 0x00000200;
58 : const sal_uInt32 BIFF12_QUERYTABLE_DISABLEEDIT = 0x00000400;
59 : const sal_uInt32 BIFF12_QUERYTABLE_PRESERVEFORMAT = 0x00000800;
60 : const sal_uInt32 BIFF12_QUERYTABLE_ADJUSTCOLWIDTH = 0x00001000;
61 : const sal_uInt32 BIFF12_QUERYTABLE_INTERMEDIATE = 0x00002000;
62 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYNUMFMT = 0x00004000;
63 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYFONT = 0x00008000;
64 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYALIGNMENT = 0x00010000;
65 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYBORDER = 0x00020000;
66 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYFILL = 0x00040000;
67 : const sal_uInt32 BIFF12_QUERYTABLE_APPLYPROTECTION = 0x00080000;
68 :
69 : const sal_uInt16 BIFF_QUERYTABLE_HEADERS = 0x0001;
70 : const sal_uInt16 BIFF_QUERYTABLE_ROWNUMBERS = 0x0002;
71 : const sal_uInt16 BIFF_QUERYTABLE_DISABLEREFRESH = 0x0004;
72 : const sal_uInt16 BIFF_QUERYTABLE_BACKGROUND = 0x0008;
73 : const sal_uInt16 BIFF_QUERYTABLE_FIRSTBACKGROUND = 0x0010;
74 : const sal_uInt16 BIFF_QUERYTABLE_REFRESHONLOAD = 0x0020;
75 : const sal_uInt16 BIFF_QUERYTABLE_DELETEUNUSED = 0x0040;
76 : const sal_uInt16 BIFF_QUERYTABLE_FILLFORMULAS = 0x0080;
77 : const sal_uInt16 BIFF_QUERYTABLE_ADJUSTCOLWIDTH = 0x0100;
78 : const sal_uInt16 BIFF_QUERYTABLE_SAVEDATA = 0x0200;
79 : const sal_uInt16 BIFF_QUERYTABLE_DISABLEEDIT = 0x0400;
80 : const sal_uInt16 BIFF_QUERYTABLE_OVERWRITEEXISTING = 0x2000;
81 :
82 : const sal_uInt16 BIFF_QUERYTABLE_APPLYNUMFMT = 0x0001;
83 : const sal_uInt16 BIFF_QUERYTABLE_APPLYFONT = 0x0002;
84 : const sal_uInt16 BIFF_QUERYTABLE_APPLYALIGNMENT = 0x0004;
85 : const sal_uInt16 BIFF_QUERYTABLE_APPLYBORDER = 0x0008;
86 : const sal_uInt16 BIFF_QUERYTABLE_APPLYFILL = 0x0010;
87 : const sal_uInt16 BIFF_QUERYTABLE_APPLYPROTECTION = 0x0020;
88 :
89 : const sal_uInt32 BIFF_QTREFRESH_PRESERVEFORMAT = 0x00000001;
90 : const sal_uInt32 BIFF_QTREFRESH_ADJUSTCOLWIDTH = 0x00000002;
91 :
92 : // ----------------------------------------------------------------------------
93 :
94 0 : void lclAppendWebQueryTableName( OUStringBuffer& rTables, const OUString& rTableName )
95 : {
96 0 : if( !rTableName.isEmpty() )
97 : {
98 0 : if( rTables.getLength() > 0 )
99 0 : rTables.append( sal_Unicode( ';' ) );
100 0 : rTables.appendAscii( RTL_CONSTASCII_STRINGPARAM( "HTML__" ) ).append( rTableName );
101 : }
102 0 : }
103 :
104 0 : void lclAppendWebQueryTableIndex( OUStringBuffer& rTables, sal_Int32 nTableIndex )
105 : {
106 0 : if( nTableIndex > 0 )
107 : {
108 0 : if( rTables.getLength() > 0 )
109 0 : rTables.append( sal_Unicode( ';' ) );
110 0 : rTables.appendAscii( RTL_CONSTASCII_STRINGPARAM( "HTML_" ) ).append( nTableIndex );
111 : }
112 0 : }
113 :
114 0 : OUString lclBuildWebQueryTables( const WebPrModel::TablesVector& rTables )
115 : {
116 0 : if( rTables.empty() )
117 0 : return OUString( "HTML_tables" );
118 :
119 0 : OUStringBuffer aTables;
120 0 : for( WebPrModel::TablesVector::const_iterator aIt = rTables.begin(), aEnd = rTables.end(); aIt != aEnd; ++aIt )
121 : {
122 0 : if( aIt->has< OUString >() )
123 0 : lclAppendWebQueryTableName( aTables, aIt->get< OUString >() );
124 0 : else if( aIt->has< sal_Int32 >() )
125 0 : lclAppendWebQueryTableIndex( aTables, aIt->get< sal_Int32 >() );
126 : }
127 0 : return aTables.makeStringAndClear();
128 : }
129 :
130 0 : Reference< XAreaLink > lclFindAreaLink(
131 : const Reference< XAreaLinks >& rxAreaLinks, const CellAddress& rDestPos,
132 : const OUString& rFileUrl, const OUString& rTables, const OUString& rFilterName, const OUString& rFilterOptions )
133 : {
134 : try
135 : {
136 0 : Reference< XEnumerationAccess > xAreaLinksEA( rxAreaLinks, UNO_QUERY_THROW );
137 0 : Reference< XEnumeration > xAreaLinksEnum( xAreaLinksEA->createEnumeration(), UNO_SET_THROW );
138 0 : while( xAreaLinksEnum->hasMoreElements() )
139 : {
140 0 : Reference< XAreaLink > xAreaLink( xAreaLinksEnum->nextElement(), UNO_QUERY_THROW );
141 0 : PropertySet aPropSet( xAreaLink );
142 0 : CellRangeAddress aDestArea = xAreaLink->getDestArea();
143 0 : OUString aString;
144 0 : if( (rDestPos.Sheet == aDestArea.Sheet) && (rDestPos.Column == aDestArea.StartColumn) && (rDestPos.Row == aDestArea.StartRow) &&
145 0 : (rTables == xAreaLink->getSourceArea()) &&
146 0 : aPropSet.getProperty( aString, PROP_Url ) && (rFileUrl == aString) &&
147 0 : aPropSet.getProperty( aString, PROP_Filter ) && (rFilterName == aString) &&
148 0 : aPropSet.getProperty( aString, PROP_FilterOptions ) && (rFilterOptions == aString) )
149 0 : return xAreaLink;
150 0 : }
151 : }
152 0 : catch( Exception& )
153 : {
154 : }
155 0 : return Reference< XAreaLink >();
156 : }
157 :
158 : } // namespace
159 :
160 : // ============================================================================
161 :
162 0 : QueryTableModel::QueryTableModel() :
163 : mnConnId( -1 ),
164 : mnGrowShrinkType( XML_insertDelete ),
165 : mbHeaders( true ),
166 : mbRowNumbers( false ),
167 : mbDisableRefresh( false ),
168 : mbBackground( true ),
169 : mbFirstBackground( false ),
170 : mbRefreshOnLoad( false ),
171 : mbFillFormulas( false ),
172 : mbRemoveDataOnSave( false ),
173 : mbDisableEdit( false ),
174 : mbPreserveFormat( true ),
175 : mbAdjustColWidth( true ),
176 0 : mbIntermediate( false )
177 : {
178 0 : }
179 :
180 : // ----------------------------------------------------------------------------
181 :
182 0 : QueryTable::QueryTable( const WorksheetHelper& rHelper ) :
183 0 : WorksheetHelper( rHelper )
184 : {
185 0 : }
186 :
187 0 : void QueryTable::importQueryTable( const AttributeList& rAttribs )
188 : {
189 0 : maModel.maDefName = rAttribs.getXString( XML_name, OUString() );
190 0 : maModel.mnConnId = rAttribs.getInteger( XML_connectionId, -1 );
191 0 : maModel.mnGrowShrinkType = rAttribs.getToken( XML_growShrinkType, XML_insertDelete );
192 0 : maModel.mnAutoFormatId = rAttribs.getInteger( XML_autoFormatId, 0 );
193 0 : maModel.mbHeaders = rAttribs.getBool( XML_headers, true );
194 0 : maModel.mbRowNumbers = rAttribs.getBool( XML_rowNumbers, false );
195 0 : maModel.mbDisableRefresh = rAttribs.getBool( XML_disableRefresh, false );
196 0 : maModel.mbBackground = rAttribs.getBool( XML_backgroundRefresh, true );
197 0 : maModel.mbFirstBackground = rAttribs.getBool( XML_firstBackgroundRefresh, false );
198 0 : maModel.mbRefreshOnLoad = rAttribs.getBool( XML_refreshOnLoad, false );
199 0 : maModel.mbFillFormulas = rAttribs.getBool( XML_fillFormulas, false );
200 0 : maModel.mbRemoveDataOnSave = rAttribs.getBool( XML_removeDataOnSave, false );
201 0 : maModel.mbDisableEdit = rAttribs.getBool( XML_disableEdit, false );
202 0 : maModel.mbPreserveFormat = rAttribs.getBool( XML_preserveFormatting, true );
203 0 : maModel.mbAdjustColWidth = rAttribs.getBool( XML_adjustColumnWidth, true );
204 0 : maModel.mbIntermediate = rAttribs.getBool( XML_intermediate, false );
205 0 : maModel.mbApplyNumFmt = rAttribs.getBool( XML_applyNumberFormats, false );
206 0 : maModel.mbApplyFont = rAttribs.getBool( XML_applyFontFormats, false );
207 0 : maModel.mbApplyAlignment = rAttribs.getBool( XML_applyAlignmentFormats, false );
208 0 : maModel.mbApplyBorder = rAttribs.getBool( XML_applyBorderFormats, false );
209 0 : maModel.mbApplyFill = rAttribs.getBool( XML_applyPatternFormats, false );
210 : // OOXML and BIFF12 documentation differ: OOXML mentions width/height, BIFF12 mentions protection
211 0 : maModel.mbApplyProtection = rAttribs.getBool( XML_applyWidthHeightFormats, false );
212 0 : }
213 :
214 0 : void QueryTable::importQueryTable( SequenceInputStream& rStrm )
215 : {
216 : sal_uInt32 nFlags;
217 0 : rStrm >> nFlags;
218 0 : maModel.mnAutoFormatId = rStrm.readuInt16();
219 0 : rStrm >> maModel.mnConnId >> maModel.maDefName;
220 :
221 : static const sal_Int32 spnGrowShrinkTypes[] = { XML_insertClear, XML_insertDelete, XML_overwriteClear };
222 0 : maModel.mnGrowShrinkType = STATIC_ARRAY_SELECT( spnGrowShrinkTypes, extractValue< sal_uInt8 >( nFlags, 6, 2 ), XML_insertDelete );
223 :
224 0 : maModel.mbHeaders = getFlag( nFlags, BIFF12_QUERYTABLE_HEADERS );
225 0 : maModel.mbRowNumbers = getFlag( nFlags, BIFF12_QUERYTABLE_ROWNUMBERS );
226 0 : maModel.mbDisableRefresh = getFlag( nFlags, BIFF12_QUERYTABLE_DISABLEREFRESH );
227 0 : maModel.mbBackground = getFlag( nFlags, BIFF12_QUERYTABLE_BACKGROUND );
228 0 : maModel.mbFirstBackground = getFlag( nFlags, BIFF12_QUERYTABLE_FIRSTBACKGROUND );
229 0 : maModel.mbRefreshOnLoad = getFlag( nFlags, BIFF12_QUERYTABLE_REFRESHONLOAD );
230 0 : maModel.mbFillFormulas = getFlag( nFlags, BIFF12_QUERYTABLE_FILLFORMULAS );
231 0 : maModel.mbRemoveDataOnSave = !getFlag( nFlags, BIFF12_QUERYTABLE_SAVEDATA ); // flag negated in BIFF12
232 0 : maModel.mbDisableEdit = getFlag( nFlags, BIFF12_QUERYTABLE_DISABLEEDIT );
233 0 : maModel.mbPreserveFormat = getFlag( nFlags, BIFF12_QUERYTABLE_PRESERVEFORMAT );
234 0 : maModel.mbAdjustColWidth = getFlag( nFlags, BIFF12_QUERYTABLE_ADJUSTCOLWIDTH );
235 0 : maModel.mbIntermediate = getFlag( nFlags, BIFF12_QUERYTABLE_INTERMEDIATE );
236 0 : maModel.mbApplyNumFmt = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYNUMFMT );
237 0 : maModel.mbApplyFont = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYFONT );
238 0 : maModel.mbApplyAlignment = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYALIGNMENT );
239 0 : maModel.mbApplyBorder = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYBORDER );
240 0 : maModel.mbApplyFill = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYFILL );
241 0 : maModel.mbApplyProtection = getFlag( nFlags, BIFF12_QUERYTABLE_APPLYPROTECTION );
242 0 : }
243 :
244 0 : void QueryTable::finalizeImport()
245 : {
246 0 : ConnectionRef xConnection = getConnections().getConnection( maModel.mnConnId );
247 : OSL_ENSURE( xConnection.get(), "QueryTable::finalizeImport - missing connection object" );
248 0 : if( xConnection.get() && (xConnection->getConnectionType() == BIFF12_CONNECTION_HTML) )
249 : {
250 : // check that valid web query properties exist
251 0 : const WebPrModel* pWebPr = xConnection->getModel().mxWebPr.get();
252 0 : if( pWebPr && !pWebPr->mbXml )
253 : {
254 0 : OUString aFileUrl = getBaseFilter().getAbsoluteUrl( pWebPr->maUrl );
255 0 : if( !aFileUrl.isEmpty() )
256 : {
257 : // resolve destination cell range (stored as defined name containing the range)
258 0 : OUString aDefName = maModel.maDefName.replace( ' ', '_' ).replace( '-', '_' );
259 0 : DefinedNameRef xDefName = getDefinedNames().getByModelName( aDefName, getSheetIndex() );
260 : OSL_ENSURE( xDefName.get(), "QueryTable::finalizeImport - missing defined name" );
261 0 : if( xDefName.get() )
262 : {
263 0 : CellRangeAddress aDestRange;
264 0 : bool bIsRange = xDefName->getAbsoluteRange( aDestRange ) && (aDestRange.Sheet == getSheetIndex());
265 : OSL_ENSURE( bIsRange, "QueryTable::finalizeImport - defined name does not contain valid cell range" );
266 0 : if( bIsRange && getAddressConverter().checkCellRange( aDestRange, false, true ) )
267 : {
268 0 : CellAddress aDestPos( aDestRange.Sheet, aDestRange.StartColumn, aDestRange.StartRow );
269 : // find tables mode: entire document, all tables, or specific tables
270 0 : OUString aTables = pWebPr->mbHtmlTables ? lclBuildWebQueryTables( pWebPr->maTables ) : "HTML_all";
271 0 : if( !aTables.isEmpty() ) try
272 : {
273 0 : PropertySet aDocProps( getDocument() );
274 0 : Reference< XAreaLinks > xAreaLinks( aDocProps.getAnyProperty( PROP_AreaLinks ), UNO_QUERY_THROW );
275 0 : OUString aFilterName = "calc_HTML_WebQuery";
276 0 : OUString aFilterOptions;
277 0 : xAreaLinks->insertAtPosition( aDestPos, aFileUrl, aTables, aFilterName, aFilterOptions );
278 : // set refresh interval (convert minutes to seconds)
279 0 : sal_Int32 nRefreshPeriod = xConnection->getModel().mnInterval * 60;
280 0 : if( nRefreshPeriod > 0 )
281 : {
282 0 : PropertySet aPropSet( lclFindAreaLink( xAreaLinks, aDestPos, aFileUrl, aTables, aFilterName, aFilterOptions ) );
283 0 : aPropSet.setProperty( PROP_RefreshPeriod, nRefreshPeriod );
284 0 : }
285 : }
286 0 : catch( Exception& )
287 : {
288 0 : }
289 : }
290 0 : }
291 0 : }
292 : }
293 0 : }
294 0 : }
295 :
296 : // ============================================================================
297 :
298 25 : QueryTableBuffer::QueryTableBuffer( const WorksheetHelper& rHelper ) :
299 25 : WorksheetHelper( rHelper )
300 : {
301 25 : }
302 :
303 0 : QueryTable& QueryTableBuffer::createQueryTable()
304 : {
305 0 : QueryTableVector::value_type xQueryTable( new QueryTable( *this ) );
306 0 : maQueryTables.push_back( xQueryTable );
307 0 : return *xQueryTable;
308 : }
309 :
310 25 : void QueryTableBuffer::finalizeImport()
311 : {
312 25 : maQueryTables.forEachMem( &QueryTable::finalizeImport );
313 25 : }
314 :
315 : // ============================================================================
316 :
317 : } // namespace xls
318 9 : } // namespace oox
319 :
320 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|