0613dc723a3e57084bcfae3701766b8c18ce38d2
[claws.git] / src / mainwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
95 #define AC_LABEL_WIDTH  240
96
97 /* list of all instantiated MainWindow */
98 static GList *mainwin_list = NULL;
99
100 static GdkCursor *watch_cursor;
101
102 static void main_window_menu_callback_block     (MainWindow     *mainwin);
103 static void main_window_menu_callback_unblock   (MainWindow     *mainwin);
104
105 static void main_window_show_cur_account        (MainWindow     *mainwin);
106
107 static void main_window_set_widgets             (MainWindow     *mainwin,
108                                                  SeparateType    type);
109
110 #if 0
111 static gboolean toolbar_account_button_pressed  (GtkWidget      *widget,
112                                                  GdkEventButton *event,
113                                                  gpointer        data);
114 #endif
115
116 static void toolbar_child_attached              (GtkWidget      *widget,
117                                                  GtkWidget      *child,
118                                                  gpointer        data);
119 static void toolbar_child_detached              (GtkWidget      *widget,
120                                                  GtkWidget      *child,
121                                                  gpointer        data);
122
123 static gboolean ac_label_button_pressed         (GtkWidget      *widget,
124                                                  GdkEventButton *event,
125                                                  gpointer        data);
126
127 static gint main_window_close_cb                (GtkWidget      *widget,
128                                                  GdkEventAny    *event,
129                                                  gpointer        data);
130 static gint folder_window_close_cb              (GtkWidget      *widget,
131                                                  GdkEventAny    *event,
132                                                  gpointer        data);
133 static gint message_window_close_cb             (GtkWidget      *widget,
134                                                  GdkEventAny    *event,
135                                                  gpointer        data);
136
137 static void main_window_size_allocate_cb        (GtkWidget      *widget,
138                                                  GtkAllocation  *allocation,
139                                                  gpointer        data);
140 static void folder_window_size_allocate_cb      (GtkWidget      *widget,
141                                                  GtkAllocation  *allocation,
142                                                  gpointer        data);
143 static void message_window_size_allocate_cb     (GtkWidget      *widget,
144                                                  GtkAllocation  *allocation,
145                                                  gpointer        data);
146
147 static void update_folderview_cb (MainWindow    *mainwin,
148                                   guint          action,
149                                   GtkWidget     *widget);
150 static void add_mailbox_cb       (MainWindow    *mainwin,
151                                   guint          action,
152                                   GtkWidget     *widget);
153 static void foldersort_cb        (MainWindow    *mainwin,
154                                   guint          action,
155                                   GtkWidget     *widget);
156 static void import_mbox_cb       (MainWindow    *mainwin,
157                                   guint          action,
158                                   GtkWidget     *widget);
159 static void export_mbox_cb       (MainWindow    *mainwin,
160                                   guint          action,
161                                   GtkWidget     *widget);
162 static void export_list_mbox_cb  (MainWindow    *mainwin, 
163                                   guint          action,
164                                   GtkWidget     *widget);
165 static void empty_trash_cb       (MainWindow    *mainwin,
166                                   guint          action,
167                                   GtkWidget     *widget);
168
169 static void save_as_cb           (MainWindow    *mainwin,
170                                   guint          action,
171                                   GtkWidget     *widget);
172 static void print_cb             (MainWindow    *mainwin,
173                                   guint          action,
174                                   GtkWidget     *widget);
175 static void app_exit_cb          (MainWindow    *mainwin,
176                                   guint          action,
177                                   GtkWidget     *widget);
178
179 static void search_cb            (MainWindow    *mainwin,
180                                   guint          action,
181                                   GtkWidget     *widget);
182
183 static void toggle_folder_cb     (MainWindow    *mainwin,
184                                   guint          action,
185                                   GtkWidget     *widget);
186 static void toggle_message_cb    (MainWindow    *mainwin,
187                                   guint          action,
188                                   GtkWidget     *widget);
189 static void toggle_toolbar_cb    (MainWindow    *mainwin,
190                                   guint          action,
191                                   GtkWidget     *widget);
192 static void toggle_statusbar_cb  (MainWindow    *mainwin,
193                                   guint          action,
194                                   GtkWidget     *widget);
195 static void separate_widget_cb   (MainWindow    *mainwin,
196                                   guint          action,
197                                   GtkWidget     *widget);
198
199 static void addressbook_open_cb (MainWindow     *mainwin,
200                                  guint           action,
201                                  GtkWidget      *widget);
202 static void log_window_show_cb  (MainWindow     *mainwin,
203                                  guint           action,
204                                  GtkWidget      *widget);
205
206 static void inc_cancel_cb               (MainWindow     *mainwin,
207                                          guint           action,
208                                          GtkWidget      *widget);
209
210 static void open_msg_cb                 (MainWindow     *mainwin,
211                                          guint           action,
212                                          GtkWidget      *widget);
213
214 static void view_source_cb              (MainWindow     *mainwin,
215                                          guint           action,
216                                          GtkWidget      *widget);
217
218 static void show_all_header_cb          (MainWindow     *mainwin,
219                                          guint           action,
220                                          GtkWidget      *widget);
221
222 static void move_to_cb                  (MainWindow     *mainwin,
223                                          guint           action,
224                                          GtkWidget      *widget);
225 static void copy_to_cb                  (MainWindow     *mainwin,
226                                          guint           action,
227                                          GtkWidget      *widget);
228 static void delete_cb                   (MainWindow     *mainwin,
229                                          guint           action,
230                                          GtkWidget      *widget);
231 static void delete_trash_cb                     (MainWindow     *mainwin,
232                                          guint           action,
233                                          GtkWidget      *widget);
234
235 static void cancel_cb                   (MainWindow     *mainwin,
236                                          guint           action,
237                                          GtkWidget      *widget);
238
239 static void mark_cb                     (MainWindow     *mainwin,
240                                          guint           action,
241                                          GtkWidget      *widget);
242 static void unmark_cb                   (MainWindow     *mainwin,
243                                          guint           action,
244                                          GtkWidget      *widget);
245
246 static void mark_as_unread_cb           (MainWindow     *mainwin,
247                                          guint           action,
248                                          GtkWidget      *widget);
249 static void mark_as_read_cb             (MainWindow     *mainwin,
250                                          guint           action,
251                                          GtkWidget      *widget);
252 static void mark_all_read_cb            (MainWindow     *mainwin,
253                                          guint           action,
254                                          GtkWidget      *widget);
255
256 static void reedit_cb                   (MainWindow     *mainwin,
257                                          guint           action,
258                                          GtkWidget      *widget);
259
260 static void add_address_cb              (MainWindow     *mainwin,
261                                          guint           action,
262                                          GtkWidget      *widget);
263
264 static void set_charset_cb              (MainWindow     *mainwin,
265                                          guint           action,
266                                          GtkWidget      *widget);
267
268 static void set_decode_cb               (MainWindow     *mainwin,
269                                          guint           action,
270                                          GtkWidget      *widget);
271
272 static void hide_read_messages   (MainWindow    *mainwin,
273                                   guint          action,
274                                   GtkWidget     *widget);
275
276 static void thread_cb            (MainWindow    *mainwin,
277                                   guint          action,
278                                   GtkWidget     *widget);
279 static void expand_threads_cb    (MainWindow    *mainwin,
280                                   guint          action,
281                                   GtkWidget     *widget);
282 static void collapse_threads_cb  (MainWindow    *mainwin,
283                                   guint          action,
284                                   GtkWidget     *widget);
285
286 static void set_summary_display_item_cb  (MainWindow    *mainwin,
287                                   guint          action,
288                                   GtkWidget     *widget);
289 static void set_folder_display_item_cb   (MainWindow    *mainwin,
290                                   guint          action,
291                                   GtkWidget     *widget);
292 static void sort_summary_cb      (MainWindow    *mainwin,
293                                   guint          action,
294                                   GtkWidget     *widget);
295 static void sort_summary_type_cb (MainWindow    *mainwin,
296                                   guint          action,
297                                   GtkWidget     *widget);
298 static void attract_by_subject_cb(MainWindow    *mainwin,
299                                   guint          action,
300                                   GtkWidget     *widget);
301
302 static void delete_duplicated_cb (MainWindow    *mainwin,
303                                   guint          action,
304                                   GtkWidget     *widget);
305 static void delete_duplicated_all_cb (MainWindow        *mainwin,
306                                   guint          action,
307                                   GtkWidget     *widget);
308 static void filter_cb            (MainWindow    *mainwin,
309                                   guint          action,
310                                   GtkWidget     *widget);
311 static void execute_summary_cb   (MainWindow    *mainwin,
312                                   guint          action,
313                                   GtkWidget     *widget);
314 static void update_summary_cb    (MainWindow    *mainwin,
315                                   guint          action,
316                                   GtkWidget     *widget);
317
318 static void prev_cb              (MainWindow    *mainwin,
319                                   guint          action,
320                                   GtkWidget     *widget);
321 static void next_cb              (MainWindow    *mainwin,
322                                   guint          action,
323                                   GtkWidget     *widget);
324 static void next_unread_cb       (MainWindow    *mainwin,
325                                   guint          action,
326                                   GtkWidget     *widget);
327 static void prev_unread_cb       (MainWindow    *mainwin,
328                                   guint          action,
329                                   GtkWidget     *widget);
330
331 static void prev_new_cb          (MainWindow    *mainwin,
332                                   guint          action,
333                                   GtkWidget     *widget);
334 static void next_new_cb          (MainWindow    *mainwin,
335                                   guint          action,
336                                   GtkWidget     *widget);
337 static void prev_marked_cb       (MainWindow    *mainwin,
338                                   guint          action,
339                                   GtkWidget     *widget);
340 static void next_marked_cb       (MainWindow    *mainwin,
341                                   guint          action,
342                                   GtkWidget     *widget);
343 static void prev_labeled_cb      (MainWindow    *mainwin,
344                                   guint          action,
345                                   GtkWidget     *widget);
346 static void next_labeled_cb      (MainWindow    *mainwin,
347                                   guint          action,
348                                   GtkWidget     *widget);
349
350 static void goto_folder_cb       (MainWindow    *mainwin,
351                                   guint          action,
352                                   GtkWidget     *widget);
353
354 static void copy_cb              (MainWindow    *mainwin,
355                                   guint          action,
356                                   GtkWidget     *widget);
357 static void allsel_cb            (MainWindow    *mainwin,
358                                   guint          action,
359                                   GtkWidget     *widget);
360 static void select_thread_cb     (MainWindow    *mainwin,
361                                   guint          action,
362                                   GtkWidget     *widget);
363
364 static void create_filter_cb     (MainWindow    *mainwin,
365                                   guint          action,
366                                   GtkWidget     *widget);
367 static void create_processing_cb (MainWindow    *mainwin,
368                                   guint          action,
369                                   GtkWidget     *widget);
370
371 static void prefs_template_open_cb      (MainWindow     *mainwin,
372                                          guint           action,
373                                          GtkWidget      *widget);
374 static void prefs_actions_open_cb       (MainWindow     *mainwin,
375                                          guint           action,
376                                          GtkWidget      *widget);
377 static void prefs_account_open_cb       (MainWindow     *mainwin,
378                                          guint           action,
379                                          GtkWidget      *widget);
380
381 static void prefs_pre_processing_open_cb  (MainWindow   *mainwin,
382                                            guint         action,
383                                            GtkWidget    *widget);
384
385 static void prefs_post_processing_open_cb (MainWindow   *mainwin,
386                                            guint         action,
387                                            GtkWidget    *widget);
388
389 static void prefs_filtering_open_cb     (MainWindow     *mainwin,
390                                          guint           action,
391                                          GtkWidget      *widget);
392 #ifdef USE_OPENSSL
393 static void ssl_manager_open_cb         (MainWindow     *mainwin,
394                                          guint           action,
395                                          GtkWidget      *widget);
396 #endif
397 static void new_account_cb       (MainWindow    *mainwin,
398                                   guint          action,
399                                   GtkWidget     *widget);
400
401 static void account_selector_menu_cb     (GtkMenuItem   *menuitem,
402                                           gpointer       data);
403 static void account_receive_menu_cb      (GtkMenuItem   *menuitem,
404                                           gpointer       data);
405
406 static void prefs_open_cb       (GtkMenuItem    *menuitem,
407                                  gpointer        data);
408 static void plugins_open_cb     (GtkMenuItem    *menuitem,
409                                  gpointer        data);
410
411 static void online_switch_clicked(GtkButton     *btn, 
412                                   gpointer data);
413
414 static void manual_open_cb       (MainWindow    *mainwin,
415                                   guint          action,
416                                   GtkWidget     *widget);
417
418 static void scan_tree_func       (Folder        *folder,
419                                   FolderItem    *item,
420                                   gpointer       data);
421                                   
422 static void toggle_work_offline_cb(MainWindow *mainwin, guint action, GtkWidget *widget);
423
424 static void addr_harvest_cb      ( MainWindow  *mainwin,
425                                    guint       action,
426                                    GtkWidget   *widget );
427
428 static void addr_harvest_msg_cb  ( MainWindow  *mainwin,
429                                    guint       action,
430                                    GtkWidget   *widget );
431
432 static gboolean mainwindow_focus_in_event       (GtkWidget      *widget, 
433                                                  GdkEventFocus  *focus,
434                                                  gpointer        data);
435 void main_window_reply_cb                       (MainWindow     *mainwin, 
436                                                  guint           action,
437                                                  GtkWidget      *widget);
438 gboolean mainwindow_progressindicator_hook      (gpointer        source,
439                                                  gpointer        userdata);
440 #define  SEPARATE_ACTION 500 
441 static void mainwindow_quicksearch              (MainWindow     *mainwin, 
442                                                  guint           action, 
443                                                  GtkWidget      *widget);
444
445 static GtkItemFactoryEntry mainwin_entries[] =
446 {
447         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
448         {N_("/_File/_Add mailbox"),             NULL, NULL, 0, "<Branch>"},
449         {N_("/_File/_Add mailbox/MH..."),       NULL, add_mailbox_cb, 0, NULL},
450         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
451         {N_("/_File/Change folder order"),      NULL, foldersort_cb,  0, NULL},
452         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
453         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
454         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
455         {N_("/_File/Exp_ort selected to mbox file..."), 
456                                                 NULL, export_list_mbox_cb, 0, NULL},
457         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
458         {N_("/_File/Empty all _Trash folders"), "<shift>D", empty_trash_cb, 0, NULL},
459         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
460         {N_("/_File/_Save as..."),              "<control>S", save_as_cb, 0, NULL},
461         {N_("/_File/_Print..."),                NULL, print_cb, 0, NULL},
462         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
463         {N_("/_File/_Work offline"),            "<control>W", toggle_work_offline_cb, 0, "<ToggleItem>"},
464         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
465         /* {N_("/_File/_Close"),                "<alt>W", app_exit_cb, 0, NULL}, */
466         {N_("/_File/E_xit"),                    "<control>Q", app_exit_cb, 0, NULL},
467
468         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
469         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
470         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
471         {N_("/_Edit/Select _thread"),           NULL, select_thread_cb, 0, NULL},
472         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
473         {N_("/_Edit/_Find in current message..."),
474                                                 "<control>F", search_cb, 0, NULL},
475         {N_("/_Edit/_Search folder..."),        "<shift><control>F", search_cb, 1, NULL},
476         {N_("/_Edit/_Quick search"),            "slash", mainwindow_quicksearch, 0, NULL},
477         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
478         {N_("/_View/Show or hi_de"),            NULL, NULL, 0, "<Branch>"},
479         {N_("/_View/Show or hi_de/_Folder tree"),
480                                                 NULL, toggle_folder_cb, 0, "<ToggleItem>"},
481         {N_("/_View/Show or hi_de/_Message view"),
482                                                 "V", toggle_message_cb, 0, "<ToggleItem>"},
483         {N_("/_View/Show or hi_de/_Toolbar"),
484                                                 NULL, NULL, 0, "<Branch>"},
485         {N_("/_View/Show or hi_de/_Toolbar/Icon _and text"),
486                                                 NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
487         {N_("/_View/Show or hi_de/_Toolbar/_Icon"),
488                                                 NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Show or hide/Toolbar/Icon and text"},
489         {N_("/_View/Show or hi_de/_Toolbar/_Text"),
490                                                 NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Show or hide/Toolbar/Icon and text"},
491         {N_("/_View/Show or hi_de/_Toolbar/_None"),
492                                                 NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Show or hide/Toolbar/Icon and text"},
493         {N_("/_View/Show or hi_de/Status _bar"),
494                                                 NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
495         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
496         {N_("/_View/Separate f_older tree"),    NULL, separate_widget_cb, SEPARATE_FOLDER, "<ToggleItem>"},
497         {N_("/_View/Separate _message view"),   NULL, separate_widget_cb, SEPARATE_MESSAGE, "<ToggleItem>"},
498         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
499         {N_("/_View/_Sort"),                    NULL, NULL, 0, "<Branch>"},
500         {N_("/_View/_Sort/by _number"),         NULL, sort_summary_cb, SORT_BY_NUMBER, "<RadioItem>"},
501         {N_("/_View/_Sort/by s_ize"),           NULL, sort_summary_cb, SORT_BY_SIZE, "/View/Sort/by number"},
502         {N_("/_View/_Sort/by _date"),           NULL, sort_summary_cb, SORT_BY_DATE, "/View/Sort/by number"},
503         {N_("/_View/_Sort/by _from"),           NULL, sort_summary_cb, SORT_BY_FROM, "/View/Sort/by number"},
504         {N_("/_View/_Sort/by _recipient"),      NULL, sort_summary_cb, SORT_BY_TO, "/View/Sort/by number"},
505         {N_("/_View/_Sort/by _subject"),        NULL, sort_summary_cb, SORT_BY_SUBJECT, "/View/Sort/by number"},
506         {N_("/_View/_Sort/by _color label"),
507                                                 NULL, sort_summary_cb, SORT_BY_LABEL, "/View/Sort/by number"},
508         {N_("/_View/_Sort/by _mark"),           NULL, sort_summary_cb, SORT_BY_MARK, "/View/Sort/by number"},
509         {N_("/_View/_Sort/by _status"),         NULL, sort_summary_cb, SORT_BY_STATUS, "/View/Sort/by number"},
510         {N_("/_View/_Sort/by a_ttachment"),
511                                                 NULL, sort_summary_cb, SORT_BY_MIME, "/View/Sort/by number"},
512         {N_("/_View/_Sort/by score"),           NULL, sort_summary_cb, SORT_BY_SCORE, "/View/Sort/by number"},
513         {N_("/_View/_Sort/by locked"),          NULL, sort_summary_cb, SORT_BY_LOCKED, "/View/Sort/by number"},
514         {N_("/_View/_Sort/D_on't sort"),        NULL, sort_summary_cb, SORT_BY_NONE, "/View/Sort/by number"},
515         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
516         {N_("/_View/_Sort/Ascending"),          NULL, sort_summary_type_cb, SORT_ASCENDING, "<RadioItem>"},
517         {N_("/_View/_Sort/Descending"),         NULL, sort_summary_type_cb, SORT_DESCENDING, "/View/Sort/Ascending"},
518         {N_("/_View/_Sort/---"),                NULL, NULL, 0, "<Separator>"},
519         {N_("/_View/_Sort/_Attract by subject"),
520                                                 NULL, attract_by_subject_cb, 0, NULL},
521         {N_("/_View/Th_read view"),             "<control>T", thread_cb, 0, "<ToggleItem>"},
522         {N_("/_View/E_xpand all threads"),      NULL, expand_threads_cb, 0, NULL},
523         {N_("/_View/Co_llapse all threads"),    NULL, collapse_threads_cb, 0, NULL},
524         {N_("/_View/_Hide read messages"),      NULL, hide_read_messages, 0, "<ToggleItem>"},
525         {N_("/_View/Set displayed _columns"),   NULL, NULL, 0, "<Branch>"},
526         {N_("/_View/Set displayed _columns/in _Folder list..."),        NULL, set_folder_display_item_cb, 0, NULL},
527         {N_("/_View/Set displayed _columns/in _Message list..."),NULL, set_summary_display_item_cb, 0, NULL},
528
529         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
530         {N_("/_View/_Go to"),                   NULL, NULL, 0, "<Branch>"},
531         {N_("/_View/_Go to/_Prev message"),     "P", prev_cb, 0, NULL},
532         {N_("/_View/_Go to/_Next message"),     "N", next_cb, 0, NULL},
533         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
534         {N_("/_View/_Go to/P_rev unread message"),
535                                                 "<shift>P", prev_unread_cb, 0, NULL},
536         {N_("/_View/_Go to/N_ext unread message"),
537                                                 "<shift>N", next_unread_cb, 0, NULL},
538         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
539         {N_("/_View/_Go to/Prev ne_w message"), NULL, prev_new_cb, 0, NULL},
540         {N_("/_View/_Go to/Ne_xt new message"), NULL, next_new_cb, 0, NULL},
541         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
542         {N_("/_View/_Go to/Prev _marked message"),
543                                                 NULL, prev_marked_cb, 0, NULL},
544         {N_("/_View/_Go to/Next m_arked message"),
545                                                 NULL, next_marked_cb, 0, NULL},
546         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
547         {N_("/_View/_Go to/Prev _labeled message"),
548                                                 NULL, prev_labeled_cb, 0, NULL},
549         {N_("/_View/_Go to/Next la_beled message"),
550                                                 NULL, next_labeled_cb, 0, NULL},
551         {N_("/_View/_Go to/---"),               NULL, NULL, 0, "<Separator>"},
552         {N_("/_View/_Go to/Other _folder..."),  "G", goto_folder_cb, 0, NULL},
553         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
554
555 #define ENC_SEPARATOR \
556         {N_("/_View/Character _encoding/---"),          NULL, NULL, 0, "<Separator>"}
557 #define ENC_ACTION(action) \
558          NULL, set_charset_cb, action, "/View/Character encoding/Auto detect"
559
560         {N_("/_View/Character _encoding"),              NULL, NULL, 0, "<Branch>"},
561         {N_("/_View/Character _encoding/_Auto detect"),
562          NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
563         {N_("/_View/Character _encoding/---"),          NULL, NULL, 0, "<Separator>"},
564         {N_("/_View/Character _encoding/7bit ascii (US-ASC_II)"),
565          ENC_ACTION(C_US_ASCII)},
566         {N_("/_View/Character _encoding/Unicode (_UTF-8)"),
567          ENC_ACTION(C_UTF_8)},
568         ENC_SEPARATOR,
569
570         {N_("/_View/Character _encoding/Western European (ISO-8859-_1)"),
571          ENC_ACTION(C_ISO_8859_1)},
572         {N_("/_View/Character _encoding/Western European (ISO-8859-15)"),
573          ENC_ACTION(C_ISO_8859_15)},
574         ENC_SEPARATOR,
575
576         {N_("/_View/Character _encoding/Central European (ISO-8859-_2)"),
577          ENC_ACTION(C_ISO_8859_2)},
578         ENC_SEPARATOR,
579
580         {N_("/_View/Character _encoding/_Baltic (ISO-8859-13)"),
581          ENC_ACTION(C_ISO_8859_13)},
582         {N_("/_View/Character _encoding/Baltic (ISO-8859-_4)"),
583          ENC_ACTION(C_ISO_8859_4)},
584         ENC_SEPARATOR,
585
586         {N_("/_View/Character _encoding/Greek (ISO-8859-_7)"),
587          ENC_ACTION(C_ISO_8859_7)},
588         ENC_SEPARATOR,
589
590         {N_("/_View/Character _encoding/Hebrew (ISO-8859-_8)"),
591          ENC_ACTION(C_ISO_8859_8)},
592         {N_("/_View/Character _encoding/Hebrew (Windows-1255)"),
593          ENC_ACTION(C_CP1255)},
594         ENC_SEPARATOR,
595
596         {N_("/_View/Character _encoding/Turkish (ISO-8859-_9)"),
597          ENC_ACTION(C_ISO_8859_9)},
598         ENC_SEPARATOR,
599
600         {N_("/_View/Character _encoding/Cyrillic (ISO-8859-_5)"),
601          ENC_ACTION(C_ISO_8859_5)},
602         {N_("/_View/Character _encoding/Cyrillic (KOI8-_R)"),
603          ENC_ACTION(C_KOI8_R)},
604         {N_("/_View/Character _encoding/Cyrillic (KOI8-U)"),
605          ENC_ACTION(C_KOI8_U)},
606         {N_("/_View/Character _encoding/Cyrillic (Windows-1251)"),
607          ENC_ACTION(C_CP1251)},
608         ENC_SEPARATOR,
609
610         {N_("/_View/Character _encoding/Japanese (ISO-2022-_JP)"),
611          ENC_ACTION(C_ISO_2022_JP)},
612         {N_("/_View/Character _encoding/Japanese (ISO-2022-JP-2)"),
613          ENC_ACTION(C_ISO_2022_JP_2)},
614         {N_("/_View/Character _encoding/Japanese (_EUC-JP)"),
615          ENC_ACTION(C_EUC_JP)},
616         {N_("/_View/Character _encoding/Japanese (_Shift__JIS)"),
617          ENC_ACTION(C_SHIFT_JIS)},
618         ENC_SEPARATOR,
619
620         {N_("/_View/Character _encoding/Simplified Chinese (_GB2312)"),
621          ENC_ACTION(C_GB2312)},
622         {N_("/_View/Character _encoding/Simplified Chinese (GBK)"),
623          ENC_ACTION(C_GBK)},
624         {N_("/_View/Character _encoding/Traditional Chinese (_Big5)"),
625          ENC_ACTION(C_BIG5)},
626         {N_("/_View/Character _encoding/Traditional Chinese (EUC-_TW)"),
627          ENC_ACTION(C_EUC_TW)},
628         {N_("/_View/Character _encoding/Chinese (ISO-2022-_CN)"),
629          ENC_ACTION(C_ISO_2022_CN)},
630         ENC_SEPARATOR,
631
632         {N_("/_View/Character _encoding/Korean (EUC-_KR)"),
633          ENC_ACTION(C_EUC_KR)},
634         {N_("/_View/Character _encoding/Korean (ISO-2022-KR)"),
635          ENC_ACTION(C_ISO_2022_KR)},
636         ENC_SEPARATOR,
637
638         {N_("/_View/Character _encoding/Thai (TIS-620)"),
639          ENC_ACTION(C_TIS_620)},
640         {N_("/_View/Character _encoding/Thai (Windows-874)"),
641          ENC_ACTION(C_WINDOWS_874)},
642
643 #undef ENC_SEPARATOR
644 #undef ENC_ACTION
645
646 #define DEC_SEPARATOR \
647         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"}
648 #define DEC_ACTION(action) \
649          NULL, set_decode_cb, action, "/View/Decode/Auto detect"
650         {N_("/_View/Decode"),           NULL, NULL, 0, "<Branch>"},
651         {N_("/_View/Decode/_Auto detect"),
652          NULL, set_decode_cb, 0, "<RadioItem>"},
653         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"},
654         {N_("/_View/Decode/_8bit"),             DEC_ACTION(ENC_8BIT)},
655         {N_("/_View/Decode/_Quoted printable"), DEC_ACTION(ENC_QUOTED_PRINTABLE)},
656         {N_("/_View/Decode/_Base64"),           DEC_ACTION(ENC_BASE64)},
657         {N_("/_View/Decode/_Uuencode"),         DEC_ACTION(ENC_X_UUENCODE)},
658
659 #undef DEC_SEPARATOR
660 #undef DEC_ACTION
661
662         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
663         {N_("/_View/Open in new _window"),      "<control><alt>N", open_msg_cb, 0, NULL},
664         {N_("/_View/Mess_age source"),          "<control>U", view_source_cb, 0, NULL},
665         {N_("/_View/Show all headers"),         "<control>H", show_all_header_cb, 0, "<ToggleItem>"},
666         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
667         {N_("/_View/_Update summary"),          "<control><alt>U", update_summary_cb,  0, NULL},
668
669         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
670         {N_("/_Message/Recei_ve"),              NULL, NULL, 0, "<Branch>"},
671         {N_("/_Message/Recei_ve/Get from _current account"),
672                                                 "<control>I",   inc_mail_cb, 0, NULL},
673         {N_("/_Message/Recei_ve/Get from _all accounts"),
674                                                 "<shift><control>I", inc_all_account_mail_cb, 0, NULL},
675         {N_("/_Message/Recei_ve/Cancel receivin_g"),
676                                                 NULL, inc_cancel_cb, 0, NULL},
677         {N_("/_Message/Recei_ve/---"),          NULL, NULL, 0, "<Separator>"},
678         {N_("/_Message/_Send queued messages"), NULL, send_queue_cb, 0, NULL},
679         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
680         {N_("/_Message/Compose a_n email message"),     "<control>M", compose_mail_cb, 0, NULL},
681         {N_("/_Message/Compose a news message"),        NULL,   compose_news_cb, 0, NULL},
682         {N_("/_Message/_Reply"),                "<control>R",   main_window_reply_cb, COMPOSE_REPLY, NULL},
683         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
684         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", main_window_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
685         {N_("/_Message/Repl_y to/_sender"),     NULL, main_window_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
686         {N_("/_Message/Repl_y to/mailing _list"),
687                                                 "<control>L", main_window_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
688         {N_("/_Message/Follow-up and reply to"),NULL, main_window_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
689         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
690         {N_("/_Message/_Forward"),              "<control><alt>F", main_window_reply_cb, COMPOSE_FORWARD, NULL},
691         {N_("/_Message/Redirect"),              NULL, main_window_reply_cb, COMPOSE_REDIRECT, NULL},
692         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
693         {N_("/_Message/M_ove..."),              "<control>O", move_to_cb, 0, NULL},
694         {N_("/_Message/_Copy..."),              "<shift><control>O", copy_to_cb, 0, NULL},
695         {N_("/_Message/Move to _trash"),        "<control>D", delete_trash_cb,  0, NULL},
696         {N_("/_Message/_Delete..."),            NULL, delete_cb,  0, NULL},
697         {N_("/_Message/Cancel a news message"), "", cancel_cb,  0, NULL},
698         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
699         {N_("/_Message/_Mark"),                 NULL, NULL, 0, "<Branch>"},
700         {N_("/_Message/_Mark/_Mark"),           "<shift>asterisk", mark_cb, 0, NULL},
701         {N_("/_Message/_Mark/_Unmark"),         "U", unmark_cb, 0, NULL},
702         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
703         {N_("/_Message/_Mark/Mark as unr_ead"), "<shift>exclam", mark_as_unread_cb, 0, NULL},
704         {N_("/_Message/_Mark/Mark as rea_d"),
705                                                 NULL, mark_as_read_cb, 0, NULL},
706         {N_("/_Message/_Mark/Mark all _read"),  NULL, mark_all_read_cb, 0, NULL},
707         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
708         {N_("/_Message/Re-_edit"),              NULL, reedit_cb, 0, NULL},
709
710         {N_("/_Tools"),                         NULL, NULL, 0, "<Branch>"},
711         {N_("/_Tools/_Address book..."),        "<shift><control>A", addressbook_open_cb, 0, NULL},
712         {N_("/_Tools/Add sender to address boo_k"),
713                                                 NULL, add_address_cb, 0, NULL},
714         {N_("/_Tools/_Harvest addresses"),      NULL, NULL, 0, "<Branch>"},
715         {N_("/_Tools/_Harvest addresses/from _Folder..."),
716                                                 NULL, addr_harvest_cb, 0, NULL},
717         {N_("/_Tools/_Harvest addresses/from _Messages..."),
718                                                 NULL, addr_harvest_msg_cb, 0, NULL},
719         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
720         {N_("/_Tools/_Filter all messages in folder"),
721                                                 NULL, filter_cb, 0, NULL},
722         {N_("/_Tools/Filter _selected messages"),
723                                                 NULL, filter_cb, 1, NULL},
724         {N_("/_Tools/_Create filter rule"),     NULL, NULL, 0, "<Branch>"},
725         {N_("/_Tools/_Create filter rule/_Automatically"),
726                                                 NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
727         {N_("/_Tools/_Create filter rule/by _From"),
728                                                 NULL, create_filter_cb, FILTER_BY_FROM, NULL},
729         {N_("/_Tools/_Create filter rule/by _To"),
730                                                 NULL, create_filter_cb, FILTER_BY_TO, NULL},
731         {N_("/_Tools/_Create filter rule/by _Subject"),
732                                                 NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
733         {N_("/_Tools/C_reate processing rule"), NULL, NULL, 0, "<Branch>"},
734         {N_("/_Tools/C_reate processing rule/_Automatically"),
735                                                 NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
736         {N_("/_Tools/C_reate processing rule/by _From"),
737                                                 NULL, create_processing_cb, FILTER_BY_FROM, NULL},
738         {N_("/_Tools/C_reate processing rule/by _To"),
739                                                 NULL, create_processing_cb, FILTER_BY_TO, NULL},
740         {N_("/_Tools/C_reate processing rule/by _Subject"),
741                                                 NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
742         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
743         {N_("/_Tools/Actio_ns"),                NULL, NULL, 0, "<Branch>"},
744         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
745         {N_("/_Tools/Ch_eck for new messages in all folders"),
746                                                 NULL, update_folderview_cb, 0, NULL},
747         {N_("/_Tools/Delete du_plicated messages"),
748                                                 NULL, NULL, 0, "<Branch>"},
749         {N_("/_Tools/Delete du_plicated messages/In selected folder"),
750                                                 NULL, delete_duplicated_cb,   0, NULL},
751         {N_("/_Tools/Delete du_plicated messages/In all folders"),
752                                                 NULL, delete_duplicated_all_cb,   0, NULL},
753         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
754         {N_("/_Tools/E_xecute"),                "X", execute_summary_cb, 0, NULL},
755 #ifdef USE_OPENSSL
756         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
757         {N_("/_Tools/SSL cer_tificates..."),    
758                                                 NULL, ssl_manager_open_cb, 0, NULL},
759 #endif
760         {N_("/_Tools/---"),                     NULL, NULL, 0, "<Separator>"},
761         {N_("/_Tools/_Log window"),             "<shift><control>L", log_window_show_cb, 0, NULL},
762
763         {N_("/_Configuration"),                 NULL, NULL, 0, "<Branch>"},
764         {N_("/_Configuration/C_hange current account"),
765                                                 NULL, NULL, 0, "<Branch>"},
766         {N_("/_Configuration/_Preferences for current account..."),
767                                                 NULL, prefs_account_open_cb, 0, NULL},
768         {N_("/_Configuration/Create _new account..."),
769                                                 NULL, new_account_cb, 0, NULL},
770         {N_("/_Configuration/_Edit accounts..."),
771                                                 NULL, account_edit_open, 0, NULL},
772         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
773         {N_("/_Configuration/P_references..."),
774                                                 NULL, prefs_open_cb, 0, NULL},
775         {N_("/_Configuration/Pre-pr_ocessing..."),
776                                                 NULL, prefs_pre_processing_open_cb, 0, NULL},
777         {N_("/_Configuration/Post-pro_cessing..."),
778                                                 NULL, prefs_post_processing_open_cb, 0, NULL},
779         {N_("/_Configuration/_Filtering..."),
780                                                 NULL, prefs_filtering_open_cb, 0, NULL},
781         {N_("/_Configuration/_Templates..."),   NULL, prefs_template_open_cb, 0, NULL},
782         {N_("/_Configuration/_Actions..."),     NULL, prefs_actions_open_cb, 0, NULL},
783         {N_("/_Configuration/Plu_gins..."),     NULL, plugins_open_cb, 0, NULL},
784
785         {N_("/_Help"),                          NULL, NULL, 0, "<Branch>"},
786         {N_("/_Help/_Manual (Local)"),          NULL, manual_open_cb, MANUAL_MANUAL_LOCAL, NULL},
787         {N_("/_Help/_FAQ (Local)"),             NULL, manual_open_cb, MANUAL_FAQ_LOCAL, NULL},
788         {N_("/_Help/_User-contributed FAQ (Online)"),   
789                                                 NULL, manual_open_cb, MANUAL_FAQ_CLAWS, NULL},
790         {N_("/_Help/---"),                      NULL, NULL, 0, "<Separator>"},
791         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
792 };
793
794 static gboolean main_window_accel_activate (GtkAccelGroup *accelgroup,
795                                             GObject *arg1,
796                                             guint value,
797                                             GdkModifierType mod,
798                                             gpointer user_data) 
799 {
800         MainWindow *mainwin = (MainWindow *)user_data;
801
802         if (mainwin->summaryview &&
803             mainwin->summaryview->quicksearch &&
804             quicksearch_has_focus(mainwin->summaryview->quicksearch) &&
805             (mod == 0 || mod == GDK_SHIFT_MASK)) {
806                 quicksearch_pass_key(mainwin->summaryview->quicksearch, value, mod);
807                 return TRUE;
808         }
809         return FALSE;
810 }
811
812 MainWindow *main_window_create(SeparateType type)
813 {
814         MainWindow *mainwin;
815         GtkWidget *window;
816         GtkWidget *vbox;
817         GtkWidget *menubar;
818         GtkWidget *handlebox;
819         GtkWidget *vbox_body;
820         GtkWidget *hbox_stat;
821         GtkWidget *statusbar;
822         GtkWidget *progressbar;
823         GtkWidget *statuslabel;
824         GtkWidget *ac_button;
825         GtkWidget *ac_label;
826         GtkWidget *online_pixmap;
827         GtkWidget *offline_pixmap;
828         GtkWidget *online_switch;
829         GtkWidget *offline_switch;
830         GtkTooltips *offline_tip;
831         GtkTooltips *online_tip;
832         GtkTooltips *sel_ac_tip;
833
834         FolderView *folderview;
835         SummaryView *summaryview;
836         MessageView *messageview;
837         GdkColormap *colormap;
838         GdkColor color[4];
839         gboolean success[4];
840         GtkItemFactory *ifactory;
841         GtkWidget *ac_menu;
842         GtkWidget *menuitem;
843         gint i;
844         guint n_menu_entries;
845
846         static GdkGeometry geometry;
847
848         debug_print("Creating main window...\n");
849         mainwin = g_new0(MainWindow, 1);
850
851         /* main window */
852         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
853         gtk_window_set_title(GTK_WINDOW(window), PROG_VERSION);
854         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
855
856         if (!geometry.min_height) {
857                 geometry.min_width = 320;
858                 geometry.min_height = 200;
859         }
860         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
861                                       GDK_HINT_MIN_SIZE);
862
863         g_signal_connect(G_OBJECT(window), "delete_event",
864                          G_CALLBACK(main_window_close_cb), mainwin);
865         MANAGE_WINDOW_SIGNALS_CONNECT(window);
866         g_signal_connect(G_OBJECT(window), "focus_in_event",
867                          G_CALLBACK(mainwindow_focus_in_event),
868                          mainwin);
869         g_signal_connect(G_OBJECT(window), "key_press_event",
870                          G_CALLBACK(mainwindow_key_pressed), mainwin);
871
872         gtk_widget_realize(window);
873         gtk_widget_add_events(window, GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK);
874         
875
876         gtkut_widget_set_app_icon(window);
877
878         vbox = gtk_vbox_new(FALSE, 0);
879         gtk_widget_show(vbox);
880         gtk_container_add(GTK_CONTAINER(window), vbox);
881
882         /* menu bar */
883         n_menu_entries = sizeof(mainwin_entries) / sizeof(mainwin_entries[0]);
884         menubar = menubar_create(window, mainwin_entries, 
885                                  n_menu_entries, "<Main>", mainwin);
886         gtk_widget_show(menubar);
887         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
888         ifactory = gtk_item_factory_from_widget(menubar);
889
890         menu_set_sensitive(ifactory, "/Help/Manual (Local)", manual_available(MANUAL_MANUAL_LOCAL));
891         menu_set_sensitive(ifactory, "/Help/FAQ (Local)", manual_available(MANUAL_FAQ_LOCAL));
892
893         if (prefs_common.toolbar_detachable) {
894                 handlebox = gtk_handle_box_new();
895                 gtk_widget_show(handlebox);
896                 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
897                 g_signal_connect(G_OBJECT(handlebox), "child_attached",
898                                  G_CALLBACK(toolbar_child_attached), mainwin);
899                 g_signal_connect(G_OBJECT(handlebox), "child_detached",
900                                  G_CALLBACK(toolbar_child_detached), mainwin);
901         } else {
902                 handlebox = gtk_hbox_new(FALSE, 0);
903                 gtk_widget_show(handlebox);
904                 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
905         }
906         /* link window to mainwin->window to avoid gdk warnings */
907         mainwin->window       = window;
908         
909         /* create toolbar */
910         mainwin->toolbar = toolbar_create(TOOLBAR_MAIN, 
911                                           handlebox, 
912                                           (gpointer)mainwin);
913
914         /* vbox that contains body */
915         vbox_body = gtk_vbox_new(FALSE, BORDER_WIDTH);
916         gtk_widget_show(vbox_body);
917         gtk_container_set_border_width(GTK_CONTAINER(vbox_body), BORDER_WIDTH);
918         gtk_box_pack_start(GTK_BOX(vbox), vbox_body, TRUE, TRUE, 0);
919
920         hbox_stat = gtk_hbox_new(FALSE, 2);
921         gtk_box_pack_end(GTK_BOX(vbox_body), hbox_stat, FALSE, FALSE, 0);
922
923         statusbar = statusbar_create();
924         gtk_box_pack_start(GTK_BOX(hbox_stat), statusbar, TRUE, TRUE, 0);
925
926         progressbar = gtk_progress_bar_new();
927         gtk_widget_set_size_request(progressbar, 120, 1);
928         gtk_box_pack_start(GTK_BOX(hbox_stat), progressbar, FALSE, FALSE, 0);
929
930         online_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_ONLINE);
931         offline_pixmap = stock_pixmap_widget(hbox_stat, STOCK_PIXMAP_OFFLINE);
932         online_tip = gtk_tooltips_new();
933         online_switch = gtk_button_new ();
934         gtk_tooltips_set_tip(GTK_TOOLTIPS(online_tip),online_switch, 
935                              _("You are online. Click the icon to go offline"), NULL);
936         offline_tip = gtk_tooltips_new();
937         offline_switch = gtk_button_new ();
938         gtk_tooltips_set_tip(GTK_TOOLTIPS(offline_tip),offline_switch, 
939                              _("You are offline. Click the icon to go online"),
940                              NULL);
941         gtk_container_add (GTK_CONTAINER(online_switch), online_pixmap);
942         gtk_button_set_relief (GTK_BUTTON(online_switch), GTK_RELIEF_NONE);
943         g_signal_connect (G_OBJECT(online_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
944         gtk_box_pack_start (GTK_BOX(hbox_stat), online_switch, FALSE, FALSE, 0);
945         gtk_container_add (GTK_CONTAINER(offline_switch), offline_pixmap);
946         gtk_button_set_relief (GTK_BUTTON(offline_switch), GTK_RELIEF_NONE);
947         g_signal_connect (G_OBJECT(offline_switch), "clicked", G_CALLBACK(online_switch_clicked), mainwin);
948         gtk_box_pack_start (GTK_BOX(hbox_stat), offline_switch, FALSE, FALSE, 0);
949         
950         statuslabel = gtk_label_new("");
951         gtk_box_pack_start(GTK_BOX(hbox_stat), statuslabel, FALSE, FALSE, 0);
952
953         sel_ac_tip = gtk_tooltips_new();
954         ac_button = gtk_button_new();
955         gtk_tooltips_set_tip(GTK_TOOLTIPS(sel_ac_tip),
956                              ac_button, _("Select account"), NULL);
957         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
958         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
959         gtk_widget_set_size_request(ac_button, -1, 0);
960         gtk_box_pack_end(GTK_BOX(hbox_stat), ac_button, FALSE, FALSE, 0);
961         g_signal_connect(G_OBJECT(ac_button), "button_press_event",
962                          G_CALLBACK(ac_label_button_pressed), mainwin);
963
964         ac_label = gtk_label_new("");
965         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
966
967         gtk_widget_show_all(hbox_stat);
968
969         gtk_widget_hide(offline_switch);
970         /* create views */
971         mainwin->folderview  = folderview  = folderview_create();
972         mainwin->summaryview = summaryview = summary_create();
973         mainwin->messageview = messageview = messageview_create(mainwin);
974         mainwin->logwin      = log_window_create();
975
976         folderview->mainwin      = mainwin;
977         folderview->summaryview  = summaryview;
978
979         summaryview->mainwin     = mainwin;
980         summaryview->folderview  = folderview;
981         summaryview->messageview = messageview;
982         summaryview->window      = window;
983
984         messageview->statusbar   = statusbar;
985         mainwin->vbox           = vbox;
986         mainwin->menubar        = menubar;
987         mainwin->menu_factory   = ifactory;
988         mainwin->handlebox      = handlebox;
989         mainwin->vbox_body      = vbox_body;
990         mainwin->hbox_stat      = hbox_stat;
991         mainwin->statusbar      = statusbar;
992         mainwin->progressbar    = progressbar;
993         mainwin->statuslabel    = statuslabel;
994         mainwin->online_switch  = online_switch;
995         mainwin->online_pixmap  = online_pixmap;
996         mainwin->offline_pixmap = offline_pixmap;
997         mainwin->ac_button      = ac_button;
998         mainwin->ac_label       = ac_label;
999         mainwin->offline_switch    = offline_switch;
1000         
1001         /* set context IDs for status bar */
1002         mainwin->mainwin_cid = gtk_statusbar_get_context_id
1003                 (GTK_STATUSBAR(statusbar), "Main Window");
1004         mainwin->folderview_cid = gtk_statusbar_get_context_id
1005                 (GTK_STATUSBAR(statusbar), "Folder View");
1006         mainwin->summaryview_cid = gtk_statusbar_get_context_id
1007                 (GTK_STATUSBAR(statusbar), "Summary View");
1008         mainwin->messageview_cid = gtk_statusbar_get_context_id
1009                 (GTK_STATUSBAR(statusbar), "Message View");
1010
1011         messageview->statusbar_cid = mainwin->messageview_cid;
1012
1013         /* allocate colors for summary view and folder view */
1014         summaryview->color_marked.red = summaryview->color_marked.green = 0;
1015         summaryview->color_marked.blue = (guint16)65535;
1016
1017         summaryview->color_dim.red = summaryview->color_dim.green =
1018                 summaryview->color_dim.blue = COLOR_DIM;
1019
1020         gtkut_convert_int_to_gdk_color(prefs_common.color_new,
1021                                        &folderview->color_new);
1022
1023         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
1024                                        &folderview->color_op);
1025
1026         summaryview->color_important.red = 0;
1027         summaryview->color_important.green = 0;
1028         summaryview->color_important.blue = (guint16)65535;
1029
1030         color[0] = summaryview->color_marked;
1031         color[1] = summaryview->color_dim;
1032         color[2] = folderview->color_new;
1033         color[3] = folderview->color_op;
1034
1035         colormap = gdk_drawable_get_colormap(window->window);
1036         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
1037         for (i = 0; i < 4; i++) {
1038                 if (success[i] == FALSE)
1039                         g_warning("MainWindow: color allocation %d failed\n", i);
1040         }
1041
1042         debug_print("done.\n");
1043
1044         messageview->visible = prefs_common.msgview_visible;
1045
1046         main_window_set_widgets(mainwin, type);
1047
1048         g_signal_connect(G_OBJECT(window), "size_allocate",
1049                          G_CALLBACK(main_window_size_allocate_cb),
1050                          mainwin);
1051
1052         /* set menu items */
1053         menuitem = gtk_item_factory_get_item
1054                 (ifactory, "/View/Character encoding/Auto detect");
1055         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1056
1057         switch (prefs_common.toolbar_style) {
1058         case TOOLBAR_NONE:
1059                 menuitem = gtk_item_factory_get_item
1060                         (ifactory, "/View/Show or hide/Toolbar/None");
1061                 break;
1062         case TOOLBAR_ICON:
1063                 menuitem = gtk_item_factory_get_item
1064                         (ifactory, "/View/Show or hide/Toolbar/Icon");
1065                 break;
1066         case TOOLBAR_TEXT:
1067                 menuitem = gtk_item_factory_get_item
1068                         (ifactory, "/View/Show or hide/Toolbar/Text");
1069                 break;
1070         case TOOLBAR_BOTH:
1071                 menuitem = gtk_item_factory_get_item
1072                         (ifactory, "/View/Show or hide/Toolbar/Icon and text");
1073         }
1074         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1075
1076         gtk_widget_hide(mainwin->hbox_stat);
1077         menuitem = gtk_item_factory_get_item
1078                 (ifactory, "/View/Show or hide/Status bar");
1079         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1080                                        prefs_common.show_statusbar);
1081         
1082         /* set account selection menu */
1083         ac_menu = gtk_item_factory_get_widget
1084                 (ifactory, "/Configuration/Change current account");
1085         mainwin->ac_menu = ac_menu;
1086
1087         toolbar_main_set_sensitive(mainwin);
1088
1089         /* create actions menu */
1090         main_window_update_actions_menu(mainwin);
1091
1092         /* attach accel groups to main window */
1093 #define ADD_MENU_ACCEL_GROUP_TO_WINDOW(menu,win)                        \
1094         gtk_window_add_accel_group                                      \
1095                 (GTK_WINDOW(win),                                       \
1096                  gtk_item_factory_from_widget(menu)->accel_group);      \
1097         g_signal_connect(G_OBJECT(gtk_item_factory_from_widget(menu)->accel_group), \
1098                         "accel_activate",                               \
1099                         G_CALLBACK(main_window_accel_activate), mainwin);
1100                          
1101         
1102         ADD_MENU_ACCEL_GROUP_TO_WINDOW(summaryview->popupmenu, mainwin->window);
1103         
1104         /* connect the accelerators for equivalent 
1105            menu items in different menus             */
1106         menu_connect_identical_items();
1107
1108
1109         
1110         /* show main window */
1111         gtk_widget_show(mainwin->window);
1112
1113         /* initialize views */
1114         folderview_init(folderview);
1115         summary_init(summaryview);
1116         messageview_init(messageview);
1117         log_window_init(mainwin->logwin);
1118         log_window_set_clipping(mainwin->logwin, prefs_common.cliplog,
1119                                 prefs_common.loglength);
1120 #ifdef USE_OPENSSL
1121         sslcertwindow_register_hook();
1122 #endif
1123         mainwin->lock_count = 0;
1124         mainwin->menu_lock_count = 0;
1125         mainwin->cursor_count = 0;
1126
1127         mainwin->progressindicator_hook =
1128                 hooks_register_hook(PROGRESSINDICATOR_HOOKLIST, mainwindow_progressindicator_hook, mainwin);
1129
1130         if (!watch_cursor)
1131                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1132
1133         mainwin_list = g_list_append(mainwin_list, mainwin);
1134
1135         /* init work_offline */
1136         if (prefs_common.work_offline)
1137                 online_switch_clicked (GTK_BUTTON(online_switch), mainwin);
1138
1139         return mainwin;
1140 }
1141
1142 void main_window_destroy(MainWindow *mainwin)
1143 {
1144         /* TODO : destroy other component */
1145         messageview_destroy(mainwin->messageview);
1146         mainwin->messageview = NULL;    
1147 }
1148
1149 void main_window_update_actions_menu(MainWindow *mainwin)
1150 {
1151         GtkItemFactory *ifactory;
1152
1153         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1154         action_update_mainwin_menu(ifactory, "/Tools/Actions", mainwin);
1155 }
1156
1157 void main_window_cursor_wait(MainWindow *mainwin)
1158 {
1159
1160         if (mainwin->cursor_count == 0)
1161                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
1162
1163         mainwin->cursor_count++;
1164
1165         gdk_flush();
1166 }
1167
1168 void main_window_cursor_normal(MainWindow *mainwin)
1169 {
1170         if (mainwin->cursor_count)
1171                 mainwin->cursor_count--;
1172
1173         if (mainwin->cursor_count == 0)
1174                 gdk_window_set_cursor(mainwin->window->window, NULL);
1175
1176         gdk_flush();
1177 }
1178
1179 /* lock / unlock the user-interface */
1180 void main_window_lock(MainWindow *mainwin)
1181 {
1182         if (mainwin->lock_count == 0)
1183                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
1184
1185         mainwin->lock_count++;
1186
1187         main_window_set_menu_sensitive(mainwin);
1188         toolbar_main_set_sensitive(mainwin);
1189 }
1190
1191 void main_window_unlock(MainWindow *mainwin)
1192 {
1193         if (mainwin->lock_count)
1194                 mainwin->lock_count--;
1195
1196         main_window_set_menu_sensitive(mainwin);
1197         toolbar_main_set_sensitive(mainwin);
1198
1199         if (mainwin->lock_count == 0)
1200                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
1201 }
1202
1203 static void main_window_menu_callback_block(MainWindow *mainwin)
1204 {
1205         mainwin->menu_lock_count++;
1206 }
1207
1208 static void main_window_menu_callback_unblock(MainWindow *mainwin)
1209 {
1210         if (mainwin->menu_lock_count)
1211                 mainwin->menu_lock_count--;
1212 }
1213
1214 static guint prefs_tag = 0;
1215
1216 void main_window_reflect_prefs_all(void)
1217 {
1218         main_window_reflect_prefs_all_real(FALSE);
1219 }
1220
1221 static gboolean reflect_prefs_timeout_cb(gpointer data) 
1222 {
1223         gboolean pixmap_theme_changed = GPOINTER_TO_INT(data);
1224         GList *cur;
1225         MainWindow *mainwin;
1226         GtkWidget *pixmap;
1227
1228         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1229                 mainwin = (MainWindow *)cur->data;
1230
1231                 main_window_show_cur_account(mainwin);
1232                 main_window_set_menu_sensitive(mainwin);
1233                 toolbar_main_set_sensitive(mainwin);
1234
1235                 /* pixmap themes */
1236                 if (pixmap_theme_changed) {
1237                         toolbar_update(TOOLBAR_MAIN, mainwin);
1238                         messageview_reflect_prefs_pixmap_theme();
1239                         compose_reflect_prefs_pixmap_theme();
1240                         folderview_reflect_prefs_pixmap_theme(mainwin->folderview);
1241                         summary_reflect_prefs_pixmap_theme(mainwin->summaryview);
1242
1243                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_ONLINE);
1244                         gtk_container_remove(GTK_CONTAINER(mainwin->online_switch), 
1245                                              mainwin->online_pixmap);
1246                         gtk_container_add (GTK_CONTAINER(mainwin->online_switch), pixmap);
1247                         gtk_widget_show(pixmap);
1248                         mainwin->online_pixmap = pixmap;
1249                         pixmap = stock_pixmap_widget(mainwin->hbox_stat, STOCK_PIXMAP_OFFLINE);
1250                         gtk_container_remove(GTK_CONTAINER(mainwin->offline_switch), 
1251                                              mainwin->offline_pixmap);
1252                         gtk_container_add (GTK_CONTAINER(mainwin->offline_switch), pixmap);
1253                         gtk_widget_show(pixmap);
1254                         mainwin->offline_pixmap = pixmap;
1255                 }
1256                 
1257                 headerview_set_font(mainwin->messageview->headerview);
1258                 headerview_set_visibility(mainwin->messageview->headerview,
1259                                           prefs_common.display_header_pane);
1260                 textview_reflect_prefs(mainwin->messageview->mimeview->textview);
1261                 folderview_reflect_prefs();
1262                 summary_reflect_prefs();
1263                 summary_redisplay_msg(mainwin->summaryview);
1264         }
1265         prefs_tag = 0;
1266         return FALSE;
1267 }
1268
1269 void main_window_reflect_prefs_all_real(gboolean pixmap_theme_changed)
1270 {
1271         if (prefs_tag == 0 || pixmap_theme_changed) {
1272                 prefs_tag = g_timeout_add(500, reflect_prefs_timeout_cb, 
1273                                                 GINT_TO_POINTER(pixmap_theme_changed));
1274         }
1275 }
1276
1277 void main_window_set_summary_column(void)
1278 {
1279         GList *cur;
1280         MainWindow *mainwin;
1281
1282         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1283                 mainwin = (MainWindow *)cur->data;
1284                 summary_set_column_order(mainwin->summaryview);
1285         }
1286 }
1287
1288 void main_window_set_folder_column(void)
1289 {
1290         GList *cur;
1291         MainWindow *mainwin;
1292
1293         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1294                 mainwin = (MainWindow *)cur->data;
1295                 folderview_set_column_order(mainwin->folderview);
1296         }
1297 }
1298
1299 static void main_window_set_account_selector_menu(MainWindow *mainwin,
1300                                                   GList *account_list)
1301 {
1302         GList *cur_ac, *cur_item;
1303         GtkWidget *menuitem;
1304         PrefsAccount *ac_prefs;
1305
1306         /* destroy all previous menu item */
1307         cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
1308         while (cur_item != NULL) {
1309                 GList *next = cur_item->next;
1310                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1311                 cur_item = next;
1312         }
1313
1314         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1315                 ac_prefs = (PrefsAccount *)cur_ac->data;
1316
1317                 menuitem = gtk_menu_item_new_with_label
1318                         (ac_prefs->account_name
1319                          ? ac_prefs->account_name : _("Untitled"));
1320                 gtk_widget_show(menuitem);
1321                 gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
1322                 g_signal_connect(G_OBJECT(menuitem), "activate",
1323                                  G_CALLBACK(account_selector_menu_cb),
1324                                  ac_prefs);
1325         }
1326 }
1327
1328 static void main_window_set_account_receive_menu(MainWindow *mainwin,
1329                                                  GList *account_list)
1330 {
1331         GList *cur_ac, *cur_item;
1332         GtkWidget *menu;
1333         GtkWidget *menuitem;
1334         PrefsAccount *ac_prefs;
1335
1336         menu = gtk_item_factory_get_widget(mainwin->menu_factory,
1337                                            "/Message/Receive");
1338
1339         /* search for separator */
1340         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1341              cur_item = cur_item->next) {
1342                 if (GTK_BIN(cur_item->data)->child == NULL) {
1343                         cur_item = cur_item->next;
1344                         break;
1345                 }
1346         }
1347
1348         /* destroy all previous menu item */
1349         while (cur_item != NULL) {
1350                 GList *next = cur_item->next;
1351                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
1352                 cur_item = next;
1353         }
1354
1355         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
1356                 ac_prefs = (PrefsAccount *)cur_ac->data;
1357
1358                 menuitem = gtk_menu_item_new_with_label
1359                         (ac_prefs->account_name ? ac_prefs->account_name
1360                          : _("Untitled"));
1361                 gtk_widget_show(menuitem);
1362                 gtk_menu_append(GTK_MENU(menu), menuitem);
1363                 g_signal_connect(G_OBJECT(menuitem), "activate",
1364                                  G_CALLBACK(account_receive_menu_cb),
1365                                  ac_prefs);
1366         }
1367 }
1368
1369 void main_window_set_account_menu(GList *account_list)
1370 {
1371         GList *cur;
1372         MainWindow *mainwin;
1373
1374         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
1375                 mainwin = (MainWindow *)cur->data;
1376                 main_window_set_account_selector_menu(mainwin, account_list);
1377                 main_window_set_account_receive_menu(mainwin, account_list);
1378         }
1379 }
1380
1381 static void main_window_show_cur_account(MainWindow *mainwin)
1382 {
1383         gchar *buf;
1384         gchar *ac_name;
1385
1386         ac_name = g_strdup(cur_account
1387                            ? (cur_account->account_name
1388                               ? cur_account->account_name : _("Untitled"))
1389                            : _("none"));
1390
1391         if (cur_account)
1392                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
1393         else
1394                 buf = g_strdup(PROG_VERSION);
1395         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
1396         g_free(buf);
1397
1398         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
1399         gtk_widget_queue_resize(mainwin->ac_button);
1400
1401         g_free(ac_name);
1402 }
1403
1404 GtkWidget *main_window_get_folder_window(MainWindow *mainwin)
1405 {
1406         switch (mainwin->type) {
1407         case SEPARATE_FOLDER:
1408                 return mainwin->win.sep_folder.folderwin;
1409         case SEPARATE_BOTH:
1410                 return mainwin->win.sep_both.folderwin;
1411         default:
1412                 return NULL;
1413         }
1414 }
1415
1416 GtkWidget *main_window_get_message_window(MainWindow *mainwin)
1417 {
1418         switch (mainwin->type) {
1419         case SEPARATE_MESSAGE:
1420                 return mainwin->win.sep_message.messagewin;
1421         case SEPARATE_BOTH:
1422                 return mainwin->win.sep_both.messagewin;
1423         default:
1424                 return NULL;
1425         }
1426 }
1427
1428 void main_window_separation_change(MainWindow *mainwin, SeparateType type)
1429 {
1430         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
1431         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
1432         /* GtkWidget *message_wid = GTK_WIDGET_PTR(mainwin->messageview); */
1433         GtkWidget *message_wid = mainwin->messageview->vbox;
1434
1435         debug_print("Changing window separation type from %d to %d\n",
1436                     mainwin->type, type);
1437
1438         if (mainwin->type == type) return;
1439
1440         /* remove widgets from those containers */
1441         gtk_widget_ref(folder_wid);
1442         gtk_widget_ref(summary_wid);
1443         gtk_widget_ref(message_wid);
1444         gtkut_container_remove
1445                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1446         gtkut_container_remove
1447                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1448         gtkut_container_remove
1449                 (GTK_CONTAINER(message_wid->parent), message_wid);
1450
1451         /* clean containers */
1452         switch (mainwin->type) {
1453         case SEPARATE_NONE:
1454                 gtk_widget_destroy(mainwin->win.sep_none.hpaned);
1455                 break;
1456         case SEPARATE_FOLDER:
1457                 gtk_widget_destroy(mainwin->win.sep_folder.vpaned);
1458                 gtk_widget_destroy(mainwin->win.sep_folder.folderwin);
1459                 break;
1460         case SEPARATE_MESSAGE:
1461                 gtk_widget_destroy(mainwin->win.sep_message.hpaned);
1462                 gtk_widget_destroy(mainwin->win.sep_message.messagewin);
1463                 break;
1464         case SEPARATE_BOTH:
1465                 gtk_widget_destroy(mainwin->win.sep_both.messagewin);
1466                 gtk_widget_destroy(mainwin->win.sep_both.folderwin);
1467                 break;
1468         }
1469
1470         gtk_widget_hide(mainwin->window);
1471         main_window_set_widgets(mainwin, type);
1472         gtk_widget_show(mainwin->window);
1473
1474         gtk_widget_unref(folder_wid);
1475         gtk_widget_unref(summary_wid);
1476         gtk_widget_unref(message_wid);
1477 }
1478
1479 void main_window_toggle_message_view(MainWindow *mainwin)
1480 {
1481         SummaryView *summaryview = mainwin->summaryview;
1482         union CompositeWin *cwin = &mainwin->win;
1483         GtkWidget *vpaned = NULL;
1484         GtkWidget *container = NULL;
1485         GtkWidget *msgwin = NULL;
1486
1487         switch (mainwin->type) {
1488         case SEPARATE_NONE:
1489                 vpaned = cwin->sep_none.vpaned;
1490                 container = cwin->sep_none.hpaned;
1491                 break;
1492         case SEPARATE_FOLDER:
1493                 vpaned = cwin->sep_folder.vpaned;
1494                 container = mainwin->vbox_body;
1495                 break;
1496         case SEPARATE_MESSAGE:
1497                 msgwin = mainwin->win.sep_message.messagewin;
1498                 break;
1499         case SEPARATE_BOTH:
1500                 msgwin = mainwin->win.sep_both.messagewin;
1501                 break;
1502         }
1503
1504         if (msgwin) {
1505                 if (GTK_WIDGET_VISIBLE(msgwin)) {
1506                         gtk_widget_hide(msgwin);
1507                         mainwin->messageview->visible = FALSE;
1508                         summaryview->displayed = NULL;
1509                 } else {
1510                         gtk_widget_show(msgwin);
1511                         mainwin->messageview->visible = TRUE;
1512                 }
1513         } else if (vpaned->parent != NULL) {
1514                 mainwin->messageview->visible = FALSE;
1515                 summaryview->displayed = NULL;
1516                 gtk_widget_ref(vpaned);
1517                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1518                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), container);
1519         } else {
1520                 mainwin->messageview->visible = TRUE;
1521                 gtk_widget_reparent(GTK_WIDGET_PTR(summaryview), vpaned);
1522                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1523                 gtk_widget_unref(vpaned);
1524         }
1525
1526         if (messageview_is_visible(mainwin->messageview))
1527                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1528                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1529         else
1530                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
1531                               GTK_ARROW_UP, GTK_SHADOW_OUT);
1532
1533         if (mainwin->messageview->visible == FALSE)
1534                 messageview_clear(mainwin->messageview);
1535
1536         main_window_set_menu_sensitive(mainwin);
1537
1538         prefs_common.msgview_visible = mainwin->messageview->visible;
1539
1540         gtk_widget_grab_focus(summaryview->ctree);
1541 }
1542
1543 void main_window_get_size(MainWindow *mainwin)
1544 {
1545         GtkAllocation *allocation;
1546
1547         if (mainwin->messageview == NULL) {
1548                 debug_print("called after messageview "
1549                             "has been deallocated!\n");
1550                 return;
1551         }
1552
1553         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1554
1555         if (allocation->width > 1 && allocation->height > 1) {
1556                 prefs_common.summaryview_width = allocation->width;
1557
1558                 if ((mainwin->type == SEPARATE_NONE ||
1559                      mainwin->type == SEPARATE_FOLDER) &&
1560                     messageview_is_visible(mainwin->messageview))
1561                         prefs_common.summaryview_height = allocation->height;
1562
1563                 prefs_common.mainview_width = allocation->width;
1564         }
1565
1566         allocation = &mainwin->window->allocation;
1567         if (allocation->width > 1 && allocation->height > 1) {
1568                 prefs_common.mainview_height = allocation->height;
1569                 prefs_common.mainwin_width   = allocation->width;
1570                 prefs_common.mainwin_height  = allocation->height;
1571         }
1572
1573         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1574         if (allocation->width > 1 && allocation->height > 1) {
1575                 prefs_common.folderview_width  = allocation->width;
1576                 prefs_common.folderview_height = allocation->height;
1577         }
1578
1579         allocation = &(GTK_WIDGET_PTR(mainwin->messageview)->allocation);
1580         if (allocation->width > 1 && allocation->height > 1) {
1581                 prefs_common.msgview_width = allocation->width;
1582                 prefs_common.msgview_height = allocation->height;
1583         }
1584
1585 /*      debug_print("summaryview size: %d x %d\n",
1586                     prefs_common.summaryview_width,
1587                     prefs_common.summaryview_height);
1588         debug_print("folderview size: %d x %d\n",
1589                     prefs_common.folderview_width,
1590                     prefs_common.folderview_height);
1591         debug_print("messageview size: %d x %d\n",
1592                     prefs_common.msgview_width,
1593                     prefs_common.msgview_height); */
1594 }
1595
1596 void main_window_get_position(MainWindow *mainwin)
1597 {
1598         gint x, y;
1599         GtkWidget *window;
1600
1601         gtkut_widget_get_uposition(mainwin->window, &x, &y);
1602
1603         prefs_common.mainview_x = x;
1604         prefs_common.mainview_y = y;
1605         prefs_common.mainwin_x = x;
1606         prefs_common.mainwin_y = y;
1607
1608         debug_print("main window position: %d, %d\n", x, y);
1609
1610         window = main_window_get_folder_window(mainwin);
1611         if (window) {
1612                 gtkut_widget_get_uposition(window, &x, &y);
1613                 prefs_common.folderwin_x = x;
1614                 prefs_common.folderwin_y = y;
1615                 debug_print("folder window position: %d, %d\n", x, y);
1616         }
1617         window = main_window_get_message_window(mainwin);
1618         if (window) {
1619                 gtkut_widget_get_uposition(window, &x, &y);
1620                 prefs_common.main_msgwin_x = x;
1621                 prefs_common.main_msgwin_y = y;
1622                 debug_print("message window position: %d, %d\n", x, y);
1623         }
1624 }
1625
1626 void main_window_progress_on(MainWindow *mainwin)
1627 {
1628         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1629 }
1630
1631 void main_window_progress_off(MainWindow *mainwin)
1632 {
1633         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
1634         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), "");
1635 }
1636
1637 void main_window_progress_set(MainWindow *mainwin, gint cur, gint total)
1638 {
1639         gchar buf[32];
1640
1641         g_snprintf(buf, sizeof(buf), "%d / %d", cur, total);
1642         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(mainwin->progressbar), buf);
1643         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(mainwin->progressbar),
1644                                 (cur == 0 && total == 0) ? 0 :
1645                                 (gfloat)cur / (gfloat)total);
1646 }
1647
1648 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
1649 {
1650         if (confirm) {
1651                 if (alertpanel(_("Empty trash"),
1652                                _("Delete all messages in trash folders?"),
1653                                GTK_STOCK_YES, GTK_STOCK_NO, NULL)
1654                     != G_ALERTDEFAULT)
1655                         return;
1656                 manage_window_focus_in(mainwin->window, NULL, NULL);
1657         }
1658
1659         procmsg_empty_all_trash();
1660
1661         if (mainwin->summaryview->folder_item &&
1662             mainwin->summaryview->folder_item->stype == F_TRASH)
1663                 gtk_widget_grab_focus(mainwin->folderview->ctree);
1664 }
1665
1666 void main_window_add_mailbox(MainWindow *mainwin)
1667 {
1668         gchar *path;
1669         Folder *folder;
1670
1671         path = input_dialog(_("Add mailbox"),
1672                             _("Input the location of mailbox.\n"
1673                               "If the existing mailbox is specified, it will be\n"
1674                               "scanned automatically."),
1675                             "Mail");
1676         if (!path) return;
1677         if (folder_find_from_path(path)) {
1678                 alertpanel_error(_("The mailbox '%s' already exists."), path);
1679                 g_free(path);
1680                 return;
1681         }
1682         folder = folder_new(folder_get_class_from_string("mh"), 
1683                             !strcmp(path, "Mail") ? _("Mailbox") : 
1684                             g_path_get_basename(path), path);
1685         g_free(path);
1686
1687         if (folder->klass->create_tree(folder) < 0) {
1688                 alertpanel_error(_("Creation of the mailbox failed.\n"
1689                                    "Maybe some files already exist, or you don't have the permission to write there."));
1690                 folder_destroy(folder);
1691                 return;
1692         }
1693
1694         folder_add(folder);
1695         folder_set_ui_func(folder, scan_tree_func, mainwin);
1696         folder_scan_tree(folder);
1697         folder_set_ui_func(folder, NULL, NULL);
1698 }
1699
1700 SensitiveCond main_window_get_current_state(MainWindow *mainwin)
1701 {
1702         SensitiveCond state = 0;
1703         SummarySelection selection;
1704         FolderItem *item = mainwin->summaryview->folder_item;
1705         GList *account_list = account_get_list();
1706         
1707         selection = summary_get_selection_type(mainwin->summaryview);
1708
1709         if (mainwin->lock_count == 0)
1710                 state |= M_UNLOCKED;
1711         if (selection != SUMMARY_NONE)
1712                 state |= M_MSG_EXIST;
1713         if (item && item->path && folder_item_parent(item) && !item->no_select) {
1714                 state |= M_EXEC;
1715                 /*              if (item->folder->type != F_NEWS) */
1716                 state |= M_ALLOW_DELETE;
1717
1718                 if (prefs_common.immediate_exec == 0
1719                     && mainwin->lock_count == 0)
1720                         state |= M_DELAY_EXEC;
1721
1722                 if ((selection == SUMMARY_NONE && item->hide_read_msgs)
1723                     || selection != SUMMARY_NONE)
1724                         state |= M_HIDE_READ_MSG;       
1725         }               
1726         if (mainwin->summaryview->threaded)
1727                 state |= M_THREADED;
1728         else
1729                 state |= M_UNTHREADED;  
1730         if (selection == SUMMARY_SELECTED_SINGLE ||
1731             selection == SUMMARY_SELECTED_MULTIPLE)
1732                 state |= M_TARGET_EXIST;
1733         if (selection == SUMMARY_SELECTED_SINGLE)
1734                 state |= M_SINGLE_TARGET_EXIST;
1735         if (mainwin->summaryview->folder_item &&
1736             mainwin->summaryview->folder_item->folder->klass->type == F_NEWS)
1737                 state |= M_NEWS;
1738         else
1739                 state |= M_NOT_NEWS;
1740         if (selection == SUMMARY_SELECTED_SINGLE &&
1741             (item &&
1742              (folder_has_parent_of_type(item, F_DRAFT) ||
1743               folder_has_parent_of_type(item, F_OUTBOX) ||
1744               folder_has_parent_of_type(item, F_QUEUE))))
1745                 state |= M_ALLOW_REEDIT;
1746         if (cur_account)
1747                 state |= M_HAVE_ACCOUNT;
1748         
1749         for ( ; account_list != NULL; account_list = account_list->next) {
1750                 if (((PrefsAccount*)account_list->data)->protocol == A_NNTP) {
1751                         state |= M_HAVE_NEWS_ACCOUNT;
1752                         break;
1753                 }
1754         }
1755
1756         if (inc_is_active())
1757                 state |= M_INC_ACTIVE;
1758
1759         return state;
1760 }
1761
1762
1763
1764 void main_window_set_menu_sensitive(MainWindow *mainwin)
1765 {
1766         GtkItemFactory *ifactory = mainwin->menu_factory;
1767         SensitiveCond state;
1768         gboolean sensitive;
1769         GtkWidget *menu;
1770         GtkWidget *menuitem;
1771         SummaryView *summaryview;
1772         gchar *menu_path;
1773         gint i;
1774         GList *cur_item;
1775
1776         static const struct {
1777                 gchar *const entry;
1778                 SensitiveCond cond;
1779         } entry[] = {
1780                 {"/File/Add mailbox"                          , M_UNLOCKED},
1781
1782                 {"/File/Add mailbox/MH..."                    , M_UNLOCKED},
1783                 {"/File/Change folder order"                  , M_UNLOCKED},
1784                 {"/File/Export to mbox file..."               , M_UNLOCKED},
1785                 {"/File/Empty all Trash folders"              , M_UNLOCKED},
1786                 {"/File/Work offline"                         , M_UNLOCKED},
1787
1788                 {"/File/Save as...", M_TARGET_EXIST|M_UNLOCKED},
1789                 {"/File/Print..."  , M_TARGET_EXIST|M_UNLOCKED},
1790                 /* {"/File/Close"  , M_UNLOCKED}, */
1791                 {"/File/Exit"      , M_UNLOCKED},
1792
1793                 {"/Edit/Select thread"             , M_SINGLE_TARGET_EXIST},
1794
1795                 {"/View/Sort"                      , M_EXEC},
1796                 {"/View/Thread view"               , M_EXEC},
1797                 {"/View/Expand all threads"        , M_MSG_EXIST},
1798                 {"/View/Collapse all threads"      , M_MSG_EXIST},
1799                 {"/View/Hide read messages"        , M_HIDE_READ_MSG},
1800                 {"/View/Go to/Prev message"        , M_MSG_EXIST},
1801                 {"/View/Go to/Next message"        , M_MSG_EXIST},
1802                 {"/View/Go to/Prev unread message" , M_MSG_EXIST},
1803                 {"/View/Go to/Prev new message"    , M_MSG_EXIST},
1804                 {"/View/Go to/Prev marked message" , M_MSG_EXIST},
1805                 {"/View/Go to/Next marked message" , M_MSG_EXIST},
1806                 {"/View/Go to/Prev labeled message", M_MSG_EXIST},
1807                 {"/View/Go to/Next labeled message", M_MSG_EXIST},
1808                 {"/View/Open in new window"        , M_SINGLE_TARGET_EXIST},
1809                 {"/View/Show all headers"          , M_SINGLE_TARGET_EXIST},
1810                 {"/View/Message source"            , M_SINGLE_TARGET_EXIST},
1811
1812                 {"/Message/Receive/Get from current account"
1813                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1814                 {"/Message/Receive/Get from all accounts"
1815                                                  , M_HAVE_ACCOUNT|M_UNLOCKED},
1816                 {"/Message/Receive/Cancel receiving"
1817                                                  , M_INC_ACTIVE},
1818
1819                 {"/Message/Compose a news message", M_HAVE_NEWS_ACCOUNT},
1820                 {"/Message/Reply"                 , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1821                 {"/Message/Reply to"              , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1822                 {"/Message/Follow-up and reply to", M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST|M_NEWS},
1823                 {"/Message/Forward"               , M_HAVE_ACCOUNT|M_TARGET_EXIST},
1824                 {"/Message/Redirect"              , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1825                 {"/Message/Move..."               , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1826                 {"/Message/Copy..."               , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1827                 {"/Message/Move to trash"         , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1828                 {"/Message/Delete..."             , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NOT_NEWS},
1829                 {"/Message/Cancel a news message" , M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED|M_NEWS},
1830                 {"/Message/Mark"                  , M_TARGET_EXIST},
1831                 {"/Message/Re-edit"               , M_HAVE_ACCOUNT|M_ALLOW_REEDIT},
1832
1833                 {"/Tools/Add sender to address book"   , M_SINGLE_TARGET_EXIST},
1834                 {"/Tools/Harvest addresses"            , M_UNLOCKED},
1835                 {"/Tools/Harvest addresses/from Folder..."       
1836                                                        , M_UNLOCKED|M_MSG_EXIST},
1837                 {"/Tools/Harvest addresses/from Messages..."
1838                                                        , M_UNLOCKED|M_MSG_EXIST|M_TARGET_EXIST},
1839                 {"/Tools/Harvest addresses"            , M_UNLOCKED},
1840                 {"/Tools/Filter all messages in folder", M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1841                 {"/Tools/Filter selected messages"     , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1842                 {"/Tools/Create filter rule"           , M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1843                 {"/Tools/Actions"                      , M_TARGET_EXIST|M_UNLOCKED},
1844                 {"/Tools/Execute"                      , M_DELAY_EXEC},
1845                 {"/Tools/Delete duplicated messages/In selected folder"   , M_MSG_EXIST|M_ALLOW_DELETE|M_UNLOCKED},
1846
1847                 {"/Configuration", M_UNLOCKED},
1848
1849                 {NULL, 0}
1850         };
1851
1852         state = main_window_get_current_state(mainwin);
1853
1854         for (i = 0; entry[i].entry != NULL; i++) {
1855                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1856                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1857         }
1858
1859         menu = gtk_item_factory_get_widget(ifactory, "/Message/Receive");
1860
1861         /* search for separator */
1862         for (cur_item = GTK_MENU_SHELL(menu)->children; cur_item != NULL;
1863              cur_item = cur_item->next) {
1864                 if (GTK_BIN(cur_item->data)->child == NULL) {
1865                         cur_item = cur_item->next;
1866                         break;
1867                 }
1868         }
1869
1870         for (; cur_item != NULL; cur_item = cur_item->next) {
1871                 gtk_widget_set_sensitive(GTK_WIDGET(cur_item->data),
1872                                          (M_UNLOCKED & state) != 0);
1873         }
1874
1875         main_window_menu_callback_block(mainwin);
1876
1877 #define SET_CHECK_MENU_ACTIVE(path, active) \
1878 { \
1879         menuitem = gtk_item_factory_get_widget(ifactory, path); \
1880         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
1881 }
1882
1883         SET_CHECK_MENU_ACTIVE("/View/Show or hide/Message view",
1884                               messageview_is_visible(mainwin->messageview));
1885
1886         summaryview = mainwin->summaryview;
1887         menu_path = "/View/Sort/Don't sort";
1888
1889         switch (summaryview->sort_key) {
1890         case SORT_BY_NUMBER:
1891                 menu_path = "/View/Sort/by number"; break;
1892         case SORT_BY_SIZE:
1893                 menu_path = "/View/Sort/by size"; break;
1894         case SORT_BY_DATE:
1895                 menu_path = "/View/Sort/by date"; break;
1896         case SORT_BY_FROM:
1897                 menu_path = "/View/Sort/by from"; break;
1898         case SORT_BY_TO:
1899                 menu_path = "/View/Sort/by recipient"; break;
1900         case SORT_BY_SUBJECT:
1901                 menu_path = "/View/Sort/by subject"; break;
1902         case SORT_BY_LABEL:
1903                 menu_path = "/View/Sort/by color label"; break;
1904         case SORT_BY_MARK:
1905                 menu_path = "/View/Sort/by mark"; break;
1906         case SORT_BY_STATUS:
1907                 menu_path = "/View/Sort/by status"; break;
1908         case SORT_BY_MIME:
1909                 menu_path = "/View/Sort/by attachment"; break;
1910         case SORT_BY_SCORE:
1911                 menu_path = "/View/Sort/by score"; break;
1912         case SORT_BY_LOCKED:
1913                 menu_path = "/View/Sort/by locked"; break;
1914         case SORT_BY_NONE:
1915         default:
1916                 menu_path = "/View/Sort/Don't sort"; break;
1917         }
1918         SET_CHECK_MENU_ACTIVE(menu_path, TRUE);
1919
1920         if (summaryview->sort_type == SORT_ASCENDING) {
1921                 SET_CHECK_MENU_ACTIVE("/View/Sort/Ascending", TRUE);
1922         } else {
1923                 SET_CHECK_MENU_ACTIVE("/View/Sort/Descending", TRUE);
1924         }
1925
1926         if (summaryview->sort_key != SORT_BY_NONE) {
1927                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", TRUE);
1928                 menu_set_sensitive(ifactory, "/View/Sort/Descending", TRUE);
1929         } else {
1930                 menu_set_sensitive(ifactory, "/View/Sort/Ascending", FALSE);
1931                 menu_set_sensitive(ifactory, "/View/Sort/Descending", FALSE);
1932         }
1933
1934         SET_CHECK_MENU_ACTIVE("/View/Show all headers",
1935                               mainwin->messageview->mimeview->textview->show_all_headers);
1936         SET_CHECK_MENU_ACTIVE("/View/Thread view", (state & M_THREADED) != 0);
1937
1938 #undef SET_CHECK_MENU_ACTIVE
1939
1940         main_window_menu_callback_unblock(mainwin);
1941 }
1942
1943 void main_window_popup(MainWindow *mainwin)
1944 {
1945         gtkut_window_popup(mainwin->window);
1946
1947         switch (mainwin->type) {
1948         case SEPARATE_FOLDER:
1949                 gtkut_window_popup(mainwin->win.sep_folder.folderwin);
1950                 break;
1951         case SEPARATE_MESSAGE:
1952                 gtkut_window_popup(mainwin->win.sep_message.messagewin);
1953                 break;
1954         case SEPARATE_BOTH:
1955                 gtkut_window_popup(mainwin->win.sep_both.folderwin);
1956                 gtkut_window_popup(mainwin->win.sep_both.messagewin);
1957                 break;
1958         default:
1959                 break;
1960         }
1961 }
1962
1963 void main_window_show(MainWindow *mainwin)
1964 {
1965         gtk_widget_show(mainwin->window);
1966         gtk_widget_show(mainwin->vbox_body);
1967
1968         gtk_widget_set_uposition(mainwin->window,
1969                                  prefs_common.mainwin_x,
1970                                  prefs_common.mainwin_y);
1971
1972         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
1973                              prefs_common.folderview_width,
1974                              prefs_common.folderview_height);
1975         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
1976                              prefs_common.summaryview_width,
1977                              prefs_common.summaryview_height);
1978         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
1979                              prefs_common.msgview_width,
1980                              prefs_common.msgview_height);
1981
1982         if (mainwin->type & SEPARATE_FOLDER) {
1983                 GtkWidget *folderwin;
1984
1985                 folderwin = main_window_get_folder_window(mainwin);
1986                 gtk_widget_show(folderwin);
1987                 gtk_widget_set_uposition(folderwin, prefs_common.folderwin_x,
1988                                          prefs_common.folderwin_y);
1989         }
1990
1991         if (mainwin->type & SEPARATE_MESSAGE) {
1992                 GtkWidget *messagewin;
1993
1994                 messagewin = main_window_get_message_window(mainwin);
1995                 gtk_widget_show(messagewin);
1996                 gtk_widget_set_uposition(messagewin, prefs_common.main_msgwin_x,
1997                                          prefs_common.main_msgwin_y);
1998         }
1999 }
2000
2001 void main_window_hide(MainWindow *mainwin)
2002 {
2003         main_window_get_size(mainwin);
2004         main_window_get_position(mainwin);
2005
2006         gtk_widget_hide(mainwin->window);
2007         gtk_widget_hide(mainwin->vbox_body);
2008
2009         if (mainwin->type & SEPARATE_FOLDER) {
2010                 gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2011         }
2012
2013         if (mainwin->type & SEPARATE_MESSAGE) {
2014                 gtk_widget_hide(mainwin->win.sep_message.messagewin);
2015         }
2016 }
2017
2018 static void main_window_set_widgets(MainWindow *mainwin, SeparateType type)
2019 {
2020         GtkWidget *folderwin = NULL;
2021         GtkWidget *messagewin = NULL;
2022         GtkWidget *hpaned;
2023         GtkWidget *vpaned;
2024         GtkWidget *vbox_body = mainwin->vbox_body;
2025         GtkItemFactory *ifactory = mainwin->menu_factory;
2026         GtkWidget *menuitem;
2027         GtkItemFactory *msgview_ifactory;
2028
2029         debug_print("Setting widgets... ");
2030
2031         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->folderview),
2032                                     prefs_common.folderview_width,
2033                                     prefs_common.folderview_height);
2034         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->summaryview),
2035                                     prefs_common.summaryview_width,
2036                                     prefs_common.summaryview_height);
2037         gtk_widget_set_size_request(GTK_WIDGET_PTR(mainwin->messageview),
2038                                     prefs_common.msgview_width,
2039                                     prefs_common.msgview_height);
2040
2041         /* create separated window(s) if needed */
2042         if (type & SEPARATE_FOLDER) {
2043                 static GdkGeometry folderwin_geometry;
2044                 
2045                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2046                 gtk_window_set_title(GTK_WINDOW(folderwin),
2047                                      _("Sylpheed - Folder View"));
2048
2049                 gtk_window_move(GTK_WINDOW(folderwin),
2050                                 prefs_common.folderwin_x,
2051                                 prefs_common.folderwin_y);
2052
2053                 if (!folderwin_geometry.min_height) {
2054                         folderwin_geometry.min_width = 320;
2055                         folderwin_geometry.min_height = 200;
2056                 }
2057                 gtk_window_set_geometry_hints(GTK_WINDOW(folderwin), NULL,
2058                                               &folderwin_geometry, GDK_HINT_MIN_SIZE);
2059                                 
2060                 gtk_widget_set_size_request(folderwin,
2061                                             prefs_common.folderview_width,
2062                                             prefs_common.folderview_height);
2063
2064                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
2065                                                BORDER_WIDTH);
2066
2067                 g_signal_connect(G_OBJECT(folderwin), "delete_event",
2068                                  G_CALLBACK(folder_window_close_cb),
2069                                    mainwin);
2070                 gtk_container_add(GTK_CONTAINER(folderwin),
2071                                   GTK_WIDGET_PTR(mainwin->folderview));
2072                 gtk_widget_realize(folderwin);
2073
2074                 if (prefs_common.folderview_visible)
2075                         gtk_widget_show(folderwin);
2076         }
2077
2078         if (type & SEPARATE_MESSAGE) {
2079                 static GdkGeometry msgwin_geometry;
2080                 
2081                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2082                 gtk_window_set_title(GTK_WINDOW(messagewin),
2083                                      _("Sylpheed - Message View"));
2084                                      
2085                 gtk_window_move(GTK_WINDOW(messagewin), 
2086                                 prefs_common.main_msgwin_x,
2087                                 prefs_common.main_msgwin_y);
2088
2089                 if (!msgwin_geometry.min_height) {
2090                         msgwin_geometry.min_width = 320;
2091                         msgwin_geometry.min_height = 200;
2092                 }
2093                 gtk_window_set_geometry_hints(GTK_WINDOW(messagewin), NULL,
2094                                               &msgwin_geometry, GDK_HINT_MIN_SIZE);
2095                 
2096                 gtk_widget_set_size_request(messagewin, 
2097                                             prefs_common.msgwin_width,
2098                                             prefs_common.msgwin_height);
2099
2100                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
2101                                                BORDER_WIDTH);
2102                 g_signal_connect(G_OBJECT(messagewin), "delete_event",
2103                                  G_CALLBACK(message_window_close_cb),
2104                                  mainwin);
2105                 if (messageview_is_visible(mainwin->messageview))
2106                         gtk_widget_show(messagewin);
2107         } else {
2108                 mainwin->messageview->statusbar = mainwin->statusbar;
2109                 mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2110         }
2111
2112         switch (type) {
2113         case SEPARATE_NONE:
2114                 hpaned = gtk_hpaned_new();
2115                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2116                 gtk_paned_add1(GTK_PANED(hpaned),
2117                                GTK_WIDGET_PTR(mainwin->folderview));
2118                 gtk_widget_show(hpaned);
2119                 gtk_widget_queue_resize(hpaned);
2120
2121                 vpaned = gtk_vpaned_new();
2122                 if (messageview_is_visible(mainwin->messageview)) {
2123                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
2124                         gtk_paned_add1(GTK_PANED(vpaned),
2125                                        GTK_WIDGET_PTR(mainwin->summaryview));
2126                 } else {
2127                         gtk_paned_add2(GTK_PANED(hpaned),
2128                                        GTK_WIDGET_PTR(mainwin->summaryview));
2129                         gtk_widget_ref(vpaned);
2130                 }
2131                 gtk_paned_add2(GTK_PANED(vpaned),
2132                                GTK_WIDGET_PTR(mainwin->messageview));
2133                 gtk_widget_show(vpaned);
2134                 gtk_widget_queue_resize(vpaned);
2135
2136                 mainwin->win.sep_none.hpaned = hpaned;
2137                 mainwin->win.sep_none.vpaned = vpaned;
2138                 
2139                 /* remove headerview if not in prefs */
2140                 headerview_set_visibility(mainwin->messageview->headerview,
2141                                           prefs_common.display_header_pane);
2142                 break;
2143         case SEPARATE_FOLDER:
2144                 vpaned = gtk_vpaned_new();
2145                 if (messageview_is_visible(mainwin->messageview)) {
2146                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
2147                                            TRUE, TRUE, 0);
2148                         gtk_paned_add1(GTK_PANED(vpaned),
2149                                        GTK_WIDGET_PTR(mainwin->summaryview));
2150                 } else {
2151                         gtk_box_pack_start(GTK_BOX(vbox_body),
2152                                            GTK_WIDGET_PTR(mainwin->summaryview),
2153                                            TRUE, TRUE, 0);
2154                         gtk_widget_ref(vpaned);
2155                 }
2156                 gtk_paned_add2(GTK_PANED(vpaned),
2157                                GTK_WIDGET_PTR(mainwin->messageview));
2158                 gtk_widget_show(vpaned);
2159                 gtk_widget_queue_resize(vpaned);
2160
2161                 mainwin->win.sep_folder.folderwin = folderwin;
2162                 mainwin->win.sep_folder.vpaned    = vpaned;
2163
2164                 /* remove headerview if not in prefs */
2165                 headerview_set_visibility(mainwin->messageview->headerview,
2166                                           prefs_common.display_header_pane);
2167                 
2168                 break;
2169         case SEPARATE_MESSAGE:
2170                 hpaned = gtk_hpaned_new();
2171                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
2172                 gtk_paned_add1(GTK_PANED(hpaned),
2173                                GTK_WIDGET_PTR(mainwin->folderview));
2174                 gtk_paned_add2(GTK_PANED(hpaned),
2175                                GTK_WIDGET_PTR(mainwin->summaryview));
2176                 gtk_widget_show(hpaned);
2177                 gtk_widget_queue_resize(hpaned);
2178                 messageview_add_toolbar(mainwin->messageview, messagewin);
2179                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2180                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2181
2182                 mainwin->win.sep_message.messagewin = messagewin;
2183                 mainwin->win.sep_message.hpaned     = hpaned;
2184
2185                 gtk_widget_realize(messagewin);
2186                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2187                 gtk_widget_show_all(messagewin);
2188                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2189                                   mainwin->messageview->handlebox, 
2190                                   prefs_common.toolbar_style);
2191
2192                 break;
2193         case SEPARATE_BOTH:
2194                 messageview_add_toolbar(mainwin->messageview, messagewin);
2195                 msgview_ifactory = gtk_item_factory_from_widget(mainwin->messageview->menubar);
2196                 menu_set_sensitive(msgview_ifactory, "/File/Close", FALSE);
2197
2198                 gtk_box_pack_start(GTK_BOX(vbox_body),
2199                                    GTK_WIDGET_PTR(mainwin->summaryview),
2200                                    TRUE, TRUE, 0);
2201                 
2202                 mainwin->win.sep_both.folderwin = folderwin;
2203                 mainwin->win.sep_both.messagewin = messagewin;
2204                 
2205                 gtk_widget_realize(messagewin);
2206                 gtk_widget_show_all(GTK_WIDGET_PTR(mainwin->messageview));
2207                 gtk_widget_show_all(messagewin);
2208                 toolbar_set_style(mainwin->messageview->toolbar->toolbar, 
2209                                   mainwin->messageview->handlebox, 
2210                                   prefs_common.toolbar_style);          
2211
2212                 break;
2213         }
2214
2215         if (messageview_is_visible(mainwin->messageview))
2216                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2217                               GTK_ARROW_DOWN, GTK_SHADOW_OUT);
2218         else
2219                 gtk_arrow_set(GTK_ARROW(mainwin->summaryview->toggle_arrow),
2220                               GTK_ARROW_UP, GTK_SHADOW_OUT);
2221
2222         gtk_window_move(GTK_WINDOW(mainwin->window),
2223                         prefs_common.mainwin_x,
2224                         prefs_common.mainwin_y);
2225
2226         gtk_widget_queue_resize(vbox_body);
2227         gtk_widget_queue_resize(mainwin->vbox);
2228         gtk_widget_queue_resize(mainwin->window);
2229         /* CLAWS: previous "gtk_widget_show_all" makes noticeview
2230          * and mimeview icon list/ctree lose track of their visibility states */
2231         if (!noticeview_is_visible(mainwin->messageview->noticeview)) 
2232                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->noticeview));
2233         if (!noticeview_is_visible(mainwin->messageview->mimeview->siginfoview)) 
2234                 gtk_widget_hide(GTK_WIDGET_PTR(mainwin->messageview->mimeview->siginfoview));
2235         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mainwin->messageview->mimeview->mime_toggle)))
2236                 gtk_widget_hide(mainwin->messageview->mimeview->icon_mainbox);
2237         else 
2238                 gtk_widget_hide(mainwin->messageview->mimeview->ctree_mainbox);
2239
2240         mainwin->type = type;
2241
2242
2243         /* toggle menu state */
2244         menuitem = gtk_item_factory_get_item
2245                 (ifactory, "/View/Show or hide/Folder tree");
2246         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2247                                        (type & SEPARATE_FOLDER) == 0 ? TRUE :
2248                                        prefs_common.folderview_visible);
2249         gtk_widget_set_sensitive(menuitem, ((type & SEPARATE_FOLDER) != 0));
2250         menuitem = gtk_item_factory_get_item
2251                 (ifactory, "/View/Show or hide/Message view");
2252         gtk_check_menu_item_set_active
2253                 (GTK_CHECK_MENU_ITEM(menuitem),
2254                  messageview_is_visible(mainwin->messageview));
2255
2256         menuitem = gtk_item_factory_get_item
2257                 (ifactory, "/View/Separate folder tree");
2258         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2259                                        ((type & SEPARATE_FOLDER) != 0));
2260         menuitem = gtk_item_factory_get_item
2261                 (ifactory, "/View/Separate message view");
2262         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2263                                        ((type & SEPARATE_MESSAGE) != 0));
2264
2265         if (folderwin) {
2266                 g_signal_connect
2267                         (G_OBJECT(folderwin), "size_allocate",
2268                          G_CALLBACK(folder_window_size_allocate_cb),
2269                          mainwin);
2270         }
2271         if (messagewin) {
2272                 g_signal_connect
2273                         (G_OBJECT(messagewin), "size_allocate",
2274                          G_CALLBACK(message_window_size_allocate_cb),
2275                          mainwin);
2276         }
2277
2278         debug_print("done.\n");
2279 }
2280
2281 void main_window_destroy_all(void)
2282 {
2283         while (mainwin_list != NULL) {
2284                 MainWindow *mainwin = (MainWindow*)mainwin_list->data;
2285                 
2286                 /* free toolbar stuff */
2287                 toolbar_clear_list(TOOLBAR_MAIN);
2288                 TOOLBAR_DESTROY_ACTIONS(mainwin->toolbar->action_list);
2289                 TOOLBAR_DESTROY_ITEMS(mainwin->toolbar->item_list);
2290
2291                 g_free(mainwin->toolbar);
2292                 g_free(mainwin);
2293                 
2294                 mainwin_list = g_list_remove(mainwin_list, mainwin);
2295         }
2296         g_list_free(mainwin_list);
2297 }
2298
2299 #if 0
2300 static gboolean toolbar_account_button_pressed(GtkWidget *widget,
2301                                                GdkEventButton *event,
2302                                                gpointer data)
2303 {
2304         MainWindow *mainwin = (MainWindow *)data;
2305
2306         if (!event) return FALSE;
2307         if (event->button != 3) return FALSE;
2308
2309         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2310         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2311                           widget);
2312
2313         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2314                        menu_button_position, widget,
2315                        event->button, event->time);
2316
2317         return FALSE;
2318 }
2319 #endif
2320
2321 static void toolbar_child_attached(GtkWidget *widget, GtkWidget *child,
2322                                    gpointer data)
2323 {
2324         gtk_widget_set_size_request(child, 1, -1);
2325 }
2326
2327 static void toolbar_child_detached(GtkWidget *widget, GtkWidget *child,
2328                                    gpointer data)
2329 {
2330         gtk_widget_set_size_request(child, -1, -1);
2331 }
2332
2333 static gboolean ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2334                                     gpointer data)
2335 {
2336         MainWindow *mainwin = (MainWindow *)data;
2337
2338         if (!event) return FALSE;
2339
2340         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2341         g_object_set_data(G_OBJECT(mainwin->ac_menu), "menu_button",
2342                           widget);
2343
2344         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2345                        menu_button_position, widget,
2346                        event->button, event->time);
2347
2348         return TRUE;
2349 }
2350
2351 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2352                                  gpointer data)
2353 {
2354         MainWindow *mainwin = (MainWindow *)data;
2355
2356         if (mainwin->lock_count == 0)
2357                 app_exit_cb(data, 0, widget);
2358
2359         return TRUE;
2360 }
2361
2362 static gint folder_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2363                                    gpointer data)
2364 {
2365         MainWindow *mainwin = (MainWindow *)data;
2366         GtkWidget *menuitem;
2367
2368         menuitem = gtk_item_factory_get_item
2369                 (mainwin->menu_factory, "/View/Show or hide/Folder tree");
2370         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2371
2372         return TRUE;
2373 }
2374
2375 static gint message_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2376                                     gpointer data)
2377 {
2378         MainWindow *mainwin = (MainWindow *)data;
2379         GtkWidget *menuitem;
2380
2381         menuitem = gtk_item_factory_get_item
2382                 (mainwin->menu_factory, "/View/Show or hide/Message view");
2383         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2384
2385         mainwin->messageview->statusbar = mainwin->statusbar;
2386         mainwin->messageview->statusbar_cid = mainwin->messageview_cid;
2387
2388         return TRUE;
2389 }
2390
2391 static void main_window_size_allocate_cb(GtkWidget *widget,
2392                                          GtkAllocation *allocation,
2393                                          gpointer data)
2394 {
2395         MainWindow *mainwin = (MainWindow *)data;
2396
2397         main_window_get_size(mainwin);
2398 }
2399
2400 static void folder_window_size_allocate_cb(GtkWidget *widget,
2401                                            GtkAllocation *allocation,
2402                                            gpointer data)
2403 {
2404         MainWindow *mainwin = (MainWindow *)data;
2405
2406         main_window_get_size(mainwin);
2407 }
2408
2409 static void message_window_size_allocate_cb(GtkWidget *widget,
2410                                             GtkAllocation *allocation,
2411                                             gpointer data)
2412 {
2413         MainWindow *mainwin = (MainWindow *)data;
2414
2415         main_window_get_size(mainwin);
2416 }
2417
2418 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2419                            GtkWidget *widget)
2420 {
2421         main_window_add_mailbox(mainwin);
2422 }
2423
2424 static void update_folderview_cb(MainWindow *mainwin, guint action,
2425                                  GtkWidget *widget)
2426 {
2427         summary_show(mainwin->summaryview, NULL);
2428         folderview_check_new_all();
2429 }
2430
2431 static void foldersort_cb(MainWindow *mainwin, guint action,
2432                            GtkWidget *widget)
2433 {
2434         foldersort_open();
2435 }
2436
2437 static void import_mbox_cb(MainWindow *mainwin, guint action,
2438                            GtkWidget *widget)
2439 {
2440         import_mbox(mainwin->summaryview->folder_item);
2441 }
2442
2443 static void export_mbox_cb(MainWindow *mainwin, guint action,
2444                            GtkWidget *widget)
2445 {
2446         export_mbox(mainwin->summaryview->folder_item);
2447 }
2448
2449 static void export_list_mbox_cb(MainWindow *mainwin, guint action,
2450                                 GtkWidget *widget)
2451 {
2452         summaryview_export_mbox_list(mainwin->summaryview);
2453 }
2454
2455 static void empty_trash_cb(MainWindow *mainwin, guint action,
2456                            GtkWidget *widget)
2457 {
2458         main_window_empty_trash(mainwin, TRUE);
2459 }
2460
2461 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2462 {
2463         summary_save_as(mainwin->summaryview);
2464 }
2465
2466 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2467 {
2468         summary_print(mainwin->summaryview);
2469 }
2470
2471 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2472 {
2473         if (prefs_common.confirm_on_exit) {
2474                 if (alertpanel(_("Exit"), _("Exit this program?"),
2475                                GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
2476                     != G_ALERTDEFAULT)
2477                         return;
2478                 manage_window_focus_in(mainwin->window, NULL, NULL);
2479         }
2480
2481         app_will_exit(widget, mainwin);
2482 }
2483
2484 static void search_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2485 {
2486         if (action == 1)
2487                 summary_search(mainwin->summaryview);
2488         else
2489                 message_search(mainwin->messageview);
2490 }
2491
2492 static void mainwindow_quicksearch(MainWindow *mainwin, guint action, GtkWidget *widget)
2493 {
2494         prefs_common.show_searchbar = TRUE;
2495         quicksearch_show(mainwin->summaryview->quicksearch);
2496 }
2497
2498 static void toggle_folder_cb(MainWindow *mainwin, guint action,
2499                              GtkWidget *widget)
2500 {
2501         gboolean active;
2502
2503         active = GTK_CHECK_MENU_ITEM(widget)->active;
2504
2505         switch (mainwin->type) {
2506         case SEPARATE_NONE:
2507         case SEPARATE_MESSAGE:
2508 #if 0
2509                 if (active)
2510                         gtk_widget_show(GTK_WIDGET_PTR(mainwin->folderview));
2511                 else
2512                         gtk_widget_hide(GTK_WIDGET_PTR(mainwin->folderview));
2513 #endif
2514                 break;
2515         case SEPARATE_FOLDER:
2516                 debug_print("separate folder\n");
2517                 if (active)
2518                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
2519                 else
2520                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2521                 break;
2522         case SEPARATE_BOTH:
2523                 if (active)
2524                         gtk_widget_show(mainwin->win.sep_both.folderwin);
2525                 else
2526                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
2527                 break;
2528         }
2529
2530         prefs_common.folderview_visible = active;
2531 }
2532
2533 static void toggle_message_cb(MainWindow *mainwin, guint action,
2534                               GtkWidget *widget)
2535 {
2536         gboolean active;
2537
2538         active = GTK_CHECK_MENU_ITEM(widget)->active;
2539
2540         if (active != messageview_is_visible(mainwin->messageview))
2541                 summary_toggle_view(mainwin->summaryview);
2542 }
2543
2544 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2545                               GtkWidget *widget)
2546 {
2547         toolbar_toggle(action, mainwin);
2548 }
2549
2550 void main_window_reply_cb(MainWindow *mainwin, guint action,
2551                           GtkWidget *widget)
2552 {
2553         MessageView *msgview = (MessageView*)mainwin->messageview;
2554         GSList *msginfo_list = NULL;
2555
2556         g_return_if_fail(msgview != NULL);
2557
2558         msginfo_list = summary_get_selection(mainwin->summaryview);
2559         g_return_if_fail(msginfo_list != NULL);
2560         compose_reply_from_messageview(msgview, msginfo_list, action);
2561         g_slist_free(msginfo_list);
2562 }
2563
2564
2565 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2566                                 GtkWidget *widget)
2567 {
2568         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2569                 gtk_widget_show(mainwin->hbox_stat);
2570                 prefs_common.show_statusbar = TRUE;
2571         } else {
2572                 gtk_widget_hide(mainwin->hbox_stat);
2573                 prefs_common.show_statusbar = FALSE;
2574         }
2575 }
2576
2577 static void separate_widget_cb(MainWindow *mainwin, guint action,
2578                                GtkWidget *widget)
2579 {
2580         SeparateType type;
2581
2582         if (GTK_CHECK_MENU_ITEM(widget)->active)
2583                 type = mainwin->type | action;
2584         else
2585                 type = mainwin->type & ~action;
2586
2587         main_window_separation_change(mainwin, type);
2588
2589         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
2590         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
2591 }
2592
2593 void main_window_toggle_work_offline (MainWindow *mainwin, gboolean offline)
2594 {
2595         if (offline)
2596                 online_switch_clicked (GTK_BUTTON(mainwin->online_switch), mainwin);
2597         else
2598                 online_switch_clicked (GTK_BUTTON(mainwin->offline_switch), mainwin);
2599 }
2600
2601 static void toggle_work_offline_cb (MainWindow *mainwin, guint action, GtkWidget *widget)
2602 {
2603         main_window_toggle_work_offline(mainwin, GTK_CHECK_MENU_ITEM(widget)->active);
2604 }
2605
2606 static void online_switch_clicked (GtkButton *btn, gpointer data) 
2607 {
2608         MainWindow *mainwin;
2609         GtkItemFactory *ifactory;
2610         GtkCheckMenuItem *menuitem;
2611
2612         mainwin = (MainWindow *) data;
2613         
2614         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2615         menuitem = GTK_CHECK_MENU_ITEM (gtk_item_factory_get_widget(ifactory, "/File/Work offline"));
2616         
2617         g_return_if_fail(mainwin != NULL);
2618         g_return_if_fail(menuitem != NULL);
2619         
2620         if (btn == GTK_BUTTON(mainwin->online_switch)) {
2621                 /* go offline */
2622                 gtk_widget_hide (mainwin->online_switch);
2623                 gtk_widget_show (mainwin->offline_switch);
2624                 menuitem->active = TRUE;
2625                 prefs_common.work_offline = TRUE;
2626                 inc_autocheck_timer_remove();           
2627         } else {
2628                 /*go online */
2629                 gtk_widget_hide (mainwin->offline_switch);
2630                 gtk_widget_show (mainwin->online_switch);
2631                 menuitem->active = FALSE;
2632                 prefs_common.work_offline = FALSE;
2633                 inc_autocheck_timer_set();
2634         }
2635 }
2636
2637 static void addressbook_open_cb(MainWindow *mainwin, guint action,
2638                                 GtkWidget *widget)
2639 {
2640         addressbook_open(NULL);
2641 }
2642
2643 static void log_window_show_cb(MainWindow *mainwin, guint action,
2644                                GtkWidget *widget)
2645 {
2646         log_window_show(mainwin->logwin);
2647 }
2648
2649 static void inc_cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2650 {
2651         inc_cancel_all();
2652 }
2653
2654 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2655 {
2656         summary_move_to(mainwin->summaryview);
2657 }
2658
2659 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2660 {
2661         summary_copy_to(mainwin->summaryview);
2662 }
2663
2664 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2665 {
2666         summary_delete(mainwin->summaryview);
2667 }
2668
2669 static void delete_trash_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2670 {
2671         summary_delete_trash(mainwin->summaryview);
2672 }
2673
2674 static void cancel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2675 {
2676         summary_cancel(mainwin->summaryview);
2677 }
2678
2679 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2680 {
2681         summary_open_msg(mainwin->summaryview);
2682 }
2683
2684 static void view_source_cb(MainWindow *mainwin, guint action,
2685                            GtkWidget *widget)
2686 {
2687         summary_view_source(mainwin->summaryview);
2688 }
2689
2690 static void show_all_header_cb(MainWindow *mainwin, guint action,
2691                                GtkWidget *widget)
2692 {
2693         if (mainwin->menu_lock_count) return;
2694         mainwin->summaryview->messageview->all_headers = 
2695                         GTK_CHECK_MENU_ITEM(widget)->active;
2696         summary_display_msg_selected(mainwin->summaryview,
2697                                      GTK_CHECK_MENU_ITEM(widget)->active);
2698 }
2699
2700 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2701 {
2702         summary_mark(mainwin->summaryview);
2703 }
2704
2705 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2706 {
2707         summary_unmark(mainwin->summaryview);
2708 }
2709
2710 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
2711                               GtkWidget *widget)
2712 {
2713         summary_mark_as_unread(mainwin->summaryview);
2714 }
2715
2716 static void mark_as_read_cb(MainWindow *mainwin, guint action,
2717                             GtkWidget *widget)
2718 {
2719         summary_mark_as_read(mainwin->summaryview);
2720 }
2721
2722 static void mark_all_read_cb(MainWindow *mainwin, guint action,
2723                              GtkWidget *widget)
2724 {
2725         summary_mark_all_read(mainwin->summaryview);
2726 }
2727
2728 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2729 {
2730         summary_reedit(mainwin->summaryview);
2731 }
2732
2733 static void add_address_cb(MainWindow *mainwin, guint action,
2734                            GtkWidget *widget)
2735 {
2736         summary_add_address(mainwin->summaryview);
2737 }
2738
2739 static void set_charset_cb(MainWindow *mainwin, guint action,
2740                            GtkWidget *widget)
2741 {
2742         const gchar *str;
2743
2744         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2745                 str = conv_get_charset_str((CharSet)action);
2746                 
2747                 g_free(mainwin->messageview->forced_charset);
2748                 mainwin->messageview->forced_charset = str ? g_strdup(str) : NULL;
2749                 procmime_force_charset(str);
2750                 
2751                 summary_redisplay_msg(mainwin->summaryview);
2752                 
2753                 debug_print("forced charset: %s\n", str ? str : "Auto-Detect");
2754         }
2755 }
2756
2757 static void set_decode_cb(MainWindow *mainwin, guint action,
2758                            GtkWidget *widget)
2759 {
2760         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2761                 mainwin->messageview->forced_encoding = (EncodingType)action;
2762                 
2763                 summary_redisplay_msg(mainwin->summaryview);
2764                 
2765                 debug_print("forced encoding: %d\n", action);
2766         }
2767 }
2768
2769 static void hide_read_messages (MainWindow *mainwin, guint action,
2770                                 GtkWidget *widget)
2771 {
2772         if (!mainwin->summaryview->folder_item
2773             || g_object_get_data(G_OBJECT(widget), "dont_toggle"))
2774                 return;
2775         summary_toggle_show_read_messages(mainwin->summaryview);
2776 }
2777
2778 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2779 {
2780         if (mainwin->menu_lock_count) return;
2781         if (!mainwin->summaryview->folder_item) return;
2782
2783         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2784                 summary_thread_build(mainwin->summaryview);
2785 /*              mainwin->summaryview->folder_item->threaded = TRUE; */
2786         } else {
2787                 summary_unthread(mainwin->summaryview);
2788 /*              mainwin->summaryview->folder_item->threaded = FALSE; */
2789         }
2790 }
2791
2792 static void expand_threads_cb(MainWindow *mainwin, guint action,
2793                               GtkWidget *widget)
2794 {
2795         summary_expand_threads(mainwin->summaryview);
2796 }
2797
2798 static void collapse_threads_cb(MainWindow *mainwin, guint action,
2799                                 GtkWidget *widget)
2800 {
2801         summary_collapse_threads(mainwin->summaryview);
2802 }
2803
2804 static void set_summary_display_item_cb(MainWindow *mainwin, guint action,
2805                                 GtkWidget *widget)
2806 {
2807         prefs_summary_column_open();
2808 }
2809
2810 static void set_folder_display_item_cb(MainWindow *mainwin, guint action,
2811                                 GtkWidget *widget)
2812 {
2813         prefs_folder_column_open();
2814 }
2815
2816 static void sort_summary_cb(MainWindow *mainwin, guint action,
2817                             GtkWidget *widget)
2818 {
2819         FolderItem *item = mainwin->summaryview->folder_item;
2820         GtkWidget *menuitem;
2821
2822         if (mainwin->menu_lock_count) return;
2823
2824         if (GTK_CHECK_MENU_ITEM(widget)->active && item) {
2825                 menuitem = gtk_item_factory_get_item
2826                         (mainwin->menu_factory, "/View/Sort/Ascending");
2827                 summary_sort(mainwin->summaryview, (FolderSortKey)action,
2828                              GTK_CHECK_MENU_ITEM(menuitem)->active
2829                              ? SORT_ASCENDING : SORT_DESCENDING);
2830                 item->sort_key = action;
2831         }
2832 }
2833
2834 static void sort_summary_type_cb(MainWindow *mainwin, guint action,
2835                                  GtkWidget *widget)
2836 {
2837         FolderItem *item = mainwin->summaryview->folder_item;
2838
2839         if (mainwin->menu_lock_count) return;
2840
2841         if (GTK_CHECK_MENU_ITEM(widget)->active && item)
2842                 summary_sort(mainwin->summaryview,
2843                              item->sort_key, (FolderSortType)action);
2844 }
2845
2846 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
2847                                   GtkWidget *widget)
2848 {
2849         summary_attract_by_subject(mainwin->summaryview);
2850 }
2851
2852 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
2853                                  GtkWidget *widget)
2854 {
2855         FolderItem *item;
2856
2857         item = folderview_get_selected_item(mainwin->folderview);
2858         if (item) {
2859                 main_window_cursor_wait(mainwin);
2860                 STATUSBAR_PUSH(mainwin, _("Deleting duplicated messages..."));
2861
2862                 folderutils_delete_duplicates(item, prefs_common.immediate_exec ?
2863                                               DELETE_DUPLICATES_REMOVE : DELETE_DUPLICATES_SETFLAG);
2864
2865                 STATUSBAR_POP(mainwin);
2866                 main_window_cursor_normal(mainwin);
2867         }
2868 }
2869
2870 struct DelDupsData
2871 {
2872         guint   dups;
2873         guint   folders;
2874 };
2875
2876 static void deldup_all(FolderItem *item, gpointer _data)
2877 {
2878         struct DelDupsData *data = _data;
2879         gint result;
2880         
2881         result = folderutils_delete_duplicates(item, DELETE_DUPLICATES_REMOVE);
2882         if (result >= 0) {
2883                 data->dups += result;
2884                 data->folders += 1;
2885         }
2886 }
2887
2888 static void delete_duplicated_all_cb(MainWindow *mainwin, guint action,
2889                                  GtkWidget *widget)
2890 {
2891         struct DelDupsData data = {0, 0};
2892
2893         folder_func_to_all_folders(deldup_all, &data);
2894         alertpanel_notice(ngettext("Deleted %d duplicate message in %d folders.\n",
2895                                    "Deleted %d duplicate messages in %d folders.\n",
2896                                    data.dups),
2897                           data.dups, data.folders);
2898 }
2899
2900 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2901 {
2902         summary_filter(mainwin->summaryview, (gboolean)action);
2903 }
2904
2905 static void execute_summary_cb(MainWindow *mainwin, guint action,
2906                                GtkWidget *widget)
2907 {
2908         summary_execute(mainwin->summaryview);
2909 }
2910
2911 static void update_summary_cb(MainWindow *mainwin, guint action,
2912                               GtkWidget *widget)
2913 {
2914         FolderItem *fitem;
2915         FolderView *folderview = mainwin->folderview;
2916
2917         if (!mainwin->summaryview->folder_item) return;
2918         if (!folderview->opened) return;
2919
2920         folder_update_op_count();
2921
2922         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
2923                                             folderview->opened);
2924         if (!fitem) return;
2925
2926         folder_item_scan(fitem);
2927         summary_show(mainwin->summaryview, fitem);
2928 }
2929
2930 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2931 {
2932         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2933 }
2934
2935 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2936 {
2937         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2938 }
2939
2940 static void prev_unread_cb(MainWindow *mainwin, guint action,
2941                            GtkWidget *widget)
2942 {
2943         summary_select_prev_unread(mainwin->summaryview);
2944 }
2945
2946 static void next_unread_cb(MainWindow *mainwin, guint action,
2947                            GtkWidget *widget)
2948 {
2949         summary_select_next_unread(mainwin->summaryview);
2950 }
2951
2952 static void prev_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2953 {
2954         summary_select_prev_new(mainwin->summaryview);
2955 }
2956
2957 static void next_new_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2958 {
2959         summary_select_next_new(mainwin->summaryview);
2960 }
2961
2962 static void prev_marked_cb(MainWindow *mainwin, guint action,
2963                            GtkWidget *widget)
2964 {
2965         summary_select_prev_marked(mainwin->summaryview);
2966 }
2967
2968 static void next_marked_cb(MainWindow *mainwin, guint action,
2969                            GtkWidget *widget)
2970 {
2971         summary_select_next_marked(mainwin->summaryview);
2972 }
2973
2974 static void prev_labeled_cb(MainWindow *mainwin, guint action,
2975                             GtkWidget *widget)
2976 {
2977         summary_select_prev_labeled(mainwin->summaryview);
2978 }
2979
2980 static void next_labeled_cb(MainWindow *mainwin, guint action,
2981                             GtkWidget *widget)
2982 {
2983         summary_select_next_labeled(mainwin->summaryview);
2984 }
2985
2986 static void goto_folder_cb(MainWindow *mainwin, guint action,
2987                            GtkWidget *widget)
2988 {
2989         FolderItem *to_folder;
2990
2991         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
2992
2993         if (to_folder)
2994                 folderview_select(mainwin->folderview, to_folder);
2995 }
2996
2997 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2998 {
2999         messageview_copy_clipboard(mainwin->messageview);
3000 }
3001
3002 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
3003 {
3004         MessageView *msgview = mainwin->messageview;
3005
3006         if (GTK_WIDGET_HAS_FOCUS(mainwin->summaryview->ctree))
3007                 summary_select_all(mainwin->summaryview);
3008         else if (messageview_is_visible(msgview) &&
3009                  (GTK_WIDGET_HAS_FOCUS(msgview->mimeview->textview->text)))
3010                 messageview_select_all(mainwin->messageview);
3011 }
3012
3013 static void select_thread_cb(MainWindow *mainwin, guint action,
3014                              GtkWidget *widget)
3015 {
3016         summary_select_thread(mainwin->summaryview);
3017 }
3018
3019 static void create_filter_cb(MainWindow *mainwin, guint action,
3020                              GtkWidget *widget)
3021 {
3022         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 0);
3023 }
3024
3025 static void create_processing_cb(MainWindow *mainwin, guint action,
3026                              GtkWidget *widget)
3027 {
3028         summary_filter_open(mainwin->summaryview, (PrefsFilterType)action, 1);
3029 }
3030
3031 static void prefs_pre_processing_open_cb(MainWindow *mainwin, guint action,
3032                                          GtkWidget *widget)
3033 {
3034         prefs_filtering_open(&pre_global_processing,
3035                              _("Processing rules to apply before folder rules"),
3036                              NULL, NULL);
3037 }
3038
3039 static void prefs_post_processing_open_cb(MainWindow *mainwin, guint action,
3040                                           GtkWidget *widget)
3041 {
3042         prefs_filtering_open(&post_global_processing,
3043                              _("Processing rules to apply after folder rules"),
3044                              NULL, NULL);
3045 }
3046
3047 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
3048                                     GtkWidget *widget)
3049 {
3050         prefs_filtering_open(&filtering_rules,
3051                              _("Filtering configuration"),
3052                              NULL, NULL);
3053 }
3054
3055 static void prefs_template_open_cb(MainWindow *mainwin, guint action,
3056                                    GtkWidget *widget)
3057 {
3058         prefs_template_open();
3059 }
3060
3061 static void prefs_actions_open_cb(MainWindow *mainwin, guint action,
3062                                   GtkWidget *widget)
3063 {
3064         prefs_actions_open(mainwin);
3065 }
3066 #ifdef USE_OPENSSL
3067 static void ssl_manager_open_cb(MainWindow *mainwin, guint action,
3068                                   GtkWidget *widget)
3069 {
3070         ssl_manager_open(mainwin);
3071 }
3072 #endif
3073 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
3074                                   GtkWidget *widget)
3075 {
3076         if (!cur_account) {
3077                 new_account_cb(mainwin, 0, widget);
3078         } else {
3079                 account_open(cur_account);
3080         }
3081 }
3082
3083 static void new_account_cb(MainWindow *mainwin, guint action,
3084                            GtkWidget *widget)
3085 {
3086         account_edit_open();
3087         if (!compose_get_compose_list()) account_add();
3088 }
3089
3090 static void account_selector_menu_cb(GtkMenuItem *menuitem, gpointer data)
3091 {
3092         cur_account = (PrefsAccount *)data;
3093         FolderItem *item = NULL;
3094         
3095         if (!mainwindow_get_mainwindow())
3096                 return;
3097         main_window_show_cur_account(mainwindow_get_mainwindow());
3098         toolbar_update(TOOLBAR_MAIN, mainwindow_get_mainwindow());
3099         main_window_set_menu_sensitive(mainwindow_get_mainwindow());
3100         toolbar_main_set_sensitive(mainwindow_get_mainwindow());
3101         gtk_button_set_relief(GTK_BUTTON(mainwindow_get_mainwindow()->ac_button), GTK_RELIEF_NONE);
3102         item = folderview_get_selected_item(
3103                         mainwindow_get_mainwindow()->folderview);
3104         if (item) {
3105                 toolbar_set_compose_button
3106                         (mainwindow_get_mainwindow()->toolbar,
3107                          FOLDER_TYPE(item->folder) == F_NEWS ? 
3108                          COMPOSEBUTTON_NEWS : COMPOSEBUTTON_MAIL);
3109         }
3110 }
3111
3112 static void account_receive_menu_cb(GtkMenuItem *menuitem, gpointer data)
3113 {
3114         MainWindow *mainwin = (MainWindow *)mainwin_list->data;
3115         PrefsAccount *account = (PrefsAccount *)data;
3116
3117         inc_account_mail(mainwin, account);
3118 }
3119
3120 static void prefs_open_cb(GtkMenuItem *menuitem, gpointer data)
3121 {
3122         prefs_gtk_open();
3123 }
3124
3125 static void plugins_open_cb(GtkMenuItem *menuitem, gpointer data)
3126 {
3127         pluginwindow_create();
3128 }
3129
3130 static void manual_open_cb(MainWindow *mainwin, guint action,
3131                            GtkWidget *widget)
3132 {
3133         manual_open((ManualType)action);
3134 }
3135
3136 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
3137 {
3138         MainWindow *mainwin = (MainWindow *)data;
3139         gchar *str;
3140
3141         if (item->path)
3142                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
3143                                       LOCAL_FOLDER(folder)->rootpath,
3144                                       G_DIR_SEPARATOR,
3145                                       item->path);
3146         else
3147                 str = g_strdup_printf(_("Scanning folder %s ..."),
3148                                       LOCAL_FOLDER(folder)->rootpath);
3149
3150         STATUSBAR_PUSH(mainwin, str);
3151         STATUSBAR_POP(mainwin);
3152         g_free(str);
3153 }
3154
3155 static gboolean mainwindow_focus_in_event(GtkWidget *widget, GdkEventFocus *focus,
3156                                           gpointer data)
3157 {
3158         SummaryView *summary;
3159
3160         g_return_val_if_fail(data, FALSE);
3161         summary = ((MainWindow *)data)->summaryview;
3162         g_return_val_if_fail(summary, FALSE);
3163
3164         if (GTK_CLIST(summary->ctree)->selection && 
3165             g_list_length(GTK_CLIST(summary->ctree)->selection) > 1)
3166                 return FALSE;
3167
3168         if (summary->selected != summary->displayed)
3169                 summary_select_node(summary, summary->displayed, FALSE, TRUE);
3170         return FALSE;
3171 }
3172
3173 #define BREAK_ON_MODIFIER_KEY() \
3174         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
3175
3176 gboolean mainwindow_key_pressed (GtkWidget *widget, GdkEventKey *event,
3177                                     gpointer data)
3178 {
3179         MainWindow *mainwin = (MainWindow*) data;
3180         
3181         if (!mainwin || !event) 
3182                 return FALSE;
3183
3184         if (quicksearch_has_focus(mainwin->summaryview->quicksearch))
3185                 return FALSE;
3186
3187         switch (event->keyval) {
3188         case GDK_Q:             /* Quit */
3189                 BREAK_ON_MODIFIER_KEY();
3190
3191                 app_exit_cb(mainwin, 0, NULL);
3192                 return FALSE;
3193         case GDK_space:
3194                 if (mainwin->folderview && mainwin->summaryview
3195                     && !mainwin->summaryview->displayed) {
3196                         summary_lock(mainwin->summaryview);
3197                         folderview_select_next_unread(mainwin->folderview);
3198                         summary_unlock(mainwin->summaryview);
3199                 }
3200                 break;
3201         default:
3202                 break;
3203         }
3204         return FALSE;
3205 }
3206
3207 #undef BREAK_ON_MODIFIER_KEY
3208
3209 /*
3210  * Harvest addresses for selected folder.
3211  */
3212 static void addr_harvest_cb( MainWindow *mainwin,
3213                             guint action,
3214                             GtkWidget *widget )
3215 {
3216         addressbook_harvest( mainwin->summaryview->folder_item, FALSE, NULL );
3217 }
3218
3219 /*
3220  * Harvest addresses for selected messages in summary view.
3221  */
3222 static void addr_harvest_msg_cb( MainWindow *mainwin,
3223                             guint action,
3224                             GtkWidget *widget )
3225 {
3226         summary_harvest_address( mainwin->summaryview );
3227 }
3228
3229 /*!
3230  *\brief        get a MainWindow
3231  *
3232  *\return       MainWindow * The first mainwindow in the mainwin_list
3233  */
3234 MainWindow *mainwindow_get_mainwindow(void)
3235 {
3236         if (mainwin_list && mainwin_list->data)
3237                 return (MainWindow *)(mainwin_list->data);
3238         else
3239                 return NULL;
3240 }
3241
3242 gboolean mainwindow_progressindicator_hook(gpointer source, gpointer userdata)
3243 {
3244         ProgressData *data = (ProgressData *) source;
3245         MainWindow *mainwin = (MainWindow *) userdata;
3246
3247         switch (data->cmd) {
3248         case PROGRESS_COMMAND_START:
3249         case PROGRESS_COMMAND_STOP:
3250                 gtk_progress_bar_set_fraction
3251                         (GTK_PROGRESS_BAR(mainwin->progressbar), 0.0);
3252                 break;
3253         case PROGRESS_COMMAND_SET_PERCENTAGE:
3254                 gtk_progress_bar_set_fraction
3255                         (GTK_PROGRESS_BAR(mainwin->progressbar), data->value);
3256                 break;          
3257         }
3258         while (gtk_events_pending()) gtk_main_iteration ();
3259
3260         return FALSE;
3261 }
3262
3263 /*
3264 * End of Source.
3265 */
3266