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
34#include <rtl/ref.hxx>
36
37using namespace com::sun::star;
38
39namespace
40{
41
42class PDFPasswordRequest:
43 public cppu::WeakImplHelper<
44 task::XInteractionRequest, task::XInteractionPassword >
45{
46private:
47 mutable std::mutex m_aMutex;
49 OUString m_aPassword;
50 bool m_bSelected;
51
52public:
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 { std::scoped_lock const guard( m_aMutex ); return m_bSelected; }
69
70private:
71 virtual ~PDFPasswordRequest() override {}
72};
73
74PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const OUString& rName ) :
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)
85{}
86
87uno::Any PDFPasswordRequest::getRequest()
88{
89 return m_aRequest;
90}
91
92uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations()
93{
94 return { this };
95}
96
97void PDFPasswordRequest::setPassword( const OUString& rPwd )
98{
99 std::scoped_lock const guard( m_aMutex );
100
101 m_aPassword = rPwd;
102}
103
105{
106 std::scoped_lock const guard( m_aMutex );
107
108 return m_aPassword;
109}
110
111void PDFPasswordRequest::select()
112{
113 std::scoped_lock const guard( m_aMutex );
114
115 m_bSelected = true;
116}
117
118class UnsupportedEncryptionFormatRequest:
119 public cppu::WeakImplHelper< task::XInteractionRequest >
120{
121public:
122 UnsupportedEncryptionFormatRequest() {}
123 UnsupportedEncryptionFormatRequest(const UnsupportedEncryptionFormatRequest&) = delete;
124 UnsupportedEncryptionFormatRequest& operator=(const UnsupportedEncryptionFormatRequest&) = delete;
125
126private:
127 virtual ~UnsupportedEncryptionFormatRequest() override {}
128
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
136 }
137
138 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > >
139 SAL_CALL getContinuations() override {
140 return
141 uno::Sequence< uno::Reference< task::XInteractionContinuation > >();
142 }
143};
144
145} // namespace
146
147namespace pdfi
148{
149
150bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
151 OUString& rOutPwd,
152 bool bFirstTry,
153 const OUString& rDocName
154 )
155{
156 bool bSuccess = false;
157
159 new PDFPasswordRequest( bFirstTry, rDocName ) );
160 try
161 {
162 xHandler->handle( xReq );
163 }
164 catch( uno::Exception& )
165 {
166 }
167
168 if( xReq->isSelected() )
169 {
170 bSuccess = true;
171 rOutPwd = xReq->getPassword();
172 }
173
174 return bSuccess;
175}
176
178 uno::Reference< task::XInteractionHandler > const & handler)
179{
180 assert(handler.is());
181 handler->handle(new UnsupportedEncryptionFormatRequest);
182}
183
184}
185
186/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void getContinuations(css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > const &rContinuations, css::uno::Reference< t1 > *pContinuation1, css::uno::Reference< t2 > *pContinuation2)
std::mutex m_aMutex
uno::Any m_aRequest
Reference
bool getPassword(const css::uno::Reference< css::task::XInteractionHandler > &xHandler, OUString &rOutPwd, bool bFirstTry, const OUString &rDocName)
retrieve password from user
void reportUnsupportedEncryptionFormat(css::uno::Reference< css::task::XInteractionHandler > const &handler)
bool getPassword(const uno::Reference< task::XInteractionHandler > &xHandler, OUString &rOutPwd, bool bFirstTry, const OUString &rDocName)
OUString m_aPassword