summaryrefslogtreecommitdiff
path: root/src/glade-catalog.c
blob: 74b495a270bbc3770f71e89997146a65995d52dd (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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
 * Copyright (C) 2004 Imendio AB
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */


#include <string.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gi18n-lib.h>

#include "glade.h"
#include "glade-catalog.h"
#include "glade-widget-adaptor.h"
#include "glade-binding.h"

typedef void   (*GladeCatalogInitFunc) (void);

struct _GladeCatalog
{
	gchar *language;	 /* Library language, NULL means language is C */
	gchar *library;          /* Library name for backend support  */

	gchar *name;             /* Symbolic catalog name             */

	gchar *dep_catalog;      /* Symbolic name of the catalog that
				  * this catalog depends on           */

	gchar *domain;           /* The domain to be used to translate
				  * strings from this catalog (otherwise this 
				  * defaults to the library name)
				  */
	
	gchar *book;             /* Devhelp search domain
				  */

	GList *widget_groups;    /* List of widget groups (palette)   */
	GList *adaptors;         /* List of widget class adaptors (all)  */

	GladeXmlContext *context;/* Xml context is stored after open
				  * before classes are loaded         */
	
	GModule *module;
	
	gchar *init_function_name;/* Catalog's init function name */
	GladeCatalogInitFunc init_function;
};

struct _GladeWidgetGroup
{
	gchar *name;             /* Group name                        */
	gchar *title;            /* Group name in the palette             */

	gboolean expanded;       /* Whether group is expanded in the palette */

	GList *adaptors;         /* List of class adaptors in the palette    */
};

static void            catalog_load         (GladeCatalog     *catalog);
static GladeCatalog   *catalog_open         (const gchar      *filename);
static GList          *catalog_sort         (GList            *catalogs);
static gboolean        catalog_load_classes (GladeCatalog     *catalog,
					     GladeXmlNode     *widgets_node);
static gboolean        catalog_load_group   (GladeCatalog     *catalog,
					     GladeXmlNode     *group_node);

static void            widget_group_free    (GladeWidgetGroup *group);

/* List of catalog names successfully loaded.
 */
static GList *loaded_catalogs = NULL;

static gboolean
catalog_get_function (GladeCatalog *catalog, 
		      const gchar *symbol_name,
		      gpointer *symbol_ptr)
{
	if (catalog->module == NULL)
		catalog->module = glade_util_load_library (catalog->library);
	
	if (catalog->module)
		return g_module_symbol (catalog->module, symbol_name, symbol_ptr);

	return FALSE;
}

static GladeCatalog *
catalog_open (const gchar *filename)
{
	GladeCatalog    *catalog;
	GladeXmlContext *context;
	GladeXmlDoc     *doc;
	GladeXmlNode    *root;

	/* get the context & root node of the catalog file */
	context = glade_xml_context_new_from_path (filename,
						   NULL, 
						   GLADE_TAG_GLADE_CATALOG);
	if (!context) 
	{
		g_warning ("Couldn't open catalog [%s].", filename);
		return NULL;
	}

	doc  = glade_xml_context_get_doc (context);
	root = glade_xml_doc_get_root (doc);

	if (!glade_xml_node_verify (root, GLADE_TAG_GLADE_CATALOG)) 
	{
		g_warning ("Catalog root node is not '%s', skipping %s",
			   GLADE_TAG_GLADE_CATALOG, filename);
		glade_xml_context_free (context);
		return NULL;
	}

	catalog = g_new0 (GladeCatalog, 1);
	catalog->context = context;
	catalog->name    = glade_xml_get_property_string (root, GLADE_TAG_NAME);

	if (!catalog->name) 
	{
		g_warning ("Couldn't find required property 'name' in catalog root node");
		g_free (catalog);
		glade_xml_context_free (context);
		return NULL;
	}
	
	catalog->language =
		glade_xml_get_property_string (root, GLADE_TAG_LANGUAGE);
	
	if (catalog->language && (glade_binding_get (catalog->language)) == NULL)
	{
		g_warning ("%s language is not supported. "
			   "Make sure the corresponding GladeBinding module is available.",
			   catalog->language);
		g_free (catalog->name);
		g_free (catalog->language);
		g_free (catalog);
		glade_xml_context_free (context);
		return NULL;
	}
	
	loaded_catalogs = g_list_prepend (loaded_catalogs, g_strdup (catalog->name));
	
	catalog->library =
		glade_xml_get_property_string (root, GLADE_TAG_LIBRARY);
	catalog->dep_catalog =
		glade_xml_get_property_string (root, GLADE_TAG_DEPENDS);
	catalog->domain =
		glade_xml_get_property_string (root, GLADE_TAG_DOMAIN);
	catalog->book =
		glade_xml_get_property_string (root, GLADE_TAG_BOOK);
	catalog->init_function_name =
		glade_xml_get_value_string (root, GLADE_TAG_INIT_FUNCTION);
	
	if (catalog->init_function_name && catalog->language == NULL)
		catalog_get_function (catalog, catalog->init_function_name,
				      (gpointer) &catalog->init_function);

	return catalog;
}


static void
catalog_load (GladeCatalog *catalog)
{
	GladeXmlDoc     *doc;
	GladeXmlNode    *root;
	GladeXmlNode    *node;

	g_return_if_fail (catalog->context != NULL);
	
	doc  = glade_xml_context_get_doc (catalog->context);
	root = glade_xml_doc_get_root (doc);
	node = glade_xml_node_get_children (root);

	for (; node; node = glade_xml_node_next (node))
	{
		const gchar *node_name;

		node_name = glade_xml_node_get_name (node);
		if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_CLASSES) == 0) 
		{
			catalog_load_classes (catalog, node);
		}
		else if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_GROUP) == 0)
		{
			catalog_load_group (catalog, node);
		}
		else 
			continue;
	}

	catalog->widget_groups = g_list_reverse (catalog->widget_groups);
	catalog->context =
		(glade_xml_context_free (catalog->context), NULL);

	return;
}

