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