9#ifndef SRC_SINGLEAPPLICATION_H_
10#define SRC_SINGLEAPPLICATION_H_
12#include <QApplication>
13#include <QLocalServer>
14#include <QLocalSocket>
56 static auto privateKey(
const QString &base) -> QString;
68 static auto privateKeyIn(
const QString &base,
const QString &dir) -> QString;
77 static auto lockPathFor(
const QString &key) -> QString;
bool _isRunning
true when another instance holds the lock and answers on the socket; this instance should forward and...
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()
Listen on the key; this instance holds the lock.
QLockFile _lock
Held by the listening instance for as long as it runs.
QString _uniqueKey
Name of the local socket; also names the lock file.
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).
static auto privateKey(const QString &base) -> QString
The key to construct with: a socket path in a folder only this user can write to (see privateKeyIn())...
void messageAvailable(const QString &message)
messageAvailable notification from commandline
static auto lockPathFor(const QString &key) -> QString
The lock file that marks the instance listening on key.
static auto privateKeyIn(const QString &base, const QString &dir) -> QString
privateKey() with the folder given.
static const int takeoverProbes
Probes for the lock's holder to start listening before giving up on IPC.
void claimOrFollow()
Take the lock and listen, or become the lock holder's secondary.
static auto serverRunsAsThisUser(const QLocalSocket &socket) -> bool
Whether the server socket is connected to runs as this user.
auto sendMessage(const QString &message) -> bool
Send a message to the running instance.
void waitForPeer()
Become the secondary of the live instance that holds the lock.
auto peerIsListening() -> bool
Probe whether an instance is actually accepting connections.
static const int takeoverProbeIntervalMs
Pause between two takeover probes, in milliseconds.