From 298fb153066816eeb8215a17fb628f12704d76ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <philipp@hoerist.com>
Date: Sat, 10 Nov 2018 18:43:54 +0100
Subject: [PATCH] Better determine soundplayer on unix systems

- sys.platform == 'linux', excludes many other unix systems
- use shutil.which() to test if a command is available
---
 gajim/common/helpers.py | 37 +++++++++++++++++++++----------------
 gajim/gui_interface.py  | 14 +-------------
 2 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py
index 0bc10946aa..15b0d053d5 100644
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -40,6 +40,7 @@ import socket
 import time
 import logging
 import json
+import shutil
 from datetime import datetime, timedelta
 from distutils.version import LooseVersion as V
 from encodings.punycode import punycode_encode
@@ -444,22 +445,26 @@ def get_uf_chatstate(chatstate):
         return _('has closed the chat window or tab')
     return ''
 
-def is_in_path(command, return_abs_path=False):
-    """
-    Return True if 'command' is found in one of the directories in the user's
-    path. If 'return_abs_path' is True, return the absolute path of the first
-    found command instead. Return False otherwise and on errors
-    """
-    for directory in os.getenv('PATH').split(os.pathsep):
-        try:
-            if command in os.listdir(directory):
-                if return_abs_path:
-                    return os.path.join(directory, command)
-                return True
-        except OSError:
-            # If the user has non directories in his path
-            pass
-    return False
+def find_soundplayer():
+    if sys.platform in ('win32', 'darwin'):
+        return
+
+    if app.config.get('soundplayer') != '':
+        return
+
+    commands = ('aucat', 'paplay', 'aplay', 'play', 'ossplay')
+    for command in commands:
+        if shutil.which(command) is not None:
+            if command == 'paplay':
+                command += ' -n gajim --property=media.role=event'
+            elif command in ('aplay', 'play'):
+                command += ' -q'
+            elif command == 'ossplay':
+                command += ' -qq'
+            elif command == 'aucat':
+                command += ' -i'
+            app.config.set('soundplayer', command)
+            break
 
 def exec_command(command, use_shell=False, posix=True):
     """
diff --git a/gajim/gui_interface.py b/gajim/gui_interface.py
index 0f12ae9ee9..4ba3299467 100644
--- a/gajim/gui_interface.py
+++ b/gajim/gui_interface.py
@@ -2756,19 +2756,7 @@ class Interface:
         # get transports type from DB
         app.transport_type = app.logger.get_transports_type()
 
-        if app.config.get('soundplayer') == '':
-            # only on first time Gajim starts
-            commands = ('paplay', 'aplay', 'play', 'ossplay')
-            for command in commands:
-                if helpers.is_in_path(command):
-                    if command == 'paplay':
-                        command += ' -n gajim --property=media.role=event'
-                    if command in ('aplay', 'play'):
-                        command += ' -q'
-                    elif command == 'ossplay':
-                        command += ' -qq'
-                    app.config.set('soundplayer', command)
-                    break
+        helpers.find_soundplayer()
 
         self.last_ftwindow_update = 0
 
-- 
GitLab