Commit 3c64c323 authored by Pino Toscano's avatar Pino Toscano

Start adding a minimal way to handle the different flavours of unrar.

For now, deny the usage of the "unrar-free", as it does not seem to be much usable...

svn path=/trunk/KDE/kdegraphics/okular/; revision=754056
parent 24e03633
......@@ -9,6 +9,7 @@ set( okularGenerator_comicbook_PART_SRCS
document.cpp
generator_comicbook.cpp
unrar.cpp qnatsort.cpp
unrarflavours.cpp
)
......
......@@ -70,6 +70,11 @@ bool Document::open( const QString &fileName )
extractImageFiles( entries );
} else {
if ( !Unrar::isAvailable() ) {
// TODO emit a visible error
return false;
}
/**
* We have a rar archive
*/
......
......@@ -11,9 +11,54 @@
#include <QtCore/QFile>
#include <QtCore/QProcess>
#include <QtCore/QRegExp>
#include <kdebug.h>
#include <kglobal.h>
#include <ktempdir.h>
#include "unrarflavours.h"
struct UnrarHelper
{
UnrarHelper();
~UnrarHelper();
UnrarFlavour *kind;
};
K_GLOBAL_STATIC( UnrarHelper, helper )
UnrarHelper::UnrarHelper()
: kind( 0 )
{
QProcess proc;
proc.start( "unrar", QStringList() << "--version" );
bool ok = proc.waitForFinished( -1 );
Q_UNUSED( ok )
const QStringList lines = QString::fromLocal8Bit( proc.readAllStandardOutput() ).split( "\n", QString::SkipEmptyParts );
if ( !lines.isEmpty() )
{
if ( lines.first().startsWith( "UNRAR " ) )
kind = new NonFreeUnrarFlavour();
else if ( lines.first().startsWith( "unrar " ) )
kind = new FreeUnrarFlavour();
}
if ( !kind )
{
// no luck so far, assume unrar-nonfree
kind = new NonFreeUnrarFlavour();
}
kDebug() << "detected:" << kind->name();
}
UnrarHelper::~UnrarHelper()
{
delete kind;
}
Unrar::Unrar()
: QObject( 0 ), mTempDir( 0 )
{
......@@ -67,7 +112,7 @@ QStringList Unrar::list()
delete mProcess;
mProcess = 0;
return QString::fromLocal8Bit( mStdOutData ).split( "\n", QString::SkipEmptyParts );
return helper->kind->processListing( QString::fromLocal8Bit( mStdOutData ).split( "\n", QString::SkipEmptyParts ) );
}
QByteArray Unrar::contentOf( const QString &fileName ) const
......@@ -79,6 +124,11 @@ QByteArray Unrar::contentOf( const QString &fileName ) const
return file.readAll();
}
bool Unrar::isAvailable()
{
return dynamic_cast< NonFreeUnrarFlavour * >( helper->kind );
}
void Unrar::readFromStdout()
{
mStdOutData += mProcess->readAllStandardOutput();
......
......@@ -46,6 +46,8 @@ class Unrar : public QObject
*/
QByteArray contentOf( const QString &fileName ) const;
static bool isAvailable();
private Q_SLOTS:
void readFromStdout();
void readFromStderr();
......
/***************************************************************************
* Copyright (C) 2007 by Pino Toscano <pino@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. *
***************************************************************************/
#include "unrarflavours.h"
#include <QtCore/QRegExp>
#include <QtCore/QStringList>
UnrarFlavour::UnrarFlavour()
{
}
UnrarFlavour::~UnrarFlavour()
{
}
void UnrarFlavour::setFileName( const QString &fileName )
{
mFileName = fileName;
}
QString UnrarFlavour::fileName() const
{
return mFileName;
}
NonFreeUnrarFlavour::NonFreeUnrarFlavour()
: UnrarFlavour()
{
}
QStringList NonFreeUnrarFlavour::processListing( const QStringList &data )
{
// unrar-nonfree just lists the files
return data;
}
QString NonFreeUnrarFlavour::name() const
{
return "unrar-nonfree";
}
FreeUnrarFlavour::FreeUnrarFlavour()
: UnrarFlavour()
{
}
QStringList FreeUnrarFlavour::processListing( const QStringList &data )
{
QRegExp re( "^ ([^/]+/([^\\s]+))$" );
QStringList newdata;
foreach ( const QString &line, data )
{
if ( re.exactMatch( line ) )
newdata.append( re.cap( 1 ) );
}
return newdata;
}
QString FreeUnrarFlavour::name() const
{
return "unrar-free";
}
/***************************************************************************
* Copyright (C) 2007 by Pino Toscano <pino@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. *
***************************************************************************/
#ifndef UNRARFLAVOURS_H
#define UNRARFLAVOURS_H
#include <QtCore/QString>
class QStringList;
class UnrarFlavour
{
public:
virtual ~UnrarFlavour();
virtual QStringList processListing( const QStringList &data ) = 0;
virtual QString name() const = 0;
void setFileName( const QString &fileName );
protected:
UnrarFlavour();
QString fileName() const;
private:
QString mFileName;
};
class NonFreeUnrarFlavour : public UnrarFlavour
{
public:
NonFreeUnrarFlavour();
virtual QStringList processListing( const QStringList &data );
virtual QString name() const;
};
class FreeUnrarFlavour : public UnrarFlavour
{
public:
FreeUnrarFlavour();
virtual QStringList processListing( const QStringList &data );
virtual QString name() const;
};
#endif
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