From 8831f808bb81b085afa843dff85ae816d16cc3dc Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 28 Feb 2018 11:11:00 +0100 Subject: [PATCH] Create setter for MatrixAPI.user_id and MatrixAPI.homeserver --- .../matrix-glib-sdk-sections.txt | 2 ++ src/matrix-api.c | 33 +++++++++++++++++++ src/matrix-api.h | 4 +++ src/matrix-http-api.c | 33 +++++++++++-------- src/matrix-http-api.h | 3 -- src/matrix-http-client.c | 10 +++--- 6 files changed, 63 insertions(+), 22 deletions(-) diff --git a/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt b/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt index 1944908..513628c 100644 --- a/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt +++ b/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt @@ -71,7 +71,9 @@ matrix_api_media_upload matrix_api_get_token matrix_api_set_token matrix_api_get_user_id +matrix_api_set_user_id matrix_api_get_homeserver +matrix_api_set_homeserver MatrixAPI MATRIX_TYPE_API diff --git a/src/matrix-api.c b/src/matrix-api.c index 159dd54..9d71465 100644 --- a/src/matrix-api.c +++ b/src/matrix-api.c @@ -101,7 +101,9 @@ * @get_token: the virtual function pointer to matrix_api_get_token() * @set_token: the virtual function pointer to matrix_api_set_token() * @get_user_id: the virtual function pointer to matrix_api_get_user_id() + * @set_user_id: the virtual function pointer to matrix_api_set_user_id() * @get_homeserver: the virtual function pointer to matrix_api_get_homeserver() + * @set_homeserver: the virtual function pointer to matrix_api_set_homeserver() * * The virtual function table for for #MatrixAPI. */ @@ -1935,6 +1937,21 @@ matrix_api_get_user_id(MatrixAPI *matrix_api) return MATRIX_API_GET_IFACE(matrix_api)->get_user_id(matrix_api); } +/** + * matrix_api_set_user_id: + * @api: a #MatrixAPI + * @user_id: (nullable): a Matrix ID + * + * Set the user ID of the user logged in using @api. This might be useful for restoring state. + */ +void +matrix_api_set_user_id(MatrixAPI *matrix_api, const gchar *user_id) +{ + g_return_if_fail(matrix_api != NULL); + + MATRIX_API_GET_IFACE(matrix_api)->set_user_id(matrix_api, user_id); +} + /** * matrix_api_get_homeserver: * @api: a #MatrixAPI @@ -1953,6 +1970,22 @@ matrix_api_get_homeserver(MatrixAPI *matrix_api) return MATRIX_API_GET_IFACE(matrix_api)->get_homeserver(matrix_api); } +/** + * matrix_api_set_homeserver: + * @api: a #MatrixAPI + * @homeserver: (nullable): a homeserver name + * + * Set the name of the homeserver, as it calls itself. This might be useful for + * restoring state. + */ +void +matrix_api_set_homeserver(MatrixAPI *api, const gchar *homeserver) +{ + g_return_if_fail(api != NULL); + + MATRIX_API_GET_IFACE(api)->set_homeserver(api, homeserver); +} + static void matrix_api_default_init(MatrixAPIInterface *iface) { diff --git a/src/matrix-api.h b/src/matrix-api.h index 6e783d8..2eed942 100644 --- a/src/matrix-api.h +++ b/src/matrix-api.h @@ -456,7 +456,9 @@ struct _MatrixAPIInterface { const gchar *(*get_token)(MatrixAPI *api); void (*set_token)(MatrixAPI *api, const gchar *token); const gchar *(*get_user_id)(MatrixAPI *api); + void (*set_user_id)(MatrixAPI *api, const gchar *user_id); const gchar *(*get_homeserver)(MatrixAPI *api); + void (*set_homeserver)(MatrixAPI *api, const gchar *homeserver); }; void matrix_api_abort_pending(MatrixAPI *api); @@ -875,7 +877,9 @@ void matrix_api_media_upload(MatrixAPI *api, const gchar *matrix_api_get_token(MatrixAPI *api); void matrix_api_set_token(MatrixAPI *api, const gchar *token); const gchar *matrix_api_get_user_id(MatrixAPI *api); +void matrix_api_set_user_id(MatrixAPI *api, const gchar *user_id); const gchar *matrix_api_get_homeserver(MatrixAPI *api); +void matrix_api_set_homeserver(MatrixAPI *api, const gchar *homeserver); G_END_DECLS diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index b204902..93e65c3 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -51,6 +51,8 @@ typedef struct { SoupURI *api_uri; SoupURI *media_uri; gchar *token; + gchar *homeserver; + gchar *user_id; } MatrixHTTPAPIPrivate; static void matrix_http_api_matrix_api_interface_init(MatrixAPIInterface * iface); @@ -216,8 +218,8 @@ _matrix_http_api_response_callback(SoupSession *session, SoupMessage *msg, gpoin g_debug("Our home server calls itself %s", homeserver); #endif - g_free(matrix_http_api->_homeserver); - matrix_http_api->_homeserver = g_strdup(homeserver); + g_free(priv->homeserver); + priv->homeserver = g_strdup(homeserver); } /* Check if the response holds a user ID; if it does, @@ -229,8 +231,8 @@ _matrix_http_api_response_callback(SoupSession *session, SoupMessage *msg, gpoin g_debug("We are reported to be logged in as %s", user_id); #endif - g_free(matrix_http_api->_user_id); - matrix_http_api->_user_id = g_strdup(user_id); + g_free(priv->user_id); + priv->user_id = g_strdup(user_id); } /* Check if the response holds an error */ @@ -2231,11 +2233,11 @@ matrix_http_api_set_base_url(MatrixHTTPAPI *matrix_http_api, const gchar *base_u priv->base_url = g_strdup(base_url); g_free(priv->token); - g_free(matrix_http_api->_homeserver); - g_free(matrix_http_api->_user_id); priv->token = NULL; - matrix_http_api->_homeserver = NULL; - matrix_http_api->_user_id = NULL; + g_free(priv->homeserver); + g_free(priv->user_id); + priv->homeserver = NULL; + priv->user_id = NULL; #if DEBUG gchar *uri; @@ -2287,7 +2289,9 @@ matrix_http_api_set_validate_certificate(MatrixHTTPAPI *matrix_http_api, gboolea static const gchar * matrix_http_api_get_user_id (MatrixAPI *api) { - return MATRIX_HTTP_API(matrix_api)->_user_id; + MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(api)); + + return priv->user_id; } static const gchar * @@ -2313,13 +2317,14 @@ matrix_http_api_set_token(MatrixAPI *matrix_api, const gchar *token) static const gchar * matrix_http_api_get_homeserver(MatrixAPI *api) { - return MATRIX_HTTP_API(api)->_homeserver; + MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(api)); + + return priv->homeserver; } static void matrix_http_api_finalize(GObject *gobject) { - MatrixHTTPAPI *matrix_http_api = MATRIX_HTTP_API(gobject); MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(gobject)); g_object_unref(priv->soup_session); @@ -2333,9 +2338,9 @@ matrix_http_api_finalize(GObject *gobject) soup_uri_free(priv->media_uri); } - g_free(matrix_http_api->_user_id); g_free(priv->token); - g_free(matrix_http_api->_homeserver); + g_free(priv->user_id); + g_free(priv->homeserver); G_OBJECT_CLASS(matrix_http_api_parent_class)->finalize(gobject); } @@ -2531,4 +2536,6 @@ matrix_http_api_init(MatrixHTTPAPI *matrix_http_api) priv->api_uri = NULL; priv->media_uri = NULL; priv->token = NULL; + priv->homeserver = NULL; + priv->user_id = NULL; } diff --git a/src/matrix-http-api.h b/src/matrix-http-api.h index e79c3dc..c1a74ed 100644 --- a/src/matrix-http-api.h +++ b/src/matrix-http-api.h @@ -37,9 +37,6 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(MatrixHTTPAPI, g_object_unref) struct _MatrixHTTPAPI { GObject parent_instance; - - gchar *_homeserver; - gchar *_user_id; }; struct _MatrixHTTPAPIClass { diff --git a/src/matrix-http-client.c b/src/matrix-http-client.c index b1f76ab..16960b5 100644 --- a/src/matrix-http-client.c +++ b/src/matrix-http-client.c @@ -802,20 +802,18 @@ matrix_http_client_real_load_state(MatrixClient *matrix_client, const gchar *fil matrix_http_api_set_validate_certificate(MATRIX_HTTP_API(matrix_client), json_node_get_boolean(node)); if ((node = json_object_get_member(root, "user_id")) != NULL) { - g_free(MATRIX_HTTP_API(matrix_client)->_user_id); - MATRIX_HTTP_API(matrix_client)->_user_id = g_strdup(json_node_get_string(node)); + matrix_api_set_user_id(MATRIX_API(matrix_client), json_node_get_string(node)); #if DEBUG - g_debug("Loaded user ID %s", MATRIX_HTTP_API(matrix_client)->_user_id); + g_debug("Loaded user ID %s", matrix_api_get_user_id(MATRIX_API(matrix_client))); #endif } if ((node = json_object_get_member(root, "homeserver_name")) != NULL) { - g_free(MATRIX_HTTP_API(matrix_client)->_homeserver); - MATRIX_HTTP_API(matrix_client)->_homeserver = g_strdup(json_node_get_string(node)); + matrix_api_set_homeserver(MATRIX_API(matrix_client), json_node_get_string(node)); #if DEBUG - g_debug("Loaded homeserver name %s", MATRIX_HTTP_API(matrix_client)->_homeserver); + g_debug("Loaded homeserver name %s", matrix_api_get_homeserver(MATRIX_API(matrix_client))); #endif }