summaryrefslogtreecommitdiff
path: root/src/libtracker-common/tracker-locale-gconfdbus.c
blob: 064567808af111170a5260f8c92199cb592e750e (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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 * Copyright (C) 2011 Nokia <ivan.frade@nokia.com>
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <locale.h>
#include <string.h>

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>

#include "tracker-locale-gconfdbus.h"

/* This helps with testing, change all the names in gconf-dbus and then you
 * can run it in parallel with an upstream gconf-d of the GNOME platform */
#define GCONF_DBUS_NAME "GConf"

#define GCONF_DBUS_SERVICE                    "org.gnome." GCONF_DBUS_NAME
#define GCONF_DBUS_SERVER_INTERFACE           "org.gnome." GCONF_DBUS_NAME ".Server"
#define GCONF_DBUS_DATABASE_INTERFACE         "org.gnome." GCONF_DBUS_NAME ".Database"
#define GCONF_DBUS_SERVER_OBJECT              "/org/gnome/" GCONF_DBUS_NAME "/Server"
#define GCONF_DBUS_CLIENT_OBJECT              "/org/gnome/" GCONF_DBUS_NAME "/Client"
#define GCONF_DBUS_CLIENT_INTERFACE           "org.gnome." GCONF_DBUS_NAME ".Client"

/* Base dir for all gconf locale values */
#define MEEGOTOUCH_LOCALE_DIR                 "/meegotouch/i18n"

#define TRACKER_DISABLE_MEEGOTOUCH_LOCALE_ENV "TRACKER_DISABLE_MEEGOTOUCH_LOCALE"

static gchar*gconf_dbus_default_db = NULL;
static GDBusConnection *connection = NULL;
static gboolean service_running = FALSE;
static guint watch_name_id = 0;
static guint registration_id = 0;
static GMutex subscribers_mutex;
GDBusNodeInfo *introspection_data = NULL;
static gboolean meegotouch_mode = TRUE;
static gboolean first_time = TRUE;

/* gconf keys for tracker locales, as defined in:
 * http://apidocs.meego.com/1.0/mtf/i18n.html
 */
static const gchar *gconf_locales[TRACKER_LOCALE_LAST] = {
	MEEGOTOUCH_LOCALE_DIR "/language",
	MEEGOTOUCH_LOCALE_DIR "/lc_time",
	MEEGOTOUCH_LOCALE_DIR "/lc_collate",
	MEEGOTOUCH_LOCALE_DIR "/lc_numeric",
	MEEGOTOUCH_LOCALE_DIR "/lc_monetary"
};

/* Structure to hold the notification data of each subscriber */
typedef struct {
	TrackerLocaleID id;
	TrackerLocaleNotifyFunc func;
	gpointer user_data;
	GFreeFunc destroy_notify;
} TrackerLocaleNotification;

/* List of subscribers which want to get notified of locale changes */
static GSList *subscribers;

static const gchar introspection_xml[] =
  "<node>"
  "  <interface name='" GCONF_DBUS_CLIENT_INTERFACE "'>"
  "    <method name='Notify'>"
  "      <arg type='s' name='database' direction='in' />"
  "      <arg type='s' name='namespace_section' direction='in' />"
  "      <arg type='(s(is)bsbb)' name='value' direction='in' />"
  "    </method>"
  "  </interface>"
  "</node>";

static gboolean
check_gconf_dbus_default_db (void) {
	if (gconf_dbus_default_db == NULL || !g_variant_is_object_path (gconf_dbus_default_db)) {
		g_critical ("gconf_dbus_default_db is '%s', which is not a valid D-Bus object path.",
		            gconf_dbus_default_db ? gconf_dbus_default_db : "(null)");
		return FALSE;
	} else {
		return TRUE;
	}
}

static gboolean
add_notify (void)
{
	GVariant *reply;
	GError *error = NULL;

	if (!check_gconf_dbus_default_db ()) {
		return FALSE;
	}

	reply = g_dbus_connection_call_sync (connection,
	                                     GCONF_DBUS_SERVICE,
	                                     gconf_dbus_default_db,
	                                     GCONF_DBUS_DATABASE_INTERFACE,
	                                     "AddNotify",
	                                     g_variant_new ("(s)", MEEGOTOUCH_LOCALE_DIR),
	                                     NULL,
	                                     G_DBUS_CALL_FLAGS_NONE,
	                                     -1,
	                                     NULL,
	                                     &error);

	if (error) {
		g_critical ("%s", error->message);
		g_clear_error (&error);
		return FALSE;
	}

	if (reply) {
		g_variant_unref (reply);
	}

	return TRUE;
}

static void
handle_method_call (GDBusConnection       *connection,
                    const gchar           *sender,
                    const gchar           *object_path,
                    const gchar           *interface_name,
                    const gchar           *method_name,
                    GVariant              *parameters,
                    GDBusMethodInvocation *invocation,
                    gpointer               user_data)
{
	if (g_strcmp0 (method_name, "Notify") == 0) {
		const gchar *key = NULL, *value = NULL;
		const gchar *schema = NULL, *database = NULL;
		const gchar *namespace_name = NULL;
		gboolean is_set, is_default, is_writable;
		gint type, i;
		GSList *li;

		if (g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss(s(is)bsbb))"))) {
			g_variant_get (parameters, "(&s&s(&s(i&s)b&sbb))",
			               &database, &namespace_name,
			               &key, &type, &value,
			               &is_set, &schema,
			               &is_default, &is_writable,
			               NULL);

			/* Find the proper locale to change */
			for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
				if (strcmp (gconf_locales[i], key) == 0) {
					break;
				}
			}

			/* Oh, not found? */
			if (i == TRACKER_LOCALE_LAST) {
				g_debug ("Skipping change on gconf key '%s' as not really needed", key);
				return;
			}

			/* Ensure a proper value was set */
			if (value == NULL || value[0] == '\0') {
				g_warning ("Locale value for '%s' cannot be NULL, not changing %s",
				           gconf_locales[i],
				           tracker_locale_get_name (i));
				return;
			}

			tracker_locale_set (i, value);

			g_mutex_lock (&subscribers_mutex);

			for (li = subscribers; li; li = g_slist_next (li)) {
				TrackerLocaleNotification *data = li->data;

				if (i == data->id) {
					g_debug ("Notifying locale '%s' change to subscriber '%p'",
					         tracker_locale_get_name(i),
					         data);
					data->func (i, data->user_data);
				}
			}

			g_mutex_unlock (&subscribers_mutex);
		}
	}
}

