Skip to content

Commit

Permalink
Fixed and3rson#37: Prevent crash setting up dbus without a notificati…
Browse files Browse the repository at this point in the history
…on target
  • Loading branch information
agg23 committed Sep 20, 2018
1 parent 6088849 commit 594e527
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions clay/core/osd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pydbus import SessionBus, Variant
from clay.core import meta, logger

NOTIFICATION_BUS_NAME = ".Notifications"
BASE_NAME = "org.freedesktop"

class _OSDManager(object):
"""
Expand All @@ -13,8 +15,11 @@ class _OSDManager(object):
def __init__(self):
self._last_id = 0
self.bus = SessionBus()
self.notifications = self.bus.get(".Notifications")
self.notifications.onActionInvoked = self._on_action

self.bus.watch_name(BASE_NAME + NOTIFICATION_BUS_NAME, name_appeared=self._registerBusName,
name_vanished=self._deregisterBusName)
self._registerBusName(None)

self._actions = {"default": print}

def add_to_action(self, action, action_name, function):
Expand Down Expand Up @@ -65,7 +70,8 @@ def _notify(self, summary, body, replace=True, actions=None, hints=None, expirat
"""
An implementation of Desktop Notifications Specification 1.2.
For a detailed explanation see: https://developer.gnome.org/notification-spec/
Does not fire if notifications were not properly set up
Args:
summary (`str`): A single line overview of the notification
body (`str`): A mutli-line body of the text
Expand All @@ -75,14 +81,28 @@ def _notify(self, summary, body, replace=True, actions=None, hints=None, expirat
expiration (`int`): The time until the notification automatically closes. -1 to make the
server decide and 0 for never. Defaults to 5000.
icon (`str`): The string to icon it displays in the notification. Defaults to headbuds.
Returns:
Nothing.
"""
if not hasattr(self, 'notifications'):
return

self._last_id = self.notifications.Notify(meta.APP_NAME, self._last_id if replace else 0,
icon, summary, body,
actions if actions is not None else list(),
hints if hints is not None else dict(),
expiration)

def _registerBusName(self, nameOwner):
try:
self.notifications = self.bus.get(NOTIFICATION_BUS_NAME)
self.notifications.onActionInvoked = self._on_action
except Exception as exception: # pylint: disable=broad-except
# Bus name did not exist
pass

def _deregisterBusName(self):
self.notifications = None

osd_manager = _OSDManager() # pylint: disable=invalid-name

0 comments on commit 594e527

Please sign in to comment.