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/.
10 #ifndef INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX
11 #define INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX
13 #include <sal/config.h>
15 #include <com/sun/star/lang/XServiceInfo.hpp>
16 #include <com/sun/star/lang/XInitialization.hpp>
17 #include <com/sun/star/task/InteractionHandler.hpp>
18 #include <com/sun/star/task/XInteractionAbort.hpp>
19 #include <com/sun/star/task/XInteractionApprove.hpp>
20 #include <com/sun/star/task/XInteractionPassword2.hpp>
21 #include <com/sun/star/task/DocumentPasswordRequest2.hpp>
22 #include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>
24 #include <cppuhelper/implbase.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <comphelper/sequence.hxx>
28 class TestInteractionHandler
: public cppu::WeakImplHelper
<css::lang::XServiceInfo
,
29 css::lang::XInitialization
,
30 css::task::XInteractionHandler2
>
33 bool mbPasswordRequested
;
35 TestInteractionHandler(const TestInteractionHandler
&) = delete;
36 TestInteractionHandler
& operator=(const TestInteractionHandler
&) = delete;
39 TestInteractionHandler(const OUString
& sPassword
)
40 : msPassword(sPassword
)
43 virtual ~TestInteractionHandler() override
46 bool wasPasswordRequested()
48 return mbPasswordRequested
;
51 virtual OUString SAL_CALL
getImplementationName()
52 throw (css::uno::RuntimeException
, std::exception
) override
54 return OUString("com.sun.star.comp.uui.TestInteractionHandler");
57 virtual sal_Bool SAL_CALL
supportsService(OUString
const & rServiceName
)
58 throw (css::uno::RuntimeException
, std::exception
) override
60 return cppu::supportsService(this, rServiceName
);
63 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
64 throw (css::uno::RuntimeException
, std::exception
) override
66 css::uno::Sequence
<OUString
> aNames(3);
67 aNames
[0] = "com.sun.star.task.InteractionHandler";
68 // added to indicate support for configuration.backend.MergeRecoveryRequest
69 aNames
[1] = "com.sun.star.configuration.backend.InteractionHandler";
70 aNames
[2] = "com.sun.star.uui.InteractionHandler";
71 // for backwards compatibility
75 virtual void SAL_CALL
initialize(css::uno::Sequence
<css::uno::Any
> const & /*rArguments*/)
76 throw (css::uno::Exception
, std::exception
) override
79 virtual void SAL_CALL
handle(css::uno::Reference
<css::task::XInteractionRequest
> const & rRequest
)
80 throw (css::uno::RuntimeException
, std::exception
) override
82 handleInteractionRequest(rRequest
);
85 virtual sal_Bool SAL_CALL
handleInteractionRequest(const css::uno::Reference
<css::task::XInteractionRequest
>& rRequest
)
86 throw (css::uno::RuntimeException
, std::exception
) override
88 mbPasswordRequested
= false;
90 css::uno::Sequence
<css::uno::Reference
<css::task::XInteractionContinuation
>> const &rContinuations
= rRequest
->getContinuations();
91 css::uno::Any
const aRequest(rRequest
->getRequest());
93 if (handlePasswordRequest(rContinuations
, aRequest
))
96 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
98 css::uno::Reference
<css::task::XInteractionApprove
> xApprove(rContinuations
[i
], css::uno::UNO_QUERY
);
106 bool handlePasswordRequest(const css::uno::Sequence
<css::uno::Reference
<css::task::XInteractionContinuation
>> &rContinuations
,
107 const css::uno::Any
& rRequest
)
109 bool bPasswordRequestFound
= false;
110 bool bIsRequestPasswordToModify
= false;
114 css::task::DocumentPasswordRequest2 passwordRequest2
;
115 if (rRequest
>>= passwordRequest2
)
117 bIsRequestPasswordToModify
= passwordRequest2
.IsRequestPasswordToModify
;
118 sUrl
= passwordRequest2
.Name
.toUtf8();
119 bPasswordRequestFound
= true;
121 css::task::DocumentMSPasswordRequest2 passwordMSRequest2
;
122 if (rRequest
>>= passwordMSRequest2
)
124 bIsRequestPasswordToModify
= passwordMSRequest2
.IsRequestPasswordToModify
;
125 sUrl
= passwordMSRequest2
.Name
.toUtf8();
126 bPasswordRequestFound
= true;
129 if (!bPasswordRequestFound
)
131 mbPasswordRequested
= false;
134 mbPasswordRequested
= true;
136 for (sal_Int32 i
= 0; i
< rContinuations
.getLength(); ++i
)
138 if (bIsRequestPasswordToModify
)
140 css::uno::Reference
<css::task::XInteractionPassword2
> const xIPW2(rContinuations
[i
], css::uno::UNO_QUERY
);
141 xIPW2
->setPasswordToModify(msPassword
);
146 css::uno::Reference
<css::task::XInteractionPassword
> const xIPW(rContinuations
[i
], css::uno::UNO_QUERY
);
149 xIPW
->setPassword(msPassword
);
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */