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