aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/contact_details/settings_provider.vala
blob: 42c690e5bb93c07d001053c47d7f1c4486276330 (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
using Gtk;

using Dino.Entities;

namespace Dino.Ui.ContactDetails {

public class SettingsProvider : Plugins.ContactDetailsProvider, Object {
    public string id { get { return "chat_settings"; } }

    private StreamInteractor stream_interactor;

    private string DETAILS_HEADLINE_CHAT = _("Settings");
    private string DETAILS_HEADLINE_ROOM = _("Local Settings");

    public SettingsProvider(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
    }

    public void populate(Conversation conversation, Plugins.ContactDetails contact_details, Plugins.WidgetType type) {
        if (type != Plugins.WidgetType.GTK4) return;

        if (!stream_interactor.get<MucManager>().is_public_room(conversation.account, conversation.counterpart)) {
            string details_headline = conversation.type_ == Conversation.Type.GROUPCHAT ? DETAILS_HEADLINE_ROOM : DETAILS_HEADLINE_CHAT;

            ComboBoxText combobox_typing = get_combobox(Dino.Application.get_default().settings.send_typing);
            combobox_typing.active_id = get_setting_id(conversation.send_typing);
            combobox_typing.changed.connect(() => { conversation.send_typing = get_setting(combobox_typing.active_id); } );
            contact_details.add(details_headline, _("Send typing notifications"), "", combobox_typing);
        }

        if (conversation.type_ == Conversation.Type.CHAT) {
            ComboBoxText combobox_marker = get_combobox(Dino.Application.get_default().settings.send_marker);
            contact_details.add(DETAILS_HEADLINE_CHAT, _("Send read receipts"), "", combobox_marker);
            combobox_marker.active_id = get_setting_id(conversation.send_marker);
            combobox_marker.changed.connect(() => { conversation.send_marker = get_setting(combobox_marker.active_id); } );

            ComboBoxText combobox_notifications = get_combobox(Dino.Application.get_default().settings.notifications);
            contact_details.add(DETAILS_HEADLINE_CHAT, _("Notifications"), "", combobox_notifications);
            combobox_notifications.active_id = get_notify_setting_id(conversation.notify_setting);
            combobox_notifications.changed.connect(() => { conversation.notify_setting = get_notify_setting(combobox_notifications.active_id); } );
        } else if (conversation.type_ == Conversation.Type.GROUPCHAT) {
            ComboBoxText combobox = new ComboBoxText() { visible=true };
            combobox.append("default", get_notify_setting_string(Conversation.NotifySetting.DEFAULT, conversation.get_notification_default_setting(stream_interactor)));
            combobox.append("highlight", get_notify_setting_string(Conversation.NotifySetting.HIGHLIGHT));
            combobox.append("on", get_notify_setting_string(Conversation.NotifySetting.ON));
            combobox.append("off", get_notify_setting_string(Conversation.NotifySetting.OFF));
            contact_details.add(DETAILS_HEADLINE_ROOM, _("Notifications"), "", combobox);

            combobox.active_id = get_notify_setting_id(conversation.notify_setting);
            combobox.changed.connect(() => { conversation.notify_setting = get_notify_setting(combobox.active_id); } );
        }
    }

    private Conversation.Setting get_setting(string id) {
        switch (id) {
            case "default":
                return Conversation.Setting.DEFAULT;
            case "on":
                return Conversation.Setting.ON;
            case "off":
                return Conversation.Setting.OFF;
        }
        assert_not_reached();
    }

    private Conversation.NotifySetting get_notify_setting(string id) {
        switch (id) {
            case "default":
                return Conversation.NotifySetting.DEFAULT;
            case "on":
                return Conversation.NotifySetting.ON;
            case "off":
                return Conversation.NotifySetting.OFF;
            case "highlight":
                return Conversation.NotifySetting.HIGHLIGHT;
        }
        assert_not_reached();
    }

    private string get_notify_setting_string(Conversation.NotifySetting setting, Conversation.NotifySetting? default_setting = null) {
        switch (setting) {
            case Conversation.NotifySetting.ON:
                return _("On");
            case Conversation.NotifySetting.OFF:
                return _("Off");
            case Conversation.NotifySetting.HIGHLIGHT:
                return _("Only when mentioned");
            case Conversation.NotifySetting.DEFAULT:
                return _("Default: %s").printf(get_notify_setting_string(default_setting));
        }
        assert_not_reached();
    }

    private string get_setting_id(Conversation.Setting setting) {
        switch (setting) {
            case Conversation.Setting.DEFAULT:
                return "default";
            case Conversation.Setting.ON:
                return "on";
            case Conversation.Setting.OFF:
                return "off";
        }
        assert_not_reached();
    }

    private string get_notify_setting_id(Conversation.NotifySetting setting) {
        switch (setting) {
            case Conversation.NotifySetting.DEFAULT:
                return "default";
            case Conversation.NotifySetting.ON:
                return "on";
            case Conversation.NotifySetting.OFF:
                return "off";
            case Conversation.NotifySetting.HIGHLIGHT:
                return "highlight";
        }
        assert_not_reached();
    }

    private ComboBoxText get_combobox(bool default_val) {
        ComboBoxText combobox = new ComboBoxText();
        combobox = new ComboBoxText() { visible=true };
        string default_setting = default_val ? _("On") : _("Off");
        combobox.append("default", _("Default: %s").printf(default_setting) );
        combobox.append("on", _("On"));
        combobox.append("off", _("Off"));
        return combobox;
    }
}

}