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