1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
4 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "connectionserver.h"
10#include "connection_p.h"
11#include "connectionbackend_p.h"
12
13using namespace KIO;
14
15class KIO::ConnectionServerPrivate
16{
17public:
18 inline ConnectionServerPrivate()
19 : backend(nullptr)
20 {
21 }
22
23 ConnectionServer *q;
24 ConnectionBackend *backend;
25};
26
27ConnectionServer::ConnectionServer(QObject *parent)
28 : QObject(parent)
29 , d(new ConnectionServerPrivate)
30{
31 d->q = this;
32}
33
34ConnectionServer::~ConnectionServer() = default;
35
36void ConnectionServer::listenForRemote()
37{
38 d->backend = new ConnectionBackend(this);
39 if (!d->backend->listenForRemote()) {
40 delete d->backend;
41 d->backend = nullptr;
42 return;
43 }
44
45 connect(sender: d->backend, signal: &ConnectionBackend::newConnection, context: this, slot: &ConnectionServer::newConnection);
46 // qDebug() << "Listening on" << d->backend->address;
47}
48
49QUrl ConnectionServer::address() const
50{
51 if (d->backend) {
52 return d->backend->address;
53 }
54 return QUrl();
55}
56
57bool ConnectionServer::isListening() const
58{
59 return d->backend && d->backend->state == ConnectionBackend::Listening;
60}
61
62void ConnectionServer::close()
63{
64 delete d->backend;
65 d->backend = nullptr;
66}
67
68Connection *ConnectionServer::nextPendingConnection()
69{
70 if (!isListening()) {
71 return nullptr;
72 }
73
74 ConnectionBackend *newBackend = d->backend->nextPendingConnection();
75 if (!newBackend) {
76 return nullptr; // no new backend...
77 }
78
79 Connection *result = new Connection(Connection::Type::Application);
80 result->d->setBackend(newBackend);
81 newBackend->setParent(result);
82
83 return result;
84}
85
86void ConnectionServer::setNextPendingConnection(Connection *conn)
87{
88 ConnectionBackend *newBackend = d->backend->nextPendingConnection();
89 Q_ASSERT(newBackend);
90
91 conn->d->setBackend(newBackend);
92 newBackend->setParent(conn);
93
94 conn->d->dequeue();
95}
96
97#include "moc_connectionserver.cpp"
98

source code of kio/src/core/connectionserver.cpp