]> git.sur5r.net Git - glabels/blob - src/template-history-model.c
Theme friendly, glabels specific, action icons.
[glabels] / src / template-history-model.c
1 /*
2  *  template-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 "template-history-model.h"
24
25 #include <gconf/gconf-client.h>
26
27 #include <libglabels.h>
28 #include "marshal.h"
29
30
31 #define BASE_KEY              "/apps/glabels"
32 #define RECENT_TEMPLATES_KEY  BASE_KEY "/recent-templates"
33
34
35 /*========================================================*/
36 /* Private types.                                         */
37 /*========================================================*/
38
39 /** GL_TEMPLATE_HISTORY_MODEL Private fields */
40 struct _glTemplateHistoryModelPrivate {
41
42         GConfClient *gconf_client;
43
44         guint        max_n;
45 };
46
47 enum {
48         CHANGED,
49         LAST_SIGNAL
50 };
51
52
53 /*========================================================*/
54 /* Private globals.                                       */
55 /*========================================================*/
56
57 static guint signals[LAST_SIGNAL] = {0};
58
59
60 /*========================================================*/
61 /* Private function prototypes.                           */
62 /*========================================================*/
63
64 static void gl_template_history_model_finalize (GObject                 *object);
65
66 static void conf_notify_cb                     (GConfClient             *client,
67                                                 guint                    cnxn_id,
68                                                 GConfEntry              *entry,
69                                                 glTemplateHistoryModel  *this);
70
71
72 /*****************************************************************************/
73 /* Object infrastructure.                                                    */
74 /*****************************************************************************/
75 G_DEFINE_TYPE (glTemplateHistoryModel, gl_template_history_model, G_TYPE_OBJECT);
76
77
78 /*****************************************************************************/
79 /* Class Init Function.                                                      */
80 /*****************************************************************************/
81 static void
82 gl_template_history_model_class_init (glTemplateHistoryModelClass *class)
83 {
84         GObjectClass  *gobject_class = (GObjectClass *) class;
85
86         gl_template_history_model_parent_class = g_type_class_peek_parent (class);
87
88         gobject_class->finalize = gl_template_history_model_finalize;
89
90         signals[CHANGED] =
91                 g_signal_new ("changed",
92                               G_OBJECT_CLASS_TYPE (gobject_class),
93                               G_SIGNAL_RUN_LAST,
94                               G_STRUCT_OFFSET (glTemplateHistoryModelClass, changed),
95                               NULL, NULL,
96                               gl_marshal_VOID__VOID,
97                               G_TYPE_NONE,
98                               0);
99 }
100
101
102 /*****************************************************************************/
103 /* Object Instance Init Function.                                            */
104 /*****************************************************************************/
105 static void
106 gl_template_history_model_init (glTemplateHistoryModel *this)
107 {
108         this->priv = g_new0 (glTemplateHistoryModelPrivate, 1);
109
110         this->priv->gconf_client = gconf_client_get_default ();
111
112         g_return_if_fail (this->priv->gconf_client != NULL);
113
114         gconf_client_add_dir (this->priv->gconf_client,
115                               BASE_KEY,
116                               GCONF_CLIENT_PRELOAD_ONELEVEL,
117                               NULL);
118
119         gconf_client_notify_add (this->priv->gconf_client,
120                                  RECENT_TEMPLATES_KEY,
121                                  (GConfClientNotifyFunc)conf_notify_cb, this,
122                                  NULL, NULL);
123 }
124
125
126 /*****************************************************************************/
127 /* Finalize Method.                                                          */
128 /*****************************************************************************/
129 static void
130 gl_template_history_model_finalize (GObject *object)
131 {
132         glTemplateHistoryModel    *this;
133
134         g_return_if_fail (object && IS_GL_TEMPLATE_HISTORY_MODEL (object));
135         this = GL_TEMPLATE_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_template_history_model_parent_class)->finalize (object);
141 }
142
143
144 /*****************************************************************************/
145 /** New Object Generator.                                                    */
146 /*****************************************************************************/
147 glTemplateHistoryModel *
148 gl_template_history_model_new (guint n)
149 {
150         glTemplateHistoryModel *this;
151
152         this = g_object_new (TYPE_GL_TEMPLATE_HISTORY_MODEL, NULL);
153
154         this->priv->max_n = n;
155
156         return this;
157 }
158
159
160 /*****************************************************************************/
161 /* Add template to history.                                                      */
162 /*****************************************************************************/
163 void
164 gl_template_history_model_add_name (glTemplateHistoryModel *this,
165                                     const gchar            *name)
166 {
167         GSList *list = NULL;
168         GList  *old_list;
169         GList  *p;
170         GSList *ps;
171
172         /*
173          * Start new list with this name.
174          */
175         list = g_slist_append (list, (gchar *)name);
176
177         /*
178          * Transfer old list to new list, ignoring any duplicate of this name
179          */
180         old_list = gl_template_history_model_get_name_list (this);
181         for ( p = old_list; p; p=p->next )
182         {
183                 if ( lgl_str_utf8_casecmp (name, 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_TEMPLATES_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                 glTemplateHistoryModel  *this)
223 {
224         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
225 }
226
227
228 /*****************************************************************************/
229 /* Get list of template families.                                            */
230 /*****************************************************************************/
231 GList *
232 gl_template_history_model_get_name_list (glTemplateHistoryModel *this)
233 {
234         GList  *list = NULL;
235         GSList *tmp_list;
236         GSList *p;
237
238         /*
239          * Get name list.
240          */
241         tmp_list = gconf_client_get_list (this->priv->gconf_client,
242                                           RECENT_TEMPLATES_KEY,
243                                           GCONF_VALUE_STRING,
244                                           NULL);
245
246         /*
247          * Proof read name list; transfer storage to new list.
248          */
249         for (p=tmp_list; p != NULL; p=p->next)
250         {
251                 if ( lgl_db_does_template_name_exist (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 template name list.                                                  */
268 /*****************************************************************************/
269 void
270 gl_template_history_model_free_name_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  */