aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java
blob: 1d4e44d18722b445311f6c7bb13cf1560060f2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package eu.siacs.conversations.xmpp.stanzas;

import eu.siacs.conversations.xml.Element;


public class IqPacket extends AbstractStanza {
	
	public static final int TYPE_ERROR = -1;
	public static final int TYPE_SET = 0;
	public static final int TYPE_RESULT = 1;
	public static final int TYPE_GET = 2;

	private IqPacket(String name) {
		super(name);
	}

	public IqPacket(int type) {
		super("iq");
		switch (type) {
		case TYPE_SET:
			this.setAttribute("type", "set");
			break;
		case TYPE_GET:
			this.setAttribute("type", "get");
			break;
		case TYPE_RESULT:
			this.setAttribute("type", "result");
			break;
		case TYPE_ERROR:
			this.setAttribute("type", "error");
			break;
		default:
			break;
		}
	}
	
	public IqPacket() {
		super("iq");
	}
	
	public Element query() {
		Element query = findChild("query");
		if (query==null) {
			query = addChild("query");
		}
		return query;
	}
	
	public Element query(String xmlns) {
		Element query = query();
		query.setAttribute("xmlns", xmlns);
		return query();
	}
	
	public int getType() {
		String type = getAttribute("type");
		if ("error".equals(type)) {
			return TYPE_ERROR;
		} else if ("result".equals(type)) {
			return TYPE_RESULT;
		} else if ("set".equals(type)) {
			return TYPE_SET;
		} else if ("get".equals(type)) {
			return TYPE_GET;
		} else {
			return 1000;
		}
	}
	
	public IqPacket generateRespone(int type) {
		IqPacket packet = new IqPacket(type);
		packet.setTo(this.getFrom());
		packet.setId(this.getId());
		return packet;
	}

}