lib
KoTarStore.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoTarStore.h"
00021
00022 #include <qbuffer.h>
00023
00024 #include <ktar.h>
00025 #include <kdebug.h>
00026 #include <kurl.h>
00027 #include <kdeversion.h>
00028 #include <kio/netaccess.h>
00029
00030 KoTarStore::KoTarStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
00031 {
00032 kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename
00033 << " mode = " << int(_mode) << endl;
00034
00035 m_pTar = new KTar( _filename, "application/x-gzip" );
00036
00037 m_bGood = init( _mode );
00038 kdDebug()<<"appIdentification :"<<appIdentification<<endl;
00039 if ( m_bGood && _mode == Write )
00040 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
00041 }
00042
00043 KoTarStore::KoTarStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
00044 {
00045 m_pTar = new KTar( dev );
00046
00047 m_bGood = init( mode );
00048
00049 if ( m_bGood && mode == Write )
00050 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
00051 }
00052
00053 KoTarStore::KoTarStore( QWidget* window, const KURL& _url, const QString & _filename, Mode _mode, const QCString & appIdentification )
00054 {
00055 kdDebug(s_area) << "KoTarStore Constructor url= " << _url.prettyURL()
00056 << " filename = " << _filename
00057 << " mode = " << int(_mode) << endl;
00058
00059 m_url = _url;
00060 m_window = window;
00061
00062 if ( _mode == KoStore::Read )
00063 {
00064 m_fileMode = KoStoreBase::RemoteRead;
00065 m_localFileName = _filename;
00066
00067 }
00068 else
00069 {
00070 m_fileMode = KoStoreBase::RemoteWrite;
00071 m_localFileName = "/tmp/kozip";
00072 }
00073
00074 m_pTar = new KTar( m_localFileName, "application/x-gzip" );
00075
00076 m_bGood = init( _mode );
00077
00078 if ( m_bGood && _mode == Write )
00079 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
00080 }
00081
00082 KoTarStore::~KoTarStore()
00083 {
00084 m_pTar->close();
00085 delete m_pTar;
00086
00087
00088 if ( m_fileMode == KoStoreBase::RemoteRead )
00089 {
00090 KIO::NetAccess::removeTempFile( m_localFileName );
00091 }
00092 else if ( m_fileMode == KoStoreBase::RemoteWrite )
00093 {
00094 KIO::NetAccess::upload( m_localFileName, m_url, m_window );
00095
00096 }
00097 }
00098
00099 QCString KoTarStore::completeMagic( const QCString& appMimetype )
00100 {
00101 kdDebug()<<"QCString KoTarStore::completeMagic( const QCString& appMimetype )********************\n";
00102 QCString res( "KOffice " );
00103 res += appMimetype;
00104 res += '\004';
00105 res += '\006';
00106 kdDebug()<<"sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
00107 kdDebug()<<" return :!!!!!!!!!!!!!!! :"<<res<<endl;
00108 return res;
00109 }
00110
00111 bool KoTarStore::init( Mode _mode )
00112 {
00113 KoStore::init( _mode );
00114 m_currentDir = 0;
00115 bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
00116
00117 if ( good && _mode == Read )
00118 good = m_pTar->directory() != 0;
00119 return good;
00120 }
00121
00122
00123
00124
00125 bool KoTarStore::openWrite( const QString& )
00126 {
00127
00128 m_byteArray.resize( 0 );
00129 m_stream = new QBuffer( m_byteArray );
00130 m_stream->open( IO_WriteOnly );
00131 return true;
00132 }
00133
00134 bool KoTarStore::openRead( const QString& name )
00135 {
00136 const KTarEntry * entry = m_pTar->directory()->entry( name );
00137 if ( entry == 0L )
00138 {
00139
00140
00141 return false;
00142 }
00143 if ( entry->isDirectory() )
00144 {
00145 kdWarning(s_area) << name << " is a directory !" << endl;
00146
00147 return false;
00148 }
00149 KTarFile * f = (KTarFile *) entry;
00150 m_byteArray.resize( 0 );
00151 delete m_stream;
00152 m_stream = f->device();
00153 m_iSize = f->size();
00154 return true;
00155 }
00156
00157 bool KoTarStore::closeWrite()
00158 {
00159
00160
00161 kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size "
00162 << m_iSize << endl;
00163 if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
00164 kdWarning( s_area ) << "Failed to write " << m_sName << endl;
00165 m_byteArray.resize( 0 );
00166 return true;
00167 }
00168
00169 bool KoTarStore::enterRelativeDirectory( const QString& dirName )
00170 {
00171 if ( m_mode == Read ) {
00172 if ( !m_currentDir ) {
00173 m_currentDir = m_pTar->directory();
00174 Q_ASSERT( m_currentPath.isEmpty() );
00175 }
00176 const KArchiveEntry *entry = m_currentDir->entry( dirName );
00177 if ( entry && entry->isDirectory() ) {
00178 m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
00179 return m_currentDir != 0;
00180 }
00181 return false;
00182 }
00183 else
00184 return true;
00185 }
00186
00187 bool KoTarStore::enterAbsoluteDirectory( const QString& path )
00188 {
00189 if ( path.isEmpty() )
00190 {
00191 m_currentDir = 0;
00192 return true;
00193 }
00194 if ( m_mode == Read ) {
00195 m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) );
00196 Q_ASSERT( m_currentDir );
00197 return m_currentDir != 0;
00198 }
00199 else
00200 return true;
00201 }
00202
00203 bool KoTarStore::fileExists( const QString& absPath ) const
00204 {
00205 return m_pTar->directory()->entry( absPath ) != 0;
00206 }
|