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