static gint
catalog_find_by_name (GladeCatalog *catalog, const gchar *name)
{
	return strcmp (catalog->name, name);
}

static GList *
catalog_sort (GList *catalogs)
{
	GList        *l, *node, *sorted = NULL, *sort;
	GladeCatalog *catalog, *cat;

	/* Add all dependant catalogs to the sorted list first */
	for (l = catalogs; l; l = l->next)
	{
		catalog = l->data;
		sort    = NULL;

		/* itterate ascending through dependancy hierarchy */
		while (catalog->dep_catalog) {
			node = g_list_find_custom
				(catalogs, catalog->dep_catalog,
				 (GCompareFunc)catalog_find_by_name);

			if ((cat = node->data) == NULL)
			{
				g_critical ("Catalog %s depends on catalog %s, not found",
					    catalog->name, catalog->dep_catalog);
				break;
			}			

			/* Prepend to sort list */
			if (g_list_find (sort, cat) == NULL &&
			    g_list_find (sorted, cat) == NULL)
				sort = g_list_prepend (sort, cat);

			catalog = cat;
		}
		sorted = g_list_concat (sorted, sort);
	}
	
	/* Append all independant catalogs after */
	for (l = catalogs; l; l = l->next)
		if (g_list_find (sorted, l->data) == NULL)
			sorted = g_list_append (sorted, l->data);

	g_list_free (catalogs);
	return sorted;
}

static GHashTable *modules = NULL;

static GModule *
catalog_load_library (GladeCatalog *catalog)
{
	GModule *module;

	if (modules == NULL)
		modules = g_hash_table_new (g_str_hash, g_str_equal);
		
	if (catalog->library == NULL) return NULL;

	if (catalog->language)
	{
		GladeBinding *binding = glade_binding_get (catalog->language);
		if (binding)
			glade_binding_library_load (binding, catalog->library);
		return NULL;
	}
	else
	{
		if ((module = g_hash_table_lookup (modules, catalog->library)))
			return module;	
		
		if ((module = glade_util_load_library (catalog->library)))
			g_hash_table_insert (modules, g_strdup (catalog->library), module);
		else
			g_warning ("Failed to load external library '%s'",
				   catalog->library);
		
		return module;
	}
}

