163e591ac61e03fa2b8c3572d63d1fb233798b0a
[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_common_open_cb        (MainWindow     *mainwin,
367                                          guint           action,
368                                          GtkWidget      *widget);
369 static void prefs_template_open_cb      (MainWindow     *mainwin,
370                                          guint           action,
371                                          GtkWidget      *widget);
372 static void prefs_actions_open_cb       (MainWindow     *mainwin,
373                                          guint           action,
374                                          GtkWidget      *widget);
375 static void prefs_account_open_cb       (MainWindow     *mainwin,
376                                          guint           action,
377                                          GtkWidget      *widget);
378
379 static void prefs_pre_processing_open_cb  (MainWindow   *mainwin,
380                                            guint         action,
381                                            GtkWidget    *widget);
382
383 static void prefs_post_processing_open_cb (MainWindow   *mainwin,
384                                            guint         action,
385                                            GtkWidget    *widget);
386
387 static void prefs_filtering_open_cb     (MainWindow     *mainwin,
388                                          guint           action,
389                                          GtkWidget      *widget);
390 #ifdef USE_OPENSSL
391 static void ssl_manager_open_cb         (MainWindow     *mainwin,
392                                          guint           action,
393                                          GtkWidget      *widget);
394 #endif
395 static void new_account_cb       (MainWindow    *mainwin,
396                                   guint          action,
397                                   GtkWidget     *widget);
398
399 static void account_selector_menu_cb     (GtkMenuItem   *menuitem,
400                                           gpointer       data);
401 static void account_receive_menu_cb      (GtkMenuItem   *menuitem,
402                                           gpointer       data);
403
404 static void prefs_open_cb       (GtkMenuItem    *menuitem,
405                                  gpointer        data);
406 static void plugins_open_cb     (GtkMenuItem    *menuitem,
407                                  gpointer        data);
408
409 static void online_switch_clicked(GtkButton     *btn, 
410                                   gpointer data);
411
412 static void manual_open_cb       (MainWindow    *mainwin,
413                                   guint          action,
414                                   GtkWidget     *widget);
415
416 static void scan_tree_func       (Folder        *folder,
417                                   FolderItem    *item,
418                                   gpointer       data);
419                                   
420 static void toggle_work_offline_cb(MainWindow *mainwin, guint action, GtkWidget *widget);
421
422 static void addr_harvest_cb      ( MainWindow  *mainwin,
423                                    guint       action,
424                                    GtkWidget   *widget );
425
426 static void addr_harvest_msg_cb  ( MainWindow  *mainwin,
427                                    guint       action,
428                                    GtkWidget   *widget );
429
430 static gboolean mainwindow_focus_in_event       (GtkWidget      *widget, 
431                                                  GdkEventFocus  *focus,
432                                                  gpointer        data);
433 void main_window_reply_cb                       (MainWindow     *mainwin, 
434                                                  guint           action,
435                                                  GtkWidget      *widget);
436 gboolean mainwindow_progressindicator_hook      (gpointer        source,
437                                                  gpointer        userdata);
438 #define  SEPARATE_ACTION 500 
439
440 static GtkItemFactoryEntry mainwin_entries[] =
441 {
442         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
443         {N_("/_File/_Add mailbox"),             NULL, NULL, 0, "<Branch>"},
444         {N_("/_File/_Add mailbox/MH..."),       NULL, add_mailbox_cb, 0, NULL},
445         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
446         {N_("/_File/Change folder order"),      NULL, foldersort_cb,  0, NULL},
447         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
448         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
449         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
450         {N_("/_File/Exp_ort selected to mbox file..."), 
451                                                 NULL, export_list_mbox_cb, 0, NULL},
452         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
453         {N_("/_File/Empty all _Trash folders"), "<shift>D", empty_trash_cb, 0, NULL},
454         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
455         {N_("/_File/_Save as..."),              "<control>S", save_as_cb, 0, NULL},
456         {N_("/_File/_Print..."),                NULL, print_cb, 0, NULL},
457         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
458         {N_("/_File/_Work offline"),            "<control>W", toggle_work_offline_cb, 0, "<ToggleItem>"},
459         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
460         /* {N_("/_File/_Close"),                "<alt>W", app_exit_cb, 0, NULL}, */
461         {N_("/_File/E_xit"),                    "<control>Q", app_exit_cb, 0, NULL},
462
463         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
464         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
465         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
466         {N_("/_Edit/Select _thread"),           NULL, select_thread_cb, 0, NULL},
467         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
468         {N_("/_Edit/_Find in current message..."),
469                                                 "<control>F", search_cb, 0, NULL},
470         {N_("/_Edit/_Search folder..."),        "<shift><control>F", search_cb, 1, NULL},
471         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
472         {N_("/_View/Show or hi_de"),            NULL, NULL, 0, "<Branch>"},
473         {N_("/_View/Show or hi_de/_Folder tree"),
474                                                 NULL, toggle_folder_cb, 0, "<ToggleItem>"},
475         {N_("/_View/Show or hi_de/_Message view"),
476                                                 "V", toggle_message_cb, 0, "<ToggleItem>"},
477         {N_("/_View/Show or hi_de/_Toolbar"),
478                                                 NULL, NULL, 0, "<Branch>"},
479         {N_("/_View/Show or hi_de/_Toolbar/Icon _and text"),
480                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
481         {N_("/_View/Show or hi_de/_Toolbar/_Icon"),
482                                                 NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Show or hide/Toolbar/Icon and text"},
483         {N_("/_View/Show or hi_de/_Toolbar/_Text"),
484                                                 NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Show or hide/Toolbar/Icon and text"},
485         {N_("/_View/Show or hi_de/_Toolbar/_None"),
486                                                 NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Show or hide/Toolbar/Icon and text"},
487         {N_("/_View/Show or hi_de/Status _bar"),
488                                                 NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
489         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
490         {N_("/_View/Separate f_older tree"),    NULL, separate_widget_cb, SEPARATE_FOLDER, "<ToggleItem>"},
491         {N_("/_View/Separate _message view"),   NULL, separate_widget_cb, SEPARATE_MESSAGE, "<ToggleItem>"},
492         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
493         {N_("/_View/_Sort"),                    NULL, NULL, 0, "<Branch>"},
494         {N_("/_View/_Sort/by _number"),         NULL, sort_summary_cb, SORT_BY_NUMBER, "<RadioItem>"},
495         {N_("/_View/_Sort/by s_ize"),           NULL, sort_summary_cb, SORT_BY_SIZE, "/View/Sort/by number"},
496         {N_("/_View/_Sort/by _date"),           NULL, sort_summary_cb, SORT_BY_DATE, "/View/Sort/by number"},
497         {N_("/_View/_Sort/by _from"),           NULL, sort_summary_cb, SORT_BY_FROM, "/View/Sort/by number"},
498         {N_("/_View/_Sort/by _recipient"),      NULL, sort_summary_cb, SORT_BY_TO, "/View/Sort/by number"},
499         {N_("/_View/_Sort/by _subject"),        NULL, sort_summary_cb, SORT_BY_SUBJECT, "/View/Sort/by number"},
500         {N_("/_View/_Sort/by _color label"),
501                                                 NULL, sort_summary_cb, SORT_BY_LABEL, "/View/Sort/by number"},
502         {N_("/_View/_Sort/by _mark"),           NULL, sort_summary_cb, SORT_BY_MARK, "/View/Sort/by number"},
503         {N_("/_View/_Sort/by _status"),         NULL, sort_summary_cb, SORT_BY_STATUS, "/View/Sort/by number"},
504         {N_("/_View/_Sort/by a_ttachment"),
505                                                 NULL, sort_summary_cb, SORT_BY_MIME, "/View/Sort/by number"},
506         {N_("/_View/_Sort/by score"),           NULL, sort_summary_cb, SORT_BY_SCORE, "/View/Sort/by number"},
507         {N_("/_View/_Sort/by locked"),          NULL, sort_summary_cb, SORT_BY_LOCKED, "/View/Sort/by number"},
508         {N_("/_View/_Sort/D_on't sort"),        NULL, sort_summary_cb, SORT_BY_NONE, "/View/Sort/by number"},
509         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
510         {N_("/_View/_Sort/Ascending"),          NULL, sort_summary_type_cb, SORT_ASCENDING, "<RadioItem>"},
511         {N_("/_View/_Sort/Descending"),         NULL, sort_summary_type_cb, SORT_DESCENDING, "/View/Sort/Ascending"},
512         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
513         {N_("/_View/_Sort/_Attract by subject"),
514                                                 NULL, attract_by_subject_cb, 0, NULL},
515         {N_("/_View/Th_read view"),             "<control>T", thread_cb, 0, "<ToggleItem>"},
516         {N_("/_View/E_xpand all threads"),      NULL, expand_threads_cb, 0, NULL},
517         {N_("/_View/Co_llapse all threads"),    NULL, collapse_threads_cb, 0, NULL},
518         {N_("/_View/_Hide read messages"),      NULL, hide_read_messages, 0, "<ToggleItem>"},
519         {N_("/_View/Set displayed _items..."),  NULL, set_display_item_cb, 0, NULL},
520
521         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
522         {N_("/_View/_Go to"),                   NULL, NULL, 0, "<Branch>"},
523         {N_("/_View/_Go to/_Prev message"),     "P", prev_cb, 0, NULL},
524         {N_("/_View/_Go to/_Next message"),     "N", next_cb, 0, NULL},
525         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
526         {N_("/_View/_Go to/P_rev unread message"),
527                                                 "<shift>P", prev_unread_cb, 0, NULL},
528         {N_("/_View/_Go to/N_ext unread message"),
529                                                 "<shift>N", next_unread_cb, 0, NULL},
530         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
531         {N_("/_View/_Go to/Prev ne_w message"), NULL, prev_new_cb, 0, NULL},
532         {N_("/_View/_Go to/Ne_xt new message"), NULL, next_new_cb, 0, NULL},
533         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
534         {N_("/_View/_Go to/Prev _marked message"),
535                                                 NULL, prev_marked_cb, 0, NULL},
536         {N_("/_View/_Go to/Next m_arked message"),
537                                                 NULL, next_marked_cb, 0, NULL},
538         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
539         {N_("/_View/_Go to/Prev _labeled message"),
540                                                 NULL, prev_labeled_cb, 0, NULL},
541         {N_("/_View/_Go to/Next la_beled message"),
542                                                 NULL, next_labeled_cb, 0, NULL},
543         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
544         {N_("/_View/_Go to/Other _folder..."),  "G", goto_folder_cb, 0, NULL},
545         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
546
547 #define ENC_SEPARATOR \
548         {N_("/_View/Character _encoding/---"),          NULL, NULL, 0, "<Separator>"}
549 #define ENC_ACTION(action) \
550          NULL, set_charset_cb, action, "/View/Character encoding/Auto detect"
551
552         {N_("/_View/Character _encoding"),              NULL, NULL, 0, "<Branch>"},
553         {N_("/_View/Character _encoding/_Auto detect"),
554          NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
555         {N_("/_View/Character _encoding/---"),          NULL, NULL, 0, "<Separator>"},
556         {N_("/_View/Character _encoding/7bit ascii (US-ASC_II)"),
557          ENC_ACTION(C_US_ASCII)},
558         {N_("/_View/Character _encoding/Unicode (_UTF-8)"),
559          ENC_ACTION(C_UTF_8)},
560         ENC_SEPARATOR,
561
562         {N_("/_View/Character _encoding/Western European (ISO-8859-_1)"),
563          ENC_ACTION(C_ISO_8859_1)},
564         {N_("/_View/Character _encoding/Western European (ISO-8859-15)"),
565          ENC_ACTION(C_ISO_8859_15)},
566         ENC_SEPARATOR,
567
568         {N_("/_View/Character _encoding/Central European (ISO-8859-_2)"),
569          ENC_ACTION(C_ISO_8859_2)},
570         ENC_SEPARATOR,
571
572         {N_("/_View/Character _encoding/_Baltic (ISO-8859-13)"),
573          ENC_ACTION(C_ISO_8859_13)},
574         {N_("/_View/Character _encoding/Baltic (ISO-8859-_4)"),
575          ENC_ACTION(C_ISO_8859_4)},
576         ENC_SEPARATOR,
577
578         {N_("/_View/Character _encoding/Greek (ISO-8859-_7)"),
579          ENC_ACTION(C_ISO_8859_7)},
580         ENC_SEPARATOR,
581
582         {N_("/_View/Character _encoding/Turkish (ISO-8859-_9)"),
583          ENC_ACTION(C_ISO_8859_9)},
584         ENC_SEPARATOR,
585
586         {N_("/_View/Character _encoding/Cyrillic (ISO-8859-_5)"),
587          ENC_ACTION(C_ISO_8859_5)},
588         {N_("/_View/Character _encoding/Cyrillic (KOI8-_R)"),
589          ENC_ACTION(C_KOI8_R)},
590         {N_("/_View/Character _encoding/Cyrillic (KOI8-U)"),
591          ENC_ACTION(C_KOI8_U)},
592         {N_("/_View/Character _encoding/Cyrillic (Windows-1251)"),
593          ENC_ACTION(C_CP1251)},
594         ENC_SEPARATOR,
595
596         {N_("/_View/Character _encoding/Japanese (ISO-2022-_JP)"),
597          ENC_ACTION(C_ISO_2022_JP)},
598         {N_("/_View/Character _encoding/Japanese (ISO-2022-JP-2)"),
599          ENC_ACTION(C_ISO_2022_JP_2)},
600         {N_("/_View/Character _encoding/Japanese (_EUC-JP)"),
601          ENC_ACTION(C_EUC_JP)},
602         {N_("/_View/Character _encoding/Japanese (_Shift__JIS)"),
603          ENC_ACTION(C_SHIFT_JIS)},
604         ENC_SEPARATOR,
605
606         {N_("/_View/Character _encoding/Simplified Chinese (_GB2312)"),
607          ENC_ACTION(C_GB2312)},
608         {N_("/_View/Character _encoding/Simplified Chinese (GBK)"),
609          ENC_ACTION(C_GBK)},
610         {N_("/_View/Character _encoding/Traditional Chinese (_Big5)"),
611          ENC_ACTION(C_BIG5)},
612         {N_("/_View/Character _encoding/Traditional Chinese (EUC-_TW)"),
613          ENC_ACTION(C_EUC_TW)},
614         {N_("/_View/Character _encoding/Chinese (ISO-2022-_CN)"),
615          ENC_ACTION(C_ISO_2022_CN)},
616         ENC_SEPARATOR,
617
618         {N_("/_View/Character _encoding/Korean (EUC-_KR)"),
619          ENC_ACTION(C_EUC_KR)},
620         {N_("/_View/Character _encoding/Korean (ISO-2022-KR)"),
621          ENC_ACTION(C_ISO_2022_KR)},
622         ENC_SEPARATOR,
623
624         {N_("/_View/Character _encoding/Thai (TIS-620)"),
625          ENC_ACTION(C_TIS_620)},
626         {N_("/_View/Character _encoding/Thai (Windows-874)"),
627          ENC_ACTION(C_WINDOWS_874)},
628
629 #undef ENC_SEPARATOR
630 #undef ENC_ACTION
631
632 #define DEC_SEPARATOR \
633         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"}
634 #define DEC_ACTION(action) \
635          NULL, set_decode_cb, action, "/View/Decode/Auto detect"
636         {N_("/_View/Decode"),           NULL, NULL, 0, "<Branch>"},
637         {N_("/_View/Decode/_Auto detect"),
638          NULL, set_decode_cb, 0, "<RadioItem>"},
639         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"},
640         {N_("/_View/Decode/_8bit"),             DEC_ACTION(ENC_8BIT)},
641         {N_("/_View/Decode/_Quoted printable"), DEC_ACTION(ENC_QUOTED_PRINTABLE)},
642         {N_("/_View/Decode/_Base64"),           DEC_ACTION(ENC_BASE64)},
643         {N_("/_View/Decode/_Uuencode"),         DEC_ACTION(ENC_X_UUENCODE)},
644
645 #undef DEC_SEPARATOR
646 #undef DEC_ACTION
647
648         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
649         {N_("/_View/Open in new _window"),      "<control><alt>N", open_msg_cb, 0, NULL},
650         {N_("/_View/Mess_age source"),          "<control>U", view_source_cb, 0, NULL},
651         {N_("/_View/Show all headers"),         "<control>H", show_all_header_cb, 0, "<ToggleItem>"},
652         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
653         {N_("/_View/_Update summary"),          "<control><alt>U", update_summary_cb,  0, NULL},
654
655         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
656         {N_("/_Message/Recei_ve"),              NULL, NULL, 0, "<Branch>"},
657         {N_("/_Message/Recei_ve/Get from _current account"),
658                                                 "<control>I",   inc_mail_cb, 0, NULL},
659         {N_("/_Message/Recei_ve/Get from _all accounts"),
660                                                 "<shift><control>I", inc_all_account_mail_cb, 0, NULL},
661         {N_("/_Message/Recei_ve/Cancel receivin_g"),
662                                                 NULL, inc_cancel_cb, 0, NULL},
663         {N_("/_Message/Recei_ve/---"),          NULL, NULL, 0, "<Separator>"},
664         {N_("/_Message/_Send queued messages"), NULL, send_queue_cb, 0, NULL},
665         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
666         {N_("/_Message/Compose a_n email message"),     "<control>M", compose_mail_cb, 0, NULL},
667         {N_("/_Message/Compose a news message"),        NULL,   compose_news_cb, 0, NULL},
668         {N_("/_Message/_Reply"),                "<control>R",   main_window_reply_cb, COMPOSE_REPLY, NULL},
669         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
670         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", main_window_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
671         {N_("/_Message/Repl_y to/_sender"),     NULL, main_window_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
672         {N_("/_Message/Repl_y to/mailing _list"),
673                                                 "<control>L", main_window_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
674         {N_("/_Message/Follow-up and reply to"),NULL, main_window_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
675         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
676         {N_("/_Message/_Forward"),              "<control><alt>F", main_window_reply_cb, COMPOSE_FORWARD, NULL},
677         {N_("/_Message/Redirect"),              NULL, main_window_reply_cb, COMPOSE_REDIRECT, NULL},
678         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
679         {N_("/_Message/M_ove..."),              "<control>O", move_to_cb, 0, NULL},
680         {N_("/_Message/_Copy..."),              "<shift><control>O", copy_to_cb, 0, NULL},
681         {N_("/_Message/_Delete"),               "<control>D", delete_cb,  0, NULL},
682         {N_("/_Message/Cancel a news message"), "", cancel_cb,  0, NULL},
683         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
684         {N_("/_Message/_Mark"),                 NULL, NULL, 0, "<Branch>"},
685         {N_("/_Message/_Mark/_Mark"),           "<shift>asterisk", mark_cb, 0, NULL},
686         {N_("/_Message/_Mark/_Unmark"),         "U", unmark_cb, 0, NULL},
687         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
688         {N_("/_Message/_Mark/Mark as unr_ead"), "<shift>exclam", mark_as_unread_cb, 0, NULL},
689         {N_("/_Message/_Mark/Mark as rea_d"),
690                                                 NULL, mark_as_read_cb, 0, NULL},
691         {N_("/_Message/_Mark/Mark all _read"),  NULL, mark_all_read_cb, 0, NULL},
692         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
693         {N_("/_Message/Re-_edit"),              NULL, reedit_cb, 0, NULL},
694
695         {N_("/_Tools"),                         NULL, NULL, 0, "<Branch>"},
696         {N_("/_Tools/_Address book..."),        "<shift><control>A", addressbook_open_cb, 0, NULL},
697         {N_("/_Tools/Add sender to address boo_k"),
698                                                 NULL, add_address_cb, 0, NULL},
699         {N_("/_Tools/_Harvest addresses"),      NULL, NULL, 0, "<Branch>"},
700         {N_("/_Tools/_Harvest addresses/from _Folder..."),
701                                                 NULL, addr_harvest_cb, 0, NULL},
702         {N_("/_Tools/_Harvest addresses/from _Messages..."),
703                                                 NULL, addr_harvest_msg_cb, 0, NULL},
704         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
705         {N_("/_Tools/_Filter all messages in folder"),
706                                                 NULL, filter_cb, 0, NULL},
707         {N_("/_Tools/Filter _selected messages"),
708                                                 NULL, filter_cb, 1, NULL},
709         {N_("/_Tools/_Create filter rule"),     NULL, NULL, 0, "<Branch>"},
710         {N_("/_Tools/_Create filter rule/_Automatically"),
711                                                 NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
712         {N_("/_Tools/_Create filter rule/by _From"),
713                                                 NULL, create_filter_cb, FILTER_BY_FROM, NULL},
714         {N_("/_Tools/_Create filter rule/by _To"),
715                                                 NULL, create_filter_cb, FILTER_BY_TO, NULL},
716         {N_("/_Tools/_Create filter rule/by _Subject"),
717                                                 NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
718         {N_("/_Tools/C_reate processing rule"), NULL, NULL, 0, "<Branch>"},
719         {N_("/_Tools/C_reate processing rule/_Automatically"),
720                                                 NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
721         {N_("/_Tools/C_reate processing rule/by _From"),
722                                                 NULL, create_processing_cb, FILTER_BY_FROM, NULL},
723         {N_("/_Tools/C_reate processing rule/by _To"),
724                                                 NULL, create_processing_cb, FILTER_BY_TO, NULL},
725         {N_("/_Tools/C_reate processing rule/by _Subject"),
726                                                 NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
727         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
728         {N_("/_Tools/Actio_ns"),                NULL, NULL, 0, "<Branch>"},
729         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
730         {N_("/_Tools/Ch_eck for new messages in all folders"),
731                                                 NULL, update_folderview_cb, 0, NULL},
732         {N_("/_Tools/Delete du_plicated messages"),
733                                                 NULL, NULL, 0, "<Branch>"},
734         {N_("/_Tools/Delete du_plicated messages/In selected folder"),
735                                                 NULL, delete_duplicated_cb,   0, NULL},
736         {N_("/_Tools/Delete du_plicated messages/In all folders"),
737                                                 NULL, delete_duplicated_all_cb,   0, NULL},
738         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
739         {N_("/_Tools/E_xecute"),                "X", execute_summary_cb, 0, NULL},
740 #ifdef USE_OPENSSL
741         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
742         {N_("/_Tools/SSL cer_tificates..."),    
743                                                 NULL, ssl_manager_open_cb, 0, NULL},
744 #endif
745         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
746         {N_("/_Tools/_Log window"),             "<shift><control>L", log_window_show_cb, 0, NULL},
747
748         {N_("/_Configuration"),                 NULL, NULL, 0, "<Branch>"},
749         {N_("/_Configuration/C_hange current account"),
750                                                 NULL, NULL, 0, "<Branch>"},
751         {N_("/_Configuration/_Preferences for current account..."),
752                                                 NULL, prefs_account_open_cb, 0, NULL},
753         {N_("/_Configuration/Create _new account..."),
754                                                 NULL, new_account_cb, 0, NULL},
755         {N_("/_Configuration/_Edit accounts..."),
756                                                 NULL, account_edit_open, 0, NULL},
757         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
758         {N_("/_Configuration/P_references..."),
759                                                 NULL, prefs_open_cb, 0, NULL},
760         {N_("/_Configuration/Pre-pr_ocessing..."),
761                                                 NULL, prefs_pre_processing_open_cb, 0, NULL},
762         {N_("/_Configuration/Post-pro_cessing..."),
763                                                 NULL, prefs_post_processing_open_cb, 0, NULL},
764         {N_("/_Configuration/_Filtering..."),
765                                                 NULL, prefs_filtering_open_cb, 0, NULL},
766         {N_("/_Configuration/_Templates..."),   NULL, prefs_template_open_cb, 0, NULL},
767         {N_("/_Configuration/_Actions..."),     NULL, prefs_actions_open_cb, 0, NULL},
768         {N_("/_Configuration/Plu_gins..."),     NULL, plugins_open_cb, 0, NULL},
769
770         {N_("/_Help"),                          NULL, NULL, 0, "<Branch>"},
771         {N_("/_Help/_Manual (Local)"),          NULL, manual_open_cb, MANUAL_MANUAL_LOCAL, NULL},
772         {N_("/_Help/Ma_nual (Sylpheed Doc Homepage)"),
773                                                 NULL, manual_open_cb, MANUAL_MANUAL_SYLDOC, NULL},
774         {N_("/_Help/_FAQ (Local)"),             NULL, manual_open_cb, MANUAL_FAQ_LOCAL, NULL},
775         {N_("/_Help/FA_Q (Sylpheed Doc Homepage)"),
776                                                 NULL, manual_open_cb, MANUAL_FAQ_SYLDOC, NULL},
777         {N_("/_Help/_Claws FAQ (Claws Documentation)"),
778                                                 NULL, manual_open_cb, MANUAL_FAQ_CLAWS, NULL},
779         {N_("/_Help/---"),                      NULL, NULL, 0, "<Separator>"},
780         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
781 };
782
783 static gboolean main_window_accel_activate (GtkAccelGroup *accelgroup,
784                                             GObject *arg1,
785                                             guint value,
786                                             GdkModifierType mod,
787                                             gpointer user_data) 
788 {
789         MainWindow *mainwin = (MainWindow *)user_data;
790
791         if (mainwin->summaryview &&
792             mainwin->summaryview->quicksearch &&
793             quicksearch_has_focus(mainwin->summaryview->quicksearch) &&
794             (mod == 0 || mod == GDK_SHIFT_MASK)) {
795                 quicksearch_pass_key(mainwin->summaryview->quicksearch, value, mod);
796                 return TRUE;
797         }
798         return FALSE;
799 }
800
801 MainWindow *main_window_create(SeparateType type)
802 {
803         MainWindow *mainwin;
804         GtkWidget *window;
805         GtkWidget *vbox;
806         GtkWidget *menubar;
807         GtkWidget *handlebox;
808         GtkWidget *vbox_body;
809         GtkWidget *hbox_stat;
810         GtkWidget *statusbar;
811         GtkWidget *progressbar;
812         GtkWidget *statuslabel;
813         GtkWidget *ac_button;
814         GtkWidget *ac_label;
815         GtkWidget *online_pixmap;
816         GtkWidget *offline_pixmap;
817         GtkWidget *online_switch;
818         GtkWidget *offline_switch;
819         GtkTooltips *offline_tip;
820         GtkTooltips *online_tip;
821         GtkTooltips *sel_ac_tip;
822
823         FolderView *folderview;
824         SummaryView *summaryview;
825         MessageView *messageview;
826         GdkColormap *colormap;
827         GdkColor color[4];
828         gboolean success[4];
829         GtkItemFactory *ifactory;
830         GtkWidget *ac_menu;
831         GtkWidget *menuitem;
832         gint i;
833         guint n_menu_entries;
834         gboolean hide_messageview = FALSE;
835
836         static GdkGeometry geometry;
837
838         debug_print("Creating main window...\n");
839         mainwin = g_new0(MainWindow, 1);
840
841         /* main window */
842         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
843         gtk_window_set_title(GTK_WINDOW(window), PROG_VERSION);
844         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
845
846         if (!geometry.min_height) {
847                 geometry.min_width = 320;
848                 geometry.min_height = 200;
849         }
850         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
851                                       GDK_HINT_MIN_SIZE);
852
853         g_signal_connect(G_OBJECT(window), "delete_event",
854                          G_CALLBACK(main_window_close_cb), mainwin);
855         MANAGE_WINDOW_SIGNALS_CONNECT(window);
856         g_signal_connect(G_OBJECT(window), "focus_in_event",
857                          G_CALLBACK(mainwindow_focus_in_event),
858                          mainwin);
859         g_signal_connect(G_OBJECT(window), "key_press_event",
860                          G_CALLBACK(mainwindow_key_pressed), mainwin);
861
862         gtk_widget_realize(window);
863         gtk_widget_add_events(window, GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK);
864         
865
866         gtkut_widget_set_app_icon(window);
867
868         vbox = gtk_vbox_new(FALSE, 0);
869         gtk_widget_show(vbox);
870         gtk_container_add(GTK_CONTAINER(window), vbox);
871
872         /* menu bar */
873         n_menu_entries = sizeof(mainwin_entries) / sizeof(mainwin_entries[0]);
874         menubar = menubar_create(window, mainwin_entries, 
875                                  n_menu_entries, "<Main>", mainwin);
876         gtk_widget_show(menubar);
877         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
878         ifactory = gtk_item_factory_from_widget(menubar);
879
880         menu_set_sensitive(ifactory, "/Help/Manual (Local)", manual_available(MANUAL_MANUAL_LOCAL));
881         menu_set_sensitive(ifactory, "/Help/FAQ (Local)", manual_available(MANUAL_FAQ_LOCAL));
882
883         handlebox = gtk_handle_box_new();
884         gtk_widget_show(handlebox);
885         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
886         g_signal_connect(G_OBJECT(handlebox), "child_attached",
887                          G_CALLBACK(toolbar_child_attached), mainwin);
888         g_signal_connect(G_OBJECT(handlebox), "child_detached",
889                          G_CALLBACK(toolbar_child_detached), mainwin);
890
891         /* link window to mainwin->window to avoid gdk warnings */
892         mainwin->window       = window;
893         
894         /* create toolbar */
895         mainwin->toolbar = toolbar_create(TOOLBAR_MAIN, 
896                                           handlebox, 
897                                           (gpointer)mainwin);
898
899         /* vbox that contains body */
900         vbox_body = gtk_vbox_new(FALSE, BORDER_WIDTH);
901         gtk_widget_show(vbox_body);
902         gtk_container_set_border_width(GTK_CONTAINER(vbox_body), BORDER_WIDTH);
903         gtk_box_pack_start(GTK_BOX(vbox), vbox_body, TRUE, TRUE, 0);
904
905         hbox_stat = gtk_hbox_new(FALSE, 2);
906         gtk_box_pack_end(GTK_BOX(vbox_body), hbox_stat, FALSE, FALSE, 0);
907
908         statusbar = statusbar_create();
909         gtk_box_pack_start(GTK_BOX(hbox_stat), statusbar, TRUE, TRUE, 0);
910
911         progressbar = gtk_progress_bar_new();
912         gtk_widget_set_size_request(progressbar, 120, 1);
913         gtk_box_pack_start(GTK_BOX(hbox_stat), progressbar, FALSE, FALSE, 0);
914
915         online_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_ONLINE);
916         offline_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_OFFLINE);
917         online_tip = gtk_tooltips_new();
918         online_switch = gtk_button_new ();
919         gtk_tooltips_set_tip(GTK_TOOLTIPS(online_tip),online_switch, 
920                              _("You are online. Click the icon to go offline"), NULL);
921         offline_tip = gtk_tooltips_new();
922         offline_switch = gtk_button_new ();
923         gtk_tooltips_set_tip(GTK_TOOLTIPS(offline_tip),offline_switch, 
924                              _("You are offline. Click the icon to go online"),
925                              NULL);
926         gtk_container_add (GTK_CONTAINER(online_switch), online_pixmap);
927         gtk_button_set_relief (GTK_BUTTON(online_switch), GTK_RELIEF_NONE);
928         g_signal_connect (G_OBJECT(online_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
929         gtk_box_pack_start (GTK_BOX(hbox_stat), online_switch, FALSE, FALSE, 0);
930         gtk_container_add (GTK_CONTAINER(offline_switch), offline_pixmap);
931         gtk_button_set_relief (GTK_BUTTON(offline_switch), GTK_RELIEF_NONE);
932         g_signal_connect (G_OBJECT(offline_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
933         gtk_box_pack_start (GTK_BOX(hbox_stat), offline_switch, FALSE, FALSE, 0);
934         
935         statuslabel = gtk_label_new("");
936         gtk_box_pack_start(GTK_BOX(hbox_stat), statuslabel, FALSE, FALSE, 0);
937
938         sel_ac_tip = gtk_tooltips_new();
939         ac_button = gtk_button_new();
940         gtk_tooltips_set_tip(GTK_TOOLTIPS(sel_ac_tip),
941                              ac_button, _("Select account"), NULL);
942         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
943         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
944         gtk_widget_set_size_request(ac_button, -1, 0);
945         gtk_box_pack_end(GTK_BOX(hbox_stat), ac_button, FALSE, FALSE, 0);
946         g_signal_connect(G_OBJECT(ac_button), "button_press_event",
947                          G_CALLBACK(ac_label_button_pressed), mainwin);
948
949         ac_label = gtk_label_new("");
950         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
951
952         gtk_widget_show_all(hbox_stat);
953
954         gtk_widget_hide(offline_switch);
955         /* create views */
956         mainwin->folderview  = folderview  = folderview_create();
957         mainwin->summaryview = summaryview = summary_create();
958         mainwin->messageview = messageview = messageview_create(mainwin);
959         mainwin->logwin      = log_window_create();
960
961         folderview->mainwin      = mainwin;
962         folderview->summaryview  = summaryview;
963
964         summaryview->mainwin     = mainwin;
965         summaryview->folderview  = folderview;
966         summaryview->messageview = messageview;
967         summaryview->window      = window;
968
969         messageview->statusbar   = statusbar;
970         mainwin->vbox           = vbox;
971         mainwin->menubar        = menubar;
972         mainwin->menu_factory   = ifactory;
973         mainwin->handlebox      = handlebox;
974         mainwin->vbox_body      = vbox_body;
975         mainwin->hbox_stat      = hbox_stat;
976         mainwin->statusbar      = statusbar;
977         mainwin->progressbar    = progressbar;
978         mainwin->statuslabel    = statuslabel;
979         mainwin->online_switch  = online_switch;
980         mainwin->online_pixmap  = online_pixmap;
981         mainwin->offline_pixmap = offline_pixmap;
982         mainwin->ac_button      = ac_button;
983         mainwin->ac_label       = ac_label;
984         mainwin->offline_switch    = offline_switch;
985         
986         /* set context IDs for status bar */
987         mainwin->mainwin_cid = gtk_statusbar_get_context_id
988                 (GTK_STATUSBAR(statusbar), "Main Window");
989         mainwin->folderview_cid = gtk_statusbar_get_context_id
990                 (GTK_STATUSBAR(statusbar), "Folder View");
991         mainwin->summaryview_cid = gtk_statusbar_get_context_id
992                 (GTK_STATUSBAR(statusbar), "Summary View");
993         mainwin->messageview_cid = gtk_statusbar_get_context_id
994                 (GTK_STATUSBAR(statusbar), "Message View");
995
996         messageview->statusbar_cid = mainwin->messageview_cid;
997
998         /* allocate colors for summary view and folder view */
999         summaryview->color_marked.red = summaryview->color_marked.green = 0;
1000         summaryview->color_marked.blue = (guint16)65535;
1001
1002         summaryview->color_dim.red = summaryview->color_dim.green =
1003                 summaryview->color_dim.blue = COLOR_DIM;
1004
1005         gtkut_convert_int_to_gdk_color(prefs_common.color_new,
1006                                        &folderview->color_new);
1007
1008         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
1009                                        &folderview->color_op);
1010
1011         summaryview->color_important.red = 0;
1012         summaryview->color_important.green = 0;
1013         summaryview->color_important.blue = (guint16)65535;
1014
1015         color[0] = summaryview->color_marked;
1016         color[1] = summaryview->color_dim;
1017         color[2] = folderview->color_new;
1018         color[3] = folderview->color_op;
1019
1020         colormap = gdk_window_get_colormap(window->window);
1021         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
1022         for (i = 0; i < 4; i++) {
1023                 if (success[i] == FALSE)
1024                         g_warning("MainWindow: color allocation %d failed\n", i);
1025         }
1026
1027         debug_print("done.\n");
1028
1029         messageview->visible = prefs_common.msgview_visible;
1030         hide_messageview = !messageview->visible;
1031         
1032         main_window_set_widgets(mainwin, type);
1033
1034         g_signal_connect(G_OBJECT(window), "size_allocate",
1035                          G_CALLBACK(main_window_size_allocate_cb),
1036                          mainwin);
1037
1038         /* set menu items */
1039         menuitem = gtk_item_factory_get_item
1040                 (ifactory, "/View/Character encoding/Auto detect");
1041         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1042
1043         switch (prefs_common.toolbar_style) {
1044         case TOOLBAR_NONE:
1045                 menuitem = gtk_item_factory_get_item
1046                         (ifactory, "/View/Show or hide/Toolbar/None");
1047                 break;
1048         case TOOLBAR_ICON:
1049                 menuitem = gtk_item_factory_get_item
1050                         (ifactory, "/View/Show or hide/Toolbar/Icon");
1051                 break;
1052         case TOOLBAR_TEXT:
1053                 menuitem = gtk_item_factory_get_item
1054                         (ifactory, "/View/Show or hide/Toolbar/Text");
1055                 break;
1056         case TOOLBAR_BOTH:
1057                 menuitem = gtk_item_factory_get_item
1058                         (ifactory, "/View/Show or hide/Toolbar/Icon and text");
1059         }
1060         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1061
1062         gtk_widget_hide(mainwin->hbox_stat);
1063         menuitem = gtk_item_factory_get_item
1064                 (ifactory, "/View/Show or hide/Status bar");
1065         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1066                                        prefs_common.show_statusbar);
1067         
1068         /* set account selection menu */
1069         ac_menu = gtk_item_factory_get_widget
1070                 (ifactory, "/Configuration/Change current account");
1071         g_signal_connect(G_OBJECT(ac_menu), "selection_done",
1072                          G_CALLBACK(ac_menu_popup_closed), mainwin);
1073         mainwin->ac_menu = ac_menu;
1074
1075         toolbar_main_set_sensitive(mainwin);
1076
1077         /* create actions menu */
1078         main_window_update_actions_menu(mainwin);
1079
1080         /* attach accel groups to main window */
1081 #define ADD_MENU_ACCEL_GROUP_TO_WINDOW(menu,win)                        \
1082         gtk_window_add_accel_group                                      \
1083                 (GTK_WINDOW(win),                                       \
1084                  gtk_item_factory_from_widget(menu)->accel_group);      \
1085         g_signal_connect(G_OBJECT(gtk_item_factory_from_widget(menu)->accel_group), \
1086                         "accel_activate",                               \
1087                         G_CALLBACK(main_window_accel_activate), mainwin);
1088                          
1089         
1090         ADD_MENU_ACCEL_GROUP_TO_WINDOW(summaryview->popupmenu, mainwin->window);
1091         
1092         /* connect the accelerators for equivalent 
1093            menu items in different menus             */
1094         menu_connect_identical_items();
1095
1096
1097         
1098         /* show main window */
1099         gtk_widget_show(mainwin->window);
1100
1101         /* initialize views */
1102         folderview_init(folderview);
1103         summary_init(summaryview);
1104         messageview_init(messageview);
1105         log_window_init(mainwin->logwin);
1106         log_window_set_clipping(mainwin->logwin, prefs_common.cliplog,
1107                                 prefs_common.loglength);
1108 #ifdef USE_OPENSSL
1109         sslcertwindow_register_hook();
1110 #endif
1111         mainwin->lock_count = 0;
1112         mainwin->menu_lock_count = 0;
1113         mainwin->cursor_count = 0;
1114
1115         mainwin->progressindicator_hook =
1116                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1117
1118         if (!watch_cursor)
1119                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1120
1121         mainwin_list = g_list_append(mainwin_list, mainwin);
1122
1123         /* init work_offline */
1124         if (prefs_common.work_offline)
1125                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1126
1127         if (mainwin->type == SEPARATE_NONE && hide_messageview)
1128                 main_window_toggle_message_view(mainwin);
1129
1130         return mainwin;
1131 }
1132
1133 void main_window_destroy(MainWindow *mainwin)
1134 {
1135         /* TODO : destroy other component */
1136         messageview_destroy(mainwin->messageview);
1137         mainwin->messageview = NULL;    
1138 }
1139
1140 void main_window_update_actions_menu(MainWindow *mainwin)
1141 {
1142         GtkItemFactory *ifactory;
1143
1144         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1145         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1146 }
1147
1148 void main_window_cursor_wait(MainWindow *mainwin)
1149 {
1150
1151         if (mainwin->cursor_count == 0)
1152                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1153
1154         mainwin->cursor_count++;
1155
1156         gdk_flush();
1157 }
1158
1159 void main_window_cursor_normal(MainWindow *mainwin)
1160 {
1161         if (mainwin->cursor_count)
1162                 mainwin->cursor_count--;
1163
1164         if (mainwin->cursor_count == 0)
1165                 gdk_window_set_cursor(mainwin->window->window, NULL);
1166
1167         gdk_flush();
1168 }
1169
1170 /* lock / unlock the user-interface */
1171 void main_window_lock(MainWindow *mainwin)
1172 {
1173         if (mainwin->lock_count == 0)
1174                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1175
1176         mainwin->lock_count++;
1177
1178         main_window_set_menu_sensitive(mainwin);
1179         toolbar_main_set_sensitive(mainwin);
1180 }
1181
1182 void main_window_unlock(MainWindow *mainwin)
1183 {
1184         if (mainwin->lock_count)
1185                 mainwin->lock_count--;
1186
1187         main_window_set_menu_sensitive(mainwin);
1188         toolbar_main_set_sensitive(mainwin);
1189
1190         if (mainwin->lock_count == 0)
1191                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
1192 }
1193
1194 static void main_window_menu_callback_block(MainWindow *mainwin)
1195 {
1196         mainwin->menu_lock_count++;
1197 }
1198
1199 static void main_window_menu_callback_unblock(MainWindow *mainwin)
1200 {
1201         if (mainwin->menu_lock_count)
1202                 mainwin->menu_lock_count--;
1203 }
1204
1205 void main_window_reflect_prefs_all(void)
1206 {
1207         main_window_reflect_prefs_all_real(FALSE);
1208 }
1209
1210 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1211 {
1212         GList *cur;
1213         MainWindow *mainwin;
1214         GtkWidget *pixmap;
1215
1216         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1217                 mainwin = (MainWindow *)cur->data;
1218
1219                 main_window_show_cur_account(mainwin);
1220                 main_window_set_menu_sensitive(mainwin);
1221                 toolbar_main_set_sensitive(mainwin);
1222
1223                 /* pixmap themes */
1224                 if (pixmap_theme_changed) {
1225                         toolbar_update(TOOLBAR_MAIN, mainwin);
1226                         messageview_reflect_prefs_pixmap_theme();
1227                         compose_reflect_prefs_pixmap_theme();
1228                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1229                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1230
1231                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1232                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1233                                              mainwin->online_pixmap);
1234                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1235                         gtk_widget_show(pixmap);
1236                         mainwin->online_pixmap = pixmap;
1237                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1238                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1239                                              mainwin->offline_pixmap);
1240                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1241                         gtk_widget_show(pixmap);
1242                         mainwin->offline_pixmap = pixmap;
1243                 }
1244                 
1245                 summary_redisplay_msg(mainwin->summaryview);
1246                 headerview_set_visibility(mainwin->messageview->headerview,
1247                                           prefs_common.display_header_pane);
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                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2012                 gtk_window_set_title(GTK_WINDOW(folderwin),
2013                                      _("Sylpheed - Folder View"));
2014                 gtk_window_set_resizable(GTK_WINDOW(folderwin), TRUE);
2015                 gtk_window_move(GTK_WINDOW(folderwin), prefs_common.folderwin_x,
2016                                 prefs_common.folderwin_y);
2017                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
2018                                                BORDER_WIDTH);
2019                 g_signal_connect(G_OBJECT(folderwin), "delete_event",
2020                                  G_CALLBACK(folder_window_close_cb),
2021                                    mainwin);
2022                 gtk_container_add(GTK_CONTAINER(folderwin),
2023                                   GTK_WIDGET_PTR(mainwin->folderview));
2024                 gtk_widget_realize(folderwin);
2025                 if (prefs_common.folderview_visible)
2026                         gtk_widget_show(folderwin);
2027         }
2028
2029         if (type & SEPARATE_MESSAGE) {
2030                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2031                 gtk_window_set_title(GTK_WINDOW(messagewin),
2032                                      _("Sylpheed - Message View"));
2033                 gtk_window_set_resizable(GTK_WINDOW(messagewin), TRUE);
2034                 gtk_window_move(GTK_WINDOW(messagewin), 
2035                                 prefs_common.main_msgwin_x,
2036                                 prefs_common.main_msgwin_y);
2037                 gtk_widget_set_size_request(messagewin, 
2038                                             prefs_common.msgwin_width,
2039                                             prefs_common.msgwin_height);
2040                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
2041                                                BORDER_WIDTH);
2042                 g_signal_connect(G_OBJECT(messagewin), "delete_event",
2043                                  G_CALLBACK(message_window_close_cb),
2044                                  mainwin);
2045                 if (messageview_is_visible(mainwin->messageview))
2046                         gtk_widget_show(messagewin);
2047         } else {
2048                 mainwin->messageview->statusbar = mainwin->statusbar;
2049                 mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2050         }
2051
2052         switch (type) {
2053         case SEPARATE_NONE:
2054                 hpaned = gtk_hpaned_new();
2055                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2056                 gtk_paned_add1(GTK_PANED(hpaned),
2057                                GTK_WIDGET_PTR(mainwin->folderview));
2058                 gtk_widget_show(hpaned);
2059                 gtk_widget_queue_resize(hpaned);
2060
2061                 vpaned = gtk_vpaned_new();
2062                 if (messageview_is_visible(mainwin->messageview)) {
2063                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2064                         gtk_paned_add1(GTK_PANED(vpaned),
2065                                        GTK_WIDGET_PTR(mainwin->summaryview));
2066                 } else {
2067                         gtk_paned_add2(GTK_PANED(hpaned),
2068                                        GTK_WIDGET_PTR(mainwin->summaryview));
2069                         gtk_widget_ref(vpaned);
2070                 }
2071                 gtk_paned_add2(GTK_PANED(vpaned),
2072                                GTK_WIDGET_PTR(mainwin->messageview));
2073                 gtk_widget_show(vpaned);
2074                 gtk_widget_queue_resize(vpaned);
2075
2076                 mainwin->win.sep_none.hpaned = hpaned;
2077                 mainwin->win.sep_none.vpaned = vpaned;
2078                 
2079                 /* remove headerview if not in prefs */
2080                 headerview_set_visibility(mainwin->messageview->headerview,
2081                                           prefs_common.display_header_pane);
2082                 break;
2083         case SEPARATE_FOLDER:
2084                 vpaned = gtk_vpaned_new();
2085                 if (messageview_is_visible(mainwin->messageview)) {
2086                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
2087                                            TRUE, TRUE, 0);
2088                         gtk_paned_add1(GTK_PANED(vpaned),
2089                                        GTK_WIDGET_PTR(mainwin->summaryview));
2090                 } else {
2091                         gtk_box_pack_start(GTK_BOX(vbox_body),
2092                                            GTK_WIDGET_PTR(mainwin->summaryview),
2093                                            TRUE, TRUE, 0);
2094                         gtk_widget_ref(vpaned);
2095                 }
2096                 gtk_paned_add2(GTK_PANED(vpaned),
2097                                GTK_WIDGET_PTR(mainwin->messageview));
2098                 gtk_widget_show(vpaned);
2099                 gtk_widget_queue_resize(vpaned);
2100
2101                 mainwin->win.sep_folder.folderwin = folderwin;
2102                 mainwin->win.sep_folder.vpaned    = vpaned;
2103
2104                 /* remove headerview if not in prefs */
2105                 headerview_set_visibility(mainwin->messageview->headerview,
2106                                           prefs_common.display_header_pane);
2107                 
2108                 break;
2109         case SEPARATE_MESSAGE:
2110                 hpaned = gtk_hpaned_new();
2111                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2112                 gtk_paned_add1(GTK_PANED(hpaned),
2113                                GTK_WIDGET_PTR(mainwin->folderview));
2114                 gtk_paned_add2(GTK_PANED(hpaned),
2115                                GTK_WIDGET_PTR(mainwin->summaryview));
2116                 gtk_widget_show(hpaned);
2117                 gtk_widget_queue_resize(hpaned);
2118                 messageview_add_toolbar(mainwin->messageview, messagewin);
2119                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2120                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2121
2122                 mainwin->win.sep_message.messagewin = messagewin;
2123                 mainwin->win.sep_message.hpaned     = hpaned;
2124
2125                 gtk_widget_realize(messagewin);
2126                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2127                 gtk_widget_show_all(messagewin);
2128                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2129                                   mainwin->messageview->handlebox, 
2130                                   prefs_common.toolbar_style);
2131
2132                 break;
2133         case SEPARATE_BOTH:
2134                 messageview_add_toolbar(mainwin->messageview, messagewin);
2135                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2136                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2137
2138                 gtk_box_pack_start(GTK_BOX(vbox_body),
2139                                    GTK_WIDGET_PTR(mainwin->summaryview),
2140                                    TRUE, TRUE, 0);
2141                 
2142                 mainwin->win.sep_both.folderwin = folderwin;
2143                 mainwin->win.sep_both.messagewin = messagewin;
2144                 
2145                 gtk_widget_realize(messagewin);
2146                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2147                 gtk_widget_show_all(messagewin);
2148                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2149                                   mainwin->messageview->handlebox, 
2150                                   prefs_common.toolbar_style);          
2151
2152                 break;
2153         }
2154
2155         if (messageview_is_visible(mainwin->messageview))
2156                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2157                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2158         else
2159                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2160                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2161
2162         gtk_window_move(GTK_WINDOW(mainwin->window),
2163                         prefs_common.mainwin_x,
2164                         prefs_common.mainwin_y);
2165
2166         gtk_widget_queue_resize(vbox_body);
2167         gtk_widget_queue_resize(mainwin->vbox);
2168         gtk_widget_queue_resize(mainwin->window);
2169         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2170          * and mimeview icon list/ctree lose track of their visibility states */
2171         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2172                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2173         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2174                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2175         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2176                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2177         else 
2178                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2179
2180         mainwin->type = type;
2181
2182
2183         /* toggle menu state */
2184         menuitem = gtk_item_factory_get_item
2185                 (ifactory, "/View/Show or hide/Folder tree");
2186         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2187                                        (type & SEPARATE_FOLDER) == 0 ? TRUE :
2188                                        prefs_common.folderview_visible);
2189         gtk_widget_set_sensitive(menuitem, ((type & SEPARATE_FOLDER) != 0));
2190         menuitem = gtk_item_factory_get_item
2191                 (ifactory, "/View/Show or hide/Message view");
2192         gtk_check_menu_item_set_active
2193                 (GTK_CHECK_MENU_ITEM(menuitem),
2194                  messageview_is_visible(mainwin->messageview));
2195
2196         menuitem = gtk_item_factory_get_item
2197                 (ifactory, "/View/Separate folder tree");
2198         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2199                                        ((type & SEPARATE_FOLDER) != 0));
2200         menuitem = gtk_item_factory_get_item
2201                 (ifactory, "/View/Separate message view");
2202         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2203                                        ((type & SEPARATE_MESSAGE) != 0));
2204
2205         if (folderwin) {
2206                 g_signal_connect
2207                         (G_OBJECT(folderwin), "size_allocate",
2208                          G_CALLBACK(folder_window_size_allocate_cb),
2209                          mainwin);
2210         }
2211         if (messagewin) {
2212                 g_signal_connect
2213                         (G_OBJECT(messagewin), "size_allocate",
2214                          G_CALLBACK(message_window_size_allocate_cb),
2215                          mainwin);
2216         }
2217
2218         debug_print("done.\n");
2219 }
2220
2221 void main_window_destroy_all(void)
2222 {
2223         while (mainwin_list != NULL) {
2224                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2225                 
2226                 /* free toolbar stuff */
2227                 toolbar_clear_list(TOOLBAR_MAIN);
2228                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2229                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2230
2231                 g_free(mainwin->toolbar);
2232                 g_free(mainwin);
2233                 
2234                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2235         }
2236         g_list_free(mainwin_list);
2237 }
2238
2239 #if 0
2240 static gboolean toolbar_account_button_pressed(GtkWidget *widget,
2241                                                GdkEventButton *event,
2242                                                gpointer data)
2243 {
2244         MainWindow *mainwin = (MainWindow *)data;
2245
2246         if (!event) return FALSE;
2247         if (event->button != 3) return FALSE;
2248
2249         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2250         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2251                           widget);
2252
2253         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2254                        menu_button_position, widget,
2255                        event->button, event->time);
2256
2257         return FALSE;
2258 }
2259 #endif
2260
2261 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2262                                    gpointer data)
2263 {
2264         gtk_widget_set_size_request(child, 1, -1);
2265 }
2266
2267 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2268                                    gpointer data)
2269 {
2270         gtk_widget_set_size_request(child, -1, -1);
2271 }
2272
2273 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2274                                     gpointer data)
2275 {
2276         MainWindow *mainwin = (MainWindow *)data;
2277
2278         if (!event) return FALSE;
2279
2280         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2281         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2282                           widget);
2283
2284         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2285                        menu_button_position, widget,
2286                        event->button, event->time);
2287
2288         return TRUE;
2289 }
2290
2291 static void ac_menu_popup_closed(GtkMenuShell *menu_shell, gpointer data)
2292 {
2293         MainWindow *mainwin = (MainWindow *)data;
2294         GtkWidget *button;
2295
2296         button = g_object_get_data(G_OBJECT(menu_shell), "menu_button");
2297         if (!button) return;
2298         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
2299         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button", NULL);
2300         manage_window_focus_in(mainwin->window, NULL, NULL);
2301 }
2302
2303 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2304                                  gpointer data)
2305 {
2306         MainWindow *mainwin = (MainWindow *)data;
2307
2308         if (mainwin->lock_count == 0)
2309                 app_exit_cb(data, 0, widget);
2310
2311         return TRUE;
2312 }
2313
2314 static gint folder_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2315                                    gpointer data)
2316 {
2317         MainWindow *mainwin = (MainWindow *)data;
2318         GtkWidget *menuitem;
2319
2320         menuitem = gtk_item_factory_get_item
2321                 (mainwin->menu_factory, "/View/Show or hide/Folder tree");
2322         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2323
2324         return TRUE;
2325 }
2326
2327 static gint message_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2328                                     gpointer data)
2329 {
2330         MainWindow *mainwin = (MainWindow *)data;
2331         GtkWidget *menuitem;
2332
2333         menuitem = gtk_item_factory_get_item
2334                 (mainwin->menu_factory, "/View/Show or hide/Message view");
2335         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2336
2337         mainwin->messageview->statusbar = mainwin->statusbar;
2338         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2339
2340         return TRUE;
2341 }
2342
2343 static void main_window_size_allocate_cb(GtkWidget *widget,
2344                                          GtkAllocation *allocation,
2345                                          gpointer data)
2346 {
2347         MainWindow *mainwin = (MainWindow *)data;
2348
2349         main_window_get_size(mainwin);
2350 }
2351
2352 static void folder_window_size_allocate_cb(GtkWidget *widget,
2353                                            GtkAllocation *allocation,
2354                                            gpointer data)
2355 {
2356         MainWindow *mainwin = (MainWindow *)data;
2357
2358         main_window_get_size(mainwin);
2359 }
2360
2361 static void message_window_size_allocate_cb(GtkWidget *widget,
2362                                             GtkAllocation *allocation,
2363                                             gpointer data)
2364 {
2365         MainWindow *mainwin = (MainWindow *)data;
2366
2367         main_window_get_size(mainwin);
2368 }
2369
2370 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2371                            GtkWidget *widget)
2372 {
2373         main_window_add_mailbox(mainwin);
2374 }
2375
2376 static void update_folderview_cb(MainWindow *mainwin, guint action,
2377                                  GtkWidget *widget)
2378 {
2379         summary_show(mainwin->summaryview, NULL);
2380         folderview_check_new_all();
2381 }
2382
2383 static void foldersort_cb(MainWindow *mainwin, guint action,
2384                            GtkWidget *widget)
2385 {
2386         foldersort_open();
2387 }
2388
2389 static void import_mbox_cb(MainWindow *mainwin, guint action,
2390                            GtkWidget *widget)
2391 {
2392         import_mbox(mainwin->summaryview->folder_item);
2393 }
2394
2395 static void export_mbox_cb(MainWindow *mainwin, guint action,
2396                            GtkWidget *widget)
2397 {
2398         export_mbox(mainwin->summaryview->folder_item);
2399 }
2400
2401 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
2402                                 GtkWidget *widget)
2403 {
2404         summaryview_export_mbox_list(mainwin->summaryview);
2405 }
2406
2407 static void empty_trash_cb(MainWindow *mainwin, guint action,
2408                            GtkWidget *widget)
2409 {
2410         main_window_empty_trash(mainwin, TRUE);
2411 }
2412
2413 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2414 {
2415         summary_save_as(mainwin->summaryview);
2416 }
2417
2418 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2419 {
2420         summary_print(mainwin->summaryview);
2421 }
2422
2423 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2424 {
2425         if (prefs_common.confirm_on_exit) {
2426                 if (alertpanel(_("Exit"), _("Exit this program?"),
2427                                GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
2428                     != G_ALERTDEFAULT)
2429                         return;
2430                 manage_window_focus_in(mainwin->window, NULL, NULL);
2431         }
2432
2433         app_will_exit(widget, mainwin);
2434 }
2435
2436 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2437 {
2438         if (action == 1)
2439                 summary_search(mainwin->summaryview);
2440         else
2441                 message_search(mainwin->messageview);
2442 }
2443
2444 static void toggle_folder_cb(MainWindow *mainwin, guint action,
2445                              GtkWidget *widget)
2446 {
2447         gboolean active;
2448
2449         active = GTK_CHECK_MENU_ITEM(widget)->active;
2450
2451         switch (mainwin->type) {
2452         case SEPARATE_NONE:
2453         case SEPARATE_MESSAGE:
2454 #if 0
2455                 if (active)
2456                         gtk_widget_show(GTK_WIDGET_PTR(mainwin->folderview));
2457                 else
2458                         gtk_widget_hide(GTK_WIDGET_PTR(mainwin->folderview));
2459 #endif
2460                 break;
2461         case SEPARATE_FOLDER:
2462                 debug_print("separate folder\n");
2463                 if (active)
2464                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
2465                 else
2466                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2467                 break;
2468         case SEPARATE_BOTH:
2469                 if (active)
2470                         gtk_widget_show(mainwin->win.sep_both.folderwin);
2471                 else
2472                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
2473                 break;
2474         }
2475
2476         prefs_common.folderview_visible = active;
2477 }
2478
2479 static void toggle_message_cb(MainWindow *mainwin, guint action,
2480                               GtkWidget *widget)
2481 {
2482         gboolean active;
2483
2484         active = GTK_CHECK_MENU_ITEM(widget)->active;
2485
2486         if (active != messageview_is_visible(mainwin->messageview))
2487                 summary_toggle_view(mainwin->summaryview);
2488 }
2489
2490 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2491                               GtkWidget *widget)
2492 {
2493         toolbar_toggle(action, mainwin);
2494 }
2495
2496 void main_window_reply_cb(MainWindow *mainwin, guint action,
2497                           GtkWidget *widget)
2498 {
2499         MessageView *msgview = (MessageView*)mainwin->messageview;
2500         GSList *msginfo_list = NULL;
2501
2502         g_return_if_fail(msgview != NULL);
2503
2504         msginfo_list = summary_get_selection(mainwin->summaryview);
2505         g_return_if_fail(msginfo_list != NULL);
2506         compose_reply_from_messageview(msgview, msginfo_list, action);
2507         g_slist_free(msginfo_list);
2508 }
2509
2510
2511 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2512                                 GtkWidget *widget)
2513 {
2514         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2515                 gtk_widget_show(mainwin->hbox_stat);
2516                 prefs_common.show_statusbar = TRUE;
2517         } else {
2518                 gtk_widget_hide(mainwin->hbox_stat);
2519                 prefs_common.show_statusbar = FALSE;
2520         }
2521 }
2522
2523 static void separate_widget_cb(MainWindow *mainwin, guint action,
2524                                GtkWidget *widget)
2525 {
2526         SeparateType type;
2527
2528         if (GTK_CHECK_MENU_ITEM(widget)->active)
2529                 type = mainwin->type | action;
2530         else
2531                 type = mainwin->type & ~action;
2532
2533         main_window_separation_change(mainwin, type);
2534
2535         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
2536         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
2537 }
2538
2539 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline)
2540 {
2541         if (offline)
2542                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
2543         else
2544                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
2545 }
2546
2547 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
2548 {
2549         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active);
2550 }
2551
2552 static void online_switch_clicked (GtkButton *btn, gpointer data) 
2553 {
2554         MainWindow *mainwin;
2555         GtkItemFactory *ifactory;
2556         GtkCheckMenuItem *menuitem;
2557
2558         mainwin = (MainWindow *) data;
2559         
2560         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2561         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
2562         
2563         g_return_if_fail(mainwin != NULL);
2564         g_return_if_fail(menuitem != NULL);
2565         
2566         if (btn == GTK_BUTTON(mainwin->online_switch)) {
2567                 /* go offline */
2568                 gtk_widget_hide (mainwin->online_switch);
2569                 gtk_widget_show (mainwin->offline_switch);
2570                 menuitem->active = TRUE;
2571                 prefs_common.work_offline = TRUE;
2572                 inc_autocheck_timer_remove();           
2573         } else {
2574                 /*go online */
2575                 gtk_widget_hide (mainwin->offline_switch);
2576                 gtk_widget_show (mainwin->online_switch);
2577                 menuitem->active = FALSE;
2578                 prefs_common.work_offline = FALSE;
2579                 inc_autocheck_timer_set();
2580         }
2581 }
2582
2583 static void addressbook_open_cb(MainWindow *mainwin, guint action,
2584                                 GtkWidget *widget)
2585 {
2586         addressbook_open(NULL);
2587 }
2588
2589 static void log_window_show_cb(MainWindow *mainwin, guint action,
2590                                GtkWidget *widget)
2591 {
2592         log_window_show(mainwin->logwin);
2593 }
2594
2595 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2596 {
2597         inc_cancel_all();
2598 }
2599
2600 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2601 {
2602         summary_move_to(mainwin->summaryview);
2603 }
2604
2605 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2606 {
2607         summary_copy_to(mainwin->summaryview);
2608 }
2609
2610 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2611 {
2612         summary_delete(mainwin->summaryview);
2613 }
2614
2615 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2616 {
2617         summary_cancel(mainwin->summaryview);
2618 }
2619
2620 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2621 {
2622         summary_open_msg(mainwin->summaryview);
2623 }
2624
2625 static void view_source_cb(MainWindow *mainwin, guint action,
2626                            GtkWidget *widget)
2627 {
2628         summary_view_source(mainwin->summaryview);
2629 }
2630
2631 static void show_all_header_cb(MainWindow *mainwin, guint action,
2632                                GtkWidget *widget)
2633 {
2634         if (mainwin->menu_lock_count) return;
2635         summary_display_msg_selected(mainwin->summaryview,
2636                                      GTK_CHECK_MENU_ITEM(widget)->active);
2637 }
2638
2639 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2640 {
2641         summary_mark(mainwin->summaryview);
2642 }
2643
2644 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2645 {
2646         summary_unmark(mainwin->summaryview);
2647 }
2648
2649 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
2650                               GtkWidget *widget)
2651 {
2652         summary_mark_as_unread(mainwin->summaryview);
2653 }
2654
2655 static void mark_as_read_cb(MainWindow *mainwin, guint action,
2656                             GtkWidget *widget)
2657 {
2658         summary_mark_as_read(mainwin->summaryview);
2659 }
2660
2661 static void mark_all_read_cb(MainWindow *mainwin, guint action,
2662                              GtkWidget *widget)
2663 {
2664         summary_mark_all_read(mainwin->summaryview);
2665 }
2666
2667 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2668 {
2669         summary_reedit(mainwin->summaryview);
2670 }
2671
2672 static void add_address_cb(MainWindow *mainwin, guint action,
2673                            GtkWidget *widget)
2674 {
2675         summary_add_address(mainwin->summaryview);
2676 }
2677
2678 static void set_charset_cb(MainWindow *mainwin, guint action,
2679                            GtkWidget *widget)
2680 {
2681         const gchar *str;
2682
2683         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2684                 str = conv_get_charset_str((CharSet)action);
2685                 
2686                 g_free(mainwin->messageview->forced_charset);
2687                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
2688                 procmime_force_charset(str);
2689                 
2690                 summary_redisplay_msg(mainwin->summaryview);
2691                 
2692                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
2693         }
2694 }
2695
2696 static void set_decode_cb(MainWindow *mainwin, guint action,
2697                            GtkWidget *widget)
2698 {
2699         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2700                 mainwin->messageview->forced_encoding = (EncodingType)action;
2701                 
2702                 summary_redisplay_msg(mainwin->summaryview);
2703                 
2704                 debug_print("forced encoding: %d\n", action);
2705         }
2706 }
2707
2708 static void hide_read_messages (MainWindow *mainwin, guint action,
2709                                 GtkWidget *widget)
2710 {
2711         if (!mainwin->summaryview->folder_item
2712             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
2713                 return;
2714         summary_toggle_show_read_messages(mainwin->summaryview);
2715 }
2716
2717 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2718 {
2719         if (mainwin->menu_lock_count) return;
2720         if (!mainwin->summaryview->folder_item) return;
2721
2722         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2723                 summary_thread_build(mainwin->summaryview);
2724 /*              mainwin->summaryview->folder_item->threaded = TRUE; */
2725         } else {
2726                 summary_unthread(mainwin->summaryview);
2727 /*              mainwin->summaryview->folder_item->threaded = FALSE; */
2728         }
2729 }
2730
2731 static void expand_threads_cb(MainWindow *mainwin, guint action,
2732                               GtkWidget *widget)
2733 {
2734         summary_expand_threads(mainwin->summaryview);
2735 }
2736
2737 static void collapse_threads_cb(MainWindow *mainwin, guint action,
2738                                 GtkWidget *widget)
2739 {
2740         summary_collapse_threads(mainwin->summaryview);
2741 }
2742
2743 static void set_display_item_cb(MainWindow *mainwin, guint action,
2744                                 GtkWidget *widget)
2745 {
2746         prefs_summary_column_open();
2747 }
2748
2749 static void sort_summary_cb(MainWindow *mainwin, guint action,
2750                             GtkWidget *widget)
2751 {
2752         FolderItem *item = mainwin->summaryview->folder_item;
2753         GtkWidget *menuitem;
2754
2755         if (mainwin->menu_lock_count) return;
2756
2757         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
2758                 menuitem = gtk_item_factory_get_item
2759                         (mainwin->menu_factory, "/View/Sort/Ascending");
2760                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
2761                              GTK_CHECK_MENU_ITEM(menuitem)->active
2762                              ? SORT_ASCENDING : SORT_DESCENDING);
2763                 item->sort_key = action;
2764         }
2765 }
2766
2767 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
2768                                  GtkWidget *widget)
2769 {
2770         FolderItem *item = mainwin->summaryview->folder_item;
2771
2772         if (mainwin->menu_lock_count) return;
2773
2774         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
2775                 summary_sort(mainwin->summaryview,
2776                              item->sort_key, (FolderSortType)action);
2777 }
2778
2779 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
2780                                   GtkWidget *widget)
2781 {
2782         summary_attract_by_subject(mainwin->summaryview);
2783 }
2784
2785 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
2786                                  GtkWidget *widget)
2787 {
2788         FolderItem *item;
2789
2790         item = folderview_get_selected_item(mainwin->folderview);
2791         if (item) {
2792                 main_window_cursor_wait(mainwin);
2793                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
2794
2795                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
2796                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
2797
2798                 STATUSBAR_POP(mainwin);
2799                 main_window_cursor_normal(mainwin);
2800         }
2801 }
2802
2803 struct DelDupsData
2804 {
2805         guint   dups;
2806         guint   folders;
2807 };
2808
2809 static void deldup_all(FolderItem *item, gpointer _data)
2810 {
2811         struct DelDupsData *data = _data;
2812         gint result;
2813         
2814         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
2815         if (result >= 0) {
2816                 data->dups += result;
2817                 data->folders += 1;
2818         }
2819 }
2820
2821 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
2822                                  GtkWidget *widget)
2823 {
2824         struct DelDupsData data = {0, 0};
2825
2826         folder_func_to_all_folders(deldup_all, &data);
2827         alertpanel_notice(_("Deleted %d duplicate message(s) in %d folders.\n"),
2828                           data.dups, data.folders);
2829 }
2830
2831 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2832 {
2833         summary_filter(mainwin->summaryview, (gboolean)action);
2834 }
2835
2836 static void execute_summary_cb(MainWindow *mainwin, guint action,
2837                                GtkWidget *widget)
2838 {
2839         summary_execute(mainwin->summaryview);
2840 }
2841
2842 static void update_summary_cb(MainWindow *mainwin, guint action,
2843                               GtkWidget *widget)
2844 {
2845         FolderItem *fitem;
2846         FolderView *folderview = mainwin->folderview;
2847
2848         if (!mainwin->summaryview->folder_item) return;
2849         if (!folderview->opened) return;
2850
2851         folder_update_op_count();
2852
2853         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
2854                                             folderview->opened);
2855         if (!fitem) return;
2856
2857         folder_item_scan(fitem);
2858         summary_show(mainwin->summaryview, fitem);
2859 }
2860
2861 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2862 {
2863         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2864 }
2865
2866 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2867 {
2868         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2869 }
2870
2871 static void prev_unread_cb(MainWindow *mainwin, guint action,
2872                            GtkWidget *widget)
2873 {
2874         summary_select_prev_unread(mainwin->summaryview);
2875 }
2876
2877 static void next_unread_cb(MainWindow *mainwin, guint action,
2878                            GtkWidget *widget)
2879 {
2880         summary_select_next_unread(mainwin->summaryview);
2881 }
2882
2883 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2884 {
2885         summary_select_prev_new(mainwin->summaryview);
2886 }
2887
2888 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2889 {
2890         summary_select_next_new(mainwin->summaryview);
2891 }
2892
2893 static void prev_marked_cb(MainWindow *mainwin, guint action,
2894                            GtkWidget *widget)
2895 {
2896         summary_select_prev_marked(mainwin->summaryview);
2897 }
2898
2899 static void next_marked_cb(MainWindow *mainwin, guint action,
2900                            GtkWidget *widget)
2901 {
2902         summary_select_next_marked(mainwin->summaryview);
2903 }
2904
2905 static void prev_labeled_cb(MainWindow *mainwin, guint action,
2906                             GtkWidget *widget)
2907 {
2908         summary_select_prev_labeled(mainwin->summaryview);
2909 }
2910
2911 static void next_labeled_cb(MainWindow *mainwin, guint action,
2912                             GtkWidget *widget)
2913 {
2914         summary_select_next_labeled(mainwin->summaryview);
2915 }
2916
2917 static void goto_folder_cb(MainWindow *mainwin, guint action,
2918                            GtkWidget *widget)
2919 {
2920         FolderItem *to_folder;
2921
2922         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
2923
2924         if (to_folder)
2925                 folderview_select(mainwin->folderview, to_folder);
2926 }
2927
2928 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2929 {
2930         messageview_copy_clipboard(mainwin->messageview);
2931 }
2932
2933 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2934 {
2935         MessageView *msgview = mainwin->messageview;
2936
2937         if (GTK_WIDGET_HAS_FOCUS(mainwin->summaryview->ctree))
2938                 summary_select_all(mainwin->summaryview);
2939         else if (messageview_is_visible(msgview) &&
2940                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
2941                 messageview_select_all(mainwin->messageview);
2942 }
2943
2944 static void select_thread_cb(MainWindow *mainwin, guint action,
2945                              GtkWidget *widget)
2946 {
2947         summary_select_thread(mainwin->summaryview);
2948 }
2949
2950 static void create_filter_cb(MainWindow *mainwin, guint action,
2951                              GtkWidget *widget)
2952 {
2953         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
2954 }
2955
2956 static void create_processing_cb(MainWindow *mainwin, guint action,
2957                              GtkWidget *widget)
2958 {
2959         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
2960 }
2961
2962 static void prefs_common_open_cb(MainWindow *mainwin, guint action,
2963                                  GtkWidget *widget)
2964 {
2965         /* prefs_common_open(); */
2966 }
2967
2968 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
2969                                          GtkWidget *widget)
2970 {
2971         prefs_filtering_open(&pre_global_processing,
2972                              _("Processing rules to apply before folder rules"),
2973                              NULL, NULL);
2974 }
2975
2976 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
2977                                           GtkWidget *widget)
2978 {
2979         prefs_filtering_open(&post_global_processing,
2980                              _("Processing rules to apply after folder rules"),
2981                              NULL, NULL);
2982 }
2983
2984 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
2985                                     GtkWidget *widget)
2986 {
2987         prefs_filtering_open(&filtering_rules,
2988                              _("Filtering configuration"),
2989                              NULL, NULL);
2990 }
2991
2992 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
2993                                    GtkWidget *widget)
2994 {
2995         prefs_template_open();
2996 }
2997
2998 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
2999                                   GtkWidget *widget)
3000 {
3001         prefs_actions_open(mainwin);
3002 }
3003 #ifdef USE_OPENSSL
3004 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
3005                                   GtkWidget *widget)
3006 {
3007         ssl_manager_open(mainwin);
3008 }
3009 #endif
3010 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
3011                                   GtkWidget *widget)
3012 {
3013         if (!cur_account) {
3014                 new_account_cb(mainwin, 0, widget);
3015         } else {
3016                 account_open(cur_account);
3017         }
3018 }
3019
3020 static void new_account_cb(MainWindow *mainwin, guint action,
3021                            GtkWidget *widget)
3022 {
3023         account_edit_open();
3024         if (!compose_get_compose_list()) account_add();
3025 }
3026
3027 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
3028 {
3029         cur_account = (PrefsAccount *)data;
3030         main_window_reflect_prefs_all();
3031 }
3032
3033 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
3034 {
3035         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3036         PrefsAccount *account = (PrefsAccount *)data;
3037
3038         inc_account_mail(mainwin, account);
3039 }
3040
3041 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
3042 {
3043         prefs_gtk_open();
3044 }
3045
3046 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
3047 {
3048         pluginwindow_create();
3049 }
3050
3051 static void manual_open_cb(MainWindow *mainwin, guint action,
3052                            GtkWidget *widget)
3053 {
3054         manual_open((ManualType)action);
3055 }
3056
3057 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
3058 {
3059         MainWindow *mainwin = (MainWindow *)data;
3060         gchar *str;
3061
3062         if (item->path)
3063                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
3064                                       LOCAL_FOLDER(folder)->rootpath,
3065                                       G_DIR_SEPARATOR,
3066                                       item->path);
3067         else
3068                 str = g_strdup_printf(_("Scanning folder %s ..."),
3069                                       LOCAL_FOLDER(folder)->rootpath);
3070
3071         STATUSBAR_PUSH(mainwin, str);
3072         STATUSBAR_POP(mainwin);
3073         g_free(str);
3074 }
3075
3076 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3077                                           gpointer data)
3078 {
3079         SummaryView *summary;
3080
3081         g_return_val_if_fail(data, FALSE);
3082         summary = ((MainWindow *)data)->summaryview;
3083         g_return_val_if_fail(summary, FALSE);
3084
3085         if (GTK_CLIST(summary->ctree)->selection && 
3086             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3087                 return FALSE;
3088
3089         if (summary->selected != summary->displayed)
3090                 summary_select_node(summary, summary->displayed, FALSE, TRUE);
3091         return FALSE;
3092 }
3093
3094 #define BREAK_ON_MODIFIER_KEY() \
3095         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3096
3097 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3098                                     gpointer data)
3099 {
3100         MainWindow *mainwin = (MainWindow*) data;
3101         
3102         if (!mainwin || !event) 
3103                 return FALSE;
3104
3105         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3106                 return FALSE;
3107
3108         switch (event->keyval) {
3109         case GDK_Q:             /* Quit */
3110                 BREAK_ON_MODIFIER_KEY();
3111
3112                 app_exit_cb(mainwin, 0, NULL);
3113                 return FALSE;
3114         case GDK_space:
3115                 if (mainwin->folderview && mainwin->summaryview
3116                     && !mainwin->summaryview->displayed) {
3117                         summary_lock(mainwin->summaryview);
3118                         folderview_select_next_unread(mainwin->folderview);
3119                         summary_unlock(mainwin->summaryview);
3120                 }
3121                 break;
3122         default:
3123                 break;
3124         }
3125         return FALSE;
3126 }
3127
3128 #undef BREAK_ON_MODIFIER_KEY
3129
3130 /*
3131  * Harvest addresses for selected folder.
3132  */
3133 static void addr_harvest_cb( MainWindow *mainwin,
3134                             guint action,
3135                             GtkWidget *widget )
3136 {
3137         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3138 }
3139
3140 /*
3141  * Harvest addresses for selected messages in summary view.
3142  */
3143 static void addr_harvest_msg_cb( MainWindow *mainwin,
3144                             guint action,
3145                             GtkWidget *widget )
3146 {
3147         summary_harvest_address( mainwin->summaryview );
3148 }
3149
3150 /*!
3151  *\brief        get a MainWindow
3152  *
3153  *\return       MainWindow * The first mainwindow in the mainwin_list
3154  */
3155 MainWindow *mainwindow_get_mainwindow(void)
3156 {
3157         if (mainwin_list && mainwin_list->data)
3158                 return (MainWindow *)(mainwin_list->data);
3159         else
3160                 return NULL;
3161 }
3162
3163 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3164 {
3165         ProgressData *data = (ProgressData *) source;
3166         MainWindow *mainwin = (MainWindow *) userdata;
3167
3168         switch (data->cmd) {
3169         case PROGRESS_COMMAND_START:
3170         case PROGRESS_COMMAND_STOP:
3171                 gtk_progress_bar_set_fraction
3172                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3173                 break;
3174         case PROGRESS_COMMAND_SET_PERCENTAGE:
3175                 gtk_progress_bar_set_fraction
3176                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3177                 break;          
3178         }
3179         while (gtk_events_pending()) gtk_main_iteration ();
3180
3181         return FALSE;
3182 }
3183
3184 /*
3185 * End of Source.
3186 */
3187