aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/util/data_forms.vala
blob: 53439149b2b0c49c71f0dc45f8ae27f64aff8887 (plain) (blame)
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
using Gee;
using Gtk;

using Dino.Entities;
using Xmpp.Xep;

namespace Dino.Ui.Util {

public static Widget? get_data_form_field_widget(DataForms.DataForm.Field field) {
    if (field.type_ == null) return null;
    switch (field.type_) {
        case DataForms.DataForm.Type.BOOLEAN:
            DataForms.DataForm.BooleanField boolean_field = field as DataForms.DataForm.BooleanField;
            Switch sw = new Switch() { active=boolean_field.value, halign=Align.START, valign=Align.CENTER, visible=true };
            sw.state_set.connect((state) => {
                boolean_field.value = state;
                return false;
            });
            return sw;
        case DataForms.DataForm.Type.JID_MULTI:
            return null;
        case DataForms.DataForm.Type.LIST_SINGLE:
            DataForms.DataForm.ListSingleField list_single_field = field as DataForms.DataForm.ListSingleField;
            ComboBoxText combobox = new ComboBoxText() { valign=Align.CENTER, visible=true };
            for (int i = 0; i < list_single_field.options.size; i++) {
                DataForms.DataForm.Option option = list_single_field.options[i];
                combobox.append(option.value, option.label);
                if (option.value == list_single_field.value) combobox.active = i;
            }
            combobox.changed.connect(() => {
                list_single_field.value = combobox.get_active_id();
            });
            return combobox;
        case DataForms.DataForm.Type.LIST_MULTI:
            return null;
        case DataForms.DataForm.Type.TEXT_PRIVATE:
            DataForms.DataForm.TextPrivateField text_private_field = field as DataForms.DataForm.TextPrivateField;
            Entry entry = new Entry() { text=text_private_field.value ?? "", valign=Align.CENTER, visible=true, visibility=false };
            var entry_key_events = new EventControllerKey();
            entry_key_events.key_released.connect(() => {
                text_private_field.value = entry.text;
            });
            entry.add_controller(entry_key_events);
            return entry;
        case DataForms.DataForm.Type.TEXT_SINGLE:
            DataForms.DataForm.TextSingleField text_single_field = field as DataForms.DataForm.TextSingleField;
            Entry entry = new Entry() { text=text_single_field.value ?? "", valign=Align.CENTER, visible=true };
            var entry_key_events = new EventControllerKey();
            entry_key_events.key_released.connect(() => {
                text_single_field.value = entry.text;
            });
            entry.add_controller(entry_key_events);
            return entry;
        default:
            return null;
    }
}

}