aboutsummaryrefslogtreecommitdiff
path: root/src/singleapplication.cpp
blob: 3c442b4f52a3f4eb50cdee873697acd55e9a55bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * 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());
}