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