LibreOffice Module sc (master) 1
documentlinkmgr.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
21#include <documentlinkmgr.hxx>
22#include <datastream.hxx>
23#include <ddelink.hxx>
24#include <externalrefmgr.hxx>
25#include <webservicelink.hxx>
26#include <strings.hrc>
27#include <scresid.hxx>
28#include <sfx2/linkmgr.hxx>
29#include <sfx2/linksrc.hxx>
30#include <o3tl/deleter.hxx>
31#include <svx/svdoole2.hxx>
32#include <vcl/svapp.hxx>
33#include <vcl/weld.hxx>
34
35#include <memory>
36
37namespace sc {
38
40{
42 std::unique_ptr<DataStream, o3tl::default_delete<DataStream>> mpDataStream;
43 std::atomic<sfx2::LinkManager*> mpLinkManager;
44
47
49 : mpShell(pShell), mpLinkManager(nullptr) {}
50
52 {
53 // Shared base links
54 sfx2::LinkManager* linkManager = mpLinkManager;
55 if (linkManager)
56 {
57 sfx2::SvLinkSources aTemp = linkManager->GetServers();
58 for (const auto& pLinkSource : aTemp)
59 pLinkSource->Closed();
60
61 if (!linkManager->GetLinks().empty())
62 linkManager->Remove(0, linkManager->GetLinks().size());
63 }
64 delete linkManager;
65 }
66};
67
69 mpImpl(new DocumentLinkManagerImpl(pShell)) {}
70
72{
73}
74
76{
77 mpImpl->mpDataStream.reset(p);
78}
79
81{
82 return mpImpl->mpDataStream.get();
83}
84
86{
87 return mpImpl->mpDataStream.get();
88}
89
91{
92 if (bCreate && mpImpl->mpShell)
93 return comphelper::doubleCheckedInit( mpImpl->mpLinkManager,
94 [this]() { return new sfx2::LinkManager(mpImpl->mpShell); } );
95 return mpImpl->mpLinkManager;
96}
97
99{
100 return mpImpl->mpLinkManager;
101}
102
104{
105 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
106 if (!pMgr)
107 return false;
108
109 bool bAnyLeft = false;
110 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
111 for (const auto & rLink : rLinks)
112 {
113 sfx2::SvBaseLink* pBase = rLink.get();
114 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
115 if (!pDdeLink || !pDdeLink->NeedsUpdate())
116 continue;
117
118 pDdeLink->TryUpdate();
119 if (pDdeLink->NeedsUpdate()) // Was not successful?
120 bAnyLeft = true;
121 }
122
123 return bAnyLeft;
124}
125
127{
128 return hasDdeOrOleOrWebServiceLinks(true, false, false);
129}
130
132{
133 return hasDdeOrOleOrWebServiceLinks(true, true, true);
134}
135
136bool DocumentLinkManager::hasDdeOrOleOrWebServiceLinks(bool bDde, bool bOle, bool bWebService) const
137{
138 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
139 if (!pMgr)
140 return false;
141
142 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
143 for (const auto & rLink : rLinks)
144 {
145 sfx2::SvBaseLink* pBase = rLink.get();
146 if (bDde && dynamic_cast<ScDdeLink*>(pBase))
147 return true;
148 if (bOle && (dynamic_cast<SdrEmbedObjectLink*>(pBase) || dynamic_cast<SdrIFrameLink*>(pBase)))
149 return true;
150 if (bWebService && dynamic_cast<ScWebServiceLink*>(pBase))
151 return true;
152 }
153
154 return false;
155}
156
158{
159 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
160 if (!pMgr)
161 return false;
162
163 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
164 for (const auto & rLink : rLinks)
165 {
166 sfx2::SvBaseLink* pBase = rLink.get();
167 if (dynamic_cast<ScExternalRefLink*>(pBase))
168 return true;
169 }
170
171 return false;
172}
173
175{
176 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
177 if (!pMgr)
178 return false;
179
180 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
181
182 // If the update takes longer, reset all values so that nothing
183 // old (wrong) is left behind
184 bool bAny = false;
185 for (const auto & rLink : rLinks)
186 {
187 sfx2::SvBaseLink* pBase = rLink.get();
188
189 SdrEmbedObjectLink* pOleLink = dynamic_cast<SdrEmbedObjectLink*>(pBase);
190 if (pOleLink)
191 {
192 pOleLink->Update();
193 continue;
194 }
195
196 SdrIFrameLink* pIFrameLink = dynamic_cast<SdrIFrameLink*>(pBase);
197 if (pIFrameLink)
198 {
199 pIFrameLink->Update();
200 continue;
201 }
202
203 ScWebServiceLink* pWebserviceLink = dynamic_cast<ScWebServiceLink*>(pBase);
204 if (pWebserviceLink)
205 {
206 pWebserviceLink->Update();
207 continue;
208 }
209
210 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
211 if (!pDdeLink)
212 continue;
213
214 if (pDdeLink->Update())
215 bAny = true;
216 else
217 {
218 // Update failed. Notify the user.
219 const OUString& aFile = pDdeLink->GetTopic();
220 const OUString& aElem = pDdeLink->GetItem();
221 const OUString& aType = pDdeLink->GetAppl();
222
223 OUString sMessage =
224 ScResId(SCSTR_DDEDOC_NOT_LOADED) +
225 "\n\n"
226 "Source : " +
227 aFile +
228 "\nElement : " +
229 aElem +
230 "\nType : " +
231 aType;
232 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pWin,
233 VclMessageType::Warning, VclButtonsType::Ok,
234 sMessage));
235 xBox->run();
236 }
237 }
238
239 pMgr->CloseCachedComps();
240
241 return bAny;
242}
243
244void DocumentLinkManager::updateDdeLink( std::u16string_view rAppl, std::u16string_view rTopic, std::u16string_view rItem )
245{
246 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
247 if (!pMgr)
248 return;
249
250 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
251
252 for (const auto & rLink : rLinks)
253 {
254 ::sfx2::SvBaseLink* pBase = rLink.get();
255 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
256 if (!pDdeLink)
257 continue;
258
259 if ( pDdeLink->GetAppl() == rAppl &&
260 pDdeLink->GetTopic() == rTopic &&
261 pDdeLink->GetItem() == rItem )
262 {
263 pDdeLink->TryUpdate();
264 // Could be multiple (Mode), so continue searching
265 }
266 }
267}
268
270{
271 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
272 if (!pMgr)
273 return 0;
274
275 size_t nDdeCount = 0;
276 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
277 for (const auto & rLink : rLinks)
278 {
279 ::sfx2::SvBaseLink* pBase = rLink.get();
280 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
281 if (!pDdeLink)
282 continue;
283
284 ++nDdeCount;
285 }
286
287 return nDdeCount;
288}
289
290}
291
292/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
const sfx2::LinkManager * getExistingLinkManager() const
bool hasDdeOrOleOrWebServiceLinks() const
DataStream * getDataStream()
std::unique_ptr< DocumentLinkManagerImpl > mpImpl
sfx2::LinkManager * getLinkManager(bool bCreate=true)
void setDataStream(DataStream *p)
void updateDdeLink(std::u16string_view rAppl, std::u16string_view rTopic, std::u16string_view rItem)
size_t getDdeLinkCount() const
DocumentLinkManager(SfxObjectShell *pShell)
bool updateDdeOrOleOrWebServiceLinks(weld::Window *pWin)
const SvLinkSources & GetServers() const
void Remove(SvBaseLink const *pLink)
const SvBaseLinks & GetLinks() const
void * p
static Type * doubleCheckedInit(std::atomic< Type * > &pointer, Function function, GuardCtor guardCtor=osl::GetGlobalMutex())
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
std::vector< tools::SvRef< SvBaseLink > > SvBaseLinks
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
OUString sMessage
DocumentLinkManagerImpl(const DocumentLinkManagerImpl &)=delete
DocumentLinkManagerImpl(SfxObjectShell *pShell)
std::unique_ptr< DataStream, o3tl::default_delete< DataStream > > mpDataStream
std::atomic< sfx2::LinkManager * > mpLinkManager
const DocumentLinkManagerImpl & operator=(const DocumentLinkManagerImpl &)=delete