Debian Codegraph @

utils/fgqcanvas/canvasconnection.cpp

raw symbols SCIP
1 //
2 // Copyright (C) 2017 James Turner [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 #include "canvasconnection.h"
19
20 #include <QUrl>
21 #include <QDebug>
22 #include <QJsonDocument>
23 #include <QJsonArray>
24 #include <QJsonObject>
25 #include <QJsonValue>
26 #include <QNetworkRequest>
27 #include <QNetworkAccessManager>
28 #include <QNetworkReply>
29 #include <QDataStream>
30
31 #include "localprop.h"
32 #include "fgqcanvasfontcache.h"
33 #include "fgqcanvasimageloader.h"
34 #include "jsonutils.h"
35
36 CanvasConnection::CanvasConnection(QObject *parent) : QObject(parent)
37 {
38 connect(&m_webSocket, &QWebSocket::connected, this, &CanvasConnection::onWebSocketConnected);
39 connect(&m_webSocket, &QWebSocket::disconnected, this, &CanvasConnection::onWebSocketClosed);
40 connect(&m_webSocket, &QWebSocket::textMessageReceived,
41 this, &CanvasConnection::onTextMessageReceived);
42
43 m_destRect = QRectF(50, 50, 400, 400);
44 m_reconnectTimer = new QTimer(this);
45 m_reconnectTimer->setInterval(1000 * 10);
46 m_reconnectTimer->setSingleShot(true);
47 connect(m_reconnectTimer, &QTimer::timeout,
48 this, &CanvasConnection::reconnect);
49 }
50
51 CanvasConnection::~CanvasConnection()
52 {
53 disconnect(&m_webSocket, &QWebSocket::disconnected,
54 this, &CanvasConnection::onWebSocketClosed);
55 m_webSocket.close();
56 }
57
58 void CanvasConnection::setNetworkAccess(QNetworkAccessManager *dl)
59 {
60 m_netAccess = dl;
61 }
62
63 void CanvasConnection::setRootPropertyPath(QByteArray path)
64 {
65 m_rootPropertyPath = path;
66 emit rootPathChanged();
67 }
68
69 void CanvasConnection::setAutoReconnect()
70 {
71 m_autoReconnect = true;
72 }
73
74 QJsonObject CanvasConnection::saveState() const
75 {
76 QJsonObject json;
77 json["url"] = m_webSocketUrl.toString();
78 json["path"] = QString::fromUtf8(m_rootPropertyPath);
79 json["rect"] = rectToJsonArray(m_destRect.toRect());
80 json["window"] = m_windowIndex;
81 return json;
82 }
83
84 bool CanvasConnection::restoreState(QJsonObject state)
85 {
86 m_webSocketUrl = state.value("url").toString();
87 m_rootPropertyPath = state.value("path").toString().toUtf8();
88 m_destRect = jsonArrayToRect(state.value("rect").toArray());
89
90 if (state.contains("window")) {
91 m_windowIndex = state.value("window").toInt();
92 }
93
94 emit geometryChanged();
95 emit rootPathChanged();
96 emit webSocketUrlChanged();
97
98 return true;
99 }
100
101 void CanvasConnection::saveSnapshot(QDataStream &ds) const
102 {
103 ds << m_webSocketUrl << m_rootPropertyPath << m_destRect;
104 m_localPropertyRoot->saveToStream(ds);
105 }
106
107 void CanvasConnection::restoreSnapshot(QDataStream &ds)
108 {
109 ds >> m_webSocketUrl >> m_rootPropertyPath >> m_destRect;
110 m_localPropertyRoot.reset(LocalProp::restoreFromStream(ds, nullptr));
111 setStatus(Snapshot);
112
113 emit geometryChanged();
114 emit rootPathChanged();
115 emit webSocketUrlChanged();
116
117 emit updated();
118 }
119
120 void CanvasConnection::reconnect()
121 {
122 qDebug() << "starting connection attempt to:" << m_webSocketUrl;
123 m_webSocket.open(m_webSocketUrl);
124 setStatus(Connecting);
125 }
126
127 void CanvasConnection::showDebugTree()
128 {
129 qWarning() << Q_FUNC_INFO << "implement me!";
130 }
131
132 void CanvasConnection::setOrigin(QPointF c)
133 {
134 if (m_destRect.topLeft() == c)
135 return;
136
137 m_destRect.moveTopLeft(c);
138 emit geometryChanged();
139 }
140
141 void CanvasConnection::setSize(QSizeF sz)
142 {
143 if (size() == sz)
144 return;
145
146 m_destRect.setSize(sz);
147 emit geometryChanged();
148 }
149
150 void CanvasConnection::connectWebSocket(QByteArray hostName, int port)
151 {
152 QUrl wsUrl;
153 wsUrl.setScheme("ws");
154 wsUrl.setHost(hostName);
155 wsUrl.setPort(port);
156 wsUrl.setPath("/PropertyTreeMirror" + m_rootPropertyPath);
157
158 m_webSocketUrl = wsUrl;
159 emit webSocketUrlChanged();
160
161 m_webSocket.open(wsUrl);
162 setStatus(Connecting);
163 }
164
165 QPointF CanvasConnection::origin() const
166 {
167 return m_destRect.topLeft();
168 }
169
170 QSizeF CanvasConnection::size() const
171 {
172 return m_destRect.size();
173 }
174
175 void CanvasConnection::setWindowIndex(int index)
176 {
177 if (m_windowIndex != index) {
178 m_windowIndex = index;
179 emit geometryChanged();
180 }
181 }
182
183 LocalProp *CanvasConnection::propertyRoot() const
184 {
185 return m_localPropertyRoot.get();
186 }
187
188 FGQCanvasImageLoader *CanvasConnection::imageLoader() const
189 {
190 if (!m_imageLoader) {
191 m_imageLoader = new FGQCanvasImageLoader(m_netAccess, const_cast<CanvasConnection*>(this));
192 m_imageLoader->setHost(m_webSocketUrl.host(),
193 m_webSocketUrl.port());
194 }
195
196 return m_imageLoader;
197 }
198
199 FGQCanvasFontCache *CanvasConnection::fontCache() const
200 {
201 if (!m_fontCache) {
202 m_fontCache = new FGQCanvasFontCache(m_netAccess, const_cast<CanvasConnection*>(this));
203 m_fontCache->setHost(m_webSocketUrl.host(),
204 m_webSocketUrl.port());
205 }
206
207 return m_fontCache;
208 }
209
210 void CanvasConnection::onWebSocketConnected()
211 {
212 qDebug() << Q_FUNC_INFO << m_webSocketUrl;
213 m_localPropertyRoot.reset(new LocalProp{nullptr, NameIndexTuple("")});
214 setStatus(Connected);
215 }
216
217 void CanvasConnection::onTextMessageReceived(QString message)
218 {
219 QJsonDocument json = QJsonDocument::fromJson(message.toUtf8());
220 if (json.isObject()) {
221 // process new nodes
222 QJsonArray created = json.object().value("created").toArray();
223 Q_FOREACH (QJsonValue v, created) {
224 QJsonObject newProp = v.toObject();
225
226 QByteArray nodePath = newProp.value("path").toString().toUtf8();
227 if (nodePath.indexOf(m_rootPropertyPath) != 0) {
228 qWarning() << "not a property path we are mirroring:" << nodePath;
229 continue;
230 }
231
232 QByteArray localPath = nodePath.mid(m_rootPropertyPath.size() + 1);
233 LocalProp* newNode = propertyFromPath(localPath);
234 newNode->setPosition(newProp.value("position").toInt());
235 // store in the global dict
236 int propId = newProp.value("id").toInt();
237 if (idPropertyDict.contains(propId)) {
238 qWarning() << "duplicate add of:" << nodePath << "old is" << idPropertyDict.value(propId)->path();
239 } else {
240 idPropertyDict.insert(propId, newNode);
241 }
242
243 // set initial value
244 newNode->processChange(newProp.value("value"));
245 }
246
247 // process removes
248 QJsonArray removed = json.object().value("removed").toArray();
249 Q_FOREACH (QJsonValue v, removed) {
250 int propId = v.toInt();
251 if (!idPropertyDict.contains(propId)) {
252 continue;
253 }
254
255 auto prop = idPropertyDict.value(propId);
256 idPropertyDict.remove(propId);
257
258 // depending on the order removes are sent, the LocalProp
259 // may already have been deleted when its parent was removed,
260 // so check if the QPointer is null
261 if (!prop.isNull()) {
262 prop->parent()->removeChild(prop);
263 }
264 } // of removes processing
265
266 // process changes
267 QJsonArray changed = json.object().value("changed").toArray();
268
269 Q_FOREACH (QJsonValue v, changed) {
270 QJsonArray change = v.toArray();
271 if (change.size() != 2) {
272 qWarning() << "malformed change notification";
273 continue;
274 }
275
276 int propId = change.at(0).toInt();
277 if (!idPropertyDict.contains(propId)) {
278 qWarning() << "ignoring unknown prop ID " << propId;
279 continue;
280 }
281
282 LocalProp* lp = idPropertyDict.value(propId);
283 if (lp != nullptr) {
284 lp->processChange(change.at(1));
285 }
286 } // of change processing
287 }
288
289 emit updated();
290 }
291
292 void CanvasConnection::onWebSocketClosed()
293 {
294 if ((m_status == Connected) || (m_status == Connected)) {
295 qDebug() << "saw web-socket closed";
296 }
297
298 m_localPropertyRoot.reset();
299 idPropertyDict.clear();
300
301 setStatus(Closed);
302
303 if (m_autoReconnect) {
304 m_reconnectTimer->start();
305 }
306 }
307
308 void CanvasConnection::setStatus(CanvasConnection::Status newStatus)
309 {
310 if (newStatus == m_status)
311 return;
312
313 m_status = newStatus;
314 emit statusChanged(m_status);
315 }
316
317 LocalProp *CanvasConnection::propertyFromPath(QByteArray path) const
318 {
319 return m_localPropertyRoot->getOrCreateWithPath(path);
320 }
321