QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
SingleApplication Class Reference

QApplication subclass enforcing single-instance behavior via IPC. Detects whether another instance with the same unique key is running and enables forwarding messages (e.g., command-line arguments) to it. More...

#include <singleapplication.h>

Inheritance diagram for SingleApplication:
Collaboration diagram for SingleApplication:

Public Slots

void receiveMessage ()
 Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent.

Signals

void messageAvailable (const QString &message)
 messageAvailable notification from commandline

Public Member Functions

 SingleApplication (int &argc, char *argv[], QString uniqueKey)
 Construct a SingleApplication with a unique instance key.
auto isRunning () -> bool
 Query whether another instance is already running.
auto sendMessage (const QString &message) -> bool
 Send a message to the running instance.

Static Public Member Functions

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()).
static auto privateKeyIn (const QString &base, const QString &dir) -> QString
 privateKey() with the folder given.
static auto lockPathFor (const QString &key) -> QString
 The lock file that marks the instance listening on key.
static auto serverRunsAsThisUser (const QLocalSocket &socket) -> bool
 Whether the server socket is connected to runs as this user.

Private Member Functions

auto peerIsListening () -> bool
 Probe whether an instance is actually accepting connections.
void claimOrFollow ()
 Take the lock and listen, or become the lock holder's secondary.
void becomePrimary ()
 Listen on the key; this instance holds the lock.
void waitForPeer ()
 Become the secondary of the live instance that holds the lock.
auto forwardMessage (const QString &message) -> bool
 Deliver a message over the local socket.

Private Attributes

bool _isRunning {false}
 true when another instance holds the lock and answers on the socket; this instance should forward and exit.
QString _uniqueKey
 Name of the local socket; also names the lock file.
QLockFile _lock
 Held by the listening instance for as long as it runs.
QScopedPointer< QLocalServer > localServer
 Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed.

Static Private Attributes

static const int timeout = 1000
 Milliseconds to wait for a socket connect or write.
static const int maxMessageBytes = 64 * 1024
 Upper bound for one forwarded message (a command line).
static const int takeoverProbes = 5
 Probes for the lock's holder to start listening before giving up on IPC.
static const int takeoverProbeIntervalMs = 100
 Pause between two takeover probes, in milliseconds.

Detailed Description

QApplication subclass enforcing single-instance behavior via IPC. Detects whether another instance with the same unique key is running and enables forwarding messages (e.g., command-line arguments) to it.

Definition at line 24 of file singleapplication.h.

Constructor & Destructor Documentation

◆ SingleApplication()

SingleApplication::SingleApplication ( int & argc,
char * argv[],
QString uniqueKey )

Construct a SingleApplication with a unique instance key.

Takes the lock file for the key (lockPathFor()). Whoever holds it is the instance that listens; a lock left by a crashed instance is stale and taken over. When another live instance holds it this one probes the socket and, if it answers, becomes a secondary: isRunning() turns true.

Parameters
argcReference to program argc forwarded to QApplication.
argvProgram argv forwarded to QApplication.
uniqueKeyName of the local socket: a full path (see privateKey()) or a bare name, which Qt puts in the temporary folder.

Definition at line 26 of file singleapplication.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ becomePrimary()

void SingleApplication::becomePrimary ( )
private

Listen on the key; this instance holds the lock.

A socket file left by a crashed instance is removed first. When listen() fails the lock is released and this instance runs without IPC.

Definition at line 218 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ claimOrFollow()

void SingleApplication::claimOrFollow ( )
private

Take the lock and listen, or become the lock holder's secondary.

A free lock, or one left by a crashed instance, is taken and this instance listens (becomePrimary()). A lock held by a live instance makes this one wait for that instance's socket (waitForPeer()). A lock that cannot be taken at all leaves this instance without IPC.

Definition at line 199 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ forwardMessage()

auto SingleApplication::forwardMessage ( const QString & message) -> bool
private

Deliver a message over the local socket.

Parameters
messageText to send; an empty string is sent as a single NUL so the peer still sees a connection with data.
Returns
true if the peer accepted the whole payload.

Definition at line 249 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRunning()

auto SingleApplication::isRunning ( ) -> bool

Query whether another instance is already running.

Returns
true if another instance with the same key is running.

Definition at line 181 of file singleapplication.cpp.

Here is the caller graph for this function:

◆ lockPathFor()

auto SingleApplication::lockPathFor ( const QString & key) -> QString
static

The lock file that marks the instance listening on key.

Next to the socket for a full path; for a bare name in the temporary folder, where Qt puts that socket too, with any slash replaced.

Parameters
keyThe constructor's key.
Returns
The lock file's path.

Definition at line 140 of file singleapplication.cpp.

Here is the caller graph for this function:

◆ messageAvailable

void SingleApplication::messageAvailable ( const QString & message)
signal

messageAvailable notification from commandline

Parameters
messageargs sent to qtpass executable
Here is the call graph for this function:
Here is the caller graph for this function:

◆ peerIsListening()

auto SingleApplication::peerIsListening ( ) -> bool
private

Probe whether an instance is actually accepting connections.

Returns
true if a connection to the unique key succeeded.

Definition at line 275 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ privateKey()

auto SingleApplication::privateKey ( const QString & base) -> QString
static

The key to construct with: a socket path in a folder only this user can write to (see privateKeyIn()).

The socket used to be named after the login name in the shared temporary folder, next to a shared-memory segment and its semaphore. Another local user could create those first and keep QtPass from starting (scan ANT-2026-1JUBRKLZ). The folder is QStandardPaths::RuntimeLocation, which Qt refuses when another user owns it. On Windows the key stays base: a pipe name cannot hold a path. Call it before the application object exists.

Parameters
basePer-user name, such as the login name plus "QtPass".
Returns
The key for the constructor.

Definition at line 37 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ privateKeyIn()

auto SingleApplication::privateKeyIn ( const QString & base,
const QString & dir ) -> QString
static

privateKey() with the folder given.

Without a folder the key is base plus a random suffix: the launch then runs as its own instance, without IPC, rather than on a name in the shared temporary folder that someone else can take.

Parameters
basePer-user name, such as the login name plus "QtPass".
dirFolder only this user can write to; empty when there is none.
Returns
A path in dir named after a hash of base, or base plus a random suffix.

Definition at line 52 of file singleapplication.cpp.

Here is the caller graph for this function:

◆ receiveMessage

void SingleApplication::receiveMessage ( )
slot

Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent.

Each accepted socket is read asynchronously so a peer that connects without sending anything cannot stall the GUI thread; the message is delivered once the peer disconnects and the socket is then deleted.

Definition at line 152 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendMessage()

auto SingleApplication::sendMessage ( const QString & message) -> bool

Send a message to the running instance.

When delivery fails the peer is taken to have exited since the constructor probed it: this instance takes its lock and becomes the listening instance, so isRunning() turns false and the window the caller opens next is reachable by later launches. If the peer still holds the lock it is probed again, as the constructor does.

Parameters
messageText to deliver (typically forwarded command-line args).
Returns
true if the message was successfully delivered.

Definition at line 183 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serverRunsAsThisUser()

auto SingleApplication::serverRunsAsThisUser ( const QLocalSocket & socket) -> bool
static

Whether the server socket is connected to runs as this user.

On Windows a named pipe's name is machine-wide: another user's session could open the pipe of the key first, and a launch would then forward its command line there and exit without a window. So the server's process is looked up (GetNamedPipeServerProcessId()) and its token's user compared with this process's; anything that cannot be checked counts as someone else's. Elsewhere the socket sits in a folder only this user can open (privateKey()), and this is always true.

Parameters
socketA socket connected to the key.
Returns
true when the server is this user's.

Definition at line 110 of file singleapplication.cpp.

Here is the caller graph for this function:

◆ waitForPeer()

void SingleApplication::waitForPeer ( )
private

Become the secondary of the live instance that holds the lock.

It may still be starting, so it is probed a few times (see takeoverProbes); if it never answers, isRunning() stays false and the caller runs without IPC rather than not at all.

Definition at line 238 of file singleapplication.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _isRunning

bool SingleApplication::_isRunning {false}
private

true when another instance holds the lock and answers on the socket; this instance should forward and exit.

Definition at line 170 of file singleapplication.h.

◆ _lock

QLockFile SingleApplication::_lock
private

Held by the listening instance for as long as it runs.

Definition at line 174 of file singleapplication.h.

◆ _uniqueKey

QString SingleApplication::_uniqueKey
private

Name of the local socket; also names the lock file.

Definition at line 172 of file singleapplication.h.

◆ localServer

QScopedPointer<QLocalServer> SingleApplication::localServer
private

Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed.

Definition at line 179 of file singleapplication.h.

◆ maxMessageBytes

const int SingleApplication::maxMessageBytes = 64 * 1024
staticprivate

Upper bound for one forwarded message (a command line).

The socket is user-only, but a peer that keeps a connection open and streams data would otherwise grow the per-connection buffer without limit; anything past this is dropped and the peer disconnected.

Definition at line 190 of file singleapplication.h.

◆ takeoverProbeIntervalMs

const int SingleApplication::takeoverProbeIntervalMs = 100
staticprivate

Pause between two takeover probes, in milliseconds.

Definition at line 194 of file singleapplication.h.

◆ takeoverProbes

const int SingleApplication::takeoverProbes = 5
staticprivate

Probes for the lock's holder to start listening before giving up on IPC.

Definition at line 192 of file singleapplication.h.

◆ timeout

const int SingleApplication::timeout = 1000
staticprivate

Milliseconds to wait for a socket connect or write.

Definition at line 182 of file singleapplication.h.


The documentation for this class was generated from the following files: