Implement a working Distant Chat prototype in Qml

Deprecate id field in JSON API as it may cause problems in Qml
Offer gxs_id field in JSON API as an id alternative
LibresapiLocalClient support callbacks now an instance may be shared for
  different tasks
Expose an instance of LibresapiLocalClient to Qml, type exposure is kept
  for retrocompatibility but deprecated
Qml app now has a tab that permit to exchange some message with selected
  distant peer
This commit is contained in:
Gio 2016-12-08 15:56:23 +01:00
parent 242338d10c
commit c3aca0cf26
11 changed files with 259 additions and 170 deletions

View file

@ -18,8 +18,8 @@
*/
#include "libresapilocalclient.h"
#include "debugutils.h"
#include <QChar>
#include <QJSEngine>
void LibresapiLocalClient::openConnection(QString socketPath)
@ -31,54 +31,35 @@ void LibresapiLocalClient::openConnection(QString socketPath)
mLocalSocket.connectToServer(socketPath);
}
int LibresapiLocalClient::request(const QString & path, const QString & jsonData)
int LibresapiLocalClient::request( const QString& path, const QString& jsonData,
QJSValue callback )
{
qDebug() << "LibresapiLocalClient::request()" << path << jsonData;
QByteArray data;
data.append(path); data.append('\n');
data.append(jsonData); data.append('\n');
mLocalSocket.write(data);
QByteArray data;
data.append(path); data.append('\n');
data.append(jsonData); data.append('\n');
callbackQueue.enqueue(callback);
mLocalSocket.write(data);
return 1;
return 1;
}
void LibresapiLocalClient::socketError(QLocalSocket::LocalSocketError)
{
myDebug("error!!!!\n" + mLocalSocket.errorString());
qDebug() << "Socket Eerror!!" << mLocalSocket.errorString();
}
void LibresapiLocalClient::read()
{
receivedBytes = mLocalSocket.readLine();
qDebug() << receivedBytes;
if(parseResponse()) // pensar en fer un buffer per parsejar, per evitar errors.
emit goodResponseReceived(QString(receivedBytes));
else
QString receivedMsg(mLocalSocket.readLine());
QJSValue callback(callbackQueue.dequeue());
if(callback.isCallable())
{
QString errMess = "The message was not understood!\n"
"It should be a JSON formatted text file\n"
"Its contents were:\n" + receivedBytes;
myDebug(errMess.replace(QChar('\n'), QChar::LineSeparator));
QJSValue params = callback.engine()->newObject();
params.setProperty("response", receivedMsg);
callback.call(QJSValueList { params });
}
}
bool LibresapiLocalClient::parseResponse()
{
QJsonParseError error;
json = QJsonDocument::fromJson(receivedBytes, &error);
myDebug(QString(json.toJson()).replace(QChar('\n'), QChar::LineSeparator));
if(error.error == QJsonParseError::NoError){
return true;
}
myDebug(error.errorString());
return false;
}
const QJsonDocument & LibresapiLocalClient::getJson()
{
return json;
emit goodResponseReceived(receivedMsg); /// @deprecated
emit responseReceived(receivedMsg);
}