From 1c14ad77ff807865bfe0dc89ed77e9afad7f823a Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 10 Nov 2017 13:54:40 +0100 Subject: [PATCH] Port MatrixEventContext to C --- src/matrix-c-compacts.c | 204 +++++++++++++++++++++++++++++++++++++++ src/matrix-c-compacts.h | 15 +++ src/matrix-compacts.vala | 32 ------ vapi/c-api.vapi | 10 ++ 4 files changed, 229 insertions(+), 32 deletions(-) diff --git a/src/matrix-c-compacts.c b/src/matrix-c-compacts.c index 8cd29c6..3c70b13 100644 --- a/src/matrix-c-compacts.c +++ b/src/matrix-c-compacts.c @@ -1961,3 +1961,207 @@ matrix_pusher_init(MatrixPusher *matrix_pusher) priv->_pushkey = NULL; priv->_data = NULL; } + +typedef struct { + gint _before_limit; + gint _after_limit; + gboolean _include_profile; +} MatrixEventContextPrivate; + +/** + * MatrixEventContext: + * + * Class to hold event context settings for searches. + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventContext, matrix_event_context, MATRIX_TYPE_JSON_COMPACT); + +static JsonNode * +matrix_event_context_get_json_node(MatrixJsonCompact *matrix_json_compact, GError **error) +{ + MatrixEventContextPrivate *priv; + JsonBuilder *builder; + JsonNode *result; + + g_return_val_if_fail(matrix_json_compact != NULL, NULL); + + priv = matrix_event_context_get_instance_private(MATRIX_EVENT_CONTEXT(matrix_json_compact)); + + builder = json_builder_new(); + + json_builder_begin_object(builder); + + if (priv->_before_limit >= 0) { + json_builder_set_member_name(builder, "before_limit"); + json_builder_add_int_value(builder, priv->_before_limit); + } + + if (priv->_after_limit >= 0) { + json_builder_set_member_name(builder, "after_limit"); + json_builder_add_int_value(builder, priv->_after_limit); + } + + json_builder_set_member_name(builder, "include_profile"); + json_builder_add_boolean_value(builder, priv->_include_profile); + + json_builder_end_object(builder); + + result = json_builder_get_root(builder); + g_object_unref(builder); + + return result; +} + +/** + * matrix_event_context_new: + * + * Create a new #MatrixEventContext object. + * + * Returns: (transfer full): a new #MatrixEventContext object + */ +MatrixEventContext * +matrix_event_context_new(void) +{ + return (MatrixEventContext *)matrix_json_compact_construct(MATRIX_TYPE_EVENT_CONTEXT); +} + +/** + * matrix_event_context_get_before_limit: + * @event_context: a #MatrixEventContext object + * + * Get the limit of messages before search results. + * + * Returns: the number of messages that will appear before the one in the result + */ +gint +matrix_event_context_get_before_limit(MatrixEventContext *matrix_event_context) +{ + MatrixEventContextPrivate *priv; + + g_return_val_if_fail(matrix_event_context != NULL, 0); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + return priv->_before_limit; +} + +/** + * matrix_event_context_set_before_limit: + * @event_context: a #MatrixEventContext object + * @before_limit: the number of messages to appear before search results + * + * Set the limit of messages to display before search results. + */ +void +matrix_event_context_set_before_limit(MatrixEventContext *matrix_event_context, gint before_limit) +{ + MatrixEventContextPrivate *priv; + + g_return_if_fail(matrix_event_context != NULL); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + priv->_before_limit = before_limit; +} + +/** + * matrix_event_context_get_after_limit: + * @event_context: a #MatrixEventContext object + * + * Get the limit of messages after search results. + * + * Returns: the number of messages that will appear after the one in the result + */ +gint +matrix_event_context_get_after_limit(MatrixEventContext *matrix_event_context) +{ + MatrixEventContextPrivate *priv; + + g_return_val_if_fail(matrix_event_context != NULL, 0); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + return priv->_after_limit; +} + +/** + * matrix_event_context_after_before_limit: + * @event_context: a #MatrixEventContext object + * @before_limit: the number of messages to appear after search results + * + * Set the limit of messages to display after search results. + */ +void +matrix_event_context_set_after_limit(MatrixEventContext *matrix_event_context, gint after_limit) +{ + MatrixEventContextPrivate *priv; + + g_return_if_fail(matrix_event_context != NULL); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + priv->_after_limit = after_limit; +} + +/** + * matrix_event_context_get_include_profile: + * @event_context: a #MatrixEventContext object + * + * If this function returns %TRUE, profile data will be included with each message in the + * result set. + * + * Returns: %TRUE or %FALSE indicating if profile data should be included + */ +gboolean +matrix_event_context_get_include_profile(MatrixEventContext *matrix_event_context) +{ + MatrixEventContextPrivate *priv; + + g_return_val_if_fail(matrix_event_context != NULL, 0); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + return priv->_include_profile; +} + +/** + * matrix_event_context_set_include_profile: + * @event_context: a #MatrixEventContext object + * @include_profile: a boolean value indicating if profile data should be included in the results + * + * Set this to %TRUE if you want profile data to be included in the search results. + */ +void +matrix_event_context_set_include_profile(MatrixEventContext *matrix_event_context, gboolean include_profile) +{ + MatrixEventContextPrivate *priv; + + g_return_if_fail(matrix_event_context != NULL); + + priv = matrix_event_context_get_instance_private(matrix_event_context); + + priv->_include_profile = include_profile; +} + +static void +matrix_event_context_finalize(MatrixJsonCompact *matrix_json_compact) +{ + MATRIX_JSON_COMPACT_CLASS (matrix_event_context_parent_class)->finalize (matrix_json_compact); +} + +static void +matrix_event_context_class_init(MatrixEventContextClass *klass) +{ + ((MatrixJsonCompactClass *)klass)->finalize = matrix_event_context_finalize; + ((MatrixJsonCompactClass *)klass)->get_json_node = matrix_event_context_get_json_node; +} + +static void +matrix_event_context_init(MatrixEventContext *matrix_event_context) +{ + MatrixEventContextPrivate *priv; + + priv = matrix_event_context_get_instance_private(matrix_event_context); + priv->_before_limit = -1; + priv->_after_limit = -1; + priv->_include_profile = FALSE; +} diff --git a/src/matrix-c-compacts.h b/src/matrix-c-compacts.h index bb3361e..6c1e03b 100644 --- a/src/matrix-c-compacts.h +++ b/src/matrix-c-compacts.h @@ -158,6 +158,21 @@ void matrix_pusher_set_pushkey(MatrixPusher *pusher, const gchar *pushkey); JsonNode *matrix_pusher_get_data(MatrixPusher *pusher); void matrix_pusher_set_data(MatrixPusher *pusher, JsonNode *data); +# define MATRIX_TYPE_EVENT_CONTEXT matrix_event_context_get_type() +G_DECLARE_DERIVABLE_TYPE(MatrixEventContext, matrix_event_context, MATRIX, EVENT_CONTEXT, MatrixJsonCompact) + +struct _MatrixEventContextClass { + MatrixJsonCompactClass parent_class; +}; + +MatrixEventContext *matrix_event_context_new (void); +gint matrix_event_context_get_before_limit (MatrixEventContext *event_context); +void matrix_event_context_set_before_limit (MatrixEventContext *event_context, gint before_limit); +gint matrix_event_context_get_after_limit (MatrixEventContext *event_context); +void matrix_event_context_set_after_limit (MatrixEventContext *event_context, gint after_limit); +gboolean matrix_event_context_get_include_profile (MatrixEventContext *event_context); +void matrix_event_context_set_include_profile (MatrixEventContext *event_context, gboolean include_profile); + G_END_DECLS #endif /* __MATRIX_GLIB_SDK_COMPACTS_H__ */ diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala index f71f4e4..45b9ee2 100644 --- a/src/matrix-compacts.vala +++ b/src/matrix-compacts.vala @@ -17,38 +17,6 @@ */ namespace Matrix { - public class EventContext : JsonCompact { - public int before_limit { get; set; default = -1; } - public int after_limit { get; set; default = -1; } - public bool include_profile { get; set; default = false; } - - public override Json.Node? - get_json_node() - throws Matrix.Error - { - var builder = new Json.Builder(); - - builder.begin_object(); - - if (before_limit >= 0) { - builder.set_member_name("before_limit"); - builder.add_int_value(before_limit); - } - - if (after_limit >= 0) { - builder.set_member_name("after_limit"); - builder.add_int_value(after_limit); - } - - builder.set_member_name("include_profile"); - builder.add_boolean_value(include_profile); - - builder.end_object(); - - return builder.get_root(); - } - } - public class SearchGrouping : JsonCompact { public SearchGroupBy key { get; set; default = SearchGroupBy.NONE; } diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index de89182..8738ce2 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -355,6 +355,16 @@ namespace Matrix { throws Matrix.Error; } + [CCode (cheader_filename = "matrix-c-compacts.h")] + public class EventContext : JsonCompact { + public int before_limit { get; set; default = -1; } + public int after_limit { get; set; default = -1; } + public bool include_profile { get; set; default = false; } + + 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, bool convert_dashes = true);