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 "font-history-model.h"
25 #include <gconf/gconf-client.h>
27 #include <libglabels/libglabels.h>
31 #define BASE_KEY "/apps/glabels"
32 #define RECENT_FONTS_KEY BASE_KEY "/recent-fonts"
34 /*========================================================*/
36 /*========================================================*/
38 /** GL_FONT_HISTORY_MODEL Private fields */
39 struct _glFontHistoryModelPrivate {
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_font_history_model_finalize (GObject *object);
65 static void conf_notify_cb (GConfClient *client,
68 glFontHistoryModel *this);
71 /*****************************************************************************/
72 /* Object infrastructure. */
73 /*****************************************************************************/
74 G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT);
77 /*****************************************************************************/
78 /* Class Init Function. */
79 /*****************************************************************************/
81 gl_font_history_model_class_init (glFontHistoryModelClass *class)
83 GObjectClass *gobject_class = (GObjectClass *) class;
85 gl_font_history_model_parent_class = g_type_class_peek_parent (class);
87 gobject_class->finalize = gl_font_history_model_finalize;
90 g_signal_new ("changed",
91 G_OBJECT_CLASS_TYPE (gobject_class),
93 G_STRUCT_OFFSET (glFontHistoryModelClass, changed),
95 gl_marshal_VOID__VOID,
101 /*****************************************************************************/
102 /* Object Instance Init Function. */
103 /*****************************************************************************/
105 gl_font_history_model_init (glFontHistoryModel *this)
107 this->priv = g_new0 (glFontHistoryModelPrivate, 1);
109 this->priv->gconf_client = gconf_client_get_default ();
111 g_return_if_fail (this->priv->gconf_client != NULL);
113 gconf_client_add_dir (this->priv->gconf_client,
115 GCONF_CLIENT_PRELOAD_ONELEVEL,
118 gconf_client_notify_add (this->priv->gconf_client,
120 (GConfClientNotifyFunc)conf_notify_cb, this,
125 /*****************************************************************************/
126 /* Finalize Method. */
127 /*****************************************************************************/
129 gl_font_history_model_finalize (GObject *object)
131 glFontHistoryModel *this;
134 g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
135 this = GL_FONT_HISTORY_MODEL (object);
137 g_object_unref (G_OBJECT(this->priv->gconf_client));
140 G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
144 /*****************************************************************************/
145 /** New Object Generator. */
146 /*****************************************************************************/
148 gl_font_history_model_new (guint n)
150 glFontHistoryModel *this;
152 this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
154 this->priv->max_n = n;
160 /*****************************************************************************/
161 /* Add font to history. */
162 /*****************************************************************************/
164 gl_font_history_model_add_family (glFontHistoryModel *this,
173 * Start new list with this family.
175 list = g_slist_append (list, (gchar *)family);
178 * Transfer old list to new list, ignoring any duplicate of this family
180 old_list = gl_font_history_model_get_family_list (this);
181 for ( p = old_list; p; p=p->next )
183 if ( lgl_str_utf8_casecmp (family, 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,
215 /*****************************************************************************/
216 /* GConf notify callback. */
217 /*****************************************************************************/
219 conf_notify_cb (GConfClient *client,
222 glFontHistoryModel *this)
224 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
228 /*****************************************************************************/
229 /* Get list of font families. */
230 /*****************************************************************************/
232 gl_font_history_model_get_family_list (glFontHistoryModel *this)
241 tmp_list = gconf_client_get_list (this->priv->gconf_client,
247 * Proof read family list; transfer storage to new list.
249 for (p=tmp_list; p != NULL; p=p->next)
251 if ( gl_font_util_is_family_installed (p->data) )
253 list = g_list_append (list, p->data);
260 g_slist_free (tmp_list);
266 /*****************************************************************************/
267 /* Free font family list. */
268 /*****************************************************************************/
270 gl_font_history_model_free_family_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