File indexing completed on 2024-06-23 05:15:11

0001 /*
0002   SPDX-FileCopyrightText: 2006-2007 KovoKs <info@kovoks.nl>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "mailtransport_export.h"
0010 
0011 #include <QSslSocket>
0012 
0013 #include <memory>
0014 
0015 namespace MailTransport
0016 {
0017 class SocketPrivate;
0018 
0019 /**
0020  * @class Socket
0021  * Responsible for communicating with the server, it's designed to work
0022  * with the ServerTest class.
0023  * @author Tom Albers <tomalbers@kde.nl>
0024  */
0025 class Socket : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     /**
0031      * Constructor, it will not auto connect. Call reconnect() to connect to
0032      * the parameters given.
0033      * @param parent the parent
0034      */
0035     explicit Socket(QObject *parent);
0036 
0037     /**
0038      * Destructor
0039      */
0040     ~Socket() override;
0041 
0042     /**
0043      * Existing connection will be closed and a new connection will be
0044      * made
0045      */
0046     virtual void reconnect();
0047 
0048     /**
0049      * Write @p text to the socket
0050      */
0051     virtual void write(const QString &text);
0052 
0053     /**
0054      * @return true when the connection is live and kicking
0055      */
0056     virtual bool available();
0057 
0058     /**
0059      * set the protocol to use
0060      */
0061     void setProtocol(const QString &proto);
0062 
0063     /**
0064      * set the server to use
0065      */
0066     void setServer(const QString &server);
0067 
0068     /**
0069      * set the port to use. If not specified, it will use the default
0070      * belonging to the protocol.
0071      */
0072     void setPort(int port);
0073 
0074     /**
0075      * returns the used port.
0076      */
0077     int port() const;
0078 
0079     /**
0080      * this will be a secure connection
0081      */
0082     void setSecure(bool what);
0083 
0084     /**
0085      * If you want to start TLS encryption, call this. For example after the starttls command.
0086      */
0087     void startTLS();
0088 
0089 private:
0090     Q_DECLARE_PRIVATE(Socket)
0091     std::unique_ptr<SocketPrivate> const d;
0092 
0093     Q_PRIVATE_SLOT(d, void slotConnected())
0094     Q_PRIVATE_SLOT(d, void slotStateChanged(QAbstractSocket::SocketState state))
0095     Q_PRIVATE_SLOT(d, void slotModeChanged(QSslSocket::SslMode state))
0096     Q_PRIVATE_SLOT(d, void slotSocketRead())
0097     Q_PRIVATE_SLOT(d, void slotSslErrors(const QList<QSslError> &errors))
0098 
0099 Q_SIGNALS:
0100     /**
0101      * emits the incoming data
0102      */
0103     void data(const QString &);
0104 
0105     /**
0106      * emitted when there is a connection (ready to send something).
0107      */
0108     void connected();
0109 
0110     /**
0111      * emitted when not connected.
0112      */
0113     void failed();
0114 
0115     /**
0116      * emitted when startShake() is completed.
0117      */
0118     void tlsDone();
0119 };
0120 } // namespace MailTransport