Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / test / testinteractionhandler.hxx
blob1cfdec663d33ae50df7a1a445e0a942ec638335e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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/XInteractionHandler2.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>
27 class TestInteractionHandler : public cppu::WeakImplHelper<css::lang::XServiceInfo,
28 css::lang::XInitialization,
29 css::task::XInteractionHandler2>
31 OUString msPassword;
32 bool mbPasswordRequested;
34 TestInteractionHandler(const TestInteractionHandler&) = delete;
35 TestInteractionHandler& operator=(const TestInteractionHandler&) = delete;
37 public:
38 TestInteractionHandler(const OUString& sPassword)
39 : msPassword(sPassword)
40 , mbPasswordRequested(false)
43 bool wasPasswordRequested()
45 return mbPasswordRequested;
48 virtual OUString SAL_CALL getImplementationName() override
50 return "com.sun.star.comp.uui.TestInteractionHandler";
53 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override
55 return cppu::supportsService(this, rServiceName);
58 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
60 return { "com.sun.star.task.InteractionHandler",
61 // added to indicate support for configuration.backend.MergeRecoveryRequest
62 "com.sun.star.configuration.backend.InteractionHandler",
63 // for backwards compatibility
64 "com.sun.star.uui.InteractionHandler" };
67 virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const & /*rArguments*/) override
70 virtual void SAL_CALL handle(css::uno::Reference<css::task::XInteractionRequest> const & rRequest) override
72 handleInteractionRequest(rRequest);
75 virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest>& rRequest) override
77 mbPasswordRequested = false;
79 css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> const &rContinuations = rRequest->getContinuations();
80 css::uno::Any const aRequest(rRequest->getRequest());
82 if (handlePasswordRequest(rContinuations, aRequest))
83 return true;
85 for (auto const & cont : rContinuations)
87 css::uno::Reference<css::task::XInteractionApprove> xApprove(cont, css::uno::UNO_QUERY);
88 if (xApprove.is())
89 xApprove->select();
92 return true;
95 bool handlePasswordRequest(const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> &rContinuations,
96 const css::uno::Any& rRequest)
98 bool bPasswordRequestFound = false;
99 bool bIsRequestPasswordToModify = false;
100 css::task::PasswordRequestMode mode{};
102 css::task::DocumentPasswordRequest2 passwordRequest2;
103 if (rRequest >>= passwordRequest2)
105 bIsRequestPasswordToModify = passwordRequest2.IsRequestPasswordToModify;
106 mode = passwordRequest2.Mode;
107 bPasswordRequestFound = true;
109 css::task::DocumentMSPasswordRequest2 passwordMSRequest2;
110 if (rRequest >>= passwordMSRequest2)
112 bIsRequestPasswordToModify = passwordMSRequest2.IsRequestPasswordToModify;
113 mode = passwordMSRequest2.Mode;
114 bPasswordRequestFound = true;
117 if (!bPasswordRequestFound)
119 mbPasswordRequested = false;
120 return false;
122 mbPasswordRequested = true;
124 for (auto const & cont : rContinuations)
126 if (mode == css::task::PasswordRequestMode_PASSWORD_REENTER)
127 { // cancel re-enter of wrong password, to avoid infinite loop
128 css::uno::Reference<css::task::XInteractionAbort> const xAbort(cont, css::uno::UNO_QUERY);
129 if (xAbort.is())
130 xAbort->select();
132 else if (bIsRequestPasswordToModify)
134 css::uno::Reference<css::task::XInteractionPassword2> const xIPW2(cont, css::uno::UNO_QUERY);
135 xIPW2->setPasswordToModify(msPassword);
136 xIPW2->select();
138 else
140 css::uno::Reference<css::task::XInteractionPassword> const xIPW(cont, css::uno::UNO_QUERY);
141 if (xIPW.is())
143 xIPW->setPassword(msPassword);
144 xIPW->select();
148 return true;
152 #endif
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */