Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdesigner_server.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "qdesigner.h"
6
7#include <QtGui/qevent.h>
8
9#include <QtNetwork/qhostaddress.h>
10#include <QtNetwork/qtcpserver.h>
11#include <QtNetwork/qtcpsocket.h>
12
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qstringlist.h>
15
17
18using namespace Qt::StringLiterals;
19
20// ### review
21
22static void readFiles(QIODevice *device)
23{
24 while (device->canReadLine()) {
25 QString file = QString::fromUtf8(device->readLine());
26 if (!file.isNull()) {
27 file.remove(u'\n');
28 file.remove(u'\r');
29 if (QFile::exists(file))
30 QCoreApplication::postEvent(qDesigner, new QFileOpenEvent(file));
31 }
32 }
33}
34
35QDesignerTcpServer::QDesignerTcpServer(QObject *parent) : QObject(parent), m_server(new QTcpServer(this))
36{
37 if (m_server->listen(QHostAddress::LocalHost, 0))
38 connect(m_server, &QTcpServer::newConnection, this, &QDesignerTcpServer::handleNewConnection);
39}
40
42
44{
45 return m_server ? m_server->serverPort() : 0;
46}
47
48void QDesignerTcpServer::sendOpenRequest(int port, const QStringList &files)
49{
50 auto *sSocket = new QTcpSocket();
51 sSocket->connectToHost(QHostAddress::LocalHost, port);
52 if (sSocket->waitForConnected(3000)) {
53 for (const QString &file : files) {
54 QFileInfo fi(file);
55 sSocket->write(fi.absoluteFilePath().toUtf8() + '\n');
56 }
57 sSocket->waitForBytesWritten(3000);
58 sSocket->close();
59 }
60 delete sSocket;
61}
62
63void QDesignerTcpServer::readFromClient()
64{
65 readFiles(m_socket);
66}
67
68void QDesignerTcpServer::socketClosed()
69{
70 m_socket = nullptr;
71}
72
73void QDesignerTcpServer::handleNewConnection()
74{
75 // no need for more than one connection
76 if (m_socket == nullptr) {
77 m_socket = m_server->nextPendingConnection();
78 connect(m_socket, &QTcpSocket::readyRead, this, &QDesignerTcpServer::readFromClient);
79 connect(m_socket, &QTcpSocket::disconnected, this, &QDesignerTcpServer::socketClosed);
80 }
81}
82
83QDesignerTcpClient::QDesignerTcpClient(quint16 port, QObject *parent)
84 : QObject(parent), m_socket(new QTcpSocket(this))
85{
86 m_socket->connectToHost(QHostAddress::LocalHost, port);
87 connect(m_socket, &QTcpSocket::readyRead, this, &QDesignerTcpClient::readFromSocket);
88}
89
91{
92 m_socket->close();
93 m_socket->flush();
94}
95
96void QDesignerTcpClient::readFromSocket()
97{
98 readFiles(m_socket);
99}
100
101QT_END_NAMESPACE
~QDesignerTcpServer() override
quint16 serverPort() const
Combined button and popup list for selecting options.
#define qDesigner
Definition qdesigner.h:19
static void readFiles(QIODevice *device)