File: e-categories.c

package info (click to toggle)
evolution-data-server 1.6.3-5etch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 59,384 kB
  • ctags: 43,218
  • sloc: ansic: 319,315; tcl: 30,499; xml: 19,166; sh: 18,776; perl: 11,529; cpp: 8,259; java: 7,653; makefile: 6,448; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (499 lines) | stat: -rw-r--r-- 13,012 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Copyright (C) 2005 Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License 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 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 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.
 */

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

#include <string.h>
#include <libxml/parser.h>
#include <glib/ghash.h>
#include <glib/gi18n-lib.h>
#include <gconf/gconf-client.h>
#include "e-categories.h"

#include "libedataserver-private.h"

typedef struct {
	char *category;
	char *icon_file;
	char *color;
	gboolean searchable;
} CategoryInfo;

static gboolean initialized = FALSE;
static GHashTable *categories_table = NULL;
static GConfClient *conf_client = NULL;
static gboolean conf_is_dirty = FALSE;
static guint idle_id = 0;

static void
free_category_info (CategoryInfo *cat_info)
{
	if (cat_info->category)
		g_free (cat_info->category);
	if (cat_info->icon_file)
		g_free (cat_info->icon_file);
	if (cat_info->color)
		g_free (cat_info->color);

	g_free (cat_info);
}

static char *
escape_string (const char *source)
{
	GString *str;
	char *dest;

	str = g_string_new ("");

	while (*source) {
		switch (*source) {
                case '<':
                        str = g_string_append (str, "&lt;");
                        break;
                case '>':
                        str = g_string_append (str, "&gt;");
                        break;
                case '&':
                        str = g_string_append (str, "&amp;");
                        break;
                case '"':
                        str = g_string_append (str, "&quot;");
                        break;

                default:
                        str = g_string_append_c (str, *source);
                        break;
                }
                source++;
	}

	dest = str->str;
	g_string_free (str, FALSE);

	return dest;
}

static void
hash_to_xml_string (gpointer key, gpointer value, gpointer user_data)
{
	CategoryInfo *cat_info = value;
	GString **str = user_data;
	char *s;

	*str = g_string_append (*str, "<category a=\"");

	s = escape_string (cat_info->category);
	*str = g_string_append (*str, s);
	g_free (s);

	*str = g_string_append_c (*str, '"');
	if (cat_info->color) {
		*str = g_string_append (*str, " color=\"");
		*str = g_string_append (*str, cat_info->color);
		*str = g_string_append_c (*str, '"');
	}
	if (cat_info->icon_file) {
		*str = g_string_append (*str, " icon=\"");
		*str = g_string_append (*str, cat_info->icon_file);
		*str = g_string_append_c (*str, '"');
	}

	*str = g_string_append (*str, " searchable=\"");
	*str = g_string_append_c (*str, cat_info->searchable ? '1' : '0');
	*str = g_string_append_c (*str, '"');

	*str = g_string_append (*str, "/>");
}

static gboolean
idle_saver_cb (gpointer user_data)
{
	if (conf_is_dirty) {
		GString *str = g_string_new ("<categories>");

		g_hash_table_foreach (categories_table, (GHFunc) hash_to_xml_string, &str);
		str = g_string_append (str, "</categories>");
		gconf_client_set_string (conf_client, "/apps/evolution/general/category_master_list", str->str, NULL);

		g_string_free (str, TRUE);

		conf_is_dirty = FALSE;
	}

	idle_id = 0;

	return FALSE;
}

static void
save_config (void)
{
	conf_is_dirty = TRUE;
	if (!idle_id) {
		idle_id = g_idle_add ((GSourceFunc) idle_saver_cb, NULL);
	}
}

static void
cleanup_at_exit (void)
{
	if (conf_is_dirty)
		idle_saver_cb (NULL);

	if (idle_id > 0) {
		g_source_remove (idle_id);
		idle_id = 0;
	}

	if (categories_table) {
		g_hash_table_destroy (categories_table);
		categories_table = NULL;
	}

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

	initialized = FALSE;
}

static void
e_categories_add_relative (const char *category, const char *color, const char *icon_file, gboolean searchable)
{
	char *full_path = g_build_filename (E_DATA_SERVER_IMAGESDIR, icon_file, NULL);
	e_categories_add (category, color, full_path, searchable);
	g_free (full_path);
}

static void
initialize_categories_config (void)
{
	char *str;

	if (initialized)
		return;

	initialized = TRUE;

	/* create all the internal data we need */
	categories_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) free_category_info);

	conf_client = gconf_client_get_default ();

	g_atexit (cleanup_at_exit);

	/* load the categories config from the config database */
	str = gconf_client_get_string (conf_client, "/apps/evolution/general/category_master_list", NULL);
	if (str) {
		xmlDoc *doc;
		xmlNode *node, *children;

		doc = xmlParseMemory (str, strlen (str));
		if (doc) {
			xmlChar *name, *color, *icon, *searchable;

			node = xmlDocGetRootElement (doc);
			for (children = node->xmlChildrenNode; children != NULL; children = children->next) {
				gboolean b = TRUE;

				name = xmlGetProp (children, "a");
				color = xmlGetProp (children, "color");
				icon = xmlGetProp (children, "icon");
				searchable = xmlGetProp (children, "searchable");

				if (searchable) {
					if (!strcmp (searchable, "0"))
						b = FALSE;
				}

				e_categories_add (name, color, icon, b ? TRUE : FALSE);
				xmlFree (name);
				xmlFree (color);
				xmlFree (icon);
				xmlFree (searchable);
			}

			xmlFreeDoc (doc);
		}

		g_free (str);

		conf_is_dirty = FALSE;
	}
	else {
		/* Add default categories only if gconf doesn't contain any category list */
		/* Make sure we have all categories */
		e_categories_add (_("Anniversary"), NULL, NULL, TRUE);
		e_categories_add_relative (_("Birthday"), NULL, "category_birthday_16.png", TRUE);
		e_categories_add_relative (_("Business"), NULL, "category_business_16.png", TRUE);
		e_categories_add (_("Competition"), NULL, NULL, TRUE);
		e_categories_add_relative (_("Favorites"), NULL, "category_favorites_16.png", TRUE);
		e_categories_add_relative (_("Gifts"), NULL, "category_gifts_16.png", TRUE);
		e_categories_add_relative (_("Goals/Objectives"), NULL, "category_goals_16.png", TRUE);
		e_categories_add_relative (_("Holiday"), NULL, "category_holiday_16.png", TRUE);
		e_categories_add_relative (_("Holiday Cards"), NULL, "category_holiday-cards_16.png", TRUE);
		/* important people (e.g. new business partners) you should pay attention to/observe */
		e_categories_add_relative (_("Hot Contacts"), NULL, "category_hot-contacts_16.png", TRUE);
		e_categories_add_relative (_("Ideas"), NULL, "category_ideas_16.png", TRUE);
		e_categories_add_relative (_("International"), NULL, "category_international_16.png", TRUE);
		e_categories_add_relative (_("Key Customer"), NULL, "category_key-customer_16.png", TRUE);
		e_categories_add_relative (_("Miscellaneous"), NULL, "category_miscellaneous_16.png", TRUE);
		e_categories_add_relative (_("Personal"), NULL, "category_personal_16.png", TRUE);
		e_categories_add_relative (_("Phone Calls"), NULL, "category_phonecalls_16.png", TRUE);
		e_categories_add_relative (_("Status"), NULL, "category_status_16.png", TRUE);
		e_categories_add_relative (_("Strategies"), NULL, "category_strategies_16.png", TRUE);
		e_categories_add_relative (_("Suppliers"), NULL, "category_suppliers_16.png", TRUE);
		e_categories_add_relative (_("Time & Expenses"), NULL, "category_time-and-expenses_16.png", TRUE);
		e_categories_add (_("VIP"), NULL, NULL, TRUE);
        	e_categories_add (_("Waiting"), NULL, NULL, TRUE);

		save_config ();
	}
}

static void
add_hash_to_list (gpointer key, gpointer value, gpointer user_data)
{
	GList **list = user_data;

	*list = g_list_append (*list, key);
}

