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