Commit a429011e authored by Henrique Pinto's avatar Henrique Pinto
Browse files

* Cleanups

svn path=/branches/work/libarchive-based-ark/ark/; revision=676929
parent e521fb70
......@@ -36,7 +36,9 @@ set(arkpart_PART_SRCS ${libark_common_SRCS}
arkviewer.cpp
extractiondialog.cpp
arch.cpp
archiveinterface.cpp
libarchivehandler.cpp
jobs.cpp
#tar.cpp
#zip.cpp
#lha.cpp
......
......@@ -31,6 +31,7 @@
#include <QList>
#include <QPair>
#include <QRegExp>
#include <QHash>
#include <QVariant>
#include <KUrl>
......
#include "archiveinterface.h"
ReadOnlyArchiveInterface::ReadOnlyArchiveInterface( const QString & filename, QObject *parent )
: QObject( parent ), m_filename( filename )
{
}
ReadOnlyArchiveInterface::~ReadOnlyArchiveInterface()
{
}
void ReadOnlyArchiveInterface::error( const QString & message, const QString & details )
{
foreach( ArchiveObserver *observer, m_observers )
{
observer->onError( message, details );
}
}
void ReadOnlyArchiveInterface::entry( const ArchiveEntry & archiveEntry )
{
foreach( ArchiveObserver *observer, m_observers )
{
observer->onEntry( archiveEntry );
}
}
void ReadOnlyArchiveInterface::progress( double p )
{
foreach( ArchiveObserver *observer, m_observers )
{
observer->onProgress( p );
}
}
void ReadOnlyArchiveInterface::registerObserver( ArchiveObserver *observer )
{
m_observers.append( observer );
}
void ReadOnlyArchiveInterface::removeObserver( ArchiveObserver *observer )
{
m_observers.removeAll( observer );
}
#include "archiveinterface.moc"
#ifndef ARCHIVEINTERFACE_H
#define ARCHIVEINTERFACE_H
#include <QObject>
#include <QStringList>
#include <QString>
#include "arch.h"
class ArchiveObserver
{
public:
ArchiveObserver() {}
virtual ~ArchiveObserver() {}
virtual void onError( const QString & message, const QString & details ) = 0;
virtual void onEntry( const ArchiveEntry & archiveEntry ) = 0;
virtual void onProgress( double ) = 0;
};
class ReadOnlyArchiveInterface: public QObject
{
Q_OBJECT
public:
ReadOnlyArchiveInterface( const QString & filename, QObject *parent = 0 );
virtual ~ReadOnlyArchiveInterface();
QString filename() const { return m_filename; }
virtual bool isReadOnly() const { return true; }
void registerObserver( ArchiveObserver *observer );
void removeObserver( ArchiveObserver *observer );
virtual bool open() { return true; }
virtual bool list() = 0;
virtual bool copyFiles( const QStringList & files, const QString & destinationDirectory ) = 0;
protected:
void error( const QString & message, const QString & details );
void entry( const ArchiveEntry & archiveEntry );
void progress( double );
private:
QList<ArchiveObserver*> m_observers;
QString m_filename;
};
/*
class ReadWriteArchiveInterface: public ReadOnlyArchiveInterface
{
Q_OBJECT
};
*/
#endif // ARCHIVEINTERFACE_H
......@@ -1821,6 +1821,7 @@ ArkWidget::openArchive( const QString & _filename )
void
ArkWidget::slotOpened( bool success )
{
kDebug( 1601 ) << k_funcinfo << " success = " << success << endl;
ready();
m_fileListView->setUpdatesEnabled(true);
......
......@@ -146,6 +146,7 @@ ArchiveEntry FileListView::item( const QString& filename )
void FileListView::addItem( const ArchiveEntry & entry )
{
kDebug( 1601 ) << k_funcinfo << endl;
QTreeWidgetItem *item = new QTreeWidgetItem( this );
m_entryMap[ item ] = entry;
......
/*
* Copyright (c) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "jobs.h"
#include <kdebug.h>
ListingJob::ListingJob( ReadOnlyArchiveInterface *archive, QObject *parent )
: ThreadWeaver::Job( parent ), m_helper( 0 ), m_archive( archive ), m_success( false )
{
}
ListingJob::~ListingJob()
{
delete m_helper;
m_helper = 0;
}
void ListingJob::run()
{
m_helper = new ListingJobHelper( m_archive );
connect( m_helper, SIGNAL( entry( const ArchiveEntry & ) ),
this, SIGNAL( entry( const ArchiveEntry & ) ) );
connect( m_helper, SIGNAL( progress( double ) ),
this, SIGNAL( progress( double ) ) );
m_success = m_helper->getTheListing();
}
ListingJobHelper::ListingJobHelper( ReadOnlyArchiveInterface *archive, QObject *parent )
: QObject( parent ), m_archive( archive )
{
}
ListingJobHelper::~ListingJobHelper()
{
}
bool ListingJobHelper::getTheListing()
{
m_archive->registerObserver( this );
bool result = m_archive->list();
m_archive->removeObserver( this );
return result;
}
void ListingJobHelper::onError( const QString & message, const QString & details )
{
// TODO: do something
}
void ListingJobHelper::onEntry( const ArchiveEntry & archiveEntry )
{
emit entry( archiveEntry );
}
void ListingJobHelper::onProgress( double d )
{
emit progress( d );
}
void ListingJobHelper::entryslot( const ArchiveEntry & e )
{
kDebug( 1601 ) << k_funcinfo << "Entry: " << e[ FileName ] << ", Owner = " << e[ Owner ] << endl;
}
#include "jobs.moc"
/*
* Copyright (c) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JOBS_H
#define JOBS_H
#include <ThreadWeaver/Job>
#include <ThreadWeaver/Weaver>
#include "archiveinterface.h"
#include <QList>
class ListingJobHelper: public QObject, public ArchiveObserver
{
Q_OBJECT
public:
ListingJobHelper( ReadOnlyArchiveInterface *archive, QObject *parent = 0 );
~ListingJobHelper();
bool getTheListing();
void onError( const QString & message, const QString & details );
void onEntry( const ArchiveEntry & archiveEntry );
void onProgress( double );
signals:
void entry( const ArchiveEntry & );
void progress( double );
private slots:
void entryslot( const ArchiveEntry & );
private:
ReadOnlyArchiveInterface *m_archive;
};
class ListingJob: public ThreadWeaver::Job
{
Q_OBJECT
public:
ListingJob( ReadOnlyArchiveInterface *archive, QObject *parent = 0 );
~ListingJob();
bool success() const { return m_success; }
protected:
void run();
signals:
void entry( const ArchiveEntry & );
//void entries( const QList<ArchiveEntry & );
void progress( double );
private:
QList<ArchiveEntry> m_entries;
ListingJobHelper *m_helper;
ReadOnlyArchiveInterface *m_archive;
bool m_success;
};
#endif // JOBS_H
......@@ -24,6 +24,7 @@
*/
#include "libarchivehandler.h"
#include "jobs.h"
#include "settings.h"
#include <archive.h>
......@@ -39,81 +40,6 @@
#include <QStringList>
#include <QDateTime>
class ArchiveListingJob: public ThreadWeaver::Job
{
public:
ArchiveListingJob( const QString& fileName, QObject *parent = 0 )
: Job( parent ), m_fileName( fileName ), m_success( false )
{
}
QList<ArchiveEntry> entries() const { return m_entries; }
virtual bool success() const { return m_success; }
protected:
void run()
{
m_success = listEntries();
kDebug( 1601 ) << k_funcinfo << "listEntries() returned " << m_success << endl;
}
bool listEntries()
{
struct archive *arch;
struct archive_entry *entry;
int result;
arch = archive_read_new();
if ( !arch )
return false;
result = archive_read_support_compression_all( arch );
if ( result != ARCHIVE_OK ) return false;
result = archive_read_support_format_all( arch );
if ( result != ARCHIVE_OK ) return false;
result = archive_read_open_filename( arch, QFile::encodeName( m_fileName ), 4096 );
if ( result != ARCHIVE_OK )
{
kDebug( 1601 ) << "Couldn't open the file '" << m_fileName << "', libarchive can't handle it." << endl;
return false;
}
while ( ( result = archive_read_next_header( arch, &entry ) ) == ARCHIVE_OK )
{
ArchiveEntry e;
e[ FileName ] = QString( archive_entry_pathname( entry ) );
e[ OriginalFileName ] = QByteArray( archive_entry_pathname( entry ) );
e[ Owner ] = QString( archive_entry_uname( entry ) );
e[ Group ] = QString( archive_entry_gname( entry ) );
kDebug( 1601 ) << "Entry: " << e[ FileName ] << ", Owner = " << e[ Owner ] << endl;
e[ Size ] = ( qlonglong ) archive_entry_size( entry );
if ( archive_entry_symlink( entry ) )
{
e[ Link ] = archive_entry_symlink( entry );
}
e[ Timestamp ] = QDateTime::fromTime_t( archive_entry_mtime( entry ) );
m_entries.append( e );
archive_read_data_skip( arch );
}
if ( result != ARCHIVE_EOF )
{
return false;
}
return archive_read_finish( arch ) == ARCHIVE_OK;
}
private:
QString m_fileName;
QList<ArchiveEntry> m_entries;
bool m_success;
};
class ExtractionJob: public ThreadWeaver::Job
{
public:
......@@ -226,19 +152,17 @@ LibArchiveHandler::~LibArchiveHandler()
void LibArchiveHandler::open()
{
ArchiveListingJob *job = new ArchiveListingJob( fileName(), this );
ListingJob *job = new ListingJob( new LibArchiveInterface( fileName(), this ) );
connect( job, SIGNAL( done( ThreadWeaver::Job* ) ),
this, SLOT( listingDone( ThreadWeaver::Job * ) ) );
connect( job, SIGNAL( entry( const ArchiveEntry & ) ),
this, SIGNAL( newEntry( const ArchiveEntry & ) ) );
ThreadWeaver::Weaver::instance()->enqueue( job );
}
void LibArchiveHandler::listingDone( ThreadWeaver::Job *job )
{
foreach( ArchiveEntry entry, static_cast<ArchiveListingJob*>( job )->entries() )
{
emit newEntry( entry );
}
emit opened( job->success() );
delete job;
}
......@@ -273,4 +197,69 @@ void LibArchiveHandler::extractionDone( ThreadWeaver::Job *job )
delete job;
}
LibArchiveInterface::LibArchiveInterface( const QString & filename, QObject *parent )
: ReadOnlyArchiveInterface( filename, parent )
{
}
LibArchiveInterface::~LibArchiveInterface()
{
}
bool LibArchiveInterface::list()
{
struct archive *arch;
struct archive_entry *aentry;
int result;
arch = archive_read_new();
if ( !arch )
return false;
result = archive_read_support_compression_all( arch );
if ( result != ARCHIVE_OK ) return false;
result = archive_read_support_format_all( arch );
if ( result != ARCHIVE_OK ) return false;
result = archive_read_open_filename( arch, QFile::encodeName( filename() ), 10240 );
if ( result != ARCHIVE_OK )
{
error( QString( "Couldn't open the file '%1', libarchive can't handle it." ).arg( filename() ), QString() );
return false;
}
while ( ( result = archive_read_next_header( arch, &aentry ) ) == ARCHIVE_OK )
{
ArchiveEntry e;
e[ FileName ] = QString( archive_entry_pathname( aentry ) );
e[ OriginalFileName ] = QByteArray( archive_entry_pathname( aentry ) );
e[ Owner ] = QString( archive_entry_uname( aentry ) );
e[ Group ] = QString( archive_entry_gname( aentry ) );
kDebug( 1601 ) << "Entry: " << e[ FileName ] << ", Owner = " << e[ Owner ] << endl;
e[ Size ] = ( qlonglong ) archive_entry_size( aentry );
if ( archive_entry_symlink( aentry ) )
{
e[ Link ] = archive_entry_symlink( aentry );
}
e[ Timestamp ] = QDateTime::fromTime_t( archive_entry_mtime( aentry ) );
entry( e );
archive_read_data_skip( arch );
}
if ( result != ARCHIVE_EOF )
{
return false;
}
return archive_read_finish( arch ) == ARCHIVE_OK;
}
bool LibArchiveInterface::copyFiles( const QStringList & files, const QString & destinationDirectory )
{
error( "Not implemented yet", QString() );
return false;
}
#include "libarchivehandler.moc"
......@@ -27,12 +27,24 @@
#define LIBARCHIVEHANDLER_H
#include "arch.h"
#include "archiveinterface.h"
namespace ThreadWeaver
{
class Job;
} // namespace ThreadWeaver
class LibArchiveInterface: public ReadOnlyArchiveInterface
{
Q_OBJECT
public:
LibArchiveInterface( const QString & filename, QObject *parent = 0 );
~LibArchiveInterface();
bool list();
bool copyFiles( const QStringList & files, const QString & destinationDirectory );
};
class LibArchiveHandler: public Arch
{
Q_OBJECT
......
......@@ -43,6 +43,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <KLocale>
// ark includes
#include "arch.h"
#include "arkapp.h"
static KCmdLineOptions option[] =
......@@ -106,6 +107,8 @@ extern "C" KDE_EXPORT int kdemain( int argc, char *argv[] )
KCmdLineArgs::addCmdLineOptions( option );
KCmdLineArgs::addTempFileOption();
qRegisterMetaType<ArchiveEntry>( "ArchiveEntry" );
if ( !ArkApplication::start() )
{
// Already running!
......
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