-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile_ui.py
568 lines (442 loc) · 15.2 KB
/
profile_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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# vi:si:noet:sw=4:sts=4:ts=8
import hildon
import gtk
import consts
import fremantle
import google_accounts
import settings_ui
import google_api
import profiles
import logger
from ErminigError import *
import error_win
def display_google_auth_error(win):
note = hildon.hildon_note_new_information(win, \
"Could not connect to your Google account. Check username "\
"and password. (And do you have Internet connectivity?)")
note.connect("delete-event", lambda w,d: w.destroy())
gtk.Dialog.run(note)
def display_add_cal_error(win):
note = hildon.hildon_note_new_information(win, \
"Could not create new local calendar. Does it already exist?")
note.connect("delete-event", lambda w,d: w.destroy())
gtk.Dialog.run(note)
def get_sync_direction():
global direction_selector
selection = direction_selector.get_current_text()
model = direction_selector.get_model(0)
iter = model.get_iter_first()
direction = 0
while iter:
if model.get_value(iter, 1) == selection:
direction = model.get_value(iter, 0)
break
iter = model.iter_next(iter)
return direction
def get_profile_enabled():
global enable_chk
return enable_chk.get_active()
def get_local_source():
global local_selector
selection = local_selector.get_current_text()
model = local_selector.get_model(0)
iter = model.get_iter_first()
id = 0
while iter:
if model.get_value(iter, 1) == selection:
id = model.get_value(iter, 0)
break
iter = model.iter_next(iter)
return (id, selection)
def get_remote_source():
global remote_selector
selection = remote_selector.get_current_text()
model = remote_selector.get_model(0)
iter = model.get_iter_first()
id = 0
while iter:
if model.get_value(iter, 1) == selection:
id = model.get_value(iter, 0)
break
iter = model.iter_next(iter)
return (id, selection)
def add_profile(widget, data):
global editing
# sync_type = get_sync_type()
# datasource = get_datasource()
# XXX Temporary:
sync_type = consts.SYNC_TYPE_CAL
datasource = consts.DATASOURCE_BUILTIN
# XXX
local_source, local_source_title = get_local_source()
remote_source, remote_source_title = get_remote_source()
remote_acct_id = current_google_account_selected
enabled = get_profile_enabled()
direction = get_sync_direction()
if editing:
profiles.update_profile((sync_type, datasource, local_source, \
local_source_title, remote_acct_id, remote_source, \
remote_source_title, enabled, direction, \
current_profile_id))
else:
profiles.add_profile((sync_type, datasource, local_source, \
local_source_title, remote_acct_id, remote_source, \
remote_source_title, enabled, direction))
data.destroy()
def new_remote_source(widget, hint):
global current_google_account_selected
global local_selector
dialog = gtk.Dialog()
dialog.set_title("New Google Calendar")
dialog.connect("delete-event", lambda w,d: w.destroy())
label = gtk.Label("Please enter the name of a new Google calendar")
new_cal_entry = hildon.Entry(gtk.HILDON_SIZE_AUTO)
new_cal_entry.set_placeholder("Calendar name")
hint = local_selector.get_current_text()
if not hint:
hint = ""
new_cal_entry.set_text(hint)
new_cal_entry.select_region(0, len(hint))
dialog.vbox.add(label)
dialog.vbox.add(new_cal_entry)
dialog.add_button("Add", gtk.RESPONSE_OK)
dialog.show_all()
resp = dialog.run()
if resp == gtk.RESPONSE_OK:
cal_name = new_cal_entry.get_text().strip()
if not cal_name == "":
cal_id = google_api.create_new_calendar(cal_name)
update_remote_data_source(consts.SYNC_TYPE_CAL,\
current_google_account_selected)
select_remote_source(cal_id)
dialog.destroy()
def new_local_source(widget, hint):
global remote_selector
dialog = gtk.Dialog()
dialog.set_title("New Local Calendar")
dialog.connect("delete-event", lambda w,d: w.destroy())
label = gtk.Label("Please enter the name of a new local calendar")
new_cal_entry = hildon.Entry(gtk.HILDON_SIZE_AUTO)
new_cal_entry.set_placeholder("Calendar name")
hint = remote_selector.get_current_text()
if not hint:
hint = ""
new_cal_entry.set_text(hint)
new_cal_entry.select_region(0, len(hint))
dialog.vbox.add(label)
dialog.vbox.add(new_cal_entry)
dialog.add_button("Add", gtk.RESPONSE_OK)
dialog.show_all()
resp = dialog.run()
if resp == gtk.RESPONSE_OK:
cal_name = new_cal_entry.get_text().strip()
if not cal_name == "":
last_id = fremantle.add_local_calendar(cal_name)
if last_id == -1:
display_add_cal_error(dialog)
else:
update_local_data_source(\
consts.DATASOURCE_BUILTIN)
select_local_source(last_id)
dialog.destroy()
def create_type_selector():
selector = hildon.TouchSelector()
type_list = gtk.ListStore(int, str)
type_list.append([consts.SYNC_TYPE_CAL, "Calendar"])
renderer = gtk.CellRendererText()
column = selector.append_column(type_list, renderer, text=1)
column.set_property("text-column", 1)
picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT,
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
picker.set_title("Synchronization item")
picker.set_selector(selector)
# default on Calendar
picker.set_active(0)
return picker
def create_direction_selector():
global direction_selector
direction_selector = hildon.TouchSelector()
type_list = gtk.ListStore(int, str)
type_list.append([consts.DIRECTION_BOTH, "Both"])
type_list.append([consts.DIRECTION_FROM_GOOGLE_ONLY, "From Google Only"])
type_list.append([consts.DIRECTION_TO_GOOGLE_ONLY, "To Google Only"])
renderer = gtk.CellRendererText()
column = direction_selector.append_column(type_list, renderer, text=1)
column.set_property("text-column", 1)
picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT,
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
picker.set_title("Synchronization direction")
picker.set_selector(direction_selector)
# default on Built-In
picker.set_active(0)
return picker
def update_local_data_source(source_id):
global local_source_list
if not local_source_list:
return
if source_id == consts.DATASOURCE_BUILTIN:
local_source_list.clear()
for id, title in fremantle.getAllLocalCalendars():
local_source_list.append([int(id), title])
def create_local_selector(win):
global local_source_list
global local_selector
local_selector = hildon.TouchSelector()
local_source_list = gtk.ListStore(int, str)
renderer = gtk.CellRendererText()
column = local_selector.append_column(local_source_list, renderer, text=1)
column.set_property("text-column", 1)
picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT,
hildon.BUTTON_ARRANGEMENT_VERTICAL)
picker.set_title("Local Source")
picker.set_selector(local_selector)
new_btn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
new_btn.set_label("New...")
new_btn.connect("clicked", new_local_source, "hint")
hbox = gtk.HBox()
hbox.pack_start(picker)
hbox.pack_start(new_btn, False, False, 30)
return hbox
def update_remote_data_source(sync_type, google_acct_id):
global dialog
remote_sources.clear()
if sync_type == consts.SYNC_TYPE_CAL:
hildon.hildon_gtk_window_set_progress_indicator(dialog, 1)
try:
if not google_api.switch_account(\
google_accounts.get_account_by_id(\
google_acct_id)):
display_google_auth_error(dialog)
google_new_btn.set_sensitive(False)
google_item_picker.set_sensitive(False)
hildon.hildon_gtk_window_set_progress_indicator(dialog, 0)
return
except ErminigError, e:
error_win.display(e.title(), e.description())
return
google_new_btn.set_sensitive(True)
google_item_picker.set_sensitive(True)
for id, title in google_api.get_all_calendars():
logger.append("ID->")
logger.append(id)
logger.append("title->")
logger.append(title)
remote_sources.append([id, title])
hildon.hildon_gtk_window_set_progress_indicator(dialog, 0)
def create_remote_selector(win):
global remote_sources
global google_new_btn
global google_item_picker
global remote_selector
remote_selector = hildon.TouchSelector()
remote_sources = gtk.ListStore(str, str)
renderer = gtk.CellRendererText()
column = remote_selector.append_column(remote_sources, renderer, text=1)
column.set_property("text-column", 1)
google_item_picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
google_item_picker.set_title("Remote Source")
google_item_picker.set_selector(remote_selector)
google_item_picker.set_sensitive(False)
google_new_btn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
google_new_btn.set_label("New...")
google_new_btn.connect("clicked", new_remote_source, "hint")
google_new_btn.set_sensitive(False)
hbox = gtk.HBox()
hbox.pack_start(google_item_picker)
hbox.pack_start(google_new_btn, False, False, 30)
return hbox
def source_selector_changed(selector, user_data):
# Brrrrr
selection = selector.get_current_text()
model = selector.get_model(0)
iter = model.get_iter_first()
local_source_id = 0
while iter:
if model.get_value(iter, 1) == selection:
local_source_id = model.get_value(iter, 0)
break
iter = model.iter_next(iter)
update_local_data_source(local_source_id)
def create_source_selector():
source_selector = hildon.TouchSelector()
type_list = gtk.ListStore(int, str)
type_list.append([consts.DATASOURCE_BUILTIN, "Built-in"])
renderer = gtk.CellRendererText()
column = source_selector.append_column(type_list, renderer, text=1)
column.set_property("text-column", 1)
source_selector.connect("changed", source_selector_changed)
picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT,
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
picker.set_title("Local Data Source")
picker.set_selector(source_selector)
# default on Built-In
picker.set_active(0)
return picker
def account_selector_changed(selector, user_data):
global current_google_account_selected
# Brrrrr
selection = selector.get_current_text()
model = selector.get_model(0)
iter = model.get_iter_first()
account_id = 0
while iter:
if model.get_value(iter, 1) == selection:
account_id = model.get_value(iter, 0)
break
iter = model.iter_next(iter)
# XXX Forcing for the moment...
current_google_account_selected = account_id
update_remote_data_source(consts.SYNC_TYPE_CAL, account_id)
def create_account_selector():
global account_selector
global account_list
account_selector = hildon.TouchSelector()
account_list = gtk.ListStore(int, str)
renderer = gtk.CellRendererText()
column = account_selector.append_column(account_list, renderer, text=1)
column.set_property("text-column", 1)
account_selector.connect("changed", account_selector_changed)
picker = hildon.PickerButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT,
hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
picker.set_title("Google Account")
picker.set_selector(account_selector)
for id, acct in google_accounts.get_registered_accounts():
account_list.append([int(id), acct])
return picker
def create_enable_button():
global enable_chk
enable_chk = hildon.CheckButton(gtk.HILDON_SIZE_AUTO | \
gtk.HILDON_SIZE_FINGER_HEIGHT)
enable_chk.set_label("Enabled")
enable_chk.set_active(True)
return enable_chk
def create_ui(win):
vbox = gtk.VBox()
pannableArea = hildon.PannableArea()
vbox.pack_start(create_type_selector())
vbox.pack_start(create_source_selector())
vbox.pack_start(create_account_selector())
vbox.pack_start(create_local_selector(win))
vbox.pack_start(create_remote_selector(win))
vbox.pack_start(create_enable_button())
vbox.pack_start(create_direction_selector())
pannableArea.set_size_request_policy(hildon.SIZE_REQUEST_CHILDREN)
pannableArea.add_with_viewport(vbox)
win.vbox.pack_start(pannableArea)
bbox = gtk.HButtonBox()
bbox.set_layout(gtk.BUTTONBOX_SPREAD)
addBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
addBtn.set_text("Save profile", "")
addBtn.connect("clicked", add_profile, win)
bbox.pack_end(addBtn, False, False, 0)
cancelBtn = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | \
gtk.HILDON_SIZE_FINGER_HEIGHT, \
hildon.BUTTON_ARRANGEMENT_VERTICAL)
cancelBtn.set_text("Cancel", "")
cancelBtn.connect("clicked", lambda d,w: w.destroy(), win)
bbox.pack_start(cancelBtn, False, False, 0)
win.vbox.pack_end(bbox, False, False, 0)
def select_local_source(localSource):
model = local_selector.get_model(0)
iter = model.get_iter_first()
# XXX Temporary!
while iter:
if model.get_value(iter, 0) == localSource:
local_selector.select_iter(0, iter, True)
break
iter = model.iter_next(iter)
def select_remote_source(remoteSource):
model = remote_selector.get_model(0)
iter = model.get_iter_first()
while iter:
if model.get_value(iter, 0) == remoteSource:
remote_selector.select_iter(0, iter, True)
break
iter = model.iter_next(iter)
def fill_form(pid):
profile = profiles.get_profile_by_id(pid)
enable_chk.set_active(profile['enabled'])
# Google Account
model = account_selector.get_model(0)
iter = model.get_iter_first()
googleAcct = profile['remoteAccountId']
while iter:
if model.get_value(iter, 0) == googleAcct:
account_selector.select_iter(0, iter, True)
break
iter = model.iter_next(iter)
# Local source
select_local_source(int(profile['localSource']))
# Remote source
select_remote_source(profile['remoteSource'])
# Sync direction
model = direction_selector.get_model(0)
iter = model.get_iter_first()
direction = profile['direction']
while iter:
if model.get_value(iter, 0) == direction:
direction_selector.select_iter(0, iter, True)
break
iter = model.iter_next(iter)
def ask_for_google_account_creation(parent):
dialog = gtk.MessageDialog(parent=parent, flags=0, \
type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_NONE)
dialog.set_title("No Google Account defined")
dialog.format_secondary_text("No Google account has been defined yet.\n You will now have the possibility to add an account.")
dialog.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
dialog.run()
dialog.destroy()
settings_ui.add_google_acct_dialog(parent, None, external=True)
for id, acct in google_accounts.get_registered_accounts():
account_list.append([int(id), acct])
def display(win, pid = None):
global dialog
global editing
global current_profile_id
global account_selector
dialog = gtk.Dialog()
dialog.set_title("Add new Profile")
create_ui(dialog)
update_local_data_source(consts.DATASOURCE_BUILTIN)
dialog.connect("delete-event", lambda w, d: w.destroy())
# editing profile
if pid is not None:
dialog.set_title("Edit Profile")
fill_form(pid)
current_profile_id = pid
editing = True
else:
if google_accounts.get_registered_accounts_count() == 0:
ask_for_google_account_creation(dialog)
if google_accounts.get_registered_accounts_count() == 1:
# Pre-select the only Google account available:
account_selector.set_active(0, 0)
dialog.show_all()
dialog.run()
local_source_list = None
local_selector = None
remote_selector = None
remote_sources = None
google_new_btn = None
account_list = None
google_item_picker = None
account_selector = None
direction_selector = None
enable_chk = None
dialog = None
current_google_account_selected = None
editing = False
current_profile_id = 0