File: ksocketfactory.cpp

package info (click to toggle)
kdelibs4support 5.78.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,628 kB
  • sloc: cpp: 87,933; perl: 304; ansic: 196; python: 113; xml: 102; sh: 8; makefile: 7
file content (151 lines) | stat: -rw-r--r-- 4,896 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * This file is part of the KDE libraries
 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "ksocketfactory.h"

#include <QSslSocket>
#include <QTcpSocket>
#include <QTcpServer>
#include <QUdpSocket>
#include <QUrl>

#include "klocalizedstring.h"

using namespace KSocketFactory;

class _k_internal_QTcpSocketSetError: public QAbstractSocket
{
public:
    _k_internal_QTcpSocketSetError(); // not defined anywhere!
    using QAbstractSocket::setSocketError;
    using QAbstractSocket::setSocketState;
    using QAbstractSocket::setErrorString;
};

static inline void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error,
                            const QString &errorString)
{
    _k_internal_QTcpSocketSetError *hackSocket = static_cast<_k_internal_QTcpSocketSetError *>(socket);
    hackSocket->setSocketError(error);
    hackSocket->setErrorString(errorString);
}

void KSocketFactory::connectToHost(QTcpSocket *socket, const QString &protocol, const QString &host,
                                   quint16 port)
{
    if (!socket) {
        return;
    }

#ifndef QT_NO_NETWORKPROXY
    socket->setProxy(proxyForConnection(protocol, host));
#endif
    socket->connectToHost(host, port);
}

void KSocketFactory::connectToHost(QTcpSocket *socket, const QUrl &url)
{
    connectToHost(socket, url.scheme(), url.host(), url.port());
}

QTcpSocket *KSocketFactory::connectToHost(const QString &protocol, const QString &host, quint16 port,
        QObject *parent)
{
    // ### TO-DO: find a way to determine if we should use QSslSocket or plain QTcpSocket
    QTcpSocket *socket = new QSslSocket(parent);
    connectToHost(socket, protocol, host, port);
    return socket;
}

QTcpSocket *KSocketFactory::connectToHost(const QUrl &url, QObject *parent)
{
    return connectToHost(url.scheme(), url.host(), url.port(), parent);
}

void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QString &protocol,
        const QString &host, quint16 port, int msecs)
{
    if (!socket) {
        return;
    }

    connectToHost(socket, protocol, host, port);
    if (!socket->waitForConnected(msecs))
        setError(socket, QAbstractSocket::SocketTimeoutError,
                 i18n("Timed out trying to connect to remote host"));
}

void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QUrl &url, int msecs)
{
    synchronousConnectToHost(socket, url.scheme(), url.host(), url.port(), msecs);
}

QTcpSocket *KSocketFactory::synchronousConnectToHost(const QString &protocol, const QString &host,
        quint16 port, int msecs, QObject *parent)
{
    QTcpSocket *socket = connectToHost(protocol, host, port, parent);
    if (!socket->waitForConnected(msecs))
        setError(socket, QAbstractSocket::SocketTimeoutError,
                 i18n("Timed out trying to connect to remote host"));
    return socket;
}

QTcpSocket *KSocketFactory::synchronousConnectToHost(const QUrl &url, int msecs, QObject *parent)
{
    return synchronousConnectToHost(url.scheme(), url.host(), url.port(), msecs, parent);
}

QTcpServer *KSocketFactory::listen(const QString &protocol, const QHostAddress &address, quint16 port,
                                   QObject *parent)
{
    QTcpServer *server = new QTcpServer(parent);
#ifndef QT_NO_NETWORKPROXY
    server->setProxy(proxyForListening(protocol));
#endif
    server->listen(address, port);
    return server;
}

QUdpSocket *KSocketFactory::datagramSocket(const QString &protocol, const QString &host, QObject *parent)
{
    QUdpSocket *socket = new QUdpSocket(parent);
#ifndef QT_NO_NETWORKPROXY
    // ### do something else?
    socket->setProxy(proxyForDatagram(protocol, host));
#endif
    return socket;
}

#ifndef QT_NO_NETWORKPROXY
QNetworkProxy KSocketFactory::proxyForConnection(const QString &, const QString &)
{
    return QNetworkProxy::DefaultProxy;
}

QNetworkProxy KSocketFactory::proxyForListening(const QString &)
{
    return QNetworkProxy::DefaultProxy;
}

QNetworkProxy KSocketFactory::proxyForDatagram(const QString &, const QString &)
{
    return QNetworkProxy::DefaultProxy;
}
#endif