This file is indexed.

/usr/share/pyshared/tryton/gui/window/view_form/view/form_gtk/parser.py is in tryton-client 2.2.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import gtk
import gettext
import copy
import tryton.rpc as rpc
from tryton.action import Action
from tryton.gui.window.view_form.view.interface import ParserInterface
import tryton.common as common
from tryton.config import CONFIG, TRYTON_ICON
from tryton.gui.main import Main
import logging
from tryton.exceptions import TrytonServerError

_ = gettext.gettext


class Button(object):

    def __init__(self, attrs=None):
        super(Button, self).__init__()
        self.attrs = attrs or {}
        self.widget = gtk.Button()
        text = attrs.get('string', _('Unknown'))
        if '_' not in text:
            text = '_' + text
        self.widget.set_use_underline(True)
        self.widget.set_label(text)
        if attrs.get('icon', False):
            try:
                stock = attrs['icon']
                common.ICONFACTORY.register_icon(stock)
                icon = gtk.Image()
                icon.set_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR)
                self.widget.set_image(icon)
            except KeyError:
                log = logging.getLogger('common')
                log.warning(_('Wrong icon for the button!'))
        self.widget.connect('clicked', self.button_clicked)
        self.form = None #fill later by ViewForm

    def button_clicked(self, widget):
        if not self.form:
            return
        record = self.form.screen.current_record
        obj_id = self.form.screen.save_current()
        if obj_id:
            if not self.attrs.get('confirm', False) or \
                    common.sur(self.attrs['confirm']):
                button_type = self.attrs.get('type', 'workflow')
                ctx = rpc.CONTEXT.copy()
                ctx.update(record.context_get())
                if button_type == 'workflow':
                    args = ('model', self.form.screen.model_name,
                            'workflow_trigger_validate', obj_id,
                            self.attrs['name'], ctx)
                    try:
                        rpc.execute(*args)
                    except TrytonServerError, exception:
                        common.process_exception(exception, *args)
                elif button_type == 'object':
                    args = ('model', self.form.screen.model_name, self.attrs['name'],
                            [obj_id], ctx)
                    try:
                        rpc.execute(*args)
                    except TrytonServerError, exception:
                        common.process_exception(exception, *args)
                elif button_type == 'action':
                    action_id = None
                    args = ('model', 'ir.action', 'get_action_id',
                            int(self.attrs['name']), ctx)
                    try:
                        action_id = rpc.execute(*args)
                    except TrytonServerError, exception:
                        action_id = common.process_exception(exception, *args)
                    if action_id:
                        Action.execute(action_id, {
                            'model': self.form.screen.model_name,
                            'id': obj_id,
                            'ids': [obj_id],
                            }, context=ctx)
                else:
                    raise Exception('Unallowed button type')
                self.form.screen.reload(written=True)
        else:
            self.form.screen.display()

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', False):
            self.widget.hide()
        else:
            self.widget.show()
        self.widget.set_sensitive(not state_changes.get('readonly', False))
        if 'icon' in state_changes:
            stock = state_changes['icon']
            if stock:
                common.ICONFACTORY.register_icon(stock)
                icon = gtk.Image()
                icon.set_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR)
                self.widget.set_image(icon)
            else:
                self.widget.set_image(gtk.Image())


class Label(gtk.Label):

    def __init__(self, str=None, attrs=None):
        super(Label, self).__init__(str=str)
        self.attrs = attrs or {}

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', self.attrs.get('invisible')):
            self.hide()
        else:
            self.show()


class VBox(gtk.VBox):

    def __init__(self, homogeneous=False, spacing=0, attrs=None):
        super(VBox, self).__init__(homogeneous, spacing)
        self.attrs = attrs or {}

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', self.attrs.get('invisible')):
            self.hide()
        else:
            self.show()


class Image(gtk.Image):

    def __init__(self, attrs=None):
        super(Image, self).__init__()
        self.attrs = attrs or {}

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', self.attrs.get('invisible')):
            self.hide()
        else:
            self.show()
        state_changes = self.attrs.get('states', {})


