* src/folderview.c
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #include <regex.h>
50
51 #include "intl.h"
52 #include "main.h"
53 #include "menu.h"
54 #include "mainwindow.h"
55 #include "folderview.h"
56 #include "summaryview.h"
57 #include "messageview.h"
58 #include "foldersel.h"
59 #include "procmsg.h"
60 #include "procheader.h"
61 #include "sourcewindow.h"
62 #include "prefs_common.h"
63 #include "prefs_summary_column.h"
64 #include "prefs_filter.h"
65 #include "prefs_filtering.h"
66 #include "account.h"
67 #include "compose.h"
68 #include "utils.h"
69 #include "gtkutils.h"
70 #include "stock_pixmap.h"
71 #include "filesel.h"
72 #include "alertpanel.h"
73 #include "inputdialog.h"
74 #include "statusbar.h"
75 #include "filter.h"
76 #include "folder.h"
77 #include "colorlabel.h"
78 #include "inc.h"
79 #include "imap.h"
80 #include "addressbook.h"
81 #include "addr_compl.h"
82 #include "scoring.h"
83 #include "prefs_folder_item.h"
84 #include "filtering.h"
85 #include "string_match.h"
86 #include "toolbar.h"
87
88 #define SUMMARY_COL_MARK_WIDTH          10
89 #define SUMMARY_COL_UNREAD_WIDTH        13
90 #define SUMMARY_COL_LOCKED_WIDTH        13
91 #define SUMMARY_COL_MIME_WIDTH          11
92
93 static GdkFont *boldfont;
94 static GdkFont *smallfont;
95
96 static GtkStyle *bold_style;
97 static GtkStyle *bold_marked_style;
98 static GtkStyle *bold_deleted_style;
99 static GtkStyle *small_style;
100 static GtkStyle *small_marked_style;
101 static GtkStyle *small_deleted_style;
102
103 static GdkPixmap *markxpm;
104 static GdkBitmap *markxpmmask;
105 static GdkPixmap *deletedxpm;
106 static GdkBitmap *deletedxpmmask;
107
108 static GdkPixmap *newxpm;
109 static GdkBitmap *newxpmmask;
110 static GdkPixmap *unreadxpm;
111 static GdkBitmap *unreadxpmmask;
112 static GdkPixmap *repliedxpm;
113 static GdkBitmap *repliedxpmmask;
114 static GdkPixmap *forwardedxpm;
115 static GdkBitmap *forwardedxpmmask;
116 static GdkPixmap *ignorethreadxpm;
117 static GdkBitmap *ignorethreadxpmmask;
118 static GdkPixmap *lockedxpm;
119 static GdkBitmap *lockedxpmmask;
120
121 static GdkPixmap *clipxpm;
122 static GdkBitmap *clipxpmmask;
123 static GdkPixmap *keyxpm;
124 static GdkBitmap *keyxpmmask;
125 static GdkPixmap *clipkeyxpm;
126 static GdkBitmap *clipkeyxpmmask;
127
128 static void summary_free_msginfo_func   (GtkCTree               *ctree,
129                                          GtkCTreeNode           *node,
130                                          gpointer                data);
131 static void summary_set_marks_func      (GtkCTree               *ctree,
132                                          GtkCTreeNode           *node,
133                                          gpointer                data);
134
135 static void summary_set_menu_sensitive  (SummaryView            *summaryview);
136
137 static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
138                                              guint action);
139
140 static guint summary_get_msgnum         (SummaryView            *summaryview,
141                                          GtkCTreeNode           *node);
142
143 static GtkCTreeNode *summary_find_prev_msg
144                                         (SummaryView            *summaryview,
145                                          GtkCTreeNode           *current_node);
146 static GtkCTreeNode *summary_find_next_msg
147                                         (SummaryView            *summaryview,
148                                          GtkCTreeNode           *current_node);
149
150 static GtkCTreeNode *summary_find_prev_flagged_msg
151                                         (SummaryView    *summaryview,
152                                          GtkCTreeNode   *current_node,
153                                          MsgPermFlags    flags,
154                                          gboolean        start_from_prev);
155 static GtkCTreeNode *summary_find_next_flagged_msg
156                                         (SummaryView    *summaryview,
157                                          GtkCTreeNode   *current_node,
158                                          MsgPermFlags    flags,
159                                          gboolean        start_from_next);
160
161 static GtkCTreeNode *summary_find_msg_by_msgnum
162                                         (SummaryView            *summaryview,
163                                          guint                   msgnum);
164
165 static void summary_update_status       (SummaryView            *summaryview);
166
167 /* display functions */
168 static void summary_status_show         (SummaryView            *summaryview);
169 static void summary_set_column_titles   (SummaryView            *summaryview);
170 static void summary_set_ctree_from_list (SummaryView            *summaryview,
171                                          GSList                 *mlist);
172 static void summary_set_header          (SummaryView            *summaryview,
173                                          gchar                  *text[],
174                                          MsgInfo                *msginfo);
175 static void summary_display_msg         (SummaryView            *summaryview,
176                                          GtkCTreeNode           *row);
177 static void summary_display_msg_full    (SummaryView            *summaryview,
178                                          GtkCTreeNode           *row,
179                                          gboolean                new_window,
180                                          gboolean                all_headers);
181 static void summary_set_row_marks       (SummaryView            *summaryview,
182                                          GtkCTreeNode           *row);
183
184 /* message handling */
185 static void summary_mark_row            (SummaryView            *summaryview,
186                                          GtkCTreeNode           *row);
187 static void summary_lock_row            (SummaryView            *summaryview,
188                                          GtkCTreeNode           *row);
189 static void summary_mark_row_as_read    (SummaryView            *summaryview,
190                                          GtkCTreeNode           *row);
191 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
192                                          GtkCTreeNode           *row);
193 static void summary_delete_row          (SummaryView            *summaryview,
194                                          GtkCTreeNode           *row);
195 static void summary_unmark_row          (SummaryView            *summaryview,
196                                          GtkCTreeNode           *row);
197 static void summary_move_row_to         (SummaryView            *summaryview,
198                                          GtkCTreeNode           *row,
199                                          FolderItem             *to_folder);
200 static void summary_copy_row_to         (SummaryView            *summaryview,
201                                          GtkCTreeNode           *row,
202                                          FolderItem             *to_folder);
203
204 static void summary_delete_duplicated_func
205                                         (GtkCTree               *ctree,
206                                          GtkCTreeNode           *node,
207                                          SummaryView            *summaryview);
208
209 static void summary_execute_move        (SummaryView            *summaryview);
210 static void summary_execute_move_func   (GtkCTree               *ctree,
211                                          GtkCTreeNode           *node,
212                                          gpointer                data);
213 static void summary_execute_copy        (SummaryView            *summaryview);
214 static void summary_execute_copy_func   (GtkCTree               *ctree,
215                                          GtkCTreeNode           *node,
216                                          gpointer                data);
217 static void summary_execute_delete      (SummaryView            *summaryview);
218 static void summary_execute_delete_func (GtkCTree               *ctree,
219                                          GtkCTreeNode           *node,
220                                          gpointer                data);
221
222 static void summary_thread_init         (SummaryView            *summaryview);
223 static void summary_ignore_thread       (SummaryView  *summaryview);
224 static void summary_unignore_thread     (SummaryView *summaryview);
225
226 static void summary_unthread_for_exec           (SummaryView    *summaryview);
227 static void summary_unthread_for_exec_func      (GtkCTree       *ctree,
228                                                  GtkCTreeNode   *node,
229                                                  gpointer        data);
230
231 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
232                               GSList * mlist);
233
234 #if 0
235 void summary_processing(SummaryView *summaryview, GSList * mlist);
236 #endif
237 static void summary_filter_func         (GtkCTree               *ctree,
238                                          GtkCTreeNode           *node,
239                                          gpointer                data);
240
241 static void summary_colorlabel_menu_item_activate_cb
242                                           (GtkWidget    *widget,
243                                            gpointer      data);
244 static void summary_colorlabel_menu_item_activate_item_cb
245                                           (GtkMenuItem  *label_menu_item,
246                                            gpointer      data);
247 static void summary_colorlabel_menu_create(SummaryView  *summaryview);
248
249 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
250
251 /* callback functions */
252 static void summary_toggle_pressed      (GtkWidget              *eventbox,
253                                          GdkEventButton         *event,
254                                          SummaryView            *summaryview);
255 static void summary_button_pressed      (GtkWidget              *ctree,
256                                          GdkEventButton         *event,
257                                          SummaryView            *summaryview);
258 static void summary_button_released     (GtkWidget              *ctree,
259                                          GdkEventButton         *event,
260                                          SummaryView            *summaryview);
261 static void summary_key_pressed         (GtkWidget              *ctree,
262                                          GdkEventKey            *event,
263                                          SummaryView            *summaryview);
264 static void summary_open_row            (GtkSCTree              *sctree,
265                                          SummaryView            *summaryview);
266 static void summary_tree_expanded       (GtkCTree               *ctree,
267                                          GtkCTreeNode           *node,
268                                          SummaryView            *summaryview);
269 static void summary_tree_collapsed      (GtkCTree               *ctree,
270                                          GtkCTreeNode           *node,
271                                          SummaryView            *summaryview);
272 static void summary_selected            (GtkCTree               *ctree,
273                                          GtkCTreeNode           *row,
274                                          gint                    column,
275                                          SummaryView            *summaryview);
276 static void summary_col_resized         (GtkCList               *clist,
277                                          gint                    column,
278                                          gint                    width,
279                                          SummaryView            *summaryview);
280 static void summary_reply_cb            (SummaryView            *summaryview,
281                                          guint                   action,
282                                          GtkWidget              *widget);
283 static void summary_execute_cb          (SummaryView            *summaryview,
284                                          guint                   action,
285                                          GtkWidget              *widget);
286 static void summary_show_all_header_cb  (SummaryView            *summaryview,
287                                          guint                   action,
288                                          GtkWidget              *widget);
289
290 static void summary_add_address_cb      (SummaryView            *summaryview,
291                                          guint                   action,
292                                          GtkWidget              *widget);
293 static void summary_create_filter_cb    (SummaryView            *summaryview,
294                                          guint                   action,
295                                          GtkWidget              *widget);
296
297 static void summary_mark_clicked        (GtkWidget              *button,
298                                          SummaryView            *summaryview);
299 static void summary_unread_clicked      (GtkWidget              *button,
300                                          SummaryView            *summaryview);
301 static void summary_mime_clicked        (GtkWidget              *button,
302                                          SummaryView            *summaryview);
303 static void summary_num_clicked         (GtkWidget              *button,
304                                          SummaryView            *summaryview);
305 static void summary_score_clicked       (GtkWidget *button,
306                                          SummaryView *summaryview);
307 static void summary_size_clicked        (GtkWidget              *button,
308                                          SummaryView            *summaryview);
309 static void summary_date_clicked        (GtkWidget              *button,
310                                          SummaryView            *summaryview);
311 static void summary_from_clicked        (GtkWidget              *button,
312                                          SummaryView            *summaryview);
313 static void summary_subject_clicked     (GtkWidget              *button,
314                                          SummaryView            *summaryview);
315 static void summary_score_clicked       (GtkWidget              *button,
316                                          SummaryView            *summaryview);
317 static void summary_locked_clicked      (GtkWidget              *button,
318                                          SummaryView            *summaryview);
319
320 static void summary_start_drag          (GtkWidget        *widget, 
321                                          int button,
322                                          GdkEvent *event,
323                                          SummaryView      *summaryview);
324 static void summary_drag_data_get       (GtkWidget        *widget,
325                                          GdkDragContext   *drag_context,
326                                          GtkSelectionData *selection_data,
327                                          guint             info,
328                                          guint             time,
329                                          SummaryView      *summaryview);
330
331 /* custom compare functions for sorting */
332
333 static gint summary_cmp_by_mark         (GtkCList               *clist,
334                                          gconstpointer           ptr1,
335                                          gconstpointer           ptr2);
336 static gint summary_cmp_by_unread       (GtkCList               *clist,
337                                          gconstpointer           ptr1,
338                                          gconstpointer           ptr2);
339 static gint summary_cmp_by_mime         (GtkCList               *clist,
340                                          gconstpointer           ptr1,
341                                          gconstpointer           ptr2);
342 static gint summary_cmp_by_num          (GtkCList               *clist,
343                                          gconstpointer           ptr1,
344                                          gconstpointer           ptr2);
345 static gint summary_cmp_by_size         (GtkCList               *clist,
346                                          gconstpointer           ptr1,
347                                          gconstpointer           ptr2);
348 static gint summary_cmp_by_date         (GtkCList               *clist,
349                                          gconstpointer           ptr1,
350                                          gconstpointer           ptr2);
351 static gint summary_cmp_by_from         (GtkCList               *clist,
352                                          gconstpointer           ptr1,
353                                          gconstpointer           ptr2);
354 static gint summary_cmp_by_subject      (GtkCList               *clist,
355                                          gconstpointer           ptr1,
356                                          gconstpointer           ptr2);
357 static gint summary_cmp_by_simplified_subject
358         (GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2);
359 static gint summary_cmp_by_score        (GtkCList               *clist,
360                                          gconstpointer           ptr1,
361                                          gconstpointer           ptr2);
362 static gint summary_cmp_by_locked       (GtkCList *clist,
363                                          gconstpointer ptr1, gconstpointer ptr2);
364 static gint summary_cmp_by_label        (GtkCList               *clist,
365                                          gconstpointer           ptr1,
366                                          gconstpointer           ptr2);
367
368 static void news_flag_crosspost         (MsgInfo *msginfo);
369
370 GtkTargetEntry summary_drag_types[1] =
371 {
372         {"text/plain", GTK_TARGET_SAME_APP, TARGET_DUMMY}
373 };
374
375 static GtkItemFactoryEntry summary_popup_entries[] =
376 {
377         {N_("/_Reply"),                 NULL, summary_reply_cb, COMPOSE_REPLY, NULL},
378         {N_("/Repl_y to sender"),       NULL, summary_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
379         {N_("/Follow-up and reply to"), NULL, summary_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
380         {N_("/Reply to a_ll"),          NULL, summary_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
381         {N_("/_Forward"),               NULL, summary_reply_cb, COMPOSE_FORWARD, NULL},
382         {N_("/Redirect"),               NULL, summary_reply_cb, COMPOSE_REDIRECT, NULL},
383         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
384         {N_("/Re-_edit"),               NULL, summary_reedit,   0, NULL},
385         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
386         {N_("/Select _thread"),         NULL, summary_select_thread, 0, NULL},
387         {N_("/Select _all"),            NULL, summary_select_all, 0, NULL},
388         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
389         {N_("/M_ove..."),               NULL, summary_move_to,  0, NULL},
390         {N_("/_Copy..."),               NULL, summary_copy_to,  0, NULL},
391         {N_("/_Delete"),                NULL, summary_delete,   0, NULL},
392         {N_("/E_xecute"),               NULL, summary_execute_cb,       0, NULL},
393         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
394         {N_("/_Mark"),                  NULL, NULL,             0, "<Branch>"},
395         {N_("/_Mark/_Mark"),            NULL, summary_mark,     0, NULL},
396         {N_("/_Mark/_Unmark"),          NULL, summary_unmark,   0, NULL},
397         {N_("/_Mark/---"),              NULL, NULL,             0, "<Separator>"},
398         {N_("/_Mark/Mark as unr_ead"),  NULL, summary_mark_as_unread, 0, NULL},
399         {N_("/_Mark/Mark as rea_d"),    NULL, summary_mark_as_read, 0, NULL},
400         {N_("/_Mark/Mark all read"),    NULL, summary_mark_all_read, 0, NULL},
401         {N_("/_Mark/Ignore thread"),    NULL, summary_ignore_thread, 0, NULL},
402         {N_("/_Mark/Unignore thread"),  NULL, summary_unignore_thread, 0, NULL},
403         {N_("/Color la_bel"),           NULL, NULL,             0, NULL},
404
405         {N_("/---"),                    NULL, NULL,             0, "<Separator>"},
406         {N_("/Add sender to address boo_k"),
407                                         NULL, summary_add_address_cb, 0, NULL},
408         {N_("/Create f_ilter rule"),    NULL, NULL,             0, "<Branch>"},
409         {N_("/Create f_ilter rule/_Automatically"),
410                                         NULL, summary_create_filter_cb, FILTER_BY_AUTO, NULL},
411         {N_("/Create f_ilter rule/by _From"),
412                                         NULL, summary_create_filter_cb, FILTER_BY_FROM, NULL},
413         {N_("/Create f_ilter rule/by _To"),
414                                         NULL, summary_create_filter_cb, FILTER_BY_TO, NULL},
415         {N_("/Create f_ilter rule/by _Subject"),
416                                         NULL, summary_create_filter_cb, FILTER_BY_SUBJECT, 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, "<ToggleItem>"},
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 };
427
428 static const gchar *const col_label[N_SUMMARY_COLS] = {
429         N_("M"),        /* S_COL_MARK    */
430         N_("U"),        /* S_COL_UNREAD  */
431         "",             /* S_COL_MIME    */
432         N_("Subject"),  /* S_COL_SUBJECT */
433         N_("From"),     /* S_COL_FROM    */
434         N_("Date"),     /* S_COL_DATE    */
435         N_("Size"),     /* S_COL_SIZE    */
436         N_("No."),      /* S_COL_NUMBER  */
437         N_("Score"),    /* S_COL_SCORE   */
438         N_("L")         /* S_COL_LOCKED  */
439 };
440
441 SummaryView *summary_create(void)
442 {
443         SummaryView *summaryview;
444         GtkWidget *vbox;
445         GtkWidget *scrolledwin;
446         GtkWidget *ctree;
447         GtkWidget *hbox;
448         GtkWidget *hbox_l;
449         GtkWidget *statlabel_folder;
450         GtkWidget *statlabel_select;
451         GtkWidget *statlabel_msgs;
452         GtkWidget *hbox_spc;
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         hbox_l = gtk_hbox_new(FALSE, 0);
486         gtk_box_pack_start(GTK_BOX(hbox), hbox_l, TRUE, TRUE, 0);
487
488         statlabel_folder = gtk_label_new("");
489         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
490         statlabel_select = gtk_label_new("");
491         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
492
493         /* toggle view button */
494         toggle_eventbox = gtk_event_box_new();
495         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
496         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
497         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
498         gtk_signal_connect(GTK_OBJECT(toggle_eventbox), "button_press_event",
499                            GTK_SIGNAL_FUNC(summary_toggle_pressed),
500                            summaryview);
501
502         statlabel_msgs = gtk_label_new("");
503         gtk_box_pack_end(GTK_BOX(hbox), statlabel_msgs, FALSE, FALSE, 4);
504
505         hbox_spc = gtk_hbox_new(FALSE, 0);
506         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
507
508         /* create popup menu */
509         n_entries = sizeof(summary_popup_entries) /
510                 sizeof(summary_popup_entries[0]);
511         popupmenu = menu_create_items(summary_popup_entries, n_entries,
512                                       "<SummaryView>", &popupfactory,
513                                       summaryview);
514
515         summaryview->vbox = vbox;
516         summaryview->scrolledwin = scrolledwin;
517         summaryview->ctree = ctree;
518         summaryview->hbox = hbox;
519         summaryview->hbox_l = hbox_l;
520         summaryview->statlabel_folder = statlabel_folder;
521         summaryview->statlabel_select = statlabel_select;
522         summaryview->statlabel_msgs = statlabel_msgs;
523         summaryview->toggle_eventbox = toggle_eventbox;
524         summaryview->toggle_arrow = toggle_arrow;
525         summaryview->popupmenu = popupmenu;
526         summaryview->popupfactory = popupfactory;
527         summaryview->lock_count = 0;
528
529         /* CLAWS: need this to get the SummaryView * from
530          * the CList */
531         gtk_object_set_data(GTK_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
532
533         gtk_widget_show_all(vbox);
534
535         return summaryview;
536 }
537
538 void summary_init(SummaryView *summaryview)
539 {
540         GtkStyle *style;
541         GtkWidget *pixmap;
542
543         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
544                          &markxpm, &markxpmmask);
545         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
546                          &deletedxpm, &deletedxpmmask);
547         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
548                          &newxpm, &newxpmmask);
549         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
550                          &unreadxpm, &unreadxpmmask);
551         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
552                          &repliedxpm, &repliedxpmmask);
553         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
554                          &forwardedxpm, &forwardedxpmmask);
555         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
556                          &clipxpm, &clipxpmmask);
557         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
558                          &lockedxpm, &lockedxpmmask);
559         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
560                          &ignorethreadxpm, &ignorethreadxpmmask);
561         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
562                          &clipkeyxpm, &clipkeyxpmmask);
563         stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
564                          &keyxpm, &keyxpmmask);
565
566         if (!small_style) {
567                 small_style = gtk_style_copy
568                         (gtk_widget_get_style(summaryview->ctree));
569                 if (!smallfont)
570                         smallfont = gdk_fontset_load(SMALL_FONT);
571                 small_style->font = smallfont;
572                 small_marked_style = gtk_style_copy(small_style);
573                 small_marked_style->fg[GTK_STATE_NORMAL] =
574                         summaryview->color_marked;
575                 small_deleted_style = gtk_style_copy(small_style);
576                 small_deleted_style->fg[GTK_STATE_NORMAL] =
577                         summaryview->color_dim;
578         }
579         if (!bold_style) {
580                 bold_style = gtk_style_copy
581                         (gtk_widget_get_style(summaryview->ctree));
582                 if (!boldfont)
583                         boldfont = gdk_fontset_load(BOLD_FONT);
584                 bold_style->font = boldfont;
585                 bold_marked_style = gtk_style_copy(bold_style);
586                 bold_marked_style->fg[GTK_STATE_NORMAL] =
587                         summaryview->color_marked;
588                 bold_deleted_style = gtk_style_copy(bold_style);
589                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
590                         summaryview->color_dim;
591         }
592
593         style = gtk_style_copy(gtk_widget_get_style
594                                 (summaryview->statlabel_folder));
595
596         gtk_widget_set_style(summaryview->statlabel_folder, style);
597         gtk_widget_set_style(summaryview->statlabel_select, style);
598         gtk_widget_set_style(summaryview->statlabel_msgs, style);
599
600         pixmap = stock_pixmap_widget(summaryview->hbox_l, STOCK_PIXMAP_DIR_OPEN);
601         gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), pixmap, FALSE, FALSE, 4);
602         gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), pixmap, 0);
603         gtk_widget_show(pixmap);
604         summaryview->folder_pixmap = pixmap;
605
606         /* Init Summaryview prefs */
607         summaryview->simplify_subject_preg = NULL;
608
609         summary_clear_list(summaryview);
610         summary_set_column_titles(summaryview);
611         summary_colorlabel_menu_create(summaryview);
612         summary_set_menu_sensitive(summaryview);
613
614 }
615
616 GtkCTreeNode * summary_find_next_important_score(SummaryView *summaryview,
617                                                  GtkCTreeNode *current_node)
618 {
619         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
620         GtkCTreeNode *node;
621         MsgInfo *msginfo;
622         gint best_score = MIN_SCORE;
623         GtkCTreeNode *best_node = NULL;
624
625         if (current_node)
626                 /*node = current_node;*/
627                 node = GTK_CTREE_NODE_NEXT(current_node);
628         else
629                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
630
631         for (; node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
632                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
633                 if (msginfo->score >= summaryview->important_score)
634                         break;
635                 if (msginfo->score > best_score) {
636                         best_score = msginfo->score;
637                         best_node = node;
638                 }
639         }
640
641         if (node != NULL)
642                 return node;
643         else
644                 return best_node;
645 }
646
647 GtkCTreeNode * summary_find_prev_important_score(SummaryView *summaryview,
648                                                  GtkCTreeNode *current_node)
649 {
650         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
651         GtkCTreeNode *node;
652         MsgInfo *msginfo;
653         gint best_score = MIN_SCORE;
654         GtkCTreeNode *best_node = NULL;
655
656         if (current_node)
657                 /*node = current_node;*/
658                 node = GTK_CTREE_NODE_PREV(current_node);
659         else
660                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
661
662         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
663                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
664                 if (msginfo->score >= summaryview->important_score)
665                         break;
666                 if (msginfo->score > best_score) {
667                         best_score = msginfo->score;
668                         best_node = node;
669                 }
670         }
671
672         if (node != NULL)
673                 return node;
674         else
675                 return best_node;
676 }
677
678 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
679 {
680         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
681         GtkCTreeNode *node;
682         GSList *mlist = NULL;
683         gchar *buf;
684         gboolean is_refresh;
685         guint selected_msgnum = 0;
686         guint displayed_msgnum = 0;
687         GSList *cur;
688
689         if (summary_is_locked(summaryview)) return FALSE;
690
691         inc_lock();
692         summary_lock(summaryview);
693
694         STATUSBAR_POP(summaryview->mainwin);
695
696         is_refresh = (!prefs_common.open_inbox_on_inc &&
697                       item == summaryview->folder_item) ? TRUE : FALSE;
698         if (is_refresh) {
699                 selected_msgnum = summary_get_msgnum(summaryview,
700                                                      summaryview->selected);
701                 displayed_msgnum = summary_get_msgnum(summaryview,
702                                                       summaryview->displayed);
703         }
704
705         /* process the marks if any */
706         if (summaryview->mainwin->lock_count == 0 &&
707             (summaryview->moved > 0 || summaryview->copied > 0)) {
708                 AlertValue val;
709
710                 val = alertpanel(_("Process mark"),
711                                  _("Some marks are left. Process it?"),
712                                  _("Yes"), _("No"), _("Cancel"));
713                 if (G_ALERTDEFAULT == val) {
714                         summary_unlock(summaryview);
715                         summary_execute(summaryview);
716                         summary_lock(summaryview);
717                 } else if (G_ALERTALTERNATE == val) {
718                         /* DO NOTHING */
719                 } else {
720                         summary_unlock(summaryview);
721                         inc_unlock();
722                         return FALSE;
723                 }
724                 folder_update_op_count();
725         }
726         
727         gtk_clist_freeze(GTK_CLIST(ctree));
728
729         summary_clear_list(summaryview);
730         summary_set_column_titles(summaryview);
731         if (!is_refresh)
732                 messageview_clear(summaryview->messageview);
733
734         buf = NULL;
735         if (!item || !item->path || !item->parent || item->no_select ||
736             (item->folder->type == F_MH &&
737              ((buf = folder_item_get_path(item)) == NULL ||
738               change_dir(buf) < 0))) {
739                 g_free(buf);
740                 debug_print("empty folder\n\n");
741                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
742                 if (is_refresh)
743                         messageview_clear(summaryview->messageview);
744                 summary_clear_all(summaryview);
745                 summaryview->folder_item = item;
746                 gtk_clist_thaw(GTK_CLIST(ctree));
747                 summary_unlock(summaryview);
748                 inc_unlock();
749                 return TRUE;
750         }
751         g_free(buf);
752
753         summaryview->folder_item = item;
754         item->opened = TRUE;
755
756         gtk_signal_handler_block_by_data(GTK_OBJECT(ctree), summaryview);
757
758         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
759         debug_print("%s\n", buf);
760         STATUSBAR_PUSH(summaryview->mainwin, buf);
761         g_free(buf);
762
763         main_window_cursor_wait(summaryview->mainwin);
764
765 /*
766         mlist = item->folder->get_msg_list(item->folder, item, !update_cache);
767
768         !!! NEW !!!
769         USE LIST FROM CACHE, WILL NOT DISPLAY ANY MESSAGES DROPED
770         BY OTHER PROGRAMS TO THE FOLDER
771 */
772         mlist = folder_item_get_msg_list(item);
773 #if 0
774         summary_processing(summaryview, mlist);
775 #endif
776         for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
777                 MsgInfo * msginfo = (MsgInfo *) cur->data;
778
779                 msginfo->score = score_message(global_scoring, msginfo);
780                 if (msginfo->score != MAX_SCORE &&
781                     msginfo->score != MIN_SCORE) {
782                         msginfo->score += score_message(item->prefs->scoring,
783                                                         msginfo);
784                 }
785         }
786
787         if (summaryview->folder_item->hide_read_msgs) {
788                 GSList *not_killed;
789                 
790                 summary_set_hide_read_msgs_menu(summaryview, TRUE);
791                 not_killed = NULL;
792                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
793                         MsgInfo * msginfo = (MsgInfo *) cur->data;
794                         
795                         if ((MSG_IS_UNREAD(msginfo->flags)
796                              || MSG_IS_MARKED(msginfo->flags)
797                              || MSG_IS_LOCKED(msginfo->flags))
798                              && !MSG_IS_IGNORE_THREAD(msginfo->flags))
799                             not_killed = g_slist_append(not_killed, msginfo);
800                         else
801                                 procmsg_msginfo_free(msginfo);
802                 }
803                 g_slist_free(mlist);
804                 mlist = not_killed;
805         } else {
806                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
807         }
808
809         if ((global_scoring || item->prefs->scoring)) {
810                 GSList *not_killed;
811                 gint kill_score;
812
813                 not_killed = NULL;
814                 kill_score = prefs_common.kill_score;
815                 if (item->prefs->kill_score > kill_score)
816                         kill_score = item->prefs->kill_score;
817                 for(cur = mlist ; cur != NULL ; cur = g_slist_next(cur)) {
818                         MsgInfo * msginfo = (MsgInfo *) cur->data;
819
820                         if (msginfo->score > kill_score)
821                                 not_killed = g_slist_append(not_killed, msginfo);
822                         else
823                                 procmsg_msginfo_free(msginfo);
824                 }
825                 g_slist_free(mlist);
826                 mlist = not_killed;
827         }
828
829         STATUSBAR_POP(summaryview->mainwin);
830
831         /* set ctree and hash table from the msginfo list
832            creating thread, and count the number of messages */
833         summary_set_ctree_from_list(summaryview, mlist);
834
835         g_slist_free(mlist);
836
837         folderview_update_msg_num(summaryview->folderview,
838                                   summaryview->folderview->opened);
839
840         if (item->sort_key != SORT_BY_NONE)
841                 summary_sort(summaryview, item->sort_key, item->sort_type);
842
843         gtk_signal_handler_unblock_by_data(GTK_OBJECT(ctree), summaryview);
844
845         gtk_clist_thaw(GTK_CLIST(ctree));
846
847         if (is_refresh) {
848                 summaryview->displayed =
849                         summary_find_msg_by_msgnum(summaryview,
850                                                    displayed_msgnum);
851                 if (!summaryview->displayed)
852                         messageview_clear(summaryview->messageview);
853                 summary_select_by_msgnum(summaryview, selected_msgnum);
854                 if (!summaryview->selected) {
855                         /* no selected message - select first unread
856                            message, but do not display it */
857                         node = summary_find_next_flagged_msg(summaryview, NULL,
858                                                              MSG_UNREAD, FALSE);
859                         if (node == NULL && GTK_CLIST(ctree)->row_list != NULL)
860                                 node = gtk_ctree_node_nth
861                                         (ctree,
862                                          item->sort_type == SORT_DESCENDING
863                                          ? 0 : GTK_CLIST(ctree)->rows - 1);
864                         summary_select_node(summaryview, node, FALSE, TRUE);
865                 }
866         } else {
867                 /* select first unread message */
868                 if (item->sort_key == SORT_BY_SCORE)
869                         node = summary_find_next_important_score(summaryview,
870                                                                  NULL);
871                 else
872                 node = summary_find_next_flagged_msg(summaryview, NULL,
873                                                      MSG_UNREAD, FALSE);
874                 if (node == NULL && GTK_CLIST(ctree)->row_list != NULL) {
875                         node = gtk_ctree_node_nth
876                                 (ctree,
877                                  item->sort_type == SORT_DESCENDING
878                                  ? 0 : GTK_CLIST(ctree)->rows - 1);
879                 }
880                 if (prefs_common.open_unread_on_enter) {
881                         summary_unlock(summaryview);
882                         summary_select_node(summaryview, node, TRUE, TRUE);
883                         summary_lock(summaryview);
884                 } else
885                         summary_select_node(summaryview, node, FALSE, TRUE);
886         }
887
888         summary_status_show(summaryview);
889         summary_set_menu_sensitive(summaryview);
890         toolbar_set_sensitive(summaryview->mainwin);
891
892         debug_print("\n");
893         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
894
895         main_window_cursor_normal(summaryview->mainwin);
896         summary_unlock(summaryview);
897         inc_unlock();
898
899         return TRUE;
900 }
901
902 void summary_clear_list(SummaryView *summaryview)
903 {
904         GtkCList *clist = GTK_CLIST(summaryview->ctree);
905         gint optimal_width;
906
907         gtk_clist_freeze(clist);
908
909         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree),
910                                 NULL, summary_free_msginfo_func, NULL);
911
912         if (summaryview->folder_item) {
913                 summaryview->folder_item->opened = FALSE;
914                 summaryview->folder_item = NULL;
915         }
916
917         summaryview->display_msg = FALSE;
918
919         summaryview->selected  = NULL;
920         summaryview->displayed = NULL;
921         summaryview->newmsgs   = summaryview->unread     = 0;
922         summaryview->messages  = summaryview->total_size = 0;
923         summaryview->deleted   = summaryview->moved      = 0;
924         summaryview->copied    = 0;
925         if (summaryview->msgid_table) {
926                 g_hash_table_destroy(summaryview->msgid_table);
927                 summaryview->msgid_table = NULL;
928         }
929         if (summaryview->subject_table) {
930                 g_hash_table_destroy(summaryview->subject_table);
931                 summaryview->subject_table = NULL;
932         }
933         summaryview->mlist = NULL;
934         if (summaryview->folder_table) {
935                 g_hash_table_destroy(summaryview->folder_table);
936                 summaryview->folder_table = NULL;
937         }
938
939         gtk_clist_clear(clist);
940         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
941                 optimal_width = gtk_clist_optimal_column_width
942                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
943                 gtk_clist_set_column_width
944                         (clist, summaryview->col_pos[S_COL_SUBJECT],
945                          optimal_width);
946         }
947
948         gtk_clist_thaw(clist);
949 }
950
951 void summary_clear_all(SummaryView *summaryview)
952 {
953         summary_clear_list(summaryview);
954         summary_set_menu_sensitive(summaryview);
955         toolbar_set_sensitive(summaryview->mainwin);
956         summary_status_show(summaryview);
957 }
958
959 void summary_lock(SummaryView *summaryview)
960 {
961         summaryview->lock_count++;
962 }
963
964 void summary_unlock(SummaryView *summaryview)
965 {
966         if (summaryview->lock_count)
967                 summaryview->lock_count--;
968 }
969
970 gboolean summary_is_locked(SummaryView *summaryview)
971 {
972         return summaryview->lock_count > 0;
973 }
974
975 SummarySelection summary_get_selection_type(SummaryView *summaryview)
976 {
977         GtkCList *clist = GTK_CLIST(summaryview->ctree);
978         SummarySelection selection;
979
980         if (!clist->row_list)
981                 selection = SUMMARY_NONE;
982         else if (!clist->selection)
983                 selection = SUMMARY_SELECTED_NONE;
984         else if (!clist->selection->next)
985                 selection = SUMMARY_SELECTED_SINGLE;
986         else
987                 selection = SUMMARY_SELECTED_MULTIPLE;
988
989         return selection;
990 }
991
992 static void summary_set_menu_sensitive(SummaryView *summaryview)
993 {
994         GtkItemFactory *ifactory = summaryview->popupfactory;
995         SummarySelection selection;
996         GtkWidget *menuitem;
997         gboolean sens;
998
999         selection = summary_get_selection_type(summaryview);
1000         main_window_set_menu_sensitive(summaryview->mainwin);
1001
1002         if (selection == SUMMARY_NONE) {
1003                 GtkWidget *submenu;
1004
1005                 submenu = gtk_item_factory_get_widget
1006                         (summaryview->popupfactory, "/Mark");
1007                 menu_set_insensitive_all(GTK_MENU_SHELL(submenu));
1008                 menu_set_insensitive_all
1009                         (GTK_MENU_SHELL(summaryview->popupmenu));
1010                 return;
1011         }
1012
1013         if (summaryview->folder_item->folder->type != F_NEWS)
1014                 menu_set_sensitive(ifactory, "/Move...", TRUE);
1015         else
1016                 menu_set_sensitive(ifactory, "/Move...", FALSE);
1017
1018         menu_set_sensitive(ifactory, "/Delete", TRUE);
1019         menu_set_sensitive(ifactory, "/Select all", TRUE);
1020         menu_set_sensitive(ifactory, "/Copy...", TRUE);
1021         menu_set_sensitive(ifactory, "/Execute", TRUE);
1022
1023         menu_set_sensitive(ifactory, "/Mark", TRUE);
1024         menu_set_sensitive(ifactory, "/Mark/Mark",   TRUE);
1025         menu_set_sensitive(ifactory, "/Mark/Unmark", TRUE);
1026
1027         menu_set_sensitive(ifactory, "/Mark/Mark as unread", TRUE);
1028         menu_set_sensitive(ifactory, "/Mark/Mark as read",   TRUE);
1029         menu_set_sensitive(ifactory, "/Mark/Mark all read", TRUE);
1030         menu_set_sensitive(ifactory, "/Mark/Ignore thread",   TRUE);
1031         menu_set_sensitive(ifactory, "/Mark/Unignore thread", TRUE);
1032
1033         menu_set_sensitive(ifactory, "/Color label", TRUE);
1034
1035         sens = (selection == SUMMARY_SELECTED_MULTIPLE) ? FALSE : TRUE;
1036         menu_set_sensitive(ifactory, "/Reply",                    sens);
1037         menu_set_sensitive(ifactory, "/Reply to sender",          sens);
1038         menu_set_sensitive(ifactory, "/Reply to all",             sens);
1039         menu_set_sensitive(ifactory, "/Forward",                  TRUE);
1040         menu_set_sensitive(ifactory, "/Redirect",                 sens);
1041
1042         menu_set_sensitive(ifactory, "/Add sender to address book", sens);
1043         menu_set_sensitive(ifactory, "/Create filter rule",         sens);
1044
1045         menu_set_sensitive(ifactory, "/View", sens);
1046         menu_set_sensitive(ifactory, "/View/Open in new window", sens);
1047         menu_set_sensitive(ifactory, "/View/Source", sens);
1048         menu_set_sensitive(ifactory, "/View/All header", sens);
1049         if (summaryview->folder_item->stype == F_OUTBOX ||
1050             summaryview->folder_item->stype == F_DRAFT  ||
1051             summaryview->folder_item->stype == F_QUEUE)
1052                 menu_set_sensitive(ifactory, "/Re-edit", sens);
1053         else
1054                 menu_set_sensitive(ifactory, "/Re-edit", FALSE);
1055
1056         menu_set_sensitive(ifactory, "/Select thread", sens);
1057         menu_set_sensitive(ifactory, "/Save as...", sens);
1058         menu_set_sensitive(ifactory, "/Print...",   TRUE);
1059
1060         if (summaryview->folder_item->folder->account)
1061                 sens = summaryview->folder_item->folder->account->protocol
1062                         == A_NNTP;
1063         else
1064                 sens = FALSE;
1065         menu_set_sensitive(ifactory, "/Follow-up and reply to", sens);
1066         summary_lock(summaryview);
1067         menuitem = gtk_item_factory_get_widget(ifactory, "/View/All header");
1068         gtk_check_menu_item_set_active
1069                 (GTK_CHECK_MENU_ITEM(menuitem),
1070                  summaryview->messageview->textview->show_all_headers);
1071         summary_unlock(summaryview);
1072 }
1073
1074 void summary_select_prev_unread(SummaryView *summaryview)
1075 {
1076         GtkCTreeNode *node;
1077
1078         node = summary_find_prev_flagged_msg
1079                 (summaryview, summaryview->selected, MSG_UNREAD, FALSE);
1080
1081         if (!node) {
1082                 AlertValue val = 0;
1083
1084                 switch (prefs_common.next_unread_msg_dialog) {
1085                         case NEXTUNREADMSGDIALOG_ALWAYS:
1086                                 val = alertpanel(_("No more unread messages"),
1087                                                  _("No unread message found. "
1088                                                    "Search from the end?"),
1089                                                  _("Yes"), _("No"), NULL);
1090                                 break;
1091                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1092                                 val = G_ALERTDEFAULT;
1093                                 break;
1094                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1095                                 val = !G_ALERTDEFAULT;
1096                                 break;
1097                         default:
1098                                 debug_print(
1099                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1100                 }
1101                 if (val != G_ALERTDEFAULT) return;
1102                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1103                                                      MSG_UNREAD, FALSE);
1104         }
1105
1106         if (!node)
1107                 alertpanel_notice(_("No unread messages."));
1108         else
1109                 summary_select_node(summaryview, node, TRUE, FALSE);
1110 }
1111
1112 void summary_select_next_unread(SummaryView *summaryview)
1113 {
1114         GtkCTreeNode *node = summaryview->selected;
1115         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1116
1117         node = summary_find_next_flagged_msg
1118                 (summaryview, node, MSG_UNREAD, FALSE);
1119         
1120         if (node)
1121                 summary_select_node(summaryview, node, TRUE, FALSE);
1122         else {
1123                 node = summary_find_next_flagged_msg
1124                         (summaryview, NULL, MSG_UNREAD, FALSE);
1125                 if (node == NULL) {
1126                         AlertValue val = 0;
1127
1128                         switch (prefs_common.next_unread_msg_dialog) {
1129                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1130                                         val = alertpanel(_("No more unread messages"),
1131                                                          _("No unread message found. "
1132                                                            "Go to next folder?"),
1133                                                          _("Yes"), _("No"), NULL);
1134                                         break;
1135                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1136                                         val = G_ALERTDEFAULT;
1137                                         break;
1138                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1139                                         val = G_ALERTOTHER;
1140                                         break;
1141                                 default:
1142                                         debug_print(
1143                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1144                         }
1145
1146                         if (val == G_ALERTDEFAULT) {
1147                                 if (gtk_signal_n_emissions_by_name
1148                                         (GTK_OBJECT(ctree), "key_press_event") > 0)
1149                                                 gtk_signal_emit_stop_by_name
1150                                                         (GTK_OBJECT(ctree),
1151                                                          "key_press_event");
1152                                 folderview_select_next_unread(summaryview->folderview);
1153                                 return;
1154                         } 
1155                         else
1156                                 return;
1157                 } else
1158                         summary_select_node(summaryview, node, TRUE, FALSE);
1159
1160         }
1161 }
1162
1163 void summary_select_prev_new(SummaryView *summaryview)
1164 {
1165         GtkCTreeNode *node;
1166
1167         node = summary_find_prev_flagged_msg
1168                 (summaryview, summaryview->selected, MSG_NEW, FALSE);
1169
1170         if (!node) {
1171                 AlertValue val;
1172
1173                 val = alertpanel(_("No more new messages"),
1174                                  _("No new message found. "
1175                                    "Search from the end?"),
1176                                  _("Yes"), _("No"), NULL);
1177                 if (val != G_ALERTDEFAULT) return;
1178                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1179                                                      MSG_NEW, FALSE);
1180         }
1181
1182         if (!node)
1183                 alertpanel_notice(_("No new messages."));
1184         else
1185                 summary_select_node(summaryview, node, TRUE, FALSE);
1186 }
1187
1188 void summary_select_next_new(SummaryView *summaryview)
1189 {
1190         GtkCTreeNode *node = summaryview->selected;
1191         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1192
1193         while ((node = summary_find_next_flagged_msg
1194                 (summaryview, node, MSG_NEW, FALSE)) == NULL) {
1195                 AlertValue val;
1196
1197                 val = alertpanel(_("No more new messages"),
1198                                  _("No new message found. "
1199                                    "Go to next folder?"),
1200                                  _("Yes"), _("Search again"), _("No"));
1201                 if (val == G_ALERTDEFAULT) {
1202                         if (gtk_signal_n_emissions_by_name
1203                                 (GTK_OBJECT(ctree), "key_press_event") > 0)
1204                                         gtk_signal_emit_stop_by_name
1205                                                 (GTK_OBJECT(ctree),
1206                                                  "key_press_event");
1207                         folderview_select_next_unread(summaryview->folderview);
1208                         return;
1209                 } else if (val == G_ALERTALTERNATE)
1210                         node = NULL;
1211                 else
1212                         return;
1213         }
1214
1215         if (node)
1216                 summary_select_node(summaryview, node, TRUE, FALSE);
1217 }
1218
1219 void summary_select_prev_marked(SummaryView *summaryview)
1220 {
1221         GtkCTreeNode *node;
1222
1223         node = summary_find_prev_flagged_msg
1224                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1225
1226         if (!node) {
1227                 AlertValue val;
1228
1229                 val = alertpanel(_("No more marked messages"),
1230                                  _("No marked message found. "
1231                                    "Search from the end?"),
1232                                  _("Yes"), _("No"), NULL);
1233                 if (val != G_ALERTDEFAULT) return;
1234                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1235                                                      MSG_MARKED, TRUE);
1236         }
1237
1238         if (!node)
1239                 alertpanel_notice(_("No marked messages."));
1240         else
1241                 summary_select_node(summaryview, node, TRUE, FALSE);
1242 }
1243
1244 void summary_select_next_marked(SummaryView *summaryview)
1245 {
1246         GtkCTreeNode *node;
1247
1248         node = summary_find_next_flagged_msg
1249                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1250
1251         if (!node) {
1252                 AlertValue val;
1253
1254                 val = alertpanel(_("No more marked messages"),
1255                                  _("No marked message found. "
1256                                    "Search from the beginning?"),
1257                                  _("Yes"), _("No"), NULL);
1258                 if (val != G_ALERTDEFAULT) return;
1259                 node = summary_find_next_flagged_msg(summaryview, NULL,
1260                                                      MSG_MARKED, TRUE);
1261         }
1262
1263         if (!node)
1264                 alertpanel_notice(_("No marked messages."));
1265         else
1266                 summary_select_node(summaryview, node, TRUE, FALSE);
1267 }
1268
1269 void summary_select_prev_labeled(SummaryView *summaryview)
1270 {
1271         GtkCTreeNode *node;
1272
1273         node = summary_find_prev_flagged_msg
1274                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1275
1276         if (!node) {
1277                 AlertValue val;
1278
1279                 val = alertpanel(_("No more labeled messages"),
1280                                  _("No labeled message found. "
1281                                    "Search from the end?"),
1282                                  _("Yes"), _("No"), NULL);
1283                 if (val != G_ALERTDEFAULT) return;
1284                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1285                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1286         }
1287
1288         if (!node)
1289                 alertpanel_notice(_("No labeled messages."));
1290         else
1291                 summary_select_node(summaryview, node, TRUE, FALSE);
1292 }
1293
1294 void summary_select_next_labeled(SummaryView *summaryview)
1295 {
1296         GtkCTreeNode *node;
1297
1298         node = summary_find_next_flagged_msg
1299                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1300
1301         if (!node) {
1302                 AlertValue val;
1303
1304                 val = alertpanel(_("No more labeled messages"),
1305                                  _("No labeled message found. "
1306                                    "Search from the beginning?"),
1307                                  _("Yes"), _("No"), NULL);
1308                 if (val != G_ALERTDEFAULT) return;
1309                 node = summary_find_next_flagged_msg(summaryview, NULL,
1310                                                      MSG_CLABEL_FLAG_MASK, TRUE);
1311         }
1312
1313         if (!node)
1314                 alertpanel_notice(_("No labeled messages."));
1315         else
1316                 summary_select_node(summaryview, node, TRUE, FALSE);
1317 }
1318
1319 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
1320 {
1321         GtkCTreeNode *node;
1322
1323         node = summary_find_msg_by_msgnum(summaryview, msgnum);
1324         summary_select_node(summaryview, node, FALSE, TRUE);
1325 }
1326
1327 /**
1328  * summary_select_node:
1329  * @summaryview: Summary view.
1330  * @node: Summary tree node.
1331  * @display_msg: TRUE to display the selected message.
1332  * @do_refresh: TRUE to refresh the widget.
1333  *
1334  * Select @node (bringing it into view by scrolling and expanding its
1335  * thread, if necessary) and unselect all others.  If @display_msg is
1336  * TRUE, display the corresponding message in the message view.
1337  * If @do_refresh is TRUE, the widget is refreshed.
1338  **/
1339 void summary_select_node(SummaryView *summaryview, GtkCTreeNode *node,
1340                          gboolean display_msg, gboolean do_refresh)
1341 {
1342         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1343
1344         if (node) {
1345                 gtkut_ctree_expand_parent_all(ctree, node);
1346                 if (do_refresh) {
1347                         GTK_EVENTS_FLUSH();
1348                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
1349                         gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
1350                 }
1351                 gtk_sctree_unselect_all(GTK_SCTREE(ctree));
1352                 if (display_msg && summaryview->displayed == node)
1353                         summaryview->displayed = NULL;
1354                 summaryview->display_msg = display_msg;
1355                 gtk_sctree_select(GTK_SCTREE(ctree), node);
1356         }
1357 }
1358
1359 static guint summary_get_msgnum(SummaryView *summaryview, GtkCTreeNode *node)
1360 {
1361         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1362         MsgInfo *msginfo;
1363
1364         if (!node)
1365                 return 0;
1366         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1367         return msginfo->msgnum;
1368 }
1369
1370 static GtkCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
1371                                            GtkCTreeNode *current_node)
1372 {
1373         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1374         GtkCTreeNode *node;
1375         MsgInfo *msginfo;
1376
1377         if (current_node)
1378                 node = current_node;
1379         else
1380                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1381
1382         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1383                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1384                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1385         }
1386
1387         return node;
1388 }
1389
1390 static GtkCTreeNode *summary_find_next_msg(SummaryView *summaryview,
1391                                            GtkCTreeNode *current_node)
1392 {
1393         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1394         GtkCTreeNode *node;
1395         MsgInfo *msginfo;
1396
1397         if (current_node)
1398                 node = current_node;
1399         else
1400                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1401
1402         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1403                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1404                 if (!MSG_IS_DELETED(msginfo->flags)) break;
1405         }
1406
1407         return node;
1408 }
1409
1410 static GtkCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
1411                                                    GtkCTreeNode *current_node,
1412                                                    MsgPermFlags flags,
1413                                                    gboolean start_from_prev)
1414 {
1415         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1416         GtkCTreeNode *node;
1417         MsgInfo *msginfo;
1418
1419         if (current_node) {
1420                 if (start_from_prev)
1421                         node = GTK_CTREE_NODE_PREV(current_node);
1422                 else
1423                         node = current_node;
1424         } else
1425                 node = gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->rows - 1);
1426
1427         for (; node != NULL; node = GTK_CTREE_NODE_PREV(node)) {
1428                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1429                 if ((msginfo->flags.perm_flags & flags) != 0) break;
1430         }
1431
1432         return node;
1433 }
1434
1435 static GtkCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
1436                                                    GtkCTreeNode *current_node,
1437                                                    MsgPermFlags flags,
1438                                                    gboolean start_from_next)
1439 {
1440         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1441         GtkCTreeNode *node;
1442         MsgInfo *msginfo;
1443
1444         if (current_node) {
1445                 if (start_from_next)
1446                         node = gtkut_ctree_node_next(ctree, current_node);
1447                 else
1448                         node = current_node;
1449         } else
1450                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1451
1452         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1453                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1454                 /* Find msg with matching flags but ignore messages with
1455                    ignore flags, if searching for new or unread messages */
1456                 if (!(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) && 
1457                         ((msginfo->flags.perm_flags & flags) != 0))
1458                         break;
1459         }
1460
1461         return node;
1462 }
1463
1464 static GtkCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
1465                                                 guint msgnum)
1466 {
1467         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1468         GtkCTreeNode *node;
1469         MsgInfo *msginfo;
1470
1471         node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1472
1473         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1474                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
1475                 if (msginfo->msgnum == msgnum) break;
1476         }
1477
1478         return node;
1479 }
1480
1481 static guint attract_hash_func(gconstpointer key)
1482 {
1483         gchar *str;
1484         gchar *p;
1485         guint h;
1486
1487         Xstrdup_a(str, (const gchar *)key, return 0);
1488         trim_subject(str);
1489
1490         p = str;
1491         h = *p;
1492
1493         if (h) {
1494                 for (p += 1; *p != '\0'; p++)
1495                         h = (h << 5) - h + *p;
1496         }
1497
1498         return h;
1499 }
1500
1501 static gint attract_compare_func(gconstpointer a, gconstpointer b)
1502 {
1503         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
1504 }
1505
1506 void summary_attract_by_subject(SummaryView *summaryview)
1507 {
1508         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1509         GtkCList *clist = GTK_CLIST(ctree);
1510         GtkCTreeNode *src_node;
1511         GtkCTreeNode *dst_node, *sibling;
1512         GtkCTreeNode *tmp;
1513         MsgInfo *src_msginfo, *dst_msginfo;
1514         GHashTable *subject_table;
1515
1516         debug_print("Attracting messages by subject...");
1517         STATUSBAR_PUSH(summaryview->mainwin,
1518                        _("Attracting messages by subject..."));
1519
1520         main_window_cursor_wait(summaryview->mainwin);
1521         gtk_clist_freeze(clist);
1522
1523         subject_table = g_hash_table_new(attract_hash_func,
1524                                          attract_compare_func);
1525
1526         for (src_node = GTK_CTREE_NODE(clist->row_list);
1527              src_node != NULL;
1528              src_node = tmp) {
1529                 tmp = GTK_CTREE_ROW(src_node)->sibling;
1530                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
1531                 if (!src_msginfo) continue;
1532                 if (!src_msginfo->subject) continue;
1533
1534                 /* find attracting node */
1535                 dst_node = g_hash_table_lookup(subject_table,
1536                                                src_msginfo->subject);
1537
1538                 if (dst_node) {
1539                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
1540
1541                         /* if the time difference is more than 20 days,
1542                            don't attract */
1543                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
1544                             > 60 * 60 * 24 * 20)
1545                                 continue;
1546
1547                         sibling = GTK_CTREE_ROW(dst_node)->sibling;
1548                         if (src_node != sibling)
1549                                 gtk_ctree_move(ctree, src_node, NULL, sibling);
1550                 }
1551
1552                 g_hash_table_insert(subject_table,
1553                                     src_msginfo->subject, src_node);
1554         }
1555
1556         g_hash_table_destroy(subject_table);
1557
1558         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1559
1560         gtk_clist_thaw(clist);
1561
1562         debug_print("done.\n");
1563         STATUSBAR_POP(summaryview->mainwin);
1564
1565         main_window_cursor_normal(summaryview->mainwin);
1566 }
1567
1568 static void summary_free_msginfo_func(GtkCTree *ctree, GtkCTreeNode *node,
1569                                       gpointer data)
1570 {
1571         MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, node);
1572
1573         if (msginfo)
1574                 procmsg_msginfo_free(msginfo);
1575 }
1576
1577 static void summary_set_marks_func(GtkCTree *ctree, GtkCTreeNode *node,
1578                                    gpointer data)
1579 {
1580         SummaryView *summaryview = data;
1581         MsgInfo *msginfo;
1582
1583         msginfo = gtk_ctree_node_get_row_data(ctree, node);
1584
1585         if (MSG_IS_NEWS(msginfo->flags))
1586                 news_flag_crosspost(msginfo);
1587
1588         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1589                 summaryview->newmsgs++;
1590         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1591                 summaryview->unread++;
1592         if (MSG_IS_DELETED(msginfo->flags))
1593                 summaryview->deleted++;
1594
1595         summaryview->messages++;
1596         summaryview->total_size += msginfo->size;
1597
1598         summary_set_row_marks(summaryview, node);
1599 }
1600
1601 static void summary_update_status(SummaryView *summaryview)
1602 {
1603         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1604         GtkCTreeNode *node;
1605         MsgInfo *msginfo;
1606
1607         summaryview->newmsgs = summaryview->unread =
1608         summaryview->messages = summaryview->total_size =
1609         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
1610
1611         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
1612              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
1613                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
1614
1615                 if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
1616                         summaryview->newmsgs++;
1617                 if (MSG_IS_UNREAD(msginfo->flags)&& !MSG_IS_IGNORE_THREAD(msginfo->flags))
1618                         summaryview->unread++;
1619                 if (MSG_IS_DELETED(msginfo->flags))
1620                         summaryview->deleted++;
1621                 if (MSG_IS_MOVE(msginfo->flags))
1622                         summaryview->moved++;
1623                 if (MSG_IS_COPY(msginfo->flags))
1624                         summaryview->copied++;
1625                 summaryview->messages++;
1626                 summaryview->total_size += msginfo->size;
1627         }
1628 }
1629
1630 static void summary_status_show(SummaryView *summaryview)
1631 {
1632         gchar *str;
1633         gchar *del, *mv, *cp;
1634         gchar *sel;
1635         gchar *spc;
1636         gchar *itstr;
1637         GList *rowlist, *cur;
1638         guint n_selected = 0;
1639         off_t sel_size = 0;
1640         MsgInfo *msginfo;
1641
1642         if (!summaryview->folder_item) {
1643                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), "");
1644                 gtk_label_set(GTK_LABEL(summaryview->statlabel_select), "");
1645                 gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs),   "");
1646                 return;
1647         }
1648
1649         rowlist = GTK_CLIST(summaryview->ctree)->selection;
1650         for (cur = rowlist; cur != NULL; cur = cur->next) {
1651                 msginfo = gtk_ctree_node_get_row_data
1652                         (GTK_CTREE(summaryview->ctree),
1653                          GTK_CTREE_NODE(cur->data));
1654                 sel_size += msginfo->size;
1655                 n_selected++;
1656         }
1657
1658         if (summaryview->folder_item->folder->type == F_NEWS &&
1659             prefs_common.ng_abbrev_len < strlen(summaryview->folder_item->path)) {
1660                 gchar *group;
1661                 group = get_abbrev_newsgroup_name
1662                         (g_basename(summaryview->folder_item->path));
1663                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder), group);
1664                 g_free(group);
1665         } else {
1666                 gtk_label_set(GTK_LABEL(summaryview->statlabel_folder),
1667                               summaryview->folder_item->path);
1668         }
1669
1670         if (summaryview->deleted)
1671                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
1672         else
1673                 del = g_strdup("");
1674         if (summaryview->moved)
1675                 mv = g_strdup_printf(_("%s%d moved"),
1676                                      summaryview->deleted ? _(", ") : "",
1677                                      summaryview->moved);
1678         else
1679                 mv = g_strdup("");
1680         if (summaryview->copied)
1681                 cp = g_strdup_printf(_("%s%d copied"),
1682                                      summaryview->deleted ||
1683                                      summaryview->moved ? _(", ") : "",
1684                                      summaryview->copied);
1685         else
1686                 cp = g_strdup("");
1687
1688         if (summaryview->deleted || summaryview->moved || summaryview->copied)
1689                 spc = "    ";
1690         else
1691                 spc = "";
1692
1693         if (n_selected) {
1694                 sel = g_strdup_printf(" (%s)", to_human_readable(sel_size));
1695                 if (n_selected == 1)
1696                         itstr = g_strdup(_(" item selected"));
1697                 else
1698                         itstr = g_strdup(_(" items selected"));
1699         } else {
1700                 sel = g_strdup("");
1701                 itstr = g_strdup("");
1702         }
1703                 
1704         str = g_strconcat(n_selected ? itos(n_selected) : "",
1705                                         itstr, sel, spc, del, mv, cp, NULL);
1706         gtk_label_set(GTK_LABEL(summaryview->statlabel_select), str);
1707         g_free(str);
1708         g_free(sel);
1709         g_free(del);
1710         g_free(mv);
1711         g_free(cp);
1712         g_free(itstr);
1713
1714         if (FOLDER_IS_LOCAL(summaryview->folder_item->folder)) {
1715                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
1716                                       summaryview->newmsgs,
1717                                       summaryview->unread,
1718                                       summaryview->messages,
1719                                       to_human_readable(summaryview->total_size));
1720         } else {
1721                 str = g_strdup_printf(_("%d new, %d unread, %d total"),
1722                                       summaryview->newmsgs,
1723                                       summaryview->unread,
1724                                       summaryview->messages);
1725         }
1726         gtk_label_set(GTK_LABEL(summaryview->statlabel_msgs), str);
1727         g_free(str);
1728 }
1729
1730 static void summary_set_column_titles(SummaryView *summaryview)
1731 {
1732         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1733         GtkWidget *hbox;
1734         GtkWidget *label;
1735         GtkWidget *arrow;
1736         gint pos;
1737         const gchar *title;
1738         SummaryColumnType type;
1739         gboolean single_char;
1740         GtkJustification justify;
1741         FolderItem *item = summaryview->folder_item;
1742
1743         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
1744                 SORT_BY_MARK,
1745                 SORT_BY_UNREAD,
1746                 SORT_BY_MIME,
1747                 SORT_BY_SUBJECT,
1748                 SORT_BY_FROM,
1749                 SORT_BY_DATE,
1750                 SORT_BY_SIZE,
1751                 SORT_BY_NUMBER,
1752                 SORT_BY_SCORE,
1753                 SORT_BY_LOCKED
1754         };
1755
1756         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
1757                 type = summaryview->col_state[pos].type;
1758
1759                 /* CLAWS: mime and unread are single char headers */
1760                 single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
1761                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
1762                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
1763
1764                 switch (type) {
1765                 case S_COL_SUBJECT:
1766                 case S_COL_FROM:
1767                 case S_COL_DATE:
1768                 case S_COL_NUMBER:
1769                         if (prefs_common.trans_hdr)
1770                                 title = gettext(col_label[type]);
1771                         else
1772                                 title = col_label[type];
1773                         break;
1774                 /* CLAWS: dummies for mark and locked headers */        
1775                 case S_COL_MARK:        
1776                 case S_COL_LOCKED:
1777                         title = "";
1778                         break;
1779                 default:
1780                         title = gettext(col_label[type]);
1781                 }
1782
1783                 if (type == S_COL_MIME) {
1784                         label = gtk_pixmap_new(clipxpm, clipxpmmask);
1785                         gtk_widget_show(label);
1786                         gtk_clist_set_column_widget(clist, pos, label);
1787                         continue;
1788                 }
1789                 if (single_char) {
1790                         gtk_clist_set_column_title(clist, pos, title);
1791                         continue;
1792                 }
1793
1794                 /* CLAWS: changed so that locked and mark headers
1795                  * show a pixmap instead of single character */
1796                 hbox  = gtk_hbox_new(FALSE, 4);
1797                 
1798                 if (type == S_COL_LOCKED)
1799                         label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
1800                 else if (type == S_COL_MARK) 
1801                         label = gtk_pixmap_new(markxpm, markxpmmask);
1802                 else 
1803                         label = gtk_label_new(title);
1804                 
1805                 if (justify == GTK_JUSTIFY_RIGHT)
1806                         gtk_box_pack_end(GTK_BOX(hbox), label,
1807                                          FALSE, FALSE, 0);
1808                 else
1809                         gtk_box_pack_start(GTK_BOX(hbox), label,
1810                                            FALSE, FALSE, 0);
1811
1812                 if (item && item->sort_key == sort_by[type]) {
1813                         arrow = gtk_arrow_new
1814                                 (item->sort_type == SORT_ASCENDING
1815                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
1816                                  GTK_SHADOW_IN);
1817                         if (justify == GTK_JUSTIFY_RIGHT)
1818                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
1819                                                    FALSE, FALSE, 0);
1820                         else
1821                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
1822                                                  FALSE, FALSE, 0);
1823                 }
1824
1825                 gtk_widget_show_all(hbox);
1826                 gtk_clist_set_column_widget(clist, pos, hbox);
1827         }
1828 }
1829
1830 void summary_sort(SummaryView *summaryview,
1831                   FolderSortKey sort_key, FolderSortType sort_type)
1832 {
1833         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1834         GtkCList *clist = GTK_CLIST(summaryview->ctree);
1835         GtkCListCompareFunc cmp_func = NULL;
1836         FolderItem *item = summaryview->folder_item;
1837
1838         if (!item || !item->path || !item->parent || item->no_select) return;
1839
1840         switch (sort_key) {
1841         case SORT_BY_MARK:
1842                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mark;
1843                 break;
1844         case SORT_BY_UNREAD:
1845                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_unread;
1846                 break;
1847         case SORT_BY_MIME:
1848                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_mime;
1849                 break;
1850         case SORT_BY_NUMBER:
1851                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_num;
1852                 break;
1853         case SORT_BY_SIZE:
1854                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_size;
1855                 break;
1856         case SORT_BY_DATE:
1857                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_date;
1858                 break;
1859         case SORT_BY_FROM:
1860                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_from;
1861                 break;
1862         case SORT_BY_SUBJECT:
1863                 if (summaryview->simplify_subject_preg)
1864                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_simplified_subject;
1865                 else
1866                         cmp_func = (GtkCListCompareFunc)summary_cmp_by_subject;
1867                 break;
1868         case SORT_BY_SCORE:
1869                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_score;
1870                 break;
1871         case SORT_BY_LOCKED:
1872                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_locked;
1873                 break;
1874         case SORT_BY_LABEL:
1875                 cmp_func = (GtkCListCompareFunc)summary_cmp_by_label;
1876                 break;
1877         case SORT_BY_NONE:
1878                 cmp_func = NULL;
1879                 return;
1880         default:
1881                 return;
1882         }
1883
1884         item->sort_key = sort_key;
1885         item->sort_type = sort_type;
1886
1887         summary_set_column_titles(summaryview);
1888         summary_set_menu_sensitive(summaryview);
1889         if(cmp_func != NULL) {
1890                 debug_print("Sorting summary...");
1891                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
1892
1893                 main_window_cursor_wait(summaryview->mainwin);
1894
1895                 gtk_clist_set_compare_func(clist, cmp_func);
1896
1897                 gtk_clist_set_sort_type(clist, (GtkSortType)sort_type);
1898
1899                 gtk_sctree_sort_recursive(ctree, NULL);
1900
1901                 gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
1902
1903                 main_window_cursor_normal(summaryview->mainwin);
1904
1905                 debug_print("done.\n");
1906                 STATUSBAR_POP(summaryview->mainwin);
1907         }
1908 }
1909
1910 gboolean summary_insert_gnode_func(GtkCTree *ctree, guint depth, GNode *gnode,
1911                                    GtkCTreeNode *cnode, gpointer data)
1912 {
1913         SummaryView *summaryview = (SummaryView *)data;
1914         MsgInfo *msginfo = (MsgInfo *)gnode->data;
1915         gchar *text[N_SUMMARY_COLS];
1916         gint *col_pos = summaryview->col_pos;
1917         const gchar *msgid = msginfo->msgid;
1918         GHashTable *msgid_table = summaryview->msgid_table;
1919
1920         summary_set_header(summaryview, text, msginfo);
1921
1922         gtk_ctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
1923                                 NULL, NULL, NULL, NULL, FALSE,
1924                                 gnode->parent->parent ? TRUE : FALSE);
1925 #define SET_TEXT(col) \
1926         gtk_ctree_node_set_text(ctree, cnode, col_pos[col], \
1927                                 text[col_pos[col]])
1928
1929         SET_TEXT(S_COL_NUMBER);
1930         SET_TEXT(S_COL_SCORE);
1931         SET_TEXT(S_COL_SIZE);
1932         SET_TEXT(S_COL_DATE);
1933         SET_TEXT(S_COL_FROM);
1934         SET_TEXT(S_COL_SUBJECT);
1935
1936 #undef SET_TEXT
1937
1938         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
1939         summary_set_marks_func(ctree, cnode, summaryview);
1940
1941         if (msgid && msgid[0] != '\0')
1942                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
1943
1944         return TRUE;
1945 }
1946
1947 static void summary_set_ctree_from_list(SummaryView *summaryview,
1948                                         GSList *mlist)
1949 {
1950         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
1951         MsgInfo *msginfo;
1952                 GtkCTreeNode *node = NULL;
1953         GHashTable *msgid_table;
1954         GHashTable *subject_table;
1955         GSList * cur;
1956         
1957         if (!mlist) return;
1958
1959         debug_print("\tSetting summary from message data...");
1960         STATUSBAR_PUSH(summaryview->mainwin,
1961                        _("Setting summary from message data..."));
1962         gdk_flush();
1963
1964         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
1965         summaryview->msgid_table = msgid_table;
1966         subject_table = g_hash_table_new(g_str_hash, g_str_equal);
1967         summaryview->subject_table = subject_table;
1968
1969         if (prefs_common.use_addr_book)
1970                 start_address_completion();
1971         
1972         for (cur = mlist ; cur != NULL; cur = cur->next) {
1973                 msginfo = (MsgInfo *)cur->data;
1974                 msginfo->threadscore = msginfo->score;
1975         }
1976
1977         if (global_scoring || summaryview->folder_item->prefs->scoring) {
1978                 summaryview->important_score = prefs_common.important_score;
1979                 if (summaryview->folder_item->prefs->important_score >
1980                     summaryview->important_score)
1981                         summaryview->important_score =
1982                                 summaryview->folder_item->prefs->important_score;
1983         }
1984
1985         if (summaryview->folder_item->threaded) {
1986                 GNode *root, *gnode;
1987
1988                 root = procmsg_get_thread_tree(mlist);
1989
1990                 for (gnode = root->children; gnode != NULL;
1991                      gnode = gnode->next) {
1992                         node = gtk_ctree_insert_gnode
1993                                 (ctree, NULL, node, gnode,
1994                                  summary_insert_gnode_func, summaryview);
1995                 }
1996
1997                 g_node_destroy(root);
1998
1999                 summary_thread_init(summaryview);
2000         } else {
2001                 gchar *text[N_SUMMARY_COLS];
2002
2003                 mlist = g_slist_reverse(mlist);
2004                 for (; mlist != NULL; mlist = mlist->next) {
2005                         msginfo = (MsgInfo *)mlist->data;
2006
2007                         summary_set_header(summaryview, text, msginfo);
2008
2009                         node = gtk_ctree_insert_node
2010                                 (ctree, NULL, node, text, 2,
2011                                  NULL, NULL, NULL, NULL, FALSE, FALSE);
2012                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
2013                         summary_set_marks_func(ctree, node, summaryview);
2014
2015                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
2016                                 g_hash_table_insert(msgid_table,
2017                                                     msginfo->msgid, node);
2018
2019                         subject_table_insert(subject_table,
2020                                              msginfo->subject,
2021                                              node);
2022                 }
2023                 mlist = g_slist_reverse(mlist);
2024         }
2025
2026         if (prefs_common.enable_hscrollbar &&
2027             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
2028                 gint optimal_width;
2029
2030                 optimal_width = gtk_clist_optimal_column_width
2031                         (GTK_CLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
2032                 gtk_clist_set_column_width(GTK_CLIST(ctree),
2033                                            summaryview->col_pos[S_COL_SUBJECT],
2034                                            optimal_width);
2035         }
2036
2037         if (prefs_common.use_addr_book)
2038                 end_address_completion();
2039
2040         debug_print("done.\n");
2041         STATUSBAR_POP(summaryview->mainwin);
2042         if (debug_mode) {
2043                 debug_print("\tmsgid hash table size = %d\n",
2044                             g_hash_table_size(msgid_table));
2045                 debug_print("\tsubject hash table size = %d\n",
2046                             g_hash_table_size(subject_table));
2047         }
2048 }
2049
2050 static gchar *summary_complete_address(const gchar *addr)
2051 {
2052         gint count;
2053         gchar *res, *tmp, *email_addr;
2054
2055         Xstrdup_a(email_addr, addr, return NULL);
2056         extract_address(email_addr);
2057         g_return_val_if_fail(*email_addr, NULL);
2058
2059         /*
2060          * completion stuff must be already initialized
2061          */
2062         res = NULL;
2063         if (1 < (count = complete_address(email_addr))) {
2064                 tmp = get_complete_address(1);
2065                 res = procheader_get_fromname(tmp);
2066                 g_free(tmp);    
2067         }
2068         
2069         return res;
2070 }
2071
2072 static void summary_set_header(SummaryView *summaryview, gchar *text[],
2073                                MsgInfo *msginfo)
2074 {
2075         static gchar date_modified[80];
2076         static gchar *to = NULL;
2077         static gchar col_score[11];
2078         static gchar buf[BUFFSIZE];
2079         gint *col_pos = summaryview->col_pos;
2080
2081         text[col_pos[S_COL_MARK]]   = NULL;
2082         text[col_pos[S_COL_UNREAD]] = NULL;
2083         text[col_pos[S_COL_MIME]]   = NULL;
2084         text[col_pos[S_COL_LOCKED]] = NULL;
2085         text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
2086         text[col_pos[S_COL_SIZE]]   = to_human_readable(msginfo->size);
2087         text[col_pos[S_COL_SCORE]]  = itos_buf(col_score, msginfo->score);
2088
2089         if (msginfo->date_t) {
2090                 procheader_date_get_localtime(date_modified,
2091                                               sizeof(date_modified),
2092                                               msginfo->date_t);
2093                 text[col_pos[S_COL_DATE]] = date_modified;
2094         } else if (msginfo->date)
2095                 text[col_pos[S_COL_DATE]] = msginfo->date;
2096         else
2097                 text[col_pos[S_COL_DATE]] = _("(No Date)");
2098
2099         text[col_pos[S_COL_FROM]] = msginfo->fromname ? msginfo->fromname :
2100                 _("(No From)");
2101         if (prefs_common.swap_from && msginfo->from && msginfo->to &&
2102             !MSG_IS_NEWS(msginfo->flags)) {
2103                 gchar *addr = NULL;
2104
2105                 Xstrdup_a(addr, msginfo->from, return);
2106                 extract_address(addr);
2107
2108                 if (prefs_common.use_addr_book) {
2109                         if (account_find_from_address(addr)) {
2110                                 addr = summary_complete_address(msginfo->to);
2111                                 g_free(to);
2112                                 to   = g_strconcat("-->", addr == NULL ? msginfo->to : addr, NULL);
2113                                 text[col_pos[S_COL_FROM]] = to;
2114                         }
2115                 } else {
2116                         if (cur_account && cur_account->address && !strcmp( addr, cur_account->address)) {
2117                                 g_free(to);
2118                                 to = g_strconcat("-->", msginfo->to, NULL);
2119                                 text[col_pos[S_COL_FROM]] = to;
2120                         }
2121                 }
2122         }
2123
2124         /*
2125          * CLAWS: note that the "text[col_pos[S_COL_FROM]] != to" is really a hack, 
2126          * checking whether the above block (which handles the special case of
2127          * the --> in sent boxes) was executed.
2128          */
2129         if (text[col_pos[S_COL_FROM]] != to && prefs_common.use_addr_book && msginfo->from) {
2130                 gchar *from;
2131                 from = summary_complete_address(msginfo->from);
2132                 if (from) {
2133                         /*
2134                          * FIXME: this text[col_pos[S_COL_FROM]] should be freed
2135                          * but may have been assigned _("No From"). Should be
2136                          * freed??? 
2137                          */
2138                         text[col_pos[S_COL_FROM]] = from;
2139                 }       
2140         }
2141
2142         if (summaryview->simplify_subject_preg != NULL)
2143                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
2144                         string_remove_match(buf, BUFFSIZE, msginfo->subject, 
2145                                         summaryview->simplify_subject_preg) : 
2146                         _("(No Subject)");
2147         else 
2148                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
2149                         _("(No Subject)");
2150 }
2151
2152 #define CHANGE_FLAGS(msginfo) \
2153 { \
2154 if (msginfo->folder->folder->change_flags != NULL) \
2155 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
2156                                       msginfo->folder, \
2157                                       msginfo); \
2158 }
2159
2160 static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row)
2161 {
2162         summary_display_msg_full(summaryview, row, FALSE, FALSE);
2163 }
2164
2165 static void summary_display_msg_full(SummaryView *summaryview,
2166                                      GtkCTreeNode *row,
2167                                      gboolean new_window, gboolean all_headers)
2168 {
2169         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2170         MsgInfo *msginfo;
2171         MsgFlags flags;
2172         gchar *filename;
2173
2174         if (!new_window && summaryview->displayed == row) return;
2175         g_return_if_fail(row != NULL);
2176
2177         if (summary_is_locked(summaryview)) return;
2178         summary_lock(summaryview);
2179
2180         STATUSBAR_POP(summaryview->mainwin);
2181         GTK_EVENTS_FLUSH();
2182
2183         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2184
2185         filename = procmsg_get_message_file(msginfo);
2186         if (!filename) {
2187                 summary_unlock(summaryview);
2188                 return;
2189         }
2190         g_free(filename);
2191
2192         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2193                 summaryview->newmsgs--;
2194         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2195                 summaryview->unread--;
2196
2197         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2198         summary_set_row_marks(summaryview, row);
2199         gtk_clist_thaw(GTK_CLIST(ctree));
2200         summary_status_show(summaryview);
2201
2202         flags = msginfo->flags;
2203
2204         if (new_window) {
2205                 MessageView *msgview;
2206
2207                 msgview = messageview_create_with_new_window();
2208                 messageview_show(msgview, msginfo, all_headers);
2209         } else {
2210                 MessageView *msgview;
2211
2212                 msgview = summaryview->messageview;
2213
2214                 summaryview->displayed = row;
2215                 if (!messageview_is_visible(msgview))
2216                         main_window_toggle_message_view(summaryview->mainwin);
2217                 messageview_show(msgview, msginfo, all_headers);
2218                 if (msgview->type == MVIEW_TEXT ||
2219                     (msgview->type == MVIEW_MIME &&
2220                      (GTK_CLIST(msgview->mimeview->ctree)->row_list == NULL ||
2221                       gtk_notebook_get_current_page
2222                         (GTK_NOTEBOOK(msgview->mimeview->notebook)) == 0)))
2223                         gtk_widget_grab_focus(summaryview->ctree);
2224                 GTK_EVENTS_FLUSH();
2225                 gtkut_ctree_node_move_if_on_the_edge(ctree, row);
2226         }
2227
2228         summary_set_menu_sensitive(summaryview);
2229         toolbar_set_sensitive(summaryview->mainwin);
2230
2231         summary_unlock(summaryview);
2232 }
2233
2234 void summary_display_msg_selected(SummaryView *summaryview,
2235                                   gboolean all_headers)
2236 {
2237         if (summary_is_locked(summaryview)) return;
2238         summaryview->displayed = NULL;
2239         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
2240                                  all_headers);
2241 }
2242
2243 void summary_redisplay_msg(SummaryView *summaryview)
2244 {
2245         GtkCTreeNode *node;
2246
2247         if (summaryview->displayed) {
2248                 node = summaryview->displayed;
2249                 summaryview->displayed = NULL;
2250                 summary_display_msg(summaryview, node);
2251         }
2252 }
2253
2254 void summary_open_msg(SummaryView *summaryview)
2255 {
2256         if (!summaryview->selected) return;
2257
2258         summary_display_msg_full(summaryview, summaryview->selected,
2259                                  TRUE, FALSE);
2260 }
2261
2262 void summary_view_source(SummaryView * summaryview)
2263 {
2264         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2265         MsgInfo *msginfo;
2266         SourceWindow *srcwin;
2267
2268         if (!summaryview->selected) return;
2269
2270         srcwin = source_window_create();
2271         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
2272         source_window_show_msg(srcwin, msginfo);
2273         source_window_show(srcwin);
2274 }
2275
2276 void summary_reedit(SummaryView *summaryview)
2277 {
2278         MsgInfo *msginfo;
2279
2280         if (!summaryview->selected) return;
2281         if (!summaryview->folder_item) return;
2282         if (summaryview->folder_item->stype != F_OUTBOX &&
2283             summaryview->folder_item->stype != F_DRAFT  &&
2284             summaryview->folder_item->stype != F_QUEUE) return;
2285
2286         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
2287                                               summaryview->selected);
2288         if (!msginfo) return;
2289
2290         compose_reedit(msginfo);
2291 }
2292
2293 void summary_step(SummaryView *summaryview, GtkScrollType type)
2294 {
2295         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2296         GtkCTreeNode *node;
2297
2298         if (summary_is_locked(summaryview)) return;
2299
2300         if (type == GTK_SCROLL_STEP_FORWARD) {
2301                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
2302                 if (node)
2303                         gtkut_ctree_expand_parent_all(ctree, node);
2304                 else
2305                         return;
2306         } else {
2307                 if (summaryview->selected) {
2308                         node = GTK_CTREE_NODE_PREV(summaryview->selected);
2309                         if (!node) return;
2310                 }
2311         }
2312
2313         if (messageview_is_visible(summaryview->messageview))
2314                 summaryview->display_msg = TRUE;
2315
2316         gtk_signal_emit_by_name(GTK_OBJECT(ctree), "scroll_vertical",
2317                                 type, 0.0);
2318 }
2319
2320 void summary_toggle_view(SummaryView *summaryview)
2321 {
2322         if (!messageview_is_visible(summaryview->messageview) &&
2323             summaryview->selected)
2324                 summary_display_msg(summaryview,
2325                                     summaryview->selected);
2326         else
2327                 main_window_toggle_message_view(summaryview->mainwin);
2328 }
2329
2330 static gboolean summary_search_unread_recursive(GtkCTree *ctree,
2331                                                 GtkCTreeNode *node)
2332 {
2333         MsgInfo *msginfo;
2334
2335         if (node) {
2336                 msginfo = gtk_ctree_node_get_row_data(ctree, node);
2337                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2338                         return TRUE;
2339                 node = GTK_CTREE_ROW(node)->children;
2340         } else
2341                 node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
2342
2343         while (node) {
2344                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2345                         return TRUE;
2346                 node = GTK_CTREE_ROW(node)->sibling;
2347         }
2348
2349         return FALSE;
2350 }
2351
2352 static gboolean summary_have_unread_children(SummaryView *summaryview,
2353                                              GtkCTreeNode *node)
2354 {
2355         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2356
2357         if (!node) return FALSE;
2358
2359         node = GTK_CTREE_ROW(node)->children;
2360
2361         while (node) {
2362                 if (summary_search_unread_recursive(ctree, node) == TRUE)
2363                         return TRUE;
2364                 node = GTK_CTREE_ROW(node)->sibling;
2365         }
2366
2367         return FALSE;
2368 }
2369
2370 static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
2371 {
2372         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2373         GtkStyle *style = NULL;
2374         MsgInfo *msginfo;
2375         MsgFlags flags;
2376         gint *col_pos = summaryview->col_pos;
2377
2378         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2379         if (!msginfo) return;
2380
2381         flags = msginfo->flags;
2382
2383         gtk_ctree_node_set_foreground(ctree, row, NULL);
2384
2385         /* set new/unread column */
2386         if (MSG_IS_IGNORE_THREAD(flags)) {
2387                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2388                                           ignorethreadxpm, ignorethreadxpmmask);
2389         } else if (MSG_IS_NEW(flags)) {
2390                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2391                                           newxpm, newxpmmask);
2392         } else if (MSG_IS_UNREAD(flags)) {
2393                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2394                                           unreadxpm, unreadxpmmask);
2395         } else if (MSG_IS_REPLIED(flags)) {
2396                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2397                                           repliedxpm, repliedxpmmask);
2398         } else if (MSG_IS_FORWARDED(flags)) {
2399                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_UNREAD],
2400                                           forwardedxpm, forwardedxpmmask);
2401         } else {
2402                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_UNREAD],
2403                                         NULL);
2404         }
2405
2406         if (prefs_common.bold_unread &&
2407             ((MSG_IS_UNREAD(flags) && !MSG_IS_IGNORE_THREAD(flags)) ||
2408              (!GTK_CTREE_ROW(row)->expanded &&
2409               GTK_CTREE_ROW(row)->children &&
2410               summary_have_unread_children(summaryview, row))))
2411                 style = bold_style;
2412
2413         /* set mark column */
2414         if (MSG_IS_DELETED(flags)) {
2415                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2416                                           deletedxpm, deletedxpmmask);
2417                 if (style)
2418                         style = bold_deleted_style;
2419                 else {
2420                         style = small_deleted_style;
2421                 }
2422                         gtk_ctree_node_set_foreground
2423                                 (ctree, row, &summaryview->color_dim);
2424         } else if (MSG_IS_MARKED(flags)) {
2425                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MARK],
2426                                           markxpm, markxpmmask);
2427         } else if (MSG_IS_MOVE(flags)) {
2428                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "o");
2429                 if (style)
2430                         style = bold_marked_style;
2431                 else {
2432                         style = small_marked_style;
2433                 }
2434                         gtk_ctree_node_set_foreground
2435                                 (ctree, row, &summaryview->color_marked);
2436         } else if (MSG_IS_COPY(flags)) {
2437                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], "O");
2438                 if (style)
2439                         style = bold_marked_style;
2440                 else {
2441                         style = small_marked_style;
2442                 }
2443                         gtk_ctree_node_set_foreground
2444                                 (ctree, row, &summaryview->color_marked);
2445         }
2446         else if ((global_scoring ||
2447                   summaryview->folder_item->prefs->scoring) &&
2448                  (msginfo->score >= summaryview->important_score) &&
2449                  (MSG_IS_MARKED(msginfo->flags) || MSG_IS_MOVE(msginfo->flags) || MSG_IS_COPY(msginfo->flags))) {
2450                 gtk_ctree_node_set_text(ctree, row, S_COL_MARK, "!");
2451                 gtk_ctree_node_set_foreground(ctree, row,
2452                                               &summaryview->color_important);
2453         } else {
2454                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MARK], NULL);
2455         }
2456
2457         if (MSG_IS_LOCKED(flags)) {
2458                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_LOCKED],
2459                                           lockedxpm, lockedxpmmask);
2460         }
2461         else {
2462                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
2463         }
2464
2465         if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
2466                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2467                                           clipkeyxpm, clipkeyxpmmask);
2468         } else if (MSG_IS_ENCRYPTED(flags)) {
2469                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2470                                           keyxpm, keyxpmmask);
2471         } else if (MSG_IS_MIME(flags)) {
2472                 gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
2473                                           clipxpm, clipxpmmask);
2474         } else {
2475                 gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_MIME], NULL);
2476         }
2477         if (!style)
2478                 style = small_style;
2479
2480         gtk_ctree_node_set_row_style(ctree, row, style);
2481
2482         if (MSG_GET_COLORLABEL(flags))
2483                 summary_set_colorlabel_color(ctree, row, MSG_GET_COLORLABEL_VALUE(flags));
2484 }
2485
2486 void summary_set_marks_selected(SummaryView *summaryview)
2487 {
2488         GList *cur;
2489
2490         for (cur = GTK_CLIST(summaryview->ctree)->selection; cur != NULL;
2491              cur = cur->next)
2492                 summary_set_row_marks(summaryview, GTK_CTREE_NODE(cur->data));
2493 }
2494
2495 static void summary_mark_row(SummaryView *summaryview, GtkCTreeNode *row)
2496 {
2497         gboolean changed = FALSE;
2498         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2499         MsgInfo *msginfo;
2500
2501         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2502         if (MSG_IS_DELETED(msginfo->flags))
2503                 summaryview->deleted--;
2504         if (MSG_IS_MOVE(msginfo->flags)) {
2505                 summaryview->moved--;
2506                 changed = TRUE;
2507         }
2508         if (MSG_IS_COPY(msginfo->flags)) {
2509                 summaryview->copied--;
2510                 changed = TRUE;
2511         }
2512         if (changed && !prefs_common.immediate_exec) {
2513                 msginfo->to_folder->op_count--;
2514                 if (msginfo->to_folder->op_count == 0)
2515                         folderview_update_item(msginfo->to_folder, 0);
2516         }
2517         msginfo->to_folder = NULL;
2518         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2519         procmsg_msginfo_set_flags(msginfo, MSG_MARKED, 0);
2520         summary_set_row_marks(summaryview, row);
2521         debug_print("Message %s/%d is marked\n", msginfo->folder->path, msginfo->msgnum);
2522 }
2523
2524 static void summary_lock_row(SummaryView *summaryview, GtkCTreeNode *row)
2525 {
2526         /* almost verbatim summary_mark_row(); may want a menu action? */
2527         gboolean changed = FALSE;
2528         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2529         MsgInfo *msginfo;
2530
2531         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2532         if (MSG_IS_DELETED(msginfo->flags))
2533                 summaryview->deleted--;
2534         if (MSG_IS_MOVE(msginfo->flags)) {
2535                 summaryview->moved--;
2536                 changed = TRUE;
2537         }
2538         if (MSG_IS_COPY(msginfo->flags)) {
2539                 summaryview->copied--;
2540                 changed = TRUE;
2541         }
2542         if (changed && !prefs_common.immediate_exec) {
2543                 msginfo->to_folder->op_count--;
2544                 if (msginfo->to_folder->op_count == 0)
2545                         folderview_update_item(msginfo->to_folder, 0);
2546         }
2547         msginfo->to_folder = NULL;
2548         procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, MSG_MOVE | MSG_COPY);
2549         procmsg_msginfo_set_flags(msginfo, MSG_LOCKED, 0);
2550         summary_set_row_marks(summaryview, row);
2551         debug_print("Message %d is locked\n", msginfo->msgnum);
2552 }
2553
2554 void summary_mark(SummaryView *summaryview)
2555 {
2556         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2557         GList *cur;
2558
2559         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2560                 summary_mark_row(summaryview, GTK_CTREE_NODE(cur->data));
2561
2562         /* summary_step(summaryview, GTK_SCROLL_STEP_FORWARD); */
2563         summary_status_show(summaryview);
2564 }
2565
2566 static void summary_mark_row_as_read(SummaryView *summaryview,
2567                                      GtkCTreeNode *row)
2568 {
2569         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2570         MsgInfo *msginfo;
2571
2572         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2573
2574         if(!MSG_IS_NEW(msginfo->flags) && !MSG_IS_UNREAD(msginfo->flags))
2575                 return;
2576
2577         if (MSG_IS_NEW(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2578                 summaryview->newmsgs--;
2579         if (MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2580                 summaryview->unread--;
2581
2582         procmsg_msginfo_unset_flags(msginfo, MSG_NEW | MSG_UNREAD, 0);
2583         summary_set_row_marks(summaryview, row);
2584         debug_print("Message %d is marked as read\n",
2585                 msginfo->msgnum);
2586 }
2587
2588 void summary_mark_as_read(SummaryView *summaryview)
2589 {
2590         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2591         GList *cur;
2592
2593         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2594                 summary_mark_row_as_read(summaryview,
2595                                          GTK_CTREE_NODE(cur->data));
2596
2597         summary_status_show(summaryview);
2598 }
2599
2600 void summary_mark_all_read(SummaryView *summaryview)
2601 {
2602         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2603         GtkCList *clist = GTK_CLIST(summaryview->ctree);
2604         GtkCTreeNode *node;
2605
2606         gtk_clist_freeze(clist);
2607         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2608              node = gtkut_ctree_node_next(ctree, node))
2609                 summary_mark_row_as_read(summaryview, node);
2610         for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list); node != NULL;
2611              node = gtkut_ctree_node_next(ctree, node)) {
2612                 if (!GTK_CTREE_ROW(node)->expanded)
2613                         summary_set_row_marks(summaryview, node);
2614         }
2615         gtk_clist_thaw(clist);
2616
2617         summary_status_show(summaryview);
2618 }
2619
2620 static void summary_mark_row_as_unread(SummaryView *summaryview,
2621                                        GtkCTreeNode *row)
2622 {
2623         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2624         MsgInfo *msginfo;
2625
2626         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2627         if (MSG_IS_DELETED(msginfo->flags)) {
2628                 msginfo->to_folder = NULL;
2629                 procmsg_msginfo_unset_flags(msginfo, MSG_DELETED, 0);
2630                 summaryview->deleted--;
2631         }
2632
2633         if (!MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
2634                 summaryview->unread++;
2635
2636         procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED | MSG_FORWARDED, 0);
2637         procmsg_msginfo_set_flags(msginfo, MSG_UNREAD, 0);
2638         debug_print("Message %d is marked as unread\n",
2639                 msginfo->msgnum);
2640
2641         summary_set_row_marks(summaryview, row);
2642 }
2643
2644 void summary_mark_as_unread(SummaryView *summaryview)
2645 {
2646         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2647         GList *cur;
2648
2649         for (cur = GTK_CLIST(ctree)->selection; cur != NULL; cur = cur->next)
2650                 summary_mark_row_as_unread(summaryview,
2651                                            GTK_CTREE_NODE(cur->data));
2652
2653         summary_status_show(summaryview);
2654 }
2655
2656 static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
2657 {
2658         GList * cur;
2659         gboolean found;
2660
2661         switch (summaryview->folder_item->folder->type) {
2662
2663         case F_NEWS:
2664
2665                 /*
2666                   security : checks if one the accounts correspond to
2667                   the author of the post
2668                 */
2669
2670                 found = FALSE;
2671                 for(cur = account_get_list() ; cur != NULL ; cur = cur->next) {
2672                         PrefsAccount * account;
2673                         gchar * from_name;
2674                         
2675                         account = cur->data;
2676                         if (account->name && *account->name)
2677                                 from_name =
2678                                         g_strdup_printf("%s <%s>",
2679                                                         account->name,
2680                                                         account->address);
2681                         else
2682                                 from_name =
2683                                         g_strdup_printf("%s",
2684                                                         account->address);
2685                         
2686                         if (g_strcasecmp(from_name, msginfo->from) == 0) {
2687                                 g_free(from_name);
2688                                 found = TRUE;
2689                                 break;
2690                         }
2691                         g_free(from_name);
2692                 }
2693
2694                 if (!found) {
2695                         alertpanel_error(_("You're not the author of the article\n"));
2696                 }
2697                 
2698                 return found;
2699
2700         default:
2701                 return TRUE;
2702         }
2703 }
2704
2705 static void summary_delete_row(SummaryView *summaryview, GtkCTreeNode *row)
2706 {
2707         gboolean changed = FALSE;
2708         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2709         MsgInfo *msginfo;
2710
2711         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2712
2713         if (!check_permission(summaryview, msginfo))
2714                 return;
2715
2716         if (MSG_IS_LOCKED(msginfo->flags)) return;
2717
2718         if (MSG_IS_DELETED(msginfo->flags)) return;
2719
2720         if (MSG_IS_MOVE(msginfo->flags)) {
2721                 summaryview->moved--;
2722                 changed = TRUE;
2723         }
2724         if (MSG_IS_COPY(msginfo->flags)) {
2725                 summaryview->copied--;
2726                 changed = TRUE;
2727         }
2728         if (changed && !prefs_common.immediate_exec) {
2729                 msginfo->to_folder->op_count--;
2730                 if (msginfo->to_folder->op_count == 0)
2731                         folderview_update_item(msginfo->to_folder, 0);
2732         }
2733         msginfo->to_folder = NULL;
2734         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED, MSG_MOVE | MSG_COPY);
2735         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
2736         summaryview->deleted++;
2737
2738         if (!prefs_common.immediate_exec && 
2739             summaryview->folder_item->stype != F_TRASH)
2740                 summary_set_row_marks(summaryview, row);
2741
2742         debug_print("Message %s/%d is set to delete\n",
2743                     msginfo->folder->path, msginfo->msgnum);
2744 }
2745
2746 void summary_delete(SummaryView *summaryview)
2747 {
2748         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2749         FolderItem *item = summaryview->folder_item;
2750         GList *cur;
2751         GtkCTreeNode *sel_last = NULL;
2752         GtkCTreeNode *node;
2753
2754         if (!item) return;
2755 #if 0
2756         if (!item || item->folder->type == F_NEWS) return;
2757 #endif
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
2789                                 (summaryview, node,
2790                                  messageview_is_visible(summaryview->messageview),
2791                                  FALSE);
2792         }
2793
2794         if (prefs_common.immediate_exec || item->stype == F_TRASH)
2795                 summary_execute(summaryview);
2796         else
2797                 summary_status_show(summaryview);
2798 }
2799
2800 void summary_delete_duplicated(SummaryView *summaryview)
2801 {
2802         if (!summaryview->folder_item ||
2803             summaryview->folder_item->folder->type == F_NEWS) return;
2804         if (summaryview->folder_item->stype == F_TRASH) return;
2805
2806         main_window_cursor_wait(summaryview->mainwin);
2807         debug_print("Deleting duplicated messages...");
2808         STATUSBAR_PUSH(summaryview->mainwin,
2809                        _("Deleting duplicated messages..."));
2810
2811         gtk_ctree_pre_recursive(GTK_CTREE(summaryview->ctree), NULL,
2812                                 GTK_CTREE_FUNC(summary_delete_duplicated_func),
2813                                 summaryview);
2814
2815         if (prefs_common.immediate_exec)
2816                 summary_execute(summaryview);
2817         else
2818                 summary_status_show(summaryview);
2819
2820         debug_print("done.\n");
2821         STATUSBAR_POP(summaryview->mainwin);
2822         main_window_cursor_normal(summaryview->mainwin);
2823 }
2824
2825 static void summary_delete_duplicated_func(GtkCTree *ctree, GtkCTreeNode *node,
2826                                            SummaryView *summaryview)
2827 {
2828         GtkCTreeNode *found;
2829         MsgInfo *msginfo = GTK_CTREE_ROW(node)->row.data;
2830
2831         if (!msginfo->msgid || !*msginfo->msgid) return;
2832
2833         found = g_hash_table_lookup(summaryview->msgid_table, msginfo->msgid);
2834
2835         if (found && found != node)
2836                 summary_delete_row(summaryview, node);
2837 }
2838
2839 static void summary_unmark_row(SummaryView *summaryview, GtkCTreeNode *row)
2840 {
2841         gboolean changed = FALSE;
2842         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2843         MsgInfo *msginfo;
2844
2845         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2846         if (MSG_IS_DELETED(msginfo->flags))
2847                 summaryview->deleted--;
2848         if (MSG_IS_MOVE(msginfo->flags)) {
2849                 summaryview->moved--;
2850                 changed = TRUE;
2851         }
2852         if (MSG_IS_COPY(msginfo->flags)) {
2853                 summaryview->copied--;
2854                 changed = TRUE;
2855         }
2856         if (changed && !prefs_common.immediate_exec) {
2857                 msginfo->to_folder->op_count--;
2858                 if (msginfo->to_folder->op_count == 0)
2859                         folderview_update_item(msginfo->to_folder, 0);
2860         }
2861         msginfo->to_folder = NULL;
2862         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE | MSG_COPY);
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; cur = cur->next)
2875                 summary_unmark_row(summaryview, GTK_CTREE_NODE(cur->data));
2876
2877         summary_status_show(summaryview);
2878 }
2879
2880 static void summary_move_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2881                                 FolderItem *to_folder)
2882 {
2883         gboolean changed = FALSE;
2884         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2885         MsgInfo *msginfo;
2886
2887         g_return_if_fail(to_folder != NULL);
2888
2889         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2890         if (MSG_IS_MOVE(msginfo->flags)) {
2891                 if (!prefs_common.immediate_exec) {
2892                         msginfo->to_folder->op_count--;
2893                         if (msginfo->to_folder->op_count == 0) {
2894                                 folderview_update_item(msginfo->to_folder, 0);
2895                                 changed = TRUE;
2896                         }
2897                 }
2898         }
2899         msginfo->to_folder = to_folder;
2900         if (MSG_IS_DELETED(msginfo->flags))
2901                 summaryview->deleted--;
2902         if (MSG_IS_COPY(msginfo->flags)) {
2903                 summaryview->copied--;
2904                 if (!prefs_common.immediate_exec)
2905                         msginfo->to_folder->op_count--;
2906         }
2907         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_COPY);
2908         if (!MSG_IS_MOVE(msginfo->flags)) {
2909                 procmsg_msginfo_set_flags(msginfo, 0, MSG_MOVE);
2910                 summaryview->moved++;
2911                 changed = TRUE;
2912         }
2913         if (!prefs_common.immediate_exec) {
2914                 summary_set_row_marks(summaryview, row);
2915                 if (changed) {
2916                         msginfo->to_folder->op_count++;
2917                         if (msginfo->to_folder->op_count == 1)
2918                                 folderview_update_item(msginfo->to_folder, 0);
2919                 }
2920         }
2921
2922         debug_print("Message %d is set to move to %s\n",
2923                     msginfo->msgnum, to_folder->path);
2924 }
2925
2926 void summary_move_selected_to(SummaryView *summaryview, FolderItem *to_folder)
2927 {
2928         GList *cur;
2929
2930         if (!to_folder) return;
2931         if (!summaryview->folder_item ||
2932             summaryview->folder_item->folder->type == F_NEWS) return;
2933
2934         if (summary_is_locked(summaryview)) return;
2935
2936         if (summaryview->folder_item == to_folder) {
2937                 alertpanel_notice(_("Destination is same as current folder."));
2938                 return;
2939         }
2940
2941         for (cur = GTK_CLIST(summaryview->ctree)->selection;
2942              cur != NULL; cur = cur->next)
2943                 summary_move_row_to
2944                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
2945
2946         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
2947
2948         if (prefs_common.immediate_exec)
2949                 summary_execute(summaryview);
2950         else {
2951                 summary_status_show(summaryview);
2952
2953                 folderview_update_item(to_folder, 0);
2954         }
2955 }
2956
2957 void summary_move_to(SummaryView *summaryview)
2958 {
2959         FolderItem *to_folder;
2960
2961         if (!summaryview->folder_item ||
2962             summaryview->folder_item->folder->type == F_NEWS) return;
2963
2964         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
2965                                          FOLDER_SEL_MOVE, NULL);
2966         summary_move_selected_to(summaryview, to_folder);
2967 }
2968
2969 static void summary_copy_row_to(SummaryView *summaryview, GtkCTreeNode *row,
2970                                 FolderItem *to_folder)
2971 {
2972         gboolean changed = FALSE;
2973         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
2974         MsgInfo *msginfo;
2975
2976         g_return_if_fail(to_folder != NULL);
2977
2978         msginfo = gtk_ctree_node_get_row_data(ctree, row);
2979         if (MSG_IS_COPY(msginfo->flags)) {
2980                 if (!prefs_common.immediate_exec) {
2981                         msginfo->to_folder->op_count--;
2982                         if (msginfo->to_folder->op_count == 0) {
2983                                 folderview_update_item(msginfo->to_folder, 0);
2984                                 changed = TRUE;
2985                         }
2986                 }
2987         }
2988         msginfo->to_folder = to_folder;
2989         if (MSG_IS_DELETED(msginfo->flags))
2990                 summaryview->deleted--;
2991         if (MSG_IS_MOVE(msginfo->flags)) {
2992                 summaryview->moved--;
2993                 if (!prefs_common.immediate_exec)
2994                         msginfo->to_folder->op_count--;
2995         }
2996         procmsg_msginfo_unset_flags(msginfo, MSG_MARKED | MSG_DELETED, MSG_MOVE);
2997         if (!MSG_IS_COPY(msginfo->flags)) {
2998                 procmsg_msginfo_set_flags(msginfo, 0, MSG_COPY);
2999                 summaryview->copied++;
3000                 changed = TRUE;
3001         }
3002         if (!prefs_common.immediate_exec) {
3003                 summary_set_row_marks(summaryview, row);
3004                 if (changed) {
3005                         msginfo->to_folder->op_count++;
3006                         if (msginfo->to_folder->op_count == 1)
3007                                 folderview_update_item(msginfo->to_folder, 0);
3008                 }
3009         }
3010
3011         debug_print("Message %d is set to copy to %s\n",
3012                     msginfo->msgnum, to_folder->path);
3013 }
3014
3015 void summary_copy_selected_to(SummaryView *summaryview, FolderItem *to_folder)
3016 {
3017         GList *cur;
3018
3019         if (!to_folder) return;
3020         if (!summaryview->folder_item) return;
3021
3022         if (summary_is_locked(summaryview)) return;
3023
3024         if (summaryview->folder_item == to_folder) {
3025                 alertpanel_notice
3026                         (_("Destination to copy is same as current folder."));
3027                 return;
3028         }
3029
3030         for (cur = GTK_CLIST(summaryview->ctree)->selection;
3031              cur != NULL; cur = cur->next)
3032                 summary_copy_row_to
3033                         (summaryview, GTK_CTREE_NODE(cur->data), to_folder);
3034
3035         summary_step(summaryview, GTK_SCROLL_STEP_FORWARD);
3036
3037         if (prefs_common.immediate_exec)
3038                 summary_execute(summaryview);
3039         else {
3040                 summary_status_show(summaryview);
3041
3042                 folderview_update_item(to_folder, 0);
3043         }
3044 }
3045
3046 void summary_copy_to(SummaryView *summaryview)
3047 {
3048         FolderItem *to_folder;
3049
3050         if (!summaryview->folder_item) return;
3051
3052         to_folder = foldersel_folder_sel(summaryview->folder_item->folder,
3053                                          FOLDER_SEL_COPY, NULL);
3054         summary_copy_selected_to(summaryview, to_folder);
3055 }
3056
3057 void summary_add_address(SummaryView *summaryview)
3058 {
3059         MsgInfo *msginfo;
3060         gchar *from;
3061
3062         msginfo = gtk_ctree_node_get_row_data(GTK_CTREE(summaryview->ctree),
3063                                               summaryview->selected);
3064         if (!msginfo) return;
3065
3066         Xstrdup_a(from, msginfo->from, return);
3067         eliminate_address_comment(from);
3068         extract_address(from);
3069         addressbook_add_contact(msginfo->fromname, from, NULL);
3070 }
3071
3072 void summary_select_all(SummaryView *summaryview)
3073 {
3074         if (summaryview->messages >= 500) {
3075                 STATUSBAR_PUSH(summaryview->mainwin,
3076                                _("Selecting all messages..."));
3077                 main_window_cursor_wait(summaryview->mainwin);
3078         }
3079
3080         gtk_clist_select_all(GTK_CLIST(summaryview->ctree));
3081
3082         if (summaryview->messages >= 500) {
3083                 STATUSBAR_POP(summaryview->mainwin);
3084                 main_window_cursor_normal(summaryview->mainwin);
3085         }
3086 }
3087
3088 void summary_unselect_all(SummaryView *summaryview)
3089 {
3090         gtk_sctree_unselect_all(GTK_SCTREE(summaryview->ctree));
3091 }
3092
3093 void summary_save_as(SummaryView *summaryview)
3094 {
3095         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3096         MsgInfo *msginfo;
3097         gchar *filename = NULL;
3098         gchar *src, *dest;
3099
3100         if (!summaryview->selected) return;
3101         msginfo = gtk_ctree_node_get_row_data(ctree, summaryview->selected);
3102         if (!msginfo) return;
3103
3104         if (msginfo->subject) {
3105                 Xstrdup_a(filename, msginfo->subject, return);
3106                 subst_for_filename(filename);
3107         }
3108         dest = filesel_select_file(_("Save as"), filename);
3109         if (!dest) return;
3110         if (is_file_exist(dest)) {
3111                 AlertValue aval;
3112
3113                 aval = alertpanel(_("Overwrite"),
3114                                   _("Overwrite existing file?"),
3115                                   _("OK"), _("Cancel"), NULL);
3116                 if (G_ALERTDEFAULT != aval) return;
3117         }
3118
3119         src = procmsg_get_message_file(msginfo);
3120         if (copy_file(src, dest, TRUE) < 0) {
3121                 alertpanel_error(_("Can't save the file `%s'."),
3122                                  g_basename(dest));
3123         }
3124         g_free(src);
3125 }
3126
3127 void summary_print(SummaryView *summaryview)
3128 {
3129         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3130         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3131         MsgInfo *msginfo;
3132         GList *cur;
3133         gchar *cmdline;
3134         gchar *p;
3135
3136         if (clist->selection == NULL) return;
3137
3138         cmdline = input_dialog(_("Print"),
3139                                _("Enter the print command line:\n"
3140                                  "(`%s' will be replaced with file name)"),
3141                                prefs_common.print_cmd);
3142         if (!cmdline) return;
3143         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
3144             strchr(p + 2, '%')) {
3145                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
3146                                  cmdline);
3147                 g_free(cmdline);
3148                 return;
3149         }
3150
3151         for (cur = clist->selection; cur != NULL; cur = cur->next) {
3152                 msginfo = gtk_ctree_node_get_row_data
3153                         (ctree, GTK_CTREE_NODE(cur->data));
3154                 if (msginfo) procmsg_print_message(msginfo, cmdline);
3155         }
3156
3157         g_free(cmdline);
3158 }
3159
3160 gboolean summary_execute(SummaryView *summaryview)
3161 {
3162         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3163         GtkCList *clist = GTK_CLIST(summaryview->ctree);
3164         GtkCTreeNode *node, *next;
3165
3166         if (!summaryview->folder_item) return FALSE;
3167
3168         if (summary_is_locked(summaryview)) return FALSE;
3169         summary_lock(summaryview);
3170
3171         gtk_clist_freeze(clist);
3172
3173         if (summaryview->folder_item->threaded)
3174                 summary_unthread_for_exec(summaryview);
3175
3176         summary_execute_move(summaryview);
3177         summary_execute_copy(summaryview);
3178         summary_execute_delete(summaryview);
3179
3180         node = GTK_CTREE_NODE(clist->row_list);
3181         while (node != NULL) {
3182                 next = gtkut_ctree_node_next(ctree, node);
3183                 if (gtk_ctree_node_get_row_data(ctree, node) == NULL) {
3184                         if (node == summaryview->displayed) {
3185                                 messageview_clear(summaryview->messageview);
3186                                 summaryview->displayed = NULL;
3187                         }
3188                         if (GTK_CTREE_ROW(node)->children != NULL)
3189                                 g_warning("summary_execute(): children != NULL\n");
3190                         else
3191                                 gtk_ctree_remove_node(ctree, node);
3192                 }
3193                 node = next;
3194         }
3195
3196         if (summaryview->folder_item->threaded)
3197                 summary_thread_build(summaryview);
3198
3199         summaryview->selected = clist->selection ?
3200                 GTK_CTREE_NODE(clist->selection->data) : NULL;
3201
3202         if (!GTK_CLIST(summaryview->ctree)->row_list) {
3203                 menu_set_insensitive_all
3204                         (GTK_MENU_SHELL(summaryview->popupmenu));
3205                 gtk_widget_grab_focus(summaryview->folderview->ctree);
3206         } else
3207                 gtk_widget_grab_focus(summaryview->ctree);
3208
3209         summary_update_status(summaryview);
3210         summary_status_show(summaryview);
3211
3212         gtk_ctree_node_moveto(ctree, summaryview->selected, -1, 0.5, 0);
3213
3214         gtk_clist_thaw(clist);
3215
3216         summary_unlock(summaryview);
3217         return TRUE;
3218 }
3219
3220 static void summary_execute_move(SummaryView *summaryview)
3221 {
3222         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3223         GSList *cur;
3224
3225         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3226
3227         /* search moving messages and execute */
3228         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_move_func,
3229                                 summaryview);
3230
3231         if (summaryview->mlist) {
3232                 procmsg_move_messages(summaryview->mlist);
3233
3234                 folderview_update_item_foreach(summaryview->folder_table, FALSE);
3235
3236                 for (cur = summaryview->mlist; cur != NULL; cur = cur->next)
3237                         procmsg_msginfo_free((MsgInfo *)cur->data);
3238                 g_slist_free(summaryview->mlist);
3239                 summaryview->mlist = NULL;
3240         }
3241
3242         folderview_update_item(summaryview->folder_item, FALSE);
3243         g_hash_table_destroy(summaryview->folder_table);
3244         summaryview->folder_table = NULL;
3245 }
3246
3247 static void summary_execute_move_func(GtkCTree *ctree, GtkCTreeNode *node,
3248                                       gpointer data)
3249 {
3250         SummaryView *summaryview = data;
3251         MsgInfo *msginfo;
3252
3253         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3254
3255         if (msginfo && MSG_IS_MOVE(msginfo->flags) && msginfo->to_folder) {
3256                 if (!prefs_common.immediate_exec &&
3257                     msginfo->to_folder->op_count > 0)
3258                         msginfo->to_folder->op_count--;
3259
3260                 g_hash_table_insert(summaryview->folder_table,
3261                                     msginfo->to_folder, GINT_TO_POINTER(1));
3262
3263                 summaryview->mlist =
3264                         g_slist_append(summaryview->mlist, msginfo);
3265                 gtk_ctree_node_set_row_data(ctree, node, NULL);
3266
3267                 if (msginfo->msgid && *msginfo->msgid &&
3268                     node == g_hash_table_lookup(summaryview->msgid_table,
3269                                                 msginfo->msgid))
3270                         g_hash_table_remove(summaryview->msgid_table,
3271                                             msginfo->msgid);
3272         }
3273 }
3274
3275 static void summary_execute_copy(SummaryView *summaryview)
3276 {
3277         GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
3278
3279         summaryview->folder_table = g_hash_table_new(NULL, NULL);
3280
3281         /* search copying messages and execute */
3282         gtk_ctree_pre_recursive(ctree, NULL, summary_execute_copy_func,
3283                                 summaryview);
3284
3285         if (summaryview->mlist) {
3286                 procmsg_copy_messages(summaryview->mlist);
3287
3288                 /* folder_item_scan_foreach(summaryview->folder_table); */
3289                 folderview_update_item_foreach(summaryview->folder_table, FALSE);
3290
3291                 g_slist_free(summaryview->mlist);
3292                 summaryview->mlist = NULL;
3293         }
3294
3295         g_hash_table_destroy(summaryview->folder_table);
3296         summaryview->folder_table = NULL;
3297 }
3298
3299 static void summary_execute_copy_func(GtkCTree *ctree, GtkCTreeNode *node,
3300                                       gpointer data)
3301 {
3302         SummaryView *summaryview = data;
3303         MsgInfo *msginfo;
3304
3305         msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
3306
3307         if (msginfo && MSG_IS_COPY(msginfo->flags) && msginfo->to_folder) {
3308                 if (!prefs_common.immediate_exec &&
3309                     msginfo->to_folder->op_count > 0)
3310                         msginfo->to_folder->op_count--;
3311
3312                 g_hash_table_insert(summaryview->folder_table,
3313                                     msginfo->to_folder, GINT_TO_POINTER(1));
3314
3315                 summaryview->mlist =
3316                         g_slist_append(summaryview->mlist, msginfo);
3317
3318                 procmsg_msginfo_unset_flags(msginfo, 0, MSG_COPY);