From 39722a4973eb3450d7b92a8fac4e67aae7b3ceae Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sun, 12 Mar 2017 16:21:46 +0100 Subject: [PATCH] Query GPG in other thread + only once (account settings) --- plugins/openpgp/data/account_settings_item.ui | 1 - .../openpgp/src/account_settings_widget.vala | 89 ++++++++++++++----- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/plugins/openpgp/data/account_settings_item.ui b/plugins/openpgp/data/account_settings_item.ui index 299b9a89..0c10b84b 100644 --- a/plugins/openpgp/data/account_settings_item.ui +++ b/plugins/openpgp/data/account_settings_item.ui @@ -20,7 +20,6 @@ True - 200 True diff --git a/plugins/openpgp/src/account_settings_widget.vala b/plugins/openpgp/src/account_settings_widget.vala index 71739096..5340db30 100644 --- a/plugins/openpgp/src/account_settings_widget.vala +++ b/plugins/openpgp/src/account_settings_widget.vala @@ -1,3 +1,4 @@ +using Gee; using Gtk; using Dino.Entities; @@ -13,6 +14,7 @@ private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget { private Plugin plugin; private Account current_account; + private Gee.List keys = null; private Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(string?)); public AccountSettingsWidget(Plugin plugin) { @@ -22,6 +24,7 @@ private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget { 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); @@ -33,7 +36,11 @@ private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget { public void set_account(Account account) { this.current_account = account; - populate(account); + if (keys == null) { + fetch_keys(); + } else { + activate_current_account(); + } } private void on_button_clicked() { @@ -42,30 +49,66 @@ private class AccountSettingsWidget : Stack, Plugins.AccountSettingsWidget { combobox.popup(); } - private void populate(Account account) { - TreeIter iter; - combobox.set_model(list_store); - - list_store.clear(); - try { - Gee.List keys = GPGHelper.get_keylist(null, true); - - list_store.append(out iter); - list_store.set(iter, 0, "Disabled\nSelect key", 1, null); - set_label_active(iter, 0); - for (int i = 0; i < keys.size; i++) { - list_store.append(out iter); - string text = @"$(Markup.escape_text(keys[i].uids[0].uid))\n0x$(Markup.escape_text(keys[i].fpr[0:16]))"; - list_store.set(iter, 0, text); - list_store.set(iter, 1, keys[i].fpr); - if (keys[i].fpr == plugin.db.get_account_key(account)) { - set_label_active(iter, i + 1); - } + private void activate_current_account() { + 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; } - } catch (Error e){ - list_store.append(out iter); - list_store.set(iter, 0, @"Disabled\nError: $(Markup.escape_text(e.message))", 1, null); } + combobox.active = activate_index; + + TreeIter selected; + combobox.get_active_iter(out selected); + set_label_active(selected); + } + + private void populate_list_store() { + TreeIter iter; + list_store.append(out iter); + list_store.set(iter, 0, "Disabled\nSelect key", 1, null); + set_label_active(iter, 0); + 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))\n0x$(Markup.escape_text(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(); + } + + private void fetch_keys() { + new Thread (null, () => { // Querying GnuPG might take some while + Idle.add(() => { + TreeIter iter; + list_store.clear(); + list_store.append(out iter); + button.sensitive = false; + label.set_markup("Loading...\nQuerying GnuPG"); + return false; + }); + try { + keys = GPGHelper.get_keylist(null, true); + Idle.add(() => { + list_store.clear(); + populate_list_store(); + button.sensitive = true; + return false; + }); + } catch (Error e) { + Idle.add(() => { + TreeIter iter; + list_store.append(out iter); + list_store.set(iter, 0, @"Disabled\nError in GnuPG", 1, null); + return false; + }); + } + return null; + }); } private void set_label_active(TreeIter iter, int i = -1) {