- commit
- 52b98d40c5586a5065a9d19137d9ca0d542a7c44
- parent
- 08ae1688ed1384549c65444db6c26b7fee28bf17
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-08-12 06:17
show notification threads in menu
Diffstat
| M | notification_hub.py | 50 | +++++++++++++++++++++++++++++++++++++++++++++++++- |
1 files changed, 49 insertions, 1 deletions
diff --git a/notification_hub.py b/notification_hub.py
@@ -8,9 +8,11 @@ import sys 8 8 import gi 9 9 10 10 gi.require_version('AyatanaAppIndicator3', '0.1') -1 11 gi.require_version('Gtk', '3.0') 11 12 12 13 from gi.repository import Gio # noqa 13 14 from gi.repository import GLib # noqa -1 15 from gi.repository import Gtk # noqa 14 16 from gi.repository import AyatanaAppIndicator3 as AppIndicator3 # noqa 15 17 16 18 VERSION = '0.0.0' @@ -57,6 +59,49 @@ INTROSPECTION_XML = """<?xml version="1.0" encoding="UTF-8"?> 57 59 58 60 next_id = 1 59 61 indicator = None -1 62 menu = None -1 63 threads = {} -1 64 -1 65 -1 66 def clear_thread(item, app_name): -1 67 del threads[app_name] -1 68 menu.remove(item) -1 69 if len(menu) == 0: -1 70 indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE) -1 71 -1 72 -1 73 def on_add_notification(params, id): -1 74 app_name = params[0] -1 75 replaces_id = params[1] -1 76 summary = params[3] -1 77 -1 78 label = f'{app_name}: {summary}' -1 79 thread = threads.get(app_name) -1 80 -1 81 if thread: -1 82 if replaces_id in thread['ids']: -1 83 thread['ids'].remove(replaces_id) -1 84 thread['ids'].add(id) -1 85 thread['menuitem'].set_label(label) -1 86 else: -1 87 item = Gtk.MenuItem(label=label) -1 88 item.connect('activate', clear_thread, app_name) -1 89 menu.append(item) -1 90 menu.show_all() -1 91 indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION) -1 92 -1 93 threads[app_name] = { -1 94 'menuitem': item, -1 95 'ids': {id}, -1 96 } -1 97 -1 98 -1 99 def on_close_notification(id): -1 100 for app_name, thread in list(threads.items()): -1 101 if id in thread['ids']: -1 102 thread['ids'].remove(id) -1 103 if not thread['ids']: -1 104 clear_thread(thread['menuitem'], app_name) 60 105 61 106 62 107 def on_call( @@ -67,10 +112,11 @@ def on_call( 67 112 # announce fake capabilities to avoid firefox fallback 68 113 reply = GLib.Variant('(as)', [['actions', 'body', 'body-hyperlinks']]) 69 114 elif method == 'Notify':70 -1 indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION)-1 115 on_add_notification(params, next_id) 71 116 reply = GLib.Variant('(u)', [next_id]) 72 117 next_id += 1 73 118 elif method == 'CloseNotification': -1 119 on_close_notification(*params) 74 120 reply = None 75 121 elif method == 'GetServerInformation': 76 122 info = ['notification-hub', 'xi', VERSION, '1.2'] @@ -111,6 +157,8 @@ if __name__ == '__main__': 111 157 AppIndicator3.IndicatorCategory.APPLICATION_STATUS, 112 158 ) 113 159 indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE) -1 160 menu = Gtk.Menu() -1 161 indicator.set_menu(menu) 114 162 115 163 try: 116 164 loop = GLib.MainLoop()