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