From 52062134457c09118878031b90c4dacf323ba029 Mon Sep 17 00:00:00 2001 From: Ignacio Rodríguez Date: Fri, 22 Nov 2013 19:41:33 +0000 Subject: Use sound for events. --- diff --git a/activity.py b/activity.py index d746d35..de76eb8 100644 --- a/activity.py +++ b/activity.py @@ -16,9 +16,17 @@ from gi.repository import Gtk from gi.repository import Gdk + +try: + from gi.repository import Gst + HAS_SOUND = True +except: + HAS_SOUND = False + import logging import json import math +import os from gettext import gettext as _ from telepathy.interfaces import CHANNEL_INTERFACE @@ -35,6 +43,7 @@ from sugar3.graphics.alert import NotifyAlert from sugar3.graphics.palette import Palette from sugar3.graphics.toolbarbox import ToolbarBox from sugar3.activity import activity +from sugar3.activity.activity import get_bundle_path from sugar3.presence import presenceservice from sugar3.activity.widgets import ActivityButton from sugar3.activity.widgets import TitleEntry @@ -50,6 +59,8 @@ logger = logging.getLogger('chat-activity') SMILIES_COLUMNS = 5 +if HAS_SOUND: + Gst.init([]) # pylint: disable-msg=W0223 class Chat(activity.Activity): @@ -111,6 +122,9 @@ class Chat(activity.Activity): self._chat_is_room = False self.text_channel = None + if HAS_SOUND: + self.element = Gst.ElementFactory.make("playbin", "Player") + if self.shared_activity: # we are joining the activity self.connect('joined', self._joined_cb) @@ -222,6 +236,11 @@ class Chat(activity.Activity): logger.debug('Received message from %s: %s', nick, text) self.chatbox.add_text(buddy, text) + if self.owner.props.nick in text: + self.play_sound("said_nick") + else: + self.play_sound("received") + def _alert(self, title, text=None): alert = NotifyAlert(timeout=5) alert.props.title = title @@ -241,6 +260,8 @@ class Chat(activity.Activity): buddy.props.nick + ' ' + _('joined the chat'), status_message=True) + self.play_sound("login") + def _buddy_left_cb(self, sender, buddy): """Show a buddy who joined""" if buddy == self.owner: @@ -249,6 +270,8 @@ class Chat(activity.Activity): buddy.props.nick + ' ' + _('left the chat'), status_message=True) + self.play_sound("logout") + def _buddy_already_exists(self, buddy): """Show a buddy already in the chat.""" if buddy == self.owner: @@ -318,6 +341,7 @@ class Chat(activity.Activity): entry.props.text = '' if self.text_channel: self.text_channel.send(text) + self.play_sound("send") else: logger.debug('Tried to send message but text channel ' 'not connected.') @@ -358,6 +382,22 @@ class Chat(activity.Activity): text, status_message) last_line_was_timestamp = False + def play_sound(self, event): + if not HAS_SOUND: + return + SOUNDS_PATH = os.path.join(get_bundle_path(), 'sounds') + SOUNDS = {"said_nick": os.path.join(SOUNDS_PATH, 'alert.wav'), + "login": os.path.join(SOUNDS_PATH, 'login.wav'), + "logout": os.path.join(SOUNDS_PATH, 'logout.wav'), + "received": os.path.join(SOUNDS_PATH, 'receive.wav'), + "send": os.path.join(SOUNDS_PATH, 'send.wav')} + + self.element.set_state(Gst.State.NULL) + self.element.set_property("uri", "file://%s" % SOUNDS[event]) + self.element.set_state(Gst.State.PLAYING) + + return True + class TextChannelWrapper(object): """Wrap a telepathy Text Channfel to make usage simpler.""" -- cgit v0.9.1