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
|
using Gee;
using Gtk;
using Dino.Entities;
namespace Dino.Plugins.OpenPgp {
[GtkTemplate (ui = "/org/dino-im/account_settings_item.ui")]
private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget {
[GtkChild] private Label label;
[GtkChild] private Button button;
[GtkChild] private ComboBox combobox;
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 AccountSettingsWidget(Plugin plugin) {
this.plugin = plugin;
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 void deactivate() {
this.set_visible_child_name("label");
}
public void set_account(Account account) {
this.current_account = account;
if (keys == null) {
fetch_keys();
} else {
activate_current_account();
}
}
private void on_button_clicked() {
activated();
this.set_visible_child_name("entry");
combobox.popup();
}
private void activate_current_account() {
combobox.changed.disconnect(key_changed);
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.size == 0) {
label.set_markup(build_markup_string("Key publishing disabled", "No keys available. Generate one!"));
return;
}
TreeIter iter;
list_store.append(out iter);
list_store.set(iter, 0, build_markup_string("Key publishing disabled", "Select key"), 1, "");
for (int i = 0; i < keys.size; i++) {
list_store.append(out iter);
list_store.set(iter, 0, build_markup_string(keys[i].uids[0].uid, "0x" + keys[i].fpr[0:16]));
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);
}
}
activate_current_account();
button.sensitive = true;
}
private void fetch_keys() {
TreeIter iter;
list_store.clear();
list_store.append(out iter);
label.set_markup(build_markup_string("Loading...", "Querying GnuPG"));
new Thread<void*> (null, () => { // Querying GnuPG might take some while
try {
keys = GPGHelper.get_keylist(null, true);
Idle.add(() => {
list_store.clear();
populate_list_store();
return false;
});
} catch (Error e) {
Idle.add(() => {
label.set_markup(build_markup_string("Key publishing disabled", "Error in GnuPG"));
return false;
});
}
return null;
});
}
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='9'>$(Markup.escape_text(secondary))</span>";
}
}
}
|