aboutsummaryrefslogtreecommitdiff
path: root/src/singleapplication.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/singleapplication.cpp')
-rw-r--r--src/singleapplication.cpp38
1 files changed, 13 insertions, 25 deletions
diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp
index 720690d..60eaa6d 100644
--- a/src/singleapplication.cpp
+++ b/src/singleapplication.cpp
@@ -10,6 +10,8 @@
#include <QDataStream>
#include <QLocalServer>
#include <QLocalSocket>
+#include <QJsonObject>
+#include <QJsonDocument>
SingleApplication::SingleApplication(int &argc, char **argv)
: QApplication(argc, argv)
@@ -46,7 +48,7 @@ bool SingleApplication::bindLocalSocket(const QString &name)
// there is either no such socket, or the socket wasn't cleaned up
else {
m_localServer = new QLocalServer(this);
- connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::parseMessage);
+ connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::receiveMessage);
// no other server
QLocalServer::removeServer(LOCALSERVER_KEY);
@@ -60,21 +62,12 @@ QString SingleApplication::serverName() const
return m_localServer->fullServerName();
}
-int SingleApplication::sendMessage(const QString &profileName, bool newWindow, const QStringList &urls)
+int SingleApplication::sendMessage(const QJsonObject &message)
{
QLocalSocket socket;
socket.connectToServer(LOCALSERVER_KEY);
if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) {
- QHash<QString, QVariant> hashedParams;
- hashedParams.insert("profile", profileName);
- hashedParams.insert("newWindow", newWindow);
- hashedParams.insert("urls", urls);
-
- QByteArray argumentData;
- QDataStream ds(&argumentData, QIODevice::WriteOnly);
- ds << hashedParams;
-
- socket.write(argumentData);
+ socket.write(QJsonDocument(message).toJson());
socket.waitForBytesWritten(LOCALSERVER_TIMEOUT);
return EXIT_SUCCESS;
}
@@ -82,28 +75,23 @@ int SingleApplication::sendMessage(const QString &profileName, bool newWindow, c
return EXIT_FAILURE;
}
-void SingleApplication::parseMessage()
+void SingleApplication::receiveMessage()
{
QLocalSocket *socket = m_localServer->nextPendingConnection();
- // null socket --> return
if(socket == nullptr) {
return;
}
socket->waitForReadyRead();
+ auto message = socket->readAll();
- QByteArray argumentData = socket->readAll();
-
- // skip if we got no data
- if(argumentData.isEmpty()) {
+ if(message.isEmpty())
return;
- }
-
- QHash<QString, QVariant> params;
- QDataStream ds(argumentData);
- ds >> params;
- socket->deleteLater();
+#ifdef QT_DEBUG
+ qDebug("received message: %s", qUtf8Printable(message));
+#endif
- emit messageAvailable(params["profile"].toString(), params["newWindow"].toBool(), params["urls"].toStringList());
+ QJsonDocument doc = QJsonDocument::fromJson(message);
+ emit messageAvailable(doc.object());
}