summaryrefslogtreecommitdiff
path: root/docs/examples/glib_telepathy_properties/example.c
blob: 5f6eca41e979ff8f8810e08e619aec94d5a2a658 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <unistd.h>

#include <glib.h>

#include <telepathy-glib/connection-manager.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/channel.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/enums.h>
#include <telepathy-glib/debug.h>

static GMainLoop *loop = NULL;
static TpDBusDaemon *bus_daemon = NULL;
static TpConnection *conn = NULL;

/* begin ex.basics.tpproperties.setup */
typedef struct _TpProperty TpProperty;
struct _TpProperty
{
	guint id;
	char *name;
	guint flags;
};

static GHashTable *
tp_property_get_map (TpProxy *proxy)
{
	g_return_val_if_fail (TP_IS_PROXY (proxy), NULL);

	GHashTable *map = g_object_get_data (G_OBJECT (proxy),
			"tpproperties-map");

	return map;
}

static guint
tp_property_get_id (TpProxy *proxy, const char *name)
{
	GHashTable *map = tp_property_get_map (proxy);

	g_return_val_if_fail (map != NULL, 0);

	return GPOINTER_TO_UINT (g_hash_table_lookup (map, name));
}

static GArray *
tp_property_get_array (TpProxy *proxy)
{
	g_return_val_if_fail (TP_IS_PROXY (proxy), NULL);

	GArray *array = g_object_get_data (G_OBJECT (proxy),
			"tpproperties-array");

	return array;
}

static TpProperty *
tp_property_from_id (TpProxy *proxy, guint id)
{
	GArray *array = tp_property_get_array (proxy);

	g_return_val_if_fail (array != NULL, NULL);

	return &g_array_index (array, TpProperty, id);
}

static void
tp_property_insert (TpProxy *proxy, TpProperty *property)
{
	GArray *array = tp_property_get_array (proxy);
	GHashTable *map = tp_property_get_map (proxy);

	g_array_append_val (array, *property);
	g_hash_table_insert (map, property->name, GUINT_TO_POINTER (property->id));
}

static void
tp_property_init (TpProxy *proxy)
{
	g_return_if_fail (TP_IS_PROXY (proxy));

	GArray *array = g_array_new (FALSE, FALSE,
			sizeof (TpProperty));
	GHashTable *map = g_hash_table_new (g_str_hash, g_str_equal);

	/* FIXME: use g_object_set_data_full() to cleanup on object finalize */
	g_object_set_data (G_OBJECT (proxy), "tpproperties-array", array);
	g_object_set_data (G_OBJECT (proxy), "tpproperties-map", map);
}
/* end ex.basics.tpproperties.setup */

static void
handle_error (const GError *error)
{
	if (error)
	{
		g_print ("ERROR: %s\n", error->message);
		tp_cli_connection_call_disconnect (conn, -1, NULL,
				NULL, NULL, NULL);
	}
}

/* begin ex.basics.tpproperties.changecb */
static void
tp_properties_changed_cb (TpProxy	  *channel,
			  const GPtrArray *properties,
			  gpointer	   user_data,
			  GObject	  *weak_obj)
{
	g_print (" > tp_properties_changed_cb\n");

	int i;
	for (i = 0; i < properties->len; i++)
	{
		GValueArray *property = g_ptr_array_index (properties, i);
		/* the id is a GValue<UINT>
		 * the variant is a GValue<GValue<??> */
		guint id = g_value_get_uint (g_value_array_get_nth (property, 0));
		GValue *value = g_value_get_boxed (g_value_array_get_nth (property, 1));
		TpProperty *tpproperty = tp_property_from_id (TP_PROXY (channel), id);

		/* get a string representation of value */
		char *str = g_strdup_value_contents (value);
		g_print ("Property %s (%i): %s\n", tpproperty->name, id, str);
		g_free (str);
	}
}
/* end ex.basics.tpproperties.changecb */

