diff --git a/.gitignore b/.gitignore index 16589d0..451b333 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,5 @@ Makefile.in /src/matrix-event-room-guest-access.c /src/matrix-event-room-redaction.c /src/matrix-event-room-third-party-invite.c -/src/matrix-event-call-invite.c /src/matrix-event-call-candidates.c /src/matrix-glib-0.0.pc diff --git a/src/Makefile.am b/src/Makefile.am index 2c90cfd..6eb40c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,7 +28,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-guest-access.vala \ matrix-event-room-redaction.vala \ matrix-event-room-third-party-invite.vala \ - matrix-event-call-invite.vala \ matrix-event-call-candidates.vala \ $(NULL) @@ -89,6 +88,7 @@ INST_H_SRC_FILES = \ matrix-event-call-base.h \ matrix-event-call-answer.h \ matrix-event-call-hangup.h \ + matrix-event-call-invite.h \ matrix-message-base.h \ matrix-message-text.h \ matrix-message-location.h \ @@ -139,6 +139,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-call-base.c \ matrix-event-call-answer.c \ matrix-event-call-hangup.c \ + matrix-event-call-invite.c \ matrix-message-base.c \ matrix-message-text.c \ matrix-message-location.c \ diff --git a/src/matrix-event-call-invite.c b/src/matrix-event-call-invite.c new file mode 100644 index 0000000..4f82dea --- /dev/null +++ b/src/matrix-event-call-invite.c @@ -0,0 +1,420 @@ +/* + * 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-invite.h" +#include "utils.h" +#include "config.h" +#include "matrix-enumtypes.h" + +/** + * SECTION:matrix-event-call-invite + * @short_description: event to signal a call request + * + * This event is sent by the caller when they wish to establish a call. + */ +enum { + PROP_0, + PROP_OFFER_TYPE, + PROP_SDP, + PROP_LIFETIME, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_call_invite_properties[NUM_PROPERTIES]; + +typedef struct { + MatrixCallOfferType _offer_type; + gchar* _sdp; + gint _lifetime; +} MatrixEventCallInvitePrivate; + +/** + * MatrixEventCallInvite: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventCallInvite, matrix_event_call_invite, MATRIX_EVENT_TYPE_CALL); + +static void +matrix_event_call_invite_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventCallInvitePrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail (json_data != NULL); + + priv = matrix_event_call_invite_get_instance_private(MATRIX_EVENT_CALL_INVITE(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, "offer")) != NULL) { + JsonObject *offer_root = json_node_get_object(node); + + if ((node = json_object_get_member(offer_root, "type")) != NULL) { + GError *inner_error = NULL; + MatrixCallOfferType offer_type = _matrix_g_enum_nick_to_value(MATRIX_TYPE_CALL_OFFER_TYPE, json_node_get_string(node), &inner_error); + + if (inner_error != NULL) { + priv->_offer_type = MATRIX_CALL_OFFER_TYPE_UNKNOWN; + +#ifdef DEBUG + g_warning("Unknown value %s in content.offer.type of a m.call.invite event", json_node_get_string(node)); +#endif + } else { + priv->_offer_type = offer_type; + } + } else { + g_warning("content.offer.type is missing from a m.call.invite event"); + } + + if ((node = json_object_get_member(offer_root, "sdp")) != NULL) { + g_free(priv->_sdp); + priv->_sdp = g_strdup(json_node_get_string(node)); + } else { + g_warning("content.offer.sdp is missing from a m.call.invite event"); + } + } + + if ((node = json_object_get_member(content_root, "lifetime")) != NULL) { + priv->_lifetime = json_node_get_int(node); + } else { + g_warning("content.lifetime is missing from a m.call.invite event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_call_invite_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_call_invite_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventCallInvitePrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonObject *offer_root; + JsonNode *content_node; + JsonNode *offer_node; + gchar *offer_type; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_call_invite_get_instance_private(MATRIX_EVENT_CALL_INVITE(matrix_event_base)); + + if (priv->_offer_type == MATRIX_CALL_OFFER_TYPE_UNKNOWN) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.call.invite without a valid offer.type"); + + return; + } + + if (priv->_sdp == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.call.invite without offer.sdp"); + + return; + } + + if (priv->_lifetime < 0) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.call.invite without lifetime"); + + return; + } + + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + + json_object_set_int_member(content_root, "lifetime", priv->_lifetime); + + offer_root = json_object_new(); + offer_node = json_node_new(JSON_NODE_OBJECT); + json_node_set_object(offer_node, offer_root); + + offer_type = _matrix_g_enum_to_string(MATRIX_TYPE_CALL_OFFER_TYPE, priv->_offer_type, '_'); + json_object_set_string_member(offer_root, "type", offer_type); + g_free(offer_type); + + json_object_set_string_member(offer_root, "sdp", priv->_sdp); + + json_object_set_member(content_root, "offer", offer_node); + + MATRIX_EVENT_BASE_CLASS(matrix_event_call_invite_parent_class)->to_json(matrix_event_base, json_data, error); +} + +/** + * matrix_event_call_invite_new: + * + * Create a new #MatrixEventCallInvite object. + * + * Returns: (transfer full): a new #MatrixEventCallInvite object + */ +MatrixEventCallInvite * +matrix_event_call_invite_new(void) { + return (MatrixEventCallInvite *)matrix_event_call_construct(MATRIX_EVENT_TYPE_CALL_INVITE); +} + +/** + * matrix_event_call_invite_get_offer_type: + * @event: a #MatrixEventCallInvite + * + * Get the offer type of @event. + * + * Returns: the call offer type + */ +MatrixCallOfferType +matrix_event_call_invite_get_offer_type(MatrixEventCallInvite *matrix_event_call_invite) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_val_if_fail(matrix_event_call_invite != NULL, 0); + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + return priv->_offer_type; +} + +/** + * matrix_event_call_invite_set_offer_type: + * @event: a #MatrixEventCallInvite + * @offer_type: the type of the call offer + * + * Set the offer type of the call in @event. + */ +void +matrix_event_call_invite_set_offer_type(MatrixEventCallInvite *matrix_event_call_invite, MatrixCallOfferType offer_type) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_if_fail(matrix_event_call_invite != NULL); + + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + if (priv->_offer_type != offer_type) { + priv->_offer_type = offer_type; + + g_object_notify_by_pspec((GObject *)matrix_event_call_invite, matrix_event_call_invite_properties[PROP_OFFER_TYPE]); + } +} + +/** + * matrix_event_call_invite_get_sdp: + * @event: a #MatrixEventCallInvite + * + * Get the SDP line of the call. + * + * The returned value is owned by @event and should not by freed. + *. + * Returns: (transfer none): the SDP line of the call + */ +const gchar * +matrix_event_call_invite_get_sdp(MatrixEventCallInvite *matrix_event_call_invite) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_val_if_fail(matrix_event_call_invite != NULL, NULL); + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + return priv->_sdp; +} + +/** + * matrix_event_call_invite_set_sdp: + * @event: a #MatrixEventCallInvite + * @sdp: an SDP line + * + * Set the SDP line ID of the call. + */ +void +matrix_event_call_invite_set_sdp(MatrixEventCallInvite *matrix_event_call_invite, const gchar *sdp) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_if_fail(matrix_event_call_invite != NULL); + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + if (g_strcmp0(sdp, priv->_sdp) != 0) { + g_free(priv->_sdp); + priv->_sdp = g_strdup(sdp); + + g_object_notify_by_pspec((GObject *)matrix_event_call_invite, matrix_event_call_invite_properties[PROP_SDP]); + } +} + +/** + * matrix_event_call_invite_get_lifetime: + * @event: a #MatrixEventCallInvite + * + * Get the lifetime of the call in @event. + * + * Returns: the lifetime, in milliseconds. + */ +gint +matrix_event_call_invite_get_lifetime(MatrixEventCallInvite *matrix_event_call_invite) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_val_if_fail(matrix_event_call_invite != NULL, 0); + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + return priv->_lifetime; +} + +/** + * matrix_event_call_invite_set_lifetime: + * @event: a #MatrixEventCallInvite + * @lifetime: the lifetime of @event + * + * Set the lifetime of @event, in milliseconds. + */ +void +matrix_event_call_invite_set_lifetime(MatrixEventCallInvite *matrix_event_call_invite, gint lifetime) +{ + MatrixEventCallInvitePrivate *priv; + + g_return_if_fail(matrix_event_call_invite != NULL); + + priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + if (priv->_lifetime != lifetime) { + priv->_lifetime = lifetime; + + g_object_notify_by_pspec((GObject *)matrix_event_call_invite, matrix_event_call_invite_properties[PROP_LIFETIME]); + } +} + +static void +matrix_event_call_invite_finalize(GObject *gobject) +{ + MatrixEventCallInvitePrivate *priv = matrix_event_call_invite_get_instance_private(MATRIX_EVENT_CALL_INVITE(gobject)); + + g_free(priv->_sdp); + + G_OBJECT_CLASS(matrix_event_call_invite_parent_class)->finalize(gobject); +} + +static void +matrix_event_call_invite_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventCallInvite *matrix_event_call_invite = MATRIX_EVENT_CALL_INVITE(gobject); + + switch (property_id) { + case PROP_OFFER_TYPE: + g_value_set_enum(value, matrix_event_call_invite_get_offer_type(matrix_event_call_invite)); + + break; + case PROP_SDP: + g_value_set_string(value, matrix_event_call_invite_get_sdp(matrix_event_call_invite)); + + break; + case PROP_LIFETIME: + g_value_set_int(value, matrix_event_call_invite_get_lifetime(matrix_event_call_invite)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_call_invite_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventCallInvite *matrix_event_call_invite = MATRIX_EVENT_CALL_INVITE(gobject); + + switch (property_id) { + case PROP_OFFER_TYPE: + matrix_event_call_invite_set_offer_type(matrix_event_call_invite, g_value_get_enum(value)); + + break; + case PROP_SDP: + matrix_event_call_invite_set_sdp(matrix_event_call_invite, g_value_get_string(value)); + + break; + case PROP_LIFETIME: + matrix_event_call_invite_set_lifetime(matrix_event_call_invite, g_value_get_int(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_call_invite_class_init(MatrixEventCallInviteClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_call_invite_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_call_invite_real_to_json; + G_OBJECT_CLASS (klass)->get_property = matrix_event_call_invite_get_property; + G_OBJECT_CLASS (klass)->set_property = matrix_event_call_invite_set_property; + G_OBJECT_CLASS (klass)->finalize = matrix_event_call_invite_finalize; + + /** + * MatrixEventCallInvite:offer-type: + * + * The type of session description. + */ + matrix_event_call_invite_properties[PROP_OFFER_TYPE] = g_param_spec_enum( + "offer-type", "offer-type", "offer-type", + MATRIX_TYPE_CALL_OFFER_TYPE, MATRIX_CALL_OFFER_TYPE_UNKNOWN, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_OFFER_TYPE, matrix_event_call_invite_properties[PROP_OFFER_TYPE]); + + /** + * MatrixEventCallInvite:sdp: + * + * The SDP text of the session description. + */ + matrix_event_call_invite_properties[PROP_SDP] = g_param_spec_string( + "sdp", "sdp", "sdp", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_SDP, matrix_event_call_invite_properties[PROP_SDP]); + + /** + * MatrixEventCallInvite:lifetime: + * + * The time in milliseconds that the invite is valid for. Once the invite age exceeds this + * value, clients should discard it. They should also no longer show the call as awaiting + * an answer in the UI. + */ + matrix_event_call_invite_properties[PROP_LIFETIME] = g_param_spec_int( + "lifetime", "lifetime", "lifetime", + G_MININT, G_MAXINT, -1, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_LIFETIME, matrix_event_call_invite_properties[PROP_LIFETIME]); +} + +static void +matrix_event_call_invite_init(MatrixEventCallInvite *matrix_event_call_invite) +{ + MatrixEventCallInvitePrivate *priv = matrix_event_call_invite_get_instance_private(matrix_event_call_invite); + + priv->_offer_type = MATRIX_CALL_OFFER_TYPE_UNKNOWN; + priv->_sdp = NULL; + priv->_lifetime = -1; +} diff --git a/src/matrix-event-call-invite.h b/src/matrix-event-call-invite.h new file mode 100644 index 0000000..c4694a2 --- /dev/null +++ b/src/matrix-event-call-invite.h @@ -0,0 +1,45 @@ +/* + * 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_INVITE_H__ +# define __MATRIX_GLIB_SDK_EVENT_CALL_INVITE_H__ + +# include +# include "matrix-event-call-base.h" +# include "matrix-types.h" + +G_BEGIN_DECLS + +#define MATRIX_EVENT_TYPE_CALL_INVITE (matrix_event_call_invite_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventCallInvite, matrix_event_call_invite, MATRIX_EVENT, CALL_INVITE, MatrixEventCall) + +struct _MatrixEventCallInviteClass { + MatrixEventCallClass parent_class; +}; + +MatrixEventCallInvite* matrix_event_call_invite_new (void); +MatrixCallOfferType matrix_event_call_invite_get_offer_type (MatrixEventCallInvite *event); +void matrix_event_call_invite_set_offer_type (MatrixEventCallInvite *event, MatrixCallOfferType offer_type); +const gchar* matrix_event_call_invite_get_sdp (MatrixEventCallInvite *event); +void matrix_event_call_invite_set_sdp (MatrixEventCallInvite *event, const gchar* sdp); +gint matrix_event_call_invite_get_lifetime (MatrixEventCallInvite *event); +void matrix_event_call_invite_set_lifetime (MatrixEventCallInvite *event, gint lifetime); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_CALL_INVITE_H__ */ diff --git a/src/matrix-event-call-invite.vala b/src/matrix-event-call-invite.vala deleted file mode 100644 index fe16b3a..0000000 --- a/src/matrix-event-call-invite.vala +++ /dev/null @@ -1,122 +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 caller when they wish to establish a - * call. - */ -public class Matrix.Event.CallInvite : Matrix.Event.Call { - /** - * The type of session description. - */ - public CallOfferType offer_type { get; set; default = CallOfferType.UNKNOWN; } - /** - * The SDP text of the session description. - */ - public string? sdp { get; set; default = null; } - - /** - * The time in milliseconds that the invite is valid for. Once the - * invite age exceeds this value, clients should discard it. They - * should also no longer show the call as awaiting an answer in - * the UI. - */ - public int lifetime { get; set; default = -1; } - - 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("offer")) != null) { - var offer_node = node.get_object(); - - if ((node = offer_node.get_member("type")) != null) { - try { - _offer_type = (CallOfferType)_g_enum_nick_to_value( - typeof(CallOfferType), node.get_string()); - } catch (Matrix.Error e) { - _offer_type = CallOfferType.UNKNOWN; - - if (Config.DEBUG) { - warning("Unknown value %s in content.offer.type of a m.call.invite event", - node.get_string()); - } - } - } else { - warning("content.offer.type is missing from a m.call.invite event"); - } - - if ((node = offer_node.get_member("sdp")) != null) { - _sdp = node.get_string(); - } else { - warning("content.offer.sdp is missing from a m.call.invite event"); - } - } - - if ((node = content_root.get_member("lifetime")) != null) { - _lifetime = (int)node.get_int(); - } else { - warning("content.lifetime is missing from a m.call.invite event"); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - if (_offer_type == CallOfferType.UNKNOWN) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.call.invite without a valid offer.type"); - } - - if (_sdp == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.call.invite without offer.sdp"); - } - - if (_lifetime < 0) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.call.invite without lifetime"); - } - - var content_root = json_data.get_object() - .get_member("content").get_object(); - - content_root.set_int_member("lifetime", _lifetime); - - var offer_root = new Json.Object(); - var offer_node = new Json.Node(Json.NodeType.OBJECT); - offer_node.set_object(offer_root); - - offer_root.set_string_member( - "type", - _g_enum_value_to_nick(typeof(CallOfferType), - _offer_type)); - offer_root.set_string_member("sdp", _sdp); - - content_root.set_member("offer", offer_node); - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 17ed87c..841d38d 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -32,6 +32,7 @@ #include "matrix-event-room-join-rules.h" #include "matrix-event-call-answer.h" #include "matrix-event-call-hangup.h" +#include "matrix-event-call-invite.h" #include "matrix-message-text.h" #include "matrix-message-location.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 095f5f8..36ea055 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -761,6 +761,19 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-call-invite.h")] + public class CallInvite : Call { + public Matrix.CallOfferType offer_type { get; set; default = Matrix.CallOfferType.UNKNOWN; } + public string? sdp { get; set; default = null; } + public int lifetime { get; set; default = -1; } + + 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")]