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