| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <QByteArray> |
| 7 | #include <QLocalServer> |
| 8 | #include <QLocalSocket> |
| 9 | #include <QObject> |
| 10 | #include <QString> |
| 11 | #include <QStringList> |
| 12 | #include <string_view> |
| 13 | |
| 14 | class SingleInstance : public QObject |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | public: |
| 18 | explicit SingleInstance(std::string_view name, QObject *parent = 0); |
| 19 | |
| 20 | bool mainInstance(QStringList const &args); |
| 21 | |
| 22 | signals: |
| 23 | void newInstance(QStringList args); |
| 24 | |
| 25 | private slots: |
| 26 | void newConnection(); |
| 27 | |
| 28 | private: |
| 29 | QByteArray toRawData(QStringList const &data) const; |
| 30 | |
| 31 | QString const name_; |
| 32 | QLocalServer server_; |
| 33 | }; |
| 34 | |
| 35 | class SingleInstanceClient : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | public: |
| 39 | SingleInstanceClient(QLocalSocket *client); |
| 40 | |
| 41 | signals: |
| 42 | void newInstance(QStringList args); |
| 43 | |
| 44 | public slots: |
| 45 | void onReadyRead(); |
| 46 | void onDisconnected(); |
| 47 | |
| 48 | private: |
| 49 | QStringList fromRawData(QByteArray const &data) const; |
| 50 | |
| 51 | QStringList args_; |
| 52 | }; |