From 81310dff2e712cf7ddbce7c4717cd5a77170c79a Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 7 Mar 2018 17:24:57 +0100 Subject: Handle multiple chat state notifications per bare jid fixes #117 --- .../conversation_summary/chat_state_populator.vala | 113 +++++++++++++++------ .../conversation_item_skeleton.vala | 4 +- 2 files changed, 84 insertions(+), 33 deletions(-) (limited to 'main/src/ui/conversation_summary') diff --git a/main/src/ui/conversation_summary/chat_state_populator.vala b/main/src/ui/conversation_summary/chat_state_populator.vala index bb9597e6..1ea52a6d 100644 --- a/main/src/ui/conversation_summary/chat_state_populator.vala +++ b/main/src/ui/conversation_summary/chat_state_populator.vala @@ -21,7 +21,7 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Object { stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).received_state.connect((account, jid, state) => { if (current_conversation != null && current_conversation.account.equals(account) && current_conversation.counterpart.equals_bare(jid)) { - update_chat_state(account, jid, state); + update_chat_state(account, jid); } }); stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => { @@ -45,70 +45,121 @@ class ChatStatePopulator : Plugins.ConversationItemPopulator, Object { public void populate_between_widgets(Conversation conversation, DateTime from, DateTime to) { } - private void update_chat_state(Account account, Jid jid, string? state = null) { - string? state_ = state; - if (state_ == null) { - state_ = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_chat_state(current_conversation.account, current_conversation.counterpart); - } - string? new_text = null; - if (state_ != null) { - if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING || state_ == Xep.ChatStateNotifications.STATE_PAUSED) { - if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING) { - new_text = _("is typing…"); - } else if (state_ == Xep.ChatStateNotifications.STATE_PAUSED) { - new_text = _("has stopped typing"); + private void update_chat_state(Account account, Jid jid) { + HashMap? states = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_chat_states(current_conversation); + + StateType? state_type = null; + Gee.List jids = new ArrayList(); + + if (states != null) { + Gee.List composing = new ArrayList(); + Gee.List paused = new ArrayList(); + foreach (Jid j in states.keys) { + string state = states[j]; + if (state == Xep.ChatStateNotifications.STATE_COMPOSING) { + composing.add(j); + } else if (state == Xep.ChatStateNotifications.STATE_PAUSED) { + paused.add(j); } } + if (composing.size == 1 || (composing.size > 1 && current_conversation.type_ != Conversation.Type.GROUPCHAT)) { + state_type = StateType.TYPING; + jids.add(composing[0]); + } else if (paused.size >= 1 && current_conversation.type_ != Conversation.Type.GROUPCHAT) { + state_type = StateType.PAUSED; + jids.add(paused[0]); + } else if (composing.size > 1) { + state_type = StateType.TYPING; + jids = composing; + } } - if (meta_item != null && new_text == null) { + if (meta_item != null && state_type == null) { item_collection.remove_item(meta_item); meta_item = null; - } else if (meta_item != null && new_text != null) { - meta_item.set_text(new_text); - } else if (new_text != null) { - meta_item = new MetaChatStateItem(stream_interactor, current_conversation, jid, new_text); + } else if (meta_item != null && state_type != null) { + meta_item.set_new(state_type, jids); + } else if (state_type != null) { + meta_item = new MetaChatStateItem(stream_interactor, current_conversation, jid, state_type, jids); item_collection.insert_item(meta_item); } } } -public class MetaChatStateItem : Plugins.MetaConversationItem { +private enum StateType { + TYPING, + PAUSED +} + +private class MetaChatStateItem : Plugins.MetaConversationItem { public override Jid? jid { get; set; } public override bool dim { get; set; default=true; } public override DateTime? sort_time { get; set; default=new DateTime.now_utc().add_years(10); } public override bool can_merge { get; set; default=false; } - public override bool requires_avatar { get; set; default=true; } + public override bool requires_avatar { get; set; default=false; } public override bool requires_header { get; set; default=false; } private StreamInteractor stream_interactor; private Conversation conversation; - private string text; + private StateType state_type; + private Gee.List jids = new ArrayList(); private Label label; + private AvatarImage image; - public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Jid jid, string text) { + public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Jid jid, StateType state_type, Gee.List jids) { this.stream_interactor = stream_interactor; this.conversation = conversation; this.jid = jid; - this.text = text; + this.state_type = state_type; + this.jids = jids; } public override Object? get_widget(Plugins.WidgetType widget_type) { label = new Label("") { xalign=0, vexpand=true, visible=true }; label.get_style_context().add_class("dim-label"); - update_text(); - return label; + image = new AvatarImage() { margin_top=2, valign=Align.START, visible=true }; + + Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true }; + image_content_box.add(image); + image_content_box.add(label); + + update(); + return image_content_box; } - public void set_text(string text) { - this.text = text; - update_text(); + public void set_new(StateType state_type, Gee.List jids) { + this.state_type = state_type; + this.jids = jids; + update(); } - private void update_text() { - string display_name = Util.get_display_name(stream_interactor, jid, conversation.account); - label.label = display_name + " " + text; + private void update() { + if (image == null || label == null) return; + + image.set_jids(stream_interactor, jids.to_array(), conversation.account, true); + + Gee.List display_names = new ArrayList(); + foreach (Jid jid in jids) { + display_names.add(Util.get_display_name(stream_interactor, jid, conversation.account)); + } + string new_text = ""; + if (jids.size > 3) { + new_text = _("%s, %s and %i others").printf(display_names[0], display_names[1], jids.size - 2); + } else if (jids.size == 3) { + new_text = _("%s, %s and %s").printf(display_names[0], display_names[1], display_names[2]); + } else if (jids.size == 2) { + new_text =_("%s and %s").printf(display_names[0], display_names[1]); + } else { + new_text = display_names[0]; + } + if (state_type == StateType.TYPING) { + new_text += " " + n("is typing…", "are typing…", jids.size); + } else { + new_text += " " + _("has stopped typing"); + } + + label.label = new_text; } } diff --git a/main/src/ui/conversation_summary/conversation_item_skeleton.vala b/main/src/ui/conversation_summary/conversation_item_skeleton.vala index 33d135fc..a8da93ef 100644 --- a/main/src/ui/conversation_summary/conversation_item_skeleton.vala +++ b/main/src/ui/conversation_summary/conversation_item_skeleton.vala @@ -22,9 +22,11 @@ public class ConversationItemSkeleton : Box { public ConversationItemSkeleton(StreamInteractor stream_interactor, Conversation conversation, Plugins.MetaConversationItem item) { this.conversation = conversation; this.stream_interactor = stream_interactor; + Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true }; if (item.requires_avatar) { image.set_jid(stream_interactor, item.jid, conversation.account); + image_content_box.add(image); } if (item.display_time != null) { default_header = new DefaultSkeletonHeader(stream_interactor, conversation, item) { visible=true }; @@ -36,8 +38,6 @@ public class ConversationItemSkeleton : Box { } add_meta_item(item); - Box image_content_box = new Box(Orientation.HORIZONTAL, 8) { visible=true }; - image_content_box.add(image); image_content_box.add(grid); this.add(image_content_box); } -- cgit v1.2.3-54-g00ecf