7ebdedb6b7993c2e540f0f846c045dc836148c0b
[claws.git] / src / summaryview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtk.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "main.h"
33 #include "menu.h"
34 #include "mbox.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "messageview.h"
39 #include "foldersel.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "sourcewindow.h"
43 #include "prefs_common.h"
44 #include "prefs_summary_column.h"
45 #include "prefs_summary_open.h"
46 #include "prefs_filtering.h"
47 #include "account.h"
48 #include "compose.h"
49 #include "utils.h"
50 #include "gtkutils.h"
51 #include "stock_pixmap.h"
52 #include "filesel.h"
53 #include "alertpanel.h"
54 #include "inputdialog.h"
55 #include "statusbar.h"
56 #include "folder.h"
57 #include "colorlabel.h"
58 #include "inc.h"
59 #include "imap.h"
60 #include "addressbook.h"
61 #include "addr_compl.h"
62 #include "folder_item_prefs.h"
63 #include "filtering.h"
64 #include "string_match.h"
65 #include "toolbar.h"
66 #include "news.h"
67 #include "hooks.h"
68 #include "description_window.h"
69 #include "folderutils.h"
70 #include "quicksearch.h"
71 #include "partial_download.h"
72 #include "tags.h"
73 #include "timing.h"
74 #include "log.h"
75 #include "edittags.h"
76 #include "manual.h"
77
78 #define SUMMARY_COL_MARK_WIDTH          10
79 #define SUMMARY_COL_STATUS_WIDTH        13
80 #define SUMMARY_COL_LOCKED_WIDTH        13
81 #define SUMMARY_COL_MIME_WIDTH          11
82
83 static int normal_row_height = -1;
84 static GtkStyle *bold_style;
85 static GtkStyle *bold_marked_style;
86 static GtkStyle *bold_deleted_style;
87 static GtkStyle *small_style;
88 static GtkStyle *small_marked_style;
89 static GtkStyle *small_deleted_style;
90
91 static GdkPixbuf *markxpm;
92 static GdkPixbuf *deletedxpm;
93 static GdkPixbuf *movedxpm;
94 static GdkPixbuf *copiedxpm;
95
96 static GdkPixbuf *newxpm;
97 static GdkPixbuf *unreadxpm;
98 static GdkPixbuf *repliedxpm;
99 static GdkPixbuf *forwardedxpm;
100 static GdkPixbuf *ignorethreadxpm;
101 static GdkPixbuf *watchthreadxpm;
102 static GdkPixbuf *lockedxpm;
103 static GdkPixbuf *spamxpm;
104
105 static GdkPixbuf *clipxpm;
106 static GdkPixbuf *keyxpm;
107 static GdkPixbuf *clipkeyxpm;
108 static GdkPixbuf *keysignxpm;
109 static GdkPixbuf *gpgsignedxpm;
110 static GdkPixbuf *clipgpgsignedxpm;
111
112 static void summary_free_msginfo_func   (GtkCMCTree             *ctree,
113                                          GtkCMCTreeNode         *node,
114                                          gpointer                data);
115 static void summary_set_marks_func      (GtkCMCTree             *ctree,
116                                          GtkCMCTreeNode         *node,
117                                          gpointer                data);
118
119 void  summary_set_menu_sensitive        (SummaryView            *summaryview);
120 guint summary_get_msgnum                (SummaryView            *summaryview,
121                                          GtkCMCTreeNode         *node);
122
123
124 static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
125                                              guint action);
126 static void summary_set_hide_del_msgs_menu (SummaryView *summaryview,
127                                              guint action);
128
129 static GtkCMCTreeNode *summary_find_prev_msg
130                                         (SummaryView            *summaryview,
131                                          GtkCMCTreeNode         *current_node);
132 static GtkCMCTreeNode *summary_find_next_msg
133                                         (SummaryView            *summaryview,
134                                          GtkCMCTreeNode         *current_node);
135
136 static GtkCMCTreeNode *summary_find_prev_flagged_msg
137                                         (SummaryView    *summaryview,
138                                          GtkCMCTreeNode *current_node,
139                                          MsgPermFlags    flags,
140                                          gboolean        start_from_prev);
141 static GtkCMCTreeNode *summary_find_next_flagged_msg
142                                         (SummaryView    *summaryview,
143                                          GtkCMCTreeNode *current_node,
144                                          MsgPermFlags    flags,
145                                          gboolean        start_from_next);
146
147 static GtkCMCTreeNode *summary_find_msg_by_msgnum
148                                         (SummaryView            *summaryview,
149                                          guint                   msgnum);
150
151 static void summary_update_status       (SummaryView            *summaryview);
152
153 /* display functions */
154 static void summary_status_show         (SummaryView            *summaryview);
155 static void summary_set_column_titles   (SummaryView            *summaryview);
156 static void summary_set_ctree_from_list (SummaryView            *summaryview,
157                                          GSList                 *mlist);
158 static inline void summary_set_header   (SummaryView            *summaryview,
159                                          gchar                  *text[],
160                                          MsgInfo                *msginfo);
161 static void summary_display_msg         (SummaryView            *summaryview,
162                                          GtkCMCTreeNode         *row);
163 static void summary_display_msg_full    (SummaryView            *summaryview,
164                                          GtkCMCTreeNode         *row,
165                                          gboolean                new_window,
166                                          gboolean                all_headers);
167 static void summary_set_row_marks       (SummaryView            *summaryview,
168                                          GtkCMCTreeNode         *row);
169
170 static gboolean summary_set_row_tag     (SummaryView            *summaryview, 
171                                          GtkCMCTreeNode                 *row, 
172                                          gboolean                refresh,
173                                          gboolean                set, 
174                                          gint                    id);
175 /* message handling */
176 static void summary_mark_row            (SummaryView            *summaryview,
177                                          GtkCMCTreeNode         *row);
178 static void summary_lock_row            (SummaryView            *summaryview,
179                                          GtkCMCTreeNode         *row);
180 static void summary_unlock_row          (SummaryView            *summaryview,
181                                          GtkCMCTreeNode         *row);
182 static void summary_mark_row_as_read    (SummaryView            *summaryview,
183                                          GtkCMCTreeNode         *row);
184 static void summary_mark_row_as_unread  (SummaryView            *summaryview,
185                                          GtkCMCTreeNode         *row);
186 static void summary_delete_row          (SummaryView            *summaryview,
187                                          GtkCMCTreeNode         *row);
188 static void summary_unmark_row          (SummaryView            *summaryview,
189                                          GtkCMCTreeNode         *row);
190 static void summary_move_row_to         (SummaryView            *summaryview,
191                                          GtkCMCTreeNode         *row,
192                                          FolderItem             *to_folder);
193 static void summary_copy_row_to         (SummaryView            *summaryview,
194                                          GtkCMCTreeNode         *row,
195                                          FolderItem             *to_folder);
196
197 static gint summary_execute_move        (SummaryView            *summaryview);
198 static void summary_execute_move_func   (GtkCMCTree             *ctree,
199                                          GtkCMCTreeNode         *node,
200                                          gpointer                data);
201 static void summary_execute_copy        (SummaryView            *summaryview);
202 static void summary_execute_copy_func   (GtkCMCTree             *ctree,
203                                          GtkCMCTreeNode         *node,
204                                          gpointer                data);
205 static void summary_execute_delete      (SummaryView            *summaryview);
206 static void summary_execute_delete_func (GtkCMCTree             *ctree,
207                                          GtkCMCTreeNode         *node,
208                                          gpointer                data);
209 static void summary_execute_expunge     (SummaryView            *summaryview);
210
211 static void summary_thread_init         (SummaryView            *summaryview);
212
213 static void summary_unthread_for_exec           (SummaryView    *summaryview);
214 static void summary_unthread_for_exec_func      (GtkCMCTree     *ctree,
215                                                  GtkCMCTreeNode *node,
216                                                  gpointer        data);
217
218 void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
219                               GSList * mlist);
220
221 static void summary_filter_func         (MsgInfo *msginfo);
222
223 static void summary_colorlabel_menu_item_activate_cb
224                                           (GtkWidget    *widget,
225                                            gpointer      data);
226 static void summary_colorlabel_menu_item_activate_item_cb
227                                           (GtkMenuItem  *label_menu_item,
228                                            gpointer      data);
229 static void summary_colorlabel_menu_create(SummaryView  *summaryview,
230                                            gboolean  refresh);
231 static void summary_tags_menu_item_activate_cb
232                                           (GtkWidget    *widget,
233                                            gpointer      data);
234 static void summary_tags_menu_item_activate_item_cb
235                                           (GtkMenuItem  *label_menu_item,
236                                            gpointer      data);
237 static void summary_tags_menu_create(SummaryView        *summaryview,
238                                            gboolean  refresh);
239
240 static GtkWidget *summary_ctree_create  (SummaryView    *summaryview);
241
242 /* callback functions */
243 static gint summary_toggle_pressed      (GtkWidget              *eventbox,
244                                          GdkEventButton         *event,
245                                          SummaryView            *summaryview);
246 #ifdef GENERIC_UMPC
247 static void summary_toggle_multiple_pressed
248                                         (GtkWidget              *widget,
249                                          SummaryView            *summaryview);
250 #endif
251 static gint summary_folder_eventbox_pressed     
252                                         (GtkWidget              *eventbox,
253                                          GdkEventButton         *event,
254                                          SummaryView            *summaryview);
255 static gboolean summary_button_pressed  (GtkWidget              *ctree,
256                                          GdkEventButton         *event,
257                                          SummaryView            *summaryview);
258 static gboolean summary_button_released (GtkWidget              *ctree,
259                                          GdkEventButton         *event,
260                                          SummaryView            *summaryview);
261 static gboolean summary_key_pressed     (GtkWidget              *ctree,
262                                          GdkEventKey            *event,
263                                          SummaryView            *summaryview);
264 static void summary_tree_expanded       (GtkCMCTree             *ctree,
265                                          GtkCMCTreeNode         *node,
266                                          SummaryView            *summaryview);
267 static void summary_tree_collapsed      (GtkCMCTree             *ctree,
268                                          GtkCMCTreeNode         *node,
269                                          SummaryView            *summaryview);
270 static void summary_selected            (GtkCMCTree             *ctree,
271                                          GtkCMCTreeNode         *row,
272                                          gint                    column,
273                                          SummaryView            *summaryview);
274 static void summary_unselected          (GtkCMCTree             *ctree,
275                                          GtkCMCTreeNode         *row,
276                                          gint                    column,
277                                          SummaryView            *summaryview);
278 static void summary_col_resized         (GtkCMCList             *clist,
279                                          gint                    column,
280                                          gint                    width,
281                                          SummaryView            *summaryview);
282 static void summary_mark_clicked        (GtkWidget              *button,
283                                          SummaryView            *summaryview);
284 static void summary_status_clicked      (GtkWidget              *button,
285                                          SummaryView            *summaryview);
286 static void summary_mime_clicked        (GtkWidget              *button,
287                                          SummaryView            *summaryview);
288 static void summary_num_clicked         (GtkWidget              *button,
289                                          SummaryView            *summaryview);
290 static void summary_score_clicked       (GtkWidget *button,
291                                          SummaryView *summaryview);
292 static void summary_size_clicked        (GtkWidget              *button,
293                                          SummaryView            *summaryview);
294 static void summary_date_clicked        (GtkWidget              *button,
295                                          SummaryView            *summaryview);
296 static void summary_from_clicked        (GtkWidget              *button,
297                                          SummaryView            *summaryview);
298 static void summary_to_clicked          (GtkWidget              *button,
299                                          SummaryView            *summaryview);
300 static void summary_subject_clicked     (GtkWidget              *button,
301                                          SummaryView            *summaryview);
302 static void summary_score_clicked       (GtkWidget              *button,
303                                          SummaryView            *summaryview);
304 static void summary_locked_clicked      (GtkWidget              *button,
305                                          SummaryView            *summaryview);
306 static void summary_tags_clicked        (GtkWidget              *button,
307                                          SummaryView            *summaryview);
308
309 static void summary_start_drag          (GtkWidget        *widget, 
310                                          int button,
311                                          GdkEvent *event,
312                                          SummaryView      *summaryview);
313 static void summary_drag_data_get       (GtkWidget        *widget,
314                                          GdkDragContext   *drag_context,
315                                          GtkSelectionData *selection_data,
316                                          guint             info,
317                                          guint             time,
318                                          SummaryView      *summaryview);
319 static void summary_drag_data_received(GtkWidget        *widget,
320                                         GdkDragContext   *drag_context,
321                                         gint              x,
322                                         gint              y,
323                                         GtkSelectionData *data,
324                                         guint             info,
325                                         guint             time,
326                                         SummaryView       *summaryview);
327 static gboolean summary_drag_motion_cb(GtkWidget      *widget,
328                                           GdkDragContext *context,
329                                           gint            x,
330                                           gint            y,
331                                           guint           time,
332                                           SummaryView    *summaryview);
333 static void summary_drag_end(GtkWidget *widget,
334                                           GdkDragContext *drag_context,
335                                           SummaryView    *summaryview);
336 /* custom compare functions for sorting */
337
338 static gint summary_cmp_by_mark         (GtkCMCList             *clist,
339                                          gconstpointer           ptr1,
340                                          gconstpointer           ptr2);
341 static gint summary_cmp_by_status       (GtkCMCList             *clist,
342                                          gconstpointer           ptr1,
343                                          gconstpointer           ptr2);
344 static gint summary_cmp_by_mime         (GtkCMCList             *clist,
345                                          gconstpointer           ptr1,
346                                          gconstpointer           ptr2);
347 static gint summary_cmp_by_num          (GtkCMCList             *clist,
348                                          gconstpointer           ptr1,
349                                          gconstpointer           ptr2);
350 static gint summary_cmp_by_size         (GtkCMCList             *clist,
351                                          gconstpointer           ptr1,
352                                          gconstpointer           ptr2);
353 static gint summary_cmp_by_date         (GtkCMCList             *clist,
354                                          gconstpointer           ptr1,
355                                          gconstpointer           ptr2);
356 static gint summary_cmp_by_thread_date  (GtkCMCList             *clist,
357                                          gconstpointer           ptr1,
358                                          gconstpointer           ptr2);
359 static gint summary_cmp_by_from         (GtkCMCList             *clist,
360                                          gconstpointer           ptr1,
361                                          gconstpointer           ptr2);
362 static gint summary_cmp_by_simplified_subject
363                                         (GtkCMCList             *clist, 
364                                          gconstpointer           ptr1, 
365                                          gconstpointer           ptr2);
366 static gint summary_cmp_by_score        (GtkCMCList             *clist,
367                                          gconstpointer           ptr1,
368                                          gconstpointer           ptr2);
369 static gint summary_cmp_by_label        (GtkCMCList             *clist,
370                                          gconstpointer           ptr1,
371                                          gconstpointer           ptr2);
372 static gint summary_cmp_by_to           (GtkCMCList             *clist,
373                                          gconstpointer           ptr1,
374                                          gconstpointer           ptr2);
375 static gint summary_cmp_by_subject      (GtkCMCList             *clist,
376                                          gconstpointer           ptr1,
377                                          gconstpointer           ptr2);
378 static gint summary_cmp_by_locked       (GtkCMCList             *clist,
379                                          gconstpointer           ptr1, 
380                                          gconstpointer           ptr2);
381 static gint summary_cmp_by_tags         (GtkCMCList             *clist,
382                                          gconstpointer           ptr1, 
383                                          gconstpointer           ptr2);
384
385 static void quicksearch_execute_cb      (QuickSearch    *quicksearch,
386                                          gpointer        data);
387 static void tog_searchbar_cb            (GtkWidget      *w,
388                                          gpointer        data);
389
390 static void summary_find_answers        (SummaryView    *summaryview, 
391                                          MsgInfo        *msg);
392
393 static gboolean summary_update_msg      (gpointer source, gpointer data);
394 static gboolean summary_update_folder_item_hook(gpointer source, gpointer data);
395 static gboolean summary_update_folder_hook(gpointer source, gpointer data);
396 static void summary_set_colorlabel_color (GtkCMCTree            *ctree,
397                                    GtkCMCTreeNode               *node,
398                                    guint                 labelcolor);
399 static void summary_thread_build(SummaryView *summaryview);
400
401 GtkTargetEntry summary_drag_types[3] =
402 {
403         {"text/uri-list", 0, TARGET_MAIL_URI_LIST},
404         {"claws-mail/internal", GTK_TARGET_SAME_APP, TARGET_DUMMY},
405         {"claws-mail/msg-path-list", 0, TARGET_MAIL_CM_PATH_LIST},
406 };
407
408 #define DO_ACTION(name, act) {                                          \
409         if(!strcmp(name, a_name)) {                                     \
410                 act;                                                    \
411         }                                                               \
412 }
413
414 static GtkActionEntry summary_popup_entries[] =
415 {
416         {"SummaryViewPopup",                            NULL, "SummaryViewPopup" },
417         {"SummaryViewPopup/ReplyTo",                    NULL, N_("Repl_y to") },
418         {"SummaryViewPopup/Mark",                       NULL, N_("_Mark") },
419         {"SummaryViewPopup/ColorLabel",                 NULL, N_("Color la_bel") },
420         {"SummaryViewPopup/Tags",                       NULL, N_("Ta_gs") },
421         {"SummaryViewPopup/CreateFilterRule",           NULL, N_("Create _filter rule") },
422 #ifndef GENERIC_UMPC
423         {"SummaryViewPopup/CreateProcessingRule",       NULL, N_("Create processing rule") },
424 #endif
425         {"SummaryViewPopup/View",                       NULL, N_("_View") },
426 };
427
428 static const gchar *const col_label[N_SUMMARY_COLS] = {
429         "",             /* S_COL_MARK    */
430         N_("S"),        /* S_COL_STATUS  */
431         "",             /* S_COL_MIME    */
432         N_("Subject"),  /* S_COL_SUBJECT */
433         N_("From"),     /* S_COL_FROM    */
434         N_("To"),       /* S_COL_TO      */
435         N_("Date"),     /* S_COL_DATE    */
436         N_("Size"),     /* S_COL_SIZE    */
437         N_("#"),        /* S_COL_NUMBER  */
438         N_("Score"),    /* S_COL_SCORE   */
439         "",             /* S_COL_LOCKED  */
440         N_("Tags"),     /* S_COL_TAGS    */
441 };
442
443 void summary_freeze(SummaryView *summaryview)
444 {
445         if (summaryview)
446                 gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
447 }
448
449 void summary_thaw(SummaryView *summaryview)
450 {
451         if (summaryview)
452                 gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
453 }
454
455 void summary_grab_focus(SummaryView *summaryview)
456 {
457         if (summaryview)
458                 gtk_widget_grab_focus(summaryview->ctree);
459 }
460
461 GtkWidget *summary_get_main_widget(SummaryView *summaryview)
462 {
463         if (summaryview)
464                 return summaryview->ctree;
465         else
466                 return NULL;
467 }
468
469 #define START_LONG_OPERATION(summaryview,force_freeze) {        \
470         summary_lock(summaryview);                              \
471         main_window_cursor_wait(summaryview->mainwin);          \
472         if (force_freeze || sc_g_list_bigger(GTK_CMCLIST(summaryview->ctree)->selection, 1)) {\
473                 froze = TRUE;                                           \
474                 summary_freeze(summaryview);    \
475         }                                                       \
476         folder_item_update_freeze();                            \
477         inc_lock();                                             \
478         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,          \
479                       summaryview->msginfo_update_callback_id); \
480 }
481 #define END_LONG_OPERATION(summaryview) {                       \
482         inc_unlock();                                           \
483         folder_item_update_thaw();                              \
484         if (froze)                                              \
485                 summary_thaw(summaryview);      \
486         main_window_cursor_normal(summaryview->mainwin);        \
487         summary_unlock(summaryview);                            \
488         summaryview->msginfo_update_callback_id =               \
489                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST,    \
490                 summary_update_msg, (gpointer) summaryview);    \
491 }
492
493 SummaryView *summary_create(MainWindow *mainwin)
494 {
495         SummaryView *summaryview;
496         GtkWidget *vbox;
497         GtkWidget *scrolledwin;
498         GtkWidget *ctree;
499         GtkWidget *hbox;
500         GtkWidget *hbox_l;
501         GtkWidget *stat_box;
502         GtkWidget *stat_box2;
503         GtkWidget *stat_vbox;
504         GtkWidget *statlabel_folder;
505         GtkWidget *statlabel_select;
506         GtkWidget *statlabel_msgs;
507         GtkWidget *hbox_spc;
508         GtkWidget *toggle_eventbox;
509 #ifdef GENERIC_UMPC
510         GtkWidget *multiple_sel_togbtn;
511 #endif
512         GtkWidget *toggle_arrow;
513         GtkWidget *toggle_search;
514         QuickSearch *quicksearch;
515         CLAWS_TIP_DECL();
516
517         debug_print("Creating summary view...\n");
518         summaryview = g_new0(SummaryView, 1);
519
520 #if !(GTK_CHECK_VERSION(2,12,0))
521         summaryview->tooltips = tips;
522 #endif
523 #define SUMMARY_VBOX_SPACING 3
524         vbox = gtk_vbox_new(FALSE, SUMMARY_VBOX_SPACING);
525         
526         /* create status label */
527         hbox = gtk_hbox_new(FALSE, 0);
528         gtk_widget_show(hbox);
529
530         stat_vbox = gtk_vbox_new(FALSE, 0);
531         gtk_widget_show(stat_vbox);
532
533         stat_box = gtk_hbox_new(FALSE, 0);
534         gtk_widget_show(stat_box);
535         
536         stat_box2 = gtk_hbox_new(FALSE, 0);
537         gtk_widget_show(stat_box2);
538         
539         toggle_search = gtk_toggle_button_new();
540         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_search),
541                                      prefs_common.show_searchbar);
542         gtkut_widget_set_can_focus(toggle_search, FALSE);
543         gtk_widget_show(toggle_search);
544
545         CLAWS_SET_TIP(toggle_search, _("Toggle quick search bar"));
546         
547         gtk_box_pack_start(GTK_BOX(hbox), toggle_search, FALSE, FALSE, 2);      
548
549         gtk_box_pack_start(GTK_BOX(hbox), stat_vbox, TRUE, TRUE, 0);    
550         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box, TRUE, TRUE, 0);        
551         gtk_box_pack_start(GTK_BOX(stat_vbox), stat_box2, TRUE, TRUE, 0);       
552
553         hbox_l = gtk_hbox_new(FALSE, 0);
554         gtk_widget_show(hbox_l);
555         gtk_box_pack_start(GTK_BOX(stat_box), hbox_l, TRUE, TRUE, 0);
556  
557         statlabel_folder = gtk_label_new("");
558         gtk_widget_show(statlabel_folder);
559         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_folder, FALSE, FALSE, 2);
560         statlabel_select = gtk_label_new("");
561         gtk_widget_show(statlabel_select);
562         gtk_box_pack_start(GTK_BOX(hbox_l), statlabel_select, FALSE, FALSE, 12);
563  
564         /* toggle view button */
565         toggle_eventbox = gtk_event_box_new();
566         gtk_widget_show(toggle_eventbox);
567         
568         gtk_box_pack_end(GTK_BOX(hbox), toggle_eventbox, FALSE, FALSE, 4);
569
570         toggle_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
571         gtk_widget_show(toggle_arrow);
572         gtk_container_add(GTK_CONTAINER(toggle_eventbox), toggle_arrow);
573         g_signal_connect(G_OBJECT(toggle_eventbox), "button_press_event",
574                          G_CALLBACK(summary_toggle_pressed),
575                          summaryview);
576
577 #ifdef GENERIC_UMPC
578         multiple_sel_togbtn = gtk_toggle_button_new();
579         gtk_widget_show(multiple_sel_togbtn);
580         gtk_box_pack_end(GTK_BOX(hbox), multiple_sel_togbtn, FALSE, FALSE, 4);
581         CLAWS_SET_TIP(multiple_sel_togbtn,
582                              _("Toggle multiple selection"));
583         g_signal_connect(G_OBJECT(multiple_sel_togbtn), "toggled",
584                          G_CALLBACK(summary_toggle_multiple_pressed),
585                          summaryview);
586 #endif
587         
588         statlabel_msgs = gtk_label_new("");
589         gtk_widget_show(statlabel_msgs);
590         gtk_box_pack_end(GTK_BOX(stat_box), statlabel_msgs, FALSE, FALSE, 4);
591
592         hbox_spc = gtk_hbox_new(FALSE, 0);
593         gtk_widget_show(hbox_spc);
594         gtk_box_pack_end(GTK_BOX(hbox), hbox_spc, FALSE, FALSE, 6);
595
596         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
597         gtk_widget_show(scrolledwin);
598         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
599                                        GTK_POLICY_AUTOMATIC,
600                                        GTK_POLICY_AUTOMATIC);
601         summaryview->mainwidget_book = gtk_notebook_new();
602         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(summaryview->mainwidget_book), FALSE);
603         gtk_notebook_set_show_border(GTK_NOTEBOOK(summaryview->mainwidget_book), FALSE);
604 #ifndef GENERIC_UMPC
605         gtk_container_add(GTK_CONTAINER(summaryview->mainwidget_book),
606                 scrolledwin);
607         gtk_box_pack_start(GTK_BOX(vbox), summaryview->mainwidget_book, TRUE, TRUE, 0);
608 #endif
609         gtk_widget_set_size_request(vbox,
610                              prefs_common.summaryview_width,
611                              prefs_common.summaryview_height);
612
613         ctree = summary_ctree_create(summaryview);
614         gtk_widget_show(ctree);
615
616         gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
617                                             GTK_CMCLIST(ctree)->hadjustment);
618         gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scrolledwin),
619                                             GTK_CMCLIST(ctree)->vadjustment);
620         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
621
622         /* status label */
623         gtk_widget_show_all(stat_vbox);
624         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
625
626         /* quick search */
627         quicksearch = quicksearch_new();
628         gtk_box_pack_start(GTK_BOX(vbox), quicksearch_get_widget(quicksearch), FALSE, FALSE, 0);
629
630 #ifdef GENERIC_UMPC
631         gtk_container_add(GTK_CONTAINER(summaryview->mainwidget_book),
632                 scrolledwin);
633         gtk_box_pack_start(GTK_BOX(vbox), summaryview->mainwidget_book, TRUE, TRUE, 0);
634 #endif
635         quicksearch_set_execute_callback(quicksearch, quicksearch_execute_cb, summaryview);
636
637         g_signal_connect (G_OBJECT(toggle_search), "toggled",
638                           G_CALLBACK(tog_searchbar_cb), summaryview);
639
640         /* create popup menu */
641         
642         gtk_action_group_add_actions(mainwin->action_group, summary_popup_entries,
643                         G_N_ELEMENTS(summary_popup_entries), (gpointer)summaryview);
644
645         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
646         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus", "SummaryViewPopup", "SummaryViewPopup", GTK_UI_MANAGER_MENU)
647         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Reply", "Message/Reply", GTK_UI_MANAGER_MENUITEM)
648 #ifndef GENERIC_UMPC
649         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ReplyTo", "SummaryViewPopup/ReplyTo", GTK_UI_MANAGER_MENU)
650         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator1", "Message/---", GTK_UI_MANAGER_SEPARATOR)
651 #endif
652         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Forward", "Message/Forward", GTK_UI_MANAGER_MENUITEM)
653 #ifndef GENERIC_UMPC
654         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ForwardAtt", "Message/ForwardAtt", GTK_UI_MANAGER_MENUITEM)
655         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Redirect", "Message/Redirect", GTK_UI_MANAGER_MENUITEM)
656 #endif
657         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator2", "Message/---", GTK_UI_MANAGER_SEPARATOR)
658         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Move", "Message/Move", GTK_UI_MANAGER_MENUITEM)
659         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Copy", "Message/Copy", GTK_UI_MANAGER_MENUITEM)
660         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Trash", "Message/Trash", GTK_UI_MANAGER_MENUITEM)
661 #ifndef GENERIC_UMPC
662         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Delete", "Message/Delete", GTK_UI_MANAGER_MENUITEM)
663 #endif
664         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator3", "Message/---", GTK_UI_MANAGER_SEPARATOR)
665         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Mark", "SummaryViewPopup/Mark", GTK_UI_MANAGER_MENU)
666         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "ColorLabel", "SummaryViewPopup/ColorLabel", GTK_UI_MANAGER_MENU)
667         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Tags", "SummaryViewPopup/Tags", GTK_UI_MANAGER_MENU)
668
669         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator4", "Message/---", GTK_UI_MANAGER_SEPARATOR)
670 #ifndef GENERIC_UMPC
671         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "AddSenderToAB", "Tools/AddSenderToAB", GTK_UI_MANAGER_MENUITEM)
672 #endif
673         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "CreateFilterRule", "SummaryViewPopup/CreateFilterRule", GTK_UI_MANAGER_MENU)
674 #ifndef GENERIC_UMPC
675         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "CreateProcessingRule", "SummaryViewPopup/CreateProcessingRule", GTK_UI_MANAGER_MENU)
676 #endif
677         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator5", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
678         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "View", "SummaryViewPopup/View", GTK_UI_MANAGER_MENU)
679         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "SaveAs", "File/SaveAs", GTK_UI_MANAGER_MENUITEM)
680 #ifndef GENERIC_UMPC
681         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Print", "File/Print", GTK_UI_MANAGER_MENUITEM)
682 #endif
683         /* last separator, for plugins */
684         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "Separator6", "File/---", GTK_UI_MANAGER_SEPARATOR)
685
686         /* submenus - replyto */
687         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "All", "Message/ReplyTo/All", GTK_UI_MANAGER_MENUITEM)
688         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "Sender", "Message/ReplyTo/Sender", GTK_UI_MANAGER_MENUITEM)
689         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/ReplyTo", "MailingList", "Message/ReplyTo/List", GTK_UI_MANAGER_MENUITEM)
690
691         /* submenus - mark */
692         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Mark", "Message/Mark/Mark", GTK_UI_MANAGER_MENUITEM)
693         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Unmark", "Message/Mark/Unmark", GTK_UI_MANAGER_MENUITEM)
694         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator1", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
695         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkUnread", "Message/Mark/MarkUnread", GTK_UI_MANAGER_MENUITEM)
696         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkRead", "Message/Mark/MarkRead", GTK_UI_MANAGER_MENUITEM)
697         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkAllRead", "Message/Mark/MarkAllRead", GTK_UI_MANAGER_MENUITEM)
698         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "IgnoreThread", "Message/Mark/IgnoreThread", GTK_UI_MANAGER_MENUITEM)
699         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "UnignoreThread", "Message/Mark/UnignoreThread", GTK_UI_MANAGER_MENUITEM)
700         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "WatchThread", "Message/Mark/WatchThread", GTK_UI_MANAGER_MENUITEM)
701         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "UnwatchThread", "Message/Mark/UnwatchThread", GTK_UI_MANAGER_MENUITEM)
702         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator2", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
703         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkSpam", "Message/Mark/MarkSpam", GTK_UI_MANAGER_MENUITEM)
704         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "MarkHam", "Message/Mark/MarkHam", GTK_UI_MANAGER_MENUITEM)
705         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Separator3", "Message/Mark/---", GTK_UI_MANAGER_SEPARATOR)
706         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Lock", "Message/Mark/Lock", GTK_UI_MANAGER_MENUITEM)
707         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/Mark", "Unlock", "Message/Mark/Unlock", GTK_UI_MANAGER_MENUITEM)
708
709         /* submenus - colorlabel and tags are dynamic */
710         /* submenus - createfilterrule */
711         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "Automatically", "Tools/CreateFilterRule/Automatically", GTK_UI_MANAGER_MENUITEM)
712         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "ByFrom", "Tools/CreateFilterRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
713         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "ByTo", "Tools/CreateFilterRule/ByTo", GTK_UI_MANAGER_MENUITEM)
714         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateFilterRule", "BySubject", "Tools/CreateFilterRule/BySubject", GTK_UI_MANAGER_MENUITEM)
715                 
716 #ifndef GENERIC_UMPC
717         /* submenus - createprocessingrule */
718         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "Automatically", "Tools/CreateProcessingRule/Automatically", GTK_UI_MANAGER_MENUITEM)
719         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "ByFrom", "Tools/CreateProcessingRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
720         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "ByTo", "Tools/CreateProcessingRule/ByTo", GTK_UI_MANAGER_MENUITEM)
721         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/CreateProcessingRule", "BySubject", "Tools/CreateProcessingRule/BySubject", GTK_UI_MANAGER_MENUITEM)
722 #endif
723                 
724         /* submenus - view */
725         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "OpenNewWindow", "View/OpenNewWindow", GTK_UI_MANAGER_MENUITEM)
726         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "MessageSource", "View/MessageSource", GTK_UI_MANAGER_MENUITEM)
727 #ifndef GENERIC_UMPC
728         MENUITEM_ADDUI_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup/View", "AllHeaders", "View/AllHeaders", GTK_UI_MANAGER_MENUITEM)
729 #endif          
730         summaryview->popupmenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
731                                 gtk_ui_manager_get_widget(mainwin->ui_manager, "/Menus/SummaryViewPopup")) );
732
733
734         summaryview->vbox = vbox;
735         summaryview->scrolledwin = scrolledwin;
736         summaryview->ctree = ctree;
737         summaryview->hbox = hbox;
738         summaryview->hbox_l = hbox_l;
739         summaryview->hbox_spc = hbox_spc;
740         summaryview->stat_box = stat_box;
741         summaryview->stat_box2 = stat_box2;
742         summaryview->statlabel_folder = statlabel_folder;
743         summaryview->statlabel_select = statlabel_select;
744         summaryview->statlabel_msgs = statlabel_msgs;
745         summaryview->toggle_eventbox = toggle_eventbox;
746         summaryview->toggle_arrow = toggle_arrow;
747 #ifdef GENERIC_UMPC
748         summaryview->multiple_sel_togbtn = multiple_sel_togbtn;
749 #endif
750         summaryview->toggle_search = toggle_search;
751         summaryview->lock_count = 0;
752         summaryview->msginfo_update_callback_id =
753                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, summary_update_msg, (gpointer) summaryview);
754         summaryview->folder_item_update_callback_id =
755                 hooks_register_hook(FOLDER_ITEM_UPDATE_HOOKLIST,
756                                 summary_update_folder_item_hook,
757                                 (gpointer) summaryview);
758         summaryview->folder_update_callback_id =
759                 hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
760                                 summary_update_folder_hook,
761                                 (gpointer) summaryview);
762
763         summaryview->target_list = gtk_target_list_new(summary_drag_types, 3);
764
765         summaryview->quicksearch = quicksearch;
766
767         /* CLAWS: need this to get the SummaryView * from
768          * the CList */
769         g_object_set_data(G_OBJECT(ctree), "summaryview", (gpointer)summaryview); 
770
771         gtk_widget_show_all(vbox);
772
773         gtk_widget_show(vbox);
774
775         if (prefs_common.show_searchbar)
776                 quicksearch_show(quicksearch);
777         else
778                 quicksearch_hide(quicksearch);
779         
780         if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
781             prefs_common.layout_mode == SMALL_LAYOUT)
782                 gtk_widget_hide(summaryview->toggle_eventbox);
783
784         return summaryview;
785 }
786
787 void summary_relayout(SummaryView *summaryview)
788 {
789         gtk_widget_realize(summaryview->stat_box);
790
791         g_object_ref(summaryview->hbox_l);
792         g_object_ref(summaryview->statlabel_msgs);
793         
794         gtkut_container_remove(GTK_CONTAINER(
795                 gtk_widget_get_parent(summaryview->hbox_l)), summaryview->hbox_l);
796         gtkut_container_remove(GTK_CONTAINER(
797                 gtk_widget_get_parent(summaryview->statlabel_msgs)), summaryview->statlabel_msgs);
798
799         switch (prefs_common.layout_mode) {
800         case NORMAL_LAYOUT:
801         case WIDE_LAYOUT:
802         case WIDE_MSGLIST_LAYOUT:
803                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
804                 gtk_box_pack_end(GTK_BOX(summaryview->stat_box), summaryview->statlabel_msgs, FALSE, FALSE, 4);
805                 gtk_widget_show_all(summaryview->stat_box);
806                 gtk_widget_show_all(summaryview->stat_box2);
807                 if (prefs_common.layout_mode == WIDE_MSGLIST_LAYOUT ||
808                     prefs_common.layout_mode == SMALL_LAYOUT)
809                         gtk_widget_hide(summaryview->toggle_eventbox);
810                 else
811                         gtk_widget_show(summaryview->toggle_eventbox);
812                 break;
813         case VERTICAL_LAYOUT:
814         case SMALL_LAYOUT:
815                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box), summaryview->hbox_l, TRUE, TRUE, 0);
816                 gtk_box_pack_start(GTK_BOX(summaryview->stat_box2), summaryview->statlabel_msgs, FALSE, FALSE, 4);
817                 gtk_widget_show_all(summaryview->stat_box);
818                 gtk_widget_show_all(summaryview->stat_box2);
819                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
820                         gtk_widget_hide(summaryview->toggle_eventbox);
821                         gtk_widget_hide(summaryview->statlabel_msgs);
822                 } else {
823                         gtk_widget_show(summaryview->toggle_eventbox);
824                         gtk_widget_show(summaryview->statlabel_msgs);
825                 }
826                         
827                 break;
828         }
829         summary_set_column_order(summaryview);
830
831         g_object_unref(summaryview->hbox_l);
832         g_object_unref(summaryview->statlabel_msgs);
833         quicksearch_relayout(summaryview->quicksearch);
834         if (prefs_common.show_searchbar)
835                 quicksearch_show(summaryview->quicksearch);
836         else
837                 quicksearch_hide(summaryview->quicksearch);
838 }
839
840 static void summary_set_fonts(SummaryView *summaryview)
841 {
842         PangoFontDescription *font_desc;
843         gint size;
844
845         font_desc = pango_font_description_from_string(NORMAL_FONT);
846         if (font_desc) {
847                 gtk_widget_modify_font(summaryview->ctree, font_desc);
848                 pango_font_description_free(font_desc);
849         }
850
851         if (!bold_style) {
852                 bold_style = gtk_style_copy
853                         (gtk_widget_get_style(summaryview->ctree));
854
855                 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
856                         font_desc = pango_font_description_from_string(NORMAL_FONT);
857                         if (font_desc) {
858                                 pango_font_description_free(bold_style->font_desc);
859                                 bold_style->font_desc = font_desc;
860                         }
861                         pango_font_description_set_weight
862                                         (bold_style->font_desc, PANGO_WEIGHT_BOLD);
863                 } else {
864                         font_desc = pango_font_description_from_string(BOLD_FONT);
865                         if (font_desc) {
866                                 pango_font_description_free(bold_style->font_desc);
867                                 bold_style->font_desc = font_desc;
868                         }
869                 }
870                 bold_marked_style = gtk_style_copy(bold_style);
871                 bold_marked_style->fg[GTK_STATE_NORMAL] =
872                         summaryview->color_marked;
873                 bold_deleted_style = gtk_style_copy(bold_style);
874                 bold_deleted_style->fg[GTK_STATE_NORMAL] =
875                         summaryview->color_dim;
876         }
877
878         if (prefs_common.derive_from_normal_font || !SMALL_FONT) {
879                 font_desc = pango_font_description_new();
880                 size = pango_font_description_get_size
881                         (gtk_widget_get_style(summaryview->ctree)->font_desc);
882                 pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
883         } else {
884                 font_desc = pango_font_description_from_string(SMALL_FONT);
885         }
886         if (font_desc) {
887                 gtk_widget_modify_font(summaryview->statlabel_folder, font_desc);
888                 gtk_widget_modify_font(summaryview->statlabel_select, font_desc);
889                 gtk_widget_modify_font(summaryview->statlabel_msgs, font_desc);
890                 pango_font_description_free(font_desc);
891         }
892
893 }
894
895 static void summary_set_folder_pixmap(SummaryView *summaryview, StockPixmap icon)
896 {
897         GtkWidget *pixmap; 
898         if (!summaryview->folder_pixmap_eventbox) {
899                 summaryview->folder_pixmap_eventbox = gtk_event_box_new();
900                 gtk_widget_show(summaryview->folder_pixmap_eventbox);
901                 gtk_box_pack_start(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, FALSE, FALSE, 4);
902                 gtk_box_reorder_child(GTK_BOX(summaryview->hbox_l), summaryview->folder_pixmap_eventbox, 0); /* search_toggle before */
903                 g_signal_connect(G_OBJECT(summaryview->folder_pixmap_eventbox), "button_press_event",
904                          G_CALLBACK(summary_folder_eventbox_pressed),
905                          summaryview);
906         }
907         if (summaryview->folder_pixmap)
908                 gtk_widget_destroy(summaryview->folder_pixmap);
909
910         pixmap = stock_pixmap_widget(summaryview->hbox_l, icon);
911         gtk_container_add(GTK_CONTAINER(summaryview->folder_pixmap_eventbox), pixmap);
912         gtk_widget_show(pixmap);
913         summaryview->folder_pixmap = pixmap; 
914 }
915
916 void summary_init(SummaryView *summaryview)
917 {
918         GtkWidget *pixmap;
919
920         gtk_widget_realize(summaryview->ctree);
921         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
922                          &markxpm);
923         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
924                          &deletedxpm);
925         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
926                          &newxpm);
927         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
928                          &unreadxpm);
929         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
930                          &repliedxpm);
931         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
932                          &forwardedxpm);
933         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
934                          &clipxpm);
935         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
936                          &lockedxpm);
937         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
938                          &ignorethreadxpm);
939         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_WATCHTHREAD,
940                          &watchthreadxpm);
941         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
942                          &clipkeyxpm);
943         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_KEY_SIGN,
944                          &keysignxpm);
945         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
946                          &keyxpm);
947         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_GPG_SIGNED,
948                          &gpgsignedxpm);
949         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_GPG_SIGNED,
950                          &clipgpgsignedxpm);
951         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_SPAM,
952                          &spamxpm);
953         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_MOVED,
954                          &movedxpm);
955         stock_pixbuf_gdk(summaryview->ctree, STOCK_PIXMAP_COPIED,
956                          &copiedxpm);
957
958         summary_set_fonts(summaryview);
959
960         summary_set_folder_pixmap(summaryview, STOCK_PIXMAP_DIR_OPEN);
961
962         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_QUICKSEARCH);
963         gtk_container_add (GTK_CONTAINER(summaryview->toggle_search), pixmap);
964         gtk_widget_show(pixmap);
965         summaryview->quick_search_pixmap = pixmap;
966         
967 #ifdef GENERIC_UMPC
968         pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_SELECTION);
969         gtk_container_add(GTK_CONTAINER(summaryview->multiple_sel_togbtn), pixmap);
970         gtk_widget_show(pixmap);
971         summaryview->multiple_sel_image = pixmap;
972 #endif
973
974         /* Init summaryview prefs */
975         summaryview->sort_key = SORT_BY_NONE;
976         summaryview->sort_type = SORT_ASCENDING;
977
978         /* Init summaryview extra data */
979 #ifndef G_OS_WIN32
980         summaryview->simplify_subject_preg = NULL;
981 #endif
982         summary_clear_list(summaryview);
983         summary_set_column_titles(summaryview);
984         summary_colorlabel_menu_create(summaryview, FALSE);
985         summary_tags_menu_create(summaryview, FALSE);
986         main_create_mailing_list_menu (summaryview->mainwin, NULL);     
987         summary_set_menu_sensitive(summaryview);
988
989 }
990
991 #define CURRENTLY_DISPLAYED(m) \
992 ( (m->msgnum == displayed_msgnum) \
993   && (!g_ascii_strcasecmp(m->folder->name,item->name)) )
994
995 #define FOLDER_SHOWS_TO_HDR(i) \
996 ( i && (folder_has_parent_of_type(i, F_OUTBOX) \
997         || folder_has_parent_of_type(i, F_DRAFT) \
998         || folder_has_parent_of_type(i, F_QUEUE)) )
999   
1000 static void summary_switch_from_to(SummaryView *summaryview, FolderItem *item)
1001 {
1002         gboolean show_from = FALSE, show_to = FALSE;
1003         gboolean showing_from = FALSE, showing_to = FALSE;
1004         gint from_pos = 0, to_pos = 0;
1005         SummaryColumnState *col_state = summaryview->col_state;
1006         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1007         
1008         if (!item || (prefs_common.layout_mode == VERTICAL_LAYOUT && prefs_common.two_line_vert) )
1009                 return;
1010         if (FOLDER_SHOWS_TO_HDR(item))
1011                 show_to = TRUE;
1012         else
1013                 show_from = TRUE;
1014         
1015         from_pos = summaryview->col_pos[S_COL_FROM];
1016         to_pos = summaryview->col_pos[S_COL_TO];
1017         showing_from = col_state[from_pos].visible;
1018         showing_to = col_state[to_pos].visible;
1019         
1020         if (showing_from && showing_to) {
1021                 debug_print("showing both\n");
1022                 return;
1023         }
1024
1025         if (!showing_from && !showing_to) {
1026                 debug_print("showing none\n");
1027                 return;
1028         }
1029
1030         debug_print("showing %s %s, must show %s %s\n", 
1031                 showing_from?"From":"",
1032                 showing_to?"To":"",
1033                 show_from?"From":"",
1034                 show_to?"To":"");
1035
1036         if (showing_from == show_from && showing_to == show_to)
1037                 return;
1038         /* else we'll switch both */
1039
1040         debug_print("switching columns\n");
1041         col_state[from_pos].type = S_COL_TO;
1042         col_state[from_pos].visible = show_to;
1043
1044         col_state[to_pos].type = S_COL_FROM;
1045         col_state[to_pos].visible = show_from;
1046
1047         summaryview->col_pos[S_COL_TO] = from_pos;
1048         summaryview->col_pos[S_COL_FROM] = to_pos;
1049
1050         gtk_cmclist_set_column_visibility
1051                 (GTK_CMCLIST(ctree), from_pos, col_state[from_pos].visible);
1052         gtk_cmclist_set_column_visibility
1053                 (GTK_CMCLIST(ctree), to_pos, col_state[to_pos].visible);
1054
1055         summary_set_column_titles(summaryview);
1056 }
1057
1058 static gboolean summaryview_quicksearch_recurse(gpointer data)
1059 {
1060         SummaryView *summaryview = (SummaryView *)data;
1061         main_window_cursor_wait(summaryview->mainwin);
1062         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
1063         quicksearch_search_subfolders(summaryview->quicksearch, 
1064                               summaryview->folderview,
1065                               summaryview->folder_item);
1066         main_window_cursor_normal(summaryview->mainwin);
1067         return FALSE;
1068 }
1069
1070 static gboolean summary_check_consistency(FolderItem *item, GSList *mlist)
1071 {
1072         int u = 0, n = 0, m = 0, t = 0, r = 0, f = 0, l = 0, i = 0, w = 0;
1073         GSList *cur;
1074         START_TIMING("");
1075         for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1076                 MsgInfo * msginfo = (MsgInfo *) cur->data;
1077                 t++;
1078                 if (MSG_IS_NEW(msginfo->flags))
1079                         n++;
1080                 if (MSG_IS_UNREAD(msginfo->flags))
1081                         u++;
1082                 if (MSG_IS_MARKED(msginfo->flags))
1083                         m++;            
1084                 if (MSG_IS_REPLIED(msginfo->flags))
1085                         r++;
1086                 if (MSG_IS_FORWARDED(msginfo->flags))
1087                         f++;
1088                 if (MSG_IS_LOCKED(msginfo->flags))
1089                         l++;
1090                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
1091                         i++;
1092                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
1093                         w++;
1094         }
1095         if (t != item->total_msgs
1096         ||  n != item->new_msgs
1097         ||  u != item->unread_msgs
1098         ||  m != item->marked_msgs
1099         ||  r != item->replied_msgs
1100         ||  f != item->forwarded_msgs
1101         ||  l != item->locked_msgs
1102         ||  i != item->ignored_msgs
1103         ||  w != item->watched_msgs
1104         ||  (m == 0 && item->unreadmarked_msgs != 0)
1105         ||  item->unreadmarked_msgs < 0) {
1106                 debug_print("Inconsistency\n");
1107                 folder_item_scan_full(item, FALSE);
1108                 END_TIMING();
1109                 return FALSE;
1110         } 
1111         END_TIMING();
1112         return TRUE;
1113 }
1114
1115 gboolean summary_show(SummaryView *summaryview, FolderItem *item)
1116 {
1117         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
1118         GtkCMCTreeNode *node = NULL;
1119         GSList *mlist = NULL;
1120         gchar *buf;
1121         gboolean is_refresh;
1122         guint selected_msgnum = 0;
1123         guint displayed_msgnum = 0;
1124         GSList *cur;
1125         GSList *not_killed;
1126         gboolean hidden_removed = FALSE;
1127
1128         if (summary_is_locked(summaryview)) return FALSE;
1129
1130         if (!summaryview->mainwin)
1131                 return FALSE;
1132         START_TIMING("");
1133         summaryview->last_displayed = NULL;
1134         summary_switch_from_to(summaryview, item);
1135
1136         inc_lock();
1137         summary_lock(summaryview);
1138         
1139         menu_set_sensitive_all(GTK_MENU_SHELL(summaryview->popupmenu), TRUE);
1140         
1141         utils_free_regex();
1142
1143         is_refresh = (item == summaryview->folder_item) ? TRUE : FALSE;
1144
1145         if (item && item->folder->klass->item_opened) {
1146                 item->folder->klass->item_opened(item);
1147         }
1148
1149         if (!is_refresh) {
1150                 main_create_mailing_list_menu (summaryview->mainwin, NULL);
1151                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
1152                         if (item) {
1153                                 mainwindow_enter_folder(summaryview->mainwin);
1154                                 gtk_widget_grab_focus(summaryview->ctree);
1155                         }
1156                 }
1157         }
1158         if (!prefs_common.summary_quicksearch_sticky
1159          && (!prefs_common.summary_quicksearch_recurse
1160           || !quicksearch_is_active(summaryview->quicksearch)
1161           || (item && !quicksearch_is_in_subfolder(summaryview->quicksearch, item)))
1162          && !quicksearch_is_running(summaryview->quicksearch)
1163          && !is_refresh) {
1164                 quicksearch_set(summaryview->quicksearch, prefs_common.summary_quicksearch_type, "");
1165         }
1166
1167         /* STATUSBAR_POP(summaryview->mainwin); */
1168
1169         if (is_refresh) {
1170                 selected_msgnum = summary_get_msgnum(summaryview,
1171                                                      summaryview->selected);
1172                 displayed_msgnum = summary_get_msgnum(summaryview,
1173                                                       summaryview->displayed);
1174         }
1175
1176         /* process the marks if any */
1177         if (summaryview->mainwin->lock_count == 0 &&
1178             (summaryview->moved > 0 || summaryview->copied > 0)) {
1179                 AlertValue val;
1180                 gboolean changed = FALSE;
1181
1182                 val = alertpanel(_("Process mark"),
1183                                  _("Some marks are left. Process them?"),
1184                                  GTK_STOCK_NO, GTK_STOCK_YES, GTK_STOCK_CANCEL);
1185                 if (G_ALERTALTERNATE == val) {
1186                         summary_unlock(summaryview);
1187                         summary_execute(summaryview);
1188                         summary_lock(summaryview);
1189                         changed = TRUE;
1190                 } else if (G_ALERTDEFAULT == val) {
1191                         /* DO NOTHING */
1192                 } else {
1193                         summary_unlock(summaryview);
1194                         inc_unlock();
1195                         END_TIMING();
1196                         return FALSE;
1197                 }
1198                 if (changed || !quicksearch_is_active(summaryview->quicksearch))
1199                         folder_update_op_count();
1200         }
1201         
1202         summary_freeze(summaryview);
1203
1204         summary_clear_list(summaryview);
1205
1206         buf = NULL;
1207         if (!item || !item->path || !folder_item_parent(item) || item->no_select) {
1208                 g_free(buf);
1209                 debug_print("empty folder (%p %s %p %d)\n\n",
1210                                 item, 
1211                                 (item && item->path)?item->path:"(null)",
1212                                 item?folder_item_parent(item):0x0,
1213                                 item?item->no_select:FALSE);
1214                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1215                 summary_set_hide_del_msgs_menu(summaryview, FALSE);
1216                 summary_clear_all(summaryview);
1217                 summaryview->folder_item = item;
1218                 summary_thaw(summaryview);
1219                 summary_unlock(summaryview);
1220                 inc_unlock();
1221                 if (item && quicksearch_is_running(summaryview->quicksearch)) {
1222                         main_window_cursor_wait(summaryview->mainwin);
1223                         quicksearch_reset_cur_folder_item(summaryview->quicksearch);
1224                         if (quicksearch_is_active(summaryview->quicksearch))
1225                                 quicksearch_search_subfolders(summaryview->quicksearch, 
1226                                               summaryview->folderview,
1227                                               summaryview->folder_item);
1228                         main_window_cursor_normal(summaryview->mainwin);
1229                 }       
1230                 END_TIMING();           
1231                 return TRUE;
1232         }
1233         g_free(buf);
1234
1235         if (!is_refresh)
1236                 messageview_clear(summaryview->messageview);
1237
1238         summaryview->folder_item = item;
1239         item->opened = TRUE;
1240
1241         buf = g_strdup_printf(_("Scanning folder (%s)..."), item->path);
1242         debug_print("%s\n", buf);
1243         STATUSBAR_PUSH(summaryview->mainwin, buf);
1244         g_free(buf);
1245
1246         main_window_cursor_wait(summaryview->mainwin);
1247
1248         mlist = folder_item_get_msg_list(item);
1249
1250         if (!summary_check_consistency(item, mlist)) {
1251                 debug_print("reloading due to inconsistency\n");
1252                 procmsg_msg_list_free(mlist);
1253                 mlist = folder_item_get_msg_list(item);
1254         }
1255
1256         if ((summaryview->folder_item->hide_read_msgs || summaryview->folder_item->hide_del_msgs) &&
1257             quicksearch_is_active(summaryview->quicksearch) == FALSE) {
1258                 GSList *not_killed;
1259                 
1260                 summary_set_hide_read_msgs_menu(summaryview, summaryview->folder_item->hide_read_msgs);
1261                 summary_set_hide_del_msgs_menu(summaryview, summaryview->folder_item->hide_del_msgs);
1262                 not_killed = NULL;
1263                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1264                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1265                         
1266                         if (!msginfo->hidden) {
1267                                 if (MSG_IS_DELETED(msginfo->flags) && summaryview->folder_item->hide_del_msgs) {
1268                                         procmsg_msginfo_free(msginfo);
1269                                         continue;
1270                                 }
1271                                 if (summaryview->folder_item->hide_read_msgs) {
1272                                         if (MSG_IS_UNREAD(msginfo->flags) &&
1273                                             !MSG_IS_IGNORE_THREAD(msginfo->flags))
1274                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1275                                         else if (MSG_IS_MARKED(msginfo->flags) ||
1276                                                  MSG_IS_LOCKED(msginfo->flags))
1277                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1278                                         else if (is_refresh &&
1279                                                 (msginfo->msgnum == selected_msgnum ||
1280                                                  msginfo->msgnum == displayed_msgnum))
1281                                                 not_killed = g_slist_prepend(not_killed, msginfo);
1282                                         else
1283                                                 procmsg_msginfo_free(msginfo);
1284                                 } else {
1285                                         not_killed = g_slist_prepend(not_killed, msginfo);
1286                                 }
1287                          } else
1288                                 procmsg_msginfo_free(msginfo);
1289                 }
1290                 hidden_removed = TRUE;
1291                 g_slist_free(mlist);
1292                 mlist = not_killed;
1293         } else {
1294                 summary_set_hide_read_msgs_menu(summaryview, FALSE);
1295                 summary_set_hide_del_msgs_menu(summaryview, FALSE);
1296         }
1297
1298         if (quicksearch_is_active(summaryview->quicksearch)) {
1299                 GSList *not_killed;
1300                 gint interval = quicksearch_is_fast(summaryview->quicksearch) ? 5000:100;
1301                 START_TIMING("quicksearch");
1302                 gint num = 0, total = summaryview->folder_item->total_msgs;
1303                 statusbar_print_all(_("Searching in %s... \n"), 
1304                         summaryview->folder_item->path ? 
1305                         summaryview->folder_item->path : "(null)");
1306                 not_killed = NULL;
1307                 folder_item_update_freeze();
1308                 for (cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1309                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1310
1311                         statusbar_progress_all(num++,total, interval);
1312
1313                         if (!msginfo->hidden && quicksearch_match(summaryview->quicksearch, msginfo))
1314                                 not_killed = g_slist_prepend(not_killed, msginfo);
1315                         else
1316                                 procmsg_msginfo_free(msginfo);
1317                         if (num % interval == 0)
1318                                 GTK_EVENTS_FLUSH();
1319                         if (!quicksearch_is_active(summaryview->quicksearch)) {
1320                                 break;
1321                         }
1322                 }
1323                 folder_item_update_thaw();
1324                 statusbar_progress_all(0,0,0);
1325                 statusbar_pop_all();
1326                 
1327                 hidden_removed = TRUE;
1328                 if (!quicksearch_is_active(summaryview->quicksearch)) {
1329                         debug_print("search cancelled!\n");
1330                         summary_thaw(summaryview);
1331                         STATUSBAR_POP(summaryview->mainwin);
1332                         main_window_cursor_normal(summaryview->mainwin);
1333                         summary_unlock(summaryview);
1334                         inc_unlock();
1335                         summary_show(summaryview, summaryview->folder_item);
1336                         END_TIMING();
1337                         return FALSE;
1338                 }
1339                 g_slist_free(mlist);
1340                 mlist = not_killed;
1341                 END_TIMING();
1342         }
1343
1344         if (!hidden_removed) {
1345                 START_TIMING("removing hidden");
1346                 not_killed = NULL;
1347                 for(cur = mlist ; cur != NULL && cur->data != NULL ; cur = g_slist_next(cur)) {
1348                         MsgInfo * msginfo = (MsgInfo *) cur->data;
1349
1350                         if (!msginfo->hidden)
1351                                 not_killed = g_slist_prepend(not_killed, msginfo);
1352                         else
1353                                 procmsg_msginfo_free(msginfo);
1354                 }
1355                 g_slist_free(mlist);
1356                 mlist = not_killed;
1357                 END_TIMING();
1358         }
1359
1360         STATUSBAR_POP(summaryview->mainwin);
1361
1362         /* set ctree and hash table from the msginfo list, and
1363            create the thread */
1364         summary_set_ctree_from_list(summaryview, mlist);
1365
1366         g_slist_free(mlist);
1367
1368         if (quicksearch_is_active(summaryview->quicksearch) &&
1369             quicksearch_is_running(summaryview->quicksearch)) {
1370                 /* only scan subfolders when quicksearch changed,
1371                  * not when search is the same and folder changed */
1372                 g_timeout_add(100, summaryview_quicksearch_recurse, summaryview);
1373         }
1374
1375         if (is_refresh) {
1376                 if (!quicksearch_is_in_typing(summaryview->quicksearch)) {
1377                         summaryview->last_displayed = summaryview->displayed;
1378                         summaryview->displayed =
1379                                 summary_find_msg_by_msgnum(summaryview,
1380                                                            displayed_msgnum);
1381                         if (!summaryview->displayed)
1382                                 messageview_clear(summaryview->messageview);
1383                         summary_unlock(summaryview);
1384                         summary_select_by_msgnum(summaryview, selected_msgnum);
1385                         summary_lock(summaryview);
1386                         if (!summaryview->selected) {
1387                                 /* no selected message - select first unread
1388                                    message, but do not display it */
1389                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1390                                                                      MSG_UNREAD, FALSE);
1391                                 if (node == NULL && GTK_CMCLIST(ctree)->row_list != NULL)
1392                                         node = gtk_cmctree_node_nth
1393                                                 (ctree,
1394                                                  item->sort_type == SORT_DESCENDING
1395                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1396                                 summary_unlock(summaryview);
1397                                 summary_select_node(summaryview, node, FALSE, TRUE);
1398                                 summary_lock(summaryview);
1399                         }
1400                 } else {
1401                         /* just select first/last */
1402                         if (GTK_CMCLIST(ctree)->row_list != NULL)
1403                                 node = gtk_cmctree_node_nth
1404                                         (ctree,
1405                                          item->sort_type == SORT_DESCENDING
1406                                          ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1407                         gtk_sctree_select(GTK_SCTREE(ctree), node);
1408                         summaryview->selected = node;
1409                         gtk_cmctree_node_moveto(ctree, node, 0, 0.5, 0);
1410                 }
1411         } else {
1412                 /* backward compat */
1413                 int i = 0;
1414                 gboolean set = FALSE, stop = FALSE;
1415                 for (i = 0; i < 6; i++) {
1416                         EntryAction act = prefs_common.summary_select_prio[i];
1417
1418                         if (act != ACTION_UNSET) {
1419                                 set = TRUE;
1420                                 break;
1421                         }
1422                 }
1423                 if (!set)
1424                         prefs_summary_open_set_defaults();
1425
1426                 for (i = 0; i < 6 && node == NULL; i++) {
1427                         EntryAction act = prefs_common.summary_select_prio[i];
1428                         
1429                         switch(act) {
1430                         case ACTION_MARKED:
1431                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1432                                              MSG_MARKED, FALSE);
1433                                 break;
1434                         case ACTION_NEW:
1435                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1436                                              MSG_NEW, FALSE);
1437                                 break;
1438                         case ACTION_UNREAD:
1439                                 node = summary_find_next_flagged_msg(summaryview, NULL,
1440                                              MSG_UNREAD, FALSE);
1441                                 break;
1442                         case ACTION_LAST_OPENED:
1443                                 if (summaryview->folder_item) {
1444                                         node = summary_find_msg_by_msgnum(summaryview, 
1445                                                         summaryview->folder_item->last_seen);
1446                                 }
1447                                 break;
1448                         case ACTION_LAST_LIST:
1449                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1450                                         node = gtk_cmctree_node_nth
1451                                                 (ctree,
1452                                                  item->sort_type == SORT_DESCENDING
1453                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1454                                 }
1455                                 break;
1456                         case ACTION_FIRST_LIST:
1457                                 if (GTK_CMCLIST(ctree)->row_list != NULL) {
1458                                         node = gtk_cmctree_node_nth
1459                                                 (ctree,
1460                                                  item->sort_type == SORT_ASCENDING
1461                                                  ? 0 : GTK_CMCLIST(ctree)->rows - 1);
1462                                 }
1463                                 break;
1464                         case ACTION_NOTHING:
1465                         case ACTION_UNSET:
1466                                 node = NULL;
1467                                 stop = TRUE;
1468                                 break;
1469                         }
1470                         
1471                         if (stop || node)
1472                                 break;
1473                 }
1474
1475                 summary_unlock(summaryview);
1476                 if (node) {
1477                         gboolean show = (prefs_common.always_show_msg == OPENMSG_ALWAYS) ||
1478                                 (prefs_common.always_show_msg == OPENMSG_WHEN_VIEW_VISIBLE &&
1479                                                 messageview_is_visible(summaryview->messageview));
1480                         summary_select_node(summaryview, node, show, TRUE);
1481                 }
1482                 summary_lock(summaryview);
1483         }
1484
1485         summary_status_show(summaryview);
1486         summary_set_menu_sensitive(summaryview);
1487         toolbar_main_set_sensitive(summaryview->mainwin);
1488         
1489         summary_thaw(summaryview);
1490         debug_print("\n");
1491         STATUSBAR_PUSH(summaryview->mainwin, _("Done."));
1492         STATUSBAR_POP(summaryview->mainwin);
1493         main_window_cursor_normal(summaryview->mainwin);
1494         summary_unlock(summaryview);
1495         inc_unlock();
1496         END_TIMING();
1497         return TRUE;
1498 }
1499
1500 #undef CURRENTLY_DISPLAYED
1501
1502
1503 void summary_clear_list(SummaryView *summaryview)
1504 {
1505         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1506         gint optimal_width;
1507
1508         summary_freeze(summaryview);
1509
1510         gtk_cmctree_pre_recursive(GTK_CMCTREE(summaryview->ctree),
1511                                 NULL, summary_free_msginfo_func, NULL);
1512
1513         if (summaryview->folder_item) {
1514                 summaryview->folder_item->opened = FALSE;
1515                 summaryview->folder_item = NULL;
1516         }
1517
1518         summaryview->display_msg = FALSE;
1519
1520         summaryview->selected = NULL;
1521         summaryview->displayed = NULL;
1522         summaryview->total_size = 0;
1523         summaryview->deleted = summaryview->moved = 0;
1524         summaryview->copied = 0;
1525         if (summaryview->msgid_table) {
1526                 g_hash_table_destroy(summaryview->msgid_table);
1527                 summaryview->msgid_table = NULL;
1528         }
1529         if (summaryview->subject_table) {
1530                 g_hash_table_destroy(summaryview->subject_table);
1531                 summaryview->subject_table = NULL;
1532         }
1533         summaryview->mlist = NULL;
1534
1535         gtk_cmclist_clear(clist);
1536         if (summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
1537                 optimal_width = gtk_cmclist_optimal_column_width
1538                         (clist, summaryview->col_pos[S_COL_SUBJECT]);
1539                 gtk_cmclist_set_column_width
1540                         (clist, summaryview->col_pos[S_COL_SUBJECT],
1541                          optimal_width);
1542         }
1543
1544         summary_thaw(summaryview);
1545 }
1546
1547 void summary_clear_all(SummaryView *summaryview)
1548 {
1549         messageview_clear(summaryview->messageview);
1550         summary_clear_list(summaryview);
1551         summary_set_menu_sensitive(summaryview);
1552         toolbar_main_set_sensitive(summaryview->mainwin);
1553         summary_status_show(summaryview);
1554 }
1555
1556 void summary_lock(SummaryView *summaryview)
1557 {
1558         summaryview->lock_count++;
1559 }
1560
1561 void summary_unlock(SummaryView *summaryview)
1562 {
1563         if (summaryview->lock_count)
1564                 summaryview->lock_count--;
1565 }
1566
1567 gboolean summary_is_locked(SummaryView *summaryview)
1568 {
1569         return summaryview->lock_count > 0;
1570 }
1571
1572 SummarySelection summary_get_selection_type(SummaryView *summaryview)
1573 {
1574         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
1575         SummarySelection selection;
1576
1577         if (!clist->row_list)
1578                 selection = SUMMARY_NONE;
1579         else if (!clist->selection)
1580                 selection = SUMMARY_SELECTED_NONE;
1581         else if (!clist->selection->next)
1582                 selection = SUMMARY_SELECTED_SINGLE;
1583         else
1584                 selection = SUMMARY_SELECTED_MULTIPLE;
1585
1586         return selection;
1587 }
1588
1589 /*!
1590  *\return       MsgInfo * Selected message if there's one selected;
1591  *              if multiple selected, or none, return NULL.
1592  */ 
1593 MsgInfo *summary_get_selected_msg(SummaryView *summaryview)
1594 {
1595         /* summaryview->selected may be valid when multiple 
1596          * messages were selected */
1597         GList *sellist = GTK_CMCLIST(summaryview->ctree)->selection;
1598
1599         if (sellist == NULL || sellist->next) 
1600                 return NULL;
1601         
1602         return GTKUT_CTREE_NODE_GET_ROW_DATA(sellist->data);
1603 }
1604
1605 GSList *summary_get_selected_msg_list(SummaryView *summaryview)
1606 {
1607         GSList *mlist = NULL;
1608         GList *cur;
1609         MsgInfo *msginfo;
1610
1611         for (cur = GTK_CMCLIST(summaryview->ctree)->selection; cur != NULL && cur->data != NULL;
1612              cur = cur->next) {
1613                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(cur->data);
1614                 mlist = g_slist_prepend(mlist, msginfo);
1615         }
1616
1617         mlist = g_slist_reverse(mlist);
1618
1619         return mlist;
1620 }
1621
1622 void summary_set_menu_sensitive(SummaryView *summaryview)
1623 {
1624         SensitiveCond state;
1625         gboolean sensitive;
1626         gint i;
1627
1628         static const struct {
1629                 gchar *const entry;
1630                 SensitiveCond cond;
1631         } entry[] = {
1632                 {"Menus/SummaryViewPopup/Reply"                 , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1633 #ifndef GENERIC_UMPC
1634                 {"Menus/SummaryViewPopup/ReplyTo"                       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1635                 {"Menus/SummaryViewPopup/ReplyTo/All"           , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1636                 {"Menus/SummaryViewPopup/ReplyTo/Sender"             , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1637                 {"Menus/SummaryViewPopup/ReplyTo/MailingList"       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1638 #endif
1639
1640                 {"Menus/SummaryViewPopup/Forward"                       , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1641 #ifndef GENERIC_UMPC
1642                 {"Menus/SummaryViewPopup/ForwardAtt"    , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1643                 {"Menus/SummaryViewPopup/Redirect"                      , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1644 #endif
1645
1646                 {"Menus/SummaryViewPopup/Move"                  , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
1647                 {"Menus/SummaryViewPopup/Copy"                  , M_TARGET_EXIST|M_EXEC},
1648                 {"Menus/SummaryViewPopup/Trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS|M_NOT_TRASH},
1649 #ifndef GENERIC_UMPC
1650                 {"Menus/SummaryViewPopup/Delete"                        , M_TARGET_EXIST|M_ALLOW_DELETE},
1651 #endif
1652
1653                 {"Menus/SummaryViewPopup/Mark"                  , M_TARGET_EXIST},
1654                 {"Menus/SummaryViewPopup/Mark/Mark"             , M_TARGET_EXIST},
1655                 {"Menus/SummaryViewPopup/Mark/Unmark"                   , M_TARGET_EXIST},
1656                 {"Menus/SummaryViewPopup/Mark/MarkUnread"       , M_TARGET_EXIST},
1657                 {"Menus/SummaryViewPopup/Mark/MarkRead"         , M_TARGET_EXIST},
1658                 {"Menus/SummaryViewPopup/Mark/MarkAllRead"      , M_TARGET_EXIST},
1659                 {"Menus/SummaryViewPopup/Mark/IgnoreThread"     , M_TARGET_EXIST},
1660                 {"Menus/SummaryViewPopup/Mark/UnignoreThread"           , M_TARGET_EXIST},
1661                 {"Menus/SummaryViewPopup/Mark/WatchThread"              , M_TARGET_EXIST},
1662                 {"Menus/SummaryViewPopup/Mark/UnwatchThread"    , M_TARGET_EXIST},
1663                 {"Menus/SummaryViewPopup/Mark/Unlock"                   , M_TARGET_EXIST},
1664                 {"Menus/SummaryViewPopup/Mark/Lock"             , M_TARGET_EXIST},
1665                 {"Menus/SummaryViewPopup/Mark/MarkSpam"         , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1666                 {"Menus/SummaryViewPopup/Mark/MarkHam"          , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
1667                 {"Menus/SummaryViewPopup/ColorLabel"                    , M_TARGET_EXIST},
1668                 {"Menus/SummaryViewPopup/Tags"                  , M_TARGET_EXIST},
1669
1670 #ifndef GENERIC_UMPC
1671                 {"Menus/SummaryViewPopup/AddSenderToAB" , M_SINGLE_TARGET_EXIST},
1672 #endif
1673                 {"Menus/SummaryViewPopup/CreateFilterRule"              , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1674 #ifndef GENERIC_UMPC
1675                 {"Menus/SummaryViewPopup/CreateProcessingRule"  , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1676 #endif
1677
1678                 {"Menus/SummaryViewPopup/View"                  , M_SINGLE_TARGET_EXIST},
1679                 {"Menus/SummaryViewPopup/View/OpenNewWindow"     , M_SINGLE_TARGET_EXIST},
1680                 {"Menus/SummaryViewPopup/View/MessageSource"            , M_SINGLE_TARGET_EXIST},
1681 #ifndef GENERIC_UMPC
1682                 {"Menus/SummaryViewPopup/View/AllHeaders"               , M_SINGLE_TARGET_EXIST},
1683 #endif
1684                 {"Menus/SummaryViewPopup/SaveAs"                        , M_TARGET_EXIST},
1685 #ifndef GENERIC_UMPC
1686                 {"Menus/SummaryViewPopup/Print"                 , M_TARGET_EXIST},
1687 #endif
1688                 {NULL, 0}
1689         };
1690
1691         main_window_set_menu_sensitive(summaryview->mainwin);
1692
1693         state = main_window_get_current_state(summaryview->mainwin);
1694
1695         for (i = 0; entry[i].entry != NULL; i++) {
1696                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1697                 cm_menu_set_sensitive_full(summaryview->mainwin->ui_manager, entry[i].entry, sensitive);
1698         }
1699
1700         summary_lock(summaryview);
1701 #ifndef GENERIC_UMPC
1702         if (summaryview->messageview 
1703         &&  summaryview->messageview->mimeview
1704         &&  summaryview->messageview->mimeview->textview)
1705                 cm_toggle_menu_set_active_full(summaryview->mainwin->ui_manager, "Menus/SummaryViewPopup/View/AllHeaders",
1706                         summaryview->messageview->mimeview->textview->show_all_headers);
1707 #endif
1708         summary_unlock(summaryview);
1709 }
1710
1711 void summary_select_prev_unread(SummaryView *summaryview)
1712 {
1713         GtkCMCTreeNode *node;
1714         gboolean skip_cur = FALSE;
1715
1716         if (summaryview->displayed 
1717         &&  summaryview->selected == summaryview->displayed) {
1718                 debug_print("skipping current\n");
1719                 skip_cur = TRUE;
1720         }
1721
1722         node = summary_find_prev_flagged_msg
1723                 (summaryview, summaryview->selected, MSG_UNREAD, skip_cur);
1724
1725         if (!node || node == summaryview->selected) {
1726                 AlertValue val = 0;
1727
1728                 switch (prefs_common.next_unread_msg_dialog) {
1729                         case NEXTUNREADMSGDIALOG_ALWAYS:
1730                                 val = alertpanel(_("No more unread messages"),
1731                                                  _("No unread message found. "
1732                                                    "Search from the end?"),
1733                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1734                                 break;
1735                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1736                                 val = G_ALERTALTERNATE;
1737                                 break;
1738                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1739                                 val = !G_ALERTALTERNATE;
1740                                 break;
1741                         default:
1742                                 debug_print(
1743                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1744                 }
1745                 if (val != G_ALERTALTERNATE) return;
1746                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1747                                                      MSG_UNREAD, FALSE);
1748         }
1749
1750         if (!node)
1751                 alertpanel_notice(_("No unread messages."));
1752         else
1753                 summary_select_node(summaryview, node, TRUE, FALSE);
1754 }
1755
1756 void summary_select_next_unread(SummaryView *summaryview)
1757 {
1758         GtkCMCTreeNode *node = summaryview->selected;
1759         gboolean skip_cur = FALSE;
1760         
1761         if (summaryview->displayed 
1762         &&  summaryview->selected == summaryview->displayed) {
1763                 debug_print("skipping cur (%p %p)\n",
1764                         summaryview->displayed, summaryview->selected);
1765                 skip_cur = TRUE;
1766         }
1767
1768
1769         node = summary_find_next_flagged_msg
1770                 (summaryview, node, MSG_UNREAD, skip_cur);
1771         
1772         if (node)
1773                 summary_select_node(summaryview, node, TRUE, FALSE);
1774         else {
1775                 node = summary_find_next_flagged_msg
1776                         (summaryview, NULL, MSG_UNREAD, FALSE);
1777                 if (node == NULL || node == summaryview->selected) {
1778                         AlertValue val = 0;
1779
1780                         switch (prefs_common.next_unread_msg_dialog) {
1781                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1782                                         val = alertpanel(_("No more unread messages"),
1783                                                          _("No unread message found. "
1784                                                            "Go to next folder?"),
1785                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1786                                         break;
1787                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1788                                         val = G_ALERTALTERNATE;
1789                                         break;
1790                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1791                                         val = G_ALERTOTHER;
1792                                         break;
1793                                 default:
1794                                         debug_print(
1795                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1796                         }
1797
1798                         if (val == G_ALERTALTERNATE) {
1799                                 folderview_select_next_unread(summaryview->folderview, TRUE);
1800                                 return;
1801                         } 
1802                         else
1803                                 return;
1804                 } else
1805                         summary_select_node(summaryview, node, TRUE, FALSE);
1806
1807         }
1808 }
1809
1810 void summary_select_prev_new(SummaryView *summaryview)
1811 {
1812         GtkCMCTreeNode *node;
1813         gboolean skip_cur = FALSE;
1814
1815         if (summaryview->displayed 
1816         &&  summaryview->selected == summaryview->displayed) {
1817                 debug_print("skipping current\n");
1818                 skip_cur = TRUE;
1819         }
1820
1821         node = summary_find_prev_flagged_msg
1822                 (summaryview, summaryview->selected, MSG_NEW, skip_cur);
1823
1824         if (!node || node == summaryview->selected) {
1825                 AlertValue val = 0;
1826
1827                 switch (prefs_common.next_unread_msg_dialog) {
1828                         case NEXTUNREADMSGDIALOG_ALWAYS:
1829                                 val = alertpanel(_("No more new messages"),
1830                                                  _("No new message found. "
1831                                                    "Search from the end?"),
1832                                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1833                                 break;
1834                         case NEXTUNREADMSGDIALOG_ASSUME_YES:
1835                                 val = G_ALERTALTERNATE;
1836                                 break;
1837                         case NEXTUNREADMSGDIALOG_ASSUME_NO:
1838                                 val = !G_ALERTALTERNATE;
1839                                 break;
1840                         default:
1841                                 debug_print(
1842                                         _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1843                 }
1844                 if (val != G_ALERTALTERNATE) return;
1845                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1846                                                      MSG_NEW, FALSE);
1847         }
1848
1849         if (!node)
1850                 alertpanel_notice(_("No new messages."));
1851         else
1852                 summary_select_node(summaryview, node, TRUE, FALSE);
1853 }
1854
1855 void summary_select_next_new(SummaryView *summaryview)
1856 {
1857         GtkCMCTreeNode *node = summaryview->selected;
1858         gboolean skip_cur = FALSE;
1859         
1860         if (summaryview->displayed 
1861         &&  summaryview->selected == summaryview->displayed) {
1862                 debug_print("skipping cur (%p %p)\n",
1863                         summaryview->displayed, summaryview->selected);
1864                 skip_cur = TRUE;
1865         }
1866
1867
1868         node = summary_find_next_flagged_msg
1869                 (summaryview, node, MSG_NEW, skip_cur);
1870         
1871         if (node)
1872                 summary_select_node(summaryview, node, TRUE, FALSE);
1873         else {
1874                 node = summary_find_next_flagged_msg
1875                         (summaryview, NULL, MSG_NEW, FALSE);
1876                 if (node == NULL || node == summaryview->selected) {
1877                         AlertValue val = 0;
1878
1879                         switch (prefs_common.next_unread_msg_dialog) {
1880                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1881                                         val = alertpanel(_("No more new messages"),
1882                                                          _("No new message found. "
1883                                                            "Go to next folder?"),
1884                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1885                                         break;
1886                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1887                                         val = G_ALERTALTERNATE;
1888                                         break;
1889                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1890                                         val = G_ALERTOTHER;
1891                                         break;
1892                                 default:
1893                                         debug_print(
1894                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1895                         }
1896
1897                         if (val == G_ALERTALTERNATE) {
1898                                 folderview_select_next_new(summaryview->folderview);
1899                                 return;
1900                         } 
1901                         else
1902                                 return;
1903                 } else
1904                         summary_select_node(summaryview, node, TRUE, FALSE);
1905
1906         }
1907 }
1908
1909 void summary_select_prev_marked(SummaryView *summaryview)
1910 {
1911         GtkCMCTreeNode *node;
1912
1913         node = summary_find_prev_flagged_msg
1914                 (summaryview, summaryview->selected, MSG_MARKED, TRUE);
1915
1916         if (!node) {
1917                 AlertValue val;
1918
1919                 val = alertpanel(_("No more marked messages"),
1920                                  _("No marked message found. "
1921                                    "Search from the end?"),
1922                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1923                 if (val != G_ALERTALTERNATE) return;
1924                 node = summary_find_prev_flagged_msg(summaryview, NULL,
1925                                                      MSG_MARKED, TRUE);
1926         }
1927
1928         if (!node)
1929                 alertpanel_notice(_("No marked messages."));
1930         else
1931                 summary_select_node(summaryview, node, TRUE, FALSE);
1932 }
1933
1934 void summary_select_next_marked(SummaryView *summaryview)
1935 {
1936         GtkCMCTreeNode *node = summaryview->selected;
1937         gboolean skip_cur = FALSE;
1938         
1939         if (summaryview->displayed 
1940         &&  summaryview->selected == summaryview->displayed) {
1941                 debug_print("skipping cur (%p %p)\n",
1942                         summaryview->displayed, summaryview->selected);
1943                 skip_cur = TRUE;
1944         }
1945
1946
1947         node = summary_find_next_flagged_msg
1948                 (summaryview, node, MSG_MARKED, skip_cur);
1949         
1950         if (node)
1951                 summary_select_node(summaryview, node, TRUE, FALSE);
1952         else {
1953                 node = summary_find_next_flagged_msg
1954                         (summaryview, NULL, MSG_MARKED, FALSE);
1955                 if (node == NULL || node == summaryview->selected) {
1956                         AlertValue val = 0;
1957
1958                         switch (prefs_common.next_unread_msg_dialog) {
1959                                 case NEXTUNREADMSGDIALOG_ALWAYS:
1960                                         val = alertpanel(_("No more marked messages"),
1961                                                          _("No marked message found. "
1962                                                            "Go to next folder?"),
1963                                                          GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
1964                                         break;
1965                                 case NEXTUNREADMSGDIALOG_ASSUME_YES:
1966                                         val = G_ALERTALTERNATE;
1967                                         break;
1968                                 case NEXTUNREADMSGDIALOG_ASSUME_NO:
1969                                         val = G_ALERTOTHER;
1970                                         break;
1971                                 default:
1972                                         debug_print(
1973                                                 _("Internal error: unexpected value for prefs_common.next_unread_msg_dialog\n"));
1974                         }
1975
1976                         if (val == G_ALERTALTERNATE) {
1977                                 folderview_select_next_marked(summaryview->folderview);
1978                                 return;
1979                         } 
1980                         else
1981                                 return;
1982                 } else
1983                         summary_select_node(summaryview, node, TRUE, FALSE);
1984
1985         }
1986 }
1987
1988 void summary_select_prev_labeled(SummaryView *summaryview)
1989 {
1990         GtkCMCTreeNode *node;
1991
1992         node = summary_find_prev_flagged_msg
1993                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
1994
1995         if (!node) {
1996                 AlertValue val;
1997
1998                 val = alertpanel(_("No more labeled messages"),
1999                                  _("No labeled message found. "
2000                                    "Search from the end?"),
2001                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2002                 if (val != G_ALERTALTERNATE) return;
2003                 node = summary_find_prev_flagged_msg(summaryview, NULL,
2004                                                      MSG_CLABEL_FLAG_MASK, TRUE);
2005         }
2006
2007         if (!node)
2008                 alertpanel_notice(_("No labeled messages."));
2009         else
2010                 summary_select_node(summaryview, node, TRUE, FALSE);
2011 }
2012
2013 void summary_select_next_labeled(SummaryView *summaryview)
2014 {
2015         GtkCMCTreeNode *node;
2016
2017         node = summary_find_next_flagged_msg
2018                 (summaryview, summaryview->selected, MSG_CLABEL_FLAG_MASK, TRUE);
2019
2020         if (!node) {
2021                 AlertValue val;
2022
2023                 val = alertpanel(_("No more labeled messages"),
2024                                  _("No labeled message found. "
2025                                    "Search from the beginning?"),
2026                                  GTK_STOCK_NO, "+"GTK_STOCK_YES, NULL);
2027                 if (val != G_ALERTALTERNATE) return;
2028                 node = summary_find_next_flagged_msg(summaryview, NULL,
2029                                                      MSG_CLABEL_FLAG_MASK, TRUE);
2030         }
2031
2032         if (!node)
2033                 alertpanel_notice(_("No labeled messages."));
2034         else
2035                 summary_select_node(summaryview, node, TRUE, FALSE);
2036 }
2037
2038 void summary_select_last_read(SummaryView *summaryview)
2039 {
2040         if (summaryview->last_displayed)
2041                 summary_select_node(summaryview, summaryview->last_displayed, TRUE, FALSE);
2042 }
2043
2044 void summary_select_parent(SummaryView *summaryview)
2045 {
2046         GtkCMCTreeNode *node = NULL;
2047
2048         if (summaryview->selected)
2049                 node = GTK_CMCTREE_ROW(summaryview->selected)->parent;
2050         if (node)
2051                 summary_select_node(summaryview, node, TRUE, FALSE);
2052 }
2053
2054 void summary_select_by_msgnum(SummaryView *summaryview, guint msgnum)
2055 {
2056         GtkCMCTreeNode *node;
2057
2058         node = summary_find_msg_by_msgnum(summaryview, msgnum);
2059         summary_select_node(summaryview, node, FALSE, TRUE);
2060 }
2061
2062 void summary_select_by_msg_list(SummaryView     *summaryview, GSList *msginfos)
2063 {
2064         GtkCMCTree *ctree;
2065         GSList *msgnum_list, *walk;
2066         gboolean froze = FALSE;
2067
2068         ctree = GTK_CMCTREE(summaryview->ctree);
2069
2070         msgnum_list = NULL;
2071         for(walk = msginfos; walk; walk = walk->next) {
2072                 MsgInfo *msginfo;
2073                 msginfo = walk->data;
2074                 msgnum_list = g_slist_prepend(msgnum_list, GUINT_TO_POINTER(msginfo->msgnum));
2075         }
2076         START_LONG_OPERATION(summaryview, FALSE);
2077         for(walk = msgnum_list; walk; walk = walk->next) {
2078                 GtkCMCTreeNode *node;
2079                 node = summary_find_msg_by_msgnum(summaryview, GPOINTER_TO_UINT(walk->data));
2080                 gtk_cmctree_select(ctree, node);
2081         }
2082         END_LONG_OPERATION(summaryview);
2083         g_slist_free(msgnum_list);
2084 }
2085
2086 typedef struct _PostponedSelectData
2087 {
2088         GtkCMCTree *ctree;
2089         GtkCMCTreeNode *row;
2090         GtkCMCTreeNode *node;
2091         GtkScrollType type;
2092         gint column;
2093         SummaryView *summaryview;
2094         gboolean display_msg;
2095         gboolean do_refresh;
2096 } PostponedSelectData;
2097
2098 static gboolean summary_select_retry(void *data)
2099 {
2100         PostponedSelectData *psdata = (PostponedSelectData *)data;
2101         debug_print("trying again\n");
2102         if (psdata->row)
2103                 summary_selected(psdata->ctree, psdata->row,
2104                             psdata->column, psdata->summaryview);
2105         else if (psdata->node)
2106                 summary_select_node(psdata->summaryview, psdata->node,
2107                             psdata->display_msg, psdata->do_refresh);
2108         else
2109                 summary_step(psdata->summaryview, psdata->type);
2110         g_free(psdata);
2111         return FALSE;
2112 }
2113
2114 /**
2115  * summary_select_node:
2116  * @summaryview: Summary view.
2117  * @node: Summary tree node.
2118  * @display_msg: TRUE to display the selected message.
2119  * @do_refresh: TRUE to refresh the widget.
2120  *
2121  * Select @node (bringing it into view by scrolling and expanding its
2122  * thread, if necessary) and unselect all others.  If @display_msg is
2123  * TRUE, display the corresponding message in the message view.
2124  * If @do_refresh is TRUE, the widget is refreshed.
2125  **/
2126 void summary_select_node(SummaryView *summaryview, GtkCMCTreeNode *node,
2127                          gboolean display_msg, gboolean do_refresh)
2128 {
2129         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2130         
2131         if (summary_is_locked(summaryview)
2132         && !GTK_SCTREE(ctree)->selecting_range
2133         && summaryview->messageview->mimeview
2134         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
2135         && summaryview->messageview->mimeview->textview->loading) {
2136                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
2137                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
2138                 
2139                 data->ctree = ctree;
2140                 data->row = NULL;
2141                 data->node = node;
2142                 data->summaryview = summaryview;
2143                 data->display_msg = display_msg;
2144                 data->do_refresh = do_refresh;
2145                 debug_print("postponing open of message till end of load\n");
2146                 g_timeout_add(100, summary_select_retry, data);
2147                 return;
2148         }
2149         if (summary_is_locked(summaryview)) {
2150                 return;
2151         }
2152         if (!summaryview->folder_item)
2153                 return;
2154         if (node) {
2155                 gtkut_ctree_expand_parent_all(ctree, node);
2156                 if (do_refresh) {
2157                         summary_lock(summaryview);
2158                         GTK_EVENTS_FLUSH();
2159                         summary_unlock(summaryview);
2160                         gtk_widget_grab_focus(GTK_WIDGET(ctree));
2161                         gtk_cmctree_node_moveto(ctree, node, 0, 0.5, 0);
2162                 }
2163                 if (display_msg && summaryview->displayed == node)
2164                         summaryview->displayed = NULL;
2165                 summaryview->display_msg = display_msg;
2166                 gtk_sctree_select(GTK_SCTREE(ctree), node);
2167                 if (summaryview->selected == NULL)
2168                         summaryview->selected = node;
2169         }
2170 }
2171
2172 guint summary_get_msgnum(SummaryView *summaryview, GtkCMCTreeNode *node)
2173 {
2174         GtkCMCTree *ctree =NULL;
2175         MsgInfo *msginfo;
2176
2177         if (!summaryview)
2178                 return 0;
2179         ctree = GTK_CMCTREE(summaryview->ctree);
2180         if (!node)
2181                 return 0;
2182         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2183         if (msginfo)
2184                 return msginfo->msgnum;
2185         else 
2186                 return -1;
2187 }
2188
2189 static GtkCMCTreeNode *summary_find_prev_msg(SummaryView *summaryview,
2190                                            GtkCMCTreeNode *current_node)
2191 {
2192         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2193         GtkCMCTreeNode *node;
2194         MsgInfo *msginfo;
2195
2196         if (current_node)
2197                 node = current_node;
2198         else
2199                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2200
2201         for (; node != NULL; node = GTK_CMCTREE_NODE_PREV(node)) {
2202                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2203                 if (msginfo && !MSG_IS_DELETED(msginfo->flags)) break;
2204         }
2205
2206         return node;
2207 }
2208
2209 static GtkCMCTreeNode *summary_find_next_msg(SummaryView *summaryview,
2210                                            GtkCMCTreeNode *current_node)
2211 {
2212         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2213         GtkCMCTreeNode *node;
2214         MsgInfo *msginfo;
2215
2216         if (current_node)
2217                 node = current_node;
2218         else
2219                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2220
2221         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2222                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2223                 if (msginfo && !MSG_IS_DELETED(msginfo->flags) 
2224                 && !MSG_IS_MOVE(msginfo->flags)) break;
2225         }
2226
2227         return node;
2228 }
2229
2230 static GtkCMCTreeNode *summary_find_prev_flagged_msg(SummaryView *summaryview,
2231                                                    GtkCMCTreeNode *current_node,
2232                                                    MsgPermFlags flags,
2233                                                    gboolean start_from_prev)
2234 {
2235         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2236         GtkCMCTreeNode *node;
2237         MsgInfo *msginfo;
2238
2239         if (current_node) {
2240                 if (start_from_prev)
2241                         node = GTK_CMCTREE_NODE_PREV(current_node);
2242                 else
2243                         node = current_node;
2244         } else
2245                 node = gtk_cmctree_node_nth(ctree, GTK_CMCLIST(ctree)->rows - 1);
2246
2247         for (; node != NULL; node = GTK_CMCTREE_NODE_PREV(node)) {
2248                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2249                 if (msginfo && (msginfo->flags.perm_flags & flags) != 0) break;
2250         }
2251
2252         return node;
2253 }
2254
2255 static GtkCMCTreeNode *summary_find_next_flagged_msg(SummaryView *summaryview,
2256                                                    GtkCMCTreeNode *current_node,
2257                                                    MsgPermFlags flags,
2258                                                    gboolean start_from_next)
2259 {
2260         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2261         GtkCMCTreeNode *node;
2262         MsgInfo *msginfo;
2263
2264         if (current_node) {
2265                 if (start_from_next)
2266                         node = gtkut_ctree_node_next(ctree, current_node);
2267                 else
2268                         node = current_node;
2269         } else
2270                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2271
2272         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2273                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2274                 /* Find msg with matching flags but ignore messages with
2275                    ignore flags, if searching for new or unread messages */
2276                 if ((msginfo && (msginfo->flags.perm_flags & flags) != 0) &&
2277                     !(((flags & (MSG_NEW | MSG_UNREAD)) != 0) && MSG_IS_IGNORE_THREAD(msginfo->flags)) 
2278                         )
2279                         break;
2280         }
2281
2282         return node;
2283 }
2284
2285 static GtkCMCTreeNode *summary_find_msg_by_msgnum(SummaryView *summaryview,
2286                                                 guint msgnum)
2287 {
2288         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2289         GtkCMCTreeNode *node;
2290         MsgInfo *msginfo;
2291
2292         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2293
2294         for (; node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2295                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2296                 if (msginfo && msginfo->msgnum == msgnum) break;
2297         }
2298
2299         return node;
2300 }
2301
2302 static guint attract_hash_func(gconstpointer key)
2303 {
2304         gchar *str;
2305         gchar *p;
2306         guint h;
2307
2308         Xstrdup_a(str, (const gchar *)key, return 0);
2309         trim_subject(str);
2310
2311         p = str;
2312         h = *p;
2313
2314         if (h) {
2315                 for (p += 1; *p != '\0'; p++)
2316                         h = (h << 5) - h + *p;
2317         }
2318
2319         return h;
2320 }
2321
2322 static gint attract_compare_func(gconstpointer a, gconstpointer b)
2323 {
2324         return subject_compare((const gchar *)a, (const gchar *)b) == 0;
2325 }
2326
2327 void summary_attract_by_subject(SummaryView *summaryview)
2328 {
2329         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2330         GtkCMCList *clist = GTK_CMCLIST(ctree);
2331         GtkCMCTreeNode *src_node;
2332         GtkCMCTreeNode *dst_node, *sibling;
2333         GtkCMCTreeNode *tmp;
2334         MsgInfo *src_msginfo, *dst_msginfo;
2335         GHashTable *subject_table;
2336
2337         debug_print("Attracting messages by subject...");
2338         STATUSBAR_PUSH(summaryview->mainwin,
2339                        _("Attracting messages by subject..."));
2340
2341         main_window_cursor_wait(summaryview->mainwin);
2342         summary_freeze(summaryview);
2343
2344         subject_table = g_hash_table_new(attract_hash_func,
2345                                          attract_compare_func);
2346
2347         for (src_node = GTK_CMCTREE_NODE(clist->row_list);
2348              src_node != NULL;
2349              src_node = tmp) {
2350                 tmp = GTK_CMCTREE_ROW(src_node)->sibling;
2351                 src_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(src_node);
2352                 if (!src_msginfo) continue;
2353                 if (!src_msginfo->subject) continue;
2354
2355                 /* find attracting node */
2356                 dst_node = g_hash_table_lookup(subject_table,
2357                                                src_msginfo->subject);
2358
2359                 if (dst_node) {
2360                         dst_msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(dst_node);
2361
2362                         /* if the time difference is more than 20 days,
2363                            don't attract */
2364                         if (ABS(src_msginfo->date_t - dst_msginfo->date_t)
2365                             > 60 * 60 * 24 * 20)
2366                                 continue;
2367
2368                         sibling = GTK_CMCTREE_ROW(dst_node)->sibling;
2369                         if (src_node != sibling)
2370                                 gtk_cmctree_move(ctree, src_node, NULL, sibling);
2371                 }
2372
2373                 g_hash_table_insert(subject_table,
2374                                     src_msginfo->subject, src_node);
2375         }
2376
2377         g_hash_table_destroy(subject_table);
2378
2379         gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2380
2381         summary_thaw(summaryview);
2382
2383         debug_print("done.\n");
2384         STATUSBAR_POP(summaryview->mainwin);
2385
2386         main_window_cursor_normal(summaryview->mainwin);
2387 }
2388
2389 static void summary_free_msginfo_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2390                                       gpointer data)
2391 {
2392         MsgInfo *msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2393
2394         if (msginfo)
2395                 procmsg_msginfo_free(msginfo);
2396 }
2397
2398 static void summary_set_marks_func(GtkCMCTree *ctree, GtkCMCTreeNode *node,
2399                                    gpointer data)
2400 {
2401         SummaryView *summaryview = data;
2402         MsgInfo *msginfo;
2403
2404         msginfo = gtk_cmctree_node_get_row_data(ctree, node);
2405
2406         if (MSG_IS_DELETED(msginfo->flags))
2407                 summaryview->deleted++;
2408
2409         summaryview->total_size += msginfo->size;
2410
2411         summary_set_row_marks(summaryview, node);
2412 }
2413
2414 static void summary_update_status(SummaryView *summaryview)
2415 {
2416         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2417         GtkCMCTreeNode *node;
2418         MsgInfo *msginfo;
2419
2420         summaryview->total_size =
2421         summaryview->deleted = summaryview->moved = summaryview->copied = 0;
2422
2423         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
2424              node != NULL; node = gtkut_ctree_node_next(ctree, node)) {
2425                 msginfo = GTKUT_CTREE_NODE_GET_ROW_DATA(node);
2426                 
2427                 if (!msginfo)
2428                         continue;
2429
2430                 if (MSG_IS_DELETED(msginfo->flags))
2431                         summaryview->deleted++;
2432                 if (MSG_IS_MOVE(msginfo->flags))
2433                         summaryview->moved++;
2434                 if (MSG_IS_COPY(msginfo->flags))
2435                         summaryview->copied++;
2436                 summaryview->total_size += msginfo->size;
2437         }
2438 }
2439
2440 static void summary_status_show(SummaryView *summaryview)
2441 {
2442         gchar *str;
2443         gchar *del, *mv, *cp;
2444         gchar *sel;
2445         gchar *spc;
2446         gchar *itstr;
2447         GList *rowlist, *cur;
2448         guint n_selected = 0, n_new = 0, n_unread = 0, n_total = 0;
2449         guint n_marked = 0, n_replied = 0, n_forwarded = 0, n_locked = 0, n_ignored = 0, n_watched = 0;
2450         goffset sel_size = 0, n_size = 0;
2451         MsgInfo *msginfo;
2452         gchar *name;
2453 #if GTK_CHECK_VERSION(2, 12, 0)
2454         gchar *tooltip;
2455 #endif
2456         
2457         if (!summaryview->folder_item) {
2458                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), "");
2459                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), "");
2460                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs),   "");
2461                 toolbar_main_set_sensitive(summaryview->mainwin);
2462                 return;
2463         }
2464
2465         rowlist = GTK_CMCLIST(summaryview->ctree)->selection;
2466         for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2467                 msginfo = gtk_cmctree_node_get_row_data
2468                         (GTK_CMCTREE(summaryview->ctree),
2469                          GTK_CMCTREE_NODE(cur->data));
2470                 if (msginfo) {
2471                         sel_size += msginfo->size;
2472                         n_selected++;
2473                 }
2474         }
2475         
2476         if (summaryview->folder_item->hide_read_msgs 
2477         || summaryview->folder_item->hide_del_msgs
2478         || quicksearch_is_active(summaryview->quicksearch)) {
2479                 rowlist = GTK_CMCLIST(summaryview->ctree)->row_list;
2480                 for (cur = rowlist; cur != NULL && cur->data != NULL; cur = cur->next) {
2481                         msginfo = gtk_cmctree_node_get_row_data
2482                                 (GTK_CMCTREE(summaryview->ctree),
2483                                  GTK_CMCTREE_NODE(cur));
2484                         if (msginfo) {
2485                                 n_size += msginfo->size;
2486                                 n_total++;
2487                                 if (MSG_IS_NEW(msginfo->flags))
2488                                         n_new++;
2489                                 if (MSG_IS_UNREAD(msginfo->flags))
2490                                         n_unread++;
2491                                 if (MSG_IS_MARKED(msginfo->flags))
2492                                         n_marked++;
2493                                 if (MSG_IS_REPLIED(msginfo->flags))
2494                                         n_replied++;
2495                                 if (MSG_IS_FORWARDED(msginfo->flags))
2496                                         n_forwarded++;
2497                                 if (MSG_IS_LOCKED(msginfo->flags))
2498                                         n_locked++;
2499                                 if (MSG_IS_IGNORE_THREAD(msginfo->flags))
2500                                         n_ignored++;
2501                                 if (MSG_IS_WATCH_THREAD(msginfo->flags))
2502                                         n_watched++;
2503                         }
2504                 }
2505         } else {
2506                 n_new = summaryview->folder_item->new_msgs;
2507                 n_unread = summaryview->folder_item->unread_msgs;
2508                 n_marked = summaryview->folder_item->marked_msgs;
2509                 n_replied = summaryview->folder_item->replied_msgs;
2510                 n_forwarded = summaryview->folder_item->forwarded_msgs;
2511                 n_locked = summaryview->folder_item->locked_msgs;
2512                 n_ignored = summaryview->folder_item->ignored_msgs;
2513                 n_watched = summaryview->folder_item->watched_msgs;
2514                 n_total = summaryview->folder_item->total_msgs;
2515                 n_size = summaryview->total_size;
2516         }
2517
2518         name = folder_item_get_name(summaryview->folder_item);
2519         gtk_label_set_text(GTK_LABEL(summaryview->statlabel_folder), name);
2520         g_free(name);
2521
2522         if (summaryview->deleted)
2523                 del = g_strdup_printf(_("%d deleted"), summaryview->deleted);
2524         else
2525                 del = g_strdup("");
2526         if (summaryview->moved)
2527                 mv = g_strdup_printf(_("%s%d moved"),
2528                                      summaryview->deleted ? _(", ") : "",
2529                                      summaryview->moved);
2530         else
2531                 mv = g_strdup("");
2532         if (summaryview->copied)
2533                 cp = g_strdup_printf(_("%s%d copied"),
2534                                      summaryview->deleted ||
2535                                      summaryview->moved ? _(", ") : "",
2536                                      summaryview->copied);
2537         else
2538                 cp = g_strdup("");
2539
2540         if (summaryview->deleted || summaryview->moved || summaryview->copied)
2541                 spc = "    ";
2542         else
2543                 spc = "";
2544
2545         if (n_selected) {
2546                 sel = g_strdup_printf(" (%s)", to_human_readable((goffset)sel_size));
2547                 itstr = g_strdup_printf(ngettext(" item selected"," items selected", n_selected));
2548         } else {
2549                 sel = g_strdup("");
2550                 itstr = g_strdup("");
2551         }
2552                 
2553         if (prefs_common.layout_mode != SMALL_LAYOUT) {
2554                 str = g_strconcat(n_selected ? itos(n_selected) : "",
2555                                                 itstr, sel, spc, del, mv, cp, NULL);
2556                 g_free(sel);
2557                 g_free(del);
2558                 g_free(mv);
2559                 g_free(cp);
2560                 g_free(itstr);
2561                 
2562                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2563                 g_free(str);
2564
2565                 str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2566                                               n_new, n_unread, n_total,
2567                                               to_human_readable((goffset)n_size));
2568
2569
2570                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_msgs), str);
2571                 g_free(str);
2572 #if GTK_CHECK_VERSION(2, 12, 0)
2573                 tooltip = g_strdup_printf(_("<b>Message summary</b>\n"
2574                                             "<b>New:</b> %d\n"
2575                                             "<b>Unread:</b> %d\n"
2576                                             "<b>Total:</b> %d\n"
2577                                             "<b>Size:</b> %s\n\n"
2578                                             "<b>Marked:</b> %d\n"
2579                                             "<b>Replied:</b> %d\n"
2580                                             "<b>Forwarded:</b> %d\n"
2581                                             "<b>Locked:</b> %d\n"
2582                                             "<b>Ignored:</b> %d\n"
2583                                             "<b>Watched:</b> %d"),
2584                                               n_new, n_unread, n_total,
2585                                               to_human_readable((goffset)n_size),
2586                                               n_marked,n_replied,n_forwarded,
2587                                               n_locked,n_ignored,n_watched);
2588
2589                 gtk_widget_set_tooltip_markup(GTK_WIDGET(summaryview->statlabel_msgs),
2590                                             tooltip); 
2591                 g_free(tooltip);
2592 #endif
2593         } else {
2594                 gchar *ssize, *tsize;
2595                 if (n_selected) {
2596                         ssize = g_strdup(to_human_readable((goffset)sel_size));
2597                         tsize = g_strdup(to_human_readable((goffset)n_size));
2598                         str = g_strdup_printf(_("%d/%d selected (%s/%s), %d unread"),
2599                                 n_selected, n_total, ssize, tsize, n_unread);
2600                         g_free(ssize);
2601                         g_free(tsize);
2602                 } else
2603                         str = g_strdup_printf(_("%d new, %d unread, %d total (%s)"),
2604                                 n_new, n_unread, n_total, to_human_readable((goffset)n_size));
2605                 g_free(sel);
2606                 g_free(del);
2607                 g_free(mv);
2608                 g_free(cp);
2609                 g_free(itstr);
2610                 
2611                 gtk_label_set_text(GTK_LABEL(summaryview->statlabel_select), str);
2612                 g_free(str);
2613         }
2614
2615         summary_set_menu_sensitive(summaryview);
2616         toolbar_main_set_sensitive(summaryview->mainwin);
2617 }
2618
2619 static void summary_set_column_titles(SummaryView *summaryview)
2620 {
2621         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
2622         FolderItem *item = summaryview->folder_item;
2623         GtkWidget *hbox;
2624         GtkWidget *label;
2625         GtkWidget *arrow;
2626         gint pos;
2627         const gchar *title;
2628         SummaryColumnType type;
2629         GtkJustification justify;
2630
2631         static FolderSortKey sort_by[N_SUMMARY_COLS] = {
2632                 SORT_BY_MARK,
2633                 SORT_BY_STATUS,
2634                 SORT_BY_MIME,
2635                 SORT_BY_SUBJECT,
2636                 SORT_BY_FROM,
2637                 SORT_BY_TO,
2638                 SORT_BY_DATE,
2639                 SORT_BY_SIZE,
2640                 SORT_BY_NUMBER,
2641                 SORT_BY_SCORE,
2642                 SORT_BY_LOCKED,
2643                 SORT_BY_TAGS
2644         };
2645
2646         for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
2647                 type = summaryview->col_state[pos].type;
2648
2649                 /* CLAWS: mime and unread are single char headers */
2650                 justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
2651                         ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
2652
2653                 switch (type) {
2654                 case S_COL_SUBJECT:
2655                 case S_COL_FROM:
2656                 case S_COL_TO:
2657                 case S_COL_DATE:
2658                 case S_COL_NUMBER:
2659                         if(type == S_COL_FROM && item != NULL &&
2660                                         FOLDER_SHOWS_TO_HDR(item) &&
2661                                         !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2662                                 type = S_COL_TO;
2663                         if(type == S_COL_NUMBER)
2664                                 title = gettext(col_label[type]);
2665                         else
2666                                 title = prefs_common_translated_header_name(col_label[type]);
2667                         break;
2668                 default:
2669                         title = gettext(col_label[type]);
2670                 }
2671
2672                 if (type == S_COL_MIME) {
2673                         label = gtk_image_new_from_pixbuf(clipxpm);
2674                         gtk_widget_show(label);
2675                         gtk_cmclist_set_column_widget(clist, pos, label);
2676                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Attachment"));
2677                         continue;
2678                 } else if (type == S_COL_MARK) {
2679                         label = gtk_image_new_from_pixbuf(markxpm);
2680                         gtk_widget_show(label);
2681                         gtk_cmclist_set_column_widget(clist, pos, label);
2682                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Mark"));
2683                         continue;
2684                 } else if (type == S_COL_LOCKED) {
2685                         label = gtk_image_new_from_pixbuf(lockedxpm);
2686                         gtk_widget_show(label);
2687                         gtk_cmclist_set_column_widget(clist, pos, label);
2688                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Locked"));
2689                         continue;
2690                 } else if (type == S_COL_STATUS) {
2691                         gtk_cmclist_set_column_title(clist, pos, title);
2692                         gtk_sctree_set_column_tooltip(GTK_SCTREE(clist), pos, _("Status"));
2693                         continue;
2694                 }
2695
2696                 hbox  = gtk_hbox_new(FALSE, 4);
2697                 label = gtk_label_new(title);
2698 #ifdef GENERIC_UMPC
2699         gtk_widget_set_size_request(hbox, -1, 20);
2700 #endif
2701
2702                 if (justify == GTK_JUSTIFY_RIGHT)
2703                         gtk_box_pack_end(GTK_BOX(hbox), label,
2704                                          FALSE, FALSE, 0);
2705                 else
2706                         gtk_box_pack_start(GTK_BOX(hbox), label,
2707                                            FALSE, FALSE, 0);
2708
2709                 if (summaryview->sort_key == sort_by[type]) {
2710                         arrow = gtk_arrow_new
2711                                 (summaryview->sort_type == SORT_ASCENDING
2712                                  ? GTK_ARROW_DOWN : GTK_ARROW_UP,
2713                                  GTK_SHADOW_IN);
2714                         gtk_widget_set_size_request(GTK_WIDGET(arrow), 10, 10);
2715                         if (justify == GTK_JUSTIFY_RIGHT)
2716                                 gtk_box_pack_start(GTK_BOX(hbox), arrow,
2717                                                    FALSE, FALSE, 0);
2718                         else
2719                                 gtk_box_pack_end(GTK_BOX(hbox), arrow,
2720                                                  FALSE, FALSE, 0);
2721                 }
2722
2723                 gtk_widget_show_all(hbox);
2724                 gtk_cmclist_set_column_widget(clist, pos, hbox);
2725         }
2726 }
2727
2728 void summary_reflect_tags_changes(SummaryView *summaryview)
2729 {
2730         GtkMenuShell *menu;
2731         GList *children, *cur;
2732         GtkCMCTreeNode *node;
2733         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2734         gboolean froze = FALSE;
2735         gboolean redisplay = FALSE;
2736
2737         /* re-create colorlabel submenu */
2738         menu = GTK_MENU_SHELL(summaryview->tags_menu);
2739         cm_return_if_fail(menu != NULL);
2740
2741         /* clear items. get item pointers. */
2742         children = gtk_container_get_children(GTK_CONTAINER(menu));
2743         for (cur = children; cur != NULL && cur->data != NULL; cur = cur->next) {
2744                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(cur->data), NULL);
2745         }
2746         g_list_free(children);
2747         summary_tags_menu_create(summaryview, TRUE);
2748
2749         START_LONG_OPERATION(summaryview, TRUE);
2750         for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
2751              node = gtkut_ctree_node_next(ctree, node)) {
2752                 redisplay |= summary_set_row_tag(summaryview,
2753                                            node, TRUE, FALSE, 0);
2754         }
2755         END_LONG_OPERATION(summaryview);
2756         if (redisplay)
2757                 summary_redisplay_msg(summaryview);
2758 }
2759
2760
2761 void summary_reflect_prefs(void)
2762 {
2763         static gchar *last_smallfont = NULL;
2764         static gchar *last_normalfont = NULL;
2765         static gchar *last_boldfont = NULL;
2766         static gboolean last_derive = 0;
2767         gboolean update_font = FALSE;
2768         SummaryView *summaryview = NULL;
2769
2770         if (!mainwindow_get_mainwindow())
2771                 return;
2772         summaryview = mainwindow_get_mainwindow()->summaryview;
2773
2774         if (!last_smallfont || strcmp(last_smallfont, SMALL_FONT) ||
2775                         !last_normalfont || strcmp(last_normalfont, NORMAL_FONT) ||
2776                         !last_boldfont || strcmp(last_boldfont, BOLD_FONT) ||
2777                         last_derive != prefs_common.derive_from_normal_font)
2778                 update_font = TRUE;
2779
2780         g_free(last_smallfont);
2781         last_smallfont = g_strdup(SMALL_FONT);
2782         g_free(last_normalfont);
2783         last_normalfont = g_strdup(NORMAL_FONT);
2784         g_free(last_boldfont);
2785         last_boldfont = g_strdup(BOLD_FONT);
2786         last_derive = prefs_common.derive_from_normal_font;
2787
2788         if (update_font) {      
2789                 bold_style = bold_marked_style = bold_deleted_style = 
2790                         small_style = small_marked_style = small_deleted_style = NULL;
2791                 summary_set_fonts(summaryview);
2792         }
2793
2794         summary_set_column_titles(summaryview);
2795         summary_relayout(summaryview);
2796         
2797         if (summaryview->folder_item)
2798                 summary_show(summaryview, summaryview->folder_item);
2799 }
2800
2801 void summary_sort(SummaryView *summaryview,
2802                   FolderSortKey sort_key, FolderSortType sort_type)
2803 {
2804         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2805         GtkCMCList *clist = GTK_CMCLIST(summaryview->ctree);
2806         GtkCMCListCompareFunc cmp_func = NULL;
2807         START_TIMING("");
2808         g_signal_handlers_block_by_func(G_OBJECT(summaryview->ctree),
2809                                        G_CALLBACK(summary_tree_expanded), summaryview);
2810         summary_freeze(summaryview);
2811
2812         switch (sort_key) {
2813         case SORT_BY_MARK:
2814                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mark;
2815                 break;
2816         case SORT_BY_STATUS:
2817                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_status;
2818                 break;
2819         case SORT_BY_MIME:
2820                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_mime;
2821                 break;
2822         case SORT_BY_NUMBER:
2823                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_num;
2824                 break;
2825         case SORT_BY_SIZE:
2826                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_size;
2827                 break;
2828         case SORT_BY_DATE:
2829                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_date;
2830                 break;
2831         case SORT_BY_THREAD_DATE:
2832                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_thread_date;
2833                 break;
2834         case SORT_BY_FROM:
2835                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_from;
2836                 break;
2837         case SORT_BY_SUBJECT:
2838 #ifndef G_OS_WIN32
2839                 if (summaryview->simplify_subject_preg)
2840                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_simplified_subject;
2841                 else
2842 #endif
2843                         cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_subject;
2844                 break;
2845         case SORT_BY_SCORE:
2846                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_score;
2847                 break;
2848         case SORT_BY_LABEL:
2849                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_label;
2850                 break;
2851         case SORT_BY_TO:
2852                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_to;
2853                 break;
2854         case SORT_BY_LOCKED:
2855                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_locked;
2856                 break;
2857         case SORT_BY_TAGS:
2858                 cmp_func = (GtkCMCListCompareFunc)summary_cmp_by_tags;
2859                 break;
2860         case SORT_BY_NONE:
2861                 break;
2862         default:
2863                 goto unlock;
2864         }
2865
2866         summaryview->sort_key = sort_key;
2867         summaryview->sort_type = sort_type;
2868
2869         summary_set_column_titles(summaryview);
2870         summary_set_menu_sensitive(summaryview);
2871
2872         /* allow fallback to don't sort */
2873         if (summaryview->sort_key == SORT_BY_NONE)
2874                 goto unlock;
2875
2876         if (cmp_func != NULL) {
2877                 debug_print("Sorting summary...");
2878                 STATUSBAR_PUSH(summaryview->mainwin, _("Sorting summary..."));
2879
2880                 main_window_cursor_wait(summaryview->mainwin);
2881
2882                 gtk_cmclist_set_compare_func(clist, cmp_func);
2883
2884                 gtk_cmclist_set_sort_type(clist, (GtkSortType)sort_type);
2885                 gtk_sctree_sort_recursive(ctree, NULL);
2886
2887                 gtk_cmctree_node_moveto(ctree, summaryview->selected, 0, 0.5, 0);
2888
2889                 main_window_cursor_normal(summaryview->mainwin);
2890
2891                 debug_print("done.\n");
2892                 STATUSBAR_POP(summaryview->mainwin);
2893         }
2894 unlock:
2895         summary_thaw(summaryview);
2896         g_signal_handlers_unblock_by_func(G_OBJECT(summaryview->ctree),
2897                                        G_CALLBACK(summary_tree_expanded), summaryview);
2898         END_TIMING();
2899 }
2900
2901 static gboolean summary_update_thread_age(GNode *node, gpointer data)
2902 {
2903         MsgInfo *msginfo = node->data;
2904         time_t *most_recent = (time_t *)data;
2905
2906         if (msginfo->date_t > *most_recent) {
2907                 *most_recent = msginfo->date_t;
2908         }
2909         return FALSE;
2910 }       
2911
2912 static void summary_find_thread_age(GNode *gnode)
2913 {
2914         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2915         time_t most_recent;
2916
2917         if (!msginfo)
2918                 return;
2919         most_recent = msginfo->thread_date = msginfo->date_t;
2920
2921         g_node_traverse(gnode, G_IN_ORDER, G_TRAVERSE_ALL, -1, summary_update_thread_age, &most_recent);
2922
2923         msginfo->thread_date = most_recent;
2924 }
2925
2926 static gboolean summary_insert_gnode_func(GtkCMCTree *ctree, guint depth, GNode *gnode,
2927                                    GtkCMCTreeNode *cnode, gpointer data)
2928 {
2929         SummaryView *summaryview = (SummaryView *)data;
2930         MsgInfo *msginfo = (MsgInfo *)gnode->data;
2931         gchar *text[N_SUMMARY_COLS];
2932         gint *col_pos = summaryview->col_pos;
2933         const gchar *msgid = msginfo->msgid;
2934         GHashTable *msgid_table = summaryview->msgid_table;
2935         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
2936
2937         summary_set_header(summaryview, text, msginfo);
2938
2939         gtk_sctree_set_node_info(ctree, cnode, text[col_pos[S_COL_SUBJECT]], 2,
2940                                 NULL, NULL, FALSE, summaryview->threaded && !summaryview->thread_collapsed);
2941 #define SET_TEXT(col) {                                         \
2942         gtk_cmctree_node_set_text(ctree, cnode, col_pos[col],   \
2943                                 text[col_pos[col]]);            \
2944 }
2945
2946         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
2947                 SET_TEXT(S_COL_NUMBER);
2948         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
2949                 SET_TEXT(S_COL_SCORE);
2950         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
2951                 SET_TEXT(S_COL_SIZE);
2952         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible)
2953                 SET_TEXT(S_COL_DATE);
2954         if (summaryview->col_state[summaryview->col_pos[S_COL_FROM]].visible)
2955                 SET_TEXT(S_COL_FROM);
2956         if (summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible)
2957                 SET_TEXT(S_COL_TO);
2958         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible)
2959                 SET_TEXT(S_COL_TAGS);
2960
2961         if (vert && prefs_common.two_line_vert)
2962                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
2963
2964 #undef SET_TEXT
2965
2966         GTKUT_CTREE_NODE_SET_ROW_DATA(cnode, msginfo);
2967         summary_set_marks_func(ctree, cnode, summaryview);
2968
2969         if (msgid && msgid[0] != '\0')
2970                 g_hash_table_insert(msgid_table, (gchar *)msgid, cnode);
2971
2972         return TRUE;
2973 }
2974
2975 static void summary_set_ctree_from_list(SummaryView *summaryview,
2976                                         GSList *mlist)
2977 {
2978         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
2979         MsgInfo *msginfo;
2980         GtkCMCTreeNode *node = NULL;
2981         GHashTable *msgid_table;
2982         GHashTable *subject_table = NULL;
2983         GSList * cur;
2984         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
2985
2986         START_TIMING("");
2987         
2988         if (!mlist) return;
2989
2990         debug_print("\tSetting summary from message data...\n");
2991         STATUSBAR_PUSH(summaryview->mainwin,
2992                        _("Setting summary from message data..."));
2993         gdk_flush();
2994
2995         g_signal_handlers_block_by_func(G_OBJECT(ctree),
2996                                        G_CALLBACK(summary_tree_expanded), summaryview);
2997
2998         msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
2999         summaryview->msgid_table = msgid_table;
3000
3001         if (prefs_common.thread_by_subject) {
3002                 subject_table = g_hash_table_new(g_str_hash, g_str_equal);
3003                 summaryview->subject_table = subject_table;
3004         } else {
3005                 summaryview->subject_table = NULL;
3006         }
3007
3008         if (prefs_common.use_addr_book)
3009                 start_address_completion(NULL);
3010         
3011         if (summaryview->threaded) {
3012                 GNode *root, *gnode;
3013                 START_TIMING("threaded");
3014                 root = procmsg_get_thread_tree(mlist);
3015
3016                 
3017                 for (gnode = root->children; gnode != NULL;
3018                      gnode = gnode->next) {
3019                         summary_find_thread_age(gnode);
3020                         node = gtk_sctree_insert_gnode
3021                                 (ctree, NULL, node, gnode,
3022                                  summary_insert_gnode_func, summaryview);
3023                 }
3024
3025                 g_node_destroy(root);
3026                 
3027                 END_TIMING();
3028         } else {
3029                 gchar *text[N_SUMMARY_COLS];
3030                 START_TIMING("unthreaded");
3031                 cur = mlist;
3032                 for (; mlist != NULL; mlist = mlist->next) {
3033                         msginfo = (MsgInfo *)mlist->data;
3034
3035                         summary_set_header(summaryview, text, msginfo);
3036
3037                         node = gtk_sctree_insert_node
3038                                 (ctree, NULL, node, text, 2,
3039                                  NULL, NULL,
3040                                  FALSE, FALSE);
3041                         if (vert && prefs_common.two_line_vert)
3042                                 g_free(text[summaryview->col_pos[S_COL_SUBJECT]]);
3043
3044                         GTKUT_CTREE_NODE_SET_ROW_DATA(node, msginfo);
3045                         summary_set_marks_func(ctree, node, summaryview);
3046
3047                         if (msginfo->msgid && msginfo->msgid[0] != '\0')
3048                                 g_hash_table_insert(msgid_table,
3049                                                     msginfo->msgid, node);
3050
3051                         if (prefs_common.thread_by_subject)
3052                                 subject_table_insert(subject_table,
3053                                              msginfo->subject,
3054                                              node);
3055                 }
3056                 mlist = cur;
3057                 END_TIMING();
3058         }
3059
3060         if (prefs_common.enable_hscrollbar &&
3061             summaryview->col_pos[S_COL_SUBJECT] == N_SUMMARY_COLS - 1) {
3062                 gint optimal_width;
3063
3064                 optimal_width = gtk_cmclist_optimal_column_width
3065                         (GTK_CMCLIST(ctree), summaryview->col_pos[S_COL_SUBJECT]);
3066                 gtk_cmclist_set_column_width(GTK_CMCLIST(ctree),
3067                                            summaryview->col_pos[S_COL_SUBJECT],
3068                                            optimal_width);
3069         }
3070
3071         if (prefs_common.use_addr_book)
3072                 end_address_completion();
3073
3074         debug_print("done.\n");
3075         STATUSBAR_POP(summaryview->mainwin);
3076         if (debug_get_mode()) {
3077                 debug_print("\tmsgid hash table size = %d\n",
3078                             g_hash_table_size(msgid_table));
3079                 if (prefs_common.thread_by_subject)
3080                         debug_print("\tsubject hash table size = %d\n",
3081                             g_hash_table_size(subject_table));
3082         }
3083
3084         summary_sort(summaryview, summaryview->sort_key, summaryview->sort_type);
3085
3086         node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
3087
3088         if (prefs_common.bold_unread) {
3089                 START_TIMING("bold_unread");
3090                 while (node) {
3091                         GtkCMCTreeNode *next = GTK_CMCTREE_NODE_NEXT(node);
3092                         if (GTK_CMCTREE_ROW(node)->children)
3093                                 summary_set_row_marks(summaryview, node);
3094                         node = next;
3095                 }
3096                 END_TIMING();
3097         }
3098
3099         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
3100                                        G_CALLBACK(summary_tree_expanded), summaryview);
3101         END_TIMING();
3102 }
3103
3104 static gchar *summary_complete_address(const gchar *addr)
3105 {
3106         gint count;
3107         gchar *res, *tmp, *email_addr;
3108         
3109         if (addr == NULL || !strchr(addr, '@'))
3110                 return NULL;
3111
3112         Xstrdup_a(email_addr, addr, return NULL);
3113         extract_address(email_addr);
3114         if (!*email_addr)
3115                 return NULL;
3116
3117         /*
3118          * completion stuff must be already initialized
3119          */
3120         res = NULL;
3121         if (1 < (count = complete_address(email_addr))) {
3122                 tmp = get_complete_address(1);
3123                 res = procheader_get_fromname(tmp);
3124                 g_free(tmp);
3125         }
3126
3127         return res;
3128 }
3129
3130 static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
3131                                MsgInfo *msginfo)
3132 {
3133         static gchar date_modified[80];
3134         static gchar col_score[11];
3135         static gchar buf[BUFFSIZE], tmp1[BUFFSIZE], tmp2[BUFFSIZE], tmp3[BUFFSIZE];
3136         gint *col_pos = summaryview->col_pos;
3137         gchar *from_text = NULL, *to_text = NULL, *tags_text = NULL;
3138         gboolean should_swap = FALSE;
3139         gboolean vert = (prefs_common.layout_mode == VERTICAL_LAYOUT);
3140 #if GTK_CHECK_VERSION(2,12,0)
3141         static const gchar *color_dim_rgb = NULL;
3142         if (!color_dim_rgb)
3143                 color_dim_rgb = gdk_color_to_string(&summaryview->color_dim);
3144 #else
3145         static const gchar *color_dim_rgb = "#888888";
3146 #endif
3147         text[col_pos[S_COL_FROM]]   = "";
3148         text[col_pos[S_COL_TO]]     = "";
3149         text[col_pos[S_COL_SUBJECT]]= "";
3150         text[col_pos[S_COL_MARK]]   = "";
3151         text[col_pos[S_COL_STATUS]] = "";
3152         text[col_pos[S_COL_MIME]]   = "";
3153         text[col_pos[S_COL_LOCKED]] = "";
3154         text[col_pos[S_COL_DATE]]   = "";
3155         text[col_pos[S_COL_TAGS]]   = "";
3156         if (summaryview->col_state[summaryview->col_pos[S_COL_NUMBER]].visible)
3157                 text[col_pos[S_COL_NUMBER]] = itos(msginfo->msgnum);
3158         else
3159                 text[col_pos[S_COL_NUMBER]] = "";
3160
3161         /* slow! */
3162         if (summaryview->col_state[summaryview->col_pos[S_COL_SIZE]].visible)
3163                 text[col_pos[S_COL_SIZE]] = to_human_readable(msginfo->size);
3164         else
3165                 text[col_pos[S_COL_SIZE]] = "";
3166
3167         if (summaryview->col_state[summaryview->col_pos[S_COL_SCORE]].visible)
3168                 text[col_pos[S_COL_SCORE]] = itos_buf(col_score, msginfo->score);
3169         else
3170                 text[col_pos[S_COL_SCORE]] = "";
3171
3172         if (summaryview->col_state[summaryview->col_pos[S_COL_TAGS]].visible) {
3173                 tags_text = procmsg_msginfo_get_tags_str(msginfo);
3174                 if (!tags_text) {
3175                         text[col_pos[S_COL_TAGS]] = "-";
3176                 } else {
3177                         strncpy2(tmp1, tags_text, sizeof(tmp1));
3178                         tmp1[sizeof(tmp1)-1]='\0';
3179                         g_free(tags_text);
3180                         text[col_pos[S_COL_TAGS]] = tmp1;
3181                 }
3182         } else
3183                 text[col_pos[S_COL_TAGS]] = "";
3184
3185         /* slow! */
3186         if (summaryview->col_state[summaryview->col_pos[S_COL_DATE]].visible || 
3187             (vert && prefs_common.two_line_vert)) {
3188                 if (msginfo->date_t && msginfo->date_t > 0) {
3189                         procheader_date_get_localtime(date_modified,
3190                                                       sizeof(date_modified),
3191                                                       msginfo->date_t);
3192                         text[col_pos[S_COL_DATE]] = date_modified;
3193                 } else if (msginfo->date)
3194                         text[col_pos[S_COL_DATE]] = msginfo->date;
3195                 else
3196                         text[col_pos[S_COL_DATE]] = _("(No Date)");
3197         }
3198         
3199         if (prefs_common.swap_from && msginfo->from && msginfo->to
3200         &&  !summaryview->col_state[summaryview->col_pos[S_COL_TO]].visible) {
3201                 gchar *addr = NULL;
3202                 
3203                 addr = g_strdup(msginfo->from);
3204
3205                 if (addr) {
3206                         extract_address(addr);
3207                         if (account_find_from_address(addr, FALSE)) {
3208                                 should_swap = TRUE;
3209                         }
3210                         g_free(addr);
3211                 }
3212         }
3213
3214         if (!prefs_common.use_addr_book) {
3215                 if (prefs_common.summary_from_show == SHOW_NAME)
3216                         from_text = msginfo->fromname;
3217                 else if (prefs_common.summary_from_show == SHOW_BOTH)
3218                         from_text = msginfo->from;
3219                 else {
3220                         from_text = msginfo->from;
3221                         extract_address(from_text);
3222                 }
3223                 if (!from_text)
3224                         _("(No From)");         
3225         } else {
3226                 gchar *tmp = summary_complete_address(msginfo->from);
3227                 if (tmp) {
3228                         strncpy2(buf, tmp, sizeof(buf));
3229                         g_free(tmp);
3230                         from_text = buf;
3231                 } else {
3232                         if (prefs_common.summary_from_show == SHOW_NAME)
3233                                 from_text = msginfo->fromname;
3234                         else if (prefs_common.summary_from_show == SHOW_BOTH)
3235                                 from_text = msginfo->from;
3236                         else {
3237                                 from_text = msginfo->from;
3238                                 extract_address(from_text);
3239                         }
3240                         if (!from_text)
3241                                 _("(No From)");         
3242                 }
3243         }
3244         
3245         to_text = msginfo->to ? msginfo->to : 
3246                    (msginfo->cc ? msginfo->cc :
3247                      (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
3248                      )
3249                    );
3250
3251         text[col_pos[S_COL_TO]] = to_text;
3252         if (!should_swap) {
3253                 text[col_pos[S_COL_FROM]] = from_text;
3254         } else {
3255                 if (prefs_common.use_addr_book) {
3256                         gchar *tmp = summary_complete_address(to_text);
3257                         if (tmp) {
3258                                 strncpy2(buf, tmp, sizeof(buf));
3259                                 g_free(tmp);
3260                                 to_text = buf;
3261                         } else {
3262                                 to_text = to_text ? to_text : _("(No From)");
3263                         }
3264                 }
3265                 snprintf(tmp2, BUFFSIZE-1, "--> %s", to_text);
3266                 tmp2[BUFFSIZE-1]='\0';
3267                 text[col_pos[S_COL_FROM]] = tmp2;
3268         }
3269         
3270 #ifndef G_OS_WIN32
3271         if (summaryview->simplify_subject_preg != NULL)
3272                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
3273                         string_remove_match(tmp3, BUFFSIZE, msginfo->subject, 
3274                                         summaryview->simplify_subject_preg) : 
3275                         _("(No Subject)");
3276         else 
3277 #endif
3278                 text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
3279                         _("(No Subject)");
3280         if (vert && prefs_common.two_line_vert) {
3281                 if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item)) {
3282                         gchar *tmp = g_markup_printf_escaped(_("%s\n<span color='%s' style='italic'>From: %s, on %s</span>"),
3283                                         text[col_pos[S_COL_SUBJECT]],
3284                                         color_dim_rgb,
3285                                         text[col_pos[S_COL_FROM]],
3286                                         text[col_pos[S_COL_DATE]]);
3287                         text[col_pos[S_COL_SUBJECT]] = tmp;
3288                 } else {
3289                         gchar *tmp = g_markup_printf_escaped(_("%s\n<span color='%s' style='italic'>To: %s, on %s</span>"),
3290                                         text[col_pos[S_COL_SUBJECT]],
3291                                         color_dim_rgb,
3292                                         text[col_pos[S_COL_TO]],
3293                                         text[col_pos[S_COL_DATE]]);
3294                         text[col_pos[S_COL_SUBJECT]] = tmp;
3295                 }
3296         }
3297 }
3298
3299 static void summary_display_msg(SummaryView *summaryview, GtkCMCTreeNode *row)
3300 {
3301         summary_display_msg_full(summaryview, row, FALSE, FALSE);
3302 }
3303
3304 static gboolean defer_change(gpointer data);
3305 typedef struct _ChangeData {
3306         MsgInfo *info;
3307         gint op; /* 0, 1, 2 for unset, set, change */
3308         MsgPermFlags set_flags;
3309         MsgTmpFlags  set_tmp_flags;
3310         MsgPermFlags unset_flags;
3311         MsgTmpFlags  unset_tmp_flags;
3312 } ChangeData;
3313
3314 static void summary_msginfo_unset_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3315 {
3316         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3317                 debug_print("flags: doing unset now\n");
3318                 procmsg_msginfo_unset_flags(msginfo, flags, tmp_flags);
3319         } else {
3320                 ChangeData *unset_data = g_new0(ChangeData, 1);
3321                 unset_data->info = msginfo;
3322                 unset_data->op = 0;
3323                 unset_data->unset_flags = flags;
3324                 unset_data->unset_tmp_flags = tmp_flags;
3325                 debug_print("flags: deferring unset\n");
3326                 g_timeout_add(100, defer_change, unset_data);
3327         }
3328 }
3329
3330 static void summary_msginfo_set_flags(MsgInfo *msginfo, MsgPermFlags flags, MsgTmpFlags tmp_flags)
3331 {
3332         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3333                 debug_print("flags: doing set now\n");
3334                 procmsg_msginfo_set_flags(msginfo, flags, tmp_flags);
3335         } else {
3336                 ChangeData *set_data = g_new0(ChangeData, 1);
3337                 set_data->info = msginfo;
3338                 set_data->op = 1;
3339                 set_data->set_flags = flags;
3340                 set_data->set_tmp_flags = tmp_flags;
3341                 debug_print("flags: deferring set\n");
3342                 g_timeout_add(100, defer_change, set_data);
3343         }
3344 }
3345
3346 static void summary_msginfo_change_flags(MsgInfo *msginfo, 
3347                 MsgPermFlags add_flags, MsgTmpFlags add_tmp_flags,
3348                 MsgPermFlags rem_flags, MsgTmpFlags rem_tmp_flags)
3349 {
3350         if (!msginfo->folder || !msginfo->folder->processing_pending) {
3351                 debug_print("flags: doing change now\n");
3352                 procmsg_msginfo_change_flags(msginfo, add_flags, add_tmp_flags,
3353                         rem_flags, rem_tmp_flags);
3354         } else {
3355                 ChangeData *change_data = g_new0(ChangeData, 1);
3356                 change_data->info = msginfo;
3357                 change_data->op = 2;
3358                 change_data->set_flags = add_flags;
3359                 change_data->set_tmp_flags = add_tmp_flags;
3360                 change_data->unset_flags = rem_flags;
3361                 change_data->unset_tmp_flags = rem_tmp_flags;
3362                 debug_print("flags: deferring change\n");
3363                 g_timeout_add(100, defer_change, change_data);
3364         }
3365 }
3366
3367 gboolean defer_change(gpointer data)
3368 {
3369         ChangeData *chg = (ChangeData *)data;
3370         if (chg->info->folder && chg->info->folder->processing_pending) {
3371                 debug_print("flags: trying later\n");
3372                 return TRUE; /* try again */
3373         } else {
3374                 debug_print("flags: finally doing it\n");
3375                 switch(chg->op) {
3376                 case 0:
3377                         procmsg_msginfo_unset_flags(chg->info, chg->unset_flags, chg->unset_tmp_flags);
3378                         break;
3379                 case 1:
3380                         procmsg_msginfo_set_flags(chg->info, chg->set_flags, chg->set_tmp_flags);
3381                         break;
3382                 case 2:
3383                         procmsg_msginfo_change_flags(chg->info, chg->set_flags, chg->set_tmp_flags,
3384                                 chg->unset_flags, chg->unset_tmp_flags);
3385                         break;
3386                 default:
3387                         g_warning("shouldn't happen\n");
3388                 }
3389                 g_free(chg);
3390         }
3391         return FALSE;
3392 }
3393
3394 static void msginfo_mark_as_read (SummaryView *summaryview, MsgInfo *msginfo,
3395                                       GtkCMCTreeNode *row)
3396 {
3397         cm_return_if_fail(summaryview != NULL);
3398         cm_return_if_fail(msginfo != NULL);
3399         cm_return_if_fail(row != NULL);
3400
3401         if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
3402                 summary_msginfo_unset_flags
3403                         (msginfo, MSG_NEW | MSG_UNREAD, 0);
3404                 summary_set_row_marks(summaryview, row);
3405                 summary_status_show(summaryview);
3406         }
3407 }
3408
3409 typedef struct  {
3410         MsgInfo *msginfo;
3411         SummaryView *summaryview;
3412 } MarkAsReadData;
3413
3414 static int msginfo_mark_as_read_timeout(void *data)
3415 {
3416         MarkAsReadData *mdata = (MarkAsReadData *)data;
3417         if (!mdata)
3418                 return FALSE;
3419         
3420         if (mdata->msginfo == summary_get_selected_msg(mdata->summaryview))
3421                 msginfo_mark_as_read(mdata->summaryview, mdata->msginfo,
3422                                      mdata->summaryview->selected); 
3423
3424         g_free(mdata);
3425
3426         return FALSE;   
3427 }
3428
3429 static void summary_display_msg_full(SummaryView *summaryview,
3430                                      GtkCMCTreeNode *row,
3431                                      gboolean new_window, gboolean all_headers)
3432 {
3433         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3434         MsgInfo *msginfo;
3435         gint val;
3436         START_TIMING("");
3437         if (!new_window) {
3438                 if (summaryview->displayed == row)
3439                         return;
3440                 else if (summaryview->messageview)
3441                         summaryview->messageview->filtered = FALSE;
3442         }                       
3443         cm_return_if_fail(row != NULL);
3444
3445         if (summary_is_locked(summaryview)) return;
3446         summary_lock(summaryview);
3447
3448         STATUSBAR_POP(summaryview->mainwin);
3449         GTK_EVENTS_FLUSH();
3450
3451         msginfo = gtk_cmctree_node_get_row_data(ctree, row);
3452
3453         if (!msginfo) {
3454                 debug_print("NULL msginfo\n");
3455                 summary_unlock(summaryview);
3456                 END_TIMING();
3457                 return;
3458         }       
3459
3460         if (new_window && prefs_common.layout_mode != SMALL_LAYOUT) {
3461                 MessageView *msgview;
3462
3463                 msgview = messageview_create_with_new_window(summaryview->mainwin);
3464                 val = messageview_show(msgview, msginfo, all_headers);
3465         } else {
3466                 MessageView *msgview;
3467
3468                 if (prefs_common.layout_mode == SMALL_LAYOUT) {
3469                         if (summaryview->ext_messageview == NULL)
3470                                 summaryview->ext_messageview = messageview_create_with_new_window(summaryview->mainwin);
3471                         else
3472                                 gtkut_window_popup(summaryview->ext_messageview->window);
3473                         msgview = summaryview->ext_messageview;
3474                         summaryview->last_displayed = summaryview->displayed;
3475                         summaryview->displayed = row;
3476                         val = messageview_show(msgview, msginfo, all_headers);
3477                         if (GTK_CMCLIST(msgview->mimeview->ctree)->row_list == NULL)
3478                                 gtk_widget_grab_focus(summaryview->ctree);
3479                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3480                                 GTK_CMCLIST(summaryview->ctree)->focus_row);
3481                 } else {
3482                         msgview = summaryview->messageview;
3483                         summaryview->last_displayed = summaryview->displayed;
3484                         summaryview->displayed = row;
3485                         if (!messageview_is_visible(msgview)) {
3486                                 main_window_toggle_message_view(summaryview->mainwin);
3487                                 GTK_EVENTS_FLUSH();
3488                         }
3489                         val = messageview_show(msgview, msginfo, all_headers);
3490                         if (GTK_CMCLIST(msgview->mimeview->ctree)->row_list == NULL)
3491                                 gtk_widget_grab_focus(summaryview->ctree);
3492                         gtkut_ctree_node_move_if_on_the_edge(ctree, row,
3493                                 GTK_CMCLIST(summaryview->ctree)->focus_row);
3494                 }
3495         }
3496
3497         if (val == 0 && MSG_IS_UNREAD(msginfo->flags)) {
3498                 if (!prefs_common.mark_as_read_on_new_window &&
3499                     prefs_common.mark_as_read_delay) {
3500                         MarkAsReadData *data = g_new0(MarkAsReadData, 1);
3501                         data->summaryview = summaryview;
3502                         data->msginfo = msginfo;
3503 #if GLIB_CHECK_VERSION(2,14,0)
3504                         g_timeout_add_seconds(prefs_common.mark_as_read_delay,
3505                                 msginfo_mark_as_read_timeout, data);
3506 #else
3507                         g_timeout_add(prefs_common.mark_as_read_delay * 1000,
3508                                 msginfo_mark_as_read_timeout, data);
3509 #endif
3510                 } else if (new_window || !prefs_common.mark_as_read_on_new_window) {
3511                         msginfo_mark_as_read(summaryview, msginfo, row);
3512                 }
3513         }
3514
3515         summary_set_menu_sensitive(summaryview);
3516         toolbar_main_set_sensitive(summaryview->mainwin);
3517         messageview_set_menu_sensitive(summaryview->messageview);
3518
3519         summary_unlock(summaryview);
3520         END_TIMING();
3521 }
3522
3523 void summary_display_msg_selected(SummaryView *summaryview,
3524                                   gboolean all_headers)
3525 {
3526         if (summary_is_locked(summaryview)) return;
3527         summaryview->displayed = NULL;
3528         summary_display_msg_full(summaryview, summaryview->selected, FALSE,
3529                                  all_headers);
3530 }
3531
3532 void summary_redisplay_msg(SummaryView *summaryview)
3533 {
3534         GtkCMCTreeNode *node;
3535
3536         if (summaryview->displayed) {
3537                 node = summaryview->displayed;
3538                 summaryview->displayed = NULL;
3539                 summary_display_msg(summaryview, node);
3540         }
3541 }
3542
3543 void summary_open_msg(SummaryView *summaryview)
3544 {
3545         if (!summaryview->selected) return;
3546         
3547         /* CLAWS: if separate message view, don't open a new window
3548          * but rather use the current separated message view */
3549         summary_display_msg_full(summaryview, summaryview->selected, 
3550                                  TRUE, FALSE);
3551 }
3552
3553 void summary_view_source(SummaryView * summaryview)
3554 {
3555         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3556         MsgInfo *msginfo;
3557         SourceWindow *srcwin;
3558
3559         if (!summaryview->selected) return;
3560
3561         srcwin = source_window_create();
3562         msginfo = gtk_cmctree_node_get_row_data(ctree, summaryview->selected);
3563         source_window_show_msg(srcwin, msginfo);
3564         source_window_show(srcwin);
3565 #ifdef MAEMO
3566         maemo_window_full_screen_if_needed(GTK_WINDOW(srcwin->window));
3567         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(srcwin->window));
3568 #endif
3569 }
3570
3571 void summary_reedit(SummaryView *summaryview)
3572 {
3573         MsgInfo *msginfo;
3574
3575         if (!summaryview->selected) return;
3576         if (!summaryview->folder_item) return;
3577         if (!FOLDER_SHOWS_TO_HDR(summaryview->folder_item))
3578                 return;
3579
3580         msginfo = gtk_cmctree_node_get_row_data(GTK_CMCTREE(summaryview->ctree),
3581                                               summaryview->selected);
3582         if (!msginfo) return;
3583
3584         compose_reedit(msginfo, FALSE);
3585 }
3586
3587 gboolean summary_step(SummaryView *summaryview, GtkScrollType type)
3588 {
3589         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3590         GtkCMCTreeNode *node;
3591
3592         if (summary_is_locked(summaryview)
3593         && !GTK_SCTREE(ctree)->selecting_range
3594         && summaryview->messageview->mimeview
3595         && summaryview->messageview->mimeview->type == MIMEVIEW_TEXT
3596         && summaryview->messageview->mimeview->textview->loading) {
3597                 PostponedSelectData *data = g_new0(PostponedSelectData, 1);
3598                 summaryview->messageview->mimeview->textview->stop_loading = TRUE;
3599                 
3600                 data->ctree = ctree;
3601                 data->row = NULL;
3602                 data->node = NULL;
3603                 data->type = type;
3604                 data->summaryview = summaryview;
3605                 debug_print("postponing open of message till end of load\n");
3606                 g_timeout_add(100, summary_select_retry, data);
3607                 return FALSE;
3608         }
3609         if (summary_is_locked(summaryview))
3610                 return FALSE;
3611         if (type == GTK_SCROLL_STEP_FORWARD) {
3612                 node = gtkut_ctree_node_next(ctree, summaryview->selected);
3613                 if (node)
3614                         gtkut_ctree_expand_parent_all(ctree, node);
3615                 else
3616                         return FALSE;
3617         } else {
3618                 if (summaryview->selected) {
3619                         node = GTK_CMCTREE_NODE_PREV(summaryview->selected);
3620                         if (!node) return FALSE;
3621                 }
3622         }
3623
3624         if (messageview_is_visible(summaryview->messageview))
3625                 summaryview->display_msg = TRUE;
3626
3627         g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical", type, 0.0);
3628
3629         if (GTK_CMCLIST(ctree)->selection)
3630                 gtk_sctree_set_anchor_row
3631                         (GTK_SCTREE(ctree),
3632                          GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->selection->data));
3633
3634         return TRUE;
3635 }
3636
3637 gboolean summary_is_list(SummaryView *summaryview)
3638 {
3639         return (gtk_notebook_get_current_page(
3640                 GTK_NOTEBOOK(summaryview->mainwidget_book)) == 0);
3641 }
3642
3643 void summary_toggle_view(SummaryView *summaryview)
3644 {
3645         if (prefs_common.layout_mode == SMALL_LAYOUT)
3646                 return;
3647         if (!messageview_is_visible(summaryview->messageview) &&
3648             summaryview->selected && summary_is_list(summaryview))
3649                 summary_display_msg(summaryview,
3650                                     summaryview->selected);
3651         else
3652                 main_window_toggle_message_view(summaryview->mainwin);
3653 }
3654
3655 static gboolean summary_search_unread_recursive(GtkCMCTree *ctree,
3656                                                 GtkCMCTreeNode *node)
3657 {
3658         MsgInfo *msginfo;
3659
3660         if (node) {
3661                 msginfo = gtk_cmctree_node_get_row_data(ctree, node);
3662                 if (msginfo && MSG_IS_UNREAD(msginfo->flags) && !MSG_IS_IGNORE_THREAD(msginfo->flags))
3663                         return TRUE;
3664                 node = GTK_CMCTREE_ROW(node)->children;
3665         } else
3666                 node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
3667
3668         while (node) {
3669                 if (summary_search_unread_recursive(ctree, node) == TRUE)
3670                         return TRUE;
3671                 node = GTK_CMCTREE_ROW(node)->sibling;
3672         }
3673
3674         return FALSE;
3675 }
3676
3677 static gboolean summary_have_unread_children(SummaryView *summaryview,
3678                                              GtkCMCTreeNode *node)
3679 {
3680         GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
3681
3682         if (!node) return FALSE;
3683
3684         node = GTK_CMCTREE_ROW(node)->children;
3685
3686         while (node) {
3687                 if (summary_search_unread_recursive(ctree, node) == TRUE)
3688                         return TRUE;
3689               &nbs