Port MatrixMessageLocation to C

This commit is contained in:
Gergely Polonkai 2017-11-18 11:52:29 +01:00
parent 3e7979b905
commit 3da4c4d70a
7 changed files with 402 additions and 88 deletions

1
.gitignore vendored
View File

@ -77,5 +77,4 @@ Makefile.in
/src/matrix-message-image.c /src/matrix-message-image.c
/src/matrix-message-audio.c /src/matrix-message-audio.c
/src/matrix-message-video.c /src/matrix-message-video.c
/src/matrix-message-location.c
/src/matrix-room.c /src/matrix-room.c

View File

@ -44,7 +44,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-message-image.vala \ matrix-message-image.vala \
matrix-message-audio.vala \ matrix-message-audio.vala \
matrix-message-video.vala \ matrix-message-video.vala \
matrix-message-location.vala \
matrix-room.vala \ matrix-room.vala \
$(NULL) $(NULL)
@ -104,6 +103,7 @@ INST_H_SRC_FILES = \
matrix-event-base.h \ matrix-event-base.h \
matrix-message-base.h \ matrix-message-base.h \
matrix-message-text.h \ matrix-message-text.h \
matrix-message-location.h \
matrix-event-room-base.h \ matrix-event-room-base.h \
matrix-event-state-base.h \ matrix-event-state-base.h \
matrix-event-tag.h \ matrix-event-tag.h \
@ -138,6 +138,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-event-base.c \ matrix-event-base.c \
matrix-message-base.c \ matrix-message-base.c \
matrix-message-text.c \ matrix-message-text.c \
matrix-message-location.c \
matrix-event-tag.c \ matrix-event-tag.c \
matrix-event-presence.c \ matrix-event-presence.c \
matrix-event-room-member.c \ matrix-event-room-member.c \

View File

