aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/presence/flag.vala
blob: 8e13d0ad1e050caecff3dbd5a08c10b4d16491b7 (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
using Gee;

namespace Xmpp.Presence {

public class Flag : XmppStreamFlag {
    public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "presence");

    private HashMap<Jid, Gee.List<Jid>> resources = new HashMap<Jid, Gee.List<Jid>>(Jid.hash_bare_func, Jid.equals_bare_func);
    private HashMap<Jid, Presence.Stanza> presences = new HashMap<Jid, Presence.Stanza>(Jid.hash_func, Jid.equals_func);

    public Set<Jid> get_available_jids() {
        return resources.keys;
    }

    public Gee.List<Jid>? get_resources(Jid jid) {
        return resources[jid];
    }

    public Presence.Stanza? get_presence(Jid full_jid) {
        return presences[full_jid];
    }

    public Gee.List<Presence.Stanza> get_presences(Jid jid) {
        Gee.List<Presence.Stanza> ret = new ArrayList<Presence.Stanza>();
        Gee.List<Jid>? jid_res = resources[jid];
        if (jid_res == null) return ret;

        foreach (Jid full_jid in jid_res) {
            ret.add(presences[full_jid]);
        }
        return ret;
    }

    public void add_presence(Presence.Stanza presence) {
        if (!resources.has_key(presence.from)) {
            resources[presence.from] = new ArrayList<Jid>(Jid.equals_func);
        }
        if (resources[presence.from].contains(presence.from)) {
            resources[presence.from].remove(presence.from);
        }
        resources[presence.from].add(presence.from);
        presences[presence.from] = presence;
    }

    public void remove_presence(Jid jid) {
        if (resources.has_key(jid)) {
            if (jid.is_bare()) {
                foreach (Jid full_jid in resources[jid]) {
                    presences.unset(full_jid);
                }
                resources.unset(jid);
            } else {
                resources[jid].remove(jid);
                if (resources[jid].size == 0) {
                    resources.unset(jid);
                }
                presences.unset(jid);
            }
        }
    }

    public override string get_ns() {
        return NS_URI;
    }

    public override string get_id() {
        return IDENTITY.id;
    }
}

}