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