Refactor colors handling with more arrays
[claws.git] / src / edittags.c
blobbd2cb914db79b7e8a2c34e2dbd9db4f1f5bbcc92
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2007-2013 The Claws Mail Team
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.
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.
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
36 #include "menu.h"
37 #include "edittags.h"
38 #include "prefs_gtk.h"
39 #include "utils.h"
40 #include "gtkutils.h"
41 #include "inputdialog.h"
42 #include "manage_window.h"
43 #include "mainwindow.h"
44 #include "prefs_common.h"
45 #include "alertpanel.h"
46 #include "summaryview.h"
47 #include "tags.h"
48 #include "gtkutils.h"
49 #include "manual.h"
51 enum {
52 TAG_SELECTED,
53 TAG_SELECTED_INCONSISTENT,
54 TAG_NAME,
55 TAG_DATA,
56 N_TAG_EDIT_COLUMNS
59 static gint tag_cmp_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
61 gchar *name1, *name2;
63 gtk_tree_model_get(model, a, TAG_NAME, &name1, -1);
64 gtk_tree_model_get(model, b, TAG_NAME, &name2, -1);
66 if (name1 == NULL)
67 return name2 == NULL ? 0:1;
69 if (name2 == NULL)
70 return 1;
72 return g_utf8_collate(name1,name2);
75 static void apply_window_create(void);
77 static struct TagApplyWindow
79 GtkWidget *window;
80 GtkWidget *hbox1;
81 GtkWidget *vbox1;
82 GtkWidget *label;
83 GtkWidget *taglist;
84 GtkWidget *close_btn;
85 GtkWidget *add_entry;
86 GtkWidget *add_btn;
87 GSList *msglist;
88 gboolean has_tag_col;
89 GdkCursor *watch_cursor;
90 gboolean busy;
91 } applywindow;
93 #define APPLYWINDOW_LOCK() { \
94 gdk_window_set_cursor(gtk_widget_get_window(applywindow.window), applywindow.watch_cursor); \
95 applywindow.busy = TRUE; \
98 #define APPLYWINDOW_UNLOCK() { \
99 gdk_window_set_cursor(gtk_widget_get_window(applywindow.window), NULL); \
100 applywindow.busy = FALSE; \
103 static void apply_window_load_tags (void);
104 static void apply_window_insert_check_column(GtkWidget *list_view);
106 void tag_apply_open(GSList *msglist)
108 if (!applywindow.window)
109 apply_window_create();
111 manage_window_set_transient(GTK_WINDOW(applywindow.window));
112 gtk_widget_grab_focus(applywindow.close_btn);
114 applywindow.msglist = msglist;
115 apply_window_load_tags();
117 if (msglist && !applywindow.has_tag_col) {
118 apply_window_insert_check_column(applywindow.taglist);
119 applywindow.has_tag_col = TRUE;
121 if (!msglist && applywindow.has_tag_col) {
122 gtk_tree_view_remove_column(GTK_TREE_VIEW(applywindow.taglist),
123 gtk_tree_view_get_column(GTK_TREE_VIEW(applywindow.taglist), 0));
124 applywindow.has_tag_col = FALSE;
127 gtk_widget_show(applywindow.window);
128 gtk_widget_grab_focus(applywindow.taglist);
129 gtk_window_set_modal(GTK_WINDOW(applywindow.window), TRUE);
132 static GtkListStore* apply_window_create_data_store(void)
134 GtkListStore *store = gtk_list_store_new(N_TAG_EDIT_COLUMNS,
135 G_TYPE_BOOLEAN,
136 G_TYPE_BOOLEAN,
137 G_TYPE_STRING,
138 G_TYPE_POINTER,
139 -1);
140 GtkTreeSortable *sortable = GTK_TREE_SORTABLE(store);
142 gtk_tree_sortable_set_sort_func(sortable, 0, tag_cmp_func,
143 NULL, NULL);
145 return store;
148 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
149 gchar *path,
150 GtkWidget *list_view);
151 static void tag_apply_selected_edited(GtkCellRendererText *widget,
152 gchar *arg1, gchar *arg2,
153 GtkWidget *list_view);
155 static void apply_window_insert_check_column(GtkWidget *list_view)
157 GtkTreeViewColumn *column;
158 GtkCellRenderer *renderer;
160 renderer = gtk_cell_renderer_toggle_new();
161 g_object_set(renderer,
162 "radio", FALSE,
163 "activatable", TRUE,
164 NULL);
165 column = gtk_tree_view_column_new_with_attributes
166 ("",
167 renderer,
168 "active", TAG_SELECTED,
169 "inconsistent", TAG_SELECTED_INCONSISTENT,
170 NULL);
171 gtk_tree_view_column_set_alignment (column, 0.5);
172 gtk_tree_view_insert_column(GTK_TREE_VIEW(list_view), column, 0);
173 g_signal_connect(G_OBJECT(renderer), "toggled",
174 G_CALLBACK(tag_apply_selected_toggled),
175 list_view);
178 static void apply_window_create_list_view_columns(GtkWidget *list_view)
180 GtkTreeViewColumn *column;
181 GtkCellRenderer *renderer;
183 renderer = gtk_cell_renderer_text_new();
184 g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
186 column = gtk_tree_view_column_new_with_attributes
187 (_("Tag"),
188 renderer,
189 "text", TAG_NAME,
190 NULL);
191 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
192 gtk_tree_view_column_set_resizable(column, TRUE);
193 gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view),
194 TAG_NAME);
195 g_signal_connect(G_OBJECT(renderer), "edited",
196 G_CALLBACK(tag_apply_selected_edited),
197 list_view);
201 static GtkWidget *apply_popup_menu = NULL;
203 static void apply_popup_delete (GtkAction *action, gpointer data)
205 GtkTreeIter sel;
206 GtkTreeModel *model;
207 gint id;
208 SummaryView *summaryview = NULL;
209 GtkTreeSelection *selection = gtk_tree_view_get_selection
210 (GTK_TREE_VIEW(applywindow.taglist));
211 model = gtk_tree_view_get_model(GTK_TREE_VIEW(applywindow.taglist));
213 if (!gtk_tree_selection_get_selected(selection, NULL, &sel))
214 return;
216 if (alertpanel(_("Delete tag"),
217 _("Do you really want to delete this tag?"),
218 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
219 return;
221 APPLYWINDOW_LOCK();
223 /* XXX: Here's the reason why we need to store the original
224 * pointer: we search the slist for it. */
225 gtk_tree_model_get(model, &sel,
226 TAG_DATA, &id,
227 -1);
229 /* Even though this is not documented, gtk_tree_model_get()
230 * seems to invalidate the GtkTreeIter that is passed to it,
231 * so we need to reacquire it. */
232 if (!gtk_tree_selection_get_selected(selection, NULL, &sel))
233 return;
235 gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
236 if (mainwindow_get_mainwindow() != NULL)
237 summaryview = mainwindow_get_mainwindow()->summaryview;
238 if (summaryview)
239 summary_set_tag(summaryview, -id, NULL);
240 tags_remove_tag(id);
241 tags_write_tags();
242 APPLYWINDOW_UNLOCK();
245 static void apply_popup_delete_all (GtkAction *action, gpointer data)
247 GSList *cur, *tags;
248 GtkTreeModel *model;
249 SummaryView *summaryview = NULL;
251 if (alertpanel(_("Delete all tags"),
252 _("Do you really want to delete all tags?"),
253 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL) != G_ALERTALTERNATE)
254 return;
256 APPLYWINDOW_LOCK();
258 model = gtk_tree_view_get_model(GTK_TREE_VIEW(applywindow.taglist));
259 gtk_list_store_clear(GTK_LIST_STORE(model));
261 if (mainwindow_get_mainwindow() != NULL)
262 summaryview = mainwindow_get_mainwindow()->summaryview;
263 cur = tags = tags_get_list();
264 for (; cur; cur = cur->next) {
265 gint id = GPOINTER_TO_INT(cur->data);
266 if (summaryview)
267 summary_set_tag(summaryview, -id, NULL);
268 tags_remove_tag(id);
270 g_slist_free(tags);
271 tags_write_tags();
273 APPLYWINDOW_UNLOCK();
276 static GtkActionEntry apply_popup_actions[] =
278 { "EditTags", NULL, "EditTags" },
279 { "EditTags/Delete", NULL, N_("_Delete"), NULL, "Delete tag", G_CALLBACK(apply_popup_delete) },
280 { "EditTags/DeleteAll", NULL, N_("Delete _all"), NULL, "Delete all tags", G_CALLBACK(apply_popup_delete_all) }
283 static gint apply_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
284 GtkTreeView *list_view)
286 GtkUIManager *gui_manager = gtkut_ui_manager();
288 if (applywindow.busy)
289 return FALSE;
291 if (event && event->button == 3) {
292 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
293 GtkTreeIter iter;
294 gboolean non_empty;
296 if (!apply_popup_menu) {
297 (void) cm_menu_create_action_group("EditTags", apply_popup_actions,
298 G_N_ELEMENTS(apply_popup_actions), (gpointer)list_view);
300 MENUITEM_ADDUI("/Menus", "EditTags", "EditTags", GTK_UI_MANAGER_MENU)
301 MENUITEM_ADDUI("/Menus/EditTags", "Delete", "EditTags/Delete",
302 GTK_UI_MANAGER_MENUITEM)
303 MENUITEM_ADDUI("/Menus/EditTags", "DeleteAll", "EditTags/DeleteAll",
304 GTK_UI_MANAGER_MENUITEM)
306 apply_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
307 gtk_ui_manager_get_widget(gui_manager, "/Menus/EditTags")) );
310 /* grey out popup menu items if list is empty */
311 non_empty = gtk_tree_model_get_iter_first(model, &iter);
312 cm_menu_set_sensitive("EditTags/Delete", non_empty);
313 cm_menu_set_sensitive("EditTags/DeleteAll", non_empty);
315 gtk_menu_popup(GTK_MENU(apply_popup_menu),
316 NULL, NULL, NULL, NULL,
317 event->button, event->time);
319 return FALSE;
321 return FALSE;
324 static gboolean apply_list_popup_menu(GtkWidget *widget, gpointer data)
326 GtkTreeView *list_view = (GtkTreeView *)data;
327 GdkEventButton event;
329 event.button = 3;
330 event.time = gtk_get_current_event_time();
332 apply_list_btn_pressed(NULL, &event, list_view);
334 return TRUE;
337 static GtkWidget *apply_window_list_view_create (void)
339 GtkTreeView *list_view;
340 GtkTreeSelection *selector;
341 GtkTreeModel *model;
343 model = GTK_TREE_MODEL(apply_window_create_data_store());
344 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
345 g_object_unref(model);
346 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model), TAG_NAME, GTK_SORT_ASCENDING);
348 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
350 selector = gtk_tree_view_get_selection(list_view);
351 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
353 /* create the columns */
354 apply_window_create_list_view_columns(GTK_WIDGET(list_view));
356 g_signal_connect(G_OBJECT(list_view), "popup-menu",
357 G_CALLBACK(apply_list_popup_menu), list_view);
358 g_signal_connect(G_OBJECT(list_view), "button-press-event",
359 G_CALLBACK(apply_list_btn_pressed), list_view);
360 return GTK_WIDGET(list_view);
364 static void apply_window_list_view_clear_tags(GtkWidget *list_view);
366 static void apply_window_close(void)
368 if (applywindow.busy)
369 return;
371 if (applywindow.msglist)
372 g_slist_free(applywindow.msglist);
373 applywindow.msglist = NULL;
374 APPLYWINDOW_LOCK();
375 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
376 APPLYWINDOW_UNLOCK();
377 apply_window_list_view_clear_tags(applywindow.taglist);
378 gtk_widget_hide(applywindow.window);
379 gtk_window_set_modal(GTK_WINDOW(applywindow.window), FALSE);
382 static void apply_window_close_cb(GtkWidget *widget,
383 gpointer data)
385 apply_window_close();
388 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
389 GtkTreeIter *row_iter,
390 gint tag);
392 typedef struct FindTagInStore {
393 gint tag_id;
394 GtkTreePath *path;
395 GtkTreeIter iter;
396 } FindTagInStore;
398 static gboolean find_tag_in_store(GtkTreeModel *model,
399 GtkTreePath *path,
400 GtkTreeIter *iter,
401 FindTagInStore *data)
403 gpointer tmp;
404 gtk_tree_model_get(model, iter, TAG_DATA, &tmp, -1);
406 if (data->tag_id == GPOINTER_TO_INT(tmp)) {
407 data->path = path; /* signal we found it */
408 data->iter = *iter;
409 return TRUE;
412 return FALSE;
415 static void apply_window_add_tag(void)
417 gchar *new_tag = gtk_editable_get_chars(GTK_EDITABLE(applywindow.add_entry), 0, -1);
418 g_strstrip(new_tag);
419 if (new_tag && *new_tag) {
420 gint id;
421 FindTagInStore fis;
422 if (!(IS_NOT_RESERVED_TAG(new_tag))) {
423 alertpanel_error(_("You entered a reserved tag name, please choose another instead."));
424 g_free(new_tag);
425 return;
427 id = tags_get_id_for_str(new_tag);
428 APPLYWINDOW_LOCK();
429 if (id == -1) {
430 id = tags_add_tag(new_tag);
431 tags_write_tags();
432 if (mainwindow_get_mainwindow())
433 summary_set_tag(
434 mainwindow_get_mainwindow()->summaryview,
435 id, NULL);
436 main_window_reflect_tags_changes(mainwindow_get_mainwindow());
437 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
439 fis.tag_id = id;
440 fis.path = NULL;
441 gtk_tree_model_foreach(gtk_tree_view_get_model
442 (GTK_TREE_VIEW(applywindow.taglist)),
443 (GtkTreeModelForeachFunc) find_tag_in_store,
444 &fis);
445 if (fis.path) {
446 GtkTreeSelection *selection;
447 GtkTreePath* path;
448 GtkTreeModel *model = gtk_tree_view_get_model(
449 GTK_TREE_VIEW(applywindow.taglist));
451 if (mainwindow_get_mainwindow())
452 summary_set_tag(
453 mainwindow_get_mainwindow()->summaryview,
454 id, NULL);
455 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(applywindow.taglist));
456 gtk_tree_selection_select_iter(selection, &fis.iter);
457 path = gtk_tree_model_get_path(model, &fis.iter);
458 /* XXX returned path may not be valid??? create new one to be sure */
459 gtk_tree_view_set_cursor(GTK_TREE_VIEW(applywindow.taglist), path, NULL, FALSE);
460 apply_window_list_view_insert_tag(applywindow.taglist, &fis.iter, id);
461 gtk_tree_path_free(path);
463 APPLYWINDOW_UNLOCK();
464 } else {
465 alertpanel_error(_("Tag is not set."));
467 g_free(new_tag);
470 static void apply_window_add_tag_cb(GtkWidget *widget,
471 gpointer data)
473 if (applywindow.busy)
474 return;
475 apply_window_add_tag();
476 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
477 gtk_widget_grab_focus(applywindow.taglist);
480 static void apply_window_del_tag_cb(GtkWidget *widget,
481 gpointer data)
483 if (applywindow.busy)
484 return;
485 apply_popup_delete(NULL, NULL);
486 gtk_widget_grab_focus(applywindow.taglist);
489 static gboolean apply_window_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
491 if (applywindow.busy)
492 return FALSE;
493 if (event && event->keyval == GDK_KEY_Escape)
494 apply_window_close();
495 else if (event && event->keyval == GDK_KEY_Delete)
496 apply_popup_delete(NULL, NULL);
497 return FALSE;
500 static gboolean apply_window_add_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
502 if (applywindow.busy)
503 return FALSE;
505 if (event && (event->keyval == GDK_KEY_KP_Enter || event->keyval == GDK_KEY_Return)) {
506 apply_window_add_tag();
507 gtk_entry_set_text(GTK_ENTRY(applywindow.add_entry), "");
508 gtk_widget_grab_focus(applywindow.taglist);
511 return FALSE;
514 static void apply_window_create(void)
516 GtkWidget *window;
517 GtkWidget *hbox1;
518 GtkWidget *vbox1;
519 GtkWidget *label;
520 GtkWidget *taglist;
521 GtkWidget *close_btn;
522 GtkWidget *scrolledwin;
523 GtkWidget *new_tag_label;
524 GtkWidget *new_tag_entry;
525 GtkWidget *add_btn;
526 GtkWidget *del_btn;
528 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "tag_apply_window");
529 gtk_window_set_title (GTK_WINDOW(window),
530 C_("Dialog title", "Apply tags"));
532 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
533 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
534 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
535 g_signal_connect(G_OBJECT(window), "delete_event",
536 G_CALLBACK(apply_window_close_cb), NULL);
537 g_signal_connect(G_OBJECT(window), "key_press_event",
538 G_CALLBACK(apply_window_key_pressed), NULL);
539 MANAGE_WINDOW_SIGNALS_CONNECT (window);
541 vbox1 = gtk_vbox_new(FALSE, 6);
542 hbox1 = gtk_hbox_new(FALSE, 6);
544 new_tag_label = gtk_label_new(_("New tag:"));
545 gtk_misc_set_alignment(GTK_MISC(new_tag_label), 0, 0.5);
546 gtk_box_pack_start(GTK_BOX(hbox1), new_tag_label, FALSE, FALSE, 0);
548 new_tag_entry = gtk_entry_new();
549 gtk_box_pack_start(GTK_BOX(hbox1), new_tag_entry, FALSE, FALSE, 0);
550 g_signal_connect(G_OBJECT(new_tag_entry), "key_press_event",
551 G_CALLBACK(apply_window_add_key_pressed), NULL);
553 add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
554 gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
556 del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
557 gtk_box_pack_start(GTK_BOX(hbox1), del_btn, FALSE, FALSE, 0);
559 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
560 gtk_box_pack_end(GTK_BOX(hbox1), close_btn, FALSE, FALSE, 0);
562 gtk_widget_show(new_tag_label);
563 gtk_widget_show(new_tag_entry);
564 gtk_widget_show(close_btn);
565 gtk_widget_show(add_btn);
566 gtk_widget_show(del_btn);
568 g_signal_connect(G_OBJECT(close_btn), "clicked",
569 G_CALLBACK(apply_window_close_cb), NULL);
570 g_signal_connect(G_OBJECT(add_btn), "clicked",
571 G_CALLBACK(apply_window_add_tag_cb), NULL);
572 g_signal_connect(G_OBJECT(del_btn), "clicked",
573 G_CALLBACK(apply_window_del_tag_cb), NULL);
575 taglist = apply_window_list_view_create();
577 label = gtk_label_new(_("Please select tags to apply/remove. Changes are immediate."));
578 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
579 gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
581 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
582 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
583 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
585 gtk_widget_set_size_request(scrolledwin, 500, 250);
587 gtk_container_add(GTK_CONTAINER(scrolledwin), taglist);
588 gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
589 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
591 gtk_widget_show(label);
592 gtk_widget_show(scrolledwin);
593 gtk_widget_show(taglist);
594 gtk_widget_show(hbox1);
595 gtk_widget_show(vbox1);
596 gtk_widget_show(close_btn);
597 gtk_container_add(GTK_CONTAINER (window), vbox1);
599 applywindow.window = window;
600 applywindow.hbox1 = hbox1;
601 applywindow.vbox1 = vbox1;
602 applywindow.label = label;
603 applywindow.taglist = taglist;
604 applywindow.close_btn = close_btn;
605 applywindow.add_btn = add_btn;
606 applywindow.add_entry = new_tag_entry;
607 applywindow.has_tag_col = FALSE;
608 applywindow.watch_cursor = gdk_cursor_new(GDK_WATCH);
611 static void apply_window_list_view_clear_tags(GtkWidget *list_view)
613 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
614 (GTK_TREE_VIEW(list_view)));
615 gtk_list_store_clear(list_store);
619 static void tag_apply_selected_toggled(GtkCellRendererToggle *widget,
620 gchar *path,
621 GtkWidget *list_view)
623 GtkTreeIter iter;
624 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
625 gboolean enabled = TRUE, set = FALSE;
626 gpointer tmp;
627 gint tag_id;
628 SummaryView *summaryview = NULL;
630 if (applywindow.busy)
631 return;
633 if (mainwindow_get_mainwindow() != NULL)
634 summaryview = mainwindow_get_mainwindow()->summaryview;
636 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
637 return;
639 gtk_tree_model_get(model, &iter,
640 TAG_SELECTED, &enabled,
641 TAG_DATA, &tmp,
642 -1);
644 set = !enabled;
645 tag_id = GPOINTER_TO_INT(tmp);
647 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
648 TAG_SELECTED, set,
649 TAG_SELECTED_INCONSISTENT, FALSE,
650 -1);
652 APPLYWINDOW_LOCK();
653 if (summaryview)
654 summary_set_tag(summaryview, set ? tag_id : -tag_id, NULL);
655 APPLYWINDOW_UNLOCK();
658 static void tag_apply_selected_edited(GtkCellRendererText *widget,
659 gchar *path, gchar *new_text,
660 GtkWidget *list_view)
662 GtkTreeIter iter;
663 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
664 gpointer tmp;
665 gint tag_id;
666 SummaryView *summaryview = NULL;
667 gboolean selected;
669 if (applywindow.busy)
670 return;
672 if (mainwindow_get_mainwindow() != NULL)
673 summaryview = mainwindow_get_mainwindow()->summaryview;
675 if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
676 return;
678 if (!new_text || !*new_text)
679 return;
681 gtk_tree_model_get(model, &iter,
682 TAG_SELECTED, &selected,
683 TAG_DATA, &tmp,
684 -1);
686 tag_id = GPOINTER_TO_INT(tmp);
688 APPLYWINDOW_LOCK();
689 if (selected) {
690 if (summaryview)
691 summary_set_tag(summaryview, -tag_id, NULL);
694 tags_update_tag(tag_id, new_text);
696 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
697 TAG_NAME, new_text,
698 -1);
699 if (selected) {
700 if (summaryview)
701 summary_set_tag(summaryview, tag_id, NULL);
702 } else {
703 if (summaryview)
704 summary_set_tag(summaryview, 0, NULL);
706 APPLYWINDOW_UNLOCK();
709 static void apply_window_get_selected_state(gint tag, gboolean *selected, gboolean *selected_inconsistent)
711 GSList *cur = applywindow.msglist;
712 gint num_mails = 0;
713 gint num_selected = 0;
714 for (; cur; cur = cur->next) {
715 MsgInfo *msginfo = (MsgInfo *)cur->data;
716 num_mails++;
717 if (msginfo->tags && g_slist_find(msginfo->tags, GINT_TO_POINTER(tag))) {
718 *selected = TRUE;
719 num_selected++;
722 if (num_selected > 0 && num_selected < num_mails)
723 *selected_inconsistent = TRUE;
724 else
725 *selected_inconsistent = FALSE;
728 static void apply_window_list_view_insert_tag(GtkWidget *list_view,
729 GtkTreeIter *row_iter,
730 gint tag)
732 GtkTreeIter iter;
733 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
734 (GTK_TREE_VIEW(list_view)));
735 const gchar *name = tags_get_tag(tag);
736 gboolean selected = FALSE, selected_inconsistent = FALSE;
738 apply_window_get_selected_state(tag, &selected, &selected_inconsistent);
739 if (row_iter == NULL) {
740 /* append new */
741 gtk_list_store_append(list_store, &iter);
742 gtk_list_store_set(list_store, &iter,
743 TAG_SELECTED, selected,
744 TAG_SELECTED_INCONSISTENT, selected_inconsistent,
745 TAG_NAME, name,
746 TAG_DATA, GINT_TO_POINTER(tag),
747 -1);
748 } else {
749 gtk_list_store_set(list_store, row_iter,
750 TAG_SELECTED, selected,
751 TAG_SELECTED_INCONSISTENT, selected_inconsistent,
752 TAG_NAME, name,
753 TAG_DATA, GINT_TO_POINTER(tag),
754 -1);
758 static void apply_window_load_tags (void)
760 GSList *cur, *tags;
761 gint id;
762 apply_window_list_view_clear_tags(applywindow.taglist);
764 cur = tags = tags_get_list();
765 for (; cur; cur = cur->next) {
766 id = GPOINTER_TO_INT(cur->data);
767 apply_window_list_view_insert_tag(applywindow.taglist, NULL, id);
769 g_slist_free(tags);