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