LibreOffice Module sc (master) 1
areasave.cxx
Go to the documentation of this file.
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#include <documentlinkmgr.hxx>
26
28 aFileName ( rSource.GetFile() ),
29 aFilterName ( rSource.GetFilter() ),
30 aOptions ( rSource.GetOptions() ),
31 aSourceArea ( rSource.GetSource() ),
32 aDestArea ( rSource.GetDestArea() ),
33 nRefreshDelaySeconds ( rSource.GetRefreshDelaySeconds() ) // seconds
34{
35}
36
37bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink& rCompare ) const
38{
39 return ( aFileName == rCompare.GetFile() &&
40 aFilterName == rCompare.GetFilter() &&
41 aOptions == rCompare.GetOptions() &&
42 aSourceArea == rCompare.GetSource() &&
44}
45
46bool ScAreaLinkSaver::IsEqual( const ScAreaLink& rCompare ) const
47{
48 return ( IsEqualSource( rCompare ) &&
49 aDestArea == rCompare.GetDestArea() );
50}
51
53{
54 rLink.SetDestArea( aDestArea );
55}
56
58{
59 // (see ScUndoRemoveAreaLink::Undo)
60
61 sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
62 SfxObjectShell* pObjSh = pDoc->GetDocumentShell();
63
64 if ( pLinkManager && pObjSh )
65 {
68 pLink->SetInCreate( true );
69 pLink->SetDestArea( aDestArea );
70 OUString aTmp1(aFilterName), aTmp2(aSourceArea);
71 pLinkManager->InsertFileLink( *pLink, sfx2::SvBaseLinkObjectType::ClientFile, aFileName, &aTmp1, &aTmp2 );
72 pLink->Update();
73 pLink->SetInCreate( false );
74 }
75}
76
78
80
82{
83 // IsEqual can be checked in sequence.
84 // Neither ref-update nor removing links will change the order.
85
86 const sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
87 if (pLinkManager)
88 {
89 size_t nPos = 0;
90 const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
91 sal_uInt16 nLinkCount = rLinks.size();
92 for (sal_uInt16 i=0; i<nLinkCount; i++)
93 {
94 ::sfx2::SvBaseLink* pBase = rLinks[i].get();
95 if (auto pAreaLink = dynamic_cast<ScAreaLink*>( pBase))
96 {
97 if ( nPos >= size() || !(*this)[nPos].IsEqual( *pAreaLink ) )
98 return false;
99
100 ++nPos;
101 }
102 }
103 if ( nPos < size() )
104 return false; // fewer links in the document than in the save collection
105 }
106
107 return true;
108}
109
110static ScAreaLink* lcl_FindLink( const ::sfx2::SvBaseLinks& rLinks, const ScAreaLinkSaver& rSaver )
111{
112 sal_uInt16 nLinkCount = rLinks.size();
113 for (sal_uInt16 i=0; i<nLinkCount; i++)
114 {
115 ::sfx2::SvBaseLink* pBase = rLinks[i].get();
116 if ( auto pAreaLink = dynamic_cast<ScAreaLink*>( pBase) )
117 if ( rSaver.IsEqualSource( *pAreaLink ) )
118 return pAreaLink; // found
119 }
120 return nullptr; // not found
121}
122
124{
125 // The save collection may contain additional entries that are not in the document.
126 // They must be inserted again.
127 // Entries from the save collection must be searched via source data, as the order
128 // of links changes if deleted entries are re-added to the link manager (always at the end).
129
130 sfx2::LinkManager* pLinkManager = pDoc->GetDocLinkManager().getLinkManager(false);
131 if (!pLinkManager)
132 return;
133
134 const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
135 size_t nSaveCount = size();
136 for (size_t nPos=0; nPos<nSaveCount; ++nPos)
137 {
138 ScAreaLinkSaver& rSaver = (*this)[nPos];
139 ScAreaLink* pLink = lcl_FindLink( rLinks, rSaver );
140 if ( pLink )
141 rSaver.WriteToLink( *pLink ); // restore output position
142 else
143 rSaver.InsertNewLink( pDoc ); // re-insert deleted link
144 }
145}
146
147std::unique_ptr<ScAreaLinkSaveCollection> ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument* pDoc )
148{
149 std::unique_ptr<ScAreaLinkSaveCollection> pColl;
150
151 sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager();
152 if (pLinkManager)
153 {
154 const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
155 sal_uInt16 nLinkCount = rLinks.size();
156 for (sal_uInt16 i=0; i<nLinkCount; i++)
157 {
158 ::sfx2::SvBaseLink* pBase = rLinks[i].get();
159 if (auto pAreaLink = dynamic_cast<ScAreaLink*>( pBase))
160 {
161 if (!pColl)
162 pColl.reset(new ScAreaLinkSaveCollection);
163
164 pColl->push_back( ScAreaLinkSaver( *pAreaLink ) );
165 }
166 }
167 }
168
169 return pColl;
170}
171
173{
174 return maData[nIndex];
175}
176
178{
179 return maData[nIndex];
180}
181
183{
184 return maData.size();
185}
186
188{
189 maData.push_back(p);
190}
191
192/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static ScAreaLink * lcl_FindLink(const ::sfx2::SvBaseLinks &rLinks, const ScAreaLinkSaver &rSaver)
Definition: areasave.cxx:110
static std::unique_ptr< ScAreaLinkSaveCollection > CreateFromDoc(const ScDocument *pDoc)
Definition: areasave.cxx:147
void push_back(const ScAreaLinkSaver &)
Definition: areasave.cxx:187
void Restore(ScDocument *pDoc)
Definition: areasave.cxx:123
bool IsEqual(const ScDocument *pDoc) const
Definition: areasave.cxx:81
size_t size() const
Definition: areasave.cxx:182
ScAreaLinkSaver & operator[](size_t nIndex)
Definition: areasave.cxx:172
OUString aSourceArea
Definition: areasave.hxx:36
bool IsEqualSource(const ScAreaLink &rCompare) const
Definition: areasave.cxx:37
bool IsEqual(const ScAreaLink &rCompare) const
Definition: areasave.cxx:46
ScRange aDestArea
Definition: areasave.hxx:37
OUString aOptions
Definition: areasave.hxx:35
void WriteToLink(ScAreaLink &rLink) const
Definition: areasave.cxx:52
OUString aFilterName
Definition: areasave.hxx:34
sal_Int32 nRefreshDelaySeconds
Definition: areasave.hxx:38
ScAreaLinkSaver(const ScAreaLink &rSource)
Definition: areasave.cxx:27
void InsertNewLink(ScDocument *pDoc)
Definition: areasave.cxx:57
OUString aFileName
Definition: areasave.hxx:33
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1081
sc::DocumentLinkManager & GetDocLinkManager()
Definition: documen2.cxx:241
SC_DLLPUBLIC sfx2::LinkManager * GetLinkManager()
Definition: documen2.cxx:231
ScAddress aStart
Definition: address.hxx:497
sal_Int32 GetRefreshDelaySeconds() const
sfx2::LinkManager * getLinkManager(bool bCreate=true)
void InsertFileLink(sfx2::SvBaseLink &, SvBaseLinkObjectType nFileType, std::u16string_view rFileNm, const OUString *pFilterNm=nullptr, const OUString *pRange=nullptr)
const SvBaseLinks & GetLinks() const
sal_Int32 nIndex
void * p
sal_uInt16 nPos
SvBaseLink * pLink
int i