Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sfx2/linkmgr.hxx>
21 :
22 : #include "areasave.hxx"
23 : #include "arealink.hxx"
24 : #include "document.hxx"
25 :
26 : // -----------------------------------------------------------------------
27 :
28 0 : ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink& rSource ) :
29 0 : aFileName ( rSource.GetFile() ),
30 0 : aFilterName ( rSource.GetFilter() ),
31 0 : aOptions ( rSource.GetOptions() ),
32 0 : aSourceArea ( rSource.GetSource() ),
33 0 : aDestArea ( rSource.GetDestArea() ),
34 0 : nRefresh ( rSource.GetRefreshDelay() ) // seconds
35 : {
36 0 : }
37 :
38 0 : 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 0 : nRefresh ( rCopy.nRefresh )
45 : {
46 0 : }
47 :
48 0 : ScAreaLinkSaver::~ScAreaLinkSaver() {}
49 :
50 0 : bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink& rCompare ) const
51 : {
52 0 : return ( aFileName.equals(rCompare.GetFile()) &&
53 0 : aFilterName.equals(rCompare.GetFilter()) &&
54 0 : aOptions.equals(rCompare.GetOptions()) &&
55 0 : aSourceArea.equals(rCompare.GetSource()) &&
56 0 : nRefresh == rCompare.GetRefreshDelay() );
57 : }
58 :
59 0 : bool ScAreaLinkSaver::IsEqual( const ScAreaLink& rCompare ) const
60 : {
61 0 : return ( IsEqualSource( rCompare ) &&
62 0 : aDestArea == rCompare.GetDestArea() );
63 : }
64 :
65 0 : void ScAreaLinkSaver::WriteToLink( ScAreaLink& rLink ) const
66 : {
67 0 : rLink.SetDestArea( aDestArea );
68 0 : }
69 :
70 0 : void ScAreaLinkSaver::InsertNewLink( ScDocument* pDoc ) const
71 : {
72 : // (see ScUndoRemoveAreaLink::Undo)
73 :
74 0 : sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
75 0 : SfxObjectShell* pObjSh = pDoc->GetDocumentShell();
76 :
77 0 : if ( pLinkManager && pObjSh )
78 : {
79 : ScAreaLink* pLink = new ScAreaLink( pObjSh, aFileName, aFilterName, aOptions,
80 0 : aSourceArea, aDestArea.aStart, nRefresh );
81 0 : pLink->SetInCreate( sal_True );
82 0 : pLink->SetDestArea( aDestArea );
83 0 : String aTmp1(aFilterName), aTmp2(aSourceArea);
84 0 : pLinkManager->InsertFileLink( *pLink, OBJECT_CLIENT_FILE, aFileName, &aTmp1, &aTmp2 );
85 0 : pLink->Update();
86 0 : pLink->SetInCreate( false );
87 : }
88 0 : }
89 :
90 0 : ScAreaLinkSaveCollection::ScAreaLinkSaveCollection() {}
91 :
92 0 : ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection& r ) :
93 0 : maData(r.maData) {}
94 :
95 0 : ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection() {}
96 :
97 0 : bool ScAreaLinkSaveCollection::IsEqual( const ScDocument* pDoc ) const
98 : {
99 : // IsEqual can be checked in sequence.
100 : // Neither ref-update nor removing links will change the order.
101 :
102 0 : sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager();
103 0 : if (pLinkManager)
104 : {
105 0 : size_t nPos = 0;
106 0 : const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
107 0 : sal_uInt16 nLinkCount = rLinks.size();
108 0 : for (sal_uInt16 i=0; i<nLinkCount; i++)
109 : {
110 0 : ::sfx2::SvBaseLink* pBase = *rLinks[i];
111 0 : if (pBase->ISA(ScAreaLink))
112 : {
113 0 : if ( nPos >= size() || !(*this)[nPos]->IsEqual( *(ScAreaLink*)pBase ) )
114 0 : return false;
115 :
116 0 : ++nPos;
117 : }
118 : }
119 0 : if ( nPos < size() )
120 0 : return false; // fewer links in the document than in the save collection
121 : }
122 :
123 0 : return true;
124 : }
125 :
126 0 : static ScAreaLink* lcl_FindLink( const ::sfx2::SvBaseLinks& rLinks, const ScAreaLinkSaver& rSaver )
127 : {
128 0 : sal_uInt16 nLinkCount = rLinks.size();
129 0 : for (sal_uInt16 i=0; i<nLinkCount; i++)
130 : {
131 0 : ::sfx2::SvBaseLink* pBase = *rLinks[i];
132 0 : if ( pBase->ISA(ScAreaLink) &&
133 0 : rSaver.IsEqualSource( *static_cast<ScAreaLink*>(pBase) ) )
134 : {
135 0 : return static_cast<ScAreaLink*>(pBase); // found
136 : }
137 : }
138 0 : return NULL; // not found
139 : }
140 :
141 0 : void ScAreaLinkSaveCollection::Restore( ScDocument* pDoc ) const
142 : {
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).
147 :
148 0 : sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
149 0 : if (pLinkManager)
150 : {
151 0 : const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
152 0 : size_t nSaveCount = size();
153 0 : for (size_t nPos=0; nPos<nSaveCount; ++nPos)
154 : {
155 0 : const ScAreaLinkSaver* pSaver = (*this)[nPos];
156 0 : ScAreaLink* pLink = lcl_FindLink( rLinks, *pSaver );
157 0 : if ( pLink )
158 0 : pSaver->WriteToLink( *pLink ); // restore output position
159 : else
160 0 : pSaver->InsertNewLink( pDoc ); // re-insert deleted link
161 : }
162 : }
163 0 : }
164 :
165 3 : ScAreaLinkSaveCollection* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument* pDoc )
166 : {
167 3 : ScAreaLinkSaveCollection* pColl = NULL;
168 :
169 3 : sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager();
170 3 : if (pLinkManager)
171 : {
172 3 : const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
173 3 : sal_uInt16 nLinkCount = rLinks.size();
174 3 : for (sal_uInt16 i=0; i<nLinkCount; i++)
175 : {
176 0 : ::sfx2::SvBaseLink* pBase = *rLinks[i];
177 0 : if (pBase->ISA(ScAreaLink))
178 : {
179 0 : if (!pColl)
180 0 : pColl = new ScAreaLinkSaveCollection;
181 :
182 0 : ScAreaLinkSaver* pSaver = new ScAreaLinkSaver( *(ScAreaLink*)pBase );
183 0 : pColl->push_back(pSaver);
184 : }
185 : }
186 : }
187 :
188 3 : return pColl;
189 : }
190 :
191 0 : const ScAreaLinkSaver* ScAreaLinkSaveCollection::operator [](size_t nIndex) const
192 : {
193 0 : return &maData[nIndex];
194 : }
195 :
196 0 : size_t ScAreaLinkSaveCollection::size() const
197 : {
198 0 : return maData.size();
199 : }
200 :
201 0 : void ScAreaLinkSaveCollection::push_back(ScAreaLinkSaver* p)
202 : {
203 0 : maData.push_back(p);
204 0 : }
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|