class Frame(gtk.Frame):

    def __init__(self, label=None, attrs=None):
        if not label: # label must be None to have no label widget
            label = None
        super(Frame, self).__init__(label=label)
        self.attrs = attrs or {}
        if not label:
            self.set_shadow_type(gtk.SHADOW_NONE)
        self.set_border_width(0)

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', self.attrs.get('invisible')):
            self.hide()
        else:
            self.show()


class ScrolledWindow(gtk.ScrolledWindow):

    def __init__(self, hadjustment=None, vadjustment=None, attrs=None):
        super(ScrolledWindow, self).__init__(hadjustment=hadjustment,
                vadjustment=vadjustment)
        self.attrs = attrs or {}

    def state_set(self, record):
        if record:
            state_changes = record.expr_eval(self.attrs.get('states', {}),
                    check_load=False)
        else:
            state_changes = {}
        if state_changes.get('invisible', self.attrs.get('invisible')):
            self.hide()
        else:
            self.show()


class Alignment(gtk.Alignment):

    def __init__(self, widget, attrs):
        super(Alignment, self).__init__(
            float(attrs.get('xalign', 0.0)),
            float(attrs.get('yalign', 0.5)),
            abs(1 - float(attrs.get('xalign', 0.0))),
            abs(1 - float(attrs.get('yalign', 0.0))))
        self.add(widget)
        widget.connect('show', lambda *a: self.show())
        widget.connect('hide', lambda *a: self.hide())


class _container(object):
    def __init__(self, tooltips):
        self.cont = []
        self.col = []
        self.tooltips = tooltips
        self.trans_box = []

    def new(self, col=4):
        table = gtk.Table(1, col)
        table.set_homogeneous(False)
        table.set_col_spacings(0)
        table.set_row_spacings(0)
        table.set_border_width(0)
        self.cont.append( (table, 0, 0) )
        self.col.append( col )

    def get(self):
        return self.cont[-1][0]

    def pop(self):
        table = self.cont.pop()[0]
        self.col.pop()
        return table

    def newline(self):
        (table, width, height) = self.cont[-1]
        if width > 0:
            self.cont[-1] = (table, 0, height + 1)
        table.resize(height + 1, self.col[-1])

    def wid_add(self, widget, name='', yexpand=False, ypadding=2, rowspan=1,
            colspan=1, translate=False, fname=None, help_tip=False, yfill=False,
            xexpand=True, xfill=True, xpadding=3):
        (table, width, height) = self.cont[-1]
        if colspan > self.col[-1]:
            colspan = self.col[-1]
        if colspan + width > self.col[-1]:
            self.newline()
            (table, width, height) = self.cont[-1]
        yopt = False
        if yexpand:
            yopt = yopt | gtk.EXPAND
        if yfill:
            yopt = yopt | gtk.FILL
        xopt = False
        if xexpand:
            xopt = xopt | gtk.EXPAND
        if xfill:
            xopt = xopt | gtk.FILL
        if help_tip:
            self.tooltips.set_tip(widget, help_tip)
            self.tooltips.enable()
        if translate:
            button = gtk.Button()
            img = gtk.Image()
            img.set_from_stock('tryton-locale', gtk.ICON_SIZE_SMALL_TOOLBAR)
            button.set_image(img)
            button.set_relief(gtk.RELIEF_NONE)
            self.trans_box.append((button, name, fname, widget.get_children()[0]))
            widget.get_child().pack_start(button, fill=False, expand=False)
        widget.show_all()
        table.attach(widget, width, width + colspan,
                height, height + rowspan,
                yoptions=yopt, ypadding=ypadding,
                xoptions=xopt, xpadding=xpadding)
        self.cont[-1] = (table, width + colspan, height)
        wid_list = table.get_children()
        wid_list.reverse()
        table.set_focus_chain(wid_list)

    def empty_add(self, colspan=1):
        (table, width, height) = self.cont[-1]
        if colspan > self.col[-1]:
            colspan = self.col[-1]
        if colspan + width > self.col[-1]:
            self.newline()
            (table, width, height) = self.cont[-1]
        self.cont[-1] = (table, width + colspan, height)


