]> git.sur5r.net Git - glabels/blob - src/color-history-model.c
Imported Upstream version 3.0.0
[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 <gio/gio.h>
26
27 #include "marshal.h"
28
29
30 /*========================================================*/
31 /* Private types.                                         */
32 /*========================================================*/
33
34 struct _glColorHistoryModelPrivate {
35
36         GSettings   *history;
37
38         guint        max_n;
39 };
40
41 enum {
42         CHANGED,
43         LAST_SIGNAL
44 };
45
46
47 /*========================================================*/
48 /* Private globals.                                       */
49 /*========================================================*/
50
51 static guint signals[LAST_SIGNAL] = {0};
52
53
54 /*========================================================*/
55 /* Private function prototypes.                           */
56 /*========================================================*/
57
58 static void    gl_color_history_model_finalize  (GObject             *object);
59
60 static void    history_changed_cb               (glColorHistoryModel *this);
61
62 static guint  *get_color_array                  (glColorHistoryModel *this,
63                                                  guint               *n_elements);
64 static void    set_color_array                  (glColorHistoryModel *this,
65                                                  guint               *array,
66                                                  guint                n_elements);
67
68
69 /*****************************************************************************/
70 /* Object infrastructure.                                                    */
71 /*****************************************************************************/
72 G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT)
73
74
75 /*****************************************************************************/
76 /* Class Init Function.                                                      */
77 /*****************************************************************************/
78 static void
79 gl_color_history_model_class_init (glColorHistoryModelClass *class)
80 {
81         GObjectClass  *gobject_class = (GObjectClass *) class;
82
83         gl_color_history_model_parent_class = g_type_class_peek_parent (class);
84
85         gobject_class->finalize = gl_color_history_model_finalize;
86
87         signals[CHANGED] =
88                 g_signal_new ("changed",
89                               G_OBJECT_CLASS_TYPE (gobject_class),
90                               G_SIGNAL_RUN_LAST,
91                               G_STRUCT_OFFSET (glColorHistoryModelClass, changed),
92                               NULL, NULL,
93                               gl_marshal_VOID__VOID,
94                               G_TYPE_NONE,
95                               0);
96 }
97
98
99 /*****************************************************************************/
100 /* Object Instance Init Function.                                            */
101 /*****************************************************************************/
102 static void
103 gl_color_history_model_init (glColorHistoryModel *this)
104 {
105         this->priv = g_new0 (glColorHistoryModelPrivate, 1);
106
107         this->priv->history = g_settings_new ("org.gnome.glabels-3.history");
108
109         g_return_if_fail (this->priv->history != NULL);
110
111         g_signal_connect_swapped (G_OBJECT (this->priv->history),
112                                   "changed::recent-colors",
113                                   G_CALLBACK (history_changed_cb), this);
114 }
115
116
117 /*****************************************************************************/
118 /* Finalize Method.                                                          */
119 /*****************************************************************************/
120 static void
121 gl_color_history_model_finalize (GObject *object)
122 {
123         glColorHistoryModel   *this;
124
125         g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
126         this = GL_COLOR_HISTORY_MODEL (object);
127
128         g_object_unref (G_OBJECT(this->priv->history));
129         g_free (this->priv);
130
131         G_OBJECT_CLASS (gl_color_history_model_parent_class)->finalize (object);
132 }
133
134
135 /*****************************************************************************/
136 /** New Object Generator.                                                    */
137 /*****************************************************************************/
138 glColorHistoryModel *
139 gl_color_history_model_new (guint n)
140 {
141         glColorHistoryModel *this;
142
143         this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);
144
145         this->priv->max_n = n;
146
147         return this;
148 }
149
150
151 /*****************************************************************************/
152 /* Add color to history.                                                      */
153 /*****************************************************************************/
154 void
155 gl_color_history_model_add_color (glColorHistoryModel *this,
156                                   guint                color)
157 {
158         guint  *old;
159         guint  *new;
160         guint   i, n;
161
162         old = get_color_array (this, &n);
163                                    
164         new = g_new0 (guint, this->priv->max_n);
165
166         new[0] = color;
167
168         for ( i = 0; (i < (this->priv->max_n-1)) && (i < n); i++ )
169         {
170                 new[i+1] = old[i];
171         }
172
173         set_color_array (this, new, i+1);
174
175         g_free (old);
176         g_free (new);
177 }
178
179
180 /*****************************************************************************/
181 /* History changed callback.                                                 */
182 /*****************************************************************************/
183 static void
184 history_changed_cb (glColorHistoryModel  *this)
185 {
186         g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
187 }
188
189
190 /*****************************************************************************/
191 /* Get list of colors.                                                       */
192 /*****************************************************************************/
193 static guint *
194 get_color_array (glColorHistoryModel *this,
195                  guint               *n_elements)
196 {
197         GVariant *value;
198         GVariant *child_value;
199         gsize     i;
200         guint    *array;
201
202         value = g_settings_get_value (this->priv->history, "recent-colors");
203         *n_elements = g_variant_n_children (value);
204
205         array = g_new0 (guint, *n_elements);
206
207         for ( i = 0; i < *n_elements; i++ )
208         {
209                 child_value = g_variant_get_child_value (value, i);
210                 array[i] = g_variant_get_uint32 (child_value);
211                 g_variant_unref (child_value);
212         }
213
214         g_variant_unref (value);
215
216         return array;
217 }
218
219
220 /*****************************************************************************/
221 /* Set list of colors.                                                       */
222 /*****************************************************************************/
223 static void
224 set_color_array (glColorHistoryModel *this,
225                  guint               *array,
226                  guint                n_elements)
227 {
228         GVariant  *value;
229         GVariant **child_values;
230         gsize      i;
231
232         child_values = g_new (GVariant *, n_elements);
233
234         for ( i = 0; i < n_elements; i++ )
235         {
236                 child_values[i] = g_variant_new_uint32 (array[i]);
237         }
238
239         value = g_variant_new_array (G_VARIANT_TYPE_UINT32, child_values, n_elements);
240
241         g_settings_set_value (this->priv->history, "recent-colors", value);
242
243         g_free (child_values);
244 }
245
246
247 /*****************************************************************************/
248 /* Get color.                                                                */
249 /*****************************************************************************/
250 guint
251 gl_color_history_model_get_color (glColorHistoryModel *this,
252                                   guint                i)
253 {
254         guint  *array;
255         guint   color = 0;
256         guint   n;
257
258         array = get_color_array (this, &n);
259         if ( array && (i < n) )
260         {
261                 color = array[i];
262         }
263         g_free (array);
264
265         return color;
266 }
267
268
269
270
271 /*
272  * Local Variables:       -- emacs
273  * mode: C                -- emacs
274  * c-basic-offset: 8      -- emacs
275  * tab-width: 8           -- emacs
276  * indent-tabs-mode: nil  -- emacs
277  * End:                   -- emacs
278  */