2006-09-10 [wwp] 2.4.0cvs172
[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/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_error(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         log_window_init(mainwin->logwin);
1264         log_window_set_clipping(mainwin->logwin, prefs_common.cliplog,
1265                                 prefs_common.loglength);
1266
1267         folderview->mainwin      = mainwin;
1268         folderview->summaryview  = summaryview;
1269
1270         summaryview->mainwin     = mainwin;
1271         summaryview->folderview  = folderview;
1272         summaryview->messageview = messageview;
1273         summaryview->window      = window;
1274
1275         messageview->statusbar   = statusbar;
1276         mainwin->vbox           = vbox;
1277         mainwin->menubar        = menubar;
1278         mainwin->menu_factory   = ifactory;
1279         mainwin->handlebox      = handlebox;
1280         mainwin->vbox_body      = vbox_body;
1281         mainwin->hbox_stat      = hbox_stat;
1282         mainwin->statusbar      = statusbar;
1283         mainwin->progressbar    = progressbar;
1284         mainwin->statuslabel    = statuslabel;
1285         mainwin->online_switch  = online_switch;
1286         mainwin->online_pixmap  = online_pixmap;
1287         mainwin->offline_pixmap = offline_pixmap;
1288         mainwin->ac_button      = ac_button;
1289         mainwin->ac_label       = ac_label;
1290         mainwin->offline_switch    = offline_switch;
1291         
1292         /* set context IDs for status bar */
1293         mainwin->mainwin_cid = gtk_statusbar_get_context_id
1294                 (GTK_STATUSBAR(statusbar), "Main Window");
1295         mainwin->folderview_cid = gtk_statusbar_get_context_id
1296                 (GTK_STATUSBAR(statusbar), "Folder View");
1297         mainwin->summaryview_cid = gtk_statusbar_get_context_id
1298                 (GTK_STATUSBAR(statusbar), "Summary View");
1299         mainwin->messageview_cid = gtk_statusbar_get_context_id
1300                 (GTK_STATUSBAR(statusbar), "Message View");
1301
1302         messageview->statusbar_cid = mainwin->messageview_cid;
1303
1304         /* allocate colors for summary view and folder view */
1305         summaryview->color_marked.red = summaryview->color_marked.green = 0;
1306         summaryview->color_marked.blue = (guint16)65535;
1307
1308         summaryview->color_dim.red = summaryview->color_dim.green =
1309                 summaryview->color_dim.blue = COLOR_DIM;
1310
1311         gtkut_convert_int_to_gdk_color(prefs_common.color_new,
1312                                        &folderview->color_new);
1313
1314         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
1315                                        &folderview->color_op);
1316
1317         summaryview->color_important.red = 0;
1318         summaryview->color_important.green = 0;
1319         summaryview->color_important.blue = (guint16)65535;
1320
1321         color[0] = summaryview->color_marked;
1322         color[1] = summaryview->color_dim;
1323         color[2] = folderview->color_new;
1324         color[3] = folderview->color_op;
1325
1326         colormap = gdk_drawable_get_colormap(window->window);
1327         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
1328         for (i = 0; i < 4; i++) {
1329                 if (success[i] == FALSE)
1330                         g_warning("MainWindow: color allocation %d failed\n", i);
1331         }
1332
1333         debug_print("done.\n");
1334
1335         messageview->visible = prefs_common.msgview_visible;
1336
1337         main_window_set_widgets(mainwin, type);
1338
1339         g_signal_connect(G_OBJECT(window), "size_allocate",
1340                          G_CALLBACK(main_window_size_allocate_cb),
1341                          mainwin);
1342
1343         /* set menu items */
1344         menuitem = gtk_item_factory_get_item
1345                 (ifactory, "/View/Character encoding/Auto detect");
1346         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1347
1348         switch (prefs_common.toolbar_style) {
1349         case TOOLBAR_NONE:
1350                 menuitem = gtk_item_factory_get_item
1351                         (ifactory, "/View/Show or hide/Toolbar/Hide");
1352                 break;
1353         case TOOLBAR_ICON:
1354                 menuitem = gtk_item_factory_get_item
1355                         (ifactory, "/View/Show or hide/Toolbar/Icons only");
1356                 break;
1357         case TOOLBAR_TEXT:
1358                 menuitem = gtk_item_factory_get_item
1359                         (ifactory, "/View/Show or hide/Toolbar/Text only");
1360                 break;
1361         case TOOLBAR_BOTH:
1362                 menuitem = gtk_item_factory_get_item
1363                         (ifactory, "/View/Show or hide/Toolbar/Text below icons");
1364                 break;
1365         case TOOLBAR_BOTH_HORIZ:
1366                 menuitem = gtk_item_factory_get_item
1367                         (ifactory,
1368                          "/View/Show or hide/Toolbar/Text beside icons");
1369         }
1370         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1371
1372         gtk_widget_hide(mainwin->hbox_stat);
1373         menuitem = gtk_item_factory_get_item
1374                 (ifactory, "/View/Show or hide/Status bar");
1375         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1376                                        prefs_common.show_statusbar);
1377         
1378         /* set account selection menu */
1379         ac_menu = gtk_item_factory_get_widget
1380                 (ifactory, "/Configuration/Change current account");
1381         mainwin->ac_menu = ac_menu;
1382
1383         toolbar_main_set_sensitive(mainwin);
1384
1385         /* create actions menu */
1386         main_window_update_actions_menu(mainwin);
1387
1388         main_create_mailing_list_menu (mainwin, NULL);
1389
1390         /* attach accel groups to main window */
1391 #define ADD_MENU_ACCEL_GROUP_TO_WINDOW(menu,win)                        \
1392         gtk_window_add_accel_group                                      \
1393                 (GTK_WINDOW(win),                                       \
1394                  gtk_item_factory_from_widget(menu)->accel_group);      \
1395         g_signal_connect(G_OBJECT(gtk_item_factory_from_widget(menu)->accel_group), \
1396                         "accel_activate",                               \
1397                         G_CALLBACK(main_window_accel_activate), mainwin);
1398                          
1399         
1400         ADD_MENU_ACCEL_GROUP_TO_WINDOW(summaryview->popupmenu, mainwin->window);
1401         
1402         /* connect the accelerators for equivalent 
1403            menu items in different menus             */
1404         menu_connect_identical_items();
1405
1406
1407         gtk_window_iconify(GTK_WINDOW(mainwin->window));
1408
1409         gtk_widget_show(mainwin->window);
1410
1411         /* initialize views */
1412         folderview_init(folderview);
1413         summary_init(summaryview);
1414         messageview_init(messageview);
1415 #ifdef USE_OPENSSL
1416         sslcertwindow_register_hook();
1417 #endif
1418         mainwin->lock_count = 0;
1419         mainwin->menu_lock_count = 0;
1420         mainwin->cursor_count = 0;
1421
1422         mainwin->progressindicator_hook =
1423                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1424
1425         if (!watch_cursor)
1426                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1427         if (!hand_cursor)
1428                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1429
1430         mainwin_list = g_list_append(mainwin_list, mainwin);
1431
1432         /* init work_offline */
1433         if (prefs_common.work_offline)
1434                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1435
1436         mainwindow_colorlabel_menu_create(mainwin, FALSE);
1437
1438         return mainwin;
1439 }
1440
1441 void main_window_destroy(MainWindow *mainwin)
1442 {
1443         /* TODO : destroy other component */
1444         messageview_destroy(mainwin->messageview);
1445         mainwin->messageview = NULL;    
1446 }
1447
1448 void main_window_update_actions_menu(MainWindow *mainwin)
1449 {
1450         GtkItemFactory *ifactory;
1451
1452         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1453         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1454 }
1455
1456 void main_window_cursor_wait(MainWindow *mainwin)
1457 {
1458
1459         if (mainwin->cursor_count == 0) {
1460                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1461                 textview_cursor_wait(mainwin->messageview->mimeview->textview);
1462         }
1463         
1464         mainwin->cursor_count++;
1465
1466         gdk_flush();
1467 }
1468
1469 void main_window_cursor_normal(MainWindow *mainwin)
1470 {
1471         if (mainwin->cursor_count)
1472                 mainwin->cursor_count--;
1473
1474         if (mainwin->cursor_count == 0) {
1475                 gdk_window_set_cursor(mainwin->window->window, NULL);
1476                 textview_cursor_normal(mainwin->messageview->mimeview->textview);
1477         }
1478         gdk_flush();
1479 }
1480
1481 /* lock / unlock the user-interface */
1482 void main_window_lock(MainWindow *mainwin)
1483 {
1484         if (mainwin->lock_count == 0)
1485                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1486
1487         mainwin->lock_count++;
1488
1489         main_window_set_menu_sensitive(mainwin);
1490         toolbar_main_set_sensitive(mainwin);
1491 }
1492
1493 void main_window_unlock(MainWindow *mainwin)
1494 {
1495         if (mainwin->lock_count)
1496                 mainwin->lock_count--;
1497
1498         main_window_set_menu_sensitive(mainwin);
1499         toolbar_main_set_sensitive(mainwin);
1500
1501         if (mainwin->lock_count == 0)
1502                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
1503 }
1504
1505 static void main_window_menu_callback_block(MainWindow *mainwin)
1506 {
1507         mainwin->menu_lock_count++;
1508 }
1509
1510 static void main_window_menu_callback_unblock(MainWindow *mainwin)
1511 {
1512         if (mainwin->menu_lock_count)
1513                 mainwin->menu_lock_count--;
1514 }
1515
1516 static guint prefs_tag = 0;
1517
1518 void main_window_reflect_prefs_all(void)
1519 {
1520         main_window_reflect_prefs_all_real(FALSE);
1521 }
1522
1523 static gboolean reflect_prefs_timeout_cb(gpointer data) 
1524 {
1525         gboolean pixmap_theme_changed = GPOINTER_TO_INT(data);
1526         GList *cur;
1527         MainWindow *mainwin;
1528         GtkWidget *pixmap;
1529
1530         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1531                 mainwin = (MainWindow *)cur->data;
1532
1533                 main_window_show_cur_account(mainwin);
1534                 main_window_set_menu_sensitive(mainwin);
1535                 toolbar_main_set_sensitive(mainwin);
1536
1537                 /* pixmap themes */
1538                 if (pixmap_theme_changed) {
1539                         toolbar_update(TOOLBAR_MAIN, mainwin);
1540                         messageview_reflect_prefs_pixmap_theme();
1541                         compose_reflect_prefs_pixmap_theme();
1542                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1543                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1544
1545                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1546                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1547                                              mainwin->online_pixmap);
1548                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1549                         gtk_widget_show(pixmap);
1550                         mainwin->online_pixmap = pixmap;
1551                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1552                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1553                                              mainwin->offline_pixmap);
1554                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1555                         gtk_widget_show(pixmap);
1556                         mainwin->offline_pixmap = pixmap;
1557                 }
1558                 
1559                 headerview_set_font(mainwin->messageview->headerview);
1560                 headerview_set_visibility(mainwin->messageview->headerview,
1561                                           prefs_common.display_header_pane);
1562                 textview_reflect_prefs(mainwin->messageview->mimeview->textview);
1563                 folderview_reflect_prefs();
1564                 summary_reflect_prefs();
1565                 summary_redisplay_msg(mainwin->summaryview);
1566         }
1567         prefs_tag = 0;
1568         return FALSE;
1569 }
1570
1571 void main_window_reflect_prefs_all_now(void)
1572 {
1573         reflect_prefs_timeout_cb(GINT_TO_POINTER(FALSE));
1574 }
1575
1576 void main_window_reflect_prefs_custom_colors(MainWindow *mainwin)
1577 {
1578         GtkMenuShell *menu;
1579         GList *cur;
1580
1581         /* re-create colorlabel submenu */
1582         menu = GTK_MENU_SHELL(mainwin->colorlabel_menu);
1583         g_return_if_fail(menu != NULL);
1584
1585         /* clear items. get item pointers. */
1586         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
1587                 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(cur->data));
1588         }
1589         mainwindow_colorlabel_menu_create(mainwin, TRUE);
1590         summary_reflect_prefs_custom_colors(mainwin->summaryview);
1591
1592 }
1593
1594 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1595 {
1596         if (prefs_tag == 0 || pixmap_theme_changed) {
1597                 prefs_tag = g_timeout_add(500, reflect_prefs_timeout_cb, 
1598                                                 GINT_TO_POINTER(pixmap_theme_changed));
1599         }
1600 }
1601
1602 void main_window_set_summary_column(void)
1603 {
1604         GList *cur;
1605         MainWindow *mainwin;
1606
1607         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1608                 mainwin = (MainWindow *)cur->data;
1609                 summary_set_column_order(mainwin->summaryview);
1610         }
1611 }
1612
1613 void main_window_set_folder_column(void)
1614 {
1615         GList *cur;
1616         MainWindow *mainwin;
1617
1618         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1619                 mainwin = (MainWindow *)cur->data;
1620                 folderview_set_column_order(mainwin->folderview);
1621         }
1622 }
1623
1624 static void main_window_set_account_selector_menu(MainWindow *mainwin,
1625                                                   GList *account_list)
1626 {
1627         GList *cur_ac, *cur_item;
1628         GtkWidget *menuitem;
1629         PrefsAccount *ac_prefs;
1630
1631         /* destroy all previous menu item */
1632         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
1633         while (cur_item != NULL) {
1634                 GList *next = cur_item->next;
1635                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1636                 cur_item = next;
1637         }
1638
1639         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1640                 ac_prefs = (PrefsAccount *)cur_ac->data;
1641
1642                 menuitem = gtk_menu_item_new_with_label
1643                         (ac_prefs->account_name
1644                          ? ac_prefs->account_name : _("Untitled"));
1645                 gtk_widget_show(menuitem);
1646                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
1647                 g_signal_connect(G_OBJECT(menuitem), "activate",
1648                                  G_CALLBACK(account_selector_menu_cb),
1649                                  ac_prefs);
1650         }
1651 }
1652
1653 static void main_window_set_account_receive_menu(MainWindow *mainwin,
1654                                                  GList *account_list)
1655 {
1656         GList *cur_ac, *cur_item;
1657         GtkWidget *menu;
1658         GtkWidget *menuitem;
1659         PrefsAccount *ac_prefs;
1660
1661         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
1662                                            "/Message/Receive");
1663
1664         /* search for separator */
1665         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1666              cur_item = cur_item->next) {
1667                 if (GTK_BIN(cur_item->data)->child == NULL) {
1668                         cur_item = cur_item->next;
1669                         break;
1670                 }
1671         }
1672
1673         /* destroy all previous menu item */
1674         while (cur_item != NULL) {
1675                 GList *next = cur_item->next;
1676                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1677                 cur_item = next;
1678         }
1679
1680         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1681                 ac_prefs = (PrefsAccount *)cur_ac->data;
1682
1683                 menuitem = gtk_menu_item_new_with_label
1684                         (ac_prefs->account_name ? ac_prefs->account_name
1685                          : _("Untitled"));
1686                 gtk_widget_show(menuitem);
1687                 gtk_menu_append(GTK_MENU(menu), menuitem);
1688                 g_signal_connect(G_OBJECT(menuitem), "activate",
1689                                  G_CALLBACK(account_receive_menu_cb),
1690                                  ac_prefs);
1691         }
1692 }
1693
1694 static void main_window_set_toolbar_combo_receive_menu(MainWindow *mainwin,
1695                                                        GList *account_list)
1696 {
1697         GList *cur_ac, *cur_item;
1698         GtkWidget *menuitem;
1699         PrefsAccount *ac_prefs;
1700         GtkWidget *menu = NULL;
1701
1702         if (mainwin->toolbar->getall_btn == NULL
1703         ||  mainwin->toolbar->getall_combo == NULL) /* button doesn't exist */
1704                 return;
1705
1706         menu = mainwin->toolbar->getall_combo->menu;
1707
1708         /* destroy all previous menu item */
1709         cur_item = GTK_MENU_SHELL(menu)->children;
1710         while (cur_item != NULL) {
1711                 GList *next = cur_item->next;
1712                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1713                 cur_item = next;
1714         }
1715
1716         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1717                 ac_prefs = (PrefsAccount *)cur_ac->data;
1718
1719                 menuitem = gtk_menu_item_new_with_label
1720                         (ac_prefs->account_name
1721                          ? ac_prefs->account_name : _("Untitled"));
1722                 gtk_widget_show(menuitem);
1723                 gtk_menu_append(GTK_MENU(menu), menuitem);
1724                 g_signal_connect(G_OBJECT(menuitem), "activate",
1725                                  G_CALLBACK(account_receive_menu_cb),
1726                                  ac_prefs);
1727         }
1728 }
1729
1730 static void main_window_set_toolbar_combo_compose_menu(MainWindow *mainwin,
1731                                                        GList *account_list)
1732 {
1733         GList *cur_ac, *cur_item;
1734         GtkWidget *menuitem;
1735         PrefsAccount *ac_prefs;
1736         GtkWidget *menu = NULL;
1737
1738         if (mainwin->toolbar->compose_mail_btn == NULL
1739         ||  mainwin->toolbar->compose_combo == NULL) /* button doesn't exist */
1740                 return;
1741
1742         menu = mainwin->toolbar->compose_combo->menu;
1743
1744         /* destroy all previous menu item */
1745         cur_item = GTK_MENU_SHELL(menu)->children;
1746         while (cur_item != NULL) {
1747                 GList *next = cur_item->next;
1748                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1749                 cur_item = next;
1750         }
1751
1752         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1753                 ac_prefs = (PrefsAccount *)cur_ac->data;
1754
1755                 menuitem = gtk_menu_item_new_with_label
1756                         (ac_prefs->account_name
1757                          ? ac_prefs->account_name : _("Untitled"));
1758                 gtk_widget_show(menuitem);
1759                 gtk_menu_append(GTK_MENU(menu), menuitem);
1760                 g_signal_connect(G_OBJECT(menuitem), "activate",
1761                                  G_CALLBACK(account_compose_menu_cb),
1762                                  ac_prefs);
1763         }
1764 }
1765
1766 void main_window_set_account_menu(GList *account_list)
1767 {
1768         GList *cur;
1769         MainWindow *mainwin;
1770
1771         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1772                 mainwin = (MainWindow *)cur->data;
1773                 main_window_set_account_selector_menu(mainwin, account_list);
1774                 main_window_set_account_receive_menu(mainwin, account_list);
1775                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
1776                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
1777         }
1778 }
1779
1780 void main_window_set_account_menu_only_toolbar(GList *account_list)
1781 {
1782         GList *cur;
1783         MainWindow *mainwin;
1784
1785         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1786                 mainwin = (MainWindow *)cur->data;
1787                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
1788                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
1789         }
1790 }
1791
1792 static void main_window_show_cur_account(MainWindow *mainwin)
1793 {
1794         gchar *buf;
1795         gchar *ac_name;
1796
1797         ac_name = g_strdup(cur_account
1798                            ? (cur_account->account_name
1799                               ? cur_account->account_name : _("Untitled"))
1800                            : _("none"));
1801
1802         if (cur_account)
1803                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
1804         else
1805                 buf = g_strdup(PROG_VERSION);
1806         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
1807         g_free(buf);
1808
1809         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
1810         gtk_widget_queue_resize(mainwin->ac_button);
1811
1812         g_free(ac_name);
1813 }
1814
1815 GtkWidget *main_window_get_folder_window(MainWindow *mainwin)
1816 {
1817         switch (mainwin->type) {
1818         case SEPARATE_FOLDER:
1819                 return mainwin->win.sep_folder.folderwin;
1820         case SEPARATE_BOTH:
1821                 return mainwin->win.sep_both.folderwin;
1822         default:
1823                 return NULL;
1824         }
1825 }
1826
1827 GtkWidget *main_window_get_message_window(MainWindow *mainwin)
1828 {
1829         switch (mainwin->type) {
1830         case SEPARATE_MESSAGE:
1831                 return mainwin->win.sep_message.messagewin;
1832         case SEPARATE_BOTH:
1833                 return mainwin->win.sep_both.messagewin;
1834         default:
1835                 return NULL;
1836         }
1837 }
1838
1839 void main_window_separation_change(MainWindow *mainwin, SeparateType type)
1840 {
1841         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
1842         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
1843         /* GtkWidget *message_wid = GTK_WIDGET_PTR(mainwin->messageview); */
1844         GtkWidget *message_wid = mainwin->messageview->vbox;
1845
1846         debug_print("Changing window separation type from %d to %d\n",
1847                     mainwin->type, type);
1848
1849         if (mainwin->type == type) return;
1850
1851         /* remove widgets from those containers */
1852         gtk_widget_ref(folder_wid);
1853         gtk_widget_ref(summary_wid);
1854         gtk_widget_ref(message_wid);
1855         gtkut_container_remove
1856                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1857         gtkut_container_remove
1858                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1859         gtkut_container_remove
1860                 (GTK_CONTAINER(message_wid->parent), message_wid);
1861
1862         /* clean containers */
1863         switch (mainwin->type) {
1864         case SEPARATE_NONE:
1865                 gtk_widget_destroy(mainwin->win.sep_none.hpaned);
1866                 break;
1867         case SEPARATE_FOLDER:
1868                 gtk_widget_destroy(mainwin->win.sep_folder.vpaned);
1869                 gtk_widget_destroy(mainwin->win.sep_folder.folderwin);
1870                 break;
1871         case SEPARATE_MESSAGE:
1872                 gtk_widget_destroy(mainwin->win.sep_message.hpaned);
1873                 gtk_widget_destroy(mainwin->win.sep_message.messagewin);
1874                 break;
1875         case SEPARATE_BOTH:
1876                 gtk_widget_destroy(mainwin->win.sep_both.messagewin);
1877                 gtk_widget_destroy(mainwin->win.sep_both.folderwin);
1878                 break;
1879         }
1880
1881         gtk_widget_hide(mainwin->window);
1882         main_window_set_widgets(mainwin, type);
1883         gtk_widget_show(mainwin->window);
1884
1885         gtk_widget_unref(folder_wid);
1886         gtk_widget_unref(summary_wid);
1887         gtk_widget_unref(message_wid);
1888 }
1889
1890 void main_window_toggle_message_view(MainWindow *mainwin)
1891 {
1892         SummaryView *summaryview = mainwin->summaryview;
1893         union CompositeWin *cwin = &mainwin->win;
1894         GtkWidget *vpaned = NULL;
1895         GtkWidget *container = NULL;
1896         GtkWidget *msgwin = NULL;
1897
1898         switch (mainwin->type) {
1899         case SEPARATE_NONE:
1900                 vpaned = cwin->sep_none.vpaned;
1901                 container = cwin->sep_none.hpaned;
1902                 break;
1903         case SEPARATE_FOLDER:
1904                 vpaned = cwin->sep_folder.vpaned;
1905                 container = mainwin->vbox_body;
1906                 break;
1907         case SEPARATE_MESSAGE:
1908                 msgwin = mainwin->win.sep_message.messagewin;
1909                 break;
1910         case SEPARATE_BOTH:
1911                 msgwin = mainwin->win.sep_both.messagewin;
1912                 break;
1913         }
1914
1915         if (msgwin) {
1916                 if (GTK_WIDGET_VISIBLE(msgwin)) {
1917                         gtk_widget_hide(msgwin);
1918                         mainwin->messageview->visible = FALSE;
1919                         summaryview->displayed = NULL;
1920                 } else {
1921                         gtk_widget_show(msgwin);
1922                         mainwin->messageview->visible = TRUE;
1923                 }
1924         } else if (vpaned->parent != NULL) {
1925                 mainwin->messageview->visible = FALSE;
1926                 summaryview->displayed = NULL;
1927                 gtk_widget_ref(vpaned);
1928                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1929                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1930         } else {
1931                 mainwin->messageview->visible = TRUE;
1932                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1933                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1934                 gtk_widget_unref(vpaned);
1935         }
1936
1937         if (messageview_is_visible(mainwin->messageview))
1938                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1939                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1940         else
1941                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1942                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1943
1944         if (mainwin->messageview->visible == FALSE)
1945                 messageview_clear(mainwin->messageview);
1946
1947         main_window_set_menu_sensitive(mainwin);
1948
1949         prefs_common.msgview_visible = mainwin->messageview->visible;
1950
1951         gtk_widget_grab_focus(summaryview->ctree);
1952 }
1953
1954 void main_window_get_size(MainWindow *mainwin)
1955 {
1956         GtkAllocation *allocation;
1957
1958         if (mainwin_list == NULL || mainwin->messageview == NULL) {
1959                 debug_print("called after messageview "
1960                             "has been deallocated!\n");
1961                 return;
1962         }
1963
1964         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1965
1966         if (allocation->width > 1 && allocation->height > 1) {
1967                 prefs_common.summaryview_width = allocation->width;
1968
1969                 if ((mainwin->type == SEPARATE_NONE ||
1970                      mainwin->type == SEPARATE_FOLDER) &&
1971                     messageview_is_visible(mainwin->messageview))
1972                         prefs_common.summaryview_height = allocation->height;
1973
1974                 prefs_common.mainview_width = allocation->width;
1975         }
1976
1977         allocation = &mainwin->window->allocation;
1978         if (allocation->width > 1 && allocation->height > 1) {
1979                 prefs_common.mainview_height = allocation->height;
1980                 prefs_common.mainwin_width   = allocation->width;
1981                 prefs_common.mainwin_height  = allocation->height;
1982         }
1983
1984         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1985         if (allocation->width > 1 && allocation->height > 1) {
1986                 prefs_common.folderview_width  = allocation->width;
1987                 prefs_common.folderview_height = allocation->height;
1988         }
1989
1990         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
1991         if (allocation->width > 1 && allocation->height > 1) {
1992                 prefs_common.msgview_width = allocation->width;
1993                 prefs_common.msgview_height = allocation->height;
1994         }
1995
1996 /*      debug_print("summaryview size: %d x %d\n",
1997                     prefs_common.summaryview_width,
1998                     prefs_common.summaryview_height);
1999         debug_print("folderview size: %d x %d\n",
2000                     prefs_common.folderview_width,
2001                     prefs_common.folderview_height);
2002         debug_print("messageview size: %d x %d\n",
2003                     prefs_common.msgview_width,
2004                     prefs_common.msgview_height); */
2005 }
2006
2007 void main_window_get_position(MainWindow *mainwin)
2008 {
2009         gint x, y;
2010         GtkWidget *window;
2011
2012         gtkut_widget_get_uposition(mainwin->window, &x, &y);
2013
2014         prefs_common.mainview_x = x;
2015         prefs_common.mainview_y = y;
2016         prefs_common.mainwin_x = x;
2017         prefs_common.mainwin_y = y;
2018
2019         debug_print("main window position: %d, %d\n", x, y);
2020
2021         window = main_window_get_folder_window(mainwin);
2022         if (window) {
2023                 gtkut_widget_get_uposition(window, &x, &y);
2024                 prefs_common.folderwin_x = x;
2025                 prefs_common.folderwin_y = y;
2026                 debug_print("folder window position: %d, %d\n", x, y);
2027         }
2028         window = main_window_get_message_window(mainwin);
2029         if (window) {
2030                 gtkut_widget_get_uposition(window, &x, &y);
2031                 prefs_common.main_msgwin_x = x;
2032                 prefs_common.main_msgwin_y = y;
2033                 debug_print("message window position: %d, %d\n", x, y);
2034         }
2035 }
2036
2037 void main_window_progress_on(MainWindow *mainwin)
2038 {
2039         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2040 }
2041
2042 void main_window_progress_off(MainWindow *mainwin)
2043 {
2044         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
2045         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2046 }
2047
2048 void main_window_progress_set(MainWindow *mainwin, gint cur, gint total)
2049 {
2050         gchar buf[32];
2051
2052         g_snprintf(buf, sizeof(buf), "%d / %d", cur, total);
2053         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), buf);
2054         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar),
2055                                 (total == 0) ? 0 : (gfloat)cur / (gfloat)total);
2056 }
2057
2058 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
2059 {
2060         if (confirm) {
2061                 if (alertpanel(_("Empty trash"),
2062                                _("Delete all messages in trash folders?"),
2063                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL)
2064                     != G_ALERTALTERNATE)
2065                         return;
2066                 manage_window_focus_in(mainwin->window, NULL, NULL);
2067         }
2068
2069         procmsg_empty_all_trash();
2070
2071         if (mainwin->summaryview->folder_item &&
2072             mainwin->summaryview->folder_item->stype == F_TRASH)
2073                 gtk_widget_grab_focus(mainwin->folderview->ctree);
2074 }
2075
2076 void main_window_add_mailbox(MainWindow *mainwin)
2077 {
2078         gchar *path;
2079         Folder *folder;
2080
2081         path = input_dialog(_("Add mailbox"),
2082                             _("Input the location of mailbox.\n"
2083                               "If the existing mailbox is specified, it will be\n"
2084                               "scanned automatically."),
2085                             "Mail");
2086         if (!path) return;
2087         if (folder_find_from_path(path)) {
2088                 alertpanel_error(_("The mailbox '%s' already exists."), path);
2089                 g_free(path);
2090                 return;
2091         }
2092         folder = folder_new(folder_get_class_from_string("mh"), 
2093                             !strcmp(path, "Mail") ? _("Mailbox") : 
2094                             g_path_get_basename(path), path);
2095         g_free(path);
2096
2097         if (folder->klass->create_tree(folder) < 0) {
2098                 alertpanel_error(_("Creation of the mailbox failed.\n"
2099                                    "Maybe some files already exist, or you don't have the permission to write there."));
2100                 folder_destroy(folder);
2101                 return;
2102         }
2103
2104         folder_add(folder);
2105         folder_set_ui_func(folder, scan_tree_func, mainwin);
2106         folder_scan_tree(folder, TRUE);
2107         folder_set_ui_func(folder, NULL, NULL);
2108 }
2109
2110 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
2111 {
2112         SensitiveCond state = 0;
2113         SummarySelection selection;
2114         FolderItem *item = mainwin->summaryview->folder_item;
2115         GList *account_list = account_get_list();
2116         
2117         selection = summary_get_selection_type(mainwin->summaryview);
2118
2119         if (mainwin->lock_count == 0)
2120                 state |= M_UNLOCKED;
2121         if (selection != SUMMARY_NONE)
2122                 state |= M_MSG_EXIST;
2123         if (item && item->path && folder_item_parent(item) && !item->no_select) {
2124                 state |= M_EXEC;
2125                 /*              if (item->folder->type != F_NEWS) */
2126                 state |= M_ALLOW_DELETE;
2127
2128                 if (prefs_common.immediate_exec == 0
2129                     && mainwin->lock_count == 0)
2130                         state |= M_DELAY_EXEC;
2131
2132                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
2133                     || selection != SUMMARY_NONE)
2134                         state |= M_HIDE_READ_MSG;
2135         }               
2136         if (mainwin->summaryview->threaded)
2137                 state |= M_THREADED;
2138         else
2139                 state |= M_UNTHREADED;  
2140         if (selection == SUMMARY_SELECTED_SINGLE ||
2141             selection == SUMMARY_SELECTED_MULTIPLE)
2142                 state |= M_TARGET_EXIST;
2143         if (selection == SUMMARY_SELECTED_SINGLE)
2144                 state |= M_SINGLE_TARGET_EXIST;
2145         if (mainwin->summaryview->folder_item &&
2146             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
2147                 state |= M_NEWS;
2148         else
2149                 state |= M_NOT_NEWS;
2150         if (prefs_common.actions_list && g_slist_length(prefs_common.actions_list))
2151                 state |= M_ACTIONS_EXIST;
2152
2153         if (procmsg_have_queued_mails_fast() && !procmsg_is_sending())
2154                 state |= M_HAVE_QUEUED_MAILS;
2155
2156         if (selection == SUMMARY_SELECTED_SINGLE &&
2157             (item &&
2158              (folder_has_parent_of_type(item, F_DRAFT) ||
2159               folder_has_parent_of_type(item, F_OUTBOX) ||
2160               folder_has_parent_of_type(item, F_QUEUE))))
2161                 state |= M_ALLOW_REEDIT;
2162         if (cur_account)
2163                 state |= M_HAVE_ACCOUNT;
2164         
2165         for ( ; account_list != NULL; account_list = account_list->next) {
2166                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
2167                         state |= M_HAVE_NEWS_ACCOUNT;
2168                         break;
2169                 }
2170         }
2171         
2172         if (procmsg_spam_can_learn()) {
2173                 state |= M_CAN_LEARN_SPAM;
2174         }
2175
2176         if (inc_is_active())
2177                 state |= M_INC_ACTIVE;
2178
2179         if (mainwin->summaryview->deleted > 0 ||
2180             mainwin->summaryview->moved > 0 ||
2181             mainwin->summaryview->copied > 0)
2182                 state |= M_DELAY_EXEC;
2183
2184         return state;
2185 }
2186
2187
2188
2189 void main_window_set_menu_sensitive(MainWindow *mainwin)
2190 {
2191         GtkItemFactory *ifactory = mainwin->menu_factory;
2192         SensitiveCond state;
2193         gboolean sensitive;
2194         GtkWidget *menu;
2195         GtkWidget *menuitem;
2196         SummaryView *summaryview;
2197         gchar *menu_path;
2198         gint i;
2199         GList *cur_item;
2200
2201         static const struct {
2202                 gchar *const entry;
2203                 SensitiveCond cond;
2204         } entry[] = {
2205                 {"/File/Save as...", M_TARGET_EXIST},
2206                 {"/File/Print..."  , M_TARGET_EXIST},
2207                 {"/File/Exit"      , M_UNLOCKED},
2208
2209                 {"/Edit/Select thread"             , M_SINGLE_TARGET_EXIST},
2210                 {"/Edit/Find in current message...", M_SINGLE_TARGET_EXIST},
2211
2212                 {"/View/Sort"                      , M_EXEC},
2213                 {"/View/Thread view"               , M_EXEC},
2214                 {"/View/Expand all threads"        , M_MSG_EXIST},
2215                 {"/View/Collapse all threads"      , M_MSG_EXIST},
2216                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
2217                 {"/View/Go to/Previous message"        , M_MSG_EXIST},
2218                 {"/View/Go to/Next message"        , M_MSG_EXIST},
2219                 {"/View/Go to/Previous unread message" , M_MSG_EXIST},
2220                 {"/View/Go to/Previous new message"    , M_MSG_EXIST},
2221                 {"/View/Go to/Previous marked message" , M_MSG_EXIST},
2222                 {"/View/Go to/Previous labeled message", M_MSG_EXIST},
2223                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
2224                 {"/View/Go to/Last read message"   , M_SINGLE_TARGET_EXIST},
2225                 {"/View/Go to/Parent message"      , M_SINGLE_TARGET_EXIST},
2226                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
2227                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
2228                 {"/View/All headers"               , M_SINGLE_TARGET_EXIST},
2229                 {"/View/Quotes"                    , M_SINGLE_TARGET_EXIST},
2230
2231                 {"/Message/Receive/Get from current account"
2232                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2233                 {"/Message/Receive/Get from all accounts"
2234                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2235                 {"/Message/Receive/Cancel receiving"
2236                                                  , M_INC_ACTIVE},
2237                 {"/Message/Send queued messages"  , M_HAVE_ACCOUNT|M_HAVE_QUEUED_MAILS},
2238                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
2239                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2240                 {"/Message/Reply to"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2241                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_TARGET_EXIST|M_NEWS},
2242                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2243                 {"/Message/Forward as attachment" , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2244                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2245                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE},
2246                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC},
2247                 {"/Message/Move to trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
2248                 {"/Message/Delete..."             , M_TARGET_EXIST|M_ALLOW_DELETE},
2249                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_NEWS},
2250                 {"/Message/Mark"                  , M_TARGET_EXIST},
2251                 {"/Message/Mark/Mark as spam"     , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2252                 {"/Message/Mark/Mark as ham"      , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2253                 {"/Message/Color label"           , M_TARGET_EXIST},
2254                 {"/Message/Re-edit"               , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
2255
2256                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
2257                 {"/Tools/Harvest addresses"            , M_MSG_EXIST},
2258                 {"/Tools/Harvest addresses/from Messages..."
2259                                                        , M_TARGET_EXIST},
2260                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC},
2261                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC},
2262                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2263                 {"/Tools/Create processing rule"       , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2264                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_ACTIONS_EXIST},
2265                 {"/Tools/Execute"                      , M_DELAY_EXEC},
2266                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE},
2267
2268                 {"/Configuration", M_UNLOCKED},
2269
2270                 {NULL, 0}
2271         };
2272
2273         state = main_window_get_current_state(mainwin);
2274
2275         for (i = 0; entry[i].entry != NULL; i++) {
2276                 sensitive = ((entry[i].cond & state) == entry[i].cond);
2277                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
2278         }
2279
2280         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
2281
2282         /* search for separator */
2283         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
2284              cur_item = cur_item->next) {
2285                 if (GTK_BIN(cur_item->data)->child == NULL) {
2286                         cur_item = cur_item->next;
2287                         break;
2288                 }
2289         }
2290
2291         for (; cur_item != NULL; cur_item = cur_item->next) {
2292                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
2293                                          (M_UNLOCKED & state) != 0);
2294         }
2295
2296         main_window_menu_callback_block(mainwin);
2297
2298 #define SET_CHECK_MENU_ACTIVE(path, active) \
2299 { \
2300         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2301         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2302 }
2303
2304         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
2305                               messageview_is_visible(mainwin->messageview));
2306
2307         summaryview = mainwin->summaryview;
2308         menu_path = "/View/Sort/Don't sort";
2309
2310         switch (summaryview->sort_key) {
2311         case SORT_BY_NUMBER:
2312                 menu_path = "/View/Sort/by number"; break;
2313         case SORT_BY_SIZE:
2314                 menu_path = "/View/Sort/by Size"; break;
2315         case SORT_BY_DATE:
2316                 menu_path = "/View/Sort/by Date"; break;
2317         case SORT_BY_FROM:
2318                 menu_path = "/View/Sort/by From"; break;
2319         case SORT_BY_TO:
2320                 menu_path = "/View/Sort/by To"; break;
2321         case SORT_BY_SUBJECT:
2322                 menu_path = "/View/Sort/by Subject"; break;
2323         case SORT_BY_LABEL:
2324                 menu_path = "/View/Sort/by color label"; break;
2325         case SORT_BY_MARK:
2326                 menu_path = "/View/Sort/by mark"; break;
2327         case SORT_BY_STATUS:
2328                 menu_path = "/View/Sort/by status"; break;
2329         case SORT_BY_MIME:
2330                 menu_path = "/View/Sort/by attachment"; break;
2331         case SORT_BY_SCORE:
2332                 menu_path = "/View/Sort/by score"; break;
2333         case SORT_BY_LOCKED:
2334                 menu_path = "/View/Sort/by locked"; break;
2335         case SORT_BY_NONE:
2336         default:
2337                 menu_path = "/View/Sort/Don't sort"; break;
2338         }
2339         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
2340
2341         if (summaryview->sort_type == SORT_ASCENDING) {
2342                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
2343         } else {
2344                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
2345         }
2346
2347         if (summaryview->sort_key != SORT_BY_NONE) {
2348                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
2349                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
2350         } else {
2351                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
2352                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
2353         }
2354
2355         if (mainwin->messageview 
2356         &&  mainwin->messageview->mimeview
2357         &&  mainwin->messageview->mimeview->textview)
2358                 SET_CHECK_MENU_ACTIVE("/View/All headers",
2359                               mainwin->messageview->mimeview->textview->show_all_headers);
2360         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
2361         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
2362         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
2363         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
2364         if (prefs_common.hide_quotes == 1)
2365                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", TRUE);
2366         if (prefs_common.hide_quotes == 2)
2367                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", TRUE);
2368         if (prefs_common.hide_quotes == 3)
2369                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", TRUE);
2370
2371 #undef SET_CHECK_MENU_ACTIVE
2372
2373         main_window_menu_callback_unblock(mainwin);
2374 }
2375
2376 void main_create_mailing_list_menu (MainWindow *mainwin, MsgInfo *msginfo)
2377 {
2378         GtkItemFactory *ifactory;
2379         gint is_menu = 0;
2380         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2381         
2382         if (msginfo) 
2383                 is_menu = mailing_list_create_submenu (ifactory, msginfo);
2384         if (is_menu)
2385                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2386                                 (ifactory,"/Message/Mailing-List"), TRUE);
2387         else
2388                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2389                                 (ifactory,"/Message/Mailing-List"), FALSE);
2390 }
2391
2392 static gint mailing_list_create_submenu (GtkItemFactory *ifactory, MsgInfo *msginfo)
2393 {
2394         gint menu_nb = 0;
2395         GtkWidget *menuitem;
2396         
2397         if (!msginfo || !msginfo->extradata) {
2398                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Post", FALSE);
2399                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Help", FALSE);
2400                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Subscribe", FALSE);
2401                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Unsubscribe", FALSE);
2402                 menu_set_sensitive(ifactory, "/Message/Mailing-List/View archive", FALSE);
2403                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Contact owner", FALSE);
2404                 return 0;
2405         }
2406                 
2407         /* Mailing list post */
2408         if (!strcmp2 (msginfo->extradata->list_post, "NO")) {
2409                 g_free(msginfo->extradata->list_post);
2410                 msginfo->extradata->list_post = g_strdup (_("No posting allowed"));
2411         }
2412         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Post");
2413                 
2414         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_post);
2415  
2416         /* Mailing list help */
2417         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Help");
2418         
2419         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_help);
2420
2421         /* Mailing list subscribe */
2422         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Subscribe");
2423         
2424         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_subscribe);
2425                 
2426         /* Mailing list unsubscribe */
2427         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Unsubscribe");
2428         
2429         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_unsubscribe);
2430         
2431         /* Mailing list view archive */
2432         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/View archive");
2433         
2434         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_archive);
2435         
2436         /* Mailing list contact owner */
2437         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Contact owner");
2438         
2439         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_owner);
2440         
2441         return menu_nb;
2442 }
2443
2444 static gint mailing_list_populate_submenu (GtkWidget *menuitem, const gchar * list_header)
2445 {
2446         GtkWidget *item, *menu;
2447         const gchar *url_pt ;
2448         gchar url_decoded[BUFFSIZE];
2449         GList *amenu, *alist;
2450         gint menu_nb = 0;
2451         
2452         menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2453         
2454         /* First delete old submenu */
2455         /* FIXME: we can optimize this, and only change/add/delete necessary items */
2456         for (amenu = (GTK_MENU_SHELL(menu)->children) ; amenu; ) {
2457                 alist = amenu->next;
2458                 item = GTK_WIDGET (amenu->data);
2459                 gtk_widget_destroy (item);
2460                 amenu = alist;
2461         }
2462         if (list_header) {
2463                 for (url_pt = list_header; url_pt && *url_pt;) {
2464                         get_url_part (&url_pt, url_decoded, BUFFSIZE);
2465                         item = NULL;
2466                         if (!g_strncasecmp(url_decoded, "mailto:", 7)) {
2467                                 item = gtk_menu_item_new_with_label ((url_decoded));
2468                                 gtk_signal_connect (GTK_OBJECT(item), "activate",
2469                                                 GTK_SIGNAL_FUNC(mailing_list_compose), NULL);
2470                         }
2471                         else if (!g_strncasecmp (url_decoded, "http:", 5) ||
2472                                  !g_strncasecmp (url_decoded, "https:",6)) {
2473
2474                                 item = gtk_menu_item_new_with_label ((url_decoded));
2475                                 gtk_signal_connect (GTK_OBJECT(item), "activate",
2476                                                 GTK_SIGNAL_FUNC(mailing_list_open_uri), NULL);
2477                         } 
2478                         if (item) {
2479                                 gtk_menu_append (GTK_MENU(menu), item);
2480                                 gtk_widget_show (item);
2481                                 menu_nb++;
2482                         }
2483                 }
2484         }
2485         if (menu_nb)
2486                 gtk_widget_set_sensitive (menuitem, TRUE);
2487         else
2488                 gtk_widget_set_sensitive (menuitem, FALSE);
2489                 
2490
2491         return menu_nb;
2492 }
2493
2494 static void get_url_part (const gchar **buffer, gchar *url_decoded, gint maxlen)
2495 {
2496         gchar tmp[BUFFSIZE];
2497         const gchar *buf;
2498         gint i = 0;
2499         buf = *buffer;
2500         
2501         if (buf == 0x00) {
2502                 *url_decoded = '\0';
2503                 *buffer = NULL;
2504                 return;
2505         }
2506         /* Ignore spaces, comments  and tabs () */
2507         for (;*buf == ' ' || *buf == '(' || *buf == '\t'; buf++)
2508                 if (*buf == '(')
2509                         for (;*buf != ')' && *buf != 0x00; buf++);
2510         
2511         /* First non space and non comment must be a < */
2512         if (*buf =='<' ) {
2513                 buf++;
2514                 for (i = 0; *buf != '>' && *buf != 0x00 && i<maxlen; tmp[i++] = *(buf++));
2515                 buf++;
2516         }
2517         else  {
2518                 *buffer = NULL;
2519                 *url_decoded = '\0';
2520                 return;
2521         }
2522         
2523         tmp[i]       = 0x00;
2524         *url_decoded = '\0';
2525         *buffer = NULL;
2526         
2527         if (i == maxlen) {
2528                 return;
2529         }
2530         decode_uri (url_decoded, (const gchar *)tmp);
2531
2532         /* Prepare the work for the next url in the list */
2533         /* after the closing bracket >, ignore space, comments and tabs */
2534         for (;buf && *buf && (*buf == ' ' || *buf == '(' || *buf == '\t'); buf++)
2535                 if (*buf == '(')
2536                         for (;*buf != ')' && *buf != 0x00; buf++);
2537                         
2538         if (!buf || !*buf) {
2539                 *buffer = NULL;
2540                 return;
2541         }
2542
2543         /* now first non space, non comment must be a comma */
2544         if (*buf != ',')
2545                 for (;*buf != 0x00; buf++);
2546         else
2547                 buf++;
2548         *buffer = buf;
2549 }
2550         
2551 static void mailing_list_compose (GtkWidget *w, gpointer *data)
2552 {
2553         gchar *mailto;
2554
2555         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
2556         compose_new(NULL, mailto+7, NULL);
2557 }
2558  
2559  static void mailing_list_open_uri (GtkWidget *w, gpointer *data)
2560 {
2561  
2562         gchar *mailto;
2563  
2564         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
2565         open_uri (mailto, prefs_common.uri_cmd);
2566
2567         
2568 void main_window_popup(MainWindow *mainwin)
2569 {
2570         if (!GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
2571                 main_window_show(mainwin);
2572
2573         gtkut_window_popup(mainwin->window);
2574
2575         switch (mainwin->type) {
2576         case SEPARATE_FOLDER:
2577                 gtkut_window_popup(mainwin->win.sep_folder.folderwin);
2578                 break;
2579         case SEPARATE_MESSAGE:
2580                 gtkut_window_popup(mainwin->win.sep_message.messagewin);
2581                 break;
2582         case SEPARATE_BOTH:
2583                 gtkut_window_popup(mainwin->win.sep_both.folderwin);
2584                 gtkut_window_popup(mainwin->win.sep_both.messagewin);
2585                 break;
2586         default:
2587                 break;
2588         }
2589 }
2590
2591 void main_window_show(MainWindow *mainwin)
2592 {
2593         gtk_widget_show(mainwin->window);
2594         gtk_widget_show(mainwin->vbox_body);
2595
2596         gtk_widget_set_uposition(mainwin->window,
2597                                  prefs_common.mainwin_x,
2598                                  prefs_common.mainwin_y);
2599
2600         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2601                              prefs_common.folderview_width,
2602                              prefs_common.folderview_height);
2603         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2604                              prefs_common.summaryview_width,
2605                              prefs_common.summaryview_height);
2606         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2607                              prefs_common.msgview_width,
2608                              prefs_common.msgview_height);
2609
2610         if (mainwin->type & SEPARATE_FOLDER) {
2611                 GtkWidget *folderwin;
2612
2613                 folderwin = main_window_get_folder_window(mainwin);
2614                 gtk_widget_show(folderwin);
2615                 gtk_widget_set_uposition(folderwin, prefs_common.folderwin_x,
2616                                          prefs_common.folderwin_y);
2617         }
2618
2619         if (mainwin->type & SEPARATE_MESSAGE) {
2620                 GtkWidget *messagewin;
2621
2622                 messagewin = main_window_get_message_window(mainwin);
2623                 gtk_widget_show(messagewin);
2624                 gtk_widget_set_uposition(messagewin, prefs_common.main_msgwin_x,
2625                                          prefs_common.main_msgwin_y);
2626         }
2627 }
2628
2629 void main_window_hide(MainWindow *mainwin)
2630 {
2631         main_window_get_size(mainwin);
2632         main_window_get_position(mainwin);
2633
2634         gtk_widget_hide(mainwin->window);
2635         gtk_widget_hide(mainwin->vbox_body);
2636
2637         if (mainwin->type & SEPARATE_FOLDER) {
2638                 gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2639         }
2640
2641         if (mainwin->type & SEPARATE_MESSAGE) {
2642                 gtk_widget_hide(mainwin->win.sep_message.messagewin);
2643         }
2644 }
2645
2646 static void main_window_set_widgets(MainWindow *mainwin, SeparateType type)
2647 {
2648         GtkWidget *folderwin = NULL;
2649         GtkWidget *messagewin = NULL;
2650         GtkWidget *hpaned;
2651         GtkWidget *vpaned;
2652         GtkWidget *vbox_body = mainwin->vbox_body;
2653         GtkItemFactory *ifactory = mainwin->menu_factory;
2654         GtkWidget *menuitem;
2655         GtkItemFactory *msgview_ifactory;
2656
2657         debug_print("Setting widgets... ");
2658
2659         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2660                                     prefs_common.folderview_width,
2661                                     prefs_common.folderview_height);
2662         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2663                                     prefs_common.summaryview_width,
2664                                     prefs_common.summaryview_height);
2665         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2666                                     prefs_common.msgview_width,
2667                                     prefs_common.msgview_height);
2668
2669         /* create separated window(s) if needed */
2670         if (type & SEPARATE_FOLDER) {
2671                 static GdkGeometry folderwin_geometry;
2672                 
2673                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2674                 gtk_window_set_title(GTK_WINDOW(folderwin),
2675                                      _("Sylpheed-Claws - Folder View"));
2676
2677                 gtk_window_move(GTK_WINDOW(folderwin),
2678                                 prefs_common.folderwin_x,
2679                                 prefs_common.folderwin_y);
2680
2681                 if (!folderwin_geometry.min_height) {
2682                         folderwin_geometry.min_width = 320;
2683                         folderwin_geometry.min_height = 200;
2684                 }
2685                 gtk_window_set_geometry_hints(GTK_WINDOW(folderwin), NULL,
2686                                               &folderwin_geometry, GDK_HINT_MIN_SIZE);
2687                                 
2688                 gtk_widget_set_size_request(folderwin,
2689                                             prefs_common.folderview_width,
2690                                             prefs_common.folderview_height);
2691
2692                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
2693                                                BORDER_WIDTH);
2694
2695                 g_signal_connect(G_OBJECT(folderwin), "delete_event",
2696                                  G_CALLBACK(folder_window_close_cb),
2697                                    mainwin);
2698                 gtk_container_add(GTK_CONTAINER(folderwin),
2699                                   GTK_WIDGET_PTR(mainwin->folderview));
2700                 gtk_widget_realize(folderwin);
2701
2702                 if (prefs_common.folderview_visible)
2703                         gtk_widget_show(folderwin);
2704         }
2705
2706         if (type & SEPARATE_MESSAGE) {
2707                 static GdkGeometry msgwin_geometry;
2708                 
2709                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2710                 gtk_window_set_title(GTK_WINDOW(messagewin),
2711                                      _("Sylpheed-Claws - Message View"));
2712                                      
2713                 gtk_window_move(GTK_WINDOW(messagewin), 
2714                                 prefs_common.main_msgwin_x,
2715                                 prefs_common.main_msgwin_y);
2716
2717                 if (!msgwin_geometry.min_height) {
2718                         msgwin_geometry.min_width = 320;
2719                         msgwin_geometry.min_height = 200;
2720                 }
2721                 gtk_window_set_geometry_hints(GTK_WINDOW(messagewin), NULL,
2722                                               &msgwin_geometry, GDK_HINT_MIN_SIZE);
2723                 
2724                 gtk_widget_set_size_request(messagewin, 
2725                                             prefs_common.msgwin_width,
2726                                             prefs_common.msgwin_height);
2727
2728                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
2729                                                BORDER_WIDTH);
2730                 g_signal_connect(G_OBJECT(messagewin), "delete_event",
2731                                  G_CALLBACK(message_window_close_cb),
2732                                  mainwin);
2733                 if (messageview_is_visible(mainwin->messageview))
2734                         gtk_widget_show(messagewin);
2735         } else {
2736                 mainwin->messageview->statusbar = mainwin->statusbar;
2737                 mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2738         }
2739
2740         switch (type) {
2741         case SEPARATE_NONE:
2742                 hpaned = gtk_hpaned_new();
2743                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2744                 gtk_paned_add1(GTK_PANED(hpaned),
2745                                GTK_WIDGET_PTR(mainwin->folderview));
2746                 gtk_widget_show(hpaned);
2747                 gtk_widget_queue_resize(hpaned);
2748
2749                 vpaned = gtk_vpaned_new();
2750                 if (messageview_is_visible(mainwin->messageview)) {
2751                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2752                         gtk_paned_add1(GTK_PANED(vpaned),
2753                                        GTK_WIDGET_PTR(mainwin->summaryview));
2754                 } else {
2755                         gtk_paned_add2(GTK_PANED(hpaned),
2756                                        GTK_WIDGET_PTR(mainwin->summaryview));
2757                         gtk_widget_ref(vpaned);
2758                 }
2759                 gtk_paned_add2(GTK_PANED(vpaned),
2760                                GTK_WIDGET_PTR(mainwin->messageview));
2761                 gtk_widget_show(vpaned);
2762                 gtk_widget_queue_resize(vpaned);
2763
2764                 mainwin->win.sep_none.hpaned = hpaned;
2765                 mainwin->win.sep_none.vpaned = vpaned;
2766                 
2767                 /* remove headerview if not in prefs */
2768                 headerview_set_visibility(mainwin->messageview->headerview,
2769                                           prefs_common.display_header_pane);
2770                 break;
2771         case SEPARATE_FOLDER:
2772                 vpaned = gtk_vpaned_new();
2773                 if (messageview_is_visible(mainwin->messageview)) {
2774                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
2775                                            TRUE, TRUE, 0);
2776                         gtk_paned_add1(GTK_PANED(vpaned),
2777                                        GTK_WIDGET_PTR(mainwin->summaryview));
2778                 } else {
2779                         gtk_box_pack_start(GTK_BOX(vbox_body),
2780                                            GTK_WIDGET_PTR(mainwin->summaryview),
2781                                            TRUE, TRUE, 0);
2782                         gtk_widget_ref(vpaned);
2783                 }
2784                 gtk_paned_add2(GTK_PANED(vpaned),
2785                                GTK_WIDGET_PTR(mainwin->messageview));
2786                 gtk_widget_show(vpaned);
2787                 gtk_widget_queue_resize(vpaned);
2788
2789                 mainwin->win.sep_folder.folderwin = folderwin;
2790                 mainwin->win.sep_folder.vpaned    = vpaned;
2791
2792                 /* remove headerview if not in prefs */
2793                 headerview_set_visibility(mainwin->messageview->headerview,
2794                                           prefs_common.display_header_pane);
2795                 
2796                 break;
2797         case SEPARATE_MESSAGE:
2798                 hpaned = gtk_hpaned_new();
2799                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2800                 gtk_paned_add1(GTK_PANED(hpaned),
2801                                GTK_WIDGET_PTR(mainwin->folderview));
2802                 gtk_paned_add2(GTK_PANED(hpaned),
2803                                GTK_WIDGET_PTR(mainwin->summaryview));
2804                 gtk_widget_show(hpaned);
2805                 gtk_widget_queue_resize(hpaned);
2806                 messageview_add_toolbar(mainwin->messageview, messagewin);
2807                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2808                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2809
2810                 mainwin->win.sep_message.messagewin = messagewin;
2811                 mainwin->win.sep_message.hpaned     = hpaned;
2812
2813                 gtk_widget_realize(messagewin);
2814                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2815                 gtk_widget_show_all(messagewin);
2816                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2817                                   mainwin->messageview->handlebox, 
2818                                   prefs_common.toolbar_style);
2819
2820                 /* remove headerview if not in prefs */
2821                 headerview_set_visibility(mainwin->messageview->headerview,
2822                                           prefs_common.display_header_pane);
2823
2824                 break;
2825         case SEPARATE_BOTH:
2826                 messageview_add_toolbar(mainwin->messageview, messagewin);
2827                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2828                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2829
2830                 gtk_box_pack_start(GTK_BOX(vbox_body),
2831                                    GTK_WIDGET_PTR(mainwin->summaryview),
2832                                    TRUE, TRUE, 0);
2833                 
2834                 mainwin->win.sep_both.folderwin = folderwin;
2835                 mainwin->win.sep_both.messagewin = messagewin;
2836                 
2837                 gtk_widget_realize(messagewin);
2838                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2839                 gtk_widget_show_all(messagewin);
2840                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2841                                   mainwin->messageview->handlebox, 
2842                                   prefs_common.toolbar_style);          
2843
2844                 /* remove headerview if not in prefs */
2845                 headerview_set_visibility(mainwin->messageview->headerview,
2846                                           prefs_common.display_header_pane);
2847
2848                 break;
2849         }
2850
2851         if (messageview_is_visible(mainwin->messageview))
2852                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2853                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2854         else
2855                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2856                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2857
2858         gtk_window_move(GTK_WINDOW(mainwin->window),
2859                         prefs_common.mainwin_x,
2860                         prefs_common.mainwin_y);
2861
2862         gtk_widget_queue_resize(vbox_body);
2863         gtk_widget_queue_resize(mainwin->vbox);
2864         gtk_widget_queue_resize(mainwin->window);
2865         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2866          * and mimeview icon list/ctree lose track of their visibility states */
2867         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2868                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2869         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2870                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2871         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2872                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2873         else 
2874                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2875
2876         mainwin->type = type;
2877
2878
2879         /* toggle menu state */
2880         menuitem = gtk_item_factory_get_item
2881                 (ifactory, "/View/Show or hide/Folder tree");
2882         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2883                                        (type & SEPARATE_FOLDER) == 0 ? TRUE :
2884                                        prefs_common.folderview_visible);
2885         gtk_widget_set_sensitive(menuitem, ((type & SEPARATE_FOLDER) != 0));
2886         menuitem = gtk_item_factory_get_item
2887                 (ifactory, "/View/Show or hide/Message view");
2888         gtk_check_menu_item_set_active
2889                 (GTK_CHECK_MENU_ITEM(menuitem),
2890                  messageview_is_visible(mainwin->messageview));
2891
2892         menuitem = gtk_item_factory_get_item
2893                 (ifactory, "/View/Separate folder tree");
2894         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2895                                        ((type & SEPARATE_FOLDER) != 0));
2896         menuitem = gtk_item_factory_get_item
2897                 (ifactory, "/View/Separate message view");
2898         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2899                                        ((type & SEPARATE_MESSAGE) != 0));
2900
2901         if (folderwin) {
2902                 g_signal_connect
2903                         (G_OBJECT(folderwin), "size_allocate",
2904                          G_CALLBACK(folder_window_size_allocate_cb),
2905                          mainwin);
2906         }
2907         if (messagewin) {
2908                 g_signal_connect
2909                         (G_OBJECT(messagewin), "size_allocate",
2910                          G_CALLBACK(message_window_size_allocate_cb),
2911                          mainwin);
2912         }
2913
2914         debug_print("done.\n");
2915 }
2916
2917 void main_window_destroy_all(void)
2918 {
2919         while (mainwin_list != NULL) {
2920                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2921                 
2922                 /* free toolbar stuff */
2923                 toolbar_clear_list(TOOLBAR_MAIN);
2924                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2925                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2926
2927                 mainwin->folderview->mainwin = NULL;
2928                 mainwin->summaryview->mainwin = NULL;
2929                 mainwin->messageview->mainwin = NULL;
2930
2931                 g_free(mainwin->toolbar);
2932                 g_free(mainwin);
2933                 
2934                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2935         }
2936         g_list_free(mainwin_list);
2937         mainwin_list = NULL;
2938 }
2939
2940 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2941                                    gpointer data)
2942 {
2943         gtk_widget_set_size_request(child, 1, -1);
2944 }
2945
2946 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2947                                    gpointer data)
2948 {
2949         gtk_widget_set_size_request(child, -1, -1);
2950 }
2951
2952 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2953                                     gpointer data)
2954 {
2955         MainWindow *mainwin = (MainWindow *)data;
2956
2957         if (!event) return FALSE;
2958
2959         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2960         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2961                           widget);
2962
2963         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2964                        menu_button_position, widget,
2965                        event->button, event->time);
2966
2967         return TRUE;
2968 }
2969
2970 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2971                                  gpointer data)
2972 {
2973         MainWindow *mainwin = (MainWindow *)data;
2974
2975         if (mainwin->lock_count == 0)
2976                 app_exit_cb(data, 0, widget);
2977
2978         return TRUE;
2979 }
2980
2981 static gint folder_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2982                                    gpointer data)
2983 {
2984         MainWindow *mainwin = (MainWindow *)data;
2985         GtkWidget *menuitem;
2986
2987         menuitem = gtk_item_factory_get_item
2988                 (mainwin->menu_factory, "/View/Show or hide/Folder tree");
2989         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2990
2991         return TRUE;
2992 }
2993
2994 static gint message_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2995                                     gpointer data)
2996 {
2997         MainWindow *mainwin = (MainWindow *)data;
2998         GtkWidget *menuitem;
2999
3000         menuitem = gtk_item_factory_get_item
3001                 (mainwin->menu_factory, "/View/Show or hide/Message view");
3002         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
3003
3004         mainwin->messageview->statusbar = mainwin->statusbar;
3005         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
3006
3007         return TRUE;
3008 }
3009
3010 static void main_window_size_allocate_cb(GtkWidget *widget,
3011                                          GtkAllocation *allocation,
3012                                          gpointer data)
3013 {
3014         MainWindow *mainwin = (MainWindow *)data;
3015
3016         main_window_get_size(mainwin);
3017 }
3018
3019 static void folder_window_size_allocate_cb(GtkWidget *widget,
3020                                            GtkAllocation *allocation,
3021                                            gpointer data)
3022 {
3023         MainWindow *mainwin = (MainWindow *)data;
3024
3025         main_window_get_size(mainwin);
3026 }
3027
3028 static void message_window_size_allocate_cb(GtkWidget *widget,
3029                                             GtkAllocation *allocation,
3030                                             gpointer data)
3031 {
3032         MainWindow *mainwin = (MainWindow *)data;
3033
3034         main_window_get_size(mainwin);
3035 }
3036
3037 static void add_mailbox_cb(MainWindow *mainwin, guint action,
3038                            GtkWidget *widget)
3039 {
3040         main_window_add_mailbox(mainwin);
3041 }
3042
3043 static void update_folderview_cb(MainWindow *mainwin, guint action,
3044                                  GtkWidget *widget)
3045 {
3046         summary_show(mainwin->summaryview, NULL);
3047         folderview_check_new_all();
3048 }
3049
3050 static void foldersort_cb(MainWindow *mainwin, guint action,
3051                            GtkWidget *widget)
3052 {
3053         foldersort_open();
3054 }
3055
3056 static void import_mbox_cb(MainWindow *mainwin, guint action,
3057                            GtkWidget *widget)
3058 {
3059         /* only notify if import has failed */
3060         if (import_mbox(mainwin->summaryview->folder_item) == -1) {
3061                 alertpanel_error(_("Mbox import has failed."));
3062         }
3063 }
3064
3065 static void export_mbox_cb(MainWindow *mainwin, guint action,
3066                            GtkWidget *widget)
3067 {
3068         /* only notify if export has failed */
3069         if (export_mbox(mainwin->summaryview->folder_item) == -1) {
3070                 alertpanel_error(_("Export to mbox has failed."));
3071         }
3072 }
3073
3074 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
3075                                 GtkWidget *widget)
3076 {
3077         /* only notify if export has failed */
3078         if (summaryview_export_mbox_list(mainwin->summaryview) == -1) {
3079                 alertpanel_error(_("Export to mbox has failed."));
3080         }
3081 }
3082
3083 static void empty_trash_cb(MainWindow *mainwin, guint action,
3084                            GtkWidget *widget)
3085 {
3086         main_window_empty_trash(mainwin, TRUE);
3087 }
3088
3089 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3090 {
3091         summary_save_as(mainwin->summaryview);
3092 }
3093
3094 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3095 {
3096         summary_print(mainwin->summaryview);
3097 }
3098
3099 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3100 {
3101         if (prefs_common.confirm_on_exit) {
3102                 if (alertpanel(_("Exit"), _("Exit Sylpheed-Claws?"),
3103                                GTK_STOCK_CANCEL, GTK_STOCK_QUIT,  NULL)
3104                     != G_ALERTALTERNATE)
3105                         return;
3106                 manage_window_focus_in(mainwin->window, NULL, NULL);
3107         }
3108
3109         app_will_exit(widget, mainwin);
3110 }
3111
3112 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3113 {
3114         if (action == 1)
3115                 summary_search(mainwin->summaryview);
3116         else
3117                 message_search(mainwin->messageview);
3118 }
3119
3120 static void mainwindow_quicksearch(MainWindow *mainwin, guint action, GtkWidget *widget)
3121 {
3122         prefs_common.show_searchbar = TRUE;
3123         summaryview_activate_quicksearch(mainwin->summaryview);
3124 }
3125
3126 static void toggle_folder_cb(MainWindow *mainwin, guint action,
3127                              GtkWidget *widget)
3128 {
3129         gboolean active;
3130
3131         active = GTK_CHECK_MENU_ITEM(widget)->active;
3132
3133         switch (mainwin->type) {
3134         case SEPARATE_NONE:
3135         case SEPARATE_MESSAGE:
3136                 break;
3137         case SEPARATE_FOLDER:
3138                 debug_print("separate folder\n");
3139                 if (active)
3140                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
3141                 else
3142                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
3143                 break;
3144         case SEPARATE_BOTH:
3145                 if (active)
3146                         gtk_widget_show(mainwin->win.sep_both.folderwin);
3147                 else
3148                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
3149                 break;
3150         }
3151
3152         prefs_common.folderview_visible = active;
3153 }
3154
3155 static void toggle_message_cb(MainWindow *mainwin, guint action,
3156                               GtkWidget *widget)
3157 {
3158         gboolean active;
3159
3160         active = GTK_CHECK_MENU_ITEM(widget)->active;
3161
3162         if (active != messageview_is_visible(mainwin->messageview))
3163                 summary_toggle_view(mainwin->summaryview);
3164 }
3165
3166 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
3167                               GtkWidget *widget)
3168 {
3169         toolbar_toggle(action, mainwin);
3170 }
3171
3172 void main_window_reply_cb(MainWindow *mainwin, guint action,
3173                           GtkWidget *widget)
3174 {
3175         MessageView *msgview = (MessageView*)mainwin->messageview;
3176         GSList *msginfo_list = NULL;
3177
3178         g_return_if_fail(msgview != NULL);
3179
3180         msginfo_list = summary_get_selection(mainwin->summaryview);
3181         g_return_if_fail(msginfo_list != NULL);
3182         compose_reply_from_messageview(msgview, msginfo_list, action);
3183         g_slist_free(msginfo_list);
3184 }
3185
3186
3187 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
3188                                 GtkWidget *widget)
3189 {
3190         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3191                 gtk_widget_show(mainwin->hbox_stat);
3192                 prefs_common.show_statusbar = TRUE;
3193         } else {
3194                 gtk_widget_hide(mainwin->hbox_stat);
3195                 prefs_common.show_statusbar = FALSE;
3196         }
3197 }
3198
3199 static void separate_widget_cb(MainWindow *mainwin, guint action,
3200                                GtkWidget *widget)
3201 {
3202         SeparateType type;
3203
3204         if (GTK_CHECK_MENU_ITEM(widget)->active)
3205                 type = mainwin->type | action;
3206         else
3207                 type = mainwin->type & ~action;
3208
3209         main_window_separation_change(mainwin, type);
3210
3211         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
3212         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
3213 }
3214
3215 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline,
3216                                         gboolean ask_sync)
3217 {
3218         offline_ask_sync = ask_sync;
3219         if (offline)
3220                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
3221         else
3222                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
3223         offline_ask_sync = TRUE;
3224 }
3225
3226 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
3227 {
3228         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active, TRUE);
3229 }
3230
3231 static void mainwindow_check_synchronise(MainWindow *mainwin, gboolean ask)
3232 {
3233         GList *folderlist = folder_get_list();
3234         gboolean found = FALSE;
3235
3236         /* see if there are synchronised folders */
3237         for (; folderlist; folderlist = folderlist->next) {
3238                 Folder *folder = (Folder *)folderlist->data;
3239                 if (folder_want_synchronise(folder)) {
3240                         found = TRUE;
3241                         break;
3242                 }
3243         }
3244         
3245         if (!found)
3246                 return;
3247                 
3248         if (offline_ask_sync && ask && alertpanel(_("Folder synchronisation"),
3249                         _("Do you want to synchronise your folders now?"),
3250                         GTK_STOCK_CANCEL, _("+_Synchronise"), NULL) != G_ALERTALTERNATE)
3251                 return;
3252
3253         folder_synchronise(NULL);
3254 }
3255
3256 static void online_switch_clicked (GtkButton *btn, gpointer data) 
3257 {
3258         MainWindow *mainwin;
3259         GtkItemFactory *ifactory;
3260         GtkCheckMenuItem *menuitem;
3261
3262         mainwin = (MainWindow *) data;
3263         
3264         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
3265         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
3266         
3267         g_return_if_fail(mainwin != NULL);
3268         g_return_if_fail(menuitem != NULL);
3269         
3270         if (btn == GTK_BUTTON(mainwin->online_switch)) {
3271                 gtk_widget_hide (mainwin->online_switch);
3272                 gtk_widget_show (mainwin->offline_switch);
3273                 menuitem->active = TRUE;
3274                 inc_autocheck_timer_remove();
3275                         
3276                 /* go offline */
3277                 if (prefs_common.work_offline)
3278                         return;
3279                 mainwindow_check_synchronise(mainwin, TRUE);
3280                 prefs_common.work_offline = TRUE;
3281                 imap_disconnect_all();
3282         } else {
3283                 /*go online */
3284                 if (!prefs_common.work_offline)
3285                         return;
3286                 gtk_widget_hide (mainwin->offline_switch);
3287                 gtk_widget_show (mainwin->online_switch);
3288                 menuitem->active = FALSE;
3289                 prefs_common.work_offline = FALSE;
3290                 inc_autocheck_timer_set();
3291                 refresh_resolvers();
3292         }
3293 }
3294
3295 static void addressbook_open_cb(MainWindow *mainwin, guint action,
3296                                 GtkWidget *widget)
3297 {
3298         addressbook_open(NULL);
3299 }
3300
3301 static void log_window_show_cb(MainWindow *mainwin, guint action,
3302                                GtkWidget *widget)
3303 {
3304         log_window_show(mainwin->logwin);
3305 }
3306
3307 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3308 {
3309         inc_cancel_all();
3310 }
3311
3312 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3313 {
3314         summary_move_to(mainwin->summaryview);
3315 }
3316
3317 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3318 {
3319         summary_copy_to(mainwin->summaryview);
3320 }
3321
3322 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3323 {
3324         summary_delete(mainwin->summaryview);
3325 }
3326
3327 static void delete_trash_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3328 {
3329         summary_delete_trash(mainwin->summaryview);
3330 }
3331
3332 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3333 {
3334         summary_cancel(mainwin->summaryview);
3335 }
3336
3337 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3338 {
3339         summary_open_msg(mainwin->summaryview);
3340 }
3341
3342 static void view_source_cb(MainWindow *mainwin, guint action,
3343                            GtkWidget *widget)
3344 {
3345         summary_view_source(mainwin->summaryview);
3346 }
3347
3348 static void show_all_header_cb(MainWindow *mainwin, guint action,
3349                                GtkWidget *widget)
3350 {
3351         if (mainwin->menu_lock_count) return;
3352         mainwin->summaryview->messageview->all_headers = 
3353                         GTK_CHECK_MENU_ITEM(widget)->active;
3354         summary_display_msg_selected(mainwin->summaryview,
3355                                      GTK_CHECK_MENU_ITEM(widget)->active);
3356 }
3357
3358 #define SET_CHECK_MENU_ACTIVE(path, active) \
3359 { \
3360         menuitem = gtk_item_factory_get_widget(ifactory, path); \
3361         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
3362 }
3363
3364 static void hide_quotes_cb(MainWindow *mainwin, guint action,
3365                                GtkWidget *widget)
3366 {
3367         GtkWidget *menuitem;
3368         GtkItemFactory *ifactory = mainwin->menu_factory;
3369
3370         if (mainwin->menu_lock_count) return;
3371
3372         prefs_common.hide_quotes = 
3373                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
3374
3375         mainwin->menu_lock_count++;
3376         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
3377         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
3378         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
3379         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);
3380         mainwin->menu_lock_count--;
3381
3382         summary_redisplay_msg(mainwin->summaryview);
3383 }
3384
3385 #undef SET_CHECK_MENU_ACTIVE
3386 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3387 {
3388         summary_mark(mainwin->summaryview);
3389 }
3390
3391 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3392 {
3393         summary_unmark(mainwin->summaryview);
3394 }
3395
3396 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
3397                               GtkWidget *widget)
3398 {
3399         summary_mark_as_unread(mainwin->summaryview);
3400 }
3401
3402 static void mark_as_read_cb(MainWindow *mainwin, guint action,
3403                             GtkWidget *widget)
3404 {
3405         summary_mark_as_read(mainwin->summaryview);
3406 }
3407
3408 static void mark_all_read_cb(MainWindow *mainwin, guint action,
3409                              GtkWidget *widget)
3410 {
3411         summary_mark_all_read(mainwin->summaryview);
3412 }
3413
3414 static void mark_as_spam_cb(MainWindow *mainwin, guint action,
3415                              GtkWidget *widget)
3416 {
3417         summary_mark_as_spam(mainwin->summaryview, action, NULL);
3418 }
3419
3420 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3421 {
3422         summary_reedit(mainwin->summaryview);
3423 }
3424
3425 static void add_address_cb(MainWindow *mainwin, guint action,
3426                            GtkWidget *widget)
3427 {
3428         summary_add_address(mainwin->summaryview);
3429 }
3430
3431 static void set_charset_cb(MainWindow *mainwin, guint action,
3432                            GtkWidget *widget)
3433 {
3434         const gchar *str;
3435
3436         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3437                 str = conv_get_charset_str((CharSet)action);
3438                 
3439                 g_free(mainwin->messageview->forced_charset);
3440                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
3441                 procmime_force_charset(str);
3442                 
3443                 summary_redisplay_msg(mainwin->summaryview);
3444                 
3445                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
3446         }
3447 }
3448
3449 static void set_decode_cb(MainWindow *mainwin, guint action,
3450                            GtkWidget *widget)
3451 {
3452         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3453                 mainwin->messageview->forced_encoding = (EncodingType)action;
3454                 
3455                 summary_redisplay_msg(mainwin->summaryview);
3456                 
3457                 debug_print("forced encoding: %d\n", action);
3458         }
3459 }
3460
3461 static void hide_read_messages (MainWindow *mainwin, guint action,
3462                                 GtkWidget *widget)
3463 {
3464         if (!mainwin->summaryview->folder_item
3465             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
3466                 return;
3467         summary_toggle_show_read_messages(mainwin->summaryview);
3468 }
3469
3470 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3471 {
3472         gboolean threaded = FALSE;
3473         if (mainwin->menu_lock_count) return;
3474         if (!mainwin->summaryview->folder_item) return;
3475
3476         threaded = GTK_CHECK_MENU_ITEM(widget)->active;
3477
3478         mainwin->summaryview->folder_item->threaded = threaded; 
3479
3480         mainwin->summaryview->threaded = threaded;
3481
3482         summary_show(mainwin->summaryview, 
3483                         mainwin->summaryview->folder_item);
3484 }
3485
3486 static void expand_threads_cb(MainWindow *mainwin, guint action,
3487                               GtkWidget *widget)
3488 {
3489         summary_expand_threads(mainwin->summaryview);
3490 }
3491
3492 static void collapse_threads_cb(MainWindow *mainwin, guint action,
3493                                 GtkWidget *widget)
3494 {
3495         summary_collapse_threads(mainwin->summaryview);
3496 }
3497
3498 static void set_summary_display_item_cb(MainWindow *mainwin, guint action,
3499                                 GtkWidget *widget)
3500 {
3501         prefs_summary_column_open();
3502 }
3503
3504 static void set_folder_display_item_cb(MainWindow *mainwin, guint action,
3505                                 GtkWidget *widget)
3506 {
3507         prefs_folder_column_open();
3508 }
3509
3510 static void sort_summary_cb(MainWindow *mainwin, guint action,
3511                             GtkWidget *widget)
3512 {
3513         FolderItem *item = mainwin->summaryview->folder_item;
3514         GtkWidget *menuitem;
3515
3516         if (mainwin->menu_lock_count) return;
3517
3518         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
3519                 menuitem = gtk_item_factory_get_item
3520                         (mainwin->menu_factory, "/View/Sort/Ascending");
3521                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
3522                              GTK_CHECK_MENU_ITEM(menuitem)->active
3523                              ? SORT_ASCENDING : SORT_DESCENDING);
3524                 item->sort_key = action;
3525         }
3526 }
3527
3528 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
3529                                  GtkWidget *widget)
3530 {
3531         FolderItem *item = mainwin->summaryview->folder_item;
3532
3533         if (mainwin->menu_lock_count) return;
3534
3535         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
3536                 summary_sort(mainwin->summaryview,
3537                              item->sort_key, (FolderSortType)action);
3538 }
3539
3540 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
3541                                   GtkWidget *widget)
3542 {
3543         summary_attract_by_subject(mainwin->summaryview);
3544 }
3545
3546 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
3547                                  GtkWidget *widget)
3548 {
3549         FolderItem *item;
3550
3551         item = folderview_get_selected_item(mainwin->folderview);
3552         if (item) {
3553                 main_window_cursor_wait(mainwin);
3554                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
3555
3556                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
3557                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
3558
3559                 STATUSBAR_POP(mainwin);
3560                 main_window_cursor_normal(mainwin);
3561         }
3562 }
3563
3564 struct DelDupsData
3565 {
3566         guint   dups;
3567         guint   folders;
3568 };
3569
3570 static void deldup_all(FolderItem *item, gpointer _data)
3571 {
3572         struct DelDupsData *data = _data;
3573         gint result;
3574         
3575         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
3576         if (result >= 0) {
3577                 data->dups += result;
3578                 data->folders += 1;
3579         }
3580 }
3581
3582 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
3583                                  GtkWidget *widget)
3584 {
3585         struct DelDupsData data = {0, 0};
3586
3587         folder_func_to_all_folders(deldup_all, &data);
3588         alertpanel_notice(ngettext("Deleted %d duplicate message in %d folders.\n",
3589                                    "Deleted %d duplicate messages in %d folders.\n",
3590                                    data.dups),
3591                           data.dups, data.folders);
3592 }
3593
3594 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3595 {
3596         summary_filter(mainwin->summaryview, (gboolean)action);
3597 }
3598
3599 static void execute_summary_cb(MainWindow *mainwin, guint action,
3600                                GtkWidget *widget)
3601 {
3602         summary_execute(mainwin->summaryview);
3603 }
3604
3605 static void update_summary_cb(MainWindow *mainwin, guint action,
3606                               GtkWidget *widget)
3607 {
3608         FolderItem *fitem;
3609         FolderView *folderview = mainwin->folderview;
3610
3611         if (!mainwin->summaryview->folder_item) return;
3612         if (!folderview->opened) return;
3613
3614         folder_update_op_count();
3615
3616         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
3617                                             folderview->opened);
3618         if (!fitem) return;
3619
3620         folder_item_scan(fitem);
3621         summary_show(mainwin->summaryview, fitem);
3622 }
3623
3624 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3625 {
3626         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
3627 }
3628
3629 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3630 {
3631         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
3632 }
3633
3634 static void prev_unread_cb(MainWindow *mainwin, guint action,
3635                            GtkWidget *widget)
3636 {
3637         summary_select_prev_unread(mainwin->summaryview);
3638 }
3639
3640 static void next_unread_cb(MainWindow *mainwin, guint action,
3641                            GtkWidget *widget)
3642 {
3643         summary_select_next_unread(mainwin->summaryview);
3644 }
3645
3646 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3647 {
3648         summary_select_prev_new(mainwin->summaryview);
3649 }
3650
3651 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3652 {
3653         summary_select_next_new(mainwin->summaryview);
3654 }
3655
3656 static void prev_marked_cb(MainWindow *mainwin, guint action,
3657                            GtkWidget *widget)
3658 {
3659         summary_select_prev_marked(mainwin->summaryview);
3660 }
3661
3662 static void next_marked_cb(MainWindow *mainwin, guint action,
3663                            GtkWidget *widget)
3664 {
3665         summary_select_next_marked(mainwin->summaryview);
3666 }
3667
3668 static void prev_labeled_cb(MainWindow *mainwin, guint action,
3669                             GtkWidget *widget)
3670 {
3671         summary_select_prev_labeled(mainwin->summaryview);
3672 }
3673
3674 static void next_labeled_cb(MainWindow *mainwin, guint action,
3675                             GtkWidget *widget)
3676 {
3677         summary_select_next_labeled(mainwin->summaryview);
3678 }
3679
3680 static void last_read_cb(MainWindow *mainwin, guint action,
3681                             GtkWidget *widget)
3682 {
3683         summary_select_last_read(mainwin->summaryview);
3684 }
3685
3686 static void parent_cb(MainWindow *mainwin, guint action,
3687                             GtkWidget *widget)
3688 {
3689         summary_select_parent(mainwin->summaryview);
3690 }
3691
3692 static void goto_folder_cb(MainWindow *mainwin, guint action,
3693                            GtkWidget *widget)
3694 {
3695         FolderItem *to_folder;
3696
3697         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
3698
3699         if (to_folder)
3700                 folderview_select(mainwin->folderview, to_folder);
3701 }
3702
3703 static void goto_unread_folder_cb(MainWindow *mainwin, guint action,
3704                            GtkWidget *widget)
3705 {
3706         folderview_select_next_unread(mainwin->folderview, FALSE);
3707 }
3708
3709 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3710 {
3711         messageview_copy_clipboard(mainwin->messageview);
3712 }
3713
3714 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3715 {
3716         MessageView *msgview = mainwin->messageview;
3717
3718         if (messageview_is_visible(msgview) &&
3719                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
3720                 messageview_select_all(mainwin->messageview);
3721         else
3722                 summary_select_all(mainwin->summaryview);
3723 }
3724
3725 static void select_thread_cb(MainWindow *mainwin, guint action,
3726                              GtkWidget *widget)
3727 {
3728         summary_select_thread(mainwin->summaryview);
3729 }
3730
3731 static void create_filter_cb(MainWindow *mainwin, guint action,
3732                              GtkWidget *widget)
3733 {
3734         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
3735 }
3736
3737 static void create_processing_cb(MainWindow *mainwin, guint action,
3738                              GtkWidget *widget)
3739 {
3740         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
3741 }
3742
3743 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
3744                                          GtkWidget *widget)
3745 {
3746         prefs_filtering_open(&pre_global_processing,
3747                              _("Processing rules to apply before folder rules"),
3748                              MANUAL_ANCHOR_PROCESSING,
3749                              NULL, NULL, FALSE);
3750 }
3751
3752 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
3753                                           GtkWidget *widget)
3754 {
3755         prefs_filtering_open(&post_global_processing,
3756                              _("Processing rules to apply after folder rules"),
3757                              MANUAL_ANCHOR_PROCESSING,
3758                              NULL, NULL, FALSE);
3759 }
3760
3761 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
3762                                     GtkWidget *widget)
3763 {
3764         prefs_filtering_open(&filtering_rules,
3765                              _("Filtering configuration"),
3766                              MANUAL_ANCHOR_FILTERING,
3767                              NULL, NULL, TRUE);
3768 }
3769
3770 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
3771                                    GtkWidget *widget)
3772 {
3773         prefs_template_open();
3774 }
3775
3776 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
3777                                   GtkWidget *widget)
3778 {
3779         prefs_actions_open(mainwin);
3780 }
3781 #ifdef USE_OPENSSL
3782 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
3783                                   GtkWidget *widget)
3784 {
3785         ssl_manager_open(mainwin);
3786 }
3787 #endif
3788 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
3789                                   GtkWidget *widget)
3790 {
3791         if (!cur_account) {
3792                 new_account_cb(mainwin, 0, widget);
3793         } else {
3794                 account_open(cur_account);
3795         }
3796 }
3797
3798 static void new_account_cb(MainWindow *mainwin, guint action,
3799                            GtkWidget *widget)
3800 {
3801         account_edit_open();
3802         if (!compose_get_compose_list()) account_add();
3803 }
3804
3805 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
3806 {
3807         FolderItem *item = NULL;
3808         cur_account = (PrefsAccount *)data;
3809         
3810         if (!mainwindow_get_mainwindow())
3811                 return;
3812         main_window_show_cur_account(mainwindow_get_mainwindow());
3813         toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
3814         main_window_set_menu_sensitive(mainwindow_get_mainwindow());
3815         toolbar_main_set_sensitive(mainwindow_get_mainwindow());
3816         item = folderview_get_selected_item(
3817                         mainwindow_get_mainwindow()->folderview);
3818         if (item) {
3819                 toolbar_set_compose_button
3820                         (mainwindow_get_mainwindow()->toolbar,
3821                          FOLDER_TYPE(item->folder) == F_NEWS ? 
3822                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
3823         }
3824 }
3825
3826 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
3827 {
3828         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3829         PrefsAccount *account = (PrefsAccount *)data;
3830
3831         inc_account_mail(mainwin, account);
3832 }
3833
3834 static void account_compose_menu_cb(GtkMenuItem *menuitem, gpointer data)
3835 {
3836         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3837         PrefsAccount *account = (PrefsAccount *)data;
3838         FolderItem *item = mainwin->summaryview->folder_item;   
3839
3840         compose_new_with_folderitem(account, item);
3841 }
3842
3843 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
3844 {
3845         prefs_gtk_open();
3846 }
3847
3848 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
3849 {
3850         pluginwindow_create();
3851 }
3852
3853 static void manual_open_cb(MainWindow *mainwin, guint action,
3854                            GtkWidget *widget)
3855 {
3856         manual_open((ManualType)action, NULL);
3857 }
3858
3859 static void legend_open_cb(GtkMenuItem *menuitem, gpointer data)
3860 {
3861         legend_show();
3862 }
3863
3864 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
3865 {
3866         MainWindow *mainwin = (MainWindow *)data;
3867         gchar *str;
3868
3869         if (item->path)
3870                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
3871                                       LOCAL_FOLDER(folder)->rootpath,
3872                                       G_DIR_SEPARATOR,
3873                                       item->path);
3874         else
3875                 str = g_strdup_printf(_("Scanning folder %s ..."),
3876                                       LOCAL_FOLDER(folder)->rootpath);
3877
3878         STATUSBAR_PUSH(mainwin, str);
3879         STATUSBAR_POP(mainwin);
3880         g_free(str);
3881 }
3882
3883 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3884                                           gpointer data)
3885 {
3886         SummaryView *summary;
3887
3888         g_return_val_if_fail(data, FALSE);
3889         if (!g_list_find(mainwin_list, data))
3890                 return TRUE;
3891         summary = ((MainWindow *)data)->summaryview;
3892         g_return_val_if_fail(summary, FALSE);
3893
3894         if (GTK_CLIST(summary->ctree)->selection && 
3895             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3896                 return FALSE;
3897
3898         return FALSE;
3899 }
3900
3901 #define BREAK_ON_MODIFIER_KEY() \
3902         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3903
3904 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3905                                     gpointer data)
3906 {
3907         MainWindow *mainwin = (MainWindow*) data;
3908         
3909         if (!mainwin || !event) 
3910                 return FALSE;
3911
3912         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3913         {
3914                 lastkey = event->keyval;
3915                 return FALSE;
3916         }
3917
3918         switch (event->keyval) {
3919         case GDK_Q:             /* Quit */
3920                 BREAK_ON_MODIFIER_KEY();
3921
3922                 app_exit_cb(mainwin, 0, NULL);
3923                 return FALSE;
3924         case GDK_space:
3925                 if (mainwin->folderview && mainwin->summaryview
3926                     && ((!mainwin->summaryview->displayed
3927                         && !mainwin->summaryview->selected) 
3928                         || (mainwin->summaryview->folder_item
3929                             && mainwin->summaryview->folder_item->total_msgs == 0))) {
3930                         g_signal_stop_emission_by_name(G_OBJECT(widget), 
3931                                        "key_press_event");
3932                         folderview_select_next_unread(mainwin->folderview, TRUE);
3933                 }
3934                 break;
3935         default:
3936                 break;
3937         }
3938         return FALSE;
3939 }
3940
3941 #undef BREAK_ON_MODIFIER_KEY
3942
3943 /*
3944  * Harvest addresses for selected folder.
3945  */
3946 static void addr_harvest_cb( MainWindow *mainwin,
3947                             guint action,
3948                             GtkWidget *widget )
3949 {
3950         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3951 }
3952
3953 /*
3954  * Harvest addresses for selected messages in summary view.
3955  */
3956 static void addr_harvest_msg_cb( MainWindow *mainwin,
3957                             guint action,
3958                             GtkWidget *widget )
3959 {
3960         summary_harvest_address( mainwin->summaryview );
3961 }
3962
3963 /*!
3964  *\brief        get a MainWindow
3965  *
3966  *\return       MainWindow * The first mainwindow in the mainwin_list
3967  */
3968 MainWindow *mainwindow_get_mainwindow(void)
3969 {
3970         if (mainwin_list && mainwin_list->data)
3971                 return (MainWindow *)(mainwin_list->data);
3972         else
3973                 return NULL;
3974 }
3975
3976 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3977 {
3978         ProgressData *data = (ProgressData *) source;
3979         MainWindow *mainwin = (MainWindow *) userdata;
3980
3981         switch (data->cmd) {
3982         case PROGRESS_COMMAND_START:
3983         case PROGRESS_COMMAND_STOP:
3984                 gtk_progress_bar_set_fraction
3985                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3986                 break;
3987         case PROGRESS_COMMAND_SET_PERCENTAGE:
3988                 gtk_progress_bar_set_fraction
3989                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3990                 break;          
3991         }
3992         while (gtk_events_pending()) gtk_main_iteration ();
3993
3994         return FALSE;
3995 }
3996
3997 static void sync_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3998 {
3999         mainwindow_check_synchronise(mainwin, FALSE);
4000 }
4001
4002 void mainwindow_learn (MainWindow *mainwin, gboolean is_spam)
4003 {
4004         summary_mark_as_spam(mainwin->summaryview, is_spam, NULL);
4005 }
4006
4007 void mainwindow_jump_to(const gchar *target)
4008 {
4009         gchar *tmp = NULL;
4010         gchar *p = NULL;
4011         FolderItem *item = NULL;
4012         gchar *msg = NULL;
4013         MainWindow *mainwin = mainwindow_get_mainwindow();
4014         
4015         if (!target)
4016                 return;
4017                 
4018         if (!mainwin) {
4019                 printf(_("not initialized\n"));
4020                 return;
4021         }
4022
4023         tmp = g_strdup(target);
4024         
4025         if ((p = strstr(tmp, "\r")) != NULL)
4026                 *p = '\0';
4027         if ((p = strstr(tmp, "\n")) != NULL)
4028                 *p = '\0';
4029
4030         if ((item = folder_find_item_from_identifier(tmp))) {
4031                 printf(_("selecting folder '%s'\n"), tmp);
4032                 folderview_select(mainwin->folderview, item);
4033                 main_window_popup(mainwin);
4034                 g_free(tmp);
4035                 return;
4036         }
4037         
4038         msg = strrchr(tmp, G_DIR_SEPARATOR);
4039         if (msg) {
4040                 *msg++ = '\0';
4041                 if ((item = folder_find_item_from_identifier(tmp))) {
4042                         printf(_("selecting folder '%s'\n"), tmp);
4043                         folderview_select(mainwin->folderview, item);
4044                 } 
4045                 if (item && msg && atoi(msg)) {
4046                         printf(_("selecting message %d\n"), atoi(msg));
4047                         summary_select_by_msgnum(mainwin->summaryview, atoi(msg));
4048                         summary_display_msg_selected(mainwin->summaryview, FALSE);
4049                         main_window_popup(mainwin);
4050                         g_free(tmp);
4051                         return;
4052                 } 
4053         }
4054         
4055         printf("'%s' not found\n", tmp);
4056         
4057         g_free(tmp);
4058 }