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