aboutsummaryrefslogtreecommitdiff
path: root/main/src/windows/preferences_window/encryption_preferences_page.vala
blob: 2222584d2ee2a864268cb213d562df491ca317ea (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 Dino.Entities;
using Xmpp;
using Xmpp.Xep;
using Gee;
using Gtk;

//[GtkTemplate (ui = "/im/dino/Dino/preferences_window_encryption.ui")]
public class Dino.Ui.PreferencesWindowEncryption : Adw.PreferencesPage {

    private DropDown drop_down = null;
    private Adw.PreferencesGroup accounts_group = new Adw.PreferencesGroup();
    private ArrayList<Adw.PreferencesGroup> added_widgets = new ArrayList<Adw.PreferencesGroup>();

    public ViewModel.PreferencesWindow model { get; set; }

    construct {
        this.add(accounts_group);

        this.notify["model"].connect(() => {
            this.model.update.connect(() => {
                if (drop_down != null) {
                    accounts_group.remove(drop_down);
                    drop_down = null;
                }

                if (model.active_accounts_selection.get_n_items() > 0) {
                    drop_down = new DropDown(model.active_accounts_selection, null) { halign=Align.CENTER };
                    drop_down.factory = new BuilderListItemFactory.from_resource(null, "/im/dino/Dino/account_picker_row.ui");

                    drop_down.notify["selected-item"].connect(() => {
                        var account_details = (ViewModel.AccountDetails) drop_down.selected_item;
                        if (account_details == null) return;
                        set_account(account_details.account);
                    });

                    drop_down.selected = 0;
                    set_account(((ViewModel.AccountDetails)model.active_accounts_selection.get_item(0)).account);
                } else {
                    drop_down = new DropDown.from_strings(new string[] { _("No active accounts")}) { halign=Align.CENTER };
                }
                accounts_group.add(drop_down);
            });
        });
    }

    public void set_account(Account account) {
        foreach (var widget in added_widgets) {
            this.remove(widget);
        }
        added_widgets.clear();

        Application app = GLib.Application.get_default() as Application;
        foreach (Plugins.EncryptionPreferencesEntry e in app.plugin_registry.encryption_preferences_entries) {
            var widget = (Adw.PreferencesGroup) e.get_widget(account, Plugins.WidgetType.GTK4);
            this.add(widget);
            this.added_widgets.add(widget);
        }
    }
}