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>
28 #include "font-util.h"
32 #define BASE_KEY "/apps/glabels"
33 #define RECENT_FONTS_KEY BASE_KEY "/recent-fonts"
36 /*========================================================*/
38 /*========================================================*/
40 /** GL_FONT_HISTORY_MODEL Private fields */
41 struct _glFontHistoryModelPrivate {
43 GConfClient *gconf_client;
54 /*========================================================*/
55 /* Private globals. */
56 /*========================================================*/
58 static guint signals[LAST_SIGNAL] = {0};
61 /*========================================================*/
62 /* Private function prototypes. */
63 /*========================================================*/
65 static void gl_font_history_model_finalize (GObject *object);
67 static void conf_notify_cb (GConfClient *client,
70 glFontHistoryModel *this);
73 /*****************************************************************************/
74 /* Object infrastructure. */
75 /*****************************************************************************/
76 G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT);
79 /*****************************************************************************/
80 /* Class Init Function. */
81 /*****************************************************************************/
83 gl_font_history_model_class_init (glFontHistoryModelClass *class)
85 GObjectClass *gobject_class = (GObjectClass *) class;
87 gl_font_history_model_parent_class = g_type_class_peek_parent (class);
89 gobject_class->finalize = gl_font_history_model_finalize;
92 g_signal_new ("changed",
93 G_OBJECT_CLASS_TYPE (gobject_class),
95 G_STRUCT_OFFSET (glFontHistoryModelClass, changed),
97 gl_marshal_VOID__VOID,
103 /*****************************************************************************/
104 /* Object Instance Init Function. */
105 /*****************************************************************************/
107 gl_font_history_model_init (glFontHistoryModel *this)
109 this->priv = g_new0 (glFontHistoryModelPrivate, 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_font_history_model_finalize (GObject *object)
133 glFontHistoryModel *this;
135 g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
136 this = GL_FONT_HISTORY_MODEL (object);
138 g_object_unref (G_OBJECT(this->priv->gconf_client));
141 G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
145 /*****************************************************************************/
146 /** New Object Generator. */
147 /*****************************************************************************/
149 gl_font_history_model_new (guint n)
151 glFontHistoryModel *this;
153 this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
155 this->priv->max_n = n;
161 /*****************************************************************************/
162 /* Add font to history. */
163 /*****************************************************************************/
165 gl_font_history_model_add_family (glFontHistoryModel *this,
174 * Start new list with this family.
176 list = g_slist_append (list, (gchar *)family);
179 * Transfer old list to new list, ignoring any duplicate of this family
181 old_list = gl_font_history_model_get_family_list (this);
182 for ( p = old_list; p; p=p->next )
184 if ( lgl_str_utf8_casecmp (family, p->data) )
186 list = g_slist_append (list, p->data);
193 g_list_free (old_list);
196 * Truncate list to maximum size
198 while (g_slist_length (list) > this->priv->max_n)
200 ps = g_slist_last (list);
201 list = g_slist_remove_link (list, ps);
208 gconf_client_set_list (this->priv->gconf_client,
216 /*****************************************************************************/
217 /* GConf notify callback. */
218 /*****************************************************************************/
220 conf_notify_cb (GConfClient *client,
223 glFontHistoryModel *this)
225 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
229 /*****************************************************************************/
230 /* Get list of font families. */
231 /*****************************************************************************/
233 gl_font_history_model_get_family_list (glFontHistoryModel *this)
242 tmp_list = gconf_client_get_list (this->priv->gconf_client,
248 * Proof read family list; transfer storage to new list.
250 for (p=tmp_list; p != NULL; p=p->next)
252 if ( gl_font_util_is_family_installed (p->data) )
254 list = g_list_append (list, p->data);
261 g_slist_free (tmp_list);
267 /*****************************************************************************/
268 /* Free font family list. */
269 /*****************************************************************************/
271 gl_font_history_model_free_family_list (GList *list)
275 for ( p = list; p; p=p->next )
285 * Local Variables: -- emacs
287 * c-basic-offset: 8 -- emacs
288 * tab-width: 8 -- emacs
289 * indent-tabs-mode: nil -- emacs