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