/* begin ex.basics.tpproperties.flagchangecb */
static void
tp_property_flags_changed_cb (TpProxy		*channel,
			      const GPtrArray	*properties,
			      gpointer		 user_data,
			      GObject		*weak_obj)
{
	g_print (" > tp_property_flags_changed_cb\n");

	int i;
	for (i = 0; i < properties->len; i++)
	{
		GValueArray *property = g_ptr_array_index (properties, i);
		guint id = g_value_get_uint (g_value_array_get_nth (property, 0));
		TpProperty *tpproperty = tp_property_from_id (TP_PROXY (channel), id);

		g_print ("Property %s (%i): %x\n",
			tpproperty->name, id,
			g_value_get_uint (g_value_array_get_nth (property, 1)));
	}
}
/* end ex.basics.tpproperties.flagchangecb */

/* begin ex.basics.tpproperties.getcb */
static void
tp_properties_get_cb (TpProxy		*channel,
		      const GPtrArray	*properties,
		      const GError	*in_error,
		      gpointer		 user_data,
		      GObject		*weak_obj)
{
	handle_error (in_error);

	g_print (" > tp_properties_get_cb\n");

	int i;
	for (i = 0; i < properties->len; i++)
	{
		GValueArray *property = g_ptr_array_index (properties, i);
		/* the id is a GValue<UINT>
		 * the variant is a GValue<GValue<??> */
		guint id = g_value_get_uint (g_value_array_get_nth (property, 0));
		GValue *value = g_value_get_boxed (g_value_array_get_nth (property, 1));
		TpProperty *tpproperty = tp_property_from_id (TP_PROXY (channel), id);

		/* get a string representation of value */
		char *str = g_strdup_value_contents (value);
		g_print ("Property %s (%i): %s\n", tpproperty->name, id, str);
		g_free (str);
	}
}
/* end ex.basics.tpproperties.getcb */

/* begin ex.basics.tpproperties.list */
static void
list_properties_cb (TpProxy		*channel,
		    const GPtrArray	*available_properties,
		    const GError	*in_error,
		    gpointer		 user_data,
		    GObject		*weak_obj)
{
	handle_error (in_error);

	g_print (" > list_properties_cb\n");

	/* @available_properties is a GPtrArray of GValueArray structs
	 * of signature (ussu) */
	int i;
	for (i = 0; i < available_properties->len; i++)
	{
		GValueArray *prop = g_ptr_array_index (available_properties, i);

		guint id = g_value_get_uint (g_value_array_get_nth (prop, 0));
		const char *name = g_value_get_string (g_value_array_get_nth (prop, 1));
		const char *sig = g_value_get_string (g_value_array_get_nth (prop, 2));
		guint flags = g_value_get_uint (g_value_array_get_nth (prop, 3));

		g_print ("%u %s (%s) %x\n", id, name, sig, flags);

		TpProperty property = { 0, };
		property.id = id;
		property.name = g_strdup (name);
		property.flags = flags;
		tp_property_insert (TP_PROXY (channel), &property);
	}

	/* call the chained callback if set */
	if (user_data)
	{
		void (* func) (TpProxy *);

		func = user_data;

		func (channel);
	}
}
/* end ex.basics.tpproperties.list */

static void
tpproperties_ready (TpChannel	*channel)
{
	{ /* pack the readable properties into a GArray */
		/* begin ex.basics.tpproperties.get */
		GArray *array = g_array_new (FALSE, FALSE, sizeof (guint));
		int i;

		g_print ("Read property: ");
		for (i = 0; i < tp_property_get_array (TP_PROXY (channel))->len; i++)
		{
			TpProperty *property = tp_property_from_id (TP_PROXY (channel), i);

			if (!(property->flags & TP_PROPERTY_FLAG_READ)) continue;

			g_print ("%i ", i);
			g_array_append_val (array, i);
		}
		g_print ("\n");

		tp_cli_properties_interface_call_get_properties (channel, -1,
				array, tp_properties_get_cb,
				NULL, NULL, NULL);

		g_array_free (array, TRUE);
		/* end ex.basics.tpproperties.get */
	}

	{ /* set some properties */
		/* begin ex.basics.tpproperties.set */
		GPtrArray *array = g_ptr_array_new_with_free_func (
				(GDestroyNotify) g_value_array_free);
		GValue value = { 0, };

		/* FIXME we're assuming this property exists, we should check */
		guint id = tp_property_get_id (TP_PROXY (channel), "subject");

		g_value_init (&value, G_TYPE_STRING);
		g_value_set_static_string (&value, "Test Subject");

		g_ptr_array_add (array, tp_value_array_build (2,
				G_TYPE_UINT, id,
				G_TYPE_VALUE, &value,
				G_TYPE_INVALID));

		g_value_unset (&value);

		g_print ("Setting properties...\n");
		tp_cli_properties_interface_call_set_properties (channel, -1,
				array, NULL, NULL, NULL, NULL);

		g_ptr_array_free (array, TRUE);
		/* end ex.basics.tpproperties.set */
	}
}

static void
muc_channel_ready (TpChannel	*channel,
		   const GError	*in_error,
		   gpointer	 user_data)
{
	g_print ("MUC channel (%s) ready\n",
			tp_channel_get_identifier (channel));

	/* exciting things about MUC channels are stored as Telepathy
	 * Properties (not D-Bus properties). This interface is a little
	 * awkward.
	 * First we need to get a list of available properties */
	tp_property_init (TP_PROXY (channel));
	tp_cli_properties_interface_call_list_properties (channel, -1,
			list_properties_cb, tpproperties_ready, NULL, NULL);

	GError *error = NULL;

	tp_cli_properties_interface_connect_to_properties_changed (
			channel, tp_properties_changed_cb,
			NULL, NULL, NULL, &error);
	handle_error (error);
	tp_cli_properties_interface_connect_to_property_flags_changed (
			channel, tp_property_flags_changed_cb,
			NULL, NULL, NULL, &error);
	handle_error (error);
}

static void
create_muc_cb (TpConnection	*conn,
	       gboolean		 yours,
	       const char	*object_path,
	       GHashTable	*props,
	       const GError	*in_error,
	       gpointer		 user_data,
	       GObject		*weak_obj)
{
	handle_error (in_error);
	GError *error = NULL;

	g_print (" > create_muc_cb (%s)\n", object_path);

	TpChannel *channel = tp_channel_new_from_properties (conn,
			object_path, props, &error);
	handle_error (error);

	tp_channel_call_when_ready (channel, muc_channel_ready, NULL);
}

static void
muc_request_channel_cb (TpConnection	*conn,
			const char	*object_path,
			const GError	*in_error,
			gpointer	 user_data,
			GObject		*weak_obj)
{
	handle_error (in_error);
	GError *error = NULL;

	g_print (" > muc_request_channel_cb (%s)\n", object_path);

	TpHandle handle = GPOINTER_TO_INT (user_data);
	TpChannel *channel = tp_channel_new (conn,
			object_path,
			TP_IFACE_CHANNEL_TYPE_TEXT,
			TP_HANDLE_TYPE_ROOM,
			handle,
			&error);
	handle_error (error);

	tp_channel_call_when_ready (channel, muc_channel_ready, NULL);
}

static void
request_handles_cb (TpConnection	*conn,
		    TpHandleType	 handle_type,
		    guint		 n_handles,
		    const TpHandle	*handles,
		    const char * const	*ids,
		    const GError	*in_error,
		    gpointer		 user_data,
		    GObject		*weak_object)
{
	g_print (" > request_handles_cb\n");

	handle_error (in_error);
	g_return_if_fail (n_handles == 1);

	/* since there is no error, and only 1 handle, let us assume that it
	 * is the handle for the room #test */

	tp_cli_connection_call_request_channel (conn, -1,
			TP_IFACE_CHANNEL_TYPE_TEXT,
			TP_HANDLE_TYPE_ROOM, handles[0],
			TRUE,
			muc_request_channel_cb,
			GINT_TO_POINTER (handles[0]), NULL, NULL);
}

