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