aboutsummaryrefslogtreecommitdiffstats
path: root/src/remotepublisher.cpp
blob: f4f37eb5f51e79e3d7fea59b0d59fda0d407c989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QML Live tool.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include "remotepublisher.h"
#include "ipc/ipcclient.h"
#include "livedocument.h"
#include "livehubengine.h"

#ifdef QMLLIVE_DEBUG
#define DEBUG qDebug()
#else
#define DEBUG if (0) qDebug()
#endif

/*!
 * \class RemotePublisher
 * \brief Publishes hub changes to a remote node.
 * \inmodule qmllive
 *
 * To see the progress which commands were really sent successfully to to the server
 * you have to connect the signals from the LiveHubEngine yourself and monitor the QUuids you
 * got and wait for sendingError() or sentSuccessfully() signals
 */

/*!
 * Standard Constructor using \a parent as parent
 */
RemotePublisher::RemotePublisher(QObject *parent)
    : QObject(parent)
    , m_ipc(new IpcClient(this))
    , m_hub(0)
{
    connect(m_ipc, &IpcClient::sentSuccessfully, this, &RemotePublisher::sentSuccessfully);
    connect(m_ipc, &IpcClient::sendingError, this, &RemotePublisher::sendingError);
    connect(m_ipc, &IpcClient::connectionError, this, &RemotePublisher::connectionError);
    connect(m_ipc, &IpcClient::connected, this, &RemotePublisher::connected);
    connect(m_ipc, &IpcClient::disconnected, this, &RemotePublisher::disconnected);

    connect(m_ipc, &IpcClient::received, this, &RemotePublisher::handleCall);

    connect(m_ipc, &IpcClient::sentSuccessfully, this, &RemotePublisher::onSentSuccessfully);
    connect(m_ipc, &IpcClient::sendingError, this, &RemotePublisher::onSendingError);
}

/*!
  Return the state of the \l IpcClient

  \sa IpcClient::state()
 */
QAbstractSocket::SocketState RemotePublisher::state() const
{
    return m_ipc->state();
}

/*!
 * Register the \a hub to be used with this publisher
 */
void RemotePublisher::registerHub(LiveHubEngine *hub)
{
    if (m_hub) {
        disconnect(m_hub);
    }
    m_hub = hub;
    connect(hub, &LiveHubEngine::activateDocument, this, &RemotePublisher::activateDocument);
    connect(hub, &LiveHubEngine::fileChanged, this, &RemotePublisher::sendDocument);
    connect(hub, &LiveHubEngine::publishFile, this, &RemotePublisher::sendDocument);
    connect(this, &RemotePublisher::needsPublishWorkspace, hub, &LiveHubEngine::publishWorkspace);
    connect(hub, &LiveHubEngine::beginPublishWorkspace, this, &RemotePublisher::beginBulkSend);
    connect(hub, &LiveHubEngine::endPublishWorkspace, this, &RemotePublisher::endBulkSend);
}

/*!
 * Sets the current workspace to \a path. Documents location will be adjusted based on
 * this workspace path.
 */
void RemotePublisher::setWorkspace(const QString &path)
{
    m_workspace = QDir(path);
}

/*!
 * Set Ipc destination to use \a hostName and \a port
 * \sa IpcClient::connectToServer
 */
void RemotePublisher::connectToServer(const QString &hostName, int port)
{
    m_ipc->connectToServer(hostName, port);
}

/*!
  Converts the socket error \a error into a string
 */
QString RemotePublisher::errorToString(QAbstractSocket::SocketError error)
{
    return m_ipc->errorToString(error);
}

/*!
 * Disconnects this publisher from the IPC
 */
void RemotePublisher::disconnectFromServer()
{
    m_ipc->disconnectFromServer();
}

/*!
 * Send "activateDocument(QString)" to IPC-server on activate document.
 * \a document defines the Document which should be activated
 */
QUuid RemotePublisher::activateDocument(const LiveDocument &document)
{
    DEBUG << "RemotePublisher::activateDocument" << document;
    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << document.relativeFilePath();
    return m_ipc->send("activateDocument(QString)", bytes);
}

/*!
 * Sends "beginBulkSend()" via IPC.
 */
QUuid RemotePublisher::beginBulkSend()
{
    DEBUG << "RemotePublisher::beginBulkSend";
    return m_ipc->send("beginBulkSend()", QByteArray());
}

/*!
 * Sends "endBulkSend()" via IPC.
 */
QUuid RemotePublisher::endBulkSend()
{
    DEBUG << "RemotePublisher::endBulkSend";
    return m_ipc->send("endBulkSend()", QByteArray());
}

/*!
 * Sends "sendDocument(QString)" using \a document to identify the document to be
 *send to via IPC.
 */
QUuid RemotePublisher::sendDocument(const LiveDocument& document)
{
    DEBUG << "RemotePublisher::sendDocument" << document;
    return sendWholeDocument(document);
}

/*!
 Send checkPin with \a pin argument and returns the package uuid.
 */
QUuid RemotePublisher::checkPin(const QString &pin)
{
    DEBUG << "RemotePublisher::checkPin" << pin;
    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << pin;
    return m_ipc->send("checkPin(QString)", bytes);
}

/*!
  Sends the \e setXOffset with \a offset as argument via IPC
 */

QUuid RemotePublisher::setXOffset(int offset)
{
    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << offset;
    return m_ipc->send("setXOffset(int)", bytes);
}

/*!
  Sends the \e setYOffset with \a offset as argument via IPC
 */

QUuid RemotePublisher::setYOffset(int offset)
{
    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << offset;
    return m_ipc->send("setYOffset(int)", bytes);
}

/*!
  Sends the \e setRotation with \a rotation as argument via IPC
 */
QUuid RemotePublisher::setRotation(int rotation)
{
    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << rotation;
    return m_ipc->send("setRotation(int)", bytes);
}

/*!
  Sends the \e sendWholeDocument with \a document as argument via IPC
 */
QUuid RemotePublisher::sendWholeDocument(const LiveDocument& document)
{
    DEBUG << "RemotePublisher::sendWholeDocument" << document;
    QFile file(document.absoluteFilePathIn(m_workspace));
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "ERROR: can't open file: " << document;
        return QUuid();
    }
    QByteArray data = file.readAll();

    QByteArray bytes;
    QDataStream out(&bytes, QIODevice::WriteOnly);
    out << document.relativeFilePath();
    out << data;
    return m_ipc->send("sendDocument(QString,QByteArray)", bytes);
}

void RemotePublisher::onSentSuccessfully(const QUuid &uuid)
{
    QString path = m_packageHash.value(uuid);
    m_packageHash.remove(uuid);

    QList<QUuid> keys = m_packageHash.keys(path);
    if (keys.count() == 1) {
        m_packageHash.remove(keys.at(0));
        emit sentSuccessfully(keys.at(0));
    }
}

void RemotePublisher::onSendingError(const QUuid &uuid, QAbstractSocket::SocketError socketError)
{
    QString path = m_packageHash.value(uuid);
    m_packageHash.remove(uuid);

    QList<QUuid> keys = m_packageHash.keys(path);
    if (keys.count() == 1) {
        m_packageHash.remove(keys.at(0));
        emit sendingError(keys.at(0), socketError);
    }
}


void RemotePublisher::handleCall(const QString &method, const QByteArray &content)
{
    DEBUG << "RemotePublisher::handleIpcCall: " << method << content;

    if (method == "needsPinAuthentication()") {
        qDebug() << "needsPinAuthentication";
        emit needsPinAuthentication();
    } else if (method == "pinOK(bool)") {
        qDebug() << "pinOk" << content.toInt();
        emit pinOk(content.toInt());
    } else if (method == "needsPublishWorkspace()") {
        emit needsPublishWorkspace();
    } else if (method == "qmlLog(QtMsgType, QString, QUrl, int, int)") {
        int msgType;
        QString description;
        QUrl url;
        int line = -1;
        int column = -1;

        QDataStream in(content);
        in >> msgType;
        in >> description;
        in >> url;
        in >> line;
        in >> column;

        emit remoteLog(msgType, description, url, line, column);
    } else if (method == "clearLog()") {
        emit clearLog();
    } else if (method == "activeDocumentChanged(QString)") {
        QString path;

        QDataStream in(content);
        in >> path;

        if (path.isEmpty() || !QDir::isRelativePath(path)) {
            qCritical() << "Invalid argument to remote call activeDocumentChanged."
                        << "Relative file path expected:" << path;
            return;
        }

        emit activeDocumentChanged(LiveDocument(path));
    }
}

/*!
 * \fn RemotePublisher::connected()
 *
 * The signal is emitted when the IPC is connected
*/

/*!
 * \fn RemotePublisher::disconnected()
 *
 * The signal is emitted when the IPC is disconnected
*/

/*!
 * \fn RemotePublisher::pinOk(bool ok)
 *
 * The signal is emitted after receiving the pinOk IPC call
 * with \a ok to indicate a valid pin
*/

/*!
 * \fn RemotePublisher::connectionError(QAbstractSocket::SocketError error)
 *
 * The signal is emitted when a connection error \a error appears on the IPC
 * level
*/

/*!
 * \fn RemotePublisher::remoteLog(int type, const QString &msg, const QUrl &url = QUrl(), int line = -1, int column = -1)
 *
 *  The signal is emmited after receiving a log call from a remote client. With the \a type, \a msg, \a url,
 * \a line and \a column of the log entry.
 */

/*!
 * \fn RemotePublisher::clearLog()
 *
 * The signal is emmited after receiving a clearLog call from a remote client.
 */

/*!
 * \fn RemotePublisher::needsPinAuthentication()
 *
 * The signal is emitted after receiving the needsPinAuthentication IPC call,
 * to indicate the client requires a pin authentication to continue.
 */

/*!
 * \fn RemotePublisher::needsPublishWorkspace()
 *
 * The signal is emitted after receiving the needsPublishWorkspace IPC call,
 * to indicate the client asks for (re)sending all workspace documents.
 */

/*!
 * \fn RemotePublisher::activeDocumentChanged(const LiveDocument &document)
 *
 * The signal is emitted after receiving the activeDocumentChanged IPC call,
 * to indicate the client's active \a document has changed.
 */

/*! \fn RemotePublisher::sentSuccessfully(const QUuid& uuid)
 *
 * The signal is emitted after the package identified by \a uuid has been send
 */

/*!
 * \fn RemotePublisher::sendingError(const QUuid &uuid, QAbstractSocket::SocketError socketError)
 *
 * The signal is emitted when an error occurred while sending a package \a uuid
 * with the error \a socketError
 */