static gboolean
catalog_load_classes (GladeCatalog *catalog, GladeXmlNode *widgets_node)
{
	GladeXmlNode *node;
	GModule *module = catalog_load_library (catalog);

	node = glade_xml_node_get_children (widgets_node);
	for (; node; node = glade_xml_node_next (node)) 
	{
		const gchar        *node_name;
		GladeWidgetAdaptor *adaptor;

		node_name = glade_xml_node_get_name (node);
		if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_CLASS) != 0) 
			continue;
	
		adaptor = glade_widget_adaptor_from_catalog 
			(node, catalog->name, module,
			 catalog->domain ? catalog->domain : catalog->library,
			 catalog->book);

		catalog->adaptors = g_list_prepend (catalog->adaptors, adaptor);
	}

	return TRUE;
}

static gboolean
catalog_load_group (GladeCatalog *catalog, GladeXmlNode *group_node)
{
	GladeWidgetGroup *group;
	GladeXmlNode     *node;

	group = g_new0 (GladeWidgetGroup, 1);
	
	group->name = glade_xml_get_property_string (group_node, 
						     GLADE_TAG_NAME);

	if (!group->name) 
	{ 
		g_warning ("Required property 'name' not found in group node");
	
		widget_group_free (group);
		return FALSE;
	}
	
	group->title = glade_xml_get_property_string (group_node,
						      GLADE_TAG_TITLE);
	if (!group->title) 
	{ 
		g_warning ("Required property 'title' not found in group node");

		widget_group_free (group);
		return FALSE;	
	}

	/* by default, group is expanded in palette  */
	group->expanded = TRUE;

	/* Translate it */
	group->title = dgettext (catalog->domain ? 
				 catalog->domain : catalog->library, 
				 group->title);

	group->adaptors = NULL;

	node = glade_xml_node_get_children (group_node);
	for (; node; node = glade_xml_node_next (node)) 
	{
		const gchar        *node_name;
		GladeWidgetAdaptor *adaptor;
		gchar              *name;

		node_name = glade_xml_node_get_name (node);
		
		if (strcmp (node_name, GLADE_TAG_GLADE_WIDGET_CLASS_REF) == 0)
		{
			if ((name = 
			     glade_xml_get_property_string (node, GLADE_TAG_NAME)) == NULL)
			{
				g_warning ("Couldn't find required property on %s",
					   GLADE_TAG_GLADE_WIDGET_CLASS);
				continue;
			}

			if ((adaptor = glade_widget_adaptor_get_by_name (name)) == NULL)
			{
				g_warning ("Tried to include undefined widget "
					   "class '%s' in a widget group", name);
				g_free (name);
				continue;
			}
			g_free (name);

			group->adaptors = g_list_prepend (group->adaptors, adaptor);

		}
		else if (strcmp (node_name, GLADE_TAG_DEFAULT_PALETTE_STATE) == 0)
		{
			group->expanded = 
				glade_xml_get_property_boolean 
				(node, GLADE_TAG_EXPANDED, group->expanded);
		}
	}

	group->adaptors = g_list_reverse (group->adaptors);

	catalog->widget_groups = g_list_prepend (catalog->widget_groups,
						 group);

	return TRUE;
}

static void
widget_group_free (GladeWidgetGroup *group)
{
	g_return_if_fail (group != NULL);
	
	g_free (group->name);
	g_free (group->title);

	/* The actual widget classes will be free elsewhere */
	g_list_free (group->adaptors);
}
	