static GVariant *
handle_get_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GError          **error,
                     gpointer          user_data)
{
	return NULL;
}

static gboolean
handle_set_property (GDBusConnection  *connection,
                     const gchar      *sender,
                     const gchar      *object_path,
                     const gchar      *interface_name,
                     const gchar      *property_name,
                     GVariant         *value,
                     GError          **error,
                     gpointer          user_data)
{
	return TRUE;
}



static gchar *
get_value_from_config (const gchar *key_in)
{
	const gchar *locale = setlocale (LC_CTYPE, NULL);
	const gchar *key, *value, *schema;
	gchar *val = NULL;
	gboolean is_set, is_default, is_writable;
	gint type;
	GError *error = NULL;
	GVariant *reply;

	if (!check_gconf_dbus_default_db ()) {
		return NULL;
	}

	reply = g_dbus_connection_call_sync (connection,
	                                     GCONF_DBUS_SERVICE,
	                                     gconf_dbus_default_db,
	                                     GCONF_DBUS_DATABASE_INTERFACE,
	                                     "LookupExtended",
	                                     g_variant_new ("(ssb)", key_in, locale, TRUE),
	                                     NULL,
	                                     G_DBUS_CALL_FLAGS_NONE,
	                                     -1,
	                                     NULL,
	                                     &error);

	if (error) {
		g_critical ("%s", error->message);
		g_clear_error (&error);

		return NULL;
	}

	if (g_variant_is_of_type (reply, G_VARIANT_TYPE ("((s(is)bsbb))"))) {
		g_variant_get (reply, "((&s(i&s)b&sbb))",
		               &key, &type, &value,
		               &is_set, &schema,
		               &is_default, &is_writable,
		               NULL);

		val = g_strdup (value);
	}

	g_variant_unref (reply);

	return val;
}


