From 08461b68154402e9fd26cc5e13b9b7b24c35a812 Mon Sep 17 00:00:00 2001
From: Raghav Gururajan <raghavgururajan@disroot.org>
Date: Fri, 3 Apr 2020 19:42:33 -0400
Subject: [PATCH 17/17] gnu: Add linphone-desktop

* gnu/packages/linphone.scm (linphone-desktop): New variable.
* gnu/packages/patches/linphone-desktop-mutex.patch: New patch file.
---
 gnu/packages/linphone.scm                     |  60 +++++++++
 .../patches/linphone-desktop-mutex.patch      | 127 ++++++++++++++++++
 2 files changed, 187 insertions(+)
 create mode 100644 gnu/packages/patches/linphone-desktop-mutex.patch

diff --git a/gnu/packages/linphone.scm b/gnu/packages/linphone.scm
index b881a820ae..65ae41d361 100644
--- a/gnu/packages/linphone.scm
+++ b/gnu/packages/linphone.scm
@@ -24,6 +24,7 @@
   #:use-module (gnu packages base)
   #:use-module (gnu packages compression)
   #:use-module (gnu packages documentation)
+  #:use-module (gnu packages gcc)
   #:use-module (gnu packages gettext)
   #:use-module (gnu packages gl)
   #:use-module (gnu packages glib)
@@ -34,6 +35,7 @@
   #:use-module (gnu packages image)
   #:use-module (gnu packages java)
   #:use-module (gnu packages linux)
+  #:use-module (gnu packages qt)
   #:use-module (gnu packages pulseaudio)
   #:use-module (gnu packages python)
   #:use-module (gnu packages python-xyz)
@@ -439,3 +441,61 @@ Linphone application is based on, and that anyone can use to add audio
 and video calls or instant messaging capabilities to an application.")
     (home-page "https://gitlab.linphone.org/BC/public/liblinphone")
     (license license:gpl2+)))
