2 * template-history-model.c
3 * Copyright (C) 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 "template-history-model.h"
25 #include <gconf/gconf-client.h>
27 #include <libglabels.h>
31 #define BASE_KEY "/apps/glabels"
32 #define RECENT_TEMPLATES_KEY BASE_KEY "/recent-templates"
35 /*========================================================*/
37 /*========================================================*/
39 /** GL_TEMPLATE_HISTORY_MODEL Private fields */
40 struct _glTemplateHistoryModelPrivate {
42 GConfClient *gconf_client;
53 /*========================================================*/
54 /* Private globals. */
55 /*========================================================*/
57 static guint signals[LAST_SIGNAL] = {0};
60 /*========================================================*/
61 /* Private function prototypes. */
62 /*========================================================*/
64 static void gl_template_history_model_finalize (GObject *object);
66 static void conf_notify_cb (GConfClient *client,
69 glTemplateHistoryModel *this);
72 /*****************************************************************************/
73 /* Object infrastructure. */
74 /*****************************************************************************/
75 G_DEFINE_TYPE (glTemplateHistoryModel, gl_template_history_model, G_TYPE_OBJECT);
78 /*****************************************************************************/
79 /* Class Init Function. */
80 /*****************************************************************************/
82 gl_template_history_model_class_init (glTemplateHistoryModelClass *class)
84 GObjectClass *gobject_class = (GObjectClass *) class;
86 gl_template_history_model_parent_class = g_type_class_peek_parent (class);
88 gobject_class->finalize = gl_template_history_model_finalize;
91 g_signal_new ("changed",
92 G_OBJECT_CLASS_TYPE (gobject_class),
94 G_STRUCT_OFFSET (glTemplateHistoryModelClass, changed),
96 gl_marshal_VOID__VOID,
102 /*****************************************************************************/
103 /* Object Instance Init Function. */
104 /*****************************************************************************/
106 gl_template_history_model_init (glTemplateHistoryModel *this)
108 this->priv = g_new0 (glTemplateHistoryModelPrivate, 1);
110 this->priv->gconf_client = gconf_client_get_default ();
112 g_return_if_fail (this->priv->gconf_client != NULL);
114 gconf_client_add_dir (this->priv->gconf_client,
116 GCONF_CLIENT_PRELOAD_ONELEVEL,
119 gconf_client_notify_add (this->priv->gconf_client,
120 RECENT_TEMPLATES_KEY,
121 (GConfClientNotifyFunc)conf_notify_cb, this,
126 /*****************************************************************************/
127 /* Finalize Method. */
128 /*****************************************************************************/
130 gl_template_history_model_finalize (GObject *object)
132 glTemplateHistoryModel *this;
134 g_return_if_fail (object && IS_GL_TEMPLATE_HISTORY_MODEL (object));
135 this = GL_TEMPLATE_HISTORY_MODEL (object);
137 g_object_unref (G_OBJECT(this->priv->gconf_client));
140 G_OBJECT_CLASS (gl_template_history_model_parent_class)->finalize (object);
144 /*****************************************************************************/
145 /** New Object Generator. */
146 /*****************************************************************************/
147 glTemplateHistoryModel *
148 gl_template_history_model_new (guint n)
150 glTemplateHistoryModel *this;
152 this = g_object_new (TYPE_GL_TEMPLATE_HISTORY_MODEL, NULL);
154 this->priv->max_n = n;
160 /*****************************************************************************/
161 /* Add template to history. */
162 /*****************************************************************************/
164 gl_template_history_model_add_name (glTemplateHistoryModel *this,
173 * Start new list with this name.
175 list = g_slist_append (list, (gchar *)name);
178 * Transfer old list to new list, ignoring any duplicate of this name
180 old_list = gl_template_history_model_get_name_list (this);
181 for ( p = old_list; p; p=p->next )
183 if ( lgl_str_utf8_casecmp (name, p->data) )
185 list = g_slist_append (list, p->data);
192 g_list_free (old_list);
195 * Truncate list to maximum size
197 while (g_slist_length (list) > this->priv->max_n)
199 ps = g_slist_last (list);
200 list = g_slist_remove_link (list, ps);
207 gconf_client_set_list (this->priv->gconf_client,
208 RECENT_TEMPLATES_KEY,
215 /*****************************************************************************/
216 /* GConf notify callback. */
217 /*****************************************************************************/
219 conf_notify_cb (GConfClient *client,
222 glTemplateHistoryModel *this)
224 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
228 /*****************************************************************************/
229 /* Get list of template families. */
230 /*****************************************************************************/
232 gl_template_history_model_get_name_list (glTemplateHistoryModel *this)
241 tmp_list = gconf_client_get_list (this->priv->gconf_client,
242 RECENT_TEMPLATES_KEY,
247 * Proof read name list; transfer storage to new list.
249 for (p=tmp_list; p != NULL; p=p->next)
251 if ( lgl_db_does_template_name_exist (p->data) )
253 list = g_list_append (list, p->data);
260 g_slist_free (tmp_list);
266 /*****************************************************************************/
267 /* Free template name list. */
268 /*****************************************************************************/
270 gl_template_history_model_free_name_list (GList *list)
274 for ( p = list; p; p=p->next )
284 * Local Variables: -- emacs
286 * c-basic-offset: 8 -- emacs
287 * tab-width: 8 -- emacs
288 * indent-tabs-mode: nil -- emacs