-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator-gmail.py
executable file
·216 lines (189 loc) · 8.46 KB
/
indicator-gmail.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
#!/usr/bin/python
# if the icons don't show up, get rid of or update the icon cache, eg at
# /usr/share/icons/ubuntu-mono-dark/icon-theme.cache
# gets gmail from atom feed
import sys, os, time, logging, gtk, appindicator, threading, gnomekeyring, \
pygame, feedparser, urllib, pynotify, optparse, getpass
optParser = optparse.OptionParser()
optParser.add_option("-u", action = "store", type = "string", dest = "username",
default = "",
help = "Username for gmail account. Password is found in gnome keyring.")
optParser.add_option("-a", action = "store", type = "string", dest = "account",
default = "https://mail.google.com/mail",
help = "Gmail account http address")
optParser.add_option("-i", action = "store", type = "string", dest = "id",
default = "id", help = "A unique id")
optParser.add_option("-s", action = "store", type = "string", dest = "sound",
default = "/usr/share/sounds/ubuntu/stereo/message-new-instant.ogg",
help = "sound file")
optParser.add_option("-c", action = "store", type = "string", dest = "color",
default = "blue", help = "color for new message icon (red or blue)")
options = optParser.parse_args()[0]
#print options
logging.basicConfig(file=sys.stdout, level=logging.INFO,
format="%(asctime)-15s " + options.id + ": %(message)s")
gtk.gdk.threads_init()
pygame.init()
logging.info("gmail indicator starting for " + options.username + ", account " + options.account + ", id " + \
options.id)
class GnomeKeyring():
def getPasswd(cls, key):
if not gnomekeyring.is_available():
err_str = "Cannot get passwords for " + key + " from gnome keyring: not available"
logging.error(err_str)
pynotify.init(self.username)
notification = pynotify.Notification(err_str)
notification.show()
return None
keys = gnomekeyring.list_item_ids_sync("login")
for k in keys:
try:
item = gnomekeyring.item_get_info_sync("login", k)
if key in item.get_display_name(): return item.get_secret()
except Exception as ex:
logging.info("Need to unlock login keyring: " + ex.message)
gnomekeyring.unlock_sync("login", getpass.getpass('Password: '))
err_str = "Cannot get passwords for " + key + " from gnome keyring: not found"
logging.error(err_str)
pynotify.init(self.username)
notification = pynotify.Notification(err_str)
notification.show()
return None
getPasswd = classmethod(getPasswd)
class Gmail():
def __init__(self, username, userkey):
self.username = username
self.passwd = GnomeKeyring.getPasswd(userkey)
if self.passwd == None:
logging.error("Cannot get password for", username, "from gnome keyring")
self.unreadCount = 0
self.msgIds = {}
self.opener = urllib.FancyURLopener()
self.show_err = 0
def getHeaders(self):
headers = []
unreadCount = 0
try:
f = self.opener.open("https://%s:%s@mail.google.com/mail/feed/atom" % (self.username, self.passwd))
feed = f.read()
atom = feedparser.parse(feed)
if len(atom.entries) != self.unreadCount:
logging.info(atom.feed.title + (": %d" % len(atom.entries)) + " unread messages")
# we have not yet seen any of the previous msgs
for key in self.msgIds.keys(): self.msgIds[key] = ""
for entry in atom.entries:
author = entry.author.split("(")[0]
msgId = int(entry.id.split(":")[2])
headers.append([entry.author + ": " + entry.title + "\n<" + entry.summary + ">", msgId])
#print headers[-1][0], id
if not msgId in self.msgIds:
# Now pop up the message
pynotify.init(self.username)
notification = pynotify.Notification(entry.title + " (" + author + ")",
entry.summary, "evolution")
notification.show()
# set to seen
self.msgIds[msgId] = "New"
else: self.msgIds[msgId] = "Old"
deletedMsgs = {}
# clear all the messages that were not seen
for key in self.msgIds.keys():
if self.msgIds[key] == "": del self.msgIds[key]
unreadCount = len(headers)
while unreadCount > self.unreadCount:
pygame.mixer.Sound(options.sound).play()
#time.sleep(1)
self.unreadCount += 1
self.unreadCount = unreadCount
self.show_err = 0
except Exception as ex:
err_str = "Cannot get gmail subjects for " + self.username + ":" + ex.message
logging.error(err_str)
self.show_err += 1
if self.show_err > 5:
pynotify.init(self.username)
notification = pynotify.Notification(err_str)
notification.show()
self.show_err = 0
# returns both the list of headers, and the list of msg ids that remain
return headers, self.msgIds
class MsgMenuItem(gtk.MenuItem):
def __init__(self, text, msg, msgId):
gtk.MenuItem.__init__(self, text)
self.msg = msg
self.msgId = msgId
def getMsgId(self):
return self.msgId
def getMsg(self):
return self.msg
class IndicatorGmail:
def __init__(self):
self.ind = appindicator.Indicator("indicator-gmail-" + options.id, "",
appindicator.CATEGORY_SYSTEM_SERVICES)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.ind.set_label("")
self.menu = gtk.Menu()
self.gmail = Gmail(options.username, options.username)
self.addToMenu("Quit", "quit", "-1")
self.addToMenu(options.username, options.account + "/#inbox", "0")
self.menu.show_all()
self.ind.set_menu(self.menu)
self.checking_mail = False
self.checkMail()
self.fetch_timer = gtk.timeout_add(20000, self.checkMail)
def checkMail(self, event=None):
if self.checking_mail: return gtk.TRUE
self.checking_mail = True
headers, msgIds = self.gmail.getHeaders()
hasChanged = False
# remove messages that are no longer in the list, skipping the first two elements
numElems = 0
for child in self.menu.get_children():
numElems += 1
if numElems <= 2: pass
elif not child.getMsgId() in msgIds:
hasChanged = True
self.menu.remove(child)
# now add all new headers to the menu
for h in headers:
try:
if msgIds[h[1]] == "New":
hasChanged = True
self.addToMenu(" " + h[0],
options.account + "/#inbox/%x" % h[1], h[1])
except Exception as ex:
logging.error("Error setting menu:", ex.message)
# set the text on the panel
num_messages = len(headers)
if num_messages > 21: num_messages = 21
#self.ind.set_label("%d" % num_messages)
if num_messages > 0:
if options.color == "red": icon_name = "new-messages-red-%d" % num_messages
else: icon_name = "indicator-messages-new-%d" % num_messages
else: icon_name = "indicator-messages"
self.ind.set_icon(icon_name)
if hasChanged: self.menu.show_all()
self.checking_mail = False
return gtk.TRUE
def onMailActivated(self, event=None):
if event.getMsg() == "quit":
sys.exit(0)
else:
logging.info("/usr/bin/firefox " + event.getMsg())
os.system("/usr/bin/firefox " + event.getMsg())
def addToMenu(self, text, msg, msgId):
i = MsgMenuItem(text, msg, msgId)
i.connect("activate", self.onMailActivated)
self.menu.add(i)
def onExit(self, event=None):
logging.info("Terminated")
gtk.main_quit()
def main(args):
i = IndicatorGmail()
try:
gtk.main()
except KeyboardInterrupt:
i.onExit()
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))