diff --git a/generators/comicbook/document.cpp b/generators/comicbook/document.cpp index 82a7a77b7..d2162cd78 100644 --- a/generators/comicbook/document.cpp +++ b/generators/comicbook/document.cpp @@ -37,7 +37,7 @@ static void imagesInArchive( const QString &prefix, const KArchiveDirectory* dir Document::Document() - : mUnrar( 0 ), mZip( 0 ) + : mUnrar( 0 ), mArchive( 0 ) { } @@ -55,30 +55,11 @@ bool Document::open( const QString &fileName ) * We have a zip archive */ if ( mime->is( "application/x-cbz" ) || mime->name() == "application/zip" ) { - mZip = new KZip( fileName ); - - if ( !mZip->open( QIODevice::ReadOnly ) ) { - delete mZip; - mZip = 0; - - return false; - } - - const KArchiveDirectory *directory = mZip->directory(); - if ( !directory ) { - delete mZip; - mZip = 0; + mArchive = new KZip( fileName ); + if ( !processArchive() ) { return false; } - - mZipDir = const_cast( directory ); - - QStringList entries; - imagesInArchive( QString(), mZipDir, &entries ); - - extractImageFiles( entries ); - } else if ( mime->is( "application/x-cbr" ) || mime->name() == "application/x-rar" ) { if ( !Unrar::isAvailable() ) { mLastErrorString = i18n( "Cannot open document, unrar was not found." ); @@ -115,16 +96,41 @@ void Document::close() { mLastErrorString.clear(); - if ( !( mZip || mUnrar ) ) + if ( !( mArchive || mUnrar ) ) return; - delete mZip; - mZip = 0; + delete mArchive; + mArchive = 0; delete mUnrar; mUnrar = 0; mPageMap.clear(); } +bool Document::processArchive() { + if ( !mArchive->open( QIODevice::ReadOnly ) ) { + delete mArchive; + mArchive = 0; + + return false; + } + + const KArchiveDirectory *directory = mArchive->directory(); + if ( !directory ) { + delete mArchive; + mArchive = 0; + + return false; + } + + mArchiveDir = const_cast( directory ); + + QStringList entries; + imagesInArchive( QString(), mArchiveDir, &entries ); + + extractImageFiles( entries ); + return true; +} + void Document::extractImageFiles( const QStringList &list ) { QStringList files( list ); @@ -154,8 +160,8 @@ QStringList Document::pageTitles() const QImage Document::pageImage( int page ) const { - if ( mZip ) { - const KArchiveFile *entry = static_cast( mZipDir->entry( mPageMap[ page ] ) ); + if ( mArchive ) { + const KArchiveFile *entry = static_cast( mArchiveDir->entry( mPageMap[ page ] ) ); if ( entry ) return QImage::fromData( entry->data() ); @@ -170,8 +176,8 @@ QSize Document::pageSize( int page ) const { std::auto_ptr< QIODevice > dev; - if ( mZip ) { - const KArchiveFile *entry = static_cast( mZipDir->entry( mPageMap[ page ] ) ); + if ( mArchive ) { + const KArchiveFile *entry = static_cast( mArchiveDir->entry( mPageMap[ page ] ) ); if ( entry ) { dev.reset( entry->createDevice() ); } diff --git a/generators/comicbook/document.h b/generators/comicbook/document.h index 1120055b6..0d9b220cf 100644 --- a/generators/comicbook/document.h +++ b/generators/comicbook/document.h @@ -13,7 +13,7 @@ #include class KArchiveDirectory; -class KZip; +class KArchive; class QImage; class QSize; class Unrar; @@ -39,11 +39,12 @@ class Document private: void extractImageFiles( const QStringList& ); + bool processArchive(); QStringList mPageMap; Unrar *mUnrar; - KZip *mZip; - KArchiveDirectory *mZipDir; + KArchive *mArchive; + KArchiveDirectory *mArchiveDir; QString mLastErrorString; };