Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
SingleInstance.h
Go to the documentation of this file.
1// The MIT License (MIT)
2//
3// Copyright (c) Itay Grudev 2015 - 2018
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23#pragma once
24
25#include <QCoreApplication>
26#include <QtCore/QtGlobal>
27#include <QtNetwork/QLocalSocket>
28
29class SingleInstancePrivate;
30
34class SingleInstance : public QObject
35{
36 Q_OBJECT
37
38 public:
49 enum Mode
50 {
51 User = 1 << 0,
52 System = 1 << 1,
55 ExcludeAppPath = 1 << 4
56 };
57 Q_DECLARE_FLAGS(Options, Mode)
58
59
62 explicit SingleInstance(QObject* parent = nullptr);
67 explicit SingleInstance(bool allowSecondary, Options options = Mode::User, int timeout = 1000, const QString& userData = {}, QObject* parent = nullptr);
68
72 ~SingleInstance() override;
73
88 void initialize(bool allowSecondary = false, Options options = Mode::User, int timeout = 1000, const QString& userData = {});
89
93 [[nodiscard]] bool isPrimary() const;
94
98 [[nodiscard]] bool isSecondary() const;
99
103 [[nodiscard]] quint32 instanceId() const;
104
108 [[nodiscard]] qint64 primaryPid() const;
109
113 [[nodiscard]] QString primaryUser() const;
114
118 [[nodiscard]] QString currentUser() const;
119
128 bool sendMessage(const QByteArray& message, int timeout = 100);
129
133 [[nodiscard]] QStringList userData() const;
134
135 Q_SIGNALS:
136
138
139 void receivedMessage(quint32 instanceId, QByteArray message);
140
141 private:
142 SingleInstancePrivate* d_ptr;
143
144 Q_DECLARE_PRIVATE(SingleInstance)
145
146 void abortSafely();
147};
148
149Q_DECLARE_OPERATORS_FOR_FLAGS(SingleInstance::Options)
The SingleInstance class handles multiple instances.
Definition SingleInstance.h:35
QString currentUser() const
Gets the username of the current user.
void instanceStarted()
void receivedMessage(quint32 instanceId, QByteArray message)
void initialize(bool allowSecondary=false, Options options=Mode::User, int timeout=1000, const QString &userData={})
Initializes this instance.
~SingleInstance() override
Destructor.
QStringList userData() const
Get the set user data.
bool isPrimary() const
Gets if the instance is the primary instance.
quint32 instanceId() const
Gets a unique identifier for the current instance.
Mode
Definition SingleInstance.h:50
@ User
Definition SingleInstance.h:51
@ System
Definition SingleInstance.h:52
@ SecondaryNotification
Definition SingleInstance.h:53
@ ExcludeAppPath
Definition SingleInstance.h:55
@ ExcludeAppVersion
Definition SingleInstance.h:54
bool isSecondary() const
Gets if the instance is a secondary instance.
qint64 primaryPid() const
Gets the process ID (PID) of the primary instance.
bool sendMessage(const QByteArray &message, int timeout=100)
Sends a message to the primary instance. Returns true on success.
QString primaryUser() const
Gets the username of the user running the primary instance.