• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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     socket->setProxy(proxyForConnection(protocol, host));
00059     socket->connectToHost(host, port);
00060 }
00061 
00062 void KSocketFactory::connectToHost(QTcpSocket *socket, const QUrl &url)
00063 {
00064     connectToHost(socket, url.scheme(), url.host(), url.port());
00065 }
00066 
00067 QTcpSocket *KSocketFactory::connectToHost(const QString &protocol, const QString &host, quint16 port,
00068                                           QObject *parent)
00069 {
00070     // ### TO-DO: find a way to determine if we should use QSslSocket or plain QTcpSocket
00071     QTcpSocket *socket = new QSslSocket(parent);
00072     connectToHost(socket, protocol, host, port);
00073     return socket;
00074 }
00075 
00076 QTcpSocket *KSocketFactory::connectToHost(const QUrl &url, QObject *parent)
00077 {
00078     return connectToHost(url.scheme(), url.host(), url.port(), parent);
00079 }
00080 
00081 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QString &protocol,
00082                                               const QString &host, quint16 port, int msecs)
00083 {
00084     if (!socket)
00085         return;
00086 
00087     connectToHost(socket, protocol, host, port);
00088     if (!socket->waitForConnected(msecs))
00089         setError(socket, QAbstractSocket::SocketTimeoutError,
00090                  i18n("Timed out trying to connect to remote host"));
00091 }
00092 
00093 void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QUrl &url, int msecs)
00094 {
00095     synchronousConnectToHost(socket, url.scheme(), url.host(), url.port(), msecs);
00096 }
00097 
00098 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QString &protocol, const QString &host,
00099                                                      quint16 port, int msecs, QObject *parent)
00100 {
00101     QTcpSocket *socket = connectToHost(protocol, host, port, parent);
00102     if (!socket->waitForConnected(msecs))
00103         setError(socket, QAbstractSocket::SocketTimeoutError,
00104                  i18n("Timed out trying to connect to remote host"));
00105     return socket;
00106 }
00107 
00108 QTcpSocket *KSocketFactory::synchronousConnectToHost(const QUrl &url, int msecs, QObject *parent)
00109 {
00110     return synchronousConnectToHost(url.scheme(), url.host(), url.port(), msecs, parent);
00111 }
00112 
00113 QTcpServer *KSocketFactory::listen(const QString &protocol, const QHostAddress &address, quint16 port,
00114                                    QObject *parent)
00115 {
00116     QTcpServer *server = new QTcpServer(parent);
00117     server->setProxy(proxyForListening(protocol));
00118     server->listen(address, port);
00119     return server;
00120 }
00121 
00122 QUdpSocket *KSocketFactory::datagramSocket(const QString &protocol, const QString &host, QObject *parent)
00123 {
00124     QUdpSocket *socket = new QUdpSocket(parent);
00125     // ### do something else?
00126     socket->setProxy(proxyForDatagram(protocol, host));
00127     return socket;
00128 }
00129 
00130 QNetworkProxy KSocketFactory::proxyForConnection(const QString &, const QString &)
00131 {
00132     return QNetworkProxy::NoProxy;
00133 }
00134 
00135 QNetworkProxy KSocketFactory::proxyForListening(const QString &)
00136 {
00137     return QNetworkProxy::NoProxy;
00138 }
00139 
00140 QNetworkProxy KSocketFactory::proxyForDatagram(const QString &, const QString &)
00141 {
00142     return QNetworkProxy::NoProxy;
00143 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal