Update ooo320-m1
[ooovba.git] / sdext / source / pdfimport / misc / pwdinteract.cxx
blob45e77e96ede654059166c856aaa8f3ef4476f9be
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pwdinteract.cxx,v $
11 * $Revision: 1.2 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "pdfihelper.hxx"
37 #include <com/sun/star/task/XInteractionHandler.hpp>
38 #include <com/sun/star/task/XInteractionRequest.hpp>
39 #include <com/sun/star/task/XInteractionPassword.hpp>
40 #include <com/sun/star/task/PasswordRequest.hpp>
42 #include <cppuhelper/exc_hlp.hxx>
43 #include <cppuhelper/compbase2.hxx>
44 #include <cppuhelper/basemutex.hxx>
45 #include <comphelper/anytostring.hxx>
48 using namespace com::sun::star;
50 namespace
53 typedef ::cppu::WeakComponentImplHelper2<
54 com::sun::star::task::XInteractionRequest,
55 com::sun::star::task::XInteractionPassword > PDFPasswordRequestBase;
57 class PDFPasswordRequest : private cppu::BaseMutex,
58 public PDFPasswordRequestBase
60 private:
61 task::PasswordRequest m_aRequest;
62 rtl::OUString m_aPassword;
63 bool m_bSelected;
65 public:
66 explicit PDFPasswordRequest(bool bFirstTry);
68 // XInteractionRequest
69 virtual uno::Any SAL_CALL getRequest( ) throw (uno::RuntimeException);
70 virtual uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL getContinuations( ) throw (uno::RuntimeException);
72 // XInteractionPassword
73 virtual void SAL_CALL setPassword( const rtl::OUString& rPwd ) throw (uno::RuntimeException);
74 virtual rtl::OUString SAL_CALL getPassword() throw (uno::RuntimeException);
76 // XInteractionContinuation
77 virtual void SAL_CALL select() throw (uno::RuntimeException);
79 bool isSelected() const { osl::MutexGuard const guard( m_aMutex ); return m_bSelected; }
82 PDFPasswordRequest::PDFPasswordRequest( bool bFirstTry ) :
83 PDFPasswordRequestBase( m_aMutex ),
84 m_aRequest(),
85 m_aPassword(),
86 m_bSelected(false)
88 m_aRequest.Mode = bFirstTry ?
89 task::PasswordRequestMode_PASSWORD_ENTER :
90 task::PasswordRequestMode_PASSWORD_REENTER;
91 m_aRequest.Classification = task::InteractionClassification_QUERY;
94 uno::Any SAL_CALL PDFPasswordRequest::getRequest() throw (uno::RuntimeException)
96 osl::MutexGuard const guard( m_aMutex );
98 uno::Any aRet;
99 aRet <<= m_aRequest;
100 return aRet;
103 uno::Sequence< uno::Reference< task::XInteractionContinuation > > SAL_CALL PDFPasswordRequest::getContinuations() throw (uno::RuntimeException)
105 osl::MutexGuard const guard( m_aMutex );
107 uno::Sequence< uno::Reference< task::XInteractionContinuation > > aRet( 1 );
108 aRet.getArray()[0] = static_cast<task::XInteractionContinuation*>(this);
109 return aRet;
112 void SAL_CALL PDFPasswordRequest::setPassword( const rtl::OUString& rPwd ) throw (uno::RuntimeException)
114 osl::MutexGuard const guard( m_aMutex );
116 m_aPassword = rPwd;
119 rtl::OUString SAL_CALL PDFPasswordRequest::getPassword() throw (uno::RuntimeException)
121 osl::MutexGuard const guard( m_aMutex );
123 return m_aPassword;
126 void SAL_CALL PDFPasswordRequest::select() throw (uno::RuntimeException)
128 osl::MutexGuard const guard( m_aMutex );
130 m_bSelected = true;
133 } // namespace
135 namespace pdfi
138 bool getPassword( const uno::Reference< task::XInteractionHandler >& xHandler,
139 rtl::OUString& rOutPwd,
140 bool bFirstTry )
142 bool bSuccess = false;
144 PDFPasswordRequest* pRequest;
145 uno::Reference< task::XInteractionRequest > xReq(
146 pRequest = new PDFPasswordRequest( bFirstTry ) );
149 xHandler->handle( xReq );
151 catch( uno::Exception& )
153 #if 0
154 OSL_ENSURE( false,
155 rtl::OUStringToOString(
156 comphelper::anyToString( cppu::getCaughtException() ),
157 RTL_TEXTENCODING_UTF8 ).getStr() );
158 #endif
161 OSL_TRACE( "request %s selected\n", pRequest->isSelected() ? "was" : "was not" );
162 if( pRequest->isSelected() )
164 bSuccess = true;
165 rOutPwd = pRequest->getPassword();
168 return bSuccess;