From ecdd3a7dcdaa76a39394e0c0059afa6814546968 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 19 Feb 2016 11:44:28 +0100 Subject: [PATCH] Rework Matrix3PidCredential in Vala --- src/c-api.vapi | 6 - src/matrix-compacts.vala | 54 +++++++++ src/matrix-types.c | 255 --------------------------------------- src/matrix-types.h | 23 ---- 4 files changed, 54 insertions(+), 284 deletions(-) diff --git a/src/c-api.vapi b/src/c-api.vapi index ba890d7..78e7d8d 100644 --- a/src/c-api.vapi +++ b/src/c-api.vapi @@ -118,12 +118,6 @@ namespace Matrix { public Json.Node? get_json_node(); } - [CCode (cheader_filename = "matrix-types.h")] - public class @3PidCredential { - public Json.Node? get_json_node() - throws Matrix.Error; - } - [CCode (cheader_filename = "utils.h", cname = "_json_node_deep_copy")] public Json.Node? _json_node_deep_copy(Json.Node? node); diff --git a/src/matrix-compacts.vala b/src/matrix-compacts.vala index a573606..6d17529 100644 --- a/src/matrix-compacts.vala +++ b/src/matrix-compacts.vala @@ -358,4 +358,58 @@ namespace Matrix { return builder.get_root(); } } + + /** + * Class to hold 3rd party credential related data. + */ + public class @3PidCredential : JsonCompact { + /** + * The Identity Server used for this credential. + */ + public string? id_server { get; set; default = null; } + + /** + * The session identifier got from the Identity Server. + */ + public string? session_id { get; set; default = null; } + + /** + * The client secret that was used in the session with the + * Identity Server. + */ + public string? client_secret { get; set; default = null; } + + /** + * Get 3rd party credential related data as a JSON node. + */ + public override Json.Node? + get_json_node() + throws Matrix.Error + { + if ((id_server == null) + || (session_id == null) + || (client_secret == null)) + { + throw new Matrix.Error.INCOMPLETE( + "All fields of a 3PID credential must be filled!"); + } + + var builder = new Json.Builder(); + + builder.begin_object(); + + builder.set_member_name("id_server"); + builder.add_string_value(id_server); + + builder.set_member_name("session_id"); + builder.add_string_value(session_id); + + builder.set_member_name("client_secret"); + builder.add_string_value(client_secret); + + builder.end_object(); + + return builder.get_root(); + } + } } diff --git a/src/matrix-types.c b/src/matrix-types.c index 7c9dea7..96ceb22 100644 --- a/src/matrix-types.c +++ b/src/matrix-types.c @@ -247,261 +247,6 @@ G_DEFINE_QUARK(matrix-error-quark, matrix_error); * Receipt types of acknowledgment. */ -/** - * Matrix3PidCredential: - * - * An opaque structure to store credentials to use with Identity - * Server communication. - */ -struct _Matrix3PidCredential { - gchar *id_server; - gchar *session_id; - gchar *client_secret; - guint refcount; -}; - -G_DEFINE_BOXED_TYPE(Matrix3PidCredential, matrix_3pid_credential, - (GBoxedCopyFunc)matrix_3pid_credential_ref, - (GBoxedFreeFunc)matrix_3pid_credential_unref); - -/** - * matrix_3pid_credential_new: - * - * Create a new #Matrix3PidCredential object with reference count - * of 1. - * - * Returns: (transfer full): a new #Matrix3PidCredential - */ -Matrix3PidCredential * -matrix_3pid_credential_new(void) -{ - Matrix3PidCredential *credential; - - credential = g_new0(Matrix3PidCredential, 1); - credential->refcount = 1; - - return credential; -} - -static void -matrix_3pid_credential_free(Matrix3PidCredential *credential) -{ - g_free(credential->id_server); - g_free(credential->session_id); - g_free(credential->client_secret); - g_free(credential); -} - -/** - * matrix_3pid_credential_ref: - * @credential: a #Matrix3PidCredential - * - * Increase reference count of @credential by one. - * - * Returns: (transfer none): the same #Matrix3PidCredential - */ -Matrix3PidCredential * -matrix_3pid_credential_ref(Matrix3PidCredential *credential) -{ - credential->refcount++; - - return credential; -} - -/** - * matrix_3pid_credential_unref: - * @credential: a #Matrix3PidCredential - * - * Decrease reference count of @credential by one. If reference count - * reaches zero, @credential gets freed. - */ -void -matrix_3pid_credential_unref(Matrix3PidCredential *credential) -{ - if (--credential->refcount == 0) { - matrix_3pid_credential_free(credential); - } -} - -/** - * matrix_3pid_credential_set_id_server: - * @credential: a #Matrix3PidCredential - * @id_server: the Identity Server to use - * - * Set the Identity Server to use for this credential. - */ -void -matrix_3pid_credential_set_id_server(Matrix3PidCredential *credential, - const gchar *id_server) -{ - g_free(credential->id_server); - credential->id_server = g_strdup(id_server); -} - -/** - * matrix_3pid_credential_get_id_server: - * @credential: a #Matrix3PidCredential - * - * Get the Identity Server used for this credential. - * - * Returns: the Identity Server's name. The returned value is owned by - * @credential and should not be freed nor modified - */ -const gchar * -matrix_3pid_credential_get_id_server(Matrix3PidCredential *credential) -{ - return credential->id_server; -} - -/** - * matrix_3pid_credential_set_session_id: - * @credential: a #Matrix3PidCredential - * @session_id: the session identifier given by the Identity Server - * - * Set the session identifier got from the Identity Server. - */ -void -matrix_3pid_credential_set_session_id(Matrix3PidCredential *credential, - const gchar *session_id) -{ - g_free(credential->session_id); - credential->session_id = g_strdup(session_id); -} - -/** - * matrix_3pid_credential_get_session_id: - * @credential: a #Matrix3PidCredential - * - * Get the session identifier for this credential. - * - * Returns: the session identifier. The returned value is owned by - * @credential and should not be freed nor modified. - */ -const gchar * -matrix_3pid_credential_get_session_id(Matrix3PidCredential *credential) -{ - return credential->session_id; -} - -/** - * matrix_3pid_credential_set_client_secret: - * @credential: a #Matrix3PidCredential - * @client_secret: the client secret used in the session with the - * Identity Server - * - * Set the client secret that was used in the session with the - * Identity Server. - */ -void -matrix_3pid_credential_set_client_secret(Matrix3PidCredential *credential, - const gchar *client_secret) -{ - g_free(credential->client_secret); - credential->client_secret = g_strdup(client_secret); -} - -/** - * matrix_3pid_credential_get_client_secret: - * @credential: a #Matrix3PidCredential - * - * Get the client secret that was used in the session with the - * Identity Server. - * - * Returns: the client secret. The returned value is owned by - * @credential and should not be freed nor modified. - */ -const gchar * -matrix_3pid_credential_get_client_secret(Matrix3PidCredential *credential) -{ - return credential->client_secret; -} - -/** - * matrix_3pid_credential_get_json_node: - * @credential: a #Matrix3PidCredential - * @error: return location for a #GError, or %NULL - * - * Get the JSON representation of @credential as a #JsonNode. If any - * fields of @credential are empty, it will yield an error, as all - * fields are mandatory. In this case, the return value is %NULL. - * - * Returns: (transfer full) (allow-none): the #JsonNode representation - * of @credential - */ -JsonNode * -matrix_3pid_credential_get_json_node(Matrix3PidCredential *credential, - GError **error) -{ - JsonBuilder *builder; - JsonNode *node; - - if (!credential->id_server - || !credential->session_id - || !credential->client_secret) { - g_set_error(error, - MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, - "All fields of the 3PID credential must be filled!"); - - return NULL; - } - - builder = json_builder_new(); - json_builder_begin_object(builder); - - json_builder_set_member_name(builder, "id_server"); - json_builder_add_string_value(builder, credential->id_server); - - json_builder_set_member_name(builder, "session_id"); - json_builder_add_string_value(builder, credential->session_id); - - json_builder_set_member_name(builder, "client_secret"); - json_builder_add_string_value(builder, credential->client_secret); - - json_builder_end_object(builder); - node = json_builder_get_root(builder); - g_object_unref(builder); - - return node; -} - -/** - * matrix_3pid_credential_get_json_data: - * @credential: a #Matrix3PidCredential - * @datalen: (out): storage location for the length of the JSON data, - * or %NULL - * @error: a #GError - * - * Get the JSON representation of @credential as a string. If any - * fields of @credential is %NULL, this function returns %NULL and - * fills @error wich %MATRIX_ERROR_INCOMPLETE. - * - * Returns: (transfer full) (allow-none): the JSON representation of - * @credential, or %NULL - */ -gchar * -matrix_3pid_credential_get_json_data(Matrix3PidCredential *credential, - gsize *datalen, - GError **error) -{ - JsonGenerator *generator; - JsonNode *node; - gchar *data; - - if ((node = matrix_3pid_credential_get_json_node( - credential, error)) == NULL) { - return NULL; - } - - 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; -} - /** * MatrixPusher: * diff --git a/src/matrix-types.h b/src/matrix-types.h index bb9f312..4ab56c4 100644 --- a/src/matrix-types.h +++ b/src/matrix-types.h @@ -129,29 +129,6 @@ typedef enum { MATRIX_ACCOUNT_KIND_GUEST } MatrixAccountKind; -typedef struct _Matrix3PidCredential Matrix3PidCredential; - -GType matrix_3pid_credential_get_type(void); -#define MATRIX_TYPE_3PID_CREDENTIAL (matrix_3pid_credential_get_type()) - -Matrix3PidCredential *matrix_3pid_credential_new(void); -Matrix3PidCredential *matrix_3pid_credential_ref(Matrix3PidCredential *credential); -void matrix_3pid_credential_unref(Matrix3PidCredential *credential); -void matrix_3pid_credential_set_id_server(Matrix3PidCredential *credential, - const gchar *id_server); -const gchar *matrix_3pid_credential_get_id_server(Matrix3PidCredential *credential); -void matrix_3pid_credential_set_session_id(Matrix3PidCredential *credential, - const gchar *session_id); -const gchar *matrix_3pid_credential_get_session_id(Matrix3PidCredential *credential); -void matrix_3pid_credential_set_client_secret(Matrix3PidCredential *credential, - const gchar *client_secret); -const gchar *matrix_3pid_credential_get_client_secret(Matrix3PidCredential *credential); -JsonNode *matrix_3pid_credential_get_json_node(Matrix3PidCredential *credential, - GError **error); -gchar *matrix_3pid_credential_get_json_data(Matrix3PidCredential *credential, - gsize *datalen, - GError **error); - typedef struct _MatrixPusher MatrixPusher; GType matrix_pusher_get_type(void);