• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

ark

karchiveplugin.cpp

Go to the documentation of this file.
00001 /*
00002  * ark -- archiver for the KDE project
00003  *
00004  * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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 #include <QDir>
00030 
00031 #include <QFileInfo>
00032 
00033 KArchiveInterface::KArchiveInterface( const QString & filename, QObject *parent )
00034     : ReadWriteArchiveInterface( filename, parent ), m_archive( 0 )
00035 {
00036     kDebug( 1601 ) ;
00037 }
00038 
00039 KArchiveInterface::~KArchiveInterface()
00040 {
00041     delete m_archive;
00042     m_archive = 0;
00043 }
00044 
00045 KArchive *KArchiveInterface::archive()
00046 {
00047     if ( m_archive == 0 )
00048     {
00049         KMimeType::Ptr mimeType = KMimeType::findByPath( filename() );
00050 
00051         if ( mimeType->is( "application/zip" ) )
00052         {
00053             m_archive = new KZip( filename() );
00054         }
00055         else
00056         {
00057             m_archive = new KTar( filename() );
00058         }
00059 
00060     }
00061     return m_archive;
00062 }
00063 
00064 bool KArchiveInterface::list()
00065 {
00066     kDebug( 1601 ) ;
00067     if ( !archive()->isOpen() && !archive()->open( QIODevice::ReadOnly ) )
00068     {
00069         error( i18n( "Could not open the archive '%1' for reading", filename() ) );
00070         return false;
00071     }
00072     else
00073     {
00074         return browseArchive( archive() );
00075     }
00076 }
00077 
00078 bool KArchiveInterface::copyFiles( const QList<QVariant> & files, const QString & destinationDirectory, bool preservePaths )
00079 {
00080     if ( !archive()->isOpen() && !archive()->open( QIODevice::ReadOnly ) )
00081     {
00082         error( i18n( "Could not open the archive '%1' for reading", filename() ) );
00083         return false;
00084     }
00085 
00086     foreach( const QVariant & file, files )
00087     {
00088         QString realDestination = destinationDirectory;
00089         const KArchiveEntry *archiveEntry = archive()->directory()->entry( file.toString() );
00090         if ( !archiveEntry )
00091         {
00092             error( i18n( "File '%1' not found in the archive" ,file.toString() ) );
00093             return false;
00094         }
00095 
00096         // TODO: handle errors, copyTo fails silently
00097         if ( preservePaths ) {
00098             QFileInfo fi( file.toString() );
00099             QDir dest( destinationDirectory );
00100             QString filepath = archiveEntry->isDirectory() ? fi.filePath() : fi.path();
00101             dest.mkpath( filepath );
00102             realDestination = dest.absolutePath() + '/' + filepath;
00103         }
00104         if ( archiveEntry->isDirectory() )
00105         {
00106             kDebug() << "Calling copyTo(" << realDestination << ") for " << archiveEntry->name();
00107             static_cast<const KArchiveDirectory*>( archiveEntry )->copyTo( realDestination );
00108         }
00109         else
00110         {
00111             static_cast<const KArchiveFile*>( archiveEntry )->copyTo( realDestination );
00112         }
00113     }
00114 
00115     return true;
00116 }
00117 
00118 bool KArchiveInterface::browseArchive( KArchive *archive )
00119 {
00120     return processDir( archive->directory() );
00121 }
00122 
00123 bool KArchiveInterface::processDir( const KArchiveDirectory *dir, const QString & prefix )
00124 {
00125     foreach( const QString& entryName, dir->entries() )
00126     {
00127         const KArchiveEntry *entry = dir->entry( entryName );
00128         createEntryFor( entry, prefix );
00129         if ( entry->isDirectory() )
00130         {
00131             QString newPrefix = ( prefix.isEmpty()? prefix : prefix + '/' ) + entryName;
00132             processDir( static_cast<const KArchiveDirectory*>( entry ), newPrefix );
00133         }
00134     }
00135     return true;
00136 }
00137 
00138 void KArchiveInterface::createEntryFor( const KArchiveEntry *aentry, const QString& prefix )
00139 {
00140     ArchiveEntry e;
00141     e[ FileName ]         = prefix.isEmpty()? aentry->name() : prefix + '/' + aentry->name();
00142     e[ InternalID ]       = e[ FileName ];
00143     e[ Permissions ]      = aentry->permissions();
00144     e[ Owner ]            = aentry->user();
00145     e[ Group ]            = aentry->group();
00146     e[ IsDirectory ]      = aentry->isDirectory();
00147     e[ Timestamp ]        = aentry->datetime();
00148     if ( !aentry->symLinkTarget().isEmpty() )
00149     {
00150         e[ Link ]             = aentry->symLinkTarget();
00151     }
00152     if ( aentry->isFile() )
00153     {
00154         e[ Size ] = static_cast<const KArchiveFile*>( aentry )->size();
00155     }
00156     entry( e );
00157 }
00158 
00159 bool KArchiveInterface::addFiles( const QStringList & files )
00160 {
00161     kDebug( 1601 ) << "Starting..." ;
00162 //  delete m_archive;
00163 //  m_archive = 0;
00164     if ( archive()->isOpen() )
00165     {
00166         archive()->close();
00167     }
00168     if ( !archive()->open( QIODevice::ReadWrite ) )
00169     {
00170         error( i18n( "Could not open the archive '%1' for writing.", filename() ) );
00171         return false;
00172     }
00173 
00174     kDebug( 1601 ) << "Archive opened for writing..." ;
00175     kDebug( 1601 ) << "Will add " << files.count() << " files" ;
00176     foreach( const QString &path, files )
00177     {
00178         kDebug( 1601 ) << "Adding " << path ;
00179         QFileInfo fi( path );
00180         Q_ASSERT( fi.exists() );
00181 
00182         if ( fi.isDir() )
00183         {
00184             if ( archive()->addLocalDirectory( path, fi.fileName() ) )
00185             {
00186                 const KArchiveEntry *entry = archive()->directory()->entry( fi.fileName() );
00187                 createEntryFor( entry, "" );
00188                 processDir( (KArchiveDirectory*) archive()->directory()->entry( fi.fileName() ), fi.fileName() );
00189             } 
00190             else
00191             {
00192                 error( i18n( "Could not add the directory %1 to the archive", path ) );
00193                 return false;
00194             }
00195         }
00196         else
00197         {
00198             if ( archive()->addLocalFile( path, fi.fileName() ) )
00199             { 
00200                 const KArchiveEntry *entry = archive()->directory()->entry( fi.fileName() );
00201                 createEntryFor( entry, "" );
00202             } 
00203             else    
00204             {
00205                 error( i18n( "Could not add the file %1 to the archive.", path ) );
00206                 return false;
00207             }
00208         }
00209     }
00210     kDebug( 1601 ) << "Closing the archive" ;
00211     archive()->close();
00212     kDebug( 1601 ) << "Done" ;
00213     return true;
00214 }
00215 
00216 bool KArchiveInterface::deleteFiles( const QList<QVariant> & files )
00217 {
00218     return false;
00219 }
00220 
00221 KERFUFFLE_PLUGIN_FACTORY( KArchiveInterface )

ark

Skip menu "ark"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal