Port MatrixMessageAudio to C

This commit is contained in:
Gergely Polonkai 2017-11-20 10:03:14 +01:00
parent c5d1fa60f4
commit 33b36a4e82
7 changed files with 328 additions and 77 deletions

1
.gitignore vendored
View File

@ -69,6 +69,5 @@ Makefile.in
/src/matrix-event-call-hangup.c /src/matrix-event-call-hangup.c
/src/matrix-glib-0.0.pc /src/matrix-glib-0.0.pc
/src/matrix-message-notice.c /src/matrix-message-notice.c
/src/matrix-message-audio.c
/src/matrix-message-video.c /src/matrix-message-video.c
/src/matrix-room.c /src/matrix-room.c

View File

@ -36,7 +36,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-event-call-answer.vala \ matrix-event-call-answer.vala \
matrix-event-call-hangup.vala \ matrix-event-call-hangup.vala \
matrix-message-notice.vala \ matrix-message-notice.vala \
matrix-message-audio.vala \
matrix-message-video.vala \ matrix-message-video.vala \
matrix-room.vala \ matrix-room.vala \
$(NULL) $(NULL)
@ -102,6 +101,7 @@ INST_H_SRC_FILES = \
matrix-message-emote.h \ matrix-message-emote.h \
matrix-message-file.h \ matrix-message-file.h \
matrix-message-image.h \ matrix-message-image.h \
matrix-message-audio.h \
matrix-event-room-base.h \ matrix-event-room-base.h \
matrix-event-state-base.h \ matrix-event-state-base.h \
matrix-event-tag.h \ matrix-event-tag.h \
@ -143,6 +143,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-message-emote.c \ matrix-message-emote.c \
matrix-message-file.c \ matrix-message-file.c \
matrix-message-image.c \ matrix-message-image.c \
matrix-message-audio.c \
matrix-event-tag.c \ matrix-event-tag.c \
matrix-event-presence.c \ matrix-event-presence.c \
matrix-event-room-member.c \ matrix-event-room-member.c \

View File

@ -33,6 +33,7 @@
#include "matrix-message-emote.h" #include "matrix-message-emote.h"
#include "matrix-message-file.h" #include "matrix-message-file.h"
#include "matrix-message-image.h" #include "matrix-message-image.h"
#include "matrix-message-audio.h"
/* /*
Borrowed from GLib Borrowed from GLib

267
src/matrix-message-audio.c Normal file
View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
#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;
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
#ifndef __MATRIX_GLIB_SDK_MESSAGE_AUDIO_H__
# define __MATRIX_GLIB_SDK_MESSAGE_AUDIO_H__
# include <glib-object.h>
# 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__ */

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
/**
* 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);
}
}

View File

@ -754,5 +754,19 @@ namespace Matrix {
to_json(Json.Node json_data) to_json(Json.Node json_data)
throws Matrix.Error; 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;
}
} }
} }