static void
conn_ready (TpConnection	*conn,
            const GError	*in_error,
	    gpointer		 user_data)
{
	GError *error = NULL;

	g_print (" > conn_ready\n");

	handle_error (in_error);

	/* check if the Requests interface is available */
	if (tp_proxy_has_interface_by_id (conn,
		TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
	{
		/* make a connection to a MUC channel */
		GHashTable *map = tp_asv_new (
			TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
			TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_ROOM,
			TP_IFACE_CHANNEL ".TargetID", G_TYPE_STRING, "#test",
			NULL);

		tp_cli_connection_interface_requests_call_ensure_channel (
				conn, -1, map,
				create_muc_cb,
				NULL, NULL, NULL);

		g_hash_table_destroy (map);
	}
	else
	{
		g_print ("Requests interface is not available\n");

		/* we need a handle for the channel we are going to join */
		const char *handles[] = { "#test", NULL };
		tp_connection_request_handles (conn, -1, TP_HANDLE_TYPE_ROOM,
				handles, request_handles_cb,
				NULL, NULL, NULL);
	}
}

static void
status_changed_cb (TpConnection	*conn,
                   guint	 status,
		   guint	 reason,
		   gpointer	 user_data,
		   GObject	*weak_object)
{
	if (status == TP_CONNECTION_STATUS_DISCONNECTED)
	{
		g_print ("Disconnected\n");
		g_main_loop_quit (loop);
	}
	else if (status == TP_CONNECTION_STATUS_CONNECTED)
	{
		g_print ("Connected\n");
	}
}

static void
request_connection_cb (TpConnectionManager	*cm,
                       const char		*bus_name,
		       const char		*object_path,
		       const GError		*in_error,
		       gpointer			 user_data,
		       GObject			*weak_object)
{
	GError *error = NULL;

	if (in_error) g_error ("%s", in_error->message);

	conn = tp_connection_new (bus_daemon, bus_name, object_path, &error);
	if (error) g_error ("%s", error->message);

	tp_connection_call_when_ready (conn, conn_ready, NULL);

	tp_cli_connection_connect_to_status_changed (conn, status_changed_cb,
			NULL, NULL, NULL, &error);
	handle_error (error);

	/* initiate the connection */
	tp_cli_connection_call_connect (conn, -1, NULL, NULL, NULL, NULL);
}

static void
cm_ready (TpConnectionManager	*cm,
	  const GError		*in_error,
	  gpointer		 user_data,
	  GObject		*weak_obj)
{
	char **argv = (char **) user_data;

	g_print (" > cm_ready\n");

	if (in_error) g_error ("%s", in_error->message);

	/* request a new connection */
	GHashTable *parameters = tp_asv_new (
			"account", G_TYPE_STRING, argv[1],
			"server", G_TYPE_STRING, argv[2],
			NULL);

	tp_cli_connection_manager_call_request_connection (cm, -1,
			"irc",
			parameters,
			request_connection_cb,
			NULL, NULL, NULL);

	g_hash_table_destroy (parameters);
}

static void
interrupt_cb (int signal)
{
	g_print ("Interrupt\n");
	/* disconnect */
	tp_cli_connection_call_disconnect (conn, -1, NULL, NULL, NULL, NULL);
}

int
main (int argc, char **argv)
{
	GError *error = NULL;

	g_type_init ();

	if (argc != 3)
	{
		g_error ("Must provide username and server!");
	}

	/* create a main loop */
	loop = g_main_loop_new (NULL, FALSE);

	/* acquire a connection to the D-Bus daemon */
	bus_daemon = tp_dbus_daemon_dup (&error);
	if (bus_daemon == NULL)
	{
		g_error ("%s", error->message);
	}

	/* we want to request the gabble CM */
	TpConnectionManager *cm = tp_connection_manager_new (bus_daemon,
			"idle", NULL, &error);
	if (error) g_error ("%s", error->message);

	tp_connection_manager_call_when_ready (cm, cm_ready,
			argv, NULL, NULL);

	/* set up a signal handler */
	struct sigaction sa = { 0 };
	sa.sa_handler = interrupt_cb;
	sigaction (SIGINT, &sa, NULL);

	g_main_loop_run (loop);

	g_object_unref (bus_daemon);

	return 0;
}