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
|
## dataforms.py
##
## Copyright (C) 2003-2007 Yann Le Boulanger <asterix@lagaule.org>
## Copyright (C) 2005-2006 Nikos Kouremenos <nkour@jabber.org>
## Copyright (C) 2005 Dimitur Kirov <dkirov@gmail.com>
## Copyright (C) 2003-2005 Vincent Hanquez <tab@snarc.org>
##
## This file is part of Gajim.
##
## Gajim 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; version 3 only.
##
## Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
##
""" This module contains widget that can display data form (JEP-0004).
Words single and multiple refers here to types of data forms:
single means these with one record of data (without <reported/> element),
multiple - these which may contain more data (with <reported/> element)."""
import gtk
import gtkgui_helpers
import common.dataforms as dataforms
import itertools
class DataFormWidget(gtk.Alignment, object):
# "public" interface
""" Data Form widget. Use like any other widget. """
def __init__(self, dataformnode=None):
""" Create a widget. """
gtk.Alignment.__init__(self, xscale=1.0, yscale=1.0)
self._data_form = None
self.xml=gtkgui_helpers.get_glade('data_form_window.glade', 'data_form_vbox')
self.xml.signal_autoconnect(self)
for name in ('instructions_label', 'instructions_hseparator',
'single_form_viewport', 'data_form_types_notebook',
'single_form_scrolledwindow', 'multiple_form_hbox',
'records_treeview', 'buttons_vbox', 'add_button', 'remove_button',
'edit_button', 'up_button', 'down_button', 'clear_button'):
self.__dict__[name] = self.xml.get_widget(name)
self.add(self.xml.get_widget('data_form_vbox'))
if dataformnode is not None:
self.set_data_form(dataformnode)
selection = self.records_treeview.get_selection()
selection.connect('changed', self.on_records_selection_changed)
selection.set_mode(gtk.SELECTION_MULTIPLE)
def set_data_form(self, dataform):
""" Set the data form (xmpp.DataForm) displayed in widget. """
assert isinstance(dataform, dataforms.DataForm)
self.del_data_form()
self._data_form = dataform
if isinstance(dataform, dataforms.SimpleDataForm):
self.build_single_data_form()
else:
self.build_multiple_data_form()
# create appropriate description for instructions field if there isn't any
if dataform.instructions=='':
self.instructions_label.set_no_show_all(True)
self.instructions_label.hide()
else:
self.instructions_label.set_text(dataform.instructions)
def get_data_form(self):
""" Data form displayed in the widget or None if no form. """
return self._data_form
def del_data_form(self):
self.clean_data_form()
self._data_form = None
data_form = property(get_data_form, set_data_form, del_data_form,
"Data form presented in a widget")
def get_title(self):
""" Get the title of data form, as a unicode object. If no
title or no form, returns u''. Useful for setting window title. """
if self._data_form is not None:
if self._data_form.title is not None:
return self._data_form.title
return u''
title = property(get_title, None, None, "Data form title")
def show(self):
""" Treat 'us' as one widget. """
self.show_all()
# "private" methods
# we have actually two different kinds of data forms: one is a simple form to fill,
# second is a table with several records;
def empty_method(self):
pass
def clean_data_form(self):
'''Remove data about existing form. This metod is empty, because
it is rewritten by build_*_data_form, according to type of form
which is actually displayed.'''
pass
def build_single_data_form(self):
'''Invoked when new single form is to be created.'''
assert isinstance(self._data_form, dataforms.SimpleDataForm)
self.clean_data_form()
self.singleform = SingleForm(self._data_form)
self.singleform.show()
self.single_form_viewport.add(self.singleform)
self.data_form_types_notebook.set_current_page(
self.data_form_types_notebook.page_num(
self.single_form_scrolledwindow))
self.clean_data_form = self.clean_single_data_form
def clean_single_data_form(self):
'''(Called as clean_data_form, read the docs of clean_data_form()).
Remove form from widget.'''
self.singleform.destroy()
self.clean_data_form = self.empty_method # we won't call it twice
del self.singleform
def build_multiple_data_form(self):
'''Invoked when new multiple form is to be created.'''
assert isinstance(self._data_form, dataforms.MultipleDataForm)
self.clean_data_form()
# creating model for form...
fieldtypes = []
fieldvars = []
for field in self._data_form.reported.iter_fields():
# note: we store also text-private and hidden fields,
# we just do not display them.
# TODO: boolean fields
#elif field.type=='boolean': fieldtypes.append(bool)
fieldtypes.append(str)
fieldvars.append(field.var)
self.multiplemodel = gtk.ListStore(*fieldtypes)
# moving all data to model
for item in self._data_form.iter_records():
iter = self.multiplemodel.append()
for field in item.iter_fields():
self.multiplemodel.set_value(iter, fieldvars.index(field.var),
field.value)
# constructing columns...
for field, counter in zip(self._data_form.reported.iter_fields(), itertools.count()):
self.records_treeview.append_column(
gtk.TreeViewColumn(field.label, gtk.CellRendererText(),
text=counter))
self.records_treeview.set_model(self.multiplemodel)
self.records_treeview.show_all()
self.data_form_types_notebook.set_current_page(
self.data_form_types_notebook.page_num(
self.multiple_form_hbox))
self.clean_data_form = self.clean_multiple_data_form
readwrite = self._data_form.type != 'result'
if not readwrite:
self.buttons_vbox.set_no_show_all(True)
self.buttons_vbox.hide()
else:
self.buttons_vbox.set_no_show_all(False)
# refresh list look
self.refresh_multiple_buttons()
def clean_multiple_data_form(self):
'''(Called as clean_data_form, read the docs of clean_data_form()).
Remove form from widget.'''
self.clean_data_form = self.empty_method # we won't call it twice
del self.multiplemodel
def refresh_multiple_buttons(self):
''' Checks for treeview state and makes control buttons sensitive.'''
selection = self.records_treeview.get_selection()
model = self.records_treeview.get_model()
count = selection.count_selected_rows()
if count == 0:
self.remove_button.set_sensitive(False)
self.edit_button.set_sensitive(False)
self.up_button.set_sensitive(False)
self.down_button.set_sensitive(False)
elif count == 1:
self.remove_button.set_sensitive(True)
self.edit_button.set_sensitive(True)
_, (path,) = selection.get_selected_rows()
iter = model.get_iter(path)
if model.iter_next(iter) is None:
self.up_button.set_sensitive(True)
self.down_button.set_sensitive(False)
elif path == (0, ):
self.up_button.set_sensitive(False)
self.down_button.set_sensitive(True)
else:
self.up_button.set_sensitive(True)
self.down_button.set_sensitive(True)
else:
self.remove_button.set_sensitive(True)
self.edit_button.set_sensitive(True)
self.up_button.set_sensitive(False)
self.down_button.set_sensitive(False)
if len(model) == 0:
self.clear_button.set_sensitive(False)
else:
self.clear_button.set_sensitive(True)
def on_clear_button_clicked(self, widget):
self.records_treeview.get_model().clear()
def on_remove_button_clicked(self, widget):
selection = self.records_treeview.get_selection()
model, rowrefs = selection.get_selected_rows() # rowref is a list of paths
for i in xrange(len(rowrefs)):
rowrefs[i] = gtk.TreeRowReference(model, rowrefs[i])
# rowref is a list of row references; need to convert because we will modify the model,
# paths would change
for rowref in rowrefs:
del model[rowref.get_path()]
def on_up_button_clicked(self, widget):
selection = self.records_treeview.get_selection()
model, (path,) = selection.get_selected_rows()
iter = model.get_iter(path)
previter = model.get_iter((path[0]-1,)) # constructing path for previous iter
model.swap(iter, previter)
self.refresh_multiple_buttons()
def on_down_button_clicked(self, widget):
selection = self.records_treeview.get_selection()
model, (path,) = selection.get_selected_rows()
iter = model.get_iter(path)
nextiter = model.iter_next(iter)
model.swap(iter, nextiter)
self.refresh_multiple_buttons()
def on_records_selection_changed(self, widget):
self.refresh_multiple_buttons()
class SingleForm(gtk.Table, object):
""" Widget that represent DATAFORM_SINGLE mode form. Because this is used
not only to display single forms, but to form input windows of multiple-type
forms, it is in another class."""
def __init__(self, dataform):
assert isinstance(dataform, dataforms.SimpleDataForm)
gtk.Table.__init__(self)
self.set_col_spacings(12)
self.set_row_spacings(6)
self.tooltips = gtk.Tooltips()
def decorate_with_tooltip(widget, field):
""" Adds a tooltip containing field's description to a widget.
Creates EventBox if widget doesn't have its own gdk window.
Returns decorated widget. """
if field.description!='':
if widget.flags()>k.NO_WINDOW:
evbox = gtk.EventBox()
evbox.add(widget)
widget = evbox
self.tooltips.set_tip(widget, field.description)
return widget
self._data_form = dataform
# building widget
linecounter = 0
# is the form changeable?
readwrite = dataform.type != 'result'
# for each field...
for field in self._data_form.iter_fields():
if field.type=='hidden': continue
commonlabel = True
commonlabelcenter = False
commonwidget = True
widget = None
if field.type=='boolean':
commonlabelcenter = True
widget = gtk.CheckButton()
widget.connect('toggled', self.on_boolean_checkbutton_toggled, field)
widget.set_active(field.value)
elif field.type=='fixed':
leftattach = 1
rightattach = 2
if field.label is None:
commonlabel = False
leftattach = 0
commonwidget=False
widget = gtk.Label(field.value)
widget.set_line_wrap(True)
self.attach(widget, leftattach, rightattach, linecounter, linecounter+1,
xoptions=gtk.FILL, yoptions=gtk.FILL)
elif field.type == 'list-single':
# TODO: What if we have radio buttons and non-required field?
# TODO: We cannot deactivate them all...
if len(field.options) < 6:
# 5 option max: show radiobutton
widget = gtk.VBox()
first_radio = None
for value, label in field.iter_options():
radio = gtk.RadioButton(first_radio, label=label)
radio.connect('toggled',
self.on_list_single_radiobutton_toggled, field, value)
if first_radio is None:
first_radio = radio
if field.value == '': # TODO: is None when done
field.value = value
if value == field.value:
radio.set_active(True)
widget.pack_start(radio, expand=False)
else:
# more than 5 options: show combobox
def on_list_single_combobox_changed(combobox, f):
iter = combobox.get_active_iter()
if iter:
model = combobox.get_model()
f.value = model[iter][1]
else:
f.value = ''
widget = gtkgui_helpers.create_combobox(field.options, field.value)
widget.connect('changed', on_list_single_combobox_changed, field)
widget.set_sensitive(readwrite)
elif field.type == 'list-multi':
# TODO: When more than few choices, make a list
widget = gtk.VBox()
for value, label in field.iter_options():
check = gtk.CheckButton(label, use_underline=False)
check.set_active(value in field.values)
check.connect('toggled', self.on_list_multi_checkbutton_toggled,
field, value)
widget.set_sensitive(readwrite)
widget.pack_start(check, expand=False)
elif field.type == 'jid-single':
widget = gtk.Entry()
widget.connect('changed', self.on_text_single_entry_changed, field)
widget.set_text(field.value)
elif field.type == 'jid-multi':
commonwidget = False
xml = gtkgui_helpers.get_glade('data_form_window.glade', 'item_list_table')
widget = xml.get_widget('item_list_table')
treeview = xml.get_widget('item_treeview')
listmodel = gtk.ListStore(str)
for value in field.iter_values():
# nobody will create several megabytes long stanza
listmodel.insert(999999, (value,))
treeview.set_model(listmodel)
renderer = gtk.CellRendererText()
renderer.set_property('editable', True)
renderer.connect('edited',
self.on_jid_multi_cellrenderertext_edited, listmodel, field)
treeview.append_column(gtk.TreeViewColumn(None, renderer,
text=0))
decorate_with_tooltip(treeview, field)
add_button=xml.get_widget('add_button')
add_button.connect('clicked',
self.on_jid_multi_add_button_clicked, treeview, listmodel, field)
edit_button=xml.get_widget('edit_button')
edit_button.connect('clicked',
self.on_jid_multi_edit_button_clicked, treeview)
remove_button=xml.get_widget('remove_button')
remove_button.connect('clicked',
self.on_jid_multi_remove_button_clicked, treeview, field)
clear_button=xml.get_widget('clear_button')
clear_button.connect('clicked',
self.on_jid_multi_clean_button_clicked, listmodel, field)
if not readwrite:
add_button.set_no_show_all(True)
edit_button.set_no_show_all(True)
remove_button.set_no_show_all(True)
clear_button.set_no_show_all(True)
widget.set_sensitive(readwrite)
self.attach(widget, 1, 2, linecounter, linecounter+1)
del xml
elif field.type == 'text-private':
commonlabelcenter = True
widget = gtk.Entry()
widget.connect('changed', self.on_text_single_entry_changed, field)
widget.set_visibility(False)
widget.set_text(field.value)
elif field.type == 'text-multi':
# TODO: bigger text view
commonwidget = False
textwidget = gtk.TextView()
textwidget.set_wrap_mode(gtk.WRAP_WORD)
textwidget.get_buffer().connect('changed', self.on_text_multi_textbuffer_changed,
field)
textwidget.get_buffer().set_text(field.value)
widget = gtk.ScrolledWindow()
widget.add(textwidget)
widget.set_sensitive(readwrite)
widget=decorate_with_tooltip(widget, field)
self.attach(widget, 1, 2, linecounter, linecounter+1)
else:# field.type == 'text-single' or field.type is nonstandard:
# JEP says that if we don't understand some type, we
# should handle it as text-single
commonlabelcenter = True
if readwrite:
widget = gtk.Entry()
widget.connect('changed', self.on_text_single_entry_changed, field)
widget.set_sensitive(readwrite)
if field.value is None:
field.value = u''
widget.set_text(field.value)
else:
commonwidget=False
widget = gtk.Label(field.value)
widget.set_sensitive(True)
widget.set_alignment(0.0, 0.5)
widget=decorate_with_tooltip(widget, field)
self.attach(widget, 1, 2, linecounter, linecounter+1,
yoptions=gtk.FILL)
if commonlabel and field.label is not None:
label = gtk.Label(field.label)
if commonlabelcenter:
label.set_alignment(0.0, 0.5)
else:
label.set_alignment(0.0, 0.0)
label = decorate_with_tooltip(label, field)
self.attach(label, 0, 1, linecounter, linecounter+1,
xoptions=gtk.FILL, yoptions=gtk.FILL)
if commonwidget:
assert widget is not None
widget.set_sensitive(readwrite)
widget = decorate_with_tooltip(widget, field)
self.attach(widget, 1, 2, linecounter, linecounter+1,
yoptions=gtk.FILL)
widget.show_all()
linecounter+=1
if self.get_property('visible'):
self.show_all()
def show(self):
# simulate that we are one widget
self.show_all()
def on_boolean_checkbutton_toggled(self, widget, field):
field.value = widget.get_active()
def on_list_single_radiobutton_toggled(self, widget, field, value):
field.value = value
def on_list_multi_checkbutton_toggled(self, widget, field, value):
# TODO: make some methods like add_value and remove_value
if widget.get_active() and value not in field.values:
field.values += [value]
elif not widget.get_active() and value in field.values:
field.values = [v for v in field.values if v!=value]
def on_text_single_entry_changed(self, widget, field):
field.value = widget.get_text()
def on_text_multi_textbuffer_changed(self, widget, field):
field.value = widget.get_text(
widget.get_start_iter(),
widget.get_end_iter())
def on_jid_multi_cellrenderertext_edited(self, cell, path, newtext, model, field):
old=model[path][0]
model[path][0]=newtext
values = field.values
values[values.index(old)]=newtext
field.values = values
def on_jid_multi_add_button_clicked(self, widget, treeview, model, field):
iter = model.insert(999999, ("new@jabber.id",))
treeview.set_cursor(model.get_path(iter), treeview.get_column(0), True)
field.values = field.values + ["new@jabber.id"]
def on_jid_multi_edit_button_clicked(self, widget, treeview):
model, iter = treeview.get_selection().get_selected()
assert iter is not None
treeview.set_cursor(model.get_path(iter), treeview.get_column(0), True)
def on_jid_multi_remove_button_clicked(self, widget, treeview, field):
selection = treeview.get_selection()
model = treeview.get_model()
deleted = []
def remove(model, path, iter, deleted):
deleted+=model[iter]
model.remove(iter)
selection.selected_foreach(remove, deleted)
field.values = (v for v in field.values if v not in deleted)
def on_jid_multi_clean_button_clicked(self, widget, model, field):
model.clear()
del field.values
|