aboutsummaryrefslogtreecommitdiff
path: root/main/src/windows/preferences_window/account_preferences_subpage.vala
blob: d52a8c835877d09ba2d83b85ce934e2085ebb034 (plain)
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
using Dino.Entities;
using Xmpp;
using Xmpp.Xep;
using Gee;
using Gtk;
using Gdk;

[GtkTemplate (ui = "/im/dino/Dino/preferences_window/account_preferences_subpage.ui")]
public class Dino.Ui.AccountPreferencesSubpage : Adw.NavigationPage {

    [GtkChild] public unowned Adw.HeaderBar headerbar;
    [GtkChild] public unowned AvatarPicture avatar;
    [GtkChild] public unowned Adw.ActionRow xmpp_address;
    [GtkChild] public unowned Adw.EntryRow local_alias;
    [GtkChild] public unowned Box account_color_box;
    [GtkChild] public unowned Adw.ActionRow password_change;
    [GtkChild] public unowned Adw.ActionRow connection_status;
    [GtkChild] public unowned Button enter_password_button;
    [GtkChild] public unowned Box avatar_menu_box;
    [GtkChild] public unowned Button edit_avatar_button;
    [GtkChild] public unowned Button remove_avatar_button;
    [GtkChild] public unowned Widget button_container;
    [GtkChild] public unowned Button remove_account_button;
    [GtkChild] public unowned Button disable_account_button;

    public Account account { get { return model.selected_account.account; } }
    public ViewModel.PreferencesDialog model { get; set; }

    private Binding[] bindings = new Binding[0];
    private ulong[] account_notify_ids = new ulong[0];
    private ulong alias_entry_changed = 0;
    private bool updating_account_color = false;
#if GTK_4_12
    private ColorDialogButton account_color_button;
#else
    private ColorButton account_color_button;
#endif

    construct {
        title = "Account";
        headerbar.show_title = false;
        button_container.layout_manager = new NaturalDirectionBoxLayout((BoxLayout)button_container.layout_manager);
        edit_avatar_button.clicked.connect(() => {
            show_select_avatar();
        });
        remove_avatar_button.clicked.connect(() => {
            model.remove_avatar(account);
        });
        disable_account_button.clicked.connect(() => {
            model.enable_disable_account(account);
        });
        remove_account_button.clicked.connect(() => {
            show_remove_account_dialog();
        });

#if GTK_4_12
        var color_dialog = new ColorDialog() {
            title = _("Choose account color"),
            with_alpha = false
        };
        account_color_button = new ColorDialogButton((owned) color_dialog) { valign=Align.CENTER };
#else
        account_color_button = new ColorButton() {
            title = _("Choose account color"),
            use_alpha = false,
            valign = Align.CENTER
        };
#endif
        account_color_button.notify["rgba"].connect(on_account_color_changed);
        account_color_box.append(account_color_button);

        password_change.activatable_widget = new Label("");
        password_change.activated.connect(() => {
            var dialog = new ChangePasswordDialog(model.get_change_password_dialog_model());
            dialog.present((Gtk.Window)this.get_root());
        });
        enter_password_button.clicked.connect(() => {
            var dialog = new Adw.MessageDialog((Window)this.get_root(), "Enter password for %s".printf(account.bare_jid.to_string()), null);
            var password = new PasswordEntry() { show_peek_icon=true };
            dialog.response.connect((response) => {
                if (response == "connect") {
                    account.password = password.text;
                    model.reconnect_account(account);
                }
            });
            dialog.set_default_response("connect");
            dialog.set_extra_child(password);
            dialog.add_response("cancel", _("Cancel"));
            dialog.add_response("connect", _("Connect"));

            dialog.present();
        });

        this.notify["model"].connect(on_model_changed);
    }

    private void on_model_changed() {
        model.notify["selected-account"].connect(refresh);
        refresh();
    }

    private static bool account_enabled_to_disable_button_label(Binding binding, Value from_value, ref Value to_value) {
        bool enabled_bool = (bool) from_value;
        to_value = enabled_bool ? _("Disable account") : _("Enable account");
        return true;
    }

    private static bool connection_status_to_label(Binding binding, Value from_value, ref Value to_value) {
        var account = binding.source as ViewModel.AccountDetails;
        to_value = get_status_label(account);
        return true;
    }

