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