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