Ticket #1984: sound.3.diff

File sound.3.diff, 10.1 kB (added by ressu@…, 2 years ago)

Updated version of the patch, created from r6355

  • src/config.py

     
    3939from common import gajim 
    4040from common import connection 
    4141from common import i18n 
     42from common import sound 
    4243 
    4344_ = i18n._ 
    4445APP = i18n.APP 
     
    977978                if not iter: 
    978979                        return 
    979980                snd_event_config_name = model[iter][3] 
    980                 helpers.play_sound(snd_event_config_name) 
     981                sound.play_event(snd_event_config_name) 
    981982 
    982983        def on_open_advanced_editor_button_clicked(self, widget, data = None): 
    983984                if gajim.interface.instances.has_key('advanced_config'): 
  • src/notify.py

     
    2525from common import gajim 
    2626from common import i18n 
    2727from common import helpers 
     28from common import sound 
    2829i18n.init() 
    2930_ = i18n._ 
    3031 
     
    159160        if (do_sound): 
    160161                if (event == 'new_message'): 
    161162                        if first: 
    162                                  helpers.play_sound('first_message_received') 
     163                                 sound.play_event('first_message_received') 
    163164                        else: 
    164                                  helpers.play_sound('next_message_received') 
     165                                 sound.play_event('next_message_received') 
    165166                elif (event == 'contact_connected' or event == 'contact_disconnected'): 
    166                         helpers.play_sound(event)        
     167                        sound.play_event(event)  
    167168          
    168169 
    169170def popup(event_type, jid, account, msg_type = '', path_to_image = None, 
  • src/gajim.py

     
    3939 
    4040from common import exceptions 
    4141from common import i18n 
     42from common import sound 
    4243i18n.init() 
    4344_ = i18n._ 
    4445 
     
    578579                msg = array[1] 
    579580                # do not play sound when standalone chatstate message (eg no msg) 
    580581                if msg and gajim.config.get_per('soundevents', 'message_sent', 'enabled'): 
    581                         helpers.play_sound('message_sent') 
     582                        sound.play_event('message_sent') 
    582583                 
    583584        def handle_event_subscribe(self, account, array): 
    584585                #('SUBSCRIBE', account, (jid, text)) 
  • src/groupchat_control.py

     
    3636 
    3737from common import gajim 
    3838from common import helpers 
     39from common import sound 
    3940 
    4041from chat_control import ChatControl 
    4142from chat_control import ChatControlBase 
     
    535536 
    536537                if kind == 'incoming': # it's a message NOT from us 
    537538                        # highlighting and sounds 
    538                         (highlight, sound) = self.highlighting_for_message(text, tim) 
     539                        (highlight, sound_event) = self.highlighting_for_message(text, tim) 
    539540                        if highlight: 
    540541                                # muc-specific chatstate 
    541542                                self.parent_win.redraw_tab(self, 'attention') 
    542543                                other_tags_for_name.append('bold') 
    543544                                other_tags_for_text.append('marked') 
    544                         if sound == 'received': 
    545                                 helpers.play_sound('muc_message_received') 
    546                         elif sound == 'highlight': 
    547                                 helpers.play_sound('muc_message_highlight') 
     545                        if sound_event == 'received': 
     546                                sound.play_event('muc_message_received') 
     547                        elif sound_event == 'highlight': 
     548                                sound.play_event('muc_message_highlight') 
    548549 
    549550                        self.check_and_possibly_add_focus_out_line() 
    550551 
     
    554555        def highlighting_for_message(self, text, tim): 
    555556                '''Returns a 2-Tuple. The first says whether or not to highlight the 
    556557                text, the second, what sound to play.''' 
    557                 highlight, sound = (None, None) 
     558                highlight, sound_event = (None, None) 
    558559 
    559560                # Do we play a sound on every muc message? 
    560561                if gajim.config.get_per('soundevents', 'muc_message_received', 'enabled'): 
    561562                        if gajim.config.get('notify_on_all_muc_messages'): 
    562                                 sound = 'received' 
     563                                sound_event = 'received' 
    563564 
    564565                # Are any of the defined highlighting words in the text? 
    565566                if self.needs_visual_notification(text): 
    566567                        highlight = True 
    567568                        if gajim.config.get_per('soundevents', 'muc_message_highlight', 
    568569                                                                        'enabled'): 
    569                                 sound = 'highlight' 
     570                                sound_event = 'highlight' 
    570571 
    571572                # Is it a history message? Don't want sound-floods when we join. 
    572573                if tim != time.localtime(): 
    573                         sound = None 
     574                        sound_event = None 
    574575 
    575                 return (highlight, sound) 
     576                return (highlight, sound_event) 
    576577 
    577578        def check_and_possibly_add_focus_out_line(self): 
    578579                '''checks and possibly adds focus out line for room_jid if it needs it 
  • src/common/sound.py

     
     1##      common/gst.py 
     2## 
     3## Contributors for this file: 
     4##      - Sami Haahtinen <ressu@ressukka.net> 
     5## 
     6## Copyright (C) 2006 Sami Haahtinen <ressu@ressukka.net> 
     7## 
     8## This program is free software; you can redistribute it and/or modify 
     9## it under the terms of the GNU General Public License as published 
     10## by the Free Software Foundation; version 2 only. 
     11## 
     12## This program is distributed in the hope that it will be useful, 
     13## but WITHOUT ANY WARRANTY; without even the implied warranty of 
     14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     15## GNU General Public License for more details. 
     16 
     17import gajim 
     18import os 
     19 
     20class GajimPlayer: 
     21  player = None 
     22 
     23  def __init__(self): 
     24    # For now, lets default to GStreamer, winsound, and then try External player 
     25    # FIXME: This really needs elegance! 
     26    if not gajim.config.get('sounds_on'): 
     27      raise NoSound 
     28       
     29    try: 
     30      self.player = GajimGstPlayer() 
     31    except self.PlayerUnavailable: 
     32      try: 
     33        self.player = GajimWin32Player() 
     34      except self.PlayerUnavailable: 
     35        try: 
     36          self.player = GajimExtPlayer() 
     37        except self.PlayerUnavailable: 
     38          self.player = None # Make sure that player is still set to None 
     39          raise NoSound 
     40 
     41  class NoSound(Exception): 
     42    '''Notifies the system that we should be silent''' 
     43 
     44  class BeepOnly(Exception): 
     45    '''Notifies the system that we should just beep''' 
     46   
     47  class PlayerUnavailable(Exception): 
     48    '''Notifies that the player is unavailable''' 
     49 
     50  def event_to_soundfile(self, event): 
     51    '''Returns the full file path for sound file of an event''' 
     52    # Check if we should play sound at all 
     53    if self.player is None: 
     54      raise NoSound 
     55 
     56    # Get soundevent 
     57    path_to_soundfile = gajim.config.get_per('soundevents', event, 'path') 
     58     
     59    if path_to_soundfile is None or not os.path.exists(path_to_soundfile): 
     60      raise NoSound 
     61 
     62    return path_to_soundfile 
     63 
     64  def play(self, soundfile): 
     65    if soundfile == 'beep': 
     66      beep() 
     67     
     68    if self.player is None: 
     69      return 
     70     
     71    self.player.play_sound(soundfile) 
     72 
     73  def beep(self): 
     74    '''Play a single beep''' 
     75    # FIXME: There must be a better way 
     76    print "\a" 
     77 
     78# FIXME: This class hasn't gotten any testing! 
     79class GajimWin32Player: 
     80  '''User native Win32 layer to play sounds''' 
     81 
     82  def __init__(self): 
     83    try: 
     84      import winsound 
     85    except ImportError: 
     86      # gst module is not available... 
     87      raise GajimPlayer.PlayerUnavailable, "Winsound library not available" 
     88   
     89  def play_sound(self,soundfile): 
     90    try: 
     91      winsound.PlaySound(soundfile, winsound.SND_FILENAME|winsound.SND_ASYNC) 
     92    except: 
     93      pass 
     94 
     95class GajimExtPlayer: 
     96  '''Use external application to play the sounds''' 
     97 
     98  def __init__(self): 
     99    self.player = gajim.config.get('soundplayer') 
     100    if self.player == '': 
     101      raise GajimPlayer.PlayerUnavailable, "External player not set" 
     102    # FIXME: We should check if the player exists! 
     103 
     104  def play_sound(self,soundfile): 
     105    soundfile = soundfile.replace('"', '\\"') 
     106    command = self.player + ' "' + soundfile + '" &' 
     107    os.system(command) 
     108 
     109class GajimGstPlayer: 
     110  '''Player that plays sound events through the GStreamer framework''' 
     111 
     112  def __init__(self): 
     113    try: 
     114      import gst 
     115    except ImportError: 
     116      # gst module is not available... 
     117      raise GajimPlayer.PlayerUnavailable, "Python Gstreamer modules not available" 
     118    self.gst = gst 
     119    self.GstBin = self.gst.parse_launch("playbin") 
     120 
     121    # Create a sink for video, if someone decides to use a video file ;) 
     122    fakesink = self.gst.element_factory_make("fakesink") 
     123    self.GstBin.set_property("video-sink", fakesink) 
     124 
     125  def play_sound(self,soundfile): 
     126    self.GstBin.set_state(self.gst.STATE_NULL) 
     127 
     128    # since playbin takes input in URI format, add file:// prefix 
     129    sounduri = "file://" + os.path.abspath(soundfile) 
     130    self.GstBin.set_property('uri', sounduri) 
     131     
     132    self.GstBin.set_state(self.gst.STATE_PLAYING) 
     133 
     134def play_event(event): 
     135  global Player 
     136  try: 
     137    try: 
     138       soundfile = Player.event_to_soundfile(event) 
     139    except NameError: 
     140       Player = GajimPlayer() 
     141       soundfile = Player.event_to_soundfile(event) 
     142  except GajimPlayer.NoSound: 
     143    # No sound. 
     144    return 
     145 
     146  # Ok, we should be clear to play the sound 
     147  Player.play(soundfile) 
  • src/common/helpers.py

     
    3030from xmpp_stringprep import nodeprep, resourceprep, nameprep 
    3131 
    3232try: 
    33         import winsound # windows-only built-in module for playing wav 
    3433        import win32api 
    3534        import win32con 
    3635except: 
     
    417416                except: 
    418417                        pass 
    419418 
    420 def play_sound(event): 
    421         if not gajim.config.get('sounds_on'): 
    422                 return 
    423         path_to_soundfile = gajim.config.get_per('soundevents', event, 'path') 
    424         if path_to_soundfile == 'beep': 
    425                 print '\a' # make a speaker beep 
    426                 return 
    427         if path_to_soundfile is None or not os.path.exists(path_to_soundfile): 
    428                 return 
    429         if os.name == 'nt': 
    430                 try: 
    431                         winsound.PlaySound(path_to_soundfile, 
    432                                 winsound.SND_FILENAME|winsound.SND_ASYNC) 
    433                 except: 
    434                         pass 
    435         elif os.name == 'posix': 
    436                 if gajim.config.get('soundplayer') == '': 
    437                         return 
    438                 player = gajim.config.get('soundplayer') 
    439                 # we add the path in "" so we have good parsing from shell 
    440                 path_to_soundfile = path_to_soundfile.replace('"', '\\"') # escape " 
    441                 command = player + ' "' + path_to_soundfile + '" &' 
    442                 #FIXME: when we require 2.4+ use subprocess module 
    443                 os.system(command) 
    444  
    445419def get_file_path_from_dnd_dropped_uri(uri): 
    446420        path = urllib.url2pathname(uri) # escape special chars 
    447421        path = path.strip('\r\n\x00') # remove \r\n and NULL