Generalize the code for KArchive support instead of using KZip directly. This will be helpful in

adding other supported formats, such as Tar. (Review ticket: 2181)


svn path=/trunk/KDE/kdegraphics/okular/; revision=1072399
remotes/origin/KDE/4.5
Harsh Chouraria J 16 years ago
parent 24b1b33906
commit f5bbebd879
  1. 64
      generators/comicbook/document.cpp
  2. 7
      generators/comicbook/document.h

@ -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<KArchiveDirectory*>( 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<KArchiveDirectory*>( 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<const KArchiveFile*>( mZipDir->entry( mPageMap[ page ] ) );
if ( mArchive ) {
const KArchiveFile *entry = static_cast<const KArchiveFile*>( 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<const KArchiveFile*>( mZipDir->entry( mPageMap[ page ] ) );
if ( mArchive ) {
const KArchiveFile *entry = static_cast<const KArchiveFile*>( mArchiveDir->entry( mPageMap[ page ] ) );
if ( entry ) {
dev.reset( entry->createDevice() );
}

@ -13,7 +13,7 @@
#include <QtCore/QStringList>
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;
};

Loading…
Cancel
Save