    private static bool connection_error_to_enter_password_visible(Binding binding, Value from_value, ref Value to_value) {
        var error = (ConnectionManager.ConnectionError) from_value;
        to_value = error != null && error.source == ConnectionManager.ConnectionError.Source.SASL;
        return true;
    }

    private static bool image_file_present_to_remove_button_visible(Binding binding, Value from_value, ref Value to_value) {
        File? image_file = (File) from_value;
        to_value = image_file != null;
        return true;
    }

    private void on_connection_error_updated() {
        if (model.selected_account.connection_error != null) {
            connection_status.add_css_class("error");
        } else {
            connection_status.remove_css_class("error");
        }
    }

    private void refresh() {
        foreach (var binding in bindings) {
            binding.unbind();
        }

        avatar.model = model.selected_account.avatar_model;
        xmpp_address.subtitle = account.bare_jid.to_string();

        if (alias_entry_changed != 0) local_alias.disconnect(alias_entry_changed);
        local_alias.text = account.alias ?? "";
        alias_entry_changed = local_alias.changed.connect(() => {
            account.alias = local_alias.text;
        });

        Gdk.RGBA account_color = Gdk.RGBA();
        account_color.parse(Dino.Application.get_default().settings.get_account_color(account));
        updating_account_color = true;
        account_color_button.rgba = account_color;
        updating_account_color = false;

        bindings += account.bind_property("enabled", disable_account_button, "label", BindingFlags.SYNC_CREATE, account_enabled_to_disable_button_label);
        bindings += account.bind_property("enabled", avatar_menu_box, "visible", BindingFlags.SYNC_CREATE);
        bindings += account.bind_property("enabled", password_change, "visible", BindingFlags.SYNC_CREATE);
        bindings += account.bind_property("enabled", connection_status, "visible", BindingFlags.SYNC_CREATE);
        bindings += model.selected_account.bind_property("connection-state", connection_status, "subtitle", BindingFlags.SYNC_CREATE, connection_status_to_label);
        bindings += model.selected_account.bind_property("connection-error", connection_status, "subtitle", BindingFlags.SYNC_CREATE, connection_status_to_label);
        bindings += model.selected_account.bind_property("connection-error", enter_password_button, "visible", BindingFlags.SYNC_CREATE, connection_error_to_enter_password_visible);

        // Only show avatar removal button if an avatar is set
        var avatar_model = model.selected_account.avatar_model.tiles.get_item(0) as ViewModel.AvatarPictureTileModel;
        bindings += avatar_model.bind_property("image-file", remove_avatar_button, "visible", BindingFlags.SYNC_CREATE, image_file_present_to_remove_button_visible);

        model.selected_account.notify["connection-error"].connect(on_connection_error_updated);
        on_connection_error_updated();
    }

    private void on_account_color_changed() {
        if (updating_account_color) return;

#if GTK_4_12
        unowned Gdk.RGBA? selected_color = account_color_button.get_rgba();
        if (selected_color != null) {
            Dino.Application.get_default().settings.set_account_color(account, Util.rgba_to_hex(selected_color));
        }
#else
        Dino.Application.get_default().settings.set_account_color(account, Util.rgba_to_hex(account_color_button.rgba));
#endif
    }

    private void show_select_avatar() {
        FileChooserNative chooser = new FileChooserNative(_("Select avatar"), (Window)this.get_root(), FileChooserAction.OPEN, _("Select"), _("Cancel"));
        FileFilter filter = new FileFilter();
        foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) {
            foreach (string mime_type in pixbuf_format.get_mime_types()) {
                filter.add_mime_type(mime_type);
            }
        }
        filter.set_filter_name(_("Images"));
        chooser.add_filter(filter);

        filter = new FileFilter();
        filter.set_filter_name(_("All files"));
        filter.add_pattern("*");
        chooser.add_filter(filter);

        chooser.response.connect(() => {
            model.set_avatar_file(account, chooser.get_file());
        });

