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 #include "SocketFactory.h"
0024 #include <stdexcept>
0025 #include <QProcess>
0026 #include <QSslSocket>
0027 #include "IODeviceSocket.h"
0028 #include "FakeSocket.h"
0029 
0030 namespace Streams {
0031 
0032 SocketFactory::SocketFactory(): m_startTls(false)
0033 {
0034 }
0035 
0036 void SocketFactory::setStartTlsRequired(const bool doIt)
0037 {
0038     m_startTls = doIt;
0039 }
0040 
0041 bool SocketFactory::startTlsRequired()
0042 {
0043     return m_startTls;
0044 }
0045 
0046 ProcessSocketFactory::ProcessSocketFactory(
0047     const QString &executable, const QStringList &args):
0048     executable(executable), args(args)
0049 {
0050 }
0051 
0052 Socket *ProcessSocketFactory::create()
0053 {
0054     // FIXME: this may leak memory if an exception strikes in this function
0055     // (before we return the pointer)
0056     return new ProcessSocket(new QProcess(), executable, args);
0057 }
0058 
0059 void ProcessSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
0060 {
0061     throw std::invalid_argument("This socket factory doesn't manufacture network sockets and therefore doesn't support proxy settings");
0062 }
0063 
0064 SslSocketFactory::SslSocketFactory(const QString &host, const quint16 port):
0065     host(host), port(port)
0066 {
0067 }
0068 
0069 void SslSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
0070 {
0071     m_proxySettings = proxySettings;
0072     m_protocolTag = protocolTag;
0073 }
0074 
0075 Socket *SslSocketFactory::create()
0076 {
0077     QSslSocket *sslSock = new QSslSocket();
0078     SslTlsSocket *sock = new SslTlsSocket(sslSock, host, port, true);
0079     sock->setProxySettings(m_proxySettings, m_protocolTag);
0080     return sock;
0081 }
0082 
0083 
0084 TlsAbleSocketFactory::TlsAbleSocketFactory(const QString &host, const quint16 port):
0085     host(host), port(port)
0086 {
0087 }
0088 
0089 void TlsAbleSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
0090 {
0091     m_proxySettings = proxySettings;
0092     m_protocolTag = protocolTag;
0093 }
0094 
0095 Socket *TlsAbleSocketFactory::create()
0096 {
0097     QSslSocket *sslSock = new QSslSocket();
0098     SslTlsSocket *sock = new SslTlsSocket(sslSock, host, port);
0099     sock->setProxySettings(m_proxySettings, m_protocolTag);
0100     return sock;
0101 }
0102 
0103 FakeSocketFactory::FakeSocketFactory(const Imap::ConnectionState initialState): SocketFactory(), m_initialState(initialState)
0104 {
0105 }
0106 
0107 Socket *FakeSocketFactory::create()
0108 {
0109     return m_last = new FakeSocket(m_initialState);
0110 }
0111 
0112 Socket *FakeSocketFactory::lastSocket()
0113 {
0114     Q_ASSERT(m_last);
0115     return m_last;
0116 }
0117 
0118 void FakeSocketFactory::setInitialState(const Imap::ConnectionState initialState)
0119 {
0120     m_initialState = initialState;
0121 }
0122 
0123 void FakeSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
0124 {
0125     throw std::invalid_argument("This socket factory doesn't manufacture network sockets and therefore doesn't support proxy settings");
0126 }
0127 
0128 }