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 SC_XILINK_HXX
21 : #define SC_XILINK_HXX
22 :
23 : #include <map>
24 : #include "xllink.hxx"
25 : #include "xiroot.hxx"
26 : #include "scmatrix.hxx"
27 :
28 : /* ============================================================================
29 : Classes for import of different kinds of internal/external references.
30 : - 3D cell and cell range links
31 : - External cell and cell range links
32 : - External defined names
33 : - Add-in functions
34 : - DDE links
35 : - OLE object links
36 : ============================================================================ */
37 :
38 : // Excel sheet indexes ========================================================
39 :
40 : /** A buffer containing information about names and creation order of sheets.
41 :
42 : The first purpose of this buffer is to translate original Excel
43 : sheet names into Calc sheet indexes. This is not trivial because the filter
44 : may rename the Calc sheets during creation. This buffer stores the original
45 : Excel sheet names with the corresponding Calc sheet indexes.
46 :
47 : The second purpose is to store the creation order of all sheets inside the
48 : Excel workbook. The creation order list is contained in the TABID record
49 : and needed to import the change log. Example: If the list contains 3;1;2
50 : this means that the second sheet in the file was created first, than the
51 : third sheet in the file was created and finally the first sheet.
52 : */
53 40 : class XclImpTabInfo
54 : {
55 : public:
56 : // original Excel sheet names ---------------------------------------------
57 :
58 : /** Appends an original Excel sheet name with corresponding Calc sheet index. */
59 : void AppendXclTabName( const String& rXclTabName, SCTAB nScTab );
60 : /** Inserts a Calc sheet index (increases all following sheet indexes). */
61 : void InsertScTab( SCTAB nScTab );
62 :
63 : /** Returns the Calc sheet index from the passed original Excel sheet name. */
64 : SCTAB GetScTabFromXclName( const String& rXclTabName ) const;
65 :
66 : // record creation order - TABID record -----------------------------------
67 :
68 : /** Reads the TABID record. */
69 : void ReadTabid( XclImpStream& rStrm );
70 :
71 : /** Returns the current sheet index calculated from creation index.
72 : @param nCreatedId The creation index of the sheet (1-based).
73 : @param nMaxTabId All values greater than this parameter are not used to find the index.
74 : @return The 0-based index of the sheet nCreatedId if it is contained in the list.
75 : Example: The buffer is 3;5;2;4;1, nCreatedId is 1 and nMaxTabId is 3. The function will
76 : return 2 which is the 0-based index of sheet 1 in the list 3;2;1. */
77 : sal_uInt16 GetCurrentIndex( sal_uInt16 nCreatedId, sal_uInt16 nMaxTabId = 0xFFFF ) const;
78 :
79 : private:
80 : typedef ::std::map< String, SCTAB > XclTabNameMap;
81 :
82 : XclTabNameMap maTabNames; /// All Excel sheet names with Calc sheet index.
83 : ScfUInt16Vec maTabIdVec; /// The vector with sheet indexes.
84 : };
85 :
86 : // External names =============================================================
87 :
88 : /** Type of an external name. */
89 : enum XclImpExtNameType
90 : {
91 : xlExtName, /// An external defined name.
92 : xlExtAddIn, /// An add-in function name.
93 : xlExtDDE, /// A DDE link range.
94 : xlExtOLE, /// An OLE object link.
95 : xlExtEuroConvert /// An external in Excel, but internal in OO function name.
96 : };
97 :
98 : // ----------------------------------------------------------------------------
99 :
100 : class XclImpCachedMatrix;
101 : class ScTokenArray;
102 : class XclImpSupbook;
103 :
104 : /** Stores contents of an external name.
105 : @descr Supported: External defined names, AddIn names, DDE links and OLE objects. */
106 : class XclImpExtName
107 : {
108 : /**
109 : * MOper, multiple operands, stores cached values of external range
110 : * specified in the record.
111 : */
112 0 : class MOper
113 : {
114 : public:
115 : MOper(XclImpStream& rStrm);
116 : const ScMatrix& GetCache() const;
117 : private:
118 : ScMatrixRef mxCached;
119 : };
120 :
121 : public:
122 : /** Reads the external name from the stream. */
123 : explicit XclImpExtName( const XclImpSupbook& rSupbook, XclImpStream& rStrm,
124 : XclSupbookType eSubType, ExcelToSc* pFormulaConv );
125 : ~XclImpExtName();
126 :
127 : /** Create and apply the cached list of this DDE Link to the document. */
128 : void CreateDdeData( ScDocument& rDoc,
129 : const String& rApplc, const String& rExtDoc ) const;
130 :
131 : void CreateExtNameData( ScDocument& rDoc, sal_uInt16 nFileId ) const;
132 :
133 : /**
134 : * Create OLE link data. OLE link data is converted to external
135 : * reference, since OLE link doesn't work cross-platform, and is not very
136 : * reliable even on Windows.
137 : */
138 : bool CreateOleData(ScDocument& rDoc, const ::rtl::OUString& rUrl,
139 : sal_uInt16& rFileId, ::rtl::OUString& rTabName, ScRange& rRange) const;
140 :
141 : bool HasFormulaTokens() const;
142 :
143 0 : inline XclImpExtNameType GetType() const { return meType; }
144 0 : inline const String& GetName() const { return maName; }
145 0 : inline sal_uInt32 GetStorageId() const { return mnStorageId; }
146 :
147 : private:
148 : typedef ::std::auto_ptr< XclImpCachedMatrix > XclImpCachedMatrixPtr;
149 : typedef ::std::auto_ptr< ScTokenArray > TokenArrayPtr;
150 :
151 : XclImpCachedMatrixPtr mxDdeMatrix; /// Cached results of the DDE link.
152 : MOper* mpMOper; /// Cached values for OLE link
153 : TokenArrayPtr mxArray; /// Formula tokens for external name.
154 : String maName; /// The name of the external name.
155 : sal_uInt32 mnStorageId; /// Storage ID for OLE object storages.
156 : XclImpExtNameType meType; /// Type of the external name.
157 : };
158 :
159 : // Import link manager ========================================================
160 :
161 : class XclImpLinkManagerImpl;
162 :
163 : /** This is the central class for the import of all internal/external links.
164 : @descr This manager stores all data about external documents with their sheets
165 : and cached cell contents. Additionally it handles external names, such as add-in
166 : function names, DDE links, and OLE object links.
167 : File contents in BIFF8:
168 : - Record SUPBOOK: Contains the name of an external document and the names of its sheets.
169 : This record is optionally followed by NAME, EXTERNNAME, XCT and CRN records.
170 : - Record XCT: Contains the number and sheet index of the following CRN records.
171 : - Record CRN: Contains addresses (row and column) and values of external referenced cells.
172 : - Record NAME: Contains defined names of the own workbook.
173 : - Record EXTERNNAME: Contains external defined names, DDE links, or OLE object links.
174 : - Record EXTERNSHEET: Contains indexes to URLs of external documents (SUPBOOKs)
175 : and sheet indexes for each external reference used anywhere in the workbook.
176 : This record follows a list of SUPBOOK records (with their attached records).
177 : */
178 : class XclImpLinkManager : protected XclImpRoot
179 : {
180 : public:
181 : explicit XclImpLinkManager( const XclImpRoot& rRoot );
182 : ~XclImpLinkManager();
183 :
184 : /** Reads the EXTERNSHEET record. */
185 : void ReadExternsheet( XclImpStream& rStrm );
186 : /** Reads a SUPBOOK record. */
187 : void ReadSupbook( XclImpStream& rStrm );
188 : /** Reads an XCT record and appends it to the current SUPBOOK. */
189 : void ReadXct( XclImpStream& rStrm );
190 : /** Reads a CRN record and appends it to the current SUPBOOK. */
191 : void ReadCrn( XclImpStream& rStrm );
192 : /** Reads an EXTERNNAME record and appends it to the current SUPBOOK. */
193 : void ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv = NULL );
194 :
195 : /** Returns true, if the specified XTI entry contains an internal reference. */
196 : bool IsSelfRef( sal_uInt16 nXtiIndex ) const;
197 : /** Returns the Calc sheet index range of the specified XTI entry.
198 : @return true = XTI data found, returned sheet index range is valid. */
199 : bool GetScTabRange(
200 : SCTAB& rnFirstScTab, SCTAB& rnLastScTab,
201 : sal_uInt16 nXtiIndex ) const;
202 : /** Returns the specified external name or 0 on error. */
203 : const XclImpExtName* GetExternName( sal_uInt16 nXtiIndex, sal_uInt16 nExtName ) const;
204 :
205 : const String* GetSupbookUrl( sal_uInt16 nXtiIndex ) const;
206 :
207 : const String& GetSupbookTabName( sal_uInt16 nXti, sal_uInt16 nXtiTab ) const;
208 :
209 : /** Tries to decode the URL of the specified XTI entry to OLE or DDE link components.
210 : @descr For DDE links: Decodes to application name and topic.
211 : For OLE object links: Decodes to class name and document URL.
212 : @return true = decoding was successful, returned strings are valid (not empty). */
213 : bool GetLinkData( String& rApplic, String& rTopic, sal_uInt16 nXtiIndex ) const;
214 : /** Returns the specified macro name or an empty string on error. */
215 : const String& GetMacroName( sal_uInt16 nExtSheet, sal_uInt16 nExtName ) const;
216 :
217 : private:
218 : typedef ::std::auto_ptr< XclImpLinkManagerImpl > XclImpLinkMgrImplPtr;
219 : XclImpLinkMgrImplPtr mxImpl;
220 : };
221 :
222 : // ============================================================================
223 :
224 : #endif
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|