1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_EXTERNALLINKBUFFER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_EXTERNALLINKBUFFER_HXX
23 #include <com/sun/star/sheet/ExternalLinkInfo.hpp>
24 #include <oox/helper/containerhelper.hxx>
25 #include "defnamesbuffer.hxx"
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
; }
34 namespace oox
{ namespace core
{
41 struct ExternalNameModel
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.
50 explicit ExternalNameModel();
55 class ExternalName
: public DefinedNameBase
58 explicit ExternalName( const ExternalLink
& rParentLink
);
60 /** Appends the passed value to the result set. */
61 template< typename Type
>
62 inline void appendResultValue( const Type
& rValue
)
63 { if( maCurrIt
!= maResults
.end() ) (*maCurrIt
++) <<= rValue
; }
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
);
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
);
89 /** Returns true, if the name refers to an OLE object. */
90 inline bool isOleObject() const { return maExtNameModel
.mbOleObj
; }
93 /** Returns the sheet cache index if this is a sheet-local external name. */
94 sal_Int32
getSheetCacheIndex() const;
97 /** Returns the DDE item info needed by the XML formula parser. */
99 ::com::sun::star::sheet::DDEItemInfo
& orItemInfo
) const;
101 /** Returns the complete DDE link data of this DDE item. */
103 OUString
& orDdeServer
,
104 OUString
& orDdeTopic
,
105 OUString
& orDdeItem
);
108 /** Sets the size of the result matrix. */
109 void setResultSize( sal_Int32 nColumns
, sal_Int32 nRows
);
112 typedef Matrix
< ::com::sun::star::uno::Any
> ResultMatrix
;
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.
123 typedef std::shared_ptr
< ExternalName
> ExternalNameRef
;
125 /** Contains indexes for a range of sheets in the spreadsheet document. */
129 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
); }
133 /** Sets this struct to deleted state. */
135 /** Sets this struct to "use current sheet" state. */
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
);
142 /** Returns true, if the sheet indexes are valid and different. */
143 inline bool isDeleted() const { return mnFirst
< 0; }
144 /** Returns true, if the sheet range points to an external document. */
145 inline bool isExternal() const { return !isDeleted() && (meType
== LINKSHEETRANGE_EXTERNAL
); }
146 /** Returns true, if the sheet indexes are valid and different. */
147 inline bool isSameSheet() const { return meType
== LINKSHEETRANGE_SAMESHEET
; }
148 /** Returns true, if the sheet indexes are valid and different. */
149 inline bool is3dRange() const { return (0 <= mnFirst
) && (mnFirst
< mnLast
); }
151 inline sal_Int32
getDocLinkIndex() const { return mnDocLink
; }
152 inline sal_Int32
getFirstSheet() const { return mnFirst
; }
153 inline sal_Int32
getLastSheet() const { return mnLast
; }
156 enum LinkSheetRangeType
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.
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.
169 enum ExternalLinkType
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.
183 class ExternalLink
: public WorkbookHelper
186 explicit ExternalLink( const WorkbookHelper
& rHelper
);
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
);
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
);
220 /** Sets the link type to 'self reference'. */
221 inline void setSelfLinkType() { meLinkType
= LINKTYPE_SELF
; }
223 /** Returns the type of this external link. */
224 inline ExternalLinkType
getLinkType() const { return meLinkType
; }
225 /** Returns true, if the link refers to the current workbook. */
226 inline bool isInternalLink() const { return (meLinkType
== LINKTYPE_SELF
) || (meLinkType
== LINKTYPE_INTERNAL
); }
228 /** Returns the relation identifier for the external link fragment. */
229 inline const OUString
& getRelId() const { return maRelId
; }
230 /** Returns the class name of this external link. */
231 inline const OUString
& getClassName() const { return maClassName
; }
232 /** Returns the target URL of this external link. */
233 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;
237 /** Returns the type of the external library if this is a library link. */
238 FunctionLibraryType
getFuncLibraryType() const;
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;
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;
251 /** Returns the external name with the passed zero-based index. */
252 ExternalNameRef
getNameByIndex( sal_Int32 nIndex
) const;
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
);
259 /** Creates an external locument link and the sheet cache for the passed sheet name. */
260 void insertExternalSheet( const OUString
& rSheetName
);
262 ExternalNameRef
createExternalName();
265 typedef ::std::vector
< sal_Int16
> Int16Vector
;
266 typedef ::std::vector
< sal_Int32
> Int32Vector
;
267 typedef RefVector
< ExternalName
> ExternalNameVector
;
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.
281 typedef std::shared_ptr
< ExternalLink
> ExternalLinkRef
;
283 /** Represents a REF entry in the BIFF12 EXTERNALSHEETS or in the BIFF8
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.
290 struct RefSheetsModel
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.
296 explicit RefSheetsModel();
298 void readBiff12Data( SequenceInputStream
& rStrm
);
301 class ExternalLinkBuffer
: public WorkbookHelper
304 explicit ExternalLinkBuffer( const WorkbookHelper
& rHelper
);
306 /** Imports the externalReference element containing . */
307 ExternalLinkRef
importExternalReference( const AttributeList
& rAttribs
);
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
);
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;
324 /** Returns the external link for the passed reference identifier. */
325 ExternalLinkRef
getExternalLink( sal_Int32 nRefId
, bool bUseRefSheets
= true ) const;
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;
333 /** Creates a new external link and inserts it into the list of links. */
334 ExternalLinkRef
createExternalLink();
336 /** Returns the specified sheet indexes for a reference identifier. */
337 const RefSheetsModel
* getRefSheets( sal_Int32 nRefId
) const;
340 typedef RefVector
< ExternalLink
> ExternalLinkVec
;
341 typedef ::std::vector
< RefSheetsModel
> RefSheetsModelVec
;
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).
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */