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