From 595d93412bffc9372fbb896f8af25716efb469db Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 10 Mar 2016 20:03:17 +0100 Subject: [PATCH] Add message handler for m.location --- .gitignore | 1 + src/Makefile.am | 1 + src/matrix-event-types.c | 3 ++ src/matrix-message-location.vala | 86 ++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 src/matrix-message-location.vala diff --git a/.gitignore b/.gitignore index 8971915..570a7db 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,4 @@ Makefile.in /src/matrix-message-image.c /src/matrix-message-audio.c /src/matrix-message-video.c +/src/matrix-message-location.c diff --git a/src/Makefile.am b/src/Makefile.am index 892f4db..4c84a36 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -58,6 +58,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-message-image.vala \ matrix-message-audio.vala \ matrix-message-video.vala \ + matrix-message-location.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 704d7b0..3ae0327 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -243,6 +243,9 @@ matrix_event_types_ctor(void) matrix_message_register_type("m.video", MATRIX_MESSAGE_TYPE_VIDEO, NULL); + matrix_message_register_type("m.location", + MATRIX_MESSAGE_TYPE_LOCATION, + NULL); } void diff --git a/src/matrix-message-location.vala b/src/matrix-message-location.vala new file mode 100644 index 0000000..b5644a0 --- /dev/null +++ b/src/matrix-message-location.vala @@ -0,0 +1,86 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with matrix-glib-sdk. If not, see + * . + */ + +/** + * This message represents a real-world location. + */ +public class Matrix.Message.Location : Matrix.Message.Base { + /** + * A geo URI representing this location. + */ + public string? geo_uri { get; set; default = null; } + + /** + * The URL to a thumbnail of the location being represented. + */ + public string? thumbnail_url { get; set; default = null; } + + /** + * Metadata about the location referred to in `thumbnail_url`. + */ + public ImageInfo? thumbnail_info { get; set; default = null; } + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + var root = json_data.get_object(); + Json.Node? node; + + if ((node = root.get_member("geo_uri")) != null) { + _geo_uri = node.get_string(); + } else if (Config.DEBUG) { + warning("geo_uri is missing from a m.location message"); + } + + if ((node = root.get_member("thumbnail_url")) != null) { + _thumbnail_url = node.get_string(); + } + + if ((node = root.get_member("thumbnail_info")) != null) { + _thumbnail_info = ImageInfo(); + _thumbnail_info.set_from_json(node); + } + + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + if (_geo_uri == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.location message without geo_uri"); + } + + var root = json_data.get_object(); + + root.set_string_member("geo_uri", _geo_uri); + + if (_thumbnail_url != null) { + root.set_string_member("thumbnail_url", _thumbnail_url); + } + + if (_thumbnail_info != null) { + root.set_member("thumbnail_info", _thumbnail_info.get_json_node()); + } + + base.to_json(json_data); + } +}