/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tigase.jaxmpp.core.client.xmpp.modules;

import java.util.List;

import tigase.jaxmpp.core.client.AsyncCallback;
import tigase.jaxmpp.core.client.PacketWriter;
import tigase.jaxmpp.core.client.SessionObject;
import tigase.jaxmpp.core.client.XMPPException;
import tigase.jaxmpp.core.client.criteria.Criteria;
import tigase.jaxmpp.core.client.criteria.ElementCriteria;
import tigase.jaxmpp.core.client.exceptions.JaxmppException;
import tigase.jaxmpp.core.client.xml.DefaultElement;
import tigase.jaxmpp.core.client.xml.Element;
import tigase.jaxmpp.core.client.xml.XMLException;
import tigase.jaxmpp.core.client.xmpp.stanzas.IQ;
import tigase.jaxmpp.core.client.xmpp.stanzas.Stanza;
import tigase.jaxmpp.core.client.xmpp.stanzas.StanzaType;

/**
 * 
 * @author andrzej
 */
public class BookmarksModule extends AbstractIQModule {

	public static abstract class BookmarksAsyncCallback implements AsyncCallback {

		public abstract void onBookmarksReceived(List<Element> bookmarks);

		@Override
		public void onSuccess(final Stanza stanza) throws XMLException {
			Element query = stanza.getChildrenNS("query", "jabber:iq:private");
			Element storage = query.getChildrenNS("storage", BOOKMARKS_XMLNS);
			onBookmarksReceived(storage.getChildren());
		}
	}

	private static final String BOOKMARKS_XMLNS = "storage:bookmarks";

	private static final Criteria CRIT = ElementCriteria.name("storage", BOOKMARKS_XMLNS);

	private static final String[] FEATURES = { BOOKMARKS_XMLNS };

	public BookmarksModule(SessionObject sessionObject, PacketWriter packetWriter) {
		super(sessionObject, packetWriter);
	}

	@Override
	public Criteria getCriteria() {
		return CRIT;
	}

	@Override
	public String[] getFeatures() {
		return FEATURES;
	}

	@Override
	protected void processGet(IQ element) throws JaxmppException {
		throw new XMPPException(XMPPException.ErrorCondition.not_allowed);
	}

	@Override
	protected void processSet(IQ element) throws JaxmppException {
		throw new XMPPException(XMPPException.ErrorCondition.not_allowed);
	}

	public void retrieveBookmarks(BookmarksAsyncCallback callback) throws JaxmppException {
		IQ iq = IQ.create();
		iq.setType(StanzaType.get);

		Element query = new DefaultElement("query");
		query.setXMLNS("jabber:iq:private");
		iq.addChild(query);

		Element storage = new DefaultElement("storage");
		storage.setXMLNS(BOOKMARKS_XMLNS);
		query.addChild(storage);

		this.writer.write(iq, callback);
	}

}

@@ -2535,10 +2535,544 @@
 k);%0A%09%7D%0A%0A
+%09public void publishBookmarks(List%3CElement%3E bookmarks, AsyncCallback callback) throws JaxmppException %7B%0A%09%09IQ iq = IQ.create();%0A%09%09iq.setType(StanzaType.set);%0A%0A%09%09Element query = new DefaultElement(%22query%22);%0A%09%09query.setXMLNS(%22jabber:iq:private%22);%0A%09%09iq.addChild(query);%0A%0A%09%09Element storage = new DefaultElement(%22storage%22);%0A%09%09storage.setXMLNS(BOOKMARKS_XMLNS);%0A%09%09query.addChild(storage);%0A%09%09%0A%09%09if (bookmarks != null) %7B%0A%09%09%09for (Element bookmark : bookmarks) %7B%0A%09%09%09%09storage.addChild(bookmark);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09this.writer.write(iq, callback);%0A%09%7D%0A
 %7D%0A

Support for publishing bookmarks