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