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 : #ifndef OOX_XLS_EXTERNALLINKBUFFER_HXX
21 : #define OOX_XLS_EXTERNALLINKBUFFER_HXX
22 :
23 : #include <com/sun/star/sheet/ExternalLinkInfo.hpp>
24 : #include "oox/helper/containerhelper.hxx"
25 : #include "defnamesbuffer.hxx"
26 :
27 : namespace com { namespace sun { namespace star {
28 : namespace sheet { struct DDEItemInfo; }
29 : namespace sheet { class XDDELink; }
30 : namespace sheet { class XExternalDocLink; }
31 : namespace sheet { class XExternalSheetCache; }
32 : } } }
33 :
34 : namespace oox { namespace core {
35 : class Relations;
36 : } }
37 :
38 : namespace oox {
39 : namespace xls {
40 :
41 : struct ExternalNameModel
42 : {
43 : bool mbBuiltIn; /// Name is a built-in name.
44 : bool mbNotify; /// Notify application on data change.
45 : bool mbPreferPic; /// Picture link.
46 : bool mbStdDocName; /// Name is the StdDocumentName for DDE.
47 : bool mbOleObj; /// Name is an OLE object.
48 : bool mbIconified; /// Iconified object link.
49 :
50 : explicit ExternalNameModel();
51 : };
52 :
53 : class ExternalLink;
54 :
55 0 : class ExternalName : public DefinedNameBase
56 : {
57 : public:
58 : explicit ExternalName( const ExternalLink& rParentLink );
59 :
60 : /** Appends the passed value to the result set. */
61 : template< typename Type >
62 0 : inline void appendResultValue( const Type& rValue )
63 0 : { if( maCurrIt != maResults.end() ) (*maCurrIt++) <<= rValue; }
64 :
65 : /** Imports the definedName element. */
66 : void importDefinedName( const AttributeList& rAttribs );
67 : /** Imports the ddeItem element describing an item of a DDE link. */
68 : void importDdeItem( const AttributeList& rAttribs );
69 : /** Imports the values element containing the size of the DDE result matrix. */
70 : void importValues( const AttributeList& rAttribs );
71 : /** Imports the oleItem element describing an object of an OLE link. */
72 : void importOleItem( const AttributeList& rAttribs );
73 :
74 : /** Imports the EXTERNALNAME record containing the name (only). */
75 : void importExternalName( SequenceInputStream& rStrm );
76 : /** Imports the EXTERNALNAMEFLAGS record containing the settings of an external name. */
77 : void importExternalNameFlags( SequenceInputStream& rStrm );
78 : /** Imports the DDEITEMVALUES record containing the size of the DDE result matrix. */
79 : void importDdeItemValues( SequenceInputStream& rStrm );
80 : /** Imports the DDEITEM_BOOL record containing a boolean value in a link result. */
81 : void importDdeItemBool( SequenceInputStream& rStrm );
82 : /** Imports the DDEITEM_DOUBLE record containing a double value in a link result. */
83 : void importDdeItemDouble( SequenceInputStream& rStrm );
84 : /** Imports the DDEITEM_ERROR record containing an error code in a link result. */
85 : void importDdeItemError( SequenceInputStream& rStrm );
86 : /** Imports the DDEITEM_STRING record containing a string in a link result. */
87 : void importDdeItemString( SequenceInputStream& rStrm );
88 :
89 : /** Returns true, if the name refers to an OLE object. */
90 : inline bool isOleObject() const { return maExtNameModel.mbOleObj; }
91 :
92 : #if 0
93 : /** Returns the sheet cache index if this is a sheet-local external name. */
94 : sal_Int32 getSheetCacheIndex() const;
95 : #endif
96 :
97 : /** Returns the DDE item info needed by the XML formula parser. */
98 : bool getDdeItemInfo(
99 : ::com::sun::star::sheet::DDEItemInfo& orItemInfo ) const;
100 :
101 : /** Returns the complete DDE link data of this DDE item. */
102 : bool getDdeLinkData(
103 : OUString& orDdeServer,
104 : OUString& orDdeTopic,
105 : OUString& orDdeItem );
106 :
107 : private:
108 : /** Sets the size of the result matrix. */
109 : void setResultSize( sal_Int32 nColumns, sal_Int32 nRows );
110 :
111 : private:
112 : typedef Matrix< ::com::sun::star::uno::Any > ResultMatrix;
113 :
114 : const ExternalLink& mrParentLink; /// External link this name belongs to.
115 : ExternalNameModel maExtNameModel; /// Additional name data.
116 : ResultMatrix maResults; /// DDE/OLE link results.
117 : ResultMatrix::iterator maCurrIt; /// Current position in result matrix.
118 : ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XDDELink >
119 : mxDdeLink; /// Interface of a DDE link.
120 : bool mbDdeLinkCreated; /// True = already tried to create the DDE link.
121 : };
122 :
123 : typedef ::boost::shared_ptr< ExternalName > ExternalNameRef;
124 :
125 : /** Contains indexes for a range of sheets in the spreadsheet document. */
126 : class LinkSheetRange
127 : {
128 : public:
129 0 : inline explicit LinkSheetRange() { setDeleted(); }
130 : inline explicit LinkSheetRange( sal_Int32 nFirst, sal_Int32 nLast ) { setRange( nFirst, nLast ); }
131 : inline explicit LinkSheetRange( sal_Int32 nDocLink, sal_Int32 nFirst, sal_Int32 nLast ) { setExternalRange( nDocLink, nFirst, nLast ); }
132 :
133 : /** Sets this struct to deleted state. */
134 : void setDeleted();
135 : /** Sets this struct to "use current sheet" state. */
136 : void setSameSheet();
137 : /** Sets the passed absolute sheet range to the members of this struct. */
138 : void setRange( sal_Int32 nFirst, sal_Int32 nLast );
139 : /** Sets the passed external sheet cache range to the members of this struct. */
140 : void setExternalRange( sal_Int32 nDocLink, sal_Int32 nFirst, sal_Int32 nLast );
141 :
142 : /** Returns true, if the sheet indexes are valid and different. */
143 0 : inline bool isDeleted() const { return mnFirst < 0; }
144 : /** Returns true, if the sheet range points to an external document. */
145 0 : inline bool isExternal() const { return !isDeleted() && (meType == LINKSHEETRANGE_EXTERNAL); }
146 : /** Returns true, if the sheet indexes are valid and different. */
147 0 : inline bool isSameSheet() const { return meType == LINKSHEETRANGE_SAMESHEET; }
148 : /** Returns true, if the sheet indexes are valid and different. */
149 0 : inline bool is3dRange() const { return (0 <= mnFirst) && (mnFirst < mnLast); }
150 :
151 0 : inline sal_Int32 getDocLinkIndex() const { return mnDocLink; }
152 0 : inline sal_Int32 getFirstSheet() const { return mnFirst; }
153 0 : inline sal_Int32 getLastSheet() const { return mnLast; }
154 :
155 : private:
156 : enum LinkSheetRangeType
157 : {
158 : LINKSHEETRANGE_INTERNAL, /// Sheet range in the own document.
159 : LINKSHEETRANGE_EXTERNAL, /// Sheet range in an external document.
160 : LINKSHEETRANGE_SAMESHEET /// Current sheet depending on context.
161 : };
162 :
163 : LinkSheetRangeType meType; /// Link sheet range type.
164 : sal_Int32 mnDocLink; /// Document link token index for external links.
165 : sal_Int32 mnFirst; /// Index of the first sheet or index of first external sheet cache.
166 : sal_Int32 mnLast; /// Index of the last sheet or index of last external sheet cache.
167 : };
168 :
169 : enum ExternalLinkType
170 : {
171 : LINKTYPE_SELF, /// Link refers to the current workbook.
172 : LINKTYPE_SAME, /// Link refers to the current sheet.
173 : LINKTYPE_INTERNAL, /// Link refers to a sheet in the own workbook.
174 : LINKTYPE_EXTERNAL, /// Link refers to an external spreadsheet document.
175 : LINKTYPE_ANALYSIS, /// Link refers to the Analysis add-in.
176 : LINKTYPE_LIBRARY, /// Link refers to an external add-in.
177 : LINKTYPE_DDE, /// DDE link.
178 : LINKTYPE_OLE, /// OLE link.
179 : LINKTYPE_MAYBE_DDE_OLE, /// Could be DDE or OLE link (BIFF only).
180 : LINKTYPE_UNKNOWN /// Unknown or unsupported link type.
181 : };
182 :
183 90 : class ExternalLink : public WorkbookHelper
184 : {
185 : public:
186 : explicit ExternalLink( const WorkbookHelper& rHelper );
187 :
188 : /** Imports the externalReference element containing the relation identifier. */
189 : void importExternalReference( const AttributeList& rAttribs );
190 : /** Imports the externalBook element describing an externally linked document. */
191 : void importExternalBook( const ::oox::core::Relations& rRelations, const AttributeList& rAttribs );
192 : /** Imports the sheetName element containing the sheet name in an externally linked document. */
193 : void importSheetName( const AttributeList& rAttribs );
194 : /** Imports the definedName element describing an external name. */
195 : void importDefinedName( const AttributeList& rAttribs );
196 : /** Imports the ddeLink element describing a DDE link. */
197 : void importDdeLink( const AttributeList& rAttribs );
198 : /** Imports the ddeItem element describing an item of a DDE link. */
199 : ExternalNameRef importDdeItem( const AttributeList& rAttribs );
200 : /** Imports the oleLink element describing an OLE link. */
201 : void importOleLink( const ::oox::core::Relations& rRelations, const AttributeList& rAttribs );
202 : /** Imports the oleItem element describing an object of an OLE link. */
203 : ExternalNameRef importOleItem( const AttributeList& rAttribs );
204 :
205 : /** Imports the EXTERNALBOOK record describing an externally linked document, DDE link, or OLE link. */
206 : void importExternalBook( const ::oox::core::Relations& rRelations, SequenceInputStream& rStrm );
207 : /** Imports the EXTSHEETNAMES record containing the sheet names in an externally linked document. */
208 : void importExtSheetNames( SequenceInputStream& rStrm );
209 : /** Imports the EXTERNALNAME record describing an external name. */
210 : ExternalNameRef importExternalName( SequenceInputStream& rStrm );
211 : /** Imports the EXTERNALREF record from the passed stream. */
212 : void importExternalRef( SequenceInputStream& rStrm );
213 : /** Imports the EXTERNALSELF record from the passed stream. */
214 : void importExternalSelf( SequenceInputStream& rStrm );
215 : /** Imports the EXTERNALSAME record from the passed stream. */
216 : void importExternalSame( SequenceInputStream& rStrm );
217 : /** Imports the EXTERNALADDIN record from the passed stream. */
218 : void importExternalAddin( SequenceInputStream& rStrm );
219 :
220 : /** Sets the link type to 'self reference'. */
221 44 : inline void setSelfLinkType() { meLinkType = LINKTYPE_SELF; }
222 :
223 : /** Returns the type of this external link. */
224 3 : inline ExternalLinkType getLinkType() const { return meLinkType; }
225 : /** Returns true, if the link refers to the current workbook. */
226 0 : inline bool isInternalLink() const { return (meLinkType == LINKTYPE_SELF) || (meLinkType == LINKTYPE_INTERNAL); }
227 :
228 : /** Returns the relation identifier for the external link fragment. */
229 1 : inline const OUString& getRelId() const { return maRelId; }
230 : /** Returns the class name of this external link. */
231 0 : inline const OUString& getClassName() const { return maClassName; }
232 : /** Returns the target URL of this external link. */
233 0 : inline const OUString& getTargetUrl() const { return maTargetUrl; }
234 : /** Returns the link info needed by the XML formula parser. */
235 : ::com::sun::star::sheet::ExternalLinkInfo getLinkInfo() const;
236 :
237 : /** Returns the type of the external library if this is a library link. */
238 : FunctionLibraryType getFuncLibraryType() const;
239 :
240 : /** Returns the token index of the external document. */
241 : sal_Int32 getDocumentLinkIndex() const;
242 : /** Returns the external sheet cache index or for the passed sheet. */
243 : sal_Int32 getSheetCacheIndex( sal_Int32 nTabId = 0 ) const;
244 : /** Returns the sheet cache of the external sheet with the passed index. */
245 : ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XExternalSheetCache >
246 : getSheetCache( sal_Int32 nTabId ) const;
247 :
248 : /** Returns the internal sheet range or range of external sheet caches for the passed sheet range (BIFF only). */
249 : void getSheetRange( LinkSheetRange& orSheetRange, sal_Int32 nTabId1, sal_Int32 nTabId2 ) const;
250 :
251 : /** Returns the external name with the passed zero-based index. */
252 : ExternalNameRef getNameByIndex( sal_Int32 nIndex ) const;
253 :
254 : private:
255 : void setExternalTargetUrl( const OUString& rTargetUrl, const OUString& rTargetType );
256 : void setDdeOleTargetUrl( const OUString& rClassName, const OUString& rTargetUrl, ExternalLinkType eLinkType );
257 : void parseExternalReference( const ::oox::core::Relations& rRelations, const OUString& rRelId );
258 :
259 : /** Creates an external locument link and the sheet cache for the passed sheet name. */
260 : void insertExternalSheet( const OUString& rSheetName );
261 :
262 : ExternalNameRef createExternalName();
263 :
264 : private:
265 : typedef ::std::vector< sal_Int16 > Int16Vector;
266 : typedef ::std::vector< sal_Int32 > Int32Vector;
267 : typedef RefVector< ExternalName > ExternalNameVector;
268 :
269 : ExternalLinkType meLinkType; /// Type of this link object.
270 : FunctionLibraryType meFuncLibType; /// Type of the function library, if link type is LINKTYPE_LIBRARY.
271 : OUString maRelId; /// Relation identifier for the external link fragment.
272 : OUString maClassName; /// DDE service, OLE class name.
273 : OUString maTargetUrl; /// Target link, DDE topic, OLE target.
274 : ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XExternalDocLink >
275 : mxDocLink; /// Interface for an external document.
276 : Int16Vector maCalcSheets; /// Internal sheet indexes.
277 : Int32Vector maSheetCaches; /// External sheet cache indexes.
278 : ExternalNameVector maExtNames; /// Defined names in external document.
279 : };
280 :
281 : typedef ::boost::shared_ptr< ExternalLink > ExternalLinkRef;
282 :
283 : /** Represents a REF entry in the BIFF12 EXTERNALSHEETS or in the BIFF8
284 : EXTERNSHEET record.
285 :
286 : This struct is used to map ref identifiers to external books (BIFF12:
287 : EXTERNALREF records, BIFF8: EXTERNALBOOK records), and provides sheet
288 : indexes into the sheet list of the external document.
289 : */
290 : struct RefSheetsModel
291 : {
292 : sal_Int32 mnExtRefId; /// Zero-based index into list of external documents.
293 : sal_Int32 mnTabId1; /// Zero-based index to first sheet in external document.
294 : sal_Int32 mnTabId2; /// Zero-based index to last sheet in external document.
295 :
296 : explicit RefSheetsModel();
297 :
298 : void readBiff12Data( SequenceInputStream& rStrm );
299 : };
300 :
301 88 : class ExternalLinkBuffer : public WorkbookHelper
302 : {
303 : public:
304 : explicit ExternalLinkBuffer( const WorkbookHelper& rHelper );
305 :
306 : /** Imports the externalReference element containing . */
307 : ExternalLinkRef importExternalReference( const AttributeList& rAttribs );
308 :
309 : /** Imports the EXTERNALREF record from the passed stream. */
310 : ExternalLinkRef importExternalRef( SequenceInputStream& rStrm );
311 : /** Imports the EXTERNALSELF record from the passed stream. */
312 : void importExternalSelf( SequenceInputStream& rStrm );
313 : /** Imports the EXTERNALSAME record from the passed stream. */
314 : void importExternalSame( SequenceInputStream& rStrm );
315 : /** Imports the EXTERNALADDIN record from the passed stream. */
316 : void importExternalAddin( SequenceInputStream& rStrm );
317 : /** Imports the EXTERNALSHEETS record from the passed stream. */
318 : void importExternalSheets( SequenceInputStream& rStrm );
319 :
320 : /** Returns the sequence of link infos needed by the XML formula parser. */
321 : ::com::sun::star::uno::Sequence< ::com::sun::star::sheet::ExternalLinkInfo >
322 : getLinkInfos() const;
323 :
324 : /** Returns the external link for the passed reference identifier. */
325 : ExternalLinkRef getExternalLink( sal_Int32 nRefId, bool bUseRefSheets = true ) const;
326 :
327 : /** Returns the sheet range for the specified reference (BIFF2-BIFF5 only). */
328 : LinkSheetRange getSheetRange( sal_Int32 nRefId, sal_Int16 nTabId1, sal_Int16 nTabId2 ) const;
329 : /** Returns the sheet range for the specified reference (BIFF8 only). */
330 : LinkSheetRange getSheetRange( sal_Int32 nRefId ) const;
331 :
332 : private:
333 : /** Creates a new external link and inserts it into the list of links. */
334 : ExternalLinkRef createExternalLink();
335 :
336 : /** Returns the specified sheet indexes for a reference identifier. */
337 : const RefSheetsModel* getRefSheets( sal_Int32 nRefId ) const;
338 :
339 : private:
340 : typedef RefVector< ExternalLink > ExternalLinkVec;
341 : typedef ::std::vector< RefSheetsModel > RefSheetsModelVec;
342 :
343 : ExternalLinkRef mxSelfRef; /// Implicit self reference at index 0.
344 : ExternalLinkVec maLinks; /// List of link structures for all kinds of links.
345 : ExternalLinkVec maExtLinks; /// Real external links needed for formula parser.
346 : RefSheetsModelVec maRefSheets; /// Sheet indexes for reference ids.
347 : bool mbUseRefSheets; /// True = use maRefSheets list (BIFF12 only).
348 : };
349 :
350 : } // namespace xls
351 : } // namespace oox
352 :
353 : #endif
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|