Better determine soundplayer on unix systems

- sys.platform == 'linux', excludes many other unix systems
- use shutil.which() to test if a command is available
This commit is contained in:
Philipp Hörist 2018-11-10 18:43:54 +01:00
parent 213da73b44
commit 298fb15306
2 changed files with 22 additions and 29 deletions

View File

@ -40,6 +40,7 @@ import socket
import time import time
import logging import logging
import json import json
import shutil
from datetime import datetime, timedelta from datetime import datetime, timedelta
from distutils.version import LooseVersion as V from distutils.version import LooseVersion as V
from encodings.punycode import punycode_encode from encodings.punycode import punycode_encode
@ -444,22 +445,26 @@ def get_uf_chatstate(chatstate):
return _('has closed the chat window or tab') return _('has closed the chat window or tab')
return '' return ''
def is_in_path(command, return_abs_path=False): def find_soundplayer():
""" if sys.platform in ('win32', 'darwin'):
Return True if 'command' is found in one of the directories in the user's return
path. If 'return_abs_path' is True, return the absolute path of the first
found command instead. Return False otherwise and on errors if app.config.get('soundplayer') != '':
""" return
for directory in os.getenv('PATH').split(os.pathsep):
try: commands = ('aucat', 'paplay', 'aplay', 'play', 'ossplay')
if command in os.listdir(directory): for command in commands:
if return_abs_path: if shutil.which(command) is not None:
return os.path.join(directory, command) if command == 'paplay':
return True command += ' -n gajim --property=media.role=event'
except OSError: elif command in ('aplay', 'play'):
# If the user has non directories in his path command += ' -q'
pass elif command == 'ossplay':
return False command += ' -qq'
elif command == 'aucat':
command += ' -i'
app.config.set('soundplayer', command)
break
def exec_command(command, use_shell=False, posix=True): def exec_command(command, use_shell=False, posix=True):
""" """

View File

@ -2756,19 +2756,7 @@ class Interface:
# get transports type from DB # get transports type from DB
app.transport_type = app.logger.get_transports_type() app.transport_type = app.logger.get_transports_type()
if app.config.get('soundplayer') == '': helpers.find_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
self.last_ftwindow_update = 0 self.last_ftwindow_update = 0