aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/roster_manager.vala
blob: bb9fbd3a61cbe90113ea7768d8edb4400cc19019 (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
using Gee;

using Xmpp;
using Dino.Entities;

namespace Dino {

public class RosterManager : StreamInteractionModule, Object {
    public static ModuleIdentity<RosterManager> IDENTITY = new ModuleIdentity<RosterManager>("roster_manager");
    public string id { get { return IDENTITY.id; } }

    public signal void removed_roster_item(Account account, Jid jid, Roster.Item roster_item);
    public signal void updated_roster_item(Account account, Jid jid, Roster.Item roster_item);

    private StreamInteractor stream_interactor;

    public static void start(StreamInteractor stream_interactor) {
        RosterManager m = new RosterManager(stream_interactor);
        stream_interactor.add_module(m);
    }

    public RosterManager(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
        stream_interactor.account_added.connect(on_account_added);
    }

    public ArrayList<Roster.Item> get_roster(Account account) {
        Core.XmppStream? stream = stream_interactor.get_stream(account);
        ArrayList<Roster.Item> ret = new ArrayList<Roster.Item>();
        if (stream != null) {
            ret.add_all(stream.get_flag(Roster.Flag.IDENTITY).get_roster());
        }
        return ret;
    }

    public Roster.Item? get_roster_item(Account account, Jid jid) {
        Core.XmppStream? stream = stream_interactor.get_stream(account);
        if (stream == null) return null;
        Xmpp.Roster.Flag? flag = stream.get_flag(Xmpp.Roster.Flag.IDENTITY);
        if (flag == null) return null;
        return flag.get_item(jid.bare_jid.to_string());
    }

    public void remove_jid(Account account, Jid jid) {
        Core.XmppStream? stream = stream_interactor.get_stream(account);
        if (stream != null) stream.get_module(Xmpp.Roster.Module.IDENTITY).remove_jid(stream, jid.bare_jid.to_string());
    }

    public void add_jid(Account account, Jid jid, string? handle) {
        Core.XmppStream? stream = stream_interactor.get_stream(account);
        if (stream != null) stream.get_module(Xmpp.Roster.Module.IDENTITY).add_jid(stream, jid.bare_jid.to_string(), handle);
    }

    private void on_account_added(Account account) {
        stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).received_roster.connect( (stream, roster) => {
            on_roster_received(account, roster);
        });
        stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_removed.connect( (stream, roster_item) => {
            removed_roster_item(account, new Jid(roster_item.jid), roster_item);
        });
        stream_interactor.module_manager.get_module(account, Roster.Module.IDENTITY).item_updated.connect( (stream, roster_item) => {
            on_roster_item_updated(account, roster_item);
        });
    }

    private void on_roster_received(Account account, Collection<Roster.Item> roster_items) {
        foreach (Roster.Item roster_item in roster_items) {
            on_roster_item_updated(account, roster_item);
        }
    }

    private void on_roster_item_updated(Account account, Roster.Item roster_item) {
        updated_roster_item(account, new Jid(roster_item.jid), roster_item);
    }
}

}