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 
0023 #ifndef STREAMS_SOCKETFACTORY_H
0024 #define STREAMS_SOCKETFACTORY_H
0025 
0026 #include <QPointer>
0027 #include <QStringList>
0028 #include "Socket.h"
0029 
0030 namespace Streams {
0031 
0032 /** @short Specify preference for Proxy Settings */
0033 enum class ProxySettings
0034 {
0035     RespectSystemProxy, /**< @short Use System Proxy Settings to connect */
0036     DirectConnect,      /**< @short Connect without using any Proxy Settings */
0037 };
0038 
0039 /** @short Abstract interface for creating new socket that is somehow connected
0040  * to the IMAP server */
0041 class SocketFactory: public QObject
0042 {
0043     Q_OBJECT
0044     bool m_startTls;
0045 public:
0046     SocketFactory();
0047     virtual ~SocketFactory() {}
0048     /** @short Create new socket and return a smart pointer to it */
0049     virtual Socket *create() = 0;
0050     virtual void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag) = 0;
0051     void setStartTlsRequired(const bool doIt);
0052     bool startTlsRequired();
0053 signals:
0054     void error(const QString &);
0055 };
0056 
0057 /** @short Manufacture sockets based on QProcess */
0058 class ProcessSocketFactory: public SocketFactory
0059 {
0060     Q_OBJECT
0061     /** @short Name of executable file to launch */
0062     QString executable;
0063     /** @short Arguments to launch the process with */
0064     QStringList args;
0065 public:
0066     ProcessSocketFactory(const QString &executable, const QStringList &args);
0067     Socket *create() override;
0068     void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag) override;
0069 };
0070 
0071 /** @short Manufacture sockets based on QSslSocket */
0072 class SslSocketFactory: public SocketFactory
0073 {
0074     Q_OBJECT
0075     /** @short Hostname of the remote host */
0076     QString host;
0077     /** @short Port number */
0078     quint16 port;
0079     /** @short Specify Proxy Settings for connection */
0080     ProxySettings m_proxySettings;
0081     /** @short Protocol for the requested connection */
0082     QString m_protocolTag;
0083 public:
0084     SslSocketFactory(const QString &host, const quint16 port);
0085     void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag) override;
0086     Socket *create() override;
0087 };
0088 
0089 /** @short Factory for regular TCP sockets that are able to STARTTLS */
0090 class TlsAbleSocketFactory: public SocketFactory
0091 {
0092     Q_OBJECT
0093     /** @short Hostname of the remote host */
0094     QString host;
0095     /** @short Port number */
0096     quint16 port;
0097     /** @short Specify Proxy Settings for connection */
0098     ProxySettings m_proxySettings;
0099     /** @short Protocol for the requested connection */
0100     QString m_protocolTag;
0101 public:
0102     TlsAbleSocketFactory(const QString &host, const quint16 port);
0103     void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag) override;
0104     Socket *create() override;
0105 };
0106 
0107 /** @short A fake factory suitable for unit tests */
0108 class FakeSocketFactory: public SocketFactory
0109 {
0110     Q_OBJECT
0111 public:
0112     explicit FakeSocketFactory(const Imap::ConnectionState initialState);
0113     Socket *create() override;
0114     /** @short Return the last created socket */
0115     Socket *lastSocket();
0116     void setInitialState(const Imap::ConnectionState initialState);
0117     void setProxySettings(const Streams::ProxySettings proxySettings, const QString &protocolTag) override;
0118 
0119 private:
0120     QPointer<Socket> m_last;
0121     Imap::ConnectionState m_initialState;
0122 };
0123 
0124 }
0125 
0126 #endif