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("senders", creator);
34 this.setAttribute("name", name);
35 }
36
37 public Version getVersion() {
38 if (hasChild("description", Version.FT_3.namespace)) {
39 return Version.FT_3;
40 } else if (hasChild("description" , Version.FT_4.namespace)) {
41 return Version.FT_4;
42 } else if (hasChild("description" , Version.FT_5.namespace)) {
43 return Version.FT_5;
44 }
45 return null;
46 }
47
48 public void setTransportId(String sid) {
49 this.transportId = sid;
50 }
51
52 public Element setFileOffer(DownloadableFile actualFile, boolean otr, Version version) {
53 Element description = this.addChild("description", version.namespace);
54 Element file;
55 if (version == Version.FT_3) {
56 Element offer = description.addChild("offer");
57 file = offer.addChild("file");
58 } else {
59 file = description.addChild("file");
60 }
61 file.addChild("size").setContent(Long.toString(actualFile.getExpectedSize()));
62 if (otr) {
63 file.addChild("name").setContent(actualFile.getName() + ".otr");
64 } else {
65 file.addChild("name").setContent(actualFile.getName());
66 }
67 return file;
68 }
69
70 public Element getFileOffer(Version version) {
71 Element description = this.findChild("description", version.namespace);
72 if (description == null) {
73 return null;
74 }
75 if (version == Version.FT_3) {
76 Element offer = description.findChild("offer");
77 if (offer == null) {
78 return null;
79 }
80 return offer.findChild("file");
81 } else {
82 return description.findChild("file");
83 }
84 }
85
86 public void setFileOffer(Element fileOffer, Version version) {
87 Element description = this.addChild("description", version.namespace);
88 if (version == Version.FT_3) {
89 description.addChild("offer").addChild(fileOffer);
90 } else {
91 description.addChild(fileOffer);
92 }
93 }
94
95 public String getTransportId() {
96 if (hasSocks5Transport()) {
97 this.transportId = socks5transport().getAttribute("sid");
98 } else if (hasIbbTransport()) {
99 this.transportId = ibbTransport().getAttribute("sid");
100 }
101 return this.transportId;
102 }
103
104 public Element socks5transport() {
105 Element transport = this.findChild("transport",
106 "urn:xmpp:jingle:transports:s5b:1");
107 if (transport == null) {
108 transport = this.addChild("transport",
109 "urn:xmpp:jingle:transports:s5b:1");
110 transport.setAttribute("sid", this.transportId);
111 }
112 return transport;
113 }
114
115 public Element ibbTransport() {
116 Element transport = this.findChild("transport",
117 "urn:xmpp:jingle:transports:ibb:1");
118 if (transport == null) {
119 transport = this.addChild("transport",
120 "urn:xmpp:jingle:transports:ibb:1");
121 transport.setAttribute("sid", this.transportId);
122 }
123 return transport;
124 }
125
126 public boolean hasSocks5Transport() {
127 return this.hasChild("transport", "urn:xmpp:jingle:transports:s5b:1");
128 }
129
130 public boolean hasIbbTransport() {
131 return this.hasChild("transport", "urn:xmpp:jingle:transports:ibb:1");
132 }
133}