Commit 544bae2f authored by Jonathan Marten's avatar Jonathan Marten
Browse files

Provide an option wheether to use the system proxy settings

This is not normally useful, so do not use the proxy by default.
Added function Session::setUseNetworkProxy() to use the proxy
if required.

Differential Revision: https://phabricator.kde.org/D21463
parent de646b90
cmake_minimum_required(VERSION 3.5)
set(PIM_VERSION "5.11.40")
set(PIM_VERSION "5.11.41")
project(KIMAP VERSION ${PIM_VERSION})
......
......@@ -98,6 +98,11 @@ quint16 Session::port() const
return d->thread->port();
}
void Session::setUseNetworkProxy(bool useProxy)
{
d->thread->setUseNetworkProxy(useProxy);
}
Session::State Session::state() const
{
return d->state;
......
......@@ -91,6 +91,18 @@ public:
*/
Q_REQUIRED_RESULT QString selectedMailBox() const;
/**
* Sets whether the IMAP network connection should use the system proxy settings.
*
* @param useProxy @c true if the proxy is to be used
* The default is to not use the proxy.
* @since 5.11.41
*
* @note If the session is currently connected to the IMAP server, calling this
* function will disconnect and reconnect to it with the changed proxy setting.
*/
void setUseNetworkProxy(bool useProxy);
Q_REQUIRED_RESULT int jobQueueSize() const;
void close();
......
......@@ -21,6 +21,7 @@
#include <QDebug>
#include <QThread>
#include <QNetworkProxy>
#include "kimap_debug.h"
......@@ -37,7 +38,8 @@ static const int _kimap_sslErrorUiData = qRegisterMetaType<KSslErrorUiData>();
SessionThread::SessionThread(const QString &hostName, quint16 port)
: QObject(), m_hostName(hostName), m_port(port),
m_socket(nullptr), m_stream(nullptr), m_mutex(),
m_encryptedMode(false)
m_encryptedMode(false),
m_useProxy(false)
{
// Just like the Qt docs now recommend, for event-driven threads:
// don't derive from QThread, create one directly and move the object to it.
......@@ -59,6 +61,12 @@ SessionThread::~SessionThread()
delete thread();
}
// Called in primary thread, passes setting to secondary thread
void SessionThread::setUseNetworkProxy(bool useProxy)
{
QMetaObject::invokeMethod(this, [this, useProxy]() { setUseProxyInternal(useProxy); }, Qt::QueuedConnection);
}
// Called in primary thread
void SessionThread::sendData(const QByteArray &payload)
{
......@@ -162,6 +170,17 @@ void SessionThread::reconnect()
}
if (m_socket->state() != SessionSocket::ConnectedState &&
m_socket->state() != SessionSocket::ConnectingState) {
QNetworkProxy proxy;
if (!m_useProxy) {
qCDebug(KIMAP_LOG) << "Connecting to IMAP server with no proxy";
proxy.setType(QNetworkProxy::NoProxy);
} else {
qCDebug(KIMAP_LOG) << "Connecting to IMAP server using default system proxy";
proxy.setType(QNetworkProxy::DefaultProxy);
}
m_socket->setProxy(proxy);
if (m_encryptedMode) {
qCDebug(KIMAP_LOG) << "connectToHostEncrypted" << m_hostName << m_port;
m_socket->connectToHostEncrypted(m_hostName, m_port);
......@@ -210,6 +229,18 @@ void SessionThread::threadQuit()
thread()->quit();
}
// Called in secondary thread
void SessionThread::setUseProxyInternal(bool useProxy)
{
m_useProxy = useProxy;
if (m_socket != nullptr) {
if (m_socket->state() != SessionSocket::UnconnectedState) {
m_socket->disconnectFromHost();
QMetaObject::invokeMethod(this, &SessionThread::reconnect, Qt::QueuedConnection);
}
}
}
// Called in primary thread
void SessionThread::startSsl(KTcpSocket::SslVersion version)
{
......
......@@ -50,6 +50,8 @@ public:
return m_port;
}
void setUseNetworkProxy(bool useProxy);
void sendData(const QByteArray &payload);
public Q_SLOTS:
......@@ -78,6 +80,7 @@ private Q_SLOTS:
void slotSocketDisconnected();
void doStartSsl(KTcpSocket::SslVersion);
void doSslErrorHandlerResponse(bool result);
void setUseProxyInternal(bool useProxy);
private:
QString m_hostName;
......@@ -92,6 +95,7 @@ private:
QMutex m_mutex;
bool m_encryptedMode = false;
bool m_useProxy = false;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment