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