Add MatrixAPI:homeserver property

This commit is contained in:
Gergely Polonkai 2016-01-06 14:10:35 +01:00
parent f6fa56e7b0
commit 95a6112a2a
4 changed files with 65 additions and 0 deletions

View File

@ -35,6 +35,7 @@ matrix_api_get_token
matrix_api_set_refresh_token
matrix_api_get_refresh_token
matrix_api_get_user_id
matrix_api_get_homeserver
<SUBSECTION>
matrix_api_media_download

View File

@ -465,6 +465,19 @@ matrix_api_default_init(MatrixAPIInterface *iface)
"The Matrix user ID that is authenticated to the server",
NULL,
G_PARAM_READABLE));
/**
* MatrixAPI:homeserver:
*
* The Matrix home server, as it calls itself. It is filled
* automatically by login and register calls.
*/
g_object_interface_install_property(
iface,
g_param_spec_string("homeserver", "Homeserver",
"The home server, as it calls itself",
NULL,
G_PARAM_READABLE));
}
/* Property getters and setters */
@ -572,6 +585,28 @@ matrix_api_get_user_id(MatrixAPI *api)
->get_user_id(api);
}
/**
* matrix_api_get_homeserver:
* @api: a #MatrixAPI implementation
*
* Get the homeserver's name, as it calls itself. It gets set
* automatically by login and register calls, e.g. matrix_api_login()
* or matrix_api_register_account().
*
* Returns: (transfer none) (allow-none): the Matrix homeserver's
* name, as it calls itself. If no homeserver name is reported yet
* (e.g. because login or register wasn't called yet), this function
* returns %NULL.
*/
const gchar *
matrix_api_get_homeserver(MatrixAPI *api)
{
g_return_if_fail(MATRIX_IS_API(api));
return MATRIX_API_GET_IFACE(api)
->get_homeserver(api);
}
/* Media */
/**

View File

@ -176,6 +176,8 @@ struct _MatrixAPIInterface {
const gchar *(*get_user_id)(MatrixAPI *api);
const gchar *(*get_homeserver)(MatrixAPI *api);
void *properties_reserved[10];
/* Media */
@ -589,6 +591,7 @@ const gchar *matrix_api_get_token(MatrixAPI *api);
void matrix_api_set_refresh_token(MatrixAPI *api, const gchar *refresh_token);
const gchar *matrix_api_get_refresh_token(MatrixAPI *api);
const gchar *matrix_api_get_user_id(MatrixAPI *api);
const gchar *matrix_api_get_homeserver(MatrixAPI *api);
/* API definition */

View File

@ -54,6 +54,7 @@ typedef struct _MatrixHTTPAPIPrivate {
gchar *token;
gchar *refresh_token;
gchar *user_id;
gchar *homeserver;
gboolean validate_certificate;
} MatrixHTTPAPIPrivate;
@ -63,6 +64,7 @@ enum {
PROP_TOKEN,
PROP_REFRESH_TOKEN,
PROP_USER_ID,
PROP_HOMESERVER,
N_PROPERTIES
};
@ -81,6 +83,7 @@ static const gchar *i_get_token(MatrixAPI *api);
static void i_set_refresh_token(MatrixAPI *api, const gchar *refresh_token);
static const gchar *i_get_refresh_token(MatrixAPI *api);
static const gchar *i_get_user_id(MatrixAPI *api);
static const gchar *i_get_homeserver(MatrixAPI *api);
G_DEFINE_TYPE_WITH_CODE(MatrixHTTPAPI, matrix_http_api, G_TYPE_OBJECT,
G_ADD_PRIVATE(MatrixHTTPAPI)
@ -96,6 +99,7 @@ matrix_http_api_finalize(GObject *gobject)
g_free(priv->token);
g_free(priv->refresh_token);
g_free(priv->user_id);
g_free(priv->homeserver);
g_signal_handlers_destroy(gobject);
G_OBJECT_CLASS(matrix_http_api_parent_class)->finalize(gobject);
@ -223,6 +227,11 @@ matrix_http_api_get_property(GObject *gobject,
break;
case PROP_HOMESERVER:
g_value_set_string(value, i_get_homeserver(MATRIX_API(api)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
@ -274,6 +283,9 @@ matrix_http_api_class_init(MatrixHTTPAPIClass *klass)
PROP_REFRESH_TOKEN,
"refresh-token");
g_object_class_override_property(gobject_class, PROP_USER_ID, "user-id");
g_object_class_override_property(gobject_class,
PROP_HOMESERVER,
"homeserver");
}
static void
@ -285,6 +297,7 @@ matrix_http_api_init(MatrixHTTPAPI *api)
priv->token = NULL;
priv->refresh_token = NULL;
priv->user_id = NULL;
priv->homeserver = NULL;
priv->validate_certificate = TRUE;
priv->soup_session = soup_session_new();
}
@ -354,6 +367,15 @@ i_get_user_id(MatrixAPI *api)
return priv->user_id;
}
static const gchar *
i_get_homeserver(MatrixAPI *api)
{
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(
MATRIX_HTTP_API(api));
return priv->homeserver;
}
/**
* matrix_http_api_set_validate_certificate:
* @api: a #MatrixHTTPAPI implementation
@ -454,6 +476,9 @@ _response_callback(SoupSession *session,
root_object, "home_server")) != NULL) {
const gchar *homeserver = json_node_get_string(node);
g_free(priv->homeserver);
priv->user_id = g_strdup(homeserver);
g_debug("Our home server calls itself %s", homeserver);
}
@ -631,5 +656,6 @@ matrix_http_api_matrix_api_init(MatrixAPIInterface *iface)
iface->set_refresh_token = i_set_refresh_token;
iface->get_refresh_token = i_get_refresh_token;
iface->get_user_id = i_get_user_id;
iface->get_homeserver = i_get_homeserver;
iface->login = i_login;
}