diff --git a/.gitignore b/.gitignore index 11405b6..d38afa6 100644 --- a/.gitignore +++ b/.gitignore @@ -69,7 +69,6 @@ Makefile.in /src/matrix-event-call-hangup.c /src/matrix-glib-0.0.pc /src/matrix-message-notice.c -/src/matrix-message-image.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 41e8453..b860a58 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-image.vala \ matrix-message-audio.vala \ matrix-message-video.vala \ matrix-room.vala \ @@ -102,6 +101,7 @@ INST_H_SRC_FILES = \ matrix-message-location.h \ matrix-message-emote.h \ matrix-message-file.h \ + matrix-message-image.h \ matrix-event-room-base.h \ matrix-event-state-base.h \ matrix-event-tag.h \ @@ -142,6 +142,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-message-location.c \ matrix-message-emote.c \ matrix-message-file.c \ + matrix-message-image.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 913c07a..dd5af69 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -32,6 +32,7 @@ #include "matrix-message-location.h" #include "matrix-message-emote.h" #include "matrix-message-file.h" +#include "matrix-message-image.h" /* Borrowed from GLib diff --git a/src/matrix-message-image.c b/src/matrix-message-image.c new file mode 100644 index 0000000..6962025 --- /dev/null +++ b/src/matrix-message-image.c @@ -0,0 +1,399 @@ +/* + * 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-image.h" +#include "config.h" + +/** + * SECTION:matrix-message-image + * @short_description: message type representing image uploads + * + * This is the default message handler for `m.image` messages. + */ +enum { + PROP_0, + PROP_URL, + PROP_THUMBNAIL_URL, + PROP_INFO, + PROP_THUMBNAIL_INFO, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_message_image_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _url; + gchar* _thumbnail_url; + MatrixImageInfo* _info; + MatrixImageInfo* _thumbnail_info; +} MatrixMessageImagePrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(MatrixMessageImage, matrix_message_image, MATRIX_MESSAGE_TYPE_BASE); + +static void +matrix_message_image_real_from_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error) +{ + MatrixMessageImagePrivate *priv; + JsonObject *root; + JsonNode *node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_message_image_get_instance_private(MATRIX_MESSAGE_IMAGE(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.image message"); + } + + if ((node = json_object_get_member(root, "info")) != NULL) { + matrix_image_info_unref(priv->_info); + priv->_info = matrix_image_info_new(); + matrix_image_info_set_from_json(priv->_info, node); + } + + 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_image_parent_class)->from_json(matrix_message_base, json_data, error); +} + +static void +matrix_message_image_real_to_json(MatrixMessageBase *matrix_message_base, JsonNode *json_data, GError **error) +{ + MatrixMessageImagePrivate *priv; + JsonObject *root; + JsonNode *node; + GError *inner_error = NULL; + + g_return_if_fail(json_data != NULL); + + priv = matrix_message_image_get_instance_private(MATRIX_MESSAGE_IMAGE(matrix_message_base)); + + if (priv->_url == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.image message without url"); + + return; + } + + root = json_node_get_object(json_data); + + json_object_set_string_member(root, "url", priv->_url); + + if (priv->_info != NULL) { + node = matrix_image_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_image_parent_class)->to_json(matrix_message_base, json_data, error); +} + +MatrixMessageImage * +matrix_message_image_new(void) +{ + return (MatrixMessageImage *)matrix_message_base_construct(MATRIX_MESSAGE_TYPE_IMAGE); +} + +const gchar * +matrix_message_image_get_url(MatrixMessageImage *matrix_message_image) +{ + MatrixMessageImagePrivate *priv; + + g_return_val_if_fail(matrix_message_image != NULL, NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + return priv->_url; +} + +void +matrix_message_image_set_url(MatrixMessageImage *matrix_message_image, const gchar *url) +{ + MatrixMessageImagePrivate *priv; + + g_return_if_fail(matrix_message_image != NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + if (g_strcmp0(url, priv->_url) != 0) { + g_free(priv->_url); + priv->_url = g_strdup(url); + + g_object_notify_by_pspec((GObject *)matrix_message_image, matrix_message_image_properties[PROP_URL]); + } +} + +const gchar * +matrix_message_image_get_thumbnail_url(MatrixMessageImage *matrix_message_image) +{ + MatrixMessageImagePrivate *priv; + + g_return_val_if_fail(matrix_message_image != NULL, NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + return priv->_thumbnail_url; +} + + +void +matrix_message_image_set_thumbnail_url(MatrixMessageImage *matrix_message_image, const gchar *thumbnail_url) +{ + MatrixMessageImagePrivate *priv; + + g_return_if_fail(matrix_message_image != NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + 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_image, matrix_message_image_properties[PROP_THUMBNAIL_URL]); + } +} + +MatrixImageInfo * +matrix_message_image_get_info(MatrixMessageImage *matrix_message_image) +{ + MatrixMessageImagePrivate *priv; + + g_return_val_if_fail(matrix_message_image != NULL, NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + return priv->_info; +} + +void +matrix_message_image_set_info(MatrixMessageImage *matrix_message_image, MatrixImageInfo *info) +{ + MatrixMessageImagePrivate *priv; + + g_return_if_fail (matrix_message_image != NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + if (priv->_info != info) { + matrix_image_info_unref(priv->_info); + priv->_info = matrix_image_info_ref(info); + + g_object_notify_by_pspec((GObject *)matrix_message_image, matrix_message_image_properties[PROP_INFO]); + } +} + +MatrixImageInfo * +matrix_message_image_get_thumbnail_info(MatrixMessageImage *matrix_message_image) +{ + MatrixMessageImagePrivate *priv; + + g_return_val_if_fail(matrix_message_image != NULL, NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + return priv->_thumbnail_info; +} + +void +matrix_message_image_set_thumbnail_info(MatrixMessageImage *matrix_message_image, MatrixImageInfo *thumbnail_info) +{ + MatrixMessageImagePrivate *priv; + + g_return_if_fail(matrix_message_image != NULL); + + priv = matrix_message_image_get_instance_private(matrix_message_image); + + if (priv->_thumbnail_info != thumbnail_info) { + matrix_image_info_unref(priv->_thumbnail_info); + priv->_thumbnail_info = matrix_image_info_ref(priv->_thumbnail_info); + + g_object_notify_by_pspec((GObject *)matrix_message_image, matrix_message_image_properties[PROP_THUMBNAIL_INFO]); + } +} + +static void +matrix_message_image_finalize(GObject *gobject) +{ + MatrixMessageImagePrivate *priv = matrix_message_image_get_instance_private(MATRIX_MESSAGE_IMAGE(gobject)); + + g_free(priv->_url); + g_free(priv->_thumbnail_url); + matrix_image_info_unref(priv->_info); + matrix_image_info_unref(priv->_thumbnail_info); + + G_OBJECT_CLASS(matrix_message_image_parent_class)->finalize(gobject); +} + +static void +matrix_message_image_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixMessageImage *matrix_message_image = MATRIX_MESSAGE_IMAGE(gobject); + + switch (property_id) { + case PROP_URL: + g_value_set_string(value, matrix_message_image_get_url(matrix_message_image)); + + break; + case PROP_THUMBNAIL_URL: + g_value_set_string(value, matrix_message_image_get_thumbnail_url(matrix_message_image)); + + break; + case PROP_INFO: + g_value_set_boxed(value, matrix_message_image_get_info(matrix_message_image)); + + break; + case PROP_THUMBNAIL_INFO: + g_value_set_boxed(value, matrix_message_image_get_thumbnail_info(matrix_message_image)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + + +static void +matrix_message_image_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixMessageImage *matrix_message_image = MATRIX_MESSAGE_IMAGE(gobject); + + switch (property_id) { + case PROP_URL: + matrix_message_image_set_url(matrix_message_image, g_value_get_string(value)); + + break; + case PROP_THUMBNAIL_URL: + matrix_message_image_set_thumbnail_url(matrix_message_image, g_value_get_string(value)); + + break; + case PROP_INFO: + matrix_message_image_set_info(matrix_message_image, g_value_get_boxed(value)); + + break; + case PROP_THUMBNAIL_INFO: + matrix_message_image_set_thumbnail_info(matrix_message_image, g_value_get_boxed(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_message_image_class_init(MatrixMessageImageClass *klass) +{ + ((MatrixMessageBaseClass *)klass)->from_json = matrix_message_image_real_from_json; + ((MatrixMessageBaseClass *)klass)->to_json = matrix_message_image_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_message_image_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_message_image_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_message_image_finalize; + + /** + * MatrixMessageImage:url: + * + * The URL to the image. + */ + matrix_message_image_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_image_properties[PROP_URL]); + + /** + * MatrixMessageImage:thumbnail-url: + * + * The URL to the thumbnail of the image. + */ + matrix_message_image_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_image_properties[PROP_THUMBNAIL_URL]); + + /** + * MatrixMessageImage:info: + * + * A #MatrixImageInfo describing #MatrixMessageImage:url. + */ + matrix_message_image_properties[PROP_INFO] = g_param_spec_boxed( + "info", "info", "info", + MATRIX_TYPE_IMAGE_INFO, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_INFO, matrix_message_image_properties[PROP_INFO]); + + /** + * MatrixMessageImage:thumbnail-info: + * + * A #MatrixImageInfo describing MatrixMessageImage:thumbnail-url. + */ + matrix_message_image_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_image_properties[PROP_THUMBNAIL_INFO]); +} + +static void +matrix_message_image_init(MatrixMessageImage *matrix_message_image) +{ + MatrixMessageImagePrivate *priv = matrix_message_image_get_instance_private(matrix_message_image); + + priv->_url = NULL; + priv->_thumbnail_url = NULL; + priv->_info = NULL; + priv->_thumbnail_info = NULL; +} diff --git a/src/matrix-message-image.h b/src/matrix-message-image.h new file mode 100644 index 0000000..c2bbf26 --- /dev/null +++ b/src/matrix-message-image.h @@ -0,0 +1,47 @@ +/* + * 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_IMAGE_H__ +# define __MATRIX_GLIB_SDK_MESSAGE_IMAGE_H__ + +# include +# include "matrix-message-base.h" +# include "matrix-types.h" + +G_BEGIN_DECLS + +# define MATRIX_MESSAGE_TYPE_IMAGE matrix_message_image_get_type() +G_DECLARE_DERIVABLE_TYPE(MatrixMessageImage, matrix_message_image, MATRIX_MESSAGE, IMAGE, MatrixMessageBase) + +struct _MatrixMessageImageClass { + MatrixMessageBaseClass parent_class; +}; + +MatrixMessageImage* matrix_message_image_new (void); +const gchar* matrix_message_image_get_url (MatrixMessageImage* self); +void matrix_message_image_set_url (MatrixMessageImage* self, const gchar* value); +const gchar* matrix_message_image_get_thumbnail_url (MatrixMessageImage* self); +void matrix_message_image_set_thumbnail_url (MatrixMessageImage* self, const gchar* value); +MatrixImageInfo* matrix_message_image_get_info (MatrixMessageImage* self); +void matrix_message_image_set_info (MatrixMessageImage* self, MatrixImageInfo* value); +MatrixImageInfo* matrix_message_image_get_thumbnail_info (MatrixMessageImage* self); +void matrix_message_image_set_thumbnail_info (MatrixMessageImage* self, MatrixImageInfo* value); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_MESSAGE_IMAGE_H__ */ diff --git a/src/matrix-message-image.vala b/src/matrix-message-image.vala deleted file mode 100644 index 4f081d3..0000000 --- a/src/matrix-message-image.vala +++ /dev/null @@ -1,102 +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.image message - * - * This message represents a single image and an optional thumbnail. - */ -public class Matrix.Message.Image : Matrix.Message.Base { - /** - * The URL to the image. - */ - public string? url { get; set; default = null; } - - /** - * The URL to the thumbnail of the image. - */ - public string? thumbnail_url { get; set; default = null; } - - /** - * Metadata about the image referred to in `url`. - */ - public ImageInfo? info { 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.image message"); - } - - if ((node = root.get_member("info")) != null) { - _info = ImageInfo(); - _info.set_from_json(node); - } - - 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.image 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()); - } - - 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); - } -} diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index e6fa513..e8c22ae 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -738,5 +738,21 @@ namespace Matrix { to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-message-image.h")] + public class Image : Base { + public string? url { get; set; default = null; } + public string? thumbnail_url { get; set; default = null; } + public Matrix.ImageInfo? info { 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; + } } }