2016-09-15 07:07:13 -04:00
|
|
|
/*
|
|
|
|
* RetroShare Android QML App
|
2017-03-17 12:22:58 -04:00
|
|
|
* Copyright (C) 2016-2017 Gioacchino Mazzurco <gio@eigenlab.org>
|
2016-09-15 07:07:13 -04:00
|
|
|
* Copyright (C) 2016 Manu Pineda <manu@cooperativa.cat>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-09-04 09:01:44 -04:00
|
|
|
#include "libresapilocalclient.h"
|
2016-12-08 09:56:23 -05:00
|
|
|
|
|
|
|
#include <QJSEngine>
|
2016-09-04 09:01:44 -04:00
|
|
|
|
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
void LibresapiLocalClient::openConnection(QString socketPath)
|
2016-09-04 09:01:44 -04:00
|
|
|
{
|
2016-09-15 07:07:13 -04:00
|
|
|
connect(& mLocalSocket, SIGNAL(error(QLocalSocket::LocalSocketError)),
|
|
|
|
this, SLOT(socketError(QLocalSocket::LocalSocketError)));
|
|
|
|
connect(& mLocalSocket, SIGNAL(readyRead()),
|
|
|
|
this, SLOT(read()));
|
|
|
|
mLocalSocket.connectToServer(socketPath);
|
2016-09-04 09:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-12-08 09:56:23 -05:00
|
|
|
int LibresapiLocalClient::request( const QString& path, const QString& jsonData,
|
|
|
|
QJSValue callback )
|
2016-09-04 09:01:44 -04:00
|
|
|
{
|
2016-12-08 09:56:23 -05:00
|
|
|
QByteArray data;
|
|
|
|
data.append(path); data.append('\n');
|
|
|
|
data.append(jsonData); data.append('\n');
|
|
|
|
callbackQueue.enqueue(callback);
|
2017-03-17 12:22:58 -04:00
|
|
|
return mLocalSocket.write(data);
|
2016-09-04 09:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
void LibresapiLocalClient::socketError(QLocalSocket::LocalSocketError)
|
2016-09-04 09:01:44 -04:00
|
|
|
{
|
2016-12-08 09:56:23 -05:00
|
|
|
qDebug() << "Socket Eerror!!" << mLocalSocket.errorString();
|
2016-09-04 09:01:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibresapiLocalClient::read()
|
|
|
|
{
|
2016-12-08 09:56:23 -05:00
|
|
|
QString receivedMsg(mLocalSocket.readLine());
|
|
|
|
QJSValue callback(callbackQueue.dequeue());
|
|
|
|
if(callback.isCallable())
|
2016-09-15 07:07:13 -04:00
|
|
|
{
|
2016-12-08 09:56:23 -05:00
|
|
|
QJSValue params = callback.engine()->newObject();
|
|
|
|
params.setProperty("response", receivedMsg);
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-12-08 09:56:23 -05:00
|
|
|
callback.call(QJSValueList { params });
|
|
|
|
}
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-12-08 09:56:23 -05:00
|
|
|
emit goodResponseReceived(receivedMsg); /// @deprecated
|
|
|
|
emit responseReceived(receivedMsg);
|
2016-09-04 09:01:44 -04:00
|
|
|
}
|