Resolves: tdf#162093 TableRef item specifier may occur standalone
[LibreOffice.git] / sc / source / core / tool / webservicelink.cxx
blobc30f34300edf1d35bb555d589efb1eaf5b3e2194
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <comphelper/processfactory.hxx>
11 #include <sfx2/linkmgr.hxx>
12 #include <sfx2/bindings.hxx>
14 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
15 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
16 #include <com/sun/star/io/XInputStream.hpp>
18 #include <tools/hostfilter.hxx>
19 #include <tools/urlobj.hxx>
21 #include <utility>
22 #include <webservicelink.hxx>
23 #include <brdcst.hxx>
24 #include <document.hxx>
25 #include <sc.hrc>
27 ScWebServiceLink::ScWebServiceLink(ScDocument* pD, OUString _aURL)
28 : ::sfx2::SvBaseLink(SfxLinkUpdateMode::ALWAYS, SotClipboardFormatId::STRING)
29 , pDoc(pD)
30 , aURL(std::move(_aURL))
31 , bHasResult(false)
35 ScWebServiceLink::~ScWebServiceLink() {}
37 sfx2::SvBaseLink::UpdateResult ScWebServiceLink::DataChanged(const OUString&, const css::uno::Any&)
39 aResult.clear();
40 bHasResult = false;
42 INetURLObject aURLObject(aURL);
43 const OUString sHost = aURLObject.GetHost();
44 if (HostFilter::isForbidden(sHost))
46 SAL_WARN("sc.ui", "ScWebServiceLink::DataChanged: blocked access to external file: \""
47 << aURL << "\"");
48 return ERROR_GENERAL;
51 css::uno::Reference<css::ucb::XSimpleFileAccess3> xFileAccess
52 = css::ucb::SimpleFileAccess::create(comphelper::getProcessComponentContext());
53 if (!xFileAccess.is())
54 return ERROR_GENERAL;
56 css::uno::Reference<css::io::XInputStream> xStream;
57 try
59 xStream = xFileAccess->openFileRead(aURL);
61 catch (...)
63 // don't let any exceptions pass
64 return ERROR_GENERAL;
66 if (!xStream.is())
67 return ERROR_GENERAL;
69 const sal_Int32 BUF_LEN = 8000;
70 css::uno::Sequence<sal_Int8> buffer(BUF_LEN);
71 OStringBuffer aBuffer(64000);
73 sal_Int32 nRead = 0;
74 while ((nRead = xStream->readBytes(buffer, BUF_LEN)) == BUF_LEN)
75 aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);
77 if (nRead > 0)
78 aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);
80 xStream->closeInput();
82 aResult = OStringToOUString(aBuffer, RTL_TEXTENCODING_UTF8);
83 bHasResult = true;
85 // Something happened...
86 if (HasListeners())
88 Broadcast(ScHint(SfxHintId::ScDataChanged, ScAddress()));
89 pDoc->TrackFormulas(); // must happen immediately
90 pDoc->StartTrackTimer();
93 return SUCCESS;
96 void ScWebServiceLink::ListenersGone()
98 ScDocument* pStackDoc = pDoc; // member pDoc can't be used after removing the link
100 sfx2::LinkManager* pLinkMgr = pDoc->GetLinkManager();
101 pLinkMgr->Remove(this); // deletes this
103 if (pLinkMgr->GetLinks().empty()) // deleted the last one ?
105 SfxBindings* pBindings = pStackDoc->GetViewBindings(); // don't use member pDoc!
106 if (pBindings)
107 pBindings->Invalidate(SID_LINKS);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */