/
gmailnotify.py
executable file
·163 lines (141 loc) · 5.13 KB
/
gmailnotify.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
#!/usr/bin/env python
#
# Copyright (C) Morris Jobke 2010 <morris.jobke@googlemail.com>
#
# GMailNotify is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GMailNotify is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import gtk
import indicate
import time
import urllib2
import base64
import subprocess
import feedparser
import pynotify
import calendar
import gobject
# own class Keyring
from gmailnotifykeyring import Keyring
GmailIcon = 'file:///usr/share/pixmaps/gmailnotify.svg'
GmailNotifyName = 'Google Mail Notify'
class GMailIndicator(indicate.Indicator):
def __init__(self, title, sender, time, link=None, settings=False):
""" indicator for a mail in inbox """
indicate.Indicator.__init__(self)
self.title = title
self.sender = sender
self.time = time
self.link = link
self.isSettingsIndicator = settings
self.set_property('name', self.title)
if not self.time == None:
self.set_property_time('time', time)
self.connect('user-display', self.click)
self.stress()
def stress(self):
""" indicator marked as unread """
self.set_property('draw-attention', 'true')
def unstress(self):
""" indicator marked as read """
self.set_property('draw-attention', 'false')
def click(self, server, something):
""" called if indicator entry is clicked """
self.unstress()
if not self.link == None:
subprocess.call(['gnome-open', self.link])
elif self.isSettingsIndicator:
self.openSettingsDialog()
def openSettingsDialog(self):
""" called if setting is not complete """
subprocess.call(['gmailnotify-settings'])
class Notifier:
def __init__(self, username, password):
""" entry in indicator applet """
self.timeout = 30
self.server = indicate.indicate_server_ref_default()
self.server.set_type('message.mail')
self.server.set_desktop_file('/usr/share/applications/gmailnotify.desktop')
self.server.connect('server-display', self.click)
self.request = urllib2.Request('https://mail.google.com/mail/feed/atom/')
self.indicators = []
self.firstRequest = False
if not username == None and not password == None:
self.request.add_header('Authorization', 'Basic %s'%(base64.encodestring('%s:%s'%(username, password))[:-1]))
indicator = GMailIndicator('Receiving data ...', GmailNotifyName, time.time())
self.indicators.append(indicator)
self.indicators[-1].unstress()
self.indicators[-1].show()
self.checkMails()
else:
indicator = GMailIndicator('Setting up ' + GmailNotifyName, GmailNotifyName, None, None, True)
self.indicators.append(indicator)
self.indicators[-1].show()
pynotify.Notification(GmailNotifyName, 'You have to set up ' + GmailNotifyName, GmailIcon).show()
def click(self, server, something):
""" called if entry is clicked """
subprocess.call(['gnome-open', 'https://mail.google.com/'])
def checkMails(self):
""" check gmail for unread mails """
print 'Receiving data ...'
try:
data = feedparser.parse(urllib2.urlopen(self.request).read())
except:
errorMsg = 'An error occured'
if self.firstRequest:
indicator = GMailIndicator('Setting up ' + GmailNotifyName, GmailNotifyName, None, None, True)
self.indicators.append(indicator)
self.indicators[-1].show()
errorMsg += ' - May the login information is not valid'
self.error = pynotify.Notification(GmailNotifyName, errorMsg, GmailIcon)
self.error.show()
deleteIndicators = []
for i in self.indicators:
if i.title in ['Receiving data ...']:
deleteIndicators.append(i)
else:
self.firstRequest = False
for mail in data['entries']:
if not mail['title'] in [indicator.title for indicator in self.indicators]:
print 'new mail ...'
i = GMailIndicator(
mail['title'],
mail['author_detail']['name'],
time.mktime(time.localtime(calendar.timegm(time.strptime(mail['issued'].replace('T24', 'T00'),'%Y-%m-%dT%H:%M:%SZ')))),
mail['link']
)
self.indicators.append(i)
self.indicators[-1].show()
n = pynotify.Notification(
mail['title'],
'from ' + mail['author_detail']['name'] + ' <' + mail['author_detail']['email'] + '>',
GmailIcon
)
n.show()
deleteIndicators = []
for i in self.indicators:
if not i.title in [mail['title'] for mail in data['entries']]:
deleteIndicators.append(i)
for i in deleteIndicators:
i.unstress()
i.hide()
self.indicators.remove(i)
gobject.timeout_add_seconds(self.timeout, self.checkMails)
if __name__ == "__main__":
gkr = Keyring(GmailNotifyName, 'login data')
username, password = gkr.getLogin()
if username == '':
username = None
if password == '':
password = None
gmn = Notifier(username,password)
gtk.main()