]> git.sur5r.net Git - glabels/commitdiff
Use GConf to store font history
authorJim Evins <evins@snaught.com>
Sat, 24 Oct 2009 11:59:11 +0000 (07:59 -0400)
committerJim Evins <evins@snaught.com>
Sat, 24 Oct 2009 11:59:11 +0000 (07:59 -0400)
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
src/font-history-model.c
src/font-history-model.h
src/font-util.c
src/font-util.h
src/label-text.c

index 4319003291eba06e8ca3f31eaa6b4f4ab817b532..2a0a7c59fa80b94ee851b86969d720debf25a77b 100644 (file)
@@ -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);
 }
 
 
index c7390b2f0738d4eec8e8047d74a2c252a13032da..e7037d150e61b8035e3039a1c157316833de6890 100644 (file)
 
 #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);
 }
 
 
index 7b2d52373085a50cf233ec3f9a42bde427f22aef..c9b8c48ef59d0b9d8774f376cdad1110962d8de4 100644 (file)
@@ -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
index e4cd2e08c5be39d6f5c82a89113c0f34a0e2bd7e..5c2391baa6d5897595c701aaccc8bf1a00923cf3 100644 (file)
@@ -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
index 79449efaaf84a2a01651f853b23a5f9d21af95b4..39f049b51a761afd4e0ba3714fedd0b94e6866a4 100644 (file)
@@ -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
 
index ca3e08e9da07fcb3c14ae9ffb807dcc5836d4c3e..5b9fa0f73668d3ebe69008528247120e1c026f11 100644 (file)
@@ -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);
 }