summaryrefslogtreecommitdiffstats
path: root/sc/source/core/tool/webservicelink.cxx
blob: f5e621867d16d9447bfbecf3dcb8b0c00647b723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/processfactory.hxx>
#include <osl/thread.h>
#include <sfx2/linkmgr.hxx>
#include <sfx2/bindings.hxx>

#include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/io/XInputStream.hpp>

#include <webservicelink.hxx>
#include <brdcst.hxx>
#include <document.hxx>
#include <scmatrix.hxx>
#include <patattr.hxx>
#include <rechead.hxx>
#include <rangeseq.hxx>
#include <sc.hrc>
#include <hints.hxx>

ScWebServiceLink::ScWebServiceLink(ScDocument* pD, const OUString& rURL)
    : ::sfx2::SvBaseLink(SfxLinkUpdateMode::ALWAYS, SotClipboardFormatId::STRING)
    , pDoc(pD)
    , aURL(rURL)
    , bHasResult(false)
{
}

ScWebServiceLink::~ScWebServiceLink() {}

sfx2::SvBaseLink::UpdateResult ScWebServiceLink::DataChanged(const OUString&, const css::uno::Any&)
{
    aResult.clear();
    bHasResult = false;

    css::uno::Reference<css::ucb::XSimpleFileAccess3> xFileAccess
        = css::ucb::SimpleFileAccess::create(comphelper::getProcessComponentContext());
    if (!xFileAccess.is())
        return ERROR_GENERAL;

    css::uno::Reference<css::io::XInputStream> xStream;
    try
    {
        xStream = xFileAccess->openFileRead(aURL);
    }
    catch (...)
    {
        // don't let any exceptions pass
        return ERROR_GENERAL;
    }
    if (!xStream.is())
        return ERROR_GENERAL;

    const sal_Int32 BUF_LEN = 8000;
    css::uno::Sequence<sal_Int8> buffer(BUF_LEN);
    OStringBuffer aBuffer(64000);

    sal_Int32 nRead = 0;
    while ((nRead = xStream->readBytes(buffer, BUF_LEN)) == BUF_LEN)
        aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);

    if (nRead > 0)
        aBuffer.append(reinterpret_cast<const char*>(buffer.getConstArray()), nRead);

    xStream->closeInput();

    aResult = OStringToOUString(aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
    bHasResult = true;

    //  Something happened...
    if (HasListeners())
    {
        Broadcast(ScHint(SfxHintId::ScDataChanged, ScAddress()));
        pDoc->TrackFormulas(); // must happen immediately
        pDoc->StartTrackTimer();
    }

    return SUCCESS;
}

void ScWebServiceLink::ListenersGone()
{
    ScDocument* pStackDoc = pDoc; // member pDoc can't be used after removing the link

    sfx2::LinkManager* pLinkMgr = pDoc->GetLinkManager();
    pLinkMgr->Remove(this); // deletes this

    if (pLinkMgr->GetLinks().empty()) // deleted the last one ?
    {
        SfxBindings* pBindings = pStackDoc->GetViewBindings(); // don't use member pDoc!
        if (pBindings)
            pBindings->Invalidate(SID_LINKS);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */