1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtCore/qfileinfo.h> |
30 | #include <QtCore/qstringlist.h> |
31 | |
32 | #include <QtNetwork/qhostaddress.h> |
33 | #include <QtNetwork/qtcpserver.h> |
34 | #include <QtNetwork/qtcpsocket.h> |
35 | |
36 | #include "qdesigner.h" |
37 | #include "qdesigner_server.h" |
38 | |
39 | #include <qevent.h> |
40 | |
41 | QT_BEGIN_NAMESPACE |
42 | |
43 | // ### review |
44 | |
45 | QDesignerServer::QDesignerServer(QObject *parent) |
46 | : QObject(parent) |
47 | { |
48 | m_socket = nullptr; |
49 | m_server = new QTcpServer(this); |
50 | if (m_server->listen(address: QHostAddress::LocalHost, port: 0)) { |
51 | connect(sender: m_server, signal: &QTcpServer::newConnection, |
52 | receiver: this, slot: &QDesignerServer::handleNewConnection); |
53 | } |
54 | } |
55 | |
56 | QDesignerServer::~QDesignerServer() = default; |
57 | |
58 | quint16 QDesignerServer::serverPort() const |
59 | { |
60 | return m_server ? m_server->serverPort() : 0; |
61 | } |
62 | |
63 | void QDesignerServer::sendOpenRequest(int port, const QStringList &files) |
64 | { |
65 | QTcpSocket *sSocket = new QTcpSocket(); |
66 | sSocket->connectToHost(address: QHostAddress::LocalHost, port); |
67 | if(sSocket->waitForConnected(msecs: 3000)) |
68 | { |
69 | for (const QString &file : files) { |
70 | QFileInfo fi(file); |
71 | sSocket->write(data: fi.absoluteFilePath().toUtf8() + '\n'); |
72 | } |
73 | sSocket->waitForBytesWritten(msecs: 3000); |
74 | sSocket->close(); |
75 | } |
76 | delete sSocket; |
77 | } |
78 | |
79 | void QDesignerServer::readFromClient() |
80 | { |
81 | while (m_socket->canReadLine()) { |
82 | QString file = QString::fromUtf8(str: m_socket->readLine()); |
83 | if (!file.isNull()) { |
84 | file.remove(c: QLatin1Char('\n')); |
85 | file.remove(c: QLatin1Char('\r')); |
86 | qDesigner->postEvent(qDesigner, event: new QFileOpenEvent(file)); |
87 | } |
88 | } |
89 | } |
90 | |
91 | void QDesignerServer::socketClosed() |
92 | { |
93 | m_socket = nullptr; |
94 | } |
95 | |
96 | void QDesignerServer::handleNewConnection() |
97 | { |
98 | // no need for more than one connection |
99 | if (m_socket == nullptr) { |
100 | m_socket = m_server->nextPendingConnection(); |
101 | connect(sender: m_socket, signal: &QTcpSocket::readyRead, |
102 | receiver: this, slot: &QDesignerServer::readFromClient); |
103 | connect(sender: m_socket, signal: &QTcpSocket::disconnected, |
104 | receiver: this, slot: &QDesignerServer::socketClosed); |
105 | } |
106 | } |
107 | |
108 | |
109 | QDesignerClient::QDesignerClient(quint16 port, QObject *parent) |
110 | : QObject(parent) |
111 | { |
112 | m_socket = new QTcpSocket(this); |
113 | m_socket->connectToHost(address: QHostAddress::LocalHost, port); |
114 | connect(sender: m_socket, signal: &QTcpSocket::readyRead, |
115 | receiver: this, slot: &QDesignerClient::readFromSocket); |
116 | |
117 | } |
118 | |
119 | QDesignerClient::~QDesignerClient() |
120 | { |
121 | m_socket->close(); |
122 | m_socket->flush(); |
123 | } |
124 | |
125 | void QDesignerClient::readFromSocket() |
126 | { |
127 | while (m_socket->canReadLine()) { |
128 | QString file = QString::fromUtf8(str: m_socket->readLine()); |
129 | if (!file.isNull()) { |
130 | file.remove(c: QLatin1Char('\n')); |
131 | file.remove(c: QLatin1Char('\r')); |
132 | if (QFile::exists(fileName: file)) |
133 | qDesigner->postEvent(qDesigner, event: new QFileOpenEvent(file)); |
134 | } |
135 | } |
136 | } |
137 | |
138 | QT_END_NAMESPACE |
139 | |