+2007-12-14 Jim Evins <evins@snaught.com>
+
+ * libglabels/db.c:
+ Optimize template search by name by using a GHashTable.
+ * src/mini-preview-pixbuf-cache.c:
+ * src/mini-preview-pixbuf-cache.h:
+ Store one pixbuf per template, but use a key for each alias.
+ * src/wdgt-media-select.c:
+ Optimize loading of tree data, thus optimizing the "new label" dialog.
+
2007-12-12 Jim Evins <evins@snaught.com>
* data/templates/avery-us-templates.xml:
#include <glib/gstrfuncs.h>
#include <glib/gdir.h>
#include <glib/gmessages.h>
+#include <glib/ghash.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
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);
}
lgl_db_free_paper_id_list (page_sizes);
+ init_template_cache ();
}
}
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
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;
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);
- new_template = lgl_template_dup (template);
+ new_template = lgl_template_dup (template);
new_template->brand = g_strdup (alias->brand);
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)
{
void
gl_mini_preview_pixbuf_cache_init (void)
{
- GList *names = NULL;
- GList *p;
+ GList *names = NULL;
+ GList *p;
+ lglTemplate *template;
gl_debug (DEBUG_PIXBUF_CACHE, "START");
for ( p=names; p != NULL; p=p->next )
{
gl_debug (DEBUG_PIXBUF_CACHE, "name = \"%s\"", p->data);
- gl_mini_preview_pixbuf_cache_add_by_name ((gchar *)p->data);
+
+ template = lgl_db_lookup_template_from_name (p->data);
+ gl_mini_preview_pixbuf_cache_add_by_template (template);
+ lgl_template_free (template);
}
lgl_db_free_template_name_list (names);
gl_debug (DEBUG_PIXBUF_CACHE, "END pixbuf_cache=%p", mini_preview_pixbuf_cache);
}
+/*****************************************************************************/
+/* Add pixbuf to cache by template. */
+/*****************************************************************************/
+void
+gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template)
+{
+ GdkPixbuf *pixbuf;
+ GList *p;
+ lglTemplateAlias *alias;
+ gchar *name;
+
+ gl_debug (DEBUG_PIXBUF_CACHE, "START");
+
+ pixbuf = gl_mini_preview_pixbuf_new (template, 72, 72);
+
+ for ( p=template->aliases; p != NULL; p=p->next )
+ {
+ alias = (lglTemplateAlias *)p->data;
+
+ name = g_strdup_printf ("%s %s", alias->brand, alias->part);
+ g_hash_table_insert (mini_preview_pixbuf_cache, name, g_object_ref (pixbuf));
+ }
+
+ g_object_unref (pixbuf);
+
+ gl_debug (DEBUG_PIXBUF_CACHE, "END");
+}
+
/*****************************************************************************/
/* Add pixbuf to cache by name. */
/*****************************************************************************/
#ifndef __MINI_PREVIEW_PIXBUF_CACHE_H__
#define __MINI_PREVIEW_PIXBUF_CACHE_H__
+#include <libglabels/template.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
G_BEGIN_DECLS
-void gl_mini_preview_pixbuf_cache_init (void);
+void gl_mini_preview_pixbuf_cache_init (void);
-void gl_mini_preview_pixbuf_cache_add_by_name (gchar *name);
+void gl_mini_preview_pixbuf_cache_add_by_name (gchar *name);
+void gl_mini_preview_pixbuf_cache_add_by_template (lglTemplate *template);
-GdkPixbuf *gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name);
+GdkPixbuf *gl_mini_preview_pixbuf_cache_get_pixbuf (gchar *name);
G_END_DECLS
gchar *size;
gchar *layout;
gchar *description;
- gchar *name;
gl_debug (DEBUG_MEDIA_SELECT, "START");
gl_debug (DEBUG_MEDIA_SELECT, "p->data = \"%s\"", p->data);
template = lgl_db_lookup_template_from_name (p->data);
-
- name = lgl_template_get_name (template);
- pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (name);
- g_free (name);
+ pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (p->data);
size = get_label_size_desc (template);
layout = get_layout_desc (template);
gchar *size;
gchar *layout;
gchar *description;
- gchar *name;
gl_debug (DEBUG_MEDIA_SELECT, "START");
gl_debug (DEBUG_MEDIA_SELECT, "p->data = \"%s\"", p->data);
template = lgl_db_lookup_template_from_name (p->data);
-
- name = lgl_template_get_name (template);
- pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (name);
- g_free (name);
+ pixbuf = gl_mini_preview_pixbuf_cache_get_pixbuf (p->data);
size = get_label_size_desc (template);
layout = get_layout_desc (template);