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