summaryrefslogtreecommitdiff
path: root/libxklavier/xklavier_config_iso.c
blob: ab8ff5fe0ddd8a90a3bd31d490d255feef473b0a (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
/*
 * Copyright (C) 2002-2006 Sergey V. Udaltsov <svu@gnome.org>
 *
 * 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 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>

#include "config.h"

#include "xklavier_private.h"

#define ISO_CODES_DATADIR    ISO_CODES_PREFIX "/share/xml/iso-codes"
#define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"

static GHashTable *country_code_names = NULL;
static GHashTable *lang_code_names = NULL;

typedef struct {
	const gchar *domain;
	const gchar **attr_names;
} LookupParams;

typedef struct {
	GHashTable *code_names;
	const gchar *tag_name;
	LookupParams *params;
} CodeBuildStruct;

static const char *countryLookupNames[] = { "alpha_2_code", NULL };
static const char *languageLookupNames[] =
    { "iso_639_2B_code", "iso_639_2T_code", NULL };

static LookupParams countryLookup = { "iso_3166", countryLookupNames };
static LookupParams languageLookup = { "iso_639", languageLookupNames };

static void
iso_codes_parse_start_tag(GMarkupParseContext * ctx,
			  const gchar * element_name,
			  const gchar ** attr_names,
			  const gchar ** attr_values,
			  gpointer user_data, GError ** error)
{
	const gchar *name;
	const gchar **san = attr_names, **sav = attr_values;
	CodeBuildStruct *cbs = (CodeBuildStruct *) user_data;

	/* Is this the tag we are looking for? */
	if (!g_str_equal(element_name, cbs->tag_name) ||
	    attr_names == NULL || attr_values == NULL) {
		return;
	}

	name = NULL;

	/* What would be the value? */
	while (*attr_names && *attr_values) {
		if (g_str_equal(*attr_names, "name")) {
			name = *attr_values;
			break;
		}

		attr_names++;
		attr_values++;
	}

	if (!name) {
		return;
	}

	attr_names = san;
	attr_values = sav;

	/* Walk again the attributes */
	while (*attr_names && *attr_values) {
		const gchar **attr = cbs->params->attr_names;
		/* Look through all the attributess we are interested in */
		while (*attr) {
			if (g_str_equal(*attr_names, *attr)) {
				if (**attr_values) {
					g_hash_table_insert
					    (cbs->code_names,
					     g_strdup(*attr_values),
					     g_strdup(name));
				}
			}
			attr++;
		}

		attr_names++;
		attr_values++;
	}
}

static GHashTable *
iso_code_names_init(LookupParams * params)
{
	GError *err = NULL;
	gchar *buf, *filename, *tag_name;
	gsize buf_len;
	CodeBuildStruct cbs;

	GHashTable *ht = g_hash_table_new_full(g_str_hash, g_str_equal,
					       g_free, g_free);

	tag_name = g_strdup_printf("%s_entry", params->domain);

	cbs.code_names = ht;
	cbs.tag_name = tag_name;
	cbs.params = params;

	bindtextdomain(params->domain, ISO_CODES_LOCALESDIR);
	bind_textdomain_codeset(params->domain, "UTF-8");

	filename =
	    g_strdup_printf("%s/%s.xml", ISO_CODES_DATADIR,
			    params->domain);
	if (g_file_get_contents(filename, &buf, &buf_len, &err)) {
		GMarkupParseContext *ctx;
		GMarkupParser parser = {
			iso_codes_parse_start_tag,
			NULL, NULL, NULL, NULL
		};

		ctx = g_markup_parse_context_new(&parser, 0, &cbs, NULL);
		if (!g_markup_parse_context_parse(ctx, buf, buf_len, &err)) {
			g_warning("Failed to parse '%s/%s.xml': %s",
				  ISO_CODES_DATADIR,
				  params->domain, err->message);
			g_error_free(err);
		}

		g_markup_parse_context_free(ctx);
		g_free(buf);
	} else {
		g_warning("Failed to load '%s/%s.xml': %s",
			  ISO_CODES_DATADIR, params->domain, err->message);
		g_error_free(err);
	}
	g_free(filename);
	g_free(tag_name);

	return ht;
}

typedef const gchar *(*DescriptionGetterFunc) (const gchar * code);

const gchar *
xkl_get_language_name(const gchar * code)
{
	const gchar *name;

	if (!lang_code_names) {
		lang_code_names = iso_code_names_init(&languageLookup);
	}

	name = g_hash_table_lookup(lang_code_names, code);
	if (!name) {
		return NULL;
	}

	return dgettext("iso_639", name);
}

const gchar *
xkl_get_country_name(const gchar * code)
{
	const gchar *name;

	if (!country_code_names) {
		country_code_names = iso_code_names_init(&countryLookup);
	}

	name = g_hash_table_lookup(country_code_names, code);
	if (!name) {
		return NULL;
	}

	return dgettext("iso_3166", name);
}

