Content.java

 1package eu.siacs.conversations.xmpp.jingle.stanzas;
 2
 3import eu.siacs.conversations.xml.Element;
 4import eu.siacs.conversations.xmpp.jingle.JingleFile;
 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 void setFileOffer(JingleFile actualFile) {
29		Element description = this.addChild("description", "urn:xmpp:jingle:apps:file-transfer:3");
30		Element offer = description.addChild("offer");
31		Element file = offer.addChild("file");
32		file.addChild("size").setContent(""+actualFile.getSize());
33		file.addChild("name").setContent(actualFile.getName());
34	}
35	
36	public Element getFileOffer() {
37		Element description = this.findChild("description", "urn:xmpp:jingle:apps:file-transfer:3");
38		if (description==null) {
39			return null;
40		}
41		Element offer = description.findChild("offer");
42		if (offer==null) {
43			return null;
44		}
45		return offer.findChild("file");
46	}
47	
48	public void setFileOffer(Element fileOffer) {
49		Element description = this.findChild("description", "urn:xmpp:jingle:apps:file-transfer:3");
50		if (description==null) {
51			description = this.addChild("description", "urn:xmpp:jingle:apps:file-transfer:3");
52		}
53		description.addChild(fileOffer);
54	}
55	
56	public String getTransportId() {
57		if (hasSocks5Transport()) {
58			this.transportId = socks5transport().getAttribute("sid");
59		} else if (hasIbbTransport()) {
60			this.transportId = ibbTransport().getAttribute("sid");
61		}
62		return this.transportId;
63	}
64	
65	public Element socks5transport() {
66		Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
67		if (transport==null) {
68			transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1");
69			transport.setAttribute("sid", this.transportId);
70		}
71		return transport;
72	}
73	
74	public Element ibbTransport() {
75		Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:ibb:1");
76		if (transport==null) {
77			transport = this.addChild("transport", "urn:xmpp:jingle:transports:ibb:1");
78			transport.setAttribute("sid", this.transportId);
79		}
80		return transport;
81	}
82	
83	public boolean hasSocks5Transport() {
84		return this.hasChild("transport", "urn:xmpp:jingle:transports:s5b:1");
85	}
86	
87	public boolean hasIbbTransport() {
88		return this.hasChild("transport","urn:xmpp:jingle:transports:ibb:1");
89	}
90}