SingleApplication
Replacement for QtSingleApplication
singleapplication.h
1 // Copyright (c) Itay Grudev 2015 - 2023
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // Permission is not granted to use this software or any of the associated files
11 // as sample data for the purposes of building machine learning models.
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 #ifndef SINGLE_APPLICATION_H
25 #define SINGLE_APPLICATION_H
26 
27 #include <QtCore/QtGlobal>
28 #include <QtNetwork/QLocalSocket>
29 
30 #ifndef QAPPLICATION_CLASS
31  #define QAPPLICATION_CLASS QCoreApplication
32 #endif
33 
34 #include QT_STRINGIFY(QAPPLICATION_CLASS)
35 
36 class SingleApplicationPrivate;
37 
43 class SingleApplication : public QAPPLICATION_CLASS
44 {
45  Q_OBJECT
46 
47  using app_t = QAPPLICATION_CLASS;
48 
49 public:
59  enum Mode {
63  User = 1 << 0,
67  System = 1 << 1,
79  ExcludeAppPath = 1 << 4
80  };
81  Q_DECLARE_FLAGS(Options, Mode)
82 
83 
103  explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000, const QString &userData = {} );
104  ~SingleApplication() override;
105 
110  bool isPrimary() const;
111 
116  bool isSecondary() const;
117 
122  quint32 instanceId() const;
123 
128  qint64 primaryPid() const;
129 
134  QString primaryUser() const;
135 
140  QString currentUser() const;
141 
145  enum SendMode {
146  NonBlocking,
148  };
149 
158  bool sendMessage( const QByteArray &message, int timeout = 100, SendMode sendMode = NonBlocking );
159 
164  QStringList userData() const;
165 
166 Q_SIGNALS:
172 
176  void receivedMessage( quint32 instanceId, QByteArray message );
177 
178 private:
179  SingleApplicationPrivate *d_ptr;
180  Q_DECLARE_PRIVATE(SingleApplication)
181  void abortSafely();
182 };
183 
184 Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
185 
186 #endif // SINGLE_APPLICATION_H
Handles multiple instances of the same Application.
Definition: singleapplication.h:44
QString currentUser() const
Returns the username of the current user.
void receivedMessage(quint32 instanceId, QByteArray message)
Triggered whenever there is a message received from a secondary instance.
void instanceStarted()
Triggered whenever a new instance had been started, except for secondary instances if the Mode::Secon...
SendMode
Mode of operation of sendMessage.
Definition: singleapplication.h:145
@ BlockUntilPrimaryExit
Definition: singleapplication.h:147
bool isSecondary() const
Checks if the instance is a secondary instance.
bool isPrimary() const
Checks if the instance is primary instance.
QStringList userData() const
Get the set user data.
qint64 primaryPid() const
Returns the process ID (PID) of the primary instance.
QString primaryUser() const
Returns the username of the user running the primary instance.
quint32 instanceId() const
Returns a unique identifier for the current instance.
bool sendMessage(const QByteArray &message, int timeout=100, SendMode sendMode=NonBlocking)
Sends a message to the primary instance.
Mode
Mode of operation of SingleApplication. Whether the block should be user-wide or system-wide and whet...
Definition: singleapplication.h:59
@ User
Definition: singleapplication.h:63
@ System
Definition: singleapplication.h:67
@ ExcludeAppVersion
Definition: singleapplication.h:75
@ ExcludeAppPath
Definition: singleapplication.h:79
@ SecondaryNotification
Definition: singleapplication.h:71