2006-09-13 11:14:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "document.h"
|
|
|
|
|
2014-10-08 11:37:04 +00:00
|
|
|
#include <KLocalizedString>
|
2006-09-13 11:14:10 +00:00
|
|
|
#include <kzip.h>
|
|
|
|
|
|
|
|
using namespace OOO;
|
|
|
|
|
|
|
|
Document::Document( const QString &fileName )
|
2017-09-05 21:27:18 +00:00
|
|
|
: mFileName( fileName ), mManifest( nullptr ), mAnyEncrypted( false )
|
2006-09-13 11:14:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-10 13:31:59 +00:00
|
|
|
bool Document::open( const QString &password )
|
2006-09-13 11:14:10 +00:00
|
|
|
{
|
|
|
|
mContent.clear();
|
|
|
|
mStyles.clear();
|
|
|
|
|
|
|
|
KZip zip( mFileName );
|
|
|
|
if ( !zip.open( QIODevice::ReadOnly ) ) {
|
2007-01-29 08:17:45 +00:00
|
|
|
setError( i18n( "Document is not a valid ZIP archive" ) );
|
2006-09-13 11:14:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KArchiveDirectory *directory = zip.directory();
|
|
|
|
if ( !directory ) {
|
2007-01-29 08:17:45 +00:00
|
|
|
setError( i18n( "Invalid document structure (main directory is missing)" ) );
|
2006-09-13 11:14:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList entries = directory->entries();
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( !entries.contains( QStringLiteral("META-INF") ) ) {
|
2008-02-14 07:35:07 +00:00
|
|
|
setError( i18n( "Invalid document structure (META-INF directory is missing)" ) );
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-29 12:37:11 +00:00
|
|
|
const KArchiveDirectory *metaInfDirectory = static_cast<const KArchiveDirectory*>( directory->entry( QStringLiteral("META-INF") ) );
|
|
|
|
if ( !(metaInfDirectory->entries().contains( QStringLiteral("manifest.xml") ) ) ) {
|
2008-02-14 07:35:07 +00:00
|
|
|
setError( i18n( "Invalid document structure (META-INF/manifest.xml is missing)" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
const KArchiveFile *file = static_cast<const KArchiveFile*>( metaInfDirectory->entry( QStringLiteral("manifest.xml") ) );
|
2014-05-10 13:31:59 +00:00
|
|
|
mManifest = new Manifest( mFileName, file->data(), password );
|
2008-02-14 07:35:07 +00:00
|
|
|
|
|
|
|
// we should really get the file names from the manifest, but for now, we only care
|
|
|
|
// if the manifest says the files are encrypted.
|
2009-06-05 17:45:59 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( !entries.contains( QStringLiteral("content.xml") ) ) {
|
2007-01-29 08:17:45 +00:00
|
|
|
setError( i18n( "Invalid document structure (content.xml is missing)" ) );
|
2006-09-13 11:14:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
file = static_cast<const KArchiveFile*>( directory->entry( QStringLiteral("content.xml") ) );
|
|
|
|
if ( mManifest->testIfEncrypted( QStringLiteral("content.xml") ) ) {
|
2014-05-10 13:31:59 +00:00
|
|
|
mAnyEncrypted = true;
|
2015-10-29 12:37:11 +00:00
|
|
|
mContent = mManifest->decryptFile( QStringLiteral("content.xml"), file->data() );
|
2008-02-14 07:35:07 +00:00
|
|
|
} else {
|
|
|
|
mContent = file->data();
|
|
|
|
}
|
2006-09-13 11:14:10 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( entries.contains( QStringLiteral("styles.xml") ) ) {
|
|
|
|
file = static_cast<const KArchiveFile*>( directory->entry( QStringLiteral("styles.xml") ) );
|
|
|
|
if ( mManifest->testIfEncrypted( QStringLiteral("styles.xml") ) ) {
|
2014-05-10 13:31:59 +00:00
|
|
|
mAnyEncrypted = true;
|
2015-10-29 12:37:11 +00:00
|
|
|
mStyles = mManifest->decryptFile( QStringLiteral("styles.xml"), file->data() );
|
2013-12-15 23:18:03 +00:00
|
|
|
} else {
|
|
|
|
mStyles = file->data();
|
|
|
|
}
|
2008-02-14 07:35:07 +00:00
|
|
|
}
|
2006-09-13 11:14:10 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( entries.contains( QStringLiteral("meta.xml") ) ) {
|
|
|
|
file = static_cast<const KArchiveFile*>( directory->entry( QStringLiteral("meta.xml") ) );
|
|
|
|
if ( mManifest->testIfEncrypted( QStringLiteral("meta.xml") ) ) {
|
2014-05-10 13:31:59 +00:00
|
|
|
mAnyEncrypted = true;
|
2015-10-29 12:37:11 +00:00
|
|
|
mMeta = mManifest->decryptFile( QStringLiteral("meta.xml"), file->data() );
|
2013-12-15 23:18:03 +00:00
|
|
|
} else {
|
|
|
|
mMeta = file->data();
|
|
|
|
}
|
2008-02-14 07:35:07 +00:00
|
|
|
}
|
2006-09-13 11:14:10 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
if ( entries.contains( QStringLiteral("Pictures") ) ) {
|
|
|
|
const KArchiveDirectory *imagesDirectory = static_cast<const KArchiveDirectory*>( directory->entry( QStringLiteral("Pictures") ) );
|
2006-09-16 16:17:31 +00:00
|
|
|
|
|
|
|
const QStringList imagesEntries = imagesDirectory->entries();
|
|
|
|
for ( int i = 0; i < imagesEntries.count(); ++i ) {
|
|
|
|
file = static_cast<const KArchiveFile*>( imagesDirectory->entry( imagesEntries[ i ] ) );
|
2015-10-29 12:37:11 +00:00
|
|
|
QString fullPath = QStringLiteral( "Pictures/%1" ).arg( imagesEntries[ i ] );
|
2008-02-14 07:35:07 +00:00
|
|
|
if ( mManifest->testIfEncrypted( fullPath ) ) {
|
2014-05-10 13:31:59 +00:00
|
|
|
mAnyEncrypted = true;
|
2009-06-05 17:45:59 +00:00
|
|
|
mImages.insert( fullPath, mManifest->decryptFile( fullPath, file->data() ) );
|
2008-02-14 07:35:07 +00:00
|
|
|
} else {
|
2009-06-05 17:45:59 +00:00
|
|
|
mImages.insert( fullPath, file->data() );
|
2008-02-14 07:35:07 +00:00
|
|
|
}
|
2006-09-16 16:17:31 +00:00
|
|
|
}
|
2006-09-16 15:52:00 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 11:14:10 +00:00
|
|
|
zip.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-14 07:35:07 +00:00
|
|
|
Document::~Document()
|
|
|
|
{
|
|
|
|
delete mManifest;
|
|
|
|
}
|
|
|
|
|
2007-01-29 08:17:45 +00:00
|
|
|
QString Document::lastErrorString() const
|
|
|
|
{
|
|
|
|
return mErrorString;
|
|
|
|
}
|
|
|
|
|
2006-09-13 11:14:10 +00:00
|
|
|
QByteArray Document::content() const
|
|
|
|
{
|
|
|
|
return mContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Document::meta() const
|
|
|
|
{
|
|
|
|
return mMeta;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Document::styles() const
|
|
|
|
{
|
|
|
|
return mStyles;
|
|
|
|
}
|
2006-09-16 15:52:00 +00:00
|
|
|
|
|
|
|
QMap<QString, QByteArray> Document::images() const
|
|
|
|
{
|
|
|
|
return mImages;
|
|
|
|
}
|
2007-01-29 08:17:45 +00:00
|
|
|
|
2014-05-10 13:31:59 +00:00
|
|
|
bool Document::anyFileEncrypted() const
|
|
|
|
{
|
|
|
|
return mAnyEncrypted;
|
|
|
|
}
|
|
|
|
|
2007-01-29 08:17:45 +00:00
|
|
|
void Document::setError( const QString &error )
|
|
|
|
{
|
|
|
|
mErrorString = error;
|
|
|
|
}
|