KDSingleApplication API Documentation 1.0
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-2023 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 *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 *q)
51 : q_ptr(q)
52 , m_name(name)
53 , m_impl(name)
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, this))
77{
78}
79
81{
82 Q_D(const KDSingleApplication);
83 return d->name();
84}
85
87{
88 Q_D(const KDSingleApplication);
89 return d->isPrimaryInstance();
90}
91
93{
94 return sendMessageWithTimeout(message, 5000);
95}
96
98{
99 Q_ASSERT(!isPrimaryInstance());
100
102 return d->sendMessage(message, timeout);
103}
104
105
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

© 2019-2023 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 Wed Dec 20 2023 00:04:51 for KDSingleApplication API Documentation by doxygen 1.9.8