From 0b17b583ab592e2927da2b34598dd2445fb0c614 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 15 Jan 2016 17:59:45 +0100 Subject: [PATCH] Add JSON getters for MatrixAPIFilter --- .../matrix-glib/matrix-glib-sections.txt | 2 + src/matrix-api-types.c | 65 +++++++++++++++++++ src/matrix-api-types.h | 2 + 3 files changed, 69 insertions(+) diff --git a/docs/reference/matrix-glib/matrix-glib-sections.txt b/docs/reference/matrix-glib/matrix-glib-sections.txt index 7c67b31..e4ed072 100644 --- a/docs/reference/matrix-glib/matrix-glib-sections.txt +++ b/docs/reference/matrix-glib/matrix-glib-sections.txt @@ -99,6 +99,8 @@ matrix_api_filter_set_presence_filter matrix_api_filter_get_presence_filter matrix_api_filter_set_room_filter matrix_api_filter_get_room_filter +matrix_api_filter_get_json_node +matrix_api_filter_get_json_data MatrixAPI3PidCredential diff --git a/src/matrix-api-types.c b/src/matrix-api-types.c index 193e319..ef65e26 100644 --- a/src/matrix-api-types.c +++ b/src/matrix-api-types.c @@ -17,6 +17,8 @@ */ #include "matrix-api-types.h" +#include "matrix-enumtypes.h" +#include "utils.h" /** * SECTION:matrix-api-types @@ -1525,6 +1527,69 @@ matrix_api_filter_get_room_filter(MatrixAPIFilter *filter) return filter->room; } +/** + * matrix_api_filter_get_json_node: + * @filter: a #MatrixAPIFilter + * + * Get the JSON representation of @filter as a #JsonNode + * + * Returns: (transfer full): the JSON representation of @filter + */ +JsonNode * +matrix_api_filter_get_json_node(MatrixAPIFilter *filter) +{ + JsonBuilder *builder; + JsonNode *root, *tmp; + + builder = json_builder_new(); + json_builder_begin_object(builder); + + json_builder_set_member_name(builder, "event_fields"); + json_builder_begin_array(builder); + g_list_foreach(filter->event_fields, (GFunc)json_add_string, builder); + json_builder_end_array(builder); + + json_builder_set_member_name(builder, "event_format"); + json_builder_add_string_value(builder, + g_enum_to_string( + MATRIX_TYPE_API_EVENT_FORMAT, + filter->event_format, + TRUE)); + + json_builder_set_member_name(builder, "presence"); + tmp = matrix_api_filter_rules_get_json_node(filter->presence); + json_builder_add_value(builder, tmp); + json_node_free(tmp); + + json_builder_set_member_name(builder, "room"); + tmp = matrix_api_room_filter_get_json_node(filter->room); + json_builder_add_value(builder, tmp); + json_node_free(tmp); + + json_builder_end_object(builder); + root = json_builder_get_root(builder); + g_object_unref(builder); + + return root; +} + +gchar * +matrix_api_filter_get_json_data(MatrixAPIFilter *filter, gsize *datalen) +{ + JsonGenerator *generator; + JsonNode *node = matrix_api_filter_get_json_node(filter); + gchar *data; + + generator = json_generator_new(); + json_generator_set_root(generator, node); + json_node_free(node); + + data = json_generator_to_data(generator, datalen); + g_object_unref(generator); + + return data; +} + /** * MatrixAPI3PidCredential: * diff --git a/src/matrix-api-types.h b/src/matrix-api-types.h index 3d3ef30..806c9ce 100644 --- a/src/matrix-api-types.h +++ b/src/matrix-api-types.h @@ -212,6 +212,8 @@ MatrixAPIFilterRules *matrix_api_filter_get_presence_filter(MatrixAPIFilter *fil void matrix_api_filter_set_room_filter(MatrixAPIFilter *filter, MatrixAPIRoomFilter *room_filter); MatrixAPIRoomFilter *matrix_api_filter_get_room_filter(MatrixAPIFilter *filter); +JsonNode *matrix_api_filter_get_json_node(MatrixAPIFilter *filter); +gchar *matrix_api_filter_get_json_data(MatrixAPIFilter *filter, gsize *datalen); typedef struct _MatrixAPI3PidCredential MatrixAPI3PidCredential;