summary refs log tree commit diff
path: root/include
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-14 21:55:00 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-14 21:55:00 +0200
commit65672d3dfbdf179df520bbaa5e7e7ba28a27db20 (patch)
tree3db1f2f563f989ecbc64af9048c2285268c2b59f /include
parentUpgrade appveyor to Qt 5.9.2 (diff)
downloadnheko-65672d3dfbdf179df520bbaa5e7e7ba28a27db20.tar.xz
Allow only one application instance
fixes #141
Diffstat (limited to 'include')
-rw-r--r--include/RunGuard.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/RunGuard.h b/include/RunGuard.h
new file mode 100644
index 00000000..f9a9641a
--- /dev/null
+++ b/include/RunGuard.h
@@ -0,0 +1,31 @@
+#pragma once
+
+//
+// Taken from
+// https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection
+//
+
+#include <QObject>
+#include <QSharedMemory>
+#include <QSystemSemaphore>
+
+class RunGuard
+{
+public:
+        RunGuard(const QString &key);
+        ~RunGuard();
+
+        bool isAnotherRunning();
+        bool tryToRun();
+        void release();
+
+private:
+        const QString key;
+        const QString memLockKey;
+        const QString sharedmemKey;
+
+        QSharedMemory sharedMem;
+        QSystemSemaphore memLock;
+
+        Q_DISABLE_COPY(RunGuard)
+};