MessagePacket.java

 1package eu.siacs.conversations.xmpp.stanzas;
 2
 3import android.util.Pair;
 4
 5import eu.siacs.conversations.parser.AbstractParser;
 6import eu.siacs.conversations.xml.Element;
 7import eu.siacs.conversations.xml.LocalizedContent;
 8
 9public class MessagePacket extends AbstractAcknowledgeableStanza {
10	public static final int TYPE_CHAT = 0;
11	public static final int TYPE_NORMAL = 2;
12	public static final int TYPE_GROUPCHAT = 3;
13	public static final int TYPE_ERROR = 4;
14	public static final int TYPE_HEADLINE = 5;
15
16	public MessagePacket() {
17		super("message");
18	}
19
20	public LocalizedContent getBody() {
21		return findInternationalizedChildContentInDefaultNamespace("body");
22	}
23
24	public void setBody(String text) {
25		removeChild(findChild("body"));
26		prependChild(new Element("body").setContent(text));
27	}
28
29	public void setAxolotlMessage(Element axolotlMessage) {
30		removeChild(findChild("body"));
31		prependChild(axolotlMessage);
32	}
33
34	public void setType(int type) {
35		switch (type) {
36		case TYPE_CHAT:
37			this.setAttribute("type", "chat");
38			break;
39		case TYPE_GROUPCHAT:
40			this.setAttribute("type", "groupchat");
41			break;
42		case TYPE_NORMAL:
43			break;
44		case TYPE_ERROR:
45			this.setAttribute("type","error");
46			break;
47		default:
48			this.setAttribute("type", "chat");
49			break;
50		}
51	}
52
53	public int getType() {
54		String type = getAttribute("type");
55		if (type == null) {
56			return TYPE_NORMAL;
57		} else if (type.equals("normal")) {
58			return TYPE_NORMAL;
59		} else if (type.equals("chat")) {
60			return TYPE_CHAT;
61		} else if (type.equals("groupchat")) {
62			return TYPE_GROUPCHAT;
63		} else if (type.equals("error")) {
64			return TYPE_ERROR;
65		} else if (type.equals("headline")) {
66			return TYPE_HEADLINE;
67		} else {
68			return TYPE_NORMAL;
69		}
70	}
71
72	public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
73		Element wrapper = findChild(name, namespace);
74		if (wrapper == null) {
75			return null;
76		}
77		Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
78		if (forwarded == null) {
79			return null;
80		}
81		MessagePacket packet = create(forwarded.findChild("message"));
82		if (packet == null) {
83			return null;
84		}
85		Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
86		return new Pair(packet,timestamp);
87	}
88
89	public static MessagePacket create(Element element) {
90		if (element == null) {
91			return null;
92		}
93		MessagePacket packet = new MessagePacket();
94		packet.setAttributes(element.getAttributes());
95		packet.setChildren(element.getChildren());
96		return packet;
97	}
98}