aboutsummaryrefslogtreecommitdiff
path: root/src/singleapplication.cpp
blob: 720690d4865dd47a72ac3321f0579f9b28a074a0 (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>

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::parseMessage);

        // 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 QString &profileName, bool newWindow, const QStringList &urls)
{
    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.waitForBytesWritten(LOCALSERVER_TIMEOUT);
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}

void SingleApplication::parseMessage()
{
    QLocalSocket *socket = m_localServer->nextPendingConnection();
    // null socket --> return
    if(socket == nullptr) {
        return;
    }

    socket->waitForReadyRead();

    QByteArray argumentData = socket->readAll();

    // skip if we got no data
    if(argumentData.isEmpty()) {
        return;
    }

    QHash<QString, QVariant> params;
    QDataStream ds(argumentData);
    ds >> params;

    socket->deleteLater();

    emit messageAvailable(params["profile"].toString(), params["newWindow"].toBool(), params["urls"].toStringList());
}