matrix-glib-sdk/src/matrix-client.c

178 lines
4.8 KiB
C
Raw Normal View History

2015-12-10 12:54:15 +00:00
/*
* This file is part of matrix-glib-sdk
*
* matrix-glib-sdk is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* matrix-glib-sdk is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with matrix-glib-sdk. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "matrix-client.h"
#include "matrix-api.h"
/**
* SECTION:matrix-client
* @short_description: Base class for communication with a Matrix.org server
* @title: MatrixClient
* @stability: Unstable
* @include: matrix-glib/matrix.h
*
* This is the base class for client communication with a Matrix.org server.
*/
/**
* MatrixClient:
*
* The MatrixClient objects instance definition.
*/
/**
* MatrixClientClass:
* @parent_class: the parent class structure (#GObjectClass)
*
* The MatrixClient objects class definition.
*/
typedef struct _MatrixClientPrivate {
MatrixAPI *api;
} MatrixClientPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(MatrixClient, matrix_client, G_TYPE_OBJECT);
enum {
PROP_HOMESERVER = 1,
PROP_TOKEN,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
static void
matrix_client_finalize(GObject *gobject)
{
g_signal_handlers_destroy(gobject);
G_OBJECT_CLASS(matrix_client_parent_class)->finalize(gobject);
}
static void
matrix_client_dispose(GObject *gobject)
{
MatrixClientPrivate *priv = matrix_client_get_instance_private(
MATRIX_CLIENT(gobject));
g_clear_object(&priv->api);
G_OBJECT_CLASS(matrix_client_parent_class)->dispose(gobject);
}
static void
matrix_client_set_property(GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_HOMESERVER:
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
break;
}
}
static void
matrix_client_get_property(GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id) {
case PROP_HOMESERVER:
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
break;
}
}
static void
matrix_client_class_init(MatrixClientClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->set_property = matrix_client_set_property;
gobject_class->get_property = matrix_client_get_property;
gobject_class->finalize = matrix_client_finalize;
gobject_class->dispose = matrix_client_dispose;
/**
* MatrixClient:homeserver:
*
* The address of the home server to connect to.
*/
obj_properties[PROP_HOMESERVER] = g_param_spec_string(
"homeserver", "Home server",
"Matrix.org home server to connect to",
NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* MatrixClient:token:
*
* The token to use for authorization.
*/
obj_properties[PROP_TOKEN] = g_param_spec_string(
"token", "Token",
"Authentication token to use",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(gobject_class,
N_PROPERTIES,
obj_properties);
}
static void
matrix_client_init(MatrixClient *client)
{
MatrixClientPrivate *priv = matrix_client_get_instance_private(client);
priv->api = NULL;
}
/**
* matrix_client_new:
* @homeserver: the home server to connect to
* @token: (allow-none): the authentication token to use
*
* Creates a new #MatrixClient instance for the specified home
* server. The token, if specified, will be used for authorization
* throughout communication with that server. The token gets
* autogenerated by matrix_client_login_password() and
* matrix_client_register_password(). No other communication is
* allowed with the server before the token is set.
*
* Returns: (transfer full): a new #MatrixClient instance
*/
MatrixClient *
matrix_client_new(const gchar *homeserver, const gchar *token)
{
return g_object_new(MATRIX_TYPE_CLIENT,
"homeserver", homeserver,
"token", token,
NULL);
}