From 7e83529afcd0ccfff5c65c99e4427bd6cf3f82ac Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 31 Oct 2017 15:19:30 +0100 Subject: Blocking setting in Contact Details --- libdino/CMakeLists.txt | 1 + libdino/src/application.vala | 1 + libdino/src/service/blocking_manager.vala | 44 ++++++++++++++++++++++ main/CMakeLists.txt | 1 + main/src/ui/contact_details/blocking_provider.vala | 36 ++++++++++++++++++ main/src/ui/contact_details/dialog.vala | 1 + .../src/module/xep/0191_blocking_command.vala | 24 +++++++----- 7 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 libdino/src/service/blocking_manager.vala create mode 100644 main/src/ui/contact_details/blocking_provider.vala diff --git a/libdino/CMakeLists.txt b/libdino/CMakeLists.txt index 54cb1932..106ddf27 100644 --- a/libdino/CMakeLists.txt +++ b/libdino/CMakeLists.txt @@ -29,6 +29,7 @@ SOURCES src/service/avatar_manager.vala src/service/avatar_storage.vala + src/service/blocking_manager.vala src/service/chat_interaction.vala src/service/connection_manager.vala src/service/conversation_manager.vala diff --git a/libdino/src/application.vala b/libdino/src/application.vala index c18b28f9..c9283127 100644 --- a/libdino/src/application.vala +++ b/libdino/src/application.vala @@ -31,6 +31,7 @@ public interface Dino.Application : GLib.Application { MessageStorage.start(stream_interactor, db); CounterpartInteractionManager.start(stream_interactor); PresenceManager.start(stream_interactor); + BlockingManager.start(stream_interactor); MucManager.start(stream_interactor); RosterManager.start(stream_interactor, db); ConversationManager.start(stream_interactor, db); diff --git a/libdino/src/service/blocking_manager.vala b/libdino/src/service/blocking_manager.vala new file mode 100644 index 00000000..de79b4d4 --- /dev/null +++ b/libdino/src/service/blocking_manager.vala @@ -0,0 +1,44 @@ +using Gee; + +using Xmpp; +using Dino.Entities; + +namespace Dino { + +public class BlockingManager : StreamInteractionModule, Object { + public static ModuleIdentity IDENTITY = new ModuleIdentity("blocking_manager"); + public string id { get { return IDENTITY.id; } } + + private StreamInteractor stream_interactor; + + public static void start(StreamInteractor stream_interactor) { + BlockingManager m = new BlockingManager(stream_interactor); + stream_interactor.add_module(m); + } + + private BlockingManager(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + } + + public bool is_blocked(Account account, Jid jid) { + Core.XmppStream stream = stream_interactor.get_stream(account); + return stream != null && stream.get_module(Xmpp.Xep.BlockingCommand.Module.IDENTITY).is_blocked(stream, jid.to_string()); + } + + public void block(Account account, Jid jid) { + Core.XmppStream stream = stream_interactor.get_stream(account); + stream.get_module(Xmpp.Xep.BlockingCommand.Module.IDENTITY).block(stream, new ArrayList.wrap(new string[] {jid.to_string()})); + } + + public void unblock(Account account, Jid jid) { + Core.XmppStream stream = stream_interactor.get_stream(account); + stream.get_module(Xmpp.Xep.BlockingCommand.Module.IDENTITY).unblock(stream, new ArrayList.wrap(new string[] {jid.to_string()})); + } + + public bool is_supported(Account account) { + Core.XmppStream stream = stream_interactor.get_stream(account); + return stream != null && stream.get_module(Xmpp.Xep.BlockingCommand.Module.IDENTITY).is_supported(stream); + } +} + +} diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 3451e91d..222adfb9 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -88,6 +88,7 @@ SOURCES src/ui/chat_input/occupants_tab_completer.vala src/ui/chat_input/smiley_converter.vala src/ui/chat_input/view.vala + src/ui/contact_details/blocking_provider.vala src/ui/contact_details/settings_provider.vala src/ui/contact_details/dialog.vala src/ui/contact_details/muc_config_form_provider.vala diff --git a/main/src/ui/contact_details/blocking_provider.vala b/main/src/ui/contact_details/blocking_provider.vala new file mode 100644 index 00000000..bf59a083 --- /dev/null +++ b/main/src/ui/contact_details/blocking_provider.vala @@ -0,0 +1,36 @@ +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui.ContactDetails { + +public class BlockingProvider : Plugins.ContactDetailsProvider, Object { + public string id { get { return "blocking"; } } + + private StreamInteractor stream_interactor; + + public BlockingProvider(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + } + + public void populate(Conversation conversation, Plugins.ContactDetails contact_details, Plugins.WidgetType type) { + if (type != Plugins.WidgetType.GTK) return; + if (conversation.type_ != Conversation.Type.CHAT) return; + + if (stream_interactor.get_module(BlockingManager.IDENTITY).is_supported(conversation.account)) { + bool is_blocked = stream_interactor.get_module(BlockingManager.IDENTITY).is_blocked(conversation.account, conversation.counterpart); + Switch sw = new Switch() { active=is_blocked, valign=Align.CENTER, visible=true }; + sw.state_set.connect((state) => { + if (state) { + stream_interactor.get_module(BlockingManager.IDENTITY).block(conversation.account, conversation.counterpart); + } else { + stream_interactor.get_module(BlockingManager.IDENTITY).unblock(conversation.account, conversation.counterpart); + } + return false; + }); + contact_details.add(_("Settings"), _("Block"), _("Communication and status updates in either direction are blocked"), sw); + } + } +} + +} diff --git a/main/src/ui/contact_details/dialog.vala b/main/src/ui/contact_details/dialog.vala index 147811f9..70e495d1 100644 --- a/main/src/ui/contact_details/dialog.vala +++ b/main/src/ui/contact_details/dialog.vala @@ -42,6 +42,7 @@ public class Dialog : Gtk.Dialog { Application app = GLib.Application.get_default() as Application; app.plugin_registry.register_contact_details_entry(new SettingsProvider(stream_interactor)); + app.plugin_registry.register_contact_details_entry(new BlockingProvider(stream_interactor)); app.plugin_registry.register_contact_details_entry(new MucConfigFormProvider(stream_interactor)); foreach (Plugins.ContactDetailsProvider provider in app.plugin_registry.contact_details_entries) { diff --git a/xmpp-vala/src/module/xep/0191_blocking_command.vala b/xmpp-vala/src/module/xep/0191_blocking_command.vala index c970955d..53d7bb7b 100644 --- a/xmpp-vala/src/module/xep/0191_blocking_command.vala +++ b/xmpp-vala/src/module/xep/0191_blocking_command.vala @@ -14,13 +14,15 @@ public class Module : XmppStreamModule, Iq.Handler { public signal void unblock_all_received(XmppStream stream); public bool is_blocked(XmppStream stream, string jid) { - return stream.get_flag(Flag.IDENTITY).blocklist.contains(jid); + foreach (string blocked in stream.get_flag(Flag.IDENTITY).blocklist) { + if (blocked.contains(jid)) return true; + } + return false; } public bool block(XmppStream stream, Gee.List jids) { - // This would otherwise be a bad-request error. - if (jids.size == 0) - return false; + if (jids.size == 0) return false; // This would otherwise be a bad-request error. + StanzaNode block_node = new StanzaNode.build("block", NS_URI).add_self_xmlns(); fill_node_with_items(block_node, jids); Iq.Stanza iq = new Iq.Stanza.set(block_node); @@ -29,9 +31,8 @@ public class Module : XmppStreamModule, Iq.Handler { } public bool unblock(XmppStream stream, Gee.List jids) { - // This would otherwise unblock all blocked JIDs. - if (jids.size == 0) - return false; + if (jids.size == 0) return false; // This would otherwise unblock all blocked JIDs. + StanzaNode unblock_node = new StanzaNode.build("unblock", NS_URI).add_self_xmlns(); fill_node_with_items(unblock_node, jids); Iq.Stanza iq = new Iq.Stanza.set(unblock_node); @@ -45,6 +46,10 @@ public class Module : XmppStreamModule, Iq.Handler { stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null); } + public bool is_supported(XmppStream stream) { + return stream.has_flag(Flag.IDENTITY); + } + private void on_iq_get(XmppStream stream, Iq.Stanza iq) { } private void on_iq_set(XmppStream stream, Iq.Stanza iq) { StanzaNode? block_node = iq.stanza.get_subnode("block", NS_URI); @@ -81,7 +86,7 @@ public class Module : XmppStreamModule, Iq.Handler { public override string get_id() { return IDENTITY.id; } private void on_stream_negotiated(XmppStream stream) { - stream.get_module(Xep.ServiceDiscovery.Module.IDENTITY).request_info(stream, "jabberfr.org", (stream, info_result) => { + stream.get_module(Xep.ServiceDiscovery.Module.IDENTITY).request_info(stream, stream.remote_name, (stream, info_result) => { if (info_result.features.contains(NS_URI)) { stream.add_flag(new Flag()); get_blocklist(stream, (stream, jids) => { @@ -110,8 +115,9 @@ public class Module : XmppStreamModule, Iq.Handler { Gee.List jids = new ArrayList(); foreach (StanzaNode item_node in item_nodes) { string? jid = item_node.get_attribute("jid", NS_URI); - if (jid != null) + if (jid != null) { jids.add(jid); + } } return jids; } -- cgit v1.2.3-70-g09d2