-
Notifications
You must be signed in to change notification settings - Fork 43
/
rofication-daemon.py
executable file
·257 lines (226 loc) · 8.9 KB
/
rofication-daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
import jsonpickle
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GObject, GLib
import os
import subprocess
import sys
import threading
import time
import socket
import json
from msg import Msg,Urgency
event = threading.Event()
"""
This is a list of applications where only the last notification is relevant.
Applications like media players fall in this category.
"""
single_notification_app=[ "VLC media player" ]
"""
A list of applications that are allowed to expire.
"""
allowed_expire_app=[ ]
class Rofication(threading.Thread):
def __init__(self):
self.socket_path = "/tmp/rofi_notification_daemon"
self.notification_queue_lock = threading.Lock()
self.notification_queue = []
self.last_id=0
super().__init__()
def load(self):
print("Loading rofication")
try:
with open('not.json', 'r') as f:
self.notification_queue = jsonpickle.decode(f.read())
except:
pass
for noti in self.notification_queue:
noti.notid=-1
if self.last_id < noti.mid:
self.last_id = int(noti.mid)
print("Found last id: {nid}".format(nid=nf._id))
def save(self):
print("Saving rofication")
try:
with open('not.json','w') as f:
f.write(jsonpickle.encode(self.notification_queue))
except:
print("Failed to store queue.")
"""
This function updates the queue. E.g. removes popups that are expired and allowed to expire
"""
def update_queue(self):
with self.notification_queue_lock:
now = time.time()
n = [ n for n in self.notification_queue if n.application in allowed_expire_app and n.deadline > 0 and n.deadline < now ];
for no in n:
print("{mid} expired.".format(mid=no.mid))
self.notification_queue.remove(no)
def remove_notification(self,id):
printf("Removing: {}".format(id))
with self.notification_queue_lock:
n = [ n for n in self.notification_queue_lock if n.notid == id ]
for no in n:
print("Closing: {id}:{sum}".format(id=no.mid, sum=no.application))
def add_notification(self,notif):
with self.notification_queue_lock:
if notif.application in single_notification_app:
n = [ n for n in self.notification_queue if n.application == notif.application ];
for no in n:
self.notification_queue.remove(no)
self.notification_queue.append(notif)
"""
Communication command.
"""
def communication_command_send_list(self,connection):
with self.notification_queue_lock:
i=0
for noti in self.notification_queue:
connection.send(bytes(jsonpickle.encode(noti),'utf-8'))
connection.send(b'\n')
i+=1
def communication_command_delete(self, connection, arg):
with self.notification_queue_lock:
for noti in self.notification_queue:
if noti.mid == int(arg):
self.notification_queue.remove(noti)
break
def communication_command_delete_apps(self, connection, arg):
remove_q = []
with self.notification_queue_lock:
for noti in self.notification_queue:
if noti.application == arg:
remove_q.append(noti);
for noti in remove_q:
self.notification_queue.remove(noti)
def communication_command_saw(self, connection, arg):
with self.notification_queue_lock:
for noti in self.notification_queue:
if noti.mid == int(arg):
noti.urgency = int(Urgency.normal)
break
def communication_command_delete_similar(self, connection, arg):
with self.notification_queue_lock:
application = None
for noti in self.notification_queue:
if noti.mid == int(arg):
application = noti.application
break
if application:
remove_q = []
for noti in self.notification_queue:
if noti.application == application:
remove_q.append(noti);
for noti in remove_q:
self.notification_queue.remove(noti)
def communication_command_num(self, connection):
with self.notification_queue_lock:
u = [ n for n in self.notification_queue if n.urgency == Urgency.critical ];
mstr = "{lent}\n{ul}".format(lent=len(self.notification_queue),ul=str(len(u)))
connection.send(bytes(mstr, 'utf-8'))
def run(self):
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(self.socket_path)
server.listen(1)
server.settimeout(1)
while 1:
try:
connection, client_address = server.accept()
self.update_queue()
try:
data = connection.recv(1024).decode('utf-8')
command = data.split(':')[0]
# Get number of notifications
if command == "num":
self.communication_command_num(connection)
# Getting a listing.
elif command == "list":
self.communication_command_send_list(connection)
# Dismiss and item.
elif command == "del":
self.communication_command_delete(connection,data.split(':')[1])
elif command == "dels":
self.communication_command_delete_similar(connection,data.split(':')[1])
elif command == "dela":
self.communication_command_delete_apps(connection,data.split(':')[1])
# Saw an item, this sets the urgency to normal.
elif command == "saw":
self.communication_command_saw(connection, data.split(':')[1])
finally:
# Clean up the connection
connection.close()
except:
if event.is_set():
break;
server.close()
os.unlink("/tmp/rofi_notification_daemon")
"""
DBUS Notification Listener and Fetcher.
"""
class NotificationFetcher(dbus.service.Object):
_id = 0
_rofication = None
@dbus.service.method("org.freedesktop.Notifications",
in_signature='susssasa{ss}i',
out_signature='u')
def Notify(self, app_name, notification_id, app_icon,
summary, body, actions, hints, expire_timeout):
msg = Msg()
# find id.
self._id += 1
msg.application = str(app_name)
msg.notid = notification_id
msg.mid = self._id
msg.summary = str(summary)
msg.body = str(body)
msg.app_icon = str(app_icon)
if int(expire_timeout) > 0:
msg.deadline = time.time()+int(expire_timeout) / 1000.0
if 'urgency' in hints:
msg.urgency = int(hints['urgency'])
self._rofication.add_notification( msg )
return notification_id
@dbus.service.method("org.freedesktop.Notifications", in_signature='', out_signature='as')
def GetCapabilities(self):
return ("body")
@dbus.service.signal('org.freedesktop.Notifications', signature='uu')
def NotificationClosed(self, id_in, reason_in):
_rofication.remove_notification(id_in)
pass
@dbus.service.method("org.freedesktop.Notifications", in_signature='u', out_signature='')
def CloseNotification(self, id):
_rofication.remove_notification(id)
pass
@dbus.service.method("org.freedesktop.Notifications", in_signature='', out_signature='ssss')
def GetServerInformation(self):
return ("rofication", "http://gmpclient.org/", "0.0.1", "1")
"""
Main function
"""
if __name__ == '__main__':
""" Create daemon """
rofication = Rofication();
""" Setup DBUS"""
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.freedesktop.Notifications", session_bus)
nf = NotificationFetcher(session_bus, '/org/freedesktop/Notifications')
nf._rofication = rofication;
rofication.load();
nf._id = rofication.last_id
# Thread handling sockets.
rofication.start()
##
# Create Glib mainloop, this is needed to make dbus work.
# GMainLoop also takes care for catching signals.
##
try:
GLib.MainLoop().run()
except:
# Set event telling it to quit.
event.set()
# Join unix socket thread.
rofication.join()
rofication.save()