[glabels] Use GConf to store custom color history



commit 87e9f1324b388483e0078ebcccda4ffa30687bd2
Author: Jim Evins <evins snaught com>
Date:   Sat Oct 24 08:53:10 2009 -0400

    Use GConf to store custom color history
    
    Renamed glColorComboHistory to glColorHistoryModel and reconciled style
    with glFontHistoryModel.  glColorHistoryModel now front-ends GConf to store
    custom color history between sessions.

 po/POTFILES.in            |   18 ++--
 src/Makefile.am           |    4 +-
 src/color-combo-history.c |  181 -----------------------------
 src/color-combo-history.h |   82 --------------
 src/color-combo-menu.c    |   13 +-
 src/color-history-model.c |  275 +++++++++++++++++++++++++++++++++++++++++++++
 src/color-history-model.h |   89 +++++++++++++++
 src/font-history-model.c  |    2 +-
 src/font-history-model.h  |    3 +-
 9 files changed, 383 insertions(+), 284 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a134b2a..f83e8ca 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -1,9 +1,9 @@
 # List of source files containing translatable strings.
 
 src/bc.c
+src/bc.h
 src/bc-gnubarcode.c
 src/bc-gnubarcode.h
-src/bc.h
 src/bc-iec16022.c
 src/bc-iec16022.h
 src/bc-iec18004.c
@@ -19,17 +19,17 @@ src/cairo-label-path.h
 src/cairo-markup-path.c
 src/cairo-markup-path.h
 src/color.c
+src/color.h
 src/color-combo-button.c
 src/color-combo-button.h
 src/color-combo.c
 src/color-combo-color-menu-item.c
 src/color-combo-color-menu-item.h
 src/color-combo.h
-src/color-combo-history.c
-src/color-combo-history.h
 src/color-combo-menu.c
 src/color-combo-menu.h
-src/color.h
+src/color-history-model.c
+src/color-history-model.h
 src/color-swatch.c
 src/color-swatch.h
 src/combo-util.c
@@ -82,9 +82,9 @@ src/label-text.h
 #src/marshal.c
 #src/marshal.h
 src/merge.c
+src/merge.h
 src/merge-evolution.c
 src/merge-evolution.h
-src/merge.h
 src/merge-init.c
 src/merge-init.h
 src/merge-properties-dialog.c
@@ -120,9 +120,9 @@ src/object-editor-text-page.c
 src/pixbuf-cache.c
 src/pixbuf-cache.h
 src/prefs.c
+src/prefs.h
 src/prefs-dialog.c
 src/prefs-dialog.h
-src/prefs.h
 src/prefs-model.c
 src/prefs-model.h
 src/print.c
@@ -144,23 +144,23 @@ src/template-designer.h
 src/text-node.c
 src/text-node.h
 src/ui.c
+src/ui.h
 src/ui-commands.c
 src/ui-commands.h
-src/ui.h
 src/ui-property-bar.c
 src/ui-property-bar.h
 src/ui-sidebar.c
 src/ui-sidebar.h
 src/ui-util.c
 src/ui-util.h
+src/view.c
+src/view.h
 src/view-barcode.c
 src/view-barcode.h
 src/view-box.c
 src/view-box.h
-src/view.c
 src/view-ellipse.c
 src/view-ellipse.h
-src/view.h
 src/view-image.c
 src/view-image.h
 src/view-line.c
diff --git a/src/Makefile.am b/src/Makefile.am
index b851e3d..36de5af 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -191,8 +191,8 @@ glabels_SOURCES = 			\
 	color-combo-menu.h		\
 	color-combo-color-menu-item.c	\
 	color-combo-color-menu-item.h	\
-	color-combo-history.c		\
-	color-combo-history.h		\
+	color-history-model.c		\
+	color-history-model.h		\
 	color-swatch.c			\
 	color-swatch.h			\
 	font-combo.c			\
diff --git a/src/color-combo-menu.c b/src/color-combo-menu.c
index 01e3b77..7e2bf15 100644
--- a/src/color-combo-menu.c
+++ b/src/color-combo-menu.c
@@ -26,7 +26,7 @@
 #include <gtk/gtk.h>
 
 #include "color-combo-color-menu-item.h"
