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_INC_EXTERNALREFMGR_HXX
21 #define INCLUDED_SC_INC_EXTERNALREFMGR_HXX
23 #include "address.hxx"
24 #include "document.hxx"
25 #include <sfx2/objsh.hxx>
26 #include <sfx2/lnkbase.hxx>
27 #include <tools/time.hxx>
28 #include <vcl/timer.hxx>
29 #include <svl/zforlist.hxx>
30 #include <svl/lstner.hxx>
32 #include "rangelst.hxx"
33 #include <osl/mutex.hxx>
34 #include <formula/types.hxx>
35 #include <tools/solar.h>
38 #include <unordered_map>
39 #include <unordered_set>
42 #include <o3tl/sorted_vector.hxx>
43 #include <formula/ExternalReferenceHelper.hxx>
46 namespace weld
{ class Window
; }
56 class ScExternalRefLink final
: public ::sfx2::SvBaseLink
59 ScExternalRefLink(ScDocument
& rDoc
, sal_uInt16 nFileId
);
60 virtual ~ScExternalRefLink() override
;
62 virtual void Closed() override
;
63 virtual ::sfx2::SvBaseLink::UpdateResult
DataChanged(
64 const OUString
& rMimeType
, const css::uno::Any
& rValue
) override
;
65 virtual void Edit(weld::Window
* pParent
, const Link
<SvBaseLink
&,void>& rEndEditHdl
) override
;
67 void SetDoRefresh(bool b
);
70 ScExternalRefLink() = delete;
71 ScExternalRefLink(const ScExternalRefLink
&) = delete;
79 * Cache table for external reference data.
81 class ScExternalRefCache
84 typedef ::formula::FormulaTokenRef TokenRef
;
85 typedef std::shared_ptr
<ScTokenArray
> TokenArrayRef
;
92 explicit TableName(const OUString
& rUpper
, const OUString
& rReal
);
98 SvNumFormatType mnType
;
101 explicit CellFormat();
105 /** individual cell within cached external ref table. */
109 sal_uLong mnFmtIndex
;
111 typedef std::unordered_map
<SCCOL
, Cell
> RowDataType
;
112 typedef std::unordered_map
<SCROW
, RowDataType
> RowsDataType
;
116 * Represents a single cached table in an external document. It only
117 * stores non-empty cells; empty cells should never be stored in the data
118 * cache. Instead, cached ranges should be used to determine whether or
119 * not a cell is empty or needs fetching from the source document. If a
120 * cell's value is not stored but its address is within the cached ranges,
121 * that cell is already queried in the source document and we know it's
134 * Add cell value to the cache.
136 * @param bSetCacheRange if true, mark this cell 'cached'. This is
137 * false _only when_ adding a range of cell
138 * values, for performance reasons.
140 SC_DLLPUBLIC
void setCell(SCCOL nCol
, SCROW nRow
, TokenRef
const & pToken
, sal_uLong nFmtIndex
= 0, bool bSetCacheRange
= true);
141 SC_DLLPUBLIC TokenRef
getCell(SCCOL nCol
, SCROW nRow
, sal_uInt32
* pnFmtIndex
= nullptr) const;
142 bool hasRow( SCROW nRow
) const;
143 /** Set/clear referenced status flag only if current status is not
144 REFERENCED_PERMANENT. */
145 void setReferenced( bool bReferenced
);
146 bool isReferenced() const;
147 /// Obtain a sorted vector of rows.
148 void getAllRows(::std::vector
<SCROW
>& rRows
, SCROW nLow
= 0, SCROW nHigh
= MAXROW
) const;
149 /// Returns the half-open range of used rows in this table. Returns [0,0) if table is empty.
150 SC_DLLPUBLIC ::std::pair
< SCROW
, SCROW
> getRowRange() const;
151 /// Obtain a sorted vector of columns.
152 void getAllCols(SCROW nRow
, ::std::vector
<SCCOL
>& rCols
, SCCOL nLow
= 0, SCCOL nHigh
= MAXCOL
) const;
153 /// Returns the half-open range of used columns in the specified row. Returns [0,0) if row is empty.
154 SC_DLLPUBLIC ::std::pair
< SCCOL
, SCCOL
> getColRange( SCROW nRow
) const;
155 void getAllNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
156 bool isRangeCached(SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
) const;
158 void setCachedCell(SCCOL nCol
, SCROW nRow
);
159 void setCachedCellRange(SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
);
162 * Call this to mark the entire table "cached". This will prevent all
163 * future attempts to access the source document even when non-cached
164 * cells are queried. In such case, non-cached cells are treated as
165 * empty cells. Useful when loading a document with own external data
168 SC_DLLPUBLIC
void setWholeTableCached();
170 bool isInCachedRanges(SCCOL nCol
, SCROW nRow
) const;
171 TokenRef
getEmptyOrNullToken(SCCOL nCol
, SCROW nRow
) const;
176 /** Collection of individual cached ranges. The table ranges are
177 * not used & always zero. */
178 ScRangeList maCachedRanges
;
182 typedef std::shared_ptr
<Table
> TableTypeRef
;
183 typedef std::unordered_map
< OUString
, size_t>
186 ScExternalRefCache();
187 ~ScExternalRefCache();
189 const OUString
* getRealTableName(sal_uInt16 nFileId
, const OUString
& rTabName
) const;
190 const OUString
* getRealRangeName(sal_uInt16 nFileId
, const OUString
& rRangeName
) const;
193 * Get a cached cell data at specified cell location.
195 * @param nFileId file ID of an external document
196 * @param rTabName sheet name
200 * @return pointer to the token instance in the cache.
202 ScExternalRefCache::TokenRef
getCellData(
203 sal_uInt16 nFileId
, const OUString
& rTabName
, SCCOL nCol
, SCROW nRow
, sal_uInt32
* pnFmtIndex
);
206 * Get a cached cell range data.
208 * @return a new token array instance. Note that <i>the caller must
209 * manage the life cycle of the returned instance</i>, which is
210 * guaranteed if the TokenArrayRef is properly used...
212 ScExternalRefCache::TokenArrayRef
getCellRangeData(
213 sal_uInt16 nFileId
, const OUString
& rTabName
, const ScRange
& rRange
);
215 ScExternalRefCache::TokenArrayRef
getRangeNameTokens(sal_uInt16 nFileId
, const OUString
& rName
);
216 void setRangeNameTokens(sal_uInt16 nFileId
, const OUString
& rName
, TokenArrayRef pArray
);
217 bool isValidRangeName(sal_uInt16 nFileId
, const OUString
& rName
) const;
218 void setRangeName(sal_uInt16 nFileId
, const OUString
& rName
);
220 void setCellData(sal_uInt16 nFileId
, const OUString
& rTabName
,
221 SCCOL nCol
, SCROW nRow
, TokenRef
const & pToken
, sal_uLong nFmtIndex
);
223 struct SingleRangeData
225 /** This name must be in upper-case. */
226 OUString maTableName
;
227 ScMatrixRef mpRangeData
;
229 void setCellRangeData(sal_uInt16 nFileId
, const ScRange
& rRange
, const ::std::vector
<SingleRangeData
>& rData
,
230 const TokenArrayRef
& pArray
);
232 bool isDocInitialized(sal_uInt16 nFileId
);
233 void initializeDoc(sal_uInt16 nFileId
, const ::std::vector
<OUString
>& rTabNames
, const OUString
& rBaseName
);
234 OUString
getTableName(sal_uInt16 nFileId
, size_t nCacheId
) const;
235 void getAllTableNames(sal_uInt16 nFileId
, ::std::vector
<OUString
>& rTabNames
) const;
236 SCTAB
getTabSpan( sal_uInt16 nFileId
, const OUString
& rStartTabName
, const OUString
& rEndTabName
) const;
237 void getAllNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
240 * Set all tables of a document as referenced, used only during
242 * @returns <TRUE/> if ALL tables of ALL documents are marked.
244 bool setCacheDocReferenced( sal_uInt16 nFileId
);
247 * Set a table as referenced, used only during store-to-file.
248 * @returns <TRUE/> if ALL tables of ALL documents are marked.
250 bool setCacheTableReferenced( sal_uInt16 nFileId
, const OUString
& rTabName
, size_t nSheets
);
251 void setAllCacheTableReferencedStati( bool bReferenced
);
252 bool areAllCacheTablesReferenced() const { return maReferenced
.mbAllReferenced
;}
255 * Collect all cached non-empty cell positions, inferred directly from the
256 * cached data, not the cached range metadata stored separately in the
259 void getAllCachedDataSpans( const ScDocument
& rSrcDoc
, sal_uInt16 nFileId
, sc::ColumnSpanSet
& rSet
) const;
261 bool getSrcDocTable( const ScDocument
& rSrcDoc
, const OUString
& rTabName
, SCTAB
& rTab
, sal_uInt16 nFileId
) const;
264 struct ReferencedStatus
268 ::std::vector
<bool> maTables
;
269 bool mbAllTablesReferenced
;
270 // Initially, documents have no tables but all referenced.
271 DocReferenced() : mbAllTablesReferenced(true) {}
273 typedef ::std::vector
<DocReferenced
> DocReferencedVec
;
275 DocReferencedVec maDocs
;
276 bool mbAllReferenced
;
279 void reset( size_t nDocs
);
283 void addCacheTableToReferenced( sal_uInt16 nFileId
, size_t nIndex
);
284 void addCacheDocToReferenced( sal_uInt16 nFileId
);
287 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, size_t nTabIndex
) const;
288 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, const OUString
& rTabName
, bool bCreateNew
,
289 size_t* pnIndex
, const OUString
* pExtUrl
);
292 * Clear all caches including the cache tables.
294 void clearCache(sal_uInt16 nFileId
);
297 * Clear all caches but keep the tables. All cache tables will be empty
298 * after the call, but the tables will not be removed.
300 void clearCacheTables(sal_uInt16 nFileId
);
302 // Get the fake doc used to pass to methods that need an ScDocument in order to do row/col validation
303 const ScDocument
* getFakeDoc() const { return mxFakeDoc
.get(); }
308 size_t operator()(const ScRange
& rRange
) const
310 const ScAddress
& s
= rRange
.aStart
;
311 const ScAddress
& e
= rRange
.aEnd
;
313 hash
= hash
* 37 + s
.Tab();
314 hash
= hash
* 37 + s
.Col();
315 hash
= hash
* 37 + s
.Row();
316 hash
= hash
* 37 + e
.Tab();
317 hash
= hash
* 37 + e
.Col();
318 hash
= hash
* 37 + e
.Row();
323 typedef std::unordered_map
<OUString
, TokenArrayRef
> RangeNameMap
;
324 typedef std::unordered_map
<ScRange
, TokenArrayRef
, RangeHash
> RangeArrayMap
;
325 typedef std::unordered_map
<OUString
, OUString
> NamePairMap
;
327 /** Represents data cached for a single external document. */
330 /** The raw cache tables. */
331 ::std::vector
<TableTypeRef
> maTables
;
332 /** Table name list in correct order, in both upper- and real-case. */
333 ::std::vector
<TableName
> maTableNames
;
334 /** Table name to index map. The names must be stored upper-case. */
335 TableNameIndexMap maTableNameIndex
;
336 /** Range name cache. */
337 RangeNameMap maRangeNames
;
338 /** Token array cache for cell ranges. */
339 RangeArrayMap maRangeArrays
;
340 /** Upper- to real-case mapping for range names. */
341 NamePairMap maRealRangeNameMap
;
343 /** Either the base name that was stored as sheet name for CSV files if
344 sheet name is Sheet1, or Sheet1 name if sheet name is base name.
346 OUString maSingleTableNameAlias
;
348 bool mbInitFromSource
;
350 DocItem() : mbInitFromSource(false) {}
352 TableNameIndexMap::const_iterator
findTableNameIndex( const OUString
& rTabName
) const;
353 bool getTableDataIndex( const OUString
& rTabName
, size_t& rIndex
) const;
354 bool getSingleTableNameAlternative( OUString
& rTabName
) const;
356 typedef std::unordered_map
<sal_uInt16
, DocItem
> DocDataType
;
357 DocItem
* getDocItem(sal_uInt16 nFileId
) const;
360 mutable osl::Mutex maMtxDocs
;
361 mutable DocDataType maDocs
;
362 ScDocumentUniquePtr mxFakeDoc
; // just to have something to pass to the methods that need to validate columns/rows
365 class SC_DLLPUBLIC ScExternalRefManager final
: public formula::ExternalReferenceHelper
, public SfxListener
369 typedef std::set
<ScFormulaCell
*> RefCellSet
;
370 typedef std::unordered_map
<sal_uInt16
, RefCellSet
> RefCellMap
;
372 enum LinkUpdateType
{ LINK_MODIFIED
, LINK_BROKEN
};
375 * Base class for objects that need to listen to link updates. When a
376 * link to a certain external file is updated, the notify() method gets
379 class SAL_DLLPRIVATE LinkListener
383 virtual ~LinkListener() COVERITY_NOEXCEPT_FALSE
= 0;
384 virtual void notify(sal_uInt16 nFileId
, LinkUpdateType eType
) = 0;
388 * Use this guard when performing something from the API that might query
389 * values from external references. Interpreting formula strings is one
392 class SC_DLLPUBLIC ApiGuard
395 ApiGuard(const ScDocument
& rDoc
);
398 ScExternalRefManager
* mpMgr
;
399 bool mbOldInteractionEnabled
;
403 /** Shell instance for a source document. */
406 SfxObjectShellRef maShell
;
407 tools::Time maLastAccess
;
409 SrcShell() : maLastAccess( tools::Time::SYSTEM
) {}
412 typedef std::unordered_map
<sal_uInt16
, SrcShell
> DocShellMap
;
413 typedef std::unordered_map
<sal_uInt16
, bool> LinkedDocMap
;
415 typedef std::unordered_map
<sal_uInt16
, SvNumberFormatterMergeMap
> NumFmtMap
;
417 typedef o3tl::sorted_vector
<LinkListener
*> LinkListeners
;
418 typedef std::unordered_map
<sal_uInt16
, LinkListeners
> LinkListenerMap
;
421 /** Source document meta-data container. */
422 struct SAL_DLLPRIVATE SrcFileData
424 OUString maFileName
; /// original file name as loaded from the file.
425 OUString maRealFileName
; /// file name created from the relative name.
426 OUString maRelativeName
;
427 OUString maFilterName
;
428 OUString maFilterOptions
;
430 void maybeCreateRealFileName(const OUString
& rOwnDocName
);
434 explicit ScExternalRefManager(ScDocument
& rDoc
);
435 virtual ~ScExternalRefManager() override
;
437 virtual OUString
getCacheTableName(sal_uInt16 nFileId
, size_t nTabIndex
) const override
;
440 * Get a cache table instance for specified table and table index. Unlike
441 * the other method that takes a table name, this method does not create a
442 * new table when a table is not available for specified index.
444 * @param nFileId file ID
445 * @param nTabIndex cache table index
447 * @return shared_ptr to the cache table instance
449 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, size_t nTabIndex
) const;
452 * Get a cache table instance for specified file and table name. If the
453 * table instance is not already present, it'll instantiate a new one and
454 * append it to the end of the table array. <I>It's important to be
455 * aware of this fact especially for multi-table ranges for which
456 * table orders are critical.</I>
458 * Excel filter calls this method to populate the cache table from the
459 * XCT/CRN records. ODF import calls it for cached tables for external
462 * @param nFileId file ID
463 * @param rTabName table name
464 * @param bCreateNew if true, create a new table instance if it's not
465 * already present. If false, it returns NULL if the
466 * specified table's cache doesn't exist.
467 * @param pnIndex if non-NULL pointer is passed, it stores the internal
468 * index of a cache table instance.
469 * @param pExtUrl if non-NULL and bCreateNew==true, the base name will be
470 * propagated as an alias for the first table (and removed
471 * later if further tables are created).
473 * @return shared_ptr to the cache table instance
475 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, const OUString
& rTabName
, bool bCreateNew
,
476 size_t* pnIndex
= nullptr, const OUString
* pExtUrl
= nullptr);
478 /** Returns a vector containing all (real) table names and cache tables of
481 The index in the returned vector corresponds to the table index used to
482 access the cache table, e.g. in getCacheTable().
484 void getAllCachedTableNames(sal_uInt16 nFileId
, ::std::vector
<OUString
>& rTabNames
) const;
487 * Get the span (distance+sign(distance)) of two sheets of a specified
490 * @param nFileId file ID
491 * @param rStartTabName name of first sheet (sheet1)
492 * @param rEndTabName name of second sheet (sheet2)
495 * 1 if sheet2 == sheet1
496 * > 1 if sheet2 > sheet1
497 * < -1 if sheet2 < sheet1
498 * -1 if nFileId or rStartTabName not found
499 * 0 if rEndTabName not found
501 SCTAB
getCachedTabSpan(
502 sal_uInt16 nFileId
, const OUString
& rStartTabName
, const OUString
& rEndTabName
) const;
505 * Get all unique number format indices that are used in the cache tables.
506 * The retrieved indices are sorted in ascending order.
508 * @param rNumFmts (reference) all unique number format indices.
510 void getAllCachedNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
512 sal_uInt16
getExternalFileCount() const;
515 * Mark all tables as referenced that are used by any LinkListener, used
516 * only during store-to-file.
518 void markUsedByLinkListeners();
520 void markUsedExternalRefCells();
523 * Set a table as referenced, used only during store-to-file.
524 * @returns <TRUE/> if ALL tables of ALL external documents are marked.
526 bool setCacheTableReferenced( sal_uInt16 nFileId
, const OUString
& rTabName
, size_t nSheets
);
527 void setAllCacheTableReferencedStati( bool bReferenced
);
530 * @returns <TRUE/> if setAllCacheTableReferencedStati(false) was called,
531 * <FALSE/> if setAllCacheTableReferencedStati(true) was called.
533 bool isInReferenceMarking() const { return mbInReferenceMarking
; }
535 void storeRangeNameTokens(sal_uInt16 nFileId
, const OUString
& rName
, const ScTokenArray
& rArray
);
537 ScExternalRefCache::TokenRef
getSingleRefToken(
538 sal_uInt16 nFileId
, const OUString
& rTabName
, const ScAddress
& rCell
,
539 const ScAddress
* pCurPos
, SCTAB
* pTab
, ScExternalRefCache::CellFormat
* pFmt
= nullptr);
542 * Get an array of tokens that consist of the specified external cell
545 * @param nFileId file ID for an external document
546 * @param rTabName referenced sheet name
547 * @param rRange referenced cell range
548 * @param pCurPos current cursor position to keep track of cells that
549 * reference an external data.
551 * @return shared_ptr to a token array instance. <i>The caller must not
552 * delete the instance returned by this method.</i>
554 ScExternalRefCache::TokenArrayRef
getDoubleRefTokens(
555 sal_uInt16 nFileId
, const OUString
& rTabName
, const ScRange
& rRange
, const ScAddress
* pCurPos
);
558 * Get an array of tokens corresponding with a specified name in a
561 * @param pCurPos current cell address where this name token is used.
562 * This is purely to keep track of all cells containing
563 * external names for refreshing purposes. If this is
564 * NULL, then the cell will not be added to the list.
566 * @return shared_ptr to array of tokens composing the name
568 ScExternalRefCache::TokenArrayRef
getRangeNameTokens(
569 sal_uInt16 nFileId
, const OUString
& rName
, const ScAddress
* pCurPos
= nullptr);
571 bool isValidRangeName(sal_uInt16 nFileId
, const OUString
& rName
);
573 OUString
getOwnDocumentName() const;
574 bool isOwnDocument(const OUString
& rFile
) const;
577 * Takes a flat file name, and convert it to an absolute URL path. An
578 * absolute URL path begins with 'file:///.
580 * @param rFile file name to convert
582 void convertToAbsName(OUString
& rFile
) const;
583 sal_uInt16
getExternalFileId(const OUString
& rFile
);
586 * It returns a pointer to the name of the URI associated with a given
587 * external file ID. In case the original document has moved, it returns
588 * a URI adjusted for the relocation.
590 * @param nFileId file ID for an external document
591 * @param bForceOriginal If true, it always returns the original document
592 * URI even if the referring document has relocated.
593 * If false, it returns a URI adjusted for
594 * relocated document.
596 * @return const OUString* external document URI.
598 const OUString
* getExternalFileName(sal_uInt16 nFileId
, bool bForceOriginal
= false);
601 * Reindex external file references to skip unused files, if skipping is enabled.
603 sal_uInt16
convertFileIdToUsedFileId(sal_uInt16 nFileId
);
604 void setSkipUnusedFileIds(std::vector
<sal_uInt16
>& pExternFileIds
);
605 void disableSkipUnusedFileIds();
608 * Get all cached external file names as an array. Array indices of the
609 * returned name array correspond with external file ID's.
611 std::vector
<OUString
> getAllCachedExternalFileNames() const;
613 bool hasExternalFile(sal_uInt16 nFileId
) const;
614 bool hasExternalFile(const OUString
& rFile
) const;
615 const SrcFileData
* getExternalFileData(sal_uInt16 nFileId
) const;
617 const OUString
* getRealTableName(sal_uInt16 nFileId
, const OUString
& rTabName
) const;
618 const OUString
* getRealRangeName(sal_uInt16 nFileId
, const OUString
& rRangeName
) const;
619 void clearCache(sal_uInt16 nFileId
);
620 bool refreshSrcDocument(sal_uInt16 nFileId
);
621 void breakLink(sal_uInt16 nFileId
);
622 void switchSrcFile(sal_uInt16 nFileId
, const OUString
& rNewFile
, const OUString
& rNewFilter
);
625 * Set a relative file path for the specified file ID. Note that the
626 * caller must ensure that the passed URL is a valid relative URL.
628 * @param nFileId file ID for an external document
629 * @param rRelUrl relative URL
631 void setRelativeFileName(sal_uInt16 nFileId
, const OUString
& rRelUrl
);
634 * Set the filter name and options if any for a given source document.
635 * These values get reset when the source document ever gets reloaded.
641 void setFilterData(sal_uInt16 nFileId
, const OUString
& rFilterName
, const OUString
& rOptions
);
645 bool hasExternalData() const;
648 * Re-generates relative names for all stored source files. This is
649 * necessary when exporting to an ods document, to ensure that all source
650 * files have their respective relative names for xlink:href export.
652 * @param rBaseFileUrl Absolute URL of the content.xml fragment of the
653 * document being exported.
655 void resetSrcFileData(const OUString
& rBaseFileUrl
);
658 * Replace the original URL with the real URL that was generated from the relative URL.
660 void updateAbsAfterLoad();
663 * Stop tracking a specific formula cell.
665 * @param pCell pointer to cell that formerly contained external
668 void removeRefCell(ScFormulaCell
* pCell
);
671 * Register a new link listener to a specified external document. Note
672 * that the caller is responsible for managing the life cycle of the
675 void addLinkListener(sal_uInt16 nFileId
, LinkListener
* pListener
);
678 * Remove an existing link listener. Note that removing a listener
679 * pointer here does not delete the listener object instance.
681 void removeLinkListener(sal_uInt16 nFileId
, LinkListener
* pListener
);
683 void removeLinkListener(LinkListener
* pListener
);
686 * Notify all listeners that are listening to a specified external
689 * @param nFileId file ID for an external document.
691 void notifyAllLinkListeners(sal_uInt16 nFileId
, LinkUpdateType eType
);
694 * Check if the file specified by the path is a legitimate file that
695 * exists & can be loaded.
697 bool isFileLoadable(const OUString
& rFile
) const;
699 virtual void Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
) override
;
702 * If we still contain unsaved files we should warn the user before saving
704 * @return true if the document still contains references to an unsaved file
706 bool containsUnsavedReferences() const { return !maUnsavedDocShells
.empty(); }
708 void insertRefCell(sal_uInt16 nFileId
, const ScAddress
& rCell
);
710 * Add a cell to reference the same files as the template cell.
712 void insertRefCellFromTemplate( ScFormulaCell
* pTemplateCell
, ScFormulaCell
* pCell
);
714 bool hasCellExternalReference(const ScAddress
& rCell
);
716 void enableDocTimer( bool bEnable
);
718 /** Add all known external files to the LinkManager. */
719 void addFilesToLinkManager();
722 ScExternalRefManager(const ScExternalRefManager
&) = delete;
724 void refreshAllRefCells(sal_uInt16 nFileId
);
726 void fillCellFormat(sal_uLong nFmtIndex
, ScExternalRefCache::CellFormat
* pFmt
) const;
728 bool getSrcDocTable( const ScDocument
& rSrcDoc
, const OUString
& rTabName
, SCTAB
& rTab
, sal_uInt16 nFileId
) const;
730 ScExternalRefCache::TokenRef
getSingleRefTokenFromSrcDoc(
731 sal_uInt16 nFileId
, ScDocument
& rSrcDoc
, const ScAddress
& rPos
,
732 ScExternalRefCache::CellFormat
* pFmt
);
735 * Retrieve a range token array from a source document instance.
737 * @param rSrcDoc reference to the source document instance.
738 * @param rTabName name of the first table.
739 * @param rRange range specified. Upon successful retrieval, this range
740 * gets modified to contain the correct table IDs, and in
741 * case the range is larger than the data area of the source
742 * document, it gets reduced to the data area.
743 * @param rCacheData an array of structs, with each struct containing the
744 * table name and the data in the specified range.
746 * @return range token array
748 ScExternalRefCache::TokenArrayRef
getDoubleRefTokensFromSrcDoc(
749 const ScDocument
& rSrcDoc
, const OUString
& rTabName
, ScRange
& rRange
,
750 ::std::vector
<ScExternalRefCache::SingleRangeData
>& rCacheData
);
753 * Retrieve range name token array from a source document instance.
755 * @param nFileId file ID of the source document.
756 * @param rSrcDoc reference to the source document instance
757 * @param rName range name to retrieve. Note that the range name lookup
758 * is case <i>in</i>-sensitive, and upon successful retrieval
759 * of the range name array, this name gets updated to the
760 * actual range name with the correct casing.
762 * @return range name token array
764 static ScExternalRefCache::TokenArrayRef
getRangeNameTokensFromSrcDoc(
765 sal_uInt16 nFileId
, const ScDocument
& rSrcDoc
, OUString
& rName
);
767 ScDocument
* getInMemorySrcDocument(sal_uInt16 nFileId
);
768 ScDocument
* getSrcDocument(sal_uInt16 nFileId
);
769 SfxObjectShellRef
loadSrcDocument(sal_uInt16 nFileId
, OUString
& rFilter
);
772 * Caller must ensure that the passed shell is not already stored.
774 ScDocument
& cacheNewDocShell( sal_uInt16 nFileId
, SrcShell
& rSrcShell
);
776 void maybeLinkExternalFile( sal_uInt16 nFileId
, bool bDeferFilterDetection
= false );
779 * Try to create a "real" file name from the relative path. The original
780 * file name may not point to the real document when the referencing and
781 * referenced documents have been moved.
783 * For the real file name to be created, the relative name should not be
784 * empty before calling this method, or the real file name will not be
787 * @param nFileId file ID for an external document
789 void maybeCreateRealFileName(sal_uInt16 nFileId
);
792 * Purge those source document instances that have not been accessed for
793 * the specified duration.
795 * @param nTimeOut time out value in 100th of a second
797 void purgeStaleSrcDocument(sal_Int32 nTimeOut
);
799 sal_uInt32
getMappedNumberFormat(sal_uInt16 nFileId
, sal_uInt32 nNumFmt
, const ScDocument
& rSrcDoc
);
802 * If in maUnsavedDocShells move it to maDocShells and create a correct
803 * external reference entry
805 * @param Pointer to the newly saved DocumentShell
807 void transformUnsavedRefToSavedRef( SfxObjectShell
* pShell
);
812 /** cache of referenced ranges and names from source documents. */
813 ScExternalRefCache maRefCache
;
816 * Source document cache. This stores the original source document shell
817 * instances. They get purged after a certain period of time.
819 DocShellMap maDocShells
;
822 * DocShells to unsaved but referenced documents. If not empty ask before saving!
823 * Move to maDocShells if document referenced here is saved
825 DocShellMap maUnsavedDocShells
;
827 /** list of source documents that are managed by the link manager. */
828 LinkedDocMap maLinkedDocs
;
831 * List of referencing cells that may contain external names. There is
832 * one list per source document.
834 RefCellMap maRefCells
;
836 LinkListenerMap maLinkListeners
;
838 NumFmtMap maNumFormatMap
;
841 * List of external source document meta-data, used to keep track of
842 * external document identifiers.
844 std::vector
<SrcFileData
> maSrcFiles
;
846 /** Status whether in reference marking state. See isInReferenceMarking(). */
847 bool mbInReferenceMarking
:1;
850 * Controls whether or not to allow user interaction. We don't want any
851 * user interaction when calling from the API.
853 bool mbUserInteractionEnabled
:1;
855 bool mbSkipUnusedFileIds
= false;
856 std::vector
<sal_uInt16
> maConvertFileIdToUsedFileId
;
858 bool mbDocTimerEnabled
:1;
860 AutoTimer maSrcDocTimer
;
861 DECL_LINK(TimeOutHdl
, Timer
*, void);
866 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */