summaryrefslogtreecommitdiff
path: root/pn_session.c
blob: 2ec58b7b66f5ec8e3231d1349c311e0ebad6aaea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#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;
}