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 "lokinteractionhandler.hxx"
22 #include <boost/property_tree/json_parser.hpp>
24 #include <rtl/ref.hxx>
25 #include <cppuhelper/supportsservice.hxx>
27 #include <com/sun/star/task/XInteractionAbort.hpp>
28 #include <com/sun/star/task/XInteractionApprove.hpp>
29 #include <com/sun/star/task/XInteractionPassword2.hpp>
30 #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp>
31 #include <com/sun/star/ucb/InteractiveNetworkOffLineException.hpp>
33 #include <com/sun/star/ucb/InteractiveIOException.hpp>
34 #include <com/sun/star/ucb/InteractiveNetworkReadException.hpp>
35 #include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp>
36 #include <com/sun/star/ucb/InteractiveNetworkWriteException.hpp>
38 #include <com/sun/star/task/DocumentPasswordRequest2.hpp>
39 #include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>
41 #include "../../inc/lib/init.hxx"
43 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
44 #include <sfx2/lokhelper.hxx>
45 #include <sfx2/viewsh.hxx>
47 using namespace com::sun::star
;
49 LOKInteractionHandler::LOKInteractionHandler(
50 const OString
& rCommand
,
51 desktop::LibLibreOffice_Impl
*const pLOKit
,
52 desktop::LibLODocument_Impl
*const pLOKDocument
)
54 , m_pLOKDocument(pLOKDocument
)
56 , m_usePassword(false)
61 LOKInteractionHandler::~LOKInteractionHandler()
65 OUString SAL_CALL
LOKInteractionHandler::getImplementationName()
67 return "com.sun.star.comp.uui.LOKInteractionHandler";
70 sal_Bool SAL_CALL
LOKInteractionHandler::supportsService(OUString
const & rServiceName
)
72 return cppu::supportsService(this, rServiceName
);
75 uno::Sequence
< OUString
> SAL_CALL
LOKInteractionHandler::getSupportedServiceNames()
77 return { "com.sun.star.task.InteractionHandler",
78 // added to indicate support for configuration.backend.MergeRecoveryRequest
79 "com.sun.star.configuration.backend.InteractionHandler",
80 // for backwards compatibility
81 "com.sun.star.uui.InteractionHandler" };
84 void SAL_CALL
LOKInteractionHandler::initialize(uno::Sequence
<uno::Any
> const & /*rArguments*/)
88 void SAL_CALL
LOKInteractionHandler::handle(
89 uno::Reference
<task::XInteractionRequest
> const & xRequest
)
91 // just do the same thing in both cases
92 handleInteractionRequest(xRequest
);
95 void LOKInteractionHandler::postError(css::task::InteractionClassification classif
, const char* kind
, ErrCode code
, const OUString
&message
)
97 const char *classification
= "error";
100 case task::InteractionClassification_ERROR
: break;
101 case task::InteractionClassification_WARNING
: classification
= "warning"; break;
102 case task::InteractionClassification_INFO
: classification
= "info"; break;
103 case task::InteractionClassification_QUERY
: classification
= "query"; break;
104 default: assert(false); break;
107 // create the JSON representation
108 boost::property_tree::ptree aTree
;
109 aTree
.put("classification", classification
);
110 aTree
.put("cmd", m_command
.getStr());
111 aTree
.put("kind", kind
);
112 aTree
.put("code", code
);
113 aTree
.put("message", message
.toUtf8());
115 std::stringstream aStream
;
116 boost::property_tree::write_json(aStream
, aTree
);
118 std::size_t nView
= SfxViewShell::Current() ? SfxLokHelper::getView() : 0;
119 if (m_pLOKDocument
&& m_pLOKDocument
->mpCallbackFlushHandlers
.count(nView
))
120 m_pLOKDocument
->mpCallbackFlushHandlers
[nView
]->queue(LOK_CALLBACK_ERROR
, aStream
.str().c_str());
121 else if (m_pLOKit
->mpCallback
)
122 m_pLOKit
->mpCallback(LOK_CALLBACK_ERROR
, aStream
.str().c_str(), m_pLOKit
->mpCallbackData
);
127 /// Just approve the interaction.
128 void selectApproved(uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> const &rContinuations
)
130 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
132 uno::Reference
<task::XInteractionApprove
> xApprove(rContinuations
[i
], uno::UNO_QUERY
);
140 bool LOKInteractionHandler::handleIOException(const css::uno::Sequence
<css::uno::Reference
<css::task::XInteractionContinuation
>> &rContinuations
, const css::uno::Any
& rRequest
)
142 ucb::InteractiveIOException aIoException
;
143 if (!(rRequest
>>= aIoException
))
146 static ErrCode
const aErrorCode
[int(ucb::IOErrorCode_WRONG_VERSION
) + 1] =
149 ERRCODE_IO_ACCESSDENIED
,
150 ERRCODE_IO_ALREADYEXISTS
,
152 ERRCODE_IO_CANTCREATE
,
156 ERRCODE_IO_CANTWRITE
,
157 ERRCODE_IO_CURRENTDIR
,
158 ERRCODE_IO_DEVICENOTREADY
,
159 ERRCODE_IO_NOTSAMEDEVICE
,
161 ERRCODE_IO_INVALIDACCESS
,
162 ERRCODE_IO_INVALIDCHAR
,
163 ERRCODE_IO_INVALIDDEVICE
,
164 ERRCODE_IO_INVALIDLENGTH
,
165 ERRCODE_IO_INVALIDPARAMETER
,
166 ERRCODE_IO_ISWILDCARD
,
167 ERRCODE_IO_LOCKVIOLATION
,
168 ERRCODE_IO_MISPLACEDCHAR
,
169 ERRCODE_IO_NAMETOOLONG
,
170 ERRCODE_IO_NOTEXISTS
,
171 ERRCODE_IO_NOTEXISTSPATH
,
172 ERRCODE_IO_NOTSUPPORTED
,
173 ERRCODE_IO_NOTADIRECTORY
,
175 ERRCODE_IO_OUTOFSPACE
,
176 ERRCODE_IO_TOOMANYOPENFILES
,
177 ERRCODE_IO_OUTOFMEMORY
,
179 ERRCODE_IO_RECURSIVE
,
181 ERRCODE_IO_WRITEPROTECTED
,
182 ERRCODE_IO_WRONGFORMAT
,
183 ERRCODE_IO_WRONGVERSION
,
186 postError(aIoException
.Classification
, "io", aErrorCode
[static_cast<int>(aIoException
.Code
)], "");
187 selectApproved(rContinuations
);
192 bool LOKInteractionHandler::handleNetworkException(const uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> &rContinuations
, const uno::Any
&rRequest
)
194 ucb::InteractiveNetworkException aNetworkException
;
195 if (!(rRequest
>>= aNetworkException
))
201 ucb::InteractiveNetworkOffLineException aOffLineException
;
202 ucb::InteractiveNetworkResolveNameException aResolveNameException
;
203 ucb::InteractiveNetworkConnectException aConnectException
;
204 ucb::InteractiveNetworkReadException aReadException
;
205 ucb::InteractiveNetworkWriteException aWriteException
;
206 if (rRequest
>>= aOffLineException
)
208 nErrorCode
= ERRCODE_INET_OFFLINE
;
210 else if (rRequest
>>= aResolveNameException
)
212 nErrorCode
= ERRCODE_INET_NAME_RESOLVE
;
213 aMessage
= aResolveNameException
.Server
;
215 else if (rRequest
>>= aConnectException
)
217 nErrorCode
= ERRCODE_INET_CONNECT
;
218 aMessage
= aConnectException
.Server
;
220 else if (rRequest
>>= aReadException
)
222 nErrorCode
= ERRCODE_INET_READ
;
223 aMessage
= aReadException
.Diagnostic
;
225 else if (rRequest
>>= aWriteException
)
227 nErrorCode
= ERRCODE_INET_WRITE
;
228 aMessage
= aWriteException
.Diagnostic
;
232 nErrorCode
= ERRCODE_INET_GENERAL
;
235 postError(aNetworkException
.Classification
, "network", nErrorCode
, aMessage
);
236 selectApproved(rContinuations
);
241 bool LOKInteractionHandler::handlePasswordRequest(const uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> &rContinuations
, const uno::Any
&rRequest
)
243 bool bPasswordRequestFound
= false;
244 bool bIsRequestPasswordToModify
= false;
248 task::DocumentPasswordRequest passwordRequest
;
249 if (rRequest
>>= passwordRequest
)
251 bIsRequestPasswordToModify
= false;
252 sUrl
= passwordRequest
.Name
.toUtf8();
253 bPasswordRequestFound
= true;
256 task::DocumentPasswordRequest2 passwordRequest2
;
257 if (rRequest
>>= passwordRequest2
)
259 bIsRequestPasswordToModify
= passwordRequest2
.IsRequestPasswordToModify
;
260 sUrl
= passwordRequest2
.Name
.toUtf8();
261 bPasswordRequestFound
= true;
264 task::DocumentMSPasswordRequest2 passwordMSRequest
;
265 if (rRequest
>>= passwordMSRequest
)
267 bIsRequestPasswordToModify
= passwordMSRequest
.IsRequestPasswordToModify
;
268 sUrl
= passwordMSRequest
.Name
.toUtf8();
269 bPasswordRequestFound
= true;
272 if (!bPasswordRequestFound
)
275 if (m_pLOKit
->mpCallback
&&
276 m_pLOKit
->hasOptionalFeature(bIsRequestPasswordToModify
? LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY
277 : LOK_FEATURE_DOCUMENT_PASSWORD
))
279 m_pLOKit
->mpCallback(bIsRequestPasswordToModify
? LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY
280 : LOK_CALLBACK_DOCUMENT_PASSWORD
,
282 m_pLOKit
->mpCallbackData
);
284 // block until SetPassword is called
285 m_havePassword
.wait();
286 m_havePassword
.reset();
289 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
293 if (bIsRequestPasswordToModify
)
295 uno::Reference
<task::XInteractionPassword2
> const xIPW2(rContinuations
[i
], uno::UNO_QUERY
);
296 xIPW2
->setPasswordToModify(m_Password
);
301 uno::Reference
<task::XInteractionPassword
> const xIPW(rContinuations
[i
], uno::UNO_QUERY
);
304 xIPW
->setPassword(m_Password
);
311 if (bIsRequestPasswordToModify
)
313 uno::Reference
<task::XInteractionPassword2
> const xIPW2(rContinuations
[i
], uno::UNO_QUERY
);
314 xIPW2
->setRecommendReadOnly(true);
319 uno::Reference
<task::XInteractionAbort
> const xAbort(rContinuations
[i
], uno::UNO_QUERY
);
330 sal_Bool SAL_CALL
LOKInteractionHandler::handleInteractionRequest(
331 const uno::Reference
<task::XInteractionRequest
>& xRequest
)
333 uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> const &rContinuations
= xRequest
->getContinuations();
334 uno::Any
const request(xRequest
->getRequest());
336 if (handleIOException(rContinuations
, request
))
339 if (handleNetworkException(rContinuations
, request
))
342 if (handlePasswordRequest(rContinuations
, request
))
345 // TODO: perform more interactions 'for real' like the above
346 selectApproved(rContinuations
);
351 void LOKInteractionHandler::SetPassword(char const*const pPassword
)
355 m_Password
= OUString(pPassword
, strlen(pPassword
), RTL_TEXTENCODING_UTF8
);
356 m_usePassword
= true;
360 m_usePassword
= false;
362 m_havePassword
.set();
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */