Debian Codegraph @

xmpp-vala/src/module/xep/0048_bookmarks.vala

raw
1 using Gee;
2
3 namespace Xmpp.Xep.Bookmarks {
4 private const string NS_URI = "storage:bookmarks";
5
6 public class Module : BookmarksProvider, XmppStreamModule {
7 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0048_bookmarks_module");
8
9 public async Set<Conference>? get_conferences(XmppStream stream) {
10 StanzaNode get_node = new StanzaNode.build("storage", NS_URI).add_self_xmlns();
11 StanzaNode? result_node = yield stream.get_module(PrivateXmlStorage.Module.IDENTITY).retrieve(stream, get_node);
12 if (result_node == null) return null;
13
14 Set<Conference> ret = new HashSet<Conference>(Conference.hash_func, Conference.equals_func);
15 Gee.List<StanzaNode> conferences_node = result_node.get_subnode("storage", NS_URI).get_subnodes("conference", NS_URI);
16 foreach (StanzaNode conference_node in conferences_node) {
17 Conference? conference = Bookmarks1Conference.create_from_stanza_node(conference_node);
18 ret.add(conference);
19 }
20 return ret;
21 }
22
23 private async void set_conferences(XmppStream stream, Set<Conference> conferences) {
24 StanzaNode storage_node = (new StanzaNode.build("storage", NS_URI)).add_self_xmlns();
25 foreach (Conference conference in conferences) {
26 Bookmarks1Conference? bookmarks1conference = conference as Bookmarks1Conference;
27 if (bookmarks1conference != null) {
28 storage_node.put_node(bookmarks1conference.stanza_node);
29 } else {
30 StanzaNode conference_node = new StanzaNode.build("conference", NS_URI)
31 .put_attribute("jid", conference.jid.to_string())
32 .put_attribute("autojoin", conference.autojoin ? "true" : "false");
33 if (conference.name != null) {
34 conference_node.put_attribute("name", conference.name);
35 }
36 if (conference.nick != null) {
37 conference_node.put_node(new StanzaNode.build("nick", NS_URI)
38 .put_node(new StanzaNode.text(conference.nick)));
39 }
40 // TODO (?) Bookmarks 2 currently doesn't define a password
41 storage_node.put_node(conference_node);
42 }
43 }
44 yield stream.get_module(PrivateXmlStorage.Module.IDENTITY).store(stream, storage_node);
45 stream.get_module(Module.IDENTITY).received_conferences(stream, conferences);
46 }
47
48 public async void add_conference(XmppStream stream, Conference conference) {
49 Set<Conference>? conferences = yield get_conferences(stream);
50 conferences.add(conference);
51 yield set_conferences(stream, conferences);
52 }
53
54 public async void replace_conference(XmppStream stream, Jid muc_jid, Conference modified_conference) {
55 Set<Conference>? conferences = yield get_conferences(stream);
56 foreach (Conference conference in conferences) {
57 if (conference.jid.equals(muc_jid)) {
58 conference.autojoin = modified_conference.autojoin;
59 conference.name = modified_conference.name;
60 conference.nick = modified_conference.nick;
61 conference.password = modified_conference.password;
62 }
63 }
64 yield set_conferences(stream, conferences);
65 }
66
67 public async void remove_conference(XmppStream stream, Conference conference_remove) {
68 Set<Conference>? conferences = yield get_conferences(stream);
69 conferences.remove(conference_remove);
70 yield set_conferences(stream, conferences);
71 }
72
73 public override void attach(XmppStream stream) { }
74
75 public override void detach(XmppStream stream) { }
76
77 public override string get_ns() { return NS_URI; }
78 public override string get_id() { return IDENTITY.id; }
79 }
80
81 }