00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 );
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';
00059 res +=
'\006';
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
00075
00076
00077
bool KoTarStore::openWrite(
const QString& )
00078 {
00079
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
00092
00093
return false;
00094 }
00095
if ( entry->isDirectory() )
00096 {
00097 kdWarning(s_area) << name <<
" is a directory !" << endl;
00098
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
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 );
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();
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
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 }