Move the ooo wallet code to use the part.cpp one

Less widgets in generators/core \o/
This commit is contained in:
Albert Astals Cid 2014-05-10 15:31:59 +02:00
parent cebe8d6509
commit 86dd9e8801
16 changed files with 142 additions and 171 deletions

View file

@ -4300,6 +4300,13 @@ void Document::setAnnotationEditingEnabled( bool enable )
foreachObserver( notifySetup( d->m_pagesVector, 0 ) );
}
void Document::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
{
if (d->m_generator) {
d->m_generator->walletDataForFile( fileName, walletName, walletFolder, walletKey );
}
}
void DocumentPrivate::requestDone( PixmapRequest * req )
{
if ( !req )

View file

@ -726,6 +726,13 @@ class OKULAR_EXPORT Document : public QObject
*/
void setAnnotationEditingEnabled( bool enable );
/**
* Returns which wallet data to use to read/write the password for the given fileName
*
* @since 0.20 (KDE 4.14)
*/
void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
public Q_SLOTS:
/**
* This slot is called whenever the user changes the @p rotation of

View file

@ -18,6 +18,7 @@
#include <kdebug.h>
#include <kicon.h>
#include <klocale.h>
#include <kwallet.h>
#include "document.h"
#include "document_p.h"
@ -358,6 +359,13 @@ bool Generator::exportTo( const QString&, const ExportFormat& )
return false;
}
void Generator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
{
*walletKey = fileName.section('/', -1, -1);
*walletName = KWallet::Wallet::NetworkWallet();
*walletFolder = "KPdf";
}
bool Generator::hasFeature( GeneratorFeature feature ) const
{
Q_D( const Generator );

View file

@ -412,6 +412,13 @@ class OKULAR_EXPORT Generator : public QObject
*/
virtual bool exportTo( const QString &fileName, const ExportFormat &format );
/**
* This method is called to know which wallet data should be used for the given file name.
* Unless you have very special requirements to where wallet data should be stored you
* don't need to reimplement this method.
*/
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
/**
* Query for the specified @p feature.
*/

View file

@ -45,6 +45,28 @@ TextDocumentConverter::~TextDocumentConverter()
delete d_ptr;
}
QTextDocument *TextDocumentConverter::convert( const QString & )
{
return 0;
}
Document::OpenResult TextDocumentConverter::convertWithPassword( const QString &fileName, const QString & )
{
QTextDocument *doc = convert( fileName );
setDocument( doc );
return doc != 0 ? Document::OpenSuccess : Document::OpenError;
}
QTextDocument *TextDocumentConverter::document()
{
return d_ptr->mDocument;
}
void TextDocumentConverter::setDocument( QTextDocument *document )
{
d_ptr->mDocument = document;
}
DocumentViewport TextDocumentConverter::calculateViewport( QTextDocument *document, const QTextBlock &block )
{
return TextDocumentUtils::calculateViewport( document, block );
@ -271,13 +293,15 @@ TextDocumentGenerator::~TextDocumentGenerator()
{
}
bool TextDocumentGenerator::loadDocument( const QString & fileName, QVector<Okular::Page*> & pagesVector )
Document::OpenResult TextDocumentGenerator::loadDocumentWithPassword( const QString & fileName, QVector<Okular::Page*> & pagesVector, const QString &password )
{
Q_D( TextDocumentGenerator );
d->mDocument = d->mConverter->convert( fileName );
const Document::OpenResult openResult = d->mConverter->convertWithPassword( fileName, password );
if ( !d->mDocument )
if ( openResult != Document::OpenSuccess )
{
d->mDocument = 0;
// loading failed, cleanup all the stuff eventually gathered from the converter
d->mTitlePositions.clear();
Q_FOREACH ( const TextDocumentGeneratorPrivate::LinkPosition &linkPos, d->mLinkPositions )
@ -291,8 +315,9 @@ bool TextDocumentGenerator::loadDocument( const QString & fileName, QVector<Okul
}
d->mAnnotationPositions.clear();
return false;
return openResult;
}
d->mDocument = d->mConverter->document();
d->generateTitleInfos();
d->generateLinkInfos();
@ -334,7 +359,7 @@ bool TextDocumentGenerator::loadDocument( const QString & fileName, QVector<Okul
}
}
return true;
return openResult;
}
bool TextDocumentGenerator::doCloseDocument()

