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