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// Qt-Security score:significant reason:default
4
5#include "qdesigner.h"
7
8#include <QtGui/qevent.h>
9
10#include <QtNetwork/qhostaddress.h>
11#include <QtNetwork/qlocalsocket.h>
12#include <QtNetwork/qtcpserver.h>
13#include <QtNetwork/qtcpsocket.h>
14
15#include <QtCore/qfileinfo.h>
16#include <QtCore/qstringlist.h>
17
19
20using namespace Qt::StringLiterals;
21
22// ### review
23
24static void readFiles(QIODevice *device)
25{
26 while (device->canReadLine()) {
27 QString file = QString::fromUtf8(device->readLine());
28 if (!file.isNull()) {
29 file.remove(u'\n');
30 file.remove(u'\r');
31 if (QFile::exists(file))
32 QCoreApplication::postEvent(qDesigner, new QFileOpenEvent(file));
33 }
34 }
35}
36
37QDesignerTcpServer::QDesignerTcpServer(QObject *parent) : QObject(parent), m_server(new QTcpServer(this))
38{
39 if (m_server->listen(QHostAddress::LocalHost, 0))
40 connect(m_server, &QTcpServer::newConnection, this, &QDesignerTcpServer::handleNewConnection);
41}
42
44
46{
47 return m_server ? m_server->serverPort() : 0;
48}
49
50void QDesignerTcpServer::sendOpenRequest(int port, const QStringList &files)
51{
52 auto *sSocket = new QTcpSocket();
53 sSocket->connectToHost(QHostAddress::LocalHost, port);
54 if (sSocket->waitForConnected(3000)) {
55 for (const QString &file : files) {
56 QFileInfo fi(file);
57 sSocket->write(fi.absoluteFilePath().toUtf8() + '\n');
58 }
59 sSocket->waitForBytesWritten(3000);
60 sSocket->close();
61 }
62 delete sSocket;
63}
64
65void QDesignerTcpServer::readFromClient()
66{
67 readFiles(m_socket);
68}
69
70void QDesignerTcpServer::socketClosed()
71{
72 m_socket = nullptr;
73}
74
75void QDesignerTcpServer::handleNewConnection()
76{
77 // no need for more than one connection
78 if (m_socket == nullptr) {
79 m_socket = m_server->nextPendingConnection();
80 connect(m_socket, &QTcpSocket::readyRead, this, &QDesignerTcpServer::readFromClient);
81 connect(m_socket, &QTcpSocket::disconnected, this, &QDesignerTcpServer::socketClosed);
82 }
83}
84
85QDesignerLocalSocketClient::QDesignerLocalSocketClient(const QString &serverName, QObject *parent)
86 : QObject(parent), m_socket(new QLocalSocket(this))
87{
88 m_socket->connectToServer(serverName, QIODevice::ReadOnly);
89 connect(m_socket, &QLocalSocket::readyRead, this, &QDesignerLocalSocketClient::readFromSocket);
90}
91
93{
94 m_socket->close();
95 m_socket->flush();
96}
97
98void QDesignerLocalSocketClient::readFromSocket()
99{
100 readFiles(m_socket);
101}
102
103QDesignerTcpClient::QDesignerTcpClient(quint16 port, QObject *parent)
104 : QObject(parent), m_socket(new QTcpSocket(this))
105{
106 m_socket->connectToHost(QHostAddress::LocalHost, port);
107 connect(m_socket, &QTcpSocket::readyRead, this, &QDesignerTcpClient::readFromSocket);
108}
109
111{
112 m_socket->close();
113 m_socket->flush();
114}
115
116void QDesignerTcpClient::readFromSocket()
117{
118 readFiles(m_socket);
119}
120
121QT_END_NAMESPACE
~QDesignerTcpServer() override
quint16 serverPort() const
Combined button and popup list for selecting options.
#define qDesigner
Definition qdesigner.h:20
static void readFiles(QIODevice *device)