1 /*
2  * %kadu copyright begin%
3  * Copyright 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * Contains code from QtSingleApplication.
7  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23 
24 #pragma once
25 
26 #include <QtCore/QObject>
27 #include <functional>
28 
29 class QLocalServer;
30 
31 class SingleApplication : public QObject
32 {
33 	Q_OBJECT
34 
35 public:
36 	explicit SingleApplication(
37 		const QString &applicationId,
38 		std::function<void(void)> executeAsFirst,
39 		std::function<void(SingleApplication &)> executeAsNext,
40 		std::function<void(const QString &)> onReceiveMessage,
41 		QObject *parent = nullptr);
42 	virtual ~SingleApplication();
43 
44 	void sendMessage(const QString &message, int timeout) const;
45 
46 private:
47 	static QString defaultApplicationId(const QString &applicationId);
48 	static QString normalizedPrefix(bool useOnlyLastSection, const QString &applicationId);
49 	static QString socketName(const QString &prefix, const QString &applicationId);
50 	static QString lockName(const QString &socketName);
51 
52 	QString m_socketName;
53 	QLocalServer *m_localServer;
54 	std::function<void(const QString &)> m_onReceiveMessage;
55 
56 	void startServer();
57 
58 private slots:
59 	void receiveConnection();
60 
61 };
62