-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmailApplet.py
271 lines (205 loc) · 7.13 KB
/
gmailApplet.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
import sys
import os
import urllib2
import gi
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import PanelApplet
import managers
am = managers.AccountManager()
cm = managers.ConnectionManager()
config = managers.ConfigManager()
class GmailApplet:
NEW_EMAIL_ICON_PATH = "/usr/share/pixmaps/gmail-applet/new-email.svg"
NO_EMAIL_ICON_PATH = "/usr/share/pixmaps/gmail-applet/no-email.svg"
NO_CONNECTION_ICON_PATH = "/usr/share/pixmaps/gmail-applet/no-connection.svg"
HAVE_ICON = False
# This is for not changing the icon every once.
NEW_EMAIL = 0
NO_EMAIL = 1
NO_CONNECTION = 2
can_connect = False
connected = False
main_menu_xml = """<menuitem name='Accounts' action='Accounts'/>
<menuitem name='Timeouts' action='Timeouts'/>
"""
def __init__(self, applet):
self.applet = applet
self.create_widgets()
# Set the status to no connection
self.status = self.NO_CONNECTION
self.gobject_id = GObject.timeout_add(config.get_ping(), self.check_for_new_mails )
# Try to connect
self.check_for_new_mails()
def create_widgets(self):
print("About to create the widgets")
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.event_box = Gtk.EventBox()
try:
# Trying to put the icon in the applet
self.icon = Gtk.Image()
if self.connected:
self.icon.set_from_file(self.NO_EMAIL_ICON_PATH)
else:
self.icon.set_from_file(self.NO_CONNECTION_ICON_PATH)
self.event_box.add(self.icon)
self.HAVE_ICON = True
except:
print("There's no icon :/")
self.event_box.set_visible_window(False)
self.event_box.connect("button_press_event", self.on_eb_press)
self.box.pack_start(self.event_box, False, False, 0)
# Setup the menu
# Action group
group = Gtk.ActionGroup("gmail_applet_actions")
# Accounts item
a_accounts = Gtk.Action("Accounts", "Accounts", "Open the account manager dialog", Gtk.STOCK_PREFERENCES)
a_accounts.connect("activate", lambda x: self.show_account_dialog())
# Timeout preferences
a_timeout = Gtk.Action("Timeouts", "Set timeout", "", Gtk.STOCK_PREFERENCES)
a_timeout.connect("activate",lambda x: self.show_timeout_dialog())
group.add_action(a_accounts)
group.add_action(a_timeout)
# Create the menu
self.applet.setup_menu(self.main_menu_xml, group)
# Making the options menu ( i.e. open a browser, update manually, etc )
self.menu = Gtk.Menu()
self.open_gmail = Gtk.MenuItem()
self.open_gmail.set_label("Open Gmail")
self.open_gmail.connect("activate", self.check_gmail_on_browser)
self.check_menu = Gtk.MenuItem()
self.check_menu.set_label('Manual check')
self.check_menu.connect("activate", self.manual_check)
self.menu.append(self.open_gmail)
self.menu.append(self.check_menu)
self.menu.show_all()
self.applet.set_background_widget(self.box)
self.applet.add(self.box)
self.applet.show_all()
def on_eb_press(self, widget, event):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
self.menu.popup(None,None,None,None,event.button, event.time)
return True
print("Event Box clicked")
return False
def connect_to_gmail(self, username):
self.can_connect = True
successful_connection = cm.connect(username, am.get_password_from_username( username ) )
if successful_connection:
self.connected = True
self.icon.set_from_file(self.NO_EMAIL_ICON_PATH)
self.status = self.NO_EMAIL
self.check_for_new_mails()
else:
self.connectied = False
if self.status != self.NO_CONNECTION:
self.icon.set_from_file(self.NO_CONNECTION_ICON_PATH)
self.status = self.NO_CONNECTION
print("Not connected")
def check_for_new_mails(self):
if have_internet_connection():
if cm.connected:
# If we're connected, then we check for new emails.
print("Idle check")
status = cm.have_new_emails()
print ( "status " + status.__str__())
print ( "self status: " + self.status.__str__())
if status != self.status:
if status == self.NO_EMAIL:
self.icon.set_from_file(self.NO_EMAIL_ICON_PATH)
self.status = self.NO_EMAIL
elif status == self.NEW_EMAIL:
self.icon.set_from_file(self.NEW_EMAIL_ICON_PATH)
self.status = self.NEW_EMAIL
else:
# If we're not connected, try to connect.
is_registered, username = config.get_email()
print(is_registered)
if is_registered:
self.connect_to_gmail(username)
else:
# If we're not connected, try to connect.
is_registered, username = config.get_email()
print(is_registered)
if is_registered:
self.connect_to_gmail(username)
#self.check_for_new_mails()
return True
def show_account_dialog(self):
print("Opening the dialog")
dialog = NewAccountDialog()
response = dialog.run()
if response == Gtk.ResponseType.OK:
print ("The OK Button it's clicked")
email = dialog.email_entry.get_text()
password = dialog.password_entry.get_text()
am.save_password( email, password )
config.set_email( dialog.email_entry.get_text() )
self.can_connect = True
password = None
elif response == Gtk.ResponseType.CANCEL:
pass
dialog.destroy()
return True
def show_timeout_dialog(self):
dialog = TimeoutDialog()
response = dialog.run()
if response == Gtk.ResponseType.OK:
new_timeout = int(dialog.timeout_entry.get_text()) * 60 * 1000
config.set_ping( new_timeout )
# Eliminate the las timeout_add to set another
GObject.source_remove( self.gobject_id )
self.gobject_id = GObject.timeout_add(new_timeout, self.check_for_new_mails )
dialog.destroy()
return True
def check_gmail_on_browser( self, widget):
os.system("gnome-open 'https://mail.google.com'")
return True
def manual_check( self, widget ):
self.check_for_new_mails()
return True
class NewAccountDialog(Gtk.Dialog):
def __init__(self,parent=None):
Gtk.Dialog.__init__(self,"New Account", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(200,150)
self.email_label = Gtk.Label("Email:")
self.email_entry = Gtk.Entry()
self.password_label = Gtk.Label("Password:")
self.password_entry = Gtk.Entry()
self.password_entry.set_visibility(False)
box = self.get_content_area()
# Add everything to the dialog
box.add(self.email_label)
box.add(self.email_entry)
box.add(self.password_label)
box.add(self.password_entry)
self.show_all()
class TimeoutDialog(Gtk.Dialog):
def __init__(self,parent=None):
Gtk.Dialog.__init__(self,"New Account", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size( 150, 100 )
self.label = Gtk.Label("Select how often you want to check mails")
self.timeout_entry = Gtk.Entry()
box = self.get_content_area()
box.add(self.label)
box.add(self.timeout_entry)
self.show_all()
def have_internet_connection():
try:
response = urllib2.urlopen(config.get_ip(),timeout=1)
return True
except urllib2.URLError as err:
pass
return False
''' Entry point '''
def applet_factory ( applet, iid, data = None):
if iid != "gmail":
return False
GObject.threads_init()
gmail = GmailApplet( applet )
return True