aboutsummaryrefslogtreecommitdiff
path: root/src/messages-service-dbus.c
blob: 211b1f5f09a64a2a1d735760e5a1c95a1d8a017f (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
/*
An indicator to show information that is in messaging applications
that the user is using.

Copyright 2009 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License version 3, as published 
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gio/gio.h>
#include "messages-service-dbus.h"
#include "dbus-data.h"
#include "gen-messages-service.xml.h"

enum {
	ATTENTION_CHANGED,
	ICON_CHANGED,
	REGISTER_APPLICATION,
	UNREGISTER_APPLICATION,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

typedef struct _MessageServiceDbusPrivate MessageServiceDbusPrivate;

struct _MessageServiceDbusPrivate
{
	GDBusConnection * connection;
	GCancellable    * accounts_cancel;
	GDBusProxy      * accounts_user;
	gboolean dot;
	gboolean hidden;
};

#define MESSAGE_SERVICE_DBUS_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), MESSAGE_SERVICE_DBUS_TYPE, MessageServiceDbusPrivate))

static void message_service_dbus_class_init (MessageServiceDbusClass *klass);
static void message_service_dbus_init       (MessageServiceDbus *self);
static void message_service_dbus_dispose    (GObject *object);
static void message_service_dbus_finalize   (GObject *object);
static void     bus_method_call             (GDBusConnection * connection,
                                             const gchar * sender,
                                             const gchar * path,
                                             const gchar * interface,
                                             const gchar * method,
                                             GVariant * params,
                                             GDBusMethodInvocation * invocation,
                                             gpointer user_data);

static GDBusNodeInfo *            bus_node_info = NULL;
static GDBusInterfaceInfo *       bus_interface_info = NULL;
static const GDBusInterfaceVTable bus_interface_table = {
	method_call:    bus_method_call,
	get_property:   NULL,  /* No properties */
	set_property:   NULL   /* No properties */
};

G_DEFINE_TYPE (MessageServiceDbus, message_service_dbus, G_TYPE_OBJECT);


static void
message_service_dbus_class_init (MessageServiceDbusClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (MessageServiceDbusPrivate));

	object_class->dispose = message_service_dbus_dispose;
	object_class->finalize = message_service_dbus_finalize;

	signals[ATTENTION_CHANGED] =  g_signal_new(MESSAGE_SERVICE_DBUS_SIGNAL_ATTENTION_CHANGED,
	                                      G_TYPE_FROM_CLASS(klass),
	                                      G_SIGNAL_RUN_LAST,
	                                      G_STRUCT_OFFSET (MessageServiceDbusClass, attention_changed),
	                                      NULL, NULL,
	                                      g_cclosure_marshal_VOID__BOOLEAN,
	                                      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);

	signals[ICON_CHANGED] =  g_signal_new(MESSAGE_SERVICE_DBUS_SIGNAL_ICON_CHANGED,
	                                      G_TYPE_FROM_CLASS(klass),
	                                      G_SIGNAL_RUN_LAST,
	                                      G_STRUCT_OFFSET (MessageServiceDbusClass, icon_changed),
	                                      NULL, NULL,
	                                      g_cclosure_marshal_VOID__BOOLEAN,
	                                      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);

	signals[REGISTER_APPLICATION] = g_signal_new(MESSAGE_SERVICE_DBUS_SIGNAL_REGISTER_APPLICATION,
						     G_TYPE_FROM_CLASS(klass),
						     G_SIGNAL_RUN_LAST,
						     0,
						     NULL, NULL,
						     g_cclosure_marshal_generic,
						     G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

	signals[UNREGISTER_APPLICATION] = g_signal_new(MESSAGE_SERVICE_DBUS_SIGNAL_UNREGISTER_APPLICATION,
						       G_TYPE_FROM_CLASS(klass),
						       G_SIGNAL_RUN_LAST,
						       0,
						       NULL, NULL,
						       g_cclosure_marshal_VOID__STRING,
						       G_TYPE_NONE, 1, G_TYPE_STRING);

	if (bus_node_info == NULL) {
		GError * error = NULL;

		bus_node_info = g_dbus_node_info_new_for_xml(_messages_service, &error);
		if (error != NULL) {
			g_error("Unable to parse Messaging Menu Interface description: %s", error->message);
			g_error_free(error);
		}
	}

	if (bus_interface_info == NULL) {
		bus_interface_info = g_dbus_node_info_lookup_interface(bus_node_info, INDICATOR_MESSAGES_DBUS_SERVICE_INTERFACE);

		if (bus_interface_info == NULL) {
			g_error("Unable to find interface '" INDICATOR_MESSAGES_DBUS_SERVICE_INTERFACE "'");
		}
	}

	return;
}

static void
connection_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
	GError * error = NULL;
	GDBusConnection * connection = g_bus_get_finish(res, &error);

	if (error != NULL) {
		g_error("Unable to connect to the session bus: %s", error->message);
		g_error_free(error);
		return;
	}

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(user_data);
	priv->connection = connection;

	g_dbus_connection_register_object(connection,
	                                  INDICATOR_MESSAGES_DBUS_SERVICE_OBJECT,
	                                  bus_interface_info,
	                                  &bus_interface_table,
	                                  user_data,
	                                  NULL, /* destroy */
	                                  &error);

	if (error != NULL) {
		g_error("Unable to register on session bus: %s", error->message);
		g_error_free(error);
		return;
	}

	g_debug("Service on session bus");

	return;
}

static void
accounts_notify_cb (GObject *source_object, GAsyncResult *res,
                    gpointer user_data)
{
	GError * error = NULL;
	GVariant * answer = g_dbus_proxy_call_finish(G_DBUS_PROXY(source_object), res, &error);

	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_error_free(error);
		return; /* Must exit before accessing freed memory */
	}

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(user_data);

	if (priv->accounts_cancel != NULL) {
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	if (error != NULL) {
		g_warning("Unable to get notify accounts service of message status: %s", error->message);
		g_error_free(error);
		return;
	}

	g_variant_unref (answer);
}

static void
accounts_notify (MessageServiceDbus *self)
{
	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(self);

	if (priv->accounts_user == NULL)
		return; /* We're not able to talk to accounts service */

	if (priv->accounts_cancel != NULL) {
		/* Cancel old notify before starting new one */
		g_cancellable_cancel(priv->accounts_cancel);
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	priv->accounts_cancel = g_cancellable_new();
	g_dbus_proxy_call(priv->accounts_user,
	                  "SetXHasMessages",
	                  g_variant_new ("(b)", priv->dot),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1, /* timeout */
	                  priv->accounts_cancel,
	                  accounts_notify_cb,
	                  self);
}

static void
get_accounts_user_proxy_cb (GObject *source_object, GAsyncResult *res,
                            gpointer user_data)
{
	GError * error = NULL;
	GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);

	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_error_free(error);
		return; /* Must exit before accessing freed memory */
	}

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(user_data);

	if (priv->accounts_cancel != NULL) {
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	if (error != NULL) {
		g_warning("Unable to get proxy of accountsservice: %s", error->message);
		g_error_free(error);
		return;
	}

	priv->accounts_user = proxy;
	accounts_notify (MESSAGE_SERVICE_DBUS (user_data));
}

static void
get_accounts_user_find_user_cb (GObject *source_object, GAsyncResult *res,
                                gpointer user_data)
{
	GError * error = NULL;
	GVariant * answer = g_dbus_proxy_call_finish(G_DBUS_PROXY(source_object), res, &error);

	/* We're done with main accounts proxy now */
	g_object_unref (source_object);

	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_error_free(error);
		return; /* Must exit before accessing freed memory */
	}

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(user_data);

	if (priv->accounts_cancel != NULL) {
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	if (error != NULL) {
		g_warning("Unable to get object name of user from accountsservice: %s", error->message);
		g_error_free(error);
		return;
	}

	if (!g_variant_is_of_type (answer, G_VARIANT_TYPE ("(o)"))) {
		g_warning("Unexpected type from FindUserByName: %s", g_variant_get_type_string (answer));
		g_variant_unref(answer);
		return;
	}

	const gchar *path;
	g_variant_get(answer, "(&o)", &path);

	priv->accounts_cancel = g_cancellable_new();
	g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
	                         G_DBUS_PROXY_FLAGS_NONE,
	                         NULL,
	                         "org.freedesktop.Accounts", 
	                         path,
	                         "org.freedesktop.Accounts.User",
	                         priv->accounts_cancel,
	                         get_accounts_user_proxy_cb,
	                         user_data);

	g_variant_unref (answer);
}

static void
get_accounts_proxy_cb (GObject *source_object, GAsyncResult *res,
                       gpointer user_data)
{
	GError * error = NULL;
	GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);

	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_error_free(error);
		return; /* Must exit before accessing freed memory */
	}

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(user_data);

	if (priv->accounts_cancel != NULL) {
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	if (error != NULL) {
		g_warning("Unable to get proxy of accountsservice: %s", error->message);
		g_error_free(error);
		return;
	}

	priv->accounts_cancel = g_cancellable_new();
	g_dbus_proxy_call(proxy,
	                  "FindUserByName",
	                  g_variant_new ("(s)", g_get_user_name ()),
	                  G_DBUS_CALL_FLAGS_NONE,
	                  -1, /* timeout */
	                  priv->accounts_cancel,
	                  get_accounts_user_find_user_cb,
	                  user_data);
}

static void
get_accounts_proxy (MessageServiceDbus *self)
{
	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(self);

	g_return_if_fail(priv->accounts_cancel == NULL);

	priv->accounts_cancel = g_cancellable_new();
	g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
	                         G_DBUS_PROXY_FLAGS_NONE,
	                         NULL,
	                         "org.freedesktop.Accounts", 
	                         "/org/freedesktop/Accounts",
	                         "org.freedesktop.Accounts",
	                         priv->accounts_cancel,
	                         get_accounts_proxy_cb,
	                         self);
}