class ParserForm(ParserInterface):

    def __init__(self, parent=None, attrs=None, screen=None,
            children_field=None):
        super(ParserForm, self).__init__(parent=parent, attrs=attrs,
            screen=screen, children_field=children_field)
        self.widget_id = 0

    def parse(self, model_name, root_node, fields, notebook=None, paned=None,
            tooltips=None):
        dict_widget = {}
        button_list = []
        notebook_list = []
        attrs = common.node_attributes(root_node)
        on_write = attrs.get('on_write', '')
        if not tooltips:
            tooltips = common.Tooltips()
        container = _container(tooltips)
        if CONFIG['client.modepda']:
            container.new(col=1)
        else:
            container.new(col=int(attrs.get('col', 4)))
        cursor_widget = attrs.get('cursor')

        if not self.title:
            self.title = attrs.get('string', 'Unknown')

        for node in root_node.childNodes:
            if not node.nodeType == node.ELEMENT_NODE:
                continue
            attrs = common.node_attributes(node)
            if not cursor_widget:
                if attrs.get('name') and fields.get(attrs['name']) \
                        and attrs['name'] != self.screen.exclude_field:
                    cursor_widget = attrs.get('name')
            yexpand = int(attrs.get('yexpand', 0))
            yfill = int(attrs.get('yfill', 0))
            xexpand = int(attrs.get('xexpand', 1))
            xfill = int(attrs.get('xfill', 1))
            colspan = int(attrs.get('colspan', 1))
            if node.localName == 'image':
                common.ICONFACTORY.register_icon(attrs['name'])
                icon = Image(attrs)
                button_list.append(icon)
                icon.set_from_stock(attrs['name'], gtk.ICON_SIZE_DIALOG)
                container.wid_add(icon,
                    help_tip=attrs.get('help', False),
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill, ypadding=10,
                    xexpand=xexpand, xfill=xfill)
            elif node.localName == 'separator':
                text = attrs.get('string', '')
                if 'name' in attrs:
                    for attr_name in ('states', 'invisible'):
                        if attr_name not in attrs and attrs['name'] in fields:
                            if attr_name in fields[attrs['name']].attrs:
                                attrs[attr_name] = fields[attrs['name']
                                        ].attrs[attr_name]
                    if not text:
                        text = fields[attrs['name']].attrs['string']
                vbox = VBox(attrs=attrs)
                button_list.append(vbox)
                if text:
                    label = gtk.Label(text)
                    label.set_use_markup(True)
                    label.set_alignment(float(attrs.get('xalign', 0.0)), 0.5)
                    vbox.pack_start(label)
                vbox.pack_start(gtk.HSeparator())
                container.wid_add(vbox,
                    help_tip=attrs.get('help', False),
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill, ypadding=10,
                    xexpand=xexpand, xfill=xfill)
            elif node.localName == 'label':
                text = attrs.get('string', '')
                if 'name' in attrs and attrs['name'] in fields:
                    if attrs['name'] == self.screen.exclude_field:
                        container.empty_add(int(attrs.get('colspan', 1)))
                        continue
                    for attr_name in ('states', 'invisible'):
                        if attr_name not in attrs \
                                and attr_name in fields[attrs['name']].attrs:
                            attrs[attr_name] = fields[attrs['name']
                                    ].attrs[attr_name]
                    if not text:
                        if gtk.widget_get_default_direction() == \
                                gtk.TEXT_DIR_RTL:
                            text = _(':') + \
                                    fields[attrs['name']].attrs['string']
                        else:
                            text = fields[attrs['name']].attrs['string'] + \
                                    _(':')
                    if 'xalign' not in attrs:
                        attrs['xalign'] = 1.0
                elif not text:
                    for node in node.childNodes:
                        if node.nodeType == node.TEXT_NODE:
                            text += node.data
                        else:
                            text += node.toxml()
                if not text:
                    container.empty_add(int(attrs.get('colspan', 1)))
                    continue
                label = Label(text, attrs)
                button_list.append(label)
                label.set_use_markup(True)
                if CONFIG['client.modepda']:
                    attrs['xalign'] = 0.0
                label.set_alignment(float(attrs.get('xalign', 1.0)),
                    float(attrs.get('yalign', 0.0)))
                label.set_angle(int(attrs.get('angle', 0)))
                xexpand = bool(attrs.get('xexpand', 0))
                container.wid_add(label,
                    help_tip=attrs.get('help', False),
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill,
                    xexpand=xexpand, xfill=xfill)

            elif node.localName == 'newline':
                container.newline()

            elif node.localName == 'button':
                button = Button(attrs)
                button_list.append(button)
                container.wid_add(button.widget,
                    help_tip=attrs.get('help', False),
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill,
                    xexpand=xexpand, xfill=xfill)

            elif node.localName == 'notebook':
                notebook = gtk.Notebook()
                notebook.set_scrollable(True)
                notebook_list.append(notebook)
                if CONFIG['client.form_tab'] == 'top':
                    pos = gtk.POS_TOP
                elif CONFIG['client.form_tab'] == 'left':
                    pos = gtk.POS_LEFT
                elif CONFIG['client.form_tab'] == 'right':
                    pos = gtk.POS_RIGHT
                elif CONFIG['client.form_tab'] == 'bottom':
                    pos = gtk.POS_BOTTOM
                notebook.set_tab_pos(pos)
                notebook.set_border_width(3)
                colspan = int(attrs.get('colspan', 4))
                yexpand = bool(attrs.get('yexpand', 1))
                yfill = bool(attrs.get('yfill', 1))
                container.wid_add(notebook,
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill,
                    xexpand=xexpand, xfill=xfill)
                widget, widgets, buttons, spam, notebook_list2, cursor_widget2 = \
                        self.parse(model_name, node, fields, notebook,
                                tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                button_list += buttons
                for widget_name, widgets in widgets.items():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)

            elif node.localName == 'page':
                if CONFIG['client.form_tab'] == 'left':
                    angle = 90
                    tab_box  = gtk.VBox(spacing=3)
                    image_pos, image_rotate = ('end',
                        gtk.gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE)
                elif CONFIG['client.form_tab'] == 'right':
                    angle = -90
                    tab_box = gtk.VBox(spacing=3)
                    image_pos, image_rotate = ('start',
                        gtk.gdk.PIXBUF_ROTATE_CLOCKWISE)
                else:
                    angle = 0
                    tab_box = gtk.HBox(spacing=3)
                    image_pos, image_rotate = ('start',
                        gtk.gdk.PIXBUF_ROTATE_NONE)
                text = attrs.get('string', '')
                if 'name' in attrs and attrs['name'] in fields:
                    if attrs['name'] == self.screen.exclude_field:
                        continue
                    for attr_name in ('states', 'invisible'):
                        if attr_name in fields[attrs['name']].attrs:
                            attrs[attr_name] = \
                                    fields[attrs['name']].attrs[attr_name]
                    if not text:
                        text = fields[attrs['name']].attrs['string']
                if not text:
                    text = _('No String Attr.')
                if '_' not in text:
                    text = '_' + text
                tab_label = gtk.Label(text)
                tab_label.set_angle(angle)
                tab_label.set_use_underline(True)
                tab_box.pack_start(tab_label)
                if 'icon' in attrs:
                    common.ICONFACTORY.register_icon(attrs['icon'])
                    pixbuf = tab_box.render_icon(attrs['icon'],
                        gtk.ICON_SIZE_SMALL_TOOLBAR)
                    pixbuf = pixbuf.rotate_simple(image_rotate)
                    icon = gtk.Image()
                    icon.set_from_pixbuf(pixbuf)
                    if image_pos == 'end':
                        tab_box.pack_end(icon)
                    else:
                        tab_box.pack_start(icon)
                tab_box.show_all()
                widget, widgets, buttons, spam, notebook_list2, cursor_widget2 = \
                        self.parse(model_name, node, fields, notebook,
                                tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                button_list += buttons
                for widget_name, widgets in widgets.iteritems():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)

                viewport = gtk.Viewport()
                viewport.set_shadow_type(gtk.SHADOW_NONE)
                viewport.add(widget)
                viewport.show()
                scrolledwindow = ScrolledWindow(attrs=attrs)
                scrolledwindow.set_shadow_type(gtk.SHADOW_NONE)
                scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
                        gtk.POLICY_AUTOMATIC)
                scrolledwindow.add(viewport)

                button_list.append(scrolledwindow)
                notebook.append_page(scrolledwindow, tab_box)

            elif node.localName == 'field':
                name = str(attrs['name'])
                if name == self.screen.exclude_field:
                    container.empty_add(int(attrs.get('colspan', 1)))
                    continue
                if name not in fields:
                    container.empty_add(int(attrs.get('colspan', 1)))
                    log = logging.getLogger('view')
                    log.error('Unknown field "%s"' % str(name))
                    continue
                ftype = attrs.get('widget', fields[name].attrs['type'])
                if not ftype in WIDGETS_TYPE:
                    container.empty_add(int(attrs.get('colspan', 1)))
                    continue
                for attr_name in ('relation', 'domain', 'selection',
                        'relation_field', 'string', 'views', 'invisible',
                        'add_remove', 'sort', 'context', 'size', 'filename',
                        'autocomplete'):
                    if attr_name in fields[name].attrs and \
                            not attr_name in attrs:
                        attrs[attr_name] = fields[name].attrs[attr_name]

                widget_act = WIDGETS_TYPE[ftype][0](name, model_name, attrs)
                self.widget_id += 1
                widget_act.position = self.widget_id
                dict_widget.setdefault(name, [])
                dict_widget[name].append(widget_act)
                yexpand = bool(attrs.get('yexpand', WIDGETS_TYPE[ftype][2]))
                yfill = bool(attrs.get('yfill', WIDGETS_TYPE[ftype][3]))
                hlp = fields[name].attrs.get('help', attrs.get('help', False))
                if attrs.get('height', False) or attrs.get('width', False):
                    widget_act.widget.set_size_request(
                            int(attrs.get('width', -1)),
                            int(attrs.get('height', -1)))
                translate = False
                if ftype in ('char', 'text'):
                    translate = fields[name].attrs.get('translate', False)
                container.wid_add(Alignment(widget_act.widget, attrs),
                    fields[name].attrs['string'], fname=name,
                    help_tip=hlp,
                    translate=translate,
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill,
                    xexpand=xexpand, xfill=xfill)

            elif node.localName == 'group':
                widget, widgets, buttons, spam, notebook_list2, cursor_widget2 = \
                        self.parse(model_name, node, fields, tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                for widget_name, widgets in widgets.iteritems():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)
                button_list += buttons
                text = ''
                if 'name' in attrs and attrs['name'] in fields:
                    if attrs['name'] == self.screen.exclude_field:
                        container.empty_add(int(attrs.get('colspan', 1)))
                        continue
                    for attr_name in ('states', 'invisible'):
                        if attr_name in fields[attrs['name']].attrs:
                            attrs[attr_name] = fields[attrs['name']
                                    ].attrs[attr_name]
                    text = fields[attrs['name']].attrs['string']
                if attrs.get('string'):
                    text = attrs['string']

                frame = Frame(text, attrs)
                frame.add(widget)
                button_list.append(frame)
                container.wid_add(frame,
                    colspan=colspan,
                    yexpand=yexpand, yfill=yfill, ypadding=0,
                    xexpand=xexpand, xfill=xfill,  xpadding=0)
            elif node.localName == 'hpaned':
                hpaned = gtk.HPaned()
                container.wid_add(hpaned, colspan=int(attrs.get('colspan', 4)),
                        yexpand=True, yfill=True)
                widget, widgets, buttons, spam, notebook_list2, cursor_widget2 = \
                        self.parse(model_name, node, fields, paned=hpaned,
                                tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                button_list += buttons
                for widget_name, widgets in widgets.iteritems():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)
                if 'position' in attrs:
                    hpaned.set_position(int(attrs['position']))
            elif node.localName == 'vpaned':
                vpaned = gtk.VPaned()
                container.wid_add(vpaned, colspan=int(attrs.get('colspan', 4)),
                        yexpand=True, yfill=True)
                widget, widgets, buttons, spam, notebook_list, cursor_widget2 = \
                        self.parse(model_name, node, fields, paned=vpaned,
                                tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                button_list += buttons
                for widget_name, widgets in widgets.iteritems():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)
                if 'position' in attrs:
                    vpaned.set_position(int(attrs['position']))
            elif node.localName == 'child':
                widget, widgets, buttons, spam, notebook_list2, cursor_widget2 = \
                        self.parse(model_name, node, fields, paned=paned,
                                tooltips=tooltips)
                if not cursor_widget:
                    cursor_widget = cursor_widget2
                notebook_list.extend(notebook_list2)
                button_list += buttons
                for widget_name, widgets in widgets.iteritems():
                    dict_widget.setdefault(widget_name, [])
                    dict_widget[widget_name].extend(widgets)
                if not paned.get_child1():
                    paned.pack1(widget, resize=True, shrink=True)
                elif not paned.get_child2():
                    paned.pack2(widget, resize=True, shrink=True)
        for (button, src, name, widget) in container.trans_box:
            button.connect('clicked', self.translate, model_name, name,
                    src, widget)
        return container.pop(), dict_widget, button_list, on_write, \
                notebook_list, cursor_widget

    def translate(self, widget, model_name, name, src, widget_entry):
        obj_id = self.screen.current_record.id
        if obj_id < 0:
            common.message(
                    _('You need to save the record before adding translations!'))
            return False

        obj_id = self.screen.current_record.save(force_reload=False)
        try:
            lang_ids = rpc.execute('model', 'ir.lang',
                    'search', [('translatable', '=', '1')],
                    rpc.CONTEXT)
        except TrytonServerError, exception:
            common.process_exception(exception)
            return False

        if not lang_ids:
            common.message(_('No other language available!'))
            return False
        try:
            lang_ids += rpc.execute('model', 'ir.lang',
                    'search', [('code', '=', 'en_US')],
                    rpc.CONTEXT)
            langs = rpc.execute('model', 'ir.lang',
                    'read', lang_ids, ['code', 'name'],
                    rpc.CONTEXT)
        except TrytonServerError, exception:
            common.process_exception(exception)
            return False

        code = rpc.CONTEXT.get('language', 'en_US')

        widget_entry = widget_entry.get_children()[0]
        if isinstance(widget_entry, gtk.ScrolledWindow):
            widget_entry = widget_entry.get_child()
        #widget accessor functions
        def value_get(widget):
            if isinstance(widget, gtk.Entry):
                return widget.get_text()
            elif isinstance(widget, gtk.TextView):
                buf = widget.get_buffer()
                iter_start = buf.get_start_iter()
                iter_end = buf.get_end_iter()
                return buf.get_text(iter_start, iter_end, False)
            else:
                return None

        def value_set(widget, value):
            if not value:
                value = ''
            if isinstance(widget, gtk.Entry):
                widget.set_text(value)
            elif isinstance(widget, gtk.TextView):
                buf = widget.get_buffer()
                buf.delete(buf.get_start_iter(), buf.get_end_iter())
                iter_start = buf.get_start_iter()
                buf.insert(iter_start, value)

        def widget_duplicate(widget):
            if isinstance(widget, gtk.Entry):
                entry = gtk.Entry()
                entry.set_property('activates_default', True)
                entry.set_max_length(widget.get_max_length())
                entry.set_width_chars(widget.get_width_chars())
                return entry, gtk.FILL
            elif isinstance(widget, gtk.TextView):
                textview = gtk.TextView()
                textview.set_wrap_mode(gtk.WRAP_WORD)
                scrolledwindow = gtk.ScrolledWindow()
                scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
                        gtk.POLICY_ALWAYS)
                scrolledwindow.set_shadow_type(gtk.SHADOW_NONE)
                scrolledwindow.set_size_request(-1, 80)
                scrolledwindow.add(textview)
                textview.set_accepts_tab(False)
                return scrolledwindow, gtk.FILL | gtk.EXPAND
            else:
                return None, False


        parent = common.get_toplevel_window()
        win = gtk.Dialog(_('Add Translation'),parent,
                gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
        win.set_has_separator(True)
        win.vbox.set_spacing(5)
        win.set_property('default-width', 600)
        win.set_property('default-height', 400)
        win.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        win.set_icon(TRYTON_ICON)

        accel_group = gtk.AccelGroup()
        win.add_accel_group(accel_group)

        but_cancel = win.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)

        but_ok = win.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        but_ok.add_accelerator('clicked', accel_group, gtk.keysyms.Return,
                gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)

        vbox = gtk.VBox(spacing=5)

        entries_list = []
        table = gtk.Table(len(langs), 2)
        table.set_homogeneous(False)
        table.set_col_spacings(3)
        table.set_row_spacings(0)
        table.set_border_width(1)
        i = 0
        for lang in langs:
            context = copy.copy(rpc.CONTEXT)
            context['language'] = lang['code']
            try:
                val = rpc.execute('model', model_name,
                        'read', [obj_id], [name], context)
            except TrytonServerError, exception:
                common.process_exception(exception)
                return False
            val = val[0]
            if gtk.widget_get_default_direction() == gtk.TEXT_DIR_RTL:
                label = gtk.Label(_(':') + lang['name'])
            else:
                label = gtk.Label(lang['name'] + _(':'))
            label.set_alignment(1.0, 0.5)
            (entry, yoptions) = widget_duplicate(widget_entry)

            hbox = gtk.HBox(homogeneous=False)
            if code == lang['code']:
                value_set(entry, value_get(widget_entry))
            else:
                value_set(entry, val[name])

            entries_list.append((val['id'], lang['code'], entry))
            table.attach(label, 0, 1, i, i+1, yoptions=False, xoptions=gtk.FILL,
                    ypadding=2, xpadding=3)
            table.attach(entry, 1, 2, i, i+1, yoptions=yoptions,
                    ypadding=2, xpadding=3)
            i += 1

        vbox.pack_start(table)
        viewport = gtk.Viewport()
        viewport.set_shadow_type(gtk.SHADOW_NONE)
        viewport.add(vbox)
        scrolledwindow = gtk.ScrolledWindow()
        scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,
                gtk.POLICY_AUTOMATIC)
        scrolledwindow.set_shadow_type(gtk.SHADOW_NONE)
        scrolledwindow.add(viewport)
        win.vbox.add(scrolledwindow)
        win.show_all()

        data = []
        response = win.run()
        if response == gtk.RESPONSE_OK:
            to_save = [(x[0], x[1], value_get(x[2])) for x in entries_list]
            while to_save != []:
                new_val = {}
                new_val['id'], new_val['code'], new_val['value'] = \
                        to_save.pop()
                #update form field
                if new_val['code'] == code:
                    value_set(widget_entry, new_val['value'])
                context = copy.copy(rpc.CONTEXT)
                context['language'] = new_val['code']
                args = ('model', model_name, 'write', [obj_id],
                        {str(name):  new_val['value']}, context)
                try:
                    rpc.execute(*args)
                except TrytonServerError, exception:
                    common.process_exception(exception, *args)
        if response != gtk.RESPONSE_CANCEL:
            self.screen.current_record.reload()
        win.destroy()
        parent.present()
        return response != gtk.RESPONSE_CANCEL

