lib Library API Documentation

koZipStore.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 "koZipStore.h"
00021 
00022 #include <qbuffer.h>
00023 
00024 #include <kzip.h>
00025 #include <kdebug.h>
00026 #include <kdeversion.h>
00027 #include <kurl.h>
00028 #include <kio/netaccess.h>
00029 #if ! KDE_IS_VERSION( 3, 4, 1 )
00030 #include <qdir.h>
00031 #include <qfileinfo.h>
00032 #endif
00033 
00034 KoZipStore::KoZipStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
00035 {
00036     kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename
00037                     << " mode = " << int(_mode)
00038                     << " mimetype = " << appIdentification << endl;
00039 
00040     m_pZip = new KZip( _filename );
00041 
00042 #if ! KDE_IS_VERSION( 3, 4, 1 )
00043     // Workaround for KZip KSaveFile double deletion in kdelibs-3.4,
00044     // when trying to write to a non-writable directory.
00045     QDir dir( QFileInfo( _filename ).dir() );
00046     if (_mode == Write && !QFileInfo( dir.path() ).isWritable()  )
00047     {
00048         kdWarning(s_area) << dir.path() << " isn't writable" << endl;
00049         m_bGood = false;
00050         m_currentDir = 0;
00051         KoStore::init( _mode );
00052     }
00053     else
00054 #endif
00055     {
00056         m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars
00057     }
00058 }
00059 
00060 KoZipStore::KoZipStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
00061 {
00062     m_pZip = new KZip( dev );
00063     m_bGood = init( mode, appIdentification );
00064 }
00065 
00066 KoZipStore::KoZipStore( QWidget* window, const KURL & _url, const QString & _filename, Mode _mode, const QCString & appIdentification )
00067 {
00068     kdDebug(s_area) << "KoZipStore Constructor url" << _url.prettyURL()
00069                     << " filename = " << _filename
00070                     << " mode = " << int(_mode)
00071                     << " mimetype = " << appIdentification << endl;
00072 
00073     m_url = _url;
00074     m_window = window;
00075 
00076     if ( _mode == KoStore::Read )
00077     {
00078         m_fileMode = KoStoreBase::RemoteRead;
00079         m_localFileName = _filename;
00080 
00081     }
00082     else
00083     {
00084         m_fileMode = KoStoreBase::RemoteWrite;
00085         m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
00086     }
00087 
00088     m_pZip = new KZip( m_localFileName );
00089     m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars
00090 }
00091 
00092 KoZipStore::~KoZipStore()
00093 {
00094     kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl;
00095     m_pZip->close();
00096     delete m_pZip;
00097 
00098     // Now we have still some job to do for remote files.
00099     if ( m_fileMode == KoStoreBase::RemoteRead )
00100     {
00101         KIO::NetAccess::removeTempFile( m_localFileName );
00102     }
00103     else if ( m_fileMode == KoStoreBase::RemoteWrite )
00104     {
00105         KIO::NetAccess::upload( m_localFileName, m_url, m_window );
00106         // ### FIXME: delete temp file
00107     }
00108 }
00109 
00110 bool KoZipStore::init( Mode _mode, const QCString& appIdentification )
00111 {
00112     KoStore::init( _mode );
00113     m_currentDir = 0;
00114     bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
00115 
00116     if ( good && _mode == Read )
00117         good = m_pZip->directory() != 0;
00118     else if ( good && _mode == Write )
00119     {
00120         //kdDebug(s_area) << "KoZipStore::init writing mimetype " << appIdentification << endl;
00121 
00122         m_pZip->setCompression( KZip::NoCompression );
00123         m_pZip->setExtraField( KZip::NoExtraField );
00124         // Write identification
00125         (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() );
00126         m_pZip->setCompression( KZip::DeflateCompression );
00127         // We don't need the extra field in KOffice - so we leave it as "no extra field".
00128     }
00129     return good;
00130 }
00131 
00132 bool KoZipStore::openWrite( const QString& name )
00133 {
00134 #if 0
00135     // Prepare memory buffer for writing
00136     m_byteArray.resize( 0 );
00137     m_stream = new QBuffer( m_byteArray );
00138     m_stream->open( IO_WriteOnly );
00139     return true;
00140 #endif
00141     m_stream = 0L; // Don't use!
00142     return m_pZip->prepareWriting( name, "", "" /*m_pZip->rootDir()->user(), m_pZip->rootDir()->group()*/, 0 );
00143 }
00144 
00145 bool KoZipStore::openRead( const QString& name )
00146 {
00147     const KArchiveEntry * entry = m_pZip->directory()->entry( name );
00148     if ( entry == 0L )
00149     {
00150         //kdWarning(s_area) << "Unknown filename " << name << endl;
00151         //return KIO::ERR_DOES_NOT_EXIST;
00152         return false;
00153     }
00154     if ( entry->isDirectory() )
00155     {
00156         kdWarning(s_area) << name << " is a directory !" << endl;
00157         //return KIO::ERR_IS_DIRECTORY;
00158         return false;
00159     }
00160     // Must cast to KZipFileEntry, not only KArchiveFile, because device() isn't virtual!
00161     const KZipFileEntry * f = static_cast<const KZipFileEntry *>(entry);
00162     delete m_stream;
00163     m_stream = f->device();
00164     m_iSize = f->size();
00165     return true;
00166 }
00167 
00168 Q_LONG KoZipStore::write( const char* _data, Q_ULONG _len )
00169 {
00170   if ( _len == 0L ) return 0;
00171   //kdDebug(s_area) << "KoZipStore::write " << _len << endl;
00172 
00173   if ( !m_bIsOpen )
00174   {
00175     kdError(s_area) << "KoStore: You must open before writing" << endl;
00176     return 0L;
00177   }
00178   if ( m_mode != Write  )
00179   {
00180     kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl;
00181     return 0L;
00182   }
00183 
00184   m_iSize += _len;
00185   if ( m_pZip->writeData( _data, _len ) ) // writeData returns a bool!
00186       return _len;
00187   return 0L;
00188 }
00189 
00190 bool KoZipStore::closeWrite()
00191 {
00192     kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size "
00193                     << m_iSize << endl;
00194     return m_pZip->doneWriting( m_iSize );
00195 #if 0
00196     if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
00197         kdWarning( s_area ) << "Failed to write " << m_sName << endl;
00198     m_byteArray.resize( 0 ); // save memory
00199     return true;
00200 #endif
00201 }
00202 
00203 bool KoZipStore::enterRelativeDirectory( const QString& dirName )
00204 {
00205     if ( m_mode == Read ) {
00206         if ( !m_currentDir ) {
00207             m_currentDir = m_pZip->directory(); // initialize
00208             Q_ASSERT( m_currentPath.isEmpty() );
00209         }
00210         const KArchiveEntry *entry = m_currentDir->entry( dirName );
00211         if ( entry && entry->isDirectory() ) {
00212             m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
00213             return m_currentDir != 0;
00214         }
00215         return false;
00216     }
00217     else  // Write, no checking here
00218         return true;
00219 }
00220 
00221 bool KoZipStore::enterAbsoluteDirectory( const QString& path )
00222 {
00223     if ( path.isEmpty() )
00224     {
00225         m_currentDir = 0;
00226         return true;
00227     }
00228     m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pZip->directory()->entry( path ) );
00229     Q_ASSERT( m_currentDir );
00230     return m_currentDir != 0;
00231 }
00232 
00233 bool KoZipStore::fileExists( const QString& absPath ) const
00234 {
00235     const KArchiveEntry *entry = m_pZip->directory()->entry( absPath );
00236     return entry && entry->isFile();
00237 }
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:14 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003