diff --git a/src/matrix-c-compacts.c b/src/matrix-c-compacts.c index bb2f5e2..4081d2c 100644 --- a/src/matrix-c-compacts.c +++ b/src/matrix-c-compacts.c @@ -2957,3 +2957,137 @@ matrix_search_room_events_init(MatrixSearchRoomEvents *matrix_search_room_events priv->_filter = NULL; priv->_groupings = NULL; } + +typedef struct { + MatrixSearchRoomEvents *_room_events; +} MatrixSearchCategoriesPrivate; + +/** + * MatrixSearchCategories: + * + * Class to hold search categories. + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixSearchCategories, matrix_search_categories, MATRIX_TYPE_JSON_COMPACT); + +static JsonNode * +matrix_search_categories_get_json_node(MatrixJsonCompact *matrix_json_compact, GError **error) +{ + MatrixSearchCategoriesPrivate *priv; + JsonBuilder *builder; + JsonNode *result; + JsonNode *node; + GError *inner_error = NULL; + + g_return_val_if_fail(matrix_json_compact != NULL, NULL); + + priv = matrix_search_categories_get_instance_private(MATRIX_SEARCH_CATEGORIES(matrix_json_compact)); + + if (priv->_room_events == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, "room_events filter must be filled"); + + return NULL; + } + + node = matrix_json_compact_get_json_node(MATRIX_JSON_COMPACT(priv->_room_events), &inner_error); + + if (inner_error != NULL) { + g_propagate_error(error, inner_error); + + return NULL; + } + + builder = json_builder_new(); + json_builder_begin_object(builder); + + json_builder_set_member_name(builder, "room_events"); + json_builder_add_value(builder, node); + + json_builder_end_object(builder); + + result = json_builder_get_root(builder); + g_object_unref(builder); + + return result; +} + +/** + * matrix_search_categories_new: + * + * Create a new #MatrixSearchCategories object. + * + * Returns: (transfer full): a new #MatrixSearchCategories object + */ +MatrixSearchCategories * +matrix_search_categories_new(void) +{ + return (MatrixSearchCategories *)matrix_json_compact_construct(MATRIX_TYPE_SEARCH_CATEGORIES); +} + +/** + * matrix_search_categories_get_room_events: + * @search_categories: a #MatrixSearchCategories object + * + * Get the search ruleset from @search_categories. + * + * The returned value is owned by @search_categories and should not be freed. + * + * Returns: (transfer none) (nullable): the search ruleset + */ +MatrixSearchRoomEvents * +matrix_search_categories_get_room_events(MatrixSearchCategories *matrix_search_categories) +{ + MatrixSearchCategoriesPrivate *priv; + + g_return_val_if_fail(matrix_search_categories != NULL, NULL); + + priv = matrix_search_categories_get_instance_private(matrix_search_categories); + + return priv->_room_events; +} + +/** + * matrix_search_categories_set_room_events: + * @search_categories: a #MatrixSearchCategories object + * @room_events: (transfer none) (nullable): a #MatrixSearchRoomEvents object + * + * Set the ruleset to use during a search. Null is considered an invalid value, but doesn’t + * result in an error until matrix_json_compact_get_json_node() is called. + * + * This function gets a reference on @search_categories, so it can be unreffed after the call. + */ +void +matrix_search_categories_set_room_events(MatrixSearchCategories *matrix_search_categories, MatrixSearchRoomEvents *room_events) +{ + MatrixSearchCategoriesPrivate *priv; + + g_return_if_fail(matrix_search_categories != NULL); + + priv = matrix_search_categories_get_instance_private(matrix_search_categories); + + matrix_json_compact_unref(MATRIX_JSON_COMPACT(priv->_room_events)); + priv->_room_events = (MatrixSearchRoomEvents *)matrix_json_compact_ref(MATRIX_JSON_COMPACT(room_events)); +} + +static void +matrix_search_categories_finalize(MatrixJsonCompact *matrix_json_compact) +{ + MatrixSearchCategoriesPrivate *priv = matrix_search_categories_get_instance_private(MATRIX_SEARCH_CATEGORIES(matrix_json_compact)); + + matrix_json_compact_unref(MATRIX_JSON_COMPACT(priv->_room_events)); + + MATRIX_JSON_COMPACT_CLASS(matrix_search_categories_parent_class)->finalize(matrix_json_compact); +} + +static void +matrix_search_categories_class_init(MatrixSearchCategoriesClass *klass) +{ + ((MatrixJsonCompactClass *)klass)->finalize = matrix_search_categories_finalize; + ((MatrixJsonCompactClass *)klass)->get_json_node = matrix_search_categories_get_json_node; +} + +static void +matrix_search_categories_init(MatrixSearchCategories *matrix_search_categories) +{ + MatrixSearchCategoriesPrivate *priv = matrix_search_categories_get_instance_private(matrix_search_categories); + priv->_room_events = NULL; +} diff --git a/src/matrix-c-compacts.h b/src/matrix-c-compacts.h index 37807ad..65f07b8 100644 --- a/src/matrix-c-compacts.h +++ b/src/matrix-c-compacts.h @@ -220,6 +220,17 @@ void matrix_search_room_events_set_search_term(MatrixSearchRoomEvents *search_ro MatrixSearchGroupings *matrix_search_room_events_get_groupings(MatrixSearchRoomEvents *search_room_events); void matrix_search_room_events_set_groupings(MatrixSearchRoomEvents *search_room_events, MatrixSearchGroupings *groupings); +# define MATRIX_TYPE_SEARCH_CATEGORIES matrix_search_categories_get_type() +G_DECLARE_DERIVABLE_TYPE(MatrixSearchCategories, matrix_search_categories, MATRIX, SEARCH_CATEGORIES, MatrixJsonCompact) + +struct _MatrixSearchCategoriesClass { + MatrixJsonCompactClass parent_class; +}; + +MatrixSearchCategories *matrix_search_categories_new(void); +MatrixSearchRoomEvents *matrix_search_categories_get_room_events(MatrixSearchCategories *search_categories); +void matrix_search_categories_set_room_events(MatrixSearchCategories *search_categories, MatrixSearchRoomEvents *room_events); + G_END_DECLS #endif /* __MATRIX_GLIB_SDK_COMPACTS_H__ */ diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala index 41023f7..2efa382 100644 --- a/src/matrix-compacts.vala +++ b/src/matrix-compacts.vala @@ -17,33 +17,6 @@ */ namespace Matrix { - public class SearchCategories : JsonCompact { - public SearchRoomEvents? room_events { get; set; default = null; } - - public override Json.Node? - get_json_node() - throws Matrix.Error - { - Json.Node? node = null; - - if ((room_events == null) - && ((node = room_events.get_json_node()) != null)) { - return null; - } - - var builder = new Json.Builder(); - - builder.begin_object(); - - builder.set_member_name("room_events"); - builder.add_value(node); - - builder.end_object(); - - return builder.get_root(); - } - } - public Json.Node? _json_node_deep_copy(Json.Node? node) { diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index ee3a796..35ef228 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -397,6 +397,14 @@ namespace Matrix { throws Matrix.Error; } + [CCode (cheader_filename = "matrix-c-compacts.h")] + public class SearchCategories : JsonCompact { + public SearchRoomEvents? room_events { get; set; default = null; } + + public override Json.Node? get_json_node() + throws Matrix.Error; + } + /* Utilities */ [CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")] public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, char convert_dashes = '_');