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 #include <sfx2/linkmgr.hxx>
22 #include "areasave.hxx"
23 #include "arealink.hxx"
24 #include "document.hxx"
26 // -----------------------------------------------------------------------
28 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink
& rSource
) :
29 aFileName ( rSource
.GetFile() ),
30 aFilterName ( rSource
.GetFilter() ),
31 aOptions ( rSource
.GetOptions() ),
32 aSourceArea ( rSource
.GetSource() ),
33 aDestArea ( rSource
.GetDestArea() ),
34 nRefresh ( rSource
.GetRefreshDelay() ) // seconds
38 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLinkSaver
& rCopy
) :
39 aFileName ( rCopy
.aFileName
),
40 aFilterName ( rCopy
.aFilterName
),
41 aOptions ( rCopy
.aOptions
),
42 aSourceArea ( rCopy
.aSourceArea
),
43 aDestArea ( rCopy
.aDestArea
),
44 nRefresh ( rCopy
.nRefresh
)
48 ScAreaLinkSaver::~ScAreaLinkSaver() {}
50 bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink
& rCompare
) const
52 return ( aFileName
.equals(rCompare
.GetFile()) &&
53 aFilterName
.equals(rCompare
.GetFilter()) &&
54 aOptions
.equals(rCompare
.GetOptions()) &&
55 aSourceArea
.equals(rCompare
.GetSource()) &&
56 nRefresh
== rCompare
.GetRefreshDelay() );
59 bool ScAreaLinkSaver::IsEqual( const ScAreaLink
& rCompare
) const
61 return ( IsEqualSource( rCompare
) &&
62 aDestArea
== rCompare
.GetDestArea() );
65 void ScAreaLinkSaver::WriteToLink( ScAreaLink
& rLink
) const
67 rLink
.SetDestArea( aDestArea
);
70 void ScAreaLinkSaver::InsertNewLink( ScDocument
* pDoc
) const
72 // (see ScUndoRemoveAreaLink::Undo)
74 sfx2::LinkManager
* pLinkManager
= pDoc
->GetLinkManager();
75 SfxObjectShell
* pObjSh
= pDoc
->GetDocumentShell();
77 if ( pLinkManager
&& pObjSh
)
79 ScAreaLink
* pLink
= new ScAreaLink( pObjSh
, aFileName
, aFilterName
, aOptions
,
80 aSourceArea
, aDestArea
.aStart
, nRefresh
);
81 pLink
->SetInCreate( sal_True
);
82 pLink
->SetDestArea( aDestArea
);
83 OUString
aTmp1(aFilterName
), aTmp2(aSourceArea
);
84 pLinkManager
->InsertFileLink( *pLink
, OBJECT_CLIENT_FILE
, aFileName
, &aTmp1
, &aTmp2
);
86 pLink
->SetInCreate( false );
90 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection() {}
92 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection
& r
) :
95 ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection() {}
97 bool ScAreaLinkSaveCollection::IsEqual( const ScDocument
* pDoc
) const
99 // IsEqual can be checked in sequence.
100 // Neither ref-update nor removing links will change the order.
102 sfx2::LinkManager
* pLinkManager
= const_cast<ScDocument
*>(pDoc
)->GetLinkManager();
106 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
107 sal_uInt16 nLinkCount
= rLinks
.size();
108 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
110 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
111 if (pBase
->ISA(ScAreaLink
))
113 if ( nPos
>= size() || !(*this)[nPos
]->IsEqual( *(ScAreaLink
*)pBase
) )
120 return false; // fewer links in the document than in the save collection
126 static ScAreaLink
* lcl_FindLink( const ::sfx2::SvBaseLinks
& rLinks
, const ScAreaLinkSaver
& rSaver
)
128 sal_uInt16 nLinkCount
= rLinks
.size();
129 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
131 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
132 if ( pBase
->ISA(ScAreaLink
) &&
133 rSaver
.IsEqualSource( *static_cast<ScAreaLink
*>(pBase
) ) )
135 return static_cast<ScAreaLink
*>(pBase
); // found
138 return NULL
; // not found
141 void ScAreaLinkSaveCollection::Restore( ScDocument
* pDoc
) const
143 // The save collection may contain additional entries that are not in the document.
144 // They must be inserted again.
145 // Entries from the save collection must be searched via source data, as the order
146 // of links changes if deleted entries are re-added to the link manager (always at the end).
148 sfx2::LinkManager
* pLinkManager
= pDoc
->GetLinkManager();
151 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
152 size_t nSaveCount
= size();
153 for (size_t nPos
=0; nPos
<nSaveCount
; ++nPos
)
155 const ScAreaLinkSaver
* pSaver
= (*this)[nPos
];
156 ScAreaLink
* pLink
= lcl_FindLink( rLinks
, *pSaver
);
158 pSaver
->WriteToLink( *pLink
); // restore output position
160 pSaver
->InsertNewLink( pDoc
); // re-insert deleted link
165 ScAreaLinkSaveCollection
* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument
* pDoc
)
167 ScAreaLinkSaveCollection
* pColl
= NULL
;
169 sfx2::LinkManager
* pLinkManager
= const_cast<ScDocument
*>(pDoc
)->GetLinkManager();
172 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
173 sal_uInt16 nLinkCount
= rLinks
.size();
174 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
176 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
177 if (pBase
->ISA(ScAreaLink
))
180 pColl
= new ScAreaLinkSaveCollection
;
182 ScAreaLinkSaver
* pSaver
= new ScAreaLinkSaver( *(ScAreaLink
*)pBase
);
183 pColl
->push_back(pSaver
);
191 const ScAreaLinkSaver
* ScAreaLinkSaveCollection::operator [](size_t nIndex
) const
193 return &maData
[nIndex
];
196 size_t ScAreaLinkSaveCollection::size() const
198 return maData
.size();
201 void ScAreaLinkSaveCollection::push_back(ScAreaLinkSaver
* p
)
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */