1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 Oleg Shparber
4 ** Contact: https://go.zealdocs.org/l/contact
5 **
6 ** This file is part of Zeal.
7 **
8 ** Zeal is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation, either version 3 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Zeal is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with Zeal. If not, see <https://www.gnu.org/licenses/>.
20 **
21 ****************************************************************************/
22 
23 #ifndef ZEAL_CORE_APPLICATIONSINGLETON_H
24 #define ZEAL_CORE_APPLICATIONSINGLETON_H
25 
26 #include <QObject>
27 
28 class QLocalServer;
29 class QSharedMemory;
30 
31 namespace Zeal {
32 namespace Core {
33 
34 class ApplicationSingleton : public QObject
35 {
36     Q_OBJECT
37 public:
38     explicit ApplicationSingleton(QObject *parent = nullptr);
39 
40     bool isPrimary() const;
41     bool isSecondary() const;
42     qint64 primaryPid() const;
43 
44     bool sendMessage(QByteArray &data, int timeout = 500);
45 
46 signals:
47     void messageReceived(const QByteArray &data);
48 
49 private:
50     void setupPrimary();
51     void setupSecondary();
52 
53     static QString computeId();
54 
55     QString m_id;
56 
57     bool m_isPrimary = false;
58     qint64 m_primaryPid = 0;
59 
60     QSharedMemory *m_sharedMemory = nullptr;
61     QLocalServer *m_localServer = nullptr;
62 };
63 
64 } // namespace Core
65 } // namespace Zeal
66 
67 #endif // ZEAL_CORE_APPLICATIONSINGLETON_H
68