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