aboutsummaryrefslogtreecommitdiff
path: root/plugins/openpgp/src/account_settings_entry.vala
blob: d2e5ac2385127cffefda6f69063bba341d2a544b (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Dino.Entities;
using Gtk;

namespace Dino.Plugins.OpenPgp {

public class AccountSettingsEntry : Plugins.AccountSettingsEntry {

    private Label label;
    private Button button;
    private ComboBox combobox;
    private Stack stack;

    private Plugin plugin;
    private Account current_account;
    private Gee.List<GPG.Key> keys = null;
    private Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(string?));

    public override string id { get { return "pgp_key_picker"; }}

    public override string name { get { return "OpenPGP"; }}

    public AccountSettingsEntry(Plugin plugin) {
        this.plugin = plugin;

        Builder builder = new Builder.from_resource("/im/dino/Dino/openpgp/account_settings_item.ui");
        stack = (Stack) builder.get_object("stack");
        label = (Label) builder.get_object("label");
        button = (Button) builder.get_object("button");
        combobox = (ComboBox) builder.get_object("combobox");

        CellRendererText renderer = new CellRendererText();
        renderer.set_padding(0, 0);
        combobox.pack_start(renderer, true);
        combobox.add_attribute(renderer, "markup", 0);
        combobox.set_model(list_store);

        button.clicked.connect(on_button_clicked);
        combobox.changed.connect(key_changed);
    }

    public override void deactivate() {
        stack.set_visible_child_name("label");
    }

    public override void set_account(Account account) {
        set_account_.begin(account);
    }

    private async void set_account_(Account account) {
        this.current_account = account;
        if (keys == null) {
            yield fetch_keys();
            populate_list_store();
        }
        activate_current_account();
    }

    private void on_button_clicked() {
        activated();
        stack.set_visible_child_name("entry");
        combobox.grab_focus();
        combobox.popup();
    }

    private void activate_current_account() {
        combobox.changed.disconnect(key_changed);
        if (keys == null) {
            label.set_markup(build_markup_string(_("Key publishing disabled"), _("Error in GnuPG")));
            return;
        }
        if (keys.size == 0) {
            label.set_markup(build_markup_string(_("Key publishing disabled"), _("No keys available. Generate one!")));
            return;
        }

        string? account_key = plugin.db.get_account_key(current_account);
        int activate_index = 0;
        for (int i = 0; i < keys.size; i++) {
            GPG.Key key = keys[i];
            if (key.fpr == account_key) {
                activate_index = i + 1;
            }
        }
        combobox.active = activate_index;

        TreeIter selected;
        combobox.get_active_iter(out selected);
        set_label_active(selected);

        combobox.changed.connect(key_changed);
    }

    private void populate_list_store() {
        if (keys == null || keys.size == 0) {
            return;
        }

        list_store.clear();
        TreeIter iter;
        list_store.append(out iter);
        list_store.set(iter, 0, build_markup_string(_("Key publishing disabled"), _("Select key") + "<span font_family='monospace' font='8'> \n </span>"), 1, "");
        for (int i = 0; i < keys.size; i++) {
            list_store.append(out iter);
            list_store.set(iter, 0, @"$(Markup.escape_text(keys[i].uids[0].uid))\n<span font_family='monospace' font='8'>$(markup_colorize_id(keys[i].fpr, true))</span><span font='8'> </span>");
            list_store.set(iter, 1, keys[i].fpr);
            if (keys[i].fpr == plugin.db.get_account_key(current_account)) {
                set_label_active(iter, i + 1);
            }
        }
        button.sensitive = true;
    }

    private async void fetch_keys() {
        label.set_markup(build_markup_string(_("Loading…"), _("Querying GnuPG")));

        SourceFunc callback = fetch_keys.callback;
        new Thread<void*> (null, () => { // Querying GnuPG might take some time
            try {
            keys = GPGHelper.get_keylist(null, true);
        } catch (Error e) { }
            Idle.add((owned)callback);
            return null;
        });
        yield;
    }

    private void set_label_active(TreeIter iter, int i = -1) {
        Value text;
        list_store.get_value(iter, 0, out text);
        label.set_markup((string) text);
        if (i != -1) combobox.active = i;
    }

    private void key_changed() {
        TreeIter selected;
        bool iter_valid = combobox.get_active_iter(out selected);
        if (iter_valid) {
            Value key_value;
            list_store.get_value(selected, 1, out key_value);
            string? key_id = key_value as string;
            if (key_id != null) {
                if (plugin.modules.has_key(current_account)) {
                    plugin.modules[current_account].set_private_key_id(key_id);
                }
                plugin.db.set_account_key(current_account, key_id);
            }
            set_label_active(selected);
            deactivate();
        }
    }

    private string build_markup_string(string primary, string secondary) {
        return @"$(Markup.escape_text(primary))\n<span font='8'>$secondary</span>";
    }

    public override Object? get_widget(WidgetType type) {
        if (type != WidgetType.GTK4) return null;
        return stack;
    }
}
}