Skip to content

Commit

Permalink
Add graphical notifications using notify-send
Browse files Browse the repository at this point in the history
  • Loading branch information
tdryer committed Jul 13, 2014
1 parent 5ee394c commit 4b68320
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hangups/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import urwid

import hangups
from hangups.notify import Notifier


LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
Expand All @@ -36,6 +37,7 @@ def __init__(self):
self._tabbed_window = None # TabbedWindowWidget
self._conv_list = None # hangups.ConversationList
self._user_list = None # hangups.UserList
self._notifier = None # hangups.notify.Notifier

# TODO Add urwid widget for getting auth.
try:
Expand Down Expand Up @@ -102,6 +104,7 @@ def _on_connect(self, client):
"""Handle connecting for the first time."""
self._conv_list = hangups.ConversationList(self._client)
self._user_list = hangups.UserList(self._client)
self._notifier = Notifier(self._client, self._conv_list)
# show the conversation menu
self._tabbed_window = TabbedWindowWidget([
ConversationPickerWidget(
Expand Down
40 changes: 40 additions & 0 deletions hangups/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Graphical notifications for the hangups UI.
TODO:
- Support different notification systems either by allowing NOTIFY_CMD to
be customized, or by implementing Notifier subclasses.
- Replace notify-send with something that supports merging notifications.
- Create notifications for other events like (dis)connection
"""

import logging
import subprocess

logger = logging.getLogger(__name__)
NOTIFY_CMD = ['notify-send', '{sender_name}', '{msg_text}']


class Notifier(object):

"""Receives events from hangups and creates system notifications."""

def __init__(self, client, conv_list):
self._conv_list = conv_list # hangups.ConversationList
self._client = client # hangups.Client
self._client.on_message += self._on_message

def _on_message(self, client, conv_id, user_id, timestamp, text):
"""Create notification for new messages."""
conv = self._conv_list.get(conv_id)
user = conv.get_user(user_id)
# Ignore messages sent by yourself.
if not user.is_self:
cmd = [arg.format(
sender_name=user.full_name,
msg_text=text,
) for arg in NOTIFY_CMD]
logger.info('Creating notification with command: {}'.format(cmd))
# Run the command without blocking, and without a shell. Since the
# command is a list of arguments, and we're not using a shell, this
# should be safe.
subprocess.Popen(cmd)

0 comments on commit 4b68320

Please sign in to comment.