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