KDSingleApplication API Documentation 1.1
Loading...
Searching...
No Matches
kdsingleapplication.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDSingleApplication.
3
4 SPDX-FileCopyrightText: 2019 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5
6 SPDX-License-Identifier: MIT
7
8 Contact KDAB at <info@kdab.com> for commercial licensing options.
9*/
10
11#include "kdsingleapplication.h"
12
13#include <QtCore/QCoreApplication>
14#include <QtCore/QFileInfo>
15
16// TODO: make this pluggable.
17#include "kdsingleapplication_localsocket_p.h"
18
19// Avoiding dragging in Qt private APIs for now, so this does not inherit
20// from QObjectPrivate.
21class KDSingleApplicationPrivate
22{
23public:
24 explicit KDSingleApplicationPrivate(const QString &name, KDSingleApplication::Options options, KDSingleApplication *q);
25
26 QString name() const
27 {
28 return m_name;
29 }
30
31 bool isPrimaryInstance() const
32 {
33 return m_impl.isPrimaryInstance();
34 }
35
36 bool sendMessage(const QByteArray &message, int timeout)
37 {
38 return m_impl.sendMessage(message, timeout);
39 }
40
41private:
42 Q_DECLARE_PUBLIC(KDSingleApplication)
43
45 QString m_name;
46
47 KDSingleApplicationLocalSocket m_impl;
48};
49
50KDSingleApplicationPrivate::KDSingleApplicationPrivate(const QString &name, KDSingleApplication::Options options, KDSingleApplication *q)
51 : q_ptr(q)
52 , m_name(name)
53 , m_impl(name, options)
54{
55 if (Q_UNLIKELY(name.isEmpty()))
56 qFatal("KDSingleApplication requires a non-empty application name");
57
58 if (isPrimaryInstance()) {
59 QObject::connect(&m_impl, &KDSingleApplicationLocalSocket::messageReceived,
61 }
62}
63
64static QString extractExecutableName(const QString &applicationFilePath)
65{
66 return QFileInfo(applicationFilePath).fileName();
67}
68
73
75 : QObject(parent)
76 , d_ptr(new KDSingleApplicationPrivate(name, Option::IncludeUsernameInSocketName | Option::IncludeSessionInSocketName, this))
77{
78}
79
80KDSingleApplication::KDSingleApplication(const QString &name, Options options, QObject *parent)
81 : QObject(parent)
82 , d_ptr(new KDSingleApplicationPrivate(name, options, this))
83{
84}
85
87{
88 Q_D(const KDSingleApplication);
89 return d->name();
90}
91
93{
94 Q_D(const KDSingleApplication);
95 return d->isPrimaryInstance();
96}
97
99{
100 return sendMessageWithTimeout(message, 5000);
101}
102
104{
105 Q_ASSERT(!isPrimaryInstance());
106
108 return d->sendMessage(message, timeout);
109}
110
111
bool sendMessage(const QByteArray &message)
bool sendMessageWithTimeout(const QByteArray &message, int timeout)
void messageReceived(const QByteArray &message)
KDSingleApplication(QObject *parent=nullptr)
static QString extractExecutableName(const QString &applicationFilePath)
QString fileName() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool isEmpty() const const

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDSingleApplication
A helper class for single-instance policy Qt applications
Generated on Tue Jul 9 2024 00:00:38 for KDSingleApplication API Documentation by doxygen 1.9.8