/**
 * e_categories_get_list:
 *
 * Returns a list of all the category names currently configured.
 *
 * Return value: a GList containing the names of the categories. The list
 * should be freed using g_list_free, but the names of the categories should
 * not be touched at all, they are internal strings.
 */
GList *
e_categories_get_list (void)
{
	GList *list = NULL;

	if (!initialized)
		initialize_categories_config ();

	g_hash_table_foreach (categories_table, (GHFunc) add_hash_to_list, &list);

	return list;
}

/**
 * e_categories_add:
 * @category: name of category to add.
 * @color: associated color.
 * @icon_file: full path of the icon associated to the category.
 * @searchable: whether the category can be used for searching in the GUI.
 *
 * Adds a new category, with its corresponding color and icon, to the
 * configuration database.
 */
void
e_categories_add (const char *category, const char *color, const char *icon_file, gboolean searchable)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	/* remove the category if already in the hash table */
	if (g_hash_table_lookup (categories_table, category))
		g_hash_table_remove (categories_table, category);

	/* add the new category */
	cat_info = g_new0 (CategoryInfo, 1);
	cat_info->category = g_strdup (category);
	cat_info->color = color ? g_strdup (color) : NULL;
	cat_info->icon_file = icon_file ? g_strdup (icon_file) : NULL;
	cat_info->searchable = searchable;

	g_hash_table_insert (categories_table, g_strdup (category), cat_info);

	save_config ();
}

/**
 * e_categories_remove:
 * @category: category to be removed.
 *
 * Removes the given category from the configuration.
 */
void
e_categories_remove (const char *category)
{
	g_return_if_fail (category != NULL);

	if (!initialized)
		initialize_categories_config ();

	if (g_hash_table_lookup (categories_table, category)) {
		g_hash_table_remove (categories_table, category);

		save_config ();
	}
}

/**
 * e_categories_exist:
 * @category: category to be searched.
 *
 * Checks whether the given category is available in the configuration.
 *
 * Return value: %TRUE if the category is available, %FALSE otherwise.
 */
gboolean
e_categories_exist (const char *category)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);

	return cat_info ? TRUE : FALSE;
}

/**
 * e_categories_get_color_for:
 * @category: category to retrieve the color for.
 *
 * Gets the color associated with the given category.
 *
 * Return value: a string representation of the color.
 */
const char *
e_categories_get_color_for (const char *category)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);
	if (!cat_info)
		return NULL;

	return (const char *) cat_info->color;
}

/**
 * e_categories_set_color_for:
 * @category: category to set the color for.
 * @color: X color.
 *
 * Sets the color associated with the given category.
 */
void
e_categories_set_color_for (const char *category, const char *color)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);
	if (!cat_info)
		return;

	if (cat_info->color)
		g_free (cat_info->color);
	cat_info->color = g_strdup (color);

	save_config ();
}

/**
 * e_categories_get_icon_file_for:
 * @category: category to retrieve the icon file for.
 *
 * Gets the icon file associated with the given category.
 *
 * Return value: a string representation of the color.
 */
const char *
e_categories_get_icon_file_for (const char *category)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);
	if (!cat_info)
		return NULL;

	return (const char *) cat_info->icon_file;
}

/**
 * e_categories_set_icon_file_for:
 * @category: category to set the icon file for.
 * @color: X color.
 *
 * Sets the icon file associated with the given category.
 */
void
e_categories_set_icon_file_for (const char *category, const char *icon_file)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);
	if (!cat_info)
		return;

	if (cat_info->icon_file)
		g_free (cat_info->icon_file);
	cat_info->icon_file = g_strdup (icon_file);

	save_config ();
}

/**
 * e_categories_is_searchable:
 * @category: category name.
 *
 * Gets whether the given calendar is to be used for searches in the GUI.
 *
 * Return value; %TRUE% if the category is searchable, %FALSE% if not.
 */
gboolean
e_categories_is_searchable (const char *category)
{
	CategoryInfo *cat_info;

	if (!initialized)
		initialize_categories_config ();

	cat_info = g_hash_table_lookup (categories_table, category);
	if (!cat_info)
		return FALSE;

	return cat_info->searchable;
}