1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
42 class PDFPasswordRequest
:
43 public cppu::WeakImplHelper
<
44 task::XInteractionRequest
, task::XInteractionPassword
>
47 mutable std::mutex m_aMutex
;
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
; }
71 virtual ~PDFPasswordRequest() override
{}
74 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry
, const OUString
& rName
) :
77 task::DocumentPasswordRequest(
78 OUString(), uno::Reference
< uno::XInterface
>(),
79 task::InteractionClassification_QUERY
,
81 ? task::PasswordRequestMode_PASSWORD_ENTER
82 : task::PasswordRequestMode_PASSWORD_REENTER
),
87 uno::Any
PDFPasswordRequest::getRequest()
92 uno::Sequence
< uno::Reference
< task::XInteractionContinuation
> > PDFPasswordRequest::getContinuations()
97 void PDFPasswordRequest::setPassword( const OUString
& rPwd
)
99 std::scoped_lock
const guard( m_aMutex
);
104 OUString
PDFPasswordRequest::getPassword()
106 std::scoped_lock
const guard( m_aMutex
);
111 void PDFPasswordRequest::select()
113 std::scoped_lock
const guard( m_aMutex
);
118 class UnsupportedEncryptionFormatRequest
:
119 public cppu::WeakImplHelper
< task::XInteractionRequest
>
122 UnsupportedEncryptionFormatRequest() {}
123 UnsupportedEncryptionFormatRequest(const UnsupportedEncryptionFormatRequest
&) = delete;
124 UnsupportedEncryptionFormatRequest
& operator=(const UnsupportedEncryptionFormatRequest
&) = delete;
127 virtual ~UnsupportedEncryptionFormatRequest() override
{}
129 virtual uno::Any SAL_CALL
getRequest() override
{
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
{
141 uno::Sequence
< uno::Reference
< task::XInteractionContinuation
> >();
150 bool getPassword( const uno::Reference
< task::XInteractionHandler
>& xHandler
,
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() )
171 rOutPwd
= xReq
->getPassword();
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: */