Commit 3cd1fc32 authored by Henrique Pinto's avatar Henrique Pinto
Browse files

* Remove a lot of unused code from Arch

svn path=/branches/work/libarchive-based-ark/ark/; revision=672755
parent f387748c
......@@ -50,353 +50,21 @@
// the archive types
#include "libarchivehandler.h"
//#include "tar.h"
//#include "zip.h"
//#include "lha.h"
//#include "compressedfile.h"
//#include "zoo.h"
//#include "rar.h"
//#include "ar.h"
//#include "sevenzip.h"
//#include "ace.h"
Arch::ArchColumns::ArchColumns( int col, const QRegExp &reg, int length, bool opt )
: colRef( col ), pattern( reg ), maxLength( length ), optional( opt )
{
}
Arch::Arch( ArkWidget *gui, const QString &filename )
: m_filename( filename ), m_buffer( "" ), m_gui( gui ),
m_bReadOnly( false ), m_bNotifyWhenDeleteFails( true ),
m_header_removed( false ), m_finished( false ),
m_numCols( 0 ), m_dateCol( -1 ), m_fixYear( -1 ), m_fixMonth( -1 ),
m_fixDay( -1 ), m_fixTime( -1 ), m_repairYear( -1 ), m_repairMonth( -1 ),
m_repairTime( -1 ), m_currentProcess( 0 )
: m_filename( filename ), m_gui( gui ),
m_readOnly( false )
{
}
Arch::~Arch()
{
if ( m_currentProcess )
m_currentProcess->kill();
}
void Arch::verifyUtilityIsAvailable( const QString &utility1,
const QString &utility2)
{
// see if the utility is in the PATH of the user. If there is a
// second utility specified, it must also be present.
QString cmd1 = KGlobal::dirs()->findExe( utility1 );
if( utility2.isNull() )
{
m_bUtilityIsAvailable = !cmd1.isEmpty();
}
else
{
QString cmd2 = KGlobal::dirs()->findExe( utility2 );
m_bUtilityIsAvailable = ( !cmd1.isEmpty() && !cmd2.isEmpty() );
}
}
void Arch::slotOpenExited( K3Process* _kp )
{
int exitStatus = 100; // arbitrary bad exit status
if ( _kp->normalExit() )
exitStatus = _kp->exitStatus();
if ( exitStatus == 1 )
{
exitStatus = 0; // because 1 means empty archive - not an error.
// Is this a safe assumption?
}
if ( !exitStatus )
emit sigOpen( this, true, m_filename,
Arch::Extract | Arch::Delete | Arch::Add | Arch::View );
else
emit sigOpen( this, false, QString(), 0 );
delete _kp;
_kp = m_currentProcess = 0;
}
void Arch::slotDeleteExited( K3Process *_kp )
{
bool success = ( _kp->normalExit() && ( _kp->exitStatus() == 0 ) );
if ( !success )
{
QApplication::restoreOverrideCursor();
QString msg = i18n( "The deletion operation failed." );
if ( !getLastShellOutput().isNull() )
{
msg += i18n( "\nUse \"Details\" to view the last shell output." );
KMessageBox::detailedError( m_gui, msg, getLastShellOutput() );
}
else
{
KMessageBox::error( m_gui, msg );
}
}
emit sigDelete( success );
delete _kp;
_kp = m_currentProcess = 0;
}
void Arch::slotExtractExited( K3Process *_kp )
{
bool success = ( _kp->normalExit() && ( _kp->exitStatus() == 0 ) );
if( !success )
{
if ( passwordRequired() )
{
QString msg;
if ( !m_password.isEmpty() )
msg = i18n("The password was incorrect. ");
KPasswordDialog dlg(m_gui);
dlg.setPrompt( msg+i18n("You must enter a password to extract the file:") );
if( dlg.exec() == KPasswordDialog::Accepted )
{
m_password=dlg.password().toLocal8Bit();
delete _kp;
_kp = m_currentProcess = 0;
clearShellOutput();
unarchFileInternal(); // try to extract the file again with a password
return;
}
emit sigExtract( true );
delete _kp;
_kp = m_currentProcess = 0;
return;
}
else if ( m_password.isEmpty() || _kp->exitStatus() > 1 )
{
QApplication::restoreOverrideCursor();
QString msg = i18n( "The extraction operation failed." );
if ( !getLastShellOutput().isNull() )
{
msg += i18n( "\nUse \"Details\" to view the last shell output." );
KMessageBox::detailedError( m_gui, msg, getLastShellOutput() );
}
else
{
KMessageBox::error( m_gui, msg );
}
}
}
m_password = "";
emit sigExtract( success );
delete _kp;
_kp = m_currentProcess = 0;
}
void Arch::unarchFile( QStringList *fileList, const QString & destDir,
bool viewFriendly )
{
m_fileList = fileList;
m_destDir = destDir;
m_viewFriendly = viewFriendly;
unarchFileInternal();
}
void Arch::slotAddExited( K3Process *_kp )
{
bool success = ( _kp->normalExit() && ( _kp->exitStatus() == 0 ) );
if( !success )
{
QApplication::restoreOverrideCursor();
QString msg = i18n( "The addition operation failed." );
if ( !getLastShellOutput().isNull() )
{
msg += i18n( "\nUse \"Details\" to view the last shell output." );
KMessageBox::detailedError( m_gui, msg, getLastShellOutput() );
}
else
{
KMessageBox::error( m_gui, msg );
}
}
emit sigAdd( success );
delete _kp;
_kp = m_currentProcess = 0;
}
void Arch::slotReceivedOutput( K3Process*, char* data, int length )
{
char c = data[ length ];
data[ length ] = '\0';
appendShellOutputData( data );
data[ length ] = c;
}
void Arch::slotReceivedTOC( K3Process*, char* data, int length )
{
char c = data[ length ];
data[ length ] = '\0';
appendShellOutputData( data );
int lfChar, startChar = 0;
while ( !m_finished )
{
for ( lfChar = startChar; data[ lfChar ] != '\n' && lfChar < length;
lfChar++ );
if ( data[ lfChar ] != '\n')
break; // We are done all the complete lines
data[ lfChar ] = '\0';
m_buffer.append( data + startChar );
data[ lfChar ] = '\n';
startChar = lfChar + 1;
if ( m_headerString.isEmpty() )
{
processLine( m_buffer );
}
else if ( m_buffer.indexOf( m_headerString ) == -1 )
{
if ( m_header_removed && !m_finished )
{
if ( !processLine( m_buffer ) )
{
// Have faith - maybe it wasn't a header?
m_header_removed = false;
m_error = true;
}
}
}
else if ( !m_header_removed )
{
m_header_removed = true;
}
else
{
m_finished = true;
}
m_buffer = "";
}
if ( !m_finished )
m_buffer.append( data + startChar); // Append what's left of the buffer
data[ length ] = c;
}
bool Arch::processLine( const QByteArray &line )
{
QString columns[ 11 ];
unsigned int pos = 0;
int strpos, len;
// Go through our columns, try to pick out data, return silently on failure
foreach( const ArchColumns &column, m_archCols )
{
strpos = column.pattern.indexIn( line, pos );
len = column.pattern.matchedLength();
if ( ( strpos == -1 ) || ( len > column.maxLength ) )
{
if ( column.optional )
continue; // More?
else
{
kDebug(1601) << "processLine failed to match critical column" << endl;
return false;
}
}
pos = strpos + len;
columns[ column.colRef ] = line.mid( strpos, len );
}
if ( m_dateCol >= 0 )
{
QString year = ( m_repairYear >= 0 ) ?
ArkUtils::fixYear( qPrintable(columns[ m_repairYear ]))
: columns[ m_fixYear ];
QString month = ( m_repairMonth >= 0 ) ?
QString( "%1" )
.arg( ArkUtils::getMonth( qPrintable( columns[ m_repairMonth ] ) ) )
: columns[ m_fixMonth ];
QString timestamp = QString::fromLatin1( "%1-%2-%3 %4" )
.arg( year )
.arg( month )
.arg( columns[ m_fixDay ] )
.arg( columns[ m_fixTime ] );
columns[ m_dateCol ] = timestamp;
}
QStringList list;
for ( int i = 0; i < m_numCols; ++i )
{
list.append( columns[ i ] );
}
//m_gui->fileList()->addItem( list ); // send the entry to the GUI
emit newEntry( list );
return true;
}
Arch *Arch::archFactory( ArchType aType,
ArkWidget *parent, const QString &filename,
const QString &openAsMimeType )
{
switch( aType )
{
/*case TAR_FORMAT:
return new TarArch( parent, filename, openAsMimeType );
case ZIP_FORMAT:
return new ZipArch( parent, filename );
case LHA_FORMAT:
return new LhaArch( parent, filename );
case COMPRESSED_FORMAT:
return new CompressedFile( parent, filename, openAsMimeType );
case ZOO_FORMAT:
return new ZooArch( parent, filename );
case RAR_FORMAT:
return new RarArch( parent, filename );
case AA_FORMAT:
return new ArArch( parent, filename );
case SEVENZIP_FORMAT:
return new SevenZipArch( parent, filename );
case ACE_FORMAT:
return new AceArch( parent, filename );*/
case UNKNOWN_FORMAT:
default:
return new LibArchiveHandler( parent, filename );
return 0;
}
return new LibArchiveHandler( parent, filename );
}
#include "arch.moc"
......@@ -80,22 +80,6 @@ class Arch : public QObject
{
Q_OBJECT
protected:
/**
* A struct representing column data. This makes it possible to abstract
* archive output, and save writing the same function for every archive
* type. It is also much more robust than sscanf (which was breaking).
*/
struct ArchColumns
{
int colRef; // Which column to load to in processLine
QRegExp pattern;
int maxLength;
bool optional;
ArchColumns( int col, const QRegExp &reg, int length = 64, bool opt = false );
};
public:
Arch( ArkWidget *_viewer, const QString & _fileName );
virtual ~Arch();
......@@ -107,93 +91,34 @@ class Arch : public QObject
virtual void addFile( const QStringList & ) = 0;
virtual void addDir( const QString & ) = 0;
// unarch the files in m_fileList or all files if m_fileList is empty.
// if m_destDir is empty, abort with error.
// m_viewFriendly forces certain options like directory junking required by view/edit
virtual void unarchFileInternal() = 0;
// returns true if a password is required
virtual bool passwordRequired() { return false; }
virtual void extractFile( const QStringList &, const QString & _destDir ) = 0;
virtual void unarchFile( QStringList *, const QString & _destDir,
bool viewFriendly = false );
virtual bool passwordRequired() { return false; }
QString fileName() const { return m_filename; }
enum EditProperties{ Add = 1, Delete = 2, Extract = 4,
View = 8, Integrity = 16 };
// is the archive readonly?
bool isReadOnly() { return m_bReadOnly; }
void setReadOnly( bool bVal ) { m_bReadOnly = bVal; }
bool isError() { return m_error; }
void resetError() { m_error = false; }
// check to see if the utility exists in the PATH of the user
void verifyUtilityIsAvailable( const QString &,
const QString & = QString() );
bool utilityIsAvailable() { return m_bUtilityIsAvailable; }
QString getUtility() { return m_archiver_program; }
void appendShellOutputData( const char * data ) { m_lastShellOutput.append( data ); }
void clearShellOutput() { m_lastShellOutput.truncate( 0 ); }
const QString& getLastShellOutput() const { return m_lastShellOutput; }
bool isReadOnly() { return m_readOnly; }
void setReadOnly( bool readOnly ) { m_readOnly = readOnly; }
static Arch *archFactory( ArchType aType, ArkWidget *parent,
const QString &filename,
const QString &openAsMimeType = QString() );
protected slots:
void slotOpenExited( K3Process* );
void slotExtractExited( K3Process* );
void slotDeleteExited( K3Process* );
void slotAddExited( K3Process* );
void slotReceivedOutput( K3Process *, char*, int );
virtual bool processLine( const QByteArray &line );
virtual void slotReceivedTOC( K3Process *, char *, int );
signals:
void sigOpen( Arch * archive, bool success, const QString &filename, int );
void sigCreate( Arch *, bool, const QString &, int );
void sigDelete( bool );
void sigExtract( bool );
void sigAdd( bool );
void headers( const ColumnList& columns );
void newEntry( const QStringList& entry );
void newEntry( const ArchiveEntry& entry );
protected: // data
QString m_filename;
QString m_lastShellOutput;
QByteArray m_buffer;
ArkWidget *m_gui;
bool m_bReadOnly; // for readonly archives
bool m_error;
// lets tar delete unsuccessfully before adding without confusing the user
bool m_bNotifyWhenDeleteFails;
// set to whether the archiving utility/utilities is/are in the user's PATH
bool m_bUtilityIsAvailable;
QString m_archiver_program;
QString m_unarchiver_program;
// Archive parsing information
QByteArray m_headerString;
bool m_header_removed, m_finished;
QList<ArchColumns> m_archCols;
int m_numCols, m_dateCol, m_fixYear, m_fixMonth, m_fixDay, m_fixTime;
int m_repairYear, m_repairMonth, m_repairTime;
K3Process *m_currentProcess;
QStringList *m_fileList;
QString m_destDir;
bool m_viewFriendly;
QByteArray m_password;
bool m_readOnly; // for readonly archives
};
// Columns
......
......@@ -295,7 +295,7 @@ ArkWidget::convertTo( const KUrl & u )
m_convert_tmpDir = new KTempDir( tmpDir() + "convtmp" );
connect( arch, SIGNAL( sigExtract( bool ) ), this, SLOT( convertSlotExtractDone( bool ) ) );
m_convert_saveAsURL = u;
arch->unarchFile( 0, m_convert_tmpDir->name() );
arch->extractFile( QStringList(), m_convert_tmpDir->name() );
}
void
......@@ -509,7 +509,7 @@ ArkWidget::extractToSlotOpenDone( bool success )
{
disableAll();
connect( arch, SIGNAL( sigExtract( bool ) ), this, SLOT( extractToSlotExtractDone( bool ) ) );
arch->unarchFile( 0, extractDir );
arch->extractFile( QStringList(), extractDir );
}
else
{
......@@ -528,7 +528,6 @@ ArkWidget::extractToSlotExtractDone( bool success )
disconnect( arch, SIGNAL( sigExtract( bool ) ), this, SLOT( extractToSlotExtractDone( bool ) ) );
if ( !success )
{
kDebug( 1601 ) << "Last Shell Output" << arch->getLastShellOutput() << endl;
KMessageBox::error( this, i18n( "An error occurred while extracting the archive." ) );
emit request_file_quit();
return;
......@@ -1340,9 +1339,7 @@ ArkWidget::prepareViewFiles( const QStringList & fileList )
it != fileList.end(); ++it)
QFile::remove(destTmpDirectory + *it);
QStringList * list = new QStringList( fileList );
arch->unarchFile( list, destTmpDirectory, true);
delete list;
arch->extractFile( fileList, destTmpDirectory );
}
bool
......@@ -1506,14 +1503,13 @@ ArkWidget::action_extract()
disableAll();
busy( i18n( "Extracting..." ) );
connect( arch, SIGNAL( sigExtract( bool ) ), this, SLOT( slotExtractDone() ) );
arch->unarchFile(0, extractDir);
arch->extractFile(QStringList(), extractDir);
}
}
}
else
{
KIO::filesize_t nTotalSize = 0;
// make a list to send to unarchFile
QStringList selectedFiles = m_fileListView->selectedFilenames();
for ( QStringList::const_iterator it = selectedFiles.constBegin();
it != selectedFiles.constEnd();
......@@ -1534,7 +1530,7 @@ ArkWidget::action_extract()
busy( i18n( "Extracting..." ) );
connect( arch, SIGNAL( sigExtract( bool ) ),
this, SLOT( slotExtractDone() ) );
arch->unarchFile(m_extractList, extractDir); // extract selected files
arch->extractFile(selectedFiles, extractDir); // extract selected files
}
}
}
......@@ -1988,12 +1984,6 @@ Arch * ArkWidget::getNewArchive( const QString & _fileName, const QString& _mime
return NULL;
}
if (!newArch->utilityIsAvailable())
{
KMessageBox::error(this, i18n("The utility %1 is not in your PATH.\nPlease install it or contact your system administrator.", newArch->getUtility()));
return NULL;
}
connect( newArch, SIGNAL(headers(const ColumnList&)),
m_fileListView, SLOT(setHeaders(const ColumnList&)));
......@@ -2100,12 +2090,6 @@ ArkWidget::openArchive( const QString & _filename )
return;
}
if (!newArch->utilityIsAvailable())
{
KMessageBox::error(this, i18n("The utility %1 is not in your PATH.\nPlease install it or contact your system administrator.", newArch->getUtility()));
return;
}
m_archType = archtype;
connect( newArch, SIGNAL(sigOpen(Arch *, bool, const QString &, int)),
......
......@@ -157,7 +157,6 @@ LibArchiveHandler::LibArchiveHandler( ArkWidget *gui, const QString &filename )
: Arch( gui, filename )
{
kDebug( 1601 ) << "libarchive api version = " << archive_api_version() << endl;
m_bUtilityIsAvailable = true;
}
LibArchiveHandler::~LibArchiveHandler()
......@@ -199,19 +198,12 @@ void LibArchiveHandler::remove( QStringList* )
{
}
void LibArchiveHandler::unarchFileInternal()
void LibArchiveHandler::extractFile( const QStringList & files, const QString& destinationDir )
{
}
void LibArchiveHandler::unarchFile( QStringList *files, const QString& destinationDir, bool /*viewFriendly*/ )
{
if ( files )
{
ExtractionJob *job = new ExtractionJob( fileName(), *files, destinationDir, this );
connect( job, SIGNAL( done( ThreadWeaver::Job* ) ),
this, SLOT( extractionDone( ThreadWeaver::Job * ) ) );
ThreadWeaver::Weaver::instance()->enqueue( job );
}
ExtractionJob *job = new ExtractionJob( fileName(), files, destinationDir, this );
connect( job, SIGNAL( done( ThreadWeaver::Job* ) ),
this, SLOT( extractionDone( ThreadWeaver::Job * ) ) );
ThreadWeaver::Weaver::instance()->enqueue( job );
}
void LibArchiveHandler::extractionDone( ThreadWeaver::Job *job )
......
......@@ -46,8 +46,7 @@ class LibArchiveHandler: public Arch
virtual void addFile( const QStringList & );
virtual void addDir( const QString & );
virtual void remove( QStringList* );
virtual void unarchFileInternal();
virtual void unarchFile( QStringList *files, const QString& destinationDir, bool viewFriendly = false );
virtual void extractFile( const QStringList & files, const QString& destinationDir );
private slots:
void listingDone( ThreadWeaver::Job* );
......
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