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