@ -27,6 +27,7 @@
#include "matrix-event-room-aliases.h" #include "matrix-event-room-aliases.h"
#include "matrix-message-text.h" #include "matrix-message-text.h"
#include "matrix-message-location.h"
/* /*
Borrowed from GLib Borrowed from GLib

View File

@ -0,0 +1,337 @@
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
#include "matrix-message-location.h"
#include "matrix-types.h"
#include "config.h"
/**
* SECTION:matrix-message-location
* @short_description: message type representing a real-world location
*
* This is the defaul message handler for `m.location` messages.
*/
enum {
PROP_0,
PROP_GEO_URI,
PROP_THUMBNAIL_URL,
PROP_THUMBNAIL_INFO,
NUM_PROPERTIES
};
static GParamSpec *matrix_message_location_properties[NUM_PROPERTIES];
typedef struct {
gchar* _geo_uri;
gchar* _thumbnail_url;
MatrixImageInfo* _thumbnail_info;
} MatrixMessageLocationPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(MatrixMessageLocation, matrix_message_location, MATRIX_MESSAGE_TYPE_BASE);
static void
matrix_message_location_real_from_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error)
{
MatrixMessageLocationPrivate *priv;
JsonObject *root;
JsonNode *node;
GError *inner_error = NULL;
g_return_if_fail(json_data != NULL);
priv = matrix_message_location_get_instance_private(MATRIX_MESSAGE_LOCATION(matrix_message_base));
root = json_node_get_object(json_data);
if ((node = json_object_get_member(root, "geo_uri")) != NULL) {
g_free(priv->_geo_uri);
priv->_geo_uri = g_strdup(json_node_get_string(node));
} else if (DEBUG) {
g_warning("geo_uri is missing from a m.location message");
}
if ((node = json_object_get_member(root, "thumbnail_url")) != NULL) {
g_free(priv->_thumbnail_url);
priv->_thumbnail_url = g_strdup(json_node_get_string(node));
}
if ((node = json_object_get_member(root, "thumbnail_info")) != NULL) {
matrix_image_info_unref(priv->_thumbnail_info);
priv->_thumbnail_info = matrix_image_info_new();
matrix_image_info_set_from_json(priv->_thumbnail_info, node);
}
MATRIX_MESSAGE_BASE_CLASS(matrix_message_location_parent_class)->from_json(matrix_message_base, json_data, &inner_error);
if (inner_error != NULL) {
g_propagate_error(error, inner_error);
}
}
static void
matrix_message_location_real_to_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error)
{
MatrixMessageLocationPrivate *priv;
JsonObject *root;
GError *inner_error = NULL;
g_return_if_fail (json_data != NULL);
priv = matrix_message_location_get_instance_private(MATRIX_MESSAGE_LOCATION(matrix_message_base));
if (priv->_geo_uri == NULL) {
g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
"Won't generate a m.location message without geo_uri");
return;
}
root = json_node_get_object(json_data);
json_object_set_string_member(root, "geo_uri", priv->_geo_uri);
if (priv->_thumbnail_url != NULL) {
json_object_set_string_member(root, "thumbnail_url", priv->_thumbnail_url);
}
if (priv->_thumbnail_info != NULL) {
JsonNode *node = matrix_image_info_get_json_node(priv->_thumbnail_info, &inner_error);
if (inner_error != NULL) {
g_propagate_error(error, inner_error);
return;
}
json_object_set_member(root, "thumbnail_info", node);
}
MATRIX_MESSAGE_BASE_CLASS(matrix_message_location_parent_class)->to_json(matrix_message_base, json_data, &inner_error);
if (inner_error != NULL) {
g_propagate_error(error, inner_error);
}
}
MatrixMessageLocation *
matrix_message_location_new(void)
{
return (MatrixMessageLocation *)matrix_message_base_construct(MATRIX_MESSAGE_TYPE_LOCATION);
}
const gchar *
matrix_message_location_get_geo_uri(MatrixMessageLocation *matrix_message_location)
{
MatrixMessageLocationPrivate *priv;
g_return_val_if_fail(matrix_message_location != NULL, NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
return priv->_geo_uri;
}
void
matrix_message_location_set_geo_uri(MatrixMessageLocation *matrix_message_location, const gchar *geo_uri)
{
MatrixMessageLocationPrivate *priv;
g_return_if_fail(matrix_message_location != NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
if (g_strcmp0(geo_uri, priv->_geo_uri) != 0) {
g_free(priv->_geo_uri);
priv->_geo_uri = g_strdup(geo_uri);
g_object_notify_by_pspec((GObject *)matrix_message_location, matrix_message_location_properties[PROP_GEO_URI]);
}
}
const gchar *
matrix_message_location_get_thumbnail_url(MatrixMessageLocation *matrix_message_location)
{
MatrixMessageLocationPrivate *priv;
g_return_val_if_fail(matrix_message_location != NULL, NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
return priv->_thumbnail_url;
}
void
matrix_message_location_set_thumbnail_url(MatrixMessageLocation *matrix_message_location, const gchar *thumbnail_url)
{
MatrixMessageLocationPrivate *priv;
g_return_if_fail(matrix_message_location != NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
if (g_strcmp0(thumbnail_url, priv->_thumbnail_url) != 0) {
g_free(priv->_thumbnail_url);
priv->_thumbnail_url = g_strdup(thumbnail_url);
g_object_notify_by_pspec((GObject *)matrix_message_location, matrix_message_location_properties[PROP_THUMBNAIL_URL]);
}
}
MatrixImageInfo *
matrix_message_location_get_thumbnail_info(MatrixMessageLocation *matrix_message_location)
{
MatrixMessageLocationPrivate *priv;
g_return_val_if_fail(matrix_message_location != NULL, NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
return priv->_thumbnail_info;
}
void
matrix_message_location_set_thumbnail_info(MatrixMessageLocation *matrix_message_location, MatrixImageInfo *thumbnail_info)
{
MatrixMessageLocationPrivate *priv;
g_return_if_fail(matrix_message_location != NULL);
priv = matrix_message_location_get_instance_private(matrix_message_location);
if (thumbnail_info != priv->_thumbnail_info) {
matrix_image_info_unref(priv->_thumbnail_info);
priv->_thumbnail_info = matrix_image_info_ref(thumbnail_info);
g_object_notify_by_pspec((GObject *)matrix_message_location, matrix_message_location_properties[PROP_THUMBNAIL_INFO]);
}
}
static void
matrix_message_location_finalize(GObject *gobject)
{
MatrixMessageLocationPrivate *priv = matrix_message_location_get_instance_private(MATRIX_MESSAGE_LOCATION(gobject));
g_free(priv->_geo_uri);
g_free(priv->_thumbnail_url);
matrix_image_info_unref(priv->_thumbnail_info);
G_OBJECT_CLASS(matrix_message_location_parent_class)->finalize(gobject);
}
static void
matrix_message_location_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec)
{
MatrixMessageLocation *matrix_message_location = MATRIX_MESSAGE_LOCATION(gobject);
switch (property_id) {
case PROP_GEO_URI:
g_value_set_string(value, matrix_message_location_get_geo_uri(matrix_message_location));
break;
case PROP_THUMBNAIL_URL:
g_value_set_string(value, matrix_message_location_get_thumbnail_url(matrix_message_location));
break;
case PROP_THUMBNAIL_INFO:
g_value_set_boxed(value, matrix_message_location_get_thumbnail_info(matrix_message_location));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_message_location_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec)
{
MatrixMessageLocation *matrix_message_location = MATRIX_MESSAGE_LOCATION(gobject);
switch (property_id) {
case PROP_GEO_URI:
matrix_message_location_set_geo_uri(matrix_message_location, g_value_get_string(value));
break;
case PROP_THUMBNAIL_URL:
matrix_message_location_set_thumbnail_url(matrix_message_location, g_value_get_string(value));
break;
case PROP_THUMBNAIL_INFO:
matrix_message_location_set_thumbnail_info(matrix_message_location, g_value_get_boxed(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_message_location_class_init(MatrixMessageLocationClass *klass)
{
((MatrixMessageBaseClass *)klass)->from_json = matrix_message_location_real_from_json;
((MatrixMessageBaseClass *)klass)->to_json = matrix_message_location_real_to_json;
G_OBJECT_CLASS(klass)->get_property = matrix_message_location_get_property;
G_OBJECT_CLASS(klass)->set_property = matrix_message_location_set_property;
G_OBJECT_CLASS(klass)->finalize = matrix_message_location_finalize;
/**
* MatrixMessageLocation:geo-uri:
*
* A geo URI representing this location.
*/
matrix_message_location_properties[PROP_GEO_URI] = g_param_spec_string(
"geo-uri", "geo-uri", "geo-uri",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_GEO_URI, matrix_message_location_properties[PROP_GEO_URI]);
/**
* MatrixMessageLocation:thumbnail-url:
*
* The URL to a thumbnail of the location being represented.
*/
matrix_message_location_properties[PROP_THUMBNAIL_URL] = g_param_spec_string(
"thumbnail-url", "thumbnail-url", "thumbnail-url",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_THUMBNAIL_URL, matrix_message_location_properties[PROP_THUMBNAIL_URL]);
/**
* MatrixMessageLocation:thumbnail-info:
*
* A #MatrixImageInfo object describing #MatrixMessageLocation:thumbnail-url.
*/
matrix_message_location_properties[PROP_THUMBNAIL_INFO] = g_param_spec_boxed(
"thumbnail-info", "thumbnail-info", "thumbnail-info",
MATRIX_TYPE_IMAGE_INFO,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_THUMBNAIL_INFO, matrix_message_location_properties[PROP_THUMBNAIL_INFO]);
}
static void
matrix_message_location_init(MatrixMessageLocation *matrix_message_location)
{
MatrixMessageLocationPrivate *priv = matrix_message_location_get_instance_private(matrix_message_location);
priv->_geo_uri = NULL;
priv->_thumbnail_url = NULL;
priv->_thumbnail_info = NULL;
}

View File

@ -0,0 +1,47 @@
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
#ifndef __MATRIX_GLIB_SDK_MESSAGE_LOCATION_H__
# define __MATRIX_GLIB_SDK_MESSAGE_LOCATION_H__
# include <glib-object.h>
# include "matrix-message-base.h"
# include "matrix-compacts.h"
G_BEGIN_DECLS
# define MATRIX_MESSAGE_TYPE_LOCATION matrix_message_location_get_type()
G_DECLARE_DERIVABLE_TYPE(MatrixMessageLocation, matrix_message_location, MATRIX_MESSAGE, LOCATION, MatrixMessageBase)
struct _MatrixMessageLocationClass {
MatrixMessageBaseClass parent_class;
};
MatrixMessageLocation* matrix_message_location_new (void);
const gchar* matrix_message_location_get_geo_uri (MatrixMessageLocation* self);
void matrix_message_location_set_geo_uri (MatrixMessageLocation* self, const gchar* value);
const gchar* matrix_message_location_get_thumbnail_url (MatrixMessageLocation* self);
void matrix_message_location_set_thumbnail_url (MatrixMessageLocation* self, const gchar* value);
MatrixImageInfo* matrix_message_location_get_thumbnail_info (MatrixMessageLocation* self);
void matrix_message_location_set_thumbnail_info (MatrixMessageLocation* self, MatrixImageInfo* value);
G_END_DECLS
#endif /* __MATRIX_GLIB_SDK_MESSAGE_LOCATION_H__ */

View File

@ -1,86 +0,0 @@
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
/**
* 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);
}
}

View File

@ -650,5 +650,20 @@ namespace Matrix {
public override void to_json(Json.Node json_data) public override void to_json(Json.Node json_data)
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-message-location.h")]
public class Location : Base {
public string? geo_uri { get; set; default = null; }
public string? thumbnail_url { get; set; default = null; }
public Matrix.ImageInfo? thumbnail_info { get; set; default = null; }
protected override void
from_json(Json.Node json_data)
throws Matrix.Error;
protected override void
to_json(Json.Node json_data)
throws Matrix.Error;
}
} }
} }