File indexing completed on 2024-05-12 05:17:17

0001 /*
0002     SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QMutex>
0010 #include <QQueue>
0011 
0012 #include <QSslSocket>
0013 
0014 #include <memory>
0015 
0016 class KSslErrorUiData;
0017 
0018 namespace KIMAP
0019 {
0020 class ImapStreamParser;
0021 struct Response;
0022 
0023 class SessionThread : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     explicit SessionThread(const QString &hostName, quint16 port);
0029     ~SessionThread();
0030 
0031     inline QString hostName()
0032     {
0033         return m_hostName;
0034     }
0035     inline quint16 port()
0036     {
0037         return m_port;
0038     }
0039 
0040     void setUseNetworkProxy(bool useProxy);
0041 
0042     void sendData(const QByteArray &payload);
0043 
0044 public Q_SLOTS:
0045     void closeSocket();
0046     void startSsl(QSsl::SslProtocol protocol);
0047     void sslErrorHandlerResponse(bool result);
0048 
0049 Q_SIGNALS:
0050     void socketConnected();
0051     void socketDisconnected();
0052     void socketActivity();
0053     void socketError(QAbstractSocket::SocketError);
0054     void responseReceived(const KIMAP::Response &response);
0055     void encryptionNegotiationResult(bool, QSsl::SslProtocol);
0056     void sslError(const KSslErrorUiData &);
0057 
0058 private Q_SLOTS:
0059     void reconnect();
0060     void threadInit();
0061     void threadQuit();
0062     void readMessage();
0063     void writeDataQueue();
0064     void sslConnected();
0065     void doCloseSocket();
0066     void slotSocketError(QAbstractSocket::SocketError);
0067     void slotSocketDisconnected();
0068     void doStartSsl(QSsl::SslProtocol);
0069     void doSslErrorHandlerResponse(bool result);
0070     void setUseProxyInternal(bool useProxy);
0071 
0072 private:
0073     QString m_hostName;
0074     quint16 m_port;
0075 
0076     std::unique_ptr<QSslSocket> m_socket;
0077     std::unique_ptr<ImapStreamParser> m_stream;
0078 
0079     QQueue<QByteArray> m_dataQueue;
0080 
0081     // Protects m_dataQueue
0082     QMutex m_mutex;
0083 
0084     bool m_encryptedMode = false;
0085     bool m_useProxy = false;
0086 };
0087 
0088 }