from calendar import Calendar, DateTime
from float import Float
from integer import Integer
from selection import Selection
from char import Char, Sha
from float_time import FloatTime
from checkbox import CheckBox
from reference import Reference
from binary import Binary
from textbox import TextBox
from one2many import One2Many
from many2many import Many2Many
from many2one import Many2One
from url import Email, URL, CallTo, SIP
from image import Image as Image2
from progressbar import ProgressBar
from one2one import One2One


WIDGETS_TYPE = {
    'date': (Calendar, 1, False, False),
    'datetime': (DateTime, 1, False, False),
    'float': (Float, 1, False, False),
    'numeric': (Float, 1, False, False),
    'integer': (Integer, 1, False, False),
    'biginteger': (Integer, 1, False, False),
    'selection': (Selection, 1, False, False),
    'char': (Char, 1, False, False),
    'sha': (Sha, 1, False, False),
    'float_time': (FloatTime, 1, False, False),
    'boolean': (CheckBox, 1, False, False),
    'reference': (Reference, 1, False, False),
    'binary': (Binary, 1, False, False),
    'text': (TextBox, 1, True, True),
    'one2many': (One2Many, 1, True, True),
    'many2many': (Many2Many, 1, True, True),
    'many2one': (Many2One, 1, False, False),
    'email' : (Email, 1, False, False),
    'url' : (URL, 1, False, False),
    'callto' : (CallTo, 1, False, False),
    'sip' : (SIP, 1, False, False),
    'image' : (Image2, 1, False, False),
    'progressbar': (ProgressBar, 1, False, False),
    'one2one': (One2One, 1, False, False),
}