aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_summary/slashme_message_display.vala
blob: f57950bbd41a1e6457192cd473d436eba5ed173f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Gtk;

using Dino.Entities;

namespace Dino.Ui.ConversationSummary {

public class SlashmeMessageDisplay : Plugins.MessageDisplayProvider, Object {
    public string id { get; set; default="slashme"; }
    public double priority { get; set; default=1; }

    public StreamInteractor stream_interactor;

    public SlashmeMessageDisplay(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
    }

    public bool can_display(Entities.Message? message) {
        return message.body.has_prefix("/me");
    }

    public Plugins.MetaConversationItem? get_item(Entities.Message message, Conversation conversation) {
        return new MetaSlashmeItem(stream_interactor, message, conversation);
    }
}

public class MetaSlashmeItem : Plugins.MetaConversationItem {
    public override Jid? jid { get; set; }
    public override DateTime? sort_time { get; set; }
    public override DateTime? display_time { get; set; }
    public override Encryption? encryption { get; set; }

    private StreamInteractor stream_interactor;
    private Conversation conversation;
    private Message message;
    private TextTag nick_tag;
    private MessageTextView text_view;

    public MetaSlashmeItem(StreamInteractor stream_interactor, Message message, Conversation conversation) {
        this.stream_interactor = stream_interactor;
        this.conversation = conversation;
        this.message = message;
        this.jid = message.from;
        this.sort_time = message.local_time;
        this.seccondary_sort_indicator = message.id + 0.0845;
        this.display_time = message.time;
        this.encryption = message.encryption;
    }

    public override bool can_merge { get; set; default=false; }
    public override bool requires_avatar { get; set; default=true; }
    public override bool requires_header { get; set; default=false; }

    public override Object? get_widget(Plugins.WidgetType widget_type) {
        text_view = new MessageTextView() { valign=Align.CENTER, vexpand=true, visible = true };

        string display_name = Util.get_message_display_name(stream_interactor, message, conversation.account);
        string color = Util.get_name_hex_color(stream_interactor, conversation.account, conversation.counterpart, Util.is_dark_theme(text_view));
        nick_tag = text_view.buffer.create_tag("nick", foreground: "#" + color);
        TextIter iter;
        text_view.buffer.get_start_iter(out iter);
        text_view.buffer.insert_with_tags(ref iter, display_name, display_name.length, nick_tag);
        text_view.add_text(message.body.substring(3));

        text_view.style_updated.connect(update_style);
        text_view.realize.connect(update_style);
        return text_view;
    }

    private void update_style() {
        string color = Util.get_name_hex_color(stream_interactor, conversation.account, message.real_jid ?? message.from, Util.is_dark_theme(text_view));
        nick_tag.foreground = "#" + color;
    }
}

}