1 /*
2  * e-mail-label-list-store.c
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  *
17  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
18  *
19  */
20 
21 #include "evolution-config.h"
22 
23 #include "e-mail-label-list-store.h"
24 
25 #include <glib/gi18n.h>
26 #include <camel/camel.h>
27 #include <e-util/e-util.h>
28 
29 #define E_MAIL_LABEL_LIST_STORE_GET_PRIVATE(obj) \
30 	(G_TYPE_INSTANCE_GET_PRIVATE \
31 	((obj), E_TYPE_MAIL_LABEL_LIST_STORE, EMailLabelListStorePrivate))
32 
33 #define E_MAIL_LABEL_LIST_STORE_GET_PRIVATE(obj) \
34 	(G_TYPE_INSTANCE_GET_PRIVATE \
35 	((obj), E_TYPE_MAIL_LABEL_LIST_STORE, EMailLabelListStorePrivate))
36 
37 enum {
38 	CHANGED,
39 	LAST_SIGNAL
40 };
41 
42 static guint signals[LAST_SIGNAL];
43 
44 struct _EMailLabelListStorePrivate {
45 	GHashTable *tag_index; /* gchar *tag_name ~> GtkTreeIter * */
46 	GSettings *mail_settings;
47 	guint idle_changed_id;
48 };
49 
50 static struct {
51 	const gchar *label_name;
52 	const gchar *label_color;
53 	const gchar *label_tag;
54 } label_defaults[] = {
55 	{ N_("I_mportant"), "#EF2929", "$Labelimportant" },	/* red */
56 	{ N_("_Work"),      "#F57900", "$Labelwork" },		/* orange */
57 	{ N_("_Personal"),  "#4E9A06", "$Labelpersonal" },	/* green */
58 	{ N_("_To Do"),     "#3465A4", "$Labeltodo" },		/* blue */
59 	{ N_("_Later"),     "#75507B", "$Labellater" }		/* purple */
60 };
61 
62 /* Forward Declarations */
63 static void	e_mail_label_list_store_interface_init
64 						(GtkTreeModelIface *iface);
65 static void	labels_settings_changed_cb	(GSettings *settings,
66 						 const gchar *key,
67 						 gpointer user_data);
68 
G_DEFINE_TYPE_WITH_CODE(EMailLabelListStore,e_mail_label_list_store,GTK_TYPE_LIST_STORE,G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,e_mail_label_list_store_interface_init))69 G_DEFINE_TYPE_WITH_CODE (
70 	EMailLabelListStore,
71 	e_mail_label_list_store,
72 	GTK_TYPE_LIST_STORE,
73 	G_IMPLEMENT_INTERFACE (
74 		GTK_TYPE_TREE_MODEL,
75 		e_mail_label_list_store_interface_init))
76 
77 static gchar *
78 mail_label_list_store_tag_from_name (const gchar *label_name)
79 {
80 	gchar *label_tag;
81 	gchar *temp;
82 
83 	/* Thunderbird compatible */
84 	temp = g_ascii_strdown (label_name, -1);
85 	g_strdelimit (temp, " ()/{%*<>\\\"", '_');
86 	label_tag = camel_utf8_utf7 (temp);
87 	g_free (temp);
88 
89 	return label_tag;
90 }
91 
92 static gchar *
mail_label_list_store_encode_label(const gchar * label_name,const gchar * label_color,const gchar * label_tag)93 mail_label_list_store_encode_label (const gchar *label_name,
94                                       const gchar *label_color,
95                                       const gchar *label_tag)
96 {
97 	GString *string;
98 
99 	/* Encoded Form: <name> ':' <color> [ '|' <tag> ] */
100 
101 	string = g_string_new (label_name);
102 	g_string_append_printf (string, ":%s", label_color);
103 
104 	if (label_tag != NULL)
105 		g_string_append_printf (string, "|%s", label_tag);
106 
107 	return g_string_free (string, FALSE);
108 }
109 
110 static void
mail_label_list_store_fill_tag_index(EMailLabelListStore * store)111 mail_label_list_store_fill_tag_index (EMailLabelListStore *store)
112 {
113 	GtkTreeModel *model;
114 	GtkTreeIter iter;
115 
116 	g_hash_table_remove_all (store->priv->tag_index);
117 
118 	model = GTK_TREE_MODEL (store);
119 	if (!gtk_tree_model_get_iter_first (model, &iter))
120 		return;
121 
122 	do {
123 		gchar *tag;
124 
125 		tag = e_mail_label_list_store_get_tag (store, &iter);
126 		if (!tag)
127 			continue;
128 
129 		g_hash_table_insert (store->priv->tag_index, tag, gtk_tree_iter_copy (&iter));
130 	} while (gtk_tree_model_iter_next (model, &iter));
131 }
132 
133 static void
mail_label_list_store_ensure_defaults(EMailLabelListStore * store)134 mail_label_list_store_ensure_defaults (EMailLabelListStore *store)
135 {
136 	gint ii;
137 
138 	for (ii = 0; ii < G_N_ELEMENTS (label_defaults); ii++) {
139 		GtkTreeIter iter;
140 		const gchar *label_name;
141 		const gchar *label_color;
142 		const gchar *label_tag;
143 		gchar *encoded;
144 
145 		label_name = gettext (label_defaults[ii].label_name);
146 		label_color = label_defaults[ii].label_color;
147 		label_tag = label_defaults[ii].label_tag;
148 
149 		if (e_mail_label_list_store_lookup (store, label_tag, &iter))
150 			continue;
151 
152 		encoded = mail_label_list_store_encode_label (
153 			label_name, label_color, label_tag);
154 
155 		gtk_list_store_insert_with_values (
156 			GTK_LIST_STORE (store),
157 			NULL, -1, 0, encoded, -1);
158 
159 		g_free (encoded);
160 	}
161 }
162 
163 static gchar *
mail_label_list_store_get_stock_id(EMailLabelListStore * store,const gchar * color_spec)164 mail_label_list_store_get_stock_id (EMailLabelListStore *store,
165                                     const gchar *color_spec)
166 {
167 	EMailLabelListStoreClass *class;
168 	GtkIconFactory *icon_factory;
169 	GdkColor color;
170 	gchar *stock_id;
171 
172 	class = E_MAIL_LABEL_LIST_STORE_GET_CLASS (store);
173 	icon_factory = class->icon_factory;
174 
175 	if (!gdk_color_parse (color_spec, &color))
176 		return NULL;
177 
178 	stock_id = g_strdup_printf ("evolution-label-%s", color_spec);
179 
180 	/* Themes need not be taken into account here.
181 	 * It's just a solid block of a user-chosen color. */
182 	if (gtk_icon_factory_lookup (icon_factory, stock_id) == NULL) {
183 		GtkIconSet *icon_set;
184 		GdkPixbuf *pixbuf;
185 		guint32 pixel;
186 
187 		pixel = (e_color_to_value (&color) & 0xffffff) << 8;
188 
189 		pixbuf = gdk_pixbuf_new (
190 			GDK_COLORSPACE_RGB, FALSE, 8, 16, 16);
191 		gdk_pixbuf_fill (pixbuf, pixel);
192 
193 		icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
194 		gtk_icon_factory_add (icon_factory, stock_id, icon_set);
195 		gtk_icon_set_unref (icon_set);
196 
197 		g_object_unref (pixbuf);
198 	}
199 
200 	return stock_id;
201 }
202 
203 static void
mail_label_list_store_dispose(GObject * object)204 mail_label_list_store_dispose (GObject *object)
205 {
206 	EMailLabelListStorePrivate *priv;
207 
208 	priv = E_MAIL_LABEL_LIST_STORE_GET_PRIVATE (object);
209 
210 	if (priv->idle_changed_id) {
211 		g_source_remove (priv->idle_changed_id);
212 		priv->idle_changed_id = 0;
213 	}
214 
215 	g_clear_object (&priv->mail_settings);
216 
217 	/* Chain up to parent's dispose() method. */
218 	G_OBJECT_CLASS (e_mail_label_list_store_parent_class)->
219 		dispose (object);
220 }
221 
222 static void
mail_label_list_store_finalize(GObject * object)223 mail_label_list_store_finalize (GObject *object)
224 {
225 	EMailLabelListStorePrivate *priv;
226 
227 	priv = E_MAIL_LABEL_LIST_STORE_GET_PRIVATE (object);
228 
229 	g_hash_table_destroy (priv->tag_index);
230 
231 	/* Chain up to parent's finalize() method. */
232 	G_OBJECT_CLASS (e_mail_label_list_store_parent_class)->
233 		finalize (object);
234 }
235 
236 static gboolean
labels_model_changed_idle_cb(gpointer user_data)237 labels_model_changed_idle_cb (gpointer user_data)
238 {
239 	EMailLabelListStore *store = user_data;
240 	GPtrArray *array;
241 	GtkTreeIter tmp_iter;
242 	gboolean iter_set;
243 
244 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), FALSE);
245 
246 	store->priv->idle_changed_id = 0;
247 
248 	/* Make sure we don't enter an infinite synchronizing loop */
249 	g_signal_handlers_block_by_func (
250 		store->priv->mail_settings,
251 		labels_settings_changed_cb, store);
252 
253 	/* Build list to store in GSettings */
254 
255 	array = g_ptr_array_new ();
256 
257 	iter_set = gtk_tree_model_get_iter_first (
258 		GTK_TREE_MODEL (store), &tmp_iter);
259 
260 	while (iter_set) {
261 		gchar *string;
262 
263 		gtk_tree_model_get (
264 			GTK_TREE_MODEL (store), &tmp_iter,
265 			0, &string, -1);
266 		g_ptr_array_add (array, string);
267 
268 		iter_set = gtk_tree_model_iter_next (
269 			GTK_TREE_MODEL (store), &tmp_iter);
270 	}
271 
272 	g_ptr_array_add (array, NULL);
273 
274 	g_settings_set_strv (
275 		store->priv->mail_settings, "labels",
276 		(const gchar * const *) array->pdata);
277 
278 	g_ptr_array_foreach (array, (GFunc) g_free, NULL);
279 	g_ptr_array_free (array, TRUE);
280 
281 	g_signal_handlers_unblock_by_func (
282 		store->priv->mail_settings,
283 		labels_settings_changed_cb, store);
284 
285 	mail_label_list_store_fill_tag_index (store);
286 
287 	g_signal_emit (store, signals[CHANGED], 0);
288 
289 	return FALSE;
290 }
291 
292 static void
labels_model_changed_cb(EMailLabelListStore * store)293 labels_model_changed_cb (EMailLabelListStore *store)
294 {
295 	g_return_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store));
296 
297 	mail_label_list_store_fill_tag_index (store);
298 
299 	/* do the actual save and signal emission on idle,
300 	 * to accumulate as many changes as possible */
301 	if (!store->priv->idle_changed_id)
302 		store->priv->idle_changed_id = g_idle_add (labels_model_changed_idle_cb, store);
303 }
304 
305 static void
labels_settings_changed_cb(GSettings * settings,const gchar * key,gpointer user_data)306 labels_settings_changed_cb (GSettings *settings,
307                             const gchar *key,
308                             gpointer user_data)
309 {
310 	EMailLabelListStore *store;
311 	GtkTreeModel *model;
312 	GtkTreeIter iter;
313 	GHashTable *changed_labels;
314 	gchar **strv;
315 	gint i;
316 
317 	store = E_MAIL_LABEL_LIST_STORE (user_data);
318 	model = GTK_TREE_MODEL (store);
319 
320 	strv = g_settings_get_strv (store->priv->mail_settings, "labels");
321 
322 	/* Check if any label changed first, because GSettings can claim
323 	 * change when nothing changed at all */
324 	changed_labels = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
325 	if (gtk_tree_model_get_iter_first (model, &iter)) {
326 		do {
327 			gchar *label_str = NULL;
328 
329 			gtk_tree_model_get (model, &iter, 0, &label_str, -1);
330 
331 			if (label_str)
332 				g_hash_table_insert (changed_labels, label_str, NULL);
333 		} while (gtk_tree_model_iter_next (model, &iter));
334 
335 		for (i = 0; strv[i] != NULL; i++) {
336 			if (!g_hash_table_remove (changed_labels, strv[i])) {
337 				g_hash_table_insert (changed_labels, g_strdup (""), NULL);
338 				break;
339 			}
340 		}
341 	} else {
342 		/* nothing in the store, thus fill it (pretend change) */
343 		g_hash_table_insert (changed_labels, g_strdup (""), NULL);
344 	}
345 
346 	/* Nothing changed */
347 	if (g_hash_table_size (changed_labels) == 0) {
348 		g_hash_table_destroy (changed_labels);
349 		g_strfreev (strv);
350 
351 		mail_label_list_store_fill_tag_index (store);
352 		return;
353 	}
354 
355 	g_hash_table_destroy (changed_labels);
356 
357 	/* Make sure we don't enter an infinite synchronizing loop */
358 	g_signal_handlers_block_by_func (
359 		store, labels_model_changed_cb, store);
360 
361 	gtk_list_store_clear (GTK_LIST_STORE (store));
362 
363 	for (i = 0; strv[i] != NULL; i++) {
364 		GtkTreeIter iter;
365 
366 		gtk_list_store_insert_with_values (
367 			GTK_LIST_STORE (store), &iter, -1, 0, strv[i], -1);
368 	}
369 
370 	g_strfreev (strv);
371 
372 	g_signal_handlers_unblock_by_func (
373 		store, labels_model_changed_cb, store);
374 
375 	mail_label_list_store_fill_tag_index (store);
376 }
377 
378 static void
mail_label_list_store_constructed(GObject * object)379 mail_label_list_store_constructed (GObject *object)
380 {
381 	EMailLabelListStore *store;
382 
383 	store = E_MAIL_LABEL_LIST_STORE (object);
384 
385 	/* Connect to GSettings' change notifications */
386 	store->priv->mail_settings = e_util_ref_settings ("org.gnome.evolution.mail");
387 	g_signal_connect (
388 		store->priv->mail_settings, "changed::labels",
389 		G_CALLBACK (labels_settings_changed_cb), store);
390 	labels_settings_changed_cb (
391 		store->priv->mail_settings, "labels", store);
392 
393 	/* Connect to ListStore change notifications */
394 	g_signal_connect_swapped (
395 		store, "row-inserted",
396 		G_CALLBACK (labels_model_changed_cb), store);
397 	g_signal_connect_swapped (
398 		store, "row-changed",
399 		G_CALLBACK (labels_model_changed_cb), store);
400 	g_signal_connect_swapped (
401 		store, "row-deleted",
402 		G_CALLBACK (labels_model_changed_cb), store);
403 	g_signal_connect_swapped (
404 		store, "rows-reordered",
405 		G_CALLBACK (labels_model_changed_cb), store);
406 
407 	mail_label_list_store_ensure_defaults (store);
408 
409 	/* Chain up to parent's constructed() method. */
410 	G_OBJECT_CLASS (e_mail_label_list_store_parent_class)->constructed (object);
411 }
412 
413 static void
e_mail_label_list_store_class_init(EMailLabelListStoreClass * class)414 e_mail_label_list_store_class_init (EMailLabelListStoreClass *class)
415 {
416 	GObjectClass *object_class;
417 
418 	g_type_class_add_private (class, sizeof (EMailLabelListStorePrivate));
419 
420 	object_class = G_OBJECT_CLASS (class);
421 	object_class->dispose = mail_label_list_store_dispose;
422 	object_class->finalize = mail_label_list_store_finalize;
423 	object_class->constructed = mail_label_list_store_constructed;
424 
425 	class->icon_factory = gtk_icon_factory_new ();
426 	gtk_icon_factory_add_default (class->icon_factory);
427 
428 	signals[CHANGED] = g_signal_new (
429 		"changed",
430 		G_OBJECT_CLASS_TYPE (class),
431 		G_SIGNAL_RUN_FIRST,
432 		0, NULL, NULL,
433 		g_cclosure_marshal_VOID__VOID,
434 		G_TYPE_NONE, 0);
435 }
436 
437 static void
e_mail_label_list_store_interface_init(GtkTreeModelIface * iface)438 e_mail_label_list_store_interface_init (GtkTreeModelIface *iface)
439 {
440 }
441 
442 static void
e_mail_label_list_store_init(EMailLabelListStore * store)443 e_mail_label_list_store_init (EMailLabelListStore *store)
444 {
445 	GHashTable *tag_index;
446 	GType type = G_TYPE_STRING;
447 
448 	tag_index = g_hash_table_new_full (
449 		g_str_hash, g_str_equal,
450 		(GDestroyNotify) g_free,
451 		(GDestroyNotify) gtk_tree_iter_free);
452 
453 	store->priv = E_MAIL_LABEL_LIST_STORE_GET_PRIVATE (store);
454 	store->priv->tag_index = tag_index;
455 
456 	/* XXX While it may seem awkward to cram the label name and color
457 	 *     into a single string column, we do it for the benefit of
458 	 *     letting GSettings keep the model in sync.
459 	 *
460 	 * XXX There's a valid argument to be made that this information
461 	 *     doesn't belong in GSettings in the first place.  A key file
462 	 *     under $(user_data_dir)/mail would work better. */
463 	gtk_list_store_set_column_types (GTK_LIST_STORE (store), 1, &type);
464 }
465 
466 EMailLabelListStore *
e_mail_label_list_store_new(void)467 e_mail_label_list_store_new (void)
468 {
469 	return g_object_new (E_TYPE_MAIL_LABEL_LIST_STORE, NULL);
470 }
471 
472 gchar *
e_mail_label_list_store_get_name(EMailLabelListStore * store,GtkTreeIter * iter)473 e_mail_label_list_store_get_name (EMailLabelListStore *store,
474                                   GtkTreeIter *iter)
475 {
476 	gchar *encoded;
477 	gchar *result;
478 	gchar **strv;
479 
480 	/* Encoded Form: <name> ':' <color> [ '|' <tag> ] */
481 
482 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
483 	g_return_val_if_fail (iter != NULL, NULL);
484 
485 	gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);
486 
487 	strv = g_strsplit_set (encoded, ":|", 3);
488 
489 	if (g_strv_length (strv) >= 2)
490 		result = g_strdup (gettext (strv[0]));
491 	else
492 		result = NULL;
493 
494 	g_strfreev (strv);
495 	g_free (encoded);
496 
497 	return result;
498 }
499 
500 gboolean
e_mail_label_list_store_get_color(EMailLabelListStore * store,GtkTreeIter * iter,GdkColor * color)501 e_mail_label_list_store_get_color (EMailLabelListStore *store,
502                                    GtkTreeIter *iter,
503                                    GdkColor *color)
504 {
505 	gchar *encoded;
506 	gchar **strv;
507 	gboolean valid;
508 
509 	/* Encoded Form: <name> ':' <color> [ '|' <tag> ] */
510 
511 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), FALSE);
512 	g_return_val_if_fail (iter != NULL, FALSE);
513 	g_return_val_if_fail (color != NULL, FALSE);
514 
515 	gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);
516 
517 	strv = g_strsplit_set (encoded, ":|", 3);
518 
519 	if (g_strv_length (strv) >= 2)
520 		valid = gdk_color_parse (strv[1], color);
521 	else
522 		valid = FALSE;
523 
524 	g_strfreev (strv);
525 	g_free (encoded);
526 
527 	return valid;
528 }
529 
530 gchar *
e_mail_label_list_store_get_stock_id(EMailLabelListStore * store,GtkTreeIter * iter)531 e_mail_label_list_store_get_stock_id (EMailLabelListStore *store,
532                                       GtkTreeIter *iter)
533 {
534 	gchar *encoded;
535 	gchar *result;
536 	gchar **strv;
537 
538 	/* Encoded Form: <name> ':' <color> [ '|' <tag> ] */
539 
540 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
541 	g_return_val_if_fail (iter != NULL, NULL);
542 	gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);
543 
544 	strv = g_strsplit_set (encoded, ":|", 3);
545 
546 	if (g_strv_length (strv) >= 2)
547 		result = mail_label_list_store_get_stock_id (store, strv[1]);
548 	else
549 		result = NULL;
550 
551 	g_strfreev (strv);
552 	g_free (encoded);
553 
554 	return result;
555 }
556 
557 gchar *
e_mail_label_list_store_get_tag(EMailLabelListStore * store,GtkTreeIter * iter)558 e_mail_label_list_store_get_tag (EMailLabelListStore *store,
559                                  GtkTreeIter *iter)
560 {
561 	gchar *encoded;
562 	gchar *result;
563 	gchar **strv;
564 
565 	/* Encoded Form: <name> ':' <color> [ '|' <tag> ] */
566 
567 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), NULL);
568 	g_return_val_if_fail (iter != NULL, NULL);
569 
570 	gtk_tree_model_get (GTK_TREE_MODEL (store), iter, 0, &encoded, -1);
571 
572 	strv = g_strsplit_set (encoded, ":|", 3);
573 
574 	/* XXX I guess for historical reasons the default label tags have
575 	 *     a "$Label" prefix, but the default list in GSettings doesn't
576 	 *     include tags.  That's why the <tag> part is optional.
577 	 *     So if we're missing the <tag> part, look it up in the
578 	 *     hard-coded default list above.
579 	 *
580 	 *     Not sure I got my facts straight here.  Double check. */
581 	if (g_strv_length (strv) >= 3)
582 		result = g_strdup (strv[2]);
583 	else {
584 		gint ii;
585 
586 		result = NULL;
587 
588 		for (ii = 0; ii < G_N_ELEMENTS (label_defaults); ii++) {
589 			const gchar *label_name;
590 			const gchar *label_tag;
591 
592 			label_name = label_defaults[ii].label_name;
593 			label_tag = label_defaults[ii].label_tag;
594 
595 			if (strcmp (strv[0], label_name) == 0) {
596 				result = g_strdup (label_tag);
597 				break;
598 			}
599 		}
600 	}
601 
602 	/* XXX Still no luck?  The label list in GSettings must be screwed up.
603 	 *     We must not return NULL because the tag is used as a key in
604 	 *     the index hash table, so generate a tag from the name. */
605 	if (result == NULL)
606 		result = mail_label_list_store_tag_from_name (strv[0]);
607 
608 	g_strfreev (strv);
609 	g_free (encoded);
610 
611 	return result;
612 }
613 
614 void
e_mail_label_list_store_set(EMailLabelListStore * store,GtkTreeIter * iter,const gchar * name,const GdkColor * color)615 e_mail_label_list_store_set (EMailLabelListStore *store,
616                              GtkTreeIter *iter,
617                              const gchar *name,
618                              const GdkColor *color)
619 {
620 	e_mail_label_list_store_set_with_tag (store, iter, NULL, name, color);
621 }
622 
623 /* The 'tag' is ignored, if the 'iter' is set. */
624 void
e_mail_label_list_store_set_with_tag(EMailLabelListStore * store,GtkTreeIter * iter,const gchar * tag,const gchar * name,const GdkColor * color)625 e_mail_label_list_store_set_with_tag (EMailLabelListStore *store,
626 				      GtkTreeIter *iter,
627 				      const gchar *tag,
628 				      const gchar *name,
629 				      const GdkColor *color)
630 {
631 	gchar *encoded;
632 	gchar *label_color;
633 	gchar *label_tag = NULL;
634 
635 	g_return_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store));
636 	g_return_if_fail (name != NULL);
637 	g_return_if_fail (color != NULL);
638 
639 	label_color = gdk_color_to_string (color);
640 
641 	if (iter != NULL)
642 		label_tag = e_mail_label_list_store_get_tag (store, iter);
643 	else if (tag && *tag)
644 		label_tag = g_strdup (tag);
645 	if (label_tag == NULL)
646 		label_tag = mail_label_list_store_tag_from_name (name);
647 
648 	encoded = mail_label_list_store_encode_label (
649 		name, label_color, label_tag);
650 
651 	/* We use gtk_list_store_insert_with_values() so the data is
652 	 * in place when the 'row-inserted' signal is emitted and our
653 	 * row_inserted() method executes. */
654 	if (iter != NULL)
655 		gtk_list_store_set (
656 			GTK_LIST_STORE (store), iter, 0, encoded, -1);
657 	else
658 		gtk_list_store_insert_with_values (
659 			GTK_LIST_STORE (store), NULL, -1, 0, encoded, -1);
660 
661 	g_free (label_color);
662 	g_free (label_tag);
663 	g_free (encoded);
664 }
665 
666 gboolean
e_mail_label_list_store_lookup(EMailLabelListStore * store,const gchar * tag,GtkTreeIter * iter)667 e_mail_label_list_store_lookup (EMailLabelListStore *store,
668                                 const gchar *tag,
669                                 GtkTreeIter *iter)
670 {
671 	GtkTreeIter *stored_iter;
672 
673 	g_return_val_if_fail (E_IS_MAIL_LABEL_LIST_STORE (store), FALSE);
674 	g_return_val_if_fail (tag != NULL, FALSE);
675 	g_return_val_if_fail (iter != NULL, FALSE);
676 
677 	stored_iter = g_hash_table_lookup (store->priv->tag_index, tag);
678 
679 	if (!stored_iter)
680 		return FALSE;
681 
682 	*iter = *stored_iter;
683 
684 	return TRUE;
685 }
686 
687 gboolean
e_mail_label_tag_is_default(const gchar * tag)688 e_mail_label_tag_is_default (const gchar *tag)
689 {
690 	g_return_val_if_fail (tag != NULL, FALSE);
691 
692 	return g_str_has_prefix (tag, "$Label");
693 }
694