aboutsummaryrefslogtreecommitdiffstats
path: root/mail/e-mail-label-list-store.c
blob: f604893bb3a6241634e8f79999bd6802955fe176 (plain) (blame)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * e-mail-label-list-store.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-mail-label-list-store.h"

#include <glib/gi18n.h>
#include <camel/camel.h>
#include "e-util/gconf-bridge.h"

struct _EMailLabelListStorePrivate {
    GHashTable *tag_index;
};

static struct {
    const gchar *label_name;
    const gchar *label_color;
    const gchar *label_tag;
} label_defaults[] = {
    { N_("I_mportant"), "#EF2929", "$Labelimportant" }, /* red */
    { N_("_Work"),      "#F57900", "$Labelwork" },      /* orange */
    { N_("_Personal"),  "#4E9A06", "$Labelpersonal" },  /* green */
    { N_("_To Do"),     "#3465A4", "$Labeltodo" },      /* blue */
    { N_("_Later"),     "#75507B", "$Labellater" }      /* purple */
};

static gpointer parent_class;

static gchar *
mail_label_list_store_tag_from_name (const gchar *label_name)
{
    gchar *label_tag;
    gchar *temp;

    /* Thunderbird compatible */
    temp = g_ascii_strdown (label_name, -1);
    g_strdelimit (temp, " ()/{%*<>\\\"", '_');
    label_tag = camel_utf8_utf7 (temp);
    g_free (temp);

    return label_tag;
}

static gchar *
mail_label_list_store_encode_label (const gchar *label_name,
                                      const gchar *label_color,
                                      const gchar *label_tag)
{
    GString *string;

    /* Encoded Form: <name> ':' <color> [ '|' <tag> ] */

    string = g_string_new (label_name);
    g_string_append_printf (string, ":%s", label_color);

    if (label_tag != NULL)
        g_string_append_printf (string, "|%s", label_tag);

    return g_string_free (string, FALSE);
}

static void
mail_label_list_store_ensure_defaults (EMailLabelListStore *store)
{
    gint ii;

    for (ii = 0; ii < G_N_ELEMENTS (label_defaults); ii++) {
        GtkTreeIter iter;
        const gchar *label_name;
        const gchar *label_color;
        const gchar *label_tag;
        gchar *encoded;

        label_name = gettext (label_defaults[ii].label_name);
        label_color = label_defaults[ii].label_color;
        label_tag = label_defaults[ii].label_tag;

        if (e_mail_label_list_store_lookup (store, label_tag, &iter))
            continue;

        encoded = mail_label_list_store_encode_label (
            label_name, label_color, label_tag);

        gtk_list_store_insert_with_values (
            GTK_LIST_STORE (store),
            NULL, -1, 0, encoded, -1);

        g_free (encoded);
    }
}

static gchar *
mail_label_list_store_get_stock_id (EMailLabelListStore *store,
                                    const gchar *color_spec)
{
    EMailLabelListStoreClass *class;
    GtkIconFactory *icon_factory;
    GdkColor color;
    gchar *stock_id;

    class = E_MAIL_LABEL_LIST_STORE_GET_CLASS (store);
    icon_factory = class->icon_factory;

    if (!gdk_color_parse (color_spec, &color))
        return NULL;

    stock_id = g_strdup_printf ("evolution-label-%s", color_spec);

    /* Themes need not be taken into account here.
     * It's just a solid block of a user-chosen color. */
    if (gtk_icon_factory_lookup (icon_factory, stock_id) == NULL) {
        GtkIconSet *icon_set;
        GdkPixbuf *pixbuf;
        guint32 pixel;

        pixel = ((color.red & 0xFF00) << 16) +
            ((color.green & 0xFF00) << 8) +
            (color.blue & 0xFF00);

        pixbuf = gdk_pixbuf_new (
            GDK_COLORSPACE_RGB, FALSE, 8, 16, 16);
        gdk_pixbuf_fill (pixbuf, pixel);

        icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
        gtk_icon_factory_add (icon_factory, stock_id, icon_set);
        gtk_icon_set_unref (icon_set);

        g_object_unref (pixbuf);
    }

    return stock_id;
}

static void
mail_label_list_store_finalize (GObject *object)
{
    EMailLabelListStorePrivate *priv;

    priv = E_MAIL_LABEL_LIST_STORE (object)->priv;

    g_hash_table_destroy (priv->tag_index);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
mail_label_list_store_constructed (GObject *object)
{
    EMailLabelListStore *store;
    GConfBridge *bridge;
    const gchar *key;

    store = E_MAIL_LABEL_LIST_STORE (object);

    bridge = gconf_bridge_get ();
    key = "/apps/evolution/mail/labels";
    gconf_bridge_bind_string_list_store (
        bridge, key, GTK_LIST_STORE (store));

    mail_label_list_store_ensure_defaults (store);

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (parent_class)->constructed (object);
}

static void
mail_label_list_store_row_inserted (GtkTreeModel *model,
                                    GtkTreePath *path,
                                    GtkTreeIter *iter)
{
    EMailLabelListStore *store;
    GtkTreeRowReference *reference;
    GHashTable *tag_index;
    gchar *tag;

    store = E_MAIL_LABEL_LIST_STORE (model);
    tag = e_mail_label_list_store_get_tag (store, iter);
    g_return_if_fail (tag != NULL);

    /* Hash table takes ownership of both tag and reference. */
    tag_index = store->priv->tag_index;
    reference = gtk_tree_row_reference_new (model, path);
    g_hash_table_insert (tag_index, tag, reference);

    /* We don't need to do anything special for row deletion.
     * The reference will automatically become invalid (that's
     * why we're storing references and not iterators or paths),
     * so garbage collection is not important.  We'll do it
     * lazily. */
}

static void
mail_label_list_store_class_init (EMailLabelListStoreClass *class)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EMailLabelListStorePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = mail_label_list_store_finalize;
    object_class->constructed = mail_label_list_store_constructed;

    class->icon_factory = gtk_icon_factory_new ();
    gtk_icon_factory_add_default (class->icon_factory);
}

static void
mail_label_list_store_init (EMailLabelListStore *store)
{
    GHashTable *tag_index;
    GType type = G_TYPE_STRING;

    tag_index = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) gtk_tree_row_reference_free);

    store->priv = G_TYPE_INSTANCE_GET_PRIVATE (
        store, E_TYPE_MAIL_LABEL_LIST_STORE,
        EMailLabelListStorePrivate);
    store->priv->tag_index = tag_index;

    /* XXX While it may seem awkward to cram the label name and color
     *     into a single string column, we do it for the benefit of
     *     letting GConfBridge keep the model in sync with GConf.
     *
     * XXX There's a valid argument to be made that this information
     *     doesn't belong in GConf in the first place.  A key file
     *     under $(user_data_dir)/mail would work better. */
    gtk_list_store_set_column_types (GTK_LIST_STORE (store), 1, &type);
}

static void
mail_label_list_store_iface_init (GtkTreeModelIface *iface)
{
    iface->row_inserted = mail_label_list_store_row_inserted;
}

GType
e_mail_label_list_store_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EMailLabelListStoreClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) mail_label_list_store_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EMailLabelListStore),
            0,     /* n_preallocs */
            (GInstanceInitFunc) mail_label_list_store_init,
            NULL   /* vaule_table */
        };

        static const GInterfaceInfo iface_info = {
            (GInterfaceInitFunc) mail_label_list_store_iface_init,
            (GInterfaceFinalizeFunc) NULL,
            NULL   /* interface_data */
        };

        type = g_type_register_static (
            GTK_TYPE_LIST_STORE, "EMailLabelListStore",
            &type_info, 0);

        g_type_add_interface_static (
            type, GTK_TYPE_TREE_MODEL, &iface_info);
    }

    return type;
}

EMailLabelListStore *
e_mail_label_list_store_new (void)
{
    return g_object_new (E_TYPE_MAIL_LABEL_LIST_STORE, NULL);
}

gchar *
e_mail_label_list_store_get_name (EMailLabelListStore *store,
                                  GtkTreeIter *iter)
{
    gchar *encoded;
    gchar *result;
    gchar **strv;

    /* Encoded Form: <name> ':' <color> [ '|' <tag> ] */

    g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
    g_return_val_if_fail (iter != NULL, NULL);

    gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);

    strv = g_strsplit_set (encoded, ":|", 3);

    if (g_strv_length (strv) >= 2)
        result = g_strdup (gettext (strv[0]));
    else
        result = NULL;

    g_strfreev (strv);
    g_free (encoded);

    return result;
}

gboolean
e_mail_label_list_store_get_color (EMailLabelListStore *store,
                                   GtkTreeIter *iter,
                                   GdkColor *color)
{
    gchar *encoded;
    gchar **strv;
    gboolean valid;

    /* Encoded Form: <name> ':' <color> [ '|' <tag> ] */

    g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), FALSE);
    g_return_val_if_fail (iter != NULL, FALSE);
    g_return_val_if_fail (color != NULL, FALSE);

    gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);

    strv = g_strsplit_set (encoded, ":|", 3);

    if (g_strv_length (strv) >= 2)
        valid = gdk_color_parse (strv[1], color);
    else
        valid = FALSE;

    g_strfreev (strv);
    g_free (encoded);

    return valid;
}

