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