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