KDSingleApplication API Documentation  1.1
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.
21 class KDSingleApplicationPrivate
22 {
23 public:
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 
41 private:
42  Q_DECLARE_PUBLIC(KDSingleApplication)
43 
44  KDSingleApplication *q_ptr;
45  QString m_name;
46 
47  KDSingleApplicationLocalSocket m_impl;
48 };
49 
50 KDSingleApplicationPrivate::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 
64 static QString extractExecutableName(const QString &applicationFilePath)
65 {
66  return QFileInfo(applicationFilePath).fileName();
67 }
68 
70  : KDSingleApplication(extractExecutableName(QCoreApplication::applicationFilePath()), parent)
71 {
72 }
73 
74 KDSingleApplication::KDSingleApplication(const QString &name, QObject *parent)
75  : QObject(parent)
76  , d_ptr(new KDSingleApplicationPrivate(name, Option::IncludeUsernameInSocketName | Option::IncludeSessionInSocketName, this))
77 {
78 }
79 
80 KDSingleApplication::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 
98 bool KDSingleApplication::sendMessage(const QByteArray &message)
99 {
100  return sendMessageWithTimeout(message, 5000);
101 }
102 
103 bool KDSingleApplication::sendMessageWithTimeout(const QByteArray &message, int timeout)
104 {
105  Q_ASSERT(!isPrimaryInstance());
106 
107  Q_D(KDSingleApplication);
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)

© 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 by doxygen 1.9.1