1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 
3    This file is part of the Trojita Qt IMAP e-mail client,
4    http://trojita.flaska.net/
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of
9    the License or (at your option) version 3 or any later version
10    accepted by the membership of KDE e.V. (or its successor approved
11    by the membership of KDE e.V.), which shall act as a proxy
12    defined in Section 14 of version 3 of the license.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "SocketFactory.h"
24 #include <stdexcept>
25 #include <QProcess>
26 #include <QSslSocket>
27 #include "IODeviceSocket.h"
28 #include "FakeSocket.h"
29 
30 namespace Streams {
31 
SocketFactory()32 SocketFactory::SocketFactory(): m_startTls(false)
33 {
34 }
35 
setStartTlsRequired(const bool doIt)36 void SocketFactory::setStartTlsRequired(const bool doIt)
37 {
38     m_startTls = doIt;
39 }
40 
startTlsRequired()41 bool SocketFactory::startTlsRequired()
42 {
43     return m_startTls;
44 }
45 
ProcessSocketFactory(const QString & executable,const QStringList & args)46 ProcessSocketFactory::ProcessSocketFactory(
47     const QString &executable, const QStringList &args):
48     executable(executable), args(args)
49 {
50 }
51 
create()52 Socket *ProcessSocketFactory::create()
53 {
54     // FIXME: this may leak memory if an exception strikes in this function
55     // (before we return the pointer)
56     return new ProcessSocket(new QProcess(), executable, args);
57 }
58 
setProxySettings(const ProxySettings proxySettings,const QString & protocolTag)59 void ProcessSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
60 {
61     throw std::invalid_argument("This socket factory doesn't manufacture network sockets and therefore doesn't support proxy settings");
62 }
63 
SslSocketFactory(const QString & host,const quint16 port)64 SslSocketFactory::SslSocketFactory(const QString &host, const quint16 port):
65     host(host), port(port)
66 {
67 }
68 
setProxySettings(const ProxySettings proxySettings,const QString & protocolTag)69 void SslSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
70 {
71     m_proxySettings = proxySettings;
72     m_protocolTag = protocolTag;
73 }
74 
create()75 Socket *SslSocketFactory::create()
76 {
77     QSslSocket *sslSock = new QSslSocket();
78     SslTlsSocket *sock = new SslTlsSocket(sslSock, host, port, true);
79     sock->setProxySettings(m_proxySettings, m_protocolTag);
80     return sock;
81 }
82 
83 
TlsAbleSocketFactory(const QString & host,const quint16 port)84 TlsAbleSocketFactory::TlsAbleSocketFactory(const QString &host, const quint16 port):
85     host(host), port(port)
86 {
87 }
88 
setProxySettings(const ProxySettings proxySettings,const QString & protocolTag)89 void TlsAbleSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
90 {
91     m_proxySettings = proxySettings;
92     m_protocolTag = protocolTag;
93 }
94 
create()95 Socket *TlsAbleSocketFactory::create()
96 {
97     QSslSocket *sslSock = new QSslSocket();
98     SslTlsSocket *sock = new SslTlsSocket(sslSock, host, port);
99     sock->setProxySettings(m_proxySettings, m_protocolTag);
100     return sock;
101 }
102 
FakeSocketFactory(const Imap::ConnectionState initialState)103 FakeSocketFactory::FakeSocketFactory(const Imap::ConnectionState initialState): SocketFactory(), m_initialState(initialState)
104 {
105 }
106 
create()107 Socket *FakeSocketFactory::create()
108 {
109     return m_last = new FakeSocket(m_initialState);
110 }
111 
lastSocket()112 Socket *FakeSocketFactory::lastSocket()
113 {
114     Q_ASSERT(m_last);
115     return m_last;
116 }
117 
setInitialState(const Imap::ConnectionState initialState)118 void FakeSocketFactory::setInitialState(const Imap::ConnectionState initialState)
119 {
120     m_initialState = initialState;
121 }
122 
setProxySettings(const ProxySettings proxySettings,const QString & protocolTag)123 void FakeSocketFactory::setProxySettings(const ProxySettings proxySettings, const QString &protocolTag)
124 {
125     throw std::invalid_argument("This socket factory doesn't manufacture network sockets and therefore doesn't support proxy settings");
126 }
127 
128 }
129