Philipp Hörist pushed to branch master at gajim / gajim
Commits: 8d95eb9b by Philipp Hörist at 2018-11-10T18:30:12Z Better determine soundplayer on unix systems
- sys.platform == 'linux', excludes many other unix systems - use shutil.which() to test if a command is available
- - - - - 1f31ab04 by Philipp Hörist at 2018-11-10T18:30:13Z Remove unused code
Code is also not cross-platform compatible
- - - - - 0d703524 by Philipp Hörist at 2018-11-10T18:30:13Z Dont test if winsound is available
Its a standard library which will be always available in our builds
- - - - - 5152611a by Philipp Hörist at 2018-11-10T18:30:18Z Try to play sound on all unix systems
sys.platform == 'linux' excludeds other unix systems, for example FreeBSD
- - - - - a7cc815c by Philipp Hörist at 2018-11-10T18:30:18Z Move imports into function
- - - - - 02a9615b by Philipp Hörist at 2018-11-10T18:30:19Z Dont test if precis_i18n is available
Its listed as dependency
- - - - - e4a9ffe1 by Philipp Hörist at 2018-11-10T18:30:19Z Dont test for sys.platform == 'linux'
This excludes other unix systems which was not intended
- - - - -
5 changed files:
- gajim/common/dbus_support.py - gajim/common/helpers.py - gajim/gui_interface.py - gajim/notify.py - gajim/roster_window.py
Changes:
===================================== gajim/common/dbus_support.py ===================================== @@ -36,7 +36,7 @@ try: DBusGMainLoop(set_as_default=True) except ImportError: supported = False - if sys.platform == 'linux': # windows and mac have no dbus + if sys.platform not in ('win32', 'darwin'): print(_('D-Bus python bindings are missing in this computer')) print(_('D-Bus capabilities of Gajim cannot be used')) else: @@ -47,7 +47,7 @@ else: supported = True # does user have D-Bus bindings? except dbus.DBusException: supported = False - if sys.platform == 'linux': # windows and mac have no dbus + if sys.platform not in ('win32', 'darwin'): print(_('D-Bus does not run correctly on this machine')) print(_('D-Bus capabilities of Gajim cannot be used')) except exceptions.SystemBusNotPresent:
===================================== gajim/common/helpers.py ===================================== @@ -40,12 +40,15 @@ 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 from string import Template
import nbxmpp +from nbxmpp.stringprepare import nameprep +import precis_i18n.codec # pylint: disable=unused-import
from gajim.common import caps_cache from gajim.common import configpaths @@ -54,35 +57,6 @@ from gajim.common.i18n import _ from gajim.common.i18n import ngettext from gajim.common.caps_cache import muc_caps_cache
-try: - import precis_i18n.codec # pylint: disable=unused-import - HAS_PRECIS_I18N = True -except ImportError: - HAS_PRECIS_I18N = False - -HAS_SOUND = True -if sys.platform == 'win32': - try: - import winsound # windows-only built-in module for playing wav - except ImportError: - HAS_SOUND = False - print('Gajim is not able to playback sound because' - 'pywin32 is missing', file=sys.stderr) - -elif sys.platform == 'darwin': - try: - from AppKit import NSSound - except ImportError: - HAS_SOUND = False - print('Gajim is not able to playback sound because' - 'pyobjc is missing', file=sys.stderr) - -try: - import wave # posix-only fallback wav playback - import ossaudiodev as oss -except Exception: - pass - log = logging.getLogger('gajim.c.helpers')
special_groups = (_('Transports'), _('Not in Roster'), _('Observers'), _('Groupchats')) @@ -180,10 +154,7 @@ def parse_resource(resource): """ if resource: try: - if HAS_PRECIS_I18N: - return resource.encode('Nickname').decode('utf-8') - from nbxmpp.stringprepare import resourceprep - return resourceprep.prepare(resource) + return resource.encode('Nickname').decode('utf-8') except UnicodeError: raise InvalidFormat('Invalid character in resource.')
@@ -217,7 +188,6 @@ def prep(user, server, resource): if not server or len(server.encode('utf-8')) > 1023: raise InvalidFormat(_('Server must be between 1 and 1023 bytes')) try: - from nbxmpp.stringprepare import nameprep server = nameprep.prepare(server) except UnicodeError: raise InvalidFormat(_('Invalid character in hostname.')) @@ -228,11 +198,7 @@ def prep(user, server, resource): if not user or len(user.encode('utf-8')) > 1023: raise InvalidFormat(_('Username must be between 1 and 1023 bytes')) try: - if HAS_PRECIS_I18N: - user = user.encode('UsernameCaseMapped').decode('utf-8') - else: - from nbxmpp.stringprepare import nodeprep - user = nodeprep.prepare(user) + user = user.encode('UsernameCaseMapped').decode('utf-8') except UnicodeError: raise InvalidFormat(_('Invalid character in username.')) else: @@ -242,11 +208,7 @@ def prep(user, server, resource): if not resource or len(resource.encode('utf-8')) > 1023: raise InvalidFormat(_('Resource must be between 1 and 1023 bytes')) try: - if HAS_PRECIS_I18N: - resource = resource.encode('OpaqueString').decode('utf-8') - else: - from nbxmpp.stringprepare import resourceprep - resource = resourceprep.prepare(resource) + resource = resource.encode('OpaqueString').decode('utf-8') except UnicodeError: raise InvalidFormat(_('Invalid character in resource.')) else: @@ -444,22 +406,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): """ @@ -758,38 +724,52 @@ def strip_soundfile_path(file_, dirs=None, abs_=True): return file_
def play_sound_file(path_to_soundfile): - if path_to_soundfile == 'beep': - exec_command('beep') - return path_to_soundfile = check_soundfile_path(path_to_soundfile) if path_to_soundfile is None: return - if sys.platform == 'win32' and HAS_SOUND: + + if sys.platform == 'win32': + import winsound try: winsound.PlaySound(path_to_soundfile, - winsound.SND_FILENAME|winsound.SND_ASYNC) + winsound.SND_FILENAME|winsound.SND_ASYNC) except Exception: log.exception('Sound Playback Error') - elif sys.platform == 'linux': - if app.config.get('soundplayer') == '': - def _oss_play(): - sndfile = wave.open(path_to_soundfile, 'rb') - nc, sw, fr, nf, _comptype, _compname = sndfile.getparams() - dev = oss.open('/dev/dsp', 'w') - dev.setparameters(sw * 8, nc, fr) - dev.write(sndfile.readframes(nf)) - sndfile.close() - dev.close() - app.thread_interface(_oss_play) + + elif sys.platform == 'darwin': + try: + from AppKit import NSSound + except ImportError: + log.exception('Sound Playback Error') return - player = app.config.get('soundplayer') - command = build_command(player, path_to_soundfile) - exec_command(command) - elif sys.platform == 'darwin' and HAS_SOUND: + sound = NSSound.alloc() sound.initWithContentsOfFile_byReference_(path_to_soundfile, True) sound.play()
+ elif app.config.get('soundplayer') == '': + try: + import wave + import ossaudiodev + except Exception: + log.exception('Sound Playback Error') + return + + def _oss_play(): + sndfile = wave.open(path_to_soundfile, 'rb') + nc, sw, fr, nf, _comptype, _compname = sndfile.getparams() + dev = ossaudiodev.open('/dev/dsp', 'w') + dev.setparameters(sw * 8, nc, fr) + dev.write(sndfile.readframes(nf)) + sndfile.close() + dev.close() + app.thread_interface(_oss_play) + + else: + player = app.config.get('soundplayer') + command = build_command(player, path_to_soundfile) + exec_command(command) + def get_global_show(): maxi = 0 for account in app.connections:
===================================== gajim/gui_interface.py ===================================== @@ -1106,7 +1106,7 @@ class Interface: if connected == invisible_show: return # send currently played music - if (pep_supported and sys.platform == 'linux' and + if (pep_supported and sys.platform not in ('win32', 'darwin') and app.config.get_per('accounts', account, 'publish_tune')): self.enable_music_listener() # enable location listener @@ -2683,7 +2683,7 @@ class Interface: self.remote_ctrl = None
# Handle screensaver - if sys.platform == 'linux': + if sys.platform not in ('win32', 'darwin'): logind.enable() screensaver.enable()
@@ -2707,19 +2707,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
===================================== gajim/notify.py ===================================== @@ -82,7 +82,7 @@ class Notification: self.daemon_capabilities = ['actions']
# Detect if actions are supported by the notification daemon - if sys.platform == 'linux': + if sys.platform not in ('win32', 'darwin'): def on_proxy_ready(source, res, data=None): try: proxy = Gio.DBusProxy.new_finish(res)
===================================== gajim/roster_window.py ===================================== @@ -4962,7 +4962,7 @@ class RosterWindow:
item = Gtk.CheckMenuItem(_('Publish Tune')) pep_submenu.append(item) - if sys.platform != 'linux': + if sys.platform in ('win32', 'darwin'): item.set_sensitive(False) else: activ = app.config.get_per('accounts', account,
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/ac2a75db489d0812a1dbf0dffd4b3359bd...