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