aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java
blob: 1e8e3fd6019a46ca197aae8a7c70c93ac954af8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package eu.siacs.conversations.xmpp.jingle.stanzas;

import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jingle.JingleFile;

public class Content extends Element {

	private String transportId;

	private Content(String name) {
		super(name);
	}

	public Content() {
		super("content");
	}

	public Content(String creator, String name) {
		super("content");
		this.setAttribute("creator", creator);
		this.setAttribute("name", name);
	}

	public void setTransportId(String sid) {
		this.transportId = sid;
	}

	public void setFileOffer(JingleFile actualFile, boolean otr) {
		Element description = this.addChild("description",
				"urn:xmpp:jingle:apps:file-transfer:3");
		Element offer = description.addChild("offer");
		Element file = offer.addChild("file");
		file.addChild("size").setContent("" + actualFile.getSize());
		if (otr) {
			file.addChild("name").setContent(actualFile.getName() + ".otr");
		} else {
			file.addChild("name").setContent(actualFile.getName());
		}
	}

	public Element getFileOffer() {
		Element description = this.findChild("description",
				"urn:xmpp:jingle:apps:file-transfer:3");
		if (description == null) {
			return null;
		}
		Element offer = description.findChild("offer");
		if (offer == null) {
			return null;
		}
		return offer.findChild("file");
	}

	public void setFileOffer(Element fileOffer) {
		Element description = this.findChild("description",
				"urn:xmpp:jingle:apps:file-transfer:3");
		if (description == null) {
			description = this.addChild("description",
					"urn:xmpp:jingle:apps:file-transfer:3");
		}
		description.addChild(fileOffer);
	}

	public String getTransportId() {
		if (hasSocks5Transport()) {
			this.transportId = socks5transport().getAttribute("sid");
		} else if (hasIbbTransport()) {
			this.transportId = ibbTransport().getAttribute("sid");
		}
		return this.transportId;
	}

	public Element socks5transport() {
		Element transport = this.findChild("transport",
				"urn:xmpp:jingle:transports:s5b:1");
		if (transport == null) {
			transport = this.addChild("transport",
					"urn:xmpp:jingle:transports:s5b:1");
			transport.setAttribute("sid", this.transportId);
		}
		return transport;
	}

	public Element ibbTransport() {
		Element transport = this.findChild("transport",
				"urn:xmpp:jingle:transports:ibb:1");
		if (transport == null) {
			transport = this.addChild("transport",
					"urn:xmpp:jingle:transports:ibb:1");
			transport.setAttribute("sid", this.transportId);
		}
		return transport;
	}

	public boolean hasSocks5Transport() {
		return this.hasChild("transport", "urn:xmpp:jingle:transports:s5b:1");
	}

	public boolean hasIbbTransport() {
		return this.hasChild("transport", "urn:xmpp:jingle:transports:ibb:1");
	}
}