summaryrefslogtreecommitdiff
path: root/include/test/testinteractionhandler.hxx
blob: 1cfdec663d33ae50df7a1a445e0a942ec638335e (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
/* -*- 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/.
 */

#ifndef INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX
#define INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX

#include <sal/config.h>

#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/task/XInteractionHandler2.hpp>
#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/task/DocumentPasswordRequest2.hpp>
#include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>

#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>

class TestInteractionHandler : public cppu::WeakImplHelper<css::lang::XServiceInfo,
                                                           css::lang::XInitialization,
                                                           css::task::XInteractionHandler2>
{
    OUString msPassword;
    bool mbPasswordRequested;

    TestInteractionHandler(const TestInteractionHandler&) = delete;
    TestInteractionHandler& operator=(const TestInteractionHandler&) = delete;

public:
    TestInteractionHandler(const OUString& sPassword)
        : msPassword(sPassword)
        , mbPasswordRequested(false)
    {}

    bool wasPasswordRequested()
    {
        return mbPasswordRequested;
    }

    virtual OUString SAL_CALL getImplementationName() override
    {
        return "com.sun.star.comp.uui.TestInteractionHandler";
    }

    virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override
    {
        return cppu::supportsService(this, rServiceName);
    }

    virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
    {
        return { "com.sun.star.task.InteractionHandler",
        // added to indicate support for configuration.backend.MergeRecoveryRequest
                 "com.sun.star.configuration.backend.InteractionHandler",
        // for backwards compatibility
                 "com.sun.star.uui.InteractionHandler" };
    }

    virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const & /*rArguments*/) override
    {}

    virtual void SAL_CALL handle(css::uno::Reference<css::task::XInteractionRequest> const & rRequest) override
    {
        handleInteractionRequest(rRequest);
    }

    virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest>& rRequest) override
    {
        mbPasswordRequested = false;

        css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> const &rContinuations = rRequest->getContinuations();
        css::uno::Any const aRequest(rRequest->getRequest());

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

        for (auto const & cont : rContinuations)
        {
            css::uno::Reference<css::task::XInteractionApprove> xApprove(cont, css::uno::UNO_QUERY);
            if (xApprove.is())
                xApprove->select();
        }

        return true;
    }

    bool handlePasswordRequest(const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> &rContinuations,
                               const css::uno::Any& rRequest)
    {
        bool bPasswordRequestFound = false;
        bool bIsRequestPasswordToModify = false;
        css::task::PasswordRequestMode mode{};

        css::task::DocumentPasswordRequest2 passwordRequest2;
        if (rRequest >>= passwordRequest2)
        {
            bIsRequestPasswordToModify = passwordRequest2.IsRequestPasswordToModify;
            mode = passwordRequest2.Mode;
            bPasswordRequestFound = true;
        }
        css::task::DocumentMSPasswordRequest2 passwordMSRequest2;
        if (rRequest >>= passwordMSRequest2)
        {
            bIsRequestPasswordToModify = passwordMSRequest2.IsRequestPasswordToModify;
            mode = passwordMSRequest2.Mode;
            bPasswordRequestFound = true;
        }

        if (!bPasswordRequestFound)
        {
            mbPasswordRequested = false;
            return false;
        }
        mbPasswordRequested = true;

        for (auto const & cont : rContinuations)
        {
            if (mode == css::task::PasswordRequestMode_PASSWORD_REENTER)
            {   // cancel re-enter of wrong password, to avoid infinite loop
                css::uno::Reference<css::task::XInteractionAbort> const xAbort(cont, css::uno::UNO_QUERY);
                if (xAbort.is())
                    xAbort->select();
            }
            else if (bIsRequestPasswordToModify)
            {
                css::uno::Reference<css::task::XInteractionPassword2> const xIPW2(cont, css::uno::UNO_QUERY);
                xIPW2->setPasswordToModify(msPassword);
                xIPW2->select();
            }
            else
            {
                css::uno::Reference<css::task::XInteractionPassword> const xIPW(cont, css::uno::UNO_QUERY);
                if (xIPW.is())
                {
                    xIPW->setPassword(msPassword);
                    xIPW->select();
                }
            }
        }
        return true;
    }
};

#endif

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