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