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