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