1package eu.siacs.conversations.xmpp.jingle.stanzas;
2
3import android.support.annotation.NonNull;
4
5import com.google.common.base.Preconditions;
6
7import java.util.Locale;
8
9import eu.siacs.conversations.entities.DownloadableFile;
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xml.Namespace;
12
13public class Content extends Element {
14
15 public Content(final Creator creator, final String name) {
16 super("content", Namespace.JINGLE);
17 this.setAttribute("creator", creator.toString());
18 this.setAttribute("name", name);
19 }
20
21 private Content() {
22 super("content", Namespace.JINGLE);
23 }
24
25 public static Content upgrade(final Element element) {
26 Preconditions.checkArgument("content".equals(element.getName()));
27 final Content content = new Content();
28 content.setAttributes(element.getAttributes());
29 content.setChildren(element.getChildren());
30 return content;
31 }
32
33 public String getContentName() {
34 return this.getAttribute("name");
35 }
36
37 public Creator getCreator() {
38 return Creator.of(getAttribute("creator"));
39 }
40
41 public Senders getSenders() {
42 return Senders.of(getAttribute("senders"));
43 }
44
45 public void setSenders(Senders senders) {
46 this.setAttribute("senders", senders.toString());
47 }
48
49 public GenericDescription getDescription() {
50 final Element description = this.findChild("description");
51 if (description == null) {
52 return null;
53 }
54 final String xmlns = description.getNamespace();
55 if (FileTransferDescription.NAMESPACES.contains(xmlns)) {
56 return FileTransferDescription.upgrade(description);
57 } else {
58 return GenericDescription.upgrade(description);
59 }
60 }
61
62 public void setDescription(final GenericDescription description) {
63 Preconditions.checkNotNull(description);
64 this.addChild(description);
65 }
66
67 public String getDescriptionNamespace() {
68 final Element description = this.findChild("description");
69 return description == null ? null : description.getNamespace();
70 }
71
72 public GenericTransportInfo getTransport() {
73 final Element transport = this.findChild("transport");
74 final String namespace = transport == null ? null : transport.getNamespace();
75 if (Namespace.JINGLE_TRANSPORTS_IBB.equals(namespace)) {
76 return IbbTransportInfo.upgrade(transport);
77 } else if (Namespace.JINGLE_TRANSPORTS_S5B.equals(namespace)) {
78 return S5BTransportInfo.upgrade(transport);
79 } else if (Namespace.JINGLE_TRANSPORT_ICE_UDP.equals(namespace)) {
80 return IceUdpTransportInfo.upgrade(transport);
81 } else if (transport != null) {
82 return GenericTransportInfo.upgrade(transport);
83 } else {
84 return null;
85 }
86 }
87
88 public void setTransport(GenericTransportInfo transportInfo) {
89 this.addChild(transportInfo);
90 }
91
92 public enum Creator {
93 INITIATOR, RESPONDER;
94
95 public static Creator of(final String value) {
96 return Creator.valueOf(value.toUpperCase(Locale.ROOT));
97 }
98
99 @Override
100 @NonNull
101 public String toString() {
102 return super.toString().toLowerCase(Locale.ROOT);
103 }
104 }
105
106 public enum Senders {
107 BOTH, INITIATOR, NONE, RESPONDER;
108
109 public static Senders of(final String value) {
110 return Senders.valueOf(value.toUpperCase(Locale.ROOT));
111 }
112
113 @Override
114 @NonNull
115 public String toString() {
116 return super.toString().toLowerCase(Locale.ROOT);
117 }
118 }
119}