aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/contact_details/dialog.vala
blob: 4e61a3f5e6fcf55ec23e94fa80f92e8ba904631e (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
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
using Gee;
using Gtk;
using Markup;
using Pango;

using Dino.Entities;

namespace Dino.Ui.ContactDetails {

[GtkTemplate (ui = "/org/dino-im/contact_details_dialog.ui")]
public class Dialog : Gtk.Dialog {

    [GtkChild] public Image avatar;
    [GtkChild] public Util.EntryLabelHybrid name_hybrid;
    [GtkChild] public Label name_label;
    [GtkChild] public Label jid_label;
    [GtkChild] public Label account_label;
    [GtkChild] public Box main_box;

    private StreamInteractor stream_interactor;
    private Conversation conversation;

    private Plugins.ContactDetails contact_details = new Plugins.ContactDetails();
    private HashMap<string, ListBox> categories = new HashMap<string, ListBox>();
    private Util.LabelHybridGroup hybrid_group = new Util.LabelHybridGroup();

    construct {
        name_hybrid.label.attributes = new AttrList();
        name_hybrid.label.attributes.insert(attr_weight_new(Weight.BOLD));
    }

    public Dialog(StreamInteractor stream_interactor, Conversation conversation) {
        Object(use_header_bar : 1);
        this.stream_interactor = stream_interactor;
        this.conversation = conversation;

        title = conversation.type_ == Conversation.Type.GROUPCHAT ? _("Conference Details") : _("Contact Details");
        (get_header_bar() as HeaderBar).set_subtitle(Util.get_conversation_display_name(stream_interactor, conversation));
        setup_top();

        contact_details.add.connect(add_entry);

        Application app = GLib.Application.get_default() as Application;
        app.plugin_registry.register_contact_details_entry(new SettingsProvider(stream_interactor));
        app.plugin_registry.register_contact_details_entry(new MucConfigFormProvider(stream_interactor));

        foreach (Plugins.ContactDetailsProvider provider in app.plugin_registry.contact_details_entries) {
            provider.populate(conversation, contact_details, Plugins.WidgetType.GTK);
        }

        destroy.connect(() => {
            contact_details.save();
        });
    }

    private void setup_top() {
        if (conversation.type_ == Conversation.Type.CHAT) {
            name_label.visible = false;
            jid_label.set_padding(new Button().get_style_context().get_padding(StateFlags.NORMAL).left + 1, 0);
            name_hybrid.text = Util.get_conversation_display_name(stream_interactor, conversation);
            destroy.connect(() => {
                if (name_hybrid.text != Util.get_conversation_display_name(stream_interactor, conversation)) {
                    stream_interactor.get_module(RosterManager.IDENTITY).set_jid_handle(conversation.account, conversation.counterpart, name_hybrid.text);
                }
            });
        } else {
            name_hybrid.visible = false;
            name_label.label = Util.get_conversation_display_name(stream_interactor, conversation);
        }
        jid_label.label = conversation.counterpart.to_string();
        account_label.label = "via " + conversation.account.bare_jid.to_string();
        Util.image_set_from_scaled_pixbuf(avatar, (new AvatarGenerator(50, 50, avatar.scale_factor)).draw_conversation(stream_interactor, conversation));
    }

    private void add_entry(string category, string label, string? description, Object wo) {
        if (!(wo is Widget)) return;
        Widget w = (Widget) wo;
        add_category(category);

        ListBoxRow list_row = new ListBoxRow() { activatable=false, visible=true };
        Box row = new Box(Orientation.HORIZONTAL, 20) { margin_left=15, margin_right=15, margin_top=3, margin_bottom=3, visible=true };
        list_row.add(row);
        Label label_label = new Label(label) { xalign=0, yalign=0.5f, hexpand=true, visible=true };
        if (description != null && description != "") {
            Box box = new Box(Orientation.VERTICAL, 0) { visible=true };
            box.add(label_label);
            Label desc_label = new Label("") { xalign=0, yalign=0.5f, hexpand=true, visible=true };
            desc_label.set_markup("<span size='small'>%s</span>".printf(Markup.escape_text(description)));
            desc_label.get_style_context().add_class("dim-label");
            box.add(desc_label);
            row.add(box);
        } else {
            row.add(label_label);
        }

        Widget widget = w;
        if (widget.get_type().is_a(typeof(Entry))) {
            Util.EntryLabelHybrid hybrid = new Util.EntryLabelHybrid.wrap(widget as Entry) { xalign=1, visible=true };
            hybrid_group.add(hybrid);
            widget = hybrid;
        } else if (widget.get_type().is_a(typeof(ComboBoxText))) {
            Util.ComboBoxTextLabelHybrid hybrid = new Util.ComboBoxTextLabelHybrid.wrap(widget as ComboBoxText) { xalign=1, visible=true };
            hybrid_group.add(hybrid);
            widget = hybrid;
        }
        widget.margin_bottom = 5;
        widget.margin_top = 5;


        row.add(widget);
        categories[category].add(list_row);

        Idle.add(() => {
            int pref_height, pref_width;
            get_content_area().get_preferred_height(null, out pref_height);
            get_preferred_width(out pref_width, null);
            resize(pref_width, int.min(500, pref_height));
            return false;
        });
    }

    private void add_category(string category) {
        if (!categories.has_key(category)) {
            ListBox list_box = new ListBox() { selection_mode=SelectionMode.NONE, visible=true };
            categories[category] = list_box;
            list_box.set_header_func((row, before_row) => {
                if (row.get_header() == null && before_row != null) {
                    row.set_header(new Separator(Orientation.HORIZONTAL));
                }
            });
            Box box = new Box(Orientation.VERTICAL, 5) { margin_top=12, margin_bottom=12, visible=true };
            Label category_label = new Label("") { xalign=0, visible=true };
            category_label.set_markup(@"<b>$(Markup.escape_text(category))</b>");
            box.add(category_label);
            Frame frame = new Frame(null) { visible=true };
            frame.add(list_box);
            box.add(frame);
            main_box.add(box);
        }
    }
}

}