1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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()
54 sfx2::LinkManager
* linkManager
= mpLinkManager
;
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());
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
;
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())
118 pDdeLink
->TryUpdate();
119 if (pDdeLink
->NeedsUpdate()) // Was not successful?
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
;
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
))
148 if (bOle
&& (dynamic_cast<SdrEmbedObjectLink
*>(pBase
) || dynamic_cast<SdrIFrameLink
*>(pBase
)))
150 if (bWebService
&& dynamic_cast<ScWebServiceLink
*>(pBase
))
157 bool DocumentLinkManager::hasExternalRefLinks() const
159 sfx2::LinkManager
* pMgr
= mpImpl
->mpLinkManager
;
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
))
174 bool DocumentLinkManager::updateDdeOrOleOrWebServiceLinks(weld::Window
* pWin
)
176 sfx2::LinkManager
* pMgr
= mpImpl
->mpLinkManager
;
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
185 for (const auto & rLink
: rLinks
)
187 sfx2::SvBaseLink
* pBase
= rLink
.get();
189 SdrEmbedObjectLink
* pOleLink
= dynamic_cast<SdrEmbedObjectLink
*>(pBase
);
196 SdrIFrameLink
* pIFrameLink
= dynamic_cast<SdrIFrameLink
*>(pBase
);
199 pIFrameLink
->Update();
203 ScWebServiceLink
* pWebserviceLink
= dynamic_cast<ScWebServiceLink
*>(pBase
);
206 pWebserviceLink
->Update();
210 ScDdeLink
* pDdeLink
= dynamic_cast<ScDdeLink
*>(pBase
);
214 if (pDdeLink
->Update())
218 // Update failed. Notify the user.
219 const OUString
& aFile
= pDdeLink
->GetTopic();
220 const OUString
& aElem
= pDdeLink
->GetItem();
221 const OUString
& aType
= pDdeLink
->GetAppl();
224 ScResId(SCSTR_DDEDOC_NOT_LOADED
) +
232 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(pWin
,
233 VclMessageType::Warning
, VclButtonsType::Ok
,
239 pMgr
->CloseCachedComps();
244 void DocumentLinkManager::updateDdeLink( std::u16string_view rAppl
, std::u16string_view rTopic
, std::u16string_view rItem
)
246 sfx2::LinkManager
* pMgr
= mpImpl
->mpLinkManager
;
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
);
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
;
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
);
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */