ark
karchiveplugin.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "karchiveplugin.h"
00022 #include "kerfuffle/archivefactory.h"
00023
00024 #include <KZip>
00025 #include <KTar>
00026 #include <KMimeType>
00027 #include <KDebug>
00028 #include <KLocale>
00029
00030 #include <QFileInfo>
00031
00032 KArchiveInterface::KArchiveInterface( const QString & filename, QObject *parent )
00033 : ReadWriteArchiveInterface( filename, parent ), m_archive( 0 )
00034 {
00035 kDebug( 1601 ) ;
00036 }
00037
00038 KArchiveInterface::~KArchiveInterface()
00039 {
00040 delete m_archive;
00041 m_archive = 0;
00042 }
00043
00044 KArchive *KArchiveInterface::archive()
00045 {
00046 if ( m_archive == 0 )
00047 {
00048 KMimeType::Ptr mimeType = KMimeType::findByPath( filename() );
00049
00050 if ( mimeType->is( "application/zip" ) )
00051 {
00052 m_archive = new KZip( filename() );
00053 }
00054 else
00055 {
00056 m_archive = new KTar( filename() );
00057 }
00058
00059 }
00060 return m_archive;
00061 }
00062
00063 bool KArchiveInterface::list()
00064 {
00065 kDebug( 1601 ) ;
00066 if ( !archive()->open( QIODevice::ReadOnly ) )
00067 {
00068 error( i18n( "Could not open the archive '%1' for reading", filename() ) );
00069 return false;
00070 }
00071 else
00072 {
00073 return browseArchive( archive() );
00074 }
00075 }
00076
00077 bool KArchiveInterface::copyFiles( const QList<QVariant> & files, const QString & destinationDirectory, bool preservePaths )
00078 {
00079 if ( preservePaths )
00080 {
00081 error( "Extraction preserving paths is not implemented yet." );
00082 return false;
00083 }
00084 foreach( const QVariant & file, files )
00085 {
00086 const KArchiveEntry *archiveEntry = archive()->directory()->entry( file.toString() );
00087 if ( !archiveEntry )
00088 {
00089 error( QString( "File '%1' not found in the archive" ).arg( file.toString() ) );
00090 return false;
00091 }
00092
00093
00094 if ( archiveEntry->isDirectory() )
00095 {
00096 static_cast<const KArchiveDirectory*>( archiveEntry )->copyTo( destinationDirectory );
00097 }
00098 else
00099 {
00100 static_cast<const KArchiveFile*>( archiveEntry )->copyTo( destinationDirectory );
00101 }
00102 }
00103
00104 return true;
00105 }
00106
00107 bool KArchiveInterface::browseArchive( KArchive *archive )
00108 {
00109 return processDir( archive->directory() );
00110 }
00111
00112 bool KArchiveInterface::processDir( const KArchiveDirectory *dir, const QString & prefix )
00113 {
00114 foreach( const QString& entryName, dir->entries() )
00115 {
00116 const KArchiveEntry *entry = dir->entry( entryName );
00117 createEntryFor( entry, prefix );
00118 if ( entry->isDirectory() )
00119 {
00120 QString newPrefix = ( prefix.isEmpty()? prefix : prefix + '/' ) + entryName;
00121 processDir( static_cast<const KArchiveDirectory*>( entry ), newPrefix );
00122 }
00123 }
00124 return true;
00125 }
00126
00127 void KArchiveInterface::createEntryFor( const KArchiveEntry *aentry, const QString& prefix )
00128 {
00129 ArchiveEntry e;
00130 e[ FileName ] = prefix.isEmpty()? aentry->name() : prefix + '/' + aentry->name();
00131 e[ OriginalFileName ] = e[ FileName ];
00132 e[ Permissions ] = aentry->permissions();
00133 e[ Owner ] = aentry->user();
00134 e[ Group ] = aentry->group();
00135 e[ IsDirectory ] = aentry->isDirectory();
00136 e[ Timestamp ] = aentry->datetime();
00137 if ( !aentry->symLinkTarget().isEmpty() )
00138 {
00139 e[ Link ] = aentry->symLinkTarget();
00140 }
00141 if ( aentry->isFile() )
00142 {
00143 e[ Size ] = static_cast<const KArchiveFile*>( aentry )->size();
00144 }
00145 entry( e );
00146 }
00147
00148 bool KArchiveInterface::addFiles( const QStringList & files )
00149 {
00150 kDebug( 1601 ) << "Starting..." ;
00151 delete m_archive;
00152 m_archive = 0;
00153
00154
00155
00156
00157 if ( !archive()->open( QIODevice::WriteOnly ) )
00158 {
00159 error( i18n( "Could not open the archive '%1' for writing.", filename() ) );
00160 return false;
00161 }
00162
00163 kDebug( 1601 ) << "Archive opened for writing..." ;
00164 kDebug( 1601 ) << "Will add " << files.count() << " files" ;
00165 foreach( const QString &path, files )
00166 {
00167 kDebug( 1601 ) << "Adding " << path ;
00168 QFileInfo fi( path );
00169 Q_ASSERT( fi.exists() );
00170
00171 if ( fi.isDir() )
00172 {
00173 if ( !archive()->addLocalDirectory( path, fi.fileName() ) )
00174 {
00175 error( i18n( "Could not add the directory %1 to the archive", path ) );
00176 return false;
00177 }
00178 }
00179 else
00180 {
00181 if ( !archive()->addLocalFile( path, fi.fileName() ) )
00182 {
00183 error( i18n( "Could not add the file %1 to the archive.", path ) );
00184 return false;
00185 }
00186 }
00187 }
00188 kDebug( 1601 ) << "Closing the archive" ;
00189 archive()->close();
00190 kDebug( 1601 ) << "Done" ;
00191 return true;
00192 }
00193
00194 bool KArchiveInterface::deleteFiles( const QList<QVariant> & files )
00195 {
00196 return false;
00197 }
00198
00199 KERFUFFLE_PLUGIN_FACTORY( KArchiveInterface )