lib Library API Documentation

koTarStore.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 2000-2002 David Faure <david@mandrakesoft.com> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #include "koTarStore.h" 00021 #include <ktar.h> 00022 #include <kdebug.h> 00023 #include <qbuffer.h> 00024 00025 KoTarStore::KoTarStore( const QString & _filename, Mode _mode, const QCString & appIdentification ) 00026 { 00027 kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename 00028 << " mode = " << int(_mode) << endl; 00029 00030 m_pTar = new KTar( _filename, "application/x-gzip" ); 00031 00032 m_bGood = init( _mode ); // open the targz file and init some vars 00033 00034 if ( m_bGood && _mode == Write ) 00035 m_pTar->setOrigFileName( completeMagic( appIdentification ) ); 00036 } 00037 00038 KoTarStore::KoTarStore( QIODevice *dev, Mode mode, const QCString & appIdentification ) 00039 { 00040 m_pTar = new KTar( dev ); 00041 00042 m_bGood = init( mode ); 00043 00044 if ( m_bGood && mode == Write ) 00045 m_pTar->setOrigFileName( completeMagic( appIdentification ) ); 00046 } 00047 00048 KoTarStore::~KoTarStore() 00049 { 00050 m_pTar->close(); 00051 delete m_pTar; 00052 } 00053 00054 QCString KoTarStore::completeMagic( const QCString& appMimetype ) 00055 { 00056 QCString res( "KOffice " ); 00057 res += appMimetype; 00058 res += '\004'; // Two magic bytes to make the identification 00059 res += '\006'; // more reliable (DF) 00060 return res; 00061 } 00062 00063 bool KoTarStore::init( Mode _mode ) 00064 { 00065 KoStore::init( _mode ); 00066 m_currentDir = 0; 00067 bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly ); 00068 00069 if ( good && _mode == Read ) 00070 good = m_pTar->directory() != 0; 00071 return good; 00072 } 00073 00074 // When reading, m_stream comes directly from KArchiveFile::device() 00075 // When writing, m_stream buffers the data into m_byteArray 00076 00077 bool KoTarStore::openWrite( const QString& /*name*/ ) 00078 { 00079 // Prepare memory buffer for writing 00080 m_byteArray.resize( 0 ); 00081 m_stream = new QBuffer( m_byteArray ); 00082 m_stream->open( IO_WriteOnly ); 00083 return true; 00084 } 00085 00086 bool KoTarStore::openRead( const QString& name ) 00087 { 00088 const KTarEntry * entry = m_pTar->directory()->entry( name ); 00089 if ( entry == 0L ) 00090 { 00091 //kdWarning(s_area) << "Unknown filename " << name << endl; 00092 //return KIO::ERR_DOES_NOT_EXIST; 00093 return false; 00094 } 00095 if ( entry->isDirectory() ) 00096 { 00097 kdWarning(s_area) << name << " is a directory !" << endl; 00098 //return KIO::ERR_IS_DIRECTORY; 00099 return false; 00100 } 00101 KTarFile * f = (KTarFile *) entry; 00102 m_byteArray.resize( 0 ); 00103 delete m_stream; 00104 m_stream = f->device(); 00105 m_iSize = f->size(); 00106 return true; 00107 } 00108 00109 bool KoTarStore::closeWrite() 00110 { 00111 // write the whole bytearray at once into the tar file 00112 00113 kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size " 00114 << m_iSize << endl; 00115 if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) ) 00116 kdWarning( s_area ) << "Failed to write " << m_sName << endl; 00117 m_byteArray.resize( 0 ); // save memory 00118 return true; 00119 } 00120 00121 bool KoTarStore::enterRelativeDirectory( const QString& dirName ) 00122 { 00123 if ( m_mode == Read ) { 00124 if ( !m_currentDir ) { 00125 m_currentDir = m_pTar->directory(); // initialize 00126 Q_ASSERT( m_currentPath.isEmpty() ); 00127 } 00128 const KArchiveEntry *entry = m_currentDir->entry( dirName ); 00129 if ( entry && entry->isDirectory() ) { 00130 m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry ); 00131 return m_currentDir != 0; 00132 } 00133 return false; 00134 } 00135 else // Write, no checking here 00136 return true; 00137 } 00138 00139 bool KoTarStore::enterAbsoluteDirectory( const QString& path ) 00140 { 00141 if ( path.isEmpty() ) 00142 { 00143 m_currentDir = 0; 00144 return true; 00145 } 00146 if ( m_mode == Read ) { 00147 m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) ); 00148 Q_ASSERT( m_currentDir ); 00149 return m_currentDir != 0; 00150 } 00151 else 00152 return true; 00153 } 00154 00155 bool KoTarStore::fileExists( const QString& absPath ) 00156 { 00157 return m_pTar->directory()->entry( absPath ) != 0; 00158 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 24 18:22:27 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003