Line data Source code
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 :
24 : #include "pdfihelper.hxx"
25 :
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>
32 :
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>
40 :
41 : using namespace com::sun::star;
42 :
43 : namespace
44 : {
45 :
46 : class PDFPasswordRequest:
47 : public cppu::WeakImplHelper2<
48 : task::XInteractionRequest, task::XInteractionPassword >,
49 : private boost::noncopyable
50 : {
51 : private:
52 : mutable osl::Mutex m_aMutex;
53 : uno::Any m_aRequest;
54 : OUString m_aPassword;
55 : bool m_bSelected;
56 :
57 : public:
58 : explicit PDFPasswordRequest(bool bFirstTry, const OUString& rName);
59 :
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;
63 :
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;
67 :
68 : // XInteractionContinuation
69 : virtual void SAL_CALL select() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
70 :
71 0 : bool isSelected() const { osl::MutexGuard const guard( m_aMutex ); return m_bSelected; }
72 :
73 : private:
74 0 : virtual ~PDFPasswordRequest() {}
75 : };
76 :
77 0 : 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 0 : m_bSelected(false)
88 0 : {}
89 :
90 0 : uno::Any PDFPasswordRequest::getRequest() throw (uno::RuntimeException, std::exception)
91 : {
92 0 : return m_aRequest;
93 : }
94 :
95 0 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > PDFPasswordRequest::getContinuations() throw (uno::RuntimeException, std::exception)
96 : {
97 0 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
98 0 : aRet[0] = this;
99 0 : return aRet;
100 : }
101 :
102 0 : void PDFPasswordRequest::setPassword( const OUString& rPwd ) throw (uno::RuntimeException, std::exception)
103 : {
104 0 : osl::MutexGuard const guard( m_aMutex );
105 :
106 0 : m_aPassword = rPwd;
107 0 : }
108 :
109 0 : OUString PDFPasswordRequest::getPassword() throw (uno::RuntimeException, std::exception)
110 : {
111 0 : osl::MutexGuard const guard( m_aMutex );
112 :
113 0 : return m_aPassword;
114 : }
115 :
116 0 : void PDFPasswordRequest::select() throw (uno::RuntimeException, std::exception)
117 : {
118 0 : osl::MutexGuard const guard( m_aMutex );
119 :
120 0 : m_bSelected = true;
121 0 : }
122 :
123 : class UnsupportedEncryptionFormatRequest:
124 : public cppu::WeakImplHelper1< task::XInteractionRequest >,
125 : private boost::noncopyable
126 : {
127 : public:
128 0 : UnsupportedEncryptionFormatRequest() {}
129 :
130 : private:
131 0 : virtual ~UnsupportedEncryptionFormatRequest() {}
132 :
133 0 : 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 0 : ERRCODE_IO_WRONGVERSION));
138 : //TODO: should be something more informative than crudely reused
139 : // ERRCODE_IO_WRONGVERSION
140 : }
141 :
142 : virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > >
143 0 : SAL_CALL getContinuations() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE {
144 : return
145 0 : uno::Sequence< uno::Reference< task::XInteractionContinuation > >();
146 : }
147 : };
148 :
149 : } // namespace
150 :
151 : namespace pdfi
152 : {
153 :
154 0 : bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
155 : OUString& rOutPwd,
156 : bool bFirstTry,
157 : const OUString& rDocName
158 : )
159 : {
160 0 : bool bSuccess = false;
161 :
162 : rtl::Reference< PDFPasswordRequest > xReq(
163 0 : new PDFPasswordRequest( bFirstTry, rDocName ) );
164 : try
165 : {
166 0 : xHandler->handle( xReq.get() );
167 : }
168 0 : catch( uno::Exception& )
169 : {
170 : }
171 :
172 : OSL_TRACE( "request %s selected", xReq->isSelected() ? "was" : "was not" );
173 0 : if( xReq->isSelected() )
174 : {
175 0 : bSuccess = true;
176 0 : rOutPwd = xReq->getPassword();
177 : }
178 :
179 0 : return bSuccess;
180 : }
181 :
182 0 : void reportUnsupportedEncryptionFormat(
183 : uno::Reference< task::XInteractionHandler > const & handler)
184 : {
185 : assert(handler.is());
186 0 : handler->handle(new UnsupportedEncryptionFormatRequest);
187 0 : }
188 :
189 : }
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|