1package im.conversations.android.xmpp.model.stanza;
2
3import com.google.common.base.Strings;
4
5import eu.siacs.conversations.xml.Element;
6
7import im.conversations.android.annotation.XmlElement;
8import im.conversations.android.xmpp.model.error.Error;
9
10import java.util.Locale;
11
12@XmlElement
13public class Iq extends Stanza {
14
15 public static Iq TIMEOUT = new Iq(Type.TIMEOUT);
16
17 public Iq() {
18 super(Iq.class);
19 }
20
21 public Iq(final Type type) {
22 super(Iq.class);
23 this.setAttribute("type", type.toString().toLowerCase(Locale.ROOT));
24 }
25
26 // TODO get rid of timeout
27 public enum Type {
28 SET,
29 GET,
30 ERROR,
31 RESULT,
32 TIMEOUT
33 }
34
35 public Type getType() {
36 return Type.valueOf(
37 Strings.nullToEmpty(this.getAttribute("type")).toUpperCase(Locale.ROOT));
38 }
39
40 @Override
41 public boolean isInvalid() {
42 final var id = getId();
43 if (Strings.isNullOrEmpty(id)) {
44 return true;
45 }
46 return super.isInvalid();
47 }
48
49 // Legacy methods that need to be refactored:
50
51 public Element query() {
52 final Element query = findChild("query");
53 if (query != null) {
54 return query;
55 }
56 return addChild("query");
57 }
58
59 public Element query(final String xmlns) {
60 final Element query = query();
61 query.setAttribute("xmlns", xmlns);
62 return query();
63 }
64
65 public Iq generateResponse(final Iq.Type type) {
66 final var packet = new Iq(type);
67 packet.setTo(this.getFrom());
68 packet.setId(this.getId());
69 return packet;
70 }
71
72 public String getErrorCondition() {
73 final Error error = getError();
74 final var condition = error == null ? null : error.getCondition();
75 return condition == null ? null : condition.getName();
76 }
77}