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