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