~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to lib/store/koTarStore.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2000-2002 David Faure <david@mandrakesoft.com>
 
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., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "koTarStore.h"
 
21
#include <ktar.h>
 
22
#include <kdebug.h>
 
23
#include <qbuffer.h>
 
24
 
 
25
KoTarStore::KoTarStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
 
26
{
 
27
    kdDebug(s_area) << "KoTarStore Constructor filename = " << _filename
 
28
                    << " mode = " << int(_mode) << endl;
 
29
 
 
30
    m_pTar = new KTar( _filename, "application/x-gzip" );
 
31
 
 
32
    m_bGood = init( _mode ); // open the targz file and init some vars
 
33
 
 
34
    if ( m_bGood && _mode == Write )
 
35
        m_pTar->setOrigFileName( completeMagic( appIdentification ) );
 
36
}
 
37
 
 
38
KoTarStore::KoTarStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
 
39
{
 
40
    m_pTar = new KTar( dev );
 
41
 
 
42
    m_bGood = init( mode );
 
43
 
 
44
    if ( m_bGood && mode == Write )
 
45
        m_pTar->setOrigFileName( completeMagic( appIdentification ) );
 
46
}
 
47
 
 
48
KoTarStore::~KoTarStore()
 
49
{
 
50
    m_pTar->close();
 
51
    delete m_pTar;
 
52
}
 
53
 
 
54
QCString KoTarStore::completeMagic( const QCString& appMimetype )
 
55
{
 
56
    QCString res( "KOffice " );
 
57
    res += appMimetype;
 
58
    res += '\004'; // Two magic bytes to make the identification
 
59
    res += '\006'; // more reliable (DF)
 
60
    return res;
 
61
}
 
62
 
 
63
bool KoTarStore::init( Mode _mode )
 
64
{
 
65
    KoStore::init( _mode );
 
66
    m_currentDir = 0;
 
67
    bool good = m_pTar->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
 
68
 
 
69
    if ( good && _mode == Read )
 
70
        good = m_pTar->directory() != 0;
 
71
    return good;
 
72
}
 
73
 
 
74
// When reading, m_stream comes directly from KArchiveFile::device()
 
75
// When writing, m_stream buffers the data into m_byteArray
 
76
 
 
77
bool KoTarStore::openWrite( const QString& /*name*/ )
 
78
{
 
79
    // Prepare memory buffer for writing
 
80
    m_byteArray.resize( 0 );
 
81
    m_stream = new QBuffer( m_byteArray );
 
82
    m_stream->open( IO_WriteOnly );
 
83
    return true;
 
84
}
 
85
 
 
86
bool KoTarStore::openRead( const QString& name )
 
87
{
 
88
    const KTarEntry * entry = m_pTar->directory()->entry( name );
 
89
    if ( entry == 0L )
 
90
    {
 
91
        //kdWarning(s_area) << "Unknown filename " << name << endl;
 
92
        //return KIO::ERR_DOES_NOT_EXIST;
 
93
        return false;
 
94
    }
 
95
    if ( entry->isDirectory() )
 
96
    {
 
97
        kdWarning(s_area) << name << " is a directory !" << endl;
 
98
        //return KIO::ERR_IS_DIRECTORY;
 
99
        return false;
 
100
    }
 
101
    KTarFile * f = (KTarFile *) entry;
 
102
    m_byteArray.resize( 0 );
 
103
    delete m_stream;
 
104
    m_stream = f->device();
 
105
    m_iSize = f->size();
 
106
    return true;
 
107
}
 
108
 
 
109
bool KoTarStore::closeWrite()
 
110
{
 
111
    // write the whole bytearray at once into the tar file
 
112
 
 
113
    kdDebug(s_area) << "Writing file " << m_sName << " into TAR archive. size "
 
114
                    << m_iSize << endl;
 
115
    if ( !m_pTar->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
 
116
        kdWarning( s_area ) << "Failed to write " << m_sName << endl;
 
117
    m_byteArray.resize( 0 ); // save memory
 
118
    return true;
 
119
}
 
120
 
 
121
bool KoTarStore::enterRelativeDirectory( const QString& dirName )
 
122
{
 
123
    if ( m_mode == Read ) {
 
124
        if ( !m_currentDir ) {
 
125
            m_currentDir = m_pTar->directory(); // initialize
 
126
            Q_ASSERT( m_currentPath.isEmpty() );
 
127
        }
 
128
        const KArchiveEntry *entry = m_currentDir->entry( dirName );
 
129
        if ( entry && entry->isDirectory() ) {
 
130
            m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
 
131
            return m_currentDir != 0;
 
132
        }
 
133
        return false;
 
134
    }
 
135
    else  // Write, no checking here
 
136
        return true;
 
137
}
 
138
 
 
139
bool KoTarStore::enterAbsoluteDirectory( const QString& path )
 
140
{
 
141
    if ( path.isEmpty() )
 
142
    {
 
143
        m_currentDir = 0;
 
144
        return true;
 
145
    }
 
146
    if ( m_mode == Read ) {
 
147
        m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pTar->directory()->entry( path ) );
 
148
        Q_ASSERT( m_currentDir );
 
149
        return m_currentDir != 0;
 
150
    }
 
151
    else
 
152
        return true;
 
153
}
 
154
 
 
155
bool KoTarStore::fileExists( const QString& absPath )
 
156
{
 
157
    return m_pTar->directory()->entry( absPath ) != 0;
 
158
}