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