Port MatrixEventTag to C
This commit is contained in:
parent
cd938caa2a
commit
734dc647c9
1
.gitignore
vendored
1
.gitignore
vendored
@ -63,7 +63,6 @@ Makefile.in
|
||||
/src/matrix-event-room-history-visibility.c
|
||||
/src/matrix-event-room-join-rules.c
|
||||
/src/matrix-event-room-name.c
|
||||
/src/matrix-event-tag.c
|
||||
/src/matrix-event-room-canonical-alias.c
|
||||
/src/matrix-event-room-create.c
|
||||
/src/matrix-event-room-power-levels.c
|
||||
|
@ -31,7 +31,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
|
||||
matrix-event-room-history-visibility.vala \
|
||||
matrix-event-room-join-rules.vala \
|
||||
matrix-event-room-name.vala \
|
||||
matrix-event-tag.vala \
|
||||
matrix-event-room-canonical-alias.vala \
|
||||
matrix-event-room-create.vala \
|
||||
matrix-event-room-power-levels.vala \
|
||||
@ -113,6 +112,7 @@ INST_H_SRC_FILES = \
|
||||
matrix-event-base.h \
|
||||
matrix-event-room-base.h \
|
||||
matrix-event-state-base.h \
|
||||
matrix-event-tag.h \
|
||||
utils.h \
|
||||
matrix-profile.h \
|
||||
$(NULL)
|
||||
@ -136,6 +136,7 @@ libmatrix_glib_0_0_la_SOURCES = \
|
||||
matrix-types.c \
|
||||
matrix-compacts.c \
|
||||
matrix-event-base.c \
|
||||
matrix-event-tag.c \
|
||||
matrix-event-room-base.c \
|
||||
matrix-event-state-base.c \
|
||||
matrix-profile.c \
|
||||
|
259
src/matrix-event-tag.c
Normal file
259
src/matrix-event-tag.c
Normal file
@ -0,0 +1,259 @@
|
||||
/*
|
||||
* 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-event-tag.h"
|
||||
|
||||
/**
|
||||
* SECTION:matrix-event-tag
|
||||
* @short_description: Event handling class for `m.room.tag`
|
||||
*
|
||||
* This is the default event handler for `m.room.tag` events.
|
||||
*/
|
||||
typedef struct {
|
||||
GHashTable *tags;
|
||||
} MatrixEventTagPrivate;
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_TAGS,
|
||||
NUM_PROPS
|
||||
};
|
||||
static GParamSpec *matrix_event_tag_properties[NUM_PROPS];
|
||||
|
||||
/**
|
||||
* MatrixEventTag:
|
||||
*
|
||||
* Object structure
|
||||
*/
|
||||
G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventTag, matrix_event_tag, MATRIX_EVENT_TYPE_BASE);
|
||||
|
||||
static void
|
||||
matrix_event_tag_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
|
||||
{
|
||||
MatrixEventTagPrivate *priv = matrix_event_tag_get_instance_private(MATRIX_EVENT_TAG(matrix_event_base));
|
||||
JsonObject *root;
|
||||
JsonNode *content_node;
|
||||
JsonObject *content_root;
|
||||
JsonNode *node;
|
||||
GError *inner_error = NULL;
|
||||
|
||||
g_return_if_fail(json_data != NULL);
|
||||
|
||||
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, "tags")) != NULL) {
|
||||
JsonObjectIter iter;
|
||||
JsonObject *tags_root = json_node_get_object(node);
|
||||
const gchar *member_name;
|
||||
JsonNode *member_node;
|
||||
|
||||
json_object_iter_init(&iter, tags_root);
|
||||
|
||||
while (json_object_iter_next(&iter, &member_name, &member_node)) {
|
||||
if (priv->tags == NULL) {
|
||||
priv->tags = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)json_node_unref);
|
||||
}
|
||||
|
||||
g_hash_table_replace(priv->tags, g_strdup(member_name), json_node_ref(member_node));
|
||||
}
|
||||
|
||||
json_node_free(node);
|
||||
}
|
||||
|
||||
MATRIX_EVENT_BASE_CLASS(matrix_event_tag_parent_class)->from_json(matrix_event_base, json_data, &inner_error);
|
||||
|
||||
if (inner_error != NULL) {
|
||||
g_propagate_error(error, inner_error);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
|
||||
{
|
||||
MatrixEventTagPrivate *priv;
|
||||
GError *inner_error = NULL;
|
||||
|
||||
priv = matrix_event_tag_get_instance_private(MATRIX_EVENT_TAG(matrix_event_base));
|
||||
|
||||
if (priv->tags != NULL) {
|
||||
JsonObject *tags_root = json_object_new();
|
||||
GHashTableIter iter;
|
||||
gpointer key;
|
||||
gpointer value;
|
||||
|
||||
g_hash_table_iter_init(&iter, priv->tags);
|
||||
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
gchar *tag = (gchar *)key;
|
||||
JsonNode *tag_contents = (JsonNode *)value;
|
||||
|
||||
json_object_set_member(tags_root, tag, tag_contents);
|
||||
}
|
||||
|
||||
if (json_object_get_size(tags_root) > 0) {
|
||||
JsonObject *root;
|
||||
JsonObject *content_root;
|
||||
JsonNode *content_node;
|
||||
JsonNode *tags_node;
|
||||
|
||||
root = json_node_get_object(json_data);
|
||||
content_node = json_object_get_member(root, "content");
|
||||
content_root = json_node_get_object(content_node);
|
||||
tags_node = json_node_new(JSON_NODE_OBJECT);
|
||||
|
||||
json_node_set_object(tags_node, tags_root);
|
||||
json_object_set_member(content_root, "tags", tags_node);
|
||||
}
|
||||
}
|
||||
|
||||
MATRIX_EVENT_BASE_CLASS(matrix_event_tag_parent_class)->to_json(matrix_event_base, json_data, &inner_error);
|
||||
|
||||
if (inner_error != NULL) {
|
||||
g_propagate_error(error, inner_error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix_event_tag_new:
|
||||
*
|
||||
* Create a new #MatrixEventTag object
|
||||
*
|
||||
* Returns: (transfer full): a new #MatrixEventTag object
|
||||
*/
|
||||
MatrixEventTag *
|
||||
matrix_event_tag_new(void)
|
||||
{
|
||||
return (MatrixEventTag *)matrix_event_base_construct(MATRIX_EVENT_TYPE_TAG);
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix_event_tag_get_tags:
|
||||
* @event: a #MatrixEventTag
|
||||
*
|
||||
* Get the list of tags as a #GHashTable
|
||||
*
|
||||
* The returned value is owned by @event and should not be freed.
|
||||
*
|
||||
* Returns: (transfer none) (nullable): the list of tags
|
||||
*/
|
||||
GHashTable *
|
||||
matrix_event_tag_get_tags(MatrixEventTag *event)
|
||||
{
|
||||
MatrixEventTagPrivate *priv = matrix_event_tag_get_instance_private(event);
|
||||
|
||||
g_return_val_if_fail(event != NULL, NULL);
|
||||
|
||||
return priv->tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* matrix_event_tag_set_tags:
|
||||
* @event: a #MatrixEventTag
|
||||
* @tags: (transfer none) (nullable): a #GHashTable
|
||||
*
|
||||
* Set the tag list of @event.
|
||||
*
|
||||
* @tags, if not %NULL, must be a #GHashTable whose keys are strings, and values are
|
||||
* #JsonNode objects.
|
||||
*
|
||||
* @event creates a reference on @tags, so it can be released by the caller.
|
||||
*/
|
||||
void
|
||||
matrix_event_tag_set_tags(MatrixEventTag *event, GHashTable *tags)
|
||||
{
|
||||
MatrixEventTagPrivate *priv = matrix_event_tag_get_instance_private(event);
|
||||
|
||||
g_return_if_fail(event != NULL);
|
||||
|
||||
g_hash_table_unref(priv->tags);
|
||||
priv->tags = g_hash_table_ref(tags);
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_finalize(GObject *gobject)
|
||||
{
|
||||
MatrixEventTagPrivate *priv = matrix_event_tag_get_instance_private(MATRIX_EVENT_TAG(gobject));
|
||||
|
||||
g_hash_table_unref(priv->tags);
|
||||
|
||||
G_OBJECT_CLASS(matrix_event_tag_parent_class)->finalize(gobject);
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_set_property(GObject *gobject, guint prop_id, const GValue *value, GParamSpec *pspec)
|
||||
{
|
||||
switch (prop_id) {
|
||||
case PROP_TAGS:
|
||||
matrix_event_tag_set_tags(MATRIX_EVENT_TAG(gobject), g_value_get_boxed(value));
|
||||
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_get_property(GObject *gobject, guint prop_id, GValue *value, GParamSpec *pspec)
|
||||
{
|
||||
switch (prop_id) {
|
||||
case PROP_TAGS:
|
||||
g_value_set_boxed(value, matrix_event_tag_get_tags(MATRIX_EVENT_TAG(gobject)));
|
||||
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_class_init(MatrixEventTagClass *klass)
|
||||
{
|
||||
((MatrixEventBaseClass *)klass)->from_json = matrix_event_tag_real_from_json;
|
||||
((MatrixEventBaseClass *)klass)->to_json = matrix_event_tag_real_to_json;
|
||||
|
||||
G_OBJECT_CLASS(klass)->finalize = matrix_event_tag_finalize;
|
||||
G_OBJECT_CLASS(klass)->get_property = matrix_event_tag_get_property;
|
||||
G_OBJECT_CLASS(klass)->set_property = matrix_event_tag_set_property;
|
||||
|
||||
/**
|
||||
* MatrixEventTag:tags:
|
||||
*
|
||||
* The list of tags as a #GHashTable.
|
||||
*/
|
||||
matrix_event_tag_properties[PROP_TAGS] = g_param_spec_boxed(
|
||||
"tags", "tags", "tags",
|
||||
G_TYPE_HASH_TABLE,
|
||||
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
|
||||
g_object_class_install_property(G_OBJECT_CLASS(klass),
|
||||
PROP_TAGS,
|
||||
matrix_event_tag_properties[PROP_TAGS]);
|
||||
}
|
||||
|
||||
static void
|
||||
matrix_event_tag_init(MatrixEventTag *matrix_event_tag)
|
||||
{
|
||||
MatrixEventTagPrivate *priv = matrix_event_tag_get_instance_private(matrix_event_tag);
|
||||
|
||||
priv->tags = NULL;
|
||||
}
|
40
src/matrix-event-tag.h
Normal file
40
src/matrix-event-tag.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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_EVENT_TAG_H__
|
||||
# define __MATRIX_GLIB_SDK_EVENT_TAG_H__
|
||||
|
||||
# include <glib-object.h>
|
||||
# include "matrix-event-base.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
# define MATRIX_EVENT_TYPE_TAG matrix_event_tag_get_type()
|
||||
G_DECLARE_DERIVABLE_TYPE(MatrixEventTag, matrix_event_tag, MATRIX_EVENT, TAG, MatrixEventBase)
|
||||
|
||||
struct _MatrixEventTagClass {
|
||||
MatrixEventBaseClass parent_class;
|
||||
};
|
||||
|
||||
MatrixEventTag* matrix_event_tag_new(void);
|
||||
GHashTable *matrix_event_tag_get_tags(MatrixEventTag *event);
|
||||
void matrix_event_tag_set_tags(MatrixEventTag *event, GHashTable *tags);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __MATRIX_GLIB_SDK_EVENT_TAG_H__ */
|
@ -1,69 +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/>.
|
||||
*/
|
||||
|
||||
public class Matrix.Event.Tag : Matrix.Event.Base {
|
||||
private HashTable<string, Json.Node>? _tags = 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("tags")) != null) {
|
||||
var tags_root = node.get_object();
|
||||
|
||||
tags_root.foreach_member((tobj, tag, tag_contents) => {
|
||||
if (_tags == null) {
|
||||
_tags = new HashTable<string, Json.Node>(
|
||||
str_hash, str_equal);
|
||||
}
|
||||
|
||||
_tags.replace(tag, tag_contents);
|
||||
});
|
||||
}
|
||||
|
||||
base.from_json(json_data);
|
||||
}
|
||||
|
||||
protected override void
|
||||
to_json(Json.Node json_data)
|
||||
throws Matrix.Error
|
||||
{
|
||||
if (_tags != null) {
|
||||
var tags_root = new Json.Object();
|
||||
|
||||
_tags.foreach((tag, tag_contents) => {
|
||||
tags_root.set_member(tag, tag_contents);
|
||||
});
|
||||
|
||||
if (tags_root.get_size() > 0) {
|
||||
var content_root = json_data.get_object()
|
||||
.get_member("content").get_object();
|
||||
var tags_node = new Json.Node(Json.NodeType.OBJECT);
|
||||
|
||||
tags_node.set_object(tags_root);
|
||||
content_root.set_member("tags", tags_node);
|
||||
}
|
||||
}
|
||||
|
||||
base.to_json(json_data);
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
#include "matrix-glib.h"
|
||||
#include "matrix-marshalers.h"
|
||||
#include "matrix-event-tag.h"
|
||||
|
||||
/*
|
||||
Borrowed from GLib
|
||||
|
@ -524,5 +524,14 @@ namespace Matrix {
|
||||
|
||||
public Json.Node? get_stripped_node();
|
||||
}
|
||||
|
||||
[CCode (cheader_filename = "matrix-event-tag.h")]
|
||||
public class Tag : Base {
|
||||
protected override void from_json(Json.Node json_data)
|
||||
throws Matrix.Error;
|
||||
|
||||
protected override void to_json(Json.Node json_data)
|
||||
throws Matrix.Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user