]> git.sur5r.net Git - glabels/blob - src/font-history-model.c
e7037d150e61b8035e3039a1c157316833de6890
[glabels] / src / font-history-model.c
1 /*
2  *  font-history-model.c
3  *  Copyright (C) 2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "font-history-model.h"
24
25 #include <gconf/gconf-client.h>
26
27 #include <libglabels/libglabels.h>
28 #include "marshal.h"
29
30
31 #define BASE_KEY          "/apps/glabels"
32 #define RECENT_FONTS_KEY  BASE_KEY "/recent-fonts"
33
34 /*========================================================*/
35 /* Private types.                                         */
36 /*========================================================*/
37
38 /** GL_FONT_HISTORY_MODEL Private fields */
39 struct _glFontHistoryModelPrivate {
40
41         GConfClient *gconf_client;
42
43         guint        max_n;
44 };
45
46 enum {
47         CHANGED,
48         LAST_SIGNAL
49 };
50
51
52 /*========================================================*/
53 /* Private globals.                                       */
54 /*========================================================*/
55
56 static guint signals[LAST_SIGNAL] = {0};
57
58
59 /*========================================================*/
60 /* Private function prototypes.                           */
61 /*========================================================*/
62
63 static void gl_font_history_model_finalize      (GObject             *object);
64
65 static void conf_notify_cb                      (GConfClient         *client,
66                                                  guint                cnxn_id,
67                                                  GConfEntry          *entry,
68                                                  glFontHistoryModel  *this);
69
70
71 /*****************************************************************************/
72 /* Object infrastructure.                                                    */
73 /*****************************************************************************/
74 G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT);
75
76
77 /*****************************************************************************/
78 /* Class Init Function.                                                      */
79 /*****************************************************************************/
80 static void
81 gl_font_history_model_class_init (glFontHistoryModelClass *class)
82 {
83         GObjectClass  *gobject_class = (GObjectClass *) class;
84
85         gl_font_history_model_parent_class = g_type_class_peek_parent (class);
86
87         gobject_class->finalize = gl_font_history_model_finalize;
88
89         signals[CHANGED] =
90                 g_signal_new ("changed",
91                               G_OBJECT_CLASS_TYPE (gobject_class),
92                               G_SIGNAL_RUN_LAST,
93                               G_STRUCT_OFFSET (glFontHistoryModelClass, changed),
94                               NULL, NULL,
95                               gl_marshal_VOID__VOID,
96                               G_TYPE_NONE,
97                               0);
98 }
99
100
101 /*****************************************************************************/
102 /* Object Instance Init Function.                                            */
103 /*****************************************************************************/
104 static void
105 gl_font_history_model_init (glFontHistoryModel *this)
106 {
107         this->priv = g_new0 (glFontHistoryModelPrivate, 1);
108
109         this->priv->gconf_client = gconf_client_get_default ();
110
111         g_return_if_fail (this->priv->gconf_client != NULL);
112
113         gconf_client_add_dir (this->priv->gconf_client,
114                               BASE_KEY,
115                               GCONF_CLIENT_PRELOAD_ONELEVEL,
116                               NULL);
117
118         gconf_client_notify_add (this->priv->gconf_client,
119                                  RECENT_FONTS_KEY,
120                                  (GConfClientNotifyFunc)conf_notify_cb, this,
121                                  NULL, NULL);
122 }
123
124
125 /*****************************************************************************/
126 /* Finalize Method.                                                          */
127 /*****************************************************************************/
128 static void
129 gl_font_history_model_finalize (GObject *object)
130 {
131         glFontHistoryModel    *this;
132         GSList                *p;
133
134         g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
135         this = GL_FONT_HISTORY_MODEL (object);
136
137         g_object_unref (G_OBJECT(this->priv->gconf_client));
138         g_free (this->priv);
139
140         G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
141 }
142
143
144 /*****************************************************************************/
145 /** New Object Generator.                                                    */
146 /*****************************************************************************/
147 glFontHistoryModel *
148 gl_font_history_model_new (guint n)
149 {
150         glFontHistoryModel *this;
151
152         this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
153
154         this->priv->max_n = n;
155
156         return this;
157 }
158
159
160 /*****************************************************************************/
161 /* Add font to history.                                                      */
162 /*****************************************************************************/
163 void
164 gl_font_history_model_add_family (glFontHistoryModel *this,
165                                   const gchar        *family)
166 {
167         GSList *list = NULL;
168         GList  *old_list;
169         GList  *p;
170         GSList *ps;
171
172         /*
173          * Start new list with this family.
174          */
175         list = g_slist_append (list, (gchar *)family);
176
177         /*
178          * Transfer old list to new list, ignoring any duplicate of this family
179          */
180         old_list = gl_font_history_model_get_family_list (this);
181         for ( p = old_list; p; p=p->next )
182         {
183                 if ( lgl_str_utf8_casecmp (family, p->data) )
184                 {
185                         list = g_slist_append (list, p->data);
186                 }
187                 else
188                 {
189                         g_free (p->data);
190                 }
191         }
192         g_list_free (old_list);
193
194         /*
195          * Truncate list to maximum size
196          */
197         while (g_slist_length (list) > this->priv->max_n)
198         {
199                 ps = g_slist_last (list);
200                 list = g_slist_remove_link (list, ps);
201                 g_slist_free_1 (ps);
202         }
203
204         /*
205          * Update conf
206          */
207         gconf_client_set_list (this->priv->gconf_client,
208                                RECENT_FONTS_KEY,
209                                GCONF_VALUE_STRING,
210                                list,
211                                NULL);
212 }
213
214
215 /*****************************************************************************/
216 /* GConf notify callback.                                                    */
217 /*****************************************************************************/
218 static void
219 conf_notify_cb (GConfClient         *client,
220                 guint                cnxn_id,
221                 GConfEntry          *entry,
222                 glFontHistoryModel  *this)
223 {
224         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
225 }
226
227
228 /*****************************************************************************/
229 /* Get list of font families.                                                */
230 /*****************************************************************************/
231 GList *
232 gl_font_history_model_get_family_list (glFontHistoryModel *this)
233 {
234         GList  *list = NULL;
235         GSList *tmp_list;
236         GSList *p;
237
238         /*
239          * Get family list.
240          */
241         tmp_list = gconf_client_get_list (this->priv->gconf_client,
242                                           RECENT_FONTS_KEY,
243                                           GCONF_VALUE_STRING,
244                                           NULL);
245
246         /*
247          * Proof read family list; transfer storage to new list.
248          */
249         for (p=tmp_list; p != NULL; p=p->next)
250         {
251                 if ( gl_font_util_is_family_installed (p->data) )
252                 {
253                         list = g_list_append (list, p->data);
254                 }
255                 else
256                 {
257                         g_free (p->data);
258                 }
259         }
260         g_slist_free (tmp_list);
261
262         return list;
263 }
264
265
266 /*****************************************************************************/
267 /* Free font family list.                                                    */
268 /*****************************************************************************/
269 void
270 gl_font_history_model_free_family_list (GList *list)
271 {
272         GList *p;
273
274         for ( p = list; p; p=p->next )
275         {
276                 g_free (p->data);
277         }
278         g_list_free (list);
279 }
280
281
282
283 /*
284  * Local Variables:       -- emacs
285  * mode: C                -- emacs
286  * c-basic-offset: 8      -- emacs
287  * tab-width: 8           -- emacs
288  * indent-tabs-mode: nil  -- emacs
289  * End:                   -- emacs
290  */