static void
xkl_config_registry_foreach_iso_code(XklConfigRegistry * config,
				     ConfigItemProcessFunc func,
				     const gchar * xpath_exprs[],
				     DescriptionGetterFunc dgf,
				     gboolean to_upper, gpointer data)
{
	GHashTable *code_pairs;
	GHashTableIter iter;
	xmlXPathObjectPtr xpath_obj;
	const gchar **xpath_expr;
	gpointer key, value;
	XklConfigItem *ci;
	gint di;

	if (!xkl_config_registry_is_initialized(config))
		return;

	code_pairs = g_hash_table_new(g_str_hash, g_str_equal);

	for (xpath_expr = xpath_exprs; *xpath_expr; xpath_expr++) {
		for (di = 0; di < XKL_NUMBER_OF_REGISTRY_DOCS; di++) {
			gint ni;
			xmlNodePtr *node;
			xmlNodeSetPtr nodes;

			xmlXPathContextPtr xmlctxt =
			    xkl_config_registry_priv(config,
						     xpath_contexts[di]);
			if (xmlctxt == NULL)
				continue;

			xpath_obj =
			    xmlXPathEval((unsigned char *) *xpath_expr,
					 xmlctxt);
			if (xpath_obj == NULL)
				continue;

			nodes = xpath_obj->nodesetval;
			if (nodes == NULL) {
				xmlXPathFreeObject(xpath_obj);
				continue;
			}

			node = nodes->nodeTab;
			for (ni = nodes->nodeNr; --ni >= 0;) {
				gchar *iso_code =
				    (gchar *) (*node)->children->content;
				const gchar *description;
				iso_code =
				    to_upper ?
				    g_ascii_strup(iso_code,
						  -1) : g_strdup(iso_code);
				description = dgf(iso_code);
/* If there is a mapping to some ISO description - consider it as ISO code (well, it is just an assumption) */
				if (description)
					g_hash_table_insert
					    (code_pairs,
					     g_strdup
					     (iso_code),
					     g_strdup(description));
				g_free(iso_code);
				node++;
			}

			xmlXPathFreeObject(xpath_obj);
		}
	}

	g_hash_table_iter_init(&iter, code_pairs);
	ci = xkl_config_item_new();
	while (g_hash_table_iter_next(&iter, &key, &value)) {
		g_strlcpy(ci->name, (const gchar *) key, sizeof(ci->name));
		g_strlcpy(ci->description, (const gchar *) value,
			  sizeof(ci->description));
		func(config, ci, data);
	}
	g_object_unref(G_OBJECT(ci));
	g_hash_table_unref(code_pairs);
}

void
xkl_config_registry_foreach_country(XklConfigRegistry *
				    config,
				    ConfigItemProcessFunc
				    func, gpointer data)
{
	const gchar *xpath_exprs[] = {
		XKBCR_LAYOUT_PATH "/configItem/countryList/iso3166Id",
		XKBCR_LAYOUT_PATH "/configItem/name",
		NULL
	};

	xkl_config_registry_foreach_iso_code(config, func, xpath_exprs,
					     xkl_get_country_name, TRUE,
					     data);
}

void
xkl_config_registry_foreach_language(XklConfigRegistry *
				     config,
				     ConfigItemProcessFunc
				     func, gpointer data)
{
	const gchar *xpath_exprs[] = {
		XKBCR_LAYOUT_PATH "/configItem/languageList/iso639Id",
		XKBCR_VARIANT_PATH "/configItem/languageList/iso639Id",
		NULL
	};

	xkl_config_registry_foreach_iso_code(config, func, xpath_exprs,
					     xkl_get_language_name, FALSE,
					     data);
}

