1
/* This file is part of the KDE project
2
Copyright (C) 2000-2002 David Faure <david@mandrakesoft.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
20
#include "koTarStore.h"
25
KoTarStore::KoTarStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
27
kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename
28
<< " mode = " << int(_mode) << endl;
30
m_pTar = new KTar( _filename, "application/x-gzip" );
32
m_bGood = init( _mode ); // open the targz file and init some vars
34
if ( m_bGood && _mode == Write )
35
m_pTar->setOrigFileName( completeMagic( appIdentification ) );
38
KoTarStore::KoTarStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
40
m_pTar = new KTar( dev );
42
m_bGood = init( mode );
44
if ( m_bGood && mode == Write )
45
m_pTar->setOrigFileName( completeMagic( appIdentification ) );
48
KoTarStore::~KoTarStore()
54
QCString KoTarStore::completeMagic( const QCString& appMimetype )
56
QCString res( "KOffice " );
58
res += '\004'; // Two magic bytes to make the identification
59
res += '\006'; // more reliable (DF)
63
bool KoTarStore::init( Mode _mode )
65
KoStore::init( _mode );
67
bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
69
if ( good && _mode == Read )
70
good = m_pTar->directory() != 0;
74
// When reading, m_stream comes directly from KArchiveFile::device()
75
// When writing, m_stream buffers the data into m_byteArray
77
bool KoTarStore::openWrite( const QString& /*name*/ )
79
// Prepare memory buffer for writing
80
m_byteArray.resize( 0 );
81
m_stream = new QBuffer( m_byteArray );
82
m_stream->open( IO_WriteOnly );
86
bool KoTarStore::openRead( const QString& name )
88
const KTarEntry * entry = m_pTar->directory()->entry( name );
91
//kdWarning(s_area) << "Unknown filename " << name << endl;
92
//return KIO::ERR_DOES_NOT_EXIST;
95
if ( entry->isDirectory() )
97
kdWarning(s_area) << name << " is a directory !" << endl;
98
//return KIO::ERR_IS_DIRECTORY;
101
KTarFile * f = (KTarFile *) entry;
102
m_byteArray.resize( 0 );
104
m_stream = f->device();
109
bool KoTarStore::closeWrite()
111
// write the whole bytearray at once into the tar file
113
kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size "
115
if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
116
kdWarning( s_area ) << "Failed to write " << m_sName << endl;
117
m_byteArray.resize( 0 ); // save memory
121
bool KoTarStore::enterRelativeDirectory( const QString& dirName )
123
if ( m_mode == Read ) {
124
if ( !m_currentDir ) {
125
m_currentDir = m_pTar->directory(); // initialize
126
Q_ASSERT( m_currentPath.isEmpty() );
128
const KArchiveEntry *entry = m_currentDir->entry( dirName );
129
if ( entry && entry->isDirectory() ) {
130
m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
131
return m_currentDir != 0;
135
else // Write, no checking here
139
bool KoTarStore::enterAbsoluteDirectory( const QString& path )
141
if ( path.isEmpty() )
146
if ( m_mode == Read ) {
147
m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) );
148
Q_ASSERT( m_currentDir );
149
return m_currentDir != 0;
155
bool KoTarStore::fileExists( const QString& absPath )
157
return m_pTar->directory()->entry( absPath ) != 0;