From 0ea4ac7e20674e3e6a8d1b3d4b53702dace72907 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 11 Mar 2017 22:48:35 +0100 Subject: Plug-In API: allow adding custom entries to account settings Also make OpenPGP code use this API --- libdino/src/service/pgp_manager.vala | 80 +++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'libdino/src/service/pgp_manager.vala') diff --git a/libdino/src/service/pgp_manager.vala b/libdino/src/service/pgp_manager.vala index 76f70b84..a91feac6 100644 --- a/libdino/src/service/pgp_manager.vala +++ b/libdino/src/service/pgp_manager.vala @@ -17,7 +17,85 @@ namespace Dino { public static void start(StreamInteractor stream_interactor, Database db) { PgpManager m = new PgpManager(stream_interactor, db); stream_interactor.add_module(m); - (GLib.Application.get_default() as Application).plugin_registry.register_encryption_list_entry(new EncryptionListEntry(m)); + + Plugins.Registry plugin_registry = (GLib.Application.get_default() as Application).plugin_registry; + plugin_registry.register_encryption_list_entry(new EncryptionListEntry(m)); + plugin_registry.register_account_settings_entry(new AccountSettingsEntry(m)); + } + + private class AccountSettingsEntry : Plugins.AccountSettingsEntry { + private PgpManager pgp_manager; + + public AccountSettingsEntry(PgpManager pgp_manager) { + this.pgp_manager = pgp_manager; + } + + public override string id { get { + return "pgp_key_picker"; + }} + + public override string name { get { + return "OpenPGP"; + }} + + public override Plugins.AccountSettingsWidget get_widget() { + return new AccountSettingsWidget(); + } + } + + [GtkTemplate (ui = "/org/dino-im/manage_accounts/pgp_stack.ui")] + private class AccountSettingsWidget : Gtk.Stack, Plugins.AccountSettingsWidget { + [GtkChild] private Gtk.Label pgp_label; + [GtkChild] private Gtk.Button pgp_button; + [GtkChild] private Gtk.ComboBox pgp_combobox; + + private Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(string?)); + + public AccountSettingsWidget() { + Gtk.CellRendererText renderer = new Gtk.CellRendererText(); + renderer.set_padding(0, 0); + pgp_combobox.pack_start(renderer, true); + pgp_combobox.add_attribute(renderer, "markup", 0); + pgp_button.clicked.connect(() => { activated(); this.set_visible_child_name("entry"); pgp_combobox.popup(); }); + } + + public void deactivate() { + this.set_visible_child_name("label"); + } + + private void key_changed() { + Gtk.TreeIter selected; + pgp_combobox.get_active_iter(out selected); + Value text; + list_store.get_value(selected, 0, out text); + pgp_label.set_markup((string) text); + deactivate(); + } + + public void set_account(Account account) { + populate_pgp_combobox(account); + } + + private void populate_pgp_combobox(Account account) { + pgp_combobox.changed.disconnect(key_changed); + + Gtk.TreeIter iter; + pgp_combobox.set_model(list_store); + + list_store.clear(); + list_store.append(out iter); + pgp_label.set_markup("Disabled\nSelect key"); + list_store.set(iter, 0, "Disabled\nSelect key", 1, null); + Gee.List list = GPGHelper.get_keylist(null, true); + foreach (GPG.Key key in list) { + list_store.append(out iter); + list_store.set(iter, 0, @"$(Markup.escape_text(key.uids[0].uid))\n0x$(Markup.escape_text(key.fpr[0:16]))"); + list_store.set(iter, 1, key.fpr); + } + + pgp_combobox.set_active(0); + pgp_combobox.changed.connect(key_changed); + } } private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { -- cgit v1.2.3-54-g00ecf