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>
46 #include <comphelper/lok.hxx>
48 using namespace com::sun::star
;
50 LOKInteractionHandler::LOKInteractionHandler(
51 const OString
& rCommand
,
52 desktop::LibLibreOffice_Impl
*const pLOKit
,
53 desktop::LibLODocument_Impl
*const pLOKDocument
)
55 , m_pLOKDocument(pLOKDocument
)
57 , m_usePassword(false)
62 LOKInteractionHandler::~LOKInteractionHandler()
66 OUString SAL_CALL
LOKInteractionHandler::getImplementationName()
68 return OUString("com.sun.star.comp.uui.LOKInteractionHandler");
71 sal_Bool SAL_CALL
LOKInteractionHandler::supportsService(OUString
const & rServiceName
)
73 return cppu::supportsService(this, rServiceName
);
76 uno::Sequence
< OUString
> SAL_CALL
LOKInteractionHandler::getSupportedServiceNames()
78 uno::Sequence
< OUString
> aNames(3);
79 aNames
[0] = "com.sun.star.task.InteractionHandler";
80 // added to indicate support for configuration.backend.MergeRecoveryRequest
81 aNames
[1] = "com.sun.star.configuration.backend.InteractionHandler";
82 aNames
[2] = "com.sun.star.uui.InteractionHandler";
83 // for backwards compatibility
87 void SAL_CALL
LOKInteractionHandler::initialize(uno::Sequence
<uno::Any
> const & /*rArguments*/)
91 void SAL_CALL
LOKInteractionHandler::handle(
92 uno::Reference
<task::XInteractionRequest
> const & xRequest
)
94 // just do the same thing in both cases
95 handleInteractionRequest(xRequest
);
98 void LOKInteractionHandler::postError(css::task::InteractionClassification classif
, const char* kind
, ErrCode code
, const OUString
&message
)
100 const char *classification
= "error";
103 case task::InteractionClassification_ERROR
: break;
104 case task::InteractionClassification_WARNING
: classification
= "warning"; break;
105 case task::InteractionClassification_INFO
: classification
= "info"; break;
106 case task::InteractionClassification_QUERY
: classification
= "query"; break;
107 default: assert(false); break;
110 // create the JSON representation
111 boost::property_tree::ptree aTree
;
112 aTree
.put("classification", classification
);
113 aTree
.put("cmd", m_command
.getStr());
114 aTree
.put("kind", kind
);
115 aTree
.put("code", code
);
116 aTree
.put("message", message
.toUtf8());
118 std::stringstream aStream
;
119 boost::property_tree::write_json(aStream
, aTree
);
121 std::size_t nView
= SfxViewShell::Current() ? SfxLokHelper::getView() : 0;
122 if (m_pLOKDocument
&& m_pLOKDocument
->mpCallbackFlushHandlers
.size() > nView
&& m_pLOKDocument
->mpCallbackFlushHandlers
[nView
])
123 m_pLOKDocument
->mpCallbackFlushHandlers
[nView
]->queue(LOK_CALLBACK_ERROR
, aStream
.str().c_str());
124 else if (m_pLOKit
->mpCallback
)
125 m_pLOKit
->mpCallback(LOK_CALLBACK_ERROR
, aStream
.str().c_str(), m_pLOKit
->mpCallbackData
);
130 /// Just approve the interaction.
131 void selectApproved(uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> const &rContinuations
)
133 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
135 uno::Reference
<task::XInteractionApprove
> xApprove(rContinuations
[i
], uno::UNO_QUERY
);
143 bool LOKInteractionHandler::handleIOException(const css::uno::Sequence
<css::uno::Reference
<css::task::XInteractionContinuation
>> &rContinuations
, const css::uno::Any
& rRequest
)
145 ucb::InteractiveIOException aIoException
;
146 if (!(rRequest
>>= aIoException
))
149 static ErrCode
const aErrorCode
[(int)ucb::IOErrorCode_WRONG_VERSION
+ 1] =
152 ERRCODE_IO_ACCESSDENIED
,
153 ERRCODE_IO_ALREADYEXISTS
,
155 ERRCODE_IO_CANTCREATE
,
159 ERRCODE_IO_CANTWRITE
,
160 ERRCODE_IO_CURRENTDIR
,
161 ERRCODE_IO_DEVICENOTREADY
,
162 ERRCODE_IO_NOTSAMEDEVICE
,
164 ERRCODE_IO_INVALIDACCESS
,
165 ERRCODE_IO_INVALIDCHAR
,
166 ERRCODE_IO_INVALIDDEVICE
,
167 ERRCODE_IO_INVALIDLENGTH
,
168 ERRCODE_IO_INVALIDPARAMETER
,
169 ERRCODE_IO_ISWILDCARD
,
170 ERRCODE_IO_LOCKVIOLATION
,
171 ERRCODE_IO_MISPLACEDCHAR
,
172 ERRCODE_IO_NAMETOOLONG
,
173 ERRCODE_IO_NOTEXISTS
,
174 ERRCODE_IO_NOTEXISTSPATH
,
175 ERRCODE_IO_NOTSUPPORTED
,
176 ERRCODE_IO_NOTADIRECTORY
,
178 ERRCODE_IO_OUTOFSPACE
,
179 ERRCODE_IO_TOOMANYOPENFILES
,
180 ERRCODE_IO_OUTOFMEMORY
,
182 ERRCODE_IO_RECURSIVE
,
184 ERRCODE_IO_WRITEPROTECTED
,
185 ERRCODE_IO_WRONGFORMAT
,
186 ERRCODE_IO_WRONGVERSION
,
189 postError(aIoException
.Classification
, "io", aErrorCode
[(int)aIoException
.Code
], "");
190 selectApproved(rContinuations
);
195 bool LOKInteractionHandler::handleNetworkException(const uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> &rContinuations
, const uno::Any
&rRequest
)
197 ucb::InteractiveNetworkException aNetworkException
;
198 if (!(rRequest
>>= aNetworkException
))
204 ucb::InteractiveNetworkOffLineException aOffLineException
;
205 ucb::InteractiveNetworkResolveNameException aResolveNameException
;
206 ucb::InteractiveNetworkConnectException aConnectException
;
207 ucb::InteractiveNetworkReadException aReadException
;
208 ucb::InteractiveNetworkWriteException aWriteException
;
209 if (rRequest
>>= aOffLineException
)
211 nErrorCode
= ERRCODE_INET_OFFLINE
;
213 else if (rRequest
>>= aResolveNameException
)
215 nErrorCode
= ERRCODE_INET_NAME_RESOLVE
;
216 aMessage
= aResolveNameException
.Server
;
218 else if (rRequest
>>= aConnectException
)
220 nErrorCode
= ERRCODE_INET_CONNECT
;
221 aMessage
= aConnectException
.Server
;
223 else if (rRequest
>>= aReadException
)
225 nErrorCode
= ERRCODE_INET_READ
;
226 aMessage
= aReadException
.Diagnostic
;
228 else if (rRequest
>>= aWriteException
)
230 nErrorCode
= ERRCODE_INET_WRITE
;
231 aMessage
= aWriteException
.Diagnostic
;
235 nErrorCode
= ERRCODE_INET_GENERAL
;
238 postError(aNetworkException
.Classification
, "network", nErrorCode
, aMessage
);
239 selectApproved(rContinuations
);
244 bool LOKInteractionHandler::handlePasswordRequest(const uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> &rContinuations
, const uno::Any
&rRequest
)
246 bool bPasswordRequestFound
= false;
247 bool bIsRequestPasswordToModify
= false;
251 task::DocumentPasswordRequest passwordRequest
;
252 if (rRequest
>>= passwordRequest
)
254 bIsRequestPasswordToModify
= false;
255 sUrl
= passwordRequest
.Name
.toUtf8();
256 bPasswordRequestFound
= true;
259 task::DocumentPasswordRequest2 passwordRequest2
;
260 if (rRequest
>>= passwordRequest2
)
262 bIsRequestPasswordToModify
= passwordRequest2
.IsRequestPasswordToModify
;
263 sUrl
= passwordRequest2
.Name
.toUtf8();
264 bPasswordRequestFound
= true;
267 task::DocumentMSPasswordRequest2 passwordMSRequest
;
268 if (rRequest
>>= passwordMSRequest
)
270 bIsRequestPasswordToModify
= passwordMSRequest
.IsRequestPasswordToModify
;
271 sUrl
= passwordMSRequest
.Name
.toUtf8();
272 bPasswordRequestFound
= true;
275 if (!bPasswordRequestFound
)
278 if (m_pLOKit
->mpCallback
&&
279 m_pLOKit
->hasOptionalFeature(bIsRequestPasswordToModify
? LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY
280 : LOK_FEATURE_DOCUMENT_PASSWORD
))
282 m_pLOKit
->mpCallback(bIsRequestPasswordToModify
? LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY
283 : LOK_CALLBACK_DOCUMENT_PASSWORD
,
285 m_pLOKit
->mpCallbackData
);
287 // block until SetPassword is called
288 m_havePassword
.wait();
289 m_havePassword
.reset();
292 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
296 if (bIsRequestPasswordToModify
)
298 uno::Reference
<task::XInteractionPassword2
> const xIPW2(rContinuations
[i
], uno::UNO_QUERY
);
299 xIPW2
->setPasswordToModify(m_Password
);
304 uno::Reference
<task::XInteractionPassword
> const xIPW(rContinuations
[i
], uno::UNO_QUERY
);
307 xIPW
->setPassword(m_Password
);
314 if (bIsRequestPasswordToModify
)
316 uno::Reference
<task::XInteractionPassword2
> const xIPW2(rContinuations
[i
], uno::UNO_QUERY
);
317 xIPW2
->setRecommendReadOnly(true);
322 uno::Reference
<task::XInteractionAbort
> const xAbort(rContinuations
[i
], uno::UNO_QUERY
);
333 sal_Bool SAL_CALL
LOKInteractionHandler::handleInteractionRequest(
334 const uno::Reference
<task::XInteractionRequest
>& xRequest
)
336 uno::Sequence
<uno::Reference
<task::XInteractionContinuation
>> const &rContinuations
= xRequest
->getContinuations();
337 uno::Any
const request(xRequest
->getRequest());
339 if (handleIOException(rContinuations
, request
))
342 if (handleNetworkException(rContinuations
, request
))
345 if (handlePasswordRequest(rContinuations
, request
))
348 // TODO: perform more interactions 'for real' like the above
349 selectApproved(rContinuations
);
354 void LOKInteractionHandler::SetPassword(char const*const pPassword
)
358 m_Password
= OUString(pPassword
, strlen(pPassword
), RTL_TEXTENCODING_UTF8
);
359 m_usePassword
= true;
363 m_usePassword
= false;
365 m_havePassword
.set();
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */