From 04471ec660946a2463ef8148dbaac79f413cbef4 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Thu, 11 Oct 2012 20:37:30 +0300 Subject: [PATCH] stop notifications spamming --- app/ui/notify.py | 28 +++++++++++++++++++++++++--- app/ui/window.py | 12 +++++++++--- app/watcher.py | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/ui/notify.py b/app/ui/notify.py index 2cd69ac6..cfe82194 100644 --- a/app/ui/notify.py +++ b/app/ui/notify.py @@ -7,11 +7,15 @@ import logging try: import notify2 as _notify + from time import time as timestamp available = True # assumed to be working since the import succeeded _active = False # not yet active _app_title = None + _TIMEOUT = 5 * 60 # after this many seconds assume the notification object is no longer valid + _notifications = {} + def init(app_title, active=True): """Init the notifications system.""" @@ -33,6 +37,11 @@ try: available = False else: if _active: + for n in _notifications.values(): + try: + n.close() + except: + logging.exception("closing notification %s", n) try: _notify.uninit() except: @@ -49,11 +58,24 @@ try: def show(status_code, title, text='', icon=None): """Show a notification with title and text.""" if available and _active: - notification = _notify.Notification(title, text, icon or title) + n = None + if title in _notifications: + n = _notifications[title] + if timestamp() - n.timestamp > _TIMEOUT: + del _notifications[title] + n = None + + if n is None: + n = _notify.Notification(title) + _notifications[title] = n + + n.update(title, text, icon or title) + n.timestamp = timestamp() try: - notification.show() + logging.debug("showing notification %s", n) + n.show() except Exception: - logging.exception("showing notification %s", notification) + logging.exception("showing notification %s", n) except ImportError: diff --git a/app/ui/window.py b/app/ui/window.py index b3706b8f..505ab2ad 100644 --- a/app/ui/window.py +++ b/app/ui/window.py @@ -109,7 +109,8 @@ def update(window, receiver, devices, icon_name=None): if icon_name is not None: window.set_icon_name(icon_name) - controls = list(window.get_child().get_children()) + vbox = window.get_child().get_child() + controls = list(vbox.get_children()) _update_receiver_box(controls[0], receiver) for index in range(1, len(controls)): _update_device_box(controls[index], devices.get(index)) @@ -202,12 +203,17 @@ def create(title, rstatus, systray=False): for i in range(1, 1 + rstatus.max_devices): vbox.add(_device_box()) vbox.set_visible(True) - window.add(vbox) + + frame = Gtk.Frame() + # frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN) + frame.set_visible(True) + frame.add(vbox) + window.add(frame) geometry = Gdk.Geometry() geometry.min_width = 300 geometry.min_height = 40 - window.set_geometry_hints(vbox, geometry, Gdk.WindowHints.MIN_SIZE) + window.set_geometry_hints(frame, geometry, Gdk.WindowHints.MIN_SIZE) window.set_resizable(False) window.set_default_size(geometry.min_width, geometry.min_height) diff --git a/app/watcher.py b/app/watcher.py index 2a23d413..cbb6388b 100644 --- a/app/watcher.py +++ b/app/watcher.py @@ -179,7 +179,7 @@ class Watcher(Thread): devstatus.text = status_text _l.debug("%s update %s => %s: %s", devstatus, old_status_code, status_code, status_text) - if self.notify: + if self.notify and (status_code <= C.STATUS.CONNECTED or status_code != old_status_code): self.notify(devstatus.code, devstatus.name, devstatus.text) return True