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"
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
;
46 class PDFPasswordRequest
:
47 public cppu::WeakImplHelper2
<
48 task::XInteractionRequest
, task::XInteractionPassword
>,
49 private boost::noncopyable
52 mutable osl::Mutex m_aMutex
;
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
; }
74 virtual ~PDFPasswordRequest() {}
77 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry
, const OUString
& rName
) :
80 task::DocumentPasswordRequest(
81 OUString(), uno::Reference
< uno::XInterface
>(),
82 task::InteractionClassification_QUERY
,
84 ? task::PasswordRequestMode_PASSWORD_ENTER
85 : task::PasswordRequestMode_PASSWORD_REENTER
),
90 uno::Any
PDFPasswordRequest::getRequest() throw (uno::RuntimeException
, std::exception
)
95 uno::Sequence
< uno::Reference
< task::XInteractionContinuation
> > PDFPasswordRequest::getContinuations() throw (uno::RuntimeException
, std::exception
)
97 uno::Sequence
< uno::Reference
< task::XInteractionContinuation
> > aRet( 1 );
102 void PDFPasswordRequest::setPassword( const OUString
& rPwd
) throw (uno::RuntimeException
, std::exception
)
104 osl::MutexGuard
const guard( m_aMutex
);
109 OUString
PDFPasswordRequest::getPassword() throw (uno::RuntimeException
, std::exception
)
111 osl::MutexGuard
const guard( m_aMutex
);
116 void PDFPasswordRequest::select() throw (uno::RuntimeException
, std::exception
)
118 osl::MutexGuard
const guard( m_aMutex
);
123 class UnsupportedEncryptionFormatRequest
:
124 public cppu::WeakImplHelper1
< task::XInteractionRequest
>,
125 private boost::noncopyable
128 UnsupportedEncryptionFormatRequest() {}
131 virtual ~UnsupportedEncryptionFormatRequest() {}
133 virtual uno::Any SAL_CALL
getRequest() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{
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
{
145 uno::Sequence
< uno::Reference
< task::XInteractionContinuation
> >();
154 bool getPassword( const uno::Reference
< task::XInteractionHandler
>& xHandler
,
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() )
176 rOutPwd
= xReq
->getPassword();
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: */