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