Port MatrixSearchCategories to C

This commit is contained in:
Gergely Polonkai 2017-11-10 19:03:00 +01:00
parent 3ffd5a5b38
commit da6e3f8b7b
4 changed files with 153 additions and 27 deletions

View File

@ -2957,3 +2957,137 @@ matrix_search_room_events_init(MatrixSearchRoomEvents *matrix_search_room_events
priv->_filter = NULL; priv->_filter = NULL;
priv->_groupings = 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 doesnt
* 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;
}

View File

@ -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); MatrixSearchGroupings *matrix_search_room_events_get_groupings(MatrixSearchRoomEvents *search_room_events);
void matrix_search_room_events_set_groupings(MatrixSearchRoomEvents *search_room_events, MatrixSearchGroupings *groupings); 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 G_END_DECLS
#endif /* __MATRIX_GLIB_SDK_COMPACTS_H__ */ #endif /* __MATRIX_GLIB_SDK_COMPACTS_H__ */

View File

@ -17,33 +17,6 @@
*/ */
namespace Matrix { 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? public Json.Node?
_json_node_deep_copy(Json.Node? node) _json_node_deep_copy(Json.Node? node)
{ {

View File

@ -397,6 +397,14 @@ namespace Matrix {
throws Matrix.Error; 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 */ /* Utilities */
[CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")] [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 = '_'); public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, char convert_dashes = '_');