using Gee; using Gdk; using Gtk; using Pango; using Xmpp; using Dino.Entities; namespace Dino.Ui.ConversationSummary { public class MessageMetaItem : ContentMetaItem { enum AdditionalInfo { NONE, PENDING, DELIVERY_FAILED } private StreamInteractor stream_interactor; private MessageItem message_item; public Message.Marked marked { get; set; } public Plugins.ConversationItemWidgetInterface outer = null; MessageItemEditMode? edit_mode = null; ChatTextViewController? controller = null; AdditionalInfo additional_info = AdditionalInfo.NONE; ulong realize_id = -1; ulong marked_notify_handler_id = -1; uint pending_timeout_id = -1; public Label label = new Label("") { use_markup=true, xalign=0, selectable=true, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, hexpand=true, vexpand=true }; private Widget? content_widget; private bool text_selectable = true; public MessageMetaItem(ContentItem content_item, StreamInteractor stream_interactor) { base(content_item); message_item = content_item as MessageItem; this.stream_interactor = stream_interactor; label.add_css_class("message-text"); stream_interactor.get_module(MessageCorrection.IDENTITY).received_correction.connect(on_updated_item); stream_interactor.get_module(MessageDeletion.IDENTITY).item_deleted.connect(on_updated_item); label.activate_link.connect(on_label_activate_link); Message message = ((MessageItem) content_item).message; if (message.direction == Message.DIRECTION_SENT && !(message.marked in Message.MARKED_RECEIVED)) { var binding = message.bind_property("marked", this, "marked"); marked_notify_handler_id = this.notify["marked"].connect(() => { // Currently "pending", but not anymore if (additional_info == AdditionalInfo.PENDING && message.marked != Message.Marked.SENDING && message.marked != Message.Marked.UNSENT) { update_label(); } // Currently "error", but not anymore if (additional_info == AdditionalInfo.DELIVERY_FAILED && message.marked != Message.Marked.ERROR) { update_label(); } // Currently not error, but should be if (additional_info != AdditionalInfo.DELIVERY_FAILED && message.marked == Message.Marked.ERROR) { update_label(); } // Nothing bad can happen anymore if (message.marked in Message.MARKED_RECEIVED) { binding.unbind(); this.disconnect(marked_notify_handler_id); marked_notify_handler_id = -1; } }); } update_label(); } private void generate_markup_text(Label label, string text, ArrayList markups, int action_name_length) { Message message = message_item.message; label.set_attributes(create_text_attributes(text, 0, markups, action_name_length)); bool theme_dependent; string markup_text = parse_text_markup(label, text, out theme_dependent); int only_emoji_count = Util.get_only_emoji_count(markup_text); if (only_emoji_count != -1) { string size_str = only_emoji_count < 5 ? "xx-large" : "large"; markup_text = @"" + markup_text + ""; } string dim_color = Util.is_dark_theme(label) ? "#BDBDBD" : "#707070"; if (message.body == "") { markup_text = @"%s".printf(_("Message deleted")); theme_dependent = true; } markup_text += get_meta_markup(label, true, ref theme_dependent); if (theme_dependent && realize_id == -1 && !label.get_realized()) { realize_id = label.realize.connect(update_label); } else if (!theme_dependent && realize_id != -1) { label.disconnect(realize_id); } label.label = markup_text; } private ArrayList copy_markups(Message message) { var markups = new ArrayList(); foreach (var markup in message.get_markups()) { markups.add(new Xep.MessageMarkup.Span() { types=markup.types, start_char=markup.start_char, end_char=markup.end_char }); } return markups; } private string prepare_message(ArrayList markups, out int action_name_length, bool truncate = true) { Message message = message_item.message; string text = message.body ?? ""; action_name_length = 0; if (truncate && text.char_count() > 10000) { text = text.substring(0, text.index_of_nth_char(10000)) + " [" + _("Message too long") + "]"; } text = Util.remove_fallbacks_adjust_markups(text, message.quoted_item_id > 0, message.get_fallbacks(), markups); if (text.has_prefix("/me ")) { string display_name = Util.get_participant_display_name(stream_interactor, message_item.conversation, message.from); text = display_name + " " + text.substring(4); action_name_length = display_name.char_count(); foreach (Xep.MessageMarkup.Span span in markups) { int difference = action_name_length - 3; span.start_char += difference; span.end_char += difference; } } return text; } private Label create_text_label() { var text_label = new Label("") { use_markup=true, xalign=0, selectable=text_selectable, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, hexpand=true }; text_label.add_css_class("message-text"); text_label.activate_link.connect(on_label_activate_link); return text_label; } public void set_text_selectable(bool selectable) { text_selectable = selectable; if (content_widget != null) set_text_selectable_recursive(content_widget, selectable); } private void set_text_selectable_recursive(Widget widget, bool selectable) { Label? text_label = widget as Label; if (text_label != null && text_label.has_css_class("message-text")) { text_label.selectable = selectable; } for (Widget? child = widget.get_first_child(); child != null; child = child.get_next_sibling()) { set_text_selectable_recursive(child, selectable); } } private void add_text_attribute(AttrList attributes, Attribute attribute, string text, int source_start, int span_start, int span_end) { int source_end = source_start + text.char_count(); int start = int.max(source_start, span_start); int end = int.min(source_end, span_end); if (start >= end) return; attribute.start_index = text.index_of_nth_char(start - source_start); attribute.end_index = text.index_of_nth_char(end - source_start); attributes.insert(attribute.copy()); } private AttrList create_text_attributes(string text, int source_start, ArrayList markups, int action_name_length) { var attributes = new AttrList(); foreach (var markup in markups) { foreach (var type in markup.types) { Attribute attribute; switch (type) { case Xep.MessageMarkup.SpanType.EMPHASIS: attribute = Pango.attr_style_new(Pango.Style.ITALIC); break; case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: attribute = Pango.attr_weight_new(Pango.Weight.BOLD); break; case Xep.MessageMarkup.SpanType.DELETED: attribute = Pango.attr_strikethrough_new(true); break; default: continue; } add_text_attribute(attributes, (owned) attribute, text, source_start, markup.start_char, markup.end_char); } } if (action_name_length > 0) { add_text_attribute(attributes, Pango.attr_weight_new(Pango.Weight.BOLD), text, source_start, 0, action_name_length); add_text_attribute(attributes, Pango.attr_style_new(Pango.Style.ITALIC), text, source_start, 0, action_name_length); } return attributes; } private string parse_text_markup(Label text_label, string text, out bool theme_dependent) { string markup_text = Util.unbreak_space_around_non_spacing_mark(text); theme_dependent = false; Conversation conversation = message_item.conversation; if (conversation.type_ == Conversation.Type.GROUPCHAT) { return Util.parse_add_markup_theme(markup_text, conversation.nickname, true, true, true, Util.is_dark_theme(text_label), ref theme_dependent); } return Util.parse_add_markup_theme(markup_text, null, true, true, true, Util.is_dark_theme(text_label), ref theme_dependent); } private void format_text_segment(Label text_label, CodeBlockPart part, ArrayList markups, int action_name_length) { text_label.set_attributes(create_text_attributes(part.text, part.source_start_char, markups, action_name_length)); bool theme_dependent; text_label.label = parse_text_markup(text_label, part.text, out theme_dependent); } private string get_meta_markup(Label meta_label, bool inline, ref bool theme_dependent) { Message message = message_item.message; string dim_color = Util.is_dark_theme(meta_label) ? "#BDBDBD" : "#707070"; string separator = inline ? " " : ""; string markup = ""; if (message.edit_to != null) { markup = @"$separator(%s)".printf(_("edited")); separator = " "; theme_dependent = true; } additional_info = AdditionalInfo.NONE; if (message.direction == Message.DIRECTION_SENT && (message.marked == Message.Marked.SENDING || message.marked == Message.Marked.UNSENT)) { if (message.time.compare(new DateTime.now_utc().add_seconds(-10)) < 0) { markup += @"$separator%s".printf(_("pending…")); theme_dependent = true; additional_info = AdditionalInfo.PENDING; } else { int time_diff = (- (int) message.time.difference(new DateTime.now_utc()) / 1000); pending_timeout_id = Timeout.add(10000 - time_diff, () => { update_label(); pending_timeout_id = -1; return false; }); } } else if (message.direction == Message.DIRECTION_SENT && message.marked == Message.Marked.ERROR) { string error_color = Util.rgba_to_hex(Util.get_label_pango_color(meta_label, "error_color")); markup += @"$separator%s".printf(_("delivery failed")); theme_dependent = true; additional_info = AdditionalInfo.DELIVERY_FAILED; } return markup; } private Widget generate_code_widget(ArrayList parts, ArrayList markups, int action_name_length) { var content = new Box(Orientation.VERTICAL, 6) { hexpand=true }; foreach (CodeBlockPart part in parts) { if (part.is_code) { content.append(new CodeBlockWidget(part.text, part.language)); } else { Label text_label = create_text_label(); format_text_segment(text_label, part, markups, action_name_length); content.append(text_label); } } Label meta_label = create_text_label(); bool theme_dependent = false; string meta_markup = get_meta_markup(meta_label, false, ref theme_dependent); if (meta_markup != "") { meta_label.label = meta_markup; content.append(meta_label); } return content; } public void update_label() { if (pending_timeout_id != -1) { Source.remove(pending_timeout_id); pending_timeout_id = -1; } if (realize_id != -1) { label.disconnect(realize_id); realize_id = -1; } var markups = copy_markups(message_item.message); int action_name_length; string text = prepare_message(markups, out action_name_length); bool has_blocks; ArrayList parts = CodeBlockParser.parse(text, out has_blocks); if (has_blocks) { content_widget = generate_code_widget(parts, markups, action_name_length); } else { generate_markup_text(label, text, markups, action_name_length); content_widget = label; } set_text_selectable_recursive(content_widget, text_selectable); if (outer != null && !in_edit_mode) { outer.set_widget(content_widget, Plugins.WidgetType.GTK4, 2); } } public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) { this.outer = outer; this.notify["in-edit-mode"].connect(on_in_edit_mode_changed); outer.set_widget(content_widget, Plugins.WidgetType.GTK4, 2); if (message_item.message.quoted_item_id > 0) { var quoted_content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_id(message_item.conversation, message_item.message.quoted_item_id); if (quoted_content_item != null) { var quote_model = new Quote.Model.from_content_item(quoted_content_item, message_item.conversation, stream_interactor); quote_model.jump_to.connect(() => { GLib.Application.get_default().activate_action("jump-to-conversation-message", new GLib.Variant.tuple(new GLib.Variant[] { new GLib.Variant.int32(message_item.conversation.id), new GLib.Variant.int32(quoted_content_item.id) })); }); var quote_widget = Quote.get_widget(quote_model); outer.set_widget(quote_widget, Plugins.WidgetType.GTK4, 1); } } return content_widget; } public override Gee.List? get_item_actions(Plugins.WidgetType type) { if (in_edit_mode) return null; Gee.List actions = new ArrayList(); bool correction_allowed = stream_interactor.get_module(MessageCorrection.IDENTITY).is_own_correction_allowed(message_item.conversation, message_item.message); if (correction_allowed) { Plugins.MessageAction action1 = new Plugins.MessageAction(); action1.name = "correction"; action1.icon_name = "dino-document-edit-symbolic"; action1.tooltip = _("Edit message"); action1.shortcut_action = false; action1.callback = () => { this.in_edit_mode = true; }; actions.add(action1); } Plugins.MessageAction copy_action = new Plugins.MessageAction(); copy_action.name = "copy"; copy_action.icon_name = "dino-edit-copy-symbolic"; copy_action.tooltip = _("Copy message"); copy_action.callback = () => { var markups = copy_markups(message_item.message); int action_name_length; string text = prepare_message(markups, out action_name_length, false); Gdk.Display? display = Gdk.Display.get_default(); if (display != null) display.get_clipboard().set_text(text); }; actions.add(copy_action); actions.add(get_reply_action(content_item, message_item.conversation, stream_interactor)); actions.add(get_reaction_action(content_item, message_item.conversation, stream_interactor)); var delete_action = get_delete_action(content_item, message_item.conversation, stream_interactor); if (delete_action != null) actions.add(delete_action); return actions; } private void on_in_edit_mode_changed() { if (in_edit_mode == false) return; bool allowed = stream_interactor.get_module(MessageCorrection.IDENTITY).is_own_correction_allowed(message_item.conversation, message_item.message); if (allowed) { MessageItem message_item = content_item as MessageItem; Message message = message_item.message; edit_mode = new MessageItemEditMode(); controller = new ChatTextViewController(edit_mode.chat_text_view, stream_interactor); Conversation conversation = message_item.conversation; controller.initialize_for_conversation(conversation); edit_mode.cancelled.connect(() => { in_edit_mode = false; outer.set_widget(content_widget, Plugins.WidgetType.GTK4, 2); }); edit_mode.send.connect(() => { string text = edit_mode.chat_text_view.text_view.buffer.text; var markups = edit_mode.chat_text_view.get_markups(); Dino.send_message(message_item.conversation, text, message_item.message.quoted_item_id, message_item.message, markups); in_edit_mode = false; outer.set_widget(content_widget, Plugins.WidgetType.GTK4, 2); }); edit_mode.chat_text_view.set_text(message); outer.set_widget(edit_mode, Plugins.WidgetType.GTK4, 2); edit_mode.chat_text_view.text_view.grab_focus(); } else { this.in_edit_mode = false; } } private void on_updated_item(ContentItem content_item) { if (this.content_item.id == content_item.id) { this.content_item = content_item; message_item = content_item as MessageItem; update_label(); } } public static bool on_label_activate_link(string uri) { // Always handle xmpp URIs with Dino if (!uri.has_prefix("xmpp:")) return false; File file = File.new_for_uri(uri); Dino.Application.get_default().open(new File[]{file}, ""); return true; } public override void dispose() { stream_interactor.get_module(MessageCorrection.IDENTITY).received_correction.disconnect(on_updated_item); stream_interactor.get_module(MessageDeletion.IDENTITY).item_deleted.disconnect(on_updated_item); this.notify["in-edit-mode"].disconnect(on_in_edit_mode_changed); if (marked_notify_handler_id != -1) { this.disconnect(marked_notify_handler_id); } if (realize_id != -1) { label.disconnect(realize_id); } if (pending_timeout_id != -1) { Source.remove(pending_timeout_id); } if (label != null) { label.unparent(); label.dispose(); label = null; } base.dispose(); } } [GtkTemplate (ui = "/im/dino/Dino/message_item_widget_edit_mode.ui")] public class MessageItemEditMode : Box { public signal void cancelled(); public signal void send(); [GtkChild] public unowned MenuButton emoji_button; [GtkChild] public unowned ChatTextView chat_text_view; [GtkChild] public unowned Button cancel_button; [GtkChild] public unowned Button send_button; [GtkChild] public unowned Frame frame; construct { EmojiChooser chooser = new EmojiChooser(); chooser.emoji_picked.connect((emoji) => { chat_text_view.text_view.buffer.insert_at_cursor(emoji, emoji.data.length); }); emoji_button.set_popover(chooser); chat_text_view.text_view.buffer.changed.connect_after(on_text_view_changed); cancel_button.clicked.connect(() => cancelled()); send_button.clicked.connect(() => send()); chat_text_view.cancel_input.connect(() => cancelled()); chat_text_view.send_text.connect(() => send()); check_widget_leak(this); } private void on_text_view_changed() { send_button.sensitive = chat_text_view.text_view.buffer.text != ""; } } }