1package eu.siacs.conversations.xmpp.jingle.stanzas;
2
3import eu.siacs.conversations.entities.DownloadableFile;
4import eu.siacs.conversations.xml.Element;
5import eu.siacs.conversations.xml.Namespace;
6
7public class Content extends Element {
8
9
10 //refactor to getDescription and getTransport
11 //return either FileTransferDescription or GenericDescription or RtpDescription (all extend Description interface)
12
13 public enum Version {
14 FT_3("urn:xmpp:jingle:apps:file-transfer:3"),
15 FT_4("urn:xmpp:jingle:apps:file-transfer:4"),
16 FT_5("urn:xmpp:jingle:apps:file-transfer:5");
17
18 private final String namespace;
19
20 Version(String namespace) {
21 this.namespace = namespace;
22 }
23
24 public String getNamespace() {
25 return namespace;
26 }
27 }
28
29 private String transportId;
30
31 public Content() {
32 super("content");
33 }
34
35 public Content(String creator, String name) {
36 super("content");
37 this.setAttribute("creator", creator);
38 this.setAttribute("senders", creator);
39 this.setAttribute("name", name);
40 }
41
42 public Version getVersion() {
43 if (hasChild("description", Version.FT_3.namespace)) {
44 return Version.FT_3;
45 } else if (hasChild("description", Version.FT_4.namespace)) {
46 return Version.FT_4;
47 } else if (hasChild("description", Version.FT_5.namespace)) {
48 return Version.FT_5;
49 }
50 return null;
51 }
52
53 public void setTransportId(String sid) {
54 this.transportId = sid;
55 }
56
57 public Element setFileOffer(DownloadableFile actualFile, boolean otr, Version version) {
58 Element description = this.addChild("description", version.namespace);
59 Element file;
60 if (version == Version.FT_3) {
61 Element offer = description.addChild("offer");
62 file = offer.addChild("file");
63 } else {
64 file = description.addChild("file");
65 }
66 file.addChild("size").setContent(Long.toString(actualFile.getExpectedSize()));
67 if (otr) {
68 file.addChild("name").setContent(actualFile.getName() + ".otr");
69 } else {
70 file.addChild("name").setContent(actualFile.getName());
71 }
72 return file;
73 }
74
75 public Element getFileOffer(Version version) {
76 Element description = this.findChild("description", version.namespace);
77 if (description == null) {
78 return null;
79 }
80 if (version == Version.FT_3) {
81 Element offer = description.findChild("offer");
82 if (offer == null) {
83 return null;
84 }
85 return offer.findChild("file");
86 } else {
87 return description.findChild("file");
88 }
89 }
90
91 public void setFileOffer(Element fileOffer, Version version) {
92 Element description = this.addChild("description", version.namespace);
93 if (version == Version.FT_3) {
94 description.addChild("offer").addChild(fileOffer);
95 } else {
96 description.addChild(fileOffer);
97 }
98 }
99
100 public String getTransportId() {
101 if (hasSocks5Transport()) {
102 this.transportId = socks5transport().getAttribute("sid");
103 } else if (hasIbbTransport()) {
104 this.transportId = ibbTransport().getAttribute("sid");
105 }
106 return this.transportId;
107 }
108
109 public Element socks5transport() {
110 Element transport = this.findChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
111 if (transport == null) {
112 transport = this.addChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
113 transport.setAttribute("sid", this.transportId);
114 }
115 return transport;
116 }
117
118 public Element ibbTransport() {
119 Element transport = this.findChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
120 if (transport == null) {
121 transport = this.addChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
122 transport.setAttribute("sid", this.transportId);
123 }
124 return transport;
125 }
126
127 public boolean hasSocks5Transport() {
128 return this.hasChild("transport", Namespace.JINGLE_TRANSPORTS_S5B);
129 }
130
131 public boolean hasIbbTransport() {
132 return this.hasChild("transport", Namespace.JINGLE_TRANSPORTS_IBB);
133 }
134}