diff options
Diffstat (limited to 'vala-xmpp/src/module/xep/0048_bookmarks')
-rw-r--r-- | vala-xmpp/src/module/xep/0048_bookmarks/conference.vala | 74 | ||||
-rw-r--r-- | vala-xmpp/src/module/xep/0048_bookmarks/module.vala | 137 |
2 files changed, 211 insertions, 0 deletions
diff --git a/vala-xmpp/src/module/xep/0048_bookmarks/conference.vala b/vala-xmpp/src/module/xep/0048_bookmarks/conference.vala new file mode 100644 index 00000000..818ab3d0 --- /dev/null +++ b/vala-xmpp/src/module/xep/0048_bookmarks/conference.vala @@ -0,0 +1,74 @@ +using Xmpp.Core; + +namespace Xmpp.Xep.Bookmarks { + +public class Conference { + + public const string ATTRIBUTE_AUTOJOIN = "autojoin"; + public const string ATTRIBUTE_JID = "jid"; + public const string ATTRIBUTE_NAME = "name"; + + public const string NODE_NICK = "nick"; + public const string NODE_PASSWORD = "password"; + + public StanzaNode stanza_node; + + public bool autojoin { + get { + string? attr = stanza_node.get_attribute(ATTRIBUTE_AUTOJOIN); + return attr == "true" || attr == "1"; // "1" isn't standard, but it's used + } + set { stanza_node.set_attribute(ATTRIBUTE_AUTOJOIN, value.to_string()); } + } + + public string jid { + get { return stanza_node.get_attribute(ATTRIBUTE_JID); } + set { stanza_node.set_attribute(ATTRIBUTE_JID, value); } + } + + public string? name { + get { return stanza_node.get_attribute(ATTRIBUTE_NAME); } + set { stanza_node.set_attribute(ATTRIBUTE_NAME, value); } + } + + public string? nick { + get { + StanzaNode? nick_node = stanza_node.get_subnode(NODE_NICK); + return nick_node == null? null : nick_node.get_string_content(); + } + set { + StanzaNode? nick_node = stanza_node.get_subnode(NODE_NICK); + if (nick_node == null) { + nick_node = new StanzaNode.build(NODE_NICK, NS_URI); + stanza_node.put_node(nick_node); + } + nick_node.put_node(new StanzaNode.text(value)); + } + } + + public string? password { + get { + StanzaNode? password_node = stanza_node.get_subnode(NODE_PASSWORD); + return password_node == null? null : password_node.get_string_content(); + } + set { + StanzaNode? password_node = stanza_node.get_subnode(NODE_PASSWORD); + if (password_node == null) { + password_node = new StanzaNode.build(NODE_PASSWORD); + stanza_node.put_node(password_node); + } + password_node.put_node(new StanzaNode.text(value)); + } + } + + public Conference.from_stanza_node(StanzaNode stanza_node) { + this.stanza_node = stanza_node; + } + + public Conference(string jid) { + this.stanza_node = new StanzaNode.build("conference", NS_URI); + this.jid = jid; + } +} + +}
\ No newline at end of file diff --git a/vala-xmpp/src/module/xep/0048_bookmarks/module.vala b/vala-xmpp/src/module/xep/0048_bookmarks/module.vala new file mode 100644 index 00000000..d7767208 --- /dev/null +++ b/vala-xmpp/src/module/xep/0048_bookmarks/module.vala @@ -0,0 +1,137 @@ +using Gee; + +using Xmpp.Core; + +namespace Xmpp.Xep.Bookmarks { +private const string NS_URI = "storage:bookmarks"; + +public class Module : XmppStreamModule { + public const string ID = "0048_bookmarks_module"; + + public signal void conferences_updated(XmppStream stream, ArrayList<Conference> conferences); + + public void get_conferences(XmppStream stream, ConferencesRetrieveResponseListener response_listener) { + StanzaNode get_node = new StanzaNode.build("storage", NS_URI).add_self_xmlns(); + PrivateXmlStorage.Module.get_module(stream).retrieve(stream, get_node, new GetConferences(response_listener)); + } + + public void set_conferences(XmppStream stream, ArrayList<Conference> conferences) { + StanzaNode storage_node = (new StanzaNode.build("storage", NS_URI)).add_self_xmlns(); + foreach (Conference conference in conferences) { + storage_node.put_node(conference.stanza_node); + } + PrivateXmlStorage.Module.get_module(stream).store(stream, storage_node, new StoreResponseListenerImpl(conferences)); + } + + private class StoreResponseListenerImpl : PrivateXmlStorage.StoreResponseListener, Object { + ArrayList<Conference> conferences; + public StoreResponseListenerImpl(ArrayList<Conference> conferences) { + this.conferences = conferences; + } + public void on_success(XmppStream stream) { + Module.get_module(stream).conferences_updated(stream, conferences); + } + } + + public void add_conference(XmppStream stream, Conference add) { + get_conferences(stream, new AddConference(add)); + } + + public void replace_conference(XmppStream stream, Conference was, Conference modified) { + get_conferences(stream, new ModifyConference(was, modified)); + } + + public void remove_conference(XmppStream stream, Conference conference) { + get_conferences(stream, new RemoveConference(conference)); + } + + private class GetConferences : PrivateXmlStorage.RetrieveResponseListener, Object { + ConferencesRetrieveResponseListener response_listener; + + public GetConferences(ConferencesRetrieveResponseListener response_listener) { + this.response_listener = response_listener; + } + + public void on_result(XmppStream stream, StanzaNode node) { + response_listener.on_result(stream, get_conferences_from_stanza(node)); + } + } + + private class AddConference : ConferencesRetrieveResponseListener, Object { + private Conference conference; + public AddConference(Conference conference) { + this.conference = conference; + } + public void on_result(XmppStream stream, ArrayList<Conference> conferences) { + conferences.add(conference); + Module.get_module(stream).set_conferences(stream, conferences); + } + } + + private class ModifyConference : ConferencesRetrieveResponseListener, Object { + private Conference was; + private Conference modified; + public ModifyConference(Conference was, Conference modified) { + this.was = was; + this.modified = modified; + } + public void on_result(XmppStream stream, ArrayList<Conference> conferences) { + foreach (Conference conference in conferences) { + if (conference.name == was.name && conference.jid == was.jid && conference.autojoin == was.autojoin) { + conference.autojoin = modified.autojoin; + conference.name = modified.name; + conference.jid = modified.jid; + break; + } + } + Module.get_module(stream).set_conferences(stream, conferences); + } + } + + private class RemoveConference : ConferencesRetrieveResponseListener, Object { + private Conference remove; + public RemoveConference(Conference remove) { + this.remove = remove; + } + public void on_result(XmppStream stream, ArrayList<Conference> conferences) { + Conference? rem = null; + foreach (Conference conference in conferences) { + if (conference.name == remove.name && conference.jid == remove.jid && conference.autojoin == remove.autojoin) { + rem = conference; + } + } + if (rem != null) conferences.remove(rem); + Module.get_module(stream).set_conferences(stream, conferences); + } + } + + public override void attach(XmppStream stream) { } + + public override void detach(XmppStream stream) { } + + public static Module? get_module(XmppStream stream) { + return (Module?) stream.get_module(NS_URI, ID); + } + + public static void require(XmppStream stream) { + if (get_module(stream) == null) stderr.printf(""); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return ID; } + + private static ArrayList<Conference> get_conferences_from_stanza(StanzaNode node) { + ArrayList<Conference> conferences = new ArrayList<Conference>(); + ArrayList<StanzaNode> conferenceNodes = node.get_subnode("storage", NS_URI).get_subnodes("conference", NS_URI); + foreach (StanzaNode conferenceNode in conferenceNodes) { + conferences.add(new Conference.from_stanza_node(conferenceNode)); + } + return conferences; + } +} + +public interface ConferencesRetrieveResponseListener : Object { + public abstract void on_result(XmppStream stream, ArrayList<Conference> conferences); +} + +} |