        chooser.show();
    }

    private void show_remove_account_dialog() {
        Adw.AlertDialog dialog = new Adw.AlertDialog (
                _("Remove account %s?".printf(account.bare_jid.to_string())),
                "You won't be able to access your conversation history anymore."
        );
        // TODO remove history!
        dialog.add_response("cancel", "Cancel");
        dialog.add_response("remove", "Remove");
        dialog.set_response_appearance("remove", Adw.ResponseAppearance.DESTRUCTIVE);
        dialog.response.connect((response) => {
            if (response == "remove") {
                model.remove_account(account);

                // Close the account subpage. Get the parent-times-x PreferencesDialog, to call pop_subpage() on it.
                Widget parent = this.parent;
                for (int i = 0; parent != null && i < 20; i++) {
                    if (parent.get_type() == typeof(Dino.Ui.PreferencesDialog)) {
                        ((Dino.Ui.PreferencesDialog) parent).pop_subpage();
                    }
                    parent = parent.parent;
                }
            }
            dialog.close();
        });
        dialog.present((Window)this.get_root());
    }

    private static string get_status_label(ViewModel.AccountDetails account) {
        ConnectionManager.ConnectionError? error = account.connection_error;
        if (error != null) {
            switch (error.source) {
                case ConnectionManager.ConnectionError.Source.SASL:
                    return _("Wrong password");
                case ConnectionManager.ConnectionError.Source.TLS:
                    return _("Invalid TLS certificate");
            }
            if (error.identifier != null) {
                return _("Error") + ": " + error.identifier;
            } else {
                return _("Error");
            }
        } else {
            ConnectionManager.ConnectionState state = account.connection_state;
            switch (state) {
                case ConnectionManager.ConnectionState.CONNECTING:
                    return _("Connecting…");
                case ConnectionManager.ConnectionState.CONNECTED:
                    return _("Connected");
                case ConnectionManager.ConnectionState.DISCONNECTED:
                    return _("Disconnected");
            }
            assert_not_reached();
        }
    }
}

public class Dino.Ui.NaturalDirectionBoxLayout : LayoutManager {
    private BoxLayout original;
    private BoxLayout alternative;

    public NaturalDirectionBoxLayout(BoxLayout original) {
        this.original = original;
        if (original.orientation == Orientation.HORIZONTAL) {
            this.alternative = new BoxLayout(Orientation.VERTICAL);
            this.alternative.spacing = this.original.spacing / 2;
        }
    }

    public override SizeRequestMode get_request_mode(Widget widget) {
        return original.orientation == Orientation.HORIZONTAL ? SizeRequestMode.HEIGHT_FOR_WIDTH : SizeRequestMode.WIDTH_FOR_HEIGHT;
    }

    public override void allocate(Widget widget, int width, int height, int baseline) {
        int blind_minimum, blind_natural, blind_minimum_baseline, blind_natural_baseline;
        original.measure(widget, original.orientation, -1, out blind_minimum, out blind_natural, out blind_minimum_baseline, out blind_natural_baseline);
        int for_size = (original.orientation == Orientation.HORIZONTAL ? width : height);
        if (for_size >= blind_minimum) {
            original.allocate(widget, width, height, baseline);
        } else {
            alternative.allocate(widget, width, height, baseline);
        }
    }

    public override void measure(Widget widget, Orientation orientation, int for_size, out int minimum, out int natural, out int minimum_baseline, out int natural_baseline) {
        if (for_size == -1) {
            original.measure(widget, orientation, -1, out minimum, out natural, out minimum_baseline, out natural_baseline);
            int alt_minimum, alt_natural, alt_minimum_baseline, alt_natural_baseline;
            alternative.measure(widget, orientation, -1, out alt_minimum, out alt_natural, out alt_minimum_baseline, out alt_natural_baseline);
            if (alt_minimum < minimum && alt_minimum != -1) minimum = alt_minimum;
            if (alt_minimum_baseline < minimum_baseline && alt_minimum_baseline != -1) minimum = alt_minimum_baseline;
        } else {
            Orientation other_orientation = orientation == Orientation.HORIZONTAL ? Orientation.VERTICAL : Orientation.HORIZONTAL;
            int blind_minimum, blind_natural, blind_minimum_baseline, blind_natural_baseline;
            original.measure(widget, other_orientation, -1, out blind_minimum, out blind_natural, out blind_minimum_baseline, out blind_natural_baseline);
            if (for_size >= blind_minimum) {
                original.measure(widget, orientation, for_size, out minimum, out natural, out minimum_baseline, out natural_baseline);
            } else {
                alternative.measure(widget, orientation, for_size, out minimum, out natural, out minimum_baseline, out natural_baseline);
            }
        }
    }
}