aboutsummaryrefslogtreecommitdiff
path: root/src/singleapplication.cpp
blob: eb51d0c487644fadfafa8da2ae02b8d2e9a3576a (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * 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: git://neueland.iserlohn-fortress.net/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "singleapplication.h"
#include <QDataStream>
#include <QLocalServer>
#include <QLocalSocket>

SingleApplication::SingleApplication(int &argc, char **argv)
    : QApplication(argc, argv)
{
#ifdef Q_OS_UNIX
    // could be a path such as "/tmp/foo"
    LOCALSERVER_KEY = "smolbote_socket";
#elif Q_OS_WIN32
    // could be a pipe path such as "\\.\pipe\foo"
    LOCALSERVER_KEY = "\\.\pipe\smolbote_socket";
#endif
}

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)
{
    // if a name has been set
    if(!name.isEmpty()) {
        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);
        if(!m_localServer->listen(LOCALSERVER_KEY)) {
            // for some reason, we still can't bind the socket
            return false;
        }
        return true;
    }
}

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());
}