[glabels] Use GConf to store font history



commit b69e6e688733168b847f4c0785f75e21b5f77dfd
Author: Jim Evins <evins snaught com>
Date:   Sat Oct 24 07:59:11 2009 -0400

    Use GConf to store font history
    
    glFontHistoryModel now front-ends GConf to store font history between
    sessions.  Also, don't store default font family when creating a blank
    label text object.

 src/font-combo-menu.c    |   14 +++--
 src/font-history-model.c |  152 ++++++++++++++++++++++++++++++++++++----------
 src/font-history-model.h |   11 ++--
 src/font-util.c          |   19 ++++++
 src/font-util.h          |    1 +
 src/label-text.c         |    2 -
 6 files changed, 153 insertions(+), 46 deletions(-)
---
diff --git a/src/font-combo-menu.c b/src/font-combo-menu.c
index 4319003..2a0a7c5 100644
--- a/src/font-combo-menu.c
+++ b/src/font-combo-menu.c
@@ -114,11 +114,11 @@ gl_font_combo_menu_class_init (glFontComboMenuClass *class)
 static void
 gl_font_combo_menu_init (glFontComboMenu *this)
 {
-        gint         i;
-        GtkWidget   *menu_item;
-        GtkWidget   *sub_menu;
-        const GList *list;
-        GList       *p;
+        gint          i;
+        GtkWidget    *menu_item;
+        GtkWidget    *sub_menu;
+        const GList  *list;
+        GList        *p;
 
 	this->priv = g_new0 (glFontComboMenuPrivate, 1);
 
@@ -272,7 +272,7 @@ new_font_sub_menu (glFontComboMenu *this,
 static void
 font_history_changed_cb (glFontComboMenu     *this)
 {
-        const GList *list;
+        GList *list;
 
         /*
          * Remove old sub menu
@@ -292,6 +292,8 @@ font_history_changed_cb (glFontComboMenu     *this)
         gtk_menu_item_set_submenu (GTK_MENU_ITEM (this->priv->recent_menu_item),
                                    this->priv->recent_sub_menu);
         gtk_widget_set_sensitive (this->priv->recent_menu_item, list != NULL);
+
+        gl_font_history_model_free_family_list (list);
 }
 
 
diff --git a/src/font-history-model.c b/src/font-history-model.c
index c7390b2..e7037d1 100644
--- a/src/font-history-model.c
+++ b/src/font-history-model.c
@@ -22,10 +22,15 @@
 
 #include "font-history-model.h"
 
+#include <gconf/gconf-client.h>
+
 #include <libglabels/libglabels.h>
 #include "marshal.h"
 
 
+#define BASE_KEY          "/apps/glabels"
+#define RECENT_FONTS_KEY  BASE_KEY "/recent-fonts"
+
 /*========================================================*/
 /* Private types.                                         */
 /*========================================================*/
@@ -33,9 +38,9 @@
 /** GL_FONT_HISTORY_MODEL Private fields */
 struct _glFontHistoryModelPrivate {
 
-        guint       max_n;
+	GConfClient *gconf_client;
 
-        GList      *family_list;
+        guint        max_n;
 };
 
 enum {
@@ -57,6 +62,11 @@ static guint signals[LAST_SIGNAL] = {0};
 
 static void gl_font_history_model_finalize      (GObject             *object);
 
+static void conf_notify_cb                      (GConfClient         *client,
+                                                 guint                cnxn_id,
+                                                 GConfEntry          *entry,
+                                                 glFontHistoryModel  *this);
+
 
 /*****************************************************************************/
 /* Object infrastructure.                                                    */
@@ -95,6 +105,20 @@ static void
 gl_font_history_model_init (glFontHistoryModel *this)
 {
         this->priv = g_new0 (glFontHistoryModelPrivate, 1);
+
+        this->priv->gconf_client = gconf_client_get_default ();
+
+        g_return_if_fail (this->priv->gconf_client != NULL);
+
+        gconf_client_add_dir (this->priv->gconf_client,
+                              BASE_KEY,
+                              GCONF_CLIENT_PRELOAD_ONELEVEL,
+                              NULL);
+
+        gconf_client_notify_add (this->priv->gconf_client,
+                                 RECENT_FONTS_KEY,
+                                 (GConfClientNotifyFunc)conf_notify_cb, this,
+                                 NULL, NULL);
 }
 
 
@@ -105,17 +129,12 @@ static void
 gl_font_history_model_finalize (GObject *object)
 {
         glFontHistoryModel    *this;
-        GList                 *p;
+        GSList                *p;
 
         g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
         this = GL_FONT_HISTORY_MODEL (object);
 
-        for ( p = this->priv->family_list; p; p=p->next )
-        {
-                g_free (p->data);
-        }
-        g_list_free (this->priv->family_list);
-
+        g_object_unref (G_OBJECT(this->priv->gconf_client));
         g_free (this->priv);
 
         G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
@@ -145,51 +164,118 @@ void
 gl_font_history_model_add_family (glFontHistoryModel *this,
                                   const gchar        *family)
 {
-        GList *p;
+        GSList *list = NULL;
+        GList  *old_list;
+        GList  *p;
+        GSList *ps;
 
         /*
-         * If already in list, remove that entry.
+         * Start new list with this family.
          */
-        p = g_list_find_custom (this->priv->family_list,
-                                family,
-                                (GCompareFunc)lgl_str_utf8_casecmp);
-        if (p)
-        {
-                this->priv->family_list =
-                        g_list_remove_link (this->priv->family_list, p);
-                g_free (p->data);
-                g_list_free_1 (p);
-        }
+        list = g_slist_append (list, (gchar *)family);
 
         /*
-         * Now prepend to list.
+         * Transfer old list to new list, ignoring any duplicate of this family
          */
-        this->priv->family_list =
-                g_list_prepend (this->priv->family_list, g_strdup (family));
+        old_list = gl_font_history_model_get_family_list (this);
+        for ( p = old_list; p; p=p->next )
+        {
+                if ( lgl_str_utf8_casecmp (family, p->data) )
+                {
+                        list = g_slist_append (list, p->data);
+                }
+                else
+                {
+                        g_free (p->data);
+                }
+        }
+        g_list_free (old_list);
 
         /*
          * Truncate list to maximum size
          */
-        while (g_list_length (this->priv->family_list) > this->priv->max_n)
+        while (g_slist_length (list) > this->priv->max_n)
         {
-                p = g_list_last (this->priv->family_list);
-                this->priv->family_list =
-                        g_list_remove_link (this->priv->family_list, p);
-                g_free (p->data);
-                g_list_free_1 (p);
+                ps = g_slist_last (list);
+                list = g_slist_remove_link (list, ps);
+                g_slist_free_1 (ps);
         }
 
+        /*
+         * Update conf
+         */
+        gconf_client_set_list (this->priv->gconf_client,
+                               RECENT_FONTS_KEY,
+                               GCONF_VALUE_STRING,
+                               list,
+                               NULL);
+}
+
+
+/*****************************************************************************/
+/* GConf notify callback.                                                    */
+/*****************************************************************************/
+static void
+conf_notify_cb (GConfClient         *client,
+                guint                cnxn_id,
+                GConfEntry          *entry,
+                glFontHistoryModel  *this)
+{
         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
 }
 
 
 /*****************************************************************************/
-/* Get font.                                                                 */
+/* Get list of font families.                                                */
 /*****************************************************************************/
-const GList *
+GList *
 gl_font_history_model_get_family_list (glFontHistoryModel *this)
 {
-        return this->priv->family_list;
+        GList  *list = NULL;
+        GSList *tmp_list;
+        GSList *p;
+
+        /*
+         * Get family list.
+         */
+	tmp_list = gconf_client_get_list (this->priv->gconf_client,
+                                          RECENT_FONTS_KEY,
+                                          GCONF_VALUE_STRING,
+                                          NULL);
+
+        /*
+         * Proof read family list; transfer storage to new list.
+         */
+        for (p=tmp_list; p != NULL; p=p->next)
+        {
+                if ( gl_font_util_is_family_installed (p->data) )
+                {
+                        list = g_list_append (list, p->data);
+                }
+                else
+                {
+                        g_free (p->data);
+                }
+        }
+        g_slist_free (tmp_list);
+
+        return list;
+}
+
+
+/*****************************************************************************/
+/* Free font family list.                                                    */
+/*****************************************************************************/
+void
+gl_font_history_model_free_family_list (GList *list)
+{
+        GList *p;
+
+        for ( p = list; p; p=p->next )
+        {
+                g_free (p->data);
+        }
+        g_list_free (list);
 }
 
 
diff --git a/src/font-history-model.h b/src/font-history-model.h
index 7b2d523..c9b8c48 100644
--- a/src/font-history-model.h
+++ b/src/font-history-model.h
@@ -63,14 +63,15 @@ struct _glFontHistoryModelClass {
 };
 
 
-GType                gl_font_history_model_get_type        (void) G_GNUC_CONST;
+GType                gl_font_history_model_get_type         (void) G_GNUC_CONST;
 
-glFontHistoryModel  *gl_font_history_model_new             (guint               n);
+glFontHistoryModel  *gl_font_history_model_new              (guint               n);
 
-void                 gl_font_history_model_add_family      (glFontHistoryModel *this,
-                                                            const gchar        *family);
+void                 gl_font_history_model_add_family       (glFontHistoryModel *this,
+                                                             const gchar        *family);
 
-const GList         *gl_font_history_model_get_family_list (glFontHistoryModel *this);
+GList               *gl_font_history_model_get_family_list  (glFontHistoryModel *this);
+void                 gl_font_history_model_free_family_list (GList              *list);
 
 
 G_END_DECLS
diff --git a/src/font-util.c b/src/font-util.c
index e4cd2e0..5c2391b 100644
--- a/src/font-util.c
+++ b/src/font-util.c
@@ -185,6 +185,25 @@ gl_font_util_validate_family (const gchar *family)
 }
 
 
+/****************************************************************************/
+/* Test if font is installed.                                               */
+/****************************************************************************/
+gboolean
+gl_font_util_is_family_installed (const gchar *family)
+{
+        const GList *installed_families;
+        GList       *p;
+
+        installed_families = gl_font_util_get_all_families ();
+
+        p = g_list_find_custom ((GList *)installed_families,
+                                family,
+                                (GCompareFunc)g_utf8_collate);
+
+        return (p != NULL);
+}
+
+
 
 /*
  * Local Variables:       -- emacs
diff --git a/src/font-util.h b/src/font-util.h
index 79449ef..39f049b 100644
--- a/src/font-util.h
+++ b/src/font-util.h
@@ -30,6 +30,7 @@ const GList *gl_font_util_get_proportional_families (void);
 const GList *gl_font_util_get_fixed_width_families  (void);
 
 gchar       *gl_font_util_validate_family           (const gchar *family);
+gboolean     gl_font_util_is_family_installed       (const gchar *family);
 
 G_END_DECLS
 
diff --git a/src/label-text.c b/src/label-text.c
index ca3e08e..5b9fa0f 100644
--- a/src/label-text.c
+++ b/src/label-text.c
@@ -218,8 +218,6 @@ gl_label_text_init (glLabelText *ltext)
 
         ltext->priv->size_changed      = TRUE;
 
-        gl_font_history_model_add_family (gl_font_history, DEFAULT_FONT_FAMILY);
-
 	g_signal_connect (G_OBJECT(ltext->priv->buffer), "changed",
 			  G_CALLBACK(buffer_changed_cb), ltext);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]