aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_content_view/unread_indicator_populator.vala
blob: 79aa75762c07e4f88c56887d0e532935c85e0a85 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
using Gee;
using Gtk;

using Dino.Entities;
using Xmpp;

namespace Dino.Ui.ConversationSummary {

    class UnreadIndicatorPopulator : Plugins.ConversationItemPopulator, Plugins.ConversationAdditionPopulator, Object {

        public string id { get { return "unread_indicator"; } }

        private StreamInteractor stream_interactor;
        private Conversation? current_conversation;
        private UnreadIndicatorItem? unread_indicator = null;
        Plugins.ConversationItemCollection item_collection = null;

        public UnreadIndicatorPopulator(StreamInteractor stream_interactor) {
            this.stream_interactor = stream_interactor;

            stream_interactor.get_module(ChatInteraction.IDENTITY).focused_out.connect(() => {
                update_unread_indicator();
            });

            stream_interactor.get_module(ContentItemStore.IDENTITY).new_item.connect(() => {
                if (!stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus(current_conversation)) {
                    update_unread_indicator();
                }
            });
        }

        private void update_unread_indicator() {
            if (current_conversation == null) return;

            ContentItem? read_up_to_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item_by_id(current_conversation, current_conversation.read_up_to_item);
            int current_num_unread = stream_interactor.get_module(ChatInteraction.IDENTITY).get_num_unread(current_conversation);
            if (current_num_unread == 0 && unread_indicator != null) {
                item_collection.remove_item(unread_indicator);
                unread_indicator = null;
            }

            if (read_up_to_item != null && current_num_unread > 0) {
                if (unread_indicator != null) {
                    item_collection.remove_item(unread_indicator);
                }

                unread_indicator = new UnreadIndicatorItem(read_up_to_item);
                item_collection.insert_item(unread_indicator);
            }
        }

        public void init(Conversation conversation, Plugins.ConversationItemCollection item_collection, Plugins.WidgetType type) {
            current_conversation = conversation;
            this.item_collection = item_collection;
            update_unread_indicator();
        }

        public void close(Conversation conversation) { }

        public void populate_timespan(Conversation conversation, DateTime after, DateTime before) { }
    }

    private class UnreadIndicatorItem : Plugins.MetaConversationItem {
        public UnreadIndicatorItem(ContentItem after_item) {
            this.time = after_item.time;
            this.secondary_sort_indicator = int.MAX;
        }

        public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) {
            Box box = new Box(Orientation.HORIZONTAL, 10) { hexpand=true };
            box.get_style_context().add_class("dino-unread-line");

            Separator sep = new Separator(Orientation.HORIZONTAL) { valign=Align.CENTER, hexpand=true };
            box.append(sep);

            Label label = new Label(_("New")) { halign=Align.END, hexpand=false };
            label.attributes = new Pango.AttrList();
            label.attributes.insert(Pango.attr_weight_new(Pango.Weight.BOLD));
            box.append(label);

            return box;
        }

        public override Gee.List<Plugins.MessageAction>? get_item_actions(Plugins.WidgetType type) {
            return null;
        }
    }
}