Rework MatrixFilter in Vala

This commit is contained in:
Gergely Polonkai 2016-02-19 11:38:47 +01:00
parent 141542fe40
commit 9cf47d2e9b
4 changed files with 89 additions and 362 deletions

View File

@ -41,6 +41,13 @@ namespace Matrix {
public static GLib.Quark quark ();
}
[CCode (cheader_filename = "matrix-enumtypes.h", cprefix = "MATRIX_EVENT_FORMAT_")]
public enum EventFormat {
DEFAULT,
CLIENT,
FEDERATION
}
[CCode (cheader_filename = "matrix-enumtypes.h", cprefix = "MATRIX_RESIZE_METHOD_")]
public enum ResizeMethod {
DEFAULT,
@ -125,9 +132,13 @@ namespace Matrix {
}
[CCode (cheader_filename = "matrix-types.h")]
public class Filter {
public class RoomFilter {
public Json.Node? get_json_node();
}
[CCode (cheader_filename = "matrix-types.h")]
public class FilterRules {
public Json.Node? get_json_node();
public string? get_json_data(out size_t datalen);
}
[CCode (cheader_filename = "utils.h", cname = "_json_node_deep_copy")]

View File

@ -35,4 +35,80 @@ namespace Matrix {
return generator.to_data(out datalen);
}
}
/**
* Class to hold a filter.
*/
public class Filter : JsonCompact {
private List<string>? _event_fields;
/**
* The event fields to include in the filtered events.
*/
public List<string>? event_fields {
get {
return _event_fields;
}
set {
_event_fields = value.copy();
}
default = null;
}
/**
* The desired event format for the filtered events (e.g. for
* matrix_api_sync())
*/
public EventFormat event_format {
get; set;
default = Matrix.EventFormat.CLIENT;
}
/**
* A filtering ruleset for presence events.
*/
public FilterRules? presence_filter { get; set; default = null; }
/**
* A filtering ruleset for room events.
*/
public RoomFilter? room_filter { get; set; default = null; }
/**
* Get the filter 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("event_fields");
builder.begin_array();
foreach (var entry in event_fields) {
builder.add_string_value(entry);
}
builder.end_array();
builder.set_member_name("event_format");
builder.add_string_value(
_g_enum_value_to_nick(typeof(EventFormat), event_format));
builder.set_member_name("presence");
builder.add_value(presence_filter.get_json_node());
builder.set_member_name("room");
builder.add_value(room_filter.get_json_node());
builder.end_object();
return builder.get_root();
}
}
}

View File

@ -1281,340 +1281,6 @@ matrix_room_filter_get_json_data(MatrixRoomFilter *filter, gsize *datalen)
return data;
}
/**
* MatrixFilter:
*
* An opaque structure to hold an event filter.
*/
struct _MatrixFilter {
GList *event_fields;
MatrixEventFormat event_format;
MatrixFilterRules *presence;
MatrixRoomFilter *room;
guint refcount;
};
G_DEFINE_BOXED_TYPE(MatrixFilter, matrix_filter,
(GBoxedCopyFunc)matrix_filter_ref,
(GBoxedFreeFunc)matrix_filter_unref);
/**
* matrix_filter_new:
*
* Create a new #MatrixFilter object with reference count of 1.
*
* Returns: (transfer full): a new #MatrixFilter
*/
MatrixFilter *
matrix_filter_new(void)
{
MatrixFilter *filter;
filter = g_new0(MatrixFilter, 1);
filter->refcount = 1;
return filter;
}
static void
matrix_filter_free(MatrixFilter *filter)
{
g_list_free_full(filter->event_fields, g_free);
if (filter->presence) {
matrix_filter_rules_unref(filter->presence);
}
if (filter->room) {
matrix_room_filter_unref(filter->room);
}
g_free(filter);
}
/**
* matrix_filter_ref:
* @filter: a #MatrixFilter
*
* Increase reference count of @filter by one.
*
* Returns: (transfer none): the same #MatrixFilter
*/
MatrixFilter *
matrix_filter_ref(MatrixFilter *filter)
{
filter->refcount++;
return filter;
}
/**
* matrix_filter_unref:
* @filter: a #MatrixFilter
*
* Decrease reference count of @filter by one. If reference count
* reaches zero, @filter is freed.
*/
void
matrix_filter_unref(MatrixFilter *filter)
{
if (--filter->refcount == 0) {
matrix_filter_free(filter);
}
}
/**
* matrix_filter_set_event_fields:
* @filter: a #MatrixFilter
* @event_fields: (in) (element-type utf8) (transfer full) (allow-none):
* a list of event fields to include. If %NULL then all
* fields are included. The entries may include
* <code>.</code> charaters to indicate sub-fields. So
* <code>['content.body']</code> will include the
* <code>body</code> field of the <code>content</code>
* object. A literal <code>.</code> character in a
* field name may be escaped using a <code>\</code>. A
* server may include more fields than were requested
*
* Set the event fields to include in the filtered events.
*/
void
matrix_filter_set_event_fields(MatrixFilter *filter, GList *event_fields)
{
g_list_free_full(filter->event_fields, g_free);
filter->event_fields = event_fields;
}
/**
* matrix_filter_add_event_field:
* @filter: a #MatrixFilter
* @event_field: an event field to add to the list. See
* matrix_filter_set_event_fields() for details
*
* Add an event field to the list of fields that will be present in
* the filtered events.
*/
void
matrix_filter_add_event_field(MatrixFilter *filter, const gchar *event_field)
{
g_return_if_fail(event_field != NULL);
if (!g_list_find_custom(filter->event_fields, event_field,
(GCompareFunc)g_strcmp0)) {
filter->event_fields = g_list_prepend(filter->event_fields,
g_strdup(event_field));
}
}
/**
* matrix_filter_delete_event_field:
* @filter: a #MatrixFilter
* @event_field: an event field to remove from the list
*
* Remove @event_field from the list of fields that will be present in
* the filtered events.
*/
void
matrix_filter_delete_event_field(MatrixFilter *filter, const gchar *event_field)
{
GList *event_field_element;
g_return_if_fail(event_field != NULL);
while ((event_field_element = g_list_find_custom(filter->event_fields,
event_field,
(GCompareFunc)g_strcmp0))) {
filter->event_fields = g_list_remove_link(filter->event_fields,
event_field_element);
g_list_free_full(event_field_element, g_free);
}
}
/**
* matrix_filter_get_event_fields:
* @filter: a #MatrixFilter
*
* Get the list of event fields that will be present in the filtered
* events.
*
* Returns: (transfer none) (element-type utf8) (allow-none): the list
* of event fields. The returned value is owned by @filter
* and should not be freed nor modified.
*/
const GList *
matrix_filter_get_event_fields(MatrixFilter *filter)
{
return filter->event_fields;
}
/**
* matrix_filter_set_event_format:
* @filter: a #MatrixFilter
* @event_format: the desired event format for filtered events
*
* Set the desired event format for the filtered events (e.g. for
* matrix_api_sync())
*/
void
matrix_filter_set_event_format(MatrixFilter *filter,
MatrixEventFormat event_format)
{
filter->event_format = event_format;
}
/**
* matrix_filter_get_event_format:
* @filter: a #MatrixFilter
*
* Get the desired event format set in @filter.
*
* Returns: the event format currently set
*/
MatrixEventFormat
matrix_filter_get_event_format(MatrixFilter *filter)
{
return filter->event_format;
}
/**
* matrix_filter_set_presence_filter:
* @filter: a #MatrixFilter
* @presence_filter: (transfer none): the desired filters to use
*
* Set a filtering ruleset for presence events.
*/
void
matrix_filter_set_presence_filter(MatrixFilter *filter,
MatrixFilterRules *presence_filter)
{
if (filter->presence) {
matrix_filter_rules_unref(filter->presence);
}
filter->presence = matrix_filter_rules_ref(presence_filter);
}
/**
* matrix_filter_get_presence_filter:
* @filter: a #MatrixFilter
*
* Get the current filtering ruleset for presence events.
*
* Returns: (transfer none): the current ruleset. The returned value
* is owned by @filter; if the callee wants to use it
* separately, it should create a reference for it
*/
MatrixFilterRules *
matrix_filter_get_presence_filter(MatrixFilter *filter)
{
return filter->presence;
}
/**
* matrix_filter_set_room_filter:
* @filter: a #MatrixFilter
* @room_filter: the desired room filters to use in @filter
*
* Set a new filtering ruleset for room events in @filter.
*/
void
matrix_filter_set_room_filter(MatrixFilter *filter,
MatrixRoomFilter *room_filter)
{
if (filter->room) {
matrix_room_filter_unref(filter->room);
}
filter->room = matrix_room_filter_ref(room_filter);
}
/**
* matrix_filter_get_room_filter:
* @filter: a #MatrixFilter
*
* Get the filtering ruleset for room events in @filter.
*
* Returns: (transfer none): the current filtering ruleset for room
* events
*/
MatrixRoomFilter *
matrix_filter_get_room_filter(MatrixFilter *filter)
{
return filter->room;
}
/**
* matrix_filter_get_json_node:
* @filter: a #MatrixFilter
*
* Get the JSON representation of @filter as a #JsonNode
*
* Returns: (transfer full): the JSON representation of @filter
*/
JsonNode *
matrix_filter_get_json_node(MatrixFilter *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,
_matrix_g_enum_value_to_nick(
MATRIX_TYPE_EVENT_FORMAT,
filter->event_format,
TRUE));
json_builder_set_member_name(builder, "presence");
tmp = matrix_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_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;
}
/**
* matrix_filter_get_json_data:
* @filter: a #MatrixFilter
* @datalen: storage for the length of the JSON data, or %NULL
*
* Get the JSON representation of @filter as a string.
*
* Returns: (transfer full): the JSON representation of @filter
*/
gchar *
matrix_filter_get_json_data(MatrixFilter *filter, gsize *datalen)
{
JsonGenerator *generator;
JsonNode *node = matrix_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:
*

View File

@ -210,32 +210,6 @@ JsonNode *matrix_room_filter_get_json_node(MatrixRoomFilter *filter);
gchar *matrix_room_filter_get_json_data(MatrixRoomFilter *filter,
gsize *datalen);
typedef struct _MatrixFilter MatrixFilter;
GType matrix_filter_get_type(void);
#define MATRIX_TYPE_FILTER (matrix_filter_get_type())
MatrixFilter *matrix_filter_new(void);
MatrixFilter *matrix_filter_ref(MatrixFilter *filter);
void matrix_filter_unref(MatrixFilter *filter);
void matrix_filter_set_event_fields(MatrixFilter *filter, GList *event_fields);
void matrix_filter_add_event_field(MatrixFilter *filter,
const gchar *event_field);
void matrix_filter_delete_event_field(MatrixFilter *filter,
const gchar *event_field);
const GList *matrix_filter_get_event_fields(MatrixFilter *filter);
void matrix_filter_set_event_format(MatrixFilter *filter,
MatrixEventFormat event_format);
MatrixEventFormat matrix_filter_get_event_format(MatrixFilter *filter);
void matrix_filter_set_presence_filter(MatrixFilter *filter,
MatrixFilterRules *presence_filter);
MatrixFilterRules *matrix_filter_get_presence_filter(MatrixFilter *filter);
void matrix_filter_set_room_filter(MatrixFilter *filter,
MatrixRoomFilter *room_filter);
MatrixRoomFilter *matrix_filter_get_room_filter(MatrixFilter *filter);
JsonNode *matrix_filter_get_json_node(MatrixFilter *filter);
gchar *matrix_filter_get_json_data(MatrixFilter *filter, gsize *datalen);
typedef struct _Matrix3PidCredential Matrix3PidCredential;
GType matrix_3pid_credential_get_type(void);