QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
singleapplication.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
9#ifndef SRC_SINGLEAPPLICATION_H_
10#define SRC_SINGLEAPPLICATION_H_
11
12#include <QApplication>
13#include <QLocalServer>
14#include <QSharedMemory>
15
24class SingleApplication : public QApplication {
25 Q_OBJECT
26
27public:
39 SingleApplication(int &argc, char *argv[], // NOLINT(modernize-avoid-c-arrays)
40 QString uniqueKey);
45 auto isRunning() -> bool;
56 auto sendMessage(const QString &message) -> bool;
57
58public slots:
67 void receiveMessage();
68
69signals:
74 void messageAvailable(const QString &message);
75
76private:
81 auto peerIsListening() -> bool;
90 void becomePrimary();
97 auto forwardMessage(const QString &message) -> bool;
98
105 QString _uniqueKey;
107 QSharedMemory sharedMemory;
112 QScopedPointer<QLocalServer> localServer;
113
115 static const int timeout = 1000;
123 static const int maxMessageBytes = 64 * 1024;
125 static const int takeoverProbes = 5;
127 static const int takeoverProbeIntervalMs = 100;
128};
129
130#endif // SRC_SINGLEAPPLICATION_H_
QSharedMemory sharedMemory
The one-byte segment whose existence marks a running instance.
bool _isRunning
true when another instance holds the segment and answers on the socket; this instance should forward ...
auto forwardMessage(const QString &message) -> bool
Deliver a message over the local socket.
void receiveMessage()
Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() wit...
QScopedPointer< QLocalServer > localServer
Server accepting forwarded command lines; only set in the primary instance, null in a secondary or wh...
SingleApplication(int &argc, char *argv[], QString uniqueKey)
Construct a SingleApplication with a unique instance key.
void becomePrimary()
Claim the segment and listen; no-op when another instance holds it.
QString _uniqueKey
Name of both the shared-memory segment and the local socket.
static const int timeout
Milliseconds to wait for a socket connect or write.
auto isRunning() -> bool
Query whether another instance is already running.
static const int maxMessageBytes
Upper bound for one forwarded message (a command line).
void messageAvailable(const QString &message)
messageAvailable notification from commandline
static const int takeoverProbes
Probes for the winner of a takeover race before giving up on IPC.
auto sendMessage(const QString &message) -> bool
Send a message to the running instance.
auto peerIsListening() -> bool
Probe whether an instance is actually accepting connections.
static const int takeoverProbeIntervalMs
Pause between two takeover probes, in milliseconds.