comparison src/tools/xml_tools.py @ 1084:03dcb6ca7e49

core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget
author Goffi <goffi@goffi.org>
date Wed, 25 Jun 2014 14:01:55 +0200
parents bb4517e69957
children 2cb30f46e560
comparison
equal deleted inserted replaced
1083:e8731b02f5ea 1084:03dcb6ca7e49
32 SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names 32 SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names
33 33
34 34
35 # Helper functions 35 # Helper functions
36 36
37 def _dataFormField2XMLUIData(field): 37 def _dataFormField2XMLUIData(field, read_only=False):
38 """ Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field 38 """ Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field
39 field can be modified (if it's fixed and it has no value) 39 field can be modified (if it's fixed and it has no value)
40 @param field: data_form.Field (it uses field.value, field.fieldType, field.label and field.var) 40 @param field: data_form.Field (it uses field.value, field.fieldType, field.label and field.var)
41 @param read_only: if True and it make sens, create a read only input widget
41 @return: widget_type, widget_args, widget_kwargs 42 @return: widget_type, widget_args, widget_kwargs
42 43
43 """ 44 """
44 widget_args = [field.value] 45 widget_args = [field.value]
45 widget_kwargs = {} 46 widget_kwargs = {}
53 field.value = field.label 54 field.value = field.label
54 field.label = None 55 field.label = None
55 widget_args[0] = field.value 56 widget_args[0] = field.value
56 elif field.fieldType == 'text-single': 57 elif field.fieldType == 'text-single':
57 widget_type = "string" 58 widget_type = "string"
59 widget_kwargs['read_only'] = read_only
58 elif field.fieldType == 'text-multi': 60 elif field.fieldType == 'text-multi':
59 widget_type = "textbox" 61 widget_type = "textbox"
60 widget_args[0] = u'\n'.join(field.values) 62 widget_args[0] = u'\n'.join(field.values)
63 widget_kwargs['read_only'] = read_only
61 elif field.fieldType == 'text-private': 64 elif field.fieldType == 'text-private':
62 widget_type = "password" 65 widget_type = "password"
66 widget_kwargs['read_only'] = read_only
63 elif field.fieldType == 'boolean': 67 elif field.fieldType == 'boolean':
64 widget_type = "bool" 68 widget_type = "bool"
65 if widget_args[0] is None: 69 if widget_args[0] is None:
66 widget_args[0] = 'false' 70 widget_args[0] = 'false'
71 widget_kwargs['read_only'] = read_only
67 elif field.fieldType == 'list-single': 72 elif field.fieldType == 'list-single':
68 widget_type = "list" 73 widget_type = "list"
69 widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options] 74 widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options]
70 widget_kwargs["selected"] = widget_args 75 widget_kwargs["selected"] = widget_args
71 widget_args = [] 76 widget_args = []
72 else: 77 else:
73 log.error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType) 78 log.error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
74 widget_type = "string" 79 widget_type = "string"
80 widget_kwargs['read_only'] = read_only
75 81
76 if field.var: 82 if field.var:
77 widget_kwargs["name"] = field.var 83 widget_kwargs["name"] = field.var
78 84
79 return widget_type, widget_args, widget_kwargs 85 return widget_type, widget_args, widget_kwargs
80 86
81 def dataForm2Widgets(form_ui, form): 87 def dataForm2Widgets(form_ui, form, read_only=False):
82 """Complete an existing XMLUI with widget converted frot XEP-0004 data forms 88 """Complete an existing XMLUI with widget converted frot XEP-0004 data forms
83 89
84 @param form_ui: XMLUI instance 90 @param form_ui: XMLUI instance
85 @param form: Wokkel's implementation of data form 91 @param form: Wokkel's implementation of data form
86 @return: completed xml_ui 92 @return: completed xml_ui
89 form_ui.addText('\n'.join(form.instructions), 'instructions') 95 form_ui.addText('\n'.join(form.instructions), 'instructions')
90 96
91 form_ui.changeContainer("pairs") 97 form_ui.changeContainer("pairs")
92 98
93 for field in form.fieldList: 99 for field in form.fieldList:
94 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field) 100 widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only)
95 label = field.label or field.var 101 label = field.label or field.var
96 if label: 102 if label:
97 form_ui.addLabel(label) 103 form_ui.addLabel(label)
98 else: 104 else:
99 form_ui.addEmpty() 105 form_ui.addEmpty()
100 106
101 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs) 107 form_ui.addWidget(widget_type, *widget_args, **widget_kwargs)
102 108
103 return form_ui 109 return form_ui
104 110
105 def dataForm2XMLUI(form, submit_id, session_id=None): 111 def dataForm2XMLUI(form, submit_id, session_id=None, read_only=False):
106 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT XML 112 """Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT XML
107 113
108 @param submit_id: callback id to call when submitting form 114 @param submit_id: callback id to call when submitting form
109 @param session_id: id to return with the data 115 @param session_id: id to return with the data
110 """ 116 """
111 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id) 117 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
112 return dataForm2Widgets(form_ui, form) 118 return dataForm2Widgets(form_ui, form, read_only=read_only)
113 119
114 def dataFormResult2AdvancedList(xmlui, form_xml): 120 def dataFormResult2AdvancedList(xmlui, form_xml):
115 """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list 121 """Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list
116 raw data form is used because Wokkel doesn't manage result items parsing yet 122 raw data form is used because Wokkel doesn't manage result items parsing yet
117 @param xmlui: the XMLUI where the AdvancedList will be added 123 @param xmlui: the XMLUI where the AdvancedList will be added
162 xml_ui = XMLUI("window", "vertical", session_id=session_id) 168 xml_ui = XMLUI("window", "vertical", session_id=session_id)
163 try: 169 try:
164 dataFormResult2AdvancedList(xml_ui, form_elt) 170 dataFormResult2AdvancedList(xml_ui, form_elt)
165 except exceptions.DataError: 171 except exceptions.DataError:
166 parsed_form = data_form.Form.fromElement(form_elt) 172 parsed_form = data_form.Form.fromElement(form_elt)
167 dataForm2Widgets(xml_ui, parsed_form) 173 dataForm2Widgets(xml_ui, parsed_form, read_only=True)
168 return xml_ui 174 return xml_ui
169 175
170 def _cleanValue(value): 176 def _cleanValue(value):
171 """Workaround method to avoid DBus types with D-Bus bridge 177 """Workaround method to avoid DBus types with D-Bus bridge
172 178
590 InternalFieldElement(self, field) 596 InternalFieldElement(self, field)
591 if data_elts: 597 if data_elts:
592 InternalDataElement(self, data_elts) 598 InternalDataElement(self, data_elts)
593 599
594 600
595 class InputWidget(Widget):
596 pass
597
598
599 class EmptyWidget(Widget): 601 class EmptyWidget(Widget):
602 """Place holder widget"""
600 type = 'empty' 603 type = 'empty'
601 604
602 605
603 class TextWidget(Widget): 606 class TextWidget(Widget):
607 """Used for blob of text"""
604 type = 'text' 608 type = 'text'
605 609
606 def __init__(self, xmlui, value, name=None, parent=None): 610 def __init__(self, xmlui, value, name=None, parent=None):
607 super(TextWidget, self).__init__(xmlui, name, parent) 611 super(TextWidget, self).__init__(xmlui, name, parent)
608 text = self.xmlui.doc.createTextNode(value) 612 text = self.xmlui.doc.createTextNode(value)
609 self.elem.appendChild(text) 613 self.elem.appendChild(text)
610 614
611 615
612 class LabelWidget(Widget): 616 class LabelWidget(Widget):
617 """Used for one line blob of text,
618 most of time to display the desciption or name of the next widget
619 """
613 type='label' 620 type='label'
614 621
615 def __init__(self, xmlui, label, name=None, parent=None): 622 def __init__(self, xmlui, label, name=None, parent=None):
616 super(LabelWidget, self).__init__(xmlui, name, parent) 623 super(LabelWidget, self).__init__(xmlui, name, parent)
617 self.elem.setAttribute('value', label) 624 self.elem.setAttribute('value', label)
618 625
619 626
620 class JidWidget(Widget): 627 class JidWidget(Widget):
628 """Used to display a Jabber ID, some specific methods can be added"""
621 type='jid' 629 type='jid'
622 630
623 def __init__(self, xmlui, jid, name=None, parent=None): 631 def __init__(self, xmlui, jid, name=None, parent=None):
624 super(JidWidget, self).__init__(xmlui, name, parent) 632 super(JidWidget, self).__init__(xmlui, name, parent)
625 try: 633 try:
646 """ 654 """
647 super(DividerWidget, self).__init__(xmlui, name, parent) 655 super(DividerWidget, self).__init__(xmlui, name, parent)
648 self.elem.setAttribute('style', style) 656 self.elem.setAttribute('style', style)
649 657
650 658
659 ### Inputs ###
660
661
662 class InputWidget(Widget):
663 """Input widget are widget which can accept user inputs,
664 used mainly in forms
665 """
666 def __init__(self, xmlui, name=None, parent=None, read_only=False):
667 super(InputWidget, self).__init__(xmlui, name, parent)
668 if read_only:
669 self.elem.setAttribute('read_only', 'true')
670
671
651 class StringWidget(InputWidget): 672 class StringWidget(InputWidget):
652 type = 'string' 673 type = 'string'
653 674
654 def __init__(self, xmlui, value=None, name=None, parent=None): 675 def __init__(self, xmlui, value=None, name=None, parent=None, read_only=False):
655 super(StringWidget, self).__init__(xmlui, name, parent) 676 super(StringWidget, self).__init__(xmlui, name, parent, read_only=read_only)
656 if value: 677 if value:
657 self.elem.setAttribute('value', value) 678 self.elem.setAttribute('value', value)
658 679
659 680
660 class PasswordWidget(StringWidget): 681 class PasswordWidget(StringWidget):
663 684
664 class TextBoxWidget(StringWidget): 685 class TextBoxWidget(StringWidget):
665 type = 'textbox' 686 type = 'textbox'
666 687
667 688
689 class JidInputWidget(StringWidget):
690 type = 'jid_input'
691
692
668 class BoolWidget(InputWidget): 693 class BoolWidget(InputWidget):
669 type = 'bool' 694 type = 'bool'
670 695
671 def __init__(self, xmlui, value='false', name=None, parent=None): 696 def __init__(self, xmlui, value='false', name=None, parent=None, read_only=False):
672 if isinstance(value, bool): 697 if isinstance(value, bool):
673 value = 'true' if value else 'false' 698 value = 'true' if value else 'false'
674 elif value == '0': 699 elif value == '0':
675 value='false' 700 value='false'
676 elif value == '1': 701 elif value == '1':
677 value='true' 702 value='true'
678 if not value in ('true', 'false'): 703 if not value in ('true', 'false'):
679 raise exceptions.DataError(_("Value must be 0, 1, false or true")) 704 raise exceptions.DataError(_("Value must be 0, 1, false or true"))
680 super(BoolWidget, self).__init__(xmlui, name, parent) 705 super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
681 self.elem.setAttribute('value', value) 706 self.elem.setAttribute('value', value)
682 707
683 708
684 class ButtonWidget(Widget): 709 class ButtonWidget(Widget):
685 type = 'button' 710 type = 'button'