static void
message_service_dbus_init (MessageServiceDbus *self)
{
	g_bus_get(G_BUS_TYPE_SESSION, NULL, connection_cb, self);
	get_accounts_proxy (self);

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(self);

	priv->dot = FALSE;
	priv->hidden = FALSE;

	return;
}

static void
message_service_dbus_dispose (GObject *object)
{
	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(object);

	if (priv->connection != NULL) {
		g_object_unref(priv->connection);
		priv->connection = NULL;
	}

	if (priv->accounts_cancel != NULL) {
		g_cancellable_cancel(priv->accounts_cancel);
		g_object_unref(priv->accounts_cancel);
		priv->accounts_cancel = NULL;
	}

	if (priv->accounts_user != NULL) {
		g_object_unref(priv->accounts_user);
		priv->accounts_user = NULL;
	}

	G_OBJECT_CLASS (message_service_dbus_parent_class)->dispose (object);
	return;
}

static void
message_service_dbus_finalize (GObject *object)
{


	G_OBJECT_CLASS (message_service_dbus_parent_class)->finalize (object);
	return;
}

MessageServiceDbus *
message_service_dbus_new (void)
{
	return MESSAGE_SERVICE_DBUS(g_object_new(MESSAGE_SERVICE_DBUS_TYPE, NULL));
}

/* Method request off of DBus */
static void
bus_method_call (GDBusConnection * connection, const gchar * sender, const gchar * path, const gchar * interface, const gchar * method, GVariant * params, GDBusMethodInvocation * invocation, gpointer user_data)
{
	MessageServiceDbus * ms = MESSAGE_SERVICE_DBUS(user_data);
	if (ms == NULL) { return; }

	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(ms);

	if (g_strcmp0("AttentionRequested", method) == 0) {
		g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", priv->dot));
		return;
	} else if (g_strcmp0("IconShown", method) == 0) {
		g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", priv->hidden));
		return;
	} else if (g_strcmp0("ClearAttention", method) == 0) {
		message_service_dbus_set_attention(ms, FALSE);
		g_dbus_method_invocation_return_value(invocation, NULL);
		return;
	} else if (g_strcmp0("RegisterApplication", method) == 0) {
		const gchar *desktop_id, *object_path;
		g_variant_get(params, "(&s&o)", &desktop_id, &object_path);
		g_signal_emit(ms, signals[REGISTER_APPLICATION], 0, sender, desktop_id, object_path);
		g_dbus_method_invocation_return_value(invocation, NULL);
	} else if (g_strcmp0("UnregisterApplication", method) == 0) {
		const gchar *desktop_id;
		g_variant_get(params, "(&s)", &desktop_id);
		g_signal_emit(ms, signals[UNREGISTER_APPLICATION], 0, desktop_id);
		g_dbus_method_invocation_return_value(invocation, NULL);
	} else {
		g_warning("Unknown function call '%s'", method);
	}

	return;
}

void
message_service_dbus_set_attention (MessageServiceDbus * self, gboolean attention)
{
	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(self);
	/* Do signal */
	if (attention != priv->dot) {
		priv->dot = attention;
		g_signal_emit(G_OBJECT(self), signals[ATTENTION_CHANGED], 0, priv->dot, TRUE);

		if (priv->connection != NULL) {
			g_dbus_connection_emit_signal(priv->connection,
			                              NULL,
			                              INDICATOR_MESSAGES_DBUS_SERVICE_OBJECT,
			                              INDICATOR_MESSAGES_DBUS_SERVICE_INTERFACE,
			                              "AttentionChanged",
			                              g_variant_new("(b)", priv->dot),
			                              NULL);
		}

		accounts_notify (self);
	}
	return;
}

void
message_service_dbus_set_icon (MessageServiceDbus * self, gboolean hidden)
{
	MessageServiceDbusPrivate * priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(self);
	/* Do signal */
	if (hidden != priv->hidden) {
		priv->hidden = hidden;
		g_signal_emit(G_OBJECT(self), signals[ICON_CHANGED], 0, priv->hidden, TRUE);

		if (priv->connection != NULL) {
			g_dbus_connection_emit_signal(priv->connection,
			                              NULL,
			                              INDICATOR_MESSAGES_DBUS_SERVICE_OBJECT,
			                              INDICATOR_MESSAGES_DBUS_SERVICE_INTERFACE,
			                              "IconChanged",
			                              g_variant_new("(b)", priv->hidden),
			                              NULL);
		}
	}
	return;
}

GDBusConnection *
message_service_dbus_get_connection (MessageServiceDbus *msd)
{
	MessageServiceDbusPrivate * priv;

	g_return_val_if_fail (IS_MESSAGE_SERVICE_DBUS (msd), NULL);

        priv = MESSAGE_SERVICE_DBUS_GET_PRIVATE(msd);
	return priv->connection;
}