summaryrefslogtreecommitdiff
path: root/src/color-history-model.c
blob: 78bc6a058e21787a15d554c3f90850e5dd184b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *  color-history-model.c
 *  Copyright (C) 2008-2009  Jim Evins <evins@snaught.com>.
 *
 *  This file is part of gLabels.
 *
 *  gLabels is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  gLabels is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "color-history-model.h"

#include <gio/gio.h>

#include "marshal.h"


/*========================================================*/
/* Private types.                                         */
/*========================================================*/

struct _glColorHistoryModelPrivate {

        GSettings   *history;

        guint        max_n;
};

enum {
        CHANGED,
        LAST_SIGNAL
};


/*========================================================*/
/* Private globals.                                       */
/*========================================================*/

static guint signals[LAST_SIGNAL] = {0};


/*========================================================*/
/* Private function prototypes.                           */
/*========================================================*/

static void    gl_color_history_model_finalize  (GObject             *object);

static void    history_changed_cb               (glColorHistoryModel *this);

static guint  *get_color_array                  (glColorHistoryModel *this,
                                                 guint               *n_elements);
static void    set_color_array                  (glColorHistoryModel *this,
                                                 guint               *array,
                                                 guint                n_elements);


/*****************************************************************************/
/* Object infrastructure.                                                    */
/*****************************************************************************/
G_DEFINE_TYPE (glColorHistoryModel, gl_color_history_model, G_TYPE_OBJECT);


/*****************************************************************************/
/* Class Init Function.                                                      */
/*****************************************************************************/
static void
gl_color_history_model_class_init (glColorHistoryModelClass *class)
{
        GObjectClass  *gobject_class = (GObjectClass *) class;

        gl_color_history_model_parent_class = g_type_class_peek_parent (class);

        gobject_class->finalize = gl_color_history_model_finalize;

        signals[CHANGED] =
                g_signal_new ("changed",
                              G_OBJECT_CLASS_TYPE (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (glColorHistoryModelClass, changed),
                              NULL, NULL,
                              gl_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);
}


/*****************************************************************************/
/* Object Instance Init Function.                                            */
/*****************************************************************************/
static void
gl_color_history_model_init (glColorHistoryModel *this)
{
        this->priv = g_new0 (glColorHistoryModelPrivate, 1);

        this->priv->history = g_settings_new ("org.gnome.glabels-3.history");

        g_return_if_fail (this->priv->history != NULL);

        g_signal_connect_swapped (G_OBJECT (this->priv->history),
                                  "changed::recent-colors",
                                  G_CALLBACK (history_changed_cb), this);
}


/*****************************************************************************/
/* Finalize Method.                                                          */
/*****************************************************************************/
static void
gl_color_history_model_finalize (GObject *object)
{
        glColorHistoryModel   *this;

        g_return_if_fail (object && IS_GL_COLOR_HISTORY_MODEL (object));
        this = GL_COLOR_HISTORY_MODEL (object);

        g_object_unref (G_OBJECT(this->priv->history));
        g_free (this->priv);

        G_OBJECT_CLASS (gl_color_history_model_parent_class)->finalize (object);
}


/*****************************************************************************/
/** New Object Generator.                                                    */
/*****************************************************************************/
glColorHistoryModel *
gl_color_history_model_new (guint n)
{
        glColorHistoryModel *this;

        this = g_object_new (TYPE_GL_COLOR_HISTORY_MODEL, NULL);

        this->priv->max_n = n;

        return this;
}


/*****************************************************************************/
/* Add color to history.                                                      */
/*****************************************************************************/
void
gl_color_history_model_add_color (glColorHistoryModel *this,
                                  guint                color)
{
        guint  *old;
        guint  *new;
        guint   i, n;

        old = get_color_array (this, &n);
                                   
        new = g_new0 (guint, this->priv->max_n);

        new[0] = color;

        for ( i = 0; (i < this->priv->max_n) && (i < n); i++ )
        {
                new[i+1] = old[i];
        }

        set_color_array (this, new, i);

        g_free (old);
        g_free (new);
}


/*****************************************************************************/
/* History changed callback.                                                 */
/*****************************************************************************/
static void
history_changed_cb (glColorHistoryModel  *this)
{
        g_signal_emit (G_OBJECT(this), signals[CHANGED], 0);
}


/*****************************************************************************/
/* Get list of colors.                                                       */
/*****************************************************************************/
static guint *
get_color_array (glColorHistoryModel *this,
                 guint               *n_elements)
{
        GVariant *value;
        GVariant *child_value;
        gsize     i;
        guint    *array;

        value = g_settings_get_value (this->priv->history, "recent-colors");
        *n_elements = g_variant_n_children (value);

        array = g_new0 (guint, *n_elements);

        for ( i = 0; i < *n_elements; i++ )
        {
                child_value = g_variant_get_child_value (value, i);
                array[i] = g_variant_get_uint32 (child_value);
                g_variant_unref (child_value);
        }

        g_variant_unref (value);

        return array;
}


/*****************************************************************************/
/* Set list of colors.                                                       */
/*****************************************************************************/
static void
set_color_array (glColorHistoryModel *this,
                 guint               *array,
                 guint                n_elements)
{
        GVariant  *value;
        GVariant **child_values;
        gsize      i;

        child_values = g_new (GVariant *, n_elements);

        for ( i = 0; i < n_elements; i++ )
        {
                child_values[i] = g_variant_ref_sink (g_variant_new_uint32 (array[i]));
        }

        g_variant_new_array (G_VARIANT_TYPE ("u"), child_values, n_elements);

        g_settings_set_value (this->priv->history, "recent-colors", value);

        g_variant_unref (value);
}


/*****************************************************************************/
/* Get color.                                                                */
/*****************************************************************************/
guint
gl_color_history_model_get_color (glColorHistoryModel *this,
                                  guint                i)
{
        guint  *array;
        guint   color = 0;
        guint   n;

        array = get_color_array (this, &n);
        if ( array && (i < n) )
        {
                color = array[i];
        }
        g_free (array);

        return color;
}




/*
 * Local Variables:       -- emacs
 * mode: C                -- emacs
 * c-basic-offset: 8      -- emacs
 * tab-width: 8           -- emacs
 * indent-tabs-mode: nil  -- emacs
 * End:                   -- emacs
 */