2007-09-02 [colin] 2.10.0cvs190
[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         g_strstrip(tag);
338         if (tag[0] == '\0') {
339                 alertpanel_error(_("Tag is not set."));
340                 return -1;
341         }
342
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         g_strstrip(new_tag);
569         id = tags_get_id_for_str(new_tag);
570         if (id != -1) {
571                 g_free(new_tag);
572                 return id;
573         }
574         id = tags_add_tag(new_tag);
575         g_free(new_tag);
576         
577         tags_write_tags();
578         return id;
579         
580 }
581
582 enum {
583         TAG_SELECTED,
584         TAG_SELECTED_INCONSISTENT,
585         TAG_NAME,
586         TAG_DATA,
587         N_TAG_EDIT_COLUMNS
588 };
589
590 static void apply_window_create(void);
591
592 static struct TagApplyWindow
593 {
594         GtkWidget *window;
595         GtkWidget *hbox1;
596         GtkWidget *vbox1;
597         GtkWidget *label;
598         GtkWidget *taglist;
599         GtkWidget *close_btn;
600         GtkWidget *add_entry;
601         GtkWidget *add_btn;
602         GSList *msglist;
603 } applywindow;
604
605 static void apply_window_load_tags (void);
606
607 void tag_apply_open(GSList *msglist)
608 {
609         g_return_if_fail(msglist);
610
611         if (!applywindow.window)
612                 apply_window_create();
613
614         manage_window_set_transient(GTK_WINDOW(applywindow.window));
615         gtk_widget_grab_focus(applywindow.close_btn);
616         
617         applywindow.msglist = msglist;
618         apply_window_load_tags();
619
620         gtk_widget_show(applywindow.window);
621         gtk_widget_grab_focus(applywindow.taglist);
622         gtk_window_set_modal(GTK_WINDOW(applywindow.window), TRUE);
623 }
624
625 static GtkListStore* apply_window_create_data_store(void)
626 {
627         return gtk_list_store_new(N_TAG_EDIT_COLUMNS,
628                                   G_TYPE_BOOLEAN,
629                                   G_TYPE_BOOLEAN,
630                                   G_TYPE_STRING,
631                                   G_TYPE_POINTER,
632                                   -1);
633 }
634
635 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
636                 gchar *path,
637                 GtkWidget *list_view);
638
639 static void apply_window_create_list_view_columns(GtkWidget *list_view)
640 {
641         GtkTreeViewColumn *column;
642         GtkCellRenderer *renderer;
643
644         renderer = gtk_cell_renderer_toggle_new();
645         g_object_set(renderer,
646                      "radio", FALSE,
647                      "activatable", TRUE,
648                      NULL);
649         column = gtk_tree_view_column_new_with_attributes
650                 ("",
651                  renderer,
652                  "active", TAG_SELECTED,
653                  "inconsistent", TAG_SELECTED_INCONSISTENT,
654                  NULL);
655         gtk_tree_view_column_set_alignment (column, 0.5);
656         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
657         g_signal_connect(G_OBJECT(renderer), "toggled",
658                          G_CALLBACK(tag_apply_selected_toggled),
659                          list_view);
660
661         renderer = gtk_cell_renderer_text_new();
662         column = gtk_tree_view_column_new_with_attributes
663                 (_("Tag"),
664                  renderer,
665                  "text", TAG_NAME,
666                  NULL);
667         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
668         gtk_tree_view_column_set_resizable(column, TRUE);
669
670         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view),
671                                         TAG_NAME);
672 }
673
674 static GtkWidget *apply_window_list_view_create (void)
675 {
676         GtkTreeView *list_view;
677         GtkTreeSelection *selector;
678         GtkTreeModel *model;
679
680         model = GTK_TREE_MODEL(apply_window_create_data_store());
681         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
682         g_object_unref(model);  
683         
684         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
685         
686         selector = gtk_tree_view_get_selection(list_view);
687         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
688
689         /* create the columns */
690         apply_window_create_list_view_columns(GTK_WIDGET(list_view));
691
692         return GTK_WIDGET(list_view);
693
694 }
695
696 static void apply_window_close(void) 
697 {
698         g_slist_free(applywindow.msglist);
699         applywindow.msglist = NULL;
700         gtk_widget_hide(applywindow.window);
701 }
702
703 static void apply_window_close_cb(GtkWidget *widget,
704                                  gpointer data) 
705 {
706         apply_window_close();
707 }
708
709 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
710                                                   GtkTreeIter *row_iter,
711                                                   gint tag);
712
713 typedef struct FindTagInStore {
714         gint             tag_id;
715         GtkTreePath     *path;
716         GtkTreeIter      iter;
717 } FindTagInStore;
718
719 static gboolean find_tag_in_store(GtkTreeModel *model,
720                                       GtkTreePath  *path,
721                                       GtkTreeIter  *iter,
722                                       FindTagInStore *data)
723 {
724         gpointer tmp;
725         gtk_tree_model_get(model, iter, TAG_DATA, &tmp, -1);
726
727         if (data->tag_id == GPOINTER_TO_INT(tmp)) {
728                 data->path = path; /* signal we found it */
729                 data->iter = *iter;
730                 return TRUE;
731         }
732
733         return FALSE; 
734 }
735
736 static void apply_window_add_tag(void)
737 {
738         gchar *new_tag = gtk_editable_get_chars(GTK_EDITABLE(applywindow.add_entry), 0, -1);
739         g_strstrip(new_tag);
740         if (new_tag && *new_tag) {
741                 gint id = tags_get_id_for_str(new_tag);
742                 if (id == -1) {
743                         id = tags_add_tag(new_tag);
744                         tags_write_tags();
745                         if (mainwindow_get_mainwindow())
746                                 summary_set_tag(
747                                         mainwindow_get_mainwindow()->summaryview, 
748                                         id, NULL);
749                         main_window_reflect_tags_changes(mainwindow_get_mainwindow());
750                         apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
751                 } else {
752                         FindTagInStore fis;
753                         fis.tag_id = id;
754                         fis.path = NULL;
755                         gtk_tree_model_foreach(gtk_tree_view_get_model
756                                         (GTK_TREE_VIEW(applywindow.taglist)), 
757                                         (GtkTreeModelForeachFunc) find_tag_in_store,
758                                         &fis);
759                         if (fis.path) {
760                                 GtkTreeSelection *selection;
761                                 GtkTreePath* path;
762                                 GtkTreeModel *model = gtk_tree_view_get_model(
763                                         GTK_TREE_VIEW(applywindow.taglist));
764
765                                 if (mainwindow_get_mainwindow())
766                                         summary_set_tag(
767                                                 mainwindow_get_mainwindow()->summaryview, 
768                                                 id, NULL);
769                                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(applywindow.taglist));
770                                 gtk_tree_selection_select_iter(selection, &fis.iter);
771                                 path = gtk_tree_model_get_path(model, &fis.iter);
772                                 /* XXX returned path may not be valid??? create new one to be sure */ 
773                                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(applywindow.taglist), path, NULL, FALSE);
774                                 apply_window_list_view_insert_tag(applywindow.taglist, &fis.iter, id);
775                                 gtk_tree_path_free(path);
776                         }
777                 }
778                 g_free(new_tag);
779         } else {
780                 alertpanel_error(_("Tag is not set."));
781         }
782 }
783
784 static void apply_window_add_tag_cb(GtkWidget *widget,
785                                  gpointer data) 
786 {
787         apply_window_add_tag();
788         gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
789         gtk_widget_grab_focus(applywindow.taglist);
790 }
791
792 static gboolean apply_window_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
793 {
794         if (event && event->keyval == GDK_Escape)
795                 apply_window_close();
796         return FALSE;
797 }
798
799 static gboolean apply_window_add_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
800 {
801         if (event && (event->keyval == GDK_KP_Enter || event->keyval == GDK_Return)) {
802                 apply_window_add_tag();
803                 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
804                 gtk_widget_grab_focus(applywindow.taglist);
805         }
806                 
807         return FALSE;
808 }
809
810 static void apply_window_create(void) 
811 {
812         GtkWidget *window;
813         GtkWidget *hbox1;
814         GtkWidget *vbox1;
815         GtkWidget *label;
816         GtkWidget *taglist;
817         GtkWidget *close_btn;
818         GtkWidget *scrolledwin;
819         GtkWidget *new_tag_label;
820         GtkWidget *new_tag_entry;
821         GtkWidget *add_btn;
822
823         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "tag_apply_window");
824         gtk_window_set_title (GTK_WINDOW(window),
825                               Q_("Dialog title|Apply tags"));
826
827         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
828         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
829         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
830         g_signal_connect(G_OBJECT(window), "delete_event",
831                          G_CALLBACK(apply_window_close_cb), NULL);
832         g_signal_connect(G_OBJECT(window), "key_press_event",
833                          G_CALLBACK(apply_window_key_pressed), NULL);
834         MANAGE_WINDOW_SIGNALS_CONNECT (window);
835
836         vbox1 = gtk_vbox_new(FALSE, 6);
837         hbox1 = gtk_hbox_new(FALSE, 6);
838         
839         new_tag_label = gtk_label_new(_("New tag:"));
840         gtk_misc_set_alignment(GTK_MISC(new_tag_label), 0, 0.5);
841         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_label, FALSE, FALSE, 0);
842         
843         new_tag_entry = gtk_entry_new();
844         gtk_box_pack_start(GTK_BOX(hbox1), new_tag_entry, FALSE, FALSE, 0);
845         g_signal_connect(G_OBJECT(new_tag_entry), "key_press_event",
846                          G_CALLBACK(apply_window_add_key_pressed), NULL);
847         
848         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
849         gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
850         
851         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
852         gtk_box_pack_end(GTK_BOX(hbox1), close_btn, FALSE, FALSE, 0);
853
854         gtk_widget_show(new_tag_label);
855         gtk_widget_show(new_tag_entry);
856         gtk_widget_show(close_btn);
857         gtk_widget_show(add_btn);
858         g_signal_connect(G_OBJECT(close_btn), "clicked",
859                          G_CALLBACK(apply_window_close_cb), NULL);
860         g_signal_connect(G_OBJECT(add_btn), "clicked",
861                          G_CALLBACK(apply_window_add_tag_cb), NULL);
862
863         taglist = apply_window_list_view_create();
864         
865         label = gtk_label_new(_("Please select tags to apply/remove. Changes are immediate."));
866         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
867         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
868         
869         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
870         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
871                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
872                                        
873         gtk_widget_set_size_request(scrolledwin, 500, 250);
874
875         gtk_container_add(GTK_CONTAINER(scrolledwin), taglist);
876         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
877         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
878         
879         gtk_widget_show(label);
880         gtk_widget_show(scrolledwin);
881         gtk_widget_show(taglist);
882         gtk_widget_show(hbox1);
883         gtk_widget_show(vbox1);
884         gtk_widget_show(close_btn);
885         gtk_container_add(GTK_CONTAINER (window), vbox1);
886
887         applywindow.window = window;
888         applywindow.hbox1 = hbox1;
889         applywindow.vbox1 = vbox1;
890         applywindow.label = label;
891         applywindow.taglist = taglist;
892         applywindow.close_btn = close_btn;
893         applywindow.add_btn = add_btn;
894         applywindow.add_entry = new_tag_entry;
895 }
896
897 static void apply_window_list_view_clear_tags(GtkWidget *list_view)
898 {
899         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
900                                         (GTK_TREE_VIEW(list_view)));
901         gtk_list_store_clear(list_store);
902 }
903
904
905 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
906                 gchar *path,
907                 GtkWidget *list_view)
908 {
909         GtkTreeIter iter;
910         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
911         gboolean enabled = TRUE, set = FALSE;
912         gpointer tmp;
913         gint tag_id;
914         SummaryView *summaryview = NULL;
915         
916         if (mainwindow_get_mainwindow() != NULL)
917                 summaryview = mainwindow_get_mainwindow()->summaryview;
918
919         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
920                 return;
921
922         gtk_tree_model_get(model, &iter,
923                            TAG_SELECTED, &enabled,
924                            TAG_DATA, &tmp,
925                            -1);
926
927         set = !enabled;
928         tag_id = GPOINTER_TO_INT(tmp);
929
930         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
931                            TAG_SELECTED, set,
932                            TAG_SELECTED_INCONSISTENT, FALSE,
933                            -1);
934         
935         if (summaryview)
936                 summary_set_tag(summaryview, set ? tag_id : -tag_id, NULL);
937 }
938
939 static void apply_window_get_selected_state(gint tag, gboolean *selected, gboolean *selected_inconsistent)
940 {
941         GSList *cur = applywindow.msglist;
942         gint num_mails = 0;
943         gint num_selected = 0;
944         for (; cur; cur = cur->next) {
945                 MsgInfo *msginfo = (MsgInfo *)cur->data;
946                 num_mails++;
947                 if (msginfo->tags && g_slist_find(msginfo->tags, GINT_TO_POINTER(tag))) {
948                         *selected = TRUE;
949                         num_selected++;
950                 }
951         }
952         if (num_selected > 0 && num_selected < num_mails)
953                 *selected_inconsistent = TRUE;
954         else
955                 *selected_inconsistent = FALSE;
956 }
957
958 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
959                                                   GtkTreeIter *row_iter,
960                                                   gint tag) 
961 {
962         GtkTreeIter iter;
963         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
964                                         (GTK_TREE_VIEW(list_view)));
965         const gchar *name = tags_get_tag(tag);
966         gboolean selected = FALSE, selected_inconsistent = FALSE;
967
968         apply_window_get_selected_state(tag, &selected, &selected_inconsistent);
969         if (row_iter == NULL) {
970                 /* append new */
971                 gtk_list_store_append(list_store, &iter);
972                 gtk_list_store_set(list_store, &iter,
973                                    TAG_SELECTED, selected,
974                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
975                                    TAG_NAME, name,
976                                    TAG_DATA, GINT_TO_POINTER(tag),
977                                    -1);
978         } else {
979                 gtk_list_store_set(list_store, row_iter,
980                                    TAG_SELECTED, selected,
981                                    TAG_SELECTED_INCONSISTENT, selected_inconsistent,
982                                    TAG_NAME, name,
983                                    TAG_DATA, GINT_TO_POINTER(tag),
984                                    -1);
985         }
986 }
987
988 static void apply_window_load_tags (void) 
989 {
990         GSList *cur;
991         GSList *tags = tags_get_list();
992         apply_window_list_view_clear_tags(applywindow.taglist);
993         
994         cur = tags;
995         for (; cur; cur = cur->next) {
996                 gint id = GPOINTER_TO_INT(cur->data);
997                 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
998         }
999 }
1000