File indexing completed on 2024-04-28 15:39:41

0001 // SPDX-FileCopyrightText: 2014-2022 Jesper K. Pedersen <blackie@kde.org>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "Client.h"
0006 
0007 #include "RemoteCommand.h"
0008 #include <QTcpSocket>
0009 #include <QUdpSocket>
0010 
0011 using namespace RemoteControl;
0012 
0013 Client::Client(QObject *parent)
0014     : RemoteConnection(parent)
0015 {
0016     connect(&m_server, &QTcpServer::newConnection, this, &Client::acceptConnection);
0017     connect(&m_timer, &QTimer::timeout, this, &Client::sendBroadcastPackage);
0018     m_server.listen(QHostAddress::Any, TCPPORT);
0019     m_timer.start(500);
0020 }
0021 
0022 bool Client::isConnected() const
0023 {
0024     return m_socket != nullptr;
0025 }
0026 
0027 QTcpSocket *Client::socket()
0028 {
0029     return m_socket;
0030 }
0031 
0032 void Client::acceptConnection()
0033 {
0034     m_timer.stop();
0035     m_socket = m_server.nextPendingConnection();
0036     connect(m_socket, &QTcpSocket::disconnected, this, &Client::disconnect);
0037     connect(m_socket, &QTcpSocket::readyRead, this, &Client::dataReceived);
0038     Q_EMIT gotConnected();
0039 }
0040 
0041 void Client::sendBroadcastPackage()
0042 {
0043     QUdpSocket socket;
0044     QByteArray data = QStringLiteral("KPhotoAlbum %1").arg(RemoteControl::VERSION).toUtf8();
0045     socket.writeDatagram(data, QHostAddress::Broadcast, UDPPORT);
0046 }
0047 
0048 void Client::disconnect()
0049 {
0050     m_timer.start(500);
0051     m_socket->deleteLater();
0052     m_socket = nullptr;
0053     Q_EMIT disconnected();
0054 }
0055 
0056 #include "moc_Client.cpp"