gchar *
e_mail_label_list_store_get_stock_id (EMailLabelListStore *store,
                                      GtkTreeIter *iter)
{
    gchar *encoded;
    gchar *result;
    gchar **strv;

    /* Encoded Form: <name> ':' <color> [ '|' <tag> ] */

    g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
    g_return_val_if_fail (iter != NULL, NULL);
    gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);

    strv = g_strsplit_set (encoded, ":|", 3);

    if (g_strv_length (strv) >= 2)
        result = mail_label_list_store_get_stock_id (store, strv[1]);
    else
        result = NULL;

    g_strfreev (strv);
    g_free (encoded);

    return result;
}

gchar *
e_mail_label_list_store_get_tag (EMailLabelListStore *store,
                                 GtkTreeIter *iter)
{
    gchar *encoded;
    gchar *result;
    gchar **strv;

    /* Encoded Form: <name> ':' <color> [ '|' <tag> ] */

    g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
    g_return_val_if_fail (iter != NULL, NULL);

    gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);

    strv = g_strsplit_set (encoded, ":|", 3);

    /* XXX I guess for historical reasons the default label tags have
     *     a "$Label" prefix, but the default list in GConf doesn't
     *     include tags.  That's why the <tag> part is optional.
     *     So if we're missing the <tag> part, look it up in the
     *     hard-coded default list above.
     *
     *     Not sure I got my facts straight here.  Double check. */
    if (g_strv_length (strv) >= 3)
        result = g_strdup (strv[2]);
    else {
        gint ii;

        result = NULL;

        for (ii = 0; ii < G_N_ELEMENTS (label_defaults); ii++) {
            const gchar *label_name;
            const gchar *label_tag;

            label_name = label_defaults[ii].label_name;
            label_tag = label_defaults[ii].label_tag;

            if (strcmp (strv[0], label_name) == 0) {
                result = g_strdup (label_tag);
                break;
            }
        }
    }

    /* XXX Still no luck?  The label list in GConf must be screwed up.
     *     We must not return NULL because the tag is used as a key in
     *     the index hash table, so generate a tag from the name. */
    if (result == NULL)
        result = mail_label_list_store_tag_from_name (strv[0]);

    g_strfreev (strv);
    g_free (encoded);

    return result;
}

void
e_mail_label_list_store_set (EMailLabelListStore *store,
                             GtkTreeIter *iter,
                             const gchar *name,
                             const GdkColor *color)
{
    gchar *encoded;
    gchar *label_color;
    gchar *label_tag = NULL;

    g_return_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store));
    g_return_if_fail (name != NULL);
    g_return_if_fail (color != NULL);

    label_color = gdk_color_to_string (color);

    if (iter != NULL)
        label_tag = e_mail_label_list_store_get_tag (store, iter);
    if (label_tag == NULL)
        label_tag = mail_label_list_store_tag_from_name (name);

    encoded = mail_label_list_store_encode_label (
        name, label_color, label_tag);

    /* We use gtk_list_store_insert_with_values() so the data is
     * in place when the 'row-inserted' signal is emitted and our
     * row_inserted() method executes. */
    if (iter != NULL)
        gtk_list_store_set (
            GTK_LIST_STORE (store), iter, 0, encoded, -1);
    else
        gtk_list_store_insert_with_values (
            GTK_LIST_STORE (store), NULL, -1, 0, encoded, -1);

    g_free (label_color);
    g_free (label_tag);
    g_free (encoded);
}

gboolean
e_mail_label_list_store_lookup (EMailLabelListStore *store,
                                const gchar *tag,
                                GtkTreeIter *iter)
{
    GtkTreeRowReference *reference;
    GHashTable *tag_index;
    GtkTreeModel *model;
    GtkTreePath *path;

    g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), FALSE);
    g_return_val_if_fail (tag != NULL, FALSE);
    g_return_val_if_fail (iter != NULL, FALSE);

    tag_index = store->priv->tag_index;
    reference = g_hash_table_lookup (tag_index, tag);

    if (reference == NULL)
        return FALSE;

    if (!gtk_tree_row_reference_valid (reference)) {
        /* Garbage collect the dead reference. */
        g_hash_table_remove (tag_index, tag);
        return FALSE;
    }

    model = gtk_tree_row_reference_get_model (reference);
    path = gtk_tree_row_reference_get_path (reference);
    gtk_tree_model_get_iter (model, iter, path);
    gtk_tree_path_free (path);

    return TRUE;
}

gboolean
e_mail_label_tag_is_default (const gchar *tag)
{
    g_return_val_if_fail (tag != NULL, FALSE);

    return g_str_has_prefix (tag, "$Label");
}