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