Notifications: Check if DBus is available

This commit is contained in:
Philipp Hörist 2019-03-16 15:47:51 +01:00
parent c1948d05e0
commit 36e80622bc
1 changed files with 32 additions and 25 deletions

View File

@ -75,31 +75,10 @@ class Notification:
Handle notifications Handle notifications
""" """
def __init__(self): def __init__(self):
self._dbus_available = False
self.daemon_capabilities = ['actions'] self.daemon_capabilities = ['actions']
# Detect if actions are supported by the notification daemon self._detect_dbus_caps()
if sys.platform not in ('win32', 'darwin'):
def on_proxy_ready(source, res, data=None):
try:
proxy = Gio.DBusProxy.new_finish(res)
self.daemon_capabilities = proxy.GetCapabilities()
except GLib.Error as e:
if e.domain == 'g-dbus-error-quark':
log.info('Notifications D-Bus connection failed: %s',
e.message)
else:
raise
else:
log.debug('Notifications D-Bus connected')
log.debug('Connecting to Notifications D-Bus')
Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
Gio.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
None,
'org.freedesktop.Notifications',
'/org/freedesktop/Notifications',
'org.freedesktop.Notifications',
None, on_proxy_ready)
app.ged.register_event_handler( app.ged.register_event_handler(
'notification', ged.GUI2, self._nec_notification) 'notification', ged.GUI2, self._nec_notification)
@ -107,6 +86,30 @@ class Notification:
'our-show', ged.GUI2, self._nec_our_status) 'our-show', ged.GUI2, self._nec_our_status)
app.events.event_removed_subscribe(self._on_event_removed) app.events.event_removed_subscribe(self._on_event_removed)
def _detect_dbus_caps(self):
if sys.platform in ('win32', 'darwin'):
return
def on_proxy_ready(_source, res, _data=None):
try:
proxy = Gio.DBusProxy.new_finish(res)
self.daemon_capabilities = proxy.GetCapabilities()
except GLib.Error:
log.exception('Notifications D-Bus connection failed')
else:
self._dbus_available = True
log.info('Notifications D-Bus connected')
log.info('Connecting to Notifications D-Bus')
Gio.DBusProxy.new_for_bus(Gio.BusType.SESSION,
Gio.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
None,
'org.freedesktop.Notifications',
'/org/freedesktop/Notifications',
'org.freedesktop.Notifications',
None,
on_proxy_ready)
def _nec_notification(self, obj): def _nec_notification(self, obj):
if obj.do_popup: if obj.do_popup:
icon_name = self._get_icon_name(obj) icon_name = self._get_icon_name(obj)
@ -170,6 +173,9 @@ class Notification:
app.interface.roster.popup_notification_windows.append(instance) app.interface.roster.popup_notification_windows.append(instance)
return return
if not self._dbus_available:
return
scale = gtkgui_helpers.get_monitor_scale_factor() scale = gtkgui_helpers.get_monitor_scale_factor()
icon_pixbuf = gtkgui_helpers.gtk_icon_theme.load_icon_for_scale( icon_pixbuf = gtkgui_helpers.gtk_icon_theme.load_icon_for_scale(
icon_name, 48, scale, 0) icon_name, 48, scale, 0)
@ -220,7 +226,8 @@ class Notification:
app.app.send_notification(notif_id, notification) app.app.send_notification(notif_id, notification)
def withdraw(self, *args): def withdraw(self, *args):
if sys.platform != 'win32': if not self._dbus_available:
return
app.app.withdraw_notification(self._id(*args)) app.app.withdraw_notification(self._id(*args))
def _id(self, *args): def _id(self, *args):