]> git.sur5r.net Git - glabels/blobdiff - glabels2/libglabels/db.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / libglabels / db.c
index 8e4f1924a6ca5730a8985c658293b1c631912673..4a582e8d60c97632bce4194e57abd5efb1f50ae8 100644 (file)
@@ -1,38 +1,29 @@
-/* -*- 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>
@@ -48,6 +39,7 @@
 /* Private types                             */
 /*===========================================*/
 
+
 /*===========================================*/
 /* Private globals                           */
 /*===========================================*/
@@ -56,10 +48,16 @@ static GList *papers     = NULL;
 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);
@@ -72,8 +70,7 @@ static GList *read_templates               (void);
 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);
 
 
 /*===========================================*/
@@ -109,7 +106,7 @@ lgl_db_init (void)
                 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);
 
        }
@@ -146,11 +143,11 @@ lgl_db_init (void)
                 }
                 lgl_db_free_paper_id_list (page_sizes);
 
+                init_template_cache ();
        }
 }
 
 
-
 /*===========================================*/
 /* Paper db functions.                       */
 /*===========================================*/
@@ -436,6 +433,7 @@ lgl_db_is_paper_id_known (const gchar *id)
        return FALSE;
 }
 
+
 /**
  * lgl_db_is_paper_id_other:
  * @id: paper id to test
@@ -956,7 +954,6 @@ lgl_db_print_known_categories (void)
 }
 
 
-
 /*===========================================*/
 /* Brand db functions.                       */
 /*===========================================*/
@@ -999,11 +996,11 @@ lgl_db_get_brand_list (const gchar *paper_id,
                                 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);
                                 }
                        }
                }
@@ -1036,7 +1033,6 @@ lgl_db_free_brand_list (GList *brands)
 }
 
 
-
 /*===========================================*/
 /* Template db functions.                    */
 /*===========================================*/
@@ -1083,11 +1079,11 @@ lgl_db_register_template (const lglTemplate *template)
                         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;
                 }
        }
@@ -1146,6 +1142,53 @@ lgl_db_does_template_exist (const gchar *brand,
 }
 
 
+/**
+ * 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
@@ -1287,10 +1330,11 @@ lgl_db_free_template_name_list (GList *names)
 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)
         {
@@ -1303,27 +1347,77 @@ lgl_db_lookup_template_from_name (const gchar *name)
                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)
 {
@@ -1482,6 +1576,7 @@ lgl_db_print_known_templates (void)
 
 }
 
+
 /**
  * lgl_db_print_aliases:
  *   @template: template
@@ -1508,3 +1603,12 @@ lgl_db_print_aliases (const lglTemplate *template)
 }
 
 
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */