0359db76b54b9bb136e9807ff985737ba47f5b25
[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 static guint prefs_tag = 0;
1217
1218 void main_window_reflect_prefs_all(void)
1219 {
1220         main_window_reflect_prefs_all_real(FALSE);
1221 }
1222
1223 static gboolean reflect_prefs_timeout_cb(gpointer data) 
1224 {
1225         gboolean pixmap_theme_changed = GPOINTER_TO_INT(data);
1226         GList *cur;
1227         MainWindow *mainwin;
1228         GtkWidget *pixmap;
1229
1230         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1231                 mainwin = (MainWindow *)cur->data;
1232
1233                 main_window_show_cur_account(mainwin);
1234                 main_window_set_menu_sensitive(mainwin);
1235                 toolbar_main_set_sensitive(mainwin);
1236
1237                 /* pixmap themes */
1238                 if (pixmap_theme_changed) {
1239                         toolbar_update(TOOLBAR_MAIN, mainwin);
1240                         messageview_reflect_prefs_pixmap_theme();
1241                         compose_reflect_prefs_pixmap_theme();
1242                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1243                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1244
1245                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1246                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1247                                              mainwin->online_pixmap);
1248                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1249                         gtk_widget_show(pixmap);
1250                         mainwin->online_pixmap = pixmap;
1251                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1252                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1253                                              mainwin->offline_pixmap);
1254                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1255                         gtk_widget_show(pixmap);
1256                         mainwin->offline_pixmap = pixmap;
1257                 }
1258                 
1259                 headerview_set_visibility(mainwin->messageview->headerview,
1260                                           prefs_common.display_header_pane);
1261
1262                 textview_reflect_prefs(mainwin->messageview->mimeview->textview);
1263                 folderview_reflect_prefs();
1264                 summary_reflect_prefs();
1265                 summary_redisplay_msg(mainwin->summaryview);
1266         }
1267         prefs_tag = 0;
1268         return FALSE;
1269 }
1270
1271 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1272 {
1273         if (prefs_tag == 0 || pixmap_theme_changed) {
1274                 prefs_tag = g_timeout_add(500, reflect_prefs_timeout_cb, 
1275                                                 GINT_TO_POINTER(pixmap_theme_changed));
1276         }
1277 }
1278
1279 void main_window_set_summary_column(void)
1280 {
1281         GList *cur;
1282         MainWindow *mainwin;
1283
1284         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1285                 mainwin = (MainWindow *)cur->data;
1286                 summary_set_column_order(mainwin->summaryview);
1287         }
1288 }
1289
1290 static void main_window_set_account_selector_menu(MainWindow *mainwin,
1291                                                   GList *account_list)
1292 {
1293         GList *cur_ac, *cur_item;
1294         GtkWidget *menuitem;
1295         PrefsAccount *ac_prefs;
1296
1297         /* destroy all previous menu item */
1298         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
1299         while (cur_item != NULL) {
1300                 GList *next = cur_item->next;
1301                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1302                 cur_item = next;
1303         }
1304
1305         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1306                 ac_prefs = (PrefsAccount *)cur_ac->data;
1307
1308                 menuitem = gtk_menu_item_new_with_label
1309                         (ac_prefs->account_name
1310                          ? ac_prefs->account_name : _("Untitled"));
1311                 gtk_widget_show(menuitem);
1312                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
1313                 g_signal_connect(G_OBJECT(menuitem), "activate",
1314                                  G_CALLBACK(account_selector_menu_cb),
1315                                  ac_prefs);
1316         }
1317 }
1318
1319 static void main_window_set_account_receive_menu(MainWindow *mainwin,
1320                                                  GList *account_list)
1321 {
1322         GList *cur_ac, *cur_item;
1323         GtkWidget *menu;
1324         GtkWidget *menuitem;
1325         PrefsAccount *ac_prefs;
1326
1327         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
1328                                            "/Message/Receive");
1329
1330         /* search for separator */
1331         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1332              cur_item = cur_item->next) {
1333                 if (GTK_BIN(cur_item->data)->child == NULL) {
1334                         cur_item = cur_item->next;
1335                         break;
1336                 }
1337         }
1338
1339         /* destroy all previous menu item */
1340         while (cur_item != NULL) {
1341                 GList *next = cur_item->next;
1342                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1343                 cur_item = next;
1344         }
1345
1346         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1347                 ac_prefs = (PrefsAccount *)cur_ac->data;
1348
1349                 menuitem = gtk_menu_item_new_with_label
1350                         (ac_prefs->account_name ? ac_prefs->account_name
1351                          : _("Untitled"));
1352                 gtk_widget_show(menuitem);
1353                 gtk_menu_append(GTK_MENU(menu), menuitem);
1354                 g_signal_connect(G_OBJECT(menuitem), "activate",
1355                                  G_CALLBACK(account_receive_menu_cb),
1356                                  ac_prefs);
1357         }
1358 }
1359
1360 void main_window_set_account_menu(GList *account_list)
1361 {
1362         GList *cur;
1363         MainWindow *mainwin;
1364
1365         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1366                 mainwin = (MainWindow *)cur->data;
1367                 main_window_set_account_selector_menu(mainwin, account_list);
1368                 main_window_set_account_receive_menu(mainwin, account_list);
1369         }
1370 }
1371
1372 static void main_window_show_cur_account(MainWindow *mainwin)
1373 {
1374         gchar *buf;
1375         gchar *ac_name;
1376
1377         ac_name = g_strdup(cur_account
1378                            ? (cur_account->account_name
1379                               ? cur_account->account_name : _("Untitled"))
1380                            : _("none"));
1381
1382         if (cur_account)
1383                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
1384         else
1385                 buf = g_strdup(PROG_VERSION);
1386         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
1387         g_free(buf);
1388
1389         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
1390         gtk_widget_queue_resize(mainwin->ac_button);
1391
1392         g_free(ac_name);
1393 }
1394
1395 GtkWidget *main_window_get_folder_window(MainWindow *mainwin)
1396 {
1397         switch (mainwin->type) {
1398         case SEPARATE_FOLDER:
1399                 return mainwin->win.sep_folder.folderwin;
1400         case SEPARATE_BOTH:
1401                 return mainwin->win.sep_both.folderwin;
1402         default:
1403                 return NULL;
1404         }
1405 }
1406
1407 GtkWidget *main_window_get_message_window(MainWindow *mainwin)
1408 {
1409         switch (mainwin->type) {
1410         case SEPARATE_MESSAGE:
1411                 return mainwin->win.sep_message.messagewin;
1412         case SEPARATE_BOTH:
1413                 return mainwin->win.sep_both.messagewin;
1414         default:
1415                 return NULL;
1416         }
1417 }
1418
1419 void main_window_separation_change(MainWindow *mainwin, SeparateType type)
1420 {
1421         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
1422         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
1423         /* GtkWidget *message_wid = GTK_WIDGET_PTR(mainwin->messageview); */
1424         GtkWidget *message_wid = mainwin->messageview->vbox;
1425
1426         debug_print("Changing window separation type from %d to %d\n",
1427                     mainwin->type, type);
1428
1429         if (mainwin->type == type) return;
1430
1431         /* remove widgets from those containers */
1432         gtk_widget_ref(folder_wid);
1433         gtk_widget_ref(summary_wid);
1434         gtk_widget_ref(message_wid);
1435         gtkut_container_remove
1436                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1437         gtkut_container_remove
1438                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1439         gtkut_container_remove
1440                 (GTK_CONTAINER(message_wid->parent), message_wid);
1441
1442         /* clean containers */
1443         switch (mainwin->type) {
1444         case SEPARATE_NONE:
1445                 gtk_widget_destroy(mainwin->win.sep_none.hpaned);
1446                 break;
1447         case SEPARATE_FOLDER:
1448                 gtk_widget_destroy(mainwin->win.sep_folder.vpaned);
1449                 gtk_widget_destroy(mainwin->win.sep_folder.folderwin);
1450                 break;
1451         case SEPARATE_MESSAGE:
1452                 gtk_widget_destroy(mainwin->win.sep_message.hpaned);
1453                 gtk_widget_destroy(mainwin->win.sep_message.messagewin);
1454                 break;
1455         case SEPARATE_BOTH:
1456                 gtk_widget_destroy(mainwin->win.sep_both.messagewin);
1457                 gtk_widget_destroy(mainwin->win.sep_both.folderwin);
1458                 break;
1459         }
1460
1461         gtk_widget_hide(mainwin->window);
1462         main_window_set_widgets(mainwin, type);
1463         gtk_widget_show(mainwin->window);
1464
1465         gtk_widget_unref(folder_wid);
1466         gtk_widget_unref(summary_wid);
1467         gtk_widget_unref(message_wid);
1468 }
1469
1470 void main_window_toggle_message_view(MainWindow *mainwin)
1471 {
1472         SummaryView *summaryview = mainwin->summaryview;
1473         union CompositeWin *cwin = &mainwin->win;
1474         GtkWidget *vpaned = NULL;
1475         GtkWidget *container = NULL;
1476         GtkWidget *msgwin = NULL;
1477
1478         switch (mainwin->type) {
1479         case SEPARATE_NONE:
1480                 vpaned = cwin->sep_none.vpaned;
1481                 container = cwin->sep_none.hpaned;
1482                 break;
1483         case SEPARATE_FOLDER:
1484                 vpaned = cwin->sep_folder.vpaned;
1485                 container = mainwin->vbox_body;
1486                 break;
1487         case SEPARATE_MESSAGE:
1488                 msgwin = mainwin->win.sep_message.messagewin;
1489                 break;
1490         case SEPARATE_BOTH:
1491                 msgwin = mainwin->win.sep_both.messagewin;
1492                 break;
1493         }
1494
1495         if (msgwin) {
1496                 if (GTK_WIDGET_VISIBLE(msgwin)) {
1497                         gtk_widget_hide(msgwin);
1498                         mainwin->messageview->visible = FALSE;
1499                         summaryview->displayed = NULL;
1500                 } else {
1501                         gtk_widget_show(msgwin);
1502                         mainwin->messageview->visible = TRUE;
1503                 }
1504         } else if (vpaned->parent != NULL) {
1505                 mainwin->messageview->visible = FALSE;
1506                 summaryview->displayed = NULL;
1507                 gtk_widget_ref(vpaned);
1508                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1509                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1510         } else {
1511                 mainwin->messageview->visible = TRUE;
1512                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1513                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1514                 gtk_widget_unref(vpaned);
1515         }
1516
1517         if (messageview_is_visible(mainwin->messageview))
1518                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1519                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1520         else
1521                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1522                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1523
1524         if (mainwin->messageview->visible == FALSE)
1525                 messageview_clear(mainwin->messageview);
1526
1527         main_window_set_menu_sensitive(mainwin);
1528
1529         prefs_common.msgview_visible = mainwin->messageview->visible;
1530
1531         gtk_widget_grab_focus(summaryview->ctree);
1532 }
1533
1534 void main_window_get_size(MainWindow *mainwin)
1535 {
1536         GtkAllocation *allocation;
1537
1538         if (mainwin->messageview == NULL) {
1539                 debug_print("called after messageview "
1540                             "has been deallocated!\n");
1541                 return;
1542         }
1543
1544         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1545
1546         if (allocation->width > 1 && allocation->height > 1) {
1547                 prefs_common.summaryview_width = allocation->width;
1548
1549                 if ((mainwin->type == SEPARATE_NONE ||
1550                      mainwin->type == SEPARATE_FOLDER) &&
1551                     messageview_is_visible(mainwin->messageview))
1552                         prefs_common.summaryview_height = allocation->height;
1553
1554                 prefs_common.mainview_width = allocation->width;
1555         }
1556
1557         allocation = &mainwin->window->allocation;
1558         if (allocation->width > 1 && allocation->height > 1) {
1559                 prefs_common.mainview_height = allocation->height;
1560                 prefs_common.mainwin_width   = allocation->width;
1561                 prefs_common.mainwin_height  = allocation->height;
1562         }
1563
1564         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1565         if (allocation->width > 1 && allocation->height > 1) {
1566                 prefs_common.folderview_width  = allocation->width;
1567                 prefs_common.folderview_height = allocation->height;
1568         }
1569
1570         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
1571         if (allocation->width > 1 && allocation->height > 1) {
1572                 prefs_common.msgview_width = allocation->width;
1573                 prefs_common.msgview_height = allocation->height;
1574         }
1575
1576 /*      debug_print("summaryview size: %d x %d\n",
1577                     prefs_common.summaryview_width,
1578                     prefs_common.summaryview_height);
1579         debug_print("folderview size: %d x %d\n",
1580                     prefs_common.folderview_width,
1581                     prefs_common.folderview_height);
1582         debug_print("messageview size: %d x %d\n",
1583                     prefs_common.msgview_width,
1584                     prefs_common.msgview_height); */
1585 }
1586
1587 void main_window_get_position(MainWindow *mainwin)
1588 {
1589         gint x, y;
1590         GtkWidget *window;
1591
1592         gtkut_widget_get_uposition(mainwin->window, &x, &y);
1593
1594         prefs_common.mainview_x = x;
1595         prefs_common.mainview_y = y;
1596         prefs_common.mainwin_x = x;
1597         prefs_common.mainwin_y = y;
1598
1599         debug_print("main window position: %d, %d\n", x, y);
1600
1601         window = main_window_get_folder_window(mainwin);
1602         if (window) {
1603                 gtkut_widget_get_uposition(window, &x, &y);
1604                 prefs_common.folderwin_x = x;
1605                 prefs_common.folderwin_y = y;
1606                 debug_print("folder window position: %d, %d\n", x, y);
1607         }
1608         window = main_window_get_message_window(mainwin);
1609         if (window) {
1610                 gtkut_widget_get_uposition(window, &x, &y);
1611                 prefs_common.main_msgwin_x = x;
1612                 prefs_common.main_msgwin_y = y;
1613                 debug_print("message window position: %d, %d\n", x, y);
1614         }
1615 }
1616
1617 void main_window_progress_on(MainWindow *mainwin)
1618 {
1619         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1620 }
1621
1622 void main_window_progress_off(MainWindow *mainwin)
1623 {
1624         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
1625         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1626 }
1627
1628 void main_window_progress_set(MainWindow *mainwin, gint cur, gint total)
1629 {
1630         gchar buf[32];
1631
1632         g_snprintf(buf, sizeof(buf), "%d / %d", cur, total);
1633         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), buf);
1634         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar),
1635                                 (cur == 0 && total == 0) ? 0 :
1636                                 (gfloat)cur / (gfloat)total);
1637 }
1638
1639 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
1640 {
1641         GList *list;
1642         guint has_trash;
1643         Folder *folder;
1644
1645         for (has_trash = 0, list = folder_get_list(); list != NULL; list = list->next) {
1646                 folder = FOLDER(list->data);
1647                 if (folder && folder->trash && folder->trash->total_msgs > 0)
1648                         has_trash++;
1649         }
1650
1651         if (!has_trash) return;
1652         
1653         if (confirm) {
1654                 if (alertpanel(_("Empty trash"),
1655                                _("Empty all messages in trash?"),
1656                                GTK_STOCK_YES, GTK_STOCK_NO, NULL)
1657                     != G_ALERTDEFAULT)
1658                         return;
1659                 manage_window_focus_in(mainwin->window, NULL, NULL);
1660         }
1661
1662         procmsg_empty_all_trash();
1663
1664         if (mainwin->summaryview->folder_item &&
1665             mainwin->summaryview->folder_item->stype == F_TRASH)
1666                 gtk_widget_grab_focus(mainwin->folderview->ctree);
1667 }
1668
1669 void main_window_add_mailbox(MainWindow *mainwin)
1670 {
1671         gchar *path;
1672         Folder *folder;
1673
1674         path = input_dialog(_("Add mailbox"),
1675                             _("Input the location of mailbox.\n"
1676                               "If the existing mailbox is specified, it will be\n"
1677                               "scanned automatically."),
1678                             "Mail");
1679         if (!path) return;
1680         if (folder_find_from_path(path)) {
1681                 alertpanel_error(_("The mailbox '%s' already exists."), path);
1682                 g_free(path);
1683                 return;
1684         }
1685         folder = folder_new(folder_get_class_from_string("mh"), 
1686                             !strcmp(path, "Mail") ? _("Mailbox") : 
1687                             g_path_get_basename(path), path);
1688         g_free(path);
1689
1690         if (folder->klass->create_tree(folder) < 0) {
1691                 alertpanel_error(_("Creation of the mailbox failed.\n"
1692                                    "Maybe some files already exist, or you don't have the permission to write there."));
1693                 folder_destroy(folder);
1694                 return;
1695         }
1696
1697         folder_add(folder);
1698         folder_set_ui_func(folder, scan_tree_func, mainwin);
1699         folder_scan_tree(folder);
1700         folder_set_ui_func(folder, NULL, NULL);
1701 }
1702
1703 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
1704 {
1705         SensitiveCond state = 0;
1706         SummarySelection selection;
1707         FolderItem *item = mainwin->summaryview->folder_item;
1708         GList *account_list = account_get_list();
1709         
1710         selection = summary_get_selection_type(mainwin->summaryview);
1711
1712         if (mainwin->lock_count == 0)
1713                 state |= M_UNLOCKED;
1714         if (selection != SUMMARY_NONE)
1715                 state |= M_MSG_EXIST;
1716         if (item && item->path && folder_item_parent(item) && !item->no_select) {
1717                 state |= M_EXEC;
1718                 /*              if (item->folder->type != F_NEWS) */
1719                 state |= M_ALLOW_DELETE;
1720
1721                 if (prefs_common.immediate_exec == 0
1722                     && mainwin->lock_count == 0)
1723                         state |= M_DELAY_EXEC;
1724
1725                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
1726                     || selection != SUMMARY_NONE)
1727                         state |= M_HIDE_READ_MSG;       
1728         }               
1729         if (mainwin->summaryview->threaded)
1730                 state |= M_THREADED;
1731         else
1732                 state |= M_UNTHREADED;  
1733         if (selection == SUMMARY_SELECTED_SINGLE ||
1734             selection == SUMMARY_SELECTED_MULTIPLE)
1735                 state |= M_TARGET_EXIST;
1736         if (selection == SUMMARY_SELECTED_SINGLE)
1737                 state |= M_SINGLE_TARGET_EXIST;
1738         if (mainwin->summaryview->folder_item &&
1739             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
1740                 state |= M_NEWS;
1741         else
1742                 state |= M_NOT_NEWS;
1743         if (selection == SUMMARY_SELECTED_SINGLE &&
1744             (item &&
1745              (item->stype == F_OUTBOX || item->stype == F_DRAFT ||
1746               item->stype == F_QUEUE)))
1747                 state |= M_ALLOW_REEDIT;
1748         if (cur_account)
1749                 state |= M_HAVE_ACCOUNT;
1750         
1751         for ( ; account_list != NULL; account_list = account_list->next) {
1752                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
1753                         state |= M_HAVE_NEWS_ACCOUNT;
1754                         break;
1755                 }
1756         }
1757
1758         if (inc_is_active())
1759                 state |= M_INC_ACTIVE;
1760
1761         return state;
1762 }
1763
1764
1765
1766 void main_window_set_menu_sensitive(MainWindow *mainwin)
1767 {
1768         GtkItemFactory *ifactory = mainwin->menu_factory;
1769         SensitiveCond state;
1770         gboolean sensitive;
1771         GtkWidget *menu;
1772         GtkWidget *menuitem;
1773         SummaryView *summaryview;
1774         gchar *menu_path;
1775         gint i;
1776         GList *cur_item;
1777
1778         static const struct {
1779                 gchar *const entry;
1780                 SensitiveCond cond;
1781         } entry[] = {
1782                 {"/File/Add mailbox"                          , M_UNLOCKED},
1783
1784                 {"/File/Add mailbox/MH..."                    , M_UNLOCKED},
1785                 {"/File/Change folder order"                  , M_UNLOCKED},
1786                 {"/File/Export to mbox file..."               , M_UNLOCKED},
1787                 {"/File/Empty all Trash folders"              , M_UNLOCKED},
1788                 {"/File/Work offline"                         , M_UNLOCKED},
1789
1790                 {"/File/Save as...", M_TARGET_EXIST|M_UNLOCKED},
1791                 {"/File/Print..."  , M_TARGET_EXIST|M_UNLOCKED},
1792                 /* {"/File/Close"  , M_UNLOCKED}, */
1793                 {"/File/Exit"      , M_UNLOCKED},
1794
1795                 {"/Edit/Select thread"             , M_SINGLE_TARGET_EXIST},
1796
1797                 {"/View/Sort"                      , M_EXEC},
1798                 {"/View/Thread view"               , M_EXEC},
1799                 {"/View/Expand all threads"        , M_MSG_EXIST},
1800                 {"/View/Collapse all threads"      , M_MSG_EXIST},
1801                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
1802                 {"/View/Go to/Prev message"        , M_MSG_EXIST},
1803                 {"/View/Go to/Next message"        , M_MSG_EXIST},
1804                 {"/View/Go to/Prev unread message" , M_MSG_EXIST},
1805                 {"/View/Go to/Prev new message"    , M_MSG_EXIST},
1806                 {"/View/Go to/Prev marked message" , M_MSG_EXIST},
1807                 {"/View/Go to/Next marked message" , M_MSG_EXIST},
1808                 {"/View/Go to/Prev labeled message", M_MSG_EXIST},
1809                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
1810                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
1811                 {"/View/Show all headers"          , M_SINGLE_TARGET_EXIST},
1812                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
1813
1814                 {"/Message/Receive/Get from current account"
1815                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1816                 {"/Message/Receive/Get from all accounts"
1817                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1818                 {"/Message/Receive/Cancel receiving"
1819                                                  , M_INC_ACTIVE},
1820
1821                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
1822                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1823                 {"/Message/Reply to"              , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1824                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1825                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1826                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1827                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1828                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1829                 {"/Message/Move to trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1830                 {"/Message/Delete..."             , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1831                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1832                 {"/Message/Mark"                  , M_TARGET_EXIST},
1833                 {"/Message/Re-edit"               , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1834
1835                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
1836                 {"/Tools/Harvest addresses"            , M_UNLOCKED},
1837                 {"/Tools/Harvest addresses/from Folder..."       
1838                                                        , M_UNLOCKED|M_MSG_EXIST},
1839                 {"/Tools/Harvest addresses/from Messages..."
1840                                                        , M_UNLOCKED|M_MSG_EXIST|M_TARGET_EXIST},
1841                 {"/Tools/Harvest addresses"            , M_UNLOCKED},
1842                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1843                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1844                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1845                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_UNLOCKED},
1846                 {"/Tools/Execute"                      , M_DELAY_EXEC},
1847                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1848
1849                 {"/Configuration", M_UNLOCKED},
1850
1851                 {NULL, 0}
1852         };
1853
1854         state = main_window_get_current_state(mainwin);
1855
1856         for (i = 0; entry[i].entry != NULL; i++) {
1857                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1858                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1859         }
1860
1861         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
1862
1863         /* search for separator */
1864         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1865              cur_item = cur_item->next) {
1866                 if (GTK_BIN(cur_item->data)->child == NULL) {
1867                         cur_item = cur_item->next;
1868                         break;
1869                 }
1870         }
1871
1872         for (; cur_item != NULL; cur_item = cur_item->next) {
1873                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
1874                                          (M_UNLOCKED & state) != 0);
1875         }
1876
1877         main_window_menu_callback_block(mainwin);
1878
1879 #define SET_CHECK_MENU_ACTIVE(path, active) \
1880 { \
1881         menuitem = gtk_item_factory_get_widget(ifactory, path); \
1882         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
1883 }
1884
1885         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
1886                               messageview_is_visible(mainwin->messageview));
1887
1888         summaryview = mainwin->summaryview;
1889         menu_path = "/View/Sort/Don't sort";
1890
1891         switch (summaryview->sort_key) {
1892         case SORT_BY_NUMBER:
1893                 menu_path = "/View/Sort/by number"; break;
1894         case SORT_BY_SIZE:
1895                 menu_path = "/View/Sort/by size"; break;
1896         case SORT_BY_DATE:
1897                 menu_path = "/View/Sort/by date"; break;
1898         case SORT_BY_FROM:
1899                 menu_path = "/View/Sort/by from"; break;
1900         case SORT_BY_TO:
1901                 menu_path = "/View/Sort/by recipient"; break;
1902         case SORT_BY_SUBJECT:
1903                 menu_path = "/View/Sort/by subject"; break;
1904         case SORT_BY_LABEL:
1905                 menu_path = "/View/Sort/by color label"; break;
1906         case SORT_BY_MARK:
1907                 menu_path = "/View/Sort/by mark"; break;
1908         case SORT_BY_STATUS:
1909                 menu_path = "/View/Sort/by status"; break;
1910         case SORT_BY_MIME:
1911                 menu_path = "/View/Sort/by attachment"; break;
1912         case SORT_BY_SCORE:
1913                 menu_path = "/View/Sort/by score"; break;
1914         case SORT_BY_LOCKED:
1915                 menu_path = "/View/Sort/by locked"; break;
1916         case SORT_BY_NONE:
1917         default:
1918                 menu_path = "/View/Sort/Don't sort"; break;
1919         }
1920         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
1921
1922         if (summaryview->sort_type == SORT_ASCENDING) {
1923                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
1924         } else {
1925                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
1926         }
1927
1928         if (summaryview->sort_key != SORT_BY_NONE) {
1929                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
1930                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
1931         } else {
1932                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
1933                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
1934         }
1935
1936         SET_CHECK_MENU_ACTIVE("/View/Show all headers",
1937                               mainwin->messageview->mimeview->textview->show_all_headers);
1938         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
1939
1940 #undef SET_CHECK_MENU_ACTIVE
1941
1942         main_window_menu_callback_unblock(mainwin);
1943 }
1944
1945 void main_window_popup(MainWindow *mainwin)
1946 {
1947         gtkut_window_popup(mainwin->window);
1948
1949         switch (mainwin->type) {
1950         case SEPARATE_FOLDER:
1951                 gtkut_window_popup(mainwin->win.sep_folder.folderwin);
1952                 break;
1953         case SEPARATE_MESSAGE:
1954                 gtkut_window_popup(mainwin->win.sep_message.messagewin);
1955                 break;
1956         case SEPARATE_BOTH:
1957                 gtkut_window_popup(mainwin->win.sep_both.folderwin);
1958                 gtkut_window_popup(mainwin->win.sep_both.messagewin);
1959                 break;
1960         default:
1961                 break;
1962         }
1963 }
1964
1965 void main_window_show(MainWindow *mainwin)
1966 {
1967         gtk_widget_show(mainwin->window);
1968         gtk_widget_show(mainwin->vbox_body);
1969
1970         gtk_widget_set_uposition(mainwin->window,
1971                                  prefs_common.mainwin_x,
1972                                  prefs_common.mainwin_y);
1973
1974         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
1975                              prefs_common.folderview_width,
1976                              prefs_common.folderview_height);
1977         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
1978                              prefs_common.summaryview_width,
1979                              prefs_common.summaryview_height);
1980         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
1981                              prefs_common.msgview_width,
1982                              prefs_common.msgview_height);
1983
1984         if (mainwin->type & SEPARATE_FOLDER) {
1985                 GtkWidget *folderwin;
1986
1987                 folderwin = main_window_get_folder_window(mainwin);
1988                 gtk_widget_show(folderwin);
1989                 gtk_widget_set_uposition(folderwin, prefs_common.folderwin_x,
1990                                          prefs_common.folderwin_y);
1991         }
1992
1993         if (mainwin->type & SEPARATE_MESSAGE) {
1994                 GtkWidget *messagewin;
1995
1996                 messagewin = main_window_get_message_window(mainwin);
1997                 gtk_widget_show(messagewin);
1998                 gtk_widget_set_uposition(messagewin, prefs_common.main_msgwin_x,
1999                                          prefs_common.main_msgwin_y);
2000         }
2001 }
2002
2003 void main_window_hide(MainWindow *mainwin)
2004 {
2005         main_window_get_size(mainwin);
2006         main_window_get_position(mainwin);
2007
2008         gtk_widget_hide(mainwin->window);
2009         gtk_widget_hide(mainwin->vbox_body);
2010
2011         if (mainwin->type & SEPARATE_FOLDER) {
2012                 gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2013         }
2014
2015         if (mainwin->type & SEPARATE_MESSAGE) {
2016                 gtk_widget_hide(mainwin->win.sep_message.messagewin);
2017         }
2018 }
2019
2020 static void main_window_set_widgets(MainWindow *mainwin, SeparateType type)
2021 {
2022         GtkWidget *folderwin = NULL;
2023         GtkWidget *messagewin = NULL;
2024         GtkWidget *hpaned;
2025         GtkWidget *vpaned;
2026         GtkWidget *vbox_body = mainwin->vbox_body;
2027         GtkItemFactory *ifactory = mainwin->menu_factory;
2028         GtkWidget *menuitem;
2029         GtkItemFactory *msgview_ifactory;
2030
2031         debug_print("Setting widgets... ");
2032
2033         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2034                                     prefs_common.folderview_width,
2035                                     prefs_common.folderview_height);
2036         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2037                                     prefs_common.summaryview_width,
2038                                     prefs_common.summaryview_height);
2039         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2040                                     prefs_common.msgview_width,
2041                                     prefs_common.msgview_height);
2042
2043         /* create separated window(s) if needed */
2044         if (type & SEPARATE_FOLDER) {
2045                 static GdkGeometry folderwin_geometry;
2046                 
2047                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2048                 gtk_window_set_title(GTK_WINDOW(folderwin),
2049                                      _("Sylpheed - Folder View"));
2050
2051                 gtk_window_move(GTK_WINDOW(folderwin),
2052                                 prefs_common.folderwin_x,
2053                                 prefs_common.folderwin_y);
2054
2055                 if (!folderwin_geometry.min_height) {
2056                         folderwin_geometry.min_width = 320;
2057                         folderwin_geometry.min_height = 200;
2058                 }
2059                 gtk_window_set_geometry_hints(GTK_WINDOW(folderwin), NULL,
2060                                               &folderwin_geometry, GDK_HINT_MIN_SIZE);
2061                                 
2062                 gtk_widget_set_size_request(folderwin,
2063                                             prefs_common.folderview_width,
2064                                             prefs_common.folderview_height);
2065
2066                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
2067                                                BORDER_WIDTH);
2068
2069                 g_signal_connect(G_OBJECT(folderwin), "delete_event",
2070                                  G_CALLBACK(folder_window_close_cb),
2071                                    mainwin);
2072                 gtk_container_add(GTK_CONTAINER(folderwin),
2073                                   GTK_WIDGET_PTR(mainwin->folderview));
2074                 gtk_widget_realize(folderwin);
2075
2076                 if (prefs_common.folderview_visible)
2077                         gtk_widget_show(folderwin);
2078         }
2079
2080         if (type & SEPARATE_MESSAGE) {
2081                 static GdkGeometry msgwin_geometry;
2082                 
2083                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2084                 gtk_window_set_title(GTK_WINDOW(messagewin),
2085                                      _("Sylpheed - Message View"));
2086                                      
2087                 gtk_window_move(GTK_WINDOW(messagewin), 
2088                                 prefs_common.main_msgwin_x,
2089                                 prefs_common.main_msgwin_y);
2090
2091                 if (!msgwin_geometry.min_height) {
2092                         msgwin_geometry.min_width = 320;
2093                         msgwin_geometry.min_height = 200;
2094                 }
2095                 gtk_window_set_geometry_hints(GTK_WINDOW(messagewin), NULL,
2096                                               &msgwin_geometry, GDK_HINT_MIN_SIZE);
2097                 
2098                 gtk_widget_set_size_request(messagewin, 
2099                                             prefs_common.msgwin_width,
2100                                             prefs_common.msgwin_height);
2101
2102                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
2103                                                BORDER_WIDTH);
2104                 g_signal_connect(G_OBJECT(messagewin), "delete_event",
2105                                  G_CALLBACK(message_window_close_cb),
2106                                  mainwin);
2107                 if (messageview_is_visible(mainwin->messageview))
2108                         gtk_widget_show(messagewin);
2109         } else {
2110                 mainwin->messageview->statusbar = mainwin->statusbar;
2111                 mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2112         }
2113
2114         switch (type) {
2115         case SEPARATE_NONE:
2116                 hpaned = gtk_hpaned_new();
2117                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2118                 gtk_paned_add1(GTK_PANED(hpaned),
2119                                GTK_WIDGET_PTR(mainwin->folderview));
2120                 gtk_widget_show(hpaned);
2121                 gtk_widget_queue_resize(hpaned);
2122
2123                 vpaned = gtk_vpaned_new();
2124                 if (messageview_is_visible(mainwin->messageview)) {
2125                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2126                         gtk_paned_add1(GTK_PANED(vpaned),
2127                                        GTK_WIDGET_PTR(mainwin->summaryview));
2128                 } else {
2129                         gtk_paned_add2(GTK_PANED(hpaned),
2130                                        GTK_WIDGET_PTR(mainwin->summaryview));
2131                         gtk_widget_ref(vpaned);
2132                 }
2133                 gtk_paned_add2(GTK_PANED(vpaned),
2134                                GTK_WIDGET_PTR(mainwin->messageview));
2135                 gtk_widget_show(vpaned);
2136                 gtk_widget_queue_resize(vpaned);
2137
2138                 mainwin->win.sep_none.hpaned = hpaned;
2139                 mainwin->win.sep_none.vpaned = vpaned;
2140                 
2141                 /* remove headerview if not in prefs */
2142                 headerview_set_visibility(mainwin->messageview->headerview,
2143                                           prefs_common.display_header_pane);
2144                 break;
2145         case SEPARATE_FOLDER:
2146                 vpaned = gtk_vpaned_new();
2147                 if (messageview_is_visible(mainwin->messageview)) {
2148                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
2149                                            TRUE, TRUE, 0);
2150                         gtk_paned_add1(GTK_PANED(vpaned),
2151                                        GTK_WIDGET_PTR(mainwin->summaryview));
2152                 } else {
2153                         gtk_box_pack_start(GTK_BOX(vbox_body),
2154                                            GTK_WIDGET_PTR(mainwin->summaryview),
2155                                            TRUE, TRUE, 0);
2156                         gtk_widget_ref(vpaned);
2157                 }
2158                 gtk_paned_add2(GTK_PANED(vpaned),
2159                                GTK_WIDGET_PTR(mainwin->messageview));
2160                 gtk_widget_show(vpaned);
2161                 gtk_widget_queue_resize(vpaned);
2162
2163                 mainwin->win.sep_folder.folderwin = folderwin;
2164                 mainwin->win.sep_folder.vpaned    = vpaned;
2165
2166                 /* remove headerview if not in prefs */
2167                 headerview_set_visibility(mainwin->messageview->headerview,
2168                                           prefs_common.display_header_pane);
2169                 
2170                 break;
2171         case SEPARATE_MESSAGE:
2172                 hpaned = gtk_hpaned_new();
2173                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2174                 gtk_paned_add1(GTK_PANED(hpaned),
2175                                GTK_WIDGET_PTR(mainwin->folderview));
2176                 gtk_paned_add2(GTK_PANED(hpaned),
2177                                GTK_WIDGET_PTR(mainwin->summaryview));
2178                 gtk_widget_show(hpaned);
2179                 gtk_widget_queue_resize(hpaned);
2180                 messageview_add_toolbar(mainwin->messageview, messagewin);
2181                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2182                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2183
2184                 mainwin->win.sep_message.messagewin = messagewin;
2185                 mainwin->win.sep_message.hpaned     = hpaned;
2186
2187                 gtk_widget_realize(messagewin);
2188                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2189                 gtk_widget_show_all(messagewin);
2190                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2191                                   mainwin->messageview->handlebox, 
2192                                   prefs_common.toolbar_style);
2193
2194                 break;
2195         case SEPARATE_BOTH:
2196                 messageview_add_toolbar(mainwin->messageview, messagewin);
2197                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2198                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2199
2200                 gtk_box_pack_start(GTK_BOX(vbox_body),
2201                                    GTK_WIDGET_PTR(mainwin->summaryview),
2202                                    TRUE, TRUE, 0);
2203                 
2204                 mainwin->win.sep_both.folderwin = folderwin;
2205                 mainwin->win.sep_both.messagewin = messagewin;
2206                 
2207                 gtk_widget_realize(messagewin);
2208                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2209                 gtk_widget_show_all(messagewin);
2210                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2211                                   mainwin->messageview->handlebox, 
2212                                   prefs_common.toolbar_style);          
2213
2214                 break;
2215         }
2216
2217         if (messageview_is_visible(mainwin->messageview))
2218                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2219                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2220         else
2221                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2222                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2223
2224         gtk_window_move(GTK_WINDOW(mainwin->window),
2225                         prefs_common.mainwin_x,
2226                         prefs_common.mainwin_y);
2227
2228         gtk_widget_queue_resize(vbox_body);
2229         gtk_widget_queue_resize(mainwin->vbox);
2230         gtk_widget_queue_resize(mainwin->window);
2231         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2232          * and mimeview icon list/ctree lose track of their visibility states */
2233         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2234                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2235         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2236                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2237         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2238                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2239         else 
2240                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2241
2242         mainwin->type = type;
2243
2244
2245         /* toggle menu state */
2246         menuitem = gtk_item_factory_get_item
2247                 (ifactory, "/View/Show or hide/Folder tree");
2248         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2249                                        (type & SEPARATE_FOLDER) == 0 ? TRUE :
2250                                        prefs_common.folderview_visible);
2251         gtk_widget_set_sensitive(menuitem, ((type & SEPARATE_FOLDER) != 0));
2252         menuitem = gtk_item_factory_get_item
2253                 (ifactory, "/View/Show or hide/Message view");
2254         gtk_check_menu_item_set_active
2255                 (GTK_CHECK_MENU_ITEM(menuitem),
2256                  messageview_is_visible(mainwin->messageview));
2257
2258         menuitem = gtk_item_factory_get_item
2259                 (ifactory, "/View/Separate folder tree");
2260         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2261                                        ((type & SEPARATE_FOLDER) != 0));
2262         menuitem = gtk_item_factory_get_item
2263                 (ifactory, "/View/Separate message view");
2264         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2265                                        ((type & SEPARATE_MESSAGE) != 0));
2266
2267         if (folderwin) {
2268                 g_signal_connect
2269                         (G_OBJECT(folderwin), "size_allocate",
2270                          G_CALLBACK(folder_window_size_allocate_cb),
2271                          mainwin);
2272         }
2273         if (messagewin) {
2274                 g_signal_connect
2275                         (G_OBJECT(messagewin), "size_allocate",
2276                          G_CALLBACK(message_window_size_allocate_cb),
2277                          mainwin);
2278         }
2279
2280         debug_print("done.\n");
2281 }
2282
2283 void main_window_destroy_all(void)
2284 {
2285         while (mainwin_list != NULL) {
2286                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2287                 
2288                 /* free toolbar stuff */
2289                 toolbar_clear_list(TOOLBAR_MAIN);
2290                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2291                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2292
2293                 g_free(mainwin->toolbar);
2294                 g_free(mainwin);
2295                 
2296                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2297         }
2298         g_list_free(mainwin_list);
2299 }
2300
2301 #if 0
2302 static gboolean toolbar_account_button_pressed(GtkWidget *widget,
2303                                                GdkEventButton *event,
2304                                                gpointer data)
2305 {
2306         MainWindow *mainwin = (MainWindow *)data;
2307
2308         if (!event) return FALSE;
2309         if (event->button != 3) return FALSE;
2310
2311         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2312         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2313                           widget);
2314
2315         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2316                        menu_button_position, widget,
2317                        event->button, event->time);
2318
2319         return FALSE;
2320 }
2321 #endif
2322
2323 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2324                                    gpointer data)
2325 {
2326         gtk_widget_set_size_request(child, 1, -1);
2327 }
2328
2329 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2330                                    gpointer data)
2331 {
2332         gtk_widget_set_size_request(child, -1, -1);
2333 }
2334
2335 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2336                                     gpointer data)
2337 {
2338         MainWindow *mainwin = (MainWindow *)data;
2339
2340         if (!event) return FALSE;
2341
2342         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2343         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2344                           widget);
2345
2346         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2347                        menu_button_position, widget,
2348                        event->button, event->time);
2349
2350         return TRUE;
2351 }
2352
2353 static void ac_menu_popup_closed(GtkMenuShell *menu_shell, gpointer data)
2354 {
2355         MainWindow *mainwin = (MainWindow *)data;
2356         GtkWidget *button;
2357
2358         button = g_object_get_data(G_OBJECT(menu_shell), "menu_button");
2359         if (!button) return;
2360         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
2361         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button", NULL);
2362         manage_window_focus_in(mainwin->window, NULL, NULL);
2363 }
2364
2365 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2366                                  gpointer data)
2367 {
2368         MainWindow *mainwin = (MainWindow *)data;
2369
2370         if (mainwin->lock_count == 0)
2371                 app_exit_cb(data, 0, widget);
2372
2373         return TRUE;
2374 }
2375
2376 static gint folder_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2377                                    gpointer data)
2378 {
2379         MainWindow *mainwin = (MainWindow *)data;
2380         GtkWidget *menuitem;
2381
2382         menuitem = gtk_item_factory_get_item
2383                 (mainwin->menu_factory, "/View/Show or hide/Folder tree");
2384         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2385
2386         return TRUE;
2387 }
2388
2389 static gint message_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2390                                     gpointer data)
2391 {
2392         MainWindow *mainwin = (MainWindow *)data;
2393         GtkWidget *menuitem;
2394
2395         menuitem = gtk_item_factory_get_item
2396                 (mainwin->menu_factory, "/View/Show or hide/Message view");
2397         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2398
2399         mainwin->messageview->statusbar = mainwin->statusbar;
2400         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2401
2402         return TRUE;
2403 }
2404
2405 static void main_window_size_allocate_cb(GtkWidget *widget,
2406                                          GtkAllocation *allocation,
2407                                          gpointer data)
2408 {
2409         MainWindow *mainwin = (MainWindow *)data;
2410
2411         main_window_get_size(mainwin);
2412 }
2413
2414 static void folder_window_size_allocate_cb(GtkWidget *widget,
2415                                            GtkAllocation *allocation,
2416                                            gpointer data)
2417 {
2418         MainWindow *mainwin = (MainWindow *)data;
2419
2420         main_window_get_size(mainwin);
2421 }
2422
2423 static void message_window_size_allocate_cb(GtkWidget *widget,
2424                                             GtkAllocation *allocation,
2425                                             gpointer data)
2426 {
2427         MainWindow *mainwin = (MainWindow *)data;
2428
2429         main_window_get_size(mainwin);
2430 }
2431
2432 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2433                            GtkWidget *widget)
2434 {
2435         main_window_add_mailbox(mainwin);
2436 }
2437
2438 static void update_folderview_cb(MainWindow *mainwin, guint action,
2439                                  GtkWidget *widget)
2440 {
2441         summary_show(mainwin->summaryview, NULL);
2442         folderview_check_new_all();
2443 }
2444
2445 static void foldersort_cb(MainWindow *mainwin, guint action,
2446                            GtkWidget *widget)
2447 {
2448         foldersort_open();
2449 }
2450
2451 static void import_mbox_cb(MainWindow *mainwin, guint action,
2452                            GtkWidget *widget)
2453 {
2454         import_mbox(mainwin->summaryview->folder_item);
2455 }
2456
2457 static void export_mbox_cb(MainWindow *mainwin, guint action,
2458                            GtkWidget *widget)
2459 {
2460         export_mbox(mainwin->summaryview->folder_item);
2461 }
2462
2463 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
2464                                 GtkWidget *widget)
2465 {
2466         summaryview_export_mbox_list(mainwin->summaryview);
2467 }
2468
2469 static void empty_trash_cb(MainWindow *mainwin, guint action,
2470                            GtkWidget *widget)
2471 {
2472         main_window_empty_trash(mainwin, TRUE);
2473 }
2474
2475 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2476 {
2477         summary_save_as(mainwin->summaryview);
2478 }
2479
2480 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2481 {
2482         summary_print(mainwin->summaryview);
2483 }
2484
2485 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2486 {
2487         if (prefs_common.confirm_on_exit) {
2488                 if (alertpanel(_("Exit"), _("Exit this program?"),
2489                                GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
2490                     != G_ALERTDEFAULT)
2491                         return;
2492                 manage_window_focus_in(mainwin->window, NULL, NULL);
2493         }
2494
2495         app_will_exit(widget, mainwin);
2496 }
2497
2498 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2499 {
2500         if (action == 1)
2501                 summary_search(mainwin->summaryview);
2502         else
2503                 message_search(mainwin->messageview);
2504 }
2505
2506 static void mainwindow_quicksearch(MainWindow *mainwin, guint action, GtkWidget *widget)
2507 {
2508         prefs_common.show_searchbar = TRUE;
2509         quicksearch_show(mainwin->summaryview->quicksearch);
2510 }
2511
2512 static void toggle_folder_cb(MainWindow *mainwin, guint action,
2513                              GtkWidget *widget)
2514 {
2515         gboolean active;
2516
2517         active = GTK_CHECK_MENU_ITEM(widget)->active;
2518
2519         switch (mainwin->type) {
2520         case SEPARATE_NONE:
2521         case SEPARATE_MESSAGE:
2522 #if 0
2523                 if (active)
2524                         gtk_widget_show(GTK_WIDGET_PTR(mainwin->folderview));
2525                 else
2526                         gtk_widget_hide(GTK_WIDGET_PTR(mainwin->folderview));
2527 #endif
2528                 break;
2529         case SEPARATE_FOLDER:
2530                 debug_print("separate folder\n");
2531                 if (active)
2532                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
2533                 else
2534                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2535                 break;
2536         case SEPARATE_BOTH:
2537                 if (active)
2538                         gtk_widget_show(mainwin->win.sep_both.folderwin);
2539                 else
2540                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
2541                 break;
2542         }
2543
2544         prefs_common.folderview_visible = active;
2545 }
2546
2547 static void toggle_message_cb(MainWindow *mainwin, guint action,
2548                               GtkWidget *widget)
2549 {
2550         gboolean active;
2551
2552         active = GTK_CHECK_MENU_ITEM(widget)->active;
2553
2554         if (active != messageview_is_visible(mainwin->messageview))
2555                 summary_toggle_view(mainwin->summaryview);
2556 }
2557
2558 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2559                               GtkWidget *widget)
2560 {
2561         toolbar_toggle(action, mainwin);
2562 }
2563
2564 void main_window_reply_cb(MainWindow *mainwin, guint action,
2565                           GtkWidget *widget)
2566 {
2567         MessageView *msgview = (MessageView*)mainwin->messageview;
2568         GSList *msginfo_list = NULL;
2569
2570         g_return_if_fail(msgview != NULL);
2571
2572         msginfo_list = summary_get_selection(mainwin->summaryview);
2573         g_return_if_fail(msginfo_list != NULL);
2574         compose_reply_from_messageview(msgview, msginfo_list, action);
2575         g_slist_free(msginfo_list);
2576 }
2577
2578
2579 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2580                                 GtkWidget *widget)
2581 {
2582         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2583                 gtk_widget_show(mainwin->hbox_stat);
2584                 prefs_common.show_statusbar = TRUE;
2585         } else {
2586                 gtk_widget_hide(mainwin->hbox_stat);
2587                 prefs_common.show_statusbar = FALSE;
2588         }
2589 }
2590
2591 static void separate_widget_cb(MainWindow *mainwin, guint action,
2592                                GtkWidget *widget)
2593 {
2594         SeparateType type;
2595
2596         if (GTK_CHECK_MENU_ITEM(widget)->active)
2597                 type = mainwin->type | action;
2598         else
2599                 type = mainwin->type & ~action;
2600
2601         main_window_separation_change(mainwin, type);
2602
2603         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
2604         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
2605 }
2606
2607 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline)
2608 {
2609         if (offline)
2610                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
2611         else
2612                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
2613 }
2614
2615 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
2616 {
2617         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active);
2618 }
2619
2620 static void online_switch_clicked (GtkButton *btn, gpointer data) 
2621 {
2622         MainWindow *mainwin;
2623         GtkItemFactory *ifactory;
2624         GtkCheckMenuItem *menuitem;
2625
2626         mainwin = (MainWindow *) data;
2627         
2628         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2629         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
2630         
2631         g_return_if_fail(mainwin != NULL);
2632         g_return_if_fail(menuitem != NULL);
2633         
2634         if (btn == GTK_BUTTON(mainwin->online_switch)) {
2635                 /* go offline */
2636                 gtk_widget_hide (mainwin->online_switch);
2637                 gtk_widget_show (mainwin->offline_switch);
2638                 menuitem->active = TRUE;
2639                 prefs_common.work_offline = TRUE;
2640                 inc_autocheck_timer_remove();           
2641         } else {
2642                 /*go online */
2643                 gtk_widget_hide (mainwin->offline_switch);
2644                 gtk_widget_show (mainwin->online_switch);
2645                 menuitem->active = FALSE;
2646                 prefs_common.work_offline = FALSE;
2647                 inc_autocheck_timer_set();
2648         }
2649 }
2650
2651 static void addressbook_open_cb(MainWindow *mainwin, guint action,
2652                                 GtkWidget *widget)
2653 {
2654         addressbook_open(NULL);
2655 }
2656
2657 static void log_window_show_cb(MainWindow *mainwin, guint action,
2658                                GtkWidget *widget)
2659 {
2660         log_window_show(mainwin->logwin);
2661 }
2662
2663 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2664 {
2665         inc_cancel_all();
2666 }
2667
2668 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2669 {
2670         summary_move_to(mainwin->summaryview);
2671 }
2672
2673 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2674 {
2675         summary_copy_to(mainwin->summaryview);
2676 }
2677
2678 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2679 {
2680         summary_delete(mainwin->summaryview);
2681 }
2682
2683 static void delete_trash_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2684 {
2685         summary_delete_trash(mainwin->summaryview);
2686 }
2687
2688 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2689 {
2690         summary_cancel(mainwin->summaryview);
2691 }
2692
2693 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2694 {
2695         summary_open_msg(mainwin->summaryview);
2696 }
2697
2698 static void view_source_cb(MainWindow *mainwin, guint action,
2699                            GtkWidget *widget)
2700 {
2701         summary_view_source(mainwin->summaryview);
2702 }
2703
2704 static void show_all_header_cb(MainWindow *mainwin, guint action,
2705                                GtkWidget *widget)
2706 {
2707         if (mainwin->menu_lock_count) return;
2708         mainwin->summaryview->messageview->all_headers = 
2709                         GTK_CHECK_MENU_ITEM(widget)->active;
2710         summary_display_msg_selected(mainwin->summaryview,
2711                                      GTK_CHECK_MENU_ITEM(widget)->active);
2712 }
2713
2714 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2715 {
2716         summary_mark(mainwin->summaryview);
2717 }
2718
2719 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2720 {
2721         summary_unmark(mainwin->summaryview);
2722 }
2723
2724 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
2725                               GtkWidget *widget)
2726 {
2727         summary_mark_as_unread(mainwin->summaryview);
2728 }
2729
2730 static void mark_as_read_cb(MainWindow *mainwin, guint action,
2731                             GtkWidget *widget)
2732 {
2733         summary_mark_as_read(mainwin->summaryview);
2734 }
2735
2736 static void mark_all_read_cb(MainWindow *mainwin, guint action,
2737                              GtkWidget *widget)
2738 {
2739         summary_mark_all_read(mainwin->summaryview);
2740 }
2741
2742 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2743 {
2744         summary_reedit(mainwin->summaryview);
2745 }
2746
2747 static void add_address_cb(MainWindow *mainwin, guint action,
2748                            GtkWidget *widget)
2749 {
2750         summary_add_address(mainwin->summaryview);
2751 }
2752
2753 static void set_charset_cb(MainWindow *mainwin, guint action,
2754                            GtkWidget *widget)
2755 {
2756         const gchar *str;
2757
2758         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2759                 str = conv_get_charset_str((CharSet)action);
2760                 
2761                 g_free(mainwin->messageview->forced_charset);
2762                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
2763                 procmime_force_charset(str);
2764                 
2765                 summary_redisplay_msg(mainwin->summaryview);
2766                 
2767                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
2768         }
2769 }
2770
2771 static void set_decode_cb(MainWindow *mainwin, guint action,
2772                            GtkWidget *widget)
2773 {
2774         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2775                 mainwin->messageview->forced_encoding = (EncodingType)action;
2776                 
2777                 summary_redisplay_msg(mainwin->summaryview);
2778                 
2779                 debug_print("forced encoding: %d\n", action);
2780         }
2781 }
2782
2783 static void hide_read_messages (MainWindow *mainwin, guint action,
2784                                 GtkWidget *widget)
2785 {
2786         if (!mainwin->summaryview->folder_item
2787             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
2788                 return;
2789         summary_toggle_show_read_messages(mainwin->summaryview);
2790 }
2791
2792 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2793 {
2794         if (mainwin->menu_lock_count) return;
2795         if (!mainwin->summaryview->folder_item) return;
2796
2797         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2798                 summary_thread_build(mainwin->summaryview);
2799 /*              mainwin->summaryview->folder_item->threaded = TRUE; */
2800         } else {
2801                 summary_unthread(mainwin->summaryview);
2802 /*              mainwin->summaryview->folder_item->threaded = FALSE; */
2803         }
2804 }
2805
2806 static void expand_threads_cb(MainWindow *mainwin, guint action,
2807                               GtkWidget *widget)
2808 {
2809         summary_expand_threads(mainwin->summaryview);
2810 }
2811
2812 static void collapse_threads_cb(MainWindow *mainwin, guint action,
2813                                 GtkWidget *widget)
2814 {
2815         summary_collapse_threads(mainwin->summaryview);
2816 }
2817
2818 static void set_display_item_cb(MainWindow *mainwin, guint action,
2819                                 GtkWidget *widget)
2820 {
2821         prefs_summary_column_open();
2822 }
2823
2824 static void sort_summary_cb(MainWindow *mainwin, guint action,
2825                             GtkWidget *widget)
2826 {
2827         FolderItem *item = mainwin->summaryview->folder_item;
2828         GtkWidget *menuitem;
2829
2830         if (mainwin->menu_lock_count) return;
2831
2832         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
2833                 menuitem = gtk_item_factory_get_item
2834                         (mainwin->menu_factory, "/View/Sort/Ascending");
2835                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
2836                              GTK_CHECK_MENU_ITEM(menuitem)->active
2837                              ? SORT_ASCENDING : SORT_DESCENDING);
2838                 item->sort_key = action;
2839         }
2840 }
2841
2842 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
2843                                  GtkWidget *widget)
2844 {
2845         FolderItem *item = mainwin->summaryview->folder_item;
2846
2847         if (mainwin->menu_lock_count) return;
2848
2849         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
2850                 summary_sort(mainwin->summaryview,
2851                              item->sort_key, (FolderSortType)action);
2852 }
2853
2854 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
2855                                   GtkWidget *widget)
2856 {
2857         summary_attract_by_subject(mainwin->summaryview);
2858 }
2859
2860 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
2861                                  GtkWidget *widget)
2862 {
2863         FolderItem *item;
2864
2865         item = folderview_get_selected_item(mainwin->folderview);
2866         if (item) {
2867                 main_window_cursor_wait(mainwin);
2868                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
2869
2870                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
2871                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
2872
2873                 STATUSBAR_POP(mainwin);
2874                 main_window_cursor_normal(mainwin);
2875         }
2876 }
2877
2878 struct DelDupsData
2879 {
2880         guint   dups;
2881         guint   folders;
2882 };
2883
2884 static void deldup_all(FolderItem *item, gpointer _data)
2885 {
2886         struct DelDupsData *data = _data;
2887         gint result;
2888         
2889         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
2890         if (result >= 0) {
2891                 data->dups += result;
2892                 data->folders += 1;
2893         }
2894 }
2895
2896 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
2897                                  GtkWidget *widget)
2898 {
2899         struct DelDupsData data = {0, 0};
2900
2901         folder_func_to_all_folders(deldup_all, &data);
2902         alertpanel_notice(ngettext("Deleted %d duplicate message in %d folders.\n",
2903                                    "Deleted %d duplicate messages in %d folders.\n",
2904                                    data.dups),
2905                           data.dups, data.folders);
2906 }
2907
2908 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2909 {
2910         summary_filter(mainwin->summaryview, (gboolean)action);
2911 }
2912
2913 static void execute_summary_cb(MainWindow *mainwin, guint action,
2914                                GtkWidget *widget)
2915 {
2916         summary_execute(mainwin->summaryview);
2917 }
2918
2919 static void update_summary_cb(MainWindow *mainwin, guint action,
2920                               GtkWidget *widget)
2921 {
2922         FolderItem *fitem;
2923         FolderView *folderview = mainwin->folderview;
2924
2925         if (!mainwin->summaryview->folder_item) return;
2926         if (!folderview->opened) return;
2927
2928         folder_update_op_count();
2929
2930         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
2931                                             folderview->opened);
2932         if (!fitem) return;
2933
2934         folder_item_scan(fitem);
2935         summary_show(mainwin->summaryview, fitem);
2936 }
2937
2938 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2939 {
2940         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2941 }
2942
2943 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2944 {
2945         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2946 }
2947
2948 static void prev_unread_cb(MainWindow *mainwin, guint action,
2949                            GtkWidget *widget)
2950 {
2951         summary_select_prev_unread(mainwin->summaryview);
2952 }
2953
2954 static void next_unread_cb(MainWindow *mainwin, guint action,
2955                            GtkWidget *widget)
2956 {
2957         summary_select_next_unread(mainwin->summaryview);
2958 }
2959
2960 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2961 {
2962         summary_select_prev_new(mainwin->summaryview);
2963 }
2964
2965 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2966 {
2967         summary_select_next_new(mainwin->summaryview);
2968 }
2969
2970 static void prev_marked_cb(MainWindow *mainwin, guint action,
2971                            GtkWidget *widget)
2972 {
2973         summary_select_prev_marked(mainwin->summaryview);
2974 }
2975
2976 static void next_marked_cb(MainWindow *mainwin, guint action,
2977                            GtkWidget *widget)
2978 {
2979         summary_select_next_marked(mainwin->summaryview);
2980 }
2981
2982 static void prev_labeled_cb(MainWindow *mainwin, guint action,
2983                             GtkWidget *widget)
2984 {
2985         summary_select_prev_labeled(mainwin->summaryview);
2986 }
2987
2988 static void next_labeled_cb(MainWindow *mainwin, guint action,
2989                             GtkWidget *widget)
2990 {
2991         summary_select_next_labeled(mainwin->summaryview);
2992 }
2993
2994 static void goto_folder_cb(MainWindow *mainwin, guint action,
2995                            GtkWidget *widget)
2996 {
2997         FolderItem *to_folder;
2998
2999         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
3000
3001         if (to_folder)
3002                 folderview_select(mainwin->folderview, to_folder);
3003 }
3004
3005 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3006 {
3007         messageview_copy_clipboard(mainwin->messageview);
3008 }
3009
3010 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3011 {
3012         MessageView *msgview = mainwin->messageview;
3013
3014         if (GTK_WIDGET_HAS_FOCUS(mainwin->summaryview->ctree))
3015                 summary_select_all(mainwin->summaryview);
3016         else if (messageview_is_visible(msgview) &&
3017                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
3018                 messageview_select_all(mainwin->messageview);
3019 }
3020
3021 static void select_thread_cb(MainWindow *mainwin, guint action,
3022                              GtkWidget *widget)
3023 {
3024         summary_select_thread(mainwin->summaryview);
3025 }
3026
3027 static void create_filter_cb(MainWindow *mainwin, guint action,
3028                              GtkWidget *widget)
3029 {
3030         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
3031 }
3032
3033 static void create_processing_cb(MainWindow *mainwin, guint action,
3034                              GtkWidget *widget)
3035 {
3036         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
3037 }
3038
3039 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
3040                                          GtkWidget *widget)
3041 {
3042         prefs_filtering_open(&pre_global_processing,
3043                              _("Processing rules to apply before folder rules"),
3044                              NULL, NULL);
3045 }
3046
3047 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
3048                                           GtkWidget *widget)
3049 {
3050         prefs_filtering_open(&post_global_processing,
3051                              _("Processing rules to apply after folder rules"),
3052                              NULL, NULL);
3053 }
3054
3055 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
3056                                     GtkWidget *widget)
3057 {
3058         prefs_filtering_open(&filtering_rules,
3059                              _("Filtering configuration"),
3060                              NULL, NULL);
3061 }
3062
3063 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
3064                                    GtkWidget *widget)
3065 {
3066         prefs_template_open();
3067 }
3068
3069 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
3070                                   GtkWidget *widget)
3071 {
3072         prefs_actions_open(mainwin);
3073 }
3074 #ifdef USE_OPENSSL
3075 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
3076                                   GtkWidget *widget)
3077 {
3078         ssl_manager_open(mainwin);
3079 }
3080 #endif
3081 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
3082                                   GtkWidget *widget)
3083 {
3084         if (!cur_account) {
3085                 new_account_cb(mainwin, 0, widget);
3086         } else {
3087                 account_open(cur_account);
3088         }
3089 }
3090
3091 static void new_account_cb(MainWindow *mainwin, guint action,
3092                            GtkWidget *widget)
3093 {
3094         account_edit_open();
3095         if (!compose_get_compose_list()) account_add();
3096 }
3097
3098 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
3099 {
3100         cur_account = (PrefsAccount *)data;
3101         main_window_reflect_prefs_all();
3102 }
3103
3104 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
3105 {
3106         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3107         PrefsAccount *account = (PrefsAccount *)data;
3108
3109         inc_account_mail(mainwin, account);
3110 }
3111
3112 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
3113 {
3114         prefs_gtk_open();
3115 }
3116
3117 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
3118 {
3119         pluginwindow_create();
3120 }
3121
3122 static void manual_open_cb(MainWindow *mainwin, guint action,
3123                            GtkWidget *widget)
3124 {
3125         manual_open((ManualType)action);
3126 }
3127
3128 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
3129 {
3130         MainWindow *mainwin = (MainWindow *)data;
3131         gchar *str;
3132
3133         if (item->path)
3134                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
3135                                       LOCAL_FOLDER(folder)->rootpath,
3136                                       G_DIR_SEPARATOR,
3137                                       item->path);
3138         else
3139                 str = g_strdup_printf(_("Scanning folder %s ..."),
3140                                       LOCAL_FOLDER(folder)->rootpath);
3141
3142         STATUSBAR_PUSH(mainwin, str);
3143         STATUSBAR_POP(mainwin);
3144         g_free(str);
3145 }
3146
3147 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3148                                           gpointer data)
3149 {
3150         SummaryView *summary;
3151
3152         g_return_val_if_fail(data, FALSE);
3153         summary = ((MainWindow *)data)->summaryview;
3154         g_return_val_if_fail(summary, FALSE);
3155
3156         if (GTK_CLIST(summary->ctree)->selection && 
3157             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3158                 return FALSE;
3159
3160         if (summary->selected != summary->displayed)
3161                 summary_select_node(summary, summary->displayed, FALSE, TRUE);
3162         return FALSE;
3163 }
3164
3165 #define BREAK_ON_MODIFIER_KEY() \
3166         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3167
3168 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3169                                     gpointer data)
3170 {
3171         MainWindow *mainwin = (MainWindow*) data;
3172         
3173         if (!mainwin || !event) 
3174                 return FALSE;
3175
3176         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3177                 return FALSE;
3178
3179         switch (event->keyval) {
3180         case GDK_Q:             /* Quit */
3181                 BREAK_ON_MODIFIER_KEY();
3182
3183                 app_exit_cb(mainwin, 0, NULL);
3184                 return FALSE;
3185         case GDK_space:
3186                 if (mainwin->folderview && mainwin->summaryview
3187                     && !mainwin->summaryview->displayed) {
3188                         summary_lock(mainwin->summaryview);
3189                         folderview_select_next_unread(mainwin->folderview);
3190                         summary_unlock(mainwin->summaryview);
3191                 }
3192                 break;
3193         default:
3194                 break;
3195         }
3196         return FALSE;
3197 }
3198
3199 #undef BREAK_ON_MODIFIER_KEY
3200
3201 /*
3202  * Harvest addresses for selected folder.
3203  */
3204 static void addr_harvest_cb( MainWindow *mainwin,
3205                             guint action,
3206                             GtkWidget *widget )
3207 {
3208         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3209 }
3210
3211 /*
3212  * Harvest addresses for selected messages in summary view.
3213  */
3214 static void addr_harvest_msg_cb( MainWindow *mainwin,
3215                             guint action,
3216                             GtkWidget *widget )
3217 {
3218         summary_harvest_address( mainwin->summaryview );
3219 }
3220
3221 /*!
3222  *\brief        get a MainWindow
3223  *
3224  *\return       MainWindow * The first mainwindow in the mainwin_list
3225  */
3226 MainWindow *mainwindow_get_mainwindow(void)
3227 {
3228         if (mainwin_list && mainwin_list->data)
3229                 return (MainWindow *)(mainwin_list->data);
3230         else
3231                 return NULL;
3232 }
3233
3234 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3235 {
3236         ProgressData *data = (ProgressData *) source;
3237         MainWindow *mainwin = (MainWindow *) userdata;
3238
3239         switch (data->cmd) {
3240         case PROGRESS_COMMAND_START:
3241         case PROGRESS_COMMAND_STOP:
3242                 gtk_progress_bar_set_fraction
3243                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3244                 break;
3245         case PROGRESS_COMMAND_SET_PERCENTAGE:
3246                 gtk_progress_bar_set_fraction
3247                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3248                 break;          
3249         }
3250         while (gtk_events_pending()) gtk_main_iteration ();
3251
3252         return FALSE;
3253 }
3254
3255 /*
3256 * End of Source.
3257 */
3258