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 <QLocalSocket>
15#include <QLockFile>
16
24class SingleApplication : public QApplication {
25 Q_OBJECT
26
27public:
40 SingleApplication(int &argc, char *argv[], // NOLINT(modernize-avoid-c-arrays)
41 QString uniqueKey);
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;
91 static auto serverRunsAsThisUser(const QLocalSocket &socket) -> bool;
96 auto isRunning() -> bool;
108 auto sendMessage(const QString &message) -> bool;
109
110public slots:
119 void receiveMessage();
120
121signals:
126 void messageAvailable(const QString &message);
127
128private:
133 auto peerIsListening() -> bool;
142 void claimOrFollow();
149 void becomePrimary();
157 void waitForPeer();
164 auto forwardMessage(const QString &message) -> bool;
165
170 bool _isRunning{false};
172 QString _uniqueKey;
174 QLockFile _lock;
179 QScopedPointer<QLocalServer> localServer;
180
182 static const int timeout = 1000;
190 static const int maxMessageBytes = 64 * 1024;
192 static const int takeoverProbes = 5;
194 static const int takeoverProbeIntervalMs = 100;
195};
196
197#endif // SRC_SINGLEAPPLICATION_H_
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.