2 * color-history-model.c
3 * Copyright (C) 2008-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "color-history-model.h"
25 #include <gconf/gconf-client.h>
30 #define BASE_KEY "/apps/glabels"
31 #define RECENT_COLORS_KEY BASE_KEY "/recent-colors"
34 /*========================================================*/
36 /*========================================================*/
38 /** GL_COLOR_HISTORY_MODEL Private fields */
39 struct _glColorHistoryModelPrivate {
41 GConfClient *gconf_client;
52 /*========================================================*/
53 /* Private globals. */
54 /*========================================================*/
56 static guint signals[LAST_SIGNAL] = {0};
59 /*========================================================*/
60 /* Private function prototypes. */
61 /*========================================================*/
63 static void gl_color_history_model_finalize (GObject *object);
65 static void conf_notify_cb (GConfClient *client,
68 glColorHistoryModel *this);
70 static GSList *get_color_list (glColorHistoryModel *this);
73 /*****************************************************************************/
74 /* Object infrastructure. */
75 /*****************************************************************************/
76 G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT);
79 /*****************************************************************************/
80 /* Class Init Function. */
81 /*****************************************************************************/
83 gl_color_history_model_class_init (glColorHistoryModelClass *class)
85 GObjectClass *gobject_class = (GObjectClass *) class;
87 gl_color_history_model_parent_class = g_type_class_peek_parent (class);
89 gobject_class->finalize = gl_color_history_model_finalize;
92 g_signal_new ("changed",
93 G_OBJECT_CLASS_TYPE (gobject_class),
95 G_STRUCT_OFFSET (glColorHistoryModelClass, changed),
97 gl_marshal_VOID__VOID,
103 /*****************************************************************************/
104 /* Object Instance Init Function. */
105 /*****************************************************************************/
107 gl_color_history_model_init (glColorHistoryModel *this)
109 this->priv = g_new0 (glColorHistoryModelPrivate, 1);
111 this->priv->gconf_client = gconf_client_get_default ();
113 g_return_if_fail (this->priv->gconf_client != NULL);
115 gconf_client_add_dir (this->priv->gconf_client,
117 GCONF_CLIENT_PRELOAD_ONELEVEL,
120 gconf_client_notify_add (this->priv->gconf_client,
122 (GConfClientNotifyFunc)conf_notify_cb, this,
127 /*****************************************************************************/
128 /* Finalize Method. */
129 /*****************************************************************************/
131 gl_color_history_model_finalize (GObject *object)
133 glColorHistoryModel *this;
135 g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
136 this = GL_COLOR_HISTORY_MODEL (object);
138 g_object_unref (G_OBJECT(this->priv->gconf_client));
141 G_OBJECT_CLASS (gl_color_history_model_parent_class)->finalize (object);
145 /*****************************************************************************/
146 /** New Object Generator. */
147 /*****************************************************************************/
148 glColorHistoryModel *
149 gl_color_history_model_new (guint n)
151 glColorHistoryModel *this;
153 this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);
155 this->priv->max_n = n;
161 /*****************************************************************************/
162 /* Add color to history. */
163 /*****************************************************************************/
165 gl_color_history_model_add_color (glColorHistoryModel *this,
173 * Start new list with this color.
175 list = g_slist_append (list, GINT_TO_POINTER (color));
178 * Transfer old list to new list, ignoring any duplicate of this color
180 old_list = get_color_list (this);
181 for ( p = old_list; p; p=p->next )
183 if ( color != (guint)GPOINTER_TO_INT (p->data) )
185 list = g_slist_append (list, p->data);
188 g_slist_free (old_list);
191 * Truncate list to maximum size
193 while (g_slist_length (list) > this->priv->max_n)
195 p = g_slist_last (list);
196 list = g_slist_remove_link (list, p);
203 gconf_client_set_list (this->priv->gconf_client,
211 /*****************************************************************************/
212 /* GConf notify callback. */
213 /*****************************************************************************/
215 conf_notify_cb (GConfClient *client,
218 glColorHistoryModel *this)
220 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
224 /*****************************************************************************/
225 /* Get list of colors. */
226 /*****************************************************************************/
228 get_color_list (glColorHistoryModel *this)
235 list = gconf_client_get_list (this->priv->gconf_client,
243 /*****************************************************************************/
245 /*****************************************************************************/
247 gl_color_history_model_get_color (glColorHistoryModel *this,
254 list = get_color_list (this);
255 p = g_slist_nth (list, i);
258 color = GPOINTER_TO_INT (p->data);
269 * Local Variables: -- emacs
271 * c-basic-offset: 8 -- emacs
272 * tab-width: 8 -- emacs
273 * indent-tabs-mode: nil -- emacs