SingleApplication
Replacement for QtSingleApplication
singleapplication.h
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 #ifndef SINGLE_APPLICATION_H
24 #define SINGLE_APPLICATION_H
25 
26 #include <QtCore/QtGlobal>
27 #include <QtNetwork/QLocalSocket>
28 
29 #ifndef QAPPLICATION_CLASS
30  #define QAPPLICATION_CLASS QCoreApplication
31 #endif
32 
33 #include QT_STRINGIFY(QAPPLICATION_CLASS)
34 
35 class SingleApplicationPrivate;
36 
42 class SingleApplication : public QAPPLICATION_CLASS
43 {
44  Q_OBJECT
45 
46  using app_t = QAPPLICATION_CLASS;
47 
48 public:
58  enum Mode {
62  User = 1 << 0,
66  System = 1 << 1,
78  ExcludeAppPath = 1 << 4
79  };
80  Q_DECLARE_FLAGS(Options, Mode)
81 
82 
102  explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000, const QString &userData = {} );
103  ~SingleApplication() override;
104 
109  bool isPrimary() const;
110 
115  bool isSecondary() const;
116 
121  quint32 instanceId() const;
122 
127  qint64 primaryPid() const;
128 
133  QString primaryUser() const;
134 
139  QString currentUser() const;
140 
144  enum SendMode {
145  NonBlocking,
147  };
148 
157  bool sendMessage( const QByteArray &message, int timeout = 100, SendMode sendMode = NonBlocking );
158 
163  QStringList userData() const;
164 
165 Q_SIGNALS:
171 
175  void receivedMessage( quint32 instanceId, QByteArray message );
176 
177 private:
178  SingleApplicationPrivate *d_ptr;
179  Q_DECLARE_PRIVATE(SingleApplication)
180  void abortSafely();
181 };
182 
183 Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
184 
185 #endif // SINGLE_APPLICATION_H
Handles multiple instances of the same Application.
Definition: singleapplication.h:43
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:144
@ BlockUntilPrimaryExit
Definition: singleapplication.h:146
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:58
@ User
Definition: singleapplication.h:62
@ System
Definition: singleapplication.h:66
@ ExcludeAppVersion
Definition: singleapplication.h:74
@ ExcludeAppPath
Definition: singleapplication.h:78
@ SecondaryNotification
Definition: singleapplication.h:70