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