aboutsummaryrefslogtreecommitdiff
path: root/src/singleapplication.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/singleapplication.cpp')
-rw-r--r--src/singleapplication.cpp109
1 files changed, 0 insertions, 109 deletions
diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp
deleted file mode 100644
index 3c442b4..0000000
--- a/src/singleapplication.cpp
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: https://neueland.iserlohn-fortress.net/smolbote.hg
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#include "singleapplication.h"
-#include <QDataStream>
-#include <QLocalServer>
-#include <QLocalSocket>
-#include <QJsonObject>
-#include <QJsonDocument>
-
-SingleApplication::SingleApplication(int &argc, char **argv)
- : QApplication(argc, argv)
-{
-}
-
-SingleApplication::~SingleApplication()
-{
- if(m_localServer != nullptr) {
- if(m_localServer->isListening()) {
- m_localServer->close();
- QLocalServer::removeServer(LOCALSERVER_KEY);
- }
- }
-}
-
-/**
- * @brief SingleApplication::bindLocalSocket check for running local server by connecting to it
- * @return true if no other instance, false otherwise
- */
-bool SingleApplication::bindLocalSocket(const QString &name)
-{
- LOCALSERVER_KEY = name;
-
- QLocalSocket socket;
- socket.connectToServer(LOCALSERVER_KEY);
-
- if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) {
- // another server is running
- socket.close();
- return false;
- }
-
- // 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::receiveMessage);
-
- // no other server
- QLocalServer::removeServer(LOCALSERVER_KEY);
- return m_localServer->listen(LOCALSERVER_KEY);
- }
-}
-
-QString SingleApplication::serverName() const
-{
- Q_CHECK_PTR(m_localServer);
- return m_localServer->fullServerName();
-}
-
-int SingleApplication::sendMessage(const QByteArray &data)
-{
- QLocalSocket socket;
- socket.connectToServer(LOCALSERVER_KEY);
- if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) {
- socket.write(data);
- socket.waitForBytesWritten(LOCALSERVER_TIMEOUT);
- return EXIT_SUCCESS;
- }
- return EXIT_FAILURE;
-}
-
-int SingleApplication::sendMessage(const QJsonObject &message)
-{
- QLocalSocket socket;
- socket.connectToServer(LOCALSERVER_KEY);
- if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) {
- socket.write(QJsonDocument(message).toJson());
- socket.waitForBytesWritten(LOCALSERVER_TIMEOUT);
- return EXIT_SUCCESS;
- }
-
- return EXIT_FAILURE;
-}
-
-void SingleApplication::receiveMessage()
-{
- QLocalSocket *socket = m_localServer->nextPendingConnection();
- if(socket == nullptr) {
- return;
- }
-
- socket->waitForReadyRead();
- auto message = socket->readAll();
-
- if(message.isEmpty())
- return;
-
-#ifdef QT_DEBUG
- qDebug("received message: %s", qUtf8Printable(message));
-#endif
-
- QJsonDocument doc = QJsonDocument::fromJson(message);
- emit messageAvailable(doc.object());
-}