diff --git a/.gitignore b/.gitignore index a4fb7f4..ed1862d 100644 --- a/.gitignore +++ b/.gitignore @@ -62,6 +62,5 @@ Makefile.in /src/matrix-event-room-third-party-invite.c /src/matrix-event-call-invite.c /src/matrix-event-call-candidates.c -/src/matrix-event-call-answer.c /src/matrix-event-call-hangup.c /src/matrix-glib-0.0.pc diff --git a/src/Makefile.am b/src/Makefile.am index ca6fb74..b06d608 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,7 +30,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-third-party-invite.vala \ matrix-event-call-invite.vala \ matrix-event-call-candidates.vala \ - matrix-event-call-answer.vala \ matrix-event-call-hangup.vala \ $(NULL) @@ -89,6 +88,7 @@ INST_H_SRC_FILES = \ matrix-compacts.h \ matrix-event-base.h \ matrix-event-call-base.h \ + matrix-event-call-answer.h \ matrix-message-base.h \ matrix-message-text.h \ matrix-message-location.h \ @@ -137,6 +137,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-compacts.c \ matrix-event-base.c \ matrix-event-call-base.c \ + matrix-event-call-answer.c \ matrix-message-base.c \ matrix-message-text.c \ matrix-message-location.c \ diff --git a/src/matrix-event-call-answer.c b/src/matrix-event-call-answer.c new file mode 100644 index 0000000..d25e4a2 --- /dev/null +++ b/src/matrix-event-call-answer.c @@ -0,0 +1,343 @@ +/* + * 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-call-answer.h" +#include "utils.h" +#include "config.h" + +/** + * SECTION:matrix-event-call-answer + * @short_description: event sent when a callee wishes to answer the call + * + * This event is sent by the callee when they wish to answer the call. + */ + +enum { + PROP_0, + PROP_ANSWER_TYPE, + PROP_ANSWER_SDP, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_call_answer_properties[NUM_PROPERTIES]; + +typedef struct { + MatrixCallAnswerType _answer_type; + gchar* _answer_sdp; +} MatrixEventCallAnswerPrivate; + +/** + * MatrixEventCallAnswer: + * + * Object structure. + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventCallAnswer, matrix_event_call_answer, MATRIX_EVENT_TYPE_CALL); + +static void +matrix_event_call_answer_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventCallAnswerPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_call_answer_get_instance_private(MATRIX_EVENT_CALL_ANSWER(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 ((node = json_object_get_member(content_root, "answer")) != NULL) { + JsonObject *answer_root = json_node_get_object(node); + + if ((node = json_object_get_member(answer_root, "type")) != NULL) { + GError *inner_error = NULL; + MatrixCallAnswerType answer_type = _matrix_g_enum_nick_to_value(MATRIX_TYPE_CALL_ANSWER_TYPE, json_node_get_string(node), &inner_error); + + if (inner_error != NULL) { + priv->_answer_type = MATRIX_CALL_ANSWER_TYPE_UNKNOWN; + +#if DEBUG + g_warning("Unknown value %s for content.answer.type in a m.call.answer event", json_node_get_string(node)); +#endif + } else { + priv->_answer_type = answer_type; + } + } else { + g_warning("content.answer.type is missing from a m.call.answer event"); + } + + if ((node = json_object_get_member(answer_root, "sdp")) != NULL) { + g_free(priv->_answer_sdp); + priv->_answer_sdp = g_strdup(json_node_get_string(node)); + } else { + g_warning("content.answer.sdp is missing from a m.call.answer event"); + } + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_call_answer_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_call_answer_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventCallAnswerPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonObject *answer_root; + JsonNode *content_node; + JsonNode *answer_node; + gchar *answer_type; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_call_answer_get_instance_private(MATRIX_EVENT_CALL_ANSWER(matrix_event_base)); + + if (priv->_answer_type == MATRIX_CALL_ANSWER_TYPE_UNKNOWN) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.call.answer event without a valid answer.type"); + + return; + } + + if (priv->_answer_sdp == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.call.answer event without answer.sdp"); + + return; + } + + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + + answer_root = json_object_new(); + answer_node = json_node_new(JSON_NODE_OBJECT); + json_node_set_object(answer_node, answer_root); + + answer_type = _matrix_g_enum_to_string(MATRIX_TYPE_CALL_ANSWER_TYPE, priv->_answer_type, '_'); + json_object_set_string_member(answer_root, "type", answer_type); + g_free(answer_type); + + json_object_set_string_member(answer_root, "sdp", priv->_answer_sdp); + + json_object_set_member(content_root, "answer", answer_node); + + MATRIX_EVENT_BASE_CLASS(matrix_event_call_answer_parent_class)->to_json(matrix_event_base, json_data, error); +} + +MatrixEventCallAnswer * +matrix_event_call_answer_construct(GType object_type) +{ + return (MatrixEventCallAnswer *)matrix_event_call_construct(object_type); +} + +/** + * matrix_event_call_answer_new: + * + * Create a new #MatrixEventCallAnswer object. + * + * Returns: (transfer full): a new #MatrixEventCallAnswer object + */ +MatrixEventCallAnswer * +matrix_event_call_answer_new(void) +{ + return matrix_event_call_answer_construct(MATRIX_EVENT_TYPE_CALL_ANSWER); +} + +/** + * matrix_event_call_answer_get_answer_type: + * @event: a #MatrixEventCallAnswer + * + * Get the answer type from @event. + * + * Returns: the answer type. + */ +MatrixCallAnswerType +matrix_event_call_answer_get_answer_type(MatrixEventCallAnswer *matrix_event_call_answer) +{ + MatrixEventCallAnswerPrivate *priv; + + g_return_val_if_fail(matrix_event_call_answer != NULL, 0); + + priv = matrix_event_call_answer_get_instance_private(matrix_event_call_answer); + + return priv->_answer_type; +} + +/** + * matrix_event_call_answer_set_answer_type: + * @event: a #MatrixEventCallAnswer + * @answer_type: the answer type to set + * + * Set the answer type in @event. + */ +void +matrix_event_call_answer_set_answer_type(MatrixEventCallAnswer *matrix_event_call_answer, MatrixCallAnswerType answer_type) +{ + MatrixEventCallAnswerPrivate *priv; + + g_return_if_fail(matrix_event_call_answer != NULL); + + priv = matrix_event_call_answer_get_instance_private(matrix_event_call_answer); + + if (priv->_answer_type != answer_type) { + priv->_answer_type = answer_type; + + g_object_notify_by_pspec((GObject *)matrix_event_call_answer, matrix_event_call_answer_properties[PROP_ANSWER_TYPE]); + } +} + +/** + * matrix_event_call_answer_get_answer_sdp: + * @event: a #MatrixEventCallAnswer + * + * Get the answer SDP of @event. + * + * The returned value is owned by @event and should not be freed. + * + * Returns: (transfer none) (nullable): the answer SDP + */ +const gchar * +matrix_event_call_answer_get_answer_sdp(MatrixEventCallAnswer *matrix_event_call_answer) +{ + MatrixEventCallAnswerPrivate *priv; + + g_return_val_if_fail(matrix_event_call_answer != NULL, NULL); + + priv = matrix_event_call_answer_get_instance_private(matrix_event_call_answer); + + return priv->_answer_sdp; +} + +/** + * matrix_event_call_answer_set_answer_sdp: + * @event: a #MatrixEventCallAnswer + * @answer_sdp: (transfer none): an answew SDP + * + * Set the answer SDP in @event. + */ +void +matrix_event_call_answer_set_answer_sdp(MatrixEventCallAnswer *matrix_event_call_answer, const gchar *answer_sdp) +{ + MatrixEventCallAnswerPrivate *priv; + + g_return_if_fail(matrix_event_call_answer != NULL); + + priv = matrix_event_call_answer_get_instance_private(matrix_event_call_answer); + + if (g_strcmp0(answer_sdp, priv->_answer_sdp) != 0) { + g_free(priv->_answer_sdp); + priv->_answer_sdp = g_strdup(answer_sdp); + + g_object_notify_by_pspec((GObject *)matrix_event_call_answer, matrix_event_call_answer_properties[PROP_ANSWER_SDP]); + } +} + +static void +matrix_event_call_answer_finalize(GObject *gobject) { + MatrixEventCallAnswerPrivate *priv = matrix_event_call_answer_get_instance_private(MATRIX_EVENT_CALL_ANSWER(gobject)); + + g_free(priv->_answer_sdp); + + G_OBJECT_CLASS(matrix_event_call_answer_parent_class)->finalize(gobject); +} + +static void +matrix_event_call_answer_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventCallAnswer *matrix_event_call_answer = MATRIX_EVENT_CALL_ANSWER(gobject); + + switch (property_id) { + case PROP_ANSWER_TYPE: + g_value_set_enum(value, matrix_event_call_answer_get_answer_type(matrix_event_call_answer)); + + break; + case PROP_ANSWER_SDP: + g_value_set_string(value, matrix_event_call_answer_get_answer_sdp(matrix_event_call_answer)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_call_answer_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventCallAnswer *matrix_event_call_answer = MATRIX_EVENT_CALL_ANSWER(gobject); + + switch (property_id) { + case PROP_ANSWER_TYPE: + matrix_event_call_answer_set_answer_type(matrix_event_call_answer, g_value_get_enum(value)); + + break; + case PROP_ANSWER_SDP: + matrix_event_call_answer_set_answer_sdp(matrix_event_call_answer, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_call_answer_class_init(MatrixEventCallAnswerClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_call_answer_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_call_answer_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_call_answer_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_call_answer_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_call_answer_finalize; + + /** + * MatrixEventCallAnswer:answer-type: + * + * The type of session description. + */ + matrix_event_call_answer_properties[PROP_ANSWER_TYPE] = g_param_spec_enum( + "answer-type", "answer-type", "answer-type", + MATRIX_TYPE_CALL_ANSWER_TYPE, MATRIX_CALL_ANSWER_TYPE_UNKNOWN, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_ANSWER_TYPE, matrix_event_call_answer_properties[PROP_ANSWER_TYPE]); + + /** + * MatrixEventCallAnswer:answer-sdp: + * The SDP text of the session description. + */ + matrix_event_call_answer_properties[PROP_ANSWER_SDP] = g_param_spec_string( + "answer-sdp", "answer-sdp", "answer-sdp", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_ANSWER_SDP, matrix_event_call_answer_properties[PROP_ANSWER_SDP]); +} + +static void +matrix_event_call_answer_init(MatrixEventCallAnswer *matrix_event_call_answer) +{ + MatrixEventCallAnswerPrivate *priv = matrix_event_call_answer_get_instance_private(matrix_event_call_answer); + + priv->_answer_type = MATRIX_CALL_ANSWER_TYPE_UNKNOWN; + priv->_answer_sdp = NULL; +} diff --git a/src/matrix-event-call-answer.h b/src/matrix-event-call-answer.h new file mode 100644 index 0000000..054656c --- /dev/null +++ b/src/matrix-event-call-answer.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_EVENT_CALL_ANSWER_H__ +# define __MATRIX_GLIB_SDK_EVENT_CALL_ANSWER_H__ + +# include +# include "matrix-event-call-base.h" +# include "matrix-enumtypes.h" + +G_BEGIN_DECLS + +# define MATRIX_EVENT_TYPE_CALL_ANSWER (matrix_event_call_answer_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventCallAnswer, matrix_event_call_answer, MATRIX_EVENT, CALL_ANSWER, MatrixEventCall); + +struct _MatrixEventCallAnswerClass { + MatrixEventCallClass parent_class; +}; + +MatrixEventCallAnswer* matrix_event_call_answer_new (void); +MatrixEventCallAnswer* matrix_event_call_answer_construct (GType object_type); +MatrixCallAnswerType matrix_event_call_answer_get_answer_type (MatrixEventCallAnswer *event); +void matrix_event_call_answer_set_answer_type (MatrixEventCallAnswer *event, MatrixCallAnswerType answer_type); +const gchar* matrix_event_call_answer_get_answer_sdp (MatrixEventCallAnswer *event); +void matrix_event_call_answer_set_answer_sdp (MatrixEventCallAnswer *event, const gchar *answer_sdp); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_CALL_ANSWER_H__ */ diff --git a/src/matrix-event-call-answer.vala b/src/matrix-event-call-answer.vala deleted file mode 100644 index 3b621a2..0000000 --- a/src/matrix-event-call-answer.vala +++ /dev/null @@ -1,100 +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 - * . - */ - -/** - * This event is sent by the callee when they wish to answer the call. - */ -public class Matrix.Event.CallAnswer : Matrix.Event.Call { - /** - * The type of session description. - */ - public CallAnswerType answer_type { get; set; default = CallAnswerType.UNKNOWN; } - - /** - * The SDP text of the session description. - */ - public string? answer_sdp { get; set; default = null; } - - protected override void - from_json(Json.Node json_data) - throws Matrix.Error - { - var content_root = json_data.get_object() - .get_member("content").get_object(); - Json.Node? node; - - if ((node = content_root.get_member("answer")) != null) { - var answer_root = node.get_object(); - - if ((node = answer_root.get_member("type")) != null) { - try { - _answer_type = (CallAnswerType)_g_enum_nick_to_value( - typeof(CallAnswerType), node.get_string()); - } catch (Matrix.Error e) { - _answer_type = CallAnswerType.UNKNOWN; - - if (Config.DEBUG) { - warning("Unknown value %s for content.answer.type in a m.call.answer event", - node.get_string()); - } - } - } else { - warning("content.answer.type is missing from a m.call.answer event"); - } - - if ((node = answer_root.get_member("sdp")) != null) { - _answer_sdp = node.get_string(); - } else { - warning("content.answer.sdp is missing from a m.call.answer event"); - } - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - if (_answer_type == CallAnswerType.UNKNOWN) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.call.answer event without a valid answer.type"); - } - - if (_answer_sdp == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.call.answer event without answer.sdp"); - } - - var content_root = json_data.get_object() - .get_member("content").get_object(); - - var answer_root = new Json.Object(); - var answer_node = new Json.Node(Json.NodeType.OBJECT); - answer_node.set_object(answer_root); - - answer_root.set_string_member("type", - _g_enum_value_to_nick(typeof(CallAnswerType), - _answer_type)); - answer_root.set_string_member("sdp", _answer_sdp); - - content_root.set_member("answer", answer_node); - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index b17648e..d700f5a 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-call-answer.h" #include "matrix-message-text.h" #include "matrix-message-location.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index d058828..0d8ddbb 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -740,6 +740,18 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-call-answer.h")] + public class CallAnswer : Call { + public Matrix.CallAnswerType answer_type { get; set; default = Matrix.CallAnswerType.UNKNOWN; } + public string? answer_sdp { 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")]