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