]> git.sur5r.net Git - glabels/blob - src/color-history-model.c
Theme friendly, glabels specific, action icons.
[glabels] / src / color-history-model.c
1 /*
2  *  color-history-model.c
3  *  Copyright (C) 2008-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 "color-history-model.h"
24
25 #include <gconf/gconf-client.h>
26
27 #include "marshal.h"
28
29
30 #define BASE_KEY          "/apps/glabels"
31 #define RECENT_COLORS_KEY  BASE_KEY "/recent-colors"
32
33
34 /*========================================================*/
35 /* Private types.                                         */
36 /*========================================================*/
37
38 /** GL_COLOR_HISTORY_MODEL Private fields */
39 struct _glColorHistoryModelPrivate {
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_color_history_model_finalize     (GObject             *object);
64
65 static void conf_notify_cb                      (GConfClient         *client,
66                                                  guint                cnxn_id,
67                                                  GConfEntry          *entry,
68                                                  glColorHistoryModel *this);
69
70 static GSList *get_color_list                   (glColorHistoryModel *this);
71
72
73 /*****************************************************************************/
74 /* Object infrastructure.                                                    */
75 /*****************************************************************************/
76 G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT);
77
78
79 /*****************************************************************************/
80 /* Class Init Function.                                                      */
81 /*****************************************************************************/
82 static void
83 gl_color_history_model_class_init (glColorHistoryModelClass *class)
84 {
85         GObjectClass  *gobject_class = (GObjectClass *) class;
86
87         gl_color_history_model_parent_class = g_type_class_peek_parent (class);
88
89         gobject_class->finalize = gl_color_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 (glColorHistoryModelClass, 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_color_history_model_init (glColorHistoryModel *this)
108 {
109         this->priv = g_new0 (glColorHistoryModelPrivate, 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_COLORS_KEY,
122                                  (GConfClientNotifyFunc)conf_notify_cb, this,
123                                  NULL, NULL);
124 }
125
126
127 /*****************************************************************************/
128 /* Finalize Method.                                                          */
129 /*****************************************************************************/
130 static void
131 gl_color_history_model_finalize (GObject *object)
132 {
133         glColorHistoryModel   *this;
134
135         g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
136         this = GL_COLOR_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_color_history_model_parent_class)->finalize (object);
142 }
143
144
145 /*****************************************************************************/
146 /** New Object Generator.                                                    */
147 /*****************************************************************************/
148 glColorHistoryModel *
149 gl_color_history_model_new (guint n)
150 {
151         glColorHistoryModel *this;
152
153         this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);
154
155         this->priv->max_n = n;
156
157         return this;
158 }
159
160
161 /*****************************************************************************/
162 /* Add color to history.                                                      */
163 /*****************************************************************************/
164 void
165 gl_color_history_model_add_color (glColorHistoryModel *this,
166                                   guint                color)
167 {
168         GSList  *list = NULL;
169         GSList  *old_list;
170         GSList  *p;
171
172         /*
173          * Start new list with this color.
174          */
175         list = g_slist_append (list, GINT_TO_POINTER (color));
176
177         /*
178          * Transfer old list to new list, ignoring any duplicate of this color
179          */
180         old_list = get_color_list (this);
181         for ( p = old_list; p; p=p->next )
182         {
183                 if ( color != (guint)GPOINTER_TO_INT (p->data) )
184                 {
185                         list = g_slist_append (list, p->data);
186                 }
187         }
188         g_slist_free (old_list);
189
190         /*
191          * Truncate list to maximum size
192          */
193         while (g_slist_length (list) > this->priv->max_n)
194         {
195                 p = g_slist_last (list);
196                 list = g_slist_remove_link (list, p);
197                 g_slist_free_1 (p);
198         }
199
200         /*
201          * Update conf
202          */
203         gconf_client_set_list (this->priv->gconf_client,
204                                RECENT_COLORS_KEY,
205                                GCONF_VALUE_INT,
206                                list,
207                                NULL);
208 }
209
210
211 /*****************************************************************************/
212 /* GConf notify callback.                                                    */
213 /*****************************************************************************/
214 static void
215 conf_notify_cb (GConfClient         *client,
216                 guint                cnxn_id,
217                 GConfEntry          *entry,
218                 glColorHistoryModel  *this)
219 {
220         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
221 }
222
223
224 /*****************************************************************************/
225 /* Get list of colors.                                                       */
226 /*****************************************************************************/
227 static GSList *
228 get_color_list (glColorHistoryModel *this)
229 {
230         GSList *list;
231
232         /*
233          * Get color list.
234          */
235         list = gconf_client_get_list (this->priv->gconf_client,
236                                       RECENT_COLORS_KEY,
237                                       GCONF_VALUE_INT,
238                                       NULL);
239         return list;
240 }
241
242
243 /*****************************************************************************/
244 /* Get color.                                                                */
245 /*****************************************************************************/
246 guint
247 gl_color_history_model_get_color (glColorHistoryModel *this,
248                                   guint                i)
249 {
250         guint   color = 0;
251         GSList *list;
252         GSList *p;
253
254         list = get_color_list (this);
255         p = g_slist_nth (list, i);
256         if (p)
257         {
258                 color = GPOINTER_TO_INT (p->data);
259         }
260         g_slist_free (list);
261
262         return color;
263 }
264
265
266
267
268 /*
269  * Local Variables:       -- emacs
270  * mode: C                -- emacs
271  * c-basic-offset: 8      -- emacs
272  * tab-width: 8           -- emacs
273  * indent-tabs-mode: nil  -- emacs
274  * End:                   -- emacs
275  */