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"
27 #include <libglabels.h>
31 /*========================================================*/
33 /*========================================================*/
35 struct _glTemplateHistoryModelPrivate {
48 /*========================================================*/
49 /* Private globals. */
50 /*========================================================*/
52 static guint signals[LAST_SIGNAL] = {0};
55 /*========================================================*/
56 /* Private function prototypes. */
57 /*========================================================*/
59 static void gl_template_history_model_finalize (GObject *object);
61 static void history_changed_cb (glTemplateHistoryModel *this);
64 /*****************************************************************************/
65 /* Object infrastructure. */
66 /*****************************************************************************/
67 G_DEFINE_TYPE (glTemplateHistoryModel, gl_template_history_model, G_TYPE_OBJECT)
70 /*****************************************************************************/
71 /* Class Init Function. */
72 /*****************************************************************************/
74 gl_template_history_model_class_init (glTemplateHistoryModelClass *class)
76 GObjectClass *gobject_class = (GObjectClass *) class;
78 gl_template_history_model_parent_class = g_type_class_peek_parent (class);
80 gobject_class->finalize = gl_template_history_model_finalize;
83 g_signal_new ("changed",
84 G_OBJECT_CLASS_TYPE (gobject_class),
86 G_STRUCT_OFFSET (glTemplateHistoryModelClass, changed),
88 gl_marshal_VOID__VOID,
94 /*****************************************************************************/
95 /* Object Instance Init Function. */
96 /*****************************************************************************/
98 gl_template_history_model_init (glTemplateHistoryModel *this)
100 this->priv = g_new0 (glTemplateHistoryModelPrivate, 1);
102 this->priv->history = g_settings_new ("org.gnome.glabels-3.history");
104 g_return_if_fail (this->priv->history != NULL);
106 g_signal_connect_swapped (G_OBJECT (this->priv->history),
107 "changed::recent-templates",
108 G_CALLBACK (history_changed_cb), this);
112 /*****************************************************************************/
113 /* Finalize Method. */
114 /*****************************************************************************/
116 gl_template_history_model_finalize (GObject *object)
118 glTemplateHistoryModel *this;
120 g_return_if_fail (object && IS_GL_TEMPLATE_HISTORY_MODEL (object));
121 this = GL_TEMPLATE_HISTORY_MODEL (object);
123 g_object_unref (G_OBJECT(this->priv->history));
126 G_OBJECT_CLASS (gl_template_history_model_parent_class)->finalize (object);
130 /*****************************************************************************/
131 /** New Object Generator. */
132 /*****************************************************************************/
133 glTemplateHistoryModel *
134 gl_template_history_model_new (guint n)
136 glTemplateHistoryModel *this;
138 this = g_object_new (TYPE_GL_TEMPLATE_HISTORY_MODEL, NULL);
140 this->priv->max_n = n;
146 /*****************************************************************************/
147 /* Add template to history. */
148 /*****************************************************************************/
150 gl_template_history_model_add_name (glTemplateHistoryModel *this,
157 old = g_settings_get_strv (this->priv->history, "recent-templates");
159 new = g_new0 (gchar *, this->priv->max_n+1);
161 /* Put in first slot. */
162 new[0] = g_strdup (name);
164 /* Push everthing else down, pruning any duplicate found. */
165 for ( i = 0, j = 1; (j < (this->priv->max_n-1)) && old[i]; i++ )
167 if ( lgl_str_utf8_casecmp (name, old[i]) != 0 )
169 new[j++] = g_strdup (old[i]);
173 g_settings_set_strv (this->priv->history, "recent-templates",
174 (const gchar * const *)new);
181 /*****************************************************************************/
182 /* History changed callback. */
183 /*****************************************************************************/
185 history_changed_cb (glTemplateHistoryModel *this)
187 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
191 /*****************************************************************************/
192 /* Get list of template families. */
193 /*****************************************************************************/
195 gl_template_history_model_get_name_list (glTemplateHistoryModel *this)
201 strv = g_settings_get_strv (this->priv->history, "recent-templates");
204 * Proof read name list; transfer storage to new list.
206 for ( i = 0; strv[i]; i++ )
208 if ( lgl_db_does_template_name_exist (strv[i]) )
210 list = g_list_append (list, g_strdup (strv[i]));
219 /*****************************************************************************/
220 /* Free template name list. */
221 /*****************************************************************************/
223 gl_template_history_model_free_name_list (GList *list)
227 for ( p = list; p; p=p->next )
237 * Local Variables: -- emacs
239 * c-basic-offset: 8 -- emacs
240 * tab-width: 8 -- emacs
241 * indent-tabs-mode: nil -- emacs