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