2004-09-28 [colin] 0.9.12cvs112.1
[claws.git] / src / mainwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkmain.h>
25 #include <gtk/gtkwindow.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtksignal.h>
28 #include <gtk/gtkvbox.h>
29 #include <gtk/gtkcontainer.h>
30 #include <gtk/gtkstatusbar.h>
31 #include <gtk/gtkprogressbar.h>
32 #include <gtk/gtkhpaned.h>
33 #include <gtk/gtkvpaned.h>
34 #include <gtk/gtkcheckmenuitem.h>
35 #include <gtk/gtkitemfactory.h>
36 #include <gtk/gtkeditable.h>
37 #include <gtk/gtkmenu.h>
38 #include <gtk/gtkmenuitem.h>
39 #include <gtk/gtkhandlebox.h>
40 #include <gtk/gtktoolbar.h>
41 #include <gtk/gtkbutton.h>
42 #include <gtk/gtktooltips.h>
43 #include <string.h>
44
45 #include "intl.h"
46 #include "main.h"
47 #include "mainwindow.h"
48 #include "folderview.h"
49 #include "foldersel.h"
50 #include "summaryview.h"
51 #include "summary_search.h"
52 #include "messageview.h"
53 #include "message_search.h"
54 #include "headerview.h"
55 #include "menu.h"
56 #include "stock_pixmap.h"
57 #include "folder.h"
58 #include "inc.h"
59 #include "compose.h"
60 #include "procmsg.h"
61 #include "import.h"
62 #include "export.h"
63 #include "prefs_common.h"
64 #include "prefs_actions.h"
65 #include "prefs_filtering.h"
66 #include "prefs_account.h"
67 #include "prefs_summary_column.h"
68 #include "prefs_template.h"
69 #include "action.h"
70 #include "account.h"
71 #include "addressbook.h"
72 #include "logwindow.h"
73 #include "manage_window.h"
74 #include "alertpanel.h"
75 #include "statusbar.h"
76 #include "inputdialog.h"
77 #include "utils.h"
78 #include "gtkutils.h"
79 #include "codeconv.h"
80 #include "about.h"
81 #include "manual.h"
82 #include "version.h"
83 #include "ssl_manager.h"
84 #include "sslcertwindow.h"
85 #include "prefs_gtk.h"
86 #include "pluginwindow.h"
87 #include "hooks.h"
88 #include "progressindicator.h"
89 #include "localfolder.h"
90 #include "filtering.h"
91 #include "folderutils.h"
92 #include "foldersort.h"
93
94 #define AC_LABEL_WIDTH  240
95
96 /* list of all instantiated MainWindow */
97 static GList *mainwin_list = NULL;
98
99 static GdkCursor *watch_cursor;
100
101 static void main_window_menu_callback_block     (MainWindow     *mainwin);
102 static void main_window_menu_callback_unblock   (MainWindow     *mainwin);
103
104 static void main_window_show_cur_account        (MainWindow     *mainwin);
105
106 static void main_window_set_widgets             (MainWindow     *mainwin,
107                                                  SeparateType    type);
108
109 #if 0
110 static gboolean toolbar_account_button_pressed  (GtkWidget      *widget,
111                                                  GdkEventButton *event,
112                                                  gpointer        data);
113 #endif
114
115 static void toolbar_child_attached              (GtkWidget      *widget,
116                                                  GtkWidget      *child,
117                                                  gpointer        data);
118 static void toolbar_child_detached              (GtkWidget      *widget,
119                                                  GtkWidget      *child,
120                                                  gpointer        data);
121
122 static gboolean ac_label_button_pressed         (GtkWidget      *widget,
123                                                  GdkEventButton *event,
124                                                  gpointer        data);
125 static void ac_menu_popup_closed                (GtkMenuShell   *menu_shell,
126                                                  gpointer        data);
127
128 static gint main_window_close_cb                (GtkWidget      *widget,
129                                                  GdkEventAny    *event,
130                                                  gpointer        data);
131 static gint folder_window_close_cb              (GtkWidget      *widget,
132                                                  GdkEventAny    *event,
133                                                  gpointer        data);
134 static gint message_window_close_cb             (GtkWidget      *widget,
135                                                  GdkEventAny    *event,
136                                                  gpointer        data);
137
138 static void main_window_size_allocate_cb        (GtkWidget      *widget,
139                                                  GtkAllocation  *allocation,
140                                                  gpointer        data);
141 static void folder_window_size_allocate_cb      (GtkWidget      *widget,
142                                                  GtkAllocation  *allocation,
143                                                  gpointer        data);
144 static void message_window_size_allocate_cb     (GtkWidget      *widget,
145                                                  GtkAllocation  *allocation,
146                                                  gpointer        data);
147
148 static void update_folderview_cb (MainWindow    *mainwin,
149                                   guint          action,
150                                   GtkWidget     *widget);
151 static void add_mailbox_cb       (MainWindow    *mainwin,
152                                   guint          action,
153                                   GtkWidget     *widget);
154 static void foldersort_cb        (MainWindow    *mainwin,
155                                   guint          action,
156                                   GtkWidget     *widget);
157 static void import_mbox_cb       (MainWindow    *mainwin,
158                                   guint          action,
159                                   GtkWidget     *widget);
160 static void export_mbox_cb       (MainWindow    *mainwin,
161                                   guint          action,
162                                   GtkWidget     *widget);
163 static void empty_trash_cb       (MainWindow    *mainwin,
164                                   guint          action,
165                                   GtkWidget     *widget);
166
167 static void save_as_cb           (MainWindow    *mainwin,
168                                   guint          action,
169                                   GtkWidget     *widget);
170 static void print_cb             (MainWindow    *mainwin,
171                                   guint          action,
172                                   GtkWidget     *widget);
173 static void app_exit_cb          (MainWindow    *mainwin,
174                                   guint          action,
175                                   GtkWidget     *widget);
176
177 static void search_cb            (MainWindow    *mainwin,
178                                   guint          action,
179                                   GtkWidget     *widget);
180
181 static void toggle_folder_cb     (MainWindow    *mainwin,
182                                   guint          action,
183                                   GtkWidget     *widget);
184 static void toggle_message_cb    (MainWindow    *mainwin,
185                                   guint          action,
186                                   GtkWidget     *widget);
187 static void toggle_toolbar_cb    (MainWindow    *mainwin,
188                                   guint          action,
189                                   GtkWidget     *widget);
190 static void toggle_statusbar_cb  (MainWindow    *mainwin,
191                                   guint          action,
192                                   GtkWidget     *widget);
193 static void separate_widget_cb   (MainWindow    *mainwin,
194                                   guint          action,
195                                   GtkWidget     *widget);
196
197 static void addressbook_open_cb (MainWindow     *mainwin,
198                                  guint           action,
199                                  GtkWidget      *widget);
200 static void log_window_show_cb  (MainWindow     *mainwin,
201                                  guint           action,
202                                  GtkWidget      *widget);
203
204 static void inc_cancel_cb               (MainWindow     *mainwin,
205                                          guint           action,
206                                          GtkWidget      *widget);
207
208 static void open_msg_cb                 (MainWindow     *mainwin,
209                                          guint           action,
210                                          GtkWidget      *widget);
211
212 static void view_source_cb              (MainWindow     *mainwin,
213                                          guint           action,
214                                          GtkWidget      *widget);
215
216 static void show_all_header_cb          (MainWindow     *mainwin,
217                                          guint           action,
218                                          GtkWidget      *widget);
219
220 static void move_to_cb                  (MainWindow     *mainwin,
221                                          guint           action,
222                                          GtkWidget      *widget);
223 static void copy_to_cb                  (MainWindow     *mainwin,
224                                          guint           action,
225                                          GtkWidget      *widget);
226 static void delete_cb                   (MainWindow     *mainwin,
227                                          guint           action,
228                                          GtkWidget      *widget);
229
230 static void cancel_cb                   (MainWindow     *mainwin,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void mark_cb                     (MainWindow     *mainwin,
235                                          guint           action,
236                                          GtkWidget      *widget);
237 static void unmark_cb                   (MainWindow     *mainwin,
238                                          guint           action,
239                                          GtkWidget      *widget);
240
241 static void mark_as_unread_cb           (MainWindow     *mainwin,
242                                          guint           action,
243                                          GtkWidget      *widget);
244 static void mark_as_read_cb             (MainWindow     *mainwin,
245                                          guint           action,
246                                          GtkWidget      *widget);
247 static void mark_all_read_cb            (MainWindow     *mainwin,
248                                          guint           action,
249                                          GtkWidget      *widget);
250
251 static void reedit_cb                   (MainWindow     *mainwin,
252                                          guint           action,
253                                          GtkWidget      *widget);
254
255 static void add_address_cb              (MainWindow     *mainwin,
256                                          guint           action,
257                                          GtkWidget      *widget);
258
259 static void set_charset_cb              (MainWindow     *mainwin,
260                                          guint           action,
261                                          GtkWidget      *widget);
262
263 static void hide_read_messages   (MainWindow    *mainwin,
264                                   guint          action,
265                                   GtkWidget     *widget);
266
267 static void thread_cb            (MainWindow    *mainwin,
268                                   guint          action,
269                                   GtkWidget     *widget);
270 static void expand_threads_cb    (MainWindow    *mainwin,
271                                   guint          action,
272                                   GtkWidget     *widget);
273 static void collapse_threads_cb  (MainWindow    *mainwin,
274                                   guint          action,
275                                   GtkWidget     *widget);
276
277 static void set_display_item_cb  (MainWindow    *mainwin,
278                                   guint          action,
279                                   GtkWidget     *widget);
280 static void sort_summary_cb      (MainWindow    *mainwin,
281                                   guint          action,
282                                   GtkWidget     *widget);
283 static void sort_summary_type_cb (MainWindow    *mainwin,
284                                   guint          action,
285                                   GtkWidget     *widget);
286 static void attract_by_subject_cb(MainWindow    *mainwin,
287                                   guint          action,
288                                   GtkWidget     *widget);
289
290 static void delete_duplicated_cb (MainWindow    *mainwin,
291                                   guint          action,
292                                   GtkWidget     *widget);
293 static void delete_duplicated_all_cb (MainWindow        *mainwin,
294                                   guint          action,
295                                   GtkWidget     *widget);
296 static void filter_cb            (MainWindow    *mainwin,
297                                   guint          action,
298                                   GtkWidget     *widget);
299 static void execute_summary_cb   (MainWindow    *mainwin,
300                                   guint          action,
301                                   GtkWidget     *widget);
302 static void update_summary_cb    (MainWindow    *mainwin,
303                                   guint          action,
304                                   GtkWidget     *widget);
305
306 static void prev_cb              (MainWindow    *mainwin,
307                                   guint          action,
308                                   GtkWidget     *widget);
309 static void next_cb              (MainWindow    *mainwin,
310                                   guint          action,
311                                   GtkWidget     *widget);
312 static void next_unread_cb       (MainWindow    *mainwin,
313                                   guint          action,
314                                   GtkWidget     *widget);
315 static void prev_unread_cb       (MainWindow    *mainwin,
316                                   guint          action,
317                                   GtkWidget     *widget);
318
319 static void prev_new_cb          (MainWindow    *mainwin,
320                                   guint          action,
321                                   GtkWidget     *widget);
322 static void next_new_cb          (MainWindow    *mainwin,
323                                   guint          action,
324                                   GtkWidget     *widget);
325 static void prev_marked_cb       (MainWindow    *mainwin,
326                                   guint          action,
327                                   GtkWidget     *widget);
328 static void next_marked_cb       (MainWindow    *mainwin,
329                                   guint          action,
330                                   GtkWidget     *widget);
331 static void prev_labeled_cb      (MainWindow    *mainwin,
332                                   guint          action,
333                                   GtkWidget     *widget);
334 static void next_labeled_cb      (MainWindow    *mainwin,
335                                   guint          action,
336                                   GtkWidget     *widget);
337
338 static void goto_folder_cb       (MainWindow    *mainwin,
339                                   guint          action,
340                                   GtkWidget     *widget);
341
342 static void copy_cb              (MainWindow    *mainwin,
343                                   guint          action,
344                                   GtkWidget     *widget);
345 static void allsel_cb            (MainWindow    *mainwin,
346                                   guint          action,
347                                   GtkWidget     *widget);
348 static void select_thread_cb     (MainWindow    *mainwin,
349                                   guint          action,
350                                   GtkWidget     *widget);
351
352 static void create_filter_cb     (MainWindow    *mainwin,
353                                   guint          action,
354                                   GtkWidget     *widget);
355 static void create_processing_cb (MainWindow    *mainwin,
356                                   guint          action,
357                                   GtkWidget     *widget);
358
359 static void prefs_common_open_cb        (MainWindow     *mainwin,
360                                          guint           action,
361                                          GtkWidget      *widget);
362 static void prefs_template_open_cb      (MainWindow     *mainwin,
363                                          guint           action,
364                                          GtkWidget      *widget);
365 static void prefs_actions_open_cb       (MainWindow     *mainwin,
366                                          guint           action,
367                                          GtkWidget      *widget);
368 static void prefs_account_open_cb       (MainWindow     *mainwin,
369                                          guint           action,
370                                          GtkWidget      *widget);
371
372 static void prefs_pre_processing_open_cb  (MainWindow   *mainwin,
373                                            guint         action,
374                                            GtkWidget    *widget);
375
376 static void prefs_post_processing_open_cb (MainWindow   *mainwin,
377                                            guint         action,
378                                            GtkWidget    *widget);
379
380 static void prefs_filtering_open_cb     (MainWindow     *mainwin,
381                                          guint           action,
382                                          GtkWidget      *widget);
383 #ifdef USE_OPENSSL
384 static void ssl_manager_open_cb         (MainWindow     *mainwin,
385                                          guint           action,
386                                          GtkWidget      *widget);
387 #endif
388 static void new_account_cb       (MainWindow    *mainwin,
389                                   guint          action,
390                                   GtkWidget     *widget);
391
392 static void account_selector_menu_cb     (GtkMenuItem   *menuitem,
393                                           gpointer       data);
394 static void account_receive_menu_cb      (GtkMenuItem   *menuitem,
395                                           gpointer       data);
396
397 static void prefs_open_cb       (GtkMenuItem    *menuitem,
398                                  gpointer        data);
399 static void plugins_open_cb     (GtkMenuItem    *menuitem,
400                                  gpointer        data);
401
402 static void online_switch_clicked(GtkButton     *btn, 
403                                   gpointer data);
404
405 static void manual_open_cb       (MainWindow    *mainwin,
406                                   guint          action,
407                                   GtkWidget     *widget);
408
409 static void scan_tree_func       (Folder        *folder,
410                                   FolderItem    *item,
411                                   gpointer       data);
412                                   
413 static void toggle_work_offline_cb(MainWindow *mainwin, guint action, GtkWidget *widget);
414
415 static void addr_harvest_cb      ( MainWindow  *mainwin,
416                                    guint       action,
417                                    GtkWidget   *widget );
418
419 static void addr_harvest_msg_cb  ( MainWindow  *mainwin,
420                                    guint       action,
421                                    GtkWidget   *widget );
422
423 static gboolean mainwindow_focus_in_event       (GtkWidget      *widget, 
424                                                  GdkEventFocus  *focus,
425                                                  gpointer        data);
426 void main_window_reply_cb                       (MainWindow     *mainwin, 
427                                                  guint           action,
428                                                  GtkWidget      *widget);
429 gboolean mainwindow_progressindicator_hook      (gpointer        source,
430                                                  gpointer        userdata);
431 #define  SEPARATE_ACTION 500 
432
433 static GtkItemFactoryEntry mainwin_entries[] =
434 {
435         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
436         {N_("/_File/_Add mailbox"),             NULL, NULL, 0, "<Branch>"},
437         {N_("/_File/_Add mailbox/MH..."),       NULL, add_mailbox_cb, 0, NULL},
438         {N_("/_File/Change folder order"),      NULL, foldersort_cb,  0, NULL},
439         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
440         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
441         {N_("/_File/Empty all _Trash folders"), "<shift>D", empty_trash_cb, 0, NULL},
442         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
443         {N_("/_File/_Save as..."),              "<control>S", save_as_cb, 0, NULL},
444         {N_("/_File/_Print..."),                NULL, print_cb, 0, NULL},
445         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
446         {N_("/_File/_Work offline"),            "<control>W", toggle_work_offline_cb, 0, "<ToggleItem>"},
447         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
448         /* {N_("/_File/_Close"),                "<alt>W", app_exit_cb, 0, NULL}, */
449         {N_("/_File/E_xit"),                    "<control>Q", app_exit_cb, 0, NULL},
450
451         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
452         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
453         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
454         {N_("/_Edit/Select _thread"),           NULL, select_thread_cb, 0, NULL},
455         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
456         {N_("/_Edit/_Find in current message..."),
457                                                 "<control>F", search_cb, 0, NULL},
458         {N_("/_Edit/_Search folder..."),        "<shift><control>F", search_cb, 1, NULL},
459         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
460         {N_("/_View/Show or hi_de"),            NULL, NULL, 0, "<Branch>"},
461         {N_("/_View/Show or hi_de/_Folder tree"),
462                                                 NULL, toggle_folder_cb, 0, "<ToggleItem>"},
463         {N_("/_View/Show or hi_de/_Message view"),
464                                                 "V", toggle_message_cb, 0, "<ToggleItem>"},
465         {N_("/_View/Show or hi_de/_Toolbar"),
466                                                 NULL, NULL, 0, "<Branch>"},
467         {N_("/_View/Show or hi_de/_Toolbar/Icon _and text"),
468                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
469         {N_("/_View/Show or hi_de/_Toolbar/_Icon"),
470                                                 NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Show or hide/Toolbar/Icon and text"},
471         {N_("/_View/Show or hi_de/_Toolbar/_Text"),
472                                                 NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Show or hide/Toolbar/Icon and text"},
473         {N_("/_View/Show or hi_de/_Toolbar/_None"),
474                                                 NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Show or hide/Toolbar/Icon and text"},
475         {N_("/_View/Show or hi_de/Status _bar"),
476                                                 NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
477         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
478         {N_("/_View/Separate f_older tree"),    NULL, separate_widget_cb, SEPARATE_FOLDER, "<ToggleItem>"},
479         {N_("/_View/Separate m_essage view"),   NULL, separate_widget_cb, SEPARATE_MESSAGE, "<ToggleItem>"},
480         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
481         {N_("/_View/_Sort"),                    NULL, NULL, 0, "<Branch>"},
482         {N_("/_View/_Sort/by _number"),         NULL, sort_summary_cb, SORT_BY_NUMBER, "<RadioItem>"},
483         {N_("/_View/_Sort/by s_ize"),           NULL, sort_summary_cb, SORT_BY_SIZE, "/View/Sort/by number"},
484         {N_("/_View/_Sort/by _date"),           NULL, sort_summary_cb, SORT_BY_DATE, "/View/Sort/by number"},
485         {N_("/_View/_Sort/by _from"),           NULL, sort_summary_cb, SORT_BY_FROM, "/View/Sort/by number"},
486         {N_("/_View/_Sort/by _recipient"),      NULL, sort_summary_cb, SORT_BY_TO, "/View/Sort/by number"},
487         {N_("/_View/_Sort/by _subject"),        NULL, sort_summary_cb, SORT_BY_SUBJECT, "/View/Sort/by number"},
488         {N_("/_View/_Sort/by _color label"),
489                                                 NULL, sort_summary_cb, SORT_BY_LABEL, "/View/Sort/by number"},
490         {N_("/_View/_Sort/by _mark"),           NULL, sort_summary_cb, SORT_BY_MARK, "/View/Sort/by number"},
491         {N_("/_View/_Sort/by _status"),         NULL, sort_summary_cb, SORT_BY_STATUS, "/View/Sort/by number"},
492         {N_("/_View/_Sort/by a_ttachment"),
493                                                 NULL, sort_summary_cb, SORT_BY_MIME, "/View/Sort/by number"},
494         {N_("/_View/_Sort/by score"),           NULL, sort_summary_cb, SORT_BY_SCORE, "/View/Sort/by number"},
495         {N_("/_View/_Sort/by locked"),          NULL, sort_summary_cb, SORT_BY_LOCKED, "/View/Sort/by number"},
496         {N_("/_View/_Sort/D_on't sort"),        NULL, sort_summary_cb, SORT_BY_NONE, "/View/Sort/by number"},
497         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
498         {N_("/_View/_Sort/Ascending"),          NULL, sort_summary_type_cb, SORT_ASCENDING, "<RadioItem>"},
499         {N_("/_View/_Sort/Descending"),         NULL, sort_summary_type_cb, SORT_DESCENDING, "/View/Sort/Ascending"},
500         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
501         {N_("/_View/_Sort/_Attract by subject"),
502                                                 NULL, attract_by_subject_cb, 0, NULL},
503         {N_("/_View/Th_read view"),             "<control>T", thread_cb, 0, "<ToggleItem>"},
504         {N_("/_View/E_xpand all threads"),      NULL, expand_threads_cb, 0, NULL},
505         {N_("/_View/Co_llapse all threads"),    NULL, collapse_threads_cb, 0, NULL},
506         {N_("/_View/_Hide read messages"),      NULL, hide_read_messages, 0, "<ToggleItem>"},
507         {N_("/_View/Set displayed _items..."),  NULL, set_display_item_cb, 0, NULL},
508
509         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
510         {N_("/_View/_Go to"),                   NULL, NULL, 0, "<Branch>"},
511         {N_("/_View/_Go to/_Prev message"),     "P", prev_cb, 0, NULL},
512         {N_("/_View/_Go to/_Next message"),     "N", next_cb, 0, NULL},
513         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
514         {N_("/_View/_Go to/P_rev unread message"),
515                                                 "<shift>P", prev_unread_cb, 0, NULL},
516         {N_("/_View/_Go to/N_ext unread message"),
517                                                 "<shift>N", next_unread_cb, 0, NULL},
518         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
519         {N_("/_View/_Go to/Prev ne_w message"), NULL, prev_new_cb, 0, NULL},
520         {N_("/_View/_Go to/Ne_xt new message"), NULL, next_new_cb, 0, NULL},
521         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
522         {N_("/_View/_Go to/Prev _marked message"),
523                                                 NULL, prev_marked_cb, 0, NULL},
524         {N_("/_View/_Go to/Next m_arked message"),
525                                                 NULL, next_marked_cb, 0, NULL},
526         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
527         {N_("/_View/_Go to/Prev _labeled message"),
528                                                 NULL, prev_labeled_cb, 0, NULL},
529         {N_("/_View/_Go to/Next la_beled message"),
530                                                 NULL, next_labeled_cb, 0, NULL},
531         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
532         {N_("/_View/_Go to/Other _folder..."),  "G", goto_folder_cb, 0, NULL},
533         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
534
535 #define CODESET_SEPARATOR \
536         {N_("/_View/_Code set/---"),            NULL, NULL, 0, "<Separator>"}
537 #define CODESET_ACTION(action) \
538          NULL, set_charset_cb, action, "/View/Code set/Auto detect"
539
540         {N_("/_View/_Code set"),                NULL, NULL, 0, "<Branch>"},
541         {N_("/_View/_Code set/_Auto detect"),
542          NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
543         {N_("/_View/_Code set/---"),            NULL, NULL, 0, "<Separator>"},
544         {N_("/_View/_Code set/7bit ascii (US-ASC_II)"),
545          CODESET_ACTION(C_US_ASCII)},
546
547 #if HAVE_ICONV
548         {N_("/_View/_Code set/Unicode (_UTF-8)"),
549          CODESET_ACTION(C_UTF_8)},
550         CODESET_SEPARATOR,
551 #endif
552         {N_("/_View/_Code set/Western European (ISO-8859-_1)"),
553          CODESET_ACTION(C_ISO_8859_1)},
554         {N_("/_View/_Code set/Western European (ISO-8859-15)"),
555          CODESET_ACTION(C_ISO_8859_15)},
556         CODESET_SEPARATOR,
557 #if HAVE_ICONV
558         {N_("/_View/_Code set/Central European (ISO-8859-_2)"),
559          CODESET_ACTION(C_ISO_8859_2)},
560         CODESET_SEPARATOR,
561         {N_("/_View/_Code set/_Baltic (ISO-8859-13)"),
562          CODESET_ACTION(C_ISO_8859_13)},
563         {N_("/_View/_Code set/Baltic (ISO-8859-_4)"),
564          CODESET_ACTION(C_ISO_8859_4)},
565         CODESET_SEPARATOR,
566         {N_("/_View/_Code set/Greek (ISO-8859-_7)"),
567          CODESET_ACTION(C_ISO_8859_7)},
568         CODESET_SEPARATOR,
569         {N_("/_View/_Code set/Turkish (ISO-8859-_9)"),
570          CODESET_ACTION(C_ISO_8859_9)},
571         CODESET_SEPARATOR,
572         {N_("/_View/_Code set/Cyrillic (ISO-8859-_5)"),
573          CODESET_ACTION(C_ISO_8859_5)},
574         {N_("/_View/_Code set/Cyrillic (KOI8-_R)"),
575          CODESET_ACTION(C_KOI8_R)},
576         {N_("/_View/_Code set/Cyrillic (KOI8-U)"),
577          CODESET_ACTION(C_KOI8_U)},
578         {N_("/_View/_Code set/Cyrillic (Windows-1251)"),
579          CODESET_ACTION(C_WINDOWS_1251)},
580         CODESET_SEPARATOR,
581 #endif
582         {N_("/_View/_Code set/Japanese (ISO-2022-_JP)"),
583          CODESET_ACTION(C_ISO_2022_JP)},
584 #if HAVE_ICONV
585         {N_("/_View/_Code set/Japanese (ISO-2022-JP-2)"),
586          CODESET_ACTION(C_ISO_2022_JP_2)},
587 #endif
588         {N_("/_View/_Code set/Japanese (_EUC-JP)"),
589          CODESET_ACTION(C_EUC_JP)},
590         {N_("/_View/_Code set/Japanese (_Shift__JIS)"),
591          CODESET_ACTION(C_SHIFT_JIS)},
592 #if HAVE_ICONV
593         CODESET_SEPARATOR,
594         {N_("/_View/_Code set/Simplified Chinese (_GB2312)"),
595          CODESET_ACTION(C_GB2312)},
596         {N_("/_View/_Code set/Traditional Chinese (_Big5)"),
597          CODESET_ACTION(C_BIG5)},
598         {N_("/_View/_Code set/Traditional Chinese (EUC-_TW)"),
599          CODESET_ACTION(C_EUC_TW)},
600         {N_("/_View/_Code set/Chinese (ISO-2022-_CN)"),
601          CODESET_ACTION(C_ISO_2022_CN)},
602         CODESET_SEPARATOR,
603         {N_("/_View/_Code set/Korean (EUC-_KR)"),
604          CODESET_ACTION(C_EUC_KR)},
605         {N_("/_View/_Code set/Korean (ISO-2022-KR)"),
606          CODESET_ACTION(C_ISO_2022_KR)},
607         CODESET_SEPARATOR,
608         {N_("/_View/_Code set/Thai (TIS-620)"),
609          CODESET_ACTION(C_TIS_620)},
610         {N_("/_View/_Code set/Thai (Windows-874)"),
611          CODESET_ACTION(C_WINDOWS_874)},
612 #endif
613
614 #undef CODESET_SEPARATOR
615 #undef CODESET_ACTION
616
617         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
618         {N_("/_View/Open in new _window"),      "<control><alt>N", open_msg_cb, 0, NULL},
619         {N_("/_View/Mess_age source"),          "<control>U", view_source_cb, 0, NULL},
620         {N_("/_View/Show all _headers"),        "<control>H", show_all_header_cb, 0, "<ToggleItem>"},
621         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
622         {N_("/_View/_Update summary"),          "<control><alt>U", update_summary_cb,  0, NULL},
623
624         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
625         {N_("/_Message/Recei_ve"),              NULL, NULL, 0, "<Branch>"},
626         {N_("/_Message/Recei_ve/Get from _current account"),
627                                                 "<control>I",   inc_mail_cb, 0, NULL},
628         {N_("/_Message/Recei_ve/Get from _all accounts"),
629                                                 "<shift><control>I", inc_all_account_mail_cb, 0, NULL},
630         {N_("/_Message/Recei_ve/Cancel receivin_g"),
631                                                 NULL, inc_cancel_cb, 0, NULL},
632         {N_("/_Message/Recei_ve/---"),          NULL, NULL, 0, "<Separator>"},
633         {N_("/_Message/_Send queued messages"), NULL, send_queue_cb, 0, NULL},
634         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
635         {N_("/_Message/Compose a_n email message"),     "<control>M", compose_mail_cb, 0, NULL},
636         {N_("/_Message/Compose a news message"),        NULL,   compose_news_cb, 0, NULL},
637         {N_("/_Message/_Reply"),                "<control>R",   main_window_reply_cb, COMPOSE_REPLY, NULL},
638         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
639         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", main_window_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
640         {N_("/_Message/Repl_y to/_sender"),     NULL, main_window_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
641         {N_("/_Message/Repl_y to/mailing _list"),
642                                                 "<control>L", main_window_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
643         {N_("/_Message/Follow-up and reply to"),NULL, main_window_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
644         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
645         {N_("/_Message/_Forward"),              "<control><alt>F", main_window_reply_cb, COMPOSE_FORWARD, NULL},
646         {N_("/_Message/Redirect"),              NULL, main_window_reply_cb, COMPOSE_REDIRECT, NULL},
647         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
648         {N_("/_Message/M_ove..."),              "<control>O", move_to_cb, 0, NULL},
649         {N_("/_Message/_Copy..."),              "<shift><control>O", copy_to_cb, 0, NULL},
650         {N_("/_Message/_Delete"),               "<control>D", delete_cb,  0, NULL},
651         {N_("/_Message/Cancel a news message"), "", cancel_cb,  0, NULL},
652         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
653         {N_("/_Message/_Mark"),                 NULL, NULL, 0, "<Branch>"},
654         {N_("/_Message/_Mark/_Mark"),           "<shift>asterisk", mark_cb, 0, NULL},
655         {N_("/_Message/_Mark/_Unmark"),         "U", unmark_cb, 0, NULL},
656         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
657         {N_("/_Message/_Mark/Mark as unr_ead"), "<shift>exclam", mark_as_unread_cb, 0, NULL},
658         {N_("/_Message/_Mark/Mark as rea_d"),
659                                                 NULL, mark_as_read_cb, 0, NULL},
660         {N_("/_Message/_Mark/Mark all _read"),  NULL, mark_all_read_cb, 0, NULL},
661         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
662         {N_("/_Message/Re-_edit"),              NULL, reedit_cb, 0, NULL},
663
664         {N_("/_Tools"),                         NULL, NULL, 0, "<Branch>"},
665         {N_("/_Tools/_Address book..."),        "<shift><control>A", addressbook_open_cb, 0, NULL},
666         {N_("/_Tools/Add sender to address boo_k"),
667                                                 NULL, add_address_cb, 0, NULL},
668         {N_("/_Tools/_Harvest addresses"),      NULL, NULL, 0, "<Branch>"},
669         {N_("/_Tools/_Harvest addresses/from _Folder..."),
670                                                 NULL, addr_harvest_cb, 0, NULL},
671         {N_("/_Tools/_Harvest addresses/from _Messages..."),
672                                                 NULL, addr_harvest_msg_cb, 0, NULL},
673         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
674         {N_("/_Tools/_Filter all messages in folder"),
675                                                 NULL, filter_cb, 0, NULL},
676         {N_("/_Tools/Filter _selected messages"),
677                                                 NULL, filter_cb, 1, NULL},
678         {N_("/_Tools/_Create filter rule"),     NULL, NULL, 0, "<Branch>"},
679         {N_("/_Tools/_Create filter rule/_Automatically"),
680                                                 NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
681         {N_("/_Tools/_Create filter rule/by _From"),
682                                                 NULL, create_filter_cb, FILTER_BY_FROM, NULL},
683         {N_("/_Tools/_Create filter rule/by _To"),
684                                                 NULL, create_filter_cb, FILTER_BY_TO, NULL},
685         {N_("/_Tools/_Create filter rule/by _Subject"),
686                                                 NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
687         {N_("/_Tools/_Create processing rule"), NULL, NULL, 0, "<Branch>"},
688         {N_("/_Tools/_Create processing rule/_Automatically"),
689                                                 NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
690         {N_("/_Tools/_Create processing rule/by _From"),
691                                                 NULL, create_processing_cb, FILTER_BY_FROM, NULL},
692         {N_("/_Tools/_Create processing rule/by _To"),
693                                                 NULL, create_processing_cb, FILTER_BY_TO, NULL},
694         {N_("/_Tools/_Create processing rule/by _Subject"),
695                                                 NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
696         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
697         {N_("/_Tools/Actio_ns"),                NULL, NULL, 0, "<Branch>"},
698         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
699         {N_("/_Tools/_Check for new messages in all folders"),
700                                                 NULL, update_folderview_cb, 0, NULL},
701         {N_("/_Tools/Delete du_plicated messages"),
702                                                 NULL, NULL, 0, "<Branch>"},
703         {N_("/_Tools/Delete du_plicated messages/In selected folder"),
704                                                 NULL, delete_duplicated_cb,   0, NULL},
705         {N_("/_Tools/Delete du_plicated messages/In all folders"),
706                                                 NULL, delete_duplicated_all_cb,   0, NULL},
707         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
708         {N_("/_Tools/E_xecute"),                "X", execute_summary_cb, 0, NULL},
709 #ifdef USE_OPENSSL
710         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
711         {N_("/_Tools/SSL certi_ficates..."),    
712                                                 NULL, ssl_manager_open_cb, 0, NULL},
713 #endif
714         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
715         {N_("/_Tools/_Log window"),             "<shift><control>L", log_window_show_cb, 0, NULL},
716
717         {N_("/_Configuration"),                 NULL, NULL, 0, "<Branch>"},
718         {N_("/_Configuration/C_hange current account"),
719                                                 NULL, NULL, 0, "<Branch>"},
720         {N_("/_Configuration/_Preferences for current account..."),
721                                                 NULL, prefs_account_open_cb, 0, NULL},
722         {N_("/_Configuration/Create _new account..."),
723                                                 NULL, new_account_cb, 0, NULL},
724         {N_("/_Configuration/_Edit accounts..."),
725                                                 NULL, account_edit_open, 0, NULL},
726         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
727         {N_("/_Configuration/_Common preferences..."),
728                                                 NULL, prefs_common_open_cb, 0, NULL},
729         {N_("/_Configuration/Pre-processing..."),
730                                                 NULL, prefs_pre_processing_open_cb, 0, NULL},
731         {N_("/_Configuration/Post-processing..."),
732                                                 NULL, prefs_post_processing_open_cb, 0, NULL},
733         {N_("/_Configuration/_Filtering..."),
734                                                 NULL, prefs_filtering_open_cb, 0, NULL},
735         {N_("/_Configuration/_Templates..."),   NULL, prefs_template_open_cb, 0, NULL},
736         {N_("/_Configuration/_Actions..."),     NULL, prefs_actions_open_cb, 0, NULL},
737         {N_("/_Configuration/_Other Preferences..."),  NULL, prefs_open_cb, 0, NULL},
738         {N_("/_Configuration/Plugins..."),      NULL, plugins_open_cb, 0, NULL},
739
740         {N_("/_Help"),                          NULL, NULL, 0, "<Branch>"},
741         {N_("/_Help/_Manual (Local)"),          NULL, manual_open_cb, MANUAL_MANUAL_LOCAL, NULL},
742         {N_("/_Help/_Manual (Sylpheed Doc Homepage)"),
743                                                 NULL, manual_open_cb, MANUAL_MANUAL_SYLDOC, NULL},
744         {N_("/_Help/_FAQ (Local)"),             NULL, manual_open_cb, MANUAL_FAQ_LOCAL, NULL},
745         {N_("/_Help/_FAQ (Sylpheed Doc Homepage)"),
746                                                 NULL, manual_open_cb, MANUAL_FAQ_SYLDOC, NULL},
747         {N_("/_Help/_Claws FAQ (Claws Documentation)"),
748                                                 NULL, manual_open_cb, MANUAL_FAQ_CLAWS, NULL},
749         {N_("/_Help/---"),                      NULL, NULL, 0, "<Separator>"},
750         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
751 };
752
753 static gboolean main_window_accel_activate (GtkAccelGroup *accelgroup,
754                                             GObject *arg1,
755                                             guint value,
756                                             GdkModifierType mod,
757                                             gpointer user_data) 
758 {
759         MainWindow *mainwin = (MainWindow *)user_data;
760
761         if (mainwin->summaryview &&
762             mainwin->summaryview->quicksearch &&
763             quicksearch_has_focus(mainwin->summaryview->quicksearch) &&
764             (mod == 0 || mod == GDK_SHIFT_MASK)) {
765                 quicksearch_pass_key(mainwin->summaryview->quicksearch, value, mod);
766                 return TRUE;
767         }
768         return FALSE;
769 }
770
771 MainWindow *main_window_create(SeparateType type)
772 {
773         MainWindow *mainwin;
774         GtkWidget *window;
775         GtkWidget *vbox;
776         GtkWidget *menubar;
777         GtkWidget *handlebox;
778         GtkWidget *vbox_body;
779         GtkWidget *hbox_stat;
780         GtkWidget *statusbar;
781         GtkWidget *progressbar;
782         GtkWidget *statuslabel;
783         GtkWidget *ac_button;
784         GtkWidget *ac_label;
785         GtkWidget *online_pixmap;
786         GtkWidget *offline_pixmap;
787         GtkWidget *online_switch;
788         GtkWidget *offline_switch;
789         GtkTooltips *offline_tip;
790         GtkTooltips *online_tip;
791         GtkTooltips *sel_ac_tip;
792
793         FolderView *folderview;
794         SummaryView *summaryview;
795         MessageView *messageview;
796         GdkColormap *colormap;
797         GdkColor color[4];
798         gboolean success[4];
799         GtkItemFactory *ifactory;
800         GtkWidget *ac_menu;
801         GtkWidget *menuitem;
802         gint i;
803         guint n_menu_entries;
804         gboolean hide_messageview = FALSE;
805
806         static GdkGeometry geometry;
807
808         debug_print("Creating main window...\n");
809         mainwin = g_new0(MainWindow, 1);
810
811         /* main window */
812         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
813         gtk_window_set_title(GTK_WINDOW(window), PROG_VERSION);
814         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
815
816         if (!geometry.min_height) {
817                 geometry.min_width = 320;
818                 geometry.min_height = 200;
819         }
820         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
821                                       GDK_HINT_MIN_SIZE);
822
823         g_signal_connect(G_OBJECT(window), "delete_event",
824                          G_CALLBACK(main_window_close_cb), mainwin);
825         MANAGE_WINDOW_SIGNALS_CONNECT(window);
826         g_signal_connect(G_OBJECT(window), "focus_in_event",
827                          G_CALLBACK(mainwindow_focus_in_event),
828                          mainwin);
829         g_signal_connect(G_OBJECT(window), "key_press_event",
830                          G_CALLBACK(mainwindow_key_pressed), mainwin);
831
832         gtk_widget_realize(window);
833         gtk_widget_add_events(window, GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK);
834         
835
836         gtkut_widget_set_app_icon(window);
837
838         vbox = gtk_vbox_new(FALSE, 0);
839         gtk_widget_show(vbox);
840         gtk_container_add(GTK_CONTAINER(window), vbox);
841
842         /* menu bar */
843         n_menu_entries = sizeof(mainwin_entries) / sizeof(mainwin_entries[0]);
844         menubar = menubar_create(window, mainwin_entries, 
845                                  n_menu_entries, "<Main>", mainwin);
846         gtk_widget_show(menubar);
847         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
848         ifactory = gtk_item_factory_from_widget(menubar);
849
850         menu_set_sensitive(ifactory, "/Help/Manual (Local)", manual_available(MANUAL_MANUAL_LOCAL));
851         menu_set_sensitive(ifactory, "/Help/FAQ (Local)", manual_available(MANUAL_FAQ_LOCAL));
852
853         handlebox = gtk_handle_box_new();
854         gtk_widget_show(handlebox);
855         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
856         g_signal_connect(G_OBJECT(handlebox), "child_attached",
857                          G_CALLBACK(toolbar_child_attached), mainwin);
858         g_signal_connect(G_OBJECT(handlebox), "child_detached",
859                          G_CALLBACK(toolbar_child_detached), mainwin);
860
861         /* link window to mainwin->window to avoid gdk warnings */
862         mainwin->window       = window;
863         
864         /* create toolbar */
865         mainwin->toolbar = toolbar_create(TOOLBAR_MAIN, 
866                                           handlebox, 
867                                           (gpointer)mainwin);
868
869         /* vbox that contains body */
870         vbox_body = gtk_vbox_new(FALSE, BORDER_WIDTH);
871         gtk_widget_show(vbox_body);
872         gtk_container_set_border_width(GTK_CONTAINER(vbox_body), BORDER_WIDTH);
873         gtk_box_pack_start(GTK_BOX(vbox), vbox_body, TRUE, TRUE, 0);
874
875         hbox_stat = gtk_hbox_new(FALSE, 2);
876         gtk_box_pack_end(GTK_BOX(vbox_body), hbox_stat, FALSE, FALSE, 0);
877
878         statusbar = statusbar_create();
879         gtk_box_pack_start(GTK_BOX(hbox_stat), statusbar, TRUE, TRUE, 0);
880
881         progressbar = gtk_progress_bar_new();
882         gtk_widget_set_size_request(progressbar, 120, 1);
883         gtk_box_pack_start(GTK_BOX(hbox_stat), progressbar, FALSE, FALSE, 0);
884
885         online_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_ONLINE);
886         offline_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_OFFLINE);
887         online_tip = gtk_tooltips_new();
888         online_switch = gtk_button_new ();
889         gtk_tooltips_set_tip(GTK_TOOLTIPS(online_tip),online_switch, 
890                              _("You are online. Click the icon to go offline"), NULL);
891         offline_tip = gtk_tooltips_new();
892         offline_switch = gtk_button_new ();
893         gtk_tooltips_set_tip(GTK_TOOLTIPS(offline_tip),offline_switch, 
894                              _("You are offline. Click the icon to go online"),
895                              NULL);
896         gtk_container_add (GTK_CONTAINER(online_switch), online_pixmap);
897         gtk_button_set_relief (GTK_BUTTON(online_switch), GTK_RELIEF_NONE);
898         g_signal_connect (G_OBJECT(online_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
899         gtk_box_pack_start (GTK_BOX(hbox_stat), online_switch, FALSE, FALSE, 0);
900         gtk_container_add (GTK_CONTAINER(offline_switch), offline_pixmap);
901         gtk_button_set_relief (GTK_BUTTON(offline_switch), GTK_RELIEF_NONE);
902         g_signal_connect (G_OBJECT(offline_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
903         gtk_box_pack_start (GTK_BOX(hbox_stat), offline_switch, FALSE, FALSE, 0);
904         
905         statuslabel = gtk_label_new("");
906         gtk_box_pack_start(GTK_BOX(hbox_stat), statuslabel, FALSE, FALSE, 0);
907
908         sel_ac_tip = gtk_tooltips_new();
909         ac_button = gtk_button_new();
910         gtk_tooltips_set_tip(GTK_TOOLTIPS(sel_ac_tip),
911                              ac_button, _("Select account"), NULL);
912         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
913         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
914         gtk_widget_set_size_request(ac_button, -1, 0);
915         gtk_box_pack_end(GTK_BOX(hbox_stat), ac_button, FALSE, FALSE, 0);
916         g_signal_connect(G_OBJECT(ac_button), "button_press_event",
917                          G_CALLBACK(ac_label_button_pressed), mainwin);
918
919         ac_label = gtk_label_new("");
920         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
921
922         gtk_widget_show_all(hbox_stat);
923
924         gtk_widget_hide(offline_switch);
925         /* create views */
926         mainwin->folderview  = folderview  = folderview_create();
927         mainwin->summaryview = summaryview = summary_create();
928         mainwin->messageview = messageview = messageview_create(mainwin);
929         mainwin->logwin      = log_window_create();
930
931         folderview->mainwin      = mainwin;
932         folderview->summaryview  = summaryview;
933
934         summaryview->mainwin     = mainwin;
935         summaryview->folderview  = folderview;
936         summaryview->messageview = messageview;
937         summaryview->window      = window;
938
939         messageview->statusbar   = statusbar;
940         mainwin->vbox           = vbox;
941         mainwin->menubar        = menubar;
942         mainwin->menu_factory   = ifactory;
943         mainwin->handlebox      = handlebox;
944         mainwin->vbox_body      = vbox_body;
945         mainwin->hbox_stat      = hbox_stat;
946         mainwin->statusbar      = statusbar;
947         mainwin->progressbar    = progressbar;
948         mainwin->statuslabel    = statuslabel;
949         mainwin->online_switch  = online_switch;
950         mainwin->online_pixmap  = online_pixmap;
951         mainwin->offline_pixmap = offline_pixmap;
952         mainwin->ac_button      = ac_button;
953         mainwin->ac_label       = ac_label;
954         mainwin->offline_switch    = offline_switch;
955         
956         /* set context IDs for status bar */
957         mainwin->mainwin_cid = gtk_statusbar_get_context_id
958                 (GTK_STATUSBAR(statusbar), "Main Window");
959         mainwin->folderview_cid = gtk_statusbar_get_context_id
960                 (GTK_STATUSBAR(statusbar), "Folder View");
961         mainwin->summaryview_cid = gtk_statusbar_get_context_id
962                 (GTK_STATUSBAR(statusbar), "Summary View");
963         mainwin->messageview_cid = gtk_statusbar_get_context_id
964                 (GTK_STATUSBAR(statusbar), "Message View");
965
966         messageview->statusbar_cid = mainwin->messageview_cid;
967
968         /* allocate colors for summary view and folder view */
969         summaryview->color_marked.red = summaryview->color_marked.green = 0;
970         summaryview->color_marked.blue = (guint16)65535;
971
972         summaryview->color_dim.red = summaryview->color_dim.green =
973                 summaryview->color_dim.blue = COLOR_DIM;
974
975         gtkut_convert_int_to_gdk_color(prefs_common.color_new,
976                                        &folderview->color_new);
977
978         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
979                                        &folderview->color_op);
980
981         summaryview->color_important.red = 0;
982         summaryview->color_important.green = 0;
983         summaryview->color_important.blue = (guint16)65535;
984
985         color[0] = summaryview->color_marked;
986         color[1] = summaryview->color_dim;
987         color[2] = folderview->color_new;
988         color[3] = folderview->color_op;
989
990         colormap = gdk_window_get_colormap(window->window);
991         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
992         for (i = 0; i < 4; i++) {
993                 if (success[i] == FALSE)
994                         g_warning("MainWindow: color allocation %d failed\n", i);
995         }
996
997         debug_print("done.\n");
998
999         messageview->visible = prefs_common.msgview_visible;
1000         hide_messageview = !messageview->visible;
1001         
1002         main_window_set_widgets(mainwin, type);
1003
1004         g_signal_connect(G_OBJECT(window), "size_allocate",
1005                          G_CALLBACK(main_window_size_allocate_cb),
1006                          mainwin);
1007
1008         /* set menu items */
1009         menuitem = gtk_item_factory_get_item
1010                 (ifactory, "/View/Code set/Auto detect");
1011         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1012
1013         switch (prefs_common.toolbar_style) {
1014         case TOOLBAR_NONE:
1015                 menuitem = gtk_item_factory_get_item
1016                         (ifactory, "/View/Show or hide/Toolbar/None");
1017                 break;
1018         case TOOLBAR_ICON:
1019                 menuitem = gtk_item_factory_get_item
1020                         (ifactory, "/View/Show or hide/Toolbar/Icon");
1021                 break;
1022         case TOOLBAR_TEXT:
1023                 menuitem = gtk_item_factory_get_item
1024                         (ifactory, "/View/Show or hide/Toolbar/Text");
1025                 break;
1026         case TOOLBAR_BOTH:
1027                 menuitem = gtk_item_factory_get_item
1028                         (ifactory, "/View/Show or hide/Toolbar/Icon and text");
1029         }
1030         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1031
1032         gtk_widget_hide(mainwin->hbox_stat);
1033         menuitem = gtk_item_factory_get_item
1034                 (ifactory, "/View/Show or hide/Status bar");
1035         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1036                                        prefs_common.show_statusbar);
1037         
1038         /* set account selection menu */
1039         ac_menu = gtk_item_factory_get_widget
1040                 (ifactory, "/Configuration/Change current account");
1041         g_signal_connect(G_OBJECT(ac_menu), "selection_done",
1042                          G_CALLBACK(ac_menu_popup_closed), mainwin);
1043         mainwin->ac_menu = ac_menu;
1044
1045         toolbar_main_set_sensitive(mainwin);
1046
1047         /* create actions menu */
1048         main_window_update_actions_menu(mainwin);
1049
1050         /* attach accel groups to main window */
1051 #define ADD_MENU_ACCEL_GROUP_TO_WINDOW(menu,win)                        \
1052         gtk_window_add_accel_group                                      \
1053                 (GTK_WINDOW(win),                                       \
1054                  gtk_item_factory_from_widget(menu)->accel_group);      \
1055         g_signal_connect(G_OBJECT(gtk_item_factory_from_widget(menu)->accel_group), \
1056                         "accel_activate",                               \
1057                         G_CALLBACK(main_window_accel_activate), mainwin);
1058                          
1059         
1060         ADD_MENU_ACCEL_GROUP_TO_WINDOW(summaryview->popupmenu, mainwin->window);
1061         
1062         /* connect the accelerators for equivalent 
1063            menu items in different menus             */
1064         menu_connect_identical_items();
1065
1066
1067         
1068         /* show main window */
1069         gtk_widget_show(mainwin->window);
1070
1071         /* initialize views */
1072         folderview_init(folderview);
1073         summary_init(summaryview);
1074         messageview_init(messageview);
1075         log_window_init(mainwin->logwin);
1076         log_window_set_clipping(mainwin->logwin, prefs_common.cliplog,
1077                                 prefs_common.loglength);
1078 #ifdef USE_OPENSSL
1079         sslcertwindow_register_hook();
1080 #endif
1081         mainwin->lock_count = 0;
1082         mainwin->menu_lock_count = 0;
1083         mainwin->cursor_count = 0;
1084
1085         mainwin->progressindicator_hook =
1086                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1087
1088         if (!watch_cursor)
1089                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1090
1091         mainwin_list = g_list_append(mainwin_list, mainwin);
1092
1093         /* init work_offline */
1094         if (prefs_common.work_offline)
1095                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1096
1097         if (mainwin->type == SEPARATE_NONE && hide_messageview)
1098                 main_window_toggle_message_view(mainwin);
1099
1100         return mainwin;
1101 }
1102
1103 void main_window_destroy(MainWindow *mainwin)
1104 {
1105         /* TODO : destroy other component */
1106         messageview_destroy(mainwin->messageview);
1107 }
1108
1109 void main_window_update_actions_menu(MainWindow *mainwin)
1110 {
1111         GtkItemFactory *ifactory;
1112
1113         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1114         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1115 }
1116
1117 void main_window_cursor_wait(MainWindow *mainwin)
1118 {
1119
1120         if (mainwin->cursor_count == 0)
1121                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1122
1123         mainwin->cursor_count++;
1124
1125         gdk_flush();
1126 }
1127
1128 void main_window_cursor_normal(MainWindow *mainwin)
1129 {
1130         if (mainwin->cursor_count)
1131                 mainwin->cursor_count--;
1132
1133         if (mainwin->cursor_count == 0)
1134                 gdk_window_set_cursor(mainwin->window->window, NULL);
1135
1136         gdk_flush();
1137 }
1138
1139 /* lock / unlock the user-interface */
1140 void main_window_lock(MainWindow *mainwin)
1141 {
1142         if (mainwin->lock_count == 0)
1143                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1144
1145         mainwin->lock_count++;
1146
1147         main_window_set_menu_sensitive(mainwin);
1148         toolbar_main_set_sensitive(mainwin);
1149 }
1150
1151 void main_window_unlock(MainWindow *mainwin)
1152 {
1153         if (mainwin->lock_count)
1154                 mainwin->lock_count--;
1155
1156         main_window_set_menu_sensitive(mainwin);
1157         toolbar_main_set_sensitive(mainwin);
1158
1159         if (mainwin->lock_count == 0)
1160                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
1161 }
1162
1163 static void main_window_menu_callback_block(MainWindow *mainwin)
1164 {
1165         mainwin->menu_lock_count++;
1166 }
1167
1168 static void main_window_menu_callback_unblock(MainWindow *mainwin)
1169 {
1170         if (mainwin->menu_lock_count)
1171                 mainwin->menu_lock_count--;
1172 }
1173
1174 void main_window_reflect_prefs_all(void)
1175 {
1176         main_window_reflect_prefs_all_real(FALSE);
1177 }
1178
1179 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1180 {
1181         GList *cur;
1182         MainWindow *mainwin;
1183         GtkWidget *pixmap;
1184
1185         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1186                 mainwin = (MainWindow *)cur->data;
1187
1188                 main_window_show_cur_account(mainwin);
1189                 main_window_set_menu_sensitive(mainwin);
1190                 toolbar_main_set_sensitive(mainwin);
1191
1192                 /* pixmap themes */
1193                 if (pixmap_theme_changed) {
1194                         toolbar_update(TOOLBAR_MAIN, mainwin);
1195                         messageview_reflect_prefs_pixmap_theme();
1196                         compose_reflect_prefs_pixmap_theme();
1197                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1198                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1199
1200                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1201                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1202                                              mainwin->online_pixmap);
1203                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1204                         gtk_widget_show(pixmap);
1205                         mainwin->online_pixmap = pixmap;
1206                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1207                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1208                                              mainwin->offline_pixmap);
1209                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1210                         gtk_widget_show(pixmap);
1211                         mainwin->offline_pixmap = pixmap;
1212                 }
1213                 
1214                 summary_redisplay_msg(mainwin->summaryview);
1215                 headerview_set_visibility(mainwin->messageview->headerview,
1216                                           prefs_common.display_header_pane);
1217         }
1218 }
1219
1220 void main_window_set_summary_column(void)
1221 {
1222         GList *cur;
1223         MainWindow *mainwin;
1224
1225         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1226                 mainwin = (MainWindow *)cur->data;
1227                 summary_set_column_order(mainwin->summaryview);
1228         }
1229 }
1230
1231 static void main_window_set_account_selector_menu(MainWindow *mainwin,
1232                                                   GList *account_list)
1233 {
1234         GList *cur_ac, *cur_item;
1235         GtkWidget *menuitem;
1236         PrefsAccount *ac_prefs;
1237
1238         /* destroy all previous menu item */
1239         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
1240         while (cur_item != NULL) {
1241                 GList *next = cur_item->next;
1242                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1243                 cur_item = next;
1244         }
1245
1246         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1247                 ac_prefs = (PrefsAccount *)cur_ac->data;
1248
1249                 menuitem = gtk_menu_item_new_with_label
1250                         (ac_prefs->account_name
1251                          ? ac_prefs->account_name : _("Untitled"));
1252                 gtk_widget_show(menuitem);
1253                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
1254                 g_signal_connect(G_OBJECT(menuitem), "activate",
1255                                  G_CALLBACK(account_selector_menu_cb),
1256                                  ac_prefs);
1257         }
1258 }
1259
1260 static void main_window_set_account_receive_menu(MainWindow *mainwin,
1261                                                  GList *account_list)
1262 {
1263         GList *cur_ac, *cur_item;
1264         GtkWidget *menu;
1265         GtkWidget *menuitem;
1266         PrefsAccount *ac_prefs;
1267
1268         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
1269                                            "/Message/Receive");
1270
1271         /* search for separator */
1272         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1273              cur_item = cur_item->next) {
1274                 if (GTK_BIN(cur_item->data)->child == NULL) {
1275                         cur_item = cur_item->next;
1276                         break;
1277                 }
1278         }
1279
1280         /* destroy all previous menu item */
1281         while (cur_item != NULL) {
1282                 GList *next = cur_item->next;
1283                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1284                 cur_item = next;
1285         }
1286
1287         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1288                 ac_prefs = (PrefsAccount *)cur_ac->data;
1289
1290                 menuitem = gtk_menu_item_new_with_label
1291                         (ac_prefs->account_name ? ac_prefs->account_name
1292                          : _("Untitled"));
1293                 gtk_widget_show(menuitem);
1294                 gtk_menu_append(GTK_MENU(menu), menuitem);
1295                 g_signal_connect(G_OBJECT(menuitem), "activate",
1296                                  G_CALLBACK(account_receive_menu_cb),
1297                                  ac_prefs);
1298         }
1299 }
1300
1301 void main_window_set_account_menu(GList *account_list)
1302 {
1303         GList *cur;
1304         MainWindow *mainwin;
1305
1306         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1307                 mainwin = (MainWindow *)cur->data;
1308                 main_window_set_account_selector_menu(mainwin, account_list);
1309                 main_window_set_account_receive_menu(mainwin, account_list);
1310         }
1311 }
1312
1313 static void main_window_show_cur_account(MainWindow *mainwin)
1314 {
1315         gchar *buf;
1316         gchar *ac_name;
1317
1318         ac_name = g_strdup(cur_account
1319                            ? (cur_account->account_name
1320                               ? cur_account->account_name : _("Untitled"))
1321                            : _("none"));
1322
1323         if (cur_account)
1324                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
1325         else
1326                 buf = g_strdup(PROG_VERSION);
1327         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
1328         g_free(buf);
1329
1330         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
1331         gtk_widget_queue_resize(mainwin->ac_button);
1332
1333         g_free(ac_name);
1334 }
1335
1336 GtkWidget *main_window_get_folder_window(MainWindow *mainwin)
1337 {
1338         switch (mainwin->type) {
1339         case SEPARATE_FOLDER:
1340                 return mainwin->win.sep_folder.folderwin;
1341         case SEPARATE_BOTH:
1342                 return mainwin->win.sep_both.folderwin;
1343         default:
1344                 return NULL;
1345         }
1346 }
1347
1348 GtkWidget *main_window_get_message_window(MainWindow *mainwin)
1349 {
1350         switch (mainwin->type) {
1351         case SEPARATE_MESSAGE:
1352                 return mainwin->win.sep_message.messagewin;
1353         case SEPARATE_BOTH:
1354                 return mainwin->win.sep_both.messagewin;
1355         default:
1356                 return NULL;
1357         }
1358 }
1359
1360 void main_window_separation_change(MainWindow *mainwin, SeparateType type)
1361 {
1362         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
1363         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
1364         /* GtkWidget *message_wid = GTK_WIDGET_PTR(mainwin->messageview); */
1365         GtkWidget *message_wid = mainwin->messageview->vbox;
1366
1367         debug_print("Changing window separation type from %d to %d\n",
1368                     mainwin->type, type);
1369
1370         if (mainwin->type == type) return;
1371
1372         /* remove widgets from those containers */
1373         gtk_widget_ref(folder_wid);
1374         gtk_widget_ref(summary_wid);
1375         gtk_widget_ref(message_wid);
1376         gtkut_container_remove
1377                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1378         gtkut_container_remove
1379                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1380         gtkut_container_remove
1381                 (GTK_CONTAINER(message_wid->parent), message_wid);
1382
1383         /* clean containers */
1384         switch (mainwin->type) {
1385         case SEPARATE_NONE:
1386                 gtk_widget_destroy(mainwin->win.sep_none.hpaned);
1387                 break;
1388         case SEPARATE_FOLDER:
1389                 gtk_widget_destroy(mainwin->win.sep_folder.vpaned);
1390                 gtk_widget_destroy(mainwin->win.sep_folder.folderwin);
1391                 break;
1392         case SEPARATE_MESSAGE:
1393                 gtk_widget_destroy(mainwin->win.sep_message.hpaned);
1394                 gtk_widget_destroy(mainwin->win.sep_message.messagewin);
1395                 break;
1396         case SEPARATE_BOTH:
1397                 gtk_widget_destroy(mainwin->win.sep_both.messagewin);
1398                 gtk_widget_destroy(mainwin->win.sep_both.folderwin);
1399                 break;
1400         }
1401
1402         gtk_widget_hide(mainwin->window);
1403         main_window_set_widgets(mainwin, type);
1404         gtk_widget_show(mainwin->window);
1405
1406         gtk_widget_unref(folder_wid);
1407         gtk_widget_unref(summary_wid);
1408         gtk_widget_unref(message_wid);
1409 }
1410
1411 void main_window_toggle_message_view(MainWindow *mainwin)
1412 {
1413         SummaryView *summaryview = mainwin->summaryview;
1414         union CompositeWin *cwin = &mainwin->win;
1415         GtkWidget *vpaned = NULL;
1416         GtkWidget *container = NULL;
1417         GtkWidget *msgwin = NULL;
1418
1419         switch (mainwin->type) {
1420         case SEPARATE_NONE:
1421                 vpaned = cwin->sep_none.vpaned;
1422                 container = cwin->sep_none.hpaned;
1423                 break;
1424         case SEPARATE_FOLDER:
1425                 vpaned = cwin->sep_folder.vpaned;
1426                 container = mainwin->vbox_body;
1427                 break;
1428         case SEPARATE_MESSAGE:
1429                 msgwin = mainwin->win.sep_message.messagewin;
1430                 break;
1431         case SEPARATE_BOTH:
1432                 msgwin = mainwin->win.sep_both.messagewin;
1433                 break;
1434         }
1435
1436         if (msgwin) {
1437                 if (GTK_WIDGET_VISIBLE(msgwin)) {
1438                         gtk_widget_hide(msgwin);
1439                         mainwin->messageview->visible = FALSE;
1440                         summaryview->displayed = NULL;
1441                 } else {
1442                         gtk_widget_show(msgwin);
1443                         mainwin->messageview->visible = TRUE;
1444                 }
1445         } else if (vpaned->parent != NULL) {
1446                 mainwin->messageview->visible = FALSE;
1447                 summaryview->displayed = NULL;
1448                 gtk_widget_ref(vpaned);
1449                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1450                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1451                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1452                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1453         } else {
1454                 mainwin->messageview->visible = TRUE;
1455                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1456                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1457                 gtk_widget_unref(vpaned);
1458                 gtk_arrow_set(GTK_ARROW(summaryview->toggle_arrow),
1459                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1460         }
1461
1462         if (mainwin->messageview->visible == FALSE)
1463                 messageview_clear(mainwin->messageview);
1464
1465         main_window_set_menu_sensitive(mainwin);
1466
1467         prefs_common.msgview_visible = mainwin->messageview->visible;
1468
1469         gtk_widget_grab_focus(summaryview->ctree);
1470 }
1471
1472 void main_window_get_size(MainWindow *mainwin)
1473 {
1474         GtkAllocation *allocation;
1475
1476         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1477
1478         if (allocation->width > 1 && allocation->height > 1) {
1479                 prefs_common.summaryview_width = allocation->width;
1480
1481                 if ((mainwin->type == SEPARATE_NONE ||
1482                      mainwin->type == SEPARATE_FOLDER) &&
1483                     messageview_is_visible(mainwin->messageview))
1484                         prefs_common.summaryview_height = allocation->height;
1485
1486                 prefs_common.mainview_width = allocation->width;
1487         }
1488
1489         allocation = &mainwin->window->allocation;
1490         if (allocation->width > 1 && allocation->height > 1) {
1491                 prefs_common.mainview_height = allocation->height;
1492                 prefs_common.mainwin_width   = allocation->width;
1493                 prefs_common.mainwin_height  = allocation->height;
1494         }
1495
1496         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1497         if (allocation->width > 1 && allocation->height > 1) {
1498                 prefs_common.folderview_width  = allocation->width;
1499                 prefs_common.folderview_height = allocation->height;
1500         }
1501
1502         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
1503         if (allocation->width > 1 && allocation->height > 1) {
1504                 prefs_common.msgview_width = allocation->width;
1505                 prefs_common.msgview_height = allocation->height;
1506         }
1507
1508 /*      debug_print("summaryview size: %d x %d\n",
1509                     prefs_common.summaryview_width,
1510                     prefs_common.summaryview_height);
1511         debug_print("folderview size: %d x %d\n",
1512                     prefs_common.folderview_width,
1513                     prefs_common.folderview_height);
1514         debug_print("messageview size: %d x %d\n",
1515                     prefs_common.msgview_width,
1516                     prefs_common.msgview_height); */
1517 }
1518
1519 void main_window_get_position(MainWindow *mainwin)
1520 {
1521         gint x, y;
1522         GtkWidget *window;
1523
1524         gtkut_widget_get_uposition(mainwin->window, &x, &y);
1525
1526         prefs_common.mainview_x = x;
1527         prefs_common.mainview_y = y;
1528         prefs_common.mainwin_x = x;
1529         prefs_common.mainwin_y = y;
1530
1531         debug_print("main window position: %d, %d\n", x, y);
1532
1533         window = main_window_get_folder_window(mainwin);
1534         if (window) {
1535                 gtkut_widget_get_uposition(window, &x, &y);
1536                 prefs_common.folderwin_x = x;
1537                 prefs_common.folderwin_y = y;
1538                 debug_print("folder window position: %d, %d\n", x, y);
1539         }
1540         window = main_window_get_message_window(mainwin);
1541         if (window) {
1542                 gtkut_widget_get_uposition(window, &x, &y);
1543                 prefs_common.main_msgwin_x = x;
1544                 prefs_common.main_msgwin_y = y;
1545                 debug_print("message window position: %d, %d\n", x, y);
1546         }
1547 }
1548
1549 void main_window_progress_on(MainWindow *mainwin)
1550 {
1551         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1552 }
1553
1554 void main_window_progress_off(MainWindow *mainwin)
1555 {
1556         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
1557         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1558 }
1559
1560 void main_window_progress_set(MainWindow *mainwin, gint cur, gint total)
1561 {
1562         gchar buf[32];
1563
1564         g_snprintf(buf, sizeof(buf), "%d / %d", cur, total);
1565         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), buf);
1566         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar),
1567                                 (cur == 0 && total == 0) ? 0 :
1568                                 (gfloat)cur / (gfloat)total);
1569 }
1570
1571 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
1572 {
1573         GList *list;
1574         guint has_trash;
1575         Folder *folder;
1576
1577         for (has_trash = 0, list = folder_get_list(); list != NULL; list = list->next) {
1578                 folder = FOLDER(list->data);
1579                 if (folder && folder->trash && folder->trash->total_msgs > 0)
1580                         has_trash++;
1581         }
1582
1583         if (!has_trash) return;
1584         
1585         if (confirm) {
1586                 if (alertpanel(_("Empty trash"),
1587                                _("Empty all messages in trash?"),
1588                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
1589                         return;
1590                 manage_window_focus_in(mainwin->window, NULL, NULL);
1591         }
1592
1593         procmsg_empty_trash();
1594
1595         if (mainwin->summaryview->folder_item &&
1596             mainwin->summaryview->folder_item->stype == F_TRASH)
1597                 gtk_widget_grab_focus(mainwin->folderview->ctree);
1598 }
1599
1600 void main_window_add_mailbox(MainWindow *mainwin)
1601 {
1602         gchar *path;
1603         Folder *folder;
1604
1605         path = input_dialog(_("Add mailbox"),
1606                             _("Input the location of mailbox.\n"
1607                               "If the existing mailbox is specified, it will be\n"
1608                               "scanned automatically."),
1609                             "Mail");
1610         if (!path) return;
1611         if (folder_find_from_path(path)) {
1612                 alertpanel_error(_("The mailbox `%s' already exists."), path);
1613                 g_free(path);
1614                 return;
1615         }
1616         folder = folder_new(folder_get_class_from_string("mh"), 
1617                             !strcmp(path, "Mail") ? _("Mailbox") : g_basename(path),
1618                             path);
1619         g_free(path);
1620
1621         if (folder->klass->create_tree(folder) < 0) {
1622                 alertpanel_error(_("Creation of the mailbox failed.\n"
1623                                    "Maybe some files already exist, or you don't have the permission to write there."));
1624                 folder_destroy(folder);
1625                 return;
1626         }
1627
1628         folder_add(folder);
1629         folder_set_ui_func(folder, scan_tree_func, mainwin);
1630         folder_scan_tree(folder);
1631         folder_set_ui_func(folder, NULL, NULL);
1632 }
1633
1634 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
1635 {
1636         SensitiveCond state = 0;
1637         SummarySelection selection;
1638         FolderItem *item = mainwin->summaryview->folder_item;
1639         GList *account_list = account_get_list();
1640         
1641         selection = summary_get_selection_type(mainwin->summaryview);
1642
1643         if (mainwin->lock_count == 0)
1644                 state |= M_UNLOCKED;
1645         if (selection != SUMMARY_NONE)
1646                 state |= M_MSG_EXIST;
1647         if (item && item->path && folder_item_parent(item) && !item->no_select) {
1648                 state |= M_EXEC;
1649                 /*              if (item->folder->type != F_NEWS) */
1650                 state |= M_ALLOW_DELETE;
1651
1652                 if (prefs_common.immediate_exec == 0
1653                     && mainwin->lock_count == 0)
1654                         state |= M_DELAY_EXEC;
1655
1656                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
1657                     || selection != SUMMARY_NONE)
1658                         state |= M_HIDE_READ_MSG;       
1659         }               
1660         if (mainwin->summaryview->threaded)
1661                 state |= M_THREADED;
1662         else
1663                 state |= M_UNTHREADED;  
1664         if (selection == SUMMARY_SELECTED_SINGLE ||
1665             selection == SUMMARY_SELECTED_MULTIPLE)
1666                 state |= M_TARGET_EXIST;
1667         if (selection == SUMMARY_SELECTED_SINGLE)
1668                 state |= M_SINGLE_TARGET_EXIST;
1669         if (mainwin->summaryview->folder_item &&
1670             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
1671                 state |= M_NEWS;
1672         else
1673                 state |= M_NOT_NEWS;
1674         if (selection == SUMMARY_SELECTED_SINGLE &&
1675             (item &&
1676              (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1677               item->stype == F_QUEUE)))
1678                 state |= M_ALLOW_REEDIT;
1679         if (cur_account)
1680                 state |= M_HAVE_ACCOUNT;
1681         
1682         for ( ; account_list != NULL; account_list = account_list->next) {
1683                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
1684                         state |= M_HAVE_NEWS_ACCOUNT;
1685                         break;
1686                 }
1687         }
1688
1689         if (inc_is_active())
1690                 state |= M_INC_ACTIVE;
1691
1692         return state;
1693 }
1694
1695
1696
1697 void main_window_set_menu_sensitive(MainWindow *mainwin)
1698 {
1699         GtkItemFactory *ifactory = mainwin->menu_factory;
1700         SensitiveCond state;
1701         gboolean sensitive;
1702         GtkWidget *menu;
1703         GtkWidget *menuitem;
1704         SummaryView *summaryview;
1705         gchar *menu_path;
1706         gint i;
1707         GList *cur_item;
1708
1709         static const struct {
1710                 gchar *const entry;
1711                 SensitiveCond cond;
1712         } entry[] = {
1713                 {"/File/Add mailbox"                          , M_UNLOCKED},
1714
1715                 {"/File/Add mailbox/MH..."                    , M_UNLOCKED},
1716                 {"/File/Change folder order"                  , M_UNLOCKED},
1717                 {"/File/Export to mbox file..."               , M_UNLOCKED},
1718                 {"/File/Empty all Trash folders"              , M_UNLOCKED},
1719                 {"/File/Work offline"                         , M_UNLOCKED},
1720
1721                 {"/File/Save as...", M_TARGET_EXIST|M_UNLOCKED},
1722                 {"/File/Print..."  , M_TARGET_EXIST|M_UNLOCKED},
1723                 /* {"/File/Close"  , M_UNLOCKED}, */
1724                 {"/File/Exit"      , M_UNLOCKED},
1725
1726                 {"/Edit/Select thread"             , M_SINGLE_TARGET_EXIST},
1727
1728                 {"/View/Sort"                      , M_EXEC},
1729                 {"/View/Thread view"               , M_EXEC},
1730                 {"/View/Expand all threads"        , M_MSG_EXIST},
1731                 {"/View/Collapse all threads"      , M_MSG_EXIST},
1732                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
1733                 {"/View/Go to/Prev message"        , M_MSG_EXIST},
1734                 {"/View/Go to/Next message"        , M_MSG_EXIST},
1735                 {"/View/Go to/Prev unread message" , M_MSG_EXIST},
1736                 {"/View/Go to/Prev new message"    , M_MSG_EXIST},
1737                 {"/View/Go to/Prev marked message" , M_MSG_EXIST},
1738                 {"/View/Go to/Next marked message" , M_MSG_EXIST},
1739                 {"/View/Go to/Prev labeled message", M_MSG_EXIST},
1740                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
1741                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
1742                 {"/View/Show all headers"          , M_SINGLE_TARGET_EXIST},
1743                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
1744
1745                 {"/Message/Receive/Get from current account"
1746                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1747                 {"/Message/Receive/Get from all accounts"
1748                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1749                 {"/Message/Receive/Cancel receiving"
1750                                                  , M_INC_ACTIVE},
1751
1752                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
1753                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1754                 {"/Message/Reply to"             , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1755                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1756                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1757                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1758                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1759                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1760                 {"/Message/Delete"                , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1761                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1762                 {"/Message/Mark"                  , M_TARGET_EXIST},
1763                 {"/Message/Re-edit"              , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1764
1765                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
1766                 {"/Tools/Harvest addresses"            , M_UNLOCKED},
1767                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1768                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1769                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1770                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_UNLOCKED},
1771                 {"/Tools/Execute"                      , M_DELAY_EXEC},
1772                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1773
1774                 {"/Configuration", M_UNLOCKED},
1775
1776                 {NULL, 0}
1777         };
1778
1779         state = main_window_get_current_state(mainwin);
1780
1781         for (i = 0; entry[i].entry != NULL; i++) {
1782                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1783                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1784         }
1785
1786         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
1787
1788         /* search for separator */
1789         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1790              cur_item = cur_item->next) {
1791                 if (GTK_BIN(cur_item->data)->child == NULL) {
1792                         cur_item = cur_item->next;
1793                         break;
1794                 }
1795         }
1796
1797         for (; cur_item != NULL; cur_item = cur_item->next) {
1798                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
1799                                          (M_UNLOCKED & state) != 0);
1800         }
1801
1802         main_window_menu_callback_block(mainwin);
1803
1804 #define SET_CHECK_MENU_ACTIVE(path, active) \
1805 { \
1806         menuitem = gtk_item_factory_get_widget(ifactory, path); \
1807         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
1808 }
1809
1810         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
1811                               messageview_is_visible(mainwin->messageview));
1812
1813         summaryview = mainwin->summaryview;
1814         menu_path = "/View/Sort/Don't sort";
1815
1816         switch (summaryview->sort_key) {
1817         case SORT_BY_NUMBER:
1818                 menu_path = "/View/Sort/by number"; break;
1819         case SORT_BY_SIZE:
1820                 menu_path = "/View/Sort/by size"; break;
1821         case SORT_BY_DATE:
1822                 menu_path = "/View/Sort/by date"; break;
1823         case SORT_BY_FROM:
1824                 menu_path = "/View/Sort/by from"; break;
1825         case SORT_BY_TO:
1826                 menu_path = "/View/Sort/by recipient"; break;
1827         case SORT_BY_SUBJECT:
1828                 menu_path = "/View/Sort/by subject"; break;
1829         case SORT_BY_LABEL:
1830                 menu_path = "/View/Sort/by color label"; break;
1831         case SORT_BY_MARK:
1832                 menu_path = "/View/Sort/by mark"; break;
1833         case SORT_BY_STATUS:
1834                 menu_path = "/View/Sort/by status"; break;
1835         case SORT_BY_MIME:
1836                 menu_path = "/View/Sort/by attachment"; break;
1837         case SORT_BY_SCORE:
1838                 menu_path = "/View/Sort/by score"; break;
1839         case SORT_BY_LOCKED:
1840                 menu_path = "/View/Sort/by locked"; break;
1841         case SORT_BY_NONE:
1842         default:
1843                 menu_path = "/View/Sort/Don't sort"; break;
1844         }
1845         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
1846
1847         if (summaryview->sort_type == SORT_ASCENDING) {
1848                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
1849         } else {
1850                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
1851         }
1852
1853         if (summaryview->sort_key != SORT_BY_NONE) {
1854                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
1855                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
1856         } else {
1857                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
1858                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
1859         }
1860
1861         SET_CHECK_MENU_ACTIVE("/View/Show all headers",
1862                               mainwin->messageview->mimeview->textview->show_all_headers);
1863         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
1864
1865 #undef SET_CHECK_MENU_ACTIVE
1866
1867         main_window_menu_callback_unblock(mainwin);
1868 }
1869
1870 void main_window_popup(MainWindow *mainwin)
1871 {
1872         gtkut_window_popup(mainwin->window);
1873
1874         switch (mainwin->type) {
1875         case SEPARATE_FOLDER:
1876                 gtkut_window_popup(mainwin->win.sep_folder.folderwin);
1877                 break;
1878         case SEPARATE_MESSAGE:
1879                 gtkut_window_popup(mainwin->win.sep_message.messagewin);
1880                 break;
1881         case SEPARATE_BOTH:
1882                 gtkut_window_popup(mainwin->win.sep_both.folderwin);
1883                 gtkut_window_popup(mainwin->win.sep_both.messagewin);
1884                 break;
1885         default:
1886                 break;
1887         }
1888 }
1889
1890 void main_window_show(MainWindow *mainwin)
1891 {
1892         gtk_widget_show(mainwin->window);
1893         gtk_widget_show(mainwin->vbox_body);
1894
1895         gtk_widget_set_uposition(mainwin->window,
1896                                  prefs_common.mainwin_x,
1897                                  prefs_common.mainwin_y);
1898
1899         gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->folderview),
1900                              prefs_common.folderview_width,
1901                              prefs_common.folderview_height);
1902         gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->summaryview),
1903                              prefs_common.summaryview_width,
1904                              prefs_common.summaryview_height);
1905         gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->messageview),
1906                              prefs_common.msgview_width,
1907                              prefs_common.msgview_height);
1908
1909         if (mainwin->type & SEPARATE_FOLDER) {
1910                 GtkWidget *folderwin;
1911
1912                 folderwin = main_window_get_folder_window(mainwin);
1913                 gtk_widget_show(folderwin);
1914                 gtk_widget_set_uposition(folderwin, prefs_common.folderwin_x,
1915                                          prefs_common.folderwin_y);
1916         }
1917
1918         if (mainwin->type & SEPARATE_MESSAGE) {
1919                 GtkWidget *messagewin;
1920
1921                 messagewin = main_window_get_message_window(mainwin);
1922                 gtk_widget_show(messagewin);
1923                 gtk_widget_set_uposition(messagewin, prefs_common.main_msgwin_x,
1924                                          prefs_common.main_msgwin_y);
1925         }
1926 }
1927
1928 void main_window_hide(MainWindow *mainwin)
1929 {
1930         main_window_get_size(mainwin);
1931         main_window_get_position(mainwin);
1932
1933         gtk_widget_hide(mainwin->window);
1934         gtk_widget_hide(mainwin->vbox_body);
1935
1936         if (mainwin->type & SEPARATE_FOLDER) {
1937                 gtk_widget_hide(mainwin->win.sep_folder.folderwin);
1938         }
1939
1940         if (mainwin->type & SEPARATE_MESSAGE) {
1941                 gtk_widget_hide(mainwin->win.sep_message.messagewin);
1942         }
1943 }
1944
1945 static void main_window_set_widgets(MainWindow *mainwin, SeparateType type)
1946 {
1947         GtkWidget *folderwin = NULL;
1948         GtkWidget *messagewin = NULL;
1949         GtkWidget *hpaned;
1950         GtkWidget *vpaned;
1951         GtkWidget *vbox_body = mainwin->vbox_body;
1952         GtkItemFactory *ifactory = mainwin->menu_factory;
1953         GtkWidget *menuitem;
1954         GtkItemFactory *msgview_ifactory;
1955
1956         debug_print("Setting widgets... ");
1957
1958         /* create separated window(s) if needed */
1959         if (type & SEPARATE_FOLDER) {
1960                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1961                 gtk_window_set_title(GTK_WINDOW(folderwin),
1962                                      _("Sylpheed - Folder View"));
1963                 gtk_window_set_resizable(GTK_WINDOW(folderwin), TRUE);
1964                 gtk_window_move(GTK_WINDOW(folderwin), prefs_common.folderwin_x,
1965                                 prefs_common.folderwin_y);
1966                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
1967                                                BORDER_WIDTH);
1968                 g_signal_connect(G_OBJECT(folderwin), "delete_event",
1969                                  G_CALLBACK(folder_window_close_cb),
1970                                    mainwin);
1971                 gtk_container_add(GTK_CONTAINER(folderwin),
1972                                   GTK_WIDGET_PTR(mainwin->folderview));
1973                 gtk_widget_realize(folderwin);
1974                 if (prefs_common.folderview_visible)
1975                         gtk_widget_show(folderwin);
1976         }
1977         if (type & SEPARATE_MESSAGE) {
1978                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1979                 gtk_window_set_title(GTK_WINDOW(messagewin),
1980                                      _("Sylpheed - Message View"));
1981                 gtk_window_set_resizable(GTK_WINDOW(messagewin), TRUE);
1982                 gtk_window_move(GTK_WINDOW(messagewin), 
1983                                 prefs_common.main_msgwin_x,
1984                                 prefs_common.main_msgwin_y);
1985                 gtk_widget_set_size_request(messagewin, 
1986                                             prefs_common.msgwin_width,
1987                                             prefs_common.msgwin_height);
1988                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
1989                                                BORDER_WIDTH);
1990                 g_signal_connect(G_OBJECT(messagewin), "delete_event",
1991                                  G_CALLBACK(message_window_close_cb),
1992                                  mainwin);
1993                 if (messageview_is_visible(mainwin->messageview))
1994                         gtk_widget_show(messagewin);
1995         }
1996
1997         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
1998                                     prefs_common.folderview_width,
1999                                     prefs_common.folderview_height);
2000         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2001                                     prefs_common.summaryview_width,
2002                                     prefs_common.summaryview_height);
2003         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2004                                     prefs_common.msgview_width,
2005                                     prefs_common.msgview_height);
2006
2007         switch (type) {
2008         case SEPARATE_NONE:
2009                 hpaned = gtk_hpaned_new();
2010                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2011                 gtk_paned_add1(GTK_PANED(hpaned),
2012                                GTK_WIDGET_PTR(mainwin->folderview));
2013                 gtk_widget_show(hpaned);
2014                 gtk_widget_queue_resize(hpaned);
2015
2016                 vpaned = gtk_vpaned_new();
2017                 if (messageview_is_visible(mainwin->messageview)) {
2018                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2019                         gtk_paned_add1(GTK_PANED(vpaned),
2020                                        GTK_WIDGET_PTR(mainwin->summaryview));
2021                 } else {
2022                         gtk_paned_add2(GTK_PANED(hpaned),
2023                                        GTK_WIDGET_PTR(mainwin->summaryview));
2024                         gtk_widget_ref(vpaned);
2025                 }
2026                 gtk_paned_add2(GTK_PANED(vpaned),
2027                                GTK_WIDGET_PTR(mainwin->messageview));
2028                 gtk_widget_show(vpaned);
2029                 gtk_widget_queue_resize(vpaned);
2030
2031                 mainwin->win.sep_none.hpaned = hpaned;
2032                 mainwin->win.sep_none.vpaned = vpaned;
2033                 
2034                 /* remove headerview if not in prefs */
2035                 headerview_set_visibility(mainwin->messageview->headerview,
2036                                           prefs_common.display_header_pane);
2037                 break;
2038         case SEPARATE_FOLDER:
2039                 vpaned = gtk_vpaned_new();
2040                 if (messageview_is_visible(mainwin->messageview)) {
2041                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
2042                                            TRUE, TRUE, 0);
2043                         gtk_paned_add1(GTK_PANED(vpaned),
2044                                        GTK_WIDGET_PTR(mainwin->summaryview));
2045                 } else {
2046                         gtk_box_pack_start(GTK_BOX(vbox_body),
2047                                            GTK_WIDGET_PTR(mainwin->summaryview),
2048                                            TRUE, TRUE, 0);
2049                         gtk_widget_ref(vpaned);
2050                 }
2051                 gtk_paned_add2(GTK_PANED(vpaned),
2052                                GTK_WIDGET_PTR(mainwin->messageview));
2053                 gtk_widget_show(vpaned);
2054                 gtk_widget_queue_resize(vpaned);
2055
2056                 mainwin->win.sep_folder.folderwin = folderwin;
2057                 mainwin->win.sep_folder.vpaned    = vpaned;
2058
2059                 /* remove headerview if not in prefs */
2060                 headerview_set_visibility(mainwin->messageview->headerview,
2061                                           prefs_common.display_header_pane);
2062                 
2063                 break;
2064         case SEPARATE_MESSAGE:
2065                 hpaned = gtk_hpaned_new();
2066                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2067                 gtk_paned_add1(GTK_PANED(hpaned),
2068                                GTK_WIDGET_PTR(mainwin->folderview));
2069                 gtk_paned_add2(GTK_PANED(hpaned),
2070                                GTK_WIDGET_PTR(mainwin->summaryview));
2071                 gtk_widget_show(hpaned);
2072                 gtk_widget_queue_resize(hpaned);
2073                 messageview_add_toolbar(mainwin->messageview, messagewin);
2074                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2075                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2076
2077                 mainwin->win.sep_message.messagewin = messagewin;
2078                 mainwin->win.sep_message.hpaned     = hpaned;
2079
2080                 gtk_widget_realize(messagewin);
2081                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2082                 gtk_widget_show_all(messagewin);
2083                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2084                                   mainwin->messageview->handlebox, 
2085                                   prefs_common.toolbar_style);
2086
2087                 break;
2088         case SEPARATE_BOTH:
2089                 messageview_add_toolbar(mainwin->messageview, messagewin);
2090                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2091                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2092
2093                 gtk_box_pack_start(GTK_BOX(vbox_body),
2094                                    GTK_WIDGET_PTR(mainwin->summaryview),
2095                                    TRUE, TRUE, 0);
2096                 
2097                 mainwin->win.sep_both.folderwin = folderwin;
2098                 mainwin->win.sep_both.messagewin = messagewin;
2099                 
2100                 gtk_widget_realize(messagewin);
2101                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2102                 gtk_widget_show_all(messagewin);
2103                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2104                                   mainwin->messageview->handlebox, 
2105                                   prefs_common.toolbar_style);          
2106
2107                 break;
2108         }
2109
2110         gtk_window_move(GTK_WINDOW(mainwin->window),
2111                         prefs_common.mainwin_x,
2112                         prefs_common.mainwin_y);
2113
2114         gtk_widget_queue_resize(vbox_body);
2115         gtk_widget_queue_resize(mainwin->vbox);
2116         gtk_widget_queue_resize(mainwin->window);
2117         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2118          * and mimeview icon list/ctree lose track of their visibility states */
2119         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2120                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2121         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2122                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2123         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2124                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2125         else 
2126                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2127
2128         mainwin->type = type;
2129
2130
2131         /* toggle menu state */
2132         menuitem = gtk_item_factory_get_item
2133                 (ifactory, "/View/Show or hide/Folder tree");
2134         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2135                                        (type & SEPARATE_FOLDER) == 0 ? TRUE :
2136                                        prefs_common.folderview_visible);
2137         gtk_widget_set_sensitive(menuitem, ((type & SEPARATE_FOLDER) != 0));
2138         menuitem = gtk_item_factory_get_item
2139                 (ifactory, "/View/Show or hide/Message view");
2140         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2141                                        (type & SEPARATE_MESSAGE) == 0 ? TRUE :
2142                                        prefs_common.msgview_visible);
2143
2144         menuitem = gtk_item_factory_get_item
2145                 (ifactory, "/View/Separate folder tree");
2146         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2147                                        ((type & SEPARATE_FOLDER) != 0));
2148         menuitem = gtk_item_factory_get_item
2149                 (ifactory, "/View/Separate message view");
2150         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2151                                        ((type & SEPARATE_MESSAGE) != 0));
2152
2153         if (folderwin) {
2154                 g_signal_connect
2155                         (G_OBJECT(folderwin), "size_allocate",
2156                          G_CALLBACK(folder_window_size_allocate_cb),
2157                          mainwin);
2158         }
2159         if (messagewin) {
2160                 g_signal_connect
2161                         (G_OBJECT(messagewin), "size_allocate",
2162                          G_CALLBACK(message_window_size_allocate_cb),
2163                          mainwin);
2164         }
2165
2166         debug_print("done.\n");
2167 }
2168
2169 void main_window_destroy_all(void)
2170 {
2171         while (mainwin_list != NULL) {
2172                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2173                 
2174                 /* free toolbar stuff */
2175                 toolbar_clear_list(TOOLBAR_MAIN);
2176                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2177                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2178
2179                 g_free(mainwin->toolbar);
2180                 g_free(mainwin);
2181                 
2182                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2183         }
2184         g_list_free(mainwin_list);
2185 }
2186
2187 #if 0
2188 static gboolean toolbar_account_button_pressed(GtkWidget *widget,
2189                                                GdkEventButton *event,
2190                                                gpointer data)
2191 {
2192         MainWindow *mainwin = (MainWindow *)data;
2193
2194         if (!event) return FALSE;
2195         if (event->button != 3) return FALSE;
2196
2197         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2198         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2199                           widget);
2200
2201         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2202                        menu_button_position, widget,
2203                        event->button, event->time);
2204
2205         return FALSE;
2206 }
2207 #endif
2208
2209 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2210                                    gpointer data)
2211 {
2212         gtk_widget_set_usize(child, 1, -1);
2213 }
2214
2215 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2216                                    gpointer data)
2217 {
2218         gtk_widget_set_usize(child, -1, -1);
2219 }
2220
2221 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2222                                     gpointer data)
2223 {
2224         MainWindow *mainwin = (MainWindow *)data;
2225
2226         if (!event) return FALSE;
2227
2228         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2229         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2230                           widget);
2231
2232         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2233                        menu_button_position, widget,
2234                        event->button, event->time);
2235
2236         return TRUE;
2237 }
2238
2239 static void ac_menu_popup_closed(GtkMenuShell *menu_shell, gpointer data)
2240 {
2241         MainWindow *mainwin = (MainWindow *)data;
2242         GtkWidget *button;
2243
2244         button = g_object_get_data(G_OBJECT(menu_shell), "menu_button");
2245         if (!button) return;
2246         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
2247         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button", NULL);
2248         manage_window_focus_in(mainwin->window, NULL, NULL);
2249 }
2250
2251 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2252                                  gpointer data)
2253 {
2254         MainWindow *mainwin = (MainWindow *)data;
2255
2256         if (mainwin->lock_count == 0)
2257                 app_exit_cb(data, 0, widget);
2258
2259         return TRUE;
2260 }
2261
2262 static gint folder_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2263                                    gpointer data)
2264 {
2265         MainWindow *mainwin = (MainWindow *)data;
2266         GtkWidget *menuitem;
2267
2268         menuitem = gtk_item_factory_get_item
2269                 (mainwin->menu_factory, "/View/Show or hide/Folder tree");
2270         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2271
2272         return TRUE;
2273 }
2274
2275 static gint message_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2276                                     gpointer data)
2277 {
2278         MainWindow *mainwin = (MainWindow *)data;
2279         GtkWidget *menuitem;
2280
2281         menuitem = gtk_item_factory_get_item
2282                 (mainwin->menu_factory, "/View/Show or hide/Message view");
2283         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2284
2285         return TRUE;
2286 }
2287
2288 static void main_window_size_allocate_cb(GtkWidget *widget,
2289                                          GtkAllocation *allocation,
2290                                          gpointer data)
2291 {
2292         MainWindow *mainwin = (MainWindow *)data;
2293
2294         main_window_get_size(mainwin);
2295 }
2296
2297 static void folder_window_size_allocate_cb(GtkWidget *widget,
2298                                            GtkAllocation *allocation,
2299                                            gpointer data)
2300 {
2301         MainWindow *mainwin = (MainWindow *)data;
2302
2303         main_window_get_size(mainwin);
2304 }
2305
2306 static void message_window_size_allocate_cb(GtkWidget *widget,
2307                                             GtkAllocation *allocation,
2308                                             gpointer data)
2309 {
2310         MainWindow *mainwin = (MainWindow *)data;
2311
2312         main_window_get_size(mainwin);
2313 }
2314
2315 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2316                            GtkWidget *widget)
2317 {
2318         main_window_add_mailbox(mainwin);
2319 }
2320
2321 static void update_folderview_cb(MainWindow *mainwin, guint action,
2322                                  GtkWidget *widget)
2323 {
2324         summary_show(mainwin->summaryview, NULL);
2325         folderview_check_new_all();
2326 }
2327
2328 static void foldersort_cb(MainWindow *mainwin, guint action,
2329                            GtkWidget *widget)
2330 {
2331         foldersort_open();
2332 }
2333
2334 static void import_mbox_cb(MainWindow *mainwin, guint action,
2335                            GtkWidget *widget)
2336 {
2337         import_mbox(mainwin->summaryview->folder_item);
2338 }
2339
2340 static void export_mbox_cb(MainWindow *mainwin, guint action,
2341                            GtkWidget *widget)
2342 {
2343         export_mbox(mainwin->summaryview->folder_item);
2344 }
2345
2346 static void empty_trash_cb(MainWindow *mainwin, guint action,
2347                            GtkWidget *widget)
2348 {
2349         main_window_empty_trash(mainwin, TRUE);
2350 }
2351
2352 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2353 {
2354         summary_save_as(mainwin->summaryview);
2355 }
2356
2357 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2358 {
2359         summary_print(mainwin->summaryview);
2360 }
2361
2362 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2363 {
2364         if (prefs_common.confirm_on_exit) {
2365                 if (alertpanel(_("Exit"), _("Exit this program?"),
2366                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
2367                         return;
2368                 manage_window_focus_in(mainwin->window, NULL, NULL);
2369         }
2370
2371         app_will_exit(widget, mainwin);
2372 }
2373
2374 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2375 {
2376         if (action == 1)
2377                 summary_search(mainwin->summaryview);
2378         else
2379                 message_search(mainwin->messageview);
2380 }
2381
2382 static void toggle_folder_cb(MainWindow *mainwin, guint action,
2383                              GtkWidget *widget)
2384 {
2385         gboolean active;
2386
2387         active = GTK_CHECK_MENU_ITEM(widget)->active;
2388
2389         switch (mainwin->type) {
2390         case SEPARATE_NONE:
2391         case SEPARATE_MESSAGE:
2392 #if 0
2393                 if (active)
2394                         gtk_widget_show(GTK_WIDGET_PTR(mainwin->folderview));
2395                 else
2396                         gtk_widget_hide(GTK_WIDGET_PTR(mainwin->folderview));
2397 #endif
2398                 break;
2399         case SEPARATE_FOLDER:
2400                 debug_print("separate folder\n");
2401                 if (active)
2402                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
2403                 else
2404                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2405                 break;
2406         case SEPARATE_BOTH:
2407                 if (active)
2408                         gtk_widget_show(mainwin->win.sep_both.folderwin);
2409                 else
2410                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
2411                 break;
2412         }
2413
2414         prefs_common.folderview_visible = active;
2415 }
2416
2417 static void toggle_message_cb(MainWindow *mainwin, guint action,
2418                               GtkWidget *widget)
2419 {
2420         gboolean active;
2421
2422         active = GTK_CHECK_MENU_ITEM(widget)->active;
2423
2424         if (active != messageview_is_visible(mainwin->messageview))
2425                 summary_toggle_view(mainwin->summaryview);
2426 }
2427
2428 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2429                               GtkWidget *widget)
2430 {
2431         toolbar_toggle(action, mainwin);
2432 }
2433
2434 void main_window_reply_cb(MainWindow *mainwin, guint action,
2435                           GtkWidget *widget)
2436 {
2437         MessageView *msgview = (MessageView*)mainwin->messageview;
2438         GSList *msginfo_list = NULL;
2439         gchar *body;
2440
2441         g_return_if_fail(msgview != NULL);
2442
2443         msginfo_list = summary_get_selection(mainwin->summaryview);
2444         g_return_if_fail(msginfo_list != NULL);
2445         
2446         body = messageview_get_selection(msgview);
2447         compose_reply_mode((ComposeMode)action, msginfo_list, body);
2448         g_free(body);
2449         g_slist_free(msginfo_list);
2450 }
2451
2452
2453 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2454                                 GtkWidget *widget)
2455 {
2456         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2457                 gtk_widget_show(mainwin->hbox_stat);
2458                 prefs_common.show_statusbar = TRUE;
2459         } else {
2460                 gtk_widget_hide(mainwin->hbox_stat);
2461                 prefs_common.show_statusbar = FALSE;
2462         }
2463 }
2464
2465 static void separate_widget_cb(MainWindow *mainwin, guint action,
2466                                GtkWidget *widget)
2467 {
2468         SeparateType type;
2469
2470         if (GTK_CHECK_MENU_ITEM(widget)->active)
2471                 type = mainwin->type | action;
2472         else
2473                 type = mainwin->type & ~action;
2474
2475         main_window_separation_change(mainwin, type);
2476
2477         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
2478         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
2479 }
2480
2481 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline)
2482 {
2483         if (offline)
2484                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
2485         else
2486                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
2487 }
2488
2489 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
2490 {
2491         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active);
2492 }
2493
2494 static void online_switch_clicked (GtkButton *btn, gpointer data) 
2495 {
2496         MainWindow *mainwin;
2497         GtkItemFactory *ifactory;
2498         GtkCheckMenuItem *menuitem;
2499
2500         mainwin = (MainWindow *) data;
2501         
2502         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2503         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
2504         
2505         g_return_if_fail(mainwin != NULL);
2506         g_return_if_fail(menuitem != NULL);
2507         
2508         if (btn == GTK_BUTTON(mainwin->online_switch)) {
2509                 /* go offline */
2510                 gtk_widget_hide (mainwin->online_switch);
2511                 gtk_widget_show (mainwin->offline_switch);
2512                 menuitem->active = TRUE;
2513                 prefs_common.work_offline = TRUE;
2514                 inc_autocheck_timer_remove();           
2515         } else {
2516                 /*go online */
2517                 gtk_widget_hide (mainwin->offline_switch);
2518                 gtk_widget_show (mainwin->online_switch);
2519                 menuitem->active = FALSE;
2520                 prefs_common.work_offline = FALSE;
2521                 inc_autocheck_timer_set();
2522         }
2523 }
2524
2525 static void addressbook_open_cb(MainWindow *mainwin, guint action,
2526                                 GtkWidget *widget)
2527 {
2528         addressbook_open(NULL);
2529 }
2530
2531 static void log_window_show_cb(MainWindow *mainwin, guint action,
2532                                GtkWidget *widget)
2533 {
2534         log_window_show(mainwin->logwin);
2535 }
2536
2537 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2538 {
2539         inc_cancel_all();
2540 }
2541
2542 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2543 {
2544         summary_move_to(mainwin->summaryview);
2545 }
2546
2547 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2548 {
2549         summary_copy_to(mainwin->summaryview);
2550 }
2551
2552 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2553 {
2554         summary_delete(mainwin->summaryview);
2555 }
2556
2557 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2558 {
2559         summary_cancel(mainwin->summaryview);
2560 }
2561
2562 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2563 {
2564         summary_open_msg(mainwin->summaryview);
2565 }
2566
2567 static void view_source_cb(MainWindow *mainwin, guint action,
2568                            GtkWidget *widget)
2569 {
2570         summary_view_source(mainwin->summaryview);
2571 }
2572
2573 static void show_all_header_cb(MainWindow *mainwin, guint action,
2574                                GtkWidget *widget)
2575 {
2576         if (mainwin->menu_lock_count) return;
2577         summary_display_msg_selected(mainwin->summaryview,
2578                                      GTK_CHECK_MENU_ITEM(widget)->active);
2579 }
2580
2581 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2582 {
2583         summary_mark(mainwin->summaryview);
2584 }
2585
2586 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2587 {
2588         summary_unmark(mainwin->summaryview);
2589 }
2590
2591 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
2592                               GtkWidget *widget)
2593 {
2594         summary_mark_as_unread(mainwin->summaryview);
2595 }
2596
2597 static void mark_as_read_cb(MainWindow *mainwin, guint action,
2598                             GtkWidget *widget)
2599 {
2600         summary_mark_as_read(mainwin->summaryview);
2601 }
2602
2603 static void mark_all_read_cb(MainWindow *mainwin, guint action,
2604                              GtkWidget *widget)
2605 {
2606         summary_mark_all_read(mainwin->summaryview);
2607 }
2608
2609 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2610 {
2611         summary_reedit(mainwin->summaryview);
2612 }
2613
2614 static void add_address_cb(MainWindow *mainwin, guint action,
2615                            GtkWidget *widget)
2616 {
2617         summary_add_address(mainwin->summaryview);
2618 }
2619
2620 static void set_charset_cb(MainWindow *mainwin, guint action,
2621                            GtkWidget *widget)
2622 {
2623         const gchar *str;
2624
2625         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2626                 str = conv_get_charset_str((CharSet)action);
2627                 g_free(prefs_common.force_charset);
2628                 prefs_common.force_charset = str ? g_strdup(str) : NULL;
2629
2630                 summary_redisplay_msg(mainwin->summaryview);
2631                 
2632                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
2633         }
2634 }
2635
2636 static void hide_read_messages (MainWindow *mainwin, guint action,
2637                                 GtkWidget *widget)
2638 {
2639         if (!mainwin->summaryview->folder_item
2640             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
2641                 return;
2642         summary_toggle_show_read_messages(mainwin->summaryview);
2643 }
2644
2645 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2646 {
2647         if (mainwin->menu_lock_count) return;
2648         if (!mainwin->summaryview->folder_item) return;
2649
2650         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2651                 summary_thread_build(mainwin->summaryview);
2652 /*              mainwin->summaryview->folder_item->threaded = TRUE; */
2653         } else {
2654                 summary_unthread(mainwin->summaryview);
2655 /*              mainwin->summaryview->folder_item->threaded = FALSE; */
2656         }
2657 }
2658
2659 static void expand_threads_cb(MainWindow *mainwin, guint action,
2660                               GtkWidget *widget)
2661 {
2662         summary_expand_threads(mainwin->summaryview);
2663 }
2664
2665 static void collapse_threads_cb(MainWindow *mainwin, guint action,
2666                                 GtkWidget *widget)
2667 {
2668         summary_collapse_threads(mainwin->summaryview);
2669 }
2670
2671 static void set_display_item_cb(MainWindow *mainwin, guint action,
2672                                 GtkWidget *widget)
2673 {
2674         prefs_summary_column_open();
2675 }
2676
2677 static void sort_summary_cb(MainWindow *mainwin, guint action,
2678                             GtkWidget *widget)
2679 {
2680         FolderItem *item = mainwin->summaryview->folder_item;
2681         GtkWidget *menuitem;
2682
2683         if (mainwin->menu_lock_count) return;
2684
2685         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
2686                 menuitem = gtk_item_factory_get_item
2687                         (mainwin->menu_factory, "/View/Sort/Ascending");
2688                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
2689                              GTK_CHECK_MENU_ITEM(menuitem)->active
2690                              ? SORT_ASCENDING : SORT_DESCENDING);
2691                 item->sort_key = action;
2692         }
2693 }
2694
2695 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
2696                                  GtkWidget *widget)
2697 {
2698         FolderItem *item = mainwin->summaryview->folder_item;
2699
2700         if (mainwin->menu_lock_count) return;
2701
2702         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
2703                 summary_sort(mainwin->summaryview,
2704                              item->sort_key, (FolderSortType)action);
2705 }
2706
2707 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
2708                                   GtkWidget *widget)
2709 {
2710         summary_attract_by_subject(mainwin->summaryview);
2711 }
2712
2713 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
2714                                  GtkWidget *widget)
2715 {
2716         FolderItem *item;
2717
2718         item = folderview_get_selected(mainwin->folderview);
2719         if (item) {
2720                 main_window_cursor_wait(mainwin);
2721                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
2722
2723                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
2724                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
2725
2726                 STATUSBAR_POP(mainwin);
2727                 main_window_cursor_normal(mainwin);
2728         }
2729 }
2730
2731 struct DelDupsData
2732 {
2733         guint   dups;
2734         guint   folders;
2735 };
2736
2737 static void deldup_all(FolderItem *item, gpointer _data)
2738 {
2739         struct DelDupsData *data = _data;
2740         gint result;
2741         
2742         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
2743         if (result >= 0) {
2744                 data->dups += result;
2745                 data->folders += 1;
2746         }
2747 }
2748
2749 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
2750                                  GtkWidget *widget)
2751 {
2752         struct DelDupsData data = {0, 0};
2753
2754         folder_func_to_all_folders(deldup_all, &data);
2755         alertpanel_notice(_("Deleted %d duplicate message(s) in %d folders.\n"),
2756                           data.dups, data.folders);
2757 }
2758
2759 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2760 {
2761         summary_filter(mainwin->summaryview, (gboolean)action);
2762 }
2763
2764 static void execute_summary_cb(MainWindow *mainwin, guint action,
2765                                GtkWidget *widget)
2766 {
2767         summary_execute(mainwin->summaryview);
2768 }
2769
2770 static void update_summary_cb(MainWindow *mainwin, guint action,
2771                               GtkWidget *widget)
2772 {
2773         FolderItem *fitem;
2774         FolderView *folderview = mainwin->folderview;
2775
2776         if (!mainwin->summaryview->folder_item) return;
2777         if (!folderview->opened) return;
2778
2779         folder_update_op_count();
2780
2781         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
2782                                             folderview->opened);
2783         if (!fitem) return;
2784
2785         folder_item_scan(fitem);
2786         summary_show(mainwin->summaryview, fitem);
2787 }
2788
2789 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2790 {
2791         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2792 }
2793
2794 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2795 {
2796         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2797 }
2798
2799 static void prev_unread_cb(MainWindow *mainwin, guint action,
2800                            GtkWidget *widget)
2801 {
2802         summary_select_prev_unread(mainwin->summaryview);
2803 }
2804
2805 static void next_unread_cb(MainWindow *mainwin, guint action,
2806                            GtkWidget *widget)
2807 {
2808         summary_select_next_unread(mainwin->summaryview);
2809 }
2810
2811 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2812 {
2813         summary_select_prev_new(mainwin->summaryview);
2814 }
2815
2816 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2817 {
2818         summary_select_next_new(mainwin->summaryview);
2819 }
2820
2821 static void prev_marked_cb(MainWindow *mainwin, guint action,
2822                            GtkWidget *widget)
2823 {
2824         summary_select_prev_marked(mainwin->summaryview);
2825 }
2826
2827 static void next_marked_cb(MainWindow *mainwin, guint action,
2828                            GtkWidget *widget)
2829 {
2830         summary_select_next_marked(mainwin->summaryview);
2831 }
2832
2833 static void prev_labeled_cb(MainWindow *mainwin, guint action,
2834                             GtkWidget *widget)
2835 {
2836         summary_select_prev_labeled(mainwin->summaryview);
2837 }
2838
2839 static void next_labeled_cb(MainWindow *mainwin, guint action,
2840                             GtkWidget *widget)
2841 {
2842         summary_select_next_labeled(mainwin->summaryview);
2843 }
2844
2845 static void goto_folder_cb(MainWindow *mainwin, guint action,
2846                            GtkWidget *widget)
2847 {
2848         FolderItem *to_folder;
2849
2850         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
2851
2852         if (to_folder)
2853                 folderview_select(mainwin->folderview, to_folder);
2854 }
2855
2856 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2857 {
2858         messageview_copy_clipboard(mainwin->messageview);
2859 }
2860
2861 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2862 {
2863         MessageView *msgview = mainwin->messageview;
2864
2865         if (GTK_WIDGET_HAS_FOCUS(mainwin->summaryview->ctree))
2866                 summary_select_all(mainwin->summaryview);
2867         else if (messageview_is_visible(msgview) &&
2868                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
2869                 messageview_select_all(mainwin->messageview);
2870 }
2871
2872 static void select_thread_cb(MainWindow *mainwin, guint action,
2873                              GtkWidget *widget)
2874 {
2875         summary_select_thread(mainwin->summaryview);
2876 }
2877
2878 static void create_filter_cb(MainWindow *mainwin, guint action,
2879                              GtkWidget *widget)
2880 {
2881         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
2882 }
2883
2884 static void create_processing_cb(MainWindow *mainwin, guint action,
2885                              GtkWidget *widget)
2886 {
2887         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
2888 }
2889
2890 static void prefs_common_open_cb(MainWindow *mainwin, guint action,
2891                                  GtkWidget *widget)
2892 {
2893         prefs_common_open();
2894 }
2895
2896 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
2897                                          GtkWidget *widget)
2898 {
2899         prefs_filtering_open(&pre_global_processing,
2900                              _("Processing rules to apply before folder rules"),
2901                              NULL, NULL);
2902 }
2903
2904 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
2905                                           GtkWidget *widget)
2906 {
2907         prefs_filtering_open(&post_global_processing,
2908                              _("Processing rules to apply after folder rules"),
2909                              NULL, NULL);
2910 }
2911
2912 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
2913                                     GtkWidget *widget)
2914 {
2915         prefs_filtering_open(&filtering_rules,
2916                              _("Filtering configuration"),
2917                              NULL, NULL);
2918 }
2919
2920 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
2921                                    GtkWidget *widget)
2922 {
2923         prefs_template_open();
2924 }
2925
2926 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
2927                                   GtkWidget *widget)
2928 {
2929         prefs_actions_open(mainwin);
2930 }
2931 #ifdef USE_OPENSSL
2932 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
2933                                   GtkWidget *widget)
2934 {
2935         ssl_manager_open(mainwin);
2936 }
2937 #endif
2938 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
2939                                   GtkWidget *widget)
2940 {
2941         if (!cur_account) {
2942                 new_account_cb(mainwin, 0, widget);
2943         } else {
2944                 account_open(cur_account);
2945         }
2946 }
2947
2948 static void new_account_cb(MainWindow *mainwin, guint action,
2949                            GtkWidget *widget)
2950 {
2951         account_edit_open();
2952         if (!compose_get_compose_list()) account_add();
2953 }
2954
2955 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
2956 {
2957         cur_account = (PrefsAccount *)data;
2958         main_window_reflect_prefs_all();
2959 }
2960
2961 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
2962 {
2963         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
2964         PrefsAccount *account = (PrefsAccount *)data;
2965
2966         inc_account_mail(mainwin, account);
2967 }
2968
2969 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
2970 {
2971         prefs_gtk_open();
2972 }
2973
2974 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
2975 {
2976         pluginwindow_create();
2977 }
2978
2979 static void manual_open_cb(MainWindow *mainwin, guint action,
2980                            GtkWidget *widget)
2981 {
2982         manual_open((ManualType)action);
2983 }
2984
2985 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
2986 {
2987         MainWindow *mainwin = (MainWindow *)data;
2988         gchar *str;
2989
2990         if (item->path)
2991                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
2992                                       LOCAL_FOLDER(folder)->rootpath,
2993                                       G_DIR_SEPARATOR,
2994                                       item->path);
2995         else
2996                 str = g_strdup_printf(_("Scanning folder %s ..."),
2997                                       LOCAL_FOLDER(folder)->rootpath);
2998
2999         STATUSBAR_PUSH(mainwin, str);
3000         STATUSBAR_POP(mainwin);
3001         g_free(str);
3002 }
3003
3004 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3005                                           gpointer data)
3006 {
3007         SummaryView *summary;
3008
3009         g_return_val_if_fail(data, FALSE);
3010         summary = ((MainWindow *)data)->summaryview;
3011         g_return_val_if_fail(summary, FALSE);
3012
3013         if (GTK_CLIST(summary->ctree)->selection && 
3014             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3015                 return FALSE;
3016
3017         if (summary->selected != summary->displayed)
3018                 summary_select_node(summary, summary->displayed, FALSE, TRUE);
3019         return FALSE;
3020 }
3021
3022 #define BREAK_ON_MODIFIER_KEY() \
3023         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3024
3025 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3026                                     gpointer data)
3027 {
3028         MainWindow *mainwin = (MainWindow*) data;
3029         
3030         if (!mainwin || !event) 
3031                 return FALSE;
3032
3033         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3034                 return FALSE;
3035
3036         switch (event->keyval) {
3037         case GDK_Q:             /* Quit */
3038                 BREAK_ON_MODIFIER_KEY();
3039
3040                 app_exit_cb(mainwin, 0, NULL);
3041                 return FALSE;
3042         case GDK_space:
3043                 if (mainwin->folderview && mainwin->summaryview
3044                     && !mainwin->summaryview->displayed) {
3045                         summary_lock(mainwin->summaryview);
3046                         folderview_select_next_unread(mainwin->folderview);
3047                         summary_unlock(mainwin->summaryview);
3048                 }
3049                 break;
3050         default:
3051                 break;
3052         }
3053         return FALSE;
3054 }
3055
3056 #undef BREAK_ON_MODIFIER_KEY
3057
3058 /*
3059  * Harvest addresses for selected folder.
3060  */
3061 static void addr_harvest_cb( MainWindow *mainwin,
3062                             guint action,
3063                             GtkWidget *widget )
3064 {
3065         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3066 }
3067
3068 /*
3069  * Harvest addresses for selected messages in summary view.
3070  */
3071 static void addr_harvest_msg_cb( MainWindow *mainwin,
3072                             guint action,
3073                             GtkWidget *widget )
3074 {
3075         summary_harvest_address( mainwin->summaryview );
3076 }
3077
3078 /*!
3079  *\brief        get a MainWindow
3080  *
3081  *\return       MainWindow * The first mainwindow in the mainwin_list
3082  */
3083 MainWindow *mainwindow_get_mainwindow(void)
3084 {
3085         if (mainwin_list && mainwin_list->data)
3086                 return (MainWindow *)(mainwin_list->data);
3087         else
3088                 return NULL;
3089 }
3090
3091 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3092 {
3093         ProgressData *data = (ProgressData *) source;
3094         MainWindow *mainwin = (MainWindow *) userdata;
3095
3096         switch (data->cmd) {
3097         case PROGRESS_COMMAND_START:
3098         case PROGRESS_COMMAND_STOP:
3099                 gtk_progress_bar_set_fraction
3100                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3101                 break;
3102         case PROGRESS_COMMAND_SET_PERCENTAGE:
3103                 gtk_progress_bar_set_fraction
3104                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3105                 break;          
3106         }
3107         while (gtk_events_pending()) gtk_main_iteration ();
3108
3109         return FALSE;
3110 }
3111
3112 /*
3113 * End of Source.
3114 */
3115