#include "pn_session.h" #include "pn_ns.h" static void *parent_class; struct pn_session_priv { char *username; char *password; struct pn_node *ns; }; enum { PROP_USERNAME = 1, PROP_PASSWORD, }; struct pn_session * pn_session_new(const char *username, const char *password) { return g_object_new(PN_SESSION_TYPE, "username", username, "password", password, NULL); } void pn_session_free(struct pn_session *session) { if (!session) return; g_object_unref(session); } void pn_session_connect(struct pn_session *session, const char *host, int port) { pn_node_connect(session->priv->ns, host, port); } void pn_session_disconnect(struct pn_session *session) { pn_node_close(session->priv->ns); } const char * pn_session_get_username(struct pn_session *session) { return session->priv->username; } /* GObject stuff */ static void instance_init(GTypeInstance *instance, void *g_class) { struct pn_session *self = PN_SESSION(instance); struct pn_session_priv *priv; self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(instance, PN_SESSION_TYPE, struct pn_session_priv); priv->ns = PN_NODE(pn_ns_new(self)); } static void get_property(GObject *object, unsigned property_id, GValue *value, GParamSpec *spec) { struct pn_session *self = PN_SESSION(object); struct pn_session_priv *priv = self->priv; switch (property_id) { case PROP_USERNAME: g_value_set_string(value, priv->username); break; case PROP_PASSWORD: g_value_set_string(value, priv->password); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, spec); } } static void set_property(GObject *object, unsigned property_id, const GValue *value, GParamSpec *spec) { struct pn_session *self = PN_SESSION(object); struct pn_session_priv *priv = self->priv; switch (property_id) { case PROP_USERNAME: g_free(priv->username); priv->username = g_utf8_strdown(g_value_get_string(value), -1); break; case PROP_PASSWORD: g_free(priv->password); priv->password = g_strdup(g_value_get_string(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, spec); } } static void finalize(GObject *obj) { struct pn_session *self = PN_SESSION(obj); struct pn_session_priv *priv = self->priv; g_object_unref(priv->ns); g_free(priv->username); g_free(priv->password); G_OBJECT_CLASS(parent_class)->finalize(obj); } static void class_init(void *g_class, void *class_data) { GObjectClass *gobject_class = G_OBJECT_CLASS(g_class); gobject_class->finalize = finalize; gobject_class->get_property = get_property; gobject_class->set_property = set_property; { GParamSpec *param_spec; param_spec = g_param_spec_string("username", "Username", "The username", NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); g_object_class_install_property(gobject_class, PROP_USERNAME, param_spec); param_spec = g_param_spec_string("password", "Password", "The password", NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); g_object_class_install_property(gobject_class, PROP_PASSWORD, param_spec); } parent_class = g_type_class_peek_parent(g_class); g_type_class_add_private(g_class, sizeof(struct pn_session_priv)); } GType pn_session_get_type(void) { static gsize init_type; if (g_once_init_enter(&init_type)) { GType type; GTypeInfo type_info = { .class_size = sizeof(struct pn_session_class), .class_init = class_init, .instance_size = sizeof(struct pn_session), .instance_init = instance_init, }; type = g_type_register_static(G_TYPE_OBJECT, "PnSession", &type_info, 0); g_once_init_leave(&init_type, type); } return init_type; }