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