GList *
glade_catalog_load_all (void)
{
	GDir         *dir;
	GError       *error = NULL;
	const gchar  *filename;
	GList        *catalogs, *l;
	GladeCatalog *catalog;
	
	/* Read all files in catalog dir */
	dir = g_dir_open (glade_catalogs_dir, 0, &error);
	if (!dir) 
	{
		g_warning ("Failed to open catalog directory: %s",
			   error->message);
		return NULL;
	}

	/* Catalogs need dependancies, most catalogs depend on
	 * the gtk+ catalog, but some custom toolkits may depend
	 * on the gnome catalog for instance.
	 */
	catalogs = NULL;
	while ((filename = g_dir_read_name (dir)))
	{
		gchar *catalog_filename;

		if (!g_str_has_suffix (filename, ".xml")) 
			continue;

		catalog_filename = g_build_filename (glade_catalogs_dir,
						     filename, NULL);
		catalog = catalog_open (catalog_filename);
		g_free (catalog_filename);

		if (catalog) 
			catalogs = g_list_prepend (catalogs, catalog);
		else
			g_warning ("Unable to open the catalog file %s.\n", 
				   filename);
	}

	/* After sorting, execute init function and then load */
	catalogs = catalog_sort (catalogs);
	
	for (l = catalogs; l; l = l->next)
	{
		catalog = l->data;
		if (catalog->init_function) catalog->init_function ();
	}
	
	for (l = catalogs; l; l = l->next)
	{
		catalog = l->data;
		catalog_load (catalog);
	}
	
	g_dir_close (dir);
	return catalogs;
}

const gchar *
glade_catalog_get_name (GladeCatalog *catalog)
{
	g_return_val_if_fail (catalog != NULL, NULL);

	return catalog->name;
}

GList *
glade_catalog_get_widget_groups (GladeCatalog *catalog)
{
	g_return_val_if_fail (catalog != NULL, NULL);

	return catalog->widget_groups;	
}

GList *
glade_catalog_get_adaptors (GladeCatalog *catalog)
{
	g_return_val_if_fail (catalog != NULL, NULL);

	return catalog->adaptors;	
}

void
glade_catalog_free (GladeCatalog *catalog)
{
	GList *list;

	if (catalog == NULL)
		return;

	g_free (catalog->name);
	if (catalog->book)
		g_free (catalog->book);
	
	for (list = catalog->adaptors; list; list = list->next)
		g_object_unref (list->data);
	
	g_list_free (catalog->adaptors);

	for (list = catalog->widget_groups; list; list = list->next) 
		widget_group_free (GLADE_WIDGET_GROUP (list->data));
				   
	g_list_free (catalog->widget_groups);
				   
	
	g_free (catalog);
}
const gchar *
glade_widget_group_get_name (GladeWidgetGroup *group)
{
	g_return_val_if_fail (group != NULL, NULL);

	return group->name;
}

const gchar *
glade_widget_group_get_title (GladeWidgetGroup *group)
{
	g_return_val_if_fail (group != NULL, NULL);

	return group->title;
}

gboolean
glade_widget_group_get_expanded (GladeWidgetGroup *group)
{
	g_return_val_if_fail (group != NULL, FALSE);

	return group->expanded;
}

GList *
glade_widget_group_get_adaptors (GladeWidgetGroup *group)
{
	g_return_val_if_fail (group != NULL, NULL);

	return group->adaptors;
}

gboolean
glade_catalog_is_loaded (const gchar *name)
{
	GList *l;
	g_return_val_if_fail (name != NULL, FALSE);
	for (l = loaded_catalogs; l; l = l->next)
		if (!strcmp (name, (gchar *)l->data))
			return TRUE;
	return FALSE;
}


static void 
catalog_module_close (gpointer key, gpointer value, gpointer user_data)
{
	g_module_close (value);
	g_free (key);
}

/**
 * glade_catalog_modules_close:
 *
 * Close every opened module.
 *
 */
void
glade_catalog_modules_close ()
{
	if (modules == NULL) return;
		
	g_hash_table_foreach (modules, catalog_module_close, NULL);
	g_hash_table_destroy (modules);
	modules = NULL;
}