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