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 private String transportId;
15
16
17 //refactor to getDescription and getTransport
18 //return either FileTransferDescription or GenericDescription or RtpDescription (all extend Description interface)
19
20 public Content(final Creator creator, final String name) {
21 super("content", Namespace.JINGLE);
22 this.setAttribute("creator", creator.toString());
23 this.setAttribute("name", name);
24 }
25
26 private Content() {
27 super("content", Namespace.JINGLE);
28 }
29
30 public static Content upgrade(final Element element) {
31 Preconditions.checkArgument("content".equals(element.getName()));
32 final Content content = new Content();
33 content.setAttributes(element.getAttributes());
34 content.setChildren(element.getChildren());
35 return content;
36 }
37
38 public Creator getCreator() {
39 return Creator.of(getAttribute("creator"));
40 }
41
42 public Senders getSenders() {
43 return Senders.of(getAttribute("senders"));
44 }
45
46 public void setSenders(Senders senders) {
47 this.setAttribute("senders", senders.toString());
48 }
49
50 public GenericDescription getDescription() {
51 final Element description = this.findChild("description");
52 if (description == null) {
53 return null;
54 }
55 final String xmlns = description.getNamespace();
56 if (FileTransferDescription.NAMESPACES.contains(xmlns)) {
57 return FileTransferDescription.upgrade(description);
58 } else {
59 return GenericDescription.upgrade(description);
60 }
61 }
62
63 public void setDescription(final GenericDescription description) {
64 Preconditions.checkNotNull(description);
65 this.addChild(description);
66 }
67
68 public String getDescriptionNamespace() {
69 final Element description = this.findChild("description");
70 return description == null ? null : description.getNamespace();
71 }
72
73 public String getTransportId() {
74 if (hasSocks5Transport()) {
75 this.transportId = socks5transport().getAttribute("sid");
76 } else if (hasIbbTransport()) {
77 this.transportId = ibbTransport().getAttribute("sid");
78 }
79 return this.transportId;
80 }
81
82 public void setTransportId(String sid) {
83 this.transportId = sid;
84 }
85
86 public Element socks5transport() {
87 Element transport = this.findChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
88 if (transport == null) {
89 transport = this.addChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
90 transport.setAttribute("sid", this.transportId);
91 }
92 return transport;
93 }
94
95 public Element ibbTransport() {
96 Element transport = this.findChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
97 if (transport == null) {
98 transport = this.addChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
99 transport.setAttribute("sid", this.transportId);
100 }
101 return transport;
102 }
103
104 public boolean hasSocks5Transport() {
105 return this.hasChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
106 }
107
108 public boolean hasIbbTransport() {
109 return this.hasChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
110 }
111
112 public enum Creator {
113 INITIATOR, RESPONDER;
114
115 public static Creator of(final String value) {
116 return Creator.valueOf(value.toUpperCase(Locale.ROOT));
117 }
118
119 @Override
120 @NonNull
121 public String toString() {
122 return super.toString().toLowerCase(Locale.ROOT);
123 }
124 }
125
126 public enum Senders {
127 BOTH, INITIATOR, NONE, RESPONDER;
128
129 public static Senders of(final String value) {
130 return Senders.valueOf(value.toUpperCase(Locale.ROOT));
131 }
132
133 @Override
134 @NonNull
135 public String toString() {
136 return super.toString().toLowerCase(Locale.ROOT);
137 }
138 }
139}