aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_summary/message_populator.vala
blob: f6d55a929c6dcdd8c40b686ce1a42e3bc810f0b6 (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
using Gee;
using Gtk;

using Dino.Entities;

namespace Dino.Ui.ConversationSummary {

public class MessagePopulator : Object {

    private StreamInteractor? stream_interactor;
    private Conversation? current_conversation;
    private Plugins.ConversationItemCollection? item_collection;

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

        Application app = GLib.Application.get_default() as Application;
        app.plugin_registry.register_message_display(new DefaultMessageDisplay(stream_interactor));
        app.plugin_registry.register_message_display(new SlashmeMessageDisplay(stream_interactor));


        stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect((message, conversation) => {
            Idle.add(() => { handle_message(message, conversation); return false; });
        });
        stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect((message, conversation) => {
            Idle.add(() => { handle_message(message, conversation); return false; });
        });
    }

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

    public void close(Conversation conversation) { }

    public void populate_number(Conversation conversation, DateTime from, int n) {
        Gee.List<Entities.Message>? messages = stream_interactor.get_module(MessageStorage.IDENTITY).get_messages_before(conversation, from, n);
        if (messages != null) {
            foreach (Entities.Message message in messages) {
                handle_message(message, conversation);
            }
        }
    }

    private void handle_message(Message message, Conversation conversation) {
        if (!conversation.equals(current_conversation)) return;

        Plugins.MessageDisplayProvider? best_provider = null;
        double priority = -1;
        Application app = GLib.Application.get_default() as Application;
        foreach (Plugins.MessageDisplayProvider provider in app.plugin_registry.message_displays) {
            if (provider.can_display(message) && provider.priority > priority) {
                best_provider = provider;
                priority = provider.priority;
            }
        }
        Plugins.MetaConversationItem? meta_item = best_provider.get_item(message, conversation);
        if (meta_item == null) return;

        meta_item.mark = message.marked;
        message.notify["marked"].connect(() => {
            meta_item.mark = message.marked;
        });
        item_collection.insert_item(meta_item);
    }
}

}