a63891cb354e66866f61e55a2e00641afd83bb07
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkscrolledwindow.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtkpixmap.h>
27 #include <gtk/gtkctree.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtktext.h>
31 #include <gtk/gtkmenu.h>
32 #include <gtk/gtkmenuitem.h>
33 #include <gtk/gtkitemfactory.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtkwindow.h>
37 #include <gtk/gtkstyle.h>
38 #include <gtk/gtkarrow.h>
39 #include <gtk/gtkeventbox.h>
40 #include <gtk/gtkstatusbar.h>
41 #include <gtk/gtkmenuitem.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <unistd.h>
48 #include <sys/stat.h>
49
50 #include "intl.h"
51 #include "main.h"
52 #include "menu.h"
53 #include "mainwindow.h"
54 #include "folderview.h"
55 #include "summaryview.h"
56 #include "messageview.h"
57 #include "foldersel.h"
58 #include "procmsg.h"
59 #include "procheader.h"
60 #include "headerwindow.h"
61 #include "sourcewindow.h"
62 #include "prefs_common.h"
63 #include "prefs_summary_column.h"
64 #include "account.h"
65 #include "compose.h"
66 #include "utils.h"
67 #include "gtkutils.h"
68 #include "filesel.h"
69 #include "manage_window.h"
70 #include "alertpanel.h"
71 #include "inputdialog.h"
72 #include "statusbar.h"
73 #include "filter.h"
74 #include "folder.h"
75 #include "colorlabel.h"
76 #include "inc.h"
77 #include "addressbook.h"
78 #include "addr_compl.h"
79 #include "scoring.h"
80 #include "prefs_folder_item.h"
81 #include "filtering.h"
82
83 #include "pixmaps/dir-open.xpm"
84 #include "pixmaps/mark.xpm"
85 #include "pixmaps/deleted.xpm"
86 #include "pixmaps/new.xpm"
87 #include "pixmaps/unread.xpm"
88 #include "pixmaps/replied.xpm"
89 #include "pixmaps/forwarded.xpm"
90 #include "pixmaps/clip.xpm"
91 #include "pixmaps/ignorethread.xpm"
92
93 #define STATUSBAR_PUSH(mainwin, str) \
94 { \
95         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
96                            mainwin->summaryview_cid, str); \
97         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
98 }
99
100 #define STATUSBAR_POP(mainwin) \
101 { \
102         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
103                           mainwin->summaryview_cid); \
104 }
105
106 #define SUMMARY_COL_MARK_WIDTH          10
107 #define SUMMARY_COL_UNREAD_WIDTH        13
108 #define SUMMARY_COL_MIME_WIDTH          10
109
110 static GdkFont *boldfont;
111 static GdkFont *smallfont;
112
113 static GtkStyle *bold_style;
114 static GtkStyle *bold_marked_style;
115 static GtkStyle *bold_deleted_style;
116 static GtkStyle *small_style;
117 static GtkStyle *small_marked_style;
118 static GtkStyle *small_deleted_style;
119
120 static GdkPixmap *folderxpm;
121 static GdkBitmap *folderxpmmask;
122
123 static GdkPixmap *markxpm;
124 static GdkBitmap *markxpmmask;
125 static GdkPixmap *deletedxpm;
126 static GdkBitmap *deletedxpmmask;
127
128 static GdkPixmap *newxpm;
129 static GdkBitmap *newxpmmask;
130 static GdkPixmap *unreadxpm;
131 static GdkBitmap *unreadxpmmask;
132 static GdkPixmap *repliedxpm;
133 static GdkBitmap *repliedxpmmask;
134 static GdkPixmap *forwardedxpm;
135 static GdkBitmap *forwardedxpmmask;
136 static GdkPixmap *ignorethreadxpm;
137 static GdkBitmap *ignorethreadxpmmask;
138
139 static GdkPixmap *clipxpm;
140 static GdkBitmap *clipxpmmask;
141
142 static void summary_free_msginfo_func   (GtkCTree               *ctree,
143                                          GtkCTreeNode           *node,
144                                          gpointer                data);
145 static void summary_set_marks_func      (GtkCTree               *ctree,
146                                          GtkCTreeNode           *node,
147                                          gpointer                data);
148 static void summary_write_cache_func    (GtkCTree               *ctree,
149                                          GtkCTreeNode           *node,
150                                          gpointer                data);
151
152 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
153
154 static guint summary_get_msgnum         (SummaryView            *summaryview,
155                                          GtkCTreeNode           *node);
156
157 static GtkCTreeNode *summary_find_prev_msg
158                                         (SummaryView            *summaryview,
159                                          GtkCTreeNode           *current_node);
160 static GtkCTreeNode *summary_find_next_msg
161                                         (SummaryView            *summaryview,
162                                          GtkCTreeNode           *current_node);
163 static GtkCTreeNode *summary_find_prev_unread_msg
164                                         (SummaryView            *summaryview,
165                                          GtkCTreeNode           *current_node);
166 static GtkCTreeNode *summary_find_next_unread_msg
167                                         (SummaryView            *summaryview,
168                                          GtkCTreeNode           *current_node);
169 static GtkCTreeNode *summary_find_prev_marked_msg
170                                         (SummaryView            *summaryview,
171                                          GtkCTreeNode           *current_node);
172 static GtkCTreeNode *summary_find_next_marked_msg
173                                         (SummaryView            *summaryview,
174                                          GtkCTreeNode           *current_node);
175 static GtkCTreeNode *summary_find_prev_labeled_msg
176                                         (SummaryView            *summaryview,
177                                          GtkCTreeNode           *current_node);
178 static GtkCTreeNode *summary_find_next_labeled_msg
179                                         (SummaryView            *summaryview,
180                                          GtkCTreeNode           *current_node);
181 static GtkCTreeNode *summary_find_msg_by_msgnum
182                                         (SummaryView            *summaryview,
183                                          guint                   msgnum);
184
185 static void summary_update_status       (SummaryView            *summaryview);
186
187 /* display functions */
188 static void summary_status_show         (SummaryView            *summaryview);
189 static void summary_set_ctree_from_list (SummaryView            *summaryview,
190                                          GSList                 *mlist);
191 static void summary_set_header          (SummaryView            *summaryview,
192                                          gchar                  *text[],
193                                          MsgInfo                *msginfo);
194 static void summary_display_msg         (SummaryView            *summaryview,
195                                          GtkCTreeNode           *row,
196                                          gboolean                new_window);
197 static void summary_toggle_view         (SummaryView            *summaryview);
198 static void summary_set_row_marks       (SummaryView            *summaryview,
199                                          GtkCTreeNode           *row);
200
201 /* message handling */
202 static void summary_mark_row            (SummaryView            *summaryview,
203                                          GtkCTreeNode           *row);
204 static void summary_mark_row_as_read    (SummaryView            *summaryview,
205                                          GtkCTreeNode           *row);
206 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
207                                          GtkCTreeNode           *row);
208 static void summary_delete_row          (SummaryView            *summaryview,
209                                          GtkCTreeNode           *row);
210 static void summary_unmark_row          (SummaryView            *summaryview,
211                                          GtkCTreeNode           *row);
212 static void summary_move_row_to         (SummaryView            *summaryview,
213                                          GtkCTreeNode           *row,
214                                          FolderItem             *to_folder);
215 static void summary_copy_row_to         (SummaryView            *summaryview,
216                                          GtkCTreeNode           *row,
217                                          FolderItem             *to_folder);
218
219 static void summary_delete_duplicated_func
220                                         (GtkCTree               *ctree,
221                                          GtkCTreeNode           *node,
222                                          SummaryView            *summaryview);
223
224 static void summary_execute_move        (SummaryView            *summaryview);
225 static void summary_execute_move_func   (GtkCTree               *ctree,
226                                          GtkCTreeNode           *node,
227                                          gpointer                data);
228 static void summary_execute_copy        (SummaryView            *summaryview);
229 static void summary_execute_copy_func   (GtkCTree               *ctree,
230                                          GtkCTreeNode           *node,
231                                          gpointer                data);
232 static void summary_execute_delete      (SummaryView            *summaryview);
233 static void summary_execute_delete_func (GtkCTree               *ctree,
234                                          GtkCTreeNode           *node,
235                                          gpointer                data);
236
237 static void summary_thread_init         (SummaryView            *summaryview);
238 static void summary_ignore_thread       (SummaryView  *summaryview);
239 static void summary_unignore_thread     (SummaryView *summaryview);
240
241 static void summary_unthread_for_exec           (SummaryView    *summaryview);
242 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
243                                                  GtkCTreeNode   *node,
244                                                  gpointer        data);
245
246 static void summary_filter_func         (GtkCTree               *ctree,
247                                          GtkCTreeNode           *node,
248                                          gpointer                data);
249
250 static void summary_colorlabel_menu_item_activate_cb
251                                           (GtkWidget    *widget,
252                                            gpointer      data);
253 static void summary_colorlabel_menu_item_activate_item_cb
254                                           (GtkMenuItem  *label_menu_item,
255                                            gpointer      data);
256 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
257
258 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
259
260 /* callback functions */
261 static void summary_toggle_pressed      (GtkWidget              *eventbox,
262                                          GdkEventButton         *event,
263                                          SummaryView            *summaryview);
264 static void summary_button_pressed      (GtkWidget              *ctree,
265                                          GdkEventButton         *event,
266                                          SummaryView            *summaryview);
267 static void summary_button_released     (GtkWidget              *ctree,
268                                          GdkEventButton         *event,
269                                          SummaryView            *summaryview);
270 static void summary_key_pressed         (GtkWidget              *ctree,
271                                          GdkEventKey            *event,
272                                          SummaryView            *summaryview);
273 static void summary_open_row            (GtkSCTree              *sctree,
274                                          SummaryView            *summaryview);
275 static void summary_tree_expanded       (GtkCTree               *ctree,
276                                          GtkCTreeNode           *node,
277                                          SummaryView            *summaryview);
278 static void summary_tree_collapsed      (GtkCTree               *ctree,
279                                          GtkCTreeNode           *node,
280                                          SummaryView            *summaryview);
281 static void summary_selected            (GtkCTree               *ctree,
282                                          GtkCTreeNode           *row,
283                                          gint                    column,
284                                          SummaryView            *summaryview);
285 static void summary_col_resized         (GtkCList               *clist,
286                                          gint                    column,
287                                          gint                    width,
288                                          SummaryView            *summaryview);
289 static void summary_reply_cb            (SummaryView            *summaryview,
290                                          guint                   action,
291                                          GtkWidget              *widget);
292 static void summary_execute_cb          (SummaryView            *summaryview,
293                                          guint                   action,
294                                          GtkWidget              *widget);
295 static void summary_show_all_header_cb  (SummaryView            *summaryview,
296                                          guint                   action,
297                                          GtkWidget              *widget);
298
299 static void summary_add_address_cb      (SummaryView            *summaryview,
300                                          guint                   action,
301                                          GtkWidget              *widget);
302
303 static void summary_mark_clicked        (GtkWidget              *button,
304                                          SummaryView            *summaryview);
305 static void summary_unread_clicked      (GtkWidget              *button,
306                                          SummaryView            *summaryview);
307 static void summary_mime_clicked        (GtkWidget              *button,
308                                          SummaryView            *summaryview);
309 static void summary_num_clicked         (GtkWidget              *button,
310                                          SummaryView            *summaryview);
311 static void summary_score_clicked       (GtkWidget *button,
312                                          SummaryView *summaryview);
313 static void summary_size_clicked        (GtkWidget              *button,
314                                          SummaryView            *summaryview);
315 static void summary_date_clicked        (GtkWidget              *button,
316                                          SummaryView            *summaryview);
317 static void summary_from_clicked        (GtkWidget              *button,
318                                          SummaryView            *summaryview);
319 static void summary_subject_clicked     (GtkWidget              *button,
320                                          SummaryView            *summaryview);
321 static void summary_score_clicked       (GtkWidget              *button,
322                                          SummaryView            *summaryview);
323
324 static void summary_start_drag          (GtkWidget        *widget, 
325                                          int button,
326                                          GdkEvent *event,
327                                          SummaryView      *summaryview);
328 static void summary_drag_data_get       (GtkWidget        *widget,
329                                          GdkDragContext   *drag_context,
330                                          GtkSelectionData *selection_data,
331                                          guint             info,
332                                          guint             time,
333                                          SummaryView      *summaryview);
334
335 /* custom compare functions for sorting */
336
337 static gint summary_cmp_by_mark         (GtkCList               *clist,
338                                          gconstpointer           ptr1,
339                                          gconstpointer           ptr2);
340 static gint summary_cmp_by_unread       (GtkCList               *clist,
341                                          gconstpointer           ptr1,
342                                          gconstpointer           ptr2);
343 static gint summary_cmp_by_mime         (GtkCList               *clist,
344                                          gconstpointer           ptr1,
345                                          gconstpointer           ptr2);
346 static gint summary_cmp_by_num          (GtkCList               *clist,
347                                          gconstpointer           ptr1,
348                                          gconstpointer           ptr2);
349 static gint summary_cmp_by_size         (GtkCList               *clist,
350                                          gconstpointer           ptr1,
351                                          gconstpointer           ptr2);
352 static gint summary_cmp_by_date         (GtkCList               *clist,
353                                          gconstpointer           ptr1,
354                                          gconstpointer           ptr2);
355 static gint summary_cmp_by_from         (GtkCList               *clist,
356                                          gconstpointer           ptr1,
357                                          gconstpointer           ptr2);
358 static gint summary_cmp_by_subject      (GtkCList               *clist,
359                                          gconstpointer           ptr1,
360                                          gconstpointer           ptr2);
361 static gint summary_cmp_by_score        (GtkCList               *clist,
362                                          gconstpointer           ptr1,
363                                          gconstpointer           ptr2);
364 static gint summary_cmp_by_label        (GtkCList               *clist,
365                                          gconstpointer           ptr1,
366                                          gconstpointer           ptr2);
367
368 #if MARK_ALL_READ
369 static void summary_mark_all_read (SummaryView *summaryview);                                    
370 #endif
371
372 GtkTargetEntry summary_drag_types[1] =
373 {
374         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
375 };
376
377 static GtkItemFactoryEntry summary_popup_entries[] =
378 {
379         {N_("/_Reply"),                 NULL, summary_reply_cb, COMPOSE_REPLY, NULL},
380         {N_("/Repl_y to sender"),       NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
381         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
382         {N_("/Reply to a_ll"),          NULL, summary_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
383         {N_("/_Forward"),               NULL, summary_reply_cb, COMPOSE_FORWARD, NULL},
384         {N_("/Forward as a_ttachment"),
385                                         NULL, summary_reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
386         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
387         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
388         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
389         {N_("/M_ove..."),               NULL, summary_move_to,  0, NULL},
390         {N_("/_Copy..."),               NULL, summary_copy_to,  0, NULL},
391         {N_("/_Delete"),                NULL, summary_delete,   0, NULL},
392         {N_("/E_xecute"),               NULL, summary_execute_cb,       0, NULL},
393         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
394         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
395         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
396         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
397         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
398         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
399         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
400         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
401         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
402         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
403         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
404
405         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
406         {N_("/Add sender to address boo_k"),
407                                         NULL, summary_add_address_cb, 0, NULL},
408         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
409         {N_("/_View"),                  NULL, NULL,             0, "<Branch>"},
410         {N_("/_View/Open in new _window"),
411                                         NULL, summary_open_msg, 0, NULL},
412         {N_("/_View/_Source"),          NULL, summary_view_source, 0, NULL},
413         {N_("/_View/All _header"),      NULL, summary_show_all_header_cb, 0, NULL},
414         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
415         {N_("/_Save as..."),            NULL, summary_save_as,  0, NULL},
416         {N_("/_Print..."),              NULL, summary_print,    0, NULL},
417         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
418         {N_("/Select _all"),            NULL, summary_select_all, 0, NULL}
419 };
420
421 static const gchar *const col_label[N_SUMMARY_COLS] = {
422         N_("M"),        /* S_COL_MARK    */
423         N_("U"),        /* S_COL_UNREAD  */
424         "",             /* S_COL_MIME    */
425         N_("Subject"),  /* S_COL_SUBJECT */
426         N_("From"),     /* S_COL_FROM    */
427         N_("Date"),     /* S_COL_DATE    */
428         N_("Size"),     /* S_COL_SIZE    */
429         N_("No."),      /* S_COL_NUMBER  */
430         N_("Score")     /* S_COL_SCORE   */
431 };
432
433 SummaryView *summary_create(void)
434 {
435         SummaryView *summaryview;
436         GtkWidget *vbox;
437         GtkWidget *scrolledwin;
438         GtkWidget *ctree;
439         GtkWidget *hbox;
440         GtkWidget *statlabel_folder;
441         GtkWidget *statlabel_select;
442         GtkWidget *statlabel_msgs;
443         GtkWidget *toggle_eventbox;
444         GtkWidget *toggle_arrow;
445         GtkWidget *popupmenu;
446         GtkItemFactory *popupfactory;
447         gint n_entries;
448
449         debug_print(_("Creating summary view...\n"));
450         summaryview = g_new0(SummaryView, 1);
451
452         vbox = gtk_vbox_new(FALSE, 2);
453
454         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
455         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
456                                        GTK_POLICY_AUTOMATIC,
457                                        GTK_POLICY_ALWAYS);
458         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
459         gtk_widget_set_usize(vbox,
460                              prefs_common.summaryview_width,
461                              prefs_common.summaryview_height);
462
463         ctree = summary_ctree_create(summaryview);
464
465         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
466                                             GTK_CLIST(ctree)->hadjustment);
467         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
468                                             GTK_CLIST(ctree)->vadjustment);
469         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
470
471         /* create status label */
472         hbox = gtk_hbox_new(FALSE, 0);
473         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
474
475         statlabel_folder = gtk_label_new("");
476         gtk_box_pack_start(GTK_BOX(hbox), statlabel_folder, FALSE, FALSE, 2);
477         statlabel_select = gtk_label_new("");
478         gtk_box_pack_start(GTK_BOX(hbox), statlabel_select, FALSE, FALSE, 16);
479
480         /* toggle view button */
481         toggle_eventbox = gtk_event_box_new();
482         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
483         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
484         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
485         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
486                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
487                            summaryview);
488
489         statlabel_msgs = gtk_label_new("");
490         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
491
492         /* create popup menu */
493         n_entries = sizeof(summary_popup_entries) /
494                 sizeof(summary_popup_entries[0]);
495         popupmenu = menu_create_items(summary_popup_entries, n_entries,
496                                       "<SummaryView>", &popupfactory,
497                                       summaryview);
498
499         summaryview->vbox = vbox;
500         summaryview->scrolledwin = scrolledwin;
501         summaryview->ctree = ctree;
502         summaryview->hbox = hbox;
503         summaryview->statlabel_folder = statlabel_folder;
504         summaryview->statlabel_select = statlabel_select;
505         summaryview->statlabel_msgs = statlabel_msgs;
506         summaryview->toggle_eventbox = toggle_eventbox;
507         summaryview->toggle_arrow = toggle_arrow;
508         summaryview->popupmenu = popupmenu;
509         summaryview->popupfactory = popupfactory;
510         summaryview->msg_is_toggled_on = TRUE;
511         summaryview->lock_count = 0;
512         summaryview->sort_mode = SORT_BY_NONE;
513         summaryview->sort_type = GTK_SORT_ASCENDING;
514
515         gtk_widget_show_all(vbox);
516
517         return summaryview;
518 }
519
520 void summary_init(SummaryView *summaryview)
521 {
522         GtkStyle *style;
523         GtkWidget *pixmap;
524
525         PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
526         PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
527                       deleted_xpm);
528         PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
529         PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
530         PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
531                       replied_xpm);
532         PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
533                       forwarded_xpm);
534         PIXMAP_CREATE(summaryview->ctree, ignorethreadxpm, ignorethreadxpmmask,
535                       ignorethread_xpm);
536         PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
537         PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
538                       dir_open_xpm);
539
540         pixmap = gtk_pixmap_new(clipxpm, clipxpmmask);
541         gtk_clist_set_column_widget(GTK_CLIST(summaryview->ctree),
542                                     summaryview->col_pos[S_COL_MIME], pixmap);
543         gtk_widget_show(pixmap);
544
545         if (!small_style) {
546                 small_style = gtk_style_copy
547                         (gtk_widget_get_style(summaryview->ctree));
548                 if (!smallfont)
549                         smallfont = gdk_fontset_load(SMALL_FONT);
550                 small_style->font = smallfont;
551                 small_marked_style = gtk_style_copy(small_style);
552                 small_marked_style->fg[GTK_STATE_NORMAL] =
553                         summaryview->color_marked;
554                 small_deleted_style = gtk_style_copy(small_style);
555                 small_deleted_style->fg[GTK_STATE_NORMAL] =
556                         summaryview->color_dim;
557         }
558         if (!bold_style) {
559                 bold_style = gtk_style_copy
560                         (gtk_widget_get_style(summaryview->ctree));
561                 if (!boldfont)
562                         boldfont = gdk_fontset_load(BOLD_FONT);
563                 bold_style->font = boldfont;
564                 bold_marked_style = gtk_style_copy(bold_style);
565                 bold_marked_style->fg[GTK_STATE_NORMAL] =
566                         summaryview->color_marked;
567                 bold_deleted_style = gtk_style_copy(bold_style);
568                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
569                         summaryview->color_dim;
570         }
571
572         style = gtk_style_copy(gtk_widget_get_style
573                                 (summaryview->statlabel_folder));
574
575         gtk_widget_set_style(summaryview->statlabel_folder, style);
576         gtk_widget_set_style(summaryview->statlabel_select, style);
577         gtk_widget_set_style(summaryview->statlabel_msgs, style);
578
579         pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
580         gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
581         gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
582         gtk_widget_show(pixmap);
583
584         summary_clear_list(summaryview);
585         summary_colorlabel_menu_create(summaryview);
586         summary_set_menu_sensitive(summaryview);
587
588 }
589
590 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
591                                                  GtkCTreeNode *current_node)
592 {
593         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
594         GtkCTreeNode *node;
595         MsgInfo *msginfo;
596         gint best_score = MIN_SCORE;
597         GtkCTreeNode *best_node = NULL;
598
599         if (current_node)
600                 /*node = current_node;*/
601                 node = GTK_CTREE_NODE_NEXT(current_node);
602         else
603                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
604
605         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
606                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
607                 if (msginfo->score >= summaryview->important_score)
608                         break;
609                 if (msginfo->score > best_score) {
610                         best_score = msginfo->score;
611                         best_node = node;
612                 }
613         }
614
615         if (node != NULL)
616                 return node;
617         else
618                 return best_node;
619 }
620
621 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
622                                                  GtkCTreeNode *current_node)
623 {
624         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
625         GtkCTreeNode *node;
626         MsgInfo *msginfo;
627         gint best_score = MIN_SCORE;
628         GtkCTreeNode *best_node = NULL;
629
630         if (current_node)
631                 /*node = current_node;*/
632                 node = GTK_CTREE_NODE_PREV(current_node);
633         else
634                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
635
636         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
637                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
638                 if (msginfo->score >= summaryview->important_score)
639                         break;
640                 if (msginfo->score > best_score) {
641                         best_score = msginfo->score;
642                         best_node = node;
643                 }
644         }
645
646         if (node != NULL)
647                 return node;
648         else
649                 return best_node;
650 }
651
652 gboolean summary_show(SummaryView *summaryview, FolderItem *item,
653                       gboolean update_cache)
654 {
655         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
656         GtkCTreeNode *node;
657         GSList *mlist = NULL;
658         gchar *buf;
659         gboolean is_refresh;
660         guint selected_msgnum = 0;
661         guint displayed_msgnum = 0;
662         GtkCTreeNode *selected_node = summaryview->folderview->selected;
663         GSList *cur;
664         gint sort_mode;
665         gint sort_type;
666         static gboolean locked = FALSE;
667
668         if (summary_is_locked(summaryview)) return FALSE;
669
670         inc_lock();
671         summary_lock(summaryview);
672
673         STATUSBAR_POP(summaryview->mainwin);
674
675         is_refresh = (!prefs_common.open_inbox_on_inc &&
676                       item == summaryview->folder_item &&
677                       update_cache == FALSE) ? TRUE : FALSE;
678         if (is_refresh) {
679                 selected_msgnum = summary_get_msgnum(summaryview,
680                                                      summaryview->selected);
681                 displayed_msgnum = summary_get_msgnum(summaryview,
682                                                       summaryview->displayed);
683         }
684
685         /* process the marks if any */
686         if (summaryview->mainwin->lock_count == 0 &&
687             (summaryview->moved > 0 || summaryview->copied > 0)) {
688                 AlertValue val;
689
690                 val = alertpanel(_("Process mark"),
691                                  _("Some marks are left. Process it?"),
692                                  _("Yes"), _("No"), _("Cancel"));
693                 if (G_ALERTDEFAULT == val)
694                         summary_execute(summaryview);
695                 else if (G_ALERTALTERNATE == val)
696                         summary_write_cache(summaryview);
697                 else {
698                         summary_unlock(summaryview);
699                         inc_unlock();
700                         return FALSE;
701                 }
702                 folder_update_op_count();
703         }
704         else if (!summaryview->filtering_happened) {
705                 summary_write_cache(summaryview);
706         }
707
708         summaryview->filtering_happened = FALSE;
709
710         summaryview->folderview->opened = selected_node;
711
712         gtk_clist_freeze(GTK_CLIST(ctree));
713
714         summary_clear_list(summaryview);
715         summary_set_menu_sensitive(summaryview);
716         if (!is_refresh)
717                 messageview_clear(summaryview->messageview);
718
719         buf = NULL;
720         if (!item || !item->path || !item->parent || item->no_select ||
721             (item->folder->type == F_MH &&
722              ((buf = folder_item_get_path(item)) == NULL ||
723               change_dir(buf) < 0))) {
724                 g_free(buf);
725                 debug_print(_("empty folder\n\n"));
726                 if (is_refresh)
727                         messageview_clear(summaryview->messageview);
728                 summary_clear_all(summaryview);
729                 summaryview->folder_item = item;
730                 gtk_clist_thaw(GTK_CLIST(ctree));
731                 summary_unlock(summaryview);
732                 inc_unlock();
733                 return TRUE;
734         }
735         g_free(buf);
736
737         summaryview->folder_item = item;
738
739         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
740
741         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
742         debug_print("%s\n", buf);
743         STATUSBAR_PUSH(summaryview->mainwin, buf);
744         g_free(buf);
745
746         main_window_cursor_wait(summaryview->mainwin);
747
748         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
749
750         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
751                 MsgInfo * msginfo = (MsgInfo *) cur->data;
752
753                 msginfo->score = score_message(global_scoring, msginfo);
754                 if (msginfo->score != MAX_SCORE &&
755                     msginfo->score != MIN_SCORE) {
756                         msginfo->score += score_message(item->prefs->scoring,
757                                                         msginfo);
758                 }
759         }
760
761         summaryview->killed_messages = NULL;
762         if ((global_scoring || item->prefs->scoring) &&
763             (item->folder->type == F_NEWS)) {
764                 GSList *not_killed;
765                 gint kill_score;
766
767                 not_killed = NULL;
768                 kill_score = prefs_common.kill_score;
769                 if (item->prefs->kill_score > kill_score)
770                         kill_score = item->prefs->kill_score;
771                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
772                         MsgInfo * msginfo = (MsgInfo *) cur->data;
773
774                         if (MSG_IS_NEWS(msginfo->flags) &&
775                             (msginfo->score <= kill_score))
776                                 summaryview->killed_messages = g_slist_append(summaryview->killed_messages, msginfo);
777                         else
778                                 not_killed = g_slist_append(not_killed,
779                                                             msginfo);
780                 }
781                 g_slist_free(mlist);
782                 mlist = not_killed;
783         }
784
785         STATUSBAR_POP(summaryview->mainwin);
786
787         /* set ctree and hash table from the msginfo list
788            creating thread, and count the number of messages */
789         summary_set_ctree_from_list(summaryview, mlist);
790
791         g_slist_free(mlist);
792
793         summary_write_cache(summaryview);
794
795         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
796
797         gtk_clist_thaw(GTK_CLIST(ctree));
798
799         /* sort before */
800         sort_mode = prefs_folder_item_get_sort_mode(item);
801         sort_type = prefs_folder_item_get_sort_type(item);
802
803         if (sort_mode != SORT_BY_NONE) {
804                 summaryview->sort_mode = sort_mode;
805                 if (sort_type == GTK_SORT_DESCENDING)
806                         summaryview->sort_type = GTK_SORT_ASCENDING;
807                 else
808                         summaryview->sort_type = GTK_SORT_DESCENDING;
809
810                 summary_sort(summaryview, sort_mode);
811         }
812
813         if (is_refresh) {
814                 summaryview->displayed =
815                         summary_find_msg_by_msgnum(summaryview,
816                                                    displayed_msgnum);
817                 if (!summaryview->displayed)
818                         messageview_clear(summaryview->messageview);
819                 summary_select_by_msgnum(summaryview, selected_msgnum);
820                 if (!summaryview->selected) {
821                         /* no selected message - select first unread
822                            message, but do not display it */
823                         node = summary_find_next_unread_msg(summaryview, NULL);
824                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
825                                 node = gtk_ctree_node_nth
826                                         (ctree, GTK_CLIST(ctree)->rows - 1);
827                         summary_select_node(summaryview, node, FALSE);
828                 }
829         } else {
830                 /* select first unread message */
831                 if (sort_mode == SORT_BY_SCORE)
832                         node = summary_find_next_important_score(summaryview,
833                                                                  NULL);
834                 else
835                         node = summary_find_next_unread_msg(summaryview, NULL);
836
837                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
838                         /* Get the last visible node on screen */
839                         /* FIXME: huh, what happens if node is null? that allowed?? */
840                         node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
841                 }       
842                 if (prefs_common.open_unread_on_enter) {
843                         summary_unlock(summaryview);
844                         summary_select_node(summaryview, node, TRUE);
845                         summary_lock(summaryview);
846                 } else
847                         summary_select_node(summaryview, node, FALSE);
848         }
849
850         summary_status_show(summaryview);
851
852         summary_set_menu_sensitive(summaryview);
853
854         main_window_set_toolbar_sensitive(summaryview->mainwin);
855
856         debug_print("\n");
857         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
858
859         main_window_cursor_normal(summaryview->mainwin);
860         summary_unlock(summaryview);
861         inc_unlock();
862
863         return TRUE;
864 }
865
866 void summary_clear_list(SummaryView *summaryview)
867 {
868         GtkCList *clist = GTK_CLIST(summaryview->ctree);
869         gint optimal_width;
870         GSList * cur;
871
872         gtk_clist_freeze(clist);
873
874         for(cur = summaryview->killed_messages ; cur != NULL ; 
875             cur = g_slist_next(cur)) {
876                 MsgInfo * msginfo = (MsgInfo *) cur->data;
877
878                 procmsg_msginfo_free(msginfo);
879         }
880         g_slist_free(summaryview->killed_messages);
881         summaryview->killed_messages = NULL;
882
883         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
884                                 NULL, summary_free_msginfo_func, NULL);
885
886         summaryview->folder_item = NULL;
887
888         summaryview->display_msg = FALSE;
889
890         summaryview->selected  = NULL;
891         summaryview->displayed = NULL;
892         summaryview->newmsgs   = summaryview->unread     = 0;
893         summaryview->messages  = summaryview->total_size = 0;
894         summaryview->deleted   = summaryview->moved      = 0;
895         summaryview->copied    = 0;
896         if (summaryview->msgid_table) {
897                 g_hash_table_destroy(summaryview->msgid_table);
898                 summaryview->msgid_table = NULL;
899         }
900         if (summaryview->subject_table) {
901                 g_hash_table_destroy(summaryview->subject_table);
902                 summaryview->subject_table = NULL;
903         }
904         summaryview->mlist = NULL;
905         if (summaryview->folder_table) {
906                 g_hash_table_destroy(summaryview->folder_table);
907                 summaryview->folder_table = NULL;
908         }
909         summaryview->sort_mode = SORT_BY_NONE;
910         summaryview->sort_type = GTK_SORT_ASCENDING;
911
912         gtk_clist_clear(clist);
913         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
914                 optimal_width = gtk_clist_optimal_column_width
915                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
916                 gtk_clist_set_column_width
917                         (clist, summaryview->col_pos[S_COL_SUBJECT],
918                          optimal_width);
919         }
920
921         gtk_clist_thaw(clist);
922 }
923
924 void summary_clear_all(SummaryView *summaryview)
925 {
926         summary_clear_list(summaryview);
927         summary_set_menu_sensitive(summaryview);
928         main_window_set_toolbar_sensitive(summaryview->mainwin);
929         summary_status_show(summaryview);
930 }
931
932 void summary_lock(SummaryView *summaryview)
933 {
934         summaryview->lock_count++;
935 }
936
937 void summary_unlock(SummaryView *summaryview)
938 {
939         if (summaryview->lock_count)
940                 summaryview->lock_count--;
941 }
942
943 gboolean summary_is_locked(SummaryView *summaryview)
944 {
945         return summaryview->lock_count > 0;
946 }
947
948 SummarySelection summary_get_selection_type(SummaryView *summaryview)
949 {
950         GtkCList *clist = GTK_CLIST(summaryview->ctree);
951         SummarySelection selection;
952
953         if (!clist->row_list)
954                 selection = SUMMARY_NONE;
955         else if (!clist->selection)
956                 selection = SUMMARY_SELECTED_NONE;
957         else if (!clist->selection->next)
958                 selection = SUMMARY_SELECTED_SINGLE;
959         else
960                 selection = SUMMARY_SELECTED_MULTIPLE;
961
962         return selection;
963 }
964
965 static void summary_set_menu_sensitive(SummaryView *summaryview)
966 {
967         GtkItemFactory *ifactory = summaryview->popupfactory;
968         SummarySelection selection;
969         gboolean sens;
970
971         selection = summary_get_selection_type(summaryview);
972         main_window_set_menu_sensitive(summaryview->mainwin);
973
974         if (selection == SUMMARY_NONE) {
975                 GtkWidget *submenu;
976
977                 submenu = gtk_item_factory_get_widget
978                         (summaryview->popupfactory, "/Mark");
979                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
980                 menu_set_insensitive_all
981                         (GTK_MENU_SHELL(summaryview->popupmenu));
982                 return;
983         }
984
985         if (summaryview->folder_item->folder->type != F_NEWS) {
986                 menu_set_sensitive(ifactory, "/Move...", TRUE);
987                 menu_set_sensitive(ifactory, "/Copy...", TRUE);
988                 menu_set_sensitive(ifactory, "/Delete", TRUE);
989         }
990
991         menu_set_sensitive(ifactory, "/Execute", TRUE);
992
993         menu_set_sensitive(ifactory, "/Mark", TRUE);
994         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
995         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
996
997         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
998         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
999         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1000         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1001         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1002
1003         menu_set_sensitive(ifactory, "/Color label", TRUE);
1004
1005         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1006         menu_set_sensitive(ifactory, "/Reply",                    sens);
1007         menu_set_sensitive(ifactory, "/Reply to sender",          sens);
1008         menu_set_sensitive(ifactory, "/Reply to all",             sens);
1009         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1010         menu_set_sensitive(ifactory, "/Forward as attachment",    TRUE);
1011
1012         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1013
1014         menu_set_sensitive(ifactory, "/View", sens);
1015         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1016         menu_set_sensitive(ifactory, "/View/Source", sens);
1017         menu_set_sensitive(ifactory, "/View/All header", sens);
1018         if (summaryview->folder_item->stype == F_OUTBOX ||
1019             summaryview->folder_item->stype == F_DRAFT  ||
1020             summaryview->folder_item->stype == F_QUEUE)
1021                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1022
1023         menu_set_sensitive(ifactory, "/Save as...", sens);
1024         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1025
1026         menu_set_sensitive(ifactory, "/Select all", TRUE);
1027
1028         if (summaryview->folder_item->folder->account)
1029                 sens = summaryview->folder_item->folder->account->protocol
1030                         == A_NNTP;
1031         else
1032                 sens = FALSE;
1033         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1034 }
1035
1036 void summary_select_prev_unread(SummaryView *summaryview)
1037 {
1038         GtkCTreeNode *node;
1039         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1040
1041         node = summary_find_prev_unread_msg(summaryview, summaryview->selected);
1042
1043         if (!node) {
1044                 AlertValue val;
1045
1046                 switch (prefs_common.next_unread_msg_dialog) {
1047                         case NEXTUNREADMSGDIALOG_ALWAYS:
1048                                 val = alertpanel(_("No more unread messages"),
1049                                                  _("No unread message found. "
1050                                                    "Search from the end?"),
1051                                                  _("Yes"), _("No"), NULL);
1052                                 break;
1053                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1054                                 val = G_ALERTDEFAULT;
1055                                 break;
1056                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1057                                 val = !G_ALERTDEFAULT;
1058                                 break;
1059                         default:
1060                                 debug_print(
1061                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1062                 }
1063                 if (val != G_ALERTDEFAULT) return;
1064                 node = summary_find_prev_unread_msg(summaryview, NULL);
1065         }
1066
1067         if (!node) {
1068                 alertpanel_notice(_("No unread messages."));
1069         } else {
1070                 gtkut_ctree_expand_parent_all(ctree, node);
1071                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1072                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1073                 if (summaryview->displayed == node)
1074                         summaryview->displayed = NULL;
1075                 summary_display_msg(summaryview, node, FALSE);
1076         }
1077 }
1078
1079 void summary_select_next_unread(SummaryView *summaryview)
1080 {
1081         GtkCTreeNode *node;
1082         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1083
1084         node = summary_find_next_unread_msg(summaryview, summaryview->selected);
1085
1086         if (node) {
1087                 gtkut_ctree_expand_parent_all(ctree, node);
1088                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1089                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1090                 if (summaryview->displayed == node)
1091                         summaryview->displayed = NULL;
1092                 summary_display_msg(summaryview, node, FALSE);
1093         } else {
1094                 AlertValue val;
1095
1096                 switch (prefs_common.next_unread_msg_dialog) {
1097                         case NEXTUNREADMSGDIALOG_ALWAYS:
1098                                 val = alertpanel(_("No more unread messages"),
1099                                                  _("No unread message found. "
1100                                                    "Go to next folder?"),
1101                                                  _("Yes"), _("No"), NULL);
1102                                 break;
1103                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1104                                 val = G_ALERTDEFAULT;
1105                                 break;
1106                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1107                                 val = !G_ALERTDEFAULT;
1108                                 break;
1109                         default:
1110                                 debug_print(
1111                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1112                 }
1113
1114                 if (val == G_ALERTDEFAULT) {
1115                         if (gtk_signal_n_emissions_by_name
1116                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1117                                         gtk_signal_emit_stop_by_name
1118                                                 (GTK_OBJECT(ctree),
1119                                                  "key_press_event");
1120                         folderview_select_next_unread(summaryview->folderview);
1121                 }
1122         }
1123 }
1124
1125 void summary_select_prev_marked(SummaryView *summaryview)
1126 {
1127         GtkCTreeNode *node;
1128         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1129
1130         node = summary_find_prev_marked_msg(summaryview, summaryview->selected);
1131
1132         if (!node) {
1133                 AlertValue val;
1134
1135                 val = alertpanel(_("No more marked messages"),
1136                                  _("No marked message found. "
1137                                    "Search from the end?"),
1138                                  _("Yes"), _("No"), NULL);
1139                 if (val != G_ALERTDEFAULT) return;
1140                 node = summary_find_prev_marked_msg(summaryview, NULL);
1141         }
1142
1143         if (!node) {
1144                 alertpanel_notice(_("No marked messages."));
1145         } else {
1146                 gtkut_ctree_expand_parent_all(ctree, node);
1147                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1148                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1149                 if (summaryview->displayed == node)
1150                         summaryview->displayed = NULL;
1151                 summary_display_msg(summaryview, node, FALSE);
1152         }
1153 }
1154
1155 void summary_select_next_marked(SummaryView *summaryview)
1156 {
1157         GtkCTreeNode *node;
1158         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1159
1160         node = summary_find_next_marked_msg(summaryview, summaryview->selected);
1161
1162         if (!node) {
1163                 AlertValue val;
1164
1165                 val = alertpanel(_("No more marked messages"),
1166                                  _("No marked message found. "
1167                                    "Search from the beginning?"),
1168                                  _("Yes"), _("No"), NULL);
1169                 if (val != G_ALERTDEFAULT) return;
1170                 node = summary_find_next_marked_msg(summaryview, NULL);
1171         }
1172
1173         if (!node) {
1174                 alertpanel_notice(_("No marked messages."));
1175         } else {
1176                 gtkut_ctree_expand_parent_all(ctree, node);
1177                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1178                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1179                 if (summaryview->displayed == node)
1180                         summaryview->displayed = NULL;
1181                 summary_display_msg(summaryview, node, FALSE);
1182         }
1183 }
1184
1185 void summary_select_prev_labeled(SummaryView *summaryview)
1186 {
1187         GtkCTreeNode *node;
1188         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1189
1190         node = summary_find_prev_labeled_msg(summaryview, summaryview->selected);
1191
1192         if (!node) {
1193                 AlertValue val;
1194
1195                 val = alertpanel(_("No more labeled messages"),
1196                                  _("No labeled message found. "
1197                                    "Search from the end?"),
1198                                  _("Yes"), _("No"), NULL);
1199                 if (val != G_ALERTDEFAULT) return;
1200                 node = summary_find_prev_labeled_msg(summaryview, NULL);
1201         }
1202
1203         if (!node) {
1204                 alertpanel_notice(_("No labeled messages."));
1205         } else {
1206                 gtkut_ctree_expand_parent_all(ctree, node);
1207                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1208                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1209                 if (summaryview->displayed == node)
1210                         summaryview->displayed = NULL;
1211                 summary_display_msg(summaryview, node, FALSE);
1212         }
1213 }
1214
1215 void summary_select_next_labeled(SummaryView *summaryview)
1216 {
1217         GtkCTreeNode *node;
1218         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1219
1220         node = summary_find_next_labeled_msg(summaryview, summaryview->selected);
1221
1222         if (!node) {
1223                 AlertValue val;
1224
1225                 val = alertpanel(_("No more labeled messages"),
1226                                  _("No labeled message found. "
1227                                    "Search from the beginning?"),
1228                                  _("Yes"), _("No"), NULL);
1229                 if (val != G_ALERTDEFAULT) return;
1230                 node = summary_find_next_labeled_msg(summaryview, NULL);
1231         }
1232
1233         if (!node) {
1234                 alertpanel_notice(_("No labeled messages."));
1235         } else {
1236                 gtkut_ctree_expand_parent_all(ctree, node);
1237                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1238                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1239                 if (summaryview->displayed == node)
1240                         summaryview->displayed = NULL;
1241                 summary_display_msg(summaryview, node, FALSE);
1242         }
1243 }
1244
1245 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1246 {
1247         GtkCTreeNode *node;
1248
1249         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1250         summary_select_node(summaryview, node, FALSE);
1251 }
1252
1253 /**
1254  * summary_select_node:
1255  * @summaryview: Summary view.
1256  * @node: Summary tree node.
1257  * @display_msg: TRUE to display the selected message.
1258  *
1259  * Select @node (bringing it into view by scrolling and expanding its
1260  * thread, if necessary) and unselect all others.  If @display_msg is
1261  * TRUE, display the corresponding message in the message view.
1262  **/
1263 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1264                          gboolean display_msg)
1265 {
1266         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1267
1268         if (node) {
1269                 GTK_EVENTS_FLUSH();
1270                 gtkut_ctree_expand_parent_all(ctree, node);
1271                 gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1272                 gtk_widget_grab_focus(GTK_WIDGET(ctree));
1273                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1274                 summaryview->display_msg = display_msg;
1275                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1276         }
1277 }
1278
1279 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1280 {
1281         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1282         MsgInfo *msginfo;
1283
1284         if (!node)
1285                 return 0;
1286         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1287         return msginfo->msgnum;
1288 }
1289
1290 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1291                                            GtkCTreeNode *current_node)
1292 {
1293         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1294         GtkCTreeNode *node;
1295         MsgInfo *msginfo;
1296
1297         if (current_node)
1298                 node = current_node;
1299         else
1300                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1301
1302         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1303                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1304                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1305         }
1306
1307         return node;
1308 }
1309
1310 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1311                                            GtkCTreeNode *current_node)
1312 {
1313         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1314         GtkCTreeNode *node;
1315         MsgInfo *msginfo;
1316
1317         if (current_node)
1318                 node = current_node;
1319         else
1320                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1321
1322         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1323                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1324                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1325         }
1326
1327         return node;
1328 }
1329
1330 static GtkCTreeNode *summary_find_prev_unread_msg(SummaryView *summaryview,
1331                                                   GtkCTreeNode *current_node)
1332 {
1333         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1334         GtkCTreeNode *node;
1335         MsgInfo *msginfo;
1336
1337         if (current_node)
1338                 node = current_node;
1339         else
1340                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1341
1342         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1343                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1344                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) break;
1345         }
1346
1347         return node;
1348 }
1349
1350 static GtkCTreeNode *summary_find_next_unread_msg(SummaryView *summaryview,
1351                                                   GtkCTreeNode *current_node)
1352 {
1353         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1354         GtkCTreeNode *node;
1355         MsgInfo *msginfo;
1356
1357         if (current_node)
1358                 node = current_node;
1359         else
1360                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1361
1362         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1363                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1364                 if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags)) break;
1365         }
1366
1367         return node;
1368 }
1369
1370 static GtkCTreeNode *summary_find_prev_marked_msg(SummaryView *summaryview,
1371                                                   GtkCTreeNode *current_node)
1372 {
1373         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1374         GtkCTreeNode *node;
1375         MsgInfo *msginfo;
1376
1377         if (current_node)
1378                 node = GTK_CTREE_NODE_PREV(current_node);
1379         else
1380                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1381
1382         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1383                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1384                 if (MSG_IS_MARKED(msginfo->flags)) break;
1385         }
1386
1387         return node;
1388 }
1389
1390 static GtkCTreeNode *summary_find_next_marked_msg(SummaryView *summaryview,
1391                                                   GtkCTreeNode *current_node)
1392 {
1393         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1394         GtkCTreeNode *node;
1395         MsgInfo *msginfo;
1396
1397         if (current_node)
1398                 node = gtkut_ctree_node_next(ctree, current_node);
1399         else
1400                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1401
1402         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1403                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1404                 if (MSG_IS_MARKED(msginfo->flags)) break;
1405         }
1406
1407         return node;
1408 }
1409
1410 static GtkCTreeNode *summary_find_prev_labeled_msg(SummaryView *summaryview,
1411                                                    GtkCTreeNode *current_node)
1412 {
1413         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1414         GtkCTreeNode *node;
1415         MsgInfo *msginfo;
1416
1417         if (current_node)
1418                 node = GTK_CTREE_NODE_PREV(current_node);
1419         else
1420                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1421
1422         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1423                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1424                 if (MSG_GET_COLORLABEL_VALUE(msginfo->flags) > 0) break;
1425         }
1426
1427         return node;
1428 }
1429
1430 static GtkCTreeNode *summary_find_next_labeled_msg(SummaryView *summaryview,
1431                                                    GtkCTreeNode *current_node)
1432 {
1433         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1434         GtkCTreeNode *node;
1435         MsgInfo *msginfo;
1436
1437         if (current_node)
1438                 node = gtkut_ctree_node_next(ctree, current_node);
1439         else
1440                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1441
1442         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1443                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1444                 if (MSG_GET_COLORLABEL_VALUE(msginfo->flags) > 0) break;
1445         }
1446
1447         return node;
1448 }
1449
1450 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1451                                                 guint msgnum)
1452 {
1453         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1454         GtkCTreeNode *node;
1455         MsgInfo *msginfo;
1456
1457         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1458
1459         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1460                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1461                 if (msginfo->msgnum == msgnum) break;
1462         }
1463
1464         return node;
1465 }
1466
1467 static guint attract_hash_func(gconstpointer key)
1468 {
1469         gchar *str;
1470         gchar *p;
1471         guint h;
1472
1473         Xstrdup_a(str, (const gchar *)key, return 0);
1474         trim_subject(str);
1475
1476         p = str;
1477         h = *p;
1478
1479         if (h) {
1480                 for (p += 1; *p != '\0'; p++)
1481                         h = (h << 5) - h + *p;
1482         }
1483
1484         return h;
1485 }
1486
1487 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1488 {
1489         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1490 }
1491
1492 void summary_attract_by_subject(SummaryView *summaryview)
1493 {
1494         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1495         GtkCList *clist = GTK_CLIST(ctree);
1496         GtkCTreeNode *src_node;
1497         GtkCTreeNode *dst_node, *sibling;
1498         GtkCTreeNode *tmp;
1499         MsgInfo *src_msginfo, *dst_msginfo;
1500         GHashTable *subject_table;
1501
1502         debug_print(_("Attracting messages by subject..."));
1503         STATUSBAR_PUSH(summaryview->mainwin,
1504                        _("Attracting messages by subject..."));
1505
1506         main_window_cursor_wait(summaryview->mainwin);
1507         gtk_clist_freeze(clist);
1508
1509         subject_table = g_hash_table_new(attract_hash_func,
1510                                          attract_compare_func);
1511
1512         for (src_node = GTK_CTREE_NODE(clist->row_list);
1513              src_node != NULL;
1514              src_node = tmp) {
1515                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1516                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1517                 if (!src_msginfo) continue;
1518                 if (!src_msginfo->subject) continue;
1519
1520                 /* find attracting node */
1521                 dst_node = g_hash_table_lookup(subject_table,
1522                                                src_msginfo->subject);
1523
1524                 if (dst_node) {
1525                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1526
1527                         /* if the time difference is more than 20 days,
1528                            don't attract */
1529                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1530                             > 60 * 60 * 24 * 20)
1531                                 continue;
1532
1533                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1534                         if (src_node != sibling)
1535                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1536                 }
1537
1538                 g_hash_table_insert(subject_table,
1539                                     src_msginfo->subject, src_node);
1540         }
1541
1542         g_hash_table_destroy(subject_table);
1543
1544         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1545
1546         gtk_clist_thaw(clist);
1547
1548         debug_print(_("done.\n"));
1549         STATUSBAR_POP(summaryview->mainwin);
1550
1551         main_window_cursor_normal(summaryview->mainwin);
1552 }
1553
1554 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1555                                       gpointer data)
1556 {
1557         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1558
1559         if (msginfo)
1560                 procmsg_msginfo_free(msginfo);
1561 }
1562
1563 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1564                                    gpointer data)
1565 {
1566         SummaryView *summaryview = data;
1567         MsgInfo *msginfo;
1568
1569         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1570
1571         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1572                 summaryview->newmsgs++;
1573         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1574                 summaryview->unread++;
1575         if (MSG_IS_DELETED(msginfo->flags))
1576                 summaryview->deleted++;
1577
1578         summaryview->messages++;
1579         summaryview->total_size += msginfo->size;
1580
1581         summary_set_row_marks(summaryview, node);
1582 }
1583
1584 static void summary_update_status(SummaryView *summaryview)
1585 {
1586         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1587         GtkCTreeNode *node;
1588         MsgInfo *msginfo;
1589
1590         summaryview->newmsgs = summaryview->unread =
1591         summaryview->messages = summaryview->total_size =
1592         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1593
1594         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1595              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1596                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1597
1598                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1599                         summaryview->newmsgs++;
1600                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1601                         summaryview->unread++;
1602                 if (MSG_IS_DELETED(msginfo->flags))
1603                         summaryview->deleted++;
1604                 if (MSG_IS_MOVE(msginfo->flags))
1605                         summaryview->moved++;
1606                 if (MSG_IS_COPY(msginfo->flags))
1607                         summaryview->copied++;
1608                 summaryview->messages++;
1609                 summaryview->total_size += msginfo->size;
1610         }
1611 }
1612
1613 static void summary_status_show(SummaryView *summaryview)
1614 {
1615         gchar *str;
1616         gchar *del, *mv, *cp;
1617         gchar *sel;
1618         gchar *spc;
1619         GList *rowlist, *cur;
1620         guint n_selected = 0;
1621         off_t sel_size = 0;
1622         MsgInfo *msginfo;
1623
1624         if (!summaryview->folder_item) {
1625                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1626                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1627                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1628                 return;
1629         }
1630
1631         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1632         for (cur = rowlist; cur != NULL; cur = cur->next) {
1633                 msginfo = gtk_ctree_node_get_row_data
1634                         (GTK_CTREE(summaryview->ctree),
1635                          GTK_CTREE_NODE(cur->data));
1636                 sel_size += msginfo->size;
1637                 n_selected++;
1638         }
1639
1640         gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1641                       summaryview->folder_item &&
1642                       summaryview->folder_item->folder->type == F_NEWS
1643                       ? g_basename(summaryview->folder_item->path)
1644                       : summaryview->folder_item->path);
1645
1646         if (summaryview->deleted)
1647                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1648         else
1649                 del = g_strdup("");
1650         if (summaryview->moved)
1651                 mv = g_strdup_printf(_("%s%d moved"),
1652                                      summaryview->deleted ? _(", ") : "",
1653                                      summaryview->moved);
1654         else
1655                 mv = g_strdup("");
1656         if (summaryview->copied)
1657                 cp = g_strdup_printf(_("%s%d copied"),
1658                                      summaryview->deleted ||
1659                                      summaryview->moved ? _(", ") : "",
1660                                      summaryview->copied);
1661         else
1662                 cp = g_strdup("");
1663
1664         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1665                 spc = "    ";
1666         else
1667                 spc = "";
1668
1669         if (n_selected)
1670                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1671         else
1672                 sel = g_strdup("");
1673         str = g_strconcat(n_selected ? itos(n_selected) : "",
1674                           n_selected ? _(" item(s) selected") : "",
1675                           sel, spc, del, mv, cp, NULL);
1676         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1677         g_free(str);
1678         g_free(sel);
1679         g_free(del);
1680         g_free(mv);
1681         g_free(cp);
1682
1683         if (summaryview->folder_item &&
1684             FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1685                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1686                                       summaryview->newmsgs,
1687                                       summaryview->unread,
1688                                       summaryview->messages,
1689                                       to_human_readable(summaryview->total_size));
1690         } else {
1691                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1692                                       summaryview->newmsgs,
1693                                       summaryview->unread,
1694                                       summaryview->messages);
1695         }
1696         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1697         g_free(str);
1698
1699         folderview_update_msg_num(summaryview->folderview,
1700                                   summaryview->folderview->opened,
1701                                   summaryview->newmsgs,
1702                                   summaryview->unread,
1703                                   summaryview->messages);
1704 }
1705
1706 void summary_sort(SummaryView *summaryview, SummarySortType type)
1707 {
1708         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1709         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1710         GtkCListCompareFunc cmp_func;
1711
1712         if (!summaryview->folder_item)
1713                 return;
1714
1715         switch (type) {
1716         case SORT_BY_MARK:
1717                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1718                 break;
1719         case SORT_BY_UNREAD:
1720                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1721                 break;
1722         case SORT_BY_MIME:
1723                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1724                 break;
1725         case SORT_BY_NUMBER:
1726                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1727                 break;
1728         case SORT_BY_SIZE:
1729                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1730                 break;
1731         case SORT_BY_DATE:
1732                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1733                 break;
1734         case SORT_BY_FROM:
1735                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1736                 break;
1737         case SORT_BY_SUBJECT:
1738                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1739                 break;
1740         case SORT_BY_SCORE:
1741                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1742                 break;
1743         case SORT_BY_LABEL:
1744                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
1745                 break;
1746         default:
1747                 return;
1748         }
1749
1750         debug_print(_("Sorting summary..."));
1751         STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1752
1753         main_window_cursor_wait(summaryview->mainwin);
1754
1755         gtk_clist_set_compare_func(clist, cmp_func);
1756
1757         /* toggle sort type if the same column is selected */
1758         if (summaryview->sort_mode == type)
1759                 summaryview->sort_type =
1760                         summaryview->sort_type == GTK_SORT_ASCENDING
1761                         ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
1762         else
1763                 summaryview->sort_type = GTK_SORT_ASCENDING;
1764         gtk_clist_set_sort_type(clist, summaryview->sort_type);
1765         summaryview->sort_mode = type;
1766
1767         gtk_ctree_sort_node(ctree, NULL);
1768
1769         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1770         /*gtkut_ctree_set_focus_row(ctree, summaryview->selected);*/
1771
1772         prefs_folder_item_set_config(summaryview->folder_item,
1773                                      summaryview->sort_type,
1774                                      summaryview->sort_mode);
1775         prefs_folder_item_save_config(summaryview->folder_item);
1776
1777         debug_print(_("done.\n"));
1778         STATUSBAR_POP(summaryview->mainwin);
1779
1780         main_window_cursor_normal(summaryview->mainwin);
1781 }
1782
1783 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
1784                                    GtkCTreeNode *cnode, gpointer data)
1785 {
1786         SummaryView *summaryview = (SummaryView *)data;
1787         MsgInfo *msginfo = (MsgInfo *)gnode->data;
1788         gchar *text[N_SUMMARY_COLS];
1789         gint *col_pos = summaryview->col_pos;
1790         const gchar *msgid = msginfo->msgid;
1791         GHashTable *msgid_table = summaryview->msgid_table;
1792
1793         summary_set_header(summaryview, text, msginfo);
1794
1795         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
1796                                 NULL, NULL, NULL, NULL, FALSE,
1797                                 gnode->parent->parent ? TRUE : FALSE);
1798 #define SET_TEXT(col) \
1799         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
1800                                 text[col_pos[col]])
1801
1802         SET_TEXT(S_COL_NUMBER);
1803         SET_TEXT(S_COL_SCORE);
1804         SET_TEXT(S_COL_SIZE);
1805         SET_TEXT(S_COL_DATE);
1806         SET_TEXT(S_COL_FROM);
1807         SET_TEXT(S_COL_SUBJECT);
1808
1809 #undef SET_TEXT
1810
1811         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
1812         summary_set_marks_func(ctree, cnode, summaryview);
1813
1814         if (msgid)
1815                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
1816
1817         return TRUE;
1818 }
1819
1820 static GtkCTreeNode * subject_table_lookup(GHashTable *subject_table,
1821                                            gchar * subject)
1822 {
1823         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1824                 return g_hash_table_lookup(subject_table, subject + 4);
1825         else
1826                 return g_hash_table_lookup(subject_table, subject);
1827 }
1828
1829 static void subject_table_insert(GHashTable *subject_table, gchar * subject,
1830                                  GtkCTreeNode * node)
1831 {
1832         if (g_strncasecmp(subject, "Re: ", 4) == 0)
1833                 g_hash_table_insert(subject_table, subject + 4, node);
1834         else
1835                 g_hash_table_insert(subject_table, subject, node);
1836 }
1837
1838 static void summary_set_ctree_from_list(SummaryView *summaryview,
1839                                         GSList *mlist)
1840 {
1841         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1842         MsgInfo *msginfo;
1843         MsgInfo *parentinfo;
1844         MsgInfo *cur_msginfo;
1845         GtkCTreeNode *node = NULL;
1846         GHashTable *msgid_table;
1847         GHashTable *subject_table;
1848         GSList * cur;
1849         GtkCTreeNode *cur_parent;
1850
1851         if (!mlist) return;
1852
1853         debug_print(_("\tSetting summary from message data..."));
1854         STATUSBAR_PUSH(summaryview->mainwin,
1855                        _("Setting summary from message data..."));
1856         gdk_flush();
1857
1858         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1859         summaryview->msgid_table = msgid_table;
1860         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1861         summaryview->subject_table = subject_table;
1862
1863         if (prefs_common.use_addr_book)
1864                 start_address_completion();
1865         
1866         for (cur = mlist ; cur != NULL; cur = cur->next) {
1867                 msginfo = (MsgInfo *)cur->data;
1868                 msginfo->threadscore = msginfo->score;
1869         }
1870
1871         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1872                 summaryview->important_score = prefs_common.important_score;
1873                 if (summaryview->folder_item->prefs->important_score >
1874                     summaryview->important_score)
1875                         summaryview->important_score =
1876                                 summaryview->folder_item->prefs->important_score;
1877         }
1878         
1879         if (summaryview->folder_item->threaded) {
1880                 GNode *root, *gnode;
1881
1882                 root = procmsg_get_thread_tree(mlist);
1883
1884                 for (gnode = root->children; gnode != NULL;
1885                      gnode = gnode->next) {
1886                         node = gtk_ctree_insert_gnode
1887                                 (ctree, NULL, node, gnode,
1888                                  summary_insert_gnode_func, summaryview);
1889                 }
1890
1891                 g_node_destroy(root);
1892
1893                 summary_thread_init(summaryview);
1894         } else {
1895                 gchar *text[N_SUMMARY_COLS];
1896
1897                 mlist = g_slist_reverse(mlist);
1898                 for (; mlist != NULL; mlist = mlist->next) {
1899                         msginfo = (MsgInfo *)mlist->data;
1900
1901                         summary_set_header(summaryview, text, msginfo);
1902
1903                         node = gtk_ctree_insert_node
1904                                 (ctree, NULL, node, text, 2,
1905                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
1906                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
1907                         summary_set_marks_func(ctree, node, summaryview);
1908
1909                         if (msginfo->msgid)
1910                                 g_hash_table_insert(msgid_table,
1911                                                     msginfo->msgid, node);
1912                 }
1913                 mlist = g_slist_reverse(mlist);
1914         }
1915
1916         if (prefs_common.enable_hscrollbar &&
1917             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1918                 gint optimal_width;
1919
1920                 optimal_width = gtk_clist_optimal_column_width
1921                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
1922                 gtk_clist_set_column_width(GTK_CLIST(ctree),
1923                                            summaryview->col_pos[S_COL_SUBJECT],
1924                                            optimal_width);
1925         }
1926
1927         if (prefs_common.use_addr_book)
1928                 end_address_completion();
1929
1930         debug_print(_("done.\n"));
1931         STATUSBAR_POP(summaryview->mainwin);
1932         if (debug_mode) {
1933                 debug_print("\tmsgid hash table size = %d\n",
1934                             g_hash_table_size(msgid_table));
1935                 debug_print("\tsubject hash table size = %d\n",
1936                             g_hash_table_size(subject_table));
1937         }
1938 }
1939
1940 struct wcachefp
1941 {
1942         FILE *cache_fp;
1943         FILE *mark_fp;
1944 };
1945
1946 gint summary_write_cache(SummaryView *summaryview)
1947 {
1948         struct wcachefp fps;
1949         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1950         gint ver = CACHE_VERSION;
1951         gchar *buf;
1952         gchar *cachefile, *markfile;
1953         GSList * cur;
1954         gint filemode = 0;
1955         PrefsFolderItem *prefs;
1956
1957         if (!summaryview->folder_item || !summaryview->folder_item->path)
1958                 return -1;
1959
1960         if (summaryview->folder_item->folder->update_mark != NULL)
1961                 summaryview->folder_item->folder->update_mark(summaryview->folder_item->folder, summaryview->folder_item);
1962
1963         cachefile = folder_item_get_cache_file(summaryview->folder_item);
1964         g_return_val_if_fail(cachefile != NULL, -1);
1965         if ((fps.cache_fp = fopen(cachefile, "w")) == NULL) {
1966                 FILE_OP_ERROR(cachefile, "fopen");
1967                 g_free(cachefile);
1968                 return -1;
1969         }
1970         if (change_file_mode_rw(fps.cache_fp, cachefile) < 0)
1971                 FILE_OP_ERROR(cachefile, "chmod");
1972
1973         prefs = summaryview->folder_item->prefs;
1974         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1975                 /* for cache file */
1976                 filemode = prefs->folder_chmod;
1977                 if (filemode & S_IRGRP) filemode |= S_IWGRP;
1978                 if (filemode & S_IROTH) filemode |= S_IWOTH;
1979 #if HAVE_FCHMOD
1980                 fchmod(fileno(fps.cache_fp), filemode);
1981 #else
1982                 chmod(cachefile, filemode);
1983 #endif
1984         }
1985
1986         g_free(cachefile);
1987
1988         markfile = folder_item_get_mark_file(summaryview->folder_item);
1989         if ((fps.mark_fp = fopen(markfile, "w")) == NULL) {
1990                 FILE_OP_ERROR(markfile, "fopen");
1991                 fclose(fps.cache_fp);
1992                 g_free(markfile);
1993                 return -1;
1994         }
1995         if (change_file_mode_rw(fps.mark_fp, markfile) < 0)
1996                 FILE_OP_ERROR(markfile, "chmod");
1997         if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
1998 #if HAVE_FCHMOD
1999                 fchmod(fileno(fps.mark_fp), filemode);
2000 #else
2001                 chmod(markfile, filemode);
2002 #endif
2003         }
2004
2005         g_free(markfile);
2006
2007         buf = g_strdup_printf(_("Writing summary cache (%s)..."),
2008                               summaryview->folder_item->path);
2009         debug_print(buf);
2010         STATUSBAR_PUSH(summaryview->mainwin, buf);
2011         g_free(buf);
2012
2013         WRITE_CACHE_DATA_INT(ver, fps.cache_fp);
2014         ver = MARK_VERSION;
2015         WRITE_CACHE_DATA_INT(ver, fps.mark_fp);
2016
2017         gtk_ctree_pre_recursive(ctree, NULL, summary_write_cache_func, &fps);
2018
2019         for(cur = summaryview->killed_messages ; cur != NULL ;
2020             cur = g_slist_next(cur)) {
2021                 MsgInfo *msginfo = (MsgInfo *) cur->data;
2022
2023                 procmsg_write_cache(msginfo, fps.cache_fp);
2024                 procmsg_write_flags(msginfo, fps.mark_fp);
2025         }
2026
2027         fclose(fps.cache_fp);
2028         fclose(fps.mark_fp);
2029
2030         debug_print(_("done.\n"));
2031         STATUSBAR_POP(summaryview->mainwin);
2032
2033         return 0;
2034 }
2035
2036 static void summary_write_cache_func(GtkCTree *ctree, GtkCTreeNode *node,
2037                                      gpointer data)
2038 {
2039         struct wcachefp *fps = data;
2040         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
2041
2042         if (msginfo == NULL) return;
2043
2044         procmsg_write_cache(msginfo, fps->cache_fp);
2045         procmsg_write_flags(msginfo, fps->mark_fp);
2046 }
2047
2048 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2049                                MsgInfo *msginfo)
2050 {
2051         static gchar date_modified[80];
2052         static gchar *to = NULL;
2053         static gchar *from_name = NULL;
2054         static gchar col_number[11];
2055         static gchar col_score[11];
2056         gint *col_pos = summaryview->col_pos;
2057
2058         text[col_pos[S_COL_MARK]]   = NULL;
2059         text[col_pos[S_COL_UNREAD]] = NULL;
2060         text[col_pos[S_COL_MIME]]   = NULL;
2061         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2062         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2063
2064 #if 0
2065         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->threadscore);
2066 #else
2067         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2068 #endif
2069
2070         if (msginfo->date_t) {
2071                 procheader_date_get_localtime(date_modified,
2072                                               sizeof(date_modified),
2073                                               msginfo->date_t);
2074                 text[col_pos[S_COL_DATE]] = date_modified;
2075         } else if (msginfo->date)
2076                 text[col_pos[S_COL_DATE]] = msginfo->date;
2077         else
2078                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2079
2080         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2081                 _("(No From)");
2082         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2083             !MSG_IS_NEWS(msginfo->flags)) {
2084                 gchar *from;
2085
2086                 Xalloca(from, strlen(msginfo->from) + 1, return);
2087                 strcpy(from, msginfo->from);
2088                 extract_address(from);
2089                 if (account_find_from_address(from)) {
2090                         g_free(to);
2091                         to = g_strconcat("-->", msginfo->to, NULL);
2092                         text[col_pos[S_COL_FROM]] = to;
2093                 }
2094         }
2095
2096         if ((text[S_COL_FROM] != to) && prefs_common.use_addr_book &&
2097             msginfo->from) {
2098                 gint count;
2099                 gchar *from;
2100   
2101                 Xalloca(from, strlen(msginfo->from) + 1, return);
2102                 strcpy(from, msginfo->from);
2103                 extract_address(from);
2104                 if (*from) {
2105                         count = complete_address(from);
2106                         if (count > 1) {
2107                                 g_free(from_name);
2108                                 from = get_complete_address(1);
2109                                 from_name = procheader_get_fromname(from);
2110                                 g_free(from);
2111                                 text[S_COL_FROM] = from_name;
2112                         }
2113                 }
2114         }
2115
2116         text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2117                 _("(No Subject)");
2118 }
2119
2120 #define CHANGE_FLAGS(msginfo) \
2121 { \
2122 if (msginfo->folder->folder->change_flags != NULL) \
2123 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2124                                       msginfo->folder, \
2125                                       msginfo); \
2126 }
2127
2128 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
2129                                 gboolean new_window)
2130 {
2131         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2132         MsgInfo *msginfo;
2133         gchar *filename;
2134
2135         if (!new_window && summaryview->displayed == row) return;
2136         g_return_if_fail(row != NULL);
2137
2138         if (summary_is_locked(summaryview)) return;
2139         summary_lock(summaryview);
2140
2141         STATUSBAR_POP(summaryview->mainwin);
2142         GTK_EVENTS_FLUSH();
2143
2144         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2145
2146         filename = procmsg_get_message_file(msginfo);
2147         if (!filename) {
2148                 summary_unlock(summaryview);
2149                 return;
2150         }
2151         g_free(filename);
2152
2153         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2154                 summaryview->newmsgs--;
2155         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2156                 summaryview->unread--;
2157         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
2158                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2159                 CHANGE_FLAGS(msginfo);
2160                 summary_set_row_marks(summaryview, row);
2161                 gtk_clist_thaw(GTK_CLIST(ctree));
2162                 summary_status_show(summaryview);
2163         }
2164
2165         if (new_window) {
2166                 MessageView *msgview;
2167
2168                 msgview = messageview_create_with_new_window();
2169                 messageview_show(msgview, msginfo);
2170         } else {
2171                 MessageView *msgview;
2172
2173                 msgview = summaryview->messageview;
2174
2175                 summaryview->displayed = row;
2176                 if (!summaryview->msg_is_toggled_on)
2177                         summary_toggle_view(summaryview);
2178                 messageview_show(msgview, msginfo);
2179                 if (msgview->type == MVIEW_TEXT ||
2180                     (msgview->type == MVIEW_MIME &&
2181                      GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL))
2182                         gtk_widget_grab_focus(summaryview->ctree);
2183                 GTK_EVENTS_FLUSH();
2184                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2185         }
2186
2187         if (GTK_WIDGET_VISIBLE(summaryview->headerwin->window))
2188                 header_window_show(summaryview->headerwin, msginfo);
2189
2190         summary_unlock(summaryview);
2191 }
2192
2193 void summary_redisplay_msg(SummaryView *summaryview)
2194 {
2195         GtkCTreeNode *node;
2196
2197         if (summaryview->displayed) {
2198                 node = summaryview->displayed;
2199                 summaryview->displayed = NULL;
2200                 summary_display_msg(summaryview, node, FALSE);
2201         }
2202 }
2203
2204 void summary_open_msg(SummaryView *summaryview)
2205 {
2206         if (!summaryview->selected) return;
2207
2208         summary_display_msg(summaryview, summaryview->selected, TRUE);
2209 }
2210
2211 void summary_view_source(SummaryView * summaryview)
2212 {
2213         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2214         MsgInfo *msginfo;
2215         SourceWindow *srcwin;
2216
2217         if (!summaryview->selected) return;
2218
2219         srcwin = source_window_create();
2220         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2221         source_window_show_msg(srcwin, msginfo);
2222         source_window_show(srcwin);
2223 }
2224
2225 void summary_reedit(SummaryView *summaryview)
2226 {
2227         MsgInfo *msginfo;
2228
2229         if (!summaryview->selected) return;
2230         if (!summaryview->folder_item) return;
2231         if (summaryview->folder_item->stype != F_OUTBOX &&
2232             summaryview->folder_item->stype != F_DRAFT  &&
2233             summaryview->folder_item->stype != F_QUEUE) return;
2234
2235         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2236                                               summaryview->selected);
2237         if (!msginfo) return;
2238
2239         compose_reedit(msginfo);
2240 }
2241
2242 void summary_step(SummaryView *summaryview, GtkScrollType type)
2243 {
2244         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2245
2246         if (type == GTK_SCROLL_STEP_FORWARD) {
2247                 GtkCTreeNode *node;
2248                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2249                 if (node)
2250                         gtkut_ctree_expand_parent_all(ctree, node);
2251         }
2252
2253         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2254                                 type, 0.0);
2255         
2256         if (summaryview->msg_is_toggled_on)
2257                 summary_display_msg(summaryview, summaryview->selected, FALSE);
2258 }
2259
2260 static void summary_toggle_view(SummaryView *summaryview)
2261 {
2262         MainWindow *mainwin = summaryview->mainwin;
2263         union CompositeWin *cwin = &mainwin->win;
2264         GtkWidget *vpaned = NULL;
2265         GtkWidget *container = NULL;
2266
2267         switch (mainwin->type) {
2268         case SEPARATE_NONE:
2269                 vpaned = cwin->sep_none.vpaned;
2270                 container = cwin->sep_none.hpaned;
2271                 break;
2272         case SEPARATE_FOLDER:
2273                 vpaned = cwin->sep_folder.vpaned;
2274                 container = mainwin->vbox_body;
2275                 break;
2276         case SEPARATE_MESSAGE:
2277         case SEPARATE_BOTH:
2278                 return;
2279         }
2280
2281         if (vpaned->parent != NULL) {
2282                 summaryview->msg_is_toggled_on = FALSE;
2283                 summaryview->displayed = NULL;
2284                 gtk_widget_ref(vpaned);
2285                 gtk_container_remove(GTK_CONTAINER(container), vpaned);
2286                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
2287                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2288                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2289         } else {
2290                 summaryview->msg_is_toggled_on = TRUE;
2291                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
2292                 gtk_container_add(GTK_CONTAINER(container), vpaned);
2293                 gtk_widget_unref(vpaned);
2294                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
2295                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2296         }
2297
2298         gtk_widget_grab_focus(summaryview->ctree);
2299 }
2300
2301 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2302                                                 GtkCTreeNode *node)
2303 {
2304         MsgInfo *msginfo;
2305
2306         if (node) {
2307                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2308                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2309                         return TRUE;
2310                 node = GTK_CTREE_ROW(node)->children;
2311         } else
2312                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2313
2314         while (node) {
2315                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2316                         return TRUE;
2317                 node = GTK_CTREE_ROW(node)->sibling;
2318         }
2319
2320         return FALSE;
2321 }
2322
2323 static gboolean summary_have_unread_children(SummaryView *summaryview,
2324                                              GtkCTreeNode *node)
2325 {
2326         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2327
2328         if (!node) return FALSE;
2329
2330         node = GTK_CTREE_ROW(node)->children;
2331
2332         while (node) {
2333                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2334                         return TRUE;
2335                 node = GTK_CTREE_ROW(node)->sibling;
2336         }
2337
2338         return FALSE;
2339 }
2340
2341 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2342 {
2343         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2344         GtkStyle *style = NULL;
2345         MsgInfo *msginfo;
2346         MsgFlags flags;
2347         gint *col_pos = summaryview->col_pos;
2348
2349         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2350         if (!msginfo) return;
2351
2352         flags = msginfo->flags;
2353
2354         gtk_ctree_node_set_foreground(ctree, row, NULL);
2355
2356         /* set new/unread column */
2357         if (MSG_IS_IGNORE_THREAD(flags)) {
2358                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2359                                           ignorethreadxpm, ignorethreadxpmmask);
2360         } else if (MSG_IS_NEW(flags)) {
2361                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2362                                           newxpm, newxpmmask);
2363         } else if (MSG_IS_UNREAD(flags)) {
2364                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2365                                           unreadxpm, unreadxpmmask);
2366         } else if (MSG_IS_REPLIED(flags)) {
2367                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2368                                           repliedxpm, repliedxpmmask);
2369         } else if (MSG_IS_FORWARDED(flags)) {
2370                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2371                                           forwardedxpm, forwardedxpmmask);
2372         } else {
2373                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2374                                         NULL);
2375         }
2376
2377         if (prefs_common.bold_unread &&
2378             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2379              (!GTK_CTREE_ROW(row)->expanded &&
2380               GTK_CTREE_ROW(row)->children &&
2381               summary_have_unread_children(summaryview, row))))
2382                 style = bold_style;
2383
2384         /* set mark column */
2385         if (MSG_IS_DELETED(flags)) {
2386                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2387                                           deletedxpm, deletedxpmmask);
2388                 if (style)
2389                         style = bold_deleted_style;
2390                 else {
2391                         style = small_deleted_style;
2392                 }
2393                         gtk_ctree_node_set_foreground
2394                                 (ctree, row, &summaryview->color_dim);
2395         } else if (MSG_IS_MARKED(flags)) {
2396                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2397                                           markxpm, markxpmmask);
2398         } else if (MSG_IS_MOVE(flags)) {
2399                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2400                 if (style)
2401                         style = bold_marked_style;
2402                 else {
2403                         style = small_marked_style;
2404                 }
2405                         gtk_ctree_node_set_foreground
2406                                 (ctree, row, &summaryview->color_marked);
2407         } else if (MSG_IS_COPY(flags)) {
2408                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2409                 if (style)
2410                         style = bold_marked_style;
2411                 else {
2412                         style = small_marked_style;
2413                 }
2414                         gtk_ctree_node_set_foreground
2415                                 (ctree, row, &summaryview->color_marked);
2416         }
2417         else if ((global_scoring ||
2418                   summaryview->folder_item->prefs->scoring) &&
2419                  (msginfo->score >= summaryview->important_score) &&
2420                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2421                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2422                 gtk_ctree_node_set_foreground(ctree, row,
2423                                               &summaryview->color_important);
2424         } else {
2425                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2426         }
2427
2428         if (MSG_IS_MIME(flags)) {
2429                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2430                                           clipxpm, clipxpmmask);
2431         } else {
2432                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2433         }
2434         if (!style)
2435                 style = small_style;
2436
2437         gtk_ctree_node_set_row_style(ctree, row, style);
2438
2439         if (MSG_GET_COLORLABEL(flags))
2440                 summary_set_colorlabel_color(ctree, row,
2441                                              MSG_GET_COLORLABEL_VALUE(flags));
2442 }
2443
2444 void summary_set_marks_selected(SummaryView *summaryview)
2445 {
2446         summary_set_row_marks(summaryview, summaryview->selected);
2447 }
2448
2449 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2450 {
2451         gboolean changed = FALSE;
2452         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2453         MsgInfo *msginfo;
2454
2455         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2456         if (MSG_IS_DELETED(msginfo->flags))
2457                 summaryview->deleted--;
2458         if (MSG_IS_MOVE(msginfo->flags)) {
2459                 summaryview->moved--;
2460                 changed = TRUE;
2461         }
2462         if (MSG_IS_COPY(msginfo->flags)) {
2463                 summaryview->copied--;
2464                 changed = TRUE;
2465         }
2466         if (changed && !prefs_common.immediate_exec) {
2467                 msginfo->to_folder->op_count--;
2468                 if (msginfo->to_folder->op_count == 0)
2469                         folderview_update_item(msginfo->to_folder, 0);
2470         }
2471         msginfo->to_folder = NULL;
2472         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2473         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2474         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2475         CHANGE_FLAGS(msginfo);
2476         summary_set_row_marks(summaryview, row);
2477         debug_print(_("Message %d is marked\n"), msginfo->msgnum);
2478 }
2479
2480 void summary_mark(SummaryView *summaryview)
2481 {
2482         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2483         GList *cur;
2484
2485         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2486                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2487
2488         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2489         summary_status_show(summaryview);
2490 }
2491
2492 static void summary_mark_row_as_read(SummaryView *summaryview,
2493                                      GtkCTreeNode *row)
2494 {
2495         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2496         MsgInfo *msginfo;
2497
2498         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2499         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2500                 summaryview->newmsgs--;
2501         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2502                 summaryview->unread--;
2503         if (MSG_IS_NEW(msginfo->flags) ||
2504             MSG_IS_UNREAD(msginfo->flags)) {
2505                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
2506                 CHANGE_FLAGS(msginfo);
2507                 summary_set_row_marks(summaryview, row);
2508                 debug_print(_("Message %d is marked as read\n"),
2509                             msginfo->msgnum);
2510         }
2511 }
2512
2513 void summary_mark_as_read(SummaryView *summaryview)
2514 {
2515         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2516         GList *cur;
2517
2518         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2519                 summary_mark_row_as_read(summaryview,
2520                                          GTK_CTREE_NODE(cur->data));
2521
2522         summary_status_show(summaryview);
2523 }
2524
2525 void summary_mark_all_read(SummaryView *summaryview)
2526 {
2527         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2528         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2529         GtkCTreeNode *node;
2530
2531         gtk_clist_freeze(clist);
2532         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2533              node = gtkut_ctree_node_next(ctree, node))
2534                 summary_mark_row_as_read(summaryview, node);
2535         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2536              node = gtkut_ctree_node_next(ctree, node)) {
2537                 if (!GTK_CTREE_ROW(node)->expanded)
2538                         summary_set_row_marks(summaryview, node);
2539         }
2540         gtk_clist_thaw(clist);
2541
2542         summary_status_show(summaryview);
2543 }
2544
2545 static void summary_mark_row_as_unread(SummaryView *summaryview,
2546                                        GtkCTreeNode *row)
2547 {
2548         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2549         MsgInfo *msginfo;
2550
2551         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2552         if (MSG_IS_DELETED(msginfo->flags)) {
2553                 msginfo->to_folder = NULL;
2554                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2555                 summaryview->deleted--;
2556         }
2557         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED | MSG_FORWARDED);
2558         if (!MSG_IS_UNREAD(msginfo->flags)) {
2559                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
2560                 summaryview->unread++;
2561                 debug_print(_("Message %d is marked as unread\n"),
2562                             msginfo->msgnum);
2563         }
2564
2565         CHANGE_FLAGS(msginfo);
2566
2567         summary_set_row_marks(summaryview, row);
2568 }
2569
2570 void summary_mark_as_unread(SummaryView *summaryview)
2571 {
2572         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2573         GList *cur;
2574
2575         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2576                 summary_mark_row_as_unread(summaryview,
2577                                            GTK_CTREE_NODE(cur->data));
2578
2579         summary_status_show(summaryview);
2580 }
2581
2582 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2583 {
2584         gboolean changed = FALSE;
2585         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2586         MsgInfo *msginfo;
2587
2588         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2589
2590         if (MSG_IS_DELETED(msginfo->flags)) return;
2591
2592         if (MSG_IS_MOVE(msginfo->flags)) {
2593                 summaryview->moved--;
2594                 changed = TRUE;
2595         }
2596         if (MSG_IS_COPY(msginfo->flags)) {
2597                 summaryview->copied--;
2598                 changed = TRUE;
2599         }
2600         if (changed && !prefs_common.immediate_exec) {
2601                 msginfo->to_folder->op_count--;
2602                 if (msginfo->to_folder->op_count == 0)
2603                         folderview_update_item(msginfo->to_folder, 0);
2604         }
2605         msginfo->to_folder = NULL;
2606         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2607         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2608         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2609         CHANGE_FLAGS(msginfo);
2610         summaryview->deleted++;
2611
2612         if (!prefs_common.immediate_exec && 
2613             summaryview->folder_item->stype != F_TRASH)
2614                 summary_set_row_marks(summaryview, row);
2615
2616         debug_print(_("Message %s/%d is set to delete\n"),
2617                     msginfo->folder->path, msginfo->msgnum);
2618 }
2619
2620 void summary_delete(SummaryView *summaryview)
2621 {
2622         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2623         FolderItem *item = summaryview->folder_item;
2624         GList *cur;
2625         GtkCTreeNode *sel_last = NULL;
2626         GtkCTreeNode *node;
2627
2628         if (!item || item->folder->type == F_NEWS) return;
2629
2630         if (summary_is_locked(summaryview)) return;
2631
2632         /* if current folder is trash, ask for confirmation */
2633         if (item->stype == F_TRASH) {
2634                 AlertValue aval;
2635
2636                 aval = alertpanel(_("Delete message(s)"),
2637                                   _("Do you really want to delete message(s) from the trash?"),
2638                                   _("Yes"), _("No"), NULL);
2639                 if (aval != G_ALERTDEFAULT) return;
2640         }
2641
2642         /* next code sets current row focus right. We need to find a row
2643          * that is not deleted. */
2644         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next) {
2645                 sel_last = GTK_CTREE_NODE(cur->data);
2646                 summary_delete_row(summaryview, sel_last);
2647         }
2648
2649         node = summary_find_next_msg(summaryview, sel_last);
2650         if (!node)
2651                 node = summary_find_prev_msg(summaryview, sel_last);
2652
2653         if (node) {
2654                 if (sel_last && node == gtkut_ctree_node_next(ctree, sel_last))
2655                         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2656                 else if (sel_last && node == GTK_CTREE_NODE_PREV(sel_last))
2657                         summary_step(summaryview, GTK_SCROLL_STEP_BACKWARD);
2658                 else
2659                         summary_select_node(summaryview, node,
2660                                             summaryview->msg_is_toggled_on);
2661         }
2662
2663         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2664                 summary_execute(summaryview);
2665         else
2666                 summary_status_show(summaryview);
2667 }
2668
2669 void summary_delete_duplicated(SummaryView *summaryview)
2670 {
2671         if (!summaryview->folder_item ||
2672             summaryview->folder_item->folder->type == F_NEWS) return;
2673         if (summaryview->folder_item->stype == F_TRASH) return;
2674
2675         main_window_cursor_wait(summaryview->mainwin);
2676         debug_print(_("Deleting duplicated messages..."));
2677         STATUSBAR_PUSH(summaryview->mainwin,
2678                        _("Deleting duplicated messages..."));
2679
2680         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2681                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2682                                 summaryview);
2683
2684         if (prefs_common.immediate_exec)
2685                 summary_execute(summaryview);
2686         else
2687                 summary_status_show(summaryview);
2688
2689         debug_print(_("done.\n"));
2690         STATUSBAR_POP(summaryview->mainwin);
2691         main_window_cursor_normal(summaryview->mainwin);
2692 }
2693
2694 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2695                                            SummaryView *summaryview)
2696 {
2697         GtkCTreeNode *found;
2698         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2699
2700         if (!msginfo->msgid || !*msginfo->msgid) return;
2701
2702         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2703
2704         if (found && found != node)
2705                 summary_delete_row(summaryview, node);
2706 }
2707
2708 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2709 {
2710         gboolean changed = FALSE;
2711         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2712         MsgInfo *msginfo;
2713
2714         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2715         if (MSG_IS_DELETED(msginfo->flags))
2716                 summaryview->deleted--;
2717         if (MSG_IS_MOVE(msginfo->flags)) {
2718                 summaryview->moved--;
2719                 changed = TRUE;
2720         }
2721         if (MSG_IS_COPY(msginfo->flags)) {
2722                 summaryview->copied--;
2723                 changed = TRUE;
2724         }
2725         if (changed && !prefs_common.immediate_exec) {
2726                 msginfo->to_folder->op_count--;
2727                 if (msginfo->to_folder->op_count == 0)
2728                         folderview_update_item(msginfo->to_folder, 0);
2729         }
2730         msginfo->to_folder = NULL;
2731         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2732         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE | MSG_COPY);
2733         CHANGE_FLAGS(msginfo);
2734         summary_set_row_marks(summaryview, row);
2735
2736         debug_print(_("Message %s/%d is unmarked\n"),
2737                     msginfo->folder->path, msginfo->msgnum);
2738 }
2739
2740 void summary_unmark(SummaryView *summaryview)
2741 {
2742         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2743         GList *cur;
2744
2745         for (cur = GTK_CLIST(ctree)->selection; cur != NULL;
2746              cur = cur->next)
2747                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2748
2749         summary_status_show(summaryview);
2750 }
2751
2752 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2753                                 FolderItem *to_folder)
2754 {
2755         gboolean changed = FALSE;
2756         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2757         MsgInfo *msginfo;
2758
2759         g_return_if_fail(to_folder != NULL);
2760
2761         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2762         if (MSG_IS_MOVE(msginfo->flags)) {
2763                 if (!prefs_common.immediate_exec) {
2764                         msginfo->to_folder->op_count--;
2765                         if (msginfo->to_folder->op_count == 0) {
2766                                 folderview_update_item(msginfo->to_folder, 0);
2767                                 changed = TRUE;
2768                         }
2769                 }
2770         }
2771         msginfo->to_folder = to_folder;
2772         if (MSG_IS_DELETED(msginfo->flags))
2773                 summaryview->deleted--;
2774         if (MSG_IS_COPY(msginfo->flags)) {
2775                 summaryview->copied--;
2776                 if (!prefs_common.immediate_exec)
2777                         msginfo->to_folder->op_count--;
2778         }
2779         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2780         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
2781         if (!MSG_IS_MOVE(msginfo->flags)) {
2782                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
2783                 summaryview->moved++;
2784                 changed = TRUE;
2785         }
2786         if (!prefs_common.immediate_exec) {
2787                 summary_set_row_marks(summaryview, row);
2788                 if (changed) {
2789                         msginfo->to_folder->op_count++;
2790                         if (msginfo->to_folder->op_count == 1)
2791                                 folderview_update_item(msginfo->to_folder, 0);
2792                 }
2793         }
2794
2795         debug_print(_("Message %d is set to move to %s\n"),
2796                     msginfo->msgnum, to_folder->path);
2797 }
2798
2799 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2800 {
2801         GList *cur;
2802
2803         if (!to_folder) return;
2804         if (!summaryview->folder_item ||
2805             summaryview->folder_item->folder->type == F_NEWS) return;
2806
2807         if (summary_is_locked(summaryview)) return;
2808
2809         if (summaryview->folder_item == to_folder) {
2810                 alertpanel_notice(_("Destination is same as current folder."));
2811                 return;
2812         }
2813
2814         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2815              cur != NULL; cur = cur->next)
2816                 summary_move_row_to
2817                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2818
2819         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2820
2821         if (prefs_common.immediate_exec)
2822                 summary_execute(summaryview);
2823         else {
2824                 summary_status_show(summaryview);
2825
2826                 folderview_update_item(to_folder, 0);
2827         }
2828 }
2829
2830 void summary_move_to(SummaryView *summaryview)
2831 {
2832         FolderItem *to_folder;
2833
2834         if (!summaryview->folder_item ||
2835             summaryview->folder_item->folder->type == F_NEWS) return;
2836
2837         to_folder = foldersel_folder_sel(NULL, NULL);
2838         summary_move_selected_to(summaryview, to_folder);
2839 }
2840
2841 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2842                                 FolderItem *to_folder)
2843 {
2844         gboolean changed = FALSE;
2845         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2846         MsgInfo *msginfo;
2847
2848         g_return_if_fail(to_folder != NULL);
2849
2850         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2851         if (MSG_IS_COPY(msginfo->flags)) {
2852                 if (!prefs_common.immediate_exec) {
2853                         msginfo->to_folder->op_count--;
2854                         if (msginfo->to_folder->op_count == 0) {
2855                                 folderview_update_item(msginfo->to_folder, 0);
2856                                 changed = TRUE;
2857                         }
2858                 }
2859         }
2860         msginfo->to_folder = to_folder;
2861         if (MSG_IS_DELETED(msginfo->flags))
2862                 summaryview->deleted--;
2863         if (MSG_IS_MOVE(msginfo->flags)) {
2864                 summaryview->moved--;
2865                 if (!prefs_common.immediate_exec)
2866                         msginfo->to_folder->op_count--;
2867         }
2868         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_MARKED | MSG_DELETED);
2869         MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MOVE);
2870         if (!MSG_IS_COPY(msginfo->flags)) {
2871                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_COPY);
2872                 summaryview->copied++;
2873                 changed = TRUE;
2874         }
2875         if (!prefs_common.immediate_exec) {
2876                 summary_set_row_marks(summaryview, row);
2877                 if (changed) {
2878                         msginfo->to_folder->op_count++;
2879                         if (msginfo->to_folder->op_count == 1)
2880                                 folderview_update_item(msginfo->to_folder, 0);
2881                 }
2882         }
2883
2884         debug_print(_("Message %d is set to copy to %s\n"),
2885                     msginfo->msgnum, to_folder->path);
2886 }
2887
2888 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2889 {
2890         GList *cur;
2891
2892         if (!to_folder) return;
2893         if (!summaryview->folder_item ||
2894             summaryview->folder_item->folder->type == F_NEWS) return;
2895
2896         if (summary_is_locked(summaryview)) return;
2897
2898         if (summaryview->folder_item == to_folder) {
2899                 alertpanel_notice
2900                         (_("Destination to copy is same as current folder."));
2901                 return;
2902         }
2903
2904         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2905              cur != NULL; cur = cur->next)
2906                 summary_copy_row_to
2907                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2908
2909         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2910
2911         if (prefs_common.immediate_exec)
2912                 summary_execute(summaryview);
2913         else {
2914                 summary_status_show(summaryview);
2915
2916                 folderview_update_item(to_folder, 0);
2917         }
2918 }
2919
2920 void summary_copy_to(SummaryView *summaryview)
2921 {
2922         FolderItem *to_folder;
2923
2924         if (!summaryview->folder_item ||
2925             summaryview->folder_item->folder->type == F_NEWS) return;
2926
2927         to_folder = foldersel_folder_sel(NULL, NULL);
2928         summary_copy_selected_to(summaryview, to_folder);
2929 }
2930
2931 void summary_add_address(SummaryView *summaryview)
2932 {
2933         MsgInfo *msginfo;
2934         gchar *from;
2935
2936         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2937                                               summaryview->selected);
2938         if (!msginfo) return;
2939
2940         Xstrdup_a(from, msginfo->from, return);
2941         eliminate_address_comment(from);
2942         extract_address(from);
2943         addressbook_add_contact(msginfo->fromname, from, NULL);
2944 }
2945
2946 void summary_select_all(SummaryView *summaryview)
2947 {
2948         if (summaryview->messages >= 500) {
2949                 STATUSBAR_PUSH(summaryview->mainwin,
2950                                _("Selecting all messages..."));
2951                 main_window_cursor_wait(summaryview->mainwin);
2952         }
2953
2954         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
2955
2956         if (summaryview->messages >= 500) {
2957                 STATUSBAR_POP(summaryview->mainwin);
2958                 main_window_cursor_normal(summaryview->mainwin);
2959         }
2960 }
2961
2962 void summary_unselect_all(SummaryView *summaryview)
2963 {
2964         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
2965 }
2966
2967 void summary_save_as(SummaryView *summaryview)
2968 {
2969         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2970         MsgInfo *msginfo;
2971         gchar *src, *dest;
2972
2973         if (!summaryview->selected) return;
2974         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2975         if (!msginfo) return;
2976
2977         dest = filesel_select_file(_("Save as"), NULL);
2978         if (!dest) return;
2979         if (is_file_exist(dest)) {
2980                 AlertValue aval;
2981
2982                 aval = alertpanel(_("Overwrite"),
2983                                   _("Overwrite existing file?"),
2984                                   _("OK"), _("Cancel"), NULL);
2985                 if (G_ALERTDEFAULT != aval) return;
2986         }
2987
2988         src = procmsg_get_message_file(msginfo);
2989         copy_file(src, dest);
2990         g_free(src);
2991 }
2992
2993 void summary_print(SummaryView *summaryview)
2994 {
2995         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2996         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2997         MsgInfo *msginfo;
2998         GList *cur;
2999         gchar *cmdline;
3000         gchar *p;
3001
3002         if (clist->selection == NULL) return;
3003
3004         cmdline = input_dialog(_("Print"),
3005                                _("Enter the print command line:\n"
3006                                  "(`%s' will be replaced with file name)"),
3007                                prefs_common.print_cmd);
3008         if (!cmdline) return;
3009         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3010             strchr(p + 2, '%')) {
3011                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3012                                  cmdline);
3013                 g_free(cmdline);
3014                 return;
3015         }
3016
3017         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3018                 msginfo = gtk_ctree_node_get_row_data
3019                         (ctree, GTK_CTREE_NODE(cur->data));
3020                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3021         }
3022
3023         g_free(cmdline);
3024 }
3025
3026 gboolean summary_execute(SummaryView *summaryview)
3027 {
3028         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3029         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3030         GtkCTreeNode *node, *next;
3031
3032         if (!summaryview->folder_item ||
3033             summaryview->folder_item->folder->type == F_NEWS) return FALSE;
3034
3035         if (summary_is_locked(summaryview)) return FALSE;
3036         summary_lock(summaryview);
3037
3038         gtk_clist_freeze(clist);
3039
3040         if (summaryview->folder_item->threaded)
3041                 summary_unthread_for_exec(summaryview);
3042
3043         summary_execute_move(summaryview);
3044         summary_execute_copy(summaryview);
3045         summary_execute_delete(summaryview);
3046
3047         node = GTK_CTREE_NODE(clist->row_list);
3048         while (node != NULL) {
3049                 next = gtkut_ctree_node_next(ctree, node);
3050                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
3051                         if (node == summaryview->displayed) {
3052                                 messageview_clear(summaryview->messageview);
3053                                 summaryview->displayed = NULL;
3054                         }
3055                         if (GTK_CTREE_ROW(node)->children != NULL)
3056                                 g_warning("summary_execute(): children != NULL\n");
3057                         else
3058                                 gtk_ctree_remove_node(ctree, node);
3059                 }
3060                 node = next;
3061         }
3062
3063         if (summaryview->folder_item->threaded)
3064                 summary_thread_build(summaryview);
3065
3066         summaryview->selected = clist->selection ?
3067                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3068
3069         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3070                 menu_set_insensitive_all
3071                         (GTK_MENU_SHELL(summaryview->popupmenu));
3072                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3073         } else
3074                 gtk_widget_grab_focus(summaryview->ctree);
3075
3076         summary_update_status(summaryview);
3077         summary_status_show(summaryview);
3078
3079         summary_write_cache(summaryview);
3080
3081         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3082
3083         gtk_clist_thaw(clist);
3084
3085         summary_unlock(summaryview);
3086         return TRUE;
3087 }
3088
3089 static void summary_execute_move(SummaryView *summaryview)
3090 {
3091         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3092         GSList *cur;
3093
3094         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3095
3096         /* search moving messages and execute */
3097         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3098                                 summaryview);
3099
3100         if (summaryview->mlist) {
3101                 procmsg_move_messages(summaryview->mlist);
3102
3103                 folder_item_scan_foreach(summaryview->folder_table);
3104                 folderview_update_item_foreach(summaryview->folder_table);
3105
3106                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3107                         procmsg_msginfo_free((MsgInfo *)cur->data);
3108                 g_slist_free(summaryview->mlist);
3109                 summaryview->mlist = NULL;
3110         }
3111
3112         g_hash_table_destroy(summaryview->folder_table);
3113         summaryview->folder_table = NULL;
3114 }
3115
3116 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3117                                       gpointer data)
3118 {
3119         SummaryView *summaryview = data;
3120         MsgInfo *msginfo;
3121
3122         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3123
3124         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3125                 g_hash_table_insert(summaryview->folder_table,
3126                                     msginfo->to_folder, GINT_TO_POINTER(1));
3127
3128                 summaryview->mlist =
3129                         g_slist_append(summaryview->mlist, msginfo);
3130                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3131
3132                 if (msginfo->msgid && *msginfo->msgid &&
3133                     node == g_hash_table_lookup(summaryview->msgid_table,
3134                                                 msginfo->msgid))
3135                         g_hash_table_remove(summaryview->msgid_table,
3136                                             msginfo->msgid);
3137         }
3138 }
3139
3140 static void summary_execute_copy(SummaryView *summaryview)
3141 {
3142         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3143
3144         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3145
3146         /* search copying messages and execute */
3147         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3148                                 summaryview);
3149
3150         if (summaryview->mlist) {
3151                 procmsg_copy_messages(summaryview->mlist);
3152
3153                 folder_item_scan_foreach(summaryview->folder_table);
3154                 folderview_update_item_foreach(summaryview->folder_table);
3155
3156                 g_slist_free(summaryview->mlist);
3157                 summaryview->mlist = NULL;
3158         }
3159
3160         g_hash_table_destroy(summaryview->folder_table);
3161         summaryview->folder_table = NULL;
3162 }
3163
3164 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3165                                       gpointer data)
3166 {
3167         SummaryView *summaryview = data;
3168         MsgInfo *msginfo;
3169
3170         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3171
3172         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3173                 g_hash_table_insert(summaryview->folder_table,
3174                                     msginfo->to_folder, GINT_TO_POINTER(1));
3175
3176                 summaryview->mlist =
3177                         g_slist_append(summaryview->mlist, msginfo);
3178
3179                 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_COPY);
3180                 summary_set_row_marks(summaryview, node);
3181         }
3182 }
3183
3184 static void summary_execute_delete(SummaryView *summaryview)
3185 {
3186         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3187         FolderItem *trash;
3188         GSList *cur;
3189
3190         trash = summaryview->folder_item->folder->trash;
3191         if (summaryview->folder_item->folder->type == F_MH) {
3192                 g_return_if_fail(trash != NULL);
3193         }
3194
3195         /* search deleting messages and execute */
3196         gtk_ctree_pre_recursive
3197                 (ctree, NULL, summary_execute_delete_func, summaryview);
3198
3199         if (!summaryview->mlist) return;
3200
3201         if (summaryview->folder_item != trash)
3202                 folder_item_move_msgs_with_dest(trash, summaryview->mlist);
3203         else
3204                 folder_item_remove_msgs(trash, summaryview->mlist);
3205
3206         for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3207                 procmsg_msginfo_free((MsgInfo *)cur->data);
3208
3209         g_slist_free(summaryview->mlist);
3210         summaryview->mlist = NULL;
3211
3212         if (summaryview->folder_item != trash) {
3213                 folder_item_scan(trash);
3214                 folderview_update_item(trash, FALSE);
3215         }
3216 }
3217
3218 static void summary_execute_delete_func(GtkCTree *ctree, GtkCTreeNode *node,
3219                                         gpointer data)
3220 {
3221         SummaryView *summaryview = data;
3222         MsgInfo *msginfo;
3223
3224         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3225
3226         if (msginfo && MSG_IS_DELETED(msginfo->flags)) {
3227                 summaryview->mlist =
3228                         g_slist_append(summaryview->mlist, msginfo);
3229                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3230
3231                 if (msginfo->msgid && *msginfo->msgid &&
3232                     node == g_hash_table_lookup(summaryview->msgid_table,
3233                                                 msginfo->msgid))
3234                         g_hash_table_remove(summaryview->msgid_table,
3235                                             msginfo->msgid);
3236
3237                 if (msginfo->subject && 
3238                     node == subject_table_lookup(summaryview->subject_table, 
3239                                                 msginfo->subject)) {
3240                         gchar *s = msginfo->subject + (g_strncasecmp(msginfo->subject, "Re: ", 4) == 0 ? 4 : 0);
3241                         g_hash_table_remove(summaryview->subject_table, s);
3242                 }                       
3243         }
3244 }
3245
3246 /* thread functions */
3247
3248 void summary_thread_build(SummaryView *summaryview)
3249 {
3250         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3251         GtkCTreeNode *node;
3252         GtkCTreeNode *next;
3253         GtkCTreeNode *parent;
3254         MsgInfo *msginfo;
3255
3256         summary_lock(summaryview);
3257
3258         debug_print(_("Building threads..."));
3259         STATUSBAR_PUSH(summaryview->mainwin, _("Building threads..."));
3260         main_window_cursor_wait(summaryview->mainwin);
3261
3262         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3263                                          summary_tree_expanded, summaryview);
3264         gtk_clist_freeze(GTK_CLIST(ctree));
3265
3266         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3267         while (node) {
3268                 next = GTK_CTREE_ROW(node)->sibling;
3269
3270                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3271
3272                 /* alfons - claws seems to prefer subject threading before
3273                  * inreplyto threading. we should look more deeply in this,
3274                  * because inreplyto should have precedence... */
3275                 if (msginfo && msginfo->inreplyto) {
3276                         parent = g_hash_table_lookup(summaryview->msgid_table,
3277                                                      msginfo->inreplyto);
3278                         if (parent && parent != node) {
3279                                 gtk_ctree_move(ctree, node, parent, NULL);
3280                                 gtk_ctree_expand(ctree, node);
3281                         }
3282                 }
3283                 else if (msginfo && msginfo->subject) {
3284                         parent = g_hash_table_lookup
3285                                         (summaryview->subject_table, msginfo->subject);
3286                         if (parent && parent != node) {
3287                                 gtk_ctree_move(ctree, node, parent, NULL);
3288                                 gtk_ctree_expand(ctree, node);
3289                         }
3290                 }
3291
3292                 node = next;
3293         }
3294
3295         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3296
3297         while (node) {
3298                 next = GTK_CTREE_NODE_NEXT(node);
3299                 if (prefs_common.expand_thread)
3300                         gtk_ctree_expand(ctree, node);
3301                 if (prefs_common.bold_unread &&
3302                     GTK_CTREE_ROW(node)->children)
3303                         summary_set_row_marks(summaryview, node);
3304                 node = next;
3305         }
3306
3307         gtk_clist_thaw(GTK_CLIST(ctree));
3308         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3309                                            summary_tree_expanded, summaryview);
3310
3311         debug_print(_("done.\n"));
3312         STATUSBAR_POP(summaryview->mainwin);
3313         main_window_cursor_normal(summaryview->mainwin);
3314
3315         summary_unlock(summaryview);
3316 }
3317
3318 static void summary_thread_init(SummaryView *summaryview)
3319 {
3320         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3321         GtkCTreeNode *node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3322         GtkCTreeNode *next;
3323
3324         if (prefs_common.expand_thread) {
3325                 while (node) {
3326                         next = GTK_CTREE_ROW(node)->sibling;
3327                         if (GTK_CTREE_ROW(node)->children)
3328                                 gtk_ctree_expand(ctree, node);
3329                         node = next;
3330                 }
3331         } else if (prefs_common.bold_unread) {
3332                 while (node) {
3333                         next = GTK_CTREE_ROW(node)->sibling;
3334                         if (GTK_CTREE_ROW(node)->children)
3335                                 summary_set_row_marks(summaryview, node);
3336                         node = next;
3337                 }
3338         }
3339 }
3340
3341 void summary_unthread(SummaryView *summaryview)
3342 {
3343         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3344         GtkCTreeNode *node;
3345         GtkCTreeNode *child;
3346         GtkCTreeNode *sibling;
3347         GtkCTreeNode *next_child;
3348
3349         summary_lock(summaryview);
3350
3351         debug_print(_("Unthreading..."));
3352         STATUSBAR_PUSH(summaryview->mainwin, _("Unthreading..."));
3353         main_window_cursor_wait(summaryview->mainwin);
3354
3355         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
3356                                          summary_tree_collapsed, summaryview);
3357         gtk_clist_freeze(GTK_CLIST(ctree));
3358
3359         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3360              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3361                 child = GTK_CTREE_ROW(node)->children;
3362                 sibling = GTK_CTREE_ROW(node)->sibling;
3363
3364                 while (child != NULL) {
3365                         next_child = GTK_CTREE_ROW(child)->sibling;
3366                         gtk_ctree_move(ctree, child, NULL, sibling);
3367                         child = next_child;
3368                 }
3369         }
3370
3371         gtk_clist_thaw(GTK_CLIST(ctree));
3372         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
3373                                            summary_tree_collapsed, summaryview);
3374
3375         debug_print(_("done.\n"));
3376         STATUSBAR_POP(summaryview->mainwin);
3377         main_window_cursor_normal(summaryview->mainwin);
3378
3379         summary_unlock(summaryview);
3380 }
3381
3382 static void summary_unthread_for_exec(SummaryView *summaryview)
3383 {
3384         GtkCTreeNode *node;
3385         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3386
3387         summary_lock(summaryview);
3388
3389         debug_print(_("Unthreading for execution..."));
3390
3391         gtk_clist_freeze(GTK_CLIST(ctree));
3392
3393         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
3394              node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
3395                 summary_unthread_for_exec_func(ctree, node, NULL);
3396         }
3397
3398         gtk_clist_thaw(GTK_CLIST(ctree));
3399
3400         debug_print(_("done.\n"));
3401
3402         summary_unlock(summaryview);
3403 }
3404
3405 static void summary_unthread_for_exec_func(GtkCTree *ctree, GtkCTreeNode *node,
3406                                            gpointer data)
3407 {
3408         MsgInfo *msginfo;
3409         GtkCTreeNode *top_parent;
3410         GtkCTreeNode *child;
3411         GtkCTreeNode *sibling;
3412
3413         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3414
3415         if (!msginfo ||
3416             (!MSG_IS_MOVE(msginfo->flags) &&
3417              !MSG_IS_DELETED(msginfo->flags)))
3418                 return;
3419         child = GTK_CTREE_ROW(node)->children;
3420         if (!child) return;
3421
3422         for (top_parent = node;
3423              GTK_CTREE_ROW(top_parent)->parent != NULL;
3424              top_parent = GTK_CTREE_ROW(top_parent)->parent)
3425                 ;
3426         sibling = GTK_CTREE_ROW(top_parent)->sibling;
3427
3428         while (child != NULL) {
3429                 GtkCTreeNode *next_child;
3430
3431                 next_child = GTK_CTREE_ROW(child)->sibling;
3432                 gtk_ctree_move(ctree, child, NULL, sibling);
3433                 child = next_child;
3434         }
3435 }
3436
3437 void summary_filter(SummaryView *summaryview)
3438 {
3439         if (!prefs_common.fltlist) return;
3440
3441         summary_lock(summaryview);
3442
3443         debug_print(_("filtering..."));
3444         STATUSBAR_PUSH(summaryview->mainwin, _("Filtering..."));
3445         main_window_cursor_wait(summaryview->mainwin);
3446
3447         gtk_clist_freeze(GTK_CLIST(summaryview->ctree));
3448
3449         if (prefs_filtering == NULL) {
3450                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3451                                         GTK_CTREE_FUNC(summary_filter_func),
3452                                         summaryview);
3453                 
3454                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3455                 
3456                 if (prefs_common.immediate_exec) {
3457                         summary_unlock(summaryview);
3458                         summary_execute(summaryview);
3459                         summary_lock(summaryview);
3460                 } else
3461                         summary_status_show(summaryview);
3462         }
3463         else {
3464                 summaryview->folder_table = g_hash_table_new(NULL, NULL);
3465
3466                 gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
3467                                         GTK_CTREE_FUNC(summary_filter_func),
3468                                         summaryview);
3469
3470                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));
3471
3472                 folder_item_scan_foreach(summaryview->folder_table);
3473                 folderview_update_item_foreach(summaryview->folder_table);
3474
3475                 g_hash_table_destroy(summaryview->folder_table);
3476                 summaryview->folder_table = NULL;
3477
3478                 summary_show(summaryview, summaryview->folder_item, FALSE);
3479         }
3480
3481         debug_print(_("done.\n"));
3482         STATUSBAR_POP(summaryview->mainwin);
3483         main_window_cursor_normal(summaryview->mainwin);
3484
3485         summary_unlock(summaryview);
3486 }
3487
3488 static void summary_filter_func(GtkCTree *ctree, GtkCTreeNode *node,
3489                                 gpointer data)
3490 {
3491         MsgInfo *msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3492         SummaryView *summaryview = data;
3493         gchar *file;
3494         FolderItem *dest;
3495
3496         if (prefs_filtering == NULL) {
3497                 /* old filtering */
3498                 file = procmsg_get_message_file_path(msginfo);
3499                 dest = filter_get_dest_folder(prefs_common.fltlist, file);
3500                 g_free(file);
3501
3502                 if (dest && strcmp2(dest->path, FILTER_NOT_RECEIVE) != 0 &&
3503                     summaryview->folder_item != dest)
3504                         summary_move_row_to(summaryview, node, dest);
3505         }
3506         else
3507                 filter_msginfo_move_or_delete(prefs_filtering, msginfo,
3508                                               summaryview->folder_table);
3509 }
3510
3511 /* color label */
3512
3513 #define N_COLOR_LABELS colorlabel_get_color_count()
3514
3515 static void summary_colorlabel_menu_item_activate_cb(GtkWidget *widget,
3516                                                      gpointer data)
3517 {
3518         guint color = GPOINTER_TO_UINT(data);
3519         SummaryView *summaryview;
3520
3521         summaryview = gtk_object_get_data(GTK_OBJECT(widget), "summaryview");
3522         g_return_if_fail(summaryview != NULL);
3523
3524         /* "dont_toggle" state set? */
3525         if (gtk_object_get_data(GTK_OBJECT(summaryview->colorlabel_menu),
3526                                 "dont_toggle"))
3527                 return;
3528
3529         summary_set_colorlabel(summaryview, color, NULL);
3530 }
3531
3532 /* summary_set_colorlabel_color() - labelcolor parameter is the color *flag*
3533  * for the messsage; not the color index */
3534 void summary_set_colorlabel_color(GtkCTree *ctree, GtkCTreeNode *node,
3535                                   guint labelcolor)
3536 {
3537         GdkColor color;
3538         GtkStyle *style, *prev_style, *ctree_style;
3539         MsgInfo *msginfo;
3540         gint color_index;
3541
3542         msginfo = gtk_ctree_node_get_row_data(ctree, node);
3543         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_CLABEL_FLAG_MASK);
3544         MSG_SET_COLORLABEL_VALUE(msginfo->flags, labelcolor);
3545
3546         color_index = labelcolor == 0 ? -1 : (gint)labelcolor - 1;
3547         ctree_style = gtk_widget_get_style(GTK_WIDGET(ctree));
3548         prev_style = gtk_ctree_node_get_row_style(ctree, node);
3549
3550         if (color_index < 0 || color_index >= N_COLOR_LABELS) {
3551                 if (!prev_style) return;
3552                 style = gtk_style_copy(prev_style);
3553                 color = ctree_style->fg[GTK_STATE_NORMAL];
3554                 style->fg[GTK_STATE_NORMAL] = color;
3555                 color = ctree_style->fg[GTK_STATE_SELECTED];
3556                 style->fg[GTK_STATE_SELECTED] = color;
3557         } else {
3558                 if (prev_style)
3559                         style = gtk_style_copy(prev_style);
3560                 else
3561                         style = gtk_style_copy(ctree_style);
3562                 color = colorlabel_get_color(color_index);
3563                 style->fg[GTK_STATE_NORMAL] = color;
3564                 /* get the average of label color and selected fg color
3565                    for visibility */
3566                 style->fg[GTK_STATE_SELECTED].red   = (color.red   + ctree_style->fg[GTK_STATE_SELECTED].red  ) / 2;
3567                 style->fg[GTK_STATE_SELECTED].green = (color.green + ctree_style->fg[GTK_STATE_SELECTED].green) / 2;
3568                 style->fg[GTK_STATE_SELECTED].blue  = (color.blue  + ctree_style->fg[GTK_STATE_SELECTED].blue ) / 2;
3569         }
3570
3571         gtk_ctree_node_set_row_style(ctree, node, style);
3572 }
3573
3574 void summary_set_colorlabel(SummaryView *summaryview, guint labelcolor,
3575                             GtkWidget *widget)
3576 {
3577         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3578         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3579         GList *cur;
3580
3581         for (cur = clist->selection; cur != NULL; cur = cur->next)
3582                 summary_set_colorlabel_color(ctree, GTK_CTREE_NODE(cur->data),
3583                                              labelcolor);
3584 }
3585
3586 static void summary_colorlabel_menu_item_activate_item_cb(GtkMenuItem *menu_item,
3587                                                           gpointer data)
3588 {
3589         SummaryView *summaryview;
3590         GtkMenuShell *menu;
3591         GtkCheckMenuItem **items;
3592         gint n;
3593         GList *cur, *sel;
3594
3595         summaryview = (SummaryView *)data;
3596         g_return_if_fail(summaryview != NULL);
3597
3598         sel = GTK_CLIST(summaryview->ctree)->selection;
3599         if (!sel) return;
3600
3601         menu = GTK_MENU_SHELL(summaryview->colorlabel_menu);
3602         g_return_if_fail(menu != NULL);
3603
3604         Xalloca(items, (N_COLOR_LABELS + 1) * sizeof(GtkWidget *), return);
3605
3606         /* NOTE: don't return prematurely because we set the "dont_toggle"
3607          * state for check menu items */
3608         gtk_object_set_data(GTK_OBJECT(menu), "dont_toggle",
3609                             GINT_TO_POINTER(1));
3610
3611         /* clear items. get item pointers. */
3612         for (n = 0, cur = menu->children; cur != NULL; cur = cur->next) {
3613                 if (GTK_IS_CHECK_MENU_ITEM(cur->data)) {
3614                         gtk_check_menu_item_set_state
3615                                 (GTK_CHECK_MENU_ITEM(cur->data), FALSE);
3616                         items[n] = GTK_CHECK_MENU_ITEM(cur->data);
3617                         n++;
3618                 }
3619         }
3620
3621         if (n == (N_COLOR_LABELS + 1)) {
3622                 /* iterate all messages and set the state of the appropriate
3623                  * items */
3624                 for (; sel != NULL; sel = sel->next) {
3625                         MsgInfo *msginfo;
3626                         gint clabel;
3627
3628                         msginfo = gtk_ctree_node_get_row_data
3629                                 (GTK_CTREE(summaryview->ctree),
3630                                  GTK_CTREE_NODE(sel->data));
3631                         if (msginfo) {
3632                                 clabel = MSG_GET_COLORLABEL_VALUE(msginfo->flags);
3633                                 if (!items[clabel]->active)
3634                                         gtk_check_menu_item_set_state
3635                                                 (items[clabel], TRUE);
3636                         }
3637                 }
3638         } else
3639                 g_warning("invalid number of color elements (%d)\n", n);
3640
3641         /* reset "dont_toggle" state */
3642         gtk_object_set_data(GTK_OBJECT(menu), "dont_toggle",
3643                             GINT_TO_POINTER(0));
3644 }
3645
3646 static void summary_colorlabel_menu_create(SummaryView *summaryview)
3647 {
3648         GtkWidget *label_menuitem;
3649         GtkWidget *menu;
3650         GtkWidget *item;
3651         gint i;
3652
3653         label_menuitem = gtk_item_factory_get_item(summaryview->popupfactory,
3654                                                    "/Color label");
3655         gtk_signal_connect(GTK_OBJECT(label_menuitem), "activate",
3656                            GTK_SIGNAL_FUNC(summary_colorlabel_menu_item_activate_item_cb),
3657                            summaryview);
3658         gtk_widget_show(label_menuitem);
3659
3660         menu = gtk_menu_new();
3661
3662         /* create sub items. for the menu item activation callback we pass the
3663          * index of label_colors[] as data parameter. for the None color we
3664          * pass an invalid (high) value. also we attach a data pointer so we
3665          * can always get back the SummaryView pointer. */
3666
3667         item = gtk_check_menu_item_new_with_label(_("None"));
3668         gtk_menu_append(GTK_MENU(menu), item);
3669         gtk_signal_connect(GTK_OBJECT(item), "activate",
3670                            GTK_SIGNAL_FUNC(summary_colorlabel_menu_item_activate_cb),
3671                            GUINT_TO_POINTER(0));
3672         gtk_object_set_data(GTK_OBJECT(item), "summaryview", summaryview);
3673         gtk_widget_show(item);
3674
3675         item = gtk_menu_item_new();
3676         gtk_menu_append(GTK_MENU(menu), item);
3677         gtk_widget_show(item);
3678
3679         /* create pixmap/label menu items */
3680         for (i = 0; i < N_COLOR_LABELS; i++) {
3681                 item = colorlabel_create_check_color_menu_item(i);
3682                 gtk_menu_append(GTK_MENU(menu), item);
3683                 gtk_signal_connect(GTK_OBJECT(item), "activate",
3684                                    GTK_SIGNAL_FUNC(summary_colorlabel_menu_item_activate_cb),
3685                                    GUINT_TO_POINTER(i + 1));
3686                 gtk_object_set_data(GTK_OBJECT(item), "summaryview",
3687                                     summaryview);
3688                 gtk_widget_show(item);
3689         }
3690
3691         gtk_widget_show(menu);
3692         gtk_menu_item_set_submenu(GTK_MENU_ITEM(label_menuitem), menu);
3693         summaryview->colorlabel_menu = menu;
3694 }
3695
3696 static GtkWidget *summary_ctree_create(SummaryView *summaryview)
3697 {
3698         GtkWidget *ctree;
3699         gint *col_pos = summaryview->col_pos;
3700         SummaryColumnState *col_state;
3701         const gchar *titles[N_SUMMARY_COLS];
3702         SummaryColumnType type;
3703         gint pos;
3704
3705         col_state = prefs_summary_column_get_config();
3706         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
3707                 summaryview->col_state[pos] = col_state[pos];
3708                 type = col_state[pos].type;
3709                 col_pos[type] = pos;
3710
3711                 switch (type) {
3712                 case S_COL_NUMBER:
3713                 case S_COL_DATE:
3714                 case S_COL_FROM:
3715                 case S_COL_SUBJECT:
3716                 case S_COL_SCORE:
3717                         if (prefs_common.trans_hdr)
3718                                 titles[pos] = gettext(col_label[type]);
3719                         else
3720                                 titles[pos] = col_label[type];
3721                         break;
3722                 default:
3723                         titles[pos] = gettext(col_label[type]);
3724                 }
3725         }
3726         col_state = summaryview->col_state;
3727
3728         ctree = gtk_sctree_new_with_titles
3729                 (N_SUMMARY_COLS, col_pos[S_COL_SUBJECT], (gchar **)titles);
3730
3731         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_EXTENDED);
3732         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_MARK],
3733                                            GTK_JUSTIFY_CENTER);
3734         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_UNREAD],
3735                                            GTK_JUSTIFY_CENTER);
3736         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_MIME],
3737                                            GTK_JUSTIFY_CENTER);
3738         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_SIZE],
3739                                            GTK_JUSTIFY_RIGHT);
3740         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_NUMBER],
3741                                            GTK_JUSTIFY_RIGHT);
3742         gtk_clist_set_column_justification(GTK_CLIST(ctree), col_pos[S_COL_SCORE],
3743                                            GTK_JUSTIFY_RIGHT);
3744         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_MARK],
3745                                    SUMMARY_COL_MARK_WIDTH);
3746         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_UNREAD],
3747                                    SUMMARY_COL_UNREAD_WIDTH);
3748         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_MIME],
3749                                    SUMMARY_COL_MIME_WIDTH);
3750         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_SUBJECT],
3751                                    prefs_common.summary_col_size[S_COL_SUBJECT]);
3752         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_FROM],
3753                                    prefs_common.summary_col_size[S_COL_FROM]);
3754         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_DATE],
3755                                    prefs_common.summary_col_size[S_COL_DATE]);
3756         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_SIZE],
3757                                    prefs_common.summary_col_size[S_COL_SIZE]);
3758         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_NUMBER],
3759                                    prefs_common.summary_col_size[S_COL_NUMBER]);
3760         gtk_clist_set_column_width(GTK_CLIST(ctree), col_pos[S_COL_SCORE],
3761                                    prefs_common.summary_col_size[S_COL_SCORE]);
3762         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
3763         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
3764                                      GTK_CTREE_EXPANDER_SQUARE);
3765 #if 0
3766         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
3767         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
3768                                      GTK_CTREE_EXPANDER_TRIANGLE);
3769 #endif
3770         gtk_ctree_set_indent(GTK_CTREE(ctree), 18);
3771         gtk_object_set_user_data(GTK_OBJECT(ctree), summaryview);
3772
3773         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
3774                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[pos].button,
3775                                        GTK_CAN_FOCUS);
3776                 gtk_clist_set_column_visibility
3777                         (GTK_CLIST(ctree), pos, col_state[pos].visible);
3778         }
3779
3780         /* connect signal to the buttons for sorting */
3781 #define CLIST_BUTTON_SIGNAL_CONNECT(col, func) \
3782         gtk_signal_connect \
3783                 (GTK_OBJECT(GTK_CLIST(ctree)->column[col_pos[col]].button), \
3784                  "clicked", \
3785                  GTK_SIGNAL_FUNC(func), \
3786                  summaryview)
3787
3788         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_MARK   , summary_mark_clicked);
3789         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_UNREAD , summary_unread_clicked);
3790         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_MIME   , summary_mime_clicked);
3791         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_NUMBER , summary_num_clicked);
3792         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_SIZE   , summary_size_clicked);
3793         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_DATE   , summary_date_clicked);
3794         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_FROM   , summary_from_clicked);
3795         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_SUBJECT, summary_subject_clicked);
3796         CLIST_BUTTON_SIGNAL_CONNECT(S_COL_SCORE,   summary_score_clicked);
3797
3798 #undef CLIST_BUTTON_SIGNAL_CONNECT
3799
3800         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
3801                            GTK_SIGNAL_FUNC(summary_selected), summaryview);
3802         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
3803                            GTK_SIGNAL_FUNC(summary_button_pressed),
3804                            summaryview);
3805         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
3806                            GTK_SIGNAL_FUNC(summary_button_released),
3807                            summaryview);
3808         gtk_signal_connect(GTK_OBJECT(ctree), "key_press_event",
3809                            GTK_SIGNAL_FUNC(summary_key_pressed), summaryview);
3810         gtk_signal_connect(GTK_OBJECT(ctree), "resize_column",
3811                            GTK_SIGNAL_FUNC(summary_col_resized), summaryview);
3812         gtk_signal_connect(GTK_OBJECT(ctree), "open_row",
3813                            GTK_SIGNAL_FUNC(summary_open_row), summaryview);
3814
3815         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_expand",
3816                                  GTK_SIGNAL_FUNC(summary_tree_expanded),
3817                                  summaryview);
3818         gtk_signal_connect_after(GTK_OBJECT(ctree), "tree_collapse",
3819                                  GTK_SIGNAL_FUNC(summary_tree_collapsed),
3820                                  summaryview);
3821
3822         gtk_signal_connect(GTK_OBJECT(ctree), "start_drag",
3823                            GTK_SIGNAL_FUNC(summary_start_drag),
3824                            summaryview);
3825         gtk_signal_connect(GTK_OBJECT(ctree), "drag_data_get",
3826                            GTK_SIGNAL_FUNC(summary_drag_data_get),
3827                            summaryview);
3828
3829         return ctree;
3830 }
3831
3832 void summary_set_column_order(SummaryView *summaryview)
3833 {
3834         GtkWidget *ctree;
3835         GtkWidget *scrolledwin = summaryview->scrolledwin;
3836         GtkWidget *pixmap;
3837         FolderItem *item;
3838
3839         item = summaryview->folder_item;
3840         summary_clear_all(summaryview);
3841         gtk_widget_destroy(summaryview->ctree);
3842
3843         summaryview->ctree = ctree = summary_ctree_create(summaryview);
3844         pixmap = gtk_pixmap_new(clipxpm, clipxpmmask);
3845         gtk_clist_set_column_widget(GTK_CLIST(ctree),
3846                                     summaryview->col_pos[S_COL_MIME], pixmap);
3847         gtk_widget_show(pixmap);
3848         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
3849                                             GTK_CLIST(ctree)->hadjustment);
3850         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
3851                                             GTK_CLIST(ctree)->vadjustment);
3852         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
3853         gtk_widget_show(ctree);
3854
3855         summary_show(summaryview, item, FALSE);
3856 }
3857
3858
3859 /* callback functions */
3860
3861 static void summary_toggle_pressed(GtkWidget *eventbox, GdkEventButton *event,
3862                                    SummaryView *summaryview)
3863 {
3864         if (!event)
3865                 return;
3866
3867         if (!summaryview->msg_is_toggled_on && summaryview->selected)
3868                 summary_display_msg(summaryview, summaryview->selected, FALSE);
3869         else
3870                 summary_toggle_view(summaryview);
3871 }
3872
3873 static void summary_button_pressed(GtkWidget *ctree, GdkEventButton *event,
3874                                    SummaryView *summaryview)
3875 {
3876         if (!event) return;
3877
3878         if (event->button == 3) {
3879                 /* right clicked */
3880                 gtk_menu_popup(GTK_MENU(summaryview->popupmenu), NULL, NULL,
3881                                NULL, NULL, event->button, event->time);
3882         } else if (event->button == 2) {
3883                 summaryview->display_msg = TRUE;
3884         } else if (event->button == 1) {
3885                 if (!prefs_common.emulate_emacs &&
3886                     summaryview->msg_is_toggled_on)
3887                         summaryview->display_msg = TRUE;
3888         }
3889 }
3890
3891 static void summary_button_released(GtkWidget *ctree, GdkEventButton *event,
3892                                     SummaryView *summaryview)
3893 {
3894 }
3895
3896 void summary_pass_key_press_event(SummaryView *summaryview, GdkEventKey *event)
3897 {
3898         summary_key_pressed(summaryview->ctree, event, summaryview);
3899 }
3900
3901 #define BREAK_ON_MODIFIER_KEY() \
3902         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3903
3904 #define RETURN_IF_LOCKED() \
3905         if (summaryview->mainwin->lock_count) return
3906
3907 #define KEY_PRESS_EVENT_STOP() \
3908         if (gtk_signal_n_emissions_by_name \
3909                 (GTK_OBJECT(ctree), "key_press_event") > 0) { \
3910                 gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
3911                                              "key_press_event"); \
3912         }
3913
3914 static void summary_key_pressed(GtkWidget *widget, GdkEventKey *event,
3915                                 SummaryView *summaryview)
3916 {
3917         GtkCTree *ctree = GTK_CTREE(widget);
3918         GtkCTreeNode *node;
3919         FolderItem *to_folder;
3920
3921         if (summary_is_locked(summaryview)) return;
3922         if (!event) return;
3923
3924         switch (event->keyval) {
3925         case GDK_g:             /* Go */
3926         case GDK_G:
3927                 RETURN_IF_LOCKED();
3928                 BREAK_ON_MODIFIER_KEY();
3929                 KEY_PRESS_EVENT_STOP();
3930                 to_folder = foldersel_folder_sel(NULL, NULL);
3931                 if (to_folder) {
3932                         debug_print(_("Go to %s\n"), to_folder->path);
3933                         folderview_select(summaryview->folderview, to_folder);
3934                 }
3935                 return;
3936         case GDK_w:             /* Write new message */
3937                 BREAK_ON_MODIFIER_KEY();
3938                 if (summaryview->folder_item) {
3939                         PrefsAccount *ac;
3940                         ac = summaryview->folder_item->folder->account;
3941                         if (ac && ac->protocol == A_NNTP)
3942                                 compose_new_with_recipient
3943             &