File indexing completed on 2024-04-21 03:54:59

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0004     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "connectionserver.h"
0010 #include "connection_p.h"
0011 #include "connectionbackend_p.h"
0012 
0013 using namespace KIO;
0014 
0015 class KIO::ConnectionServerPrivate
0016 {
0017 public:
0018     inline ConnectionServerPrivate()
0019         : backend(nullptr)
0020     {
0021     }
0022 
0023     ConnectionServer *q;
0024     ConnectionBackend *backend;
0025 };
0026 
0027 ConnectionServer::ConnectionServer(QObject *parent)
0028     : QObject(parent)
0029     , d(new ConnectionServerPrivate)
0030 {
0031     d->q = this;
0032 }
0033 
0034 ConnectionServer::~ConnectionServer() = default;
0035 
0036 void ConnectionServer::listenForRemote()
0037 {
0038     d->backend = new ConnectionBackend(this);
0039     if (!d->backend->listenForRemote()) {
0040         delete d->backend;
0041         d->backend = nullptr;
0042         return;
0043     }
0044 
0045     connect(d->backend, &ConnectionBackend::newConnection, this, &ConnectionServer::newConnection);
0046     // qDebug() << "Listening on" << d->backend->address;
0047 }
0048 
0049 QUrl ConnectionServer::address() const
0050 {
0051     if (d->backend) {
0052         return d->backend->address;
0053     }
0054     return QUrl();
0055 }
0056 
0057 bool ConnectionServer::isListening() const
0058 {
0059     return d->backend && d->backend->state == ConnectionBackend::Listening;
0060 }
0061 
0062 void ConnectionServer::close()
0063 {
0064     delete d->backend;
0065     d->backend = nullptr;
0066 }
0067 
0068 Connection *ConnectionServer::nextPendingConnection()
0069 {
0070     if (!isListening()) {
0071         return nullptr;
0072     }
0073 
0074     ConnectionBackend *newBackend = d->backend->nextPendingConnection();
0075     if (!newBackend) {
0076         return nullptr; // no new backend...
0077     }
0078 
0079     Connection *result = new Connection;
0080     result->d->setBackend(newBackend);
0081     newBackend->setParent(result);
0082 
0083     return result;
0084 }
0085 
0086 void ConnectionServer::setNextPendingConnection(Connection *conn)
0087 {
0088     ConnectionBackend *newBackend = d->backend->nextPendingConnection();
0089     Q_ASSERT(newBackend);
0090 
0091     conn->d->setBackend(newBackend);
0092     newBackend->setParent(conn);
0093 
0094     conn->d->dequeue();
0095 }
0096 
0097 #include "moc_connectionserver.cpp"