+
+(define-public linphone-desktop
+  (package
+    (name "linphoneqt")
+    (version "4.1.1")
+    (source
+     (origin
+       (method url-fetch)
+       (uri
+        (string-append "https://www.linphone.org/releases/sources/" name
+                       "/" name "-" version ".tar.gz"))
+       (sha256
+        (base32 "1g2zrr9li0g1hgs6vys06vr98h5dx36z22hx7a6ry231536c002a"))
+       (patches
+        (list
+         ;; For fixing errors with signal handling.
+         (origin
+           (method url-fetch)
+           (uri
+            (string-append "https://gitlab.linphone.org/BC/public/linphone-desktop/commit/"
+                           "9cf08623e3092fa19366e5c07fbe06898a59f039.diff"))
+           (file-name "linphone-desktop-socket.patch")
+           (sha256
+            (base32 "0cnn63n3rs5m0dbi1yzn33zpp38llh5lz286f2nraadnh7kddfm5")))
+         ;; For fixing errors with crash handling.
+         (search-patch "linphone-desktop-mutex.patch")))))
+    (build-system cmake-build-system)
+    (arguments
+     `(#:tests? #f                      ; No test target
+       #:phases
+       ;; For replacing undeclared variable.
+       (modify-phases %standard-phases
+         (add-after 'unpack 'patch
+           (lambda _
+             (substitute* "src/app/AppController.cpp"
+               (("LINPHONE_QT_GIT_VERSION")
+                "\"4.1.1\""))
+             #t)))))
+    (native-inputs
+     `(("gcc" ,gcc-5)))                 ; Required for libstd.h
+    (inputs
+     `(("bctoolbox" ,bctoolbox)
+       ("belcard" ,belcard)
+       ("bellesip" ,belle-sip)
+       ("linphone" ,liblinphone)
+       ("mediastreamer2" ,mediastreamer)))
+    (propagated-inputs
+     `(("qtbase" ,qtbase)
+       ("qtdeclarative" ,qtdeclarative)
+       ("qtquickcontrols2" ,qtquickcontrols2)
+       ("qtsvg" ,qtsvg)
+       ("qttools" ,qttools)))
+    (synopsis "Belledonne Communications Softphone Application")
+    (description "Linphone is a softphone for voice and video over IP calling
+and instant messaging.  It is fully SIP-based, for all calling, presence
+and IM features.")
+    (home-page "https://gitlab.linphone.org/BC/public/linphone-desktop")
+    (license license:gpl2+)))
diff --git a/gnu/packages/patches/linphone-desktop-mutex.patch b/gnu/packages/patches/linphone-desktop-mutex.patch
new file mode 100644
index 0000000000..29aef4b713
--- /dev/null
+++ b/gnu/packages/patches/linphone-desktop-mutex.patch
@@ -0,0 +1,127 @@
+From 03bed40d638d09f36deb9e0ec3ad1fe1d10e139f Mon Sep 17 00:00:00 2001
+From: Raghav Gururajan <raghavgururajan@disroot.org>
+Date: Fri, 3 Apr 2020 17:13:20 -0400
+Subject: [PATCH] test 2
+
+---
+ .../single-application/SingleApplication.cpp  | 63 ++-----------------
+ .../SingleApplicationPrivate.hpp              |  3 -
+ 2 files changed, 5 insertions(+), 61 deletions(-)
+
+diff --git a/src/app/single-application/SingleApplication.cpp b/src/app/single-application/SingleApplication.cpp
+index 29538ae3..1e6ab396 100644
+--- a/src/app/single-application/SingleApplication.cpp
++++ b/src/app/single-application/SingleApplication.cpp
+@@ -35,7 +35,6 @@
+ 
+ #ifdef Q_OS_UNIX
+   #include <signal.h>
+-  #include <unistd.h>
+ #endif // ifdef Q_OS_UNIX
+ 
+ #ifdef Q_OS_WIN
+@@ -135,10 +134,9 @@ void SingleApplicationPrivate::genBlockServerName (int timeout) {
+ 
+ void SingleApplicationPrivate::startPrimary (bool resetMemory) {
+   #ifdef Q_OS_UNIX
+-    // Handle any further termination signals to ensure the
+-    // QSharedMemory block is deleted even if the process crashes
+-    crashHandler();
++    signal(SIGINT, SingleApplicationPrivate::terminate);
+   #endif // ifdef Q_OS_UNIX
++
+   // Successful creation means that no main process exists
+   // So we start a QLocalServer to listen for connections
+   QLocalServer::removeServer(blockServerName);
+@@ -178,9 +176,7 @@ void SingleApplicationPrivate::startPrimary (bool resetMemory) {
+ 
+ void SingleApplicationPrivate::startSecondary () {
+   #ifdef Q_OS_UNIX
+-    // Handle any further termination signals to ensure the
+-    // QSharedMemory block is deleted even if the process crashes
+-    crashHandler();
++    signal(SIGINT, SingleApplicationPrivate::terminate);
+   #endif // ifdef Q_OS_UNIX
+ }
+ 
+@@ -222,60 +218,11 @@ void SingleApplicationPrivate::connectToPrimary (int msecs, char connectionType)
+ }
+ 
+ #ifdef Q_OS_UNIX
+-  void SingleApplicationPrivate::crashHandler () {
+-    // This guarantees the program will work even with multiple
+-    // instances of SingleApplication in different threads.
+-    // Which in my opinion is idiotic, but lets handle that too.
+-    {
+-      sharedMemMutex.lock();
+-      sharedMem.append(this);
+-      sharedMemMutex.unlock();
+-    }
+-
+-    // Handle any further termination signals to ensure the
+-    // QSharedMemory block is deleted even if the process crashes
+-    signal(SIGHUP, SingleApplicationPrivate::terminate);        // 1
+-    signal(SIGINT, SingleApplicationPrivate::terminate);        // 2
+-    signal(SIGQUIT, SingleApplicationPrivate::terminate);       // 3
+-    signal(SIGILL, SingleApplicationPrivate::terminate);        // 4
+-    signal(SIGABRT, SingleApplicationPrivate::terminate);       // 6
+-    signal(SIGFPE, SingleApplicationPrivate::terminate);        // 8
+-    signal(SIGBUS, SingleApplicationPrivate::terminate);        // 10
+-    signal(SIGSEGV, SingleApplicationPrivate::terminate);       // 11
+-    signal(SIGSYS, SingleApplicationPrivate::terminate);        // 12
+-    signal(SIGPIPE, SingleApplicationPrivate::terminate);       // 13
+-    signal(SIGALRM, SingleApplicationPrivate::terminate);       // 14
+-    signal(SIGTERM, SingleApplicationPrivate::terminate);       // 15
+-    signal(SIGXCPU, SingleApplicationPrivate::terminate);       // 24
+-    signal(SIGXFSZ, SingleApplicationPrivate::terminate);       // 25
+-  }
+ 
+   void SingleApplicationPrivate::terminate (int signum) {
+-    if (signum == SIGINT) {
+-      SingleApplication::instance()->quit();
+-      return;
+-    }
+-
+-    // Dangerous signals. Exit directly after shared memory destruction.
+-    // Don't touch sockets => avoid dead locks.
+-    for (int crashSig : { SIGABRT, SIGBUS, SIGSEGV })
+-      if (signum == crashSig) {
+-        for (SingleApplicationPrivate *d : sharedMem)
+-          delete d->memory;
+-        goto forceExit;
+-      }
+-
+-    while (!sharedMem.empty()) {
+-      delete sharedMem.back();
+-      sharedMem.pop_back();
+-    }
+-
+-  forceExit:
+-    ::exit(128 + signum);
++    Q_UNUSED(signum);
++    SingleApplication::instance()->quit();
+   }
+-
+-  QList<SingleApplicationPrivate *> SingleApplicationPrivate::sharedMem;
+-  QMutex SingleApplicationPrivate::sharedMemMutex;
+ #endif // ifdef Q_OS_UNIX
+ 
+ /**
+diff --git a/src/app/single-application/SingleApplicationPrivate.hpp b/src/app/single-application/SingleApplicationPrivate.hpp
+index d0dfc10e..db2d8cf5 100644
+--- a/src/app/single-application/SingleApplicationPrivate.hpp
++++ b/src/app/single-application/SingleApplicationPrivate.hpp
+@@ -59,10 +59,7 @@ public:
+   void connectToPrimary (int msecs, char connectionType);
+ 
+   #ifdef Q_OS_UNIX
+-    void crashHandler ();
+     static void terminate (int signum);
+-    static QList<SingleApplicationPrivate *> sharedMem;
+-    static QMutex sharedMemMutex;
+   #endif // ifdef Q_OS_UNIX
+ 
+   QSharedMemory *memory;
+-- 
+2.17.1
+
-- 
2.26.0

