2007-09-01 [colin] 2.10.0cvs189
[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         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view),
670                                         TAG_NAME);
671 }
672
673 static GtkWidget *apply_window_list_view_create (void)
674 {
675         GtkTreeView *list_view;
676         GtkTreeSelection *selector;
677         GtkTreeModel *model;
678
679         model = GTK_TREE_MODEL(apply_window_create_data_store());
680         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
681         g_object_unref(model);  
682         
683         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
684         
685         selector = gtk_tree_view_get_selection(list_view);
686         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
687
688         /* create the columns */
689         apply_window_create_list_view_columns(GTK_WIDGET(list_view));
690
691         return GTK_WIDGET(list_view);
692
693 }
694
695 static void apply_window_close(void) 
696 {
697         g_slist_free(applywindow.msglist);
698         applywindow.msglist = NULL;
699         gtk_widget_hide(applywindow.window);
700 }
701
702 static void apply_window_close_cb(GtkWidget *widget,
703                                  gpointer data) 
704 {
705         apply_window_close();
706 }
707
708 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
709                                                   GtkTreeIter *row_iter,
710                                                   gint tag);
711
712 typedef struct FindTagInStore {
713         gint             tag_id;
714         GtkTreePath     *path;
715         GtkTreeIter      iter;
716 } FindTagInStore;
717
718 static gboolean find_tag_in_store(GtkTreeModel *model,
719                                       GtkTreePath  *path,
720                                       GtkTreeIter  *iter,
721                                       FindTagInStore *data)
722 {
723         gpointer tmp;
724         gtk_tree_model_get(model, iter, TAG_DATA, &tmp, -1);
725
726         if (data->tag_id == GPOINTER_TO_INT(tmp)) {
727                 data->path = path; /* signal we found it */
728                 data->iter = *iter;
729                 return TRUE;
730         }
731
732         return FALSE; 
733 }
734
735 static void apply_window_add_tag(void)
736 {
737         gchar *new_tag = gtk_editable_get_chars(GTK_EDITABLE(applywindow.add_entry), 0, -1);
738         if (new_tag) {
739                 gint id = tags_get_id_for_str(new_tag);
740                 if (id == -1) {
741                         id = tags_add_tag(new_tag);
742                         tags_write_tags();
743                         if (mainwindow_get_mainwindow())
744                                 summary_set_tag(
745                                         mainwindow_get_mainwindow()->summaryview, 
746                                         id, NULL);
747                         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
748                         apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
749                 } else {
750                         FindTagInStore fis;
751                         fis.tag_id = id;
752                         fis.path = NULL;
753                         gtk_tree_model_foreach(gtk_tree_view_get_model
754                                         (GTK_TREE_VIEW(applywindow.taglist)), 
755                                         (GtkTreeModelForeachFunc) find_tag_in_store,
756                                         &fis);
757                         if (fis.path) {
758                                 GtkTreeSelection *selection;
759                                 GtkTreePath* path;
760                                 GtkTreeModel *model = gtk_tree_view_get_model(
761                                         GTK_TREE_VIEW(applywindow.taglist));
762
763                                 if (mainwindow_get_mainwindow())
764                                         summary_set_tag(
765                                                 mainwindow_get_mainwindow()->summaryview, 
766                                                 id, NULL);
767                                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(applywindow.taglist));
768                                 gtk_tree_selection_select_iter(selection, &fis.iter);
769                                 path = gtk_tree_model_get_path(model, &fis.iter);
770                                 /* XXX returned path may not be valid??? create new one to be sure */ 
771                                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(applywindow.taglist), path, NULL, FALSE);
772                                 apply_window_list_view_insert_tag(applywindow.taglist, &fis.iter, id);
773                                 gtk_tree_path_free(path);
774                         }
775                 }
776                 g_free(new_tag);
777         }
778 }
779
780 static void apply_window_add_tag_cb(GtkWidget *widget,
781                                  gpointer data) 
782 {
783         apply_window_add_tag();
784         gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
785         gtk_widget_grab_focus(applywindow.taglist);
786 }
787
788 static gboolean apply_window_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
789 {
790         if (event && event->keyval == GDK_Escape)
791                 apply_window_close();
792         return FALSE;
793 }
794
795 static gboolean apply_window_add_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
796 {
797         if (event && (event->keyval == GDK_KP_Enter || event->keyval == GDK_Return)) {
798                 apply_window_add_tag();
799                 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
800                 gtk_widget_grab_focus(applywindow.taglist);
801         }
802                 
803         return FALSE;
804 }
805
806 static void apply_window_create(void) 
807 {
808         GtkWidget *window;
809         GtkWidget *hbox1;
810         GtkWidget *vbox1;
811         GtkWidget *label;
812         GtkWidget *taglist;
813         GtkWidget *close_btn;
814         GtkWidget *scrolledwin;
815         GtkWidget *new_tag_label;
816         GtkWidget *new_tag_entry;
817         GtkWidget *add_btn;
818
819         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "tag_apply_window");
820         gtk_window_set_title (GTK_WINDOW(window),
821                               Q_("Dialog title|Apply tags"));
822
823         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
824         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
825         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
826         g_signal_connect(G_OBJECT(window), "delete_event",
827                          G_CALLBACK(apply_window_close_cb), NULL);
828         g_signal_connect(G_OBJECT(window), "key_press_event",
829                          G_CALLBACK(apply_window_key_pressed), NULL);
830         MANAGE_WINDOW_SIGNALS_CONNECT (window);
831
832         vbox1 = gtk_vbox_new(FALSE, 6);
833         hbox1 = gtk_hbox_new(FALSE, 6);
834         
835         new_tag_label = gtk_label_new(_("New tag:"));
836         gtk_misc_set_alignment(GTK_MISC(new_tag_label), 0, 0.5);
837         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_label, FALSE, FALSE, 0);
838         
839         new_tag_entry = gtk_entry_new();
840         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_entry, FALSE, FALSE, 0);
841         g_signal_connect(G_OBJECT(new_tag_entry), "key_press_event",
842                          G_CALLBACK(apply_window_add_key_pressed), NULL);
843         
844         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
845         gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
846         
847         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
848         gtk_box_pack_end(GTK_BOX(hbox1), close_btn, FALSE, FALSE, 0);
849
850         gtk_widget_show(new_tag_label);
851         gtk_widget_show(new_tag_entry);
852         gtk_widget_show(close_btn);
853         gtk_widget_show(add_btn);
854         g_signal_connect(G_OBJECT(close_btn), "clicked",
855                          G_CALLBACK(apply_window_close_cb), NULL);
856         g_signal_connect(G_OBJECT(add_btn), "clicked",
857                          G_CALLBACK(apply_window_add_tag_cb), NULL);
858
859         taglist = apply_window_list_view_create();
860         
861         label = gtk_label_new(_("Please select tags to apply/remove. Changes are immediate."));
862         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
863         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
864         
865         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
866         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
867                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
868                                        
869         gtk_widget_set_size_request(scrolledwin, 500, 250);
870
871         gtk_container_add(GTK_CONTAINER(scrolledwin), taglist);
872         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
873         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
874         
875         gtk_widget_show(label);
876         gtk_widget_show(scrolledwin);
877         gtk_widget_show(taglist);
878         gtk_widget_show(hbox1);
879         gtk_widget_show(vbox1);
880         gtk_widget_show(close_btn);
881         gtk_container_add(GTK_CONTAINER (window), vbox1);
882
883         applywindow.window = window;
884         applywindow.hbox1 = hbox1;
885         applywindow.vbox1 = vbox1;
886         applywindow.label = label;
887         applywindow.taglist = taglist;
888         applywindow.close_btn = close_btn;
889         applywindow.add_btn = add_btn;
890         applywindow.add_entry = new_tag_entry;
891 }
892
893 static void apply_window_list_view_clear_tags(GtkWidget *list_view)
894 {
895         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
896                                         (GTK_TREE_VIEW(list_view)));
897         gtk_list_store_clear(list_store);
898 }
899
900
901 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
902                 gchar *path,
903                 GtkWidget *list_view)
904 {
905         GtkTreeIter iter;
906         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
907         gboolean enabled = TRUE, set = FALSE;
908         gpointer tmp;
909         gint tag_id;
910         SummaryView *summaryview = NULL;
911         
912         if (mainwindow_get_mainwindow() != NULL)
913                 summaryview = mainwindow_get_mainwindow()->summaryview;
914
915         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
916                 return;
917
918         gtk_tree_model_get(model, &iter,
919                            TAG_SELECTED, &enabled,
920                            TAG_DATA, &tmp,
921                            -1);
922
923         set = !enabled;
924         tag_id = GPOINTER_TO_INT(tmp);
925
926         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
927                            TAG_SELECTED, set,
928                            TAG_SELECTED_INCONSISTENT, FALSE,
929                            -1);
930         
931         if (summaryview)
932                 summary_set_tag(summaryview, set ? tag_id : -tag_id, NULL);
933 }
934
935 static void apply_window_get_selected_state(gint tag, gboolean *selected, gboolean *selected_inconsistent)
936 {
937         GSList *cur = applywindow.msglist;
938         gint num_mails = 0;
939         gint num_selected = 0;
940         for (; cur; cur = cur->next) {
941                 MsgInfo *msginfo = (MsgInfo *)cur->data;
942                 num_mails++;
943                 if (msginfo->tags && g_slist_find(msginfo->tags, GINT_TO_POINTER(tag))) {
944                         *selected = TRUE;
945                         num_selected++;
946                 }
947         }
948         if (num_selected > 0 && num_selected < num_mails)
949                 *selected_inconsistent = TRUE;
950         else
951                 *selected_inconsistent = FALSE;
952 }
953
954 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
955                                                   GtkTreeIter *row_iter,
956                                                   gint tag) 
957 {
958         GtkTreeIter iter;
959         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
960                                         (GTK_TREE_VIEW(list_view)));
961         const gchar *name = tags_get_tag(tag);
962         gboolean selected = FALSE, selected_inconsistent = FALSE;
963
964         apply_window_get_selected_state(tag, &selected, &selected_inconsistent);
965         if (row_iter == NULL) {
966                 /* append new */
967                 gtk_list_store_append(list_store, &iter);
968                 gtk_list_store_set(list_store, &iter,
969                                    TAG_SELECTED, selected,
970                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
971                                    TAG_NAME, name,
972                                    TAG_DATA, GINT_TO_POINTER(tag),
973                                    -1);
974         } else {
975                 gtk_list_store_set(list_store, row_iter,
976                                    TAG_SELECTED, selected,
977                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
978                                    TAG_NAME, name,
979                                    TAG_DATA, GINT_TO_POINTER(tag),
980                                    -1);
981         }
982 }
983
984 static void apply_window_load_tags (void) 
985 {
986         GSList *cur;
987         GSList *tags = tags_get_list();
988         apply_window_list_view_clear_tags(applywindow.taglist);
989         
990         cur = tags;
991         for (; cur; cur = cur->next) {
992                 gint id = GPOINTER_TO_INT(cur->data);
993                 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
994         }
995 }
996