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