From 9fbab94798455c79bfadee9041a78bd47ff642e4 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 2 Jul 2018 09:24:39 +0200 Subject: Replace SingleApplication with itay-grudev/SingleApplication --- src/singleapplication.cpp | 109 ---------------------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 src/singleapplication.cpp (limited to 'src/singleapplication.cpp') 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 -#include -#include -#include -#include - -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()); -} -- cgit v1.2.1