Rework StateEvent in Vala
This commit is contained in:
parent
6bf5515693
commit
a8da282d0b
@ -40,9 +40,4 @@ namespace Matrix {
|
|||||||
UNKNOWN_ERROR;
|
UNKNOWN_ERROR;
|
||||||
public static GLib.Quark quark ();
|
public static GLib.Quark quark ();
|
||||||
}
|
}
|
||||||
|
|
||||||
[CCode (cheader_filename = "matrix-types.h")]
|
|
||||||
public class StateEvent {
|
|
||||||
public Json.Node? get_json_node();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -652,6 +652,30 @@ namespace Matrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class StateEvent : JsonCompact {
|
||||||
|
public string? state_key { get; set; default = null; }
|
||||||
|
public Json.Node? content { get; set; default = null; }
|
||||||
|
|
||||||
|
public override Json.Node?
|
||||||
|
get_json_node()
|
||||||
|
throws Matrix.Error
|
||||||
|
{
|
||||||
|
var builder = new Json.Builder();
|
||||||
|
|
||||||
|
builder.begin_object();
|
||||||
|
|
||||||
|
builder.set_member_name("state_key");
|
||||||
|
builder.add_string_value(state_key);
|
||||||
|
|
||||||
|
builder.set_member_name("content");
|
||||||
|
builder.add_value(content);
|
||||||
|
|
||||||
|
builder.end_object();
|
||||||
|
|
||||||
|
return builder.get_root();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Json.Node?
|
private Json.Node?
|
||||||
_json_node_deep_copy(Json.Node? node)
|
_json_node_deep_copy(Json.Node? node)
|
||||||
{
|
{
|
||||||
|
@ -797,7 +797,9 @@ public class Matrix.HTTPAPI : GLib.Object, Matrix.API {
|
|||||||
builder.begin_array();
|
builder.begin_array();
|
||||||
initial_state.foreach(
|
initial_state.foreach(
|
||||||
(entry) => {
|
(entry) => {
|
||||||
builder.add_value(entry.get_json_node());
|
try {
|
||||||
|
builder.add_value(entry.get_json_node());
|
||||||
|
} catch (Matrix.Error e) {}
|
||||||
});
|
});
|
||||||
builder.end_array();
|
builder.end_array();
|
||||||
}
|
}
|
||||||
|
@ -109,238 +109,3 @@
|
|||||||
* Gets the Matrix error #GQuark
|
* Gets the Matrix error #GQuark
|
||||||
*/
|
*/
|
||||||
G_DEFINE_QUARK(matrix-error-quark, matrix_error);
|
G_DEFINE_QUARK(matrix-error-quark, matrix_error);
|
||||||
|
|
||||||
/**
|
|
||||||
* MatrixStateEvent:
|
|
||||||
*
|
|
||||||
* An opaque structure to hold a state event filter.
|
|
||||||
*/
|
|
||||||
struct _MatrixStateEvent {
|
|
||||||
gchar *type;
|
|
||||||
gchar *state_key;
|
|
||||||
JsonNode *content;
|
|
||||||
guint refcount;
|
|
||||||
};
|
|
||||||
|
|
||||||
G_DEFINE_BOXED_TYPE(MatrixStateEvent, matrix_state_event,
|
|
||||||
(GBoxedCopyFunc)matrix_state_event_ref,
|
|
||||||
(GBoxedFreeFunc)matrix_state_event_unref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_new:
|
|
||||||
*
|
|
||||||
* Create a new #MatrixStateEvent object with reference count of 1.
|
|
||||||
*
|
|
||||||
* Returns: (transfer full): a new #MatrixStateEvent
|
|
||||||
*/
|
|
||||||
MatrixStateEvent *
|
|
||||||
matrix_state_event_new(void)
|
|
||||||
{
|
|
||||||
MatrixStateEvent *event;
|
|
||||||
|
|
||||||
event = g_new0(MatrixStateEvent, 1);
|
|
||||||
event->refcount = 1;
|
|
||||||
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
matrix_state_event_free(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
g_free(event->type);
|
|
||||||
g_free(event->state_key);
|
|
||||||
|
|
||||||
if (event->content) {
|
|
||||||
json_node_free(event->content);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_ref:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Increase reference count of @event by one.
|
|
||||||
*
|
|
||||||
* Returns: (transfer none): the same #MatrixStateEvent
|
|
||||||
*/
|
|
||||||
MatrixStateEvent *
|
|
||||||
matrix_state_event_ref(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
event->refcount++;
|
|
||||||
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_unref:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Decrease reference count of @event by one. If reference count
|
|
||||||
* reaches zero, @event is freed.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
matrix_state_event_unref(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
if (--event->refcount == 0) {
|
|
||||||
matrix_state_event_free(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_set_event_type:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
* @event_type: the type of the state event
|
|
||||||
*
|
|
||||||
* Set the type of the state event in @event.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
matrix_state_event_set_event_type(MatrixStateEvent *event,
|
|
||||||
const gchar *event_type)
|
|
||||||
{
|
|
||||||
g_free(event->type);
|
|
||||||
event->type = g_strdup(event_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_get_event_type:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Get the type of the state event in @event.
|
|
||||||
*
|
|
||||||
* Returns: (transfer none): the event type. The returned value is
|
|
||||||
* owned by @event and should not be freed nor modified.
|
|
||||||
*/
|
|
||||||
const gchar *
|
|
||||||
matrix_state_event_get_event_type(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
return event->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_set_state_key:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
* @state_key: the key of the state event
|
|
||||||
*
|
|
||||||
* Set the state key for the state event @event.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
matrix_state_event_set_state_key(MatrixStateEvent *event,
|
|
||||||
const gchar *state_key)
|
|
||||||
{
|
|
||||||
g_free(event->state_key);
|
|
||||||
event->state_key = g_strdup(state_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_get_state_key:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Get the state key of @event.
|
|
||||||
*
|
|
||||||
* Returns: (transfer none): the state key. The returned value is
|
|
||||||
* owned by @event and should not be freed nor modified
|
|
||||||
*/
|
|
||||||
const gchar *
|
|
||||||
matrix_state_event_get_state_key(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
return event->state_key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_set_content:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
* @content: the desired content of the state event
|
|
||||||
*
|
|
||||||
* Set the content of the state event.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
matrix_state_event_set_content(MatrixStateEvent *event, const JsonNode *content)
|
|
||||||
{
|
|
||||||
if (event->content) {
|
|
||||||
json_node_free(event->content);
|
|
||||||
}
|
|
||||||
|
|
||||||
event->content = json_node_copy((JsonNode *)content);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_get_content:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Get the contents of the state event.
|
|
||||||
*
|
|
||||||
* Returns: (transfer none): the contents of the state event. The
|
|
||||||
* returned value is owned by @event and should not be freed
|
|
||||||
* nor modified
|
|
||||||
*/
|
|
||||||
const JsonNode *
|
|
||||||
matrix_state_event_get_content(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
return event->content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_get_json_node:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
*
|
|
||||||
* Get the JSON representation of the state event as a #JsonNode
|
|
||||||
* object.
|
|
||||||
*
|
|
||||||
* Returns: (transfer full): the JSON representation of the state
|
|
||||||
* event
|
|
||||||
*/
|
|
||||||
JsonNode *
|
|
||||||
matrix_state_event_get_json_node(MatrixStateEvent *event)
|
|
||||||
{
|
|
||||||
JsonBuilder *builder;
|
|
||||||
JsonNode *node;
|
|
||||||
|
|
||||||
builder = json_builder_new();
|
|
||||||
json_builder_begin_object(builder);
|
|
||||||
|
|
||||||
json_builder_set_member_name(builder, "state_key");
|
|
||||||
json_builder_add_string_value(builder, event->state_key);
|
|
||||||
|
|
||||||
json_builder_set_member_name(builder, "type");
|
|
||||||
json_builder_add_string_value(builder, event->type);
|
|
||||||
|
|
||||||
json_builder_set_member_name(builder, "content");
|
|
||||||
json_builder_add_value(builder, event->content);
|
|
||||||
|
|
||||||
json_builder_end_object(builder);
|
|
||||||
|
|
||||||
node = json_builder_get_root(builder);
|
|
||||||
g_object_unref(builder);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* matrix_state_event_get_json_data:
|
|
||||||
* @event: a #MatrixStateEvent
|
|
||||||
* @datalen: (out): storage for the the length of the JSON data or
|
|
||||||
* %NULL
|
|
||||||
*
|
|
||||||
* Get the JSON representation of the state event as a string.
|
|
||||||
*
|
|
||||||
* Returns: (transfer full): the JSON representation of the state
|
|
||||||
* event
|
|
||||||
*/
|
|
||||||
gchar *
|
|
||||||
matrix_state_event_get_json_data(MatrixStateEvent *event, gsize *datalen)
|
|
||||||
{
|
|
||||||
JsonGenerator *generator;
|
|
||||||
JsonNode *node = matrix_state_event_get_json_node(event);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
@ -72,27 +72,6 @@ typedef enum {
|
|||||||
#define MATRIX_ERROR matrix_error_quark()
|
#define MATRIX_ERROR matrix_error_quark()
|
||||||
GQuark matrix_error_quark(void);
|
GQuark matrix_error_quark(void);
|
||||||
|
|
||||||
typedef struct _MatrixStateEvent MatrixStateEvent;
|
|
||||||
|
|
||||||
GType matrix_state_event_get_type(void);
|
|
||||||
#define MATRIX_TYPE_STATE_EVENT (matrix_state_event_get_type())
|
|
||||||
|
|
||||||
MatrixStateEvent *matrix_state_event_new(void);
|
|
||||||
MatrixStateEvent *matrix_state_event_ref(MatrixStateEvent *event);
|
|
||||||
void matrix_state_event_unref(MatrixStateEvent *event);
|
|
||||||
void matrix_state_event_set_event_type(MatrixStateEvent *event,
|
|
||||||
const gchar *event_type);
|
|
||||||
const gchar *matrix_state_event_get_event_type(MatrixStateEvent *event);
|
|
||||||
void matrix_state_event_set_state_key(MatrixStateEvent *event,
|
|
||||||
const gchar *state_key);
|
|
||||||
const gchar *matrix_state_event_get_state_key(MatrixStateEvent *event);
|
|
||||||
void matrix_state_event_set_content(MatrixStateEvent *event,
|
|
||||||
const JsonNode *content);
|
|
||||||
const JsonNode *matrix_state_event_get_content(MatrixStateEvent *event);
|
|
||||||
JsonNode *matrix_state_event_get_json_node(MatrixStateEvent *event);
|
|
||||||
gchar *matrix_state_event_get_json_data(MatrixStateEvent *event,
|
|
||||||
gsize *datalen);
|
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __MATRIX_TYPES_H__ */
|
#endif /* __MATRIX_TYPES_H__ */
|
||||||
|
Loading…
Reference in New Issue
Block a user