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