2 * color-history-model.c
3 * Copyright (C) 2008-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
23 #include "color-history-model.h"
30 /*========================================================*/
32 /*========================================================*/
34 struct _glColorHistoryModelPrivate {
47 /*========================================================*/
48 /* Private globals. */
49 /*========================================================*/
51 static guint signals[LAST_SIGNAL] = {0};
54 /*========================================================*/
55 /* Private function prototypes. */
56 /*========================================================*/
58 static void gl_color_history_model_finalize (GObject *object);
60 static void history_changed_cb (glColorHistoryModel *this);
62 static guint *get_color_array (glColorHistoryModel *this,
64 static void set_color_array (glColorHistoryModel *this,
69 /*****************************************************************************/
70 /* Object infrastructure. */
71 /*****************************************************************************/
72 G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT)
75 /*****************************************************************************/
76 /* Class Init Function. */
77 /*****************************************************************************/
79 gl_color_history_model_class_init (glColorHistoryModelClass *class)
81 GObjectClass *gobject_class = (GObjectClass *) class;
83 gl_color_history_model_parent_class = g_type_class_peek_parent (class);
85 gobject_class->finalize = gl_color_history_model_finalize;
88 g_signal_new ("changed",
89 G_OBJECT_CLASS_TYPE (gobject_class),
91 G_STRUCT_OFFSET (glColorHistoryModelClass, changed),
93 gl_marshal_VOID__VOID,
99 /*****************************************************************************/
100 /* Object Instance Init Function. */
101 /*****************************************************************************/
103 gl_color_history_model_init (glColorHistoryModel *this)
105 this->priv = g_new0 (glColorHistoryModelPrivate, 1);
107 this->priv->history = g_settings_new ("org.gnome.glabels-3.history");
109 g_return_if_fail (this->priv->history != NULL);
111 g_signal_connect_swapped (G_OBJECT (this->priv->history),
112 "changed::recent-colors",
113 G_CALLBACK (history_changed_cb), this);
117 /*****************************************************************************/
118 /* Finalize Method. */
119 /*****************************************************************************/
121 gl_color_history_model_finalize (GObject *object)
123 glColorHistoryModel *this;
125 g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
126 this = GL_COLOR_HISTORY_MODEL (object);
128 g_object_unref (G_OBJECT(this->priv->history));
131 G_OBJECT_CLASS (gl_color_history_model_parent_class)->finalize (object);
135 /*****************************************************************************/
136 /** New Object Generator. */
137 /*****************************************************************************/
138 glColorHistoryModel *
139 gl_color_history_model_new (guint n)
141 glColorHistoryModel *this;
143 this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);
145 this->priv->max_n = n;
151 /*****************************************************************************/
152 /* Add color to history. */
153 /*****************************************************************************/
155 gl_color_history_model_add_color (glColorHistoryModel *this,
162 old = get_color_array (this, &n);
164 new = g_new0 (guint, this->priv->max_n);
168 for ( i = 0; (i < (this->priv->max_n-1)) && (i < n); i++ )
173 set_color_array (this, new, i+1);
180 /*****************************************************************************/
181 /* History changed callback. */
182 /*****************************************************************************/
184 history_changed_cb (glColorHistoryModel *this)
186 g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
190 /*****************************************************************************/
191 /* Get list of colors. */
192 /*****************************************************************************/
194 get_color_array (glColorHistoryModel *this,
198 GVariant *child_value;
202 value = g_settings_get_value (this->priv->history, "recent-colors");
203 *n_elements = g_variant_n_children (value);
205 array = g_new0 (guint, *n_elements);
207 for ( i = 0; i < *n_elements; i++ )
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);
214 g_variant_unref (value);
220 /*****************************************************************************/
221 /* Set list of colors. */
222 /*****************************************************************************/
224 set_color_array (glColorHistoryModel *this,
229 GVariant **child_values;
232 child_values = g_new (GVariant *, n_elements);
234 for ( i = 0; i < n_elements; i++ )
236 child_values[i] = g_variant_new_uint32 (array[i]);
239 value = g_variant_new_array (G_VARIANT_TYPE_UINT32, child_values, n_elements);
241 g_settings_set_value (this->priv->history, "recent-colors", value);
243 g_free (child_values);
247 /*****************************************************************************/
249 /*****************************************************************************/
251 gl_color_history_model_get_color (glColorHistoryModel *this,
258 array = get_color_array (this, &n);
259 if ( array && (i < n) )
272 * Local Variables: -- emacs
274 * c-basic-offset: 8 -- emacs
275 * tab-width: 8 -- emacs
276 * indent-tabs-mode: nil -- emacs