Port MatrixMessageFile to C

This commit is contained in:
Gergely Polonkai 2017-11-20 09:25:48 +01:00
parent 3c825c663f
commit a9cddf4303
7 changed files with 535 additions and 115 deletions

1
.gitignore vendored
View File

@ -69,7 +69,6 @@ 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-file.c
/src/matrix-message-image.c /src/matrix-message-image.c
/src/matrix-message-audio.c /src/matrix-message-audio.c
/src/matrix-message-video.c /src/matrix-message-video.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-file.vala \
matrix-message-image.vala \ matrix-message-image.vala \
matrix-message-audio.vala \ matrix-message-audio.vala \
matrix-message-video.vala \ matrix-message-video.vala \
@ -102,6 +101,7 @@ INST_H_SRC_FILES = \
matrix-message-text.h \ matrix-message-text.h \
matrix-message-location.h \ matrix-message-location.h \
matrix-message-emote.h \ matrix-message-emote.h \
matrix-message-file.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 \
@ -141,6 +141,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-message-text.c \ matrix-message-text.c \
matrix-message-location.c \ matrix-message-location.c \
matrix-message-emote.c \ matrix-message-emote.c \
matrix-message-file.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

@ -31,6 +31,7 @@
#include "matrix-message-text.h" #include "matrix-message-text.h"
#include "matrix-message-location.h" #include "matrix-message-location.h"
#include "matrix-message-emote.h" #include "matrix-message-emote.h"
#include "matrix-message-file.h"
/* /*
Borrowed from GLib Borrowed from GLib

465
src/matrix-message-file.c Normal file
View File

@ -0,0 +1,465 @@
/*
* 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-file.h"
#include "config.h"
/**
* SECTION:matrix-message-file
* @short_description: message type for uploaded files
*
* This is the default message handler for `m.file` messages.
*/
enum {
PROP_0,
PROP_FILENAME,
PROP_URL,
PROP_THUMBNAIL_URL,
PROP_INFO,
PROP_THUMBNAIL_INFO,
NUM_PROPERTIES
};
static GParamSpec *matrix_message_file_properties[NUM_PROPERTIES];
typedef struct {
gchar* _filename;
gchar* _url;
MatrixFileInfo* _info;
gchar* _thumbnail_url;
MatrixImageInfo* _thumbnail_info;
} MatrixMessageFilePrivate;
G_DEFINE_TYPE_WITH_PRIVATE(MatrixMessageFile, matrix_message_file, MATRIX_MESSAGE_TYPE_BASE);
static void
matrix_message_file_real_from_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error)
{
MatrixMessageFilePrivate *priv;
JsonObject *root;
JsonNode *node;
priv = matrix_message_file_get_instance_private(MATRIX_MESSAGE_FILE(matrix_message_base));
g_return_if_fail(json_data != NULL);
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.file message");
}
if ((node = json_object_get_member(root, "info")) != NULL) {
matrix_file_info_unref(priv->_info);
priv->_info = matrix_file_info_new();
matrix_file_info_set_from_json(priv->_info, node);
} else if (DEBUG) {
g_warning("info is missing from a m.file message");
}
if ((node = json_object_get_member(root, "thumbnail_url")) != NULL) {
g_free(priv->_thumbnail_url);
priv->_thumbnail_url = g_strdup(json_node_get_string(node));
}
if ((node = json_object_get_member(root, "thumbnail_info")) != NULL) {
matrix_image_info_unref(priv->_thumbnail_info);
priv->_thumbnail_info = matrix_image_info_new();
matrix_image_info_set_from_json(priv->_thumbnail_info, node);
}
MATRIX_MESSAGE_BASE_CLASS(matrix_message_file_parent_class)->from_json(matrix_message_base, json_data, error);
}
static void
matrix_message_file_real_to_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error)
{
MatrixMessageFilePrivate *priv;
JsonObject *root;
JsonNode *node;
GError *inner_error = NULL;
g_return_if_fail(json_data != NULL);
priv = matrix_message_file_get_instance_private(MATRIX_MESSAGE_FILE(matrix_message_base));
if (priv->_url == NULL) {
g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
"Won't generate a m.file message without the URL");
return;
}
if (priv->_info == NULL) {
g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
"Won't generate a m.file message without the file info.");
return;
}
root = json_node_get_object(json_data);
json_object_set_string_member(root, "url", priv->_url);
node = matrix_file_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);
if (priv->_thumbnail_url != NULL) {
json_object_set_string_member(root, "thumbnail_url", priv->_thumbnail_url);
}
if (priv->_thumbnail_info != NULL) {
node = matrix_image_info_get_json_node(priv->_thumbnail_info, &inner_error);
if (inner_error != NULL) {
g_propagate_error(error, inner_error);
return;
}
json_object_set_member(root, "thumbnail_info", node);
}
MATRIX_MESSAGE_BASE_CLASS(matrix_message_file_parent_class)->to_json(matrix_message_base, json_data, error);
}
MatrixMessageFile *
matrix_message_file_construct(GType object_type)
{
return (MatrixMessageFile *)matrix_message_base_construct(object_type);
}
MatrixMessageFile *
matrix_message_file_new(void)
{
return matrix_message_file_construct(MATRIX_MESSAGE_TYPE_FILE);
}
const gchar *
matrix_message_file_get_filename(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv;
g_return_val_if_fail(matrix_message_file != NULL, NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
return priv->_filename;
}
void
matrix_message_file_set_filename(MatrixMessageFile *matrix_message_file, const gchar *filename)
{
MatrixMessageFilePrivate *priv;
g_return_if_fail(matrix_message_file != NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
if (g_strcmp0(filename, priv->_filename) != 0) {
g_free(priv->_filename);
priv->_filename = g_strdup(filename);
g_object_notify_by_pspec((GObject *)matrix_message_file, matrix_message_file_properties[PROP_FILENAME]);
}
}
const gchar *
matrix_message_file_get_url(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv;
g_return_val_if_fail(matrix_message_file != NULL, NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
return priv->_url;
}
void
matrix_message_file_set_url(MatrixMessageFile *matrix_message_file, const gchar *url)
{
MatrixMessageFilePrivate *priv;
g_return_if_fail(matrix_message_file != NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
if (g_strcmp0(url, priv->_url) != 0) {
g_free(priv->_url);
priv->_url = g_strdup(url);
g_object_notify_by_pspec((GObject *)matrix_message_file, matrix_message_file_properties[PROP_URL]);
}
}
MatrixFileInfo *
matrix_message_file_get_info(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv;
g_return_val_if_fail(matrix_message_file != NULL, NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
return priv->_info;
}
void
matrix_message_file_set_info(MatrixMessageFile *matrix_message_file, MatrixFileInfo *info)
{
MatrixMessageFilePrivate *priv;
g_return_if_fail(matrix_message_file != NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
if (priv->_info != info) {
matrix_file_info_unref(priv->_info);
priv->_info = matrix_file_info_ref(info);
g_object_notify_by_pspec((GObject *)matrix_message_file, matrix_message_file_properties[PROP_INFO]);
}
}
const gchar *
matrix_message_file_get_thumbnail_url(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv;
g_return_val_if_fail(matrix_message_file != NULL, NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
return priv->_thumbnail_url;
}
void
matrix_message_file_set_thumbnail_url(MatrixMessageFile *matrix_message_file, const gchar *thumbnail_url)
{
MatrixMessageFilePrivate *priv;
g_return_if_fail(matrix_message_file != NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
if (g_strcmp0(thumbnail_url, priv->_thumbnail_url) != 0) {
g_free(priv->_thumbnail_url);
priv->_thumbnail_url = g_strdup(thumbnail_url);
g_object_notify_by_pspec((GObject *)matrix_message_file, matrix_message_file_properties[PROP_THUMBNAIL_URL]);
}
}
MatrixImageInfo *
matrix_message_file_get_thumbnail_info(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv;
g_return_val_if_fail(matrix_message_file != NULL, NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
return priv->_thumbnail_info;
}
void
matrix_message_file_set_thumbnail_info(MatrixMessageFile *matrix_message_file, MatrixImageInfo *thumbnail_info)
{
MatrixMessageFilePrivate *priv;
g_return_if_fail(matrix_message_file != NULL);
priv = matrix_message_file_get_instance_private(matrix_message_file);
if (priv->_thumbnail_info != thumbnail_info) {
matrix_image_info_unref(priv->_thumbnail_info);
priv->_thumbnail_info = matrix_image_info_ref(thumbnail_info);
g_object_notify_by_pspec((GObject *)matrix_message_file, matrix_message_file_properties[PROP_THUMBNAIL_INFO]);
}
}
static void
matrix_message_file_finalize(GObject *gobject)
{
MatrixMessageFilePrivate *priv = matrix_message_file_get_instance_private(MATRIX_MESSAGE_FILE(gobject));
g_free(priv->_filename);
g_free(priv->_url);
g_free(priv->_info);
g_free(priv->_thumbnail_url);
matrix_file_info_unref(priv->_info);
matrix_image_info_unref(priv->_thumbnail_info);
G_OBJECT_CLASS(matrix_message_file_parent_class)->finalize(gobject);
}
static void
matrix_message_file_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec)
{
MatrixMessageFile *matrix_message_file = MATRIX_MESSAGE_FILE(gobject);
switch (property_id) {
case PROP_FILENAME:
g_value_set_string(value, matrix_message_file_get_filename(matrix_message_file));
break;
case PROP_URL:
g_value_set_string(value, matrix_message_file_get_url(matrix_message_file));
break;
case PROP_THUMBNAIL_URL:
g_value_set_string(value, matrix_message_file_get_thumbnail_url(matrix_message_file));
break;
case PROP_INFO:
g_value_set_boxed(value, matrix_message_file_get_info(matrix_message_file));
break;
case PROP_THUMBNAIL_INFO:
g_value_set_boxed(value, matrix_message_file_get_thumbnail_info(matrix_message_file));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_message_file_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec)
{
MatrixMessageFile *matrix_message_file = MATRIX_MESSAGE_FILE(gobject);
switch (property_id) {
case PROP_FILENAME:
matrix_message_file_set_filename(matrix_message_file, g_value_get_string(value));
break;
case PROP_URL:
matrix_message_file_set_url(matrix_message_file, g_value_get_string(value));
break;
case PROP_THUMBNAIL_URL:
matrix_message_file_set_thumbnail_url(matrix_message_file, g_value_get_string(value));
break;
case PROP_INFO:
matrix_message_file_set_info(matrix_message_file, g_value_get_boxed(value));
break;
case PROP_THUMBNAIL_INFO:
matrix_message_file_set_thumbnail_info(matrix_message_file, g_value_get_boxed(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_message_file_class_init(MatrixMessageFileClass *klass)
{
((MatrixMessageBaseClass *)klass)->from_json = matrix_message_file_real_from_json;
((MatrixMessageBaseClass *)klass)->to_json = matrix_message_file_real_to_json;
G_OBJECT_CLASS(klass)->get_property = matrix_message_file_get_property;
G_OBJECT_CLASS(klass)->set_property = matrix_message_file_set_property;
G_OBJECT_CLASS(klass)->finalize = matrix_message_file_finalize;
/**
* MatrixMessageFile:filename:
*
* The original filename of the uploaded file.
*/
matrix_message_file_properties[PROP_FILENAME] = g_param_spec_string(
"filename", "filename", "filename",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_FILENAME, matrix_message_file_properties[PROP_FILENAME]);
/**
* MatrixMessageFile:url:
*
* The URL of the file.
*/
matrix_message_file_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_file_properties[PROP_URL]);
/**
* MatrixMessageFile:thumbnail-url:
*
* The URL of the thumbnail of the file.
*/
matrix_message_file_properties[PROP_THUMBNAIL_URL] = g_param_spec_string(
"thumbnail-url", "thumbnail-url", "thumbnail-url",
NULL,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_THUMBNAIL_URL, matrix_message_file_properties[PROP_THUMBNAIL_URL]);
/**
* MatrixMessageFile:info:
*
* A #MatrixFileInfo object describing #MatrixMessageFile:filename.
*/
matrix_message_file_properties[PROP_INFO] = g_param_spec_boxed(
"info", "info", "info",
MATRIX_TYPE_FILE_INFO,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_INFO, matrix_message_file_properties[PROP_INFO]);
/**
* MatrixMessageFile:thumbnail-info:
*
* A #MatrixImageInfo object describing #MatrixMessageFile:thumbnail-url.
*/
matrix_message_file_properties[PROP_THUMBNAIL_INFO] = g_param_spec_boxed(
"thumbnail-info", "thumbnail_info", "thumbnail-info",
MATRIX_TYPE_IMAGE_INFO,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_THUMBNAIL_INFO, matrix_message_file_properties[PROP_THUMBNAIL_INFO]);
}
static void
matrix_message_file_init(MatrixMessageFile *matrix_message_file)
{
MatrixMessageFilePrivate *priv = matrix_message_file_get_instance_private(matrix_message_file);
priv->_filename = NULL;
priv->_url = NULL;
priv->_info = NULL;
priv->_thumbnail_url = NULL;
priv->_thumbnail_info = NULL;
}

50
src/matrix-message-file.h Normal file
View File

@ -0,0 +1,50 @@
/*
* 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_FILE_H__
# define __MATRIX_GLIB_SDK_MESSAGE_FILE_H__
# include <glib-object.h>
# include "matrix-message-base.h"
# include "matrix-types.h"
G_BEGIN_DECLS
# define MATRIX_MESSAGE_TYPE_FILE matrix_message_file_get_type()
G_DECLARE_DERIVABLE_TYPE(MatrixMessageFile, matrix_message_file, MATRIX_MESSAGE, FILE, MatrixMessageBase);
struct _MatrixMessageFileClass {
MatrixMessageBaseClass parent_class;
};
MatrixMessageFile* matrix_message_file_new (void);
MatrixMessageFile* matrix_message_file_construct (GType object_type);
const gchar* matrix_message_file_get_filename (MatrixMessageFile* self);
void matrix_message_file_set_filename (MatrixMessageFile* self, const gchar* value);
const gchar* matrix_message_file_get_url (MatrixMessageFile* self);
void matrix_message_file_set_url (MatrixMessageFile* self, const gchar* value);
MatrixFileInfo* matrix_message_file_get_info (MatrixMessageFile* self);
void matrix_message_file_set_info (MatrixMessageFile* self, MatrixFileInfo* value);
const gchar* matrix_message_file_get_thumbnail_url (MatrixMessageFile* self);
void matrix_message_file_set_thumbnail_url (MatrixMessageFile* self, const gchar* value);
MatrixImageInfo* matrix_message_file_get_thumbnail_info (MatrixMessageFile* self);
void matrix_message_file_set_thumbnail_info (MatrixMessageFile* self, MatrixImageInfo* value);
G_END_DECLS
#endif /* __MATRIX_GLIB_SDK_MESSAGE_FILE_H__ */

View File

@ -1,113 +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.file message
*
* This message represents a generic file.
*/
public class Matrix.Message.File : Matrix.Message.Base {
/**
* The original filename of the uploaded file.
*/
public string? filename { get; set; default = null; }
/**
* The URL of the file.
*/
public string? url { get; set; default = null; }
/**
* Information about the file referred to in ``url``.
*/
public FileInfo? info { get; set; default = null; }
/**
* The URL of the thumbnail of the file.
*/
public string? thumbnail_url {get; set; default = null; }
/**
* Metadata about the image referred to in `thumbnail_url`.
*/
public ImageInfo? thumbnail_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.file message");
}
if ((node = root.get_member("info")) != null) {
_info = FileInfo();
_info.set_from_json(node);
} else if (Config.DEBUG) {
warning("info is missing from a m.file message");
}
if ((node = root.get_member("thumbnail_url")) != null) {
_thumbnail_url = node.get_string();
}
if ((node = root.get_member("thumbnail_info")) != null) {
_thumbnail_info = ImageInfo();
_thumbnail_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.file message without the URL");
}
if (_info == null) {
throw new Matrix.Error.INCOMPLETE(
"Won't generate a m.file message without the file info.");
}
var root = json_data.get_object();
root.set_string_member("url", _url);
root.set_member("info", _info.get_json_node());
if (_thumbnail_url != null) {
root.set_string_member("thumbnail_url", _thumbnail_url);
}
if (_thumbnail_info != null) {
root.set_member("thumbnail_info", _thumbnail_info.get_json_node());
}
base.to_json(json_data);
}
}

View File

@ -721,5 +721,22 @@ namespace Matrix {
public override void to_json(Json.Node json_data) public override void to_json(Json.Node json_data)
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-message-file.h")]
public class File : Base {
public string? filename { get; set; default = null; }
public string? url { get; set; default = null; }
public Matrix.FileInfo? info { get; set; default = null; }
public string? thumbnail_url {get; set; default = null; }
public Matrix.ImageInfo? thumbnail_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;
}
} }
} }