Bump version to 5.0-14
[LibreOffice.git] / sdext / source / pdfimport / misc / pwdinteract.cxx
blob896f5b678bc4b576e242ee10f28d364222847139
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>
24 #include "pdfihelper.hxx"
26 #include <boost/noncopyable.hpp>
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/exc_hlp.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <cppuhelper/implbase2.hxx>
36 #include <osl/mutex.hxx>
37 #include <osl/diagnose.h>
38 #include <rtl/ref.hxx>
39 #include <tools/errcode.hxx>
41 using namespace com::sun::star;
43 namespace
46 class PDFPasswordRequest:
47 public cppu::WeakImplHelper2<
48 task::XInteractionRequest, task::XInteractionPassword >,
49 private boost::noncopyable
51 private:
52 mutable osl::Mutex m_aMutex;
53 uno::Any m_aRequest;
54 OUString m_aPassword;
55 bool m_bSelected;
57 public:
58 explicit PDFPasswordRequest(bool bFirstTry, const OUString& rName);
60 // XInteractionRequest
61 virtual uno::Any SAL_CALL getRequest( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
64 // XInteractionPassword
65 virtual void SAL_CALL setPassword( const OUString& rPwd ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 virtual OUString SAL_CALL getPassword() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 // XInteractionContinuation
69 virtual void SAL_CALL select() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 bool isSelected() const { osl::MutexGuard const guard( m_aMutex ); return m_bSelected; }
73 private:
74 virtual ~PDFPasswordRequest() {}
77 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const OUString& rName ) :
78 m_aRequest(
79 uno::makeAny(
80 task::DocumentPasswordRequest(
81 OUString(), uno::Reference< uno::XInterface >(),
82 task::InteractionClassification_QUERY,
83 (bFirstTry
84 ? task::PasswordRequestMode_PASSWORD_ENTER
85 : task::PasswordRequestMode_PASSWORD_REENTER),
86 rName))),
87 m_bSelected(false)
90 uno::Any PDFPasswordRequest::getRequest() throw (uno::RuntimeException, std::exception)
92 return m_aRequest;
95 uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations() throw (uno::RuntimeException, std::exception)
97 uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
98 aRet[0] = this;
99 return aRet;
102 void PDFPasswordRequest::setPassword( const OUString& rPwd ) throw (uno::RuntimeException, std::exception)
104 osl::MutexGuard const guard( m_aMutex );
106 m_aPassword = rPwd;
109 OUString PDFPasswordRequest::getPassword() throw (uno::RuntimeException, std::exception)
111 osl::MutexGuard const guard( m_aMutex );
113 return m_aPassword;
116 void PDFPasswordRequest::select() throw (uno::RuntimeException, std::exception)
118 osl::MutexGuard const guard( m_aMutex );
120 m_bSelected = true;
123 class UnsupportedEncryptionFormatRequest:
124 public cppu::WeakImplHelper1< task::XInteractionRequest >,
125 private boost::noncopyable
127 public:
128 UnsupportedEncryptionFormatRequest() {}
130 private:
131 virtual ~UnsupportedEncryptionFormatRequest() {}
133 virtual uno::Any SAL_CALL getRequest() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE {
134 return uno::makeAny(
135 task::ErrorCodeRequest(
136 OUString(), uno::Reference< uno::XInterface >(),
137 ERRCODE_IO_WRONGVERSION));
138 //TODO: should be something more informative than crudely reused
139 // ERRCODE_IO_WRONGVERSION
142 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > >
143 SAL_CALL getContinuations() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE {
144 return
145 uno::Sequence< uno::Reference< task::XInteractionContinuation > >();
149 } // namespace
151 namespace pdfi
154 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
155 OUString& rOutPwd,
156 bool bFirstTry,
157 const OUString& rDocName
160 bool bSuccess = false;
162 rtl::Reference< PDFPasswordRequest > xReq(
163 new PDFPasswordRequest( bFirstTry, rDocName ) );
166 xHandler->handle( xReq.get() );
168 catch( uno::Exception& )
172 OSL_TRACE( "request %s selected", xReq->isSelected() ? "was" : "was not" );
173 if( xReq->isSelected() )
175 bSuccess = true;
176 rOutPwd = xReq->getPassword();
179 return bSuccess;
182 void reportUnsupportedEncryptionFormat(
183 uno::Reference< task::XInteractionHandler > const & handler)
185 assert(handler.is());
186 handler->handle(new UnsupportedEncryptionFormatRequest);
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */