aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0264_jingle_content_thumbnails.vala
blob: cb281f80b0a34be996ba93879eb88841a6f265ba (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
namespace Xmpp.Xep.JingleContentThumbnails {
    public const string NS_URI = "urn:xmpp:thumbs:1";
    public const string STANZA_NAME = "thumbnail";

    public class Thumbnail {
        public string uri;
        public string? media_type;
        public int width;
        public int height;

        const string URI_ATTRIBUTE = "uri";
        const string MIME_ATTRIBUTE = "media-type";
        const string WIDTH_ATTRIBUTE = "width";
        const string HEIGHT_ATTRIBUTE = "height";

        public StanzaNode to_stanza_node() {
            StanzaNode node = new StanzaNode.build(STANZA_NAME, NS_URI).add_self_xmlns()
                    .put_attribute(URI_ATTRIBUTE, this.uri);
            if (this.media_type != null) {
                node.put_attribute(MIME_ATTRIBUTE, this.media_type);
            }
            if (this.width != -1) {
                node.put_attribute(WIDTH_ATTRIBUTE, this.width.to_string());
            }
            if (this.height != -1) {
                node.put_attribute(HEIGHT_ATTRIBUTE, this.height.to_string());
            }
            return node;
        }

        public static Thumbnail? from_stanza_node(StanzaNode node) {
            Thumbnail thumbnail = new Thumbnail();
            thumbnail.uri = node.get_attribute(URI_ATTRIBUTE);
            if (thumbnail.uri == null) {
                return null;
            }
            thumbnail.media_type = node.get_attribute(MIME_ATTRIBUTE);
            string? width = node.get_attribute(WIDTH_ATTRIBUTE);
            if (width != null) {
                thumbnail.width = int.parse(width);
            }
            string? height = node.get_attribute(HEIGHT_ATTRIBUTE);
            if (height != null) {
                thumbnail.height = int.parse(height);
            }
            return thumbnail;
        }
    }
}