lib

KoTarStore.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 "KoTarStore.h"
21
22#include <tqbuffer.h>
23
24#include <ktar.h>
25#include <kdebug.h>
26#include <kurl.h>
27#include <tdeio/netaccess.h>
28
29KoTarStore::KoTarStore( const TQString & _filename, Mode _mode, const TQCString & appIdentification )
30{
31 kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename
32 << " mode = " << int(_mode) << endl;
33
34 m_pTar = new KTar( _filename, "application/x-gzip" );
35
36 m_bGood = init( _mode ); // open the targz file and init some vars
37 kdDebug()<<"appIdentification :"<<appIdentification<<endl;
38 if ( m_bGood && _mode == Write )
39 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
40}
41
42KoTarStore::KoTarStore( TQIODevice *dev, Mode mode, const TQCString & appIdentification )
43{
44 m_pTar = new KTar( dev );
45
46 m_bGood = init( mode );
47
48 if ( m_bGood && mode == Write )
49 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
50}
51
52KoTarStore::KoTarStore( TQWidget* window, const KURL& _url, const TQString & _filename, Mode _mode, const TQCString & appIdentification )
53{
54 kdDebug(s_area) << "KoTarStore Constructor url= " << _url.prettyURL()
55 << " filename = " << _filename
56 << " mode = " << int(_mode) << endl;
57
58 m_url = _url;
59 m_window = window;
60
61 if ( _mode == KoStore::Read )
62 {
63 m_fileMode = KoStoreBase::RemoteRead;
64 m_localFileName = _filename;
65
66 }
67 else
68 {
69 m_fileMode = KoStoreBase::RemoteWrite;
70 m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
71 }
72
73 m_pTar = new KTar( m_localFileName, "application/x-gzip" );
74
75 m_bGood = init( _mode ); // open the targz file and init some vars
76
77 if ( m_bGood && _mode == Write )
78 m_pTar->setOrigFileName( completeMagic( appIdentification ) );
79}
80
81KoTarStore::~KoTarStore()
82{
83 m_pTar->close();
84 delete m_pTar;
85
86 // Now we have still some job to do for remote files.
87 if ( m_fileMode == KoStoreBase::RemoteRead )
88 {
89 TDEIO::NetAccess::removeTempFile( m_localFileName );
90 }
91 else if ( m_fileMode == KoStoreBase::RemoteWrite )
92 {
93 TDEIO::NetAccess::upload( m_localFileName, m_url, m_window );
94 // ### FIXME: delete temp file
95 }
96}
97
98TQCString KoTarStore::completeMagic( const TQCString& appMimetype )
99{
100 kdDebug()<<"TQCString KoTarStore::completeMagic( const TQCString& appMimetype )********************\n";
101 TQCString res( "KOffice " );
102 res += appMimetype;
103 res += '\004'; // Two magic bytes to make the identification
104 res += '\006'; // more reliable (DF)
105 kdDebug()<<"sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
106 kdDebug()<<" return :!!!!!!!!!!!!!!! :"<<res<<endl;
107 return res;
108}
109
110bool KoTarStore::init( Mode _mode )
111{
112 KoStore::init( _mode );
113 m_currentDir = 0;
114 bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
115
116 if ( good && _mode == Read )
117 good = m_pTar->directory() != 0;
118 return good;
119}
120
121// When reading, m_stream comes directly from KArchiveFile::device()
122// When writing, m_stream buffers the data into m_byteArray
123
124bool KoTarStore::openWrite( const TQString& /*name*/ )
125{
126 // Prepare memory buffer for writing
127 m_byteArray.resize( 0 );
128 m_stream = new TQBuffer( m_byteArray );
129 m_stream->open( IO_WriteOnly );
130 return true;
131}
132
133bool KoTarStore::openRead( const TQString& name )
134{
135 const KTarEntry * entry = m_pTar->directory()->entry( name );
136 if ( entry == 0L )
137 {
138 //kdWarning(s_area) << "Unknown filename " << name << endl;
139 //return TDEIO::ERR_DOES_NOT_EXIST;
140 return false;
141 }
142 if ( entry->isDirectory() )
143 {
144 kdWarning(s_area) << name << " is a directory !" << endl;
145 //return TDEIO::ERR_IS_DIRECTORY;
146 return false;
147 }
148 KTarFile * f = (KTarFile *) entry;
149 m_byteArray.resize( 0 );
150 delete m_stream;
151 m_stream = f->device();
152 m_iSize = f->size();
153 return true;
154}
155
157{
158 // write the whole bytearray at once into the tar file
159
160 kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size "
161 << m_iSize << endl;
162 if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
163 kdWarning( s_area ) << "Failed to write " << m_sName << endl;
164 m_byteArray.resize( 0 ); // save memory
165 return true;
166}
167
168bool KoTarStore::enterRelativeDirectory( const TQString& dirName )
169{
170 if ( m_mode == Read ) {
171 if ( !m_currentDir ) {
172 m_currentDir = m_pTar->directory(); // initialize
173 Q_ASSERT( m_currentPath.isEmpty() );
174 }
175 const KArchiveEntry *entry = m_currentDir->entry( dirName );
176 if ( entry && entry->isDirectory() ) {
177 m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
178 return m_currentDir != 0;
179 }
180 return false;
181 }
182 else // Write, no checking here
183 return true;
184}
185
186bool KoTarStore::enterAbsoluteDirectory( const TQString& path )
187{
188 if ( path.isEmpty() )
189 {
190 m_currentDir = 0;
191 return true;
192 }
193 if ( m_mode == Read ) {
194 m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) );
195 Q_ASSERT( m_currentDir );
196 return m_currentDir != 0;
197 }
198 else
199 return true;
200}
201
202bool KoTarStore::fileExists( const TQString& absPath ) const
203{
204 return m_pTar->directory()->entry( absPath ) != 0;
205}
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)