File indexing completed on 2024-04-28 11:33:46

0001 /* This file is part of the KDE libraries
0002    SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #ifndef __kfilterdev_h
0007 #define __kfilterdev_h
0008 
0009 #include <karchive_export.h>
0010 #include <kcompressiondevice.h>
0011 
0012 #include <QString>
0013 
0014 #if KARCHIVE_ENABLE_DEPRECATED_SINCE(5, 85)
0015 
0016 class QFile;
0017 class KFilterBase;
0018 
0019 /**
0020  * @class KFilterDev kfilterdev.h KFilterDev
0021  *
0022  * A class for reading and writing compressed data onto a device
0023  * (e.g. file, but other usages are possible, like a buffer or a socket).
0024  *
0025  * To simply read/write compressed files, see deviceForFile.
0026  *
0027  * KFilterDev adds MIME type support to KCompressionDevice, and also
0028  * provides compatibility methods for KDE 4 code.
0029  *
0030  * @author David Faure <faure@kde.org>
0031  * @deprecated Since 5.85, use KCompressionDevice directly
0032  */
0033 class KARCHIVE_EXPORT KFilterDev : public KCompressionDevice
0034 {
0035     Q_OBJECT
0036 public:
0037     /**
0038      * Constructs a KFilterDev for a given FileName.
0039      * @param fileName the name of the file to filter.
0040      * @since 5.0
0041      * @deprecated Since 5.85, use KCompressionDevice(const QString &)
0042      */
0043     KFilterDev(const QString &fileName);
0044 
0045     /**
0046      * Returns the compression type for the given mimetype, if possible. Otherwise returns None.
0047      * This handles simple cases like application/gzip, but also application/x-compressed-tar, and inheritance.
0048      * @deprecated Since 5.85, use KCompressionDevice::compressionTypeForMimeType(const QString &)
0049      */
0050     KARCHIVE_DEPRECATED_VERSION(5, 85, "Use KCompressionDevice::compressionTypeForMimeType(const QString &)")
0051     static CompressionType compressionTypeForMimeType(const QString &mimetype);
0052 
0053 #if KARCHIVE_ENABLE_DEPRECATED_SINCE(5, 0)
0054     /**
0055      * @deprecated Since 5.0, use the constructor instead (if mimetype is empty), or KCompressionDevice (if
0056      * the mimetype is known).
0057      *
0058      * Use:
0059      * KFilterDev dev(fileName)
0060      * instead of:
0061      * QIODevice * dev = KFilterDev::deviceForFile(fileName);
0062      *
0063      * If the mimetype was specified explicitly, use:
0064      * KCompressionDevice dev(fileName, KCompressionDevice::GZip);
0065      * instead of:
0066      * QIODevice * dev = KFilterDev::deviceForFile(fileName, "application/gzip");
0067      *
0068      * Creates an i/o device that is able to read from @p fileName,
0069      * whether it's compressed or not. Available compression filters
0070      * (gzip/bzip2 etc.) will automatically be used.
0071      *
0072      * The compression filter to be used is determined from the @p fileName
0073      * if @p mimetype is empty. Pass "application/gzip" or "application/x-bzip"
0074      * to force the corresponding decompression filter, if available.
0075      *
0076      * Warning: application/x-bzip may not be available.
0077      * In that case a QFile opened on the compressed data will be returned !
0078      * Use KFilterBase::findFilterByMimeType and code similar to what
0079      * deviceForFile is doing, to better control what's happening.
0080      *
0081      * The returned QIODevice has to be deleted after using.
0082      *
0083      * @param fileName the name of the file to filter
0084      * @param mimetype the mime type of the file to filter, or QString() if unknown
0085      * @param forceFilter if true, the function will either find a compression filter, or return 0.
0086      *                    If false, it will always return a QIODevice. If no
0087      *                    filter is available it will return a simple QFile.
0088      *                    This can be useful if the file is usable without a filter.
0089      * @return if a filter has been found, the KCompressionDevice for the filter. If the
0090      *         filter does not exist, the return value depends on @p forceFilter.
0091      *         The returned KCompressionDevice has to be deleted after using.
0092      */
0093     KARCHIVE_DEPRECATED_VERSION(5, 0, "See API docs")
0094     static KCompressionDevice *deviceForFile(const QString &fileName, const QString &mimetype = QString(), bool forceFilter = false)
0095     {
0096         KCompressionDevice *device;
0097         if (mimetype.isEmpty()) {
0098             device = new KFilterDev(fileName);
0099         } else {
0100             device = new KCompressionDevice(fileName, compressionTypeForMimeType(mimetype));
0101         }
0102         if (device->compressionType() == KCompressionDevice::None && forceFilter) {
0103             delete device;
0104             return nullptr;
0105         } else {
0106             return device;
0107         }
0108     }
0109 #endif
0110 
0111 #if KARCHIVE_ENABLE_DEPRECATED_SINCE(5, 0)
0112     /**
0113      * @deprecated Since 5.0, use KCompressionDevice
0114      *
0115      * Use:
0116      * KCompressionDevice::CompressionType type = KFilterDev::compressionTypeForMimeType(mimeType);
0117      * KCompressionDevice flt(&file, false, type);
0118      * instead of:
0119      * QIODevice *flt = KFilterDev::device(&file, mimeType, false);
0120      *
0121      * Creates an i/o device that is able to read from the QIODevice @p inDevice,
0122      * whether the data is compressed or not. Available compression filters
0123      * (gzip/bzip2 etc.) will automatically be used.
0124      *
0125      * The compression filter to be used is determined @p mimetype .
0126      * Pass "application/gzip" or "application/x-bzip"
0127      * to use the corresponding decompression filter.
0128      *
0129      * Warning: application/x-bzip may not be available.
0130      * In that case 0 will be returned !
0131      *
0132      * The returned QIODevice has to be deleted after using.
0133      * @param inDevice input device. Won't be deleted if @p autoDeleteInDevice = false
0134      * @param mimetype the mime type for the filter
0135      * @param autoDeleteInDevice if true, @p inDevice will be deleted automatically
0136      * @return a KCompressionDevice that filters the original stream. Must be deleted after using
0137      */
0138     KARCHIVE_DEPRECATED_VERSION(5, 0, "See API docs")
0139     static KCompressionDevice *device(QIODevice *inDevice, const QString &mimetype, bool autoDeleteInDevice = true)
0140     {
0141         if (inDevice == nullptr) {
0142             return nullptr;
0143         }
0144         CompressionType type = compressionTypeForMimeType(mimetype);
0145         KCompressionDevice *device = new KCompressionDevice(inDevice, autoDeleteInDevice, type);
0146         return device;
0147     }
0148 #endif
0149 };
0150 
0151 #endif // KARCHIVE_ENABLE_DEPRECATED_SINCE(5, 85)
0152 
0153 #endif