2017-03-12 15:21:46 +00:00
|
|
|
using Gee;
|
2017-03-12 13:44:09 +00:00
|
|
|
using Gtk;
|
|
|
|
|
2017-03-12 01:49:53 +00:00
|
|
|
using Dino.Entities;
|
|
|
|
|
|
|
|
namespace Dino.Plugins.OpenPgp {
|
|
|
|
|
2017-08-17 19:24:01 +00:00
|
|
|
[GtkTemplate (ui = "/im/dino/account_settings_item.ui")]
|
2017-03-12 01:49:53 +00:00
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget {
|
|
|
|
[GtkChild] private Label label;
|
|
|
|
[GtkChild] private Button button;
|
|
|
|
[GtkChild] private ComboBox combobox;
|
2017-03-12 01:49:53 +00:00
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private Plugin plugin;
|
|
|
|
private Account current_account;
|
2017-03-12 15:21:46 +00:00
|
|
|
private Gee.List<GPG.Key> keys = null;
|
2017-03-12 01:49:53 +00:00
|
|
|
private Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(string?));
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
public AccountSettingsWidget(Plugin plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
|
|
|
|
CellRendererText renderer = new CellRendererText();
|
2017-03-12 01:49:53 +00:00
|
|
|
renderer.set_padding(0, 0);
|
2017-03-12 13:44:09 +00:00
|
|
|
combobox.pack_start(renderer, true);
|
|
|
|
combobox.add_attribute(renderer, "markup", 0);
|
2017-03-12 15:21:46 +00:00
|
|
|
combobox.set_model(list_store);
|
2017-03-12 13:44:09 +00:00
|
|
|
|
|
|
|
button.clicked.connect(on_button_clicked);
|
|
|
|
combobox.changed.connect(key_changed);
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deactivate() {
|
2017-08-23 15:49:55 +00:00
|
|
|
set_visible_child_name("label");
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void set_account(Account account) {
|
2017-03-12 13:44:09 +00:00
|
|
|
this.current_account = account;
|
2017-03-12 15:21:46 +00:00
|
|
|
if (keys == null) {
|
|
|
|
fetch_keys();
|
|
|
|
} else {
|
|
|
|
activate_current_account();
|
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
private void on_button_clicked() {
|
|
|
|
activated();
|
2017-08-23 15:49:55 +00:00
|
|
|
set_visible_child_name("entry");
|
|
|
|
combobox.grab_focus();
|
2017-03-12 13:44:09 +00:00
|
|
|
combobox.popup();
|
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
|
2017-03-12 15:21:46 +00:00
|
|
|
private void activate_current_account() {
|
2017-03-13 09:35:47 +00:00
|
|
|
combobox.changed.disconnect(key_changed);
|
|
|
|
|
2017-03-12 15:21:46 +00:00
|
|
|
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;
|
2017-03-12 01:49:53 +00:00
|
|
|
|
2017-03-12 15:21:46 +00:00
|
|
|
TreeIter selected;
|
|
|
|
combobox.get_active_iter(out selected);
|
|
|
|
set_label_active(selected);
|
2017-03-13 09:35:47 +00:00
|
|
|
|
|
|
|
combobox.changed.connect(key_changed);
|
2017-03-12 15:21:46 +00:00
|
|
|
}
|
2017-03-12 13:44:09 +00:00
|
|
|
|
2017-03-12 15:21:46 +00:00
|
|
|
private void populate_list_store() {
|
2017-03-12 17:12:30 +00:00
|
|
|
if (keys.size == 0) {
|
2017-04-07 09:09:47 +00:00
|
|
|
label.set_markup(build_markup_string(_("Key publishing disabled"), _("No keys available. Generate one!")));
|
2017-03-12 17:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:21:46 +00:00
|
|
|
TreeIter iter;
|
|
|
|
list_store.append(out iter);
|
2017-08-14 11:48:43 +00:00
|
|
|
list_store.set(iter, 0, build_markup_string(_("Key publishing disabled"), _("Select key") + "<span font_family='monospace' font='8'> \n </span>"), 1, "");
|
2017-03-12 15:21:46 +00:00
|
|
|
for (int i = 0; i < keys.size; i++) {
|
2017-03-12 13:44:09 +00:00
|
|
|
list_store.append(out iter);
|
2017-08-14 11:48:43 +00:00
|
|
|
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>");
|
2017-03-12 15:21:46 +00:00
|
|
|
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);
|
2017-03-12 13:44:09 +00:00
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
2017-03-12 15:21:46 +00:00
|
|
|
activate_current_account();
|
2017-03-12 17:12:30 +00:00
|
|
|
button.sensitive = true;
|
2017-03-12 15:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void fetch_keys() {
|
2017-03-12 17:12:30 +00:00
|
|
|
TreeIter iter;
|
|
|
|
list_store.clear();
|
|
|
|
list_store.append(out iter);
|
2017-04-07 09:09:47 +00:00
|
|
|
label.set_markup(build_markup_string(_("Loading..."), _("Querying GnuPG")));
|
2017-04-08 09:53:10 +00:00
|
|
|
new Thread<void*> (null, () => { // Querying GnuPG might take some time
|
2017-03-12 15:21:46 +00:00
|
|
|
try {
|
|
|
|
keys = GPGHelper.get_keylist(null, true);
|
|
|
|
Idle.add(() => {
|
|
|
|
list_store.clear();
|
|
|
|
populate_list_store();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
} catch (Error e) {
|
|
|
|
Idle.add(() => {
|
2017-04-07 09:09:47 +00:00
|
|
|
label.set_markup(build_markup_string(_("Key publishing disabled"), _("Error in GnuPG")));
|
2017-03-12 15:21:46 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2017-03-12 13:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
|
2017-03-12 13:44:09 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
2017-03-12 17:12:30 +00:00
|
|
|
|
|
|
|
private string build_markup_string(string primary, string secondary) {
|
2017-08-14 11:48:43 +00:00
|
|
|
return @"$(Markup.escape_text(primary))\n<span font='8'>$secondary</span>";
|
2017-03-12 17:12:30 +00:00
|
|
|
}
|
2017-03-12 01:49:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 12:29:02 +00:00
|
|
|
}
|