2017-08-27 21:55:49 +00:00
|
|
|
using Gee;
|
|
|
|
using Gtk;
|
|
|
|
|
|
|
|
using Dino.Entities;
|
|
|
|
using Xmpp;
|
|
|
|
|
|
|
|
namespace Dino.Ui.ConversationSummary {
|
|
|
|
|
2018-06-19 16:07:00 +00:00
|
|
|
class ChatStatePopulator : Plugins.ConversationItemPopulator, Plugins.ConversationAdditionPopulator, Object {
|
2017-08-27 21:55:49 +00:00
|
|
|
|
|
|
|
public string id { get { return "chat_state"; } }
|
|
|
|
|
|
|
|
private StreamInteractor? stream_interactor;
|
|
|
|
private Conversation? current_conversation;
|
|
|
|
private Plugins.ConversationItemCollection? item_collection;
|
|
|
|
|
|
|
|
private MetaChatStateItem? meta_item;
|
|
|
|
|
|
|
|
public ChatStatePopulator(StreamInteractor stream_interactor) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).received_state.connect((conversation, state) => {
|
|
|
|
if (current_conversation != null && current_conversation.equals(conversation)) {
|
|
|
|
update_chat_state();
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => {
|
|
|
|
if (conversation.equals(current_conversation)) {
|
2020-02-20 15:59:34 +00:00
|
|
|
update_chat_state();
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init(Conversation conversation, Plugins.ConversationItemCollection item_collection, Plugins.WidgetType type) {
|
|
|
|
current_conversation = conversation;
|
|
|
|
this.item_collection = item_collection;
|
|
|
|
this.meta_item = null;
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
update_chat_state();
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void close(Conversation conversation) { }
|
|
|
|
|
|
|
|
public void populate_timespan(Conversation conversation, DateTime from, DateTime to) { }
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
private void update_chat_state() {
|
|
|
|
Gee.List<Jid>? typing_jids = stream_interactor.get_module(CounterpartInteractionManager.IDENTITY).get_typing_jids(current_conversation);
|
|
|
|
|
|
|
|
if (meta_item != null && typing_jids == null) {
|
|
|
|
// Remove state (stoped typing)
|
2017-08-27 21:55:49 +00:00
|
|
|
item_collection.remove_item(meta_item);
|
|
|
|
meta_item = null;
|
2020-02-20 15:59:34 +00:00
|
|
|
} else if (meta_item != null && typing_jids != null) {
|
|
|
|
// Update state (other people typing in MUC)
|
|
|
|
meta_item.set_new(typing_jids);
|
|
|
|
} else if (typing_jids != null) {
|
|
|
|
// New state (started typing)
|
|
|
|
meta_item = new MetaChatStateItem(stream_interactor, current_conversation, typing_jids);
|
2017-08-27 21:55:49 +00:00
|
|
|
item_collection.insert_item(meta_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
private class MetaChatStateItem : Plugins.MetaConversationItem {
|
2017-08-27 21:55:49 +00:00
|
|
|
public override bool dim { get; set; default=true; }
|
2020-01-21 23:09:06 +00:00
|
|
|
public override DateTime sort_time { get; set; default=new DateTime.now_utc().add_years(10); }
|
2017-08-27 21:55:49 +00:00
|
|
|
|
|
|
|
public override bool can_merge { get; set; default=false; }
|
2018-03-07 16:24:57 +00:00
|
|
|
public override bool requires_avatar { get; set; default=false; }
|
2017-08-27 21:55:49 +00:00
|
|
|
public override bool requires_header { get; set; default=false; }
|
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
|
|
|
private Conversation conversation;
|
2018-03-07 16:24:57 +00:00
|
|
|
private Gee.List<Jid> jids = new ArrayList<Jid>();
|
2017-08-27 21:55:49 +00:00
|
|
|
private Label label;
|
2018-03-07 16:24:57 +00:00
|
|
|
private AvatarImage image;
|
2017-08-27 21:55:49 +00:00
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
public MetaChatStateItem(StreamInteractor stream_interactor, Conversation conversation, Gee.List<Jid> jids) {
|
2017-08-27 21:55:49 +00:00
|
|
|
this.stream_interactor = stream_interactor;
|
|
|
|
this.conversation = conversation;
|
2018-03-07 16:24:57 +00:00
|
|
|
this.jids = jids;
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 14:15:28 +00:00
|
|
|
public override Object? get_widget(Plugins.WidgetType widget_type) {
|
2017-08-27 21:55:49 +00:00
|
|
|
label = new Label("") { xalign=0, vexpand=true, visible=true };
|
|
|
|
label.get_style_context().add_class("dim-label");
|
2018-03-07 16:24:57 +00:00
|
|
|
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;
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 15:59:34 +00:00
|
|
|
public void set_new(Gee.List<Jid> jids) {
|
2018-03-07 16:24:57 +00:00
|
|
|
this.jids = jids;
|
|
|
|
update();
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 16:24:57 +00:00
|
|
|
private void update() {
|
|
|
|
if (image == null || label == null) return;
|
|
|
|
|
2019-10-18 14:52:29 +00:00
|
|
|
image.set_conversation_participants(stream_interactor, conversation, jids.to_array());
|
2018-03-07 16:24:57 +00:00
|
|
|
|
|
|
|
Gee.List<string> display_names = new ArrayList<string>();
|
|
|
|
foreach (Jid jid in jids) {
|
2019-10-18 14:52:29 +00:00
|
|
|
display_names.add(Util.get_participant_display_name(stream_interactor, conversation, jid));
|
2018-03-07 16:24:57 +00:00
|
|
|
}
|
|
|
|
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) {
|
2020-02-20 15:59:34 +00:00
|
|
|
new_text = _("%s, %s and %s are typing…").printf(display_names[0], display_names[1], display_names[2]);
|
2018-03-07 16:24:57 +00:00
|
|
|
} else if (jids.size == 2) {
|
2020-02-20 15:59:34 +00:00
|
|
|
new_text =_("%s and %s are typing…").printf(display_names[0], display_names[1]);
|
2018-03-07 16:24:57 +00:00
|
|
|
} else {
|
2020-02-20 15:59:34 +00:00
|
|
|
new_text = "%s is typing…".printf(display_names[0]);
|
2018-03-07 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
label.label = new_text;
|
2017-08-27 21:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|