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: areasave.cxx,v $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 // -----------------------------------------------------------------------
38 // INCLUDE ---------------------------------------------------------------
40 #include <svx/linkmgr.hxx>
41 #include <tools/debug.hxx>
43 #include "areasave.hxx"
44 #include "arealink.hxx"
45 #include "document.hxx"
47 // -----------------------------------------------------------------------
49 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink
& rSource
) :
50 aFileName ( rSource
.GetFile() ),
51 aFilterName ( rSource
.GetFilter() ),
52 aOptions ( rSource
.GetOptions() ),
53 aSourceArea ( rSource
.GetSource() ),
54 aDestArea ( rSource
.GetDestArea() ),
55 nRefresh ( rSource
.GetRefreshDelay() ) // seconds
59 ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLinkSaver
& rCopy
) :
61 aFileName ( rCopy
.aFileName
),
62 aFilterName ( rCopy
.aFilterName
),
63 aOptions ( rCopy
.aOptions
),
64 aSourceArea ( rCopy
.aSourceArea
),
65 aDestArea ( rCopy
.aDestArea
),
66 nRefresh ( rCopy
.nRefresh
)
70 ScAreaLinkSaver::~ScAreaLinkSaver()
74 ScDataObject
* ScAreaLinkSaver::Clone() const
76 return new ScAreaLinkSaver( *this );
79 BOOL
ScAreaLinkSaver::IsEqualSource( const ScAreaLink
& rCompare
) const
81 return ( aFileName
== rCompare
.GetFile() &&
82 aFilterName
== rCompare
.GetFilter() &&
83 aOptions
== rCompare
.GetOptions() &&
84 aSourceArea
== rCompare
.GetSource() &&
85 nRefresh
== rCompare
.GetRefreshDelay() );
88 BOOL
ScAreaLinkSaver::IsEqual( const ScAreaLink
& rCompare
) const
90 return ( IsEqualSource( rCompare
) &&
91 aDestArea
== rCompare
.GetDestArea() );
94 void ScAreaLinkSaver::WriteToLink( ScAreaLink
& rLink
) const
96 rLink
.SetDestArea( aDestArea
);
99 void ScAreaLinkSaver::InsertNewLink( ScDocument
* pDoc
) const
101 // (see ScUndoRemoveAreaLink::Undo)
103 SvxLinkManager
* pLinkManager
= pDoc
->GetLinkManager();
104 SfxObjectShell
* pObjSh
= pDoc
->GetDocumentShell();
106 if ( pLinkManager
&& pObjSh
)
108 ScAreaLink
* pLink
= new ScAreaLink( pObjSh
, aFileName
, aFilterName
, aOptions
,
109 aSourceArea
, aDestArea
.aStart
, nRefresh
);
110 pLink
->SetInCreate( TRUE
);
111 pLink
->SetDestArea( aDestArea
);
112 pLinkManager
->InsertFileLink( *pLink
, OBJECT_CLIENT_FILE
, aFileName
, &aFilterName
, &aSourceArea
);
114 pLink
->SetInCreate( FALSE
);
118 // -----------------------------------------------------------------------
120 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection()
124 ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection
& rCopy
) :
125 ScCollection( rCopy
)
129 ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection()
133 ScDataObject
* ScAreaLinkSaveCollection::Clone() const
135 return new ScAreaLinkSaveCollection( *this );
138 BOOL
ScAreaLinkSaveCollection::IsEqual( const ScDocument
* pDoc
) const
140 // IsEqual can be checked in sequence.
141 // Neither ref-update nor removing links will change the order.
143 SvxLinkManager
* pLinkManager
= const_cast<ScDocument
*>(pDoc
)->GetLinkManager();
147 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
148 USHORT nLinkCount
= rLinks
.Count();
149 for (USHORT i
=0; i
<nLinkCount
; i
++)
151 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
152 if (pBase
->ISA(ScAreaLink
))
154 if ( nPos
>= GetCount() || !(*this)[nPos
]->IsEqual( *(ScAreaLink
*)pBase
) )
160 if ( nPos
< GetCount() )
161 return FALSE
; // fewer links in the document than in the save collection
167 ScAreaLink
* lcl_FindLink( const ::sfx2::SvBaseLinks
& rLinks
, const ScAreaLinkSaver
& rSaver
)
169 USHORT nLinkCount
= rLinks
.Count();
170 for (USHORT i
=0; i
<nLinkCount
; i
++)
172 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
173 if ( pBase
->ISA(ScAreaLink
) &&
174 rSaver
.IsEqualSource( *static_cast<ScAreaLink
*>(pBase
) ) )
176 return static_cast<ScAreaLink
*>(pBase
); // found
179 return NULL
; // not found
182 void ScAreaLinkSaveCollection::Restore( ScDocument
* pDoc
) const
184 // The save collection may contain additional entries that are not in the document.
185 // They must be inserted again.
186 // Entries from the save collection must be searched via source data, as the order
187 // of links changes if deleted entries are re-added to the link manager (always at the end).
189 SvxLinkManager
* pLinkManager
= pDoc
->GetLinkManager();
192 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
193 USHORT nSaveCount
= GetCount();
194 for (USHORT nPos
=0; nPos
<nSaveCount
; nPos
++)
196 ScAreaLinkSaver
* pSaver
= (*this)[nPos
];
197 ScAreaLink
* pLink
= lcl_FindLink( rLinks
, *pSaver
);
199 pSaver
->WriteToLink( *pLink
); // restore output position
201 pSaver
->InsertNewLink( pDoc
); // re-insert deleted link
207 ScAreaLinkSaveCollection
* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument
* pDoc
)
209 ScAreaLinkSaveCollection
* pColl
= NULL
;
211 SvxLinkManager
* pLinkManager
= const_cast<ScDocument
*>(pDoc
)->GetLinkManager();
214 const ::sfx2::SvBaseLinks
& rLinks
= pLinkManager
->GetLinks();
215 USHORT nLinkCount
= rLinks
.Count();
216 for (USHORT i
=0; i
<nLinkCount
; i
++)
218 ::sfx2::SvBaseLink
* pBase
= *rLinks
[i
];
219 if (pBase
->ISA(ScAreaLink
))
222 pColl
= new ScAreaLinkSaveCollection
;
224 ScAreaLinkSaver
* pSaver
= new ScAreaLinkSaver( *(ScAreaLink
*)pBase
);
225 if (!pColl
->Insert(pSaver
))