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