1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: externalrefmgr.hxx,v $
10 * $Revision: 1.1.2.23 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_EXTERNALREFMGR_HXX
32 #define SC_EXTERNALREFMGR_HXX
35 #include "address.hxx"
36 #include "sfx2/objsh.hxx"
37 #include "sfx2/lnkbase.hxx"
38 #include "tools/time.hxx"
39 #include "vcl/timer.hxx"
40 #include "svtools/zforlist.hxx"
41 #include "scmatrix.hxx"
45 #include <boost/shared_ptr.hpp>
48 #include <formula/ExternalReferenceHelper.hxx>
59 class SfxObjectShellRef
;
62 class ScExternalRefCache
;
64 class ScExternalRefLink
: public ::sfx2::SvBaseLink
67 ScExternalRefLink(ScDocument
* pDoc
, sal_uInt16 nFileId
, const String
& rFilter
);
68 virtual ~ScExternalRefLink();
70 virtual void Closed();
71 virtual void DataChanged(const String
& rMimeType
, const ::com::sun::star::uno::Any
& rValue
);
72 virtual void Edit(Window
* pParent
, const Link
& rEndEditHdl
);
74 void SetDoReferesh(bool b
);
77 ScExternalRefLink(); // disabled
78 ScExternalRefLink(const ScExternalRefLink
&); // disabled
80 DECL_LINK( ExternalRefEndEditHdl
, ::sfx2::SvBaseLink
* );
88 // ============================================================================
91 * Cache table for external reference data.
93 class ScExternalRefCache
96 typedef ::boost::shared_ptr
< formula::FormulaToken
> TokenRef
;
97 typedef ::boost::shared_ptr
<ScTokenArray
> TokenArrayRef
;
104 explicit TableName(const String
& rUppper
, const String
& rReal
);
113 explicit CellFormat();
117 /** individual cell within cached external ref table. */
121 sal_uInt32 mnFmtIndex
;
123 typedef ::std::hash_map
<SCCOL
, Cell
> RowDataType
;
124 typedef ::std::hash_map
<SCROW
, RowDataType
> RowsDataType
;
127 // SUNWS needs a forward declared friend, otherwise types and members
128 // of the outer class are not accessible.
130 friend class ScExternalRefCache::Table
;
139 REFERENCED_MARKED
, // marked as referenced during store to file
140 REFERENCED_PERMANENT
// permanently marked, e.g. from within interpreter
146 SC_DLLPUBLIC
void setCell(SCCOL nCol
, SCROW nRow
, TokenRef pToken
, sal_uInt32 nFmtIndex
= 0);
147 TokenRef
getCell(SCCOL nCol
, SCROW nRow
, sal_uInt32
* pnFmtIndex
= NULL
) const;
148 bool hasRow( SCROW nRow
) const;
149 /** Set/clear referenced status flag only if current status is not
150 REFERENCED_PERMANENT. */
151 void setReferenced( bool bReferenced
);
152 /// Unconditionally set the reference status flag.
153 void setReferencedFlag( ReferencedFlag eFlag
);
154 ReferencedFlag
getReferencedFlag() const;
155 bool isReferenced() const;
156 /// Obtain a sorted vector of rows.
157 void getAllRows(::std::vector
<SCROW
>& rRows
) const;
158 /// Obtain a sorted vector of columns.
159 void getAllCols(SCROW nRow
, ::std::vector
<SCCOL
>& rCols
) const;
160 void getAllNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
164 ReferencedFlag meReferenced
;
167 typedef ::boost::shared_ptr
<Table
> TableTypeRef
;
168 typedef ::std::hash_map
<String
, size_t, ScStringHashCode
> TableNameIndexMap
;
170 ScExternalRefCache();
171 ~ScExternalRefCache();
173 const String
* getRealTableName(sal_uInt16 nFileId
, const String
& rTabName
) const;
174 const String
* getRealRangeName(sal_uInt16 nFileId
, const String
& rRangeName
) const;
177 * Get a cached cell data at specified cell location.
179 * @param nFileId file ID of an external document
180 * @param rTabName sheet name
184 * @return pointer to the token instance in the cache.
186 ScExternalRefCache::TokenRef
getCellData(
187 sal_uInt16 nFileId
, const String
& rTabName
, SCCOL nCol
, SCROW nRow
,
188 bool bEmptyCellOnNull
, bool bWriteEmpty
, sal_uInt32
* pnFmtIndex
);
191 * Get a cached cell range data.
193 * @return a new token array instance. Note that <i>the caller must
194 * manage the life cycle of the returned instance</i>, which is
195 * guaranteed if the TokenArrayRef is properly used..
197 ScExternalRefCache::TokenArrayRef
getCellRangeData(
198 sal_uInt16 nFileId
, const String
& rTabName
, const ScRange
& rRange
, bool bEmptyCellOnNull
, bool bWriteEmpty
);
200 ScExternalRefCache::TokenArrayRef
getRangeNameTokens(sal_uInt16 nFileId
, const String
& rName
);
201 void setRangeNameTokens(sal_uInt16 nFileId
, const String
& rName
, TokenArrayRef pArray
);
203 void setCellData(sal_uInt16 nFileId
, const String
& rTabName
, SCROW nRow
, SCCOL nCol
, TokenRef pToken
, sal_uInt32 nFmtIndex
);
205 struct SingleRangeData
207 /** This name must be in upper-case. */
209 ScMatrixRef mpRangeData
;
211 void setCellRangeData(sal_uInt16 nFileId
, const ScRange
& rRange
, const ::std::vector
<SingleRangeData
>& rData
,
212 TokenArrayRef pArray
);
214 bool isDocInitialized(sal_uInt16 nFileId
);
215 void initializeDoc(sal_uInt16 nFileId
, const ::std::vector
<String
>& rTabNames
);
216 String
getTableName(sal_uInt16 nFileId
, size_t nCacheId
) const;
217 void getAllTableNames(sal_uInt16 nFileId
, ::std::vector
<String
>& rTabNames
) const;
218 SCsTAB
getTabSpan( sal_uInt16 nFileId
, const String
& rStartTabName
, const String
& rEndTabName
) const;
219 void getAllNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
220 bool hasCacheTable(sal_uInt16 nFileId
, const String
& rTabName
) const;
221 size_t getCacheTableCount(sal_uInt16 nFileId
) const;
224 * Set all tables of a document as referenced, used only during
226 * @returns <TRUE/> if ALL tables of ALL documents are marked.
228 bool setCacheDocReferenced( sal_uInt16 nFileId
);
231 * Set a table as referenced, used only during store-to-file.
232 * @returns <TRUE/> if ALL tables of ALL documents are marked.
234 bool setCacheTableReferenced( sal_uInt16 nFileId
, const String
& rTabName
, size_t nSheets
, bool bPermanent
);
235 void setAllCacheTableReferencedStati( bool bReferenced
);
236 bool areAllCacheTablesReferenced() const;
239 * Set a table as permanently referenced, to be called if not in
240 * mark-during-store-to-file cycle.
242 void setCacheTableReferencedPermanently( sal_uInt16 nFileId
, const String
& rTabName
, size_t nSheets
);
245 struct ReferencedStatus
249 ::std::vector
<bool> maTables
;
250 bool mbAllTablesReferenced
;
251 // Initially, documents have no tables but all referenced.
252 DocReferenced() : mbAllTablesReferenced(true) {}
254 typedef ::std::vector
<DocReferenced
> DocReferencedVec
;
256 DocReferencedVec maDocs
;
257 bool mbAllReferenced
;
260 explicit ReferencedStatus( size_t nDocs
);
261 void reset( size_t nDocs
);
265 void addCacheTableToReferenced( sal_uInt16 nFileId
, size_t nIndex
);
266 void addCacheDocToReferenced( sal_uInt16 nFileId
);
269 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, size_t nTabIndex
) const;
270 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, const String
& rTabName
, bool bCreateNew
, size_t* pnIndex
);
272 void clearCache(sal_uInt16 nFileId
);
277 size_t operator()(const ScRange
& rRange
) const
279 const ScAddress
& s
= rRange
.aStart
;
280 const ScAddress
& e
= rRange
.aEnd
;
281 return s
.Tab() + s
.Col() + s
.Row() + e
.Tab() + e
.Col() + e
.Row();
285 typedef ::std::hash_map
<String
, TokenArrayRef
, ScStringHashCode
> RangeNameMap
;
286 typedef ::std::hash_map
<ScRange
, TokenArrayRef
, RangeHash
> RangeArrayMap
;
287 typedef ::std::hash_map
<String
, String
, ScStringHashCode
> NamePairMap
;
289 // SUNWS needs a forward declared friend, otherwise types and members
290 // of the outer class are not accessible.
292 friend struct ScExternalRefCache::DocItem
;
294 /** Represents data cached for a single external document. */
297 /** The raw cache tables. */
298 ::std::vector
<TableTypeRef
> maTables
;
299 /** Table name list in correct order, in both upper- and real-case. */
300 ::std::vector
<TableName
> maTableNames
;
301 /** Table name to index map. The names must be stored upper-case. */
302 TableNameIndexMap maTableNameIndex
;
303 /** Range name cache. */
304 RangeNameMap maRangeNames
;
305 /** Token array cache for cell ranges. */
306 RangeArrayMap maRangeArrays
;
307 /** Upper- to real-case mapping for range names. */
308 NamePairMap maRealRangeNameMap
;
310 bool mbInitFromSource
;
312 DocItem() : mbInitFromSource(false) {}
314 typedef ::std::hash_map
<sal_uInt16
, DocItem
> DocDataType
;
315 DocItem
* getDocItem(sal_uInt16 nFileId
) const;
318 mutable DocDataType maDocs
;
321 // ============================================================================
323 class SC_DLLPUBLIC ScExternalRefManager
: public formula::ExternalReferenceHelper
327 // SUNWS needs a forward declared friend, otherwise types and members
328 // of the outer class are not accessible.
330 friend class ScExternalRefManager::RefCells
;
333 * Collection of cell addresses that contain external references. This
334 * data is used for link updates.
342 void insertCell(const ScAddress
& rAddr
);
343 void removeCell(const ScAddress
& rAddr
);
344 void moveTable(SCTAB nOldTab
, SCTAB nNewTab
, bool bCopy
);
345 void insertTable(SCTAB nPos
);
346 void removeTable(SCTAB nPos
);
347 void refreshAllCells(ScExternalRefManager
& rRefMgr
);
350 typedef ::std::hash_set
<SCROW
> RowSet
;
351 typedef ::std::hash_map
<SCCOL
, RowSet
> ColSet
;
353 // SUNWS needs a forward declared friend, otherwise types and members
354 // of the outer class are not accessible.
356 friend struct ScExternalRefManager::RefCells::TabItem
;
362 explicit TabItem(SCTAB nIndex
);
363 explicit TabItem(const TabItem
& r
);
365 typedef ::boost::shared_ptr
<TabItem
> TabItemRef
;
368 * Return the position that points either to the specified table
369 * position or to the position where a new table would be inserted in
370 * case the specified table is not present.
372 * @param nTab index of the desired table
374 ::std::list
<TabItemRef
>::iterator
getTabPos(SCTAB nTab
);
376 // This list must be sorted by the table index at all times.
377 ::std::list
<TabItemRef
> maTables
;
380 enum LinkUpdateType
{ LINK_MODIFIED
, LINK_BROKEN
};
383 * Base class for objects that need to listen to link updates. When a
384 * link to a certain external file is updated, the notify() method gets
391 virtual ~LinkListener() = 0;
392 virtual void notify(sal_uInt16 nFileId
, LinkUpdateType eType
) = 0;
396 size_t operator() (const LinkListener
* p
) const
398 return reinterpret_cast<size_t>(p
);
404 /** Shell instance for a source document. */
407 SfxObjectShellRef maShell
;
411 typedef ::std::hash_map
<sal_uInt16
, SrcShell
> DocShellMap
;
412 typedef ::std::hash_map
<sal_uInt16
, bool> LinkedDocMap
;
414 typedef ::std::hash_map
<sal_uInt16
, RefCells
> RefCellMap
;
415 typedef ::std::hash_map
<sal_uInt16
, SvNumberFormatterMergeMap
> NumFmtMap
;
418 typedef ::std::hash_set
<LinkListener
*, LinkListener::Hash
> LinkListeners
;
419 typedef ::std::hash_map
<sal_uInt16
, LinkListeners
> LinkListenerMap
;
422 /** Source document meta-data container. */
426 String maRelativeName
;
428 String maFilterOptions
;
432 explicit ScExternalRefManager(ScDocument
* pDoc
);
433 virtual ~ScExternalRefManager();
435 virtual String
getCacheTableName(sal_uInt16 nFileId
, size_t nTabIndex
) const;
438 * Get a cache table instance for specified table and table index. Unlike
439 * the other method that takes a table name, this method does not create a
440 * new table when a table is not available for specified index.
442 * @param nFileId file ID
443 * @param nTabIndex cache table index
445 * @return shared_ptr to the cache table instance
447 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, size_t nTabIndex
) const;
450 * Get a cache table instance for specified file and table name. If the
451 * table instance is not already present, it'll instantiate a new one and
452 * append it to the end of the table array. <I>It's important to be
453 * aware of this fact especially for multi-table ranges for which
454 * table orders are critical.</I>
456 * Excel filter calls this method to populate the cache table from the
459 * @param nFileId file ID
460 * @param rTabName table name
461 * @param bCreateNew if true, create a new table instance if it's not
462 * already present. If false, it returns NULL if the
463 * specified table's cache doesn't exist.
464 * @param pnIndex if non-NULL pointer is passed, it stores the internal
465 * index of a cache table instance.
467 * @return shared_ptr to the cache table instance
469 ScExternalRefCache::TableTypeRef
getCacheTable(sal_uInt16 nFileId
, const String
& rTabName
, bool bCreateNew
, size_t* pnIndex
= 0);
470 void getAllCachedTableNames(sal_uInt16 nFileId
, ::std::vector
<String
>& rTabNames
) const;
473 * Get the span (distance+sign(distance)) of two sheets of a specified
476 * @param nFileId file ID
477 * @param rStartTabName name of first sheet (sheet1)
478 * @param rEndTabName name of second sheet (sheet2)
481 * 1 if sheet2 == sheet1
482 * > 1 if sheet2 > sheet1
483 * < -1 if sheet2 < sheet1
484 * -1 if nFileId or rStartTabName not found
485 * 0 if rEndTabName not found
487 SCsTAB
getCachedTabSpan( sal_uInt16 nFileId
, const String
& rStartTabName
, const String
& rEndTabName
) const;
490 * Get all unique number format indices that are used in the cache tables.
491 * The retrieved indices are sorted in ascending order.
493 * @param rNumFmts (reference) all unique number format indices.
495 void getAllCachedNumberFormats(::std::vector
<sal_uInt32
>& rNumFmts
) const;
497 bool hasCacheTable(sal_uInt16 nFileId
, const String
& rTabName
) const;
498 size_t getCacheTableCount(sal_uInt16 nFileId
) const;
499 sal_uInt16
getExternalFileCount() const;
502 * Mark all tables as referenced that are used by any LinkListener, used
503 * only during store-to-file.
504 * @returns <TRUE/> if ALL tables of ALL external documents are marked.
506 bool markUsedByLinkListeners();
509 * Set all tables of a document as referenced, used only during
511 * @returns <TRUE/> if ALL tables of ALL external documents are marked.
513 bool setCacheDocReferenced( sal_uInt16 nFileId
);
516 * Set a table as referenced, used only during store-to-file.
517 * @returns <TRUE/> if ALL tables of ALL external documents are marked.
519 bool setCacheTableReferenced( sal_uInt16 nFileId
, const String
& rTabName
, size_t nSheets
);
520 void setAllCacheTableReferencedStati( bool bReferenced
);
523 * Set a table as permanently referenced, to be called if not in
524 * mark-during-store-to-file cycle.
526 void setCacheTableReferencedPermanently( sal_uInt16 nFileId
, const String
& rTabName
, size_t nSheets
);
529 * @returns <TRUE/> if setAllCacheTableReferencedStati(false) was called,
530 * <FALSE/> if setAllCacheTableReferencedStati(true) was called.
532 bool isInReferenceMarking() const { return bInReferenceMarking
; }
534 void storeRangeNameTokens(sal_uInt16 nFileId
, const String
& rName
, const ScTokenArray
& rArray
);
536 ScExternalRefCache::TokenRef
getSingleRefToken(
537 sal_uInt16 nFileId
, const String
& rTabName
, const ScAddress
& rCell
,
538 const ScAddress
* pCurPos
, SCTAB
* pTab
, ScExternalRefCache::CellFormat
* pFmt
= NULL
);
541 * Get an array of tokens that consist of the specified external cell
544 * @param nFileId file ID for an external document
545 * @param rTabName referenced sheet name
546 * @param rRange referenced cell range
547 * @param pCurPos current cursor position to keep track of cells that
548 * reference an external data.
550 * @return shared_ptr to a token array instance. <i>The caller must not
551 * delete the instance returned by this method.</i>
553 ScExternalRefCache::TokenArrayRef
getDoubleRefTokens(sal_uInt16 nFileId
, const String
& rTabName
, const ScRange
& rRange
, const ScAddress
* pCurPos
);
556 * Get an array of tokens corresponding with a specified name in a
559 * @param pCurPos currnet cell address where this name token is used.
560 * This is purely to keep track of all cells containing
561 * external names for refreshing purposes. If this is
562 * NULL, then the cell will not be added to the list.
564 * @return shared_ptr to array of tokens composing the name
566 ScExternalRefCache::TokenArrayRef
getRangeNameTokens(sal_uInt16 nFileId
, const String
& rName
, const ScAddress
* pCurPos
= NULL
);
568 const String
& getOwnDocumentName() const;
569 bool isOwnDocument(const String
& rFile
) const;
572 * Takes a flat file name, and convert it to an absolute URL path. An
573 * absolute URL path begines with 'file:///.
575 * @param rFile file name to convert
577 void convertToAbsName(String
& rFile
) const;
578 sal_uInt16
getExternalFileId(const String
& rFile
);
579 const String
* getExternalFileName(sal_uInt16 nFileId
) const;
580 bool hasExternalFile(sal_uInt16 nFileId
) const;
581 bool hasExternalFile(const String
& rFile
) const;
582 const SrcFileData
* getExternalFileData(sal_uInt16 nFileId
) const;
584 const String
* getRealTableName(sal_uInt16 nFileId
, const String
& rTabName
) const;
585 const String
* getRealRangeName(sal_uInt16 nFileId
, const String
& rRangeName
) const;
586 void refreshNames(sal_uInt16 nFileId
);
587 void breakLink(sal_uInt16 nFileId
);
588 void switchSrcFile(sal_uInt16 nFileId
, const String
& rNewFile
);
590 void setRelativeFileName(sal_uInt16 nFileId
, const String
& rRelUrl
);
593 * Set the filter name and options if any for a given source document.
594 * These values get reset when the source document ever gets reloaded.
600 void setFilterData(sal_uInt16 nFileId
, const String
& rFilterName
, const String
& rOptions
);
604 bool hasExternalData() const;
607 * Re-generates relative names for all stored source files. This is
608 * necessary when exporting to an ods document, to ensure that all source
609 * files have their respective relative names for xlink:href export.
611 void resetSrcFileData();
614 * Update a single referencing cell position.
616 * @param rOldPos old position
617 * @param rNewPos new position
619 void updateRefCell(const ScAddress
& rOldPos
, const ScAddress
& rNewPos
, bool bCopy
);
622 * Update referencing cells affected by sheet movement.
624 * @param nOldTab old sheet position
625 * @param nNewTab new sheet position
626 * @param bCopy whether this is a sheet move (false) or sheet copy (true)
628 void updateRefMoveTable(SCTAB nOldTab
, SCTAB nNewTab
, bool bCopy
);
631 * Update referencing cells affected by sheet insertion.
633 * @param nPos sheet insertion position. All sheets to the right
634 * including the one at the insertion poistion shift to the
637 void updateRefInsertTable(SCTAB nPos
);
639 void updateRefDeleteTable(SCTAB nPos
);
642 * Register a new link listener to a specified external document. Note
643 * that the caller is responsible for managing the life cycle of the
646 void addLinkListener(sal_uInt16 nFileId
, LinkListener
* pListener
);
649 * Remove an existing link listener. Note that removing a listener
650 * pointer here does not delete the listener object instance.
652 void removeLinkListener(sal_uInt16 nFileId
, LinkListener
* pListener
);
654 void removeLinkListener(LinkListener
* pListener
);
657 * Notify all listeners that are listening to a specified external
660 * @param nFileId file ID for an external document.
662 void notifyAllLinkListeners(sal_uInt16 nFileId
, LinkUpdateType eType
);
665 ScExternalRefManager();
666 ScExternalRefManager(const ScExternalRefManager
&);
668 void refreshAllRefCells(sal_uInt16 nFileId
);
670 void insertRefCell(sal_uInt16 nFileId
, const ScAddress
& rCell
);
672 ScDocument
* getSrcDocument(sal_uInt16 nFileId
);
673 SfxObjectShellRef
loadSrcDocument(sal_uInt16 nFileId
, String
& rFilter
);
674 bool isFileLoadable(const String
& rFile
) const;
676 void maybeLinkExternalFile(sal_uInt16 nFileId
);
678 bool compileTokensByCell(const ScAddress
& rCell
);
681 * Purge those source document instances that have not been accessed for
682 * the specified duration.
684 * @param nTimeOut time out value in 100th of a second
686 void purgeStaleSrcDocument(sal_Int32 nTimeOut
);
688 sal_uInt32
getMappedNumberFormat(sal_uInt16 nFileId
, sal_uInt32 nNumFmt
, ScDocument
* pSrcDoc
);
691 /** cache of referenced ranges and names from source documents. */
692 ScExternalRefCache maRefCache
;
697 * Source document cache. This stores the original source document shell
698 * instances. They get purged after a certain period of time.
700 DocShellMap maDocShells
;
702 /** list of source documents that are managed by the link manager. */
703 LinkedDocMap maLinkedDocs
;
706 * List of referencing cells that may contain external names. There is
707 * one list per source document.
709 RefCellMap maRefCells
;
711 LinkListenerMap maLinkListeners
;
713 NumFmtMap maNumFormatMap
;
715 /** original source file index. */
716 ::std::vector
<SrcFileData
> maSrcFiles
;
718 /** Status whether in reference marking state. See isInReferenceMarking(). */
719 bool bInReferenceMarking
;
721 AutoTimer maSrcDocTimer
;
722 DECL_LINK(TimeOutHdl
, AutoTimer
*);