1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sdext.hxx"
30 
31 #include "pdfihelper.hxx"
32 
33 #include <com/sun/star/task/XInteractionHandler.hpp>
34 #include <com/sun/star/task/XInteractionRequest.hpp>
35 #include <com/sun/star/task/XInteractionPassword.hpp>
36 #include <com/sun/star/task/DocumentPasswordRequest.hpp>
37 
38 #include <cppuhelper/exc_hlp.hxx>
39 #include <cppuhelper/compbase2.hxx>
40 #include <cppuhelper/basemutex.hxx>
41 
42 
43 using namespace com::sun::star;
44 
45 namespace
46 {
47 
48 typedef ::cppu::WeakComponentImplHelper2<
49 	com::sun::star::task::XInteractionRequest,
50 	com::sun::star::task::XInteractionPassword > PDFPasswordRequestBase;
51 
52 class PDFPasswordRequest : private cppu::BaseMutex,
53                            public PDFPasswordRequestBase
54 {
55 private:
56     task::DocumentPasswordRequest m_aRequest;
57     rtl::OUString                 m_aPassword;
58     bool                          m_bSelected;
59 
60 public:
61     explicit PDFPasswordRequest(bool bFirstTry, const rtl::OUString& rName);
62 
63     // XInteractionRequest
64     virtual uno::Any SAL_CALL getRequest(  ) throw (uno::RuntimeException);
65     virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations(  ) throw (uno::RuntimeException);
66 
67     // XInteractionPassword
68     virtual void SAL_CALL setPassword( const rtl::OUString& rPwd ) throw (uno::RuntimeException);
69     virtual rtl::OUString SAL_CALL getPassword() throw (uno::RuntimeException);
70 
71     // XInteractionContinuation
72     virtual void SAL_CALL select() throw (uno::RuntimeException);
73 
74     bool isSelected() const { osl::MutexGuard const guard( m_aMutex ); return m_bSelected; }
75 };
76 
77 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry, const rtl::OUString& rName ) :
78     PDFPasswordRequestBase( m_aMutex ),
79     m_aRequest(),
80     m_aPassword(),
81     m_bSelected(false)
82 {
83     m_aRequest.Mode = bFirstTry ?
84         task::PasswordRequestMode_PASSWORD_ENTER :
85         task::PasswordRequestMode_PASSWORD_REENTER;
86     m_aRequest.Classification = task::InteractionClassification_QUERY;
87     m_aRequest.Name = rName;
88 }
89 
90 uno::Any SAL_CALL PDFPasswordRequest::getRequest() throw (uno::RuntimeException)
91 {
92     osl::MutexGuard const guard( m_aMutex );
93 
94     uno::Any aRet;
95     aRet <<= m_aRequest;
96     return aRet;
97 }
98 
99 uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL PDFPasswordRequest::getContinuations() throw (uno::RuntimeException)
100 {
101     osl::MutexGuard const guard( m_aMutex );
102 
103     uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
104     aRet.getArray()[0] = static_cast<task::XInteractionContinuation*>(this);
105     return aRet;
106 }
107 
108 void SAL_CALL PDFPasswordRequest::setPassword( const rtl::OUString& rPwd ) throw (uno::RuntimeException)
109 {
110     osl::MutexGuard const guard( m_aMutex );
111 
112     m_aPassword = rPwd;
113 }
114 
115 rtl::OUString SAL_CALL PDFPasswordRequest::getPassword() throw (uno::RuntimeException)
116 {
117     osl::MutexGuard const guard( m_aMutex );
118 
119     return m_aPassword;
120 }
121 
122 void SAL_CALL PDFPasswordRequest::select() throw (uno::RuntimeException)
123 {
124     osl::MutexGuard const guard( m_aMutex );
125 
126     m_bSelected = true;
127 }
128 
129 } // namespace
130 
131 namespace pdfi
132 {
133 
134 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
135                   rtl::OUString&                                     rOutPwd,
136                   bool                                               bFirstTry,
137                   const rtl::OUString&                               rDocName
138                   )
139 {
140     bool bSuccess = false;
141 
142     PDFPasswordRequest* pRequest;
143     uno::Reference< task::XInteractionRequest > xReq(
144         pRequest = new PDFPasswordRequest( bFirstTry, rDocName ) );
145     try
146     {
147         xHandler->handle( xReq );
148     }
149     catch( uno::Exception& )
150     {
151 	}
152 
153     OSL_TRACE( "request %s selected\n", pRequest->isSelected() ? "was" : "was not" );
154     if( pRequest->isSelected() )
155     {
156         bSuccess = true;
157         rOutPwd = pRequest->getPassword();
158     }
159 
160     return bSuccess;
161 }
162 
163 }
164