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"
27 #include <libglabels.h>
28 #include "font-util.h"
32 /*========================================================*/
34 /*========================================================*/
36 struct _glFontHistoryModelPrivate {
49 /*========================================================*/
50 /* Private globals. */
51 /*========================================================*/
53 static guint signals[LAST_SIGNAL] = {0};
56 /*========================================================*/
57 /* Private function prototypes. */
58 /*========================================================*/
60 static void gl_font_history_model_finalize (GObject *object);
62 static void history_changed_cb (glFontHistoryModel *this);
65 /*****************************************************************************/
66 /* Object infrastructure. */
67 /*****************************************************************************/
68 G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT)
71 /*****************************************************************************/
72 /* Class Init Function. */
73 /*****************************************************************************/
75 gl_font_history_model_class_init (glFontHistoryModelClass *class)
77 GObjectClass *gobject_class = (GObjectClass *) class;
79 gl_font_history_model_parent_class = g_type_class_peek_parent (class);
81 gobject_class->finalize = gl_font_history_model_finalize;
84 g_signal_new ("changed",
85 G_OBJECT_CLASS_TYPE (gobject_class),
87 G_STRUCT_OFFSET (glFontHistoryModelClass, changed),
89 gl_marshal_VOID__VOID,
95 /*****************************************************************************/
96 /* Object Instance Init Function. */
97 /*****************************************************************************/
99 gl_font_history_model_init (glFontHistoryModel *this)
101 this->priv = g_new0 (glFontHistoryModelPrivate, 1);
103 this->priv->history = g_settings_new ("org.gnome.glabels-3.history");
105 g_return_if_fail (this->priv->history != NULL);
107 g_signal_connect_swapped (G_OBJECT (this->priv->history),
108 "changed::recent-fonts",
109 G_CALLBACK (history_changed_cb), this);
113 /*****************************************************************************/
114 /* Finalize Method. */
115 /*****************************************************************************/
117 gl_font_history_model_finalize (GObject *object)
119 glFontHistoryModel *this;
121 g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
122 this = GL_FONT_HISTORY_MODEL (object);
124 g_object_unref (G_OBJECT(this->priv->history));
127 G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
131 /*****************************************************************************/
132 /** New Object Generator. */
133 /*****************************************************************************/
135 gl_font_history_model_new (guint n)
137 glFontHistoryModel *this;
139 this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
141 this->priv->max_n = n;
147 /*****************************************************************************/
148 /* Add font to history. */
149 /*****************************************************************************/
151 gl_font_history_model_add_family (glFontHistoryModel *this,
158 old = g_settings_get_strv (this->priv->history, "recent-fonts");
160 new = g_new0 (gchar *, this->priv->max_n+1);
162 /* Put in first slot. */
163 new[0] = g_strdup (family);
165 /* Push everthing else down, pruning any duplicate found. */
166 for ( i = 0, j = 1; (j < (this->priv->max_n-1)) && old[i]; i++ )
168 if ( lgl_str_utf8_casecmp (family, old[i]) != 0 )
170 new[j++] = g_strdup (old[i]);
174 g_settings_set_strv (this->priv->history, "recent-fonts",
175 (const gchar * const *)new);
182 /*****************************************************************************/
183 /* History changed callback. */
184 /*****************************************************************************/
186 history_changed_cb (glFontHistoryModel *this)
188 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
192 /*****************************************************************************/
193 /* Get list of font families. */
194 /*****************************************************************************/
196 gl_font_history_model_get_family_list (glFontHistoryModel *this)
202 strv = g_settings_get_strv (this->priv->history, "recent-fonts");
205 * Proof read name list; transfer storage to new list.
207 for ( i = 0; strv[i]; i++ )
209 if ( gl_font_util_is_family_installed (strv[i]) )
211 list = g_list_append (list, g_strdup (strv[i]));
220 /*****************************************************************************/
221 /* Free font family list. */
222 /*****************************************************************************/
224 gl_font_history_model_free_family_list (GList *list)
228 for ( p = list; p; p=p->next )
238 * Local Variables: -- emacs
240 * c-basic-offset: 8 -- emacs
241 * tab-width: 8 -- emacs
242 * indent-tabs-mode: nil -- emacs