From 2d8078a02740163696a2214fdd7bcd5e58bb5883 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 2 Jun 2021 10:56:37 -0400
Subject: [PATCH] runguard: enable on linux

GitLab: #350
GitLab: #439
Change-Id: Iba420995401cb4a8c20f3611473eb6f5bd22334b
---
 src/runguard.cpp | 14 +++++++++-----
 src/runguard.h   |  4 ++--
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/runguard.cpp b/src/runguard.cpp
index b20cd16a9..36d13a2bf 100644
--- a/src/runguard.cpp
+++ b/src/runguard.cpp
@@ -22,6 +22,7 @@
 #include "mainapplication.h"
 
 #include <QCryptographicHash>
+#include <QLocalSocket>
 
 namespace {
 
@@ -77,16 +78,16 @@ RunGuard::isAnotherRunning()
 bool
 RunGuard::tryToRun()
 {
-#ifdef Q_OS_WIN
     if (isAnotherRunning()) {
         /*
          * This is a secondary instance,
          * connect to the primary instance to trigger a restore
          * then fail.
          */
-        if (socket_ == nullptr) {
+        if (!socket_)
             socket_ = new QLocalSocket();
-        }
+        if (!socket_)
+            return false;
         if (socket_->state() == QLocalSocket::UnconnectedState
             || socket_->state() == QLocalSocket::ClosingState) {
             socket_->connectToServer(key_);
@@ -94,7 +95,11 @@ RunGuard::tryToRun()
         if (socket_->state() == QLocalSocket::ConnectingState) {
             socket_->waitForConnected();
         }
-        return false;
+        if (socket_->state() == QLocalSocket::ConnectedState) {
+            return false;
+        }
+        // If not connected, this means that the server doesn't exists
+        // and the app can be relaunched (can be the case after a client crash or Ctrl+C)
     }
 
     memLock_.acquire();
@@ -117,7 +122,6 @@ RunGuard::tryToRun()
                      &QLocalServer::newConnection,
                      this,
                      &RunGuard::tryRestorePrimaryInstance);
-#endif
 
     return true;
 }
diff --git a/src/runguard.h b/src/runguard.h
index c9757d07f..ce6be9228 100644
--- a/src/runguard.h
+++ b/src/runguard.h
@@ -52,8 +52,8 @@ private:
     QSharedMemory sharedMem_;
     QSystemSemaphore memLock_;
 
-    QLocalSocket* socket_;
-    QLocalServer* server_;
+    QLocalSocket* socket_ {nullptr};
+    QLocalServer* server_ {nullptr};
 
     Q_DISABLE_COPY(RunGuard)
 };
-- 
GitLab