Commit 4aa3ebe4 authored by David Palacio's avatar David Palacio

Add support for reading a directory as a comicbook.

REVIEW: 6667

svn path=/trunk/KDE/kdegraphics/okular/; revision=1230519
parent d473477a
......@@ -8,6 +8,7 @@ include_directories(
set( okularGenerator_comicbook_PART_SRCS
document.cpp
generator_comicbook.cpp
directory.cpp
unrar.cpp qnatsort.cpp
unrarflavours.cpp
)
......
/***************************************************************************
* Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
* Copyright (C) 2011 by David Palacio <dpalacio@orbitalibre.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. *
***************************************************************************/
#include "directory.h"
#include <QtCore/QDir>
#include <QtCore/QDirIterator>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <memory>
Directory::Directory()
{
}
Directory::~Directory()
{
}
bool Directory::open( const QString &dirName )
{
mDir = dirName;
QFileInfo dirTest( dirName );
return dirTest.isDir() && dirTest.isReadable();
}
QStringList Directory::recurseDir( const QString &dirPath, int curDepth ) const
{
QDir dir( dirPath );
dir.setFilter( QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot );
QStringList fileList;
QDirIterator it( dir );
QFileInfo info;
while( it.hasNext() ) {
it.next();
info = it.fileInfo();
if ( info.isDir() && curDepth < staticMaxDepth ) {
fileList.append( recurseDir( info.filePath(), curDepth + 1 ) );
} else if ( info.isFile() ) {
fileList.append( info.filePath() );
}
}
return fileList;
}
QStringList Directory::list() const
{
return recurseDir( mDir, 0 );
}
QIODevice* Directory::createDevice( const QString &path ) const
{
std::auto_ptr<QFile> file( new QFile( path ) );
if ( !file->open( QIODevice::ReadOnly ) )
return 0;
return file.release();
}
/***************************************************************************
* Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
* Copyright (C) 2011 by David Palacio <dpalacio@orbitalibre.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. *
***************************************************************************/
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include <QtCore/QStringList>
class Directory
{
public:
/**
* Creates a new directory object.
*/
Directory();
/**
* Destroys the directory object.
*/
~Directory();
/**
* Opens given directory.
*/
bool open( const QString &fileName );
/**
* Returns the list of files from the directory.
*/
QStringList list() const;
/**
* Returns a new device for reading the file with the given path.
*/
QIODevice* createDevice( const QString &path ) const;
private:
/**
* Iterates over a directory and returns a file list.
*/
QStringList recurseDir( const QString &dir, int curDepth ) const;
static const int staticMaxDepth = 1;
QString mDir;
};
#endif
......@@ -20,6 +20,7 @@
#include <memory>
#include "unrar.h"
#include "directory.h"
#include "qnatsort.h"
using namespace ComicBook;
......@@ -38,7 +39,7 @@ static void imagesInArchive( const QString &prefix, const KArchiveDirectory* dir
Document::Document()
: mUnrar( 0 ), mArchive( 0 )
: mDirectory( 0 ), mUnrar( 0 ), mArchive( 0 )
{
}
......@@ -95,6 +96,17 @@ bool Document::open( const QString &fileName )
}
extractImageFiles( mUnrar->list() );
} else if ( mime->is( "inode/directory" ) ) {
mDirectory = new Directory();
if ( !mDirectory->open( fileName ) ) {
delete mDirectory;
mDirectory = 0;
return false;
}
extractImageFiles( mDirectory->list() );
} else {
mLastErrorString = i18n( "Unknown ComicBook format." );
return false;
......@@ -107,11 +119,13 @@ void Document::close()
{
mLastErrorString.clear();
if ( !( mArchive || mUnrar ) )
if ( !( mArchive || mUnrar || mDirectory ) )
return;
delete mArchive;
mArchive = 0;
delete mDirectory;
mDirectory = 0;
delete mUnrar;
mUnrar = 0;
mPageMap.clear();
......@@ -177,7 +191,8 @@ QImage Document::pageImage( int page ) const
const KArchiveFile *entry = static_cast<const KArchiveFile*>( mArchiveDir->entry( mPageMap[ page ] ) );
if ( entry )
return QImage::fromData( entry->data() );
} else if ( mDirectory ) {
return QImage( mPageMap[ page ] );
} else {
return QImage::fromData( mUnrar->contentOf( mPageMap[ page ] ) );
}
......@@ -195,6 +210,8 @@ QSize Document::pageSize( int page ) const
dev.reset( entry->createDevice() );
}
} else if ( mDirectory ) {
dev.reset( mDirectory->createDevice( mPageMap[ page ] ) );
} else {
dev.reset( mUnrar->createDevice( mPageMap[ page ] ) );
}
......@@ -217,3 +234,4 @@ QString Document::lastErrorString() const
{
return mLastErrorString;
}
......@@ -17,6 +17,7 @@ class KArchive;
class QImage;
class QSize;
class Unrar;
class Directory;
namespace ComicBook {
......@@ -42,6 +43,7 @@ class Document
bool processArchive();
QStringList mPageMap;
Directory *mDirectory;
Unrar *mUnrar;
KArchive *mArchive;
KArchiveDirectory *mArchiveDir;
......
......@@ -109,7 +109,7 @@ Comment[x-test]=xxComic book backend for Okularxx
Comment[zh_CN]=Okular 的 Comic book 格式后端
Comment[zh_TW]=Okular 的 Comic book 後端
X-KDE-ServiceTypes=okular/Generator
MimeType=application/x-cbz;application/x-cbr;application/x-cbt;
MimeType=application/x-cbz;application/x-cbr;application/x-cbt;inode/directory;
X-KDE-Library=okularGenerator_comicbook
X-KDE-Priority=1
X-KDE-okularAPIVersion=1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment