-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
executable file
·481 lines (430 loc) · 15.1 KB
/
notifier.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Uploaded by juan_grande 2005/02/24 18:38 UTC
import pygtk
pygtk.require('2.0')
import gtk
import time
import pynotify
import os
import sys
sys.path.insert (0, "/usr/lib/gmail-notify/")
import warnings
import ConfigParser
import xmllangs
import GmailConfig
import GmailPopupMenu
import gmailatom
import re
#BKG_PATH="/usr/share/apps/gmail-notify/background.jpg"
ICON_PATH="/usr/share/apps/gmail-notify/icon.svg"
ICON2_PATH="/usr/share/apps/gmail-notify/icon2.png"
ICON3_PATH="/usr/share/apps/gmail-notify/icon3.png"
STATE_NEW = 'new'
STATE_ZERO = 'zero'
STATE_NONE = 'none'
STATE_ERROR = 'error'
STATE_OK = 'ok'
def help_cb(n,action):
print "Nothing to help"
return
def removetags(text):
raw=text.split("<b>")
raw2=raw[1].split("</b>")
final=raw2[0]
return final
def shortenstring(text,characters):
if text == None: text = ""
mainstr=""
length=0
splitstr=text.split(" ")
for word in splitstr:
length=length+len(word)
if len(word)>characters:
if mainstr=="":
mainstr=word[0:characters]
break
else: break
mainstr=mainstr+word+" "
if length>characters: break
return mainstr.strip()
class GmailNotify:
configWindow = None
options = None
def __init__(self):
self.init=0
print "Gmail Notifier v1.6.1b ("+time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())+")"
print "----------"
# Configuration window
self.configWindow = GmailConfig.GmailConfigWindow( )
# Reference to global options
self.options = self.configWindow.options
# Check if there is a user and password, if not, load config window
if self.configWindow.no_username_or_password():
self.configWindow.show()
# If there's still not a username or password, just exit.
if self.configWindow.no_username_or_password():
sys.exit(0)
# Load selected language
self.lang = self.configWindow.get_lang()
print "selected language: "+self.lang.get_name()
# Creates the main window
self.window = gtk.Window(gtk.WINDOW_POPUP)
self.window.set_title(self.lang.get_string(21)) # 21 = Gmail Notifier
self.window.set_resizable(1)
self.window.set_decorated(0)
self.window.set_keep_above(1)
self.window.stick()
self.window.hide()
# Define some flags
self.senddown=0
self.popup=0
self.newmessages=0
self.mailcheck=0
self.hasshownerror=0
self.hassettimer=0
self.dont_connect=0
self.unreadmsgcount=0
# Define the timers
self.maintimer=None
self.popuptimer=0
self.waittimer=0
# # Enable Sound
# self.sound_enabled=True
# Create the popup menu
self.popup_menu = GmailPopupMenu.GmailPopupMenu( self)
# Init pynotify
pynotify.init("gmail-notify")
# Create the notify
#noti = pynotify.Notification("")
#~ self.fixed=gtk.Fixed()
#~ self.window.add(self.fixed)
#~ self.fixed.show()
#~ self.fixed.set_size_request(0,0)
#~ # Set popup's background image
#~ self.image=gtk.Image()
#~ self.image.set_from_file( BKG_PATH )
#~ self.image.show()
#~ self.fixed.put(self.image,0,0)
# Set popup's label
self.label=gtk.Label()
self.label.set_line_wrap(1)
self.label.set_size_request(170,140)
self.default_title =self.lang.get_string(21) # 21 = Gmail Notifier
self.default_label =self.lang.get_string(20) # 20 = Password:
self.label.set_markup( self.default_label)
# Show popup
self.label.show()
#~ # Create popup's event box
#~ self.event_box = gtk.EventBox()
#~ self.event_box.set_visible_window(0)
#~ self.event_box.show()
#~ self.event_box.add(self.label)
#~ self.event_box.set_size_request(180,125)
#~ self.event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK)
#~ self.event_box.connect("button_press_event", self.event_box_clicked)
#~ # Setup popup's event box
#~ self.fixed.put(self.event_box,6,25)
#~ self.event_box.realize()
#~ self.event_box.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1))
#~ # Resize and move popup's event box
#~ self.window.resize(180,1)
#~ self.width, self.height = self.window.get_size()
#~ self.height+=self.options['voffset']
#~ self.width+=self.options['hoffset']
#~ self.window.move(gtk.gdk.screen_width() - self.width, gtk.gdk.screen_height() - self.height)
# Create the tray icon object
self.tray = gtk.StatusIcon()
self.tray.set_title(self.lang.get_string(21)) # 21 = Gmail Notifier
self.tray.connect("button_press_event",self.tray_icon_clicked)
# Set the image for the tray icon
#self.pixbuf = gtk.gdk.pixbuf_new_from_file( ICON_PATH )
self.set_tray_state(STATE_ZERO)
self.init=1
while gtk.events_pending():
gtk.main_iteration(gtk.TRUE)
# Attemp connection for first time
if self.connect()==1:
# Check mail for first time
self.mail_check()
self.maintimer=gtk.timeout_add(self.options['checkinterval'],self.mail_check)
def set_tray_state(self,state=STATE_NONE,size=24):
global current
global icon_state
if state==STATE_ZERO:
icon_state = "media-playlist-repeat"
current=STATE_NONE
if state==STATE_NONE:
state=current
if state==STATE_OK or state==STATE_NEW:
icon_state = "mail-mark-unread"
if state==STATE_ERROR:
icon_state = "mail-mark-important"
icon_theme = gtk.icon_theme_get_default()
pixbuf = icon_theme.load_icon( icon_state , size, gtk.ICON_LOOKUP_FORCE_SVG)
scaled_buf = pixbuf.scale_simple(size,size,gtk.gdk.INTERP_BILINEAR)
self.tray.set_from_pixbuf(scaled_buf)
if state==STATE_NEW:
self.tray.set_blinking(True)
else:
self.tray.set_blinking(False)
current = state
return scaled_buf # pixbuf
def sound_handle(self,menuitem):
return
def connect(self):
# If connecting, cancel connection
if self.dont_connect==1:
print "connection attemp suspended"
return 0
self.dont_connect=1
print "connecting..."
self.tray.set_tooltip(self.lang.get_string(13)) # 13 = Connecting...
while gtk.events_pending():
gtk.main_iteration( gtk.TRUE)
# Attemp connection
try:
self.connection=gmailatom.GmailAtom(self.options['gmailusername'],self.options['gmailpassword'],self.options['proxy'])
self.connection.refreshInfo()
print "connection successful... continuing"
self.tray.set_tooltip_text(self.lang.get_string(14)) # 14 = Connected
self.set_tray_state(STATE_OK)
self.dont_connect=0
return 1
except:
print "login failed, will retry"
self.tray.set_tooltip_text(self.lang.get_string(15)) # 15 = Connection failed
self.default_label = "<span size='large' ><u><i>"+self.lang.get_string(15)+"</i></u></span>\n\n"+self.lang.get_string(16) # 16 = Connection to your Gmail inbox failed, will retry
self.label.set_markup(self.default_label)
self.set_tray_state(STATE_ERROR)
self.show_popup()
self.dont_connect=0
#~ #self.pixbuf = gtk.gdk.pixbuf_new_from_file( ICON3_PATH )
#~ self.pixbuf = icon_theme.load_icon("mail-mark-important", 24, gtk.ICON_LOOKUP_FORCE_SVG)
#~ scaled_buf = self.pixbuf.scale_simple(24,24,gtk.gdk.INTERP_BILINEAR)
#~ self.tray.set_from_pixbuf(scaled_buf)
return 0
def mail_check(self, event=None):
# If checking, cancel mail check
if self.mailcheck==1:
print "self.mailcheck=1"
return gtk.TRUE
# If popup is up, destroy it
if self.popup==1:
self.destroy_popup()
self.mailcheck=1
print "----------"
print "checking for new mail ("+time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())+")"
while gtk.events_pending():
gtk.main_iteration( gtk.TRUE)
# Get new messages count
attrs = self.has_new_messages()
# attrs[0] -> Unread Messages
# attrs[1] -> New messages
# If mail check was unsuccessful
if attrs[0]==-1:
self.mailcheck=0
return gtk.TRUE
if attrs[1]>0:
print str(attrs[1])+" new messages"
sender = attrs[2]
subject= attrs[3]
snippet= attrs[4]
if len(snippet)>0:
#self.default_title="<span size='large' ><u><i>"+self.lang.get_string(17)+sender[0:24]+"</i></u></span>\n"
self.default_title=self.lang.get_string(17)+sender[0:24] # 17 = New mail from
self.default_label=shortenstring(subject,20)+"\n\n"+snippet+"..."
else:
self.default_title=self.lang.get_string(17)+sender[0:24] # 17 = New mail from
self.default_label=shortenstring(subject,20)+"\n\n"+snippet+"..."
self.show_popup()
if attrs[0]>0:
if self.popup_menu.item_sound.get_active():
os.system("pacmd play-file /usr/share/sounds/sonido-huayra/stereo/suspend-error.oga 0 2>&1 >/dev/null &")
print str(attrs[0])+" unread messages"
s = ' '
if attrs[0]>1: s=self.lang.get_string(35)+" " # 35 = s
self.tray.set_tooltip_text((self.lang.get_string(19))%{'u':attrs[0],'s':s}) # 19 = %(u)d unread message%(s)s
#~ #self.pixbuf = gtk.gdk.pixbuf_new_from_file( ICON2_PATH )
#~ self.pixbuf = icon_theme.load_icon("mail-mark-unread", 24, gtk.ICON_LOOKUP_FORCE_SVG)
tray_state = STATE_NEW
else:
print "no new messages"
#self.default_title="<span size='large' ><i><u>"+self.lang.get_string(21)+"</u></i></span>\n\n\n"
self.default_title=self.lang.get_string(21) # 21 = Gmail Notifier
self.default_label=self.lang.get_string(18) # 18 = No unread mail
self.tray.set_tooltip_text(self.lang.get_string(18))
#~ #self.pixbuf = gtk.gdk.pixbuf_new_from_file( ICON_PATH )
#~ self.pixbuf = icon_theme.load_icon("mail-mark-read", 24, gtk.ICON_LOOKUP_FORCE_SVG)
tray_state = STATE_OK
p = re.compile('&')
self.label.set_markup(p.sub('&', self.default_label))
#~ scaled_buf = self.pixbuf.scale_simple(24,24,gtk.gdk.INTERP_BILINEAR)
#~ self.tray.set_from_pixbuf(scaled_buf)
self.set_tray_state(tray_state)
self.unreadmsgcount=attrs[0]
self.mailcheck=0
return gtk.TRUE
def has_new_messages( self):
unreadmsgcount=0
# Get total messages in inbox
try:
self.connection.refreshInfo()
unreadmsgcount=self.connection.getUnreadMsgCount()
except:
# If an error ocurred, cancel mail check
print "getUnreadMsgCount() failed, will try again soon"
self.tray.set_tooltip_text(self.lang.get_string(25)) # 25: Mailcheck failed, will retry
self.default_label = self.lang.get_string(25)
self.set_tray_state('error')
return (-1,)
sender=''
subject=''
snippet=''
finalsnippet=''
if unreadmsgcount>0:
# Get latest message data
sender = self.connection.getMsgAuthorName(0)
subject = self.connection.getMsgTitle(0)
snippet = self.connection.getMsgSummary(0)
if len(sender)>12:
finalsnippet=shortenstring(snippet,20)
else:
finalsnippet=shortenstring(snippet,40)
# Really new messages? Or just repeating...
newmsgcount=unreadmsgcount-self.unreadmsgcount
self.unreadmsgcount=unreadmsgcount
if unreadmsgcount>0:
return (unreadmsgcount, newmsgcount, sender, subject, finalsnippet)
else:
return (unreadmsgcount,0, sender, subject, finalsnippet)
def gotourlnotify( self , n, action ):
assert action == "default"
print "----------"
print "launching browser "+self.options['browserpath']+" http://mail.google.com"
os.system(self.options['browserpath']+" http://mail.google.com &")
n.close()
return
def show_popup(self):
if self.popup==1:return
# Generate popup
print "generating popup"
self.noti = pynotify.Notification(self.default_title,re.sub('&','&', self.default_label))
#~ self.noti.set_icon_from_pixbuf(self.pixbuf)
self.noti.set_icon_from_pixbuf(self.set_tray_state(STATE_NONE,48))
self.noti.set_category("presence.online")
self.noti.add_action("default","Default Action", self.gotourlnotify )
#self.noti.add_action("help","Help", help_cb )
self.noti.show()
self.popup=1
return
def destroy_popup(self):
self.popup=0
print "destroying popup"
return
def popup_proc(self):
# Set popup status flag
if self.popup==0:
self.popup=1
currentsize=self.window.get_size()
currentposition=self.window.get_position()
positiony=currentposition[1]
sizey=currentsize[1]
if self.senddown==1:
if sizey<2:
# If popup is down
self.senddown=0
self.window.hide()
self.window.resize(280,1)
self.window.move(gtk.gdk.screen_width() - self.width, gtk.gdk.screen_height() - self.height)
self.popup=0
return gtk.FALSE
else:
# Move it down
self.window.resize(280,sizey-2)
self.window.move(gtk.gdk.screen_width() - self.width,positiony+2)
else:
if sizey<140:
# Move it up
self.window.resize(280,sizey+2)
self.window.move(gtk.gdk.screen_width() - self.width,positiony-2)
else:
# If popup is up, run wait timer
sizex=currentsize[0]
self.popup=1
if self.hassettimer==0:
self.waittimer = gtk.timeout_add(self.options['popuptimespan'],self.wait)
self.hassettimer=1
return gtk.TRUE
def gotourl( self, wg=None):
print "----------"
print "launching browser "+self.options['browserpath']+" http://mail.google.com"
os.system(self.options['browserpath']+" http://mail.google.com &")
def wait(self):
self.senddown=1
self.hassettimer=0
return gtk.FALSE
def tray_icon_clicked(self,signal,event):
if event.button==3:
self.popup_menu.show_menu(event)
else:
self.label.set_markup(self.default_label)
self.popup=0
self.show_popup()
#~ def event_box_clicked(self,signal,event):
#~ if event.button==1:
#~ self.gotourl()
#~
def exit(self, event):
dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, self.lang.get_string(5)) # 5 = Are you sure do you want to leave Gmail Notifier?
dialog.width, dialog.height = dialog.get_size()
dialog.move( gtk.gdk.screen_width()/2-dialog.width/2, gtk.gdk.screen_height()/2-dialog.height/2)
ret = dialog.run()
if( ret==gtk.RESPONSE_YES):
gtk.main_quit(0)
dialog.destroy()
def show_quota_info( self, event):
print "Not available"
#if self.popup==1:self.destroy_popup()
#print "----------"
#print "retrieving quota info"
#while gtk.events_pending()!=0:
# gtk.main_iteration(gtk.TRUE)
#try:
# usage=self.connection.getQuotaInfo()
#except:
# if self.connect()==0:
# return
# else:
# usage=self.connection.getQuotaInfo()
#self.label.set_markup("<span size='large' ><u><i>"+self.lang.get_string(6)+"</i></u></span>\n\n"+self.lang.get_string(24)%{'u':usage[0],'t':usage[1],'p':usage[2]})
#self.show_popup()
def update_config(self, event=None):
# Kill all timers
if self.popup==1:self.destroy_popup()
if self.init==1:gtk.timeout_remove(self.maintimer)
# Run the configuration dialog
self.configWindow.show()
# Update timeout
self.maintimer = gtk.timeout_add(self.options["checkinterval"], self.mail_check )
# Update user/pass
self.connection=gmailatom.GmailAtom(self.options["gmailusername"],self.options["gmailpassword"],self.options['proxy'])
self.connect()
self.mail_check()
# Update popup location
self.window.resize(180,1)
self.width, self.height = self.window.get_size()
self.height +=self.options["voffset"]
self.width +=self.options["hoffset"]
self.window.move(gtk.gdk.screen_width() - self.width, gtk.gdk.screen_height() - self.height)
# Update language
self.lang=self.configWindow.get_lang()
# Update popup menu
self.popup_menu = GmailPopupMenu.GmailPopupMenu(self)
return
def main(self):
gtk.main()
if __name__ == "__main__":
warnings.filterwarnings( action="ignore", category=DeprecationWarning)
gmailnotifier = GmailNotify()
gmailnotifier.main()