1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docpasswordhelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper/docpasswordhelper.hxx"
35 #include <com/sun/star/task/XInteractionHandler.hpp>
36 #include "comphelper/mediadescriptor.hxx"
38 using ::rtl::OUString
;
39 using ::com::sun::star::uno::Exception
;
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::uno::UNO_SET_THROW
;
42 using ::com::sun::star::task::PasswordRequestMode
;
43 using ::com::sun::star::task::PasswordRequestMode_PASSWORD_ENTER
;
44 using ::com::sun::star::task::PasswordRequestMode_PASSWORD_REENTER
;
45 using ::com::sun::star::task::XInteractionHandler
;
46 using ::com::sun::star::task::XInteractionRequest
;
48 namespace comphelper
{
50 // ============================================================================
52 IDocPasswordVerifier::~IDocPasswordVerifier()
56 // ============================================================================
58 /*static*/ OUString
DocPasswordHelper::requestAndVerifyDocPassword(
59 IDocPasswordVerifier
& rVerifier
,
60 const OUString
& rMediaPassword
,
61 const Reference
< XInteractionHandler
>& rxInteractHandler
,
62 const OUString
& rDocumentName
,
63 DocPasswordRequestType eRequestType
,
64 const ::std::vector
< OUString
>* pDefaultPasswords
,
65 bool* pbIsDefaultPassword
)
68 DocPasswordVerifierResult eResult
= DocPasswordVerifierResult_WRONG_PASSWORD
;
70 // first, try provided default passwords
71 if( pbIsDefaultPassword
)
72 *pbIsDefaultPassword
= false;
73 if( pDefaultPasswords
)
75 for( ::std::vector
< OUString
>::const_iterator aIt
= pDefaultPasswords
->begin(), aEnd
= pDefaultPasswords
->end(); (eResult
== DocPasswordVerifierResult_WRONG_PASSWORD
) && (aIt
!= aEnd
); ++aIt
)
78 OSL_ENSURE( aPassword
.getLength() > 0, "DocPasswordHelper::requestAndVerifyDocPassword - unexpected empty default password" );
79 if( aPassword
.getLength() > 0 )
81 eResult
= rVerifier
.verifyPassword( aPassword
);
82 if( pbIsDefaultPassword
)
83 *pbIsDefaultPassword
= eResult
== DocPasswordVerifierResult_OK
;
88 // try media password (skip, if result is OK or ABORT)
89 if( eResult
== DocPasswordVerifierResult_WRONG_PASSWORD
)
91 aPassword
= rMediaPassword
;
92 if( aPassword
.getLength() > 0 )
93 eResult
= rVerifier
.verifyPassword( aPassword
);
96 // request a password (skip, if result is OK or ABORT)
97 if( (eResult
== DocPasswordVerifierResult_WRONG_PASSWORD
) && rxInteractHandler
.is() ) try
99 PasswordRequestMode eRequestMode
= PasswordRequestMode_PASSWORD_ENTER
;
100 while( eResult
== DocPasswordVerifierResult_WRONG_PASSWORD
)
102 DocPasswordRequest
* pRequest
= new DocPasswordRequest( eRequestType
, eRequestMode
, rDocumentName
);
103 Reference
< XInteractionRequest
> xRequest( pRequest
);
104 rxInteractHandler
->handle( xRequest
);
105 if( pRequest
->isPassword() )
107 aPassword
= pRequest
->getPassword();
108 if( aPassword
.getLength() > 0 )
109 eResult
= rVerifier
.verifyPassword( aPassword
);
113 eResult
= DocPasswordVerifierResult_ABORT
;
115 eRequestMode
= PasswordRequestMode_PASSWORD_REENTER
;
122 return (eResult
== DocPasswordVerifierResult_OK
) ? aPassword
: OUString();
125 /*static*/ OUString
DocPasswordHelper::requestAndVerifyDocPassword(
126 IDocPasswordVerifier
& rVerifier
,
127 MediaDescriptor
& rMediaDesc
,
128 DocPasswordRequestType eRequestType
,
129 const ::std::vector
< OUString
>* pDefaultPasswords
)
131 OUString aMediaPassword
= rMediaDesc
.getUnpackedValueOrDefault(
132 MediaDescriptor::PROP_PASSWORD(), OUString() );
133 Reference
< XInteractionHandler
> xInteractHandler
= rMediaDesc
.getUnpackedValueOrDefault(
134 MediaDescriptor::PROP_INTERACTIONHANDLER(), Reference
< XInteractionHandler
>() );
135 OUString aDocumentName
= rMediaDesc
.getUnpackedValueOrDefault(
136 MediaDescriptor::PROP_URL(), OUString() );
138 bool bIsDefaultPassword
= false;
139 OUString aPassword
= requestAndVerifyDocPassword(
140 rVerifier
, aMediaPassword
, xInteractHandler
, aDocumentName
, eRequestType
, pDefaultPasswords
, &bIsDefaultPassword
);
142 // insert valid password into media descriptor (but not a default password)
143 if( (aPassword
.getLength() > 0) && !bIsDefaultPassword
)
144 rMediaDesc
[ MediaDescriptor::PROP_PASSWORD() ] <<= aPassword
;
149 // ============================================================================
151 } // namespace comphelper