2019-01-07 01:00:58 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
2020-10-05 21:31:03 +02:00
|
|
|
// Copyright (c) Itay Grudev 2015 - 2020
|
2019-01-07 01:00:58 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
//
|
|
|
|
// W A R N I N G !!!
|
|
|
|
// -----------------
|
|
|
|
//
|
|
|
|
// This is a modified version of SingleApplication,
|
|
|
|
// The original version is at:
|
|
|
|
//
|
|
|
|
// https://github.com/itay-grudev/SingleApplication
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2019-01-11 01:04:13 +01:00
|
|
|
#ifndef SINGLEAPPLICATION_H
|
|
|
|
#define SINGLEAPPLICATION_H
|
2019-01-07 01:00:58 +01:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
#include <QtGlobal>
|
2019-01-11 01:04:13 +01:00
|
|
|
#include <QApplication>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QFlags>
|
|
|
|
#include <QByteArray>
|
2019-01-07 01:00:58 +01:00
|
|
|
|
|
|
|
class SingleApplicationPrivate;
|
|
|
|
|
|
|
|
/**
|
2021-07-02 18:45:53 +02:00
|
|
|
* @brief The SingleApplication class handles multiple instances of the same Application
|
2020-10-05 21:31:03 +02:00
|
|
|
* @see QApplication
|
2019-01-07 01:00:58 +01:00
|
|
|
*/
|
2021-06-20 19:04:08 +02:00
|
|
|
class SingleApplication : public QApplication { // clazy:exclude=ctor-missing-parent-argument
|
2019-08-26 02:00:47 +02:00
|
|
|
Q_OBJECT
|
2019-01-07 01:00:58 +01:00
|
|
|
|
2022-02-06 00:07:15 +01:00
|
|
|
using app_t = QApplication;
|
2019-01-07 01:00:58 +01:00
|
|
|
|
2020-10-04 02:18:10 +02:00
|
|
|
public:
|
2019-08-26 02:00:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Mode of operation of SingleApplication.
|
|
|
|
* Whether the block should be user-wide or system-wide and whether the
|
|
|
|
* primary instance should be notified when a secondary instance had been
|
|
|
|
* started.
|
|
|
|
* @note Operating system can restrict the shared memory blocks to the same
|
|
|
|
* user, in which case the User/System modes will have no effect and the
|
|
|
|
* block will be user wide.
|
|
|
|
* @enum
|
|
|
|
*/
|
|
|
|
enum Mode {
|
2020-10-04 02:18:10 +02:00
|
|
|
User = 1 << 0,
|
|
|
|
System = 1 << 1,
|
|
|
|
SecondaryNotification = 1 << 2,
|
|
|
|
ExcludeAppVersion = 1 << 3,
|
|
|
|
ExcludeAppPath = 1 << 4
|
2019-08-26 02:00:47 +02:00
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(Options, Mode)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Intitializes a SingleApplication instance with argc command line
|
|
|
|
* arguments in argv
|
|
|
|
* @arg {int &} argc - Number of arguments in argv
|
|
|
|
* @arg {const char *[]} argv - Supplied command line arguments
|
|
|
|
* @arg {bool} allowSecondary - Whether to start the instance as secondary
|
|
|
|
* if there is already a primary instance.
|
|
|
|
* @arg {Mode} mode - Whether for the SingleApplication block to be applied
|
|
|
|
* User wide or System wide.
|
|
|
|
* @arg {int} timeout - Timeout to wait in milliseconds.
|
|
|
|
* @note argc and argv may be changed as Qt removes arguments that it
|
|
|
|
* recognizes
|
|
|
|
* @note Mode::SecondaryNotification only works if set on both the primary
|
|
|
|
* instance and the secondary instance.
|
|
|
|
* @note The timeout is just a hint for the maximum time of blocking
|
|
|
|
* operations. It does not guarantee that the SingleApplication
|
|
|
|
* initialisation will be completed in given time, though is a good hint.
|
|
|
|
* Usually 4*timeout would be the worst case (fail) scenario.
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
explicit SingleApplication(int &argc, char *argv[], const bool allowSecondary = false, const Options options = Mode::User, const int timeout = 1000);
|
2020-06-26 23:01:57 +02:00
|
|
|
~SingleApplication() override;
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns if the instance is the primary instance
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
bool isPrimary() const;
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns if the instance is a secondary instance
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
bool isSecondary() const;
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a unique identifier for the current instance
|
|
|
|
* @returns {qint32}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
quint32 instanceId() const;
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the process ID (PID) of the primary instance
|
|
|
|
* @returns {qint64}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
qint64 primaryPid() const;
|
2019-08-26 02:00:47 +02:00
|
|
|
|
2020-10-05 21:31:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns the username of the user running the primary instance
|
|
|
|
* @returns {QString}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
QString primaryUser() const;
|
2020-10-05 21:31:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the username of the current user
|
|
|
|
* @returns {QString}
|
|
|
|
*/
|
2022-02-06 00:07:15 +01:00
|
|
|
QString currentUser() const;
|
2020-10-05 21:31:03 +02:00
|
|
|
|
2019-08-26 02:00:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Sends a message to the primary instance. Returns true on success.
|
|
|
|
* @param {int} timeout - Timeout for connecting
|
|
|
|
* @returns {bool}
|
|
|
|
* @note sendMessage() will return false if invoked from the primary
|
|
|
|
* instance.
|
|
|
|
*/
|
2021-06-20 19:04:08 +02:00
|
|
|
bool sendMessage(const QByteArray &message, const int timeout = 1000);
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void instanceStarted();
|
2020-10-04 02:18:10 +02:00
|
|
|
void receivedMessage(quint32 instanceId, QByteArray message);
|
2019-08-26 02:00:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
SingleApplicationPrivate *d_ptr;
|
|
|
|
Q_DECLARE_PRIVATE(SingleApplication)
|
2020-10-05 21:31:03 +02:00
|
|
|
void abortSafely();
|
2019-01-07 01:00:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
|
|
|
|
|
2020-10-04 02:18:10 +02:00
|
|
|
#endif // SINGLEAPPLICATION_H
|