lib

KoZipStore.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2000-2002 David Faure <faure@kde.org>
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KoZipStore.h"
21
22#include <tqbuffer.h>
23
24#include <kzip.h>
25#include <kdebug.h>
26#include <kurl.h>
27#include <tdeio/netaccess.h>
28
29KoZipStore::KoZipStore( const TQString & _filename, Mode _mode, const TQCString & appIdentification )
30{
31 kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename
32 << " mode = " << int(_mode)
33 << " mimetype = " << appIdentification << endl;
34
35 m_pZip = new KZip( _filename );
36 m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars
37}
38
39KoZipStore::KoZipStore( TQIODevice *dev, Mode mode, const TQCString & appIdentification )
40{
41 m_pZip = new KZip( dev );
42 m_bGood = init( mode, appIdentification );
43}
44
45KoZipStore::KoZipStore( TQWidget* window, const KURL & _url, const TQString & _filename, Mode _mode, const TQCString & appIdentification )
46{
47 kdDebug(s_area) << "KoZipStore Constructor url" << _url.prettyURL()
48 << " filename = " << _filename
49 << " mode = " << int(_mode)
50 << " mimetype = " << appIdentification << endl;
51
52 m_url = _url;
53 m_window = window;
54
55 if ( _mode == KoStore::Read )
56 {
57 m_fileMode = KoStoreBase::RemoteRead;
58 m_localFileName = _filename;
59
60 }
61 else
62 {
63 m_fileMode = KoStoreBase::RemoteWrite;
64 m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
65 }
66
67 m_pZip = new KZip( m_localFileName );
68 m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars
69}
70
71KoZipStore::~KoZipStore()
72{
73 kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl;
74 m_pZip->close();
75 delete m_pZip;
76
77 // Now we have still some job to do for remote files.
78 if ( m_fileMode == KoStoreBase::RemoteRead )
79 {
80 TDEIO::NetAccess::removeTempFile( m_localFileName );
81 }
82 else if ( m_fileMode == KoStoreBase::RemoteWrite )
83 {
84 TDEIO::NetAccess::upload( m_localFileName, m_url, m_window );
85 // ### FIXME: delete temp file
86 }
87}
88
89bool KoZipStore::init( Mode _mode, const TQCString& appIdentification )
90{
91 KoStore::init( _mode );
92 m_currentDir = 0;
93 bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
94
95 if ( good && _mode == Read )
96 good = m_pZip->directory() != 0;
97 else if ( good && _mode == Write )
98 {
99 //kdDebug(s_area) << "KoZipStore::init writing mimetype " << appIdentification << endl;
100
101 m_pZip->setCompression( KZip::NoCompression );
102 m_pZip->setExtraField( KZip::NoExtraField );
103 // Write identification
104 (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() );
105 m_pZip->setCompression( KZip::DeflateCompression );
106 // We don't need the extra field in KOffice - so we leave it as "no extra field".
107 }
108 return good;
109}
110
111bool KoZipStore::openWrite( const TQString& name )
112{
113#if 0
114 // Prepare memory buffer for writing
115 m_byteArray.resize( 0 );
116 m_stream = new TQBuffer( m_byteArray );
117 m_stream->open( IO_WriteOnly );
118 return true;
119#endif
120 m_stream = 0L; // Don't use!
121 return m_pZip->prepareWriting( name, "", "" /*m_pZip->rootDir()->user(), m_pZip->rootDir()->group()*/, 0 );
122}
123
124bool KoZipStore::openRead( const TQString& name )
125{
126 const KArchiveEntry * entry = m_pZip->directory()->entry( name );
127 if ( entry == 0L )
128 {
129 //kdWarning(s_area) << "Unknown filename " << name << endl;
130 //return TDEIO::ERR_DOES_NOT_EXIST;
131 return false;
132 }
133 if ( entry->isDirectory() )
134 {
135 kdWarning(s_area) << name << " is a directory !" << endl;
136 //return TDEIO::ERR_IS_DIRECTORY;
137 return false;
138 }
139 // Must cast to KZipFileEntry, not only KArchiveFile, because device() isn't virtual!
140 const KZipFileEntry * f = static_cast<const KZipFileEntry *>(entry);
141 delete m_stream;
142 m_stream = f->device();
143 m_iSize = f->size();
144 return true;
145}
146
147TQ_LONG KoZipStore::write( const char* _data, TQ_ULONG _len )
148{
149 if ( _len == 0L ) return 0;
150 //kdDebug(s_area) << "KoZipStore::write " << _len << endl;
151
152 if ( !m_bIsOpen )
153 {
154 kdError(s_area) << "KoStore: You must open before writing" << endl;
155 return 0L;
156 }
157 if ( m_mode != Write )
158 {
159 kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl;
160 return 0L;
161 }
162
163 m_iSize += _len;
164 if ( m_pZip->writeData( _data, _len ) ) // writeData returns a bool!
165 return _len;
166 return 0L;
167}
168
170{
171 kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size "
172 << m_iSize << endl;
173 return m_pZip->doneWriting( m_iSize );
174#if 0
175 if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
176 kdWarning( s_area ) << "Failed to write " << m_sName << endl;
177 m_byteArray.resize( 0 ); // save memory
178 return true;
179#endif
180}
181
182bool KoZipStore::enterRelativeDirectory( const TQString& dirName )
183{
184 if ( m_mode == Read ) {
185 if ( !m_currentDir ) {
186 m_currentDir = m_pZip->directory(); // initialize
187 Q_ASSERT( m_currentPath.isEmpty() );
188 }
189 const KArchiveEntry *entry = m_currentDir->entry( dirName );
190 if ( entry && entry->isDirectory() ) {
191 m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
192 return m_currentDir != 0;
193 }
194 return false;
195 }
196 else // Write, no checking here
197 return true;
198}
199
200bool KoZipStore::enterAbsoluteDirectory( const TQString& path )
201{
202 if ( path.isEmpty() )
203 {
204 m_currentDir = 0;
205 return true;
206 }
207 m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pZip->directory()->entry( path ) );
208 Q_ASSERT( m_currentDir );
209 return m_currentDir != 0;
210}
211
212bool KoZipStore::fileExists( const TQString& absPath ) const
213{
214 const KArchiveEntry *entry = m_pZip->directory()->entry( absPath );
215 return entry && entry->isFile();
216}
KURL m_url
original URL of the remote file (undefined for a local file)
Definition KoStoreBase.h:45
virtual bool closeWrite()=0
TQStringList m_currentPath
The "current directory" (path).
Definition KoStore.h:352
bool m_bGood
Must be set by the constructor.
Definition KoStore.h:367
TQString m_sName
Current filename (between an open() and a close()).
Definition KoStore.h:358
TQIODevice::Offset m_iSize
Current size of the file named m_sName.
Definition KoStore.h:360
virtual bool init(Mode mode)
Init store - called by constructor.
Definition KoStore.cpp:170
Mode mode() const
Definition KoStore.h:163
TQIODevice * m_stream
The stream for the current read or write operation.
Definition KoStore.h:363
const char * name(StdAction id)