5be74ef286ea295fe193af9f3ad3afd025ede7b1
[claws.git] / src / edittags.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "menu.h"
36 #include "edittags.h"
37 #include "prefs_gtk.h"
38 #include "utils.h"
39 #include "gtkutils.h"
40 #include "inputdialog.h"
41 #include "manage_window.h"
42 #include "mainwindow.h"
43 #include "prefs_common.h"
44 #include "alertpanel.h"
45 #include "summaryview.h"
46 #include "tags.h"
47 #include "gtkutils.h"
48 #include "manual.h"
49
50 enum {
51         TAG_SELECTED,
52         TAG_SELECTED_INCONSISTENT,
53         TAG_NAME,
54         TAG_DATA,
55         N_TAG_EDIT_COLUMNS
56 };
57
58 static gint tag_cmp_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
59  {
60         gchar *name1, *name2;
61
62         gtk_tree_model_get(model, a, TAG_NAME, &name1, -1);
63         gtk_tree_model_get(model, b, TAG_NAME, &name2, -1);
64         
65         if (name1 == NULL)
66                 return name2 == NULL ? 0:1;
67         
68         if (name2 == NULL)
69                 return name1 == NULL ? 0:1;
70         
71         return g_utf8_collate(name1,name2);
72 }
73
74 static void apply_window_create(void);
75
76 static struct TagApplyWindow
77 {
78         GtkWidget *window;
79         GtkWidget *list_view;
80         GtkWidget *hbox1;
81         GtkWidget *vbox1;
82         GtkWidget *label;
83         GtkWidget *taglist;
84         GtkWidget *close_btn;
85         GtkWidget *add_entry;
86         GtkWidget *add_btn;
87         GSList *msglist;
88         gboolean has_tag_col;
89 } applywindow;
90
91 static void apply_window_load_tags (void);
92 static void apply_window_insert_check_column(GtkWidget *list_view);
93
94 void tag_apply_open(GSList *msglist)
95 {
96         if (!applywindow.window)
97                 apply_window_create();
98
99         manage_window_set_transient(GTK_WINDOW(applywindow.window));
100         gtk_widget_grab_focus(applywindow.close_btn);
101         
102         applywindow.msglist = msglist;
103         apply_window_load_tags();
104
105         if (msglist && !applywindow.has_tag_col) {
106                 apply_window_insert_check_column(applywindow.list_view);
107                 applywindow.has_tag_col = TRUE;
108         }
109         if (!msglist && applywindow.has_tag_col) {
110                 gtk_tree_view_remove_column(GTK_TREE_VIEW(applywindow.list_view),
111                         gtk_tree_view_get_column(GTK_TREE_VIEW(applywindow.list_view), 0));
112                 applywindow.has_tag_col = FALSE;
113         } 
114
115         gtk_widget_show(applywindow.window);
116         gtk_widget_grab_focus(applywindow.taglist);
117         gtk_window_set_modal(GTK_WINDOW(applywindow.window), TRUE);
118 }
119
120 static GtkListStore* apply_window_create_data_store(void)
121 {
122         GtkListStore *store = gtk_list_store_new(N_TAG_EDIT_COLUMNS,
123                                   G_TYPE_BOOLEAN,
124                                   G_TYPE_BOOLEAN,
125                                   G_TYPE_STRING,
126                                   G_TYPE_POINTER,
127                                   -1);
128         GtkTreeSortable *sortable = GTK_TREE_SORTABLE(store);
129
130         gtk_tree_sortable_set_sort_func(sortable, 0, tag_cmp_func,
131                                     NULL, NULL);
132
133         return store;
134 }
135
136 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
137                 gchar *path,
138                 GtkWidget *list_view);
139 static void tag_apply_selected_edited(GtkCellRendererText *widget,
140                 gchar *arg1, gchar *arg2,
141                 GtkWidget *list_view);
142
143 static void apply_window_insert_check_column(GtkWidget *list_view)
144 {
145         GtkTreeViewColumn *column;
146         GtkCellRenderer *renderer;
147
148         renderer = gtk_cell_renderer_toggle_new();
149         g_object_set(renderer,
150                      "radio", FALSE,
151                      "activatable", TRUE,
152                      NULL);
153         column = gtk_tree_view_column_new_with_attributes
154                 ("",
155                  renderer,
156                  "active", TAG_SELECTED,
157                  "inconsistent", TAG_SELECTED_INCONSISTENT,
158                  NULL);
159         gtk_tree_view_column_set_alignment (column, 0.5);
160         gtk_tree_view_insert_column(GTK_TREE_VIEW(list_view), column, 0);               
161         g_signal_connect(G_OBJECT(renderer), "toggled",
162                          G_CALLBACK(tag_apply_selected_toggled),
163                          list_view);
164 }
165
166 static void apply_window_create_list_view_columns(GtkWidget *list_view)
167 {
168         GtkTreeViewColumn *column;
169         GtkCellRenderer *renderer;
170
171         renderer = gtk_cell_renderer_text_new();
172         g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
173
174         column = gtk_tree_view_column_new_with_attributes
175                 (_("Tag"),
176                  renderer,
177                  "text", TAG_NAME,
178                  NULL);
179         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
180         gtk_tree_view_column_set_resizable(column, TRUE);
181         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view),
182                                         TAG_NAME);
183         g_signal_connect(G_OBJECT(renderer), "edited",
184                          G_CALLBACK(tag_apply_selected_edited),
185                          list_view);
186
187 }
188
189 static GtkItemFactory *apply_popup_factory = NULL;
190 static GtkWidget *apply_popup_menu = NULL;
191
192 static void apply_popup_delete (void *obj, guint action, void *data)
193 {
194         GtkTreeIter sel;
195         GtkTreeModel *model;
196         gint id;
197         SummaryView *summaryview = NULL;
198         
199         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
200                                 (GTK_TREE_VIEW(applywindow.taglist)),
201                                 &model, &sel))
202                 return;                         
203
204         if (alertpanel(_("Delete tag"),
205                        _("Do you really want to delete this tag?"),
206                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
207                 return;
208
209         /* XXX: Here's the reason why we need to store the original 
210          * pointer: we search the slist for it. */
211         gtk_tree_model_get(model, &sel,
212                            TAG_DATA, &id,
213                            -1);
214         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
215         if (mainwindow_get_mainwindow() != NULL)
216                 summaryview = mainwindow_get_mainwindow()->summaryview;
217         if (summaryview)
218                 summary_set_tag(summaryview, -id, NULL);
219         tags_remove_tag(id);
220         tags_write_tags();
221 }
222
223 static GtkItemFactoryEntry apply_popup_entries[] =
224 {
225         {N_("/_Delete"),                NULL, apply_popup_delete, 0, NULL, NULL},
226 };
227
228 static gint apply_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
229                                     GtkTreeView *list_view)
230 {
231         if (event && event->button == 3) {
232                 if (!apply_popup_menu) {
233                         gint n_entries = sizeof(apply_popup_entries) /
234                                 sizeof(apply_popup_entries[0]);
235                         apply_popup_menu = menu_create_items(apply_popup_entries, n_entries,
236                                                       "<TagPopupMenu>", &apply_popup_factory,
237                                                       list_view);
238                 }
239                 gtk_menu_popup(GTK_MENU(apply_popup_menu), 
240                                NULL, NULL, NULL, NULL, 
241                                event->button, event->time);
242
243                 return FALSE;
244         }
245         return FALSE;
246 }
247
248 static gboolean apply_list_popup_menu(GtkWidget *widget, gpointer data)
249 {
250         GtkTreeView *list_view = (GtkTreeView *)data;
251         GdkEventButton event;
252         
253         event.button = 3;
254         event.time = gtk_get_current_event_time();
255         
256         apply_list_btn_pressed(NULL, &event, list_view);
257
258         return TRUE;
259 }
260
261 static GtkWidget *apply_window_list_view_create (void)
262 {
263         GtkTreeView *list_view;
264         GtkTreeSelection *selector;
265         GtkTreeModel *model;
266
267         model = GTK_TREE_MODEL(apply_window_create_data_store());
268         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
269         g_object_unref(model);  
270         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model), TAG_NAME, GTK_SORT_ASCENDING);
271
272         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
273         
274         selector = gtk_tree_view_get_selection(list_view);
275         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
276
277         /* create the columns */
278         apply_window_create_list_view_columns(GTK_WIDGET(list_view));
279
280 #ifndef MAEMO
281         g_signal_connect(G_OBJECT(list_view), "popup-menu",
282                          G_CALLBACK(apply_list_popup_menu), list_view);
283 #else
284         gtk_widget_tap_and_hold_setup(GTK_WIDGET(list_view), NULL, NULL,
285                         GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
286         g_signal_connect(G_OBJECT(list_view), "tap-and-hold",
287                          G_CALLBACK(apply_list_popup_menu), list_view);
288 #endif
289         g_signal_connect(G_OBJECT(list_view), "button-press-event",
290                         G_CALLBACK(apply_list_btn_pressed), list_view);
291         return GTK_WIDGET(list_view);
292
293 }
294
295 static void apply_window_close(void) 
296 {
297         if (applywindow.msglist)
298                 g_slist_free(applywindow.msglist);
299         applywindow.msglist = NULL;
300         gtk_widget_hide(applywindow.window);
301         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
302 }
303
304 static void apply_window_close_cb(GtkWidget *widget,
305                                  gpointer data) 
306 {
307         apply_window_close();
308 }
309
310 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
311                                                   GtkTreeIter *row_iter,
312                                                   gint tag);
313
314 typedef struct FindTagInStore {
315         gint             tag_id;
316         GtkTreePath     *path;
317         GtkTreeIter      iter;
318 } FindTagInStore;
319
320 static gboolean find_tag_in_store(GtkTreeModel *model,
321                                       GtkTreePath  *path,
322                                       GtkTreeIter  *iter,
323                                       FindTagInStore *data)
324 {
325         gpointer tmp;
326         gtk_tree_model_get(model, iter, TAG_DATA, &tmp, -1);
327
328         if (data->tag_id == GPOINTER_TO_INT(tmp)) {
329                 data->path = path; /* signal we found it */
330                 data->iter = *iter;
331                 return TRUE;
332         }
333
334         return FALSE; 
335 }
336
337 static void apply_window_add_tag(void)
338 {
339         gchar *new_tag = gtk_editable_get_chars(GTK_EDITABLE(applywindow.add_entry), 0, -1);
340         g_strstrip(new_tag);
341         if (new_tag && *new_tag) {
342                 gint id = tags_get_id_for_str(new_tag);
343                 FindTagInStore fis;
344                 if (id == -1) {
345                         id = tags_add_tag(new_tag);
346                         tags_write_tags();
347                         if (mainwindow_get_mainwindow())
348                                 summary_set_tag(
349                                         mainwindow_get_mainwindow()->summaryview, 
350                                         id, NULL);
351                         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
352                         apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
353                 } 
354                 fis.tag_id = id;
355                 fis.path = NULL;
356                 gtk_tree_model_foreach(gtk_tree_view_get_model
357                                 (GTK_TREE_VIEW(applywindow.taglist)), 
358                                 (GtkTreeModelForeachFunc) find_tag_in_store,
359                                 &fis);
360                 if (fis.path) {
361                         GtkTreeSelection *selection;
362                         GtkTreePath* path;
363                         GtkTreeModel *model = gtk_tree_view_get_model(
364                                 GTK_TREE_VIEW(applywindow.taglist));
365
366                         if (mainwindow_get_mainwindow())
367                                 summary_set_tag(
368                                         mainwindow_get_mainwindow()->summaryview, 
369                                         id, NULL);
370                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(applywindow.taglist));
371                         gtk_tree_selection_select_iter(selection, &fis.iter);
372                         path = gtk_tree_model_get_path(model, &fis.iter);
373                         /* XXX returned path may not be valid??? create new one to be sure */ 
374                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(applywindow.taglist), path, NULL, FALSE);
375                         apply_window_list_view_insert_tag(applywindow.taglist, &fis.iter, id);
376                         gtk_tree_path_free(path);
377                 }
378                 g_free(new_tag);
379         } else {
380                 alertpanel_error(_("Tag is not set."));
381         }
382 }
383
384 static void apply_window_add_tag_cb(GtkWidget *widget,
385                                  gpointer data) 
386 {
387         apply_window_add_tag();
388         gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
389         gtk_widget_grab_focus(applywindow.taglist);
390 }
391
392 static void apply_window_del_tag_cb(GtkWidget *widget,
393                                  gpointer data) 
394 {
395         apply_popup_delete(NULL, 0, NULL);
396         gtk_widget_grab_focus(applywindow.taglist);
397 }
398
399 static gboolean apply_window_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
400 {
401         if (event && event->keyval == GDK_Escape)
402                 apply_window_close();
403         else if (event && event->keyval == GDK_Delete)
404                 apply_popup_delete(NULL, 0, NULL);
405         return FALSE;
406 }
407
408 static gboolean apply_window_add_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
409 {
410         if (event && (event->keyval == GDK_KP_Enter || event->keyval == GDK_Return)) {
411                 apply_window_add_tag();
412                 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
413                 gtk_widget_grab_focus(applywindow.taglist);
414         }
415                 
416         return FALSE;
417 }
418
419 static void apply_window_create(void) 
420 {
421         GtkWidget *window;
422         GtkWidget *hbox1;
423         GtkWidget *vbox1;
424         GtkWidget *label;
425         GtkWidget *taglist;
426         GtkWidget *close_btn;
427         GtkWidget *scrolledwin;
428         GtkWidget *new_tag_label;
429         GtkWidget *new_tag_entry;
430         GtkWidget *add_btn;
431         GtkWidget *del_btn;
432
433         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "tag_apply_window");
434         gtk_window_set_title (GTK_WINDOW(window),
435                               Q_("Dialog title|Apply tags"));
436
437         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
438         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
439         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
440         g_signal_connect(G_OBJECT(window), "delete_event",
441                          G_CALLBACK(apply_window_close_cb), NULL);
442         g_signal_connect(G_OBJECT(window), "key_press_event",
443                          G_CALLBACK(apply_window_key_pressed), NULL);
444         MANAGE_WINDOW_SIGNALS_CONNECT (window);
445
446         vbox1 = gtk_vbox_new(FALSE, 6);
447         hbox1 = gtk_hbox_new(FALSE, 6);
448         
449         new_tag_label = gtk_label_new(_("New tag:"));
450         gtk_misc_set_alignment(GTK_MISC(new_tag_label), 0, 0.5);
451         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_label, FALSE, FALSE, 0);
452         
453         new_tag_entry = gtk_entry_new();
454         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_entry, FALSE, FALSE, 0);
455         g_signal_connect(G_OBJECT(new_tag_entry), "key_press_event",
456                          G_CALLBACK(apply_window_add_key_pressed), NULL);
457         
458         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
459         gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
460         
461         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
462         gtk_box_pack_start(GTK_BOX(hbox1), del_btn, FALSE, FALSE, 0);
463         
464         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
465         gtk_box_pack_end(GTK_BOX(hbox1), close_btn, FALSE, FALSE, 0);
466
467         gtk_widget_show(new_tag_label);
468         gtk_widget_show(new_tag_entry);
469         gtk_widget_show(close_btn);
470         gtk_widget_show(add_btn);
471         gtk_widget_show(del_btn);
472
473         g_signal_connect(G_OBJECT(close_btn), "clicked",
474                          G_CALLBACK(apply_window_close_cb), NULL);
475         g_signal_connect(G_OBJECT(add_btn), "clicked",
476                          G_CALLBACK(apply_window_add_tag_cb), NULL);
477         g_signal_connect(G_OBJECT(del_btn), "clicked",
478                          G_CALLBACK(apply_window_del_tag_cb), NULL);
479
480         taglist = apply_window_list_view_create();
481         
482         label = gtk_label_new(_("Please select tags to apply/remove. Changes are immediate."));
483         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
484         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
485         
486         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
487         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
488                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
489                                        
490         gtk_widget_set_size_request(scrolledwin, 500, 250);
491
492         gtk_container_add(GTK_CONTAINER(scrolledwin), taglist);
493         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
494         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
495         
496         gtk_widget_show(label);
497         gtk_widget_show(scrolledwin);
498         gtk_widget_show(taglist);
499         gtk_widget_show(hbox1);
500         gtk_widget_show(vbox1);
501         gtk_widget_show(close_btn);
502         gtk_container_add(GTK_CONTAINER (window), vbox1);
503
504         applywindow.window = window;
505         applywindow.list_view = taglist;
506         applywindow.hbox1 = hbox1;
507         applywindow.vbox1 = vbox1;
508         applywindow.label = label;
509         applywindow.taglist = taglist;
510         applywindow.close_btn = close_btn;
511         applywindow.add_btn = add_btn;
512         applywindow.add_entry = new_tag_entry;
513         applywindow.has_tag_col = FALSE;
514 }
515
516 static void apply_window_list_view_clear_tags(GtkWidget *list_view)
517 {
518         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
519                                         (GTK_TREE_VIEW(list_view)));
520         gtk_list_store_clear(list_store);
521 }
522
523
524 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
525                 gchar *path,
526                 GtkWidget *list_view)
527 {
528         GtkTreeIter iter;
529         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
530         gboolean enabled = TRUE, set = FALSE;
531         gpointer tmp;
532         gint tag_id;
533         SummaryView *summaryview = NULL;
534         
535         if (mainwindow_get_mainwindow() != NULL)
536                 summaryview = mainwindow_get_mainwindow()->summaryview;
537
538         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
539                 return;
540
541         gtk_tree_model_get(model, &iter,
542                            TAG_SELECTED, &enabled,
543                            TAG_DATA, &tmp,
544                            -1);
545
546         set = !enabled;
547         tag_id = GPOINTER_TO_INT(tmp);
548
549         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
550                            TAG_SELECTED, set,
551                            TAG_SELECTED_INCONSISTENT, FALSE,
552                            -1);
553         
554         if (summaryview)
555                 summary_set_tag(summaryview, set ? tag_id : -tag_id, NULL);
556 }
557
558 static void tag_apply_selected_edited(GtkCellRendererText *widget,
559                 gchar *path, gchar *new_text,
560                 GtkWidget *list_view)
561 {
562         GtkTreeIter iter;
563         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
564         gpointer tmp;
565         gint tag_id;
566         SummaryView *summaryview = NULL;
567
568         if (mainwindow_get_mainwindow() != NULL)
569                 summaryview = mainwindow_get_mainwindow()->summaryview;
570
571         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
572                 return;
573
574         if (!new_text || !*new_text)
575                 return;
576
577         gtk_tree_model_get(model, &iter,
578                            TAG_DATA, &tmp,
579                            -1);
580
581         tag_id = GPOINTER_TO_INT(tmp);
582         tags_update_tag(tag_id, new_text);
583         
584         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
585                            TAG_NAME, new_text,
586                            -1);
587         if (summaryview)
588                 summary_set_tag(summaryview, 0, NULL);
589 }
590
591 static void apply_window_get_selected_state(gint tag, gboolean *selected, gboolean *selected_inconsistent)
592 {
593         GSList *cur = applywindow.msglist;
594         gint num_mails = 0;
595         gint num_selected = 0;
596         for (; cur; cur = cur->next) {
597                 MsgInfo *msginfo = (MsgInfo *)cur->data;
598                 num_mails++;
599                 if (msginfo->tags && g_slist_find(msginfo->tags, GINT_TO_POINTER(tag))) {
600                         *selected = TRUE;
601                         num_selected++;
602                 }
603         }
604         if (num_selected > 0 && num_selected < num_mails)
605                 *selected_inconsistent = TRUE;
606         else
607                 *selected_inconsistent = FALSE;
608 }
609
610 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
611                                                   GtkTreeIter *row_iter,
612                                                   gint tag) 
613 {
614         GtkTreeIter iter;
615         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
616                                         (GTK_TREE_VIEW(list_view)));
617         const gchar *name = tags_get_tag(tag);
618         gboolean selected = FALSE, selected_inconsistent = FALSE;
619
620         apply_window_get_selected_state(tag, &selected, &selected_inconsistent);
621         if (row_iter == NULL) {
622                 /* append new */
623                 gtk_list_store_append(list_store, &iter);
624                 gtk_list_store_set(list_store, &iter,
625                                    TAG_SELECTED, selected,
626                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
627                                    TAG_NAME, name,
628                                    TAG_DATA, GINT_TO_POINTER(tag),
629                                    -1);
630         } else {
631                 gtk_list_store_set(list_store, row_iter,
632                                    TAG_SELECTED, selected,
633                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
634                                    TAG_NAME, name,
635                                    TAG_DATA, GINT_TO_POINTER(tag),
636                                    -1);
637         }
638 }
639
640 static void apply_window_load_tags (void) 
641 {
642         GSList *cur;
643         GSList *tags = tags_get_list();
644         apply_window_list_view_clear_tags(applywindow.taglist);
645         
646         cur = tags;
647         for (; cur; cur = cur->next) {
648                 gint id = GPOINTER_TO_INT(cur->data);
649                 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
650         }
651 }
652