LibreOffice Module sc (master)  1
webservicelink.cxx
Go to the documentation of this file.
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.
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 
11 #include <sfx2/linkmgr.hxx>
12 #include <sfx2/bindings.hxx>
13 
14 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
15 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
16 #include <com/sun/star/io/XInputStream.hpp>
17 
18 #include <webservicelink.hxx>
19 #include <brdcst.hxx>
20 #include <document.hxx>
21 #include <sc.hrc>
22 
25  , pDoc(pD)
26  , aURL(rURL)
27  , bHasResult(false)
28 {
29 }
30 
32 
33 sfx2::SvBaseLink::UpdateResult ScWebServiceLink::DataChanged(const OUString&, const css::uno::Any&)
34 {
35  aResult.clear();
36  bHasResult = false;
37 
38  css::uno::Reference<css::ucb::XSimpleFileAccess3> xFileAccess
39  = css::ucb::SimpleFileAccess::create(comphelper::getProcessComponentContext());
40  if (!xFileAccess.is())
41  return ERROR_GENERAL;
42 
43  css::uno::Reference<css::io::XInputStream> xStream;
44  try
45  {
46  xStream = xFileAccess->openFileRead(aURL);
47  }
48  catch (...)
49  {
50  // don't let any exceptions pass
51  return ERROR_GENERAL;
52  }
53  if (!xStream.is())
54  return ERROR_GENERAL;
55 
56  const sal_Int32 BUF_LEN = 8000;
57  css::uno::Sequence<sal_Int8> buffer(BUF_LEN);
58  OStringBuffer aBuffer(64000);
59 
60  sal_Int32 nRead = 0;
61  while ((nRead = xStream->readBytes(buffer, BUF_LEN)) == BUF_LEN)
62  aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);
63 
64  if (nRead > 0)
65  aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);
66 
67  xStream->closeInput();
68 
69  aResult = OStringToOUString(aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
70  bHasResult = true;
71 
72  // Something happened...
73  if (HasListeners())
74  {
75  Broadcast(ScHint(SfxHintId::ScDataChanged, ScAddress()));
76  pDoc->TrackFormulas(); // must happen immediately
78  }
79 
80  return SUCCESS;
81 }
82 
84 {
85  ScDocument* pStackDoc = pDoc; // member pDoc can't be used after removing the link
86 
87  sfx2::LinkManager* pLinkMgr = pDoc->GetLinkManager();
88  pLinkMgr->Remove(this); // deletes this
89 
90  if (pLinkMgr->GetLinks().empty()) // deleted the last one ?
91  {
92  SfxBindings* pBindings = pStackDoc->GetViewBindings(); // don't use member pDoc!
93  if (pBindings)
94  pBindings->Invalidate(SID_LINKS);
95  }
96 }
97 
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
STRING
URL aURL
void Invalidate(sal_uInt16 nId)
Reference< XInputStream > xStream
SotClipboardFormatId
void Broadcast(const SfxHint &rHint)
const SvBaseLinks & GetLinks() const
void Remove(SvBaseLink const *pLink)
void TrackFormulas(SfxHintId nHintId=SfxHintId::ScDataChanged)
Definition: documen7.cxx:527
std::unique_ptr< char[]> aBuffer
SfxBindings * GetViewBindings()
Definition: documen8.cxx:1188
Reference< XComponentContext > getProcessComponentContext()
bool HasListeners() const
void StartTrackTimer()
Definition: documen2.cxx:312
SC_DLLPUBLIC sfx2::LinkManager * GetLinkManager()
Definition: documen2.cxx:217
SfxLinkUpdateMode