-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator.py
executable file
·152 lines (122 loc) · 3.76 KB
/
indicator.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
#!/usr/bin/env python
import gtk, glib, sys, fcntl, os
import gobject
import indicate
class MessagingServer:
def __init__(self, subtype, desktop):
self.indicators = {}
self.actions = {}
self.user_cb = None
self.server_cb = None
self.desktop = desktop
self.srv = indicate.indicate_server_ref_default()
type = "message.%s" % subtype
self.srv.set_type(type)
self.srv.set_desktop_file(desktop)
self.srv.show()
def show_indicator(self, name, count, draw_attention=True):
# update existing indicator, or create new one
try:
ind = self.indicators[name]
except KeyError:
print "NEW"
ind = indicate.Indicator()
self.indicators[name] = ind
ind.set_property('name', name)
ind.set_property('count', str(count))
ind.set_property('draw-attention', 'true' if draw_attention else 'false')
ind.connect('user-display', self.cb_user_display)
# hide and reshow actions to keep them at top of list
for a in self.actions.values():
a.hide()
ind.show()
for a in self.actions.values():
a.show()
return ind
def hide_indicator(self, name):
try:
self.indicators[name].hide()
del(self.indicators[name])
except KeyError:
print "ERROR: No indicator named '%s' to hide" % name
def add_action(self, name, cb):
ind = indicate.Indicator()
self.actions[name] = ind
ind.set_property('name', name)
ind.set_property('subtype', 'menu')
ind.connect('user-display', cb)
ind.show()
return ind
def set_user_cb(self, cb):
self.user_cb = cb
def set_server_cb(self, cb):
self.server_cb = cb
def cb_server_display(self, srv, id):
print "SERVER DISPLAY"
if (self.server_cb):
self.server_cb(self)
def cb_user_display(self, ind, id):
print "USER DISPLAY"
if (self.user_cb):
self.user_cb(ind.get_property('name'), ind.get_property('count'))
ind.hide()
def set_nonblock(fd, nonblock):
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
if nonblock:
fl |= os.O_NONBLOCK
else:
fl &= ~os.O_NONBLOCK
fcntl.fcntl(fd, fcntl.F_SETFL, fl)
def user_display(name, count):
os.system("thunderbird -mail&")
def server_display(srv):
os.system("thunderbird -mail&")
def io_cb(f, condition, srv):
commands = {
'show': [srv.show_indicator, 2],
'hide': [srv.hide_indicator, 1],
'exit': [exit, 0]
}
if condition == glib.IO_IN:
data = f.read().strip()
args = data.strip().split("::")
cmd = args.pop(0)
try:
fn, numargs = commands[cmd]
except KeyError:
print "ERROR: command '%s' not known" % cmd
return True
if numargs != len(args):
print "ERROR: '%s' command takes %d arguments but were %d given" % (cmd,
numargs, len(args))
return True
print "CMD: %s" % cmd
if fn:
fn(*args)
else:
print "ERROR: I/O Error"
exit()
return True
if __name__ == "__main__":
def action_compose(indicator, id):
os.system("thunderbird -compose&")
def action_addressbook(indicator, id):
os.system("thunderbird -addressbook&")
def timeout(srv):
srv.add_action("Contacts", action_addressbook)
srv.add_action("Compose New Message", action_compose)
srv = MessagingServer('email', '/usr/share/applications/thunderbird.desktop')
srv.set_user_cb(user_display)
srv.set_server_cb(server_display)
fifopath = sys.argv[1]
#fifopath = "/tmp/thunderbird-indicator-fifo"
if not os.path.exists(fifopath):
os.mkfifo(fifopath)
if len(sys.argv) > 2 and sys.argv[2] == 'mkfifoonly':
exit()
fdr = os.open(fifopath, os.O_RDONLY | os.O_NONBLOCK)
fdw = os.open(fifopath, os.O_WRONLY | os.O_NONBLOCK)
f = os.fdopen(fdr)
glib.io_add_watch(f, glib.IO_IN | glib.IO_ERR, io_cb, srv)
gobject.timeout_add_seconds(0, timeout, srv)
gtk.main()