diff src/runguard.h @ 228:37d5f4329449

Implement single running instance check to prevent problems with the SQLite database.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 20 Mar 2018 13:45:21 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/runguard.h	Tue Mar 20 13:45:21 2018 +0200
@@ -0,0 +1,35 @@
+//
+// Taken and modified from https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection
+//
+#ifndef RUNGUARD_H
+#define RUNGUARD_H
+
+#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)
+};
+
+
+#endif // RUNGUARD_H