summaryrefslogtreecommitdiff
path: root/desktop/source/lib/lokinteractionhandler.cxx
blob: eaf96ab1ad5d81cfea1217470c404765dbf73c2d (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "lokinteractionhandler.hxx"

#include <boost/property_tree/json_parser.hpp>

#include <rtl/ref.hxx>
#include <cppuhelper/supportsservice.hxx>

#include <com/sun/star/task/XInteractionAbort.hpp>
#include <com/sun/star/task/XInteractionApprove.hpp>
#include <com/sun/star/task/XInteractionPassword2.hpp>
#include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkOffLineException.hpp>

#include <com/sun/star/ucb/InteractiveIOException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
#include <com/sun/star/ucb/InteractiveNetworkWriteException.hpp>

#include <../../inc/lib/init.hxx>

#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <sfx2/lokhelper.hxx>
#include <sfx2/viewsh.hxx>
#include <comphelper/lok.hxx>

using namespace com::sun::star;

LOKInteractionHandler::LOKInteractionHandler(
        uno::Reference<uno::XComponentContext> const & /*rxContext*/,
        const OString& rCommand,
        desktop::LibLibreOffice_Impl *const pLOKit,
        desktop::LibLODocument_Impl *const pLOKDocument)
    : m_pLOKit(pLOKit)
    , m_pLOKDocument(pLOKDocument)
    , m_command(rCommand)
    , m_usePassword(false)
{
    assert(m_pLOKit);
}

LOKInteractionHandler::~LOKInteractionHandler()
{
}

OUString SAL_CALL LOKInteractionHandler::getImplementationName() throw (uno::RuntimeException, std::exception)
{
    return OUString("com.sun.star.comp.uui.LOKInteractionHandler");
}

sal_Bool SAL_CALL LOKInteractionHandler::supportsService(OUString const & rServiceName) throw (uno::RuntimeException, std::exception)
{
    return cppu::supportsService(this, rServiceName);
}

uno::Sequence< OUString > SAL_CALL LOKInteractionHandler::getSupportedServiceNames() throw (uno::RuntimeException, std::exception)
{
    uno::Sequence< OUString > aNames(3);
    aNames[0] = "com.sun.star.task.InteractionHandler";
    // added to indicate support for configuration.backend.MergeRecoveryRequest
    aNames[1] = "com.sun.star.configuration.backend.InteractionHandler";
    aNames[2] = "com.sun.star.uui.InteractionHandler";
    // for backwards compatibility
    return aNames;
}

void SAL_CALL LOKInteractionHandler::initialize(uno::Sequence<uno::Any> const & /*rArguments*/) throw (uno::Exception, std::exception)
{
}

void SAL_CALL LOKInteractionHandler::handle(
        uno::Reference<task::XInteractionRequest> const & xRequest)
throw (uno::RuntimeException, std::exception)
{
    // just do the same thing in both cases
    handleInteractionRequest(xRequest);
}

void LOKInteractionHandler::postError(css::task::InteractionClassification classif, const char* kind, ErrCode code, const OUString &message)
{
    const char *classification = "error";
    switch (classif)
    {
        case task::InteractionClassification_ERROR: break;
        case task::InteractionClassification_WARNING: classification = "warning"; break;
        case task::InteractionClassification_INFO: classification = "info"; break;
        case task::InteractionClassification_QUERY: classification = "query"; break;
        default: assert(false); break;
    }

    // create the JSON representation
    boost::property_tree::ptree aTree;
    aTree.put("classification", classification);
    aTree.put("cmd", m_command.getStr());
    aTree.put("kind", kind);
    aTree.put("code", code);
    aTree.put("message", message.toUtf8());

    std::stringstream aStream;
    boost::property_tree::write_json(aStream, aTree);

    std::size_t nView = SfxViewShell::Current() ? SfxLokHelper::getView() : 0;
    if (m_pLOKDocument && m_pLOKDocument->mpCallbackFlushHandlers.size() > nView && m_pLOKDocument->mpCallbackFlushHandlers[nView])
        m_pLOKDocument->mpCallbackFlushHandlers[nView]->queue(LOK_CALLBACK_ERROR, aStream.str().c_str());
    else if (m_pLOKit->mpCallback)
        m_pLOKit->mpCallback(LOK_CALLBACK_ERROR, aStream.str().c_str(), m_pLOKit->mpCallbackData);
}

namespace {

/// Just approve the interaction.
void selectApproved(uno::Sequence<uno::Reference<task::XInteractionContinuation>> const &rContinuations)
{
    for (sal_Int32 i = 0; i < rContinuations.getLength(); ++i)
    {
        uno::Reference<task::XInteractionApprove> xApprove(rContinuations[i], uno::UNO_QUERY);
        if (xApprove.is())
            xApprove->select();
    }
}

}

bool LOKInteractionHandler::handleIOException(const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> &rContinuations, const css::uno::Any& rRequest)
{
    ucb::InteractiveIOException aIoException;
    if (!(rRequest >>= aIoException))
        return false;

    static ErrCode const aErrorCode[ucb::IOErrorCode_WRONG_VERSION + 1] =
    {
        ERRCODE_IO_ABORT,
        ERRCODE_IO_ACCESSDENIED,
        ERRCODE_IO_ALREADYEXISTS,
        ERRCODE_IO_BADCRC,
        ERRCODE_IO_CANTCREATE,
        ERRCODE_IO_CANTREAD,
        ERRCODE_IO_CANTSEEK,
        ERRCODE_IO_CANTTELL,
        ERRCODE_IO_CANTWRITE,
        ERRCODE_IO_CURRENTDIR,
        ERRCODE_IO_DEVICENOTREADY,
        ERRCODE_IO_NOTSAMEDEVICE,
        ERRCODE_IO_GENERAL,
        ERRCODE_IO_INVALIDACCESS,
        ERRCODE_IO_INVALIDCHAR,
        ERRCODE_IO_INVALIDDEVICE,
        ERRCODE_IO_INVALIDLENGTH,
        ERRCODE_IO_INVALIDPARAMETER,
        ERRCODE_IO_ISWILDCARD,
        ERRCODE_IO_LOCKVIOLATION,
        ERRCODE_IO_MISPLACEDCHAR,
        ERRCODE_IO_NAMETOOLONG,
        ERRCODE_IO_NOTEXISTS,
        ERRCODE_IO_NOTEXISTSPATH,
        ERRCODE_IO_NOTSUPPORTED,
        ERRCODE_IO_NOTADIRECTORY,
        ERRCODE_IO_NOTAFILE,
        ERRCODE_IO_OUTOFSPACE,
        ERRCODE_IO_TOOMANYOPENFILES,
        ERRCODE_IO_OUTOFMEMORY,
        ERRCODE_IO_PENDING,
        ERRCODE_IO_RECURSIVE,
        ERRCODE_IO_UNKNOWN,
        ERRCODE_IO_WRITEPROTECTED,
        ERRCODE_IO_WRONGFORMAT,
        ERRCODE_IO_WRONGVERSION,
    };

    postError(aIoException.Classification, "io", aErrorCode[aIoException.Code], "");
    selectApproved(rContinuations);

    return true;
}

bool LOKInteractionHandler::handleNetworkException(const uno::Sequence<uno::Reference<task::XInteractionContinuation>> &rContinuations, const uno::Any &rRequest)
{
    ucb::InteractiveNetworkException aNetworkException;
    if (!(rRequest >>= aNetworkException))
        return false;

    ErrCode nErrorCode;
    OUString aMessage;

    ucb::InteractiveNetworkOffLineException aOffLineException;
    ucb::InteractiveNetworkResolveNameException aResolveNameException;
    ucb::InteractiveNetworkConnectException aConnectException;
    ucb::InteractiveNetworkReadException aReadException;
    ucb::InteractiveNetworkWriteException aWriteException;
    if (rRequest >>= aOffLineException)
    {
        nErrorCode = ERRCODE_INET_OFFLINE;
    }
    else if (rRequest >>= aResolveNameException)
    {
        nErrorCode = ERRCODE_INET_NAME_RESOLVE;
        aMessage = aResolveNameException.Server;
    }
    else if (rRequest >>= aConnectException)
    {
        nErrorCode = ERRCODE_INET_CONNECT;
        aMessage = aConnectException.Server;
    }
    else if (rRequest >>= aReadException)
    {
        nErrorCode = ERRCODE_INET_READ;
        aMessage = aReadException.Diagnostic;
    }
    else if (rRequest >>= aWriteException)
    {
        nErrorCode = ERRCODE_INET_WRITE;
        aMessage = aWriteException.Diagnostic;
    }
    else
    {
        nErrorCode = ERRCODE_INET_GENERAL;
    }

    postError(aNetworkException.Classification, "network", nErrorCode, aMessage);
    selectApproved(rContinuations);

    return true;
}

bool LOKInteractionHandler::handlePasswordRequest(const uno::Sequence<uno::Reference<task::XInteractionContinuation>> &rContinuations, const uno::Any &rRequest)
{
    task::DocumentPasswordRequest2 passwordRequest;
    if (!(rRequest >>= passwordRequest))
        return false;

    if (m_pLOKit->mpCallback &&
        m_pLOKit->hasOptionalFeature((passwordRequest.IsRequestPasswordToModify)
                ? LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY
                : LOK_FEATURE_DOCUMENT_PASSWORD))
    {
        OString const url(passwordRequest.Name.toUtf8());
        m_pLOKit->mpCallback(passwordRequest.IsRequestPasswordToModify
                ? LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY
                : LOK_CALLBACK_DOCUMENT_PASSWORD,
                url.getStr(),
                m_pLOKit->mpCallbackData);

        // block until SetPassword is called
        m_havePassword.wait();
        m_havePassword.reset();
    }

    for (sal_Int32 i = 0; i < rContinuations.getLength(); ++i)
    {
        if (m_usePassword)
        {
            if (passwordRequest.IsRequestPasswordToModify)
            {
                uno::Reference<task::XInteractionPassword2> const xIPW2(rContinuations[i], uno::UNO_QUERY);
                xIPW2->setPasswordToModify(m_Password);
                xIPW2->select();
            }
            else
            {
                uno::Reference<task::XInteractionPassword> const xIPW(rContinuations[i], uno::UNO_QUERY);
                if (xIPW.is())
                {
                    xIPW->setPassword(m_Password);
                    xIPW->select();
                }
            }
        }
        else
        {
            if (passwordRequest.IsRequestPasswordToModify)
            {
                uno::Reference<task::XInteractionPassword2> const xIPW2(rContinuations[i], uno::UNO_QUERY);
                xIPW2->setRecommendReadOnly(true);
                xIPW2->select();
            }
            else
            {
                uno::Reference<task::XInteractionAbort> const xAbort(rContinuations[i], uno::UNO_QUERY);
                if (xAbort.is())
                {
                    xAbort->select();
                }
            }
        }
    }
    return true;
}

sal_Bool SAL_CALL LOKInteractionHandler::handleInteractionRequest(
        const uno::Reference<task::XInteractionRequest>& xRequest) throw (uno::RuntimeException, std::exception)
{
    uno::Sequence<uno::Reference<task::XInteractionContinuation>> const &rContinuations = xRequest->getContinuations();
    uno::Any const request(xRequest->getRequest());

    if (handleIOException(rContinuations, request))
        return true;

    if (handleNetworkException(rContinuations, request))
        return true;

    if (handlePasswordRequest(rContinuations, request))
        return true;

    // TODO: perform more interactions 'for real' like the above
    selectApproved(rContinuations);

    return true;
}

void LOKInteractionHandler::SetPassword(char const*const pPassword)
{
    if (pPassword)
    {
        m_Password = OUString(pPassword, strlen(pPassword), RTL_TEXTENCODING_UTF8);
        m_usePassword = true;
    }
    else
    {
        m_usePassword = false;
    }
    m_havePassword.set();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */