2007-01-20 [colin] 2.7.1cvs37
[claws.git] / src / mainwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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_separation_change       (MainWindow     *mainwin,
115                                                  LayoutType      layout_mode);
116
117 static void main_window_set_widgets             (MainWindow     *mainwin,
118                                                  LayoutType      layout_mode);
119
120 static void toolbar_child_attached              (GtkWidget      *widget,
121                                                  GtkWidget      *child,
122                                                  gpointer        data);
123 static void toolbar_child_detached              (GtkWidget      *widget,
124                                                  GtkWidget      *child,
125                                                  gpointer        data);
126
127 static gboolean ac_label_button_pressed         (GtkWidget      *widget,
128                                                  GdkEventButton *event,
129                                                  gpointer        data);
130
131 static gint main_window_close_cb                (GtkWidget      *widget,
132                                                  GdkEventAny    *event,
133                                                  gpointer        data);
134
135 static void main_window_size_allocate_cb        (GtkWidget      *widget,
136                                                  GtkAllocation  *allocation,
137                                                  gpointer        data);
138 static void folder_window_size_allocate_cb      (GtkWidget      *widget,
139                                                  GtkAllocation  *allocation,
140                                                  gpointer        data);
141 static void message_window_size_allocate_cb     (GtkWidget      *widget,
142                                                  GtkAllocation  *allocation,
143                                                  gpointer        data);
144
145 static void update_folderview_cb (MainWindow    *mainwin,
146                                   guint          action,
147                                   GtkWidget     *widget);
148 static void add_mailbox_cb       (MainWindow    *mainwin,
149                                   guint          action,
150                                   GtkWidget     *widget);
151 static void foldersort_cb        (MainWindow    *mainwin,
152                                   guint          action,
153                                   GtkWidget     *widget);
154 static void import_mbox_cb       (MainWindow    *mainwin,
155                                   guint          action,
156                                   GtkWidget     *widget);
157 static void export_mbox_cb       (MainWindow    *mainwin,
158                                   guint          action,
159                                   GtkWidget     *widget);
160 static void export_list_mbox_cb  (MainWindow    *mainwin, 
161                                   guint          action,
162                                   GtkWidget     *widget);
163 static void empty_trash_cb       (MainWindow    *mainwin,
164                                   guint          action,
165                                   GtkWidget     *widget);
166
167 static void save_as_cb           (MainWindow    *mainwin,
168                                   guint          action,
169                                   GtkWidget     *widget);
170 static void print_cb             (MainWindow    *mainwin,
171                                   guint          action,
172                                   GtkWidget     *widget);
173 static void app_exit_cb          (MainWindow    *mainwin,
174                                   guint          action,
175                                   GtkWidget     *widget);
176
177 static void search_cb            (MainWindow    *mainwin,
178                                   guint          action,
179                                   GtkWidget     *widget);
180
181 static void toggle_message_cb    (MainWindow    *mainwin,
182                                   guint          action,
183                                   GtkWidget     *widget);
184 static void toggle_toolbar_cb    (MainWindow    *mainwin,
185                                   guint          action,
186                                   GtkWidget     *widget);
187 static void toggle_statusbar_cb  (MainWindow    *mainwin,
188                                   guint          action,
189                                   GtkWidget     *widget);
190 static void set_layout_cb        (MainWindow    *mainwin,
191                                   guint          action,
192                                   GtkWidget     *widget);
193
194 static void addressbook_open_cb (MainWindow     *mainwin,
195                                  guint           action,
196                                  GtkWidget      *widget);
197 static void log_window_show_cb  (MainWindow     *mainwin,
198                                  guint           action,
199                                  GtkWidget      *widget);
200
201 static void inc_cancel_cb               (MainWindow     *mainwin,
202                                          guint           action,
203                                          GtkWidget      *widget);
204
205 static void open_msg_cb                 (MainWindow     *mainwin,
206                                          guint           action,
207                                          GtkWidget      *widget);
208
209 static void view_source_cb              (MainWindow     *mainwin,
210                                          guint           action,
211                                          GtkWidget      *widget);
212
213 static void show_all_header_cb          (MainWindow     *mainwin,
214                                          guint           action,
215                                          GtkWidget      *widget);
216
217 static void hide_quotes_cb(MainWindow   *mainwin,
218                                          guint           action,
219                                          GtkWidget      *widget);
220
221 static void move_to_cb                  (MainWindow     *mainwin,
222                                          guint           action,
223                                          GtkWidget      *widget);
224 static void copy_to_cb                  (MainWindow     *mainwin,
225                                          guint           action,
226                                          GtkWidget      *widget);
227 static void delete_cb                   (MainWindow     *mainwin,
228                                          guint           action,
229                                          GtkWidget      *widget);
230 static void delete_trash_cb                     (MainWindow     *mainwin,
231                                          guint           action,
232                                          GtkWidget      *widget);
233
234 static void cancel_cb                   (MainWindow     *mainwin,
235                                          guint           action,
236                                          GtkWidget      *widget);
237
238 static void mark_cb                     (MainWindow     *mainwin,
239                                          guint           action,
240                                          GtkWidget      *widget);
241 static void unmark_cb                   (MainWindow     *mainwin,
242                                          guint           action,
243                                          GtkWidget      *widget);
244
245 static void mark_as_unread_cb           (MainWindow     *mainwin,
246                                          guint           action,
247                                          GtkWidget      *widget);
248 static void mark_as_read_cb             (MainWindow     *mainwin,
249                                          guint           action,
250                                          GtkWidget      *widget);
251 static void mark_all_read_cb            (MainWindow     *mainwin,
252                                          guint           action,
253                                          GtkWidget      *widget);
254 static void mark_as_spam_cb             (MainWindow     *mainwin, 
255                                          guint           action,
256                                          GtkWidget      *widget);
257
258 static void ignore_thread_cb            (MainWindow     *mainwin, 
259                                          guint           action,
260                                          GtkWidget      *widget);
261 static void unignore_thread_cb          (MainWindow     *mainwin, 
262                                          guint           action,
263                                          GtkWidget      *widget);
264 static void lock_msgs_cb                (MainWindow     *mainwin, 
265                                          guint           action,
266                                          GtkWidget      *widget);
267 static void unlock_msgs_cb              (MainWindow     *mainwin, 
268                                          guint           action,
269                                          GtkWidget      *widget);
270
271 static void reedit_cb                   (MainWindow     *mainwin,
272                                          guint           action,
273                                          GtkWidget      *widget);
274
275 static void add_address_cb              (MainWindow     *mainwin,
276                                          guint           action,
277                                          GtkWidget      *widget);
278
279 static void set_charset_cb              (MainWindow     *mainwin,
280                                          guint           action,
281                                          GtkWidget      *widget);
282
283 static void set_decode_cb               (MainWindow     *mainwin,
284                                          guint           action,
285                                          GtkWidget      *widget);
286
287 static void hide_read_messages   (MainWindow    *mainwin,
288                                   guint          action,
289                                   GtkWidget     *widget);
290
291 static void thread_cb            (MainWindow    *mainwin,
292                                   guint          action,
293                                   GtkWidget     *widget);
294 static void expand_threads_cb    (MainWindow    *mainwin,
295                                   guint          action,
296                                   GtkWidget     *widget);
297 static void collapse_threads_cb  (MainWindow    *mainwin,
298                                   guint          action,
299                                   GtkWidget     *widget);
300
301 static void set_summary_display_item_cb  (MainWindow    *mainwin,
302                                   guint          action,
303                                   GtkWidget     *widget);
304 static void set_folder_display_item_cb   (MainWindow    *mainwin,
305                                   guint          action,
306                                   GtkWidget     *widget);
307 static void sort_summary_cb      (MainWindow    *mainwin,
308                                   guint          action,
309                                   GtkWidget     *widget);
310 static void sort_summary_type_cb (MainWindow    *mainwin,
311                                   guint          action,
312                                   GtkWidget     *widget);
313 static void attract_by_subject_cb(MainWindow    *mainwin,
314                                   guint          action,
315                                   GtkWidget     *widget);
316
317 static void delete_duplicated_cb (MainWindow    *mainwin,
318                                   guint          action,
319                                   GtkWidget     *widget);
320 static void delete_duplicated_all_cb (MainWindow        *mainwin,
321                                   guint          action,
322                                   GtkWidget     *widget);
323 static void filter_cb            (MainWindow    *mainwin,
324                                   guint          action,
325                                   GtkWidget     *widget);
326 static void execute_summary_cb   (MainWindow    *mainwin,
327                                   guint          action,
328                                   GtkWidget     *widget);
329 static void update_summary_cb    (MainWindow    *mainwin,
330                                   guint          action,
331                                   GtkWidget     *widget);
332
333 static void prev_cb              (MainWindow    *mainwin,
334                                   guint          action,
335                                   GtkWidget     *widget);
336 static void next_cb              (MainWindow    *mainwin,
337                                   guint          action,
338                                   GtkWidget     *widget);
339 static void next_unread_cb       (MainWindow    *mainwin,
340                                   guint          action,
341                                   GtkWidget     *widget);
342 static void prev_unread_cb       (MainWindow    *mainwin,
343                                   guint          action,
344                                   GtkWidget     *widget);
345
346 static void prev_new_cb          (MainWindow    *mainwin,
347                                   guint          action,
348                                   GtkWidget     *widget);
349 static void next_new_cb          (MainWindow    *mainwin,
350                                   guint          action,
351                                   GtkWidget     *widget);
352 static void prev_marked_cb       (MainWindow    *mainwin,
353                                   guint          action,
354                                   GtkWidget     *widget);
355 static void next_marked_cb       (MainWindow    *mainwin,
356                                   guint          action,
357                                   GtkWidget     *widget);
358 static void prev_labeled_cb      (MainWindow    *mainwin,
359                                   guint          action,
360                                   GtkWidget     *widget);
361 static void next_labeled_cb      (MainWindow    *mainwin,
362                                   guint          action,
363                                   GtkWidget     *widget);
364 static void last_read_cb         (MainWindow    *mainwin,
365                                   guint          action,
366                                   GtkWidget     *widget);
367 static void parent_cb            (MainWindow    *mainwin,
368                                   guint          action,
369                                   GtkWidget     *widget);
370
371 static void goto_folder_cb       (MainWindow    *mainwin,
372                                   guint          action,
373                                   GtkWidget     *widget);
374 static void goto_unread_folder_cb(MainWindow    *mainwin,
375                                   guint          action,
376                                   GtkWidget     *widget);
377
378 static void copy_cb              (MainWindow    *mainwin,
379                                   guint          action,
380                                   GtkWidget     *widget);
381 static void allsel_cb            (MainWindow    *mainwin,
382                                   guint          action,
383                                   GtkWidget     *widget);
384 static void select_thread_cb     (MainWindow    *mainwin,
385                                   guint          action,
386                                   GtkWidget     *widget);
387 static void delete_thread_cb     (MainWindow    *mainwin,
388                                   guint          action,
389                                   GtkWidget     *widget);
390
391 static void create_filter_cb     (MainWindow    *mainwin,
392                                   guint          action,
393                                   GtkWidget     *widget);
394 static void create_processing_cb (MainWindow    *mainwin,
395                                   guint          action,
396                                   GtkWidget     *widget);
397
398 static void prefs_template_open_cb      (MainWindow     *mainwin,
399                                          guint           action,
400                                          GtkWidget      *widget);
401 static void prefs_actions_open_cb       (MainWindow     *mainwin,
402                                          guint           action,
403                                          GtkWidget      *widget);
404 static void prefs_account_open_cb       (MainWindow     *mainwin,
405                                          guint           action,
406                                          GtkWidget      *widget);
407
408 static void prefs_pre_processing_open_cb  (MainWindow   *mainwin,
409                                            guint         action,
410                                            GtkWidget    *widget);
411
412 static void prefs_post_processing_open_cb (MainWindow   *mainwin,
413                                            guint         action,
414                                            GtkWidget    *widget);
415
416 static void prefs_filtering_open_cb     (MainWindow     *mainwin,
417                                          guint           action,
418                                          GtkWidget      *widget);
419 #ifdef USE_OPENSSL
420 static void ssl_manager_open_cb         (MainWindow     *mainwin,
421                                          guint           action,
422                                          GtkWidget      *widget);
423 #endif
424 static void new_account_cb       (MainWindow    *mainwin,
425                                   guint          action,
426                                   GtkWidget     *widget);
427
428 static void account_selector_menu_cb     (GtkMenuItem   *menuitem,
429                                           gpointer       data);
430 static void account_receive_menu_cb      (GtkMenuItem   *menuitem,
431                                           gpointer       data);
432 static void account_compose_menu_cb      (GtkMenuItem   *menuitem,
433                                           gpointer       data);
434
435 static void prefs_open_cb       (GtkMenuItem    *menuitem,
436                                  gpointer        data);
437 static void plugins_open_cb     (GtkMenuItem    *menuitem,
438                                  gpointer        data);
439
440 static void online_switch_clicked(GtkButton     *btn, 
441                                   gpointer data);
442
443 static void manual_open_cb       (MainWindow    *mainwin,
444                                   guint          action,
445                                   GtkWidget     *widget);
446
447 static void legend_open_cb       (GtkMenuItem   *menuitem,
448                                   gpointer       data);
449
450 static void scan_tree_func       (Folder        *folder,
451                                   FolderItem    *item,
452                                   gpointer       data);
453                                   
454 static void toggle_work_offline_cb(MainWindow *mainwin, guint action, GtkWidget *widget);
455
456 static void addr_harvest_cb      ( MainWindow  *mainwin,
457                                    guint       action,
458                                    GtkWidget   *widget );
459
460 static void addr_harvest_msg_cb  ( MainWindow  *mainwin,
461                                    guint       action,
462                                    GtkWidget   *widget );
463 static void sync_cb              ( MainWindow *mainwin, 
464                                    guint action, 
465                                    GtkWidget *widget );
466
467 static gboolean mainwindow_focus_in_event       (GtkWidget      *widget, 
468                                                  GdkEventFocus  *focus,
469                                                  gpointer        data);
470 void main_window_reply_cb                       (MainWindow     *mainwin, 
471                                                  guint           action,
472                                                  GtkWidget      *widget);
473 gboolean mainwindow_progressindicator_hook      (gpointer        source,
474                                                  gpointer        userdata);
475
476 static gint mailing_list_create_submenu(GtkItemFactory *ifactory,
477                                        MsgInfo *msginfo);
478
479 static gint mailing_list_populate_submenu(GtkWidget *menu, const gchar * list_header);
480         
481 static void get_url_part(const gchar **buf, gchar *url_decoded, gint maxlen);
482
483 static void mailing_list_compose(GtkWidget *w, gpointer *data);
484  
485 static void mailing_list_open_uri(GtkWidget *w, gpointer *data);
486 #define  SEPARATE_ACTION 500 
487 static void mainwindow_quicksearch              (MainWindow     *mainwin, 
488                                                  guint           action, 
489                                                  GtkWidget      *widget);
490 static gboolean any_folder_want_synchronise(void);
491
492 static GtkItemFactoryEntry mainwin_entries[] =
493 {
494         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
495         {N_("/_File/_Add mailbox"),             NULL, NULL, 0, "<Branch>"},
496         {N_("/_File/_Add mailbox/MH..."),       NULL, add_mailbox_cb, 0, NULL},
497         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
498         {N_("/_File/Change folder order..."),   NULL, foldersort_cb,  0, NULL},
499         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
500         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
501         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
502         {N_("/_File/Exp_ort selected to mbox file..."), 
503                                                 NULL, export_list_mbox_cb, 0, NULL},
504         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
505         {N_("/_File/Empty all _Trash folders"), "<shift>D", empty_trash_cb, 0, NULL},
506         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
507         {N_("/_File/_Save as..."),              "<control>S", save_as_cb, 0, NULL},
508         {N_("/_File/_Print..."),                "<control>P", print_cb, 0, NULL},
509         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
510         {N_("/_File/_Work offline"),            "<control>W", toggle_work_offline_cb, 0, "<ToggleItem>"},
511         {N_("/_File/Synchronise folders"),      "<control><shift>S", sync_cb, 0, NULL},
512         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
513         /* {N_("/_File/_Close"),                "<alt>W", app_exit_cb, 0, NULL}, */
514         {N_("/_File/E_xit"),                    "<control>Q", app_exit_cb, 0, NULL},
515
516         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
517         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
518         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
519         {N_("/_Edit/Select _thread"),           NULL, select_thread_cb, 0, NULL},
520         {N_("/_Edit/_Delete thread"),           NULL, delete_thread_cb, 0, NULL},
521         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
522         {N_("/_Edit/_Find in current message..."),
523                                                 "<control>F", search_cb, 0, NULL},
524         {N_("/_Edit/_Search folder..."),        "<shift><control>F", search_cb, 1, NULL},
525         {N_("/_Edit/_Quick search"),            "slash", mainwindow_quicksearch, 0, NULL},
526         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
527         {N_("/_View/Show or hi_de"),            NULL, NULL, 0, "<Branch>"},
528         {N_("/_View/Show or hi_de/_Message view"),
529                                                 "V", toggle_message_cb, 0, "<ToggleItem>"},
530         {N_("/_View/Show or hi_de/_Toolbar"),
531                                                 NULL, NULL, 0, "<Branch>"},
532         {N_("/_View/Show or hi_de/_Toolbar/Text _below icons"),
533                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
534         {N_("/_View/Show or hi_de/_Toolbar/Text be_side icons"),
535                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH_HORIZ, "/View/Show or hide/Toolbar/Text below icons"},
536         {N_("/_View/Show or hi_de/_Toolbar/_Icons only"),
537                                                 NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Show or hide/Toolbar/Text below icons"},
538         {N_("/_View/Show or hi_de/_Toolbar/_Text only"),
539                                                 NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Show or hide/Toolbar/Text below icons"},
540         {N_("/_View/Show or hi_de/_Toolbar/_Hide"),
541                                                 NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Show or hide/Toolbar/Text below icons"},
542         {N_("/_View/Show or hi_de/Status _bar"),
543                                                 NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
544         {N_("/_View/Set displayed _columns"),   NULL, NULL, 0, "<Branch>"},
545         {N_("/_View/Set displayed _columns/in _Folder list..."),        NULL, set_folder_display_item_cb, 0, NULL},
546         {N_("/_View/Set displayed _columns/in _Message list..."),NULL, set_summary_display_item_cb, 0, NULL},
547
548         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
549         {N_("/_View/La_yout"),                  NULL, NULL, 0, "<Branch>"},
550         {N_("/_View/Layout/_Standard"),         NULL, set_layout_cb, NORMAL_LAYOUT, "<RadioItem>"},
551         {N_("/_View/Layout/_Three columns"),    NULL, set_layout_cb, VERTICAL_LAYOUT, "/View/Layout/Standard"},
552         {N_("/_View/Layout/_Wide message"),     NULL, set_layout_cb, WIDE_LAYOUT, "/View/Layout/Standard"},
553         {N_("/_View/Layout/W_ide message list"),NULL, set_layout_cb, WIDE_MSGLIST_LAYOUT, "/View/Layout/Standard"},
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()
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, prefs_common.layout_mode);
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         if (prefs_common.layout_mode == VERTICAL_LAYOUT)
1427                 summary_relayout(mainwin->summaryview); 
1428
1429         gtk_widget_show(mainwin->window);
1430
1431         /* initialize views */
1432         folderview_init(folderview);
1433         summary_init(summaryview);
1434         messageview_init(messageview);
1435 #ifdef USE_OPENSSL
1436         sslcertwindow_register_hook();
1437 #endif
1438         mainwin->lock_count = 0;
1439         mainwin->menu_lock_count = 0;
1440         mainwin->cursor_count = 0;
1441
1442         mainwin->progressindicator_hook =
1443                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1444
1445         if (!watch_cursor)
1446                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1447         if (!hand_cursor)
1448                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1449
1450         mainwin_list = g_list_append(mainwin_list, mainwin);
1451
1452         /* init work_offline */
1453         if (prefs_common.work_offline)
1454                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1455
1456         mainwindow_colorlabel_menu_create(mainwin, FALSE);
1457         
1458         return mainwin;
1459 }
1460
1461 void main_window_destroy(MainWindow *mainwin)
1462 {
1463         /* TODO : destroy other component */
1464         messageview_destroy(mainwin->messageview);
1465         mainwin->messageview = NULL;    
1466 }
1467
1468 void main_window_update_actions_menu(MainWindow *mainwin)
1469 {
1470         GtkItemFactory *ifactory;
1471
1472         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1473         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1474 }
1475
1476 void main_window_cursor_wait(MainWindow *mainwin)
1477 {
1478
1479         if (mainwin->cursor_count == 0) {
1480                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1481                 textview_cursor_wait(mainwin->messageview->mimeview->textview);
1482         }
1483         
1484         mainwin->cursor_count++;
1485
1486         gdk_flush();
1487 }
1488
1489 void main_window_cursor_normal(MainWindow *mainwin)
1490 {
1491         if (mainwin->cursor_count)
1492                 mainwin->cursor_count--;
1493
1494         if (mainwin->cursor_count == 0) {
1495                 gdk_window_set_cursor(mainwin->window->window, NULL);
1496                 textview_cursor_normal(mainwin->messageview->mimeview->textview);
1497         }
1498         gdk_flush();
1499 }
1500
1501 /* lock / unlock the user-interface */
1502 void main_window_lock(MainWindow *mainwin)
1503 {
1504         if (mainwin->lock_count == 0)
1505                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1506
1507         mainwin->lock_count++;
1508
1509         main_window_set_menu_sensitive(mainwin);
1510         toolbar_main_set_sensitive(mainwin);
1511 }
1512
1513 void main_window_unlock(MainWindow *mainwin)
1514 {
1515         if (mainwin->lock_count)
1516                 mainwin->lock_count--;
1517
1518         main_window_set_menu_sensitive(mainwin);
1519         toolbar_main_set_sensitive(mainwin);
1520
1521         if (mainwin->lock_count == 0)
1522                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
1523 }
1524
1525 static void main_window_menu_callback_block(MainWindow *mainwin)
1526 {
1527         mainwin->menu_lock_count++;
1528 }
1529
1530 static void main_window_menu_callback_unblock(MainWindow *mainwin)
1531 {
1532         if (mainwin->menu_lock_count)
1533                 mainwin->menu_lock_count--;
1534 }
1535
1536 static guint prefs_tag = 0;
1537
1538 void main_window_reflect_prefs_all(void)
1539 {
1540         main_window_reflect_prefs_all_real(FALSE);
1541 }
1542
1543 static gboolean reflect_prefs_timeout_cb(gpointer data) 
1544 {
1545         gboolean pixmap_theme_changed = GPOINTER_TO_INT(data);
1546         GList *cur;
1547         MainWindow *mainwin;
1548         GtkWidget *pixmap;
1549
1550         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1551                 mainwin = (MainWindow *)cur->data;
1552
1553                 main_window_show_cur_account(mainwin);
1554                 main_window_set_menu_sensitive(mainwin);
1555                 toolbar_main_set_sensitive(mainwin);
1556
1557                 /* pixmap themes */
1558                 if (pixmap_theme_changed) {
1559                         toolbar_update(TOOLBAR_MAIN, mainwin);
1560                         messageview_reflect_prefs_pixmap_theme();
1561                         compose_reflect_prefs_pixmap_theme();
1562                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1563                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1564
1565                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1566                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1567                                              mainwin->online_pixmap);
1568                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1569                         gtk_widget_show(pixmap);
1570                         mainwin->online_pixmap = pixmap;
1571                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1572                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1573                                              mainwin->offline_pixmap);
1574                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1575                         gtk_widget_show(pixmap);
1576                         mainwin->offline_pixmap = pixmap;
1577                 }
1578                 
1579                 headerview_set_font(mainwin->messageview->headerview);
1580                 headerview_set_visibility(mainwin->messageview->headerview,
1581                                           prefs_common.display_header_pane);
1582                 textview_reflect_prefs(mainwin->messageview->mimeview->textview);
1583                 folderview_reflect_prefs();
1584                 summary_reflect_prefs();
1585                 summary_redisplay_msg(mainwin->summaryview);
1586         }
1587         prefs_tag = 0;
1588         return FALSE;
1589 }
1590
1591 void main_window_reflect_prefs_all_now(void)
1592 {
1593         reflect_prefs_timeout_cb(GINT_TO_POINTER(FALSE));
1594 }
1595
1596 void main_window_reflect_prefs_custom_colors(MainWindow *mainwin)
1597 {
1598         GtkMenuShell *menu;
1599         GList *cur;
1600
1601         /* re-create colorlabel submenu */
1602         menu = GTK_MENU_SHELL(mainwin->colorlabel_menu);
1603         g_return_if_fail(menu != NULL);
1604
1605         /* clear items. get item pointers. */
1606         for (cur = menu->children; cur != NULL && cur->data != NULL; cur = cur->next) {
1607                 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(cur->data));
1608         }
1609         mainwindow_colorlabel_menu_create(mainwin, TRUE);
1610         summary_reflect_prefs_custom_colors(mainwin->summaryview);
1611
1612 }
1613
1614 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1615 {
1616         if (prefs_tag == 0 || pixmap_theme_changed) {
1617                 prefs_tag = g_timeout_add(500, reflect_prefs_timeout_cb, 
1618                                                 GINT_TO_POINTER(pixmap_theme_changed));
1619         }
1620 }
1621
1622 void main_window_set_summary_column(void)
1623 {
1624         GList *cur;
1625         MainWindow *mainwin;
1626
1627         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1628                 mainwin = (MainWindow *)cur->data;
1629                 summary_set_column_order(mainwin->summaryview);
1630         }
1631 }
1632
1633 void main_window_set_folder_column(void)
1634 {
1635         GList *cur;
1636         MainWindow *mainwin;
1637
1638         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1639                 mainwin = (MainWindow *)cur->data;
1640                 folderview_set_column_order(mainwin->folderview);
1641         }
1642 }
1643
1644 static void main_window_set_account_selector_menu(MainWindow *mainwin,
1645                                                   GList *account_list)
1646 {
1647         GList *cur_ac, *cur_item;
1648         GtkWidget *menuitem;
1649         PrefsAccount *ac_prefs;
1650
1651         /* destroy all previous menu item */
1652         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
1653         while (cur_item != NULL) {
1654                 GList *next = cur_item->next;
1655                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1656                 cur_item = next;
1657         }
1658
1659         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1660                 ac_prefs = (PrefsAccount *)cur_ac->data;
1661
1662                 menuitem = gtk_menu_item_new_with_label
1663                         (ac_prefs->account_name
1664                          ? ac_prefs->account_name : _("Untitled"));
1665                 gtk_widget_show(menuitem);
1666                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
1667                 g_signal_connect(G_OBJECT(menuitem), "activate",
1668                                  G_CALLBACK(account_selector_menu_cb),
1669                                  ac_prefs);
1670         }
1671 }
1672
1673 static void main_window_set_account_receive_menu(MainWindow *mainwin,
1674                                                  GList *account_list)
1675 {
1676         GList *cur_ac, *cur_item;
1677         GtkWidget *menu;
1678         GtkWidget *menuitem;
1679         PrefsAccount *ac_prefs;
1680
1681         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
1682                                            "/Message/Receive");
1683
1684         /* search for separator */
1685         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1686              cur_item = cur_item->next) {
1687                 if (GTK_BIN(cur_item->data)->child == NULL) {
1688                         cur_item = cur_item->next;
1689                         break;
1690                 }
1691         }
1692
1693         /* destroy all previous menu item */
1694         while (cur_item != NULL) {
1695                 GList *next = cur_item->next;
1696                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1697                 cur_item = next;
1698         }
1699
1700         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1701                 ac_prefs = (PrefsAccount *)cur_ac->data;
1702
1703                 menuitem = gtk_menu_item_new_with_label
1704                         (ac_prefs->account_name ? ac_prefs->account_name
1705                          : _("Untitled"));
1706                 gtk_widget_show(menuitem);
1707                 gtk_menu_append(GTK_MENU(menu), menuitem);
1708                 g_signal_connect(G_OBJECT(menuitem), "activate",
1709                                  G_CALLBACK(account_receive_menu_cb),
1710                                  ac_prefs);
1711         }
1712 }
1713
1714 static void main_window_set_toolbar_combo_receive_menu(MainWindow *mainwin,
1715                                                        GList *account_list)
1716 {
1717         GList *cur_ac, *cur_item;
1718         GtkWidget *menuitem;
1719         PrefsAccount *ac_prefs;
1720         GtkWidget *menu = NULL;
1721
1722         if (mainwin->toolbar->getall_btn == NULL
1723         ||  mainwin->toolbar->getall_combo == NULL) /* button doesn't exist */
1724                 return;
1725
1726         menu = mainwin->toolbar->getall_combo->menu;
1727
1728         /* destroy all previous menu item */
1729         cur_item = GTK_MENU_SHELL(menu)->children;
1730         while (cur_item != NULL) {
1731                 GList *next = cur_item->next;
1732                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1733                 cur_item = next;
1734         }
1735
1736         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1737                 ac_prefs = (PrefsAccount *)cur_ac->data;
1738
1739                 menuitem = gtk_menu_item_new_with_label
1740                         (ac_prefs->account_name
1741                          ? ac_prefs->account_name : _("Untitled"));
1742                 gtk_widget_show(menuitem);
1743                 gtk_menu_append(GTK_MENU(menu), menuitem);
1744                 g_signal_connect(G_OBJECT(menuitem), "activate",
1745                                  G_CALLBACK(account_receive_menu_cb),
1746                                  ac_prefs);
1747         }
1748 }
1749
1750 static void main_window_set_toolbar_combo_compose_menu(MainWindow *mainwin,
1751                                                        GList *account_list)
1752 {
1753         GList *cur_ac, *cur_item;
1754         GtkWidget *menuitem;
1755         PrefsAccount *ac_prefs;
1756         GtkWidget *menu = NULL;
1757
1758         if (mainwin->toolbar->compose_mail_btn == NULL
1759         ||  mainwin->toolbar->compose_combo == NULL) /* button doesn't exist */
1760                 return;
1761
1762         menu = mainwin->toolbar->compose_combo->menu;
1763
1764         /* destroy all previous menu item */
1765         cur_item = GTK_MENU_SHELL(menu)->children;
1766         while (cur_item != NULL) {
1767                 GList *next = cur_item->next;
1768                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1769                 cur_item = next;
1770         }
1771
1772         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1773                 ac_prefs = (PrefsAccount *)cur_ac->data;
1774
1775                 menuitem = gtk_menu_item_new_with_label
1776                         (ac_prefs->account_name
1777                          ? ac_prefs->account_name : _("Untitled"));
1778                 gtk_widget_show(menuitem);
1779                 gtk_menu_append(GTK_MENU(menu), menuitem);
1780                 g_signal_connect(G_OBJECT(menuitem), "activate",
1781                                  G_CALLBACK(account_compose_menu_cb),
1782                                  ac_prefs);
1783         }
1784 }
1785
1786 void main_window_set_account_menu(GList *account_list)
1787 {
1788         GList *cur;
1789         MainWindow *mainwin;
1790
1791         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1792                 mainwin = (MainWindow *)cur->data;
1793                 main_window_set_account_selector_menu(mainwin, account_list);
1794                 main_window_set_account_receive_menu(mainwin, account_list);
1795                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
1796                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
1797         }
1798         hooks_invoke(ACCOUNT_LIST_CHANGED_HOOKLIST, NULL);
1799 }
1800
1801 void main_window_set_account_menu_only_toolbar(GList *account_list)
1802 {
1803         GList *cur;
1804         MainWindow *mainwin;
1805
1806         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1807                 mainwin = (MainWindow *)cur->data;
1808                 main_window_set_toolbar_combo_receive_menu(mainwin, account_list);
1809                 main_window_set_toolbar_combo_compose_menu(mainwin, account_list);
1810         }
1811 }
1812
1813 static void main_window_show_cur_account(MainWindow *mainwin)
1814 {
1815         gchar *buf;
1816         gchar *ac_name;
1817
1818         ac_name = g_strdup(cur_account
1819                            ? (cur_account->account_name
1820                               ? cur_account->account_name : _("Untitled"))
1821                            : _("none"));
1822
1823         if (cur_account)
1824                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
1825         else
1826                 buf = g_strdup(PROG_VERSION);
1827         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
1828         g_free(buf);
1829
1830         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
1831         gtk_widget_queue_resize(mainwin->ac_button);
1832
1833         g_free(ac_name);
1834 }
1835
1836 static void main_window_separation_change(MainWindow *mainwin, LayoutType layout_mode)
1837 {
1838         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
1839         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
1840         GtkWidget *message_wid = mainwin->messageview->vbox;
1841
1842         if (layout_mode == prefs_common.layout_mode) 
1843                 return;
1844
1845         debug_print("Changing window separation type from %d to %d\n",
1846                     prefs_common.layout_mode, layout_mode);
1847
1848         /* remove widgets from those containers */
1849         gtk_widget_ref(folder_wid);
1850         gtk_widget_ref(summary_wid);
1851         gtk_widget_ref(message_wid);
1852         gtkut_container_remove
1853                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1854         gtkut_container_remove
1855                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1856         gtkut_container_remove
1857                 (GTK_CONTAINER(message_wid->parent), message_wid);
1858
1859         gtk_widget_hide(mainwin->window);
1860         main_window_set_widgets(mainwin, layout_mode);
1861         gtk_widget_show(mainwin->window);
1862
1863         gtk_widget_unref(folder_wid);
1864         gtk_widget_unref(summary_wid);
1865         gtk_widget_unref(message_wid);
1866 }
1867
1868 static void mainwin_reset_paned(GtkPaned *paned)
1869 {
1870                 gint min, max, mid;
1871                 g_object_get (G_OBJECT(paned),
1872                                 "min-position",
1873                                 &min, NULL);
1874                 g_object_get (G_OBJECT(paned),
1875                                 "max-position",
1876                                 &max, NULL);
1877                 mid = (min+max)/2;
1878
1879                 gtk_paned_set_position(GTK_PANED(paned), mid);
1880 }
1881
1882 void main_window_toggle_message_view(MainWindow *mainwin)
1883 {
1884         SummaryView *summaryview = mainwin->summaryview;
1885         GtkWidget *ppaned = NULL;
1886         GtkWidget *container = NULL;
1887         
1888         switch (prefs_common.layout_mode) {
1889         case NORMAL_LAYOUT:
1890         case VERTICAL_LAYOUT:
1891                 ppaned = mainwin->vpaned;
1892                 container = mainwin->hpaned;
1893                 if (ppaned->parent != NULL) {
1894                         mainwin->messageview->visible = FALSE;
1895                         summaryview->displayed = NULL;
1896                         gtk_widget_ref(ppaned);
1897                         gtkut_container_remove(GTK_CONTAINER(container), ppaned);
1898                         gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1899                 } else {
1900                         mainwin->messageview->visible = TRUE;
1901                         gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), ppaned);
1902                         gtk_container_add(GTK_CONTAINER(container), ppaned);
1903                         gtk_widget_unref(ppaned);
1904                 }
1905                 break;
1906         case WIDE_LAYOUT:
1907                 ppaned = mainwin->hpaned;
1908                 container = mainwin->vpaned;
1909                 if (mainwin->messageview->vbox->parent != NULL) {
1910                         mainwin->messageview->visible = FALSE;
1911                         summaryview->displayed = NULL;
1912                         gtk_widget_ref(mainwin->messageview->vbox);
1913                         gtkut_container_remove(GTK_CONTAINER(container), mainwin->messageview->vbox);
1914                 } else {
1915                         mainwin->messageview->visible = TRUE;
1916                         gtk_container_add(GTK_CONTAINER(container), mainwin->messageview->vbox);
1917                         gtk_widget_unref(mainwin->messageview->vbox);
1918                 }
1919                 break;
1920         case WIDE_MSGLIST_LAYOUT:
1921                 g_warning("can't hide messageview in this wide msglist layout");
1922                 break;
1923         }
1924
1925         if (messageview_is_visible(mainwin->messageview))
1926                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1927                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1928         else
1929                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1930                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1931
1932         if (mainwin->messageview->visible == FALSE)
1933                 messageview_clear(mainwin->messageview);
1934
1935         main_window_set_menu_sensitive(mainwin);
1936
1937         prefs_common.msgview_visible = mainwin->messageview->visible;
1938
1939         if (messageview_is_visible(mainwin->messageview)) {
1940                 gtk_widget_queue_resize(mainwin->hpaned);
1941                 gtk_widget_queue_resize(mainwin->vpaned);
1942         }
1943         summary_grab_focus(summaryview);
1944 }
1945
1946 void main_window_get_size(MainWindow *mainwin)
1947 {
1948         GtkAllocation *allocation;
1949
1950         if (mainwin_list == NULL || mainwin->messageview == NULL) {
1951                 debug_print("called after messageview "
1952                             "has been deallocated!\n");
1953                 return;
1954         }
1955
1956         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1957
1958         if (allocation->width > 1 && allocation->height > 1) {
1959                 prefs_common.summaryview_width = allocation->width;
1960
1961                 if (messageview_is_visible(mainwin->messageview))
1962                         prefs_common.summaryview_height = allocation->height;
1963
1964                 prefs_common.mainview_width = allocation->width;
1965         }
1966
1967         allocation = &mainwin->window->allocation;
1968         if (allocation->width > 1 && allocation->height > 1) {
1969                 prefs_common.mainview_height = allocation->height;
1970                 prefs_common.mainwin_width   = allocation->width;
1971                 prefs_common.mainwin_height  = allocation->height;
1972         }
1973
1974         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1975         if (allocation->width > 1 && allocation->height > 1) {
1976                 prefs_common.folderview_width  = allocation->width;
1977                 prefs_common.folderview_height = allocation->height;
1978         }
1979
1980         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
1981         if (allocation->width > 1 && allocation->height > 1) {
1982                 prefs_common.msgview_width = allocation->width;
1983                 prefs_common.msgview_height = allocation->height;
1984         }
1985
1986 /*      debug_print("summaryview size: %d x %d\n",
1987                     prefs_common.summaryview_width,
1988                     prefs_common.summaryview_height);
1989         debug_print("folderview size: %d x %d\n",
1990                     prefs_common.folderview_width,
1991                     prefs_common.folderview_height);
1992         debug_print("messageview size: %d x %d\n",
1993                     prefs_common.msgview_width,
1994                     prefs_common.msgview_height); */
1995 }
1996
1997 void main_window_get_position(MainWindow *mainwin)
1998 {
1999         gint x, y;
2000
2001         gtkut_widget_get_uposition(mainwin->window, &x, &y);
2002
2003         prefs_common.mainview_x = x;
2004         prefs_common.mainview_y = y;
2005         prefs_common.mainwin_x = x;
2006         prefs_common.mainwin_y = y;
2007
2008         debug_print("main window position: %d, %d\n", x, y);
2009 }
2010
2011 void main_window_progress_on(MainWindow *mainwin)
2012 {
2013         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2014 }
2015
2016 void main_window_progress_off(MainWindow *mainwin)
2017 {
2018         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
2019         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
2020 }
2021
2022 void main_window_progress_set(MainWindow *mainwin, gint cur, gint total)
2023 {
2024         gchar buf[32];
2025
2026         g_snprintf(buf, sizeof(buf), "%d / %d", cur, total);
2027         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), buf);
2028         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar),
2029                                 (total == 0) ? 0 : (gfloat)cur / (gfloat)total);
2030 }
2031
2032 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
2033 {
2034         if (confirm) {
2035                 if (alertpanel(_("Empty trash"),
2036                                _("Delete all messages in trash folders?"),
2037                                GTK_STOCK_CANCEL, _("+_Empty trash"), NULL)
2038                     != G_ALERTALTERNATE)
2039                         return;
2040                 manage_window_focus_in(mainwin->window, NULL, NULL);
2041         }
2042
2043         procmsg_empty_all_trash();
2044
2045         if (mainwin->summaryview->folder_item &&
2046             mainwin->summaryview->folder_item->stype == F_TRASH)
2047                 gtk_widget_grab_focus(mainwin->folderview->ctree);
2048 }
2049
2050 void main_window_add_mailbox(MainWindow *mainwin)
2051 {
2052         gchar *path;
2053         Folder *folder;
2054
2055         path = input_dialog(_("Add mailbox"),
2056                             _("Input the location of mailbox.\n"
2057                               "If the existing mailbox is specified, it will be\n"
2058                               "scanned automatically."),
2059                             "Mail");
2060         if (!path) return;
2061         if (folder_find_from_path(path)) {
2062                 alertpanel_error(_("The mailbox '%s' already exists."), path);
2063                 g_free(path);
2064                 return;
2065         }
2066         folder = folder_new(folder_get_class_from_string("mh"), 
2067                             !strcmp(path, "Mail") ? _("Mailbox") : 
2068                             g_path_get_basename(path), path);
2069         g_free(path);
2070
2071         if (folder->klass->create_tree(folder) < 0) {
2072                 alertpanel_error(_("Creation of the mailbox failed.\n"
2073                                    "Maybe some files already exist, or you don't have the permission to write there."));
2074                 folder_destroy(folder);
2075                 return;
2076         }
2077
2078         folder_add(folder);
2079         folder_set_ui_func(folder, scan_tree_func, mainwin);
2080         folder_scan_tree(folder, TRUE);
2081         folder_set_ui_func(folder, NULL, NULL);
2082 }
2083
2084 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
2085 {
2086         SensitiveCond state = 0;
2087         SummarySelection selection;
2088         FolderItem *item = mainwin->summaryview->folder_item;
2089         GList *account_list = account_get_list();
2090         
2091         selection = summary_get_selection_type(mainwin->summaryview);
2092
2093         if (mainwin->lock_count == 0)
2094                 state |= M_UNLOCKED;
2095         if (selection != SUMMARY_NONE)
2096                 state |= M_MSG_EXIST;
2097         if (item && item->path && folder_item_parent(item) && !item->no_select) {
2098                 state |= M_EXEC;
2099                 /*              if (item->folder->type != F_NEWS) */
2100                 state |= M_ALLOW_DELETE;
2101
2102                 if (prefs_common.immediate_exec == 0
2103                     && mainwin->lock_count == 0)
2104                         state |= M_DELAY_EXEC;
2105
2106                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
2107                     || selection != SUMMARY_NONE)
2108                         state |= M_HIDE_READ_MSG;
2109         }               
2110         if (mainwin->summaryview->threaded)
2111                 state |= M_THREADED;
2112         else
2113                 state |= M_UNTHREADED;  
2114         if (selection == SUMMARY_SELECTED_SINGLE ||
2115             selection == SUMMARY_SELECTED_MULTIPLE)
2116                 state |= M_TARGET_EXIST;
2117         if (selection == SUMMARY_SELECTED_SINGLE)
2118                 state |= M_SINGLE_TARGET_EXIST;
2119         if (mainwin->summaryview->folder_item &&
2120             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
2121                 state |= M_NEWS;
2122         else
2123                 state |= M_NOT_NEWS;
2124         if (prefs_common.actions_list && g_slist_length(prefs_common.actions_list))
2125                 state |= M_ACTIONS_EXIST;
2126
2127         if (procmsg_have_queued_mails_fast() && !procmsg_is_sending())
2128                 state |= M_HAVE_QUEUED_MAILS;
2129
2130         if (selection == SUMMARY_SELECTED_SINGLE &&
2131             (item &&
2132              (folder_has_parent_of_type(item, F_DRAFT) ||
2133               folder_has_parent_of_type(item, F_OUTBOX) ||
2134               folder_has_parent_of_type(item, F_QUEUE))))
2135                 state |= M_ALLOW_REEDIT;
2136         if (cur_account)
2137                 state |= M_HAVE_ACCOUNT;
2138         
2139         if (any_folder_want_synchronise())
2140                 state |= M_WANT_SYNC;
2141
2142         for ( ; account_list != NULL; account_list = account_list->next) {
2143                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
2144                         state |= M_HAVE_NEWS_ACCOUNT;
2145                         break;
2146                 }
2147         }
2148         
2149         if (procmsg_spam_can_learn() &&
2150             (mainwin->summaryview->folder_item &&
2151              mainwin->summaryview->folder_item->folder->klass->type != F_UNKNOWN &&
2152              mainwin->summaryview->folder_item->folder->klass->type != F_NEWS)) {
2153                 state |= M_CAN_LEARN_SPAM;
2154         }
2155
2156         if (inc_is_active())
2157                 state |= M_INC_ACTIVE;
2158
2159         if (mainwin->summaryview->deleted > 0 ||
2160             mainwin->summaryview->moved > 0 ||
2161             mainwin->summaryview->copied > 0)
2162                 state |= M_DELAY_EXEC;
2163
2164         return state;
2165 }
2166
2167
2168
2169 void main_window_set_menu_sensitive(MainWindow *mainwin)
2170 {
2171         GtkItemFactory *ifactory = mainwin->menu_factory;
2172         SensitiveCond state;
2173         gboolean sensitive;
2174         GtkWidget *menu;
2175         GtkWidget *menuitem;
2176         SummaryView *summaryview;
2177         gchar *menu_path;
2178         gint i;
2179         GList *cur_item;
2180
2181         static const struct {
2182                 gchar *const entry;
2183                 SensitiveCond cond;
2184         } entry[] = {
2185                 {"/File/Save as...", M_TARGET_EXIST},
2186                 {"/File/Print..."  , M_TARGET_EXIST},
2187                 {"/File/Synchronise folders", M_WANT_SYNC},
2188                 {"/File/Exit"      , M_UNLOCKED},
2189
2190                 {"/Edit/Select thread"             , M_SINGLE_TARGET_EXIST},
2191                 {"/Edit/Delete thread"             , M_SINGLE_TARGET_EXIST},
2192                 {"/Edit/Find in current message...", M_SINGLE_TARGET_EXIST},
2193
2194                 {"/View/Set displayed columns/in Folder list..."
2195                                                    , M_UNLOCKED}, 
2196                 {"/View/Sort"                      , M_EXEC},
2197                 {"/View/Thread view"               , M_EXEC},
2198                 {"/View/Expand all threads"        , M_MSG_EXIST},
2199                 {"/View/Collapse all threads"      , M_MSG_EXIST},
2200                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
2201                 {"/View/Go to/Previous message"        , M_MSG_EXIST},
2202                 {"/View/Go to/Next message"        , M_MSG_EXIST},
2203                 {"/View/Go to/Previous unread message" , M_MSG_EXIST},
2204                 {"/View/Go to/Previous new message"    , M_MSG_EXIST},
2205                 {"/View/Go to/Previous marked message" , M_MSG_EXIST},
2206                 {"/View/Go to/Previous labeled message", M_MSG_EXIST},
2207                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
2208                 {"/View/Go to/Last read message"   , M_SINGLE_TARGET_EXIST},
2209                 {"/View/Go to/Parent message"      , M_SINGLE_TARGET_EXIST},
2210                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
2211                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
2212                 {"/View/All headers"               , M_SINGLE_TARGET_EXIST},
2213                 {"/View/Quotes"                    , M_SINGLE_TARGET_EXIST},
2214
2215                 {"/Message/Receive/Get from current account"
2216                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2217                 {"/Message/Receive/Get from all accounts"
2218                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
2219                 {"/Message/Receive/Cancel receiving"
2220                                                  , M_INC_ACTIVE},
2221                 {"/Message/Send queued messages"  , M_HAVE_ACCOUNT|M_HAVE_QUEUED_MAILS},
2222                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
2223                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2224                 {"/Message/Reply to"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2225                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_TARGET_EXIST|M_NEWS},
2226                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2227                 {"/Message/Forward as attachment" , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2228                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_TARGET_EXIST},
2229                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE},
2230                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC},
2231                 {"/Message/Move to trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_NOT_NEWS},
2232                 {"/Message/Delete..."             , M_TARGET_EXIST|M_ALLOW_DELETE},
2233                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_NEWS},
2234                 {"/Message/Mark"                  , M_TARGET_EXIST},
2235                 {"/Message/Mark/Mark as spam"     , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2236                 {"/Message/Mark/Mark as ham"      , M_TARGET_EXIST|M_CAN_LEARN_SPAM},
2237                 {"/Message/Mark/Ignore thread"    , M_TARGET_EXIST},
2238                 {"/Message/Mark/Unignore thread"  , M_TARGET_EXIST},
2239                 {"/Message/Mark/Lock"             , M_TARGET_EXIST},
2240                 {"/Message/Mark/Unlock"           , M_TARGET_EXIST},
2241                 {"/Message/Color label"           , M_TARGET_EXIST},
2242                 {"/Message/Re-edit"               , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
2243
2244                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
2245                 {"/Tools/Harvest addresses"            , M_MSG_EXIST},
2246                 {"/Tools/Harvest addresses/from Messages..."
2247                                                        , M_TARGET_EXIST},
2248                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC},
2249                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC},
2250                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2251                 {"/Tools/Create processing rule"       , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
2252                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_ACTIONS_EXIST},
2253                 {"/Tools/Execute"                      , M_DELAY_EXEC},
2254                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE},
2255
2256                 {"/Configuration", M_UNLOCKED},
2257
2258                 {NULL, 0}
2259         };
2260
2261         state = main_window_get_current_state(mainwin);
2262
2263         for (i = 0; entry[i].entry != NULL; i++) {
2264                 sensitive = ((entry[i].cond & state) == entry[i].cond);
2265                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
2266         }
2267
2268         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
2269
2270         /* search for separator */
2271         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
2272              cur_item = cur_item->next) {
2273                 if (GTK_BIN(cur_item->data)->child == NULL) {
2274                         cur_item = cur_item->next;
2275                         break;
2276                 }
2277         }
2278
2279         for (; cur_item != NULL; cur_item = cur_item->next) {
2280                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
2281                                          (M_UNLOCKED & state) != 0);
2282         }
2283
2284         main_window_menu_callback_block(mainwin);
2285
2286 #define SET_CHECK_MENU_ACTIVE(path, active) \
2287 { \
2288         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2289         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2290 }
2291
2292         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
2293                               messageview_is_visible(mainwin->messageview));
2294
2295         summaryview = mainwin->summaryview;
2296         menu_path = "/View/Sort/Don't sort";
2297
2298         switch (summaryview->sort_key) {
2299         case SORT_BY_NUMBER:
2300                 menu_path = "/View/Sort/by number"; break;
2301         case SORT_BY_SIZE:
2302                 menu_path = "/View/Sort/by Size"; break;
2303         case SORT_BY_DATE:
2304                 menu_path = "/View/Sort/by Date"; break;
2305         case SORT_BY_FROM:
2306                 menu_path = "/View/Sort/by From"; break;
2307         case SORT_BY_TO:
2308                 menu_path = "/View/Sort/by To"; break;
2309         case SORT_BY_SUBJECT:
2310                 menu_path = "/View/Sort/by Subject"; break;
2311         case SORT_BY_LABEL:
2312                 menu_path = "/View/Sort/by color label"; break;
2313         case SORT_BY_MARK:
2314                 menu_path = "/View/Sort/by mark"; break;
2315         case SORT_BY_STATUS:
2316                 menu_path = "/View/Sort/by status"; break;
2317         case SORT_BY_MIME:
2318                 menu_path = "/View/Sort/by attachment"; break;
2319         case SORT_BY_SCORE:
2320                 menu_path = "/View/Sort/by score"; break;
2321         case SORT_BY_LOCKED:
2322                 menu_path = "/View/Sort/by locked"; break;
2323         case SORT_BY_NONE:
2324         default:
2325                 menu_path = "/View/Sort/Don't sort"; break;
2326         }
2327         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
2328
2329         if (summaryview->sort_type == SORT_ASCENDING) {
2330                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
2331         } else {
2332                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
2333         }
2334
2335         if (summaryview->sort_key != SORT_BY_NONE) {
2336                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
2337                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
2338         } else {
2339                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
2340                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
2341         }
2342
2343         if (mainwin->messageview 
2344         &&  mainwin->messageview->mimeview
2345         &&  mainwin->messageview->mimeview->textview)
2346                 SET_CHECK_MENU_ACTIVE("/View/All headers",
2347                               mainwin->messageview->mimeview->textview->show_all_headers);
2348         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
2349         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
2350         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
2351         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
2352         if (prefs_common.hide_quotes == 1)
2353                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", TRUE);
2354         if (prefs_common.hide_quotes == 2)
2355                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", TRUE);
2356         if (prefs_common.hide_quotes == 3)
2357                 SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", TRUE);
2358
2359 #undef SET_CHECK_MENU_ACTIVE
2360
2361         main_window_menu_callback_unblock(mainwin);
2362 }
2363
2364 void main_create_mailing_list_menu (MainWindow *mainwin, MsgInfo *msginfo)
2365 {
2366         GtkItemFactory *ifactory;
2367         gint is_menu = 0;
2368         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2369         
2370         if (msginfo) 
2371                 is_menu = mailing_list_create_submenu (ifactory, msginfo);
2372         if (is_menu)
2373                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2374                                 (ifactory,"/Message/Mailing-List"), TRUE);
2375         else
2376                 gtk_widget_set_sensitive (gtk_item_factory_get_item
2377                                 (ifactory,"/Message/Mailing-List"), FALSE);
2378 }
2379
2380 static gint mailing_list_create_submenu (GtkItemFactory *ifactory, MsgInfo *msginfo)
2381 {
2382         gint menu_nb = 0;
2383         GtkWidget *menuitem;
2384         
2385         if (!msginfo || !msginfo->extradata) {
2386                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Post", FALSE);
2387                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Help", FALSE);
2388                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Subscribe", FALSE);
2389                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Unsubscribe", FALSE);
2390                 menu_set_sensitive(ifactory, "/Message/Mailing-List/View archive", FALSE);
2391                 menu_set_sensitive(ifactory, "/Message/Mailing-List/Contact owner", FALSE);
2392                 return 0;
2393         }
2394                 
2395         /* Mailing list post */
2396         if (!strcmp2 (msginfo->extradata->list_post, "NO")) {
2397                 g_free(msginfo->extradata->list_post);
2398                 msginfo->extradata->list_post = g_strdup (_("No posting allowed"));
2399         }
2400         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Post");
2401                 
2402         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_post);
2403  
2404         /* Mailing list help */
2405         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Help");
2406         
2407         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_help);
2408
2409         /* Mailing list subscribe */
2410         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Subscribe");
2411         
2412         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_subscribe);
2413                 
2414         /* Mailing list unsubscribe */
2415         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Unsubscribe");
2416         
2417         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_unsubscribe);
2418         
2419         /* Mailing list view archive */
2420         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/View archive");
2421         
2422         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_archive);
2423         
2424         /* Mailing list contact owner */
2425         menuitem = gtk_item_factory_get_item (ifactory, "/Message/Mailing-List/Contact owner");
2426         
2427         menu_nb += mailing_list_populate_submenu (menuitem, msginfo->extradata->list_owner);
2428         
2429         return menu_nb;
2430 }
2431
2432 static gint mailing_list_populate_submenu (GtkWidget *menuitem, const gchar * list_header)
2433 {
2434         GtkWidget *item, *menu;
2435         const gchar *url_pt ;
2436         gchar url_decoded[BUFFSIZE];
2437         GList *amenu, *alist;
2438         gint menu_nb = 0;
2439         
2440         menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2441         
2442         /* First delete old submenu */
2443         /* FIXME: we can optimize this, and only change/add/delete necessary items */
2444         for (amenu = (GTK_MENU_SHELL(menu)->children) ; amenu; ) {
2445                 alist = amenu->next;
2446                 item = GTK_WIDGET (amenu->data);
2447                 gtk_widget_destroy (item);
2448                 amenu = alist;
2449         }
2450         if (list_header) {
2451                 for (url_pt = list_header; url_pt && *url_pt;) {
2452                         get_url_part (&url_pt, url_decoded, BUFFSIZE);
2453                         item = NULL;
2454                         if (!g_strncasecmp(url_decoded, "mailto:", 7)) {
2455                                 item = gtk_menu_item_new_with_label ((url_decoded));
2456                                 g_signal_connect(G_OBJECT(item), "activate",
2457                                                  G_CALLBACK(mailing_list_compose),
2458                                                  NULL);
2459                         }
2460                         else if (!g_strncasecmp (url_decoded, "http:", 5) ||
2461                                  !g_strncasecmp (url_decoded, "https:",6)) {
2462
2463                                 item = gtk_menu_item_new_with_label ((url_decoded));
2464                                 g_signal_connect(G_OBJECT(item), "activate",
2465                                                  G_CALLBACK(mailing_list_open_uri),
2466                                                  NULL);
2467                         } 
2468                         if (item) {
2469                                 gtk_menu_append (GTK_MENU(menu), item);
2470                                 gtk_widget_show (item);
2471                                 menu_nb++;
2472                         }
2473                 }
2474         }
2475         if (menu_nb)
2476                 gtk_widget_set_sensitive (menuitem, TRUE);
2477         else
2478                 gtk_widget_set_sensitive (menuitem, FALSE);
2479                 
2480
2481         return menu_nb;
2482 }
2483
2484 static void get_url_part (const gchar **buffer, gchar *url_decoded, gint maxlen)
2485 {
2486         gchar tmp[BUFFSIZE];
2487         const gchar *buf;
2488         gint i = 0;
2489         buf = *buffer;
2490         
2491         if (buf == 0x00) {
2492                 *url_decoded = '\0';
2493                 *buffer = NULL;
2494                 return;
2495         }
2496         /* Ignore spaces, comments  and tabs () */
2497         for (;*buf == ' ' || *buf == '(' || *buf == '\t'; buf++)
2498                 if (*buf == '(')
2499                         for (;*buf != ')' && *buf != 0x00; buf++);
2500         
2501         /* First non space and non comment must be a < */
2502         if (*buf =='<' ) {
2503                 buf++;
2504                 for (i = 0; *buf != '>' && *buf != 0x00 && i<maxlen; tmp[i++] = *(buf++));
2505                 buf++;
2506         }
2507         else  {
2508                 *buffer = NULL;
2509                 *url_decoded = '\0';
2510                 return;
2511         }
2512         
2513         tmp[i]       = 0x00;
2514         *url_decoded = '\0';
2515         *buffer = NULL;
2516         
2517         if (i == maxlen) {
2518                 return;
2519         }
2520         decode_uri (url_decoded, (const gchar *)tmp);
2521
2522         /* Prepare the work for the next url in the list */
2523         /* after the closing bracket >, ignore space, comments and tabs */
2524         for (;buf && *buf && (*buf == ' ' || *buf == '(' || *buf == '\t'); buf++)
2525                 if (*buf == '(')
2526                         for (;*buf != ')' && *buf != 0x00; buf++);
2527                         
2528         if (!buf || !*buf) {
2529                 *buffer = NULL;
2530                 return;
2531         }
2532
2533         /* now first non space, non comment must be a comma */
2534         if (*buf != ',')
2535                 for (;*buf != 0x00; buf++);
2536         else
2537                 buf++;
2538         *buffer = buf;
2539 }
2540         
2541 static void mailing_list_compose (GtkWidget *w, gpointer *data)
2542 {
2543         gchar *mailto;
2544
2545         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
2546         compose_new(NULL, mailto+7, NULL);
2547 }
2548  
2549  static void mailing_list_open_uri (GtkWidget *w, gpointer *data)
2550 {
2551  
2552         gchar *mailto;
2553  
2554         gtk_label_get (GTK_LABEL (GTK_BIN (w)->child), (gchar **) &mailto);
2555         open_uri (mailto, prefs_common.uri_cmd);
2556
2557         
2558 void main_window_popup(MainWindow *mainwin)
2559 {
2560         if (!GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
2561                 main_window_show(mainwin);
2562
2563         gtkut_window_popup(mainwin->window);
2564 }
2565
2566 void main_window_show(MainWindow *mainwin)
2567 {
2568         gtk_widget_show(mainwin->window);
2569         gtk_widget_show(mainwin->vbox_body);
2570
2571         gtk_widget_set_uposition(mainwin->window,
2572                                  prefs_common.mainwin_x,
2573                                  prefs_common.mainwin_y);
2574
2575         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2576                              prefs_common.folderview_width,
2577                              prefs_common.folderview_height);
2578         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2579                              prefs_common.summaryview_width,
2580                              prefs_common.summaryview_height);
2581         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2582                              prefs_common.msgview_width,
2583                              prefs_common.msgview_height);
2584 }
2585
2586 void main_window_hide(MainWindow *mainwin)
2587 {
2588         main_window_get_size(mainwin);
2589         main_window_get_position(mainwin);
2590
2591         gtk_widget_hide(mainwin->window);
2592         gtk_widget_hide(mainwin->vbox_body);
2593 }
2594
2595 static void main_window_set_widgets(MainWindow *mainwin, LayoutType layout_mode)
2596 {
2597         GtkWidget *folderwin = NULL;
2598         GtkWidget *messagewin = NULL;
2599         GtkWidget *hpaned;
2600         GtkWidget *vpaned;
2601         GtkWidget *vbox_body = mainwin->vbox_body;
2602         GtkItemFactory *ifactory = mainwin->menu_factory;
2603         GtkWidget *menuitem;
2604
2605         debug_print("Setting widgets... ");
2606
2607         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2608                                     prefs_common.folderview_width,
2609                                     prefs_common.folderview_height);
2610         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2611                                     prefs_common.summaryview_width,
2612                                     prefs_common.summaryview_height);
2613         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2614                                     prefs_common.msgview_width,
2615                                     prefs_common.msgview_height);
2616
2617         mainwin->messageview->statusbar = mainwin->statusbar;
2618         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2619
2620         /* clean top-most container */
2621         if (mainwin->hpaned) {
2622                 if (mainwin->hpaned->parent == mainwin->vpaned)
2623                         gtk_widget_destroy(mainwin->vpaned);
2624                 else
2625                         gtk_widget_destroy(mainwin->hpaned);
2626         }
2627
2628         menu_set_sensitive(ifactory, "/View/Show or hide/Message view", (layout_mode != WIDE_MSGLIST_LAYOUT));
2629         switch (layout_mode) {
2630         case VERTICAL_LAYOUT:
2631         case NORMAL_LAYOUT:
2632                 hpaned = gtk_hpaned_new();
2633                 if (layout_mode == VERTICAL_LAYOUT)
2634                         vpaned = gtk_hpaned_new();
2635                 else
2636                         vpaned = gtk_vpaned_new();
2637                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2638                 gtk_paned_add1(GTK_PANED(hpaned),
2639                                GTK_WIDGET_PTR(mainwin->folderview));
2640                 gtk_widget_show(hpaned);
2641                 gtk_widget_queue_resize(hpaned);
2642
2643                 if (messageview_is_visible(mainwin->messageview)) {
2644                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2645                         gtk_paned_add1(GTK_PANED(vpaned),
2646                                        GTK_WIDGET_PTR(mainwin->summaryview));
2647                 } else {
2648                         gtk_paned_add2(GTK_PANED(hpaned),
2649                                        GTK_WIDGET_PTR(mainwin->summaryview));
2650                         gtk_widget_ref(vpaned);
2651                 }
2652                 gtk_paned_add2(GTK_PANED(vpaned),
2653                                GTK_WIDGET_PTR(mainwin->messageview));
2654                 gtk_widget_show(vpaned);
2655                 gtk_widget_queue_resize(vpaned);
2656                 break;
2657         case WIDE_LAYOUT:
2658                 vpaned = gtk_vpaned_new();
2659                 hpaned = gtk_hpaned_new();
2660                 gtk_box_pack_start(GTK_BOX(vbox_body), vpaned, TRUE, TRUE, 0);
2661                 gtk_paned_add1(GTK_PANED(vpaned), hpaned);
2662
2663                 gtk_paned_add1(GTK_PANED(hpaned),
2664                                GTK_WIDGET_PTR(mainwin->folderview));
2665                 gtk_paned_add2(GTK_PANED(hpaned),
2666                                GTK_WIDGET_PTR(mainwin->summaryview));
2667
2668                 gtk_widget_show(hpaned);
2669                 gtk_widget_queue_resize(hpaned);
2670
2671                 if (messageview_is_visible(mainwin->messageview)) {
2672                         gtk_paned_add2(GTK_PANED(vpaned),
2673                                GTK_WIDGET_PTR(mainwin->messageview));   
2674                 } else {
2675                         gtk_widget_ref(GTK_WIDGET_PTR(mainwin->messageview));
2676                 }
2677                 gtk_widget_show(vpaned);
2678                 gtk_widget_queue_resize(vpaned);
2679                 break;
2680         case WIDE_MSGLIST_LAYOUT:
2681                 vpaned = gtk_vpaned_new();
2682                 hpaned = gtk_hpaned_new();
2683                 gtk_box_pack_start(GTK_BOX(vbox_body), vpaned, TRUE, TRUE, 0);
2684
2685                 gtk_paned_add1(GTK_PANED(vpaned),
2686                                GTK_WIDGET_PTR(mainwin->summaryview));
2687                 gtk_paned_add1(GTK_PANED(hpaned),
2688                                GTK_WIDGET_PTR(mainwin->folderview));
2689
2690                 gtk_widget_show(hpaned);
2691                 gtk_widget_queue_resize(hpaned);
2692
2693                 if (messageview_is_visible(mainwin->messageview)) {
2694                         gtk_paned_add2(GTK_PANED(hpaned),
2695                                GTK_WIDGET_PTR(mainwin->messageview));   
2696                 } else {
2697                         gtk_widget_ref(GTK_WIDGET_PTR(mainwin->messageview));
2698                 }
2699                 gtk_paned_add2(GTK_PANED(vpaned), hpaned);
2700
2701                 gtk_widget_show(vpaned);
2702                 gtk_widget_queue_resize(vpaned);
2703                 break;
2704         default:
2705                 g_warning("Unknown layout");
2706                 return;
2707         }
2708
2709         mainwin->hpaned = hpaned;
2710         mainwin->vpaned = vpaned;
2711
2712         /* remove headerview if not in prefs */
2713         headerview_set_visibility(mainwin->messageview->headerview,
2714                                   prefs_common.display_header_pane);
2715
2716         if (messageview_is_visible(mainwin->messageview))
2717                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2718                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2719         else
2720                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2721                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2722
2723         gtk_window_move(GTK_WINDOW(mainwin->window),
2724                         prefs_common.mainwin_x,
2725                         prefs_common.mainwin_y);
2726
2727         gtk_widget_queue_resize(vbox_body);
2728         gtk_widget_queue_resize(mainwin->vbox);
2729         gtk_widget_queue_resize(mainwin->window);
2730         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2731          * and mimeview icon list/ctree lose track of their visibility states */
2732         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2733                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2734         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2735                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2736         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2737                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2738         else 
2739                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2740
2741         prefs_common.layout_mode = layout_mode;
2742
2743         menuitem = gtk_item_factory_get_item
2744                 (ifactory, "/View/Show or hide/Message view");
2745         gtk_check_menu_item_set_active
2746                 (GTK_CHECK_MENU_ITEM(menuitem),
2747                  messageview_is_visible(mainwin->messageview));
2748
2749 #define SET_CHECK_MENU_ACTIVE(path, active) \
2750 { \
2751         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2752         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2753 }
2754
2755         switch (prefs_common.layout_mode) {
2756         case NORMAL_LAYOUT:
2757                 SET_CHECK_MENU_ACTIVE("/View/Layout/Standard", TRUE);
2758                 break;
2759         case VERTICAL_LAYOUT:
2760                 SET_CHECK_MENU_ACTIVE("/View/Layout/Three columns", TRUE);
2761                 break;
2762         case WIDE_LAYOUT:
2763                 SET_CHECK_MENU_ACTIVE("/View/Layout/Wide message", TRUE);
2764                 break;
2765         case WIDE_MSGLIST_LAYOUT:
2766                 SET_CHECK_MENU_ACTIVE("/View/Layout/Wide message list", TRUE);
2767                 break;
2768         }
2769 #undef SET_CHECK_MENU_ACTIVE
2770
2771         if (folderwin) {
2772                 g_signal_connect
2773                         (G_OBJECT(folderwin), "size_allocate",
2774                          G_CALLBACK(folder_window_size_allocate_cb),
2775                          mainwin);
2776         }
2777         if (messagewin) {
2778                 g_signal_connect
2779                         (G_OBJECT(messagewin), "size_allocate",
2780                          G_CALLBACK(message_window_size_allocate_cb),
2781                          mainwin);
2782         }
2783
2784         debug_print("done.\n");
2785 }
2786
2787 void main_window_destroy_all(void)
2788 {
2789         while (mainwin_list != NULL) {
2790                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2791                 
2792                 /* free toolbar stuff */
2793                 toolbar_clear_list(TOOLBAR_MAIN);
2794                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2795                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2796
2797                 mainwin->folderview->mainwin = NULL;
2798                 mainwin->summaryview->mainwin = NULL;
2799                 mainwin->messageview->mainwin = NULL;
2800
2801                 g_free(mainwin->toolbar);
2802                 g_free(mainwin);
2803                 
2804                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2805         }
2806         g_list_free(mainwin_list);
2807         mainwin_list = NULL;
2808 }
2809
2810 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2811                                    gpointer data)
2812 {
2813         gtk_widget_set_size_request(child, 1, -1);
2814 }
2815
2816 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2817                                    gpointer data)
2818 {
2819         gtk_widget_set_size_request(child, -1, -1);
2820 }
2821
2822 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2823                                     gpointer data)
2824 {
2825         MainWindow *mainwin = (MainWindow *)data;
2826
2827         if (!event) return FALSE;
2828
2829         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2830         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2831                           widget);
2832
2833         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2834                        menu_button_position, widget,
2835                        event->button, event->time);
2836
2837         return TRUE;
2838 }
2839
2840 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2841                                  gpointer data)
2842 {
2843         MainWindow *mainwin = (MainWindow *)data;
2844
2845         if (mainwin->lock_count == 0)
2846                 app_exit_cb(data, 0, widget);
2847
2848         return TRUE;
2849 }
2850
2851 static void main_window_size_allocate_cb(GtkWidget *widget,
2852                                          GtkAllocation *allocation,
2853                                          gpointer data)
2854 {
2855         MainWindow *mainwin = (MainWindow *)data;
2856
2857         main_window_get_size(mainwin);
2858 }
2859
2860 static void folder_window_size_allocate_cb(GtkWidget *widget,
2861                                            GtkAllocation *allocation,
2862                                            gpointer data)
2863 {
2864         MainWindow *mainwin = (MainWindow *)data;
2865
2866         main_window_get_size(mainwin);
2867 }
2868
2869 static void message_window_size_allocate_cb(GtkWidget *widget,
2870                                             GtkAllocation *allocation,
2871                                             gpointer data)
2872 {
2873         MainWindow *mainwin = (MainWindow *)data;
2874
2875         main_window_get_size(mainwin);
2876 }
2877
2878 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2879                            GtkWidget *widget)
2880 {
2881         main_window_add_mailbox(mainwin);
2882 }
2883
2884 static void update_folderview_cb(MainWindow *mainwin, guint action,
2885                                  GtkWidget *widget)
2886 {
2887         summary_show(mainwin->summaryview, NULL);
2888         folderview_check_new_all();
2889 }
2890
2891 static void foldersort_cb(MainWindow *mainwin, guint action,
2892                            GtkWidget *widget)
2893 {
2894         foldersort_open();
2895 }
2896
2897 static void import_mbox_cb(MainWindow *mainwin, guint action,
2898                            GtkWidget *widget)
2899 {
2900         /* only notify if import has failed */
2901         if (import_mbox(mainwin->summaryview->folder_item) == -1) {
2902                 alertpanel_error(_("Mbox import has failed."));
2903         }
2904 }
2905
2906 static void export_mbox_cb(MainWindow *mainwin, guint action,
2907                            GtkWidget *widget)
2908 {
2909         /* only notify if export has failed */
2910         if (export_mbox(mainwin->summaryview->folder_item) == -1) {
2911                 alertpanel_error(_("Export to mbox has failed."));
2912         }
2913 }
2914
2915 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
2916                                 GtkWidget *widget)
2917 {
2918         /* only notify if export has failed */
2919         if (summaryview_export_mbox_list(mainwin->summaryview) == -1) {
2920                 alertpanel_error(_("Export to mbox has failed."));
2921         }
2922 }
2923
2924 static void empty_trash_cb(MainWindow *mainwin, guint action,
2925                            GtkWidget *widget)
2926 {
2927         main_window_empty_trash(mainwin, TRUE);
2928 }
2929
2930 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2931 {
2932         summary_save_as(mainwin->summaryview);
2933 }
2934
2935 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2936 {
2937         summary_print(mainwin->summaryview);
2938 }
2939
2940 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2941 {
2942         if (prefs_common.confirm_on_exit) {
2943                 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
2944                                GTK_STOCK_CANCEL, GTK_STOCK_QUIT,  NULL)
2945                     != G_ALERTALTERNATE)
2946                         return;
2947                 manage_window_focus_in(mainwin->window, NULL, NULL);
2948         }
2949
2950         app_will_exit(widget, mainwin);
2951 }
2952
2953 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2954 {
2955         if (action == 1)
2956                 summary_search(mainwin->summaryview);
2957         else
2958                 message_search(mainwin->messageview);
2959 }
2960
2961 static void mainwindow_quicksearch(MainWindow *mainwin, guint action, GtkWidget *widget)
2962 {
2963         summaryview_activate_quicksearch(mainwin->summaryview, TRUE);
2964 }
2965
2966 static void toggle_message_cb(MainWindow *mainwin, guint action,
2967                               GtkWidget *widget)
2968 {
2969         gboolean active;
2970
2971         active = GTK_CHECK_MENU_ITEM(widget)->active;
2972
2973         if (active != messageview_is_visible(mainwin->messageview))
2974                 summary_toggle_view(mainwin->summaryview);
2975 }
2976
2977 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2978                               GtkWidget *widget)
2979 {
2980         toolbar_toggle(action, mainwin);
2981 }
2982
2983 void main_window_reply_cb(MainWindow *mainwin, guint action,
2984                           GtkWidget *widget)
2985 {
2986         MessageView *msgview = (MessageView*)mainwin->messageview;
2987         GSList *msginfo_list = NULL;
2988
2989         g_return_if_fail(msgview != NULL);
2990
2991         msginfo_list = summary_get_selection(mainwin->summaryview);
2992         g_return_if_fail(msginfo_list != NULL);
2993         compose_reply_from_messageview(msgview, msginfo_list, action);
2994         g_slist_free(msginfo_list);
2995 }
2996
2997
2998 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2999                                 GtkWidget *widget)
3000 {
3001         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3002                 gtk_widget_show(mainwin->hbox_stat);
3003                 prefs_common.show_statusbar = TRUE;
3004         } else {
3005                 gtk_widget_hide(mainwin->hbox_stat);
3006                 prefs_common.show_statusbar = FALSE;
3007         }
3008 }
3009
3010 static void set_layout_cb(MainWindow *mainwin, guint action,
3011                                GtkWidget *widget)
3012 {
3013         LayoutType layout_mode = action;
3014
3015         if (mainwin->menu_lock_count) {
3016                 return;
3017         }
3018         if (!GTK_CHECK_MENU_ITEM(widget)->active) {
3019                 return;
3020         }
3021         
3022         if (layout_mode == prefs_common.layout_mode) {
3023                 return;
3024         }
3025         
3026         if (!mainwin->messageview->visible)
3027                 main_window_toggle_message_view(mainwin);
3028
3029         main_window_separation_change(mainwin, layout_mode);
3030
3031         mainwin_reset_paned(GTK_PANED(mainwin->vpaned));
3032         summary_relayout(mainwin->summaryview); 
3033 }
3034
3035 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline,
3036                                         gboolean ask_sync)
3037 {
3038         offline_ask_sync = ask_sync;
3039         if (offline)
3040                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
3041         else
3042                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
3043         offline_ask_sync = TRUE;
3044 }
3045
3046 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
3047 {
3048         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active, TRUE);
3049 }
3050
3051 static gboolean any_folder_want_synchronise(void)
3052 {
3053         GList *folderlist = folder_get_list();
3054
3055         /* see if there are synchronised folders */
3056         for (; folderlist; folderlist = folderlist->next) {
3057                 Folder *folder = (Folder *)folderlist->data;
3058                 if (folder_want_synchronise(folder)) {
3059                         return TRUE;
3060                 }
3061         }
3062         
3063         return FALSE;
3064 }
3065
3066 static void mainwindow_check_synchronise(MainWindow *mainwin, gboolean ask)
3067 {
3068         
3069         if (!any_folder_want_synchronise())
3070                 return;
3071
3072         if (offline_ask_sync && ask && alertpanel(_("Folder synchronisation"),
3073                         _("Do you want to synchronise your folders now?"),
3074                         GTK_STOCK_CANCEL, _("+_Synchronise"), NULL) != G_ALERTALTERNATE)
3075                 return;
3076
3077         folder_synchronise(NULL);
3078 }
3079
3080 static void online_switch_clicked (GtkButton *btn, gpointer data) 
3081 {
3082         MainWindow *mainwin;
3083         GtkItemFactory *ifactory;
3084         GtkCheckMenuItem *menuitem;
3085
3086         mainwin = (MainWindow *) data;
3087         
3088         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
3089         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
3090         
3091         g_return_if_fail(mainwin != NULL);
3092         g_return_if_fail(menuitem != NULL);
3093         
3094         if (btn == GTK_BUTTON(mainwin->online_switch)) {
3095                 gtk_widget_hide (mainwin->online_switch);
3096                 gtk_widget_show (mainwin->offline_switch);
3097                 menuitem->active = TRUE;
3098                 inc_autocheck_timer_remove();
3099                         
3100                 /* go offline */
3101                 if (prefs_common.work_offline)
3102                         return;
3103                 mainwindow_check_synchronise(mainwin, TRUE);
3104                 prefs_common.work_offline = TRUE;
3105                 imap_disconnect_all();
3106                 hooks_invoke(OFFLINE_SWITCH_HOOKLIST, NULL);
3107         } else {
3108                 /*go online */
3109                 if (!prefs_common.work_offline)
3110                         return;
3111                 gtk_widget_hide (mainwin->offline_switch);
3112                 gtk_widget_show (mainwin->online_switch);
3113                 menuitem->active = FALSE;
3114                 prefs_common.work_offline = FALSE;
3115                 inc_autocheck_timer_set();
3116                 refresh_resolvers();
3117                 hooks_invoke(OFFLINE_SWITCH_HOOKLIST, NULL);
3118         }
3119 }
3120
3121 static void addressbook_open_cb(MainWindow *mainwin, guint action,
3122                                 GtkWidget *widget)
3123 {
3124         addressbook_open(NULL);
3125 }
3126
3127 static void log_window_show_cb(MainWindow *mainwin, guint action,
3128                                GtkWidget *widget)
3129 {
3130         log_window_show(mainwin->logwin);
3131 }
3132
3133 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3134 {
3135         inc_cancel_all();
3136 }
3137
3138 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3139 {
3140         summary_move_to(mainwin->summaryview);
3141 }
3142
3143 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3144 {
3145         summary_copy_to(mainwin->summaryview);
3146 }
3147
3148 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3149 {
3150         summary_delete(mainwin->summaryview);
3151 }
3152
3153 static void delete_trash_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3154 {
3155         summary_delete_trash(mainwin->summaryview);
3156 }
3157
3158 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3159 {
3160         summary_cancel(mainwin->summaryview);
3161 }
3162
3163 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3164 {
3165         summary_open_msg(mainwin->summaryview);
3166 }
3167
3168 static void view_source_cb(MainWindow *mainwin, guint action,
3169                            GtkWidget *widget)
3170 {
3171         summary_view_source(mainwin->summaryview);
3172 }
3173
3174 static void show_all_header_cb(MainWindow *mainwin, guint action,
3175                                GtkWidget *widget)
3176 {
3177         if (mainwin->menu_lock_count) return;
3178         mainwin->summaryview->messageview->all_headers = 
3179                         GTK_CHECK_MENU_ITEM(widget)->active;
3180         summary_display_msg_selected(mainwin->summaryview,
3181                                      GTK_CHECK_MENU_ITEM(widget)->active);
3182 }
3183
3184 #define SET_CHECK_MENU_ACTIVE(path, active) \
3185 { \
3186         menuitem = gtk_item_factory_get_widget(ifactory, path); \
3187         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
3188 }
3189
3190 static void hide_quotes_cb(MainWindow *mainwin, guint action,
3191                                GtkWidget *widget)
3192 {
3193         GtkWidget *menuitem;
3194         GtkItemFactory *ifactory = mainwin->menu_factory;
3195
3196         if (mainwin->menu_lock_count) return;
3197
3198         prefs_common.hide_quotes = 
3199                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
3200
3201         mainwin->menu_lock_count++;
3202         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
3203         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
3204         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
3205         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);
3206         mainwin->menu_lock_count--;
3207
3208         summary_redisplay_msg(mainwin->summaryview);
3209 }
3210
3211 #undef SET_CHECK_MENU_ACTIVE
3212 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3213 {
3214         summary_mark(mainwin->summaryview);
3215 }
3216
3217 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3218 {
3219         summary_unmark(mainwin->summaryview);
3220 }
3221
3222 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
3223                               GtkWidget *widget)
3224 {
3225         summary_mark_as_unread(mainwin->summaryview);
3226 }
3227
3228 static void mark_as_read_cb(MainWindow *mainwin, guint action,
3229                             GtkWidget *widget)
3230 {
3231         summary_mark_as_read(mainwin->summaryview);
3232 }
3233
3234 static void mark_all_read_cb(MainWindow *mainwin, guint action,
3235                              GtkWidget *widget)
3236 {
3237         summary_mark_all_read(mainwin->summaryview);
3238 }
3239
3240 static void mark_as_spam_cb(MainWindow *mainwin, guint action,
3241                              GtkWidget *widget)
3242 {
3243         summary_mark_as_spam(mainwin->summaryview, action, NULL);
3244 }
3245
3246 static void ignore_thread_cb(MainWindow *mainwin, guint action,
3247                             GtkWidget *widget)
3248 {
3249         summary_ignore_thread(mainwin->summaryview);
3250 }
3251
3252 static void unignore_thread_cb(MainWindow *mainwin, guint action,
3253                             GtkWidget *widget)
3254 {
3255         summary_unignore_thread(mainwin->summaryview);
3256 }
3257
3258 static void lock_msgs_cb(MainWindow *mainwin, guint action,
3259                             GtkWidget *widget)
3260 {
3261         summary_msgs_lock(mainwin->summaryview);
3262 }
3263
3264 static void unlock_msgs_cb(MainWindow *mainwin, guint action,
3265                             GtkWidget *widget)
3266 {
3267         summary_msgs_unlock(mainwin->summaryview);
3268 }
3269
3270
3271 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3272 {
3273         summary_reedit(mainwin->summaryview);
3274 }
3275
3276 static void add_address_cb(MainWindow *mainwin, guint action,
3277                            GtkWidget *widget)
3278 {
3279         summary_add_address(mainwin->summaryview);
3280 }
3281
3282 static void set_charset_cb(MainWindow *mainwin, guint action,
3283                            GtkWidget *widget)
3284 {
3285         const gchar *str;
3286
3287         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3288                 str = conv_get_charset_str((CharSet)action);
3289                 
3290                 g_free(mainwin->messageview->forced_charset);
3291                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
3292                 procmime_force_charset(str);
3293                 
3294                 summary_redisplay_msg(mainwin->summaryview);
3295                 
3296                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
3297         }
3298 }
3299
3300 static void set_decode_cb(MainWindow *mainwin, guint action,
3301                            GtkWidget *widget)
3302 {
3303         if (GTK_CHECK_MENU_ITEM(widget)->active) {
3304                 mainwin->messageview->forced_encoding = (EncodingType)action;
3305                 
3306                 summary_redisplay_msg(mainwin->summaryview);
3307                 
3308                 debug_print("forced encoding: %d\n", action);
3309         }
3310 }
3311
3312 static void hide_read_messages (MainWindow *mainwin, guint action,
3313                                 GtkWidget *widget)
3314 {
3315         if (!mainwin->summaryview->folder_item
3316             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
3317                 return;
3318         summary_toggle_show_read_messages(mainwin->summaryview);
3319 }
3320
3321 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3322 {
3323         gboolean threaded = FALSE;
3324         if (mainwin->menu_lock_count) return;
3325         if (!mainwin->summaryview->folder_item) return;
3326
3327         threaded = GTK_CHECK_MENU_ITEM(widget)->active;
3328
3329         mainwin->summaryview->folder_item->threaded = threaded; 
3330
3331         mainwin->summaryview->threaded = threaded;
3332
3333         summary_show(mainwin->summaryview, 
3334                         mainwin->summaryview->folder_item);
3335 }
3336
3337 static void expand_threads_cb(MainWindow *mainwin, guint action,
3338                               GtkWidget *widget)
3339 {
3340         summary_expand_threads(mainwin->summaryview);
3341 }
3342
3343 static void collapse_threads_cb(MainWindow *mainwin, guint action,
3344                                 GtkWidget *widget)
3345 {
3346         summary_collapse_threads(mainwin->summaryview);
3347 }
3348
3349 static void set_summary_display_item_cb(MainWindow *mainwin, guint action,
3350                                 GtkWidget *widget)
3351 {
3352         prefs_summary_column_open();
3353 }
3354
3355 static void set_folder_display_item_cb(MainWindow *mainwin, guint action,
3356                                 GtkWidget *widget)
3357 {
3358         prefs_folder_column_open();
3359 }
3360
3361 static void sort_summary_cb(MainWindow *mainwin, guint action,
3362                             GtkWidget *widget)
3363 {
3364         FolderItem *item = mainwin->summaryview->folder_item;
3365         GtkWidget *menuitem;
3366
3367         if (mainwin->menu_lock_count) return;
3368
3369         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
3370                 menuitem = gtk_item_factory_get_item
3371                         (mainwin->menu_factory, "/View/Sort/Ascending");
3372                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
3373                              GTK_CHECK_MENU_ITEM(menuitem)->active
3374                              ? SORT_ASCENDING : SORT_DESCENDING);
3375                 item->sort_key = action;
3376         }
3377 }
3378
3379 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
3380                                  GtkWidget *widget)
3381 {
3382         FolderItem *item = mainwin->summaryview->folder_item;
3383
3384         if (mainwin->menu_lock_count) return;
3385
3386         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
3387                 summary_sort(mainwin->summaryview,
3388                              item->sort_key, (FolderSortType)action);
3389 }
3390
3391 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
3392                                   GtkWidget *widget)
3393 {
3394         summary_attract_by_subject(mainwin->summaryview);
3395 }
3396
3397 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
3398                                  GtkWidget *widget)
3399 {
3400         FolderItem *item;
3401
3402         item = folderview_get_selected_item(mainwin->folderview);
3403         if (item) {
3404                 main_window_cursor_wait(mainwin);
3405                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
3406
3407                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
3408                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
3409
3410                 STATUSBAR_POP(mainwin);
3411                 main_window_cursor_normal(mainwin);
3412         }
3413 }
3414
3415 struct DelDupsData
3416 {
3417         guint   dups;
3418         guint   folders;
3419 };
3420
3421 static void deldup_all(FolderItem *item, gpointer _data)
3422 {
3423         struct DelDupsData *data = _data;
3424         gint result;
3425         
3426         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
3427         if (result >= 0) {
3428                 data->dups += result;
3429                 data->folders += 1;
3430         }
3431 }
3432
3433 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
3434                                  GtkWidget *widget)
3435 {
3436         struct DelDupsData data = {0, 0};
3437
3438         main_window_cursor_wait(mainwin);
3439         folder_func_to_all_folders(deldup_all, &data);
3440         main_window_cursor_normal(mainwin);
3441         
3442         alertpanel_notice(ngettext("Deleted %d duplicate message in %d folders.\n",
3443                                    "Deleted %d duplicate messages in %d folders.\n",
3444                                    data.dups),
3445                           data.dups, data.folders);
3446 }
3447
3448 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3449 {
3450         summary_filter(mainwin->summaryview, (gboolean)action);
3451 }
3452
3453 static void execute_summary_cb(MainWindow *mainwin, guint action,
3454                                GtkWidget *widget)
3455 {
3456         summary_execute(mainwin->summaryview);
3457 }
3458
3459 static void update_summary_cb(MainWindow *mainwin, guint action,
3460                               GtkWidget *widget)
3461 {
3462         FolderItem *fitem;
3463         FolderView *folderview = mainwin->folderview;
3464
3465         if (!mainwin->summaryview->folder_item) return;
3466         if (!folderview->opened) return;
3467
3468         folder_update_op_count();
3469
3470         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
3471                                             folderview->opened);
3472         if (!fitem) return;
3473
3474         folder_item_scan(fitem);
3475         summary_show(mainwin->summaryview, fitem);
3476 }
3477
3478 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3479 {
3480         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
3481 }
3482
3483 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3484 {
3485         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
3486 }
3487
3488 static void prev_unread_cb(MainWindow *mainwin, guint action,
3489                            GtkWidget *widget)
3490 {
3491         summary_select_prev_unread(mainwin->summaryview);
3492 }
3493
3494 static void next_unread_cb(MainWindow *mainwin, guint action,
3495                            GtkWidget *widget)
3496 {
3497         summary_select_next_unread(mainwin->summaryview);
3498 }
3499
3500 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3501 {
3502         summary_select_prev_new(mainwin->summaryview);
3503 }
3504
3505 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3506 {
3507         summary_select_next_new(mainwin->summaryview);
3508 }
3509
3510 static void prev_marked_cb(MainWindow *mainwin, guint action,
3511                            GtkWidget *widget)
3512 {
3513         summary_select_prev_marked(mainwin->summaryview);
3514 }
3515
3516 static void next_marked_cb(MainWindow *mainwin, guint action,
3517                            GtkWidget *widget)
3518 {
3519         summary_select_next_marked(mainwin->summaryview);
3520 }
3521
3522 static void prev_labeled_cb(MainWindow *mainwin, guint action,
3523                             GtkWidget *widget)
3524 {
3525         summary_select_prev_labeled(mainwin->summaryview);
3526 }
3527
3528 static void next_labeled_cb(MainWindow *mainwin, guint action,
3529                             GtkWidget *widget)
3530 {
3531         summary_select_next_labeled(mainwin->summaryview);
3532 }
3533
3534 static void last_read_cb(MainWindow *mainwin, guint action,
3535                             GtkWidget *widget)
3536 {
3537         summary_select_last_read(mainwin->summaryview);
3538 }
3539
3540 static void parent_cb(MainWindow *mainwin, guint action,
3541                             GtkWidget *widget)
3542 {
3543         summary_select_parent(mainwin->summaryview);
3544 }
3545
3546 static void goto_folder_cb(MainWindow *mainwin, guint action,
3547                            GtkWidget *widget)
3548 {
3549         FolderItem *to_folder;
3550
3551         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
3552
3553         if (to_folder)
3554                 folderview_select(mainwin->folderview, to_folder);
3555 }
3556
3557 static void goto_unread_folder_cb(MainWindow *mainwin, guint action,
3558                            GtkWidget *widget)
3559 {
3560         folderview_select_next_unread(mainwin->folderview, FALSE);
3561 }
3562
3563 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3564 {
3565         messageview_copy_clipboard(mainwin->messageview);
3566 }
3567
3568 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3569 {
3570         MessageView *msgview = mainwin->messageview;
3571
3572         if (messageview_is_visible(msgview) &&
3573                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
3574                 messageview_select_all(mainwin->messageview);
3575         else
3576                 summary_select_all(mainwin->summaryview);
3577 }
3578
3579 static void select_thread_cb(MainWindow *mainwin, guint action,
3580                              GtkWidget *widget)
3581 {
3582         summary_select_thread(mainwin->summaryview, FALSE);
3583 }
3584
3585 static void delete_thread_cb(MainWindow *mainwin, guint action,
3586                              GtkWidget *widget)
3587 {
3588         summary_select_thread(mainwin->summaryview, TRUE);
3589 }
3590
3591 static void create_filter_cb(MainWindow *mainwin, guint action,
3592                              GtkWidget *widget)
3593 {
3594         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
3595 }
3596
3597 static void create_processing_cb(MainWindow *mainwin, guint action,
3598                              GtkWidget *widget)
3599 {
3600         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
3601 }
3602
3603 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
3604                                          GtkWidget *widget)
3605 {
3606         prefs_filtering_open(&pre_global_processing,
3607                              _("Processing rules to apply before folder rules"),
3608                              MANUAL_ANCHOR_PROCESSING,
3609                              NULL, NULL, FALSE);
3610 }
3611
3612 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
3613                                           GtkWidget *widget)
3614 {
3615         prefs_filtering_open(&post_global_processing,
3616                              _("Processing rules to apply after folder rules"),
3617                              MANUAL_ANCHOR_PROCESSING,
3618                              NULL, NULL, FALSE);
3619 }
3620
3621 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
3622                                     GtkWidget *widget)
3623 {
3624         prefs_filtering_open(&filtering_rules,
3625                              _("Filtering configuration"),
3626                              MANUAL_ANCHOR_FILTERING,
3627                              NULL, NULL, TRUE);
3628 }
3629
3630 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
3631                                    GtkWidget *widget)
3632 {
3633         prefs_template_open();
3634 }
3635
3636 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
3637                                   GtkWidget *widget)
3638 {
3639         prefs_actions_open(mainwin);
3640 }
3641 #ifdef USE_OPENSSL
3642 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
3643                                   GtkWidget *widget)
3644 {
3645         ssl_manager_open(mainwin);
3646 }
3647 #endif
3648 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
3649                                   GtkWidget *widget)
3650 {
3651         if (!cur_account) {
3652                 new_account_cb(mainwin, 0, widget);
3653         } else {
3654                 account_open(cur_account);
3655         }
3656 }
3657
3658 static void new_account_cb(MainWindow *mainwin, guint action,
3659                            GtkWidget *widget)
3660 {
3661         account_edit_open();
3662         if (!compose_get_compose_list()) account_add();
3663 }
3664
3665 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
3666 {
3667         FolderItem *item = NULL;
3668         cur_account = (PrefsAccount *)data;
3669         
3670         if (!mainwindow_get_mainwindow())
3671                 return;
3672         main_window_show_cur_account(mainwindow_get_mainwindow());
3673         toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
3674         main_window_set_menu_sensitive(mainwindow_get_mainwindow());
3675         toolbar_main_set_sensitive(mainwindow_get_mainwindow());
3676         item = folderview_get_selected_item(
3677                         mainwindow_get_mainwindow()->folderview);
3678         if (item) {
3679                 toolbar_set_compose_button
3680                         (mainwindow_get_mainwindow()->toolbar,
3681                          FOLDER_TYPE(item->folder) == F_NEWS ? 
3682                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
3683         }
3684 }
3685
3686 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
3687 {
3688         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3689         PrefsAccount *account = (PrefsAccount *)data;
3690
3691         inc_account_mail(mainwin, account);
3692 }
3693
3694 static void account_compose_menu_cb(GtkMenuItem *menuitem, gpointer data)
3695 {
3696         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3697         PrefsAccount *account = (PrefsAccount *)data;
3698         FolderItem *item = mainwin->summaryview->folder_item;   
3699
3700         compose_new_with_folderitem(account, item, NULL);
3701 }
3702
3703 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
3704 {
3705         prefs_gtk_open();
3706 }
3707
3708 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
3709 {
3710         pluginwindow_create();
3711 }
3712
3713 static void manual_open_cb(MainWindow *mainwin, guint action,
3714                            GtkWidget *widget)
3715 {
3716         manual_open((ManualType)action, NULL);
3717 }
3718
3719 static void legend_open_cb(GtkMenuItem *menuitem, gpointer data)
3720 {
3721         legend_show();
3722 }
3723
3724 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
3725 {
3726         MainWindow *mainwin = (MainWindow *)data;
3727         gchar *str;
3728
3729         if (item->path)
3730                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
3731                                       LOCAL_FOLDER(folder)->rootpath,
3732                                       G_DIR_SEPARATOR,
3733                                       item->path);
3734         else
3735                 str = g_strdup_printf(_("Scanning folder %s ..."),
3736                                       LOCAL_FOLDER(folder)->rootpath);
3737
3738         STATUSBAR_PUSH(mainwin, str);
3739         STATUSBAR_POP(mainwin);
3740         g_free(str);
3741 }
3742
3743 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3744                                           gpointer data)
3745 {
3746         SummaryView *summary;
3747
3748         g_return_val_if_fail(data, FALSE);
3749         if (!g_list_find(mainwin_list, data))
3750                 return TRUE;
3751         summary = ((MainWindow *)data)->summaryview;
3752         g_return_val_if_fail(summary, FALSE);
3753
3754         if (GTK_CLIST(summary->ctree)->selection && 
3755             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3756                 return FALSE;
3757
3758         return FALSE;
3759 }
3760
3761 #define BREAK_ON_MODIFIER_KEY() \
3762         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3763
3764 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3765                                     gpointer data)
3766 {
3767         MainWindow *mainwin = (MainWindow*) data;
3768         
3769         if (!mainwin || !event) 
3770                 return FALSE;
3771
3772         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3773         {
3774                 lastkey = event->keyval;
3775                 return FALSE;
3776         }
3777
3778         switch (event->keyval) {
3779         case GDK_Q:             /* Quit */
3780                 BREAK_ON_MODIFIER_KEY();
3781
3782                 app_exit_cb(mainwin, 0, NULL);
3783                 return FALSE;
3784         case GDK_space:
3785                 if (mainwin->folderview && mainwin->summaryview
3786                     && ((!mainwin->summaryview->displayed
3787                         && !mainwin->summaryview->selected) 
3788                         || (mainwin->summaryview->folder_item
3789                             && mainwin->summaryview->folder_item->total_msgs == 0))) {
3790                         g_signal_stop_emission_by_name(G_OBJECT(widget), 
3791                                        "key_press_event");
3792                         folderview_select_next_unread(mainwin->folderview, TRUE);
3793                 }
3794                 break;
3795         default:
3796                 break;
3797         }
3798         return FALSE;
3799 }
3800
3801 #undef BREAK_ON_MODIFIER_KEY
3802
3803 /*
3804  * Harvest addresses for selected folder.
3805  */
3806 static void addr_harvest_cb( MainWindow *mainwin,
3807                             guint action,
3808                             GtkWidget *widget )
3809 {
3810         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3811 }
3812
3813 /*
3814  * Harvest addresses for selected messages in summary view.
3815  */
3816 static void addr_harvest_msg_cb( MainWindow *mainwin,
3817                             guint action,
3818                             GtkWidget *widget )
3819 {
3820         summary_harvest_address( mainwin->summaryview );
3821 }
3822
3823 /*!
3824  *\brief        get a MainWindow
3825  *
3826  *\return       MainWindow * The first mainwindow in the mainwin_list
3827  */
3828 MainWindow *mainwindow_get_mainwindow(void)
3829 {
3830         if (mainwin_list && mainwin_list->data)
3831                 return (MainWindow *)(mainwin_list->data);
3832         else
3833                 return NULL;
3834 }
3835
3836 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3837 {
3838         ProgressData *data = (ProgressData *) source;
3839         MainWindow *mainwin = (MainWindow *) userdata;
3840
3841         switch (data->cmd) {
3842         case PROGRESS_COMMAND_START:
3843         case PROGRESS_COMMAND_STOP:
3844                 gtk_progress_bar_set_fraction
3845                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3846                 break;
3847         case PROGRESS_COMMAND_SET_PERCENTAGE:
3848                 gtk_progress_bar_set_fraction
3849                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3850                 break;          
3851         }
3852         while (gtk_events_pending()) gtk_main_iteration ();
3853
3854         return FALSE;
3855 }
3856
3857 static void sync_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3858 {
3859         mainwindow_check_synchronise(mainwin, FALSE);
3860 }
3861
3862 void mainwindow_learn (MainWindow *mainwin, gboolean is_spam)
3863 {
3864         summary_mark_as_spam(mainwin->summaryview, is_spam, NULL);
3865 }
3866
3867 void mainwindow_jump_to(const gchar *target)
3868 {
3869         gchar *tmp = NULL;
3870         gchar *p = NULL;
3871         FolderItem *item = NULL;
3872         gchar *msg = NULL;
3873         MainWindow *mainwin = mainwindow_get_mainwindow();
3874         
3875         if (!target)
3876                 return;
3877                 
3878         if (!mainwin) {
3879                 printf("not initialized\n");
3880                 return;
3881         }
3882
3883         tmp = g_strdup(target);
3884         
3885         if ((p = strstr(tmp, "\r")) != NULL)
3886                 *p = '\0';
3887         if ((p = strstr(tmp, "\n")) != NULL)
3888                 *p = '\0';
3889
3890         if ((item = folder_find_item_from_identifier(tmp))) {
3891                 printf("selecting folder '%s'\n", tmp);
3892                 folderview_select(mainwin->folderview, item);
3893                 main_window_popup(mainwin);
3894                 g_free(tmp);
3895                 return;
3896         }
3897         
3898         msg = strrchr(tmp, G_DIR_SEPARATOR);
3899         if (msg) {
3900                 *msg++ = '\0';
3901                 if ((item = folder_find_item_from_identifier(tmp))) {
3902                         printf("selecting folder '%s'\n", tmp);
3903                         folderview_select(mainwin->folderview, item);
3904                 } else {
3905                         printf("'%s' not found\n", tmp);
3906                 }
3907                 if (item && msg && atoi(msg)) {
3908                         printf("selecting message %d\n", atoi(msg));
3909                         summary_select_by_msgnum(mainwin->summaryview, atoi(msg));
3910                         summary_display_msg_selected(mainwin->summaryview, FALSE);
3911                         main_window_popup(mainwin);
3912                         g_free(tmp);
3913                         return;
3914                 } else if (item && msg[0] == '<' && msg[strlen(msg)-1] == '>') {
3915                         MsgInfo *msginfo = NULL;
3916                         msg++;
3917                         msg[strlen(msg)-1] = '\0';
3918                         msginfo = folder_item_get_msginfo_by_msgid(item, msg);
3919                         if (msginfo) {
3920                                 printf("selecting message %s\n", msg);
3921                                 summary_select_by_msgnum(mainwin->summaryview, msginfo->msgnum);
3922                                 summary_display_msg_selected(mainwin->summaryview, FALSE);
3923                                 main_window_popup(mainwin);
3924                                 g_free(tmp);
3925                                 procmsg_msginfo_free(msginfo);
3926                                 return;
3927                         } else {
3928                                 printf("'%s' not found\n", msg);
3929                         }
3930                 } else {
3931                         printf("'%s' not found\n", msg);
3932                 }
3933         } else {
3934                 printf("'%s' not found\n", tmp);
3935         }
3936         
3937         g_free(tmp);
3938 }