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