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