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"
25 #include <documentlinkmgr.hxx>
27 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink
& rSource
) :
28 aFileName ( rSource
.GetFile() ),
29 aFilterName ( rSource
.GetFilter() ),
30 aOptions ( rSource
.GetOptions() ),
31 aSourceArea ( rSource
.GetSource() ),
32 aDestArea ( rSource
.GetDestArea() ),
33 nRefresh ( rSource
.GetRefreshDelay() ) // seconds
37 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLinkSaver
& rCopy
) :
38 aFileName ( rCopy
.aFileName
),
39 aFilterName ( rCopy
.aFilterName
),
40 aOptions ( rCopy
.aOptions
),
41 aSourceArea ( rCopy
.aSourceArea
),
42 aDestArea ( rCopy
.aDestArea
),
43 nRefresh ( rCopy
.nRefresh
)
47 ScAreaLinkSaver::~ScAreaLinkSaver() {}
49 bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink
& rCompare
) const
51 return ( aFileName
.equals(rCompare
.GetFile()) &&
52 aFilterName
.equals(rCompare
.GetFilter()) &&
53 aOptions
.equals(rCompare
.GetOptions()) &&
54 aSourceArea
.equals(rCompare
.GetSource()) &&
55 nRefresh
== rCompare
.GetRefreshDelay() );
58 bool ScAreaLinkSaver::IsEqual( const ScAreaLink
& rCompare
) const
60 return ( IsEqualSource( rCompare
) &&
61 aDestArea
== rCompare
.GetDestArea() );
64 void ScAreaLinkSaver::WriteToLink( ScAreaLink
& rLink
) const
66 rLink
.SetDestArea( aDestArea
);
69 void ScAreaLinkSaver::InsertNewLink( ScDocument
* pDoc
)
71 // (see ScUndoRemoveAreaLink::Undo)
73 sfx2::LinkManager
* pLinkManager
= pDoc
->GetLinkManager();
74 SfxObjectShell
* pObjSh
= pDoc
->GetDocumentShell();
76 if ( pLinkManager
&& pObjSh
)
78 ScAreaLink
* pLink
= new ScAreaLink( pObjSh
, aFileName
, aFilterName
, aOptions
,
79 aSourceArea
, aDestArea
.aStart
, nRefresh
);
80 pLink
->SetInCreate( true );
81 pLink
->SetDestArea( aDestArea
);
82 OUString
aTmp1(aFilterName
), aTmp2(aSourceArea
);
83 pLinkManager
->InsertFileLink( *pLink
, OBJECT_CLIENT_FILE
, aFileName
, &aTmp1
, &aTmp2
);
85 pLink
->SetInCreate( false );
89 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection() {}
91 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection
& r
) :
94 ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection() {}
96 bool ScAreaLinkSaveCollection::IsEqual( const ScDocument
* pDoc
) const
98 // IsEqual can be checked in sequence.
99 // Neither ref-update nor removing links will change the order.
101 const sfx2::LinkManager
* pLinkManager
= pDoc
->GetLinkManager();
105 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
106 sal_uInt16 nLinkCount
= rLinks
.size();
107 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
109 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
110 if (pBase
->ISA(ScAreaLink
))
112 if ( nPos
>= size() || !(*this)[nPos
]->IsEqual( *static_cast<ScAreaLink
*>(pBase
) ) )
119 return false; // fewer links in the document than in the save collection
125 static ScAreaLink
* lcl_FindLink( const ::sfx2::SvBaseLinks
& rLinks
, const ScAreaLinkSaver
& rSaver
)
127 sal_uInt16 nLinkCount
= rLinks
.size();
128 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
130 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
131 if ( pBase
->ISA(ScAreaLink
) &&
132 rSaver
.IsEqualSource( *static_cast<ScAreaLink
*>(pBase
) ) )
134 return static_cast<ScAreaLink
*>(pBase
); // found
137 return NULL
; // not found
140 void ScAreaLinkSaveCollection::Restore( ScDocument
* pDoc
)
142 // The save collection may contain additional entries that are not in the document.
143 // They must be inserted again.
144 // Entries from the save collection must be searched via source data, as the order
145 // of links changes if deleted entries are re-added to the link manager (always at the end).
147 sfx2::LinkManager
* pLinkManager
= pDoc
->GetDocLinkManager().getLinkManager(false);
150 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
151 size_t nSaveCount
= size();
152 for (size_t nPos
=0; nPos
<nSaveCount
; ++nPos
)
154 ScAreaLinkSaver
* pSaver
= (*this)[nPos
];
155 ScAreaLink
* pLink
= lcl_FindLink( rLinks
, *pSaver
);
157 pSaver
->WriteToLink( *pLink
); // restore output position
159 pSaver
->InsertNewLink( pDoc
); // re-insert deleted link
164 ScAreaLinkSaveCollection
* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument
* pDoc
)
166 ScAreaLinkSaveCollection
* pColl
= NULL
;
168 sfx2::LinkManager
* pLinkManager
= const_cast<ScDocument
*>(pDoc
)->GetLinkManager();
171 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
172 sal_uInt16 nLinkCount
= rLinks
.size();
173 for (sal_uInt16 i
=0; i
<nLinkCount
; i
++)
175 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
176 if (pBase
->ISA(ScAreaLink
))
179 pColl
= new ScAreaLinkSaveCollection
;
181 ScAreaLinkSaver
* pSaver
= new ScAreaLinkSaver( *static_cast<ScAreaLink
*>(pBase
));
182 pColl
->push_back(pSaver
);
190 ScAreaLinkSaver
* ScAreaLinkSaveCollection::operator [](size_t nIndex
)
192 return &maData
[nIndex
];
195 const ScAreaLinkSaver
* ScAreaLinkSaveCollection::operator [](size_t nIndex
) const
197 return &maData
[nIndex
];
200 size_t ScAreaLinkSaveCollection::size() const
202 return maData
.size();
205 void ScAreaLinkSaveCollection::push_back(ScAreaLinkSaver
* p
)
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */