| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Presence { |
| 4 |
|
| 5 |
public class Flag : XmppStreamFlag { |
| 6 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "presence"); |
| 7 |
|
| 8 |
private HashMap<Jid, Gee.List<Jid>> resources = new HashMap<Jid, Gee.List<Jid>>(Jid.hash_bare_func, Jid.equals_bare_func); |
| 9 |
private HashMap<Jid, Presence.Stanza> presences = new HashMap<Jid, Presence.Stanza>(Jid.hash_func, Jid.equals_func); |
| 10 |
|
| 11 |
public Set<Jid> get_available_jids() { |
| 12 |
return resources.keys; |
| 13 |
} |
| 14 |
|
| 15 |
public Gee.List<Jid>? get_resources(Jid jid) { |
| 16 |
if (!resources.has_key(jid)) return null; |
| 17 |
ArrayList<Jid> ret = new ArrayList<Jid>(Jid.equals_func); |
| 18 |
ret.add_all(resources[jid]); |
| 19 |
return ret; |
| 20 |
} |
| 21 |
|
| 22 |
public Presence.Stanza? get_presence(Jid full_jid) { |
| 23 |
return presences[full_jid]; |
| 24 |
} |
| 25 |
|
| 26 |
public Gee.List<Presence.Stanza> get_presences(Jid jid) { |
| 27 |
Gee.List<Presence.Stanza> ret = new ArrayList<Presence.Stanza>(); |
| 28 |
Gee.List<Jid>? jid_res = resources[jid]; |
| 29 |
if (jid_res == null) return ret; |
| 30 |
|
| 31 |
foreach (Jid full_jid in jid_res) { |
| 32 |
ret.add(presences[full_jid]); |
| 33 |
} |
| 34 |
return ret; |
| 35 |
} |
| 36 |
|
| 37 |
public void add_presence(Presence.Stanza presence) { |
| 38 |
if (!resources.has_key(presence.from)) { |
| 39 |
resources[presence.from] = new ArrayList<Jid>(Jid.equals_func); |
| 40 |
} |
| 41 |
if (resources[presence.from].contains(presence.from)) { |
| 42 |
resources[presence.from].remove(presence.from); |
| 43 |
} |
| 44 |
resources[presence.from].add(presence.from); |
| 45 |
presences[presence.from] = presence; |
| 46 |
} |
| 47 |
|
| 48 |
public void remove_presence(Jid jid) { |
| 49 |
if (resources.has_key(jid)) { |
| 50 |
if (jid.is_bare()) { |
| 51 |
foreach (Jid full_jid in resources[jid]) { |
| 52 |
presences.unset(full_jid); |
| 53 |
} |
| 54 |
resources.unset(jid); |
| 55 |
} else { |
| 56 |
resources[jid].remove(jid); |
| 57 |
if (resources[jid].size == 0) { |
| 58 |
resources.unset(jid); |
| 59 |
} |
| 60 |
presences.unset(jid); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public override string get_ns() { |
| 66 |
return NS_URI; |
| 67 |
} |
| 68 |
|
| 69 |
public override string get_id() { |
| 70 |
return IDENTITY.id; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |