From e35cd53d794adb48fd7ff6f31d0227c57a7fd06c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 19 Feb 2016 10:04:22 +0100 Subject: [PATCH] Rework MatrixRoomFilter in Vala --- src/c-api.vapi | 5 - src/matrix-compacts.vala | 62 +++++++++ src/matrix-types.c | 294 --------------------------------------- src/matrix-types.h | 24 ---- 4 files changed, 62 insertions(+), 323 deletions(-) diff --git a/src/c-api.vapi b/src/c-api.vapi index bf67fe9..e5868e7 100644 --- a/src/c-api.vapi +++ b/src/c-api.vapi @@ -124,11 +124,6 @@ namespace Matrix { throws Matrix.Error; } - [CCode (cheader_filename = "matrix-types.h")] - public class RoomFilter { - public Json.Node? get_json_node(); - } - [CCode (cheader_filename = "matrix-types.h")] public class FilterRules { public Json.Node? get_json_node(); diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala index 63e8551..db28899 100644 --- a/src/matrix-compacts.vala +++ b/src/matrix-compacts.vala @@ -111,4 +111,66 @@ namespace Matrix { return builder.get_root(); } } + + /** + * Class to hold room filters. + */ + public class RoomFilter : JsonCompact { + /** + * If {{{true}}}, events for rooms that the user has left will + * be included in the filtered event list. + */ + public bool include_leave { get; set; default=true; } + + /** + * Filtering rules for ephemeral events, i.e. events that are + * not recorded in the room history (typing notifications, + * receipts, etc.) + */ + public FilterRules? ephemeral { get; set; default = null; } + + /** + * Filtering rules for state events. + */ + public FilterRules? state { get; set; default = null; } + + /** + * Filtering rules for timeline events. + */ + public FilterRules? timeline { get; set; default = null; } + + /** + * Get the room filters as a JSON node. + */ + public override Json.Node? + get_json_node() + throws Matrix.Error + { + var builder = new Json.Builder(); + + builder.begin_object(); + + builder.set_member_name("include_leave"); + builder.add_boolean_value(include_leave); + + if (ephemeral != null) { + builder.set_member_name("ephemeral"); + builder.add_value(ephemeral.get_json_node()); + } + + if (state != null) { + builder.set_member_name("state"); + builder.add_value(state.get_json_node()); + } + + if (timeline != null) { + builder.set_member_name("timeline"); + builder.add_value(timeline.get_json_node()); + } + + builder.end_object(); + + return builder.get_root(); + } + } } diff --git a/src/matrix-types.c b/src/matrix-types.c index 3d0ada9..bf61201 100644 --- a/src/matrix-types.c +++ b/src/matrix-types.c @@ -973,300 +973,6 @@ matrix_filter_rules_get_json_data(MatrixFilterRules *rules, gsize *datalen) return data; } -/** - * MatrixRoomFilter: - * - * An opaque structure to hold room filters. - */ -struct _MatrixRoomFilter { - gboolean include_leave; - MatrixFilterRules *ephemeral; - MatrixFilterRules *state; - MatrixFilterRules *timeline; - guint refcount; -}; - -G_DEFINE_BOXED_TYPE(MatrixRoomFilter, matrix_room_filter, - (GBoxedCopyFunc)matrix_room_filter_ref, - (GBoxedFreeFunc)matrix_room_filter_unref); - -/** - * matrix_room_filter_new: - * - * Create a new #MatrixRoomFilter object with reference count of 1. - * - * Returns: (transfer full): a new #MatrixRoomFilter - */ -MatrixRoomFilter * -matrix_room_filter_new(void) -{ - MatrixRoomFilter *filter; - - filter = g_new0(MatrixRoomFilter, 1); - filter->refcount = 1; - - return filter; -} - -static void -matrix_room_filter_free(MatrixRoomFilter *filter) -{ - if (filter->ephemeral) { - matrix_filter_rules_unref(filter->ephemeral); - } - - if (filter->state) { - matrix_filter_rules_unref(filter->state); - } - - if (filter->timeline) { - matrix_filter_rules_unref(filter->timeline); - } - - g_free(filter); -} - -/** - * matrix_room_filter_ref: - * @filter: a #MatrixRoomFilter - * - * Increase reference count of @filter by one. - * - * Returns: (transfer none): the same #MatrixRoomFilter - */ -MatrixRoomFilter * -matrix_room_filter_ref(MatrixRoomFilter *filter) -{ - filter->refcount++; - - return filter; -} - -/** - * matrix_room_filter_unref: - * @filter: a #MatrixRoomFilter - * - * Decrease reference count of @filter by one. If reference count - * reaches zero, @filter is freed. - */ -void -matrix_room_filter_unref(MatrixRoomFilter *filter) -{ - if (--filter->refcount == 0) { - matrix_room_filter_free(filter); - } -} - -/** - * matrix_room_filter_set_include_leave: - * @filter: a #MatrixRoomFilter - * @include_leave: %TRUE if events from left rooms should be included - * - * Sets if events for rooms that the user has left should be included - * in the filtered event list. - */ -void -matrix_room_filter_set_include_leave(MatrixRoomFilter *filter, - gboolean include_leave) -{ - filter->include_leave = include_leave; -} - -/** - * matrix_room_filter_get_include_leave: - * @filter: a #MatrixRoomFilter - * - * If %TRUE, events from rooms that the user has left will be included - * in the filtered event list. - * - * Returns: the value of include_leave - */ -gboolean -matrix_room_filter_get_include_leave(MatrixRoomFilter *filter) -{ - return filter->include_leave; -} - -/** - * matrix_room_filter_set_ephemeral: - * @filter: a #MatrixRoomFilter - * @rules: (transfer none): filtering rules for ephemeral events - * - * Set filtering rules for ephemeral events, i.e. events that are not - * recorded in the room history (typing notifications, receipts, etc.) - * @filter gets a reference on @rules, so the caller may unref it. - */ -void -matrix_room_filter_set_ephemeral(MatrixRoomFilter *filter, - MatrixFilterRules *rules) -{ - if (filter->ephemeral) { - matrix_filter_rules_unref(filter->ephemeral); - } - - filter->ephemeral = matrix_filter_rules_ref(rules); -} - -/** - * matrix_room_filter_get_ephemeral: - * @filter: a #MatrixRoomFilter - * - * Gets the filtering rules for ephemeral events. See - * matrix_room_filter_set_ephemeral() for details. - * - * Returns: (transfer none): the filtering rules for ephemeral events. - */ -MatrixFilterRules * -matrix_room_filter_get_ephemeral(MatrixRoomFilter *filter) -{ - return filter->ephemeral; -} - -/** - * matrix_room_filter_set_state: - * @filter: a #MatrixRoomFilter - * @rules: (transfer none): filtering rules for state events - * - * Sets filtering rules for state events. @filter obtains a reference - * on @rules, so the caller may unref it safely. - */ -void -matrix_room_filter_set_state(MatrixRoomFilter *filter, MatrixFilterRules *rules) -{ - if (filter->state) { - matrix_filter_rules_unref(filter->state); - } - - filter->state = matrix_filter_rules_ref(rules); -} - -/** - * matrix_room_filter_get_state: - * @filter: a #MatrixRoomFilter - * - * Gets the filtering rules for state events. - * - * Returns: (transfer none): the filtering rules for state events - */ -MatrixFilterRules * -matrix_room_filter_get_state(MatrixRoomFilter *filter) -{ - return filter->state; -} - -/** - * matrix_room_filter_set_timeline: - * @filter: a #MatrixRoomFilter - * @rules: (transfer none): filtering rules for timeline events - * (messages and state events from rooms) - * - * Set filtering rules for timeline events. - */ -void -matrix_room_filter_set_timeline(MatrixRoomFilter *filter, - MatrixFilterRules *rules) -{ - if (filter->timeline) { - matrix_filter_rules_unref(filter->timeline); - } - - filter->timeline = matrix_filter_rules_ref(rules); -} - -/** - * matrix_room_filter_get_timeline: - * @filter: a #MatrixRoomFilter - * - * Gets filtering rules for timeline events (see - * matrix_room_filter_set_timeline() for details. - * - * Returns: (transfer none): the filtering rules for timeline events - */ -MatrixFilterRules * -matrix_room_filter_get_timeline(MatrixRoomFilter *filter) -{ - return filter->timeline; -} - -/** - * matrix_room_filter_get_json_node: - * @filter: a #MatrixRoomFilter - * - * Gets the #JsonNode representation of the filtering ruleset. - * - * Returns: (transfer full): the JSON representation of the filtering - * ruleset as a #JsonNode - */ -JsonNode * -matrix_room_filter_get_json_node(MatrixRoomFilter *filter) -{ - JsonBuilder *builder; - JsonNode *node; - - builder = json_builder_new(); - json_builder_begin_object(builder); - - json_builder_set_member_name(builder, "include_leave"); - json_builder_add_boolean_value(builder, filter->include_leave); - - if (filter->ephemeral) { - json_builder_set_member_name(builder, "ephemeral"); - json_builder_add_value(builder, - matrix_filter_rules_get_json_node( - filter->ephemeral)); - } - - if (filter->state) { - json_builder_set_member_name(builder, "state"); - json_builder_add_value(builder, - matrix_filter_rules_get_json_node( - filter->state)); - } - - if (filter->timeline) { - json_builder_set_member_name(builder, "timeline"); - json_builder_add_value(builder, - matrix_filter_rules_get_json_node( - filter->timeline)); - } - - json_builder_end_object(builder); - - node = json_builder_get_root(builder); - g_object_unref(builder); - - return node; -} - -/** - * matrix_room_filter_get_json_data: - * @filter: a #MatrixRoomFilter - * @datalen: (out) (allow-none): storage for the length of the JSON - * data, or %NULL - * - * Get the string representation of the filtering ruleset as a JSON - * object. - * - * Returns: the JSON object representation of the filtering ruleset - * data as a string - */ -gchar * -matrix_room_filter_get_json_data(MatrixRoomFilter *filter, gsize *datalen) -{ - JsonGenerator *generator; - JsonNode *node = matrix_room_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; -} - /** * Matrix3PidCredential: * diff --git a/src/matrix-types.h b/src/matrix-types.h index c06b8ef..f064281 100644 --- a/src/matrix-types.h +++ b/src/matrix-types.h @@ -180,30 +180,6 @@ JsonNode *matrix_filter_rules_get_json_node(MatrixFilterRules *rules); gchar *matrix_filter_rules_get_json_data(MatrixFilterRules *rules, gsize *datalen); -typedef struct _MatrixRoomFilter MatrixRoomFilter; - -GType matrix_room_filter_get_type(void); -#define MATRIX_TYPE_ROOM_FILTER (matrix_room_filter_get_type()) - -MatrixRoomFilter *matrix_room_filter_new(void); -MatrixRoomFilter *matrix_room_filter_ref(MatrixRoomFilter *filter); -void matrix_room_filter_unref(MatrixRoomFilter *filter); -void matrix_room_filter_set_include_leave(MatrixRoomFilter *filter, - gboolean include_leave); -gboolean matrix_room_filter_get_include_leave(MatrixRoomFilter *filter); -void matrix_room_filter_set_ephemeral(MatrixRoomFilter *filter, - MatrixFilterRules *rules); -MatrixFilterRules *matrix_room_filter_get_ephemeral(MatrixRoomFilter *filter); -void matrix_room_filter_set_state(MatrixRoomFilter *filter, - MatrixFilterRules *rules); -MatrixFilterRules *matrix_room_filter_get_state(MatrixRoomFilter *filter); -void matrix_room_filter_set_timeline(MatrixRoomFilter *filter, - MatrixFilterRules *rules); -MatrixFilterRules *matrix_room_filter_get_timeline(MatrixRoomFilter *filter); -JsonNode *matrix_room_filter_get_json_node(MatrixRoomFilter *filter); -gchar *matrix_room_filter_get_json_data(MatrixRoomFilter *filter, - gsize *datalen); - typedef struct _Matrix3PidCredential Matrix3PidCredential; GType matrix_3pid_credential_get_type(void);