1
/* This file is part of the KDE libraries
2
Copyright (C) 2000 David Faure <faure@kde.org>
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 version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
18
#ifndef __kfilterdev_h
19
#define __kfilterdev_h
21
#include <QtCore/QIODevice>
22
#include <QtCore/QString>
28
* A class for reading and writing compressed data onto a device
29
* (e.g. file, but other usages are possible, like a buffer or a socket).
31
* To simply read/write compressed files, see deviceForFile.
33
* @author David Faure <faure@kde.org>
35
class KFilterDev : public QIODevice
39
* Destructs the KFilterDev.
40
* Calls close() if the filter device is still open.
42
virtual ~KFilterDev();
45
* Open for reading or writing.
46
* If the KFilterBase's device is not opened, it will be opened.
48
virtual bool open( QIODevice::OpenMode mode );
50
* Close after reading or writing.
51
* If the KFilterBase's device was opened by open(), it will be closed.
56
* For writing gzip compressed files only:
57
* set the name of the original file, to be used in the gzip header.
58
* @param fileName the name of the original file
60
void setOrigFileName( const QByteArray & fileName );
63
* Call this let this device skip the gzip headers when reading/writing.
64
* This way KFilterDev (with gzip filter) can be used as a direct wrapper
65
* around zlib - this is used by KZip.
67
void setSkipHeaders();
70
* That one can be quite slow, when going back. Use with care.
72
virtual bool seek( qint64 );
74
virtual bool atEnd() const;
76
/// Reimplemented to return true. KFilterDev is a sequential QIODevice.
77
/// Well, not really, since it supports seeking and KZip uses that.
78
//virtual bool isSequential() const { return true; }
83
// KDE4 TODO: turn those static methods into constructors
86
* Creates an i/o device that is able to read from @p fileName,
87
* whether it's compressed or not. Available compression filters
88
* (gzip/bzip2 etc.) will automatically be used.
90
* The compression filter to be used is determined from the @p fileName
91
* if @p mimetype is empty. Pass "application/x-gzip" or "application/x-bzip"
92
* to force the corresponding decompression filter, if available.
94
* Warning: application/x-bzip may not be available.
95
* In that case a QFile opened on the compressed data will be returned !
96
* Use KFilterBase::findFilterByMimeType and code similar to what
97
* deviceForFile is doing, to better control what's happening.
99
* The returned QIODevice has to be deleted after using.
101
* @param fileName the name of the file to filter
102
* @param mimetype the mime type of the file to filter, or QString() if unknown
103
* @param forceFilter if true, the function will either find a compression filter, or return 0.
104
* If false, it will always return a QIODevice. If no
105
* filter is available it will return a simple QFile.
106
* This can be useful if the file is usable without a filter.
107
* @return if a filter has been found, the QIODevice for the filter. If the
108
* filter does not exist, the return value depends on @p forceFilter.
109
* The returned QIODevice has to be deleted after using.
111
static QIODevice * deviceForFile( const QString & fileName, const QString & mimetype = QString(),
112
bool forceFilter = false );
115
* Creates an i/o device that is able to read from the QIODevice @p inDevice,
116
* whether the data is compressed or not. Available compression filters
117
* (gzip/bzip2 etc.) will automatically be used.
119
* The compression filter to be used is determined @p mimetype .
120
* Pass "application/x-gzip" or "application/x-bzip"
121
* to use the corresponding decompression filter.
123
* Warning: application/x-bzip may not be available.
124
* In that case 0 will be returned !
126
* The returned QIODevice has to be deleted after using.
127
* @param inDevice input device. Won't be deleted if @p autoDeleteInDevice = false
128
* @param mimetype the mime type for the filter
129
* @param autoDeleteInDevice if true, @p inDevice will be deleted automatically
130
* @return a QIODevice that filters the original stream. Must be deleted after
133
static QIODevice * device( QIODevice* inDevice, const QString & mimetype, bool autoDeleteInDevice = true );
136
virtual qint64 readData( char *data, qint64 maxlen );
137
virtual qint64 writeData( const char *data, qint64 len );
141
* Constructs a KFilterDev for a given filter (e.g. gzip, bzip2 etc.).
142
* @param filter the KFilterBase to use
143
* @param autoDeleteFilterBase when true this object will become the
144
* owner of @p filter.
146
explicit KFilterDev( KFilterBase * filter, bool autoDeleteFilterBase = false );