static void
on_gconfd_dbus_appeared (GDBusConnection *connection,
                         const gchar     *name,
                         const gchar     *name_owner,
                         gpointer         user_data)
{
	guint i;

	service_running = TRUE;
	add_notify ();

	if (!first_time) {
		/* And (re)initialize all */
		for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
			gchar *str;

			str = get_value_from_config (gconf_locales[i]);
			if (str) {
				tracker_locale_set (i, str);
				g_free (str);
			}
		}
	}

	first_time = FALSE;
}

static void
on_gconfd_dbus_disappeared  (GDBusConnection *connection,
                             const gchar     *name,
                             gpointer         user_data)
{
	service_running = FALSE;
}

void
tracker_locale_gconfdbus_init (void)
{
	if (!g_getenv (TRACKER_DISABLE_MEEGOTOUCH_LOCALE_ENV) && meegotouch_mode) {
		GError *error = NULL;
		GVariant *reply;
		guint i;
		GDBusInterfaceVTable interface_vtable = {
			handle_method_call,
			handle_get_property,
			handle_set_property
		};

		g_message ("Retrieving locale from GConf is ENABLED");

		connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);

		if (error) {
			g_warning ("%s", error->message);
			g_clear_error (&error);
			return;
		}

		service_running = TRUE;

		reply = g_dbus_connection_call_sync (connection,
		                                     GCONF_DBUS_SERVICE,
		                                     GCONF_DBUS_SERVER_OBJECT,
		                                     GCONF_DBUS_SERVER_INTERFACE,
		                                     "GetDefaultDatabase",
		                                     NULL,
		                                     NULL,
		                                     G_DBUS_CALL_FLAGS_NONE,
		                                     -1,
		                                     NULL,
		                                     &error);


		if (error) {
			if (error->code == G_DBUS_ERROR_UNKNOWN_METHOD) {
				g_warning ("GetDefaultDatabase doesn't exist on %s, this GConf "
				           "doesn't look like a gconf-dbus.\n"
				           "Continuing in non-meegotouch mode",
				           GCONF_DBUS_SERVER_OBJECT);
				meegotouch_mode = FALSE;
			} else {
				g_critical ("%s", error->message);
				g_clear_error (&error);
			}

			g_object_unref (connection);
			connection = NULL;
			return;
		}

		g_variant_get (reply, "(s)", &gconf_dbus_default_db, NULL);

		g_variant_unref (reply);

		if (gconf_dbus_default_db == NULL || !g_variant_is_object_path (gconf_dbus_default_db)) {
			g_critical (GCONF_DBUS_SERVER_INTERFACE ".GetDefaultDatabase returned '%s', which is not a valid D-Bus object path.",
			            gconf_dbus_default_db ? gconf_dbus_default_db : "(null)");

			g_free (gconf_dbus_default_db);
			gconf_dbus_default_db = NULL;

			g_object_unref (connection);
			connection = NULL;
			return;
		}

		introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);

		if (error) {
			g_critical ("%s", error->message);
			g_clear_error (&error);

			g_free (gconf_dbus_default_db);
			gconf_dbus_default_db = NULL;

			g_object_unref (connection);
			connection = NULL;
			return;
		}

		registration_id =
			g_dbus_connection_register_object (connection,
			                                   GCONF_DBUS_CLIENT_OBJECT,
			                                   introspection_data->interfaces[0],
			                                   &interface_vtable,
			                                   NULL,
			                                   NULL,
			                                   &error);

		if (error) {
			g_critical ("%s", error->message);
			g_clear_error (&error);

			g_dbus_node_info_unref (introspection_data);
			introspection_data = NULL;

			g_free (gconf_dbus_default_db);
			gconf_dbus_default_db = NULL;

			g_object_unref (connection);
			connection = NULL;
			return;
		}

		first_time = TRUE;

		watch_name_id = g_bus_watch_name_on_connection (connection,
		                                                GCONF_DBUS_SERVICE,
		                                                G_BUS_NAME_WATCHER_FLAGS_NONE,
		                                                on_gconfd_dbus_appeared,
		                                                on_gconfd_dbus_disappeared,
		                                                NULL, NULL);

		/* And initialize all (must be synchronously done at the return of this
		 * function, which is why we do the first_time trick in on_gconfd_dbus_
		 * appeared above) */

		for (i = 0; i < TRACKER_LOCALE_LAST; i++) {
			gchar *str;

			str = get_value_from_config (gconf_locales[i]);
			if (str) {
				tracker_locale_set (i, str);
				g_free (str);
			}
		}
	}
}

