85817f1f3f6a3ef794e425669e82de0d1a0d2485
[quassel.git] / src / common / compressor.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef COMPRESSOR_H
22 #define COMPRESSOR_H
23
24 #include <QObject>
25
26 class QTcpSocket;
27
28 typedef struct mz_stream_s *z_streamp;
29
30 class Compressor : public QObject
31 {
32     Q_OBJECT
33
34 public:
35     enum CompressionLevel {
36         NoCompression,
37         DefaultCompression,
38         BestCompression,
39         BestSpeed
40     };
41
42     enum Error {
43         NoError,
44         StreamError,
45         DeviceError
46     };
47
48     enum WriteBufferHint {
49         NoFlush,
50         Flush
51     };
52
53     Compressor(QTcpSocket *socket, CompressionLevel level, QObject *parent = 0);
54     ~Compressor();
55
56     CompressionLevel compressionLevel() const { return _level; }
57
58     qint64 bytesAvailable() const;
59
60     qint64 read(char *data, qint64 maxSize);
61     qint64 write(const char *data, qint64 count, WriteBufferHint flush = Flush);
62
63     void flush();
64
65 signals:
66     void readyRead();
67     void error(Compressor::Error errorCode = StreamError);
68
69 private slots:
70     void readData();
71
72 private:
73     bool initStreams();
74     void writeData();
75
76 private:
77     QTcpSocket *_socket;
78     CompressionLevel _level;
79
80     QByteArray _readBuffer;
81     QByteArray _writeBuffer;
82
83     QByteArray _inputBuffer;
84     QByteArray _outputBuffer;
85
86     z_streamp _inflater;
87     z_streamp _deflater;
88 };
89
90 #endif