tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / docshell / documentlinkmgr.cxx
blob0fb89cfa0d18237a23ddb730e62703cb76616926
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <comphelper/doublecheckedinit.hxx>
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>
35 #include <memory>
37 namespace sc {
39 struct DocumentLinkManagerImpl
41 SfxObjectShell* mpShell;
42 std::unique_ptr<DataStream, o3tl::default_delete<DataStream>> mpDataStream;
43 std::atomic<sfx2::LinkManager*> mpLinkManager;
45 DocumentLinkManagerImpl(const DocumentLinkManagerImpl&) = delete;
46 const DocumentLinkManagerImpl& operator=(const DocumentLinkManagerImpl&) = delete;
48 explicit DocumentLinkManagerImpl(SfxObjectShell* pShell)
49 : mpShell(pShell), mpLinkManager(nullptr) {}
51 ~DocumentLinkManagerImpl()
53 // Shared base links
54 sfx2::LinkManager* linkManager = mpLinkManager;
55 if (linkManager)
57 sfx2::SvLinkSources aTemp = linkManager->GetServers();
58 for (const auto& pLinkSource : aTemp)
59 pLinkSource->Closed();
61 if (!linkManager->GetLinks().empty())
62 linkManager->Remove(0, linkManager->GetLinks().size());
64 delete linkManager;
68 DocumentLinkManager::DocumentLinkManager( SfxObjectShell* pShell ) :
69 mpImpl(new DocumentLinkManagerImpl(pShell)) {}
71 DocumentLinkManager::~DocumentLinkManager()
75 void DocumentLinkManager::setDataStream( DataStream* p )
77 mpImpl->mpDataStream.reset(p);
80 DataStream* DocumentLinkManager::getDataStream()
82 return mpImpl->mpDataStream.get();
85 const DataStream* DocumentLinkManager::getDataStream() const
87 return mpImpl->mpDataStream.get();
90 sfx2::LinkManager* DocumentLinkManager::getLinkManager( bool bCreate )
92 if (bCreate && mpImpl->mpShell)
93 return comphelper::doubleCheckedInit( mpImpl->mpLinkManager,
94 [this]() { return new sfx2::LinkManager(mpImpl->mpShell); } );
95 return mpImpl->mpLinkManager;
98 const sfx2::LinkManager* DocumentLinkManager::getExistingLinkManager() const
100 return mpImpl->mpLinkManager;
103 bool DocumentLinkManager::idleCheckLinks()
105 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
106 if (!pMgr)
107 return false;
109 bool bAnyLeft = false;
110 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
111 for (const auto & rLink : rLinks)
113 sfx2::SvBaseLink* pBase = rLink.get();
114 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
115 if (!pDdeLink || !pDdeLink->NeedsUpdate())
116 continue;
118 pDdeLink->TryUpdate();
119 if (pDdeLink->NeedsUpdate()) // Was not successful?
120 bAnyLeft = true;
123 return bAnyLeft;
126 bool DocumentLinkManager::hasDdeLinks() const
128 return hasDdeOrOleOrWebServiceLinks(true, false, false);
131 bool DocumentLinkManager::hasDdeOrOleOrWebServiceLinks() const
133 return hasDdeOrOleOrWebServiceLinks(true, true, true);
136 bool DocumentLinkManager::hasDdeOrOleOrWebServiceLinks(bool bDde, bool bOle, bool bWebService) const
138 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
139 if (!pMgr)
140 return false;
142 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
143 for (const auto & rLink : rLinks)
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;
154 return false;
157 bool DocumentLinkManager::hasExternalRefLinks() const
159 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
160 if (!pMgr)
161 return false;
163 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
164 for (const auto & rLink : rLinks)
166 sfx2::SvBaseLink* pBase = rLink.get();
167 if (dynamic_cast<ScExternalRefLink*>(pBase))
168 return true;
171 return false;
174 bool DocumentLinkManager::updateDdeOrOleOrWebServiceLinks(weld::Window* pWin)
176 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
177 if (!pMgr)
178 return false;
180 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
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)
187 sfx2::SvBaseLink* pBase = rLink.get();
189 SdrEmbedObjectLink* pOleLink = dynamic_cast<SdrEmbedObjectLink*>(pBase);
190 if (pOleLink)
192 pOleLink->Update();
193 continue;
196 SdrIFrameLink* pIFrameLink = dynamic_cast<SdrIFrameLink*>(pBase);
197 if (pIFrameLink)
199 pIFrameLink->Update();
200 continue;
203 ScWebServiceLink* pWebserviceLink = dynamic_cast<ScWebServiceLink*>(pBase);
204 if (pWebserviceLink)
206 pWebserviceLink->Update();
207 continue;
210 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
211 if (!pDdeLink)
212 continue;
214 if (pDdeLink->Update())
215 bAny = true;
216 else
218 // Update failed. Notify the user.
219 const OUString& aFile = pDdeLink->GetTopic();
220 const OUString& aElem = pDdeLink->GetItem();
221 const OUString& aType = pDdeLink->GetAppl();
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();
239 pMgr->CloseCachedComps();
241 return bAny;
244 void DocumentLinkManager::updateDdeLink( std::u16string_view rAppl, std::u16string_view rTopic, std::u16string_view rItem )
246 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
247 if (!pMgr)
248 return;
250 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
252 for (const auto & rLink : rLinks)
254 ::sfx2::SvBaseLink* pBase = rLink.get();
255 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
256 if (!pDdeLink)
257 continue;
259 if ( pDdeLink->GetAppl() == rAppl &&
260 pDdeLink->GetTopic() == rTopic &&
261 pDdeLink->GetItem() == rItem )
263 pDdeLink->TryUpdate();
264 // Could be multiple (Mode), so continue searching
269 size_t DocumentLinkManager::getDdeLinkCount() const
271 sfx2::LinkManager* pMgr = mpImpl->mpLinkManager;
272 if (!pMgr)
273 return 0;
275 size_t nDdeCount = 0;
276 const sfx2::SvBaseLinks& rLinks = pMgr->GetLinks();
277 for (const auto & rLink : rLinks)
279 ::sfx2::SvBaseLink* pBase = rLink.get();
280 ScDdeLink* pDdeLink = dynamic_cast<ScDdeLink*>(pBase);
281 if (!pDdeLink)
282 continue;
284 ++nDdeCount;
287 return nDdeCount;
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */