summary refs log tree commit diff
path: root/src/RunGuard.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/RunGuard.h')
-rw-r--r--src/RunGuard.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/RunGuard.h b/src/RunGuard.h
new file mode 100644
index 00000000..f9a9641a
--- /dev/null
+++ b/src/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)
+};