LibreOffice Module sdext (master)  1
pwdinteract.cxx
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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <cassert>
23 #include <mutex>
24 
25 #include <pdfihelper.hxx>
26 
27 #include <com/sun/star/task/ErrorCodeRequest.hpp>
28 #include <com/sun/star/task/XInteractionHandler.hpp>
29 #include <com/sun/star/task/XInteractionRequest.hpp>
30 #include <com/sun/star/task/XInteractionPassword.hpp>
31 #include <com/sun/star/task/DocumentPasswordRequest.hpp>
32 
33 #include <cppuhelper/implbase.hxx>
34 #include <osl/mutex.hxx>
35 #include <rtl/ref.hxx>
36 #include <vcl/errcode.hxx>
37 
38 using namespace com::sun::star;
39 
40 namespace
41 {
42 
43 class PDFPasswordRequest:
44  public cppu::WeakImplHelper<
45  task::XInteractionRequest, task::XInteractionPassword >
46 {
47 private:
48  mutable std::mutex m_aMutex;
50  OUString m_aPassword;
51  bool m_bSelected;
52 
53 public:
54  explicit PDFPasswordRequest(bool bFirstTry, const OUString& rName);
55  PDFPasswordRequest(const PDFPasswordRequest&) = delete;
56  PDFPasswordRequest& operator=(const PDFPasswordRequest&) = delete;
57 
58  // XInteractionRequest
59  virtual uno::Any SAL_CALL getRequest( ) override;
60  virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations( ) override;
61 
62  // XInteractionPassword
63  virtual void SAL_CALL setPassword( const OUString& rPwd ) override;
64  virtual OUString SAL_CALL getPassword() override;
65 
66  // XInteractionContinuation
67  virtual void SAL_CALL select() override;
68 
69  bool isSelected() const { std::scoped_lock const guard( m_aMutex ); return m_bSelected; }
70 
71 private:
72  virtual ~PDFPasswordRequest() override {}
73 };
74 
75 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const OUString& rName ) :
76  m_aRequest(
77  uno::makeAny(
78  task::DocumentPasswordRequest(
79  OUString(), uno::Reference< uno::XInterface >(),
80  task::InteractionClassification_QUERY,
81  (bFirstTry
82  ? task::PasswordRequestMode_PASSWORD_ENTER
83  : task::PasswordRequestMode_PASSWORD_REENTER),
84  rName))),
85  m_bSelected(false)
86 {}
87 
88 uno::Any PDFPasswordRequest::getRequest()
89 {
90  return m_aRequest;
91 }
92 
93 uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations()
94 {
95  uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
96  aRet[0] = this;
97  return aRet;
98 }
99 
100 void PDFPasswordRequest::setPassword( const OUString& rPwd )
101 {
102  std::scoped_lock const guard( m_aMutex );
103 
104  m_aPassword = rPwd;
105 }
106 
108 {
109  std::scoped_lock const guard( m_aMutex );
110 
111  return m_aPassword;
112 }
113 
114 void PDFPasswordRequest::select()
115 {
116  std::scoped_lock const guard( m_aMutex );
117 
118  m_bSelected = true;
119 }
120 
121 class UnsupportedEncryptionFormatRequest:
122  public cppu::WeakImplHelper< task::XInteractionRequest >
123 {
124 public:
125  UnsupportedEncryptionFormatRequest() {}
126  UnsupportedEncryptionFormatRequest(const UnsupportedEncryptionFormatRequest&) = delete;
127  UnsupportedEncryptionFormatRequest& operator=(const UnsupportedEncryptionFormatRequest&) = delete;
128 
129 private:
130  virtual ~UnsupportedEncryptionFormatRequest() override {}
131 
132  virtual uno::Any SAL_CALL getRequest() override {
133  return uno::makeAny(
134  task::ErrorCodeRequest(
135  OUString(), uno::Reference< uno::XInterface >(),
136  sal_uInt32(ERRCODE_IO_WRONGVERSION)));
137  //TODO: should be something more informative than crudely reused
138  // ERRCODE_IO_WRONGVERSION
139  }
140 
141  virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > >
142  SAL_CALL getContinuations() override {
143  return
144  uno::Sequence< uno::Reference< task::XInteractionContinuation > >();
145  }
146 };
147 
148 } // namespace
149 
150 namespace pdfi
151 {
152 
153 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
154  OUString& rOutPwd,
155  bool bFirstTry,
156  const OUString& rDocName
157  )
158 {
159  bool bSuccess = false;
160 
162  new PDFPasswordRequest( bFirstTry, rDocName ) );
163  try
164  {
165  xHandler->handle( xReq );
166  }
167  catch( uno::Exception& )
168  {
169  }
170 
171  if( xReq->isSelected() )
172  {
173  bSuccess = true;
174  rOutPwd = xReq->getPassword();
175  }
176 
177  return bSuccess;
178 }
179 
181  uno::Reference< task::XInteractionHandler > const & handler)
182 {
183  assert(handler.is());
184  handler->handle(new UnsupportedEncryptionFormatRequest);
185 }
186 
187 }
188 
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno::Any m_aRequest
Reference
OUString m_aPassword
std::mutex m_aMutex
void reportUnsupportedEncryptionFormat(uno::Reference< task::XInteractionHandler > const &handler)
exports com.sun.star. task
bool getPassword(const uno::Reference< task::XInteractionHandler > &xHandler, OUString &rOutPwd, bool bFirstTry, const OUString &rDocName)
void getContinuations(css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > const &rContinuations, css::uno::Reference< t1 > *pContinuation1, css::uno::Reference< t2 > *pContinuation2)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)