stop notifications spamming
This commit is contained in:
parent
cbfcd8416a
commit
04471ec660
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue