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