LibreOffice Module test (master)  1
testinteractionhandler.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #ifndef INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX
11 #define INCLUDED_TEST_TESTINTERACTIONHANDLER_HXX
12 
13 #include <sal/config.h>
14 
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>
23 
24 #include <cppuhelper/implbase.hxx>
26 
27 class TestInteractionHandler : public cppu::WeakImplHelper<css::lang::XServiceInfo,
28  css::lang::XInitialization,
29  css::task::XInteractionHandler2>
30 {
31  OUString msPassword;
33 
36 
37 public:
38  TestInteractionHandler(const OUString& sPassword)
39  : msPassword(sPassword)
40  , mbPasswordRequested(false)
41  {}
42 
44  {
45  return mbPasswordRequested;
46  }
47 
48  virtual OUString SAL_CALL getImplementationName() override
49  {
50  return "com.sun.star.comp.uui.TestInteractionHandler";
51  }
52 
53  virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override
54  {
55  return cppu::supportsService(this, rServiceName);
56  }
57 
58  virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
59  {
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" };
65  }
66 
67  virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const & /*rArguments*/) override
68  {}
69 
70  virtual void SAL_CALL handle(css::uno::Reference<css::task::XInteractionRequest> const & rRequest) override
71  {
72  handleInteractionRequest(rRequest);
73  }
74 
75  virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest>& rRequest) override
76  {
77  mbPasswordRequested = false;
78 
79  css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> const &rContinuations = rRequest->getContinuations();
80  css::uno::Any const aRequest(rRequest->getRequest());
81 
82  if (handlePasswordRequest(rContinuations, aRequest))
83  return true;
84 
85  for (auto const & cont : rContinuations)
86  {
87  css::uno::Reference<css::task::XInteractionApprove> xApprove(cont, css::uno::UNO_QUERY);
88  if (xApprove.is())
89  xApprove->select();
90  }
91 
92  return true;
93  }
94 
95  bool handlePasswordRequest(const css::uno::Sequence<css::uno::Reference<css::task::XInteractionContinuation>> &rContinuations,
96  const css::uno::Any& rRequest)
97  {
98  bool bPasswordRequestFound = false;
99  bool bIsRequestPasswordToModify = false;
100  css::task::PasswordRequestMode mode{};
101 
102  css::task::DocumentPasswordRequest2 passwordRequest2;
103  if (rRequest >>= passwordRequest2)
104  {
105  bIsRequestPasswordToModify = passwordRequest2.IsRequestPasswordToModify;
106  mode = passwordRequest2.Mode;
107  bPasswordRequestFound = true;
108  }
109  css::task::DocumentMSPasswordRequest2 passwordMSRequest2;
110  if (rRequest >>= passwordMSRequest2)
111  {
112  bIsRequestPasswordToModify = passwordMSRequest2.IsRequestPasswordToModify;
113  mode = passwordMSRequest2.Mode;
114  bPasswordRequestFound = true;
115  }
116 
117  if (!bPasswordRequestFound)
118  {
119  mbPasswordRequested = false;
120  return false;
121  }
122  mbPasswordRequested = true;
123 
124  for (auto const & cont : rContinuations)
125  {
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();
131  }
132  else if (bIsRequestPasswordToModify)
133  {
134  css::uno::Reference<css::task::XInteractionPassword2> const xIPW2(cont, css::uno::UNO_QUERY);
135  xIPW2->setPasswordToModify(msPassword);
136  xIPW2->select();
137  }
138  else
139  {
140  css::uno::Reference<css::task::XInteractionPassword> const xIPW(cont, css::uno::UNO_QUERY);
141  if (xIPW.is())
142  {
143  xIPW->setPassword(msPassword);
144  xIPW->select();
145  }
146  }
147  }
148  return true;
149  }
150 };
151 
152 #endif
153 
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TestInteractionHandler(const TestInteractionHandler &)=delete
TestInteractionHandler(const OUString &sPassword)
virtual sal_Bool SAL_CALL handleInteractionRequest(const css::uno::Reference< css::task::XInteractionRequest > &rRequest) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual OUString SAL_CALL getImplementationName() override
unsigned char sal_Bool
ConversionMode mode
virtual sal_Bool SAL_CALL supportsService(OUString const &rServiceName) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
TestInteractionHandler & operator=(const TestInteractionHandler &)=delete
virtual void SAL_CALL handle(css::uno::Reference< css::task::XInteractionRequest > const &rRequest) override
virtual void SAL_CALL initialize(css::uno::Sequence< css::uno::Any > const &) override
bool handlePasswordRequest(const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation >> &rContinuations, const css::uno::Any &rRequest)