-#include "color-combo-history.h"
+#include "color-history-model.h"
 #include "color.h"
 #include "marshal.h"
 
@@ -122,7 +122,7 @@ static ColorTableEntry color_table[] =
 
 };
 
-static glColorComboHistory *custom_color_history = NULL;
+static glColorHistoryModel *custom_color_history = NULL;
 
 
 /*===========================================*/
@@ -195,7 +195,7 @@ gl_color_combo_menu_init (glColorComboMenu *this)
          */
         if ( !custom_color_history )
         {
-                custom_color_history = gl_color_combo_history_new (PALETTE_COLS);
+                custom_color_history = gl_color_history_model_new (PALETTE_COLS);
         }
 
 
@@ -307,7 +307,7 @@ load_custom_color_history (glColorComboMenu *this)
 
         for ( i=0; i < PALETTE_COLS; i++ )
         {
-                color = gl_color_combo_history_get_color (custom_color_history, i);
+                color = gl_color_history_model_get_color (custom_color_history, i);
 
                 if (color)
                 {
@@ -386,7 +386,7 @@ custom_menu_item_activate_cb (glColorComboMenu *this)
                                               (color.green >>8),
                                               (color.blue  >>8));
 
-                gl_color_combo_history_add_color (custom_color_history,
+                gl_color_history_model_add_color (custom_color_history,
                                                   this->priv->color);
 
                 g_signal_emit (this, signals[COLOR_CHANGED], 0,
@@ -431,8 +431,7 @@ history_menu_item_activate_cb (GtkMenuItem      *item,
 
         i = gl_color_combo_color_menu_item_get_id (GL_COLOR_COMBO_COLOR_MENU_ITEM (item));
 
-        this->priv->color = gl_color_combo_history_get_color (custom_color_history,
-                                                              i);
+        this->priv->color = gl_color_history_model_get_color (custom_color_history, i);
 
         g_signal_emit (this, signals[COLOR_CHANGED], 0,
                        this->priv->color, FALSE);
diff --git a/src/color-history-model.c b/src/color-history-model.c
new file mode 100644
index 0000000..cf11a80
--- /dev/null
+++ b/src/color-history-model.c
@@ -0,0 +1,275 @@
+/*
+ *  color-history-model.c
+ *  Copyright (C) 2008-2009  Jim Evins <evins snaught com>.
+ *
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  gLabels 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "color-history-model.h"
+
+#include <gconf/gconf-client.h>
+
+#include "marshal.h"
+
+
+#define BASE_KEY          "/apps/glabels"
+#define RECENT_COLORS_KEY  BASE_KEY "/recent-colors"
+
+
+/*========================================================*/
+/* Private types.                                         */
+/*========================================================*/
+
+/** GL_COLOR_HISTORY_MODEL Private fields */
+struct _glColorHistoryModelPrivate {
+
+	GConfClient *gconf_client;
+
+        guint        max_n;
+};
+
+enum {
+	CHANGED,
+	LAST_SIGNAL
+};
+
+
+/*========================================================*/
+/* Private globals.                                       */
+/*========================================================*/
+
+static guint signals[LAST_SIGNAL] = {0};
+
+
+/*========================================================*/
+/* Private function prototypes.                           */
+/*========================================================*/
+
+static void gl_color_history_model_finalize     (GObject             *object);
+
+static void conf_notify_cb                      (GConfClient         *client,
+                                                 guint                cnxn_id,
+                                                 GConfEntry          *entry,
+                                                 glColorHistoryModel *this);
+
+static GSList *get_color_list                   (glColorHistoryModel *this);
+
+
+/*****************************************************************************/
+/* Object infrastructure.                                                    */
+/*****************************************************************************/
+G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT);
+
+
+/*****************************************************************************/
+/* Class Init Function.                                                      */
+/*****************************************************************************/
+static void
+gl_color_history_model_class_init (glColorHistoryModelClass *class)
+{
+        GObjectClass  *gobject_class = (GObjectClass *) class;
+
+        gl_color_history_model_parent_class = g_type_class_peek_parent (class);
+
+        gobject_class->finalize = gl_color_history_model_finalize;
+
+	signals[CHANGED] =
+		g_signal_new ("changed",
+			      G_OBJECT_CLASS_TYPE (gobject_class),
+			      G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (glColorHistoryModelClass, changed),
+			      NULL, NULL,
+			      gl_marshal_VOID__VOID,
+			      G_TYPE_NONE,
+			      0);
+}
+
+
+/*****************************************************************************/
+/* Object Instance Init Function.                                            */
+/*****************************************************************************/
+static void
+gl_color_history_model_init (glColorHistoryModel *this)
+{
+        this->priv = g_new0 (glColorHistoryModelPrivate, 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_COLORS_KEY,
+                                 (GConfClientNotifyFunc)conf_notify_cb, this,
+                                 NULL, NULL);
+}
+
+
+/*****************************************************************************/
+/* Finalize Method.                                                          */
+/*****************************************************************************/
+static void
+gl_color_history_model_finalize (GObject *object)
+{
+        glColorHistoryModel   *this;
+
+        g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
+        this = GL_COLOR_HISTORY_MODEL (object);
+
+        g_object_unref (G_OBJECT(this->priv->gconf_client));
+        g_free (this->priv);
+
+        G_OBJECT_CLASS (gl_color_history_model_parent_class)->finalize (object);
+}
+
+
+/*****************************************************************************/
+/** New Object Generator.                                                    */
+/*****************************************************************************/
+glColorHistoryModel *
+gl_color_history_model_new (guint n)
+{
+        glColorHistoryModel *this;
+
+        this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);
+
+        this->priv->max_n = n;
+
+        return this;
+}
+
+
+/*****************************************************************************/
+/* Add color to history.                                                      */
+/*****************************************************************************/
+void
+gl_color_history_model_add_color (glColorHistoryModel *this,
+                                  guint                color)
+{
+        GSList  *list = NULL;
+        GSList  *old_list;
+        GSList  *p;
+
+        /*
+         * Start new list with this color.
+         */
+        list = g_slist_append (list, GINT_TO_POINTER (color));
+
+        /*
+         * Transfer old list to new list, ignoring any duplicate of this color
+         */
+        old_list = get_color_list (this);
+        for ( p = old_list; p; p=p->next )
+        {
+                if ( color != (guint)GPOINTER_TO_INT (p->data) )
+                {
+                        list = g_slist_append (list, p->data);
+                }
+        }
+        g_slist_free (old_list);
+
+        /*
+         * Truncate list to maximum size
+         */
+        while (g_slist_length (list) > this->priv->max_n)
+        {
+                p = g_slist_last (list);
+                list = g_slist_remove_link (list, p);
+                g_slist_free_1 (p);
+        }
+
+        /*
+         * Update conf
+         */
+        gconf_client_set_list (this->priv->gconf_client,
+                               RECENT_COLORS_KEY,
+                               GCONF_VALUE_INT,
+                               list,
+                               NULL);
+}
+
+
+/*****************************************************************************/
+/* GConf notify callback.                                                    */
+/*****************************************************************************/
+static void
+conf_notify_cb (GConfClient         *client,
+                guint                cnxn_id,
+                GConfEntry          *entry,
+                glColorHistoryModel  *this)
+{
+        g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
+}
+
+
+/*****************************************************************************/
+/* Get list of colors.                                                       */
+/*****************************************************************************/
+static GSList *
+get_color_list (glColorHistoryModel *this)
+{
+        GSList *list;
+
+        /*
+         * Get color list.
+         */
+	list = gconf_client_get_list (this->priv->gconf_client,
+                                      RECENT_COLORS_KEY,
+                                      GCONF_VALUE_INT,
+                                      NULL);
+        return list;
+}
+
+
+/*****************************************************************************/
+/* Get color.                                                                */
+/*****************************************************************************/
+guint
+gl_color_history_model_get_color (glColorHistoryModel *this,
+                                  guint                i)
+{
+        guint   color = 0;
+        GSList *list;
+        GSList *p;
+
+        list = get_color_list (this);
+        p = g_slist_nth (list, i);
+        if (p)
+        {
+                color = GPOINTER_TO_INT (p->data);
+        }
+        g_slist_free (list);
+
+        return color;
+}
+
+
+
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */
diff --git a/src/color-history-model.h b/src/color-history-model.h
new file mode 100644
index 0000000..765ba99
--- /dev/null
+++ b/src/color-history-model.h
@@ -0,0 +1,89 @@
+/*
+ *  color-history-model.h
+ *  Copyright (C) 2008-2009  Jim Evins <evins snaught com>.
+ *
+ *  This file is part of gLabels.
+ *
+ *  gLabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  gLabels 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GL_COLOR_HISTORY_MODEL_H__
+#define __GL_COLOR_HISTORY_MODEL_H__
+
+
+#include <glib-object.h>
+
+
+G_BEGIN_DECLS
+
+/* Utility Macros */
+#define TYPE_GL_COLOR_HISTORY_MODEL              (gl_color_history_model_get_type ())
+#define GL_COLOR_HISTORY_MODEL(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GL_COLOR_HISTORY_MODEL, glColorHistoryModel))
+#define GL_COLOR_HISTORY_MODEL_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GL_COLOR_HISTORY_MODEL, glColorHistoryModelClass))
+#define IS_GL_COLOR_HISTORY_MODEL(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GL_COLOR_HISTORY_MODEL))
+#define IS_GL_COLOR_HISTORY_MODEL_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GL_COLOR_HISTORY_MODEL))
+#define GL_COLOR_HISTORY_MODEL_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), TYPE_GL_COLOR_HISTORY_MODEL, glColorHistoryModelClass))
+
+
+/* Type definitions */
+typedef struct _glColorHistoryModel          glColorHistoryModel;
+typedef struct _glColorHistoryModelPrivate   glColorHistoryModelPrivate;
+typedef struct _glColorHistoryModelClass     glColorHistoryModelClass;
+
+
+/** GL_COLOR_HISTORY_MODEL Object */
+struct _glColorHistoryModel {
+        GObject                        parent;
+
+        glColorHistoryModelPrivate    *priv;
+};
+
+/** GL_COLOR_HISTORY_MODEL Class */
+struct _glColorHistoryModelClass {
+        GObjectClass                   parent_class;
+
+        /*
+         * Signals
+         */
+        void (*changed)     (glColorHistoryModel *this,
+                             gpointer             user_data);
+
+};
+
+
+GType                gl_color_history_model_get_type  (void) G_GNUC_CONST;
+
+glColorHistoryModel *gl_color_history_model_new       (guint                n);
+
+void                 gl_color_history_model_add_color (glColorHistoryModel *this,
+                                                       guint                color);
+
+guint                gl_color_history_model_get_color (glColorHistoryModel *this,
+                                                       guint                i);
+
+
+G_END_DECLS
+
+#endif /* __GL_COLOR_HISTORY_MODEL_H__ */
+
+
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */
diff --git a/src/font-history-model.c b/src/font-history-model.c
index e7037d1..29471d7 100644
--- a/src/font-history-model.c
+++ b/src/font-history-model.c
@@ -31,6 +31,7 @@
 #define BASE_KEY          "/apps/glabels"
 #define RECENT_FONTS_KEY  BASE_KEY "/recent-fonts"
 
+
 /*========================================================*/
 /* Private types.                                         */
 /*========================================================*/
@@ -129,7 +130,6 @@ static void
 gl_font_history_model_finalize (GObject *object)
 {
         glFontHistoryModel    *this;
-        GSList                *p;
 
         g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
         this = GL_FONT_HISTORY_MODEL (object);
diff --git a/src/font-history-model.h b/src/font-history-model.h
index c9b8c48..690c3c7 100644
--- a/src/font-history-model.h
+++ b/src/font-history-model.h
@@ -57,8 +57,7 @@ struct _glFontHistoryModelClass {
          * Signals
          */
         void (*changed)     (glFontHistoryModel     *this,
-                             gpointer           user_data);
-
+                             gpointer                user_data);
 
 };
 



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