Update git submodules
[LibreOffice.git] / sdext / source / pdfimport / misc / pwdinteract.cxx
blobdcd3f8566d1f11908b60825ca6f4638d357246ea
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <mutex>
25 #include <pdfihelper.hxx>
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>
33 #include <cppuhelper/implbase.hxx>
34 #include <rtl/ref.hxx>
35 #include <comphelper/errcode.hxx>
37 using namespace com::sun::star;
39 namespace
42 class PDFPasswordRequest:
43 public cppu::WeakImplHelper<
44 task::XInteractionRequest, task::XInteractionPassword >
46 private:
47 mutable std::mutex m_aMutex;
48 uno::Any m_aRequest;
49 OUString m_aPassword;
50 bool m_bSelected;
52 public:
53 explicit PDFPasswordRequest(bool bFirstTry, const OUString& rName);
54 PDFPasswordRequest(const PDFPasswordRequest&) = delete;
55 PDFPasswordRequest& operator=(const PDFPasswordRequest&) = delete;
57 // XInteractionRequest
58 virtual uno::Any SAL_CALL getRequest( ) override;
59 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations( ) override;
61 // XInteractionPassword
62 virtual void SAL_CALL setPassword( const OUString& rPwd ) override;
63 virtual OUString SAL_CALL getPassword() override;
65 // XInteractionContinuation
66 virtual void SAL_CALL select() override;
68 bool isSelected() const { std::scoped_lock const guard( m_aMutex ); return m_bSelected; }
70 private:
71 virtual ~PDFPasswordRequest() override {}
74 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const OUString& rName ) :
75 m_aRequest(
76 uno::Any(
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)
87 uno::Any PDFPasswordRequest::getRequest()
89 return m_aRequest;
92 uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations()
94 return { this };
97 void PDFPasswordRequest::setPassword( const OUString& rPwd )
99 std::scoped_lock const guard( m_aMutex );
101 m_aPassword = rPwd;
104 OUString PDFPasswordRequest::getPassword()
106 std::scoped_lock const guard( m_aMutex );
108 return m_aPassword;
111 void PDFPasswordRequest::select()
113 std::scoped_lock const guard( m_aMutex );
115 m_bSelected = true;
118 class UnsupportedEncryptionFormatRequest:
119 public cppu::WeakImplHelper< task::XInteractionRequest >
121 public:
122 UnsupportedEncryptionFormatRequest() {}
123 UnsupportedEncryptionFormatRequest(const UnsupportedEncryptionFormatRequest&) = delete;
124 UnsupportedEncryptionFormatRequest& operator=(const UnsupportedEncryptionFormatRequest&) = delete;
126 private:
127 virtual ~UnsupportedEncryptionFormatRequest() override {}
129 virtual uno::Any SAL_CALL getRequest() override {
130 return uno::Any(
131 task::ErrorCodeRequest(
132 OUString(), uno::Reference< uno::XInterface >(),
133 sal_uInt32(ERRCODE_IO_WRONGVERSION)));
134 //TODO: should be something more informative than crudely reused
135 // ERRCODE_IO_WRONGVERSION
138 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > >
139 SAL_CALL getContinuations() override {
140 return
141 uno::Sequence< uno::Reference< task::XInteractionContinuation > >();
145 } // namespace
147 namespace pdfi
150 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
151 OUString& rOutPwd,
152 bool bFirstTry,
153 const OUString& rDocName
156 bool bSuccess = false;
158 rtl::Reference< PDFPasswordRequest > xReq(
159 new PDFPasswordRequest( bFirstTry, rDocName ) );
162 xHandler->handle( xReq );
164 catch( uno::Exception& )
168 if( xReq->isSelected() )
170 bSuccess = true;
171 rOutPwd = xReq->getPassword();
174 return bSuccess;
177 void reportUnsupportedEncryptionFormat(
178 uno::Reference< task::XInteractionHandler > const & handler)
180 assert(handler.is());
181 handler->handle(new UnsupportedEncryptionFormatRequest);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */