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