From b6162377a9370ab8cc69d1d3e6883c9f717a8eca Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 21 Nov 2017 15:31:40 +0100 Subject: [PATCH] Port MatrixEventRoomCanonicalAlias to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-canonical-alias.c | 245 +++++++++++++++++++++ src/matrix-event-room-canonical-alias.h | 40 ++++ src/matrix-event-room-canonical-alias.vala | 71 ------ src/matrix-event-types.c | 1 + vapi/c-api.vapi | 11 + 7 files changed, 299 insertions(+), 73 deletions(-) create mode 100644 src/matrix-event-room-canonical-alias.c create mode 100644 src/matrix-event-room-canonical-alias.h delete mode 100644 src/matrix-event-room-canonical-alias.vala diff --git a/.gitignore b/.gitignore index 451b333..34ba363 100644 --- a/.gitignore +++ b/.gitignore @@ -53,7 +53,6 @@ Makefile.in /src/matrix-http-client.c /src/namespace-info.vala /src/namespace-info.c -/src/matrix-event-room-canonical-alias.c /src/matrix-event-room-create.c /src/matrix-event-room-power-levels.c /src/matrix-event-room-message-feedback.c diff --git a/src/Makefile.am b/src/Makefile.am index 6eb40c3..ccea69d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-client.vala \ matrix-http-api.vala \ matrix-http-client.vala \ - matrix-event-room-canonical-alias.vala \ matrix-event-room-create.vala \ matrix-event-room-power-levels.vala \ matrix-event-room-message-feedback.vala \ @@ -110,6 +109,7 @@ INST_H_SRC_FILES = \ matrix-event-room-message.h \ matrix-event-room-history-visibility.h \ matrix-event-room-join-rules.h \ + matrix-event-room-canonical-alias.h \ matrix-event-typing.h \ matrix-event-receipt.h \ utils.h \ @@ -163,6 +163,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-room-message.c \ matrix-event-room-history-visibility.c \ matrix-event-room-join-rules.c \ + matrix-event-room-canonical-alias.c \ matrix-profile.c \ matrix-room.c \ utils.c \ diff --git a/src/matrix-event-room-canonical-alias.c b/src/matrix-event-room-canonical-alias.c new file mode 100644 index 0000000..012326b --- /dev/null +++ b/src/matrix-event-room-canonical-alias.c @@ -0,0 +1,245 @@ +/* + * 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-canonical-alias.h" +#include "matrix-types.h" +#include "config.h" + +/** + * SECTION:matrix-event-room-canonical-alias + * @short_description: event to set a room’s canonical alias + * + * This is the default handler for `m.room.canonical_alias` events. + * + * This event is used to inform the room about which alias should be considered the canonical + * one. This could be for display purposes or as suggestion to users which alias to use to + * advertise the room. + */ +enum { + PROP_0, + PROP_CANONICAL_ALIAS, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_room_canonical_alias_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _canonical_alias; +} MatrixEventRoomCanonicalAliasPrivate; + +/** + * MatrixEventRoomCanonicalAlias: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomCanonicalAlias, matrix_event_room_canonical_alias, MATRIX_EVENT_TYPE_STATE); + +static void +matrix_event_room_canonical_alias_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomCanonicalAliasPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail (json_data != NULL); + + priv = matrix_event_room_canonical_alias_get_instance_private(MATRIX_EVENT_ROOM_CANONICAL_ALIAS(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 + if ((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.canonical_alias event is non-empty"); + } + } +#endif + + if ((node = json_object_get_member(content_root, "alias")) != NULL) { + g_free(priv->_canonical_alias); + priv->_canonical_alias = g_strdup(json_node_get_string(node)); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_canonical_alias_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_room_canonical_alias_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomCanonicalAliasPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + const gchar *state_key; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_canonical_alias_get_instance_private(MATRIX_EVENT_ROOM_CANONICAL_ALIAS(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.canonical_alias event with a non-empty state_key"); + + return; + } + + if (priv->_canonical_alias != NULL) { + json_object_set_string_member(content_root, "alias", priv->_canonical_alias); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_canonical_alias_parent_class)->to_json(matrix_event_base, json_data, error); +} + +/** + * matrix_event_room_canonical_alias_new: + * + * Create a new #MatrixEventRoomCanonicalAlias object + * + * Returns: (transfer full): a new #MatrixEventRoomCanonicalAlias + */ +MatrixEventRoomCanonicalAlias * +matrix_event_room_canonical_alias_new(void) +{ + return (MatrixEventRoomCanonicalAlias *)matrix_event_state_construct(MATRIX_EVENT_TYPE_ROOM_CANONICAL_ALIAS); +} + +/** + * matrix_event_room_canonical_alias_get_canonical_alias: + * @event: a #MatrixEventRoomCanonicalAlias + * + * Get the canonical alias from @event. + * + * The returned value is owned by @event and should not be freed. + * + * Returns: (transfer none) (nullable): the canonical alias + */ +const gchar * +matrix_event_room_canonical_alias_get_canonical_alias(MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias) +{ + MatrixEventRoomCanonicalAliasPrivate *priv; + + g_return_val_if_fail(matrix_event_room_canonical_alias != NULL, NULL); + + priv = matrix_event_room_canonical_alias_get_instance_private(matrix_event_room_canonical_alias); + + return priv->_canonical_alias; +} + +/** + * matrix_event_room_canonical_alias_set_canonical_alias: + * @event: a #MatrixEventRoomCanonicalAlias + * @canonical_alias: (transfer none) (nullable): a canonical alias + * + * Set the canonical alias in @event. + */ +void +matrix_event_room_canonical_alias_set_canonical_alias(MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias, const gchar *canonical_alias) +{ + MatrixEventRoomCanonicalAliasPrivate *priv; + + g_return_if_fail(matrix_event_room_canonical_alias != NULL); + + priv = matrix_event_room_canonical_alias_get_instance_private(matrix_event_room_canonical_alias); + + if (g_strcmp0(canonical_alias, priv->_canonical_alias) != 0) { + g_free(priv->_canonical_alias); + priv->_canonical_alias = g_strdup(canonical_alias); + + g_object_notify_by_pspec((GObject *)matrix_event_room_canonical_alias, matrix_event_room_canonical_alias_properties[PROP_CANONICAL_ALIAS]); + } +} + +static void +matrix_event_room_canonical_alias_finalize(GObject *gobject) +{ + MatrixEventRoomCanonicalAliasPrivate *priv = matrix_event_room_canonical_alias_get_instance_private(MATRIX_EVENT_ROOM_CANONICAL_ALIAS(gobject)); + + g_free(priv->_canonical_alias); + + G_OBJECT_CLASS(matrix_event_room_canonical_alias_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_canonical_alias_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias = MATRIX_EVENT_ROOM_CANONICAL_ALIAS(gobject); + + switch (property_id) { + case PROP_CANONICAL_ALIAS: + g_value_set_string(value, matrix_event_room_canonical_alias_get_canonical_alias(matrix_event_room_canonical_alias)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_canonical_alias_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias = MATRIX_EVENT_ROOM_CANONICAL_ALIAS(gobject); + + switch (property_id) { + case PROP_CANONICAL_ALIAS: + matrix_event_room_canonical_alias_set_canonical_alias(matrix_event_room_canonical_alias, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + break; + } +} + +static void +matrix_event_room_canonical_alias_class_init(MatrixEventRoomCanonicalAliasClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_canonical_alias_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_canonical_alias_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_room_canonical_alias_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_room_canonical_alias_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_room_canonical_alias_finalize; + + /** + * MatrixEventRoomCanonicalAlias:canonical-alias: + * + * The canonical alias. + */ + matrix_event_room_canonical_alias_properties[PROP_CANONICAL_ALIAS] = g_param_spec_string( + "canonical-alias", "canonical-alias", "canonical-alias", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_CANONICAL_ALIAS, matrix_event_room_canonical_alias_properties[PROP_CANONICAL_ALIAS]); +} + +static void +matrix_event_room_canonical_alias_init(MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias) +{ + MatrixEventRoomCanonicalAliasPrivate *priv = matrix_event_room_canonical_alias_get_instance_private(matrix_event_room_canonical_alias); + + priv->_canonical_alias = NULL; +} diff --git a/src/matrix-event-room-canonical-alias.h b/src/matrix-event-room-canonical-alias.h new file mode 100644 index 0000000..c90ee99 --- /dev/null +++ b/src/matrix-event-room-canonical-alias.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_CANONICAL_ALIAS_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_CANONICAL_ALIAS_H__ + +# include +# include "matrix-event-state-base.h" + +G_BEGIN_DECLS + +#define MATRIX_EVENT_TYPE_ROOM_CANONICAL_ALIAS (matrix_event_room_canonical_alias_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomCanonicalAlias, matrix_event_room_canonical_alias, MATRIX_EVENT, ROOM_CANONICAL_ALIAS, MatrixEventState) + +struct _MatrixEventRoomCanonicalAliasClass { + MatrixEventStateClass parent_class; +}; + +MatrixEventRoomCanonicalAlias *matrix_event_room_canonical_alias_new (void); +const gchar *matrix_event_room_canonical_alias_get_canonical_alias (MatrixEventRoomCanonicalAlias *event); +void matrix_event_room_canonical_alias_set_canonical_alias (MatrixEventRoomCanonicalAlias *event, const gchar *canonical_alias); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_CANONICAL_ALIAS_H__ */ diff --git a/src/matrix-event-room-canonical-alias.vala b/src/matrix-event-room-canonical-alias.vala deleted file mode 100644 index 9066a12..0000000 --- a/src/matrix-event-room-canonical-alias.vala +++ /dev/null @@ -1,71 +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.canonical_alias event - * - * This event is used to inform the room about which alias should be - * considered the canonical one. This could be for display purposes or - * as suggestion to users which alias to use to advertise the room. - */ -public class Matrix.Event.RoomCanonicalAlias : Matrix.Event.State { - /** - * The canonical alias. - */ - public string? canonical_alias { 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.canonical_alias event is non-empty"); - } - } - - if ((node = content_root.get_member("alias")) != null) { - _canonical_alias = node.get_string(); - } - - 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 generate a m.room.canonical_alias event with a non-empty state_key"); - } - - if (_canonical_alias != null) { - content_root.set_string_member("alias", _canonical_alias); - } - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 841d38d..e73cc98 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -30,6 +30,7 @@ #include "matrix-event-room-message.h" #include "matrix-event-room-history-visibility.h" #include "matrix-event-room-join-rules.h" +#include "matrix-event-room-canonical-alias.h" #include "matrix-event-call-answer.h" #include "matrix-event-call-hangup.h" #include "matrix-event-call-invite.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 36ea055..3790c05 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -774,6 +774,17 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-room-canonical-alias.h")] + public class RoomCanonicalAlias : State { + public string? canonical_alias { 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")]