From 4d8c9fe551f2d2738a9ce8959587844702e542cb Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 15 Nov 2017 08:25:06 +0100 Subject: [PATCH] Port MatrixEventRoomTopic to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-topic.c | 234 +++++++++++++++++++++++++++++++ src/matrix-event-room-topic.h | 40 ++++++ src/matrix-event-room-topic.vala | 74 ---------- src/matrix-event-types.c | 1 + vapi/c-api.vapi | 11 ++ 7 files changed, 288 insertions(+), 76 deletions(-) create mode 100644 src/matrix-event-room-topic.c create mode 100644 src/matrix-event-room-topic.h delete mode 100644 src/matrix-event-room-topic.vala diff --git a/.gitignore b/.gitignore index b9b2ce9..c648730 100644 --- a/.gitignore +++ b/.gitignore @@ -54,7 +54,6 @@ Makefile.in /src/matrix-event-room-message.c /src/namespace-info.vala /src/namespace-info.c -/src/matrix-event-room-topic.c /src/matrix-event-room-aliases.c /src/matrix-event-room-history-visibility.c /src/matrix-event-room-join-rules.c diff --git a/src/Makefile.am b/src/Makefile.am index 19a2ebd..253e902 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,7 +22,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-http-api.vala \ matrix-http-client.vala \ matrix-event-room-message.vala \ - matrix-event-room-topic.vala \ matrix-event-room-aliases.vala \ matrix-event-room-history-visibility.vala \ matrix-event-room-join-rules.vala \ @@ -111,6 +110,7 @@ INST_H_SRC_FILES = \ matrix-event-tag.h \ matrix-event-presence.h \ matrix-event-room-member.h \ + matrix-event-room-topic.h \ matrix-event-typing.h \ matrix-event-receipt.h \ utils.h \ @@ -143,6 +143,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-room-base.c \ matrix-event-state-base.c \ matrix-event-receipt.c \ + matrix-event-room-topic.c \ matrix-profile.c \ utils.c \ matrix-enumtypes.c \ diff --git a/src/matrix-event-room-topic.c b/src/matrix-event-room-topic.c new file mode 100644 index 0000000..5885e6f --- /dev/null +++ b/src/matrix-event-room-topic.c @@ -0,0 +1,234 @@ +/* + * 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 + * . + */ + +#include "matrix-event-room-topic.h" +#include "matrix-types.h" +#include "config.h" + +/** + * SECTION:matrix-event-room-topic + * @short_description: event to hold a room topic + * + * This is the default event handler for `m.room.topic` events. + * + * A topic is a short message detailing what is currently being discussed in the room. It can + * also be used as a way to display extra information about the room, which may not be + * suitable for the room name. + */ +enum { + PROP_0, + PROP_TOPIC, + NUM_PROPS +}; + +static GParamSpec *matrix_event_room_topic_properties[NUM_PROPS]; + +typedef struct { + gchar* _topic; +} MatrixEventRoomTopicPrivate; + +/** + * MatrixEventRoomTopic: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomTopic, matrix_event_room_topic, MATRIX_EVENT_TYPE_STATE); + +static void +matrix_event_room_topic_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomTopicPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + GError *inner_error = NULL; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_topic_get_instance_private(MATRIX_EVENT_ROOM_TOPIC(matrix_event_base)); + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + + if (DEBUG && ((node = json_object_get_member(root, "state_key")) != NULL)) { + const gchar *state_key = json_node_get_string(node); + + if ((state_key == NULL) || (*state_key == 0)) { + g_warning("state_key of a m.room.topic event is non-empty"); + } + } + + if ((node = json_object_get_member(content_root, "topic")) != NULL) { + g_free(priv->_topic); + priv->_topic = g_strdup(json_node_get_string(node)); + } else if (DEBUG) { + g_warning("content.topic is missing from an m.room.topic event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_topic_parent_class)->from_json(matrix_event_base, json_data, &inner_error); + + if (inner_error != NULL) { + g_propagate_error(error, inner_error); + } +} + +static void +matrix_event_room_topic_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomTopicPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + GError *inner_error = NULL; + const gchar *state_key; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_topic_get_instance_private(MATRIX_EVENT_ROOM_TOPIC(matrix_event_base)); + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + state_key = matrix_event_state_get_state_key(MATRIX_EVENT_STATE(matrix_event_base)); + + if ((state_key == NULL) || (*state_key == 0)) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.room.topic event with a non-empty state_key"); + + return; + } + + if (priv->_topic != NULL) { + json_object_set_string_member(content_root, "topic", priv->_topic); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_topic_parent_class)->to_json(matrix_event_base, json_data, &inner_error); + + if (inner_error != NULL) { + g_propagate_error(error, inner_error); + } +} + +MatrixEventRoomTopic * +matrix_event_room_topic_new(void) { + return (MatrixEventRoomTopic *)matrix_event_state_construct(MATRIX_EVENT_TYPE_ROOM_TOPIC); +} + +const gchar * +matrix_event_room_topic_get_topic(MatrixEventRoomTopic *matrix_event_room_topic) +{ + MatrixEventRoomTopicPrivate *priv; + + g_return_val_if_fail(matrix_event_room_topic != NULL, NULL); + + priv = matrix_event_room_topic_get_instance_private(matrix_event_room_topic); + + return priv->_topic; +} + +void +matrix_event_room_topic_set_topic(MatrixEventRoomTopic *matrix_event_room_topic, const gchar *topic) +{ + MatrixEventRoomTopicPrivate *priv; + + g_return_if_fail(matrix_event_room_topic != NULL); + + priv = matrix_event_room_topic_get_instance_private(matrix_event_room_topic); + + if (g_strcmp0(topic, priv->_topic) != 0) { + g_free(priv->_topic); + priv->_topic = g_strdup(topic); + + g_object_notify_by_pspec((GObject *)matrix_event_room_topic, matrix_event_room_topic_properties[PROP_TOPIC]); + } +} + +static void +matrix_event_room_topic_finalize(GObject *gobject) +{ + MatrixEventRoomTopicPrivate *priv; + + priv = matrix_event_room_topic_get_instance_private(MATRIX_EVENT_ROOM_TOPIC(gobject)); + + g_free(priv->_topic); + + G_OBJECT_CLASS(matrix_event_room_topic_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_topic_get_property(GObject *gobject, guint property_id, GValue* value, GParamSpec* pspec) +{ + MatrixEventRoomTopicPrivate *priv = matrix_event_room_topic_get_instance_private(MATRIX_EVENT_ROOM_TOPIC(gobject)); + + switch (property_id) { + case PROP_TOPIC: + g_value_set_string(value, priv->_topic); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_topic_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomTopic *matrix_event_room_topic = MATRIX_EVENT_ROOM_TOPIC(gobject); + + switch (property_id) { + case PROP_TOPIC: + matrix_event_room_topic_set_topic(matrix_event_room_topic, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_topic_class_init(MatrixEventRoomTopicClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_topic_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_topic_real_to_json; + G_OBJECT_CLASS (klass)->get_property = matrix_event_room_topic_get_property; + G_OBJECT_CLASS (klass)->set_property = matrix_event_room_topic_set_property; + G_OBJECT_CLASS (klass)->finalize = matrix_event_room_topic_finalize; + + /** + * MatrixEventRoomTopic:topic: + * + * The topic text. + */ + matrix_event_room_topic_properties[PROP_TOPIC] = g_param_spec_string( + "topic", "topic", "topic", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_TOPIC, matrix_event_room_topic_properties[PROP_TOPIC]); +} + +static void +matrix_event_room_topic_init(MatrixEventRoomTopic *matrix_event_room_topic) +{ + MatrixEventRoomTopicPrivate *priv; + + priv = matrix_event_room_topic_get_instance_private(matrix_event_room_topic); + + priv->_topic = NULL; +} diff --git a/src/matrix-event-room-topic.h b/src/matrix-event-room-topic.h new file mode 100644 index 0000000..3fed73c --- /dev/null +++ b/src/matrix-event-room-topic.h @@ -0,0 +1,40 @@ +/* + * 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 + * . + */ + +#ifndef __MATRIX_GLIB_SDK_EVENT_ROOM_TOPIC_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_TOPIC_H__ + +# include +# include "matrix-event-state-base.h" + +G_BEGIN_DECLS + +# define MATRIX_EVENT_TYPE_ROOM_TOPIC matrix_event_room_topic_get_type () +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomTopic, matrix_event_room_topic, MATRIX_EVENT, ROOM_TOPIC, MatrixEventState) + +struct _MatrixEventRoomTopicClass { + MatrixEventStateClass parent_class; +}; + +MatrixEventRoomTopic* matrix_event_room_topic_new (void); +const gchar* matrix_event_room_topic_get_topic (MatrixEventRoomTopic* self); +void matrix_event_room_topic_set_topic (MatrixEventRoomTopic* self, const gchar* value); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_TOPIC_H__ */ diff --git a/src/matrix-event-room-topic.vala b/src/matrix-event-room-topic.vala deleted file mode 100644 index bf56409..0000000 --- a/src/matrix-event-room-topic.vala +++ /dev/null @@ -1,74 +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 - * . - */ - -/** - * Event type to hold the m.room.topic event. - * - * A topic is a short message detailing what is currently being - * discussed in the room. It can also be used as a way to display - * extra information about the room, which may not be suitable for the - * room name. - */ -public class Matrix.Event.RoomTopic : Matrix.Event.State { - /** - * The topic text. - */ - public string? topic { get; set; default = null; } - - protected override void - from_json(Json.Node json_data) - throws Matrix.Error - { - var root = json_data.get_object(); - var content_root = root.get_member("content").get_object(); - Json.Node? node = null; - - if (Config.DEBUG && ((node = root.get_member("state_key")) != null)) { - if (node.get_string() != "") { - warning("state_key of a m.room.topic event is non-empty"); - } - } - - if ((node = content_root.get_member("topic")) != null) { - _topic = node.get_string(); - } else if (Config.DEBUG) { - warning("content.topic is missing from an m.room.topic event"); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - var root = json_data.get_object(); - var content_root = root.get_member("content").get_object(); - - if (_state_key != "") { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.room.topic event with a non-empty state_key"); - } - - if (_topic != null) { - content_root.set_string_member("topic", _topic); - } - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 479ddf0..f749728 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -23,6 +23,7 @@ #include "matrix-event-typing.h" #include "matrix-event-room-member.h" #include "matrix-event-receipt.h" +#include "matrix-event-room-topic.h" /* Borrowed from GLib diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index fd5e6d5..0de9211 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -582,5 +582,16 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-room-topic.h")] + public class RoomTopic : State { + public string? topic { 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; + } } }