-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-
/*
- * (LIBGLABELS) Template library for GLABELS
- *
- * db.c: template db module
+ * db.c
+ * Copyright (C) 2003-2009 Jim Evins <evins@snaught.com>.
*
- * Copyright (C) 2003-2007 Jim Evins <evins@snaught.com>.
+ * This file is part of libglabels.
*
- * This file is part of the LIBGLABELS library.
+ * libglabels 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 3 of the License, or
+ * (at your option) any later version.
*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library 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,
+ * libglabels 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
- * Library General Public License for more details.
+ * 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 Library 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
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libglabels. If not, see <http://www.gnu.org/licenses/>.
*/
+
#include <config.h>
#include "db.h"
#include <glib/gi18n.h>
-#include <glib/gmem.h>
-#include <glib/gstrfuncs.h>
-#include <glib/gdir.h>
-#include <glib/gmessages.h>
+#include <glib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
/* Private types */
/*===========================================*/
+
/*===========================================*/
/* Private globals */
/*===========================================*/
static GList *categories = NULL;
static GList *templates = NULL;
+static GHashTable *template_cache = NULL;
+
+
/*===========================================*/
/* Local function prototypes */
/*===========================================*/
+static void init_template_cache (void);
+static void add_to_template_cache (lglTemplate *template);
+
static GList *read_papers (void);
static GList *read_paper_files_from_dir (GList *papers,
const gchar *dirname);
static GList *read_template_files_from_dir (GList *templates,
const gchar *dirname);
-static lglTemplate *template_full_page (const gchar *page_size);
-
+static lglTemplate *template_full_page (const gchar *page_size);
/*===========================================*/
papers = read_papers ();
/* Create and append an "Other" entry. */
- paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0);
+ paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
papers = g_list_append (papers, paper_other);
}
}
lgl_db_free_paper_id_list (page_sizes);
+ init_template_cache ();
}
}
-
/*===========================================*/
/* Paper db functions. */
/*===========================================*/
return FALSE;
}
+
/**
* lgl_db_is_paper_id_other:
* @id: paper id to test
}
-
/*===========================================*/
/* Brand db functions. */
/*===========================================*/
alias = (lglTemplateAlias *)p_alias->data;
if ( !g_list_find_custom (brands, alias->brand,
- (GCompareFunc)g_utf8_collate) )
+ (GCompareFunc)lgl_str_utf8_casecmp) )
{
brands = g_list_insert_sorted (brands,
g_strdup (alias->brand),
- (GCompareFunc)g_utf8_collate);
+ (GCompareFunc)lgl_str_utf8_casecmp);
}
}
}
}
-
/*===========================================*/
/* Template db functions. */
/*===========================================*/
template_copy = lgl_template_dup (template);
lgl_template_add_category (template_copy, "user-defined");
templates = g_list_append (templates, template_copy);
+ add_to_template_cache (template_copy);
return LGL_DB_REG_OK;
}
else
{
- lgl_template_free (template_copy);
return LGL_DB_REG_FILE_WRITE_ERROR;
}
}
}
+/**
+ * lgl_db_does_template_name_exist:
+ * @name: name string
+ *
+ * This function test whether a template with the given name exists.
+ *
+ * Returns: TRUE if such a template exists in the database.
+ *
+ */
+gboolean
+lgl_db_does_template_name_exist (const gchar *name)
+{
+ GList *p_tmplt, *p_alias;
+ lglTemplate *template;
+ lglTemplateAlias *alias;
+ gchar *candidate_name;
+
+ if (!templates)
+ {
+ lgl_db_init ();
+ }
+
+ if (name == NULL)
+ {
+ return FALSE;
+ }
+
+ for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
+ {
+ template = (lglTemplate *) p_tmplt->data;
+ for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
+ {
+ alias = (lglTemplateAlias *)p_alias->data;
+ candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
+
+ if ( UTF8_EQUAL (candidate_name, name) ) {
+ g_free (candidate_name);
+ return TRUE;
+ }
+ g_free (candidate_name);
+ }
+ }
+
+ return FALSE;
+}
+
+
/**
* lgl_db_get_template_name_list_unique:
* @brand: If non NULL, limit results to given brand
lglTemplate *
lgl_db_lookup_template_from_name (const gchar *name)
{
- GList *p_tmplt, *p_alias;
+ GList *p_alias;
lglTemplate *template;
lglTemplateAlias *alias;
gchar *candidate_name;
+ lglTemplate *new_template;
if (!templates)
{
return lgl_template_dup ((lglTemplate *) templates->data);
}
- for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
+ template = g_hash_table_lookup (template_cache, name);
+
+ if (template)
{
- template = (lglTemplate *) p_tmplt->data;
- for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
+ for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
{
alias = (lglTemplateAlias *)p_alias->data;
candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
- if ( UTF8_EQUAL (candidate_name, name) ) {
+ if ( UTF8_EQUAL (candidate_name, name) )
+ {
g_free (candidate_name);
- return lgl_template_dup (template);
- }
+ new_template = lgl_template_dup (template);
+ g_free (new_template->brand);
+ new_template->brand = g_strdup (alias->brand);
+ g_free (new_template->part);
+ new_template->part = g_strdup (alias->part);
+ return new_template;
+ }
+
g_free (candidate_name);
- }
- }
+ }
+ }
/* No matching template has been found so return the first template */
return lgl_template_dup ((lglTemplate *) templates->data);
}
+static void
+init_template_cache (void)
+{
+ GList *p_tmplt, *p_alias;
+ lglTemplate *template;
+ lglTemplateAlias *alias;
+ gchar *name;
+
+ template_cache = g_hash_table_new (g_str_hash, g_str_equal);
+
+ for ( p_tmplt=templates; p_tmplt != NULL; p_tmplt=p_tmplt->next )
+ {
+ template = (lglTemplate *) p_tmplt->data;
+
+ for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
+ {
+ alias = (lglTemplateAlias *)p_alias->data;
+ name = g_strdup_printf ("%s %s", alias->brand, alias->part);
+
+ g_hash_table_insert (template_cache, name, template);
+ }
+ }
+}
+
+
+static void
+add_to_template_cache (lglTemplate *template)
+{
+ GList *p_alias;
+ lglTemplateAlias *alias;
+ gchar *name;
+
+ for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
+ {
+ alias = (lglTemplateAlias *)p_alias->data;
+ name = g_strdup_printf ("%s %s", alias->brand, alias->part);
+
+ g_hash_table_insert (template_cache, name, template);
+ }
+}
+
+
static GList *
read_templates (void)
{
}
+
/**
* lgl_db_print_aliases:
* @template: template
}
+
+/*
+ * Local Variables: -- emacs
+ * mode: C -- emacs
+ * c-basic-offset: 8 -- emacs
+ * tab-width: 8 -- emacs
+ * indent-tabs-mode: nil -- emacs
+ * End: -- emacs
+ */