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