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