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 
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 ); // open the targz file and init some vars
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"; // ### FIXME with KTempFile
00072     }
00073 
00074     m_pTar = new KTar( m_localFileName, "application/x-gzip" );
00075 
00076     m_bGood = init( _mode ); // open the targz file and init some vars
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     // Now we have still some job to do for remote files.
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         // ### FIXME: delete temp file
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'; // Two magic bytes to make the identification
00105     res += '\006'; // more reliable (DF)
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 // When reading, m_stream comes directly from KArchiveFile::device()
00123 // When writing, m_stream buffers the data into m_byteArray
00124 
00125 bool KoTarStore::openWrite( const QString& /*name*/ )
00126 {
00127     // Prepare memory buffer for writing
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         //kdWarning(s_area) << "Unknown filename " << name << endl;
00140         //return KIO::ERR_DOES_NOT_EXIST;
00141         return false;
00142     }
00143     if ( entry->isDirectory() )
00144     {
00145         kdWarning(s_area) << name << " is a directory !" << endl;
00146         //return KIO::ERR_IS_DIRECTORY;
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     // write the whole bytearray at once into the tar file
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 ); // save memory
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(); // initialize
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  // Write, no checking here
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 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:08 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003