MessagePacket.java

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