-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsettings_ui.py
351 lines (282 loc) · 10.2 KB
/
settings_ui.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
# vi:si:noet:sw=4:sts=4:ts=8
import hildon, alarm, time, os, sys
import gtk
import string
import google_accounts
import dblayer
def get_google_acct_id():
global acct_tree
ts = acct_tree.get_selection()
(model, iter) = ts.get_selected()
return model.get_value(iter, 0)
def del_google_acct(widget, data):
id = get_google_acct_id()
account = google_accounts.get_account_by_id(id)
text = "Are you sure that you want to delete account \"%s\"?\n" \
"Deleting it will remove all links between the data "\
"stored on your device and data stored in Google.\n"\
"Please note that no actual data will be deleted, "\
"only their relationships." % (account[0])
dialog = hildon.Note("confirmation", data, text)
dialog.set_button_texts("Yes", "No")
ans = dialog.run()
dialog.destroy()
if ans == gtk.RESPONSE_OK:
google_accounts.delete_account_by_id(id)
read_accounts_list()
update_gaccts_button()
def edit_google_acct(widget, data):
id = get_google_acct_id()
account = google_accounts.get_account_by_id(id)
add_google_acct_dialog(widget, data, account)
def check_wizard_input(nb, current, data):
if current == 1 or current == 2:
entry = nb.get_nth_page(current)
return len(entry.get_text()) != 0
else:
return True
def add_google_acct(widget, data):
add_google_acct_dialog(widget, data)
def add_google_acct_dialog(widget, data, oldData=None, external=False):
dialog = gtk.Dialog()
if not oldData:
dialog.set_title("Add Google Account")
else:
dialog.set_title("Edit Google Account")
dialog.connect("delete-event", lambda w,d: w.destroy())
username_label = gtk.Label("Username: ")
username_entry = hildon.Entry(gtk.HILDON_SIZE_AUTO)
username_entry.set_placeholder("Please enter your Google username")
password_label = gtk.Label("Password: ")
password_entry = hildon.Entry(gtk.HILDON_SIZE_AUTO)
password_entry.set_placeholder("Please enter your Google password")
password_entry.set_input_mode(gtk.HILDON_GTK_INPUT_MODE_FULL | \
(not gtk.HILDON_GTK_INPUT_MODE_AUTOCAP))
password_entry.set_visibility(False)
if oldData:
username_entry.set_text(oldData[0])
password_entry.set_text(oldData[1])
ubox = gtk.HBox()
ubox.pack_start(username_label)
ubox.pack_start(username_entry)
pbox = gtk.HBox()
pbox.pack_start(password_label)
pbox.pack_start(password_entry)
dialog.vbox.add(ubox)
dialog.vbox.add(pbox)
if not oldData:
dialog.add_button("Add", gtk.RESPONSE_OK)
else:
dialog.add_button("Save", gtk.RESPONSE_OK)
while True:
dialog.show_all()
resp = dialog.run()
if resp == gtk.RESPONSE_OK:
username = username_entry.get_text()
password = password_entry.get_text()
if not oldData:
if store_google_profile(username, password, \
dialog):
dialog.destroy()
if not external:
read_accounts_list()
update_gaccts_button()
break
else:
if update_google_profile(username, password, \
oldData[2], dialog):
dialog.destroy()
read_accounts_list()
update_gaccts_button()
break
else:
break
def read_accounts_list():
global liststore
liststore.clear()
for entry in google_accounts.get_registered_accounts():
liststore.append(entry)
def update_google_profile(username, password, id, win):
if (username == "" or password == ""):
note = hildon.hildon_note_new_information(win, \
"You must provide both the Google username "\
"and password!")
note.connect("delete-event", lambda w,d: w.destroy())
gtk.Dialog.run(note)
return False
google_accounts.update_account(username, password, id)
return True
def store_google_profile(username, password, win):
if (username == "" or password == ""):
note = hildon.hildon_note_new_information(win, \
"You must provide both the Google username "\
"and password!")
note.connect("delete-event", lambda w,d: w.destroy())
gtk.Dialog.run(note)
return False
if (check_username_existence(username)):
note = hildon.hildon_note_new_information(win, "The username"\
" \"%s\" is already defined in Erminig." \
" It will not be added a second time." \
% (username))
note.connect("delete-event", lambda w,d: w.destroy())
gtk.Dialog.run(note)
return False
google_accounts.register_new_account(username, password)
return True
def check_username_existence(username):
return google_accounts.check_username_existence(username)
def google_accounts_win(widget, data):
global liststore
global acct_tree
win = hildon.Dialog()
win.set_title("Manage Google Accounts")
vbox = gtk.VBox(False, 0)
liststore = gtk.ListStore(int, str)
acct_tree = hildon.GtkTreeView(gtk.HILDON_UI_MODE_EDIT, liststore)
read_accounts_list()
cell = gtk.CellRendererText()
col = gtk.TreeViewColumn()
col.pack_start(cell, True)
col.set_attributes(cell, text=1)
acct_tree.append_column(col)
pannableArea = hildon.PannableArea()
pannableArea.add(acct_tree)
pannableArea.set_size_request_policy(hildon.SIZE_REQUEST_CHILDREN)
vbox.pack_start(pannableArea, True, True, 0)
bbox = gtk.HButtonBox()
bbox.set_layout(gtk.BUTTONBOX_SPREAD)
newBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
newBtn.set_text("Add Account", "")
newBtn.connect("clicked", add_google_acct, win)
bbox.pack_start(newBtn, False, False, 0)
editBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
editBtn.set_text("Edit Account", "")
editBtn.connect("clicked", edit_google_acct, win)
bbox.pack_start(editBtn, False, False, 0)
delBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
delBtn.set_text("Remove Account", "")
delBtn.connect("clicked", del_google_acct, win)
bbox.pack_start(delBtn, False, False, 0)
doneBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
doneBtn.set_text("Done", "")
doneBtn.connect("clicked", lambda w,d: d.destroy(), win)
bbox.pack_start(doneBtn, False, False, 20)
vbox.pack_end(bbox, False, True, 0)
win.vbox.add(vbox)
win.connect("delete-event", lambda w,d: w.destroy())
win.show_all()
win.run()
def create_settings_buttons(win):
global google_accts_btn
google_accts_btn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
google_accts_btn.connect("clicked", google_accounts_win, win)
update_gaccts_button()
def update_gaccts_button():
count = google_accounts.get_registered_accounts_count()
text = ""
if (count == 1):
text = "1 account registered"
else:
text = "%s accounts registered" % (count)
google_accts_btn.set_text("Google Accounts", text)
# auto sync functions
def auto_sync_get_syncid():
cur = dblayer.run("SELECT value FROM Prefs where name='auto_sync_id'")
count = dblayer.getValue(cur, "value")
return count
def auto_sync_update_syncid(sync_id):
if auto_sync_get_syncid() is None:
dblayer.run("INSERT INTO Prefs (name, value) values ('auto_sync_id', '%d')" % sync_id)
else:
dblayer.run("UPDATE Prefs SET value='%d' where name='auto_sync_id'" % sync_id)
dblayer.commit()
def auto_sync_get_synctime():
cur = dblayer.run("SELECT value FROM Prefs where name='auto_sync_time'")
time = dblayer.getValue(cur, "value")
if not time is None:
return time.split(':')
else:
return None
def auto_sync_update_synctime(sync_hour, sync_min):
if auto_sync_get_synctime() is None:
dblayer.run("INSERT INTO Prefs (name, value) values ('auto_sync_time', '%d:%d')" % (sync_hour,sync_min))
else:
dblayer.run("UPDATE Prefs SET value='%d:%d' where name='auto_sync_time'" % (sync_hour,sync_min))
dblayer.commit()
def auto_sync_toggled(auto_button, time_button):
active = auto_button.get_active()
if active:
(hours, minutes) = time_button.get_time()
event = alarm.Event()
event.appid = "erminig"
event.title = "Synchronization with erminig"
#event.flags |= alarm.EVENT_RUN_DELAYED
action = event.add_actions(1)[0]
action.flags |= alarm.ACTION_WHEN_TRIGGERED | alarm.ACTION_WHEN_DELAYED | alarm.ACTION_TYPE_EXEC
action.command = os.path.abspath(sys.argv[0]) + " -a -d"
recur = event.add_recurrences(1)[0]
# let's see what this does...
recur.mask_min = 1 << minutes
recur.mask_hour = 1 << hours
# initialize alarm time to somewhere in the future
event.alarm_time = time.time() + 5
# lt = time.localtime(time.time() + 5)
# tz = time.tzname[lt.tm_isdst]
# event.alarm_time = time.mktime(recur.next(lt, tz))
event.recurrences_left = -1
sync_id=alarm.add_event(event)
auto_sync_update_syncid(sync_id)
auto_sync_update_synctime(hours, minutes)
else:
alarm.delete_event(int(auto_sync_get_syncid()))
dblayer.run("DELETE FROM Prefs where name='auto_sync_id'")
dblayer.commit()
def auto_sync_time_changed(time_button):
(hours, minutes) = time_button.get_time()
auto_sync_update_synctime(hours, minutes)
sync_id=auto_sync_get_syncid()
if not sync_id is None:
event = alarm.get_event(int(sync_id))
recur = event.get_recurrence(0)
recur.mask_min = 1 << minutes
recur.mask_hour = 1 << hours
sync_id = alarm.update_event(event)
auto_sync_update_syncid(sync_id)
def auto_sync_loadtime(time_button):
time = auto_sync_get_synctime()
if not time is None:
time_button.set_time(int(time[0]), int(time[1]))
def display(win):
dialog = gtk.Dialog()
dialog.set_title("Settings")
create_settings_buttons(dialog)
dialog.vbox.add(google_accts_btn)
# daily sync setting
auto_button = hildon.CheckButton(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT)
auto_button.set_label("Automatically synchronize daily")
auto_button.set_active(not auto_sync_get_syncid() is None)
dialog.vbox.add(auto_button)
time_button = hildon.TimeButton(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
time_button.set_title("Daily synchronization time")
time_button.set_alignment(0.25, 0.5, 0.5, 0.5)
dialog.vbox.add(time_button)
auto_sync_loadtime(time_button)
auto_button.connect("toggled", auto_sync_toggled, time_button)
time_button.connect("value-changed", auto_sync_time_changed)
dialog.connect("delete-event", lambda w,d: w.destroy())
dialog.show_all()
dialog.run()
liststore = None
acct_tree = None
google_accts_btn = None