]> git.sur5r.net Git - glabels/blob - src/font-history-model.c
Imported Upstream version 3.4.0
[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 <gio/gio.h>
26
27 #include <libglabels.h>
28 #include "font-util.h"
29 #include "marshal.h"
30
31
32 /*========================================================*/
33 /* Private types.                                         */
34 /*========================================================*/
35
36 struct _glFontHistoryModelPrivate {
37
38         GSettings   *history;
39
40         guint        max_n;
41 };
42
43 enum {
44         CHANGED,
45         LAST_SIGNAL
46 };
47
48
49 /*========================================================*/
50 /* Private globals.                                       */
51 /*========================================================*/
52
53 static guint signals[LAST_SIGNAL] = {0};
54
55
56 /*========================================================*/
57 /* Private function prototypes.                           */
58 /*========================================================*/
59
60 static void gl_font_history_model_finalize      (GObject             *object);
61
62 static void history_changed_cb                  (glFontHistoryModel  *this);
63
64
65 /*****************************************************************************/
66 /* Object infrastructure.                                                    */
67 /*****************************************************************************/
68 G_DEFINE_TYPE (glFontHistoryModel, gl_font_history_model, G_TYPE_OBJECT)
69
70
71 /*****************************************************************************/
72 /* Class Init Function.                                                      */
73 /*****************************************************************************/
74 static void
75 gl_font_history_model_class_init (glFontHistoryModelClass *class)
76 {
77         GObjectClass  *gobject_class = (GObjectClass *) class;
78
79         gl_font_history_model_parent_class = g_type_class_peek_parent (class);
80
81         gobject_class->finalize = gl_font_history_model_finalize;
82
83         signals[CHANGED] =
84                 g_signal_new ("changed",
85                               G_OBJECT_CLASS_TYPE (gobject_class),
86                               G_SIGNAL_RUN_LAST,
87                               G_STRUCT_OFFSET (glFontHistoryModelClass, changed),
88                               NULL, NULL,
89                               gl_marshal_VOID__VOID,
90                               G_TYPE_NONE,
91                               0);
92 }
93
94
95 /*****************************************************************************/
96 /* Object Instance Init Function.                                            */
97 /*****************************************************************************/
98 static void
99 gl_font_history_model_init (glFontHistoryModel *this)
100 {
101         this->priv = g_new0 (glFontHistoryModelPrivate, 1);
102 }
103
104
105 /*****************************************************************************/
106 /* Finalize Method.                                                          */
107 /*****************************************************************************/
108 static void
109 gl_font_history_model_finalize (GObject *object)
110 {
111         glFontHistoryModel    *this;
112
113         g_return_if_fail (object && IS_GL_FONT_HISTORY_MODEL (object));
114         this = GL_FONT_HISTORY_MODEL (object);
115
116         g_object_unref (G_OBJECT(this->priv->history));
117         g_free (this->priv);
118
119         G_OBJECT_CLASS (gl_font_history_model_parent_class)->finalize (object);
120 }
121
122
123 /*****************************************************************************/
124 /** New Object Generator.                                                    */
125 /*****************************************************************************/
126 glFontHistoryModel *
127 gl_font_history_model_new (guint n)
128 {
129         glFontHistoryModel *this;
130
131         this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
132
133         this->priv->history = g_settings_new ("org.gnome.glabels-3.history");
134
135         g_signal_connect_swapped (G_OBJECT (this->priv->history),
136                                   "changed::recent-fonts",
137                                   G_CALLBACK (history_changed_cb), this);
138
139         this->priv->max_n = n;
140
141         return this;
142 }
143
144
145 /*****************************************************************************/
146 /** New Null Object Generator.                                               */
147 /*****************************************************************************/
148 glFontHistoryModel *
149 gl_font_history_model_new_null (void)
150 {
151         glFontHistoryModel *this;
152
153         this = g_object_new (TYPE_GL_FONT_HISTORY_MODEL, NULL);
154
155         this->priv->max_n = 0;
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         if ( this->priv->history )
169         {
170                 gchar **old;
171                 gchar **new;
172                 gint    i, j;
173
174                 old = g_settings_get_strv (this->priv->history, "recent-fonts");
175                                    
176                 new = g_new0 (gchar *, this->priv->max_n+1);
177
178                 /* Put in first slot. */
179                 new[0] = g_strdup (family);
180
181                 /* Push everthing else down, pruning any duplicate found. */
182                 for ( i = 0, j = 1; (j < (this->priv->max_n-1)) && old[i]; i++ )
183                 {
184                         if ( lgl_str_utf8_casecmp (family, old[i]) != 0 )
185                         {
186                                 new[j++] = g_strdup (old[i]);
187                         }
188                 }
189
190                 g_settings_set_strv (this->priv->history, "recent-fonts",
191                                      (const gchar * const *)new);
192
193                 g_strfreev (old);
194                 g_strfreev (new);
195         }
196 }
197
198
199 /*****************************************************************************/
200 /* History changed callback.                                                 */
201 /*****************************************************************************/
202 static void
203 history_changed_cb                  (glFontHistoryModel  *this)
204 {
205         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
206 }
207
208
209 /*****************************************************************************/
210 /* Get list of font families.                                                */
211 /*****************************************************************************/
212 GList *
213 gl_font_history_model_get_family_list (glFontHistoryModel *this)
214 {
215         GList  *list = NULL;
216
217         if ( this->priv->history )
218         {
219                 gchar **strv;
220                 gint    i;
221
222                 strv = g_settings_get_strv (this->priv->history, "recent-fonts");
223
224                 /*
225                  * Proof read name list; transfer storage to new list.
226                  */
227                 for ( i = 0; strv[i]; i++ )
228                 {
229                         if ( gl_font_util_is_family_installed (strv[i]) )
230                         {
231                                 list = g_list_append (list, g_strdup (strv[i]));
232                         }
233                 }
234                 g_strfreev (strv);
235         }
236
237         return list;
238 }
239
240
241 /*****************************************************************************/
242 /* Free font family list.                                                    */
243 /*****************************************************************************/
244 void
245 gl_font_history_model_free_family_list (GList *list)
246 {
247         GList *p;
248
249         for ( p = list; p; p=p->next )
250         {
251                 g_free (p->data);
252         }
253         g_list_free (list);
254 }
255
256
257
258 /*
259  * Local Variables:       -- emacs
260  * mode: C                -- emacs
261  * c-basic-offset: 8      -- emacs
262  * tab-width: 8           -- emacs
263  * indent-tabs-mode: nil  -- emacs
264  * End:                   -- emacs
265  */