From d43f6517d78451d9b43d487962bfe6a54a3615a5 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 19 Nov 2017 10:46:37 +0100 Subject: [PATCH] Port MatrixEventRoomName to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-name.c | 258 ++++++++++++++++++++++++++++++++ src/matrix-event-room-name.h | 41 +++++ src/matrix-event-room-name.vala | 76 ---------- src/matrix-event-types.c | 1 + vapi/c-api.vapi | 13 ++ 7 files changed, 315 insertions(+), 78 deletions(-) create mode 100644 src/matrix-event-room-name.c create mode 100644 src/matrix-event-room-name.h delete mode 100644 src/matrix-event-room-name.vala diff --git a/.gitignore b/.gitignore index d63aa34..42f0c29 100644 --- a/.gitignore +++ b/.gitignore @@ -56,7 +56,6 @@ Makefile.in /src/namespace-info.c /src/matrix-event-room-history-visibility.c /src/matrix-event-room-join-rules.c -/src/matrix-event-room-name.c /src/matrix-event-room-canonical-alias.c /src/matrix-event-room-create.c /src/matrix-event-room-power-levels.c diff --git a/src/Makefile.am b/src/Makefile.am index b5dbaaf..e7166f1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-message.vala \ matrix-event-room-history-visibility.vala \ matrix-event-room-join-rules.vala \ - matrix-event-room-name.vala \ matrix-event-room-canonical-alias.vala \ matrix-event-room-create.vala \ matrix-event-room-power-levels.vala \ @@ -111,6 +110,7 @@ INST_H_SRC_FILES = \ matrix-event-room-topic.h \ matrix-event-room-aliases.h \ matrix-event-room-avatar.h \ + matrix-event-room-name.h \ matrix-event-typing.h \ matrix-event-receipt.h \ utils.h \ @@ -150,6 +150,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-room-topic.c \ matrix-event-room-aliases.c \ matrix-event-room-avatar.c \ + matrix-event-room-name.c \ matrix-profile.c \ utils.c \ matrix-enumtypes.c \ diff --git a/src/matrix-event-room-name.c b/src/matrix-event-room-name.c new file mode 100644 index 0000000..f6e2b65 --- /dev/null +++ b/src/matrix-event-room-name.c @@ -0,0 +1,258 @@ +/* + * 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-name.h" +#include "matrix-enumtypes.h" +#include "config.h" + +/** + * SECTION:matrix-event-room-name + * @short_description: event to handle room name changes + * + * This is the default event handler for `m.room.name` events. + * + * A room has an opaque room ID which is not human-friendly to read. A room alias is + * human-friendly, but not all rooms have room aliases. The room name is a human-friendly + * string designed to be displayed to the end-user. The room name is not unique, as multiple + * rooms can have the same room name set. The room name can also be set when creating a room + * using matrix_api_create_room() with name set. + */ +enum { + PROP_0, + PROP_NAME, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_room_name_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _name; +} MatrixEventRoomNamePrivate; + +/** + * MatrixEventRoomName: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomName, matrix_event_room_name, MATRIX_EVENT_TYPE_STATE); + +static void +matrix_event_room_name_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomNamePrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail (json_data != NULL); + + priv = matrix_event_room_name_get_instance_private(MATRIX_EVENT_ROOM_NAME(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)) { + gchar *state_key = (gchar *)json_node_get_string(node); + + if ((state_key != NULL) && (*state_key == 0)) { + g_warning("state_key of a m.room.name event is non-empty"); + } + } + + if ((node = json_object_get_member(content_root, "name")) != NULL) { + g_free(priv->_name); + priv->_name = g_strdup(json_node_get_string(node)); + } else { + g_warning("content.name is missing from a m.room.name event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_name_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_room_name_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomNamePrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + const gchar *state_key; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_name_get_instance_private(MATRIX_EVENT_ROOM_NAME(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 send a m.room.name with a non-empty state key"); + + return; + } + + if ((priv->_name == NULL) || (*(priv->_name) == 0)) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't send a m.room.name event without a name"); + + return; + } + + json_object_set_string_member(content_root, "name", priv->_name); + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_name_parent_class)->to_json(matrix_event_base, json_data, error); +} + +MatrixEventRoomName * +matrix_event_room_name_construct(GType object_type) +{ + return (MatrixEventRoomName *)matrix_event_state_construct(object_type); +} + +/** + * matrix_event_room_name_new: + * + * Create a new #MatrixEventRoomName object. + * + * Returns: (transfer full): a new #MatrixEventRoomName object + */ +MatrixEventRoomName * +matrix_event_room_name_new(void) +{ + return matrix_event_room_name_construct(MATRIX_EVENT_TYPE_ROOM_NAME); +} + +/** + * matrix_event_room_name_get_name: + * @event: a #MatrixEventRoomName + * + * Get the name from @event. + * + * The returned value is owned by @event and should not be freed. + * + * Returns: (transfer none) (nullable): the room name + */ +const gchar * +matrix_event_room_name_get_name(MatrixEventRoomName *matrix_event_room_name) +{ + MatrixEventRoomNamePrivate *priv; + + g_return_val_if_fail(matrix_event_room_name != NULL, NULL); + + priv = matrix_event_room_name_get_instance_private(matrix_event_room_name); + + return priv->_name; +} + +/** + * matrix_event_room_name_set_name: + * @event: a #MatrixEventRoomName + * @name: a room name + * + * Set the name in @event. + */ +void +matrix_event_room_name_set_name(MatrixEventRoomName *matrix_event_room_name, const gchar *name) +{ + MatrixEventRoomNamePrivate *priv; + + g_return_if_fail(matrix_event_room_name != NULL); + + priv = matrix_event_room_name_get_instance_private(matrix_event_room_name); + + if (g_strcmp0(name, priv->_name) != 0) { + g_free(priv->_name); + priv->_name = g_strdup(name); + + g_object_notify_by_pspec((GObject *)matrix_event_room_name, matrix_event_room_name_properties[PROP_NAME]); + } +} + +static void +matrix_event_room_name_finalize(GObject *gobject) +{ + MatrixEventRoomNamePrivate *priv = matrix_event_room_name_get_instance_private(MATRIX_EVENT_ROOM_NAME(gobject)); + + g_free(priv->_name); + + G_OBJECT_CLASS(matrix_event_room_name_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_name_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomName *matrix_event_room_name = MATRIX_EVENT_ROOM_NAME(gobject); + + switch (property_id) { + case PROP_NAME: + g_value_set_string(value, matrix_event_room_name_get_name(matrix_event_room_name)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_name_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomName *matrix_event_room_name = MATRIX_EVENT_ROOM_NAME(gobject); + + switch (property_id) { + case PROP_NAME: + matrix_event_room_name_set_name(matrix_event_room_name, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_name_class_init(MatrixEventRoomNameClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_name_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_name_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_room_name_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_room_name_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_room_name_finalize; + + /** + * MatrixEventRoomName:name: + * + * The room name. + */ + matrix_event_room_name_properties[PROP_NAME] = g_param_spec_string( + "name", "name", "name", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_NAME, matrix_event_room_name_properties[PROP_NAME]); +} + +static void +matrix_event_room_name_init(MatrixEventRoomName *matrix_event_room_name) +{ + MatrixEventRoomNamePrivate *priv = matrix_event_room_name_get_instance_private(matrix_event_room_name); + + priv->_name = NULL; +} diff --git a/src/matrix-event-room-name.h b/src/matrix-event-room-name.h new file mode 100644 index 0000000..708b94e --- /dev/null +++ b/src/matrix-event-room-name.h @@ -0,0 +1,41 @@ +/* + * 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_NAME_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_NAME_H__ + +# include +# include "matrix-event-state-base.h" + +G_BEGIN_DECLS + +# define MATRIX_EVENT_TYPE_ROOM_NAME matrix_event_room_name_get_type() +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomName, matrix_event_room_name, MATRIX_EVENT, ROOM_NAME, MatrixEventState); + +struct _MatrixEventRoomNameClass { + MatrixEventStateClass parent_class; +}; + +MatrixEventRoomName *matrix_event_room_name_new(void); +MatrixEventRoomName *matrix_event_room_name_construct(GType object_type); +const gchar *matrix_event_room_name_get_name(MatrixEventRoomName *event); +void matrix_event_room_name_set_name(MatrixEventRoomName *event, const gchar *name); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_NAME_H__ */ diff --git a/src/matrix-event-room-name.vala b/src/matrix-event-room-name.vala deleted file mode 100644 index 15430e7..0000000 --- a/src/matrix-event-room-name.vala +++ /dev/null @@ -1,76 +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 - * . - */ - -/** - * Class to hold a m.room.name event. - * - * A room has an opaque room ID which is not human-friendly to read. A - * room alias is human-friendly, but not all rooms have room - * aliases. The room name is a human-friendly string designed to be - * displayed to the end-user. The room name is not unique, as multiple - * rooms can have the same room name set. The room name can also be - * set when creating a room using createRoom with the name key. - */ -public class Matrix.Event.RoomName : Matrix.Event.State { - public string? name { 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.name event is non-empty"); - } - } - - if ((node = content_root.get_member("name")) != null) { - _name = node.get_string(); - } else { - warning("content.name is missing from a m.room.name event"); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - var content_root = json_data.get_object() - .get_member("content").get_object(); - - if (_state_key != "") { - throw new Matrix.Error.INCOMPLETE( - "Won't send a m.room.name with a non-empty state key"); - } - - if (_name == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't send a m.room.name event without a name"); - } - - content_root.set_string_member("name", _name); - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index b469da6..7a66c99 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -26,6 +26,7 @@ #include "matrix-event-room-topic.h" #include "matrix-event-room-aliases.h" #include "matrix-event-room-avatar.h" +#include "matrix-event-room-name.h" #include "matrix-message-text.h" #include "matrix-message-location.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 1a931fc..ebdf9ce 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -620,6 +620,19 @@ namespace Matrix { to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-room-name.h")] + public class RoomName : State { + public string? name { 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; + } } [CCode (gir_namespace = "MatrixMessage", gir_version = "0.0")]