void
xkl_config_registry_foreach_iso_variant(XklConfigRegistry *
					config,
					const gchar *
					iso_code,
					TwoConfigItemsProcessFunc
					func, gpointer data,
					const gchar * layout_xpath_exprs[],
					const gboolean
					should_code_be_lowered1[],
					const gchar *
					variant_xpath_exprs[],
					const gboolean
					should_code_be_lowered2[])
{
	xmlXPathObjectPtr xpath_obj;
	xmlNodeSetPtr nodes;
	const gchar **xpath_expr;
	const gboolean *is_low_id = should_code_be_lowered1;
	gchar *low_iso_code;

	if (!xkl_config_registry_is_initialized(config))
		return;

	low_iso_code = g_ascii_strdown(iso_code, -1);

	for (xpath_expr = layout_xpath_exprs; *xpath_expr;
	     xpath_expr++, is_low_id++) {
		const gchar *aic = *is_low_id ? low_iso_code : iso_code;
		gchar *xpe = g_strdup_printf(*xpath_expr, aic);
		gint di;
		GSList *processed_ids = NULL;

		for (di = 0; di < XKL_NUMBER_OF_REGISTRY_DOCS; di++) {
			xmlXPathContextPtr xmlctxt =
			    xkl_config_registry_priv(config,
						     xpath_contexts[di]);
			if (xmlctxt == NULL)
				continue;

			xpath_obj =
			    xmlXPathEval((unsigned char *) xpe, xmlctxt);
			if (xpath_obj == NULL)
				continue;

			nodes = xpath_obj->nodesetval;
			if (nodes != NULL) {
				gint ni;
				xmlNodePtr *node = nodes->nodeTab;
				XklConfigItem *ci = xkl_config_item_new();
				for (ni = nodes->nodeNr; --ni >= 0;) {
					if (xkl_read_config_item
					    (config, di, *node, ci)) {
						if (g_slist_find_custom
						    (processed_ids,
						     ci->name,
						     (GCompareFunc)
						     g_ascii_strcasecmp) ==
						    NULL) {
							func(config, ci,
							     NULL, data);
							processed_ids =
							    g_slist_append
							    (processed_ids,
							     g_strdup
							     (ci->name));
						}
					}
					node++;
				}
				g_object_unref(G_OBJECT(ci));
			}
			xmlXPathFreeObject(xpath_obj);
		}
		g_free(xpe);
	}

	is_low_id = should_code_be_lowered2;
	for (xpath_expr = variant_xpath_exprs; *xpath_expr;
	     xpath_expr++, is_low_id++) {
		const gchar *aic = *is_low_id ? low_iso_code : iso_code;
		gchar *xpe = g_strdup_printf(*xpath_expr, aic);
		gint di;
		for (di = 0; di < XKL_NUMBER_OF_REGISTRY_DOCS; di++) {
			xmlXPathContextPtr xmlctxt =
			    xkl_config_registry_priv(config,
						     xpath_contexts[di]);
			if (xmlctxt == NULL)
				continue;

			xpath_obj =
			    xmlXPathEval((unsigned char *) xpe, xmlctxt);
			if (xpath_obj == NULL)
				continue;

			nodes = xpath_obj->nodesetval;
			if (nodes != NULL) {
				gint ni;
				xmlNodePtr *node = nodes->nodeTab;
				XklConfigItem *ci = xkl_config_item_new();
				XklConfigItem *pci = xkl_config_item_new();
				for (ni = nodes->nodeNr; --ni >= 0;) {
					if (xkl_read_config_item
					    (config, di, *node, ci) &&
					    xkl_read_config_item
					    (config, di,
					     (*node)->parent->parent, pci))
						func(config, pci, ci,
						     data);
					node++;
				}
				g_object_unref(G_OBJECT(pci));
				g_object_unref(G_OBJECT(ci));
			}
			xmlXPathFreeObject(xpath_obj);
		}
		g_free(xpe);
	}

	g_free(low_iso_code);
}

void
xkl_config_registry_foreach_country_variant(XklConfigRegistry *
					    config,
					    const gchar *
					    country_code,
					    TwoConfigItemsProcessFunc
					    func, gpointer data)
{
	const gchar *layout_xpath_exprs[] = {
		XKBCR_LAYOUT_PATH "[configItem/name = '%s']",
		XKBCR_LAYOUT_PATH
		    "[configItem/countryList/iso3166Id = '%s']",
		NULL
	};
	const gboolean should_code_be_lowered1[] = { TRUE, FALSE };

	const gchar *variant_xpath_exprs[] = {
		XKBCR_VARIANT_PATH
		    "[configItem/countryList/iso3166Id = '%s']",
		XKBCR_VARIANT_PATH
		    "[../../configItem/name = '%s' and not(configItem/countryList/iso3166Id)]",
		XKBCR_VARIANT_PATH
		    "[../../configItem/countryList/iso3166Id = '%s' and not(configItem/countryList/iso3166Id)]",
		NULL
	};

	const gboolean should_code_be_lowered2[] = { FALSE, TRUE, FALSE };

	xkl_config_registry_foreach_iso_variant(config,
						country_code,
						func, data,
						layout_xpath_exprs,
						should_code_be_lowered1,
						variant_xpath_exprs,
						should_code_be_lowered2);
}

void
xkl_config_registry_foreach_language_variant(XklConfigRegistry *
					     config,
					     const gchar *
					     language_code,
					     TwoConfigItemsProcessFunc
					     func, gpointer data)
{
	const gchar *layout_xpath_exprs[] = {
		XKBCR_LAYOUT_PATH
		    "[configItem/languageList/iso639Id = '%s']",
		NULL
	};
	const gboolean should_code_be_lowered1[] = { FALSE };

	const gchar *variant_xpath_exprs[] = {
		XKBCR_VARIANT_PATH
		    "[configItem/languageList/iso639Id = '%s']",
		XKBCR_VARIANT_PATH
		    "[../../configItem/languageList/iso639Id = '%s' and not(configItem/languageList/iso639Id)]",
		NULL
	};
	const gboolean should_code_be_lowered2[] = { FALSE, FALSE };

	xkl_config_registry_foreach_iso_variant(config,
						language_code,
						func, data,
						layout_xpath_exprs,
						should_code_be_lowered1,
						variant_xpath_exprs,
						should_code_be_lowered2);
}