Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / externallinkbuffer.hxx
blob374a54b38ccdc5aa0a54950ac3ee88f3d0cb7e39
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #pragma once
22 #include <ostream>
24 #include <com/sun/star/sheet/ExternalLinkInfo.hpp>
25 #include <oox/helper/containerhelper.hxx>
26 #include "defnamesbuffer.hxx"
27 #include "formulabase.hxx"
29 namespace com::sun::star {
30 namespace sheet { struct DDEItemInfo; }
31 namespace sheet { class XDDELink; }
32 namespace sheet { class XExternalDocLink; }
33 namespace sheet { class XExternalSheetCache; }
36 namespace oox::core {
37 class Relations;
40 namespace oox::xls {
42 struct ExternalNameModel
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();
53 class ExternalLink;
55 class ExternalName : public DefinedNameBase
57 public:
58 explicit ExternalName( const ExternalLink& rParentLink );
60 /** Appends the passed value to the result set. */
61 template< typename Type >
62 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 the DDE item info needed by the XML formula parser. */
90 bool getDdeItemInfo(
91 css::sheet::DDEItemInfo& orItemInfo ) const;
93 /** Returns the complete DDE link data of this DDE item. */
94 bool getDdeLinkData(
95 OUString& orDdeServer,
96 OUString& orDdeTopic,
97 OUString& orDdeItem );
99 private:
100 /** Sets the size of the result matrix. */
101 void setResultSize( sal_Int32 nColumns, sal_Int32 nRows );
103 private:
104 typedef Matrix< css::uno::Any > ResultMatrix;
106 const ExternalLink& mrParentLink; /// External link this name belongs to.
107 ExternalNameModel maExtNameModel; /// Additional name data.
108 ResultMatrix maResults; /// DDE/OLE link results.
109 ResultMatrix::iterator maCurrIt; /// Current position in result matrix.
110 css::uno::Reference< css::sheet::XDDELink >
111 mxDdeLink; /// Interface of a DDE link.
112 bool mbDdeLinkCreated; /// True = already tried to create the DDE link.
115 typedef std::shared_ptr< ExternalName > ExternalNameRef;
117 /** Contains indexes for a range of sheets in the spreadsheet document. */
118 class LinkSheetRange
120 public:
121 explicit LinkSheetRange() { setDeleted(); }
123 /** Sets this struct to deleted state. */
124 void setDeleted();
125 /** Sets this struct to "use current sheet" state. */
126 void setSameSheet();
127 /** Sets the passed absolute sheet range to the members of this struct. */
128 void setRange( sal_Int32 nFirst, sal_Int32 nLast );
129 /** Sets the passed external sheet cache range to the members of this struct. */
130 void setExternalRange( sal_Int32 nDocLink, sal_Int32 nFirst, sal_Int32 nLast );
132 /** Returns true, if the sheet indexes are valid and different. */
133 bool isDeleted() const { return mnFirst < 0; }
134 /** Returns true, if the sheet range points to an external document. */
135 bool isExternal() const { return !isDeleted() && (meType == LINKSHEETRANGE_EXTERNAL); }
136 /** Returns true, if the sheet indexes are valid and different. */
137 bool isSameSheet() const { return meType == LINKSHEETRANGE_SAMESHEET; }
138 /** Returns true, if the sheet indexes are valid and different. */
139 bool is3dRange() const { return (0 <= mnFirst) && (mnFirst < mnLast); }
141 sal_Int32 getDocLinkIndex() const { return mnDocLink; }
142 sal_Int32 getFirstSheet() const { return mnFirst; }
143 sal_Int32 getLastSheet() const { return mnLast; }
145 private:
146 enum LinkSheetRangeType
148 LINKSHEETRANGE_INTERNAL, /// Sheet range in the own document.
149 LINKSHEETRANGE_EXTERNAL, /// Sheet range in an external document.
150 LINKSHEETRANGE_SAMESHEET /// Current sheet depending on context.
153 LinkSheetRangeType meType; /// Link sheet range type.
154 sal_Int32 mnDocLink; /// Document link token index for external links.
155 sal_Int32 mnFirst; /// Index of the first sheet or index of first external sheet cache.
156 sal_Int32 mnLast; /// Index of the last sheet or index of last external sheet cache.
159 enum class ExternalLinkType
161 Self, /// Link refers to the current workbook.
162 Same, /// Link refers to the current sheet.
163 External, /// Link refers to an external spreadsheet document.
164 // let's ignore xlStartup and xlAlternateStartup for now
165 PathMissing, /// Just for round-tripping (FIXME: Functionality not actually implemented after all.)
166 Library, /// Link refers to an external add-in.
167 DDE, /// DDE link.
168 OLE, /// OLE link.
169 Unknown /// Unknown or unsupported link type.
172 template< typename charT, typename traits >
173 inline std::basic_ostream<charT, traits> & operator <<(
174 std::basic_ostream<charT, traits> & stream, const ExternalLinkType& type )
176 switch (type)
178 case ExternalLinkType::Self: return stream << "self";
179 case ExternalLinkType::Same: return stream << "same";
180 case ExternalLinkType::External: return stream << "external";
181 case ExternalLinkType::PathMissing: return stream << "pathmissing";
182 case ExternalLinkType::Library: return stream << "library";
183 case ExternalLinkType::DDE: return stream << "dde";
184 case ExternalLinkType::OLE: return stream << "ole";
185 case ExternalLinkType::Unknown: return stream << "unknown";
186 default: return stream << static_cast<int>(type);
190 class ExternalLink : public WorkbookHelper
192 public:
193 explicit ExternalLink( const WorkbookHelper& rHelper );
195 /** Imports the externalReference element containing the relation identifier. */
196 void importExternalReference( const AttributeList& rAttribs );
197 /** Imports the externalBook element describing an externally linked document. */
198 void importExternalBook( const ::oox::core::Relations& rRelations, const AttributeList& rAttribs );
199 /** Imports the sheetName element containing the sheet name in an externally linked document. */
200 void importSheetName( const AttributeList& rAttribs );
201 /** Imports the definedName element describing an external name. */
202 void importDefinedName( const AttributeList& rAttribs );
203 /** Imports the ddeLink element describing a DDE link. */
204 void importDdeLink( const AttributeList& rAttribs );
205 /** Imports the ddeItem element describing an item of a DDE link. */
206 ExternalNameRef importDdeItem( const AttributeList& rAttribs );
207 /** Imports the oleLink element describing an OLE link. */
208 void importOleLink( const ::oox::core::Relations& rRelations, const AttributeList& rAttribs );
209 /** Imports the oleItem element describing an object of an OLE link. */
210 ExternalNameRef importOleItem( const AttributeList& rAttribs );
212 /** Imports the EXTERNALBOOK record describing an externally linked document, DDE link, or OLE link. */
213 void importExternalBook( const ::oox::core::Relations& rRelations, SequenceInputStream& rStrm );
214 /** Imports the EXTSHEETNAMES record containing the sheet names in an externally linked document. */
215 void importExtSheetNames( SequenceInputStream& rStrm );
216 /** Imports the EXTERNALNAME record describing an external name. */
217 ExternalNameRef importExternalName( SequenceInputStream& rStrm );
218 /** Imports the EXTERNALREF record from the passed stream. */
219 void importExternalRef( SequenceInputStream& rStrm );
220 /** Imports the EXTERNALSELF record from the passed stream. */
221 void importExternalSelf( SequenceInputStream& rStrm );
222 /** Imports the EXTERNALSAME record from the passed stream. */
223 void importExternalSame( SequenceInputStream& rStrm );
224 /** Imports the EXTERNALADDIN record from the passed stream. */
225 void importExternalAddin( SequenceInputStream& rStrm );
227 /** Sets the link type to 'self reference'. */
228 void setSelfLinkType() { meLinkType = ExternalLinkType::Self; }
230 /** Returns the type of this external link. */
231 ExternalLinkType getLinkType() const { return meLinkType; }
233 /** Returns the relation identifier for the external link fragment. */
234 const OUString& getRelId() const { return maRelId; }
235 /** Returns the class name of this external link. */
236 const OUString& getClassName() const { return maClassName; }
237 /** Returns the target URL of this external link. */
238 const OUString& getTargetUrl() const { return maTargetUrl; }
239 /** Returns the link info needed by the XML formula parser. */
240 css::sheet::ExternalLinkInfo getLinkInfo() const;
242 /** Returns the type of the external library if this is a library link. */
243 FunctionLibraryType getFuncLibraryType() const;
245 /** Returns the token index of the external document. */
246 sal_Int32 getDocumentLinkIndex() const;
247 /** Returns the external sheet cache index or for the passed sheet. */
248 sal_Int32 getSheetCacheIndex( sal_Int32 nTabId ) const;
249 /** Returns the sheet cache of the external sheet with the passed index. */
250 css::uno::Reference< css::sheet::XExternalSheetCache >
251 getSheetCache( sal_Int32 nTabId ) const;
253 /** Returns the internal sheet range or range of external sheet caches for the passed sheet range (BIFF only). */
254 void getSheetRange( LinkSheetRange& orSheetRange, sal_Int32 nTabId1, sal_Int32 nTabId2 ) const;
256 /** Returns the external name with the passed zero-based index. */
257 ExternalNameRef getNameByIndex( sal_Int32 nIndex ) const;
259 private:
260 void setExternalTargetUrl( const OUString& rTargetUrl, const OUString& rTargetType );
261 void setDdeOleTargetUrl( const OUString& rClassName, const OUString& rTargetUrl, ExternalLinkType eLinkType );
262 void parseExternalReference( const ::oox::core::Relations& rRelations, const OUString& rRelId );
264 /** Creates an external document link and the sheet cache for the passed sheet name. */
265 void insertExternalSheet( const OUString& rSheetName );
267 ExternalNameRef createExternalName();
269 private:
270 ExternalLinkType meLinkType; /// Type of this link object.
271 FunctionLibraryType meFuncLibType; /// Type of the function library, if link type is ExternalLinkType::Library.
272 OUString maRelId; /// Relation identifier for the external link fragment.
273 OUString maClassName; /// DDE service, OLE class name.
274 OUString maTargetUrl; /// Target link, DDE topic, OLE target.
275 css::uno::Reference< css::sheet::XExternalDocLink >
276 mxDocLink; /// Interface for an external document.
277 std::vector< sal_Int32 > maSheetCaches; /// External sheet cache indexes.
278 RefVector< ExternalName > 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
284 EXTERNSHEET record.
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
303 public:
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 css::uno::Sequence< css::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 (BIFF8 only). */
328 LinkSheetRange getSheetRange( sal_Int32 nRefId ) const;
330 private:
331 /** Creates a new external link and inserts it into the list of links. */
332 ExternalLinkRef createExternalLink();
334 /** Returns the specified sheet indexes for a reference identifier. */
335 const RefSheetsModel* getRefSheets( sal_Int32 nRefId ) const;
337 private:
338 typedef RefVector< ExternalLink > ExternalLinkVec;
339 typedef ::std::vector< RefSheetsModel > RefSheetsModelVec;
341 ExternalLinkRef mxSelfRef; /// Implicit self reference at index 0.
342 ExternalLinkVec maLinks; /// List of link structures for all kinds of links.
343 ExternalLinkVec maExtLinks; /// Real external links needed for formula parser.
344 RefSheetsModelVec maRefSheets; /// Sheet indexes for reference ids.
345 bool mbUseRefSheets; /// True = use maRefSheets list (BIFF12 only).
348 } // namespace oox::xls
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */