Skip to content

Commit

Permalink
Add discreet notifications option
Browse files Browse the repository at this point in the history
  • Loading branch information
StuntsPT authored and tdryer committed Mar 1, 2016
1 parent d09cdef commit 07cd1f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
3 changes: 3 additions & 0 deletions hangups.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ key-quit: ctrl e
key-menu: ctrl n
key-up: k
key-down: j

[Notifications]
notifications-type: full
30 changes: 20 additions & 10 deletions hangups/ui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class ChatUI(object):
"""User interface for hangups."""

def __init__(self, refresh_token_path, keybindings, palette,
palette_colors, datetimefmt, disable_notifier):
palette_colors, datetimefmt, notifier):
"""Start the user interface."""
self._keys = keybindings
self._datetimefmt = datetimefmt
self._notifier = notifier

set_terminal_title('hangups')

Expand All @@ -60,8 +61,6 @@ def __init__(self, refresh_token_path, keybindings, palette,
self._tabbed_window = None # TabbedWindowWidget
self._conv_list = None # hangups.ConversationList
self._user_list = None # hangups.UserList
self._notifier = None # hangups.notify.Notifier
self._disable_notifier = disable_notifier

# TODO Add urwid widget for getting auth.
try:
Expand Down Expand Up @@ -153,8 +152,7 @@ def _on_connect(self):
yield from hangups.build_user_conversation_list(self._client)
)
self._conv_list.on_event.add_observer(self._on_event)
if not self._disable_notifier:
self._notifier = Notifier(self._conv_list)

# show the conversation menu
conv_picker = ConversationPickerWidget(self._conv_list,
self.on_select_conversation,
Expand All @@ -165,7 +163,7 @@ def _on_connect(self):
self._urwid_loop.widget = self._tabbed_window

def _on_event(self, conv_event):
"""Open conversation tab for new messages when they arrive."""
"""Open conversation tab for new messages & pass events to notifier."""
conv = self._conv_list.get(conv_event.conversation_id)
user = conv.get_user(conv_event.user_id)
add_tab = all((
Expand All @@ -175,6 +173,9 @@ def _on_event(self, conv_event):
))
if add_tab:
self.add_conversation_tab(conv_event.conversation_id)
# Handle notifications
if self._notifier is not None:
self._notifier.on_event(conv, conv_event)

def _on_quit(self):
"""Handle the user quitting the application."""
Expand Down Expand Up @@ -885,8 +886,6 @@ def main():
version='hangups {}'.format(hangups.__version__))
general_group.add('-d', '--debug', action='store_true',
help='log detailed debugging messages')
general_group.add('-n', '--disable-notifications', action='store_true',
help='disable desktop notifications')
general_group.add('--log', default=default_log_path, help='log file path')
key_group = parser.add_argument_group('Keybindings')
key_group.add('--key-next-tab', default='ctrl d',
Expand All @@ -903,6 +902,13 @@ def main():
help='keybinding for alternate up key')
key_group.add('--key-down', default='j',
help='keybinding for alternate down key')
notification_group = parser.add_argument_group('Notifications')
notification_group.add('-n', '--disable-notifications',
action='store_true',
help='disable desktop notifications')
notification_group.add('-D', '--discreet-notification',
action='store_true',
help='Always display the same notification message')

# add color scheme options
col_group = parser.add_argument_group('Colors')
Expand Down Expand Up @@ -941,6 +947,11 @@ def main():
getattr(args, 'col_' + name + '_bg'),
palette_colors)

if not args.disable_notifications:
notifier = Notifier(args.discreet_notification)
else:
notifier = None

try:
ChatUI(
args.token_path, {
Expand All @@ -951,8 +962,7 @@ def main():
'menu': args.key_menu,
'up': args.key_up,
'down': args.key_down
}, col_scheme, palette_colors,
datetimefmt, args.disable_notifications
}, col_scheme, palette_colors, datetimefmt, notifier
)
except KeyboardInterrupt:
sys.exit('Caught KeyboardInterrupt, exiting abnormally')
Expand Down
24 changes: 15 additions & 9 deletions hangups/ui/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
'subtitle "{sender_name}"'),
]

def NOTIFY_ESCAPER(s):
return s.replace('"', '\\"')
def NOTIFY_ESCAPER(text):
return text.replace('"', '\\"')
else:
NOTIFY_CMD = [
'gdbus', 'call', '--session', '--dest',
Expand Down Expand Up @@ -57,14 +57,13 @@ class Notifier(object):
previous notification is instantly replaced.
"""

def __init__(self, conv_list):
self._conv_list = conv_list # hangups.ConversationList
self._conv_list.on_event.add_observer(self._on_event)
def __init__(self, discreet_notification):
self._replaces_id = 0
self._discreet_notification = discreet_notification


def _on_event(self, conv_event):
def on_event(self, conv, conv_event):
"""Create notification for new messages."""
conv = self._conv_list.get(conv_event.conversation_id)
user = conv.get_user(conv_event.user_id)
# Ignore non-messages or messages sent by yourself.
show_notification = all((
Expand All @@ -75,9 +74,16 @@ def _on_event(self, conv_event):
if show_notification:
# We have to escape angle brackets because freedesktop.org
# notifications support markup.
if self._discreet_notification:
user = NOTIFY_ESCAPER("Hangups")
message = NOTIFY_ESCAPER("New message")
else:
user = NOTIFY_ESCAPER(user.full_name)
message = NOTIFY_ESCAPER(conv_event.text)

cmd = [arg.format(
sender_name=NOTIFY_ESCAPER(user.full_name),
msg_text=NOTIFY_ESCAPER(conv_event.text),
sender_name=user,
msg_text=message,
replaces_id=self._replaces_id,
convo_name=NOTIFY_ESCAPER(get_conv_name(conv)),
) for arg in NOTIFY_CMD]
Expand Down

0 comments on commit 07cd1f7

Please sign in to comment.