void
tracker_locale_gconfdbus_shutdown (void)
{
	if (gconf_dbus_default_db != NULL && connection != NULL && check_gconf_dbus_default_db ()) {
		GVariant *reply;
		GError *error = NULL;

		reply = g_dbus_connection_call_sync (connection,
		                                     GCONF_DBUS_SERVICE,
		                                     gconf_dbus_default_db,
		                                     GCONF_DBUS_DATABASE_INTERFACE,
		                                     "RemoveNotify",
		                                     g_variant_new ("(s)", MEEGOTOUCH_LOCALE_DIR),
		                                     NULL,
		                                     G_DBUS_CALL_FLAGS_NONE,
		                                     -1,
		                                     NULL,
		                                     &error);

		if (error) {
			g_warning ("%s", error->message);
			g_clear_error (&error);
		} else {
			g_variant_unref (reply);
		}
	}

	if (watch_name_id != 0) {
		g_bus_unwatch_name (watch_name_id);
		watch_name_id = 0;
	}

	if (registration_id != 0) {
		g_dbus_connection_unregister_object (connection, registration_id);
		registration_id = 0;
	}

	g_free (gconf_dbus_default_db);
	gconf_dbus_default_db = NULL;

	if (introspection_data) {
		g_dbus_node_info_unref (introspection_data);
		introspection_data = NULL;
	}

	if (connection) {
		g_object_unref (connection);
		connection = NULL;
	}
}

gpointer
tracker_locale_gconfdbus_notify_add (TrackerLocaleID         id,
                                     TrackerLocaleNotifyFunc func,
                                     gpointer                user_data,
                                     GFreeFunc               destroy_notify)
{
	TrackerLocaleNotification *data;

	/* Can be called from a thread */

	g_assert (func != NULL);

	data = g_slice_new (TrackerLocaleNotification);
	data->id = id;
	data->func = func;
	data->user_data = user_data;
	data->destroy_notify = destroy_notify;

	g_mutex_lock (&subscribers_mutex);
	subscribers = g_slist_prepend (subscribers, data);
	g_mutex_unlock (&subscribers_mutex);

	return data;
}

static gboolean
destroy_locale_notify (gpointer data_p)
{
	/* Always on mainloop */

	TrackerLocaleNotification *data = data_p;

	/* Call the provided destroy_notify if any. */
	if (data->destroy_notify) {
		data->destroy_notify (data->user_data);
	}

	/* And fully dispose the notification data */
	g_slice_free (TrackerLocaleNotification, data);

	return FALSE;
}

void
tracker_locale_gconfdbus_notify_remove (gpointer notification_id)
{
	GSList *li;

	/* Can be called from a thread */

	g_mutex_lock (&subscribers_mutex);

	li = g_slist_find (subscribers, notification_id);
	if (li) {
		TrackerLocaleNotification *data = li->data;

		/* Remove item from list of subscribers */
		subscribers = g_slist_delete_link (subscribers, li);

		g_idle_add (destroy_locale_notify, data);
	}

	g_mutex_unlock (&subscribers_mutex);
}