6ec316bfe46ad2d95be5b0e5aa79e298863cab59
[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 "edittags.h"
36 #include "prefs_gtk.h"
37 #include "utils.h"
38 #include "gtkutils.h"
39 #include "inputdialog.h"
40 #include "manage_window.h"
41 #include "mainwindow.h"
42 #include "prefs_common.h"
43 #include "alertpanel.h"
44 #include "summaryview.h"
45 #include "tags.h"
46 #include "gtkutils.h"
47 #include "manual.h"
48
49 enum {
50         PREFS_TAGS_STRING,      /*!< string pointer managed by list store, 
51                                  *   and never touched or retrieved by 
52                                  *   us */ 
53         PREFS_TAGS_ID,          /*!< pointer to string that is not managed by 
54                                  *   the list store, and which is retrieved
55                                  *   and touched by us */
56         N_PREFS_TAGS_COLUMNS
57 };
58
59 static struct Tags
60 {
61         GtkWidget *window;
62
63         GtkWidget *ok_btn;
64
65         GtkWidget *name_entry;
66         GtkWidget *tags_list_view;
67 } tags;
68
69 static int modified = FALSE;
70
71 /* widget creating functions */
72 static void prefs_tags_create   (MainWindow *mainwin);
73 static void prefs_tags_set_dialog       (void);
74 static gint prefs_tags_clist_set_row    (GtkTreeIter *row);
75
76 /* callback functions */
77 static void prefs_tags_register_cb      (GtkWidget      *w,
78                                          gpointer        data);
79 static void prefs_tags_substitute_cb    (GtkWidget      *w,
80                                          gpointer        data);
81 static void prefs_tags_delete_cb        (GtkWidget      *w,
82                                          gpointer        data);
83 static gint prefs_tags_deleted          (GtkWidget      *widget,
84                                          GdkEventAny    *event,
85                                          gpointer       *data);
86 static gboolean prefs_tags_key_pressed(GtkWidget        *widget,
87                                           GdkEventKey   *event,
88                                           gpointer       data);
89 static void prefs_tags_ok               (GtkWidget      *w,
90                                          gpointer        data);
91
92
93 static GtkListStore* prefs_tags_create_data_store       (void);
94
95 static void prefs_tags_list_view_insert_tag     (GtkWidget *list_view,
96                                                          GtkTreeIter *row_iter,
97                                                          gchar *tag,
98                                                          gint id);
99 static GtkWidget *prefs_tags_list_view_create   (void);
100 static void prefs_tags_create_list_view_columns (GtkWidget *list_view);
101 static gboolean prefs_tags_selected                     (GtkTreeSelection *selector,
102                                                          GtkTreeModel *model, 
103                                                          GtkTreePath *path,
104                                                          gboolean currently_selected,
105                                                          gpointer data);
106
107 void prefs_tags_open(MainWindow *mainwin)
108 {
109         if (!tags.window)
110                 prefs_tags_create(mainwin);
111
112         manage_window_set_transient(GTK_WINDOW(tags.window));
113         gtk_widget_grab_focus(tags.ok_btn);
114
115         prefs_tags_set_dialog();
116
117         gtk_widget_show(tags.window);
118 }
119
120 /*!
121  *\brief        Save Gtk object size to prefs dataset
122  */
123 static void prefs_tags_size_allocate_cb(GtkWidget *widget,
124                                          GtkAllocation *allocation)
125 {
126         g_return_if_fail(allocation != NULL);
127
128         prefs_common.tagswin_width = allocation->width;
129         prefs_common.tagswin_height = allocation->height;
130 }
131
132 static void prefs_tags_create(MainWindow *mainwin)
133 {
134         GtkWidget *window;
135         GtkWidget *vbox;
136         GtkWidget *help_btn;
137         GtkWidget *ok_btn;
138         GtkWidget *confirm_area;
139
140         GtkWidget *vbox1;
141         GtkWidget *table;
142
143         GtkWidget *name_label;
144         GtkWidget *name_entry;
145
146         GtkWidget *reg_hbox;
147         GtkWidget *btn_hbox;
148         GtkWidget *arrow;
149         GtkWidget *reg_btn;
150         GtkWidget *subst_btn;
151         GtkWidget *del_btn;
152
153         GtkWidget *cond_hbox;
154         GtkWidget *cond_scrolledwin;
155         GtkWidget *cond_list_view;
156
157
158         GtkWidget *btn_vbox;
159         static GdkGeometry geometry;
160
161         debug_print("Creating tags configuration window...\n");
162
163         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_tags");
164
165         gtk_container_set_border_width(GTK_CONTAINER (window), 8);
166         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
167         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
168         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
169
170         vbox = gtk_vbox_new(FALSE, 6);
171         gtk_widget_show(vbox);
172         gtk_container_add(GTK_CONTAINER(window), vbox);
173
174         gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
175                         &ok_btn, GTK_STOCK_OK,
176                         NULL, NULL,
177                         NULL, NULL);
178         gtk_widget_show(confirm_area);
179         gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
180         gtk_widget_grab_default(ok_btn);
181
182         gtk_window_set_title(GTK_WINDOW(window), _("Tags configuration"));
183         g_signal_connect(G_OBJECT(window), "delete_event",
184                          G_CALLBACK(prefs_tags_deleted), NULL);
185         g_signal_connect(G_OBJECT(window), "size_allocate",
186                          G_CALLBACK(prefs_tags_size_allocate_cb), NULL);
187         g_signal_connect(G_OBJECT(window), "key_press_event",
188                          G_CALLBACK(prefs_tags_key_pressed), NULL);
189         MANAGE_WINDOW_SIGNALS_CONNECT(window);
190         g_signal_connect(G_OBJECT(ok_btn), "clicked",
191                          G_CALLBACK(prefs_tags_ok), mainwin);
192         g_signal_connect(G_OBJECT(help_btn), "clicked",
193                          G_CALLBACK(manual_open_with_anchor_cb),
194                          MANUAL_ANCHOR_TAGS);
195
196         vbox1 = gtk_vbox_new(FALSE, VSPACING);
197         gtk_widget_show(vbox1);
198         gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0);
199         gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2);
200
201         table = gtk_table_new(2, 2, FALSE);
202         gtk_table_set_row_spacings (GTK_TABLE (table), VSPACING_NARROW_2);
203         gtk_table_set_col_spacings (GTK_TABLE (table), 4);
204         gtk_widget_show(table);
205         gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 0);
206
207         name_label = gtk_label_new (_("Tag name"));
208         gtk_widget_show (name_label);
209         gtk_misc_set_alignment (GTK_MISC (name_label), 1, 0.5);
210         gtk_table_attach (GTK_TABLE (table), name_label, 0, 1, 0, 1,
211                           (GtkAttachOptions) (GTK_FILL),
212                           (GtkAttachOptions) (0), 0, 0);
213
214         name_entry = gtk_entry_new ();
215         gtk_widget_show (name_entry);
216         gtk_table_attach (GTK_TABLE (table), name_entry, 1, 2, 0, 1,
217                           (GtkAttachOptions) (GTK_FILL|GTK_EXPAND),
218                           (GtkAttachOptions) (0), 0, 0);
219
220         /* register / delete */
221
222         reg_hbox = gtk_hbox_new(FALSE, 4);
223         gtk_widget_show(reg_hbox);
224         gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0);
225
226         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
227         gtk_widget_show(arrow);
228         gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0);
229         gtk_widget_set_size_request(arrow, -1, 16);
230
231         btn_hbox = gtk_hbox_new(TRUE, 4);
232         gtk_widget_show(btn_hbox);
233         gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0);
234
235         reg_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
236         gtk_widget_show(reg_btn);
237         gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0);
238         g_signal_connect(G_OBJECT(reg_btn), "clicked",
239                          G_CALLBACK(prefs_tags_register_cb), NULL);
240
241         subst_btn = gtkut_get_replace_btn(_("Replace"));
242         gtk_widget_show(subst_btn);
243         gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0);
244         g_signal_connect(G_OBJECT(subst_btn), "clicked",
245                          G_CALLBACK(prefs_tags_substitute_cb),
246                          NULL);
247
248         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
249         gtk_widget_show(del_btn);
250         gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0);
251         g_signal_connect(G_OBJECT(del_btn), "clicked",
252                          G_CALLBACK(prefs_tags_delete_cb), NULL);
253
254         cond_hbox = gtk_hbox_new(FALSE, 8);
255         gtk_widget_show(cond_hbox);
256         gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0);
257
258         cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
259         gtk_widget_show(cond_scrolledwin);
260         gtk_widget_set_size_request(cond_scrolledwin, -1, 150);
261         gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin,
262                            TRUE, TRUE, 0);
263         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin),
264                                        GTK_POLICY_AUTOMATIC,
265                                        GTK_POLICY_AUTOMATIC);
266
267         cond_list_view = prefs_tags_list_view_create();                                
268         gtk_widget_show(cond_list_view);
269         gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_list_view);
270
271         btn_vbox = gtk_vbox_new(FALSE, 8);
272         gtk_widget_show(btn_vbox);
273         gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0);
274
275         if (!geometry.min_height) {
276                 geometry.min_width = 486;
277                 geometry.min_height = 322;
278         }
279
280         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
281                                       GDK_HINT_MIN_SIZE);
282         gtk_widget_set_size_request(window, prefs_common.tagswin_width,
283                                     prefs_common.tagswin_height);
284
285         gtk_widget_show(window);
286
287         tags.window = window;
288         tags.ok_btn = ok_btn;
289
290         tags.name_entry = name_entry;
291
292         tags.tags_list_view = cond_list_view;
293 }
294
295 static void prefs_tags_set_dialog(void)
296 {
297         GtkListStore *store;
298         GSList *cur, *orig;
299         GtkTreeSelection *selection;
300         GtkTreeIter iter;
301
302         store = GTK_LIST_STORE(gtk_tree_view_get_model
303                                 (GTK_TREE_VIEW(tags.tags_list_view)));
304         gtk_list_store_clear(store);
305
306         prefs_tags_list_view_insert_tag(tags.tags_list_view,
307                                               NULL, _("(New)"), -1);
308
309         for (orig = cur = tags_get_list(); cur != NULL; cur = cur->next) {
310                 gint id = GPOINTER_TO_INT(cur->data);
311                 gchar *tag = (gchar *) tags_get_tag(id);
312                 
313                 prefs_tags_list_view_insert_tag(tags.tags_list_view,
314                                                       NULL, tag, id);
315         }
316
317         g_slist_free(orig);
318         /* select first entry */
319         selection = gtk_tree_view_get_selection
320                 (GTK_TREE_VIEW(tags.tags_list_view));
321         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),
322                                           &iter))
323                 gtk_tree_selection_select_iter(selection, &iter);
324 }
325
326 static gint prefs_tags_clist_set_row(GtkTreeIter *row)
327 {
328         gchar *tag;
329         GtkListStore *store;
330         gint id = -1;
331
332         store = GTK_LIST_STORE(gtk_tree_view_get_model
333                                 (GTK_TREE_VIEW(tags.tags_list_view)));
334         
335
336         tag = gtk_editable_get_chars(GTK_EDITABLE(tags.name_entry), 0, -1);
337         if (tag[0] == '\0') {
338                 alertpanel_error(_("Tag is not set."));
339                 return -1;
340         }
341
342         g_strstrip(tag);
343         if (row == NULL) {
344                 if ((id = tags_add_tag(tag)) != -1) {
345                         prefs_tags_list_view_insert_tag(tags.tags_list_view,
346                                               row, tag, id);
347                         tags_write_tags();
348                 }
349         } else {
350                 prefs_tags_list_view_insert_tag(tags.tags_list_view,
351                                       row, tag, -1);
352                 tags_write_tags();
353         }
354         return 0;
355 }
356
357 /* callback functions */
358
359 static void prefs_tags_register_cb(GtkWidget *w, gpointer data)
360 {
361         prefs_tags_clist_set_row(NULL);
362         modified = FALSE;
363 }
364
365 static void prefs_tags_substitute_cb(GtkWidget *w, gpointer data)
366 {
367         GtkTreeIter isel, inew;
368         GtkTreePath *path_sel, *path_new;
369         GtkTreeSelection *selection = gtk_tree_view_get_selection
370                                         (GTK_TREE_VIEW(tags.tags_list_view));
371         GtkTreeModel *model;                                    
372
373         if (!gtk_tree_selection_get_selected(selection, &model, &isel))
374                 return;
375         if (!gtk_tree_model_get_iter_first(model, &inew))
376                 return;
377
378         path_sel = gtk_tree_model_get_path(model, &isel);
379         path_new = gtk_tree_model_get_path(model, &inew);
380
381         if (path_sel && path_new 
382         &&  gtk_tree_path_compare(path_sel, path_new) != 0)
383                 prefs_tags_clist_set_row(&isel);
384
385         gtk_tree_path_free(path_sel);
386         gtk_tree_path_free(path_new);
387         modified = FALSE;
388 }
389
390 static void prefs_tags_delete_cb(GtkWidget *w, gpointer data)
391 {
392         GtkTreeIter sel;
393         GtkTreeModel *model;
394         gint id;
395
396         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
397                                 (GTK_TREE_VIEW(tags.tags_list_view)),
398                                 &model, &sel))
399                 return;                         
400
401         if (alertpanel(_("Delete tag"),
402                        _("Do you really want to delete this tag?"),
403                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
404                 return;
405
406         /* XXX: Here's the reason why we need to store the original 
407          * pointer: we search the slist for it. */
408         gtk_tree_model_get(model, &sel,
409                            PREFS_TAGS_ID, &id,
410                            -1);
411         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
412         tags_remove_tag(id);
413         tags_write_tags();
414 }
415
416 static gint prefs_tags_deleted(GtkWidget *widget, GdkEventAny *event,
417                                   gpointer *data)
418 {
419         prefs_tags_ok(widget, data);
420         return TRUE;
421 }
422
423 static gboolean prefs_tags_key_pressed(GtkWidget *widget, GdkEventKey *event,
424                                           gpointer data)
425 {
426         if (event && event->keyval == GDK_Escape)
427                 prefs_tags_ok(widget, data);
428         else {
429                 GtkWidget *focused = gtkut_get_focused_child(
430                                         GTK_CONTAINER(widget));
431                 if (focused && GTK_IS_EDITABLE(focused)) {
432                         modified = TRUE;
433                 }
434         }
435         return FALSE;
436 }
437
438 static void prefs_tags_ok(GtkWidget *widget, gpointer data)
439 {
440         if (modified && alertpanel(_("Entry not saved"),
441                                  _("The entry was not saved. Close anyway?"),
442                                  GTK_STOCK_CLOSE, _("+_Continue editing"),
443                                  NULL) != G_ALERTDEFAULT) {
444                 return;
445         }
446         modified = FALSE;
447
448         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
449         gtk_widget_hide(tags.window);
450 }
451
452 static GtkListStore* prefs_tags_create_data_store(void)
453 {
454         return gtk_list_store_new(N_PREFS_TAGS_COLUMNS,
455                                   G_TYPE_STRING,        
456                                   G_TYPE_INT,
457                                   -1);
458 }
459
460 static void prefs_tags_list_view_insert_tag(GtkWidget *list_view,
461                                                   GtkTreeIter *row_iter,
462                                                   gchar *tag,
463                                                   gint id) 
464 {
465         GtkTreeIter iter;
466         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
467                                         (GTK_TREE_VIEW(list_view)));
468
469         if (row_iter == NULL) {
470                 /* append new */
471                 gtk_list_store_append(list_store, &iter);
472                 gtk_list_store_set(list_store, &iter,
473                                    PREFS_TAGS_STRING, tag,
474                                    PREFS_TAGS_ID, id,
475                                    -1);
476         } else if (id == -1) {
477                 /* change existing */
478                 gtk_tree_model_get(GTK_TREE_MODEL(list_store), row_iter,
479                                    PREFS_TAGS_ID, &id,
480                                    -1);
481                 gtk_list_store_set(list_store, row_iter,
482                                    PREFS_TAGS_STRING, tag,
483                                    PREFS_TAGS_ID, id,
484                                    -1);
485                 tags_update_tag(id, tag);
486         }
487 }
488
489 static GtkWidget *prefs_tags_list_view_create(void)
490 {
491         GtkTreeView *list_view;
492         GtkTreeSelection *selector;
493         GtkTreeModel *model;
494
495         model = GTK_TREE_MODEL(prefs_tags_create_data_store());
496         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
497         g_object_unref(model);  
498         
499         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
500
501         selector = gtk_tree_view_get_selection(list_view);
502         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
503         gtk_tree_selection_set_select_function(selector, prefs_tags_selected,
504                                                NULL, NULL);
505
506         /* create the columns */
507         prefs_tags_create_list_view_columns(GTK_WIDGET(list_view));
508
509         return GTK_WIDGET(list_view);
510 }
511
512 static void prefs_tags_create_list_view_columns(GtkWidget *list_view)
513 {
514         GtkTreeViewColumn *column;
515         GtkCellRenderer *renderer;
516
517         renderer = gtk_cell_renderer_text_new();
518         column = gtk_tree_view_column_new_with_attributes
519                 (_("Current tags"),
520                  renderer,
521                  "text", PREFS_TAGS_STRING,
522                  NULL);
523         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
524 }
525
526 #define ENTRY_SET_TEXT(entry, str) \
527         gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "")
528
529 static gboolean prefs_tags_selected(GtkTreeSelection *selector,
530                                        GtkTreeModel *model, 
531                                        GtkTreePath *path,
532                                        gboolean currently_selected,
533                                        gpointer data)
534 {
535         gchar *tag;
536         GtkTreeIter iter;
537         gint id;
538
539         if (currently_selected)
540                 return TRUE;
541
542         if (!gtk_tree_model_get_iter(model, &iter, path))
543                 return TRUE;
544
545         gtk_tree_model_get(model, &iter, 
546                            PREFS_TAGS_ID,  &id,
547                            PREFS_TAGS_STRING, &tag,
548                            -1);
549         if (id == -1) {
550                 ENTRY_SET_TEXT(tags.name_entry, "");
551                 return TRUE;
552         }
553         
554         ENTRY_SET_TEXT(tags.name_entry, tag);
555
556         return TRUE;
557 }
558
559 gint prefs_tags_create_new(MainWindow *mainwin)
560 {
561         gchar *new_tag = input_dialog(_("New tag"),
562                                 _("New tag name:"), 
563                                 ""); 
564         gint id = -1;
565         if (!new_tag || !(*new_tag))
566                 return -1;
567         
568         id = tags_get_id_for_str(new_tag);
569         if (id != -1) {
570                 g_free(new_tag);
571                 return id;
572         }
573         id = tags_add_tag(new_tag);
574         g_free(new_tag);
575         
576         tags_write_tags();
577         return id;
578         
579 }
580
581 enum {
582         TAG_SELECTED,
583         TAG_SELECTED_INCONSISTENT,
584         TAG_NAME,
585         TAG_DATA,
586         N_TAG_EDIT_COLUMNS
587 };
588
589 static void apply_window_create(void);
590
591 static struct TagApplyWindow
592 {
593         GtkWidget *window;
594         GtkWidget *hbox1;
595         GtkWidget *vbox1;
596         GtkWidget *label;
597         GtkWidget *taglist;
598         GtkWidget *close_btn;
599         GtkWidget *add_entry;
600         GtkWidget *add_btn;
601         GSList *msglist;
602 } applywindow;
603
604 static void apply_window_load_tags (void);
605
606 void tag_apply_open(GSList *msglist)
607 {
608         g_return_if_fail(msglist);
609
610         if (!applywindow.window)
611                 apply_window_create();
612
613         manage_window_set_transient(GTK_WINDOW(applywindow.window));
614         gtk_widget_grab_focus(applywindow.close_btn);
615         
616         applywindow.msglist = msglist;
617         apply_window_load_tags();
618
619         gtk_widget_show(applywindow.window);
620         gtk_widget_grab_focus(applywindow.taglist);
621         gtk_window_set_modal(GTK_WINDOW(applywindow.window), TRUE);
622 }
623
624 static GtkListStore* apply_window_create_data_store(void)
625 {
626         return gtk_list_store_new(N_TAG_EDIT_COLUMNS,
627                                   G_TYPE_BOOLEAN,
628                                   G_TYPE_BOOLEAN,
629                                   G_TYPE_STRING,
630                                   G_TYPE_POINTER,
631                                   -1);
632 }
633
634 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
635                 gchar *path,
636                 GtkWidget *list_view);
637
638 static void apply_window_create_list_view_columns(GtkWidget *list_view)
639 {
640         GtkTreeViewColumn *column;
641         GtkCellRenderer *renderer;
642
643         renderer = gtk_cell_renderer_toggle_new();
644         g_object_set(renderer,
645                      "radio", FALSE,
646                      "activatable", TRUE,
647                      NULL);
648         column = gtk_tree_view_column_new_with_attributes
649                 (_(""),
650                  renderer,
651                  "active", TAG_SELECTED,
652                  "inconsistent", TAG_SELECTED_INCONSISTENT,
653                  NULL);
654         gtk_tree_view_column_set_alignment (column, 0.5);
655         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
656         g_signal_connect(G_OBJECT(renderer), "toggled",
657                          G_CALLBACK(tag_apply_selected_toggled),
658                          list_view);
659
660         renderer = gtk_cell_renderer_text_new();
661         column = gtk_tree_view_column_new_with_attributes
662                 (_("Tag"),
663                  renderer,
664                  "text", TAG_NAME,
665                  NULL);
666         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
667         gtk_tree_view_column_set_resizable(column, TRUE);
668 }
669
670 static GtkWidget *apply_window_list_view_create (void)
671 {
672         GtkTreeView *list_view;
673         GtkTreeSelection *selector;
674         GtkTreeModel *model;
675
676         model = GTK_TREE_MODEL(apply_window_create_data_store());
677         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
678         g_object_unref(model);  
679         
680         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
681         
682         selector = gtk_tree_view_get_selection(list_view);
683         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
684
685         /* create the columns */
686         apply_window_create_list_view_columns(GTK_WIDGET(list_view));
687
688         return GTK_WIDGET(list_view);
689
690 }
691
692 static void apply_window_close(void) 
693 {
694         g_slist_free(applywindow.msglist);
695         applywindow.msglist = NULL;
696         gtk_widget_hide(applywindow.window);
697 }
698
699 static void apply_window_close_cb(GtkWidget *widget,
700                                  gpointer data) 
701 {
702         apply_window_close();
703 }
704
705 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
706                                                   GtkTreeIter *row_iter,
707                                                   gint tag);
708
709 static void apply_window_add_tag(void)
710 {
711         gchar *new_tag = gtk_editable_get_chars(GTK_EDITABLE(applywindow.add_entry), 0, -1);
712         if (new_tag) {
713                 gint id = tags_get_id_for_str(new_tag);
714                 if (id == -1) {
715                         id = tags_add_tag(new_tag);
716                         tags_write_tags();
717                         if (mainwindow_get_mainwindow())
718                                 summary_set_tag(
719                                         mainwindow_get_mainwindow()->summaryview, 
720                                         id, NULL);
721                         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
722                         apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
723                 }
724                 g_free(new_tag);
725         }
726 }
727
728 static void apply_window_add_tag_cb(GtkWidget *widget,
729                                  gpointer data) 
730 {
731         apply_window_add_tag();
732 }
733
734 static gboolean apply_window_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
735 {
736         if (event && event->keyval == GDK_Escape)
737                 apply_window_close();
738         return FALSE;
739 }
740
741 static gboolean apply_window_add_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
742 {
743         if (event && (event->keyval == GDK_KP_Enter || event->keyval == GDK_Return)) {
744                 apply_window_add_tag();
745                 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
746                 gtk_widget_grab_focus(applywindow.taglist);
747         }
748                 
749         return FALSE;
750 }
751
752 static void apply_window_create(void) 
753 {
754         GtkWidget *window;
755         GtkWidget *hbox1;
756         GtkWidget *vbox1;
757         GtkWidget *label;
758         GtkWidget *taglist;
759         GtkWidget *close_btn;
760         GtkWidget *scrolledwin;
761         GtkWidget *new_tag_label;
762         GtkWidget *new_tag_entry;
763         GtkWidget *add_btn;
764
765         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "tag_apply_window");
766         gtk_window_set_title (GTK_WINDOW(window),
767                               Q_("Dialog title|Apply tags"));
768
769         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
770         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
771         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
772         g_signal_connect(G_OBJECT(window), "delete_event",
773                          G_CALLBACK(apply_window_close_cb), NULL);
774         g_signal_connect(G_OBJECT(window), "key_press_event",
775                          G_CALLBACK(apply_window_key_pressed), NULL);
776         MANAGE_WINDOW_SIGNALS_CONNECT (window);
777
778         vbox1 = gtk_vbox_new(FALSE, 6);
779         hbox1 = gtk_hbox_new(FALSE, 6);
780         
781         new_tag_label = gtk_label_new(_("New tag:"));
782         gtk_misc_set_alignment(GTK_MISC(new_tag_label), 0, 0.5);
783         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_label, FALSE, FALSE, 0);
784         
785         new_tag_entry = gtk_entry_new();
786         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_entry, FALSE, FALSE, 0);
787         g_signal_connect(G_OBJECT(new_tag_entry), "key_press_event",
788                          G_CALLBACK(apply_window_add_key_pressed), NULL);
789         
790         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
791         gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
792         
793         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
794         gtk_box_pack_end(GTK_BOX(hbox1), close_btn, FALSE, FALSE, 0);
795
796         gtk_widget_show(new_tag_label);
797         gtk_widget_show(new_tag_entry);
798         gtk_widget_show(close_btn);
799         gtk_widget_show(add_btn);
800         g_signal_connect(G_OBJECT(close_btn), "clicked",
801                          G_CALLBACK(apply_window_close_cb), NULL);
802         g_signal_connect(G_OBJECT(add_btn), "clicked",
803                          G_CALLBACK(apply_window_add_tag_cb), NULL);
804
805         taglist = apply_window_list_view_create();
806         
807         label = gtk_label_new(_("Please select tags to apply/remove. Changes are immediate."));
808         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
809         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
810         
811         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
812         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
813                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
814                                        
815         gtk_widget_set_size_request(scrolledwin, 500, 250);
816
817         gtk_container_add(GTK_CONTAINER(scrolledwin), taglist);
818         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
819         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
820         
821         gtk_widget_show(label);
822         gtk_widget_show(scrolledwin);
823         gtk_widget_show(taglist);
824         gtk_widget_show(hbox1);
825         gtk_widget_show(vbox1);
826         gtk_widget_show(close_btn);
827         gtk_container_add(GTK_CONTAINER (window), vbox1);
828
829         applywindow.window = window;
830         applywindow.hbox1 = hbox1;
831         applywindow.vbox1 = vbox1;
832         applywindow.label = label;
833         applywindow.taglist = taglist;
834         applywindow.close_btn = close_btn;
835         applywindow.add_btn = add_btn;
836         applywindow.add_entry = new_tag_entry;
837 }
838
839 static void apply_window_list_view_clear_tags(GtkWidget *list_view)
840 {
841         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
842                                         (GTK_TREE_VIEW(list_view)));
843         gtk_list_store_clear(list_store);
844 }
845
846
847 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
848                 gchar *path,
849                 GtkWidget *list_view)
850 {
851         GtkTreeIter iter;
852         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
853         gboolean enabled = TRUE, set = FALSE;
854         gpointer tmp;
855         gint tag_id;
856         SummaryView *summaryview = NULL;
857         
858         if (mainwindow_get_mainwindow() != NULL)
859                 summaryview = mainwindow_get_mainwindow()->summaryview;
860
861         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
862                 return;
863
864         gtk_tree_model_get(model, &iter,
865                            TAG_SELECTED, &enabled,
866                            TAG_DATA, &tmp,
867                            -1);
868
869         set = !enabled;
870         tag_id = GPOINTER_TO_INT(tmp);
871
872         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
873                            TAG_SELECTED, set,
874                            TAG_SELECTED_INCONSISTENT, FALSE,
875                            -1);
876         
877         if (summaryview)
878                 summary_set_tag(summaryview, set ? tag_id : -tag_id, NULL);
879 }
880
881 static void apply_window_get_selected_state(gint tag, gboolean *selected, gboolean *selected_inconsistent)
882 {
883         GSList *cur = applywindow.msglist;
884         gint num_mails = 0;
885         gint num_selected = 0;
886         for (; cur; cur = cur->next) {
887                 MsgInfo *msginfo = (MsgInfo *)cur->data;
888                 num_mails++;
889                 if (msginfo->tags && g_slist_find(msginfo->tags, GINT_TO_POINTER(tag))) {
890                         *selected = TRUE;
891                         num_selected++;
892                 }
893         }
894         if (num_selected > 0 && num_selected < num_mails)
895                 *selected_inconsistent = TRUE;
896         else
897                 *selected_inconsistent = FALSE;
898 }
899
900 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
901                                                   GtkTreeIter *row_iter,
902                                                   gint tag) 
903 {
904         GtkTreeIter iter;
905         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
906                                         (GTK_TREE_VIEW(list_view)));
907         const gchar *name = tags_get_tag(tag);
908         gboolean selected = FALSE, selected_inconsistent = FALSE;
909
910         apply_window_get_selected_state(tag, &selected, &selected_inconsistent);
911         if (row_iter == NULL) {
912                 /* append new */
913                 gtk_list_store_append(list_store, &iter);
914                 gtk_list_store_set(list_store, &iter,
915                                    TAG_SELECTED, selected,
916                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
917                                    TAG_NAME, name,
918                                    TAG_DATA, GINT_TO_POINTER(tag),
919                                    -1);
920         } else {
921                 gtk_list_store_set(list_store, row_iter,
922                                    TAG_SELECTED, selected,
923                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
924                                    TAG_NAME, name,
925                                    TAG_DATA, GINT_TO_POINTER(tag),
926                                    -1);
927         }
928 }
929
930 static void apply_window_load_tags (void) 
931 {
932         GSList *cur;
933         GSList *tags = tags_get_list();
934         apply_window_list_view_clear_tags(applywindow.taglist);
935         
936         cur = tags;
937         for (; cur; cur = cur->next) {
938                 gint id = GPOINTER_TO_INT(cur->data);
939                 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
940         }
941 }
942