File indexing completed on 2024-04-21 14:55:39

0001 /*
0002  * This file is part of the KDE libraries
0003  * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "ksocketfactory.h"
0022 
0023 #include <QSslSocket>
0024 #include <QTcpSocket>
0025 #include <QTcpServer>
0026 #include <QUdpSocket>
0027 #include <QUrl>
0028 
0029 #include "klocalizedstring.h"
0030 
0031 using namespace KSocketFactory;
0032 
0033 class _k_internal_QTcpSocketSetError: public QAbstractSocket
0034 {
0035 public:
0036     _k_internal_QTcpSocketSetError(); // not defined anywhere!
0037     using QAbstractSocket::setSocketError;
0038     using QAbstractSocket::setSocketState;
0039     using QAbstractSocket::setErrorString;
0040 };
0041 
0042 static inline void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error,
0043                             const QString &errorString)
0044 {
0045     _k_internal_QTcpSocketSetError *hackSocket = static_cast<_k_internal_QTcpSocketSetError *>(socket);
0046     hackSocket->setSocketError(error);
0047     hackSocket->setErrorString(errorString);
0048 }
0049 
0050 void KSocketFactory::connectToHost(QTcpSocket *socket, const QString &protocol, const QString &host,
0051                                    quint16 port)
0052 {
0053     if (!socket) {
0054         return;
0055     }
0056 
0057 #ifndef QT_NO_NETWORKPROXY
0058     socket->setProxy(proxyForConnection(protocol, host));
0059 #endif
0060     socket->connectToHost(host, port);
0061 }
0062 
0063 void KSocketFactory::connectToHost(QTcpSocket *socket, const QUrl &url)
0064 {
0065     connectToHost(socket, url.scheme(), url.host(), url.port());
0066 }
0067 
0068 QTcpSocket *KSocketFactory::connectToHost(const QString &protocol, const QString &host, quint16 port,
0069         QObject *parent)
0070 {
0071     // ### TO-DO: find a way to determine if we should use QSslSocket or plain QTcpSocket
0072     QTcpSocket *socket = new QSslSocket(parent);
0073     connectToHost(socket, protocol, host, port);
0074     return socket;
0075 }
0076 
0077 QTcpSocket *KSocketFactory::connectToHost(const QUrl &url, QObject *parent)
0078 {
0079     return connectToHost(url.scheme(), url.host(), url.port(), parent);
0080 }
0081 
0082 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QString &protocol,
0083         const QString &host, quint16 port, int msecs)
0084 {
0085     if (!socket) {
0086         return;
0087     }
0088 
0089     connectToHost(socket, protocol, host, port);
0090     if (!socket->waitForConnected(msecs))
0091         setError(socket, QAbstractSocket::SocketTimeoutError,
0092                  i18n("Timed out trying to connect to remote host"));
0093 }
0094 
0095 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QUrl &url, int msecs)
0096 {
0097     synchronousConnectToHost(socket, url.scheme(), url.host(), url.port(), msecs);
0098 }
0099 
0100 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QString &protocol, const QString &host,
0101         quint16 port, int msecs, QObject *parent)
0102 {
0103     QTcpSocket *socket = connectToHost(protocol, host, port, parent);
0104     if (!socket->waitForConnected(msecs))
0105         setError(socket, QAbstractSocket::SocketTimeoutError,
0106                  i18n("Timed out trying to connect to remote host"));
0107     return socket;
0108 }
0109 
0110 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QUrl &url, int msecs, QObject *parent)
0111 {
0112     return synchronousConnectToHost(url.scheme(), url.host(), url.port(), msecs, parent);
0113 }
0114 
0115 QTcpServer *KSocketFactory::listen(const QString &protocol, const QHostAddress &address, quint16 port,
0116                                    QObject *parent)
0117 {
0118     QTcpServer *server = new QTcpServer(parent);
0119 #ifndef QT_NO_NETWORKPROXY
0120     server->setProxy(proxyForListening(protocol));
0121 #endif
0122     server->listen(address, port);
0123     return server;
0124 }
0125 
0126 QUdpSocket *KSocketFactory::datagramSocket(const QString &protocol, const QString &host, QObject *parent)
0127 {
0128     QUdpSocket *socket = new QUdpSocket(parent);
0129 #ifndef QT_NO_NETWORKPROXY
0130     // ### do something else?
0131     socket->setProxy(proxyForDatagram(protocol, host));
0132 #endif
0133     return socket;
0134 }
0135 
0136 #ifndef QT_NO_NETWORKPROXY
0137 QNetworkProxy KSocketFactory::proxyForConnection(const QString &, const QString &)
0138 {
0139     return QNetworkProxy::DefaultProxy;
0140 }
0141 
0142 QNetworkProxy KSocketFactory::proxyForListening(const QString &)
0143 {
0144     return QNetworkProxy::DefaultProxy;
0145 }
0146 
0147 QNetworkProxy KSocketFactory::proxyForDatagram(const QString &, const QString &)
0148 {
0149     return QNetworkProxy::DefaultProxy;
0150 }
0151 #endif