1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (LIBGLABELS) Template library for GLABELS
6 * category.c: template category module
8 * Copyright (C) 2001-2006 Jim Evins <evins@snaught.com>.
10 * This file is part of the LIBGLABELS library.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 #include <glib/gi18n.h>
32 #include <glib/gmem.h>
33 #include <glib/gstrfuncs.h>
34 #include <glib/gmessages.h>
35 #include <glib/gdir.h>
38 #include "libglabels-private.h"
40 #include "xml-category.h"
42 /*===========================================*/
44 /*===========================================*/
47 /*===========================================*/
49 /*===========================================*/
51 static GList *categories = NULL;
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static GList *read_categories (void);
60 static GList *read_category_files_from_dir (GList *categories,
61 const gchar *dirname);
67 * Initialize libglabels category module by reading all category definition
68 * files located in system and user template directories.
71 gl_category_init (void)
76 return; /* Already initialized. */
79 categories = read_categories ();
85 * @id: Id of category definition. (E.g. label, card, etc.) Should be
87 * @name: Localized name of category.
89 * Allocates and constructs a new #glCategory structure.
91 * Returns: a pointer to a newly allocated #glCategory structure.
95 gl_category_new (gchar *id,
100 category = g_new0 (glCategory,1);
101 category->id = g_strdup (id);
102 category->name = g_strdup (name);
110 * @orig: #glCategory structure to be duplicated.
112 * Duplicates an existing #glCategory structure.
114 * Returns: a pointer to a newly allocated #glCategory structure.
117 glCategory *gl_category_dup (const glCategory *orig)
119 glCategory *category;
121 g_return_val_if_fail (orig, NULL);
123 category = g_new0 (glCategory,1);
125 category->id = g_strdup (orig->id);
126 category->name = g_strdup (orig->name);
134 * @category: pointer to #glCategory structure to be freed.
136 * Free all memory associated with an existing #glCategory structure.
139 void gl_category_free (glCategory *category)
142 if ( category != NULL ) {
144 g_free (category->id);
147 g_free (category->name);
148 category->name = NULL;
157 * gl_category_get_name_list:
159 * Get a list of all localized category names known to libglabels.
161 * Returns: a list of localized category names.
165 gl_category_get_name_list (void)
169 glCategory *category;
175 for ( p=categories; p != NULL; p=p->next ) {
176 category = (glCategory *)p->data;
177 names = g_list_append (names, g_strdup (category->name));
185 * gl_category_free_name_list:
186 * @names: List of localized category name strings to be freed.
188 * Free up all storage associated with a name list obtained with
189 * gl_category_get_name_list().
193 gl_category_free_name_list (GList *names)
197 for (p = names; p != NULL; p = p->next) {
207 * gl_category_from_id:
208 * @id: category id string
210 * Lookup category definition from id string.
212 * Returns: pointer to a newly allocated #glCategory structure.
216 gl_category_from_id (const gchar *id)
219 glCategory *category;
226 /* If no id, return first category as a default */
227 return gl_category_dup ((glCategory *) categories->data);
230 for (p = categories; p != NULL; p = p->next) {
231 category = (glCategory *) p->data;
232 if (g_strcasecmp (category->id, id) == 0) {
233 return gl_category_dup (category);
242 * gl_category_from_name:
243 * @name: localized category name string
245 * Lookup category definition from localized category name string.
247 * Returns: pointer to a newly allocated #glCategory structure.
251 gl_category_from_name (const gchar *name)
254 glCategory *category;
261 /* If no name, return first category as a default */
262 return gl_category_dup ((glCategory *) categories->data);
265 for (p = categories; p != NULL; p = p->next) {
266 category = (glCategory *) p->data;
267 if (g_strcasecmp (category->name, name) == 0) {
268 return gl_category_dup (category);
277 * gl_category_lookup_id_from_name:
278 * @name: localized category name stringp
280 * Lookup category name string from localized category name string.
282 * Returns: pointer to a newly allocated id string.
286 gl_category_lookup_id_from_name (const gchar *name)
288 glCategory *category = NULL;
293 category = gl_category_from_name (name);
294 if ( category != NULL )
296 id = g_strdup (category->id);
297 gl_category_free (category);
306 * gl_category_lookup_name_from_id:
307 * @id: category id string
309 * Lookup localized category name string from category id string.
311 * Returns: pointer to a newly allocated localized category name string.
315 gl_category_lookup_name_from_id (const gchar *id)
317 glCategory *category = NULL;
322 category = gl_category_from_id (id);
323 if ( category != NULL )
325 name = g_strdup (category->name);
326 gl_category_free (category);
334 /*--------------------------------------------------------------------------*/
335 /* PRIVATE. Read categories from various files. */
336 /*--------------------------------------------------------------------------*/
338 read_categories (void)
341 GList *categories = NULL;
343 data_dir = GL_SYSTEM_DATA_DIR;
344 categories = read_category_files_from_dir (categories, data_dir);
347 data_dir = GL_USER_DATA_DIR;
348 categories = read_category_files_from_dir (categories, data_dir);
351 if (categories == NULL) {
352 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
358 /*--------------------------------------------------------------------------*/
359 /* PRIVATE. Read all category files from given directory. Append to list. */
360 /*--------------------------------------------------------------------------*/
362 read_category_files_from_dir (GList *categories,
363 const gchar *dirname)
366 const gchar *filename, *extension;
367 gchar *full_filename = NULL;
368 GError *gerror = NULL;
369 GList *new_categories = NULL;
371 if (dirname == NULL) {
375 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
379 dp = g_dir_open (dirname, 0, &gerror);
380 if (gerror != NULL) {
381 g_message ("cannot open data directory: %s", gerror->message );
385 while ((filename = g_dir_read_name (dp)) != NULL) {
387 extension = strrchr (filename, '.');
389 if (extension != NULL) {
391 if ( (g_strcasecmp (extension, ".categories") == 0)
392 || (g_strcasecmp (filename, "categories.xml") == 0) ) {
395 g_build_filename (dirname, filename, NULL);
397 gl_xml_category_read_categories_from_file (full_filename);
398 g_free (full_filename);
400 categories = g_list_concat (categories, new_categories);
401 new_categories = NULL;