View file

@ -46,8 +46,20 @@ class OKULAR_EXPORT TextDocumentConverter : public QObject
/**
* Returns the generated QTextDocument object.
*
* @note there is no need to implement this one if you implement convertWithPassword
*/
virtual QTextDocument *convert( const QString &fileName ) = 0;
virtual QTextDocument *convert( const QString &fileName );
/**
* Returns the generated QTextDocument object.
*/
virtual Document::OpenResult convertWithPassword( const QString &fileName, const QString &password );
/**
* Returns the generated QTextDocument object. Will be null if convert didn't succeed
*/
QTextDocument *document();
Q_SIGNALS:
/**
@ -104,6 +116,11 @@ class OKULAR_EXPORT TextDocumentConverter : public QObject
void notice( const QString &message, int duration );
protected:
/**
* Sets the converted QTextDocument object.
*/
void setDocument( QTextDocument *document );
/**
* This method can be used to calculate the viewport for a given text block.
*
@ -165,7 +182,7 @@ class OKULAR_EXPORT TextDocumentGenerator : public Generator, public Okular::Con
virtual ~TextDocumentGenerator();
// [INHERITED] load a document and fill up the pagesVector
bool loadDocument( const QString & fileName, QVector<Okular::Page*> & pagesVector );
Document::OpenResult loadDocumentWithPassword( const QString & fileName, QVector<Okular::Page*> & pagesVector, const QString &password );
// [INHERITED] perform actions on document / pages
bool canGeneratePixmap() const;

View file

@ -106,6 +106,7 @@ class TextDocumentConverterPrivate
}
TextDocumentGeneratorPrivate *mParent;
QTextDocument *mDocument;
};
class TextDocumentGeneratorPrivate : public GeneratorPrivate

View file

@ -72,12 +72,13 @@ Converter::~Converter()
{
}
QTextDocument* Converter::convert( const QString &fileName )
Okular::Document::OpenResult Converter::convertWithPassword( const QString &fileName, const QString &password )
{
Document oooDocument( fileName );
if ( !oooDocument.open() ) {
emit error( oooDocument.lastErrorString(), -1 );
return 0;
if ( !oooDocument.open( password ) ) {
if ( !oooDocument.anyFileEncrypted() )
emit error( oooDocument.lastErrorString(), -1 );
return oooDocument.anyFileEncrypted() ? Okular::Document::OpenNeedsPassword : Okular::Document::OpenError;
}
mTextDocument = new QTextDocument;
@ -94,9 +95,10 @@ QTextDocument* Converter::convert( const QString &fileName )
QString errorMsg;
QDomDocument document;
if ( !document.setContent( &source, &reader, &errorMsg ) ) {
emit error( i18n( "Invalid XML document: %1", errorMsg ), -1 );
if ( !oooDocument.anyFileEncrypted() )
emit error( i18n( "Invalid XML document: %1", errorMsg ), -1 );
delete mCursor;
return 0;
return oooDocument.anyFileEncrypted() ? Okular::Document::OpenNeedsPassword : Okular::Document::OpenError;
}
mStyleInformation = new StyleInformation();
@ -107,9 +109,10 @@ QTextDocument* Converter::convert( const QString &fileName )
*/
StyleParser styleParser( &oooDocument, document, mStyleInformation );
if ( !styleParser.parse() ) {
emit error( i18n( "Unable to read style information" ), -1 );
if ( !oooDocument.anyFileEncrypted() )
emit error( i18n( "Unable to read style information" ), -1 );
delete mCursor;
return 0;
return oooDocument.anyFileEncrypted() ? Okular::Document::OpenNeedsPassword : Okular::Document::OpenError;
}
/**
@ -154,9 +157,10 @@ QTextDocument* Converter::convert( const QString &fileName )
while ( !element.isNull() ) {
if ( element.tagName() == QLatin1String( "body" ) ) {
if ( !convertBody( element ) ) {
emit error( i18n( "Unable to convert document content" ), -1 );
if ( !oooDocument.anyFileEncrypted() )
emit error( i18n( "Unable to convert document content" ), -1 );
delete mCursor;
return 0;
return oooDocument.anyFileEncrypted() ? Okular::Document::OpenNeedsPassword : Okular::Document::OpenError;
}
}
@ -174,7 +178,8 @@ QTextDocument* Converter::convert( const QString &fileName )
delete mStyleInformation;
mStyleInformation = 0;
return mTextDocument;
setDocument( mTextDocument );
return Okular::Document::OpenSuccess;
}
bool Converter::convertBody( const QDomElement &element )

View file

@ -30,7 +30,7 @@ class Converter : public Okular::TextDocumentConverter
Converter();
~Converter();
virtual QTextDocument *convert( const QString &fileName );
virtual Okular::Document::OpenResult convertWithPassword( const QString &fileName, const QString &password );
private:
bool convertBody( const QDomElement &element );

View file

@ -15,11 +15,11 @@
using namespace OOO;
Document::Document( const QString &fileName )
: mFileName( fileName ), mManifest( 0 )
: mFileName( fileName ), mManifest( 0 ), mAnyEncrypted( false )
{
}
bool Document::open()
bool Document::open( const QString &password )
{
mContent.clear();
mStyles.clear();
@ -48,7 +48,7 @@ bool Document::open()
}
const KArchiveFile *file = static_cast<const KArchiveFile*>( metaInfDirectory->entry( "manifest.xml" ) );
mManifest = new Manifest( mFileName, file->data() );
mManifest = new Manifest( mFileName, file->data(), password );
// we should really get the file names from the manifest, but for now, we only care
// if the manifest says the files are encrypted.
@ -60,6 +60,7 @@ bool Document::open()
file = static_cast<const KArchiveFile*>( directory->entry( "content.xml" ) );
if ( mManifest->testIfEncrypted( "content.xml" ) ) {
mAnyEncrypted = true;
mContent = mManifest->decryptFile( "content.xml", file->data() );
} else {
mContent = file->data();
@ -68,6 +69,7 @@ bool Document::open()
if ( entries.contains( "styles.xml" ) ) {
file = static_cast<const KArchiveFile*>( directory->entry( "styles.xml" ) );
if ( mManifest->testIfEncrypted( "styles.xml" ) ) {
mAnyEncrypted = true;
mStyles = mManifest->decryptFile( "styles.xml", file->data() );
} else {
mStyles = file->data();
@ -77,6 +79,7 @@ bool Document::open()
if ( entries.contains( "meta.xml" ) ) {
file = static_cast<const KArchiveFile*>( directory->entry( "meta.xml" ) );
if ( mManifest->testIfEncrypted( "meta.xml" ) ) {
mAnyEncrypted = true;
mMeta = mManifest->decryptFile( "meta.xml", file->data() );
} else {
mMeta = file->data();
@ -91,6 +94,7 @@ bool Document::open()
file = static_cast<const KArchiveFile*>( imagesDirectory->entry( imagesEntries[ i ] ) );
QString fullPath = QString( "Pictures/%1" ).arg( imagesEntries[ i ] );
if ( mManifest->testIfEncrypted( fullPath ) ) {
mAnyEncrypted = true;
mImages.insert( fullPath, mManifest->decryptFile( fullPath, file->data() ) );
} else {
mImages.insert( fullPath, file->data() );
@ -133,6 +137,11 @@ QMap<QString, QByteArray> Document::images() const
return mImages;
}
bool Document::anyFileEncrypted() const
{
return mAnyEncrypted;
}
void Document::setError( const QString &error )
{
mErrorString = error;

View file

@ -24,7 +24,7 @@ class Document
Document( const QString &fileName );
~Document();
bool open();
bool open( const QString &password );
QString lastErrorString() const;
@ -32,6 +32,7 @@ class Document
QByteArray meta() const;
QByteArray styles() const;
QMap<QString, QByteArray> images() const;
bool anyFileEncrypted() const;
private:
void setError( const QString& );
@ -43,6 +44,7 @@ class Document
QMap<QString, QByteArray> mImages;
Manifest *mManifest;
QString mErrorString;
bool mAnyEncrypted;
};
}

View file

@ -14,6 +14,7 @@
#include <kaboutdata.h>
#include <klocale.h>
#include <kconfigdialog.h>
#include <kwallet.h>
static KAboutData createAboutData()
{
@ -44,3 +45,10 @@ void KOOOGenerator::addPages( KConfigDialog* dlg )
dlg->addPage( widget, generalSettings(), i18n("OpenDocument Text"), "application-vnd.oasis.opendocument.text", i18n("OpenDocument Text Backend Configuration") );
}
void KOOOGenerator::walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const
{
*walletKey = fileName + "/opendocument";
*walletName = KWallet::Wallet::LocalWallet();
*walletFolder = KWallet::Wallet::PasswordFolder();
}

View file

@ -19,6 +19,8 @@ class KOOOGenerator : public Okular::TextDocumentGenerator
// [INHERITED] reparse configuration
void addPages( KConfigDialog* dlg );
virtual void walletDataForFile( const QString &fileName, QString *walletName, QString *walletFolder, QString *walletKey ) const;
};
#endif

View file

@ -16,8 +16,6 @@
#include <KFilterDev>
#include <KLocale>
#include <KMessageBox>
#include <KPasswordDialog>
#include <KWallet/Wallet>
using namespace OOO;
@ -125,8 +123,8 @@ QByteArray ManifestEntry::salt() const
//---------------------------------------------------------------------
Manifest::Manifest( const QString &odfFileName, const QByteArray &manifestData )
: m_odfFileName( odfFileName ), m_haveGoodPassword( false ), m_userCancelled( false )
Manifest::Manifest( const QString &odfFileName, const QByteArray &manifestData, const QString &password )
: m_odfFileName( odfFileName ), m_haveGoodPassword( false ), m_password( password )
{
// I don't know why the parser barfs on this.
QByteArray manifestCopy = manifestData;
@ -212,8 +210,6 @@ Manifest::Manifest( const QString &odfFileName, const QByteArray &manifestData )
Manifest::~Manifest()
{
savePasswordToWallet();
qDeleteAll( mEntries );
}
@ -233,88 +229,6 @@ bool Manifest::testIfEncrypted( const QString &filename )
return false;
}
void Manifest::getPasswordFromUser()
{
// TODO: This should have a proper parent
KPasswordDialog dlg( 0, KPasswordDialog::KPasswordDialogFlags() );
dlg.setCaption( i18n( "Document Password" ) );
dlg.setPrompt( i18n( "Please enter the password to read the document:" ) );
if( ! dlg.exec() ) {
// user cancel
m_userCancelled = true;
} else {
m_password = dlg.password();
}
}
void Manifest::getPasswordFromWallet()
{
if ( KWallet::Wallet::folderDoesNotExist( KWallet::Wallet::LocalWallet(), KWallet::Wallet::PasswordFolder() ) ) {
return;
}
if ( m_odfFileName.isEmpty() ) {
return;
}
// This naming is consistent with how KOffice does it.
QString entryKey = m_odfFileName + "/opendocument";
if ( KWallet::Wallet::keyDoesNotExist( KWallet::Wallet::LocalWallet(), KWallet::Wallet::PasswordFolder(), entryKey ) ) {
return;
}
// TODO: this should have a proper parent. I can't see a way to get one though...
KWallet::Wallet *wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
if ( ! wallet ) {
return;
}
if ( ! wallet->setFolder( KWallet::Wallet::PasswordFolder() ) ) {
delete wallet;
return;
}
wallet->readPassword( entryKey, m_password );
delete wallet;
}
void Manifest::savePasswordToWallet()
{
if ( ! m_haveGoodPassword ) {
return;
}
if ( m_odfFileName.isEmpty() ) {
return;
}
// TODO: this should have a proper parent. I can't see a way to get one though...
KWallet::Wallet *wallet = KWallet::Wallet::openWallet( KWallet::Wallet::LocalWallet(), 0 );
if ( ! wallet ) {
return;
}
if ( ! wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) ) {
wallet->createFolder( KWallet::Wallet::PasswordFolder() );
}
if ( ! wallet->setFolder( KWallet::Wallet::PasswordFolder() ) ) {
delete wallet;
return;
}
// This naming is consistent with how KOffice does it.
QString entryKey = m_odfFileName + "/opendocument";
if ( wallet->hasEntry( entryKey ) ) {
wallet->removeEntry( entryKey );
}
wallet->writePassword( entryKey, m_password );
delete wallet;
}
void Manifest::checkPassword( ManifestEntry *entry, const QByteArray &fileData, QByteArray *decryptedData )
{
#ifdef QCA2
@ -373,52 +287,26 @@ QByteArray Manifest::decryptFile( const QString &filename, const QByteArray &fil
return QByteArray( fileData );
}
if (m_userCancelled) {
QByteArray decryptedData;
checkPassword( entry, fileData, &decryptedData );
if (! m_haveGoodPassword ) {
return QByteArray();
}
QByteArray decryptedData;
if (! m_haveGoodPassword ) {
getPasswordFromWallet();
checkPassword( entry, fileData, &decryptedData );
QIODevice *decompresserDevice = KFilterDev::device( new QBuffer( &decryptedData, 0 ), "application/x-gzip", true );
if( !decompresserDevice ) {
kDebug(OooDebug) << "Couldn't create decompressor";
// hopefully it isn't compressed then!
return QByteArray( fileData );
}
do {
if (! m_haveGoodPassword ) {
getPasswordFromUser();
}
static_cast<KFilterDev*>( decompresserDevice )->setSkipHeaders( );
if (m_userCancelled) {
return QByteArray();
}
decompresserDevice->open( QIODevice::ReadOnly );
checkPassword( entry, fileData, &decryptedData );
if ( !m_haveGoodPassword ) {
KMessageBox::information( 0, i18n("The password is not correct."), i18n("Incorrect password") );
} else {
// kDebug(OooDebug) << "Have good password";
}
return decompresserDevice->readAll();
} while ( ( ! m_haveGoodPassword ) && ( ! m_userCancelled ) );
if ( m_haveGoodPassword ) {
QIODevice *decompresserDevice = KFilterDev::device( new QBuffer( &decryptedData, 0 ), "application/x-gzip", true );
if( !decompresserDevice ) {
kDebug(OooDebug) << "Couldn't create decompressor";
// hopefully it isn't compressed then!
return QByteArray( fileData );
}
static_cast<KFilterDev*>( decompresserDevice )->setSkipHeaders( );
decompresserDevice->open( QIODevice::ReadOnly );
return decompresserDevice->readAll();
} else {
return QByteArray( fileData );
}
#else
// TODO: This should have a proper parent
KMessageBox::error( 0, i18n("This document is encrypted, but Okular was compiled without crypto support. This document will probably not open.") );

View file

@ -205,8 +205,9 @@ class Manifest
\param odfFileName the name of the parent ODF file
\param manifestData the data from the manifest file, as a byte array
of the XML format data.
\param password to decrypt the file
*/
Manifest( const QString &odfFileName, const QByteArray &manifestData );
Manifest( const QString &odfFileName, const QByteArray &manifestData, const QString &password );
~Manifest();
@ -226,16 +227,6 @@ class Manifest
*/
ManifestEntry* entryByName( const QString &filename );
/**
Try to get the password for this file from the wallet
*/
void getPasswordFromWallet();
/**
Save the password for this file to the wallet
*/
void savePasswordToWallet();
/**
Verify whether the password we have is the right one.
@ -243,18 +234,12 @@ class Manifest
*/
void checkPassword( ManifestEntry *entry, const QByteArray &fileData, QByteArray *decryptedData );
/**
Ask the user for a password
*/
void getPasswordFromUser();
#ifdef QCA2
QCA::Initializer m_init;
#endif
const QString m_odfFileName;
QMap<QString, ManifestEntry*> mEntries;
bool m_haveGoodPassword;
bool m_userCancelled;
QString m_password;
};

View file

@ -1261,7 +1261,8 @@ bool Part::openFile()
}
// if the file didn't open correctly it might be encrypted, so ask for a pass
const QString walletKey = fileNameToOpen.section('/', -1, -1);
QString walletName, walletFolder, walletKey;
m_document->walletDataForFile(fileNameToOpen, &walletName, &walletFolder, &walletKey);
bool firstInput = true;
bool triedWallet = false;
KWallet::Wallet * wallet = 0;
@ -1273,15 +1274,14 @@ bool Part::openFile()
// 1.A. try to retrieve the first password from the kde wallet system
if ( !triedWallet && !walletKey.isNull() )
{
QString walletName = KWallet::Wallet::NetworkWallet();
const WId parentwid = widget()->effectiveWinId();
wallet = KWallet::Wallet::openWallet( walletName, parentwid );
if ( wallet )
{
// use the KPdf folder (and create if missing)
if ( !wallet->hasFolder( "KPdf" ) )
wallet->createFolder( "KPdf" );
wallet->setFolder( "KPdf" );
if ( !wallet->hasFolder( walletFolder ) )
wallet->createFolder( walletFolder );
wallet->setFolder( walletFolder );
// look for the pass in that folder
QString retrievedPass;