summaryrefslogtreecommitdiffstats
path: root/lib/store/KoTarStore.cpp
blob: 160258d95a43dc73b85013cf2321d5fc9eec8dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* This file is part of the KDE project
   Copyright (C) 2000-2002 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoTarStore.h"

#include <tqbuffer.h>

#include <ktar.h>
#include <kdebug.h>
#include <kurl.h>
#include <tdeversion.h>
#include <tdeio/netaccess.h>

KoTarStore::KoTarStore( const TQString & _filename, Mode _mode, const TQCString & appIdentification )
{
    kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename
                    << " mode = " << int(_mode) << endl;

    m_pTar = new KTar( _filename, "application/x-gzip" );

    m_bGood = init( _mode ); // open the targz file and init some vars
    kdDebug()<<"appIdentification :"<<appIdentification<<endl;
    if ( m_bGood && _mode == Write )
        m_pTar->setOrigFileName( completeMagic( appIdentification ) );
}

KoTarStore::KoTarStore( TQIODevice *dev, Mode mode, const TQCString & appIdentification )
{
    m_pTar = new KTar( dev );

    m_bGood = init( mode );

    if ( m_bGood && mode == Write )
        m_pTar->setOrigFileName( completeMagic( appIdentification ) );
}

KoTarStore::KoTarStore( TQWidget* window, const KURL& _url, const TQString & _filename, Mode _mode, const TQCString & appIdentification )
{
    kdDebug(s_area) << "KoTarStore Constructor url= " << _url.prettyURL()
                    << " filename = " << _filename
                    << " mode = " << int(_mode) << endl;

    m_url = _url;
    m_window = window;

    if ( _mode == KoStore::Read )
    {
        m_fileMode = KoStoreBase::RemoteRead;
        m_localFileName = _filename;

    }
    else
    {
        m_fileMode = KoStoreBase::RemoteWrite;
        m_localFileName = "/tmp/kozip"; // ### FIXME with KTempFile
    }

    m_pTar = new KTar( m_localFileName, "application/x-gzip" );

    m_bGood = init( _mode ); // open the targz file and init some vars

    if ( m_bGood && _mode == Write )
        m_pTar->setOrigFileName( completeMagic( appIdentification ) );
}

KoTarStore::~KoTarStore()
{
    m_pTar->close();
    delete m_pTar;

    // Now we have still some job to do for remote files.
    if ( m_fileMode == KoStoreBase::RemoteRead )
    {
        TDEIO::NetAccess::removeTempFile( m_localFileName );
    }
    else if ( m_fileMode == KoStoreBase::RemoteWrite )
    {
        TDEIO::NetAccess::upload( m_localFileName, m_url, m_window );
        // ### FIXME: delete temp file
    }
}

TQCString KoTarStore::completeMagic( const TQCString& appMimetype )
{
    kdDebug()<<"TQCString KoTarStore::completeMagic( const TQCString& appMimetype )********************\n";
    TQCString res( "KOffice " );
    res += appMimetype;
    res += '\004'; // Two magic bytes to make the identification
    res += '\006'; // more reliable (DF)
    kdDebug()<<"sssssssssssssssssssssxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
    kdDebug()<<" return :!!!!!!!!!!!!!!! :"<<res<<endl;
    return res;
}

bool KoTarStore::init( Mode _mode )
{
    KoStore::init( _mode );
    m_currentDir = 0;
    bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );

    if ( good && _mode == Read )
        good = m_pTar->directory() != 0;
    return good;
}

// When reading, m_stream comes directly from KArchiveFile::device()
// When writing, m_stream buffers the data into m_byteArray

bool KoTarStore::openWrite( const TQString& /*name*/ )
{
    // Prepare memory buffer for writing
    m_byteArray.resize( 0 );
    m_stream = new TQBuffer( m_byteArray );
    m_stream->open( IO_WriteOnly );
    return true;
}

bool KoTarStore::openRead( const TQString& name )
{
    const KTarEntry * entry = m_pTar->directory()->entry( name );
    if ( entry == 0L )
    {
        //kdWarning(s_area) << "Unknown filename " << name << endl;
        //return TDEIO::ERR_DOES_NOT_EXIST;
        return false;
    }
    if ( entry->isDirectory() )
    {
        kdWarning(s_area) << name << " is a directory !" << endl;
        //return TDEIO::ERR_IS_DIRECTORY;
        return false;
    }
    KTarFile * f = (KTarFile *) entry;
    m_byteArray.resize( 0 );
    delete m_stream;
    m_stream = f->device();
    m_iSize = f->size();
    return true;
}

bool KoTarStore::closeWrite()
{
    // write the whole bytearray at once into the tar file

    kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size "
                    << m_iSize << endl;
    if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
        kdWarning( s_area ) << "Failed to write " << m_sName << endl;
    m_byteArray.resize( 0 ); // save memory
    return true;
}

bool KoTarStore::enterRelativeDirectory( const TQString& dirName )
{
    if ( m_mode == Read ) {
        if ( !m_currentDir ) {
            m_currentDir = m_pTar->directory(); // initialize
            Q_ASSERT( m_currentPath.isEmpty() );
        }
        const KArchiveEntry *entry = m_currentDir->entry( dirName );
        if ( entry && entry->isDirectory() ) {
            m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
            return m_currentDir != 0;
        }
        return false;
    }
    else  // Write, no checking here
        return true;
}

bool KoTarStore::enterAbsoluteDirectory( const TQString& path )
{
    if ( path.isEmpty() )
    {
        m_currentDir = 0;
        return true;
    }
    if ( m_mode == Read ) {
        m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) );
        Q_ASSERT( m_currentDir );
        return m_currentDir != 0;
    }
    else
        return true;
}

bool KoTarStore::fileExists( const TQString& absPath ) const
{
    return m_pTar->directory()->entry( absPath ) != 0;
}