File indexing completed on 2024-06-09 05:21:30

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 #ifndef STREAMS_IODEVICE_SOCKET_H
0023 #define STREAMS_IODEVICE_SOCKET_H
0024 
0025 #include <QProcess>
0026 #include <QSslSocket>
0027 #include "Socket.h"
0028 #include "SocketFactory.h"
0029 
0030 class QTimer;
0031 
0032 namespace Streams {
0033 
0034 class Rfc1951Compressor;
0035 class Rfc1951Decompressor;
0036 class SocketFactory;
0037 
0038 /** @short Helper class for all sockets which are based on a QIODevice */
0039 class IODeviceSocket: public Socket
0040 {
0041     Q_OBJECT
0042     Q_DISABLE_COPY(IODeviceSocket)
0043 public:
0044     explicit IODeviceSocket(QIODevice *device);
0045     ~IODeviceSocket();
0046     bool canReadLine() override;
0047     QByteArray read(qint64 maxSize) override;
0048     QByteArray readLine(qint64 maxSize = 0) override;
0049     qint64 write(const QByteArray &byteArray) override;
0050     void startTls() override;
0051     void startDeflate() override;
0052     bool isDead() override = 0;
0053 private slots:
0054     virtual void handleStateChanged() = 0;
0055     virtual void delayedStart() = 0;
0056     virtual void handleReadyRead();
0057     void emitError();
0058 protected:
0059     QIODevice *d;
0060     Rfc1951Compressor *m_compressor;
0061     Rfc1951Decompressor *m_decompressor;
0062     QTimer *delayedDisconnect;
0063     QString disconnectedMessage;
0064 };
0065 
0066 /** @short A QProcess-based socket */
0067 class ProcessSocket: public IODeviceSocket
0068 {
0069     Q_OBJECT
0070     Q_DISABLE_COPY(ProcessSocket)
0071 public:
0072     ProcessSocket(QProcess *proc, const QString &executable, const QStringList &args);
0073     ~ProcessSocket();
0074     bool isDead() override;
0075     void close() override;
0076 private slots:
0077     void handleStateChanged() override;
0078     void handleProcessError(QProcess::ProcessError);
0079     void delayedStart() override;
0080 private:
0081     QString executable;
0082     QStringList args;
0083 };
0084 
0085 /** @short An SSL socket, usable both in SSL-from-start and STARTTLS-on-demand mode */
0086 class SslTlsSocket: public IODeviceSocket
0087 {
0088     Q_OBJECT
0089     Q_DISABLE_COPY(SslTlsSocket)
0090 public:
0091     /** Set the @arg startEncrypted to true if the wrapper is supposed to emit
0092     connected() only after it has established proper encryption */
0093     SslTlsSocket(QSslSocket *sock, const QString &host, const quint16 port, const bool startEncrypted=false);
0094     void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag);
0095     bool isDead() override;
0096     QList<QSslCertificate> sslChain() const override;
0097     QList<QSslError> sslErrors() const override;
0098     bool isConnectingEncryptedSinceStart() const override;
0099     void close() override;
0100 private slots:
0101     void handleStateChanged() override;
0102     void handleSocketError(QAbstractSocket::SocketError);
0103     void delayedStart() override;
0104 private:
0105     bool startEncrypted;
0106     QString host;
0107     quint16 port;
0108     QString m_protocolTag;
0109     ProxySettings m_proxySettings;
0110 };
0111 
0112 };
0113 
0114 #endif