1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26
27 #include "pdfihelper.hxx"
28
29 #include <com/sun/star/task/XInteractionHandler.hpp>
30 #include <com/sun/star/task/XInteractionRequest.hpp>
31 #include <com/sun/star/task/XInteractionPassword.hpp>
32 #include <com/sun/star/task/DocumentPasswordRequest.hpp>
33
34 #include <cppuhelper/exc_hlp.hxx>
35 #include <cppuhelper/compbase2.hxx>
36 #include <cppuhelper/basemutex.hxx>
37
38
39 using namespace com::sun::star;
40
41 namespace
42 {
43
44 typedef ::cppu::WeakComponentImplHelper2<
45 com::sun::star::task::XInteractionRequest,
46 com::sun::star::task::XInteractionPassword > PDFPasswordRequestBase;
47
48 class PDFPasswordRequest : private cppu::BaseMutex,
49 public PDFPasswordRequestBase
50 {
51 private:
52 task::DocumentPasswordRequest 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
isSelected() const70 bool isSelected() const { osl::MutexGuard const guard( m_aMutex ); return m_bSelected; }
71 };
72
PDFPasswordRequest(bool bFirstTry,const rtl::OUString & rName)73 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const rtl::OUString& rName ) :
74 PDFPasswordRequestBase( m_aMutex ),
75 m_aRequest(),
76 m_aPassword(),
77 m_bSelected(false)
78 {
79 m_aRequest.Mode = bFirstTry ?
80 task::PasswordRequestMode_PASSWORD_ENTER :
81 task::PasswordRequestMode_PASSWORD_REENTER;
82 m_aRequest.Classification = task::InteractionClassification_QUERY;
83 m_aRequest.Name = rName;
84 }
85
getRequest()86 uno::Any SAL_CALL PDFPasswordRequest::getRequest() throw (uno::RuntimeException)
87 {
88 osl::MutexGuard const guard( m_aMutex );
89
90 uno::Any aRet;
91 aRet <<= m_aRequest;
92 return aRet;
93 }
94
getContinuations()95 uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL PDFPasswordRequest::getContinuations() throw (uno::RuntimeException)
96 {
97 osl::MutexGuard const guard( m_aMutex );
98
99 uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
100 aRet.getArray()[0] = static_cast<task::XInteractionContinuation*>(this);
101 return aRet;
102 }
103
setPassword(const rtl::OUString & rPwd)104 void SAL_CALL PDFPasswordRequest::setPassword( const rtl::OUString& rPwd ) throw (uno::RuntimeException)
105 {
106 osl::MutexGuard const guard( m_aMutex );
107
108 m_aPassword = rPwd;
109 }
110
getPassword()111 rtl::OUString SAL_CALL PDFPasswordRequest::getPassword() throw (uno::RuntimeException)
112 {
113 osl::MutexGuard const guard( m_aMutex );
114
115 return m_aPassword;
116 }
117
select()118 void SAL_CALL PDFPasswordRequest::select() throw (uno::RuntimeException)
119 {
120 osl::MutexGuard const guard( m_aMutex );
121
122 m_bSelected = true;
123 }
124
125 } // namespace
126
127 namespace pdfi
128 {
129
getPassword(const uno::Reference<task::XInteractionHandler> & xHandler,rtl::OUString & rOutPwd,bool bFirstTry,const rtl::OUString & rDocName)130 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
131 rtl::OUString& rOutPwd,
132 bool bFirstTry,
133 const rtl::OUString& rDocName
134 )
135 {
136 bool bSuccess = false;
137
138 PDFPasswordRequest* pRequest;
139 uno::Reference< task::XInteractionRequest > xReq(
140 pRequest = new PDFPasswordRequest( bFirstTry, rDocName ) );
141 try
142 {
143 xHandler->handle( xReq );
144 }
145 catch( uno::Exception& )
146 {
147 }
148
149 OSL_TRACE( "request %s selected\n", pRequest->isSelected() ? "was" : "was not" );
150 if( pRequest->isSelected() )
151 {
152 bSuccess = true;
153 rOutPwd = pRequest->getPassword();
154 }
155
156 return bSuccess;
157 }
158
159 }
160