From 08a5088c16ae0bd69adc42ac6489adde3a9ad13f Mon Sep 17 00:00:00 2001 From: fiaxh Date: Fri, 2 Aug 2019 03:15:12 +0200 Subject: Rework encryption enabling logic + UI --- plugins/openpgp/src/encryption_list_entry.vala | 30 ++++++++++++++++++++------ plugins/openpgp/src/plugin.vala | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'plugins/openpgp') diff --git a/plugins/openpgp/src/encryption_list_entry.vala b/plugins/openpgp/src/encryption_list_entry.vala index 603c5c94..5b89ec1c 100644 --- a/plugins/openpgp/src/encryption_list_entry.vala +++ b/plugins/openpgp/src/encryption_list_entry.vala @@ -8,9 +8,11 @@ namespace Dino.Plugins.OpenPgp { private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { private StreamInteractor stream_interactor; + private Database db; - public EncryptionListEntry(StreamInteractor stream_interactor) { + public EncryptionListEntry(StreamInteractor stream_interactor, Database db) { this.stream_interactor = stream_interactor; + this.db = db; } public Entities.Encryption encryption { get { @@ -21,12 +23,25 @@ private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { return "OpenPGP"; }} - public bool can_encrypt(Entities.Conversation conversation) { + public void encryption_activated(Entities.Conversation conversation, Plugins.SetInputFieldStatus input_status_callback) { + try { + GPGHelper.get_public_key(db.get_account_key(conversation.account) ?? ""); + } catch (Error e) { + input_status_callback(new Plugins.InputFieldStatus("You didn't configure OpenPGP for this account. You can do that in the Accounts Dialog.", Plugins.InputFieldStatus.MessageType.ERROR, Plugins.InputFieldStatus.InputState.NO_SEND)); + return; + } + if (conversation.type_ == Conversation.Type.CHAT) { string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, conversation.counterpart); + if (key_id == null) { + input_status_callback(new Plugins.InputFieldStatus("This contact does not support %s encryption.".printf("OpenPGP"), Plugins.InputFieldStatus.MessageType.ERROR, Plugins.InputFieldStatus.InputState.NO_SEND)); + return; + } try { - return key_id != null && GPGHelper.get_keylist(key_id).size > 0; - } catch (Error e) { return false; } + GPGHelper.get_keylist(key_id); + } catch (Error e) { + input_status_callback(new Plugins.InputFieldStatus("This contact's OpenPGP key is not in your keyring.", Plugins.InputFieldStatus.MessageType.ERROR, Plugins.InputFieldStatus.InputState.NO_SEND)); + } } else if (conversation.type_ == Conversation.Type.GROUPCHAT) { Gee.List muc_jids = new Gee.ArrayList(); Gee.List? occupants = stream_interactor.get_module(MucManager.IDENTITY).get_occupants(conversation.counterpart, conversation.account); @@ -36,11 +51,12 @@ private class EncryptionListEntry : Plugins.EncryptionListEntry, Object { foreach (Jid jid in muc_jids) { string? key_id = stream_interactor.get_module(Manager.IDENTITY).get_key_id(conversation.account, jid); - if (key_id == null) return false; + if (key_id == null) { + input_status_callback(new Plugins.InputFieldStatus("A member's OpenPGP key is not in your keyring: %s / %s.".printf(jid.to_string(), key_id), Plugins.InputFieldStatus.MessageType.ERROR, Plugins.InputFieldStatus.InputState.NO_SEND)); + return; + } } - return true; } - return false; } } diff --git a/plugins/openpgp/src/plugin.vala b/plugins/openpgp/src/plugin.vala index 35ede01e..b4581f31 100644 --- a/plugins/openpgp/src/plugin.vala +++ b/plugins/openpgp/src/plugin.vala @@ -19,7 +19,7 @@ public class Plugin : Plugins.RootInterface, Object { public void registered(Dino.Application app) { this.app = app; this.db = new Database(Path.build_filename(Application.get_storage_dir(), "pgp.db")); - this.list_entry = new EncryptionListEntry(app.stream_interactor); + this.list_entry = new EncryptionListEntry(app.stream_interactor, db); this.settings_entry = new AccountSettingsEntry(this); this.contact_details_provider = new ContactDetailsProvider(app.stream_interactor); -- cgit v1.2.3-70-g09d2