KDECore
ksocketfactory.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "ksocketfactory.h" 00022 00023 #include <QSslSocket> 00024 #include <QTcpSocket> 00025 #include <QTcpServer> 00026 #include <QUdpSocket> 00027 #include <QUrl> 00028 00029 #include "klocalizedstring.h" 00030 00031 #include <config-network.h> 00032 00033 using namespace KSocketFactory; 00034 00035 class _k_internal_QTcpSocketSetError: public QAbstractSocket 00036 { 00037 public: 00038 _k_internal_QTcpSocketSetError(); // not defined anywhere! 00039 using QAbstractSocket::setSocketError; 00040 using QAbstractSocket::setSocketState; 00041 using QAbstractSocket::setErrorString; 00042 }; 00043 00044 static inline void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error, 00045 const QString &errorString) 00046 { 00047 _k_internal_QTcpSocketSetError *hackSocket = static_cast<_k_internal_QTcpSocketSetError *>(socket); 00048 hackSocket->setSocketError(error); 00049 hackSocket->setErrorString(errorString); 00050 } 00051 00052 void KSocketFactory::connectToHost(QTcpSocket *socket, const QString &protocol, const QString &host, 00053 quint16 port) 00054 { 00055 if (!socket) 00056 return; 00057 00058 #ifndef QT_NO_NETWORKPROXY 00059 socket->setProxy(proxyForConnection(protocol, host)); 00060 #endif 00061 socket->connectToHost(host, port); 00062 } 00063 00064 void KSocketFactory::connectToHost(QTcpSocket *socket, const QUrl &url) 00065 { 00066 connectToHost(socket, url.scheme(), url.host(), url.port()); 00067 } 00068 00069 QTcpSocket *KSocketFactory::connectToHost(const QString &protocol, const QString &host, quint16 port, 00070 QObject *parent) 00071 { 00072 // ### TO-DO: find a way to determine if we should use QSslSocket or plain QTcpSocket 00073 QTcpSocket *socket = new QSslSocket(parent); 00074 connectToHost(socket, protocol, host, port); 00075 return socket; 00076 } 00077 00078 QTcpSocket *KSocketFactory::connectToHost(const QUrl &url, QObject *parent) 00079 { 00080 return connectToHost(url.scheme(), url.host(), url.port(), parent); 00081 } 00082 00083 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QString &protocol, 00084 const QString &host, quint16 port, int msecs) 00085 { 00086 if (!socket) 00087 return; 00088 00089 connectToHost(socket, protocol, host, port); 00090 if (!socket->waitForConnected(msecs)) 00091 setError(socket, QAbstractSocket::SocketTimeoutError, 00092 i18n("Timed out trying to connect to remote host")); 00093 } 00094 00095 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QUrl &url, int msecs) 00096 { 00097 synchronousConnectToHost(socket, url.scheme(), url.host(), url.port(), msecs); 00098 } 00099 00100 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QString &protocol, const QString &host, 00101 quint16 port, int msecs, QObject *parent) 00102 { 00103 QTcpSocket *socket = connectToHost(protocol, host, port, parent); 00104 if (!socket->waitForConnected(msecs)) 00105 setError(socket, QAbstractSocket::SocketTimeoutError, 00106 i18n("Timed out trying to connect to remote host")); 00107 return socket; 00108 } 00109 00110 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QUrl &url, int msecs, QObject *parent) 00111 { 00112 return synchronousConnectToHost(url.scheme(), url.host(), url.port(), msecs, parent); 00113 } 00114 00115 QTcpServer *KSocketFactory::listen(const QString &protocol, const QHostAddress &address, quint16 port, 00116 QObject *parent) 00117 { 00118 QTcpServer *server = new QTcpServer(parent); 00119 #ifndef QT_NO_NETWORKPROXY 00120 server->setProxy(proxyForListening(protocol)); 00121 #endif 00122 server->listen(address, port); 00123 return server; 00124 } 00125 00126 QUdpSocket *KSocketFactory::datagramSocket(const QString &protocol, const QString &host, QObject *parent) 00127 { 00128 QUdpSocket *socket = new QUdpSocket(parent); 00129 #ifndef QT_NO_NETWORKPROXY 00130 // ### do something else? 00131 socket->setProxy(proxyForDatagram(protocol, host)); 00132 #endif 00133 return socket; 00134 } 00135 00136 #ifndef QT_NO_NETWORKPROXY 00137 QNetworkProxy KSocketFactory::proxyForConnection(const QString &, const QString &) 00138 { 00139 return QNetworkProxy::NoProxy; 00140 } 00141 00142 QNetworkProxy KSocketFactory::proxyForListening(const QString &) 00143 { 00144 return QNetworkProxy::NoProxy; 00145 } 00146 00147 QNetworkProxy KSocketFactory::proxyForDatagram(const QString &, const QString &) 00148 { 00149 return QNetworkProxy::NoProxy; 00150 } 00151 #endif
KDE 4.6 API Reference