Make requestAndVerifyDocPassword var with MediaDescriptor a mem fn of latter

Change-Id: I645d554c7ce592d93ca0696fa7e6068ce40c9058
This commit is contained in:
Stephan Bergmann
2013-11-12 18:12:26 +01:00
parent 18be6f161d
commit 8061c8c70b
5 changed files with 80 additions and 88 deletions

View File

@@ -19,8 +19,8 @@
#include "comphelper/docpasswordhelper.hxx"
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/task/XInteractionHandler.hpp>
#include "comphelper/mediadescriptor.hxx"
#include <osl/time.h>
#include <rtl/digest.h>
@@ -412,37 +412,6 @@ Sequence< sal_Int8 > DocPasswordHelper::GetXLHashAsSequence(
return (eResult == DocPasswordVerifierResult_OK) ? aEncData : uno::Sequence< beans::NamedValue >();
}
/*static*/ ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > DocPasswordHelper::requestAndVerifyDocPassword(
IDocPasswordVerifier& rVerifier,
MediaDescriptor& rMediaDesc,
DocPasswordRequestType eRequestType,
const ::std::vector< OUString >* pDefaultPasswords )
{
uno::Sequence< beans::NamedValue > aMediaEncData = rMediaDesc.getUnpackedValueOrDefault(
MediaDescriptor::PROP_ENCRYPTIONDATA(), uno::Sequence< beans::NamedValue >() );
OUString aMediaPassword = rMediaDesc.getUnpackedValueOrDefault(
MediaDescriptor::PROP_PASSWORD(), OUString() );
Reference< XInteractionHandler > xInteractHandler = rMediaDesc.getUnpackedValueOrDefault(
MediaDescriptor::PROP_INTERACTIONHANDLER(), Reference< XInteractionHandler >() );
OUString aDocumentName = rMediaDesc.getUnpackedValueOrDefault(
MediaDescriptor::PROP_URL(), OUString() );
bool bIsDefaultPassword = false;
uno::Sequence< beans::NamedValue > aEncryptionData = requestAndVerifyDocPassword(
rVerifier, aMediaEncData, aMediaPassword, xInteractHandler, aDocumentName, eRequestType, pDefaultPasswords, &bIsDefaultPassword );
rMediaDesc.erase( MediaDescriptor::PROP_PASSWORD() );
rMediaDesc.erase( MediaDescriptor::PROP_ENCRYPTIONDATA() );
// insert valid password into media descriptor (but not a default password)
if( (aEncryptionData.getLength() > 0) && !bIsDefaultPassword )
rMediaDesc[ MediaDescriptor::PROP_ENCRYPTIONDATA() ] <<= aEncryptionData;
return aEncryptionData;
}
// ============================================================================
} // namespace comphelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -17,6 +17,7 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <comphelper/docpasswordhelper.hxx>
#include <comphelper/mediadescriptor.hxx>
#include <comphelper/namedvaluecollection.hxx>
#include <comphelper/stillreadwriteinteraction.hxx>
@@ -444,6 +445,34 @@ void MediaDescriptor::clearComponentDataEntry( const OUString& rName )
}
}
::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > MediaDescriptor::requestAndVerifyDocPassword(
IDocPasswordVerifier& rVerifier,
DocPasswordRequestType eRequestType,
const ::std::vector< OUString >* pDefaultPasswords )
{
css::uno::Sequence< css::beans::NamedValue > aMediaEncData = getUnpackedValueOrDefault(
PROP_ENCRYPTIONDATA(), css::uno::Sequence< css::beans::NamedValue >() );
OUString aMediaPassword = getUnpackedValueOrDefault(
PROP_PASSWORD(), OUString() );
css::uno::Reference< css::task::XInteractionHandler > xInteractHandler = getUnpackedValueOrDefault(
PROP_INTERACTIONHANDLER(), css::uno::Reference< css::task::XInteractionHandler >() );
OUString aDocumentName = getUnpackedValueOrDefault(
PROP_URL(), OUString() );
bool bIsDefaultPassword = false;
css::uno::Sequence< css::beans::NamedValue > aEncryptionData = DocPasswordHelper::requestAndVerifyDocPassword(
rVerifier, aMediaEncData, aMediaPassword, xInteractHandler, aDocumentName, eRequestType, pDefaultPasswords, &bIsDefaultPassword );
erase( PROP_PASSWORD() );
erase( PROP_ENCRYPTIONDATA() );
// insert valid password into media descriptor (but not a default password)
if( (aEncryptionData.getLength() > 0) && !bIsDefaultPassword )
(*this)[ PROP_ENCRYPTIONDATA() ] <<= aEncryptionData;
return aEncryptionData;
}
sal_Bool MediaDescriptor::addInputStream()
{
return impl_addInputStream( sal_True );

View File

@@ -30,10 +30,6 @@ namespace com { namespace sun { namespace star { namespace beans { struct Proper
namespace comphelper {
class MediaDescriptor;
// ============================================================================
enum DocPasswordVerifierResult
{
DocPasswordVerifierResult_OK,
@@ -290,56 +286,6 @@ public:
const ::std::vector< OUString >* pDefaultPasswords = 0,
bool* pbIsDefaultPassword = 0 );
// ------------------------------------------------------------------------
/** This helper function tries to find a password for the document
described by the passed media descriptor.
First, the list of default passwords will be tried if provided. This is
needed by import filters for external file formats that have to check a
predefined password in some cases without asking the user for a
password. Every password is checked using the passed password verifier.
If not successful, the passed media descriptor is asked for a password,
that has been set e.g. by an API call to load a document. If existing,
the password is checked using the passed password verifier.
If still not successful, the interaction handler contained in the
passed nmedia descriptor is used to request a password from the user.
This will be repeated until the passed password verifier validates the
entered password, or if the user chooses to cancel password input.
@param rVerifier
The password verifier used to check every processed password.
@param rMediaDesc
The media descriptor of the document that needs to be opened with
a password. If a valid password (that is not contained in the
passed list of default passwords) was found, it will be inserted
into the "Password" property of this descriptor.
@param eRequestType
The password request type that will be passed to the
DocPasswordRequest object created internally. See
docpasswordrequest.hxx for more details.
@param pDefaultPasswords
If not null, contains default passwords that will be tried before a
password will be requested from the media descriptor or the user.
@return
If not empty, contains the password that has been validated by the
passed password verifier. If empty, no valid password has been
found, or the user has chossen to cancel password input.
*/
static ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > requestAndVerifyDocPassword(
IDocPasswordVerifier& rVerifier,
MediaDescriptor& rMediaDesc,
DocPasswordRequestType eRequestType,
const ::std::vector< OUString >* pDefaultPasswords = 0 );
// ------------------------------------------------------------------------
private:
~DocPasswordHelper();
};

View File

@@ -20,6 +20,7 @@
#ifndef INCLUDED_COMPHELPER_MEDIADESCRIPTOR_HXX
#define INCLUDED_COMPHELPER_MEDIADESCRIPTOR_HXX
#include <comphelper/docpasswordrequest.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <rtl/ustring.hxx>
#include <comphelper/comphelperdllapi.h>
@@ -30,6 +31,8 @@ namespace com { namespace sun { namespace star { namespace io {
namespace comphelper{
class IDocPasswordVerifier;
/** @short can be used to work with a ::com::sun::star::document::MediaDescriptor
struct.
@@ -216,6 +219,51 @@ class COMPHELPER_DLLPUBLIC MediaDescriptor : public SequenceAsHashMap
void clearComponentDataEntry(
const OUString& rName );
/** This helper function tries to find a password for the document
described by this media descriptor.
First, the list of default passwords will be tried if provided. This
is needed by import filters for external file formats that have to
check a predefined password in some cases without asking the user
for a password. Every password is checked using the passed password
verifier.
If not successful, this media descriptor is asked for a password,
that has been set e.g. by an API call to load a document. If
existing, the password is checked using the passed password
verifier.
If still not successful, the interaction handler contained in this
media descriptor is used to request a password from the user. This
will be repeated until the passed password verifier validates the
entered password, or if the user chooses to cancel password input.
If a valid password (that is not contained in the passed list of
default passwords) was found, it will be inserted into the
"Password" property of this descriptor.
@param rVerifier
The password verifier used to check every processed password.
@param eRequestType
The password request type that will be passed to the
DocPasswordRequest object created internally. See
docpasswordrequest.hxx for more details.
@param pDefaultPasswords
If not null, contains default passwords that will be tried before a
password will be requested from the media descriptor or the user.
@return
If not empty, contains the password that has been validated by the
passed password verifier. If empty, no valid password has been
found, or the user has chossen to cancel password input.
*/
::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > requestAndVerifyDocPassword(
IDocPasswordVerifier& rVerifier,
DocPasswordRequestType eRequestType,
const ::std::vector< OUString >* pDefaultPasswords = 0 );
//-------------------------------------------
// helper
private:

View File

@@ -343,8 +343,8 @@ Reference< XInputStream > FilterDetect::extractUnencryptedPackage( MediaDescript
user has cancelled the password input dialog. */
PasswordVerifier aVerifier( aDecryptor );
Sequence<NamedValue> aEncryptionData;
aEncryptionData = comphelper::DocPasswordHelper::requestAndVerifyDocPassword(
aVerifier, rMediaDescriptor,
aEncryptionData = rMediaDescriptor.requestAndVerifyDocPassword(
aVerifier,
comphelper::DocPasswordRequestType_MS,
&aDefaultPasswords );