From 33b36a4e82cdf6d6dcf08ac148a53ce8dfcd4362 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 20 Nov 2017 10:03:14 +0100 Subject: [PATCH] Port MatrixMessageAudio to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-types.c | 1 + src/matrix-message-audio.c | 267 ++++++++++++++++++++++++++++++++++ src/matrix-message-audio.h | 44 ++++++ src/matrix-message-audio.vala | 75 ---------- vapi/c-api.vapi | 14 ++ 7 files changed, 328 insertions(+), 77 deletions(-) create mode 100644 src/matrix-message-audio.c create mode 100644 src/matrix-message-audio.h delete mode 100644 src/matrix-message-audio.vala diff --git a/.gitignore b/.gitignore index d38afa6..3ca2361 100644 --- a/.gitignore +++ b/.gitignore @@ -69,6 +69,5 @@ Makefile.in /src/matrix-event-call-hangup.c /src/matrix-glib-0.0.pc /src/matrix-message-notice.c -/src/matrix-message-audio.c /src/matrix-message-video.c /src/matrix-room.c diff --git a/src/Makefile.am b/src/Makefile.am index b860a58..f685489 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,7 +36,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-call-answer.vala \ matrix-event-call-hangup.vala \ matrix-message-notice.vala \ - matrix-message-audio.vala \ matrix-message-video.vala \ matrix-room.vala \ $(NULL) @@ -102,6 +101,7 @@ INST_H_SRC_FILES = \ matrix-message-emote.h \ matrix-message-file.h \ matrix-message-image.h \ + matrix-message-audio.h \ matrix-event-room-base.h \ matrix-event-state-base.h \ matrix-event-tag.h \ @@ -143,6 +143,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-message-emote.c \ matrix-message-file.c \ matrix-message-image.c \ + matrix-message-audio.c \ matrix-event-tag.c \ matrix-event-presence.c \ matrix-event-room-member.c \ diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index dd5af69..d1a99dd 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -33,6 +33,7 @@ #include "matrix-message-emote.h" #include "matrix-message-file.h" #include "matrix-message-image.h" +#include "matrix-message-audio.h" /* Borrowed from GLib diff --git a/src/matrix-message-audio.c b/src/matrix-message-audio.c new file mode 100644 index 0000000..3fa8187 --- /dev/null +++ b/src/matrix-message-audio.c @@ -0,0 +1,267 @@ +/* + * 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-message-audio.h" +#include "matrix-enumtypes.h" +#include "config.h" + +/** + * SECTION:matrix-message-audio + * @short_description: message representing a single audio clip + * + * This is the default message handler for `m.audio` messages. + */ +enum { + PROP_0, + PROP_URL, + PROP_INFO, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_message_audio_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _url; + MatrixAudioInfo* _info; +} MatrixMessageAudioPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(MatrixMessageAudio, matrix_message_audio, MATRIX_MESSAGE_TYPE_BASE); + +static void +matrix_message_audio_real_from_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error) +{ + MatrixMessageAudioPrivate *priv; + JsonObject *root; + JsonNode *node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_message_audio_get_instance_private(MATRIX_MESSAGE_AUDIO(matrix_message_base)); + root = json_node_get_object(json_data); + + if ((node = json_object_get_member(root, "url")) != NULL) { + g_free(priv->_url); + priv->_url = g_strdup(json_node_get_string(node)); + } else if (DEBUG) { + g_warning("url is missing from a m.audio message"); + } + + if ((node = json_object_get_member(root, "info")) != NULL) { + matrix_audio_info_unref(priv->_info); + priv->_info = matrix_audio_info_new(); + matrix_audio_info_set_from_json(priv->_info, node); + } + + MATRIX_MESSAGE_BASE_CLASS(matrix_message_audio_parent_class)->from_json(matrix_message_base, json_data, error); +} + +static void +matrix_message_audio_real_to_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error) +{ + MatrixMessageAudioPrivate *priv; + JsonObject *root; + + g_return_if_fail(json_data != NULL); + + priv = matrix_message_audio_get_instance_private(MATRIX_MESSAGE_AUDIO(matrix_message_base)); + + if (priv->_url == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.audio message without url"); + + return; + } + + root = json_node_get_object(json_data); + + json_object_set_string_member(root, "url", priv->_url); + + if (priv->_info != NULL) { + GError *inner_error = NULL; + JsonNode *node = matrix_audio_info_get_json_node(priv->_info, &inner_error); + + if (inner_error != NULL) { + g_propagate_error(error, inner_error); + + return; + } + + json_object_set_member(root, "info", node); + } + + MATRIX_MESSAGE_BASE_CLASS(matrix_message_audio_parent_class)->to_json(matrix_message_base, json_data, error); +} + +MatrixMessageAudio * +matrix_message_audio_new(void) +{ + return (MatrixMessageAudio *)matrix_message_base_construct(MATRIX_MESSAGE_TYPE_AUDIO); +} + +const gchar * +matrix_message_audio_get_url(MatrixMessageAudio *matrix_message_audio) +{ + MatrixMessageAudioPrivate *priv; + + g_return_val_if_fail(matrix_message_audio != NULL, NULL); + + priv = matrix_message_audio_get_instance_private(matrix_message_audio); + + return priv->_url; +} + +void +matrix_message_audio_set_url(MatrixMessageAudio *matrix_message_audio, const gchar *url) +{ + MatrixMessageAudioPrivate *priv; + + g_return_if_fail(matrix_message_audio != NULL); + + priv = matrix_message_audio_get_instance_private(matrix_message_audio); + + if (g_strcmp0(url, priv->_url) != 0) { + g_free(priv->_url); + priv->_url = g_strdup(url); + + g_object_notify_by_pspec((GObject *)matrix_message_audio, matrix_message_audio_properties[PROP_URL]); + } +} + +MatrixAudioInfo * +matrix_message_audio_get_info(MatrixMessageAudio *matrix_message_audio) +{ + MatrixMessageAudioPrivate *priv; + + g_return_val_if_fail(matrix_message_audio != NULL, NULL); + + priv = matrix_message_audio_get_instance_private(matrix_message_audio); + + return priv->_info; +} + +void +matrix_message_audio_set_info(MatrixMessageAudio *matrix_message_audio, MatrixAudioInfo *info) +{ + MatrixMessageAudioPrivate *priv; + + g_return_if_fail (matrix_message_audio != NULL); + + priv = matrix_message_audio_get_instance_private(matrix_message_audio); + + if (priv->_info != info) { + matrix_audio_info_unref(priv->_info); + priv->_info = matrix_audio_info_ref(info); + + g_object_notify_by_pspec((GObject *)matrix_message_audio, matrix_message_audio_properties[PROP_INFO]); + } +} +static void +matrix_message_audio_finalize(GObject *gobject) +{ + MatrixMessageAudioPrivate *priv; + + priv = matrix_message_audio_get_instance_private(MATRIX_MESSAGE_AUDIO(gobject)); + + g_free(priv->_url); + matrix_audio_info_unref(priv->_info); + + G_OBJECT_CLASS(matrix_message_audio_parent_class)->finalize(gobject); +} + +static void +matrix_message_audio_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixMessageAudio *matrix_message_audio = MATRIX_MESSAGE_AUDIO(gobject); + + switch (property_id) { + case PROP_URL: + g_value_set_string(value, matrix_message_audio_get_url(matrix_message_audio)); + + break; + case PROP_INFO: + g_value_set_boxed(value, matrix_message_audio_get_info(matrix_message_audio)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_message_audio_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixMessageAudio * matrix_message_audio = MATRIX_MESSAGE_AUDIO(gobject); + + switch (property_id) { + case PROP_URL: + matrix_message_audio_set_url(matrix_message_audio, g_value_get_string(value)); + + break; + case PROP_INFO: + matrix_message_audio_set_info(matrix_message_audio, g_value_get_boxed(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_message_audio_class_init(MatrixMessageAudioClass *klass) +{ + ((MatrixMessageBaseClass *)klass)->from_json = matrix_message_audio_real_from_json; + ((MatrixMessageBaseClass *)klass)->to_json = matrix_message_audio_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_message_audio_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_message_audio_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_message_audio_finalize; + + /** + * MatrixMessageAudio:url: + * + * The URL to the audio clip. + */ + matrix_message_audio_properties[PROP_URL] = g_param_spec_string( + "url", "url", "url", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_URL, matrix_message_audio_properties[PROP_URL]); + + /** + * MatrixMessageAudio:info: + * + * A #MatrixAudioInfo describing #MatrixMessageAudio:url. + */ + matrix_message_audio_properties[PROP_INFO] = g_param_spec_boxed( + "info", "info", "info", + MATRIX_TYPE_AUDIO_INFO, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_INFO, matrix_message_audio_properties[PROP_INFO]); +} + +static void +matrix_message_audio_init(MatrixMessageAudio *matrix_message_audio) +{ + MatrixMessageAudioPrivate *priv = matrix_message_audio_get_instance_private(matrix_message_audio); + + priv->_url = NULL; + priv->_info = NULL; +} diff --git a/src/matrix-message-audio.h b/src/matrix-message-audio.h new file mode 100644 index 0000000..65965e0 --- /dev/null +++ b/src/matrix-message-audio.h @@ -0,0 +1,44 @@ +/* + * 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_MESSAGE_AUDIO_H__ +# define __MATRIX_GLIB_SDK_MESSAGE_AUDIO_H__ + +# include +# include "matrix-message-base.h" +# include "matrix-types.h" + +G_BEGIN_DECLS + +#define MATRIX_MESSAGE_TYPE_AUDIO (matrix_message_audio_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixMessageAudio, matrix_message_audio, MATRIX_MESSAGE, AUDIO, MatrixMessageBase) + +struct _MatrixMessageAudioClass { + MatrixMessageBaseClass parent_class; +}; + +MatrixMessageAudio* matrix_message_audio_new (void); +const gchar* matrix_message_audio_get_url (MatrixMessageAudio* self); +void matrix_message_audio_set_url (MatrixMessageAudio* self, const gchar* value); +MatrixAudioInfo* matrix_message_audio_get_info (MatrixMessageAudio* self); +void matrix_message_audio_set_info (MatrixMessageAudio* self, MatrixAudioInfo* value); + + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_MESSAGE_AUDIO_H__ */ diff --git a/src/matrix-message-audio.vala b/src/matrix-message-audio.vala deleted file mode 100644 index b5c119c..0000000 --- a/src/matrix-message-audio.vala +++ /dev/null @@ -1,75 +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.audio message - * - * This message represents a single audio clip. - */ -public class Matrix.Message.Audio : Matrix.Message.Base { - /** - * The URL to the audio clip. - */ - public string? url { get; set; default = null; } - - /** - * Metadata for the audio clip referred to in `url`. - */ - public AudioInfo? 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("url")) != null) { - _url = node.get_string(); - } else if (Config.DEBUG) { - warning("url is missing from a m.audio message"); - } - - if ((node = root.get_member("info")) != null) { - _info = AudioInfo(); - _info.set_from_json(node); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - if (_url == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.audio message without url"); - } - - var root = json_data.get_object(); - - root.set_string_member("url", _url); - - if (_info != null) { - root.set_member("info", _info.get_json_node()); - } - - base.to_json(json_data); - } -} diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index e8c22ae..0c5bb83 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -754,5 +754,19 @@ namespace Matrix { to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-message-audio.h")] + public class Audio : Base { + public string? url { get; set; default = null; } + public Matrix.AudioInfo? 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; + } } }