New address book.
[claws.git] / src / mainwindow.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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 <gtk/gtkmain.h>
24 #include <gtk/gtkwindow.h>
25 #include <gtk/gtkwidget.h>
26 #include <gtk/gtksignal.h>
27 #include <gtk/gtkvbox.h>
28 #include <gtk/gtkcontainer.h>
29 #include <gtk/gtkstatusbar.h>
30 #include <gtk/gtkprogressbar.h>
31 #include <gtk/gtkhpaned.h>
32 #include <gtk/gtkvpaned.h>
33 #include <gtk/gtkcheckmenuitem.h>
34 #include <gtk/gtkitemfactory.h>
35 #include <gtk/gtkeditable.h>
36 #include <gtk/gtkmenu.h>
37 #include <gtk/gtkmenuitem.h>
38 #include <gtk/gtkhandlebox.h>
39 #include <gtk/gtktoolbar.h>
40 #include <gtk/gtkbutton.h>
41 #include <string.h>
42
43 #include "intl.h"
44 #include "main.h"
45 #include "mainwindow.h"
46 #include "folderview.h"
47 #include "foldersel.h"
48 #include "summaryview.h"
49 #include "summary_search.h"
50 #include "messageview.h"
51 #include "headerview.h"
52 #include "menu.h"
53 #include "folder.h"
54 #include "inc.h"
55 #include "compose.h"
56 #include "procmsg.h"
57 #include "import.h"
58 #include "export.h"
59 #include "prefs_common.h"
60 #include "prefs_filter.h"
61 #include "prefs_filtering.h"
62 #include "prefs_scoring.h"
63 #include "prefs_account.h"
64 #include "prefs_folder_item.h"
65 #include "account.h"
66 #include "addressbook.h"
67 #include "headerwindow.h"
68 #include "logwindow.h"
69 #include "manage_window.h"
70 #include "alertpanel.h"
71 #include "statusbar.h"
72 #include "inputdialog.h"
73 #include "utils.h"
74 #include "gtkutils.h"
75 #include "codeconv.h"
76 #include "about.h"
77 #include "manual.h"
78 #include "prefs_templates.h"
79
80 #define AC_LABEL_WIDTH  240
81
82 #define STATUSBAR_PUSH(mainwin, str) \
83 { \
84         gtk_statusbar_push(GTK_STATUSBAR(mainwin->statusbar), \
85                            mainwin->mainwin_cid, str); \
86         gtkut_widget_wait_for_draw(mainwin->hbox_stat); \
87 }
88
89 #define STATUSBAR_POP(mainwin) \
90 { \
91         gtk_statusbar_pop(GTK_STATUSBAR(mainwin->statusbar), \
92                           mainwin->mainwin_cid); \
93 }
94
95 /* list of all instantiated MainWindow */
96 static GList *mainwin_list = NULL;
97
98 static GdkCursor *watch_cursor;
99
100 static void main_window_show_cur_account        (MainWindow     *mainwin);
101
102 static void main_window_set_widgets             (MainWindow     *mainwin,
103                                                  SeparateType    type);
104 static void main_window_toolbar_create          (MainWindow     *mainwin,
105                                                  GtkWidget      *container);
106
107 /* callback functions */
108 static void toolbar_inc_cb              (GtkWidget      *widget,
109                                          gpointer        data);
110 static void toolbar_inc_all_cb          (GtkWidget      *widget,
111                                          gpointer        data);
112 static void toolbar_send_cb             (GtkWidget      *widget,
113                                          gpointer        data);
114
115 static void toolbar_compose_cb          (GtkWidget      *widget,
116                                          gpointer        data);
117 static void toolbar_popup_compose_type_cb(GtkWidget     *widget,
118                                          gpointer        data);
119 static void toolbar_popup_compose_type_set(GtkWidget    *widget,
120                                          gpointer        data);
121 static void toolbar_compose_news_cb             (GtkWidget      *widget,
122                                          gpointer        data);
123 static void toolbar_compose_mail_cb             (GtkWidget      *widget,
124                                          gpointer        data);
125 static void toolbar_reply_cb            (GtkWidget      *widget,
126                                          gpointer        data);
127 static void toolbar_reply_to_all_cb     (GtkWidget      *widget,
128                                          gpointer        data);
129 static void toolbar_reply_to_sender_cb  (GtkWidget      *widget,
130                                          gpointer        data);
131 static void toolbar_forward_cb          (GtkWidget      *widget,
132                                          gpointer        data);
133
134 static void toolbar_delete_cb           (GtkWidget      *widget,
135                                          gpointer        data);
136 static void toolbar_exec_cb             (GtkWidget      *widget,
137                                          gpointer        data);
138
139 static void toolbar_next_unread_cb      (GtkWidget      *widget,
140                                          gpointer        data);
141
142 static void toolbar_prefs_cb            (GtkWidget      *widget,
143                                          gpointer        data);
144 static void toolbar_account_cb          (GtkWidget      *widget,
145                                          gpointer        data);
146
147 static void toolbar_account_button_pressed      (GtkWidget      *widget,
148                                                  GdkEventButton *event,
149                                                  gpointer        data);
150 static void ac_label_button_pressed             (GtkWidget      *widget,
151                                                  GdkEventButton *event,
152                                                  gpointer        data);
153 static void ac_menu_popup_closed                (GtkMenuShell   *menu_shell,
154                                                  gpointer        data);
155
156 static gint main_window_close_cb (GtkWidget     *widget,
157                                   GdkEventAny   *event,
158                                   gpointer       data);
159
160 static void add_mailbox_cb       (MainWindow    *mainwin,
161                                   guint          action,
162                                   GtkWidget     *widget);
163 static void add_mbox_cb          (MainWindow    *mainwin,
164                                   guint          action,
165                                   GtkWidget     *widget);
166 static void update_folderview_cb (MainWindow    *mainwin,
167                                   guint          action,
168                                   GtkWidget     *widget);
169 static void new_folder_cb        (MainWindow    *mainwin,
170                                   guint          action,
171                                   GtkWidget     *widget);
172 static void rename_folder_cb     (MainWindow    *mainwin,
173                                   guint          action,
174                                   GtkWidget     *widget);
175 static void delete_folder_cb     (MainWindow    *mainwin,
176                                   guint          action,
177                                   GtkWidget     *widget);
178 static void import_mbox_cb       (MainWindow    *mainwin,
179                                   guint          action,
180                                   GtkWidget     *widget);
181 static void export_mbox_cb       (MainWindow    *mainwin,
182                                   guint          action,
183                                   GtkWidget     *widget);
184 static void empty_trash_cb       (MainWindow    *mainwin,
185                                   guint          action,
186                                   GtkWidget     *widget);
187
188 static void save_as_cb           (MainWindow    *mainwin,
189                                   guint          action,
190                                   GtkWidget     *widget);
191 static void print_cb             (MainWindow    *mainwin,
192                                   guint          action,
193                                   GtkWidget     *widget);
194 static void app_exit_cb          (MainWindow    *mainwin,
195                                   guint          action,
196                                   GtkWidget     *widget);
197
198 static void toggle_folder_cb     (MainWindow    *mainwin,
199                                   guint          action,
200                                   GtkWidget     *widget);
201 static void toggle_message_cb    (MainWindow    *mainwin,
202                                   guint          action,
203                                   GtkWidget     *widget);
204 static void toggle_toolbar_cb    (MainWindow    *mainwin,
205                                   guint          action,
206                                   GtkWidget     *widget);
207 static void toggle_statusbar_cb  (MainWindow    *mainwin,
208                                   guint          action,
209                                   GtkWidget     *widget);
210 static void separate_widget_cb  (GtkCheckMenuItem *checkitem,
211                                  guint action,
212                                  GtkWidget *widget);
213
214 static void addressbook_open_cb (MainWindow     *mainwin,
215                                  guint           action,
216                                  GtkWidget      *widget);
217 static void log_window_show_cb  (MainWindow     *mainwin,
218                                  guint           action,
219                                  GtkWidget      *widget);
220
221 static void inc_mail_cb                 (MainWindow     *mainwin,
222                                          guint           action,
223                                          GtkWidget      *widget);
224 static void inc_all_account_mail_cb     (MainWindow     *mainwin,
225                                          guint           action,
226                                          GtkWidget      *widget);
227
228 static void send_queue_cb               (MainWindow     *mainwin,
229                                          guint           action,
230                                          GtkWidget      *widget);
231
232 static void compose_cb                  (MainWindow     *mainwin,
233                                          guint           action,
234                                          GtkWidget      *widget);
235 static void compose_mail_cb(MainWindow *mainwin, guint action,
236                             GtkWidget *widget);
237 static void compose_news_cb(MainWindow *mainwin, guint action,
238                             GtkWidget *widget);
239 static void reply_cb                    (MainWindow     *mainwin,
240                                          guint           action,
241                                          GtkWidget      *widget);
242
243 static void open_msg_cb                 (MainWindow     *mainwin,
244                                          guint           action,
245                                          GtkWidget      *widget);
246
247 static void view_source_cb              (MainWindow     *mainwin,
248                                          guint           action,
249                                          GtkWidget      *widget);
250
251 static void reedit_cb                   (MainWindow     *mainwin,
252                                          guint           action,
253                                          GtkWidget      *widget);
254
255 static void move_to_cb                  (MainWindow     *mainwin,
256                                          guint           action,
257                                          GtkWidget      *widget);
258 static void copy_to_cb                  (MainWindow     *mainwin,
259                                          guint           action,
260                                          GtkWidget      *widget);
261 static void delete_cb                   (MainWindow     *mainwin,
262                                          guint           action,
263                                          GtkWidget      *widget);
264
265 static void mark_cb                     (MainWindow     *mainwin,
266                                          guint           action,
267                                          GtkWidget      *widget);
268 static void unmark_cb                   (MainWindow     *mainwin,
269                                          guint           action,
270                                          GtkWidget      *widget);
271
272 static void mark_as_unread_cb           (MainWindow     *mainwin,
273                                          guint           action,
274                                          GtkWidget      *widget);
275 static void mark_as_read_cb             (MainWindow     *mainwin,
276                                          guint           action,
277                                          GtkWidget      *widget);
278
279 static void set_charset_cb              (MainWindow     *mainwin,
280                                          guint           action,
281                                          GtkWidget      *widget);
282
283 static void thread_cb            (MainWindow    *mainwin,
284                                   guint          action,
285                                   GtkWidget     *widget);
286 static void set_display_item_cb  (MainWindow    *mainwin,
287                                   guint          action,
288                                   GtkWidget     *widget);
289 static void sort_summary_cb      (MainWindow    *mainwin,
290                                   guint          action,
291                                   GtkWidget     *widget);
292 static void attract_by_subject_cb(MainWindow    *mainwin,
293                                   guint          action,
294                                   GtkWidget     *widget);
295
296 static void delete_duplicated_cb (MainWindow    *mainwin,
297                                   guint          action,
298                                   GtkWidget     *widget);
299 static void filter_cb            (MainWindow    *mainwin,
300                                   guint          action,
301                                   GtkWidget     *widget);
302 static void execute_summary_cb   (MainWindow    *mainwin,
303                                   guint          action,
304                                   GtkWidget     *widget);
305 static void update_summary_cb    (MainWindow    *mainwin,
306                                   guint          action,
307                                   GtkWidget     *widget);
308
309 static void prev_cb              (MainWindow    *mainwin,
310                                   guint          action,
311                                   GtkWidget     *widget);
312 static void next_cb              (MainWindow    *mainwin,
313                                   guint          action,
314                                   GtkWidget     *widget);
315
316 static void next_unread_cb       (MainWindow    *mainwin,
317                                   guint          action,
318                                   GtkWidget     *widget);
319
320 static void next_marked_cb       (MainWindow    *mainwin,
321                                   guint          action,
322                                   GtkWidget     *widget);
323
324 static void prev_marked_cb       (MainWindow    *mainwin,
325                                   guint          action,
326                                   GtkWidget     *widget);
327
328 static void next_labeled_cb      (MainWindow    *mainwin,
329                                   guint          action,
330                                   GtkWidget     *widget);
331
332
333 static void prev_labeled_cb      (MainWindow    *mainwin,
334                                   guint          action,
335                                   GtkWidget     *widget);
336
337
338 static void goto_folder_cb       (MainWindow    *mainwin,
339                                   guint          action,
340                                   GtkWidget     *widget);
341
342 static void copy_cb              (MainWindow    *mainwin,
343                                   guint          action,
344                                   GtkWidget     *widget);
345 static void allsel_cb            (MainWindow    *mainwin,
346                                   guint          action,
347                                   GtkWidget     *widget);
348
349 static void prefs_common_open_cb (MainWindow    *mainwin,
350                                   guint          action,
351                                   GtkWidget     *widget);
352 static void prefs_filter_open_cb (MainWindow    *mainwin,
353                                   guint          action,
354                                   GtkWidget     *widget);
355 static void prefs_scoring_open_cb (MainWindow   *mainwin,
356                                   guint          action,
357                                   GtkWidget     *widget);
358 static void prefs_filtering_open_cb (MainWindow *mainwin,
359                                   guint          action,
360                                   GtkWidget     *widget);
361 static void prefs_account_open_cb(MainWindow    *mainwin,
362                                   guint          action,
363                                   GtkWidget     *widget);
364 static void new_account_cb       (MainWindow    *mainwin,
365                                   guint          action,
366                                   GtkWidget     *widget);
367
368 static void account_menu_cb      (GtkMenuItem   *menuitem,
369                                   gpointer       data);
370
371 static void manual_open_cb       (MainWindow    *mainwin,
372                                   guint          action,
373                                   GtkWidget     *widget);
374
375 static void scan_tree_func       (Folder        *folder,
376                                   FolderItem    *item,
377                                   gpointer       data);
378                                   
379 static void activate_compose_button (MainWindow *mainwin,
380                                 ToolbarStyle      style,
381                                 ComposeButtonType type);
382
383 static void prefs_templates_open_cb(MainWindow  *mainwin,
384                                   guint          action,
385                                   GtkWidget     *widget);
386
387 #define  SEPARATE_ACTION  667
388
389 static GtkItemFactoryEntry mainwin_entries[] =
390 {
391         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
392         {N_("/_File/_Add mailbox..."),          NULL, add_mailbox_cb, 0, NULL},
393         {N_("/_File/_Add mbox mailbox..."),     NULL, add_mbox_cb, 0, NULL},
394         {N_("/_File/_Update folder tree"),      NULL, update_folderview_cb, 0, NULL},
395         {N_("/_File/_Folder"),                  NULL, NULL, 0, "<Branch>"},
396         {N_("/_File/_Folder/Create _new folder..."),
397                                                 NULL, new_folder_cb, 0, NULL},
398         {N_("/_File/_Folder/_Rename folder..."),NULL, rename_folder_cb, 0, NULL},
399         {N_("/_File/_Folder/_Delete folder"),   NULL, delete_folder_cb, 0, NULL},
400         {N_("/_File/_Import mbox file..."),     NULL, import_mbox_cb, 0, NULL},
401         {N_("/_File/_Export to mbox file..."),  NULL, export_mbox_cb, 0, NULL},
402         {N_("/_File/Empty _trash"),             NULL, empty_trash_cb, 0, NULL},
403         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
404         {N_("/_File/_Save as..."),              NULL, save_as_cb, 0, NULL},
405         {N_("/_File/_Print..."),                "<alt>P", print_cb, 0, NULL},
406         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
407         {N_("/_File/_Close"),                   "<alt>W", app_exit_cb, 0, NULL},
408         {N_("/_File/E_xit"),                    "<alt>Q", app_exit_cb, 0, NULL},
409
410         {N_("/_Edit"),                          NULL, NULL, 0, "<Branch>"},
411         {N_("/_Edit/_Copy"),                    "<control>C", copy_cb, 0, NULL},
412         {N_("/_Edit/Select _all"),              "<control>A", allsel_cb, 0, NULL},
413         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
414         {N_("/_Edit/_Search"),                  "<control>S", summary_search_cb, 0, NULL},
415
416         {N_("/_View"),                          NULL, NULL, 0, "<Branch>"},
417         {N_("/_View/_Folder tree"),             NULL, toggle_folder_cb, 0, "<ToggleItem>"},
418         {N_("/_View/_Message view"),            NULL, toggle_message_cb, 0, "<ToggleItem>"},
419         {N_("/_View/_Toolbar"),                 NULL, NULL, 0, "<Branch>"},
420         {N_("/_View/_Toolbar/Icon _and text"),  NULL, toggle_toolbar_cb, TOOLBAR_BOTH, "<RadioItem>"},
421         {N_("/_View/_Toolbar/_Icon"),           NULL, toggle_toolbar_cb, TOOLBAR_ICON, "/View/Toolbar/Icon and text"},
422         {N_("/_View/_Toolbar/_Text"),           NULL, toggle_toolbar_cb, TOOLBAR_TEXT, "/View/Toolbar/Icon and text"},
423         {N_("/_View/_Toolbar/_None"),           NULL, toggle_toolbar_cb, TOOLBAR_NONE, "/View/Toolbar/Icon and text"},
424         {N_("/_View/_Status bar"),              NULL, toggle_statusbar_cb, 0, "<ToggleItem>"},
425         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
426         {N_("/_View/Separate f_older tree"),    NULL, NULL, SEPARATE_ACTION + SEPARATE_FOLDER, "<ToggleItem>"},
427         {N_("/_View/Separate m_essage view"),   NULL, NULL, SEPARATE_ACTION + SEPARATE_MESSAGE, "<ToggleItem>"},
428         {N_("/_View/---"),                      NULL, NULL, 0, "<Separator>"},
429         {N_("/_View/_Code set"),                NULL, NULL, 0, "<Branch>"},
430         {N_("/_View/_Code set/_Auto detect"),
431          NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
432
433 #define CODESET_SEPARATOR \
434         {N_("/_View/_Code set/---"),            NULL, NULL, 0, "<Separator>"}
435 #define CODESET_ACTION(action) \
436          NULL, set_charset_cb, action, "/View/Code set/Auto detect"
437
438         {N_("/_View/_Code set/---"),            NULL, NULL, 0, "<Separator>"},
439         {N_("/_View/_Code set/7bit ascii (US-ASC_II)"),
440          CODESET_ACTION(C_US_ASCII)},
441
442 #if HAVE_LIBJCONV
443         {N_("/_View/_Code set/Unicode (_UTF-8)"),
444          CODESET_ACTION(C_UTF_8)},
445         CODESET_SEPARATOR,
446 #endif
447         {N_("/_View/_Code set/Western European (ISO-8859-_1)"),
448          CODESET_ACTION(C_ISO_8859_1)},
449         CODESET_SEPARATOR,
450 #if HAVE_LIBJCONV
451         {N_("/_View/_Code set/Central European (ISO-8859-_2)"),
452          CODESET_ACTION(C_ISO_8859_2)},
453         CODESET_SEPARATOR,
454         {N_("/_View/_Code set/_Baltic (ISO-8859-13)"),
455          CODESET_ACTION(C_ISO_8859_13)},
456         {N_("/_View/_Code set/Baltic (ISO-8859-_4)"),
457          CODESET_ACTION(C_ISO_8859_4)},
458         CODESET_SEPARATOR,
459         {N_("/_View/_Code set/Greek (ISO-8859-_7)"),
460          CODESET_ACTION(C_ISO_8859_7)},
461         CODESET_SEPARATOR,
462         {N_("/_View/_Code set/Turkish (ISO-8859-_9)"),
463          CODESET_ACTION(C_ISO_8859_9)},
464         CODESET_SEPARATOR,
465         {N_("/_View/_Code set/Cyrillic (ISO-8859-_5)"),
466          CODESET_ACTION(C_ISO_8859_5)},
467         {N_("/_View/_Code set/Cyrillic (KOI8-_R)"),
468          CODESET_ACTION(C_KOI8_R)},
469         {N_("/_View/_Code set/Cyrillic (Windows-1251)"),
470          CODESET_ACTION(C_CP1251)},
471         CODESET_SEPARATOR,
472 #endif
473         {N_("/_View/_Code set/Japanese (ISO-2022-_JP)"),
474          CODESET_ACTION(C_ISO_2022_JP)},
475 #if HAVE_LIBJCONV
476         {N_("/_View/_Code set/Japanese (ISO-2022-JP-2)"),
477          CODESET_ACTION(C_ISO_2022_JP_2)},
478 #endif
479         {N_("/_View/_Code set/Japanese (_EUC-JP)"),
480          CODESET_ACTION(C_EUC_JP)},
481         {N_("/_View/_Code set/Japanese (_Shift__JIS)"),
482          CODESET_ACTION(C_SHIFT_JIS)},
483 #if HAVE_LIBJCONV
484         CODESET_SEPARATOR,
485         {N_("/_View/_Code set/Simplified Chinese (_GB2312)"),
486          CODESET_ACTION(C_GB2312)},
487         {N_("/_View/_Code set/Traditional Chinese (_Big5)"),
488          CODESET_ACTION(C_BIG5)},
489         {N_("/_View/_Code set/Traditional Chinese (EUC-_TW)"),
490          CODESET_ACTION(C_EUC_TW)},
491         {N_("/_View/_Code set/Chinese (ISO-2022-_CN)"),
492          CODESET_ACTION(C_ISO_2022_CN)},
493         CODESET_SEPARATOR,
494         {N_("/_View/_Code set/Korean (EUC-_KR)"),
495          CODESET_ACTION(C_EUC_KR)},
496         {N_("/_View/_Code set/Korean (ISO-2022-KR)"),
497          CODESET_ACTION(C_ISO_2022_KR)},
498 #endif
499
500 #undef CODESET_SEPARATOR
501 #undef CODESET_ACTION
502
503         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
504         {N_("/_Message/Get new ma_il"),         "<alt>I",       inc_mail_cb, 0, NULL},
505         {N_("/_Message/Get from _all accounts"),
506                                                 "<shift><alt>I", inc_all_account_mail_cb, 0, NULL},
507         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
508         {N_("/_Message/Send queued messa_ges"),
509                                                 NULL, send_queue_cb, 0, NULL},
510         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
511         {N_("/_Message/Compose a_n email message"),     "<alt>N", compose_mail_cb, 0, NULL},
512         {N_("/_Message/Compose a news message"),        NULL,   compose_news_cb, 0, NULL},
513         {N_("/_Message/_Reply"),                "<alt>R",       reply_cb, COMPOSE_REPLY, NULL},
514         {N_("/_Message/Repl_y to sender"),      "<control><alt>R", reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
515         {N_("/_Message/Follow-up and reply to"), NULL, reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
516         {N_("/_Message/Reply to a_ll"),         "<shift><alt>R", reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
517         {N_("/_Message/_Forward"),              "<control>F", reply_cb, COMPOSE_FORWARD, NULL},
518         {N_("/_Message/Forward as a_ttachment"),
519                                                 "<shift><control>F", reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
520         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
521         {N_("/_Message/M_ove..."),              "<alt>O", move_to_cb, 0, NULL},
522         {N_("/_Message/_Copy..."),              NULL, copy_to_cb, 0, NULL},
523         {N_("/_Message/_Delete"),               "<alt>D", delete_cb,  0, NULL},
524         {N_("/_Message/_Mark"),                 NULL, NULL, 0, "<Branch>"},
525         {N_("/_Message/_Mark/_Mark"),           NULL, mark_cb,   0, NULL},
526         {N_("/_Message/_Mark/_Unmark"),         NULL, unmark_cb, 0, NULL},
527         {N_("/_Message/_Mark/---"),             NULL, NULL, 0, "<Separator>"},
528         {N_("/_Message/_Mark/Mark as unr_ead"), NULL, mark_as_unread_cb, 0, NULL},
529         {N_("/_Message/_Mark/Mark as rea_d"),
530                                                 NULL, mark_as_read_cb, 0, NULL},
531         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
532         {N_("/_Message/Open in new _window"),   "<shift><control>N", open_msg_cb, 0, NULL},
533         {N_("/_Message/View _source"),          "<control>U", view_source_cb, 0, NULL},
534         {N_("/_Message/Show all _header"),      "<control>H", header_window_show_cb, 0, NULL},
535         {N_("/_Message/Re-_edit"),              NULL, reedit_cb, 0, NULL},
536
537         {N_("/_Summary"),                       NULL, NULL, 0, "<Branch>"},
538         {N_("/_Summary/_Delete duplicated messages"),
539                                                 NULL, delete_duplicated_cb,   0, NULL},
540         {N_("/_Summary/_Filter messages"),      NULL, filter_cb, 0, NULL},
541         {N_("/_Summary/E_xecute"),              "<alt>X", execute_summary_cb, 0, NULL},
542         {N_("/_Summary/_Update"),               "<alt>U", update_summary_cb,  0, NULL},
543         {N_("/_Summary/---"),                   NULL, NULL, 0, "<Separator>"},
544         {N_("/_Summary/_Prev message"),         NULL, prev_cb, 0, NULL},
545         {N_("/_Summary/_Next message"),         NULL, next_cb, 0, NULL},
546         {N_("/_Summary/N_ext unread message"),  NULL, next_unread_cb, 0, NULL},
547         {N_("/_Summary/Prev marked message"),   NULL, prev_marked_cb, 0, NULL},
548         {N_("/_Summary/Next marked message"),   NULL, next_marked_cb, 0, NULL},
549         {N_("/_Summary/Prev labeled message"),  NULL, prev_labeled_cb, 0, NULL},
550         {N_("/_Summary/Next labeled message"),  NULL, next_labeled_cb, 0, NULL},
551         {N_("/_Summary/---"),                   NULL, NULL, 0, "<Separator>"},
552         {N_("/_Summary/_Go to other folder"),   "<alt>G", goto_folder_cb, 0, NULL},
553         {N_("/_Summary/---"),                   NULL, NULL, 0, "<Separator>"},
554         {N_("/_Summary/_Sort"),                 NULL, NULL, 0, "<Branch>"},
555         {N_("/_Summary/_Sort/Sort by _number"), NULL, sort_summary_cb, SORT_BY_NUMBER, NULL},
556         {N_("/_Summary/_Sort/Sort by s_ize"),   NULL, sort_summary_cb, SORT_BY_SIZE, NULL},
557         {N_("/_Summary/_Sort/Sort by _date"),   NULL, sort_summary_cb, SORT_BY_DATE, NULL},
558         {N_("/_Summary/_Sort/Sort by _from"),   NULL, sort_summary_cb, SORT_BY_FROM, NULL},
559         {N_("/_Summary/_Sort/Sort by _subject"),NULL, sort_summary_cb, SORT_BY_SUBJECT, NULL},
560         {N_("/_Summary/_Sort/Sort by sco_re"),  NULL, sort_summary_cb, SORT_BY_SCORE, NULL},
561         {N_("/_Summary/_Sort/Sort by _label"),  NULL, sort_summary_cb, SORT_BY_LABEL, NULL},
562         {N_("/_Summary/_Sort/---"),             NULL, NULL, 0, "<Separator>"},
563         {N_("/_Summary/_Sort/_Attract by subject"),
564                                                 NULL, attract_by_subject_cb, 0, NULL},
565         {N_("/_Summary/_Thread view"),          "<control>T",        thread_cb, 0, NULL},
566         {N_("/_Summary/Unt_hread view"),        "<shift><control>T", thread_cb, 1, NULL},
567         {N_("/_Summary/Set display _item..."),  NULL, set_display_item_cb, 0, NULL},
568
569         {N_("/_Tool"),                          NULL, NULL, 0, "<Branch>"},
570         {N_("/_Tool/_Address book"),            "<alt>A", addressbook_open_cb, 0, NULL},
571         {N_("/_Tool/_Log window"),              "<alt>L", log_window_show_cb, 0, NULL},
572
573         {N_("/_Configuration"),                 NULL, NULL, 0, "<Branch>"},
574         {N_("/_Configuration/_Common preferences..."),
575                                                 NULL, prefs_common_open_cb, 0, NULL},
576         {N_("/_Configuration/_Filter setting..."),
577                                                 NULL, prefs_filter_open_cb, 0, NULL},
578         {N_("/_Configuration/_Scoring ..."),
579                                                 NULL, prefs_scoring_open_cb, 0, NULL},
580         {N_("/_Configuration/_Filtering ..."),
581                                                 NULL, prefs_filtering_open_cb, 0, NULL},
582         {N_("/_Configuration/_Templates ..."),
583                                                 NULL, prefs_templates_open_cb, 0, NULL},
584         {N_("/_Configuration/_Preferences per account..."),
585                                                 NULL, prefs_account_open_cb, 0, NULL},
586         {N_("/_Configuration/---"),             NULL, NULL, 0, "<Separator>"},
587         {N_("/_Configuration/Create _new account..."),
588                                                 NULL, new_account_cb, 0, NULL},
589         {N_("/_Configuration/_Edit accounts..."),
590                                                 NULL, account_edit_open, 0, NULL},
591         {N_("/_Configuration/C_hange current account"),
592                                                 NULL, NULL, 0, "<Branch>"},
593
594         {N_("/_Help"),                          NULL, NULL, 0, "<LastBranch>"},
595         {N_("/_Help/_Manual"),                  NULL, NULL, 0, "<Branch>"},
596         {N_("/_Help/_Manual/_English"),         NULL, manual_open_cb, MANUAL_LANG_EN, NULL},
597         {N_("/_Help/_Manual/_Japanese"),        NULL, manual_open_cb, MANUAL_LANG_JA, NULL},
598         {N_("/_Help/---"),                      NULL, NULL, 0, "<Separator>"},
599         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
600 };
601
602 MainWindow *main_window_create(SeparateType type)
603 {
604         MainWindow *mainwin;
605         GtkWidget *window;
606         GtkWidget *vbox;
607         GtkWidget *menubar;
608         GtkWidget *handlebox;
609         GtkWidget *vbox_body;
610         GtkWidget *hbox_stat;
611         GtkWidget *statusbar;
612         GtkWidget *progressbar;
613         GtkWidget *statuslabel;
614         GtkWidget *ac_button;
615         GtkWidget *ac_label;
616
617         FolderView *folderview;
618         SummaryView *summaryview;
619         MessageView *messageview;
620         GdkColormap *colormap;
621         GdkColor color[4];
622         gboolean success[4];
623         guint n_menu_entries;
624         GtkItemFactory *ifactory;
625         GtkWidget *ac_menu;
626         GtkWidget *menuitem;
627         gint i;
628
629         debug_print(_("Creating main window...\n"));
630         mainwin = g_new0(MainWindow, 1);
631
632         /* main window */
633         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
634         gtk_window_set_title(GTK_WINDOW(window), PROG_VERSION);
635         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
636         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
637                            GTK_SIGNAL_FUNC(main_window_close_cb), mainwin);
638         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
639                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
640         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
641                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
642
643         gtk_widget_realize(window);
644
645         gtkut_widget_set_app_icon(window);
646
647         vbox = gtk_vbox_new(FALSE, 0);
648         gtk_widget_show(vbox);
649         gtk_container_add(GTK_CONTAINER(window), vbox);
650
651         /* menu bar */
652         n_menu_entries = sizeof(mainwin_entries) / sizeof(mainwin_entries[0]);
653         menubar = menubar_create(window, mainwin_entries, 
654                                  n_menu_entries, "<Main>", mainwin);
655         gtk_widget_show(menubar);
656         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
657
658         handlebox = gtk_handle_box_new();
659         gtk_widget_show(handlebox);
660         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
661
662         main_window_toolbar_create(mainwin, handlebox);
663
664         /* vbox that contains body */
665         vbox_body = gtk_vbox_new(FALSE, BORDER_WIDTH);
666         gtk_widget_show(vbox_body);
667         gtk_container_set_border_width(GTK_CONTAINER(vbox_body), BORDER_WIDTH);
668         gtk_box_pack_start(GTK_BOX(vbox), vbox_body, TRUE, TRUE, 0);
669
670         hbox_stat = gtk_hbox_new(FALSE, 2);
671         gtk_box_pack_end(GTK_BOX(vbox_body), hbox_stat, FALSE, FALSE, 0);
672
673         statusbar = statusbar_create();
674         gtk_box_pack_start(GTK_BOX(hbox_stat), statusbar, TRUE, TRUE, 0);
675
676         progressbar = gtk_progress_bar_new();
677         gtk_widget_set_usize(progressbar, 120, 1);
678         gtk_box_pack_start(GTK_BOX(hbox_stat), progressbar, FALSE, FALSE, 0);
679
680         statuslabel = gtk_label_new("");
681         gtk_box_pack_start(GTK_BOX(hbox_stat), statuslabel, FALSE, FALSE, 0);
682
683         ac_button = gtk_button_new();
684         gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
685         GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
686         gtk_widget_set_usize(ac_button, -1, 1);
687         gtk_box_pack_end(GTK_BOX(hbox_stat), ac_button, FALSE, FALSE, 0);
688         gtk_signal_connect(GTK_OBJECT(ac_button), "button_press_event",
689                            GTK_SIGNAL_FUNC(ac_label_button_pressed), mainwin);
690
691         ac_label = gtk_label_new("");
692         gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
693
694         gtk_widget_show_all(hbox_stat);
695
696         /* create views */
697         mainwin->folderview  = folderview  = folderview_create();
698         mainwin->summaryview = summaryview = summary_create();
699         mainwin->messageview = messageview = messageview_create();
700         mainwin->headerwin   = header_window_create();
701         mainwin->logwin      = log_window_create();
702
703         folderview->mainwin      = mainwin;
704         folderview->summaryview  = summaryview;
705
706         summaryview->mainwin     = mainwin;
707         summaryview->folderview  = folderview;
708         summaryview->messageview = messageview;
709         summaryview->headerwin   = mainwin->headerwin;
710         summaryview->window      = window;
711
712         messageview->mainwin     = mainwin;
713
714         mainwin->window      = window;
715         mainwin->vbox        = vbox;
716         mainwin->menubar     = menubar;
717         mainwin->handlebox   = handlebox;
718         mainwin->vbox_body   = vbox_body;
719         mainwin->hbox_stat   = hbox_stat;
720         mainwin->statusbar   = statusbar;
721         mainwin->progressbar = progressbar;
722         mainwin->statuslabel = statuslabel;
723         mainwin->ac_button   = ac_button;
724         mainwin->ac_label    = ac_label;
725
726         /* set context IDs for status bar */
727         mainwin->mainwin_cid = gtk_statusbar_get_context_id
728                 (GTK_STATUSBAR(statusbar), "Main Window");
729         mainwin->folderview_cid = gtk_statusbar_get_context_id
730                 (GTK_STATUSBAR(statusbar), "Folder View");
731         mainwin->summaryview_cid = gtk_statusbar_get_context_id
732                 (GTK_STATUSBAR(statusbar), "Summary View");
733
734         /* allocate colors for summary view and folder view */
735         summaryview->color_marked.red = summaryview->color_marked.green = 0;
736         summaryview->color_marked.blue = (guint16)65535;
737
738         summaryview->color_dim.red = summaryview->color_dim.green =
739                 summaryview->color_dim.blue = COLOR_DIM;
740
741         folderview->color_new.red = (guint16)55000;
742         folderview->color_new.green = folderview->color_new.blue = 15000;
743
744         gtkut_convert_int_to_gdk_color(prefs_common.tgt_folder_col,
745                                        &folderview->color_op);
746
747         summaryview->color_important.red = 0;
748         summaryview->color_marked.green = 0;
749         summaryview->color_important.blue = (guint16)65535;
750
751         color[0] = summaryview->color_marked;
752         color[1] = summaryview->color_dim;
753         color[2] = folderview->color_new;
754         color[3] = folderview->color_op;
755
756         colormap = gdk_window_get_colormap(window->window);
757         gdk_colormap_alloc_colors(colormap, color, 4, FALSE, TRUE, success);
758         for (i = 0; i < 4; i++) {
759                 if (success[i] == FALSE)
760                         g_warning(_("MainWindow: color allocation %d failed\n"), i);
761         }
762
763         debug_print(_("done.\n"));
764
765         main_window_set_widgets(mainwin, type);
766
767         /* set menu items */
768         ifactory = gtk_item_factory_from_widget(menubar);
769         menuitem = gtk_item_factory_get_item
770                 (ifactory, "/View/Code set/Auto detect");
771         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
772
773         switch (prefs_common.toolbar_style) {
774         case TOOLBAR_NONE:
775                 menuitem = gtk_item_factory_get_item
776                         (ifactory, "/View/Toolbar/None");
777                 break;
778         case TOOLBAR_ICON:
779                 menuitem = gtk_item_factory_get_item
780                         (ifactory, "/View/Toolbar/Icon");
781                 break;
782         case TOOLBAR_TEXT:
783                 menuitem = gtk_item_factory_get_item
784                         (ifactory, "/View/Toolbar/Text");
785                 break;
786         case TOOLBAR_BOTH:
787                 menuitem = gtk_item_factory_get_item
788                         (ifactory, "/View/Toolbar/Icon and text");
789         }
790         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
791
792         gtk_widget_hide(mainwin->hbox_stat);
793         menuitem = gtk_item_factory_get_item(ifactory, "/View/Status bar");
794         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
795                                        prefs_common.show_statusbar);
796
797         /* set the check of the SEPARATE_xxx menu items. we also need the main window
798          * as a property and pass the action type to the callback */
799         menuitem = gtk_item_factory_get_widget_by_action(ifactory, SEPARATE_ACTION + SEPARATE_FOLDER);
800         g_assert(menuitem);
801         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), type & SEPARATE_FOLDER);
802         gtk_object_set_data(GTK_OBJECT(menuitem), "mainwindow", mainwin);
803         gtk_signal_connect(GTK_OBJECT(menuitem), "toggled", GTK_SIGNAL_FUNC(separate_widget_cb), 
804                                            GUINT_TO_POINTER(SEPARATE_FOLDER));
805
806         menuitem = gtk_item_factory_get_widget_by_action(ifactory, SEPARATE_ACTION + SEPARATE_MESSAGE);
807         g_assert(menuitem);
808         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), type & SEPARATE_MESSAGE);
809         gtk_object_set_data(GTK_OBJECT(menuitem), "mainwindow", mainwin);
810         gtk_signal_connect(GTK_OBJECT(menuitem), "toggled", GTK_SIGNAL_FUNC(separate_widget_cb), 
811                                            GUINT_TO_POINTER(SEPARATE_MESSAGE));
812
813
814         menu_set_sensitive(ifactory, "/Summary/Thread view",
815                            prefs_common.enable_thread ? FALSE : TRUE);
816         menu_set_sensitive(ifactory, "/Summary/Unthread view",
817                            prefs_common.enable_thread ? TRUE : FALSE);
818         
819         /*main_window_set_thread_option(mainwin);*/
820
821
822         /* set account selection menu */
823         ac_menu = gtk_item_factory_get_widget
824                 (ifactory, "/Configuration/Change current account");
825         gtk_signal_connect(GTK_OBJECT(ac_menu), "selection_done",
826                            GTK_SIGNAL_FUNC(ac_menu_popup_closed), mainwin);
827         mainwin->ac_menu = ac_menu;
828
829         main_window_set_toolbar_sensitive(mainwin);
830
831         /* show main window */
832         gtk_widget_set_uposition(mainwin->window,
833                                  prefs_common.mainwin_x,
834                                  prefs_common.mainwin_y);
835         gtk_widget_set_usize(window, prefs_common.mainwin_width,
836                              prefs_common.mainwin_height);
837                              
838         gtk_widget_show(mainwin->window);
839
840         /* initialize views */
841         folderview_init(folderview);
842         summary_init(summaryview);
843         messageview_init(messageview);
844         header_window_init(mainwin->headerwin);
845         log_window_init(mainwin->logwin);
846
847
848         mainwin->lock_count = 0;
849         mainwin->cursor_count = 0;
850
851         if (!watch_cursor)
852                 watch_cursor = gdk_cursor_new(GDK_WATCH);
853
854         mainwin_list = g_list_append(mainwin_list, mainwin);
855
856         return mainwin;
857 }
858
859 void main_window_cursor_wait(MainWindow *mainwin)
860 {
861
862         if (mainwin->cursor_count == 0)
863                 gdk_window_set_cursor(mainwin->window->window, watch_cursor);
864
865         mainwin->cursor_count++;
866
867         gdk_flush();
868 }
869
870 void main_window_cursor_normal(MainWindow *mainwin)
871 {
872         if (mainwin->cursor_count)
873                 mainwin->cursor_count--;
874
875         if (mainwin->cursor_count == 0)
876                 gdk_window_set_cursor(mainwin->window->window, NULL);
877
878         gdk_flush();
879 }
880
881 /* lock / unlock the user-interface */
882 void main_window_lock(MainWindow *mainwin)
883 {
884         if (mainwin->lock_count == 0)
885                 gtk_widget_set_sensitive(mainwin->ac_button, FALSE);
886
887         mainwin->lock_count++;
888
889         main_window_set_menu_sensitive(mainwin);
890         main_window_set_toolbar_sensitive(mainwin);
891 }
892
893 void main_window_unlock(MainWindow *mainwin)
894 {
895         if (mainwin->lock_count)
896                 mainwin->lock_count--;
897
898         main_window_set_menu_sensitive(mainwin);
899         main_window_set_toolbar_sensitive(mainwin);
900
901         if (mainwin->lock_count == 0)
902                 gtk_widget_set_sensitive(mainwin->ac_button, TRUE);
903 }
904
905 void main_window_reflect_prefs_all(void)
906 {
907         GList *cur;
908         MainWindow *mainwin;
909
910         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
911                 mainwin = (MainWindow *)cur->data;
912
913                 main_window_show_cur_account(mainwin);
914                 main_window_set_menu_sensitive(mainwin);
915                 main_window_set_toolbar_sensitive(mainwin);
916
917                 if (prefs_common.immediate_exec)
918                         gtk_widget_hide(mainwin->exec_btn);
919                 else
920                         gtk_widget_show(mainwin->exec_btn);
921
922                 summary_change_display_item(mainwin->summaryview);
923                 summary_redisplay_msg(mainwin->summaryview);
924                 headerview_set_visibility(mainwin->messageview->headerview,
925                                           prefs_common.display_header_pane);
926         }
927 }
928
929 void main_window_set_account_menu(GList *account_list)
930 {
931         GList *cur, *cur_ac, *cur_item;
932         GtkWidget *menuitem;
933         MainWindow *mainwin;
934         PrefsAccount *ac_prefs;
935
936         for (cur = mainwin_list; cur != NULL; cur = cur->next) {
937                 mainwin = (MainWindow *)cur->data;
938
939                 /* destroy all previous menu item */
940                 cur_item = GTK_MENU_SHELL(mainwin->ac_menu)->children;
941                 while (cur_item != NULL) {
942                         GList *next = cur_item->next;
943                         gtk_widget_destroy(GTK_WIDGET(cur_item->data));
944                         cur_item = next;
945                 }
946
947                 for (cur_ac = account_list; cur_ac != NULL;
948                      cur_ac = cur_ac->next) {
949                         ac_prefs = (PrefsAccount *)cur_ac->data;
950
951                         menuitem = gtk_menu_item_new_with_label
952                                 (ac_prefs->account_name
953                                  ? ac_prefs->account_name : _("Untitled"));
954                         gtk_widget_show(menuitem);
955                         gtk_menu_append(GTK_MENU(mainwin->ac_menu), menuitem);
956                         gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
957                                            GTK_SIGNAL_FUNC(account_menu_cb),
958                                            ac_prefs);
959                 }
960         }
961 }
962
963 static void main_window_show_cur_account(MainWindow *mainwin)
964 {
965         gchar *buf;
966         gchar *ac_name;
967
968         ac_name = g_strdup(cur_account
969                            ? (cur_account->account_name
970                               ? cur_account->account_name : _("Untitled"))
971                            : _("none"));
972
973         if (cur_account)
974                 buf = g_strdup_printf("%s - %s", ac_name, PROG_VERSION);
975         else
976                 buf = g_strdup(PROG_VERSION);
977         gtk_window_set_title(GTK_WINDOW(mainwin->window), buf);
978         g_free(buf);
979
980         /* buf = g_strdup_printf(_("Current account: %s"), ac_name);
981         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), buf);   */
982         gtk_label_set_text(GTK_LABEL(mainwin->ac_label), ac_name);
983         gtk_widget_queue_resize(mainwin->ac_button);
984         /* g_free(buf); */
985
986         g_free(ac_name);
987 }
988
989 void main_window_separation_change(MainWindow *mainwin, SeparateType type)
990 {
991         GtkWidget *folder_wid  = GTK_WIDGET_PTR(mainwin->folderview);
992         GtkWidget *summary_wid = GTK_WIDGET_PTR(mainwin->summaryview);
993         GtkWidget *message_wid = GTK_WIDGET_PTR(mainwin->messageview);
994
995         if (mainwin->type == type) return;
996
997         /* remove widgets from those containers */
998         gtk_widget_ref(folder_wid);
999         gtk_widget_ref(summary_wid);
1000         gtk_widget_ref(message_wid);
1001         gtk_container_remove
1002                 (GTK_CONTAINER(folder_wid->parent), folder_wid);
1003         gtk_container_remove
1004                 (GTK_CONTAINER(summary_wid->parent), summary_wid);
1005         gtk_container_remove
1006                 (GTK_CONTAINER(message_wid->parent), message_wid);
1007
1008         /* clean containers */
1009         switch (mainwin->type) {
1010         case SEPARATE_NONE:
1011                 gtk_widget_destroy(mainwin->win.sep_none.hpaned);
1012                 break;
1013         case SEPARATE_FOLDER:
1014                 gtk_widget_destroy(mainwin->win.sep_folder.vpaned);
1015                 gtk_widget_destroy(mainwin->win.sep_folder.folderwin);
1016                 break;
1017         case SEPARATE_MESSAGE:
1018                 gtk_widget_destroy(mainwin->win.sep_message.hpaned);
1019                 gtk_widget_destroy(mainwin->win.sep_message.messagewin);
1020                 break;
1021         case SEPARATE_BOTH:
1022                 gtk_widget_destroy(mainwin->win.sep_both.messagewin);
1023                 gtk_widget_destroy(mainwin->win.sep_both.folderwin);
1024                 break;
1025         }
1026
1027         gtk_widget_hide(mainwin->window);
1028         main_window_set_widgets(mainwin, type);
1029         gtk_widget_show(mainwin->window);
1030
1031         gtk_widget_unref(folder_wid);
1032         gtk_widget_unref(summary_wid);
1033         gtk_widget_unref(message_wid);
1034 }
1035
1036 void main_window_get_size(MainWindow *mainwin)
1037 {
1038         GtkAllocation *allocation;
1039
1040         allocation = &(GTK_WIDGET_PTR(mainwin->summaryview)->allocation);
1041
1042         prefs_common.summaryview_width  = allocation->width;
1043
1044         if (mainwin->summaryview->msg_is_toggled_on)
1045                 prefs_common.summaryview_height = allocation->height;
1046
1047         prefs_common.mainview_width     = allocation->width;
1048
1049         allocation = &mainwin->window->allocation;
1050
1051         prefs_common.mainview_height = allocation->height;
1052         prefs_common.mainwin_width   = allocation->width;
1053         prefs_common.mainwin_height  = allocation->height;
1054
1055         allocation = &(GTK_WIDGET_PTR(mainwin->folderview)->allocation);
1056
1057         prefs_common.folderview_width  = allocation->width;
1058         prefs_common.folderview_height = allocation->height;
1059 }
1060
1061 void main_window_get_position(MainWindow *mainwin)
1062 {
1063         gint x, y;
1064
1065         gtkut_widget_get_uposition(mainwin->window, &x, &y);
1066
1067         prefs_common.mainview_x = x;
1068         prefs_common.mainview_y = y;
1069         prefs_common.mainwin_x = x;
1070         prefs_common.mainwin_y = y;
1071
1072         debug_print(_("window position: x = %d, y = %d\n"), x, y);
1073 }
1074
1075 void main_window_empty_trash(MainWindow *mainwin, gboolean confirm)
1076 {
1077         GList *list;
1078
1079         if (confirm) {
1080                 if (alertpanel(_("Empty trash"),
1081                                _("Empty all messages in trash?"),
1082                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
1083                         return;
1084                 manage_window_focus_in(mainwin->window, NULL, NULL);
1085         }
1086
1087         procmsg_empty_trash();
1088
1089         for (list = folder_get_list(); list != NULL; list = list->next) {
1090                 Folder *folder;
1091
1092                 folder = list->data;
1093                 if (folder->trash) {
1094                         folder_item_scan(folder->trash);
1095                         folderview_update_item(folder->trash, TRUE);
1096                 }
1097         }
1098
1099         if (mainwin->summaryview->folder_item &&
1100             mainwin->summaryview->folder_item->stype == F_TRASH)
1101                 gtk_widget_grab_focus(mainwin->folderview->ctree);
1102 }
1103
1104 void main_window_add_mailbox(MainWindow *mainwin)
1105 {
1106         gchar *path;
1107         Folder *folder;
1108
1109         path = input_dialog(_("Add mailbox"),
1110                             _("Input the location of mailbox.\n"
1111                               "If the existing mailbox is specified, it will be\n"
1112                               "scanned automatically."),
1113                             "Mail");
1114         if (!path) return;
1115         if (folder_find_from_path(path)) {
1116                 alertpanel_error(_("The mailbox `%s' already exists."), path);
1117                 g_free(path);
1118                 return;
1119         }
1120         if (!strcmp(path, "Mail"))
1121                 folder = folder_new(F_MH, _("Mailbox"), path);
1122         else
1123                 folder = folder_new(F_MH, g_basename(path), path);
1124         g_free(path);
1125
1126         if (folder->create_tree(folder) < 0) {
1127                 alertpanel_error(_("Creation of the mailbox failed.\n"
1128                                    "Maybe some files already exist, or you don't have the permission to write there."));
1129                 folder_destroy(folder);
1130                 return;
1131         }
1132
1133         folder_add(folder);
1134         folder_set_ui_func(folder, scan_tree_func, mainwin);
1135         folder->scan_tree(folder);
1136         folder_set_ui_func(folder, NULL, NULL);
1137
1138         folderview_set(mainwin->folderview);
1139 }
1140
1141 void main_window_add_mbox(MainWindow *mainwin)
1142 {
1143         gchar *path;
1144         Folder *folder;
1145         FolderItem * item;
1146
1147         path = input_dialog(_("Add mbox mailbox"),
1148                             _("Input the location of mailbox."),
1149                             "mail");
1150
1151         if (!path) return;
1152
1153         if (folder_find_from_path(path)) {
1154                 alertpanel_error(_("The mailbox `%s' already exists."), path);
1155                 g_free(path);
1156                 return;
1157         }
1158
1159         /*
1160         if (!strcmp(path, "Mail"))
1161                 folder = folder_new(F_MBOX, _("Mailbox"), path);
1162                 else
1163         */
1164
1165         folder = folder_new(F_MBOX, g_basename(path), path);
1166         g_free(path);
1167
1168         if (folder->create_tree(folder) < 0) {
1169                 alertpanel_error(_("Creation of the mailbox failed."));
1170                 folder_destroy(folder);
1171                 return;
1172         }
1173
1174         folder_add(folder);
1175
1176         item = folder_item_new(folder->name, NULL);
1177         item->folder = folder;
1178         folder->node = g_node_new(item);
1179
1180         folder->create_folder(folder, item, "inbox");
1181         folder->create_folder(folder, item, "outbox");
1182         folder->create_folder(folder, item, "queue");
1183         folder->create_folder(folder, item, "draft");
1184         folder->create_folder(folder, item, "trash");
1185
1186         folderview_set(mainwin->folderview);
1187 }
1188
1189 typedef enum
1190 {
1191         M_UNLOCKED            = 1 << 0,
1192         M_MSG_EXIST           = 1 << 1,
1193         M_TARGET_EXIST        = 1 << 2,
1194         M_SINGLE_TARGET_EXIST = 1 << 3,
1195         M_EXEC                = 1 << 4,
1196         M_ALLOW_REEDIT        = 1 << 5,
1197         M_HAVE_ACCOUNT        = 1 << 6
1198 } SensitiveCond;
1199
1200 static SensitiveCond main_window_get_current_state(MainWindow *mainwin)
1201 {
1202         SensitiveCond state = 0;
1203         SummarySelection selection;
1204
1205         selection = summary_get_selection_type(mainwin->summaryview);
1206
1207         if (mainwin->lock_count == 0)
1208                 state |= M_UNLOCKED;
1209         if (selection != SUMMARY_NONE)
1210                 state |= M_MSG_EXIST;
1211         if (selection == SUMMARY_SELECTED_SINGLE ||
1212             selection == SUMMARY_SELECTED_MULTIPLE)
1213                 state |= M_TARGET_EXIST;
1214         if (selection == SUMMARY_SELECTED_SINGLE)
1215                 state |= M_SINGLE_TARGET_EXIST;
1216         if (mainwin->summaryview->folder_item &&
1217             mainwin->summaryview->folder_item->folder->type != F_NEWS)
1218                 state |= M_EXEC;
1219         if (selection == SUMMARY_SELECTED_SINGLE &&
1220             (mainwin->summaryview->folder_item &&
1221              mainwin->summaryview->folder_item->stype == F_DRAFT))
1222                 state |= M_ALLOW_REEDIT;
1223         if (cur_account)
1224                 state |= M_HAVE_ACCOUNT;
1225
1226         return state;
1227 }
1228
1229 void main_window_set_toolbar_sensitive(MainWindow *mainwin)
1230 {
1231         SensitiveCond state;
1232         gboolean sensitive;
1233         gint i;
1234
1235         const struct {
1236                 GtkWidget *widget;
1237                 SensitiveCond cond;
1238         } entry[] = {
1239                 {mainwin->get_btn         , M_HAVE_ACCOUNT|M_UNLOCKED},
1240                 {mainwin->getall_btn      , M_HAVE_ACCOUNT|M_UNLOCKED},
1241                 {mainwin->compose_mail_btn, M_HAVE_ACCOUNT},
1242                 {mainwin->compose_news_btn, M_HAVE_ACCOUNT},
1243                 {mainwin->reply_btn       , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1244                 {mainwin->replyall_btn    , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1245                 {mainwin->replysender_btn , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1246                 {mainwin->fwd_btn         , M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST},
1247                 /* {mainwin->prefs_btn      , M_UNLOCKED},
1248                 {mainwin->account_btn    , M_UNLOCKED}, */
1249                 {mainwin->next_btn        , M_MSG_EXIST},
1250                 {mainwin->delete_btn      , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1251                 {mainwin->exec_btn        , M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1252                 {NULL, 0}
1253         };
1254
1255         state = main_window_get_current_state(mainwin);
1256
1257         for (i = 0; entry[i].widget != NULL; i++) {
1258                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1259                 gtk_widget_set_sensitive(entry[i].widget, sensitive);
1260         }
1261 }
1262
1263 void main_window_set_menu_sensitive(MainWindow *mainwin)
1264 {
1265         GtkItemFactory *ifactory;
1266         SensitiveCond state;
1267         gboolean sensitive;
1268         gint i;
1269
1270         static const struct {
1271                 gchar *const entry;
1272                 SensitiveCond cond;
1273         } entry[] = {
1274                 {"/File/Add mailbox..."        , M_UNLOCKED},
1275                 {"/File/Add mbox mailbox..."   , M_UNLOCKED},
1276                 {"/File/Update folder tree"    , M_UNLOCKED},
1277                 {"/File/Folder"                , M_UNLOCKED},
1278                 {"/File/Import mbox file..."   , M_UNLOCKED},
1279                 {"/File/Export to mbox file...", M_UNLOCKED},
1280                 {"/File/Empty trash"           , M_UNLOCKED},
1281                 {"/File/Save as...", M_SINGLE_TARGET_EXIST|M_UNLOCKED},
1282                 {"/File/Print..."  , M_TARGET_EXIST|M_UNLOCKED},
1283                 {"/File/Close", M_UNLOCKED},
1284                 {"/File/Exit" , M_UNLOCKED},
1285
1286                 {"/Message/Get new mail"         , M_UNLOCKED},
1287                 {"/Message/Get from all accounts", M_UNLOCKED},
1288                 {"/Message/Reply"                , M_SINGLE_TARGET_EXIST},
1289                 {"/Message/Reply to sender"      , M_SINGLE_TARGET_EXIST},
1290                 {"/Message/Follow-up and reply to", M_SINGLE_TARGET_EXIST},
1291                 {"/Message/Reply to all"         , M_SINGLE_TARGET_EXIST},
1292                 {"/Message/Forward"              , M_SINGLE_TARGET_EXIST},
1293                 {"/Message/Forward as attachment", M_SINGLE_TARGET_EXIST},
1294                 {"/Message/Open in new window"   , M_SINGLE_TARGET_EXIST},
1295                 {"/Message/Show all header"      , M_SINGLE_TARGET_EXIST},
1296                 {"/Message/View source"          , M_SINGLE_TARGET_EXIST},
1297                 {"/Message/Move...", M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1298                 {"/Message/Copy...", M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1299                 {"/Message/Delete" , M_TARGET_EXIST|M_EXEC|M_UNLOCKED},
1300                 {"/Message/Mark"   , M_TARGET_EXIST},
1301                 {"/Message/Re-edit", M_ALLOW_REEDIT},
1302
1303                 {"/Summary/Delete duplicated messages", M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1304                 {"/Summary/Filter messages"     , M_MSG_EXIST|M_EXEC|M_UNLOCKED},
1305                 {"/Summary/Execute"             , M_MSG_EXIST|M_UNLOCKED},
1306                 {"/Summary/Prev message"        , M_MSG_EXIST},
1307                 {"/Summary/Next message"        , M_MSG_EXIST},
1308                 {"/Summary/Prev marked message" , M_MSG_EXIST},
1309                 {"/Summary/Next marked message" , M_MSG_EXIST},
1310                 {"/Summary/Prev labeled message", M_MSG_EXIST},
1311                 {"/Summary/Next labeled message", M_MSG_EXIST},
1312                 {"/Summary/Next unread message" , M_MSG_EXIST},
1313                 {"/Summary/Sort"                , M_MSG_EXIST},
1314
1315                 {"/Configuration", M_UNLOCKED},
1316
1317                 {NULL, 0}
1318         };
1319
1320         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1321         state = main_window_get_current_state(mainwin);
1322
1323         for (i = 0; entry[i].entry != NULL; i++) {
1324                 sensitive = ((entry[i].cond & state) == entry[i].cond);
1325                 menu_set_sensitive(ifactory, entry[i].entry, sensitive);
1326         }
1327 }
1328
1329 void main_window_popup(MainWindow *mainwin)
1330 {
1331         gint x, y;
1332         gint sx, sy;
1333         GtkWidget *widget;
1334
1335         gdk_window_get_origin(mainwin->window->window, &x, &y);
1336         sx = gdk_screen_width();
1337         sy = gdk_screen_height();
1338         x %= sx; if (x < 0) x += sx;
1339         y %= sy; if (y < 0) y += sy;
1340         gdk_window_move(mainwin->window->window, x, y);
1341         gdk_window_raise(mainwin->window->window);
1342         gdk_window_show(mainwin->window->window);
1343
1344         debug_print("window position: x = %d, y = %d\n", x, y);
1345
1346         switch (mainwin->type) {
1347         case SEPARATE_FOLDER:
1348                 widget = mainwin->win.sep_folder.folderwin;
1349                 gdk_window_get_origin(widget->window, &x, &y);
1350                 x %= sx; if (x < 0) x += sx;
1351                 y %= sy; if (y < 0) y += sy;
1352                 gdk_window_move(widget->window, x, y);
1353                 gdk_window_raise(widget->window);
1354                 break;
1355         case SEPARATE_MESSAGE:
1356                 widget = mainwin->win.sep_message.messagewin;
1357                 gdk_window_get_origin(widget->window, &x, &y);
1358                 x %= sx; if (x < 0) x += sx;
1359                 y %= sy; if (y < 0) y += sy;
1360                 gdk_window_move(widget->window, x, y);
1361                 gdk_window_raise(widget->window);
1362                 break;
1363         case SEPARATE_BOTH:
1364                 widget = mainwin->win.sep_both.folderwin;
1365                 gdk_window_get_origin(widget->window, &x, &y);
1366                 x %= sx; if (x < 0) x += sx;
1367                 y %= sy; if (y < 0) y += sy;
1368                 gdk_window_move(widget->window, x, y);
1369                 gdk_window_raise(widget->window);
1370                 widget = mainwin->win.sep_both.messagewin;
1371                 gdk_window_get_origin(widget->window, &x, &y);
1372                 x %= sx; if (x < 0) x += sx;
1373                 y %= sy; if (y < 0) y += sy;
1374                 gdk_window_move(widget->window, x, y);
1375                 gdk_window_raise(widget->window);
1376                 break;
1377         default:
1378                 break;
1379         }
1380 }
1381
1382 static void main_window_set_widgets(MainWindow *mainwin, SeparateType type)
1383 {
1384         GtkWidget *folderwin = NULL;
1385         GtkWidget *messagewin = NULL;
1386         GtkWidget *hpaned;
1387         GtkWidget *vpaned;
1388         GtkWidget *vbox_body = mainwin->vbox_body;
1389
1390         debug_print(_("Setting widgets..."));
1391
1392         /* create separated window(s) if needed */
1393         if (type & SEPARATE_FOLDER) {
1394                 folderwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1395                 gtk_window_set_policy(GTK_WINDOW(folderwin),
1396                                       TRUE, TRUE, FALSE);
1397                 gtk_widget_set_usize(folderwin, -1,
1398                                      prefs_common.mainview_height);
1399                 gtk_container_set_border_width(GTK_CONTAINER(folderwin),
1400                                                BORDER_WIDTH);
1401                 gtk_signal_connect(GTK_OBJECT(folderwin), "delete_event",
1402                                    GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1403                                    NULL);
1404         }
1405         if (type & SEPARATE_MESSAGE) {
1406                 messagewin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1407                 gtk_window_set_policy(GTK_WINDOW(messagewin),
1408                                       TRUE, TRUE, FALSE);
1409                 gtk_widget_set_usize
1410                         (messagewin, prefs_common.mainview_width,
1411                          prefs_common.mainview_height
1412                          - prefs_common.summaryview_height
1413                          + DEFAULT_HEADERVIEW_HEIGHT);
1414                 gtk_container_set_border_width(GTK_CONTAINER(messagewin),
1415                                                BORDER_WIDTH);
1416                 gtk_signal_connect(GTK_OBJECT(messagewin), "delete_event",
1417                                    GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1418                                    NULL);
1419         }
1420
1421         switch (type) {
1422         case SEPARATE_NONE:
1423                 hpaned = gtk_hpaned_new();
1424                 gtk_widget_show(hpaned);
1425                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
1426                 gtk_paned_add1(GTK_PANED(hpaned),
1427                                GTK_WIDGET_PTR(mainwin->folderview));
1428
1429                 vpaned = gtk_vpaned_new();
1430                 if (mainwin->summaryview->msg_is_toggled_on) {
1431                         gtk_paned_add2(GTK_PANED(hpaned), vpaned);
1432                         gtk_paned_add1(GTK_PANED(vpaned),
1433                                        GTK_WIDGET_PTR(mainwin->summaryview));
1434                 } else {
1435                         gtk_paned_add2(GTK_PANED(hpaned),
1436                                        GTK_WIDGET_PTR(mainwin->summaryview));
1437                         gtk_widget_ref(vpaned);
1438                 }
1439                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->summaryview),
1440                                      prefs_common.summaryview_width,
1441                                      prefs_common.summaryview_height);
1442                 gtk_paned_add2(GTK_PANED(vpaned),
1443                                GTK_WIDGET_PTR(mainwin->messageview));
1444                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->messageview),
1445                                      prefs_common.mainview_width, -1);
1446                 gtk_widget_set_usize(mainwin->window,
1447                                      prefs_common.folderview_width +
1448                                      prefs_common.mainview_width,
1449                                      prefs_common.mainwin_height);
1450                 gtk_widget_show_all(vpaned);
1451
1452                 mainwin->win.sep_none.hpaned = hpaned;
1453                 mainwin->win.sep_none.vpaned = vpaned;
1454                 break;
1455         case SEPARATE_FOLDER:
1456                 vpaned = gtk_vpaned_new();
1457                 if (mainwin->summaryview->msg_is_toggled_on) {
1458                         gtk_box_pack_start(GTK_BOX(vbox_body), vpaned,
1459                                            TRUE, TRUE, 0);
1460                         gtk_paned_add1(GTK_PANED(vpaned),
1461                                        GTK_WIDGET_PTR(mainwin->summaryview));
1462                 } else {
1463                         gtk_box_pack_start(GTK_BOX(vbox_body),
1464                                            GTK_WIDGET_PTR(mainwin->summaryview),
1465                                            TRUE, TRUE, 0);
1466                         gtk_widget_ref(vpaned);
1467                 }
1468                 gtk_paned_add2(GTK_PANED(vpaned),
1469                                GTK_WIDGET_PTR(mainwin->messageview));
1470                 gtk_widget_show_all(vpaned);
1471                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->summaryview),
1472                                      prefs_common.summaryview_width,
1473                                      prefs_common.summaryview_height);
1474                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->messageview),
1475                                      prefs_common.mainview_width, -1);
1476                 gtk_widget_set_usize(mainwin->window,
1477                                      prefs_common.mainview_width,
1478                                      prefs_common.mainview_height);
1479
1480                 gtk_container_add(GTK_CONTAINER(folderwin),
1481                                   GTK_WIDGET_PTR(mainwin->folderview));
1482
1483                 mainwin->win.sep_folder.folderwin = folderwin;
1484                 mainwin->win.sep_folder.vpaned    = vpaned;
1485
1486                 gtk_widget_show_all(folderwin);
1487                 break;
1488         case SEPARATE_MESSAGE:
1489                 hpaned = gtk_hpaned_new();
1490                 gtk_box_pack_start(GTK_BOX(vbox_body), hpaned, TRUE, TRUE, 0);
1491
1492                 gtk_paned_add1(GTK_PANED(hpaned),
1493                                GTK_WIDGET_PTR(mainwin->folderview));
1494                 gtk_paned_add2(GTK_PANED(hpaned),
1495                                GTK_WIDGET_PTR(mainwin->summaryview));
1496                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->summaryview),
1497                                      prefs_common.summaryview_width,
1498                                      prefs_common.summaryview_height);
1499                 gtk_widget_set_usize(mainwin->window,
1500                                      prefs_common.folderview_width +
1501                                      prefs_common.mainview_width,
1502                                      prefs_common.mainwin_height);
1503                 gtk_widget_show_all(hpaned);
1504                 gtk_container_add(GTK_CONTAINER(messagewin),
1505                                   GTK_WIDGET_PTR(mainwin->messageview));
1506
1507                 mainwin->win.sep_message.messagewin = messagewin;
1508                 mainwin->win.sep_message.hpaned     = hpaned;
1509
1510                 gtk_widget_show_all(messagewin);
1511                 break;
1512         case SEPARATE_BOTH:
1513                 gtk_box_pack_start(GTK_BOX(vbox_body),
1514                                    GTK_WIDGET_PTR(mainwin->summaryview),
1515                                    TRUE, TRUE, 0);
1516                 gtk_widget_set_usize(GTK_WIDGET_PTR(mainwin->summaryview),
1517                                      prefs_common.summaryview_width,
1518                                      prefs_common.summaryview_height);
1519                 gtk_widget_set_usize(mainwin->window,
1520                                      prefs_common.mainview_width,
1521                                      prefs_common.mainwin_height);
1522                 gtk_container_add(GTK_CONTAINER(folderwin),
1523                                   GTK_WIDGET_PTR(mainwin->folderview));
1524                 gtk_container_add(GTK_CONTAINER(messagewin),
1525                                   GTK_WIDGET_PTR(mainwin->messageview));
1526
1527                 mainwin->win.sep_both.folderwin = folderwin;
1528                 mainwin->win.sep_both.messagewin = messagewin;
1529
1530                 gtk_widget_show_all(folderwin);
1531                 gtk_widget_show_all(messagewin);
1532                 break;
1533         }
1534
1535         mainwin->type = type;
1536
1537         debug_print(_("done.\n"));
1538 }
1539
1540 #include "pixmaps/stock_mail_receive.xpm"
1541 #include "pixmaps/stock_mail_receive_all.xpm"
1542 #include "pixmaps/stock_mail_compose.xpm"
1543 #include "pixmaps/stock_news_compose.xpm"
1544 #include "pixmaps/stock_mail_reply.xpm"
1545 #include "pixmaps/stock_mail_reply_to_all.xpm"
1546 #include "pixmaps/stock_mail_reply_to_author.xpm"
1547 #include "pixmaps/stock_mail_forward.xpm"
1548 #include "pixmaps/stock_mail_send.xpm"
1549 #include "pixmaps/stock_preferences.xpm"
1550 #include "pixmaps/stock_properties.xpm"
1551 #include "pixmaps/stock_down_arrow.xpm"
1552 #include "pixmaps/stock_close.xpm"
1553 #include "pixmaps/stock_exec.xpm"
1554
1555 #define CREATE_TOOLBAR_ICON(xpm_d) \
1556 { \
1557         icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
1558                                             &container->style->white, \
1559                                             xpm_d); \
1560         icon_wid = gtk_pixmap_new(icon, mask); \
1561 }
1562
1563 static void main_window_toolbar_create(MainWindow *mainwin,
1564                                        GtkWidget *container)
1565 {
1566         GtkWidget *toolbar;
1567         GdkPixmap *icon;
1568         GdkBitmap *mask;
1569         GtkWidget *icon_wid;
1570         GtkWidget *get_btn;
1571         GtkWidget *getall_btn;
1572         GtkWidget *compose_mail_btn;
1573         GtkWidget *compose_news_btn;
1574         GtkWidget *compose_mail_btn_plain;
1575         GtkWidget *compose_news_btn_plain;
1576         GtkWidget *reply_btn;
1577         GtkWidget *replyall_btn;
1578         GtkWidget *replysender_btn;
1579         GtkWidget *fwd_btn;
1580         GtkWidget *send_btn;
1581         /*
1582         GtkWidget *prefs_btn;
1583         GtkWidget *account_btn;
1584         */
1585         GtkWidget *next_btn;
1586         GtkWidget *delete_btn;
1587         GtkWidget *exec_btn;
1588         GtkWidget *compose_type_btn;
1589         GtkWidget *compose_type_arrow;
1590         GtkWidget *compose_box;
1591         GtkWidget *compose_label;
1592         GtkTooltips *tooltips;
1593
1594         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1595                                   GTK_TOOLBAR_BOTH);
1596         gtk_container_add(GTK_CONTAINER(container), toolbar);
1597         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1598         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
1599         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
1600                                     GTK_TOOLBAR_SPACE_LINE);
1601
1602         CREATE_TOOLBAR_ICON(stock_mail_receive_xpm);
1603         get_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1604                                           _("Get"),
1605                                           _("Get new mail from current account"),
1606                                           "Get",
1607                                           icon_wid, toolbar_inc_cb, mainwin);
1608         CREATE_TOOLBAR_ICON(stock_mail_receive_all_xpm);
1609         getall_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1610                                              _("Get all"),
1611                                              _("Get new mail from all accounts"),
1612                                              "Get all",
1613                                              icon_wid,
1614                                              toolbar_inc_all_cb,
1615                                              mainwin);
1616
1617         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1618
1619         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
1620         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1621                                            _("Send"),
1622                                            _("Send queued message(s)"),
1623                                            "Send",
1624                                            icon_wid,
1625                                            toolbar_send_cb,
1626                                            mainwin);
1627
1628         /* to implement Leandro's "combined" compose buttons, we create
1629          * two sets of compose buttons, one for normal (text + icon) 
1630          * toolbar, and one for both text-only and icon-only toolbar;
1631          * we switch between those sets. */
1632
1633         /* insert compose mail button widget */                                    
1634
1635         compose_mail_btn = gtk_button_new();
1636         gtk_widget_show(compose_mail_btn);
1637         tooltips = gtk_tooltips_new();
1638         gtk_tooltips_set_tip(tooltips, compose_mail_btn, 
1639                                                  _("Compose email message"),
1640                                                  _("email"));
1641         compose_box = gtk_vbox_new(0, 0);
1642         gtk_widget_show(compose_box);
1643         
1644         gtk_container_add(GTK_CONTAINER(compose_mail_btn), compose_box);
1645         CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
1646         gtk_box_pack_start(GTK_BOX(compose_box), icon_wid, FALSE, FALSE, 0);
1647
1648         compose_label = gtk_label_new(_("Email"));
1649         gtk_widget_show(compose_label);
1650         gtk_box_pack_start(GTK_BOX(compose_box), compose_label, FALSE, FALSE, 0);
1651         
1652         GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(compose_mail_btn), GTK_CAN_FOCUS);
1653         gtk_button_set_relief(GTK_BUTTON(compose_mail_btn), GTK_RELIEF_NONE);
1654         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar), compose_mail_btn,
1655                 NULL, NULL);
1656
1657         /* insert compose news button widget */
1658
1659         compose_news_btn = gtk_button_new();
1660         gtk_widget_show(compose_news_btn);
1661         tooltips = gtk_tooltips_new();
1662         gtk_tooltips_set_tip(tooltips, compose_news_btn,
1663                                                  _("Compose news article"),
1664                                                  _("news"));
1665         compose_box = gtk_vbox_new(0, 0);
1666         gtk_widget_show(compose_box);
1667         
1668         gtk_container_add(GTK_CONTAINER(compose_news_btn), compose_box);
1669         CREATE_TOOLBAR_ICON(stock_news_compose_xpm);
1670         gtk_box_pack_start(GTK_BOX(compose_box), icon_wid, FALSE, FALSE, 0);
1671
1672         compose_label = gtk_label_new(_("News"));
1673         gtk_widget_show(compose_label);
1674         gtk_box_pack_start(GTK_BOX(compose_box), compose_label, FALSE, FALSE, 0);
1675         
1676         GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(compose_news_btn), GTK_CAN_FOCUS);
1677         gtk_button_set_relief(GTK_BUTTON(compose_news_btn), GTK_RELIEF_NONE);
1678         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar), compose_news_btn,
1679                 NULL, NULL);
1680         
1681         /* insert compose btn plain */
1682         
1683         CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
1684         compose_mail_btn_plain = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1685                                               _("Email"),
1686                                               _("Compose an email message"),
1687                                               "New",
1688                                               icon_wid,
1689                                               toolbar_compose_mail_cb,
1690                                               mainwin);
1691
1692         /* insert compose btn plain */
1693
1694         CREATE_TOOLBAR_ICON(stock_news_compose_xpm);
1695         compose_news_btn_plain = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1696                                               _("News"),
1697                                               _("Compose a news message"),
1698                                               "New",
1699                                               icon_wid,
1700                                               toolbar_compose_news_cb,
1701                                               mainwin);
1702
1703         /* insert compose button type widget */
1704         
1705         compose_type_btn = gtk_button_new();
1706         gtk_widget_show(compose_type_btn);
1707         
1708         compose_type_arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT);
1709         gtk_widget_show(compose_type_arrow);
1710         
1711         gtk_container_add(GTK_CONTAINER(compose_type_btn), compose_type_arrow);
1712
1713         GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(compose_type_btn), GTK_CAN_FOCUS);
1714         gtk_button_set_relief(GTK_BUTTON(compose_type_btn), GTK_RELIEF_NONE);
1715
1716         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar), compose_type_btn,
1717                 NULL, NULL);
1718
1719         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1720         
1721         CREATE_TOOLBAR_ICON(stock_mail_reply_xpm);
1722         reply_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1723                                             _("Reply"),
1724                                             _("Reply to the message"),
1725                                             "Reply",
1726                                             icon_wid,
1727                                             toolbar_reply_cb,
1728                                             mainwin);
1729         CREATE_TOOLBAR_ICON(stock_mail_reply_to_all_xpm);
1730         replyall_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1731                                                _("All"),
1732                                                _("Reply to all"),
1733                                                "Reply to all",
1734                                                icon_wid,
1735                                                toolbar_reply_to_all_cb,
1736                                                mainwin);
1737         CREATE_TOOLBAR_ICON(stock_mail_reply_to_author_xpm);
1738         replysender_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1739                                                   _("Sender"),
1740                                                   _("Reply to sender"),
1741                                                   "Reply to sender",
1742                                                   icon_wid,
1743                                                   toolbar_reply_to_sender_cb,
1744                                                   mainwin);
1745         CREATE_TOOLBAR_ICON(stock_mail_forward_xpm);
1746         fwd_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1747                                           _("Forward"),
1748                                           _("Forward the message"),
1749                                           "Fwd",
1750                                           icon_wid,
1751                                           toolbar_forward_cb,
1752                                           mainwin);
1753
1754         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1755
1756         CREATE_TOOLBAR_ICON(stock_close_xpm);
1757         delete_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1758                                           _("Delete"),
1759                                           _("Delete the message"),
1760                                           "Delete",
1761                                           icon_wid,
1762                                           toolbar_delete_cb,
1763                                           mainwin);
1764
1765         CREATE_TOOLBAR_ICON(stock_exec_xpm);
1766         exec_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1767                                            _("Execute"),
1768                                            _("Execute marked process"),
1769                                            "Execute",
1770                                            icon_wid,
1771                                            toolbar_exec_cb,
1772                                            mainwin);
1773
1774         CREATE_TOOLBAR_ICON(stock_down_arrow_xpm);
1775         next_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1776                                            _("Next"),
1777                                            _("Next unread message"),
1778                                            "Next unread",
1779                                            icon_wid,
1780                                            toolbar_next_unread_cb,
1781                                            mainwin);
1782
1783         /*
1784         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1785
1786         CREATE_TOOLBAR_ICON(stock_preferences_xpm);
1787         prefs_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1788                                             _("Prefs"),
1789                                             _("Common preference"),
1790                                             "Prefs",
1791                                             icon_wid,
1792                                             toolbar_prefs_cb,
1793                                             mainwin);
1794         CREATE_TOOLBAR_ICON(stock_properties_xpm);
1795         account_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1796                                               _("Account"),
1797                                               _("Account setting"),
1798                                               "Account",
1799                                               icon_wid,
1800                                               toolbar_account_cb,
1801                                               mainwin);
1802         gtk_signal_connect(GTK_OBJECT(account_btn), "button_press_event",
1803                            GTK_SIGNAL_FUNC(toolbar_account_button_pressed),
1804                            mainwin);
1805         */
1806
1807         gtk_signal_connect(GTK_OBJECT(compose_type_btn), "clicked",
1808                 GTK_SIGNAL_FUNC(toolbar_popup_compose_type_cb),
1809                 mainwin);
1810
1811         gtk_signal_connect(GTK_OBJECT(compose_mail_btn), "clicked",
1812                 GTK_SIGNAL_FUNC(toolbar_compose_mail_cb),
1813                 mainwin);
1814
1815         gtk_signal_connect(GTK_OBJECT(compose_news_btn), "clicked",
1816                 GTK_SIGNAL_FUNC(toolbar_compose_news_cb),
1817                 mainwin);
1818
1819         mainwin->toolbar         = toolbar;
1820         mainwin->get_btn         = get_btn;
1821         mainwin->getall_btn      = getall_btn;
1822         mainwin->compose_mail_btn = compose_mail_btn;
1823         mainwin->compose_news_btn = compose_news_btn;
1824         mainwin->compose_mail_btn_plain = compose_mail_btn_plain;
1825         mainwin->compose_news_btn_plain = compose_news_btn_plain;
1826         mainwin->reply_btn       = reply_btn;
1827         mainwin->replyall_btn    = replyall_btn;
1828         mainwin->replysender_btn = replysender_btn;
1829         mainwin->fwd_btn         = fwd_btn;
1830         mainwin->send_btn        = send_btn;
1831         /*
1832         mainwin->prefs_btn       = prefs_btn;
1833         mainwin->account_btn     = account_btn;
1834         */
1835         mainwin->next_btn        = next_btn;
1836         mainwin->delete_btn      = delete_btn;
1837         mainwin->exec_btn        = exec_btn;
1838
1839         gtk_widget_show_all(toolbar);
1840
1841         /* activate Leandro menu system */
1842         activate_compose_button(mainwin, 
1843                                 prefs_common.toolbar_style,
1844                                 mainwin->compose_btn_type);
1845 }
1846
1847 /* callback functions */
1848
1849 static void toolbar_popup_compose_type_cb       (GtkWidget      *widget,
1850                                  gpointer data)
1851 {
1852         MainWindow *mainwindow = (MainWindow *) data;
1853         GtkWidget *compose_menu, *compose_item;
1854
1855         g_return_if_fail(mainwindow != NULL);
1856         
1857         compose_menu = gtk_menu_new();
1858         
1859         compose_item = gtk_menu_item_new_with_label(_("Email message"));
1860         gtk_widget_show(compose_item);
1861         gtk_menu_append(GTK_MENU(compose_menu), compose_item);
1862         gtk_signal_connect(GTK_OBJECT(compose_item), "activate",
1863                 GTK_SIGNAL_FUNC(toolbar_popup_compose_type_set),
1864                 mainwindow);
1865         gtk_object_set_data(GTK_OBJECT(compose_item), "entry", GINT_TO_POINTER(COMPOSEBUTTON_MAIL));            
1866         
1867         compose_item = gtk_menu_item_new_with_label(_("News article"));
1868         gtk_widget_show(compose_item);
1869         gtk_menu_append(GTK_MENU(compose_menu), compose_item);
1870         gtk_signal_connect(GTK_OBJECT(compose_item), "activate",
1871                 GTK_SIGNAL_FUNC(toolbar_popup_compose_type_set),
1872                 mainwindow);
1873         gtk_object_set_data(GTK_OBJECT(compose_item), "entry", GINT_TO_POINTER(COMPOSEBUTTON_NEWS));            
1874                 
1875         gtk_menu_popup(GTK_MENU(compose_menu), NULL, NULL, NULL,
1876                 NULL, 1, 0);
1877 }
1878
1879 static void toolbar_popup_compose_type_set(GtkWidget *widget, gpointer data)
1880 {
1881         ComposeButtonType compose_type = GPOINTER_TO_INT( gtk_object_get_data(GTK_OBJECT(widget), "entry") );
1882         MainWindow *mainwindow = (MainWindow *) data;
1883
1884         mainwindow->compose_btn_type = compose_type;
1885
1886         activate_compose_button(mainwindow, prefs_common.toolbar_style, mainwindow->compose_btn_type);
1887 }       
1888
1889 static void toolbar_inc_cb      (GtkWidget      *widget,
1890                                  gpointer        data)
1891 {
1892         MainWindow *mainwin = (MainWindow *)data;
1893
1894         inc_mail_cb(mainwin, 0, NULL);
1895 }
1896
1897 static void toolbar_inc_all_cb  (GtkWidget      *widget,
1898                                  gpointer        data)
1899 {
1900         MainWindow *mainwin = (MainWindow *)data;
1901
1902         inc_all_account_mail_cb(mainwin, 0, NULL);
1903 }
1904
1905 static void toolbar_send_cb     (GtkWidget      *widget,
1906                                  gpointer        data)
1907 {
1908         MainWindow *mainwin = (MainWindow *)data;
1909
1910         send_queue_cb(mainwin, 0, NULL);
1911 }
1912
1913 static void toolbar_compose_cb  (GtkWidget      *widget,
1914                                  gpointer        data)
1915 {
1916         MainWindow *mainwin = (MainWindow *)data;
1917
1918         if (mainwin->compose_btn_type == COMPOSEBUTTON_MAIL)
1919                 compose_cb(mainwin, 0, NULL);
1920         else
1921                 compose_news_cb(mainwin, 0, NULL);
1922 }
1923
1924 static void toolbar_compose_news_cb     (GtkWidget      *widget,
1925                                  gpointer        data)
1926 {
1927         MainWindow *mainwin = (MainWindow *)data;
1928
1929         compose_news_cb(mainwin, 0, NULL);
1930 }
1931
1932 static void toolbar_compose_mail_cb     (GtkWidget      *widget,
1933                                  gpointer        data)
1934 {
1935         MainWindow *mainwin = (MainWindow *)data;
1936
1937         compose_mail_cb(mainwin, 0, NULL);
1938 }
1939
1940 static void toolbar_reply_cb    (GtkWidget      *widget,
1941                                  gpointer        data)
1942 {
1943         MainWindow *mainwin = (MainWindow *)data;
1944
1945         reply_cb(mainwin, COMPOSE_REPLY, NULL);
1946 }
1947
1948 static void toolbar_reply_to_all_cb     (GtkWidget      *widget,
1949                                          gpointer        data)
1950 {
1951         MainWindow *mainwin = (MainWindow *)data;
1952
1953         reply_cb(mainwin, COMPOSE_REPLY_TO_ALL, NULL);
1954 }
1955
1956 static void toolbar_reply_to_sender_cb  (GtkWidget      *widget,
1957                                          gpointer        data)
1958 {
1959         MainWindow *mainwin = (MainWindow *)data;
1960
1961         reply_cb(mainwin, COMPOSE_REPLY_TO_SENDER, NULL);
1962 }
1963
1964 static void toolbar_forward_cb  (GtkWidget      *widget,
1965                                  gpointer        data)
1966 {
1967         MainWindow *mainwin = (MainWindow *)data;
1968
1969         if (prefs_common.forward_as_attachment)
1970                 reply_cb(mainwin, COMPOSE_FORWARD_AS_ATTACH, NULL);
1971         else
1972                 reply_cb(mainwin, COMPOSE_FORWARD, NULL);
1973 }
1974
1975 static void toolbar_delete_cb   (GtkWidget      *widget,
1976                                  gpointer        data)
1977 {
1978         MainWindow *mainwin = (MainWindow *)data;
1979
1980         summary_delete(mainwin->summaryview);
1981 }
1982
1983 static void toolbar_exec_cb     (GtkWidget      *widget,
1984                                  gpointer        data)
1985 {
1986         MainWindow *mainwin = (MainWindow *)data;
1987
1988         summary_execute(mainwin->summaryview);
1989 }
1990
1991 static void toolbar_next_unread_cb      (GtkWidget      *widget,
1992                                          gpointer        data)
1993 {
1994         MainWindow *mainwin = (MainWindow *)data;
1995
1996         next_unread_cb(mainwin, 0, NULL);
1997 }
1998
1999 static void toolbar_prefs_cb    (GtkWidget      *widget,
2000                                  gpointer        data)
2001 {
2002         prefs_common_open();
2003 }
2004
2005 static void toolbar_account_cb  (GtkWidget      *widget,
2006                                  gpointer        data)
2007 {
2008         MainWindow *mainwin = (MainWindow *)data;
2009
2010         prefs_account_open_cb(mainwin, 0, NULL);
2011 }
2012
2013 static void toolbar_account_button_pressed(GtkWidget *widget,
2014                                            GdkEventButton *event,
2015                                            gpointer data)
2016 {
2017         MainWindow *mainwin = (MainWindow *)data;
2018
2019         if (!event) return;
2020         if (event->button != 3) return;
2021
2022         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2023         gtk_object_set_data(GTK_OBJECT(mainwin->ac_menu), "menu_button",
2024                             widget);
2025
2026         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2027                        menu_button_position, widget,
2028                        event->button, event->time);
2029 }
2030
2031 static void ac_label_button_pressed(GtkWidget *widget, GdkEventButton *event,
2032                                     gpointer data)
2033 {
2034         MainWindow *mainwin = (MainWindow *)data;
2035
2036         if (!event) return;
2037
2038         gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
2039         gtk_object_set_data(GTK_OBJECT(mainwin->ac_menu), "menu_button",
2040                             widget);
2041
2042         gtk_menu_popup(GTK_MENU(mainwin->ac_menu), NULL, NULL,
2043                        menu_button_position, widget,
2044                        event->button, event->time);
2045 }
2046
2047 static void ac_menu_popup_closed(GtkMenuShell *menu_shell, gpointer data)
2048 {
2049         MainWindow *mainwin = (MainWindow *)data;
2050         GtkWidget *button;
2051
2052         button = gtk_object_get_data(GTK_OBJECT(menu_shell), "menu_button");
2053         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
2054         gtk_object_remove_data(GTK_OBJECT(mainwin->ac_menu), "menu_button");
2055 }
2056
2057 static gint main_window_close_cb(GtkWidget *widget, GdkEventAny *event,
2058                                  gpointer data)
2059 {
2060         app_exit_cb(data, 0, widget);
2061
2062         return TRUE;
2063 }
2064
2065 static void add_mailbox_cb(MainWindow *mainwin, guint action,
2066                            GtkWidget *widget)
2067 {
2068         main_window_add_mailbox(mainwin);
2069 }
2070
2071 static void add_mbox_cb(MainWindow *mainwin, guint action,
2072                         GtkWidget *widget)
2073 {
2074         main_window_add_mbox(mainwin);
2075 }
2076
2077 static void update_folderview_cb(MainWindow *mainwin, guint action,
2078                                  GtkWidget *widget)
2079 {
2080         summary_show(mainwin->summaryview, NULL, FALSE);
2081         folderview_update_all();
2082 }
2083
2084 static void new_folder_cb(MainWindow *mainwin, guint action,
2085                           GtkWidget *widget)
2086 {
2087         folderview_new_folder(mainwin->folderview);
2088 }
2089
2090 static void rename_folder_cb(MainWindow *mainwin, guint action,
2091                              GtkWidget *widget)
2092 {
2093         folderview_rename_folder(mainwin->folderview);
2094 }
2095
2096 static void delete_folder_cb(MainWindow *mainwin, guint action,
2097                              GtkWidget *widget)
2098 {
2099         folderview_delete_folder(mainwin->folderview);
2100 }
2101
2102 static void import_mbox_cb(MainWindow *mainwin, guint action,
2103                            GtkWidget *widget)
2104 {
2105         import_mbox(mainwin->summaryview->folder_item);
2106 }
2107
2108 static void export_mbox_cb(MainWindow *mainwin, guint action,
2109                            GtkWidget *widget)
2110 {
2111         export_mbox(mainwin->summaryview->folder_item);
2112 }
2113
2114 static void empty_trash_cb(MainWindow *mainwin, guint action,
2115                            GtkWidget *widget)
2116 {
2117         main_window_empty_trash(mainwin, TRUE);
2118 }
2119
2120 static void save_as_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2121 {
2122         summary_save_as(mainwin->summaryview);
2123 }
2124
2125 static void print_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2126 {
2127         summary_print(mainwin->summaryview);
2128 }
2129
2130 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2131 {
2132         if (prefs_common.confirm_on_exit) {
2133                 if (alertpanel(_("Exit"), _("Exit this program?"),
2134                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
2135                         return;
2136                 manage_window_focus_in(mainwin->window, NULL, NULL);
2137         }
2138
2139         app_will_exit(widget, mainwin);
2140 }
2141
2142 static void toggle_folder_cb(MainWindow *mainwin, guint action,
2143                              GtkWidget *widget)
2144 {
2145         switch (mainwin->type) {
2146         case SEPARATE_NONE:
2147         case SEPARATE_MESSAGE:
2148                 break;
2149         case SEPARATE_FOLDER:
2150                 if (GTK_CHECK_MENU_ITEM(widget)->active)
2151                         gtk_widget_show(mainwin->win.sep_folder.folderwin);
2152                 else
2153                         gtk_widget_hide(mainwin->win.sep_folder.folderwin);
2154                 break;
2155         case SEPARATE_BOTH:
2156                 if (GTK_CHECK_MENU_ITEM(widget)->active)
2157                         gtk_widget_show(mainwin->win.sep_both.folderwin);
2158                 else
2159                         gtk_widget_hide(mainwin->win.sep_both.folderwin);
2160                 break;
2161         }
2162 }
2163
2164 static void toggle_message_cb(MainWindow *mainwin, guint action,
2165                               GtkWidget *widget)
2166 {
2167         switch (mainwin->type) {
2168         case SEPARATE_NONE:
2169         case SEPARATE_FOLDER:
2170                 break;
2171         case SEPARATE_MESSAGE:
2172                 if (GTK_CHECK_MENU_ITEM(widget)->active)
2173                         gtk_widget_show(mainwin->win.sep_message.messagewin);
2174                 else
2175                         gtk_widget_hide(mainwin->win.sep_message.messagewin);
2176                 break;
2177         case SEPARATE_BOTH:
2178                 if (GTK_CHECK_MENU_ITEM(widget)->active)
2179                         gtk_widget_show(mainwin->win.sep_both.messagewin);
2180                 else
2181                         gtk_widget_hide(mainwin->win.sep_both.messagewin);
2182                 break;
2183         }
2184 }
2185
2186 static void toggle_toolbar_cb(MainWindow *mainwin, guint action,
2187                               GtkWidget *widget)
2188 {
2189         activate_compose_button(mainwin, (ToolbarStyle)action, 
2190                         mainwin->compose_btn_type);
2191         
2192         switch ((ToolbarStyle)action) {
2193         case TOOLBAR_NONE:
2194                 gtk_widget_hide(mainwin->handlebox);
2195         case TOOLBAR_ICON:
2196                 gtk_toolbar_set_style(GTK_TOOLBAR(mainwin->toolbar),
2197                                       GTK_TOOLBAR_ICONS);
2198                 break;
2199         case TOOLBAR_TEXT:
2200                 gtk_toolbar_set_style(GTK_TOOLBAR(mainwin->toolbar),
2201                                       GTK_TOOLBAR_TEXT);
2202                 break;
2203         case TOOLBAR_BOTH:
2204                 gtk_toolbar_set_style(GTK_TOOLBAR(mainwin->toolbar),
2205                                       GTK_TOOLBAR_BOTH);
2206                 break;
2207         }
2208
2209         if (action != TOOLBAR_NONE) {
2210                 gtk_widget_show(mainwin->handlebox);
2211                 gtk_widget_queue_resize(mainwin->handlebox);
2212         }
2213
2214         mainwin->toolbar_style = (ToolbarStyle)action;
2215         prefs_common.toolbar_style = (ToolbarStyle)action;
2216 }
2217
2218 static void toggle_statusbar_cb(MainWindow *mainwin, guint action,
2219                                 GtkWidget *widget)
2220 {
2221         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2222                 gtk_widget_show(mainwin->hbox_stat);
2223                 prefs_common.show_statusbar = TRUE;
2224         } else {
2225                 gtk_widget_hide(mainwin->hbox_stat);
2226                 prefs_common.show_statusbar = FALSE;
2227         }
2228 }
2229
2230 static void separate_widget_cb(GtkCheckMenuItem *checkitem, guint action, GtkWidget *widget)
2231
2232 {
2233         MainWindow *mainwin;
2234         SeparateType type;
2235
2236         mainwin = (MainWindow *) gtk_object_get_data(GTK_OBJECT(checkitem), "mainwindow");
2237         g_return_if_fail(mainwin != NULL);
2238
2239         type = mainwin->type ^ action;
2240         main_window_separation_change(mainwin, type);
2241
2242         prefs_common.sep_folder = (type & SEPARATE_FOLDER)  != 0;
2243         prefs_common.sep_msg    = (type & SEPARATE_MESSAGE) != 0;
2244 }
2245
2246 static void addressbook_open_cb(MainWindow *mainwin, guint action,
2247                                 GtkWidget *widget)
2248 {
2249         addressbook_open(NULL);
2250 }
2251
2252 static void log_window_show_cb(MainWindow *mainwin, guint action,
2253                                GtkWidget *widget)
2254 {
2255         log_window_show(mainwin->logwin);
2256 }
2257
2258 static void inc_mail_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2259 {
2260         inc_mail(mainwin);
2261 }
2262
2263 static void inc_all_account_mail_cb(MainWindow *mainwin, guint action,
2264                                     GtkWidget *widget)
2265 {
2266         inc_all_account_mail(mainwin);
2267 }
2268
2269 static void send_queue_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2270 {
2271         GList *list;
2272
2273         if (procmsg_send_queue() < 0)
2274                 alertpanel_error(_("Sending queued message failed."));
2275
2276         statusbar_pop_all();
2277
2278         for (list = folder_get_list(); list != NULL; list = list->next) {
2279                 Folder *folder;
2280
2281                 folder = list->data;
2282                 if (folder->queue) {
2283                         folder_item_scan(folder->queue);
2284                         folderview_update_item(folder->queue, TRUE);
2285                 }
2286         }
2287 }
2288
2289 static void compose_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2290 {
2291         if (mainwin->summaryview->folder_item) {
2292                 if (mainwin->summaryview->folder_item->folder->account != NULL
2293                     && mainwin->summaryview->folder_item->folder->account->protocol == A_NNTP)
2294                         compose_new_with_recipient(mainwin->summaryview->folder_item->folder->account, mainwin->summaryview->folder_item->path);
2295                 else
2296                         compose_new_with_folderitem(mainwin->summaryview->folder_item->folder->account, mainwin->summaryview->folder_item);
2297         }
2298         else
2299                 compose_new(NULL);
2300 }
2301
2302 static void compose_mail_cb(MainWindow *mainwin, guint action,
2303                             GtkWidget *widget)
2304 {
2305         PrefsAccount * ac;
2306         GList * list;
2307         GList * cur;
2308
2309         if (mainwin->summaryview->folder_item) {
2310                 ac = mainwin->summaryview->folder_item->folder->account;
2311                 if (ac && ac->protocol != A_NNTP) {
2312                         compose_new_with_folderitem(ac, mainwin->summaryview->folder_item);
2313                         return;
2314                 }
2315         }
2316
2317         if(cur_account && (cur_account->protocol != A_NNTP)) {
2318                 compose_new_with_folderitem(cur_account, mainwin->summaryview->folder_item);
2319                 return;
2320         }
2321
2322         list = account_get_list();
2323         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2324                 ac = (PrefsAccount *) cur->data;
2325                 if (ac->protocol != A_NNTP) {
2326                         compose_new_with_folderitem(ac, mainwin->summaryview->folder_item);
2327                         return;
2328                 }
2329         }
2330 }
2331
2332 static void compose_news_cb(MainWindow *mainwin, guint action,
2333                             GtkWidget *widget)
2334 {
2335         PrefsAccount * ac = NULL;
2336         GList * list;
2337         GList * cur;
2338
2339         if (mainwin->summaryview->folder_item) {
2340                 ac = mainwin->summaryview->folder_item->folder->account;
2341                 if (ac && ac->protocol == A_NNTP) {
2342                         compose_new_with_recipient
2343                                 (ac, mainwin->summaryview->folder_item->path);
2344                         return;
2345                 }
2346         }
2347
2348         list = account_get_list();
2349         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2350                 ac = (PrefsAccount *) cur->data;
2351                 if (ac->protocol == A_NNTP) {
2352                         compose_new(ac);
2353                         return;
2354                 }
2355         }
2356 }
2357
2358 static void reply_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2359 {
2360         GList  *sel = GTK_CLIST(mainwin->summaryview->ctree)->selection;
2361         MsgInfo *msginfo;
2362
2363         msginfo = gtk_ctree_node_get_row_data
2364                 (GTK_CTREE(mainwin->summaryview->ctree),
2365                  mainwin->summaryview->selected);
2366
2367         if (!msginfo) return;
2368
2369         switch (action) {
2370         case COMPOSE_REPLY:
2371                 compose_reply(msginfo, prefs_common.reply_with_quote,
2372                               FALSE, FALSE);
2373                 break;
2374         case COMPOSE_REPLY_TO_SENDER:
2375                 compose_reply(msginfo, prefs_common.reply_with_quote,
2376                               FALSE, TRUE);
2377                 break;
2378         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
2379                 compose_followup_and_reply_to(msginfo,
2380                                               prefs_common.reply_with_quote,
2381                                               FALSE, TRUE);
2382                 break;
2383         case COMPOSE_REPLY_TO_ALL:
2384                 compose_reply(msginfo, prefs_common.reply_with_quote,
2385                               TRUE, FALSE);
2386                 break;
2387         case COMPOSE_FORWARD:
2388                 if (!sel->next) {
2389                         compose_forward(NULL, msginfo, FALSE);
2390                         break;
2391                 }
2392                 /* if (sel->next) FALL_THROUGH */
2393         case COMPOSE_FORWARD_AS_ATTACH:
2394                 {
2395                         GSList *msginfo_list = NULL;
2396                         for ( ; sel != NULL; sel = sel->next)
2397                                 msginfo_list = g_slist_append(msginfo_list, 
2398                                         gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->summaryview->ctree),
2399                                                 GTK_CTREE_NODE(sel->data)));
2400                         compose_forward_multiple(NULL, msginfo_list);
2401                         g_slist_free(msginfo_list);
2402                 }                       
2403                 break;
2404         default:
2405                 g_warning("reply_cb(): invalid action type: %d\n", action);
2406         }
2407
2408         summary_set_marks_selected(mainwin->summaryview);
2409 }
2410
2411 static void move_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2412 {
2413         summary_move_to(mainwin->summaryview);
2414 }
2415
2416 static void copy_to_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2417 {
2418         summary_copy_to(mainwin->summaryview);
2419 }
2420
2421 static void delete_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2422 {
2423         summary_delete(mainwin->summaryview);
2424 }
2425
2426 static void open_msg_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2427 {
2428         summary_open_msg(mainwin->summaryview);
2429 }
2430
2431 static void view_source_cb(MainWindow *mainwin, guint action,
2432                            GtkWidget *widget)
2433 {
2434         summary_view_source(mainwin->summaryview);
2435 }
2436
2437 static void reedit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2438 {
2439         summary_reedit(mainwin->summaryview);
2440 }
2441
2442 static void mark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2443 {
2444         summary_mark(mainwin->summaryview);
2445 }
2446
2447 static void unmark_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2448 {
2449         summary_unmark(mainwin->summaryview);
2450 }
2451
2452 static void mark_as_unread_cb(MainWindow *mainwin, guint action,
2453                               GtkWidget *widget)
2454 {
2455         summary_mark_as_unread(mainwin->summaryview);
2456 }
2457
2458 static void mark_as_read_cb(MainWindow *mainwin, guint action,
2459                             GtkWidget *widget)
2460 {
2461         summary_mark_as_read(mainwin->summaryview);
2462 }
2463
2464 static void set_charset_cb(MainWindow *mainwin, guint action,
2465                            GtkWidget *widget)
2466 {
2467         const gchar *str;
2468
2469         str = conv_get_charset_str((CharSet)action);
2470         g_free(prefs_common.force_charset);
2471         prefs_common.force_charset = str ? g_strdup(str) : NULL;
2472
2473         summary_redisplay_msg(mainwin->summaryview);
2474
2475         debug_print(_("forced charset: %s\n"), str ? str : "Auto-Detect");
2476 }
2477
2478 /*void main_window_set_thread_option(MainWindow *mainwin)
2479 {
2480         GtkItemFactory *ifactory;
2481         gboolean no_item = FALSE;
2482
2483         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
2484
2485         if (mainwin->summaryview == NULL)
2486                 no_item = TRUE;
2487         else if (mainwin->summaryview->folder_item == NULL)
2488                 no_item = TRUE;
2489
2490         if (no_item) {
2491                 menu_set_sensitive(ifactory, "/Summary/Thread view",   FALSE);
2492                 menu_set_sensitive(ifactory, "/Summary/Unthread view", FALSE);
2493         }
2494         else {
2495                 if (mainwin->summaryview->folder_item->prefs->enable_thread) {
2496                         menu_set_sensitive(ifactory,
2497                                            "/Summary/Thread view",   FALSE);
2498                         menu_set_sensitive(ifactory,
2499                                            "/Summary/Unthread view", TRUE);
2500                         summary_thread_build(mainwin->summaryview, TRUE);
2501                 }
2502                 else {
2503                         menu_set_sensitive(ifactory,
2504                                            "/Summary/Thread view",   TRUE);
2505                         menu_set_sensitive(ifactory,
2506                                            "/Summary/Unthread view", FALSE);
2507                         summary_unthread(mainwin->summaryview);
2508                 }
2509                 prefs_folder_item_save_config(mainwin->summaryview->folder_item);
2510         }
2511 }*/
2512
2513 static void thread_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2514 {
2515         /*mainwin->summaryview->folder_item->prefs->enable_thread =
2516                 !mainwin->summaryview->folder_item->prefs->enable_thread;
2517         main_window_set_thread_option(mainwin);
2518         */
2519
2520         GtkItemFactory *ifactory;
2521
2522         ifactory = gtk_item_factory_from_widget(widget);
2523
2524         if (0 == action) {
2525                 summary_thread_build(mainwin->summaryview, FALSE);
2526                 prefs_common.enable_thread = TRUE;
2527                 menu_set_sensitive(ifactory, "/Summary/Thread view",   FALSE);
2528                 menu_set_sensitive(ifactory, "/Summary/Unthread view", TRUE);
2529         } else {
2530                 summary_unthread(mainwin->summaryview);
2531                 prefs_common.enable_thread = FALSE;
2532                 menu_set_sensitive(ifactory, "/Summary/Thread view",   TRUE);
2533                 menu_set_sensitive(ifactory, "/Summary/Unthread view", FALSE);
2534         }
2535 }
2536
2537 static void set_display_item_cb(MainWindow *mainwin, guint action,
2538                                 GtkWidget *widget)
2539 {
2540         prefs_summary_display_item_set();
2541 }
2542
2543 static void sort_summary_cb(MainWindow *mainwin, guint action,
2544                             GtkWidget *widget)
2545 {
2546         summary_sort(mainwin->summaryview, (SummarySortType)action);
2547 }
2548
2549 static void attract_by_subject_cb(MainWindow *mainwin, guint action,
2550                                   GtkWidget *widget)
2551 {
2552         summary_attract_by_subject(mainwin->summaryview);
2553 }
2554
2555 static void delete_duplicated_cb(MainWindow *mainwin, guint action,
2556                                  GtkWidget *widget)
2557 {
2558         summary_delete_duplicated(mainwin->summaryview);
2559 }
2560
2561 static void filter_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2562 {
2563         summary_filter(mainwin->summaryview);
2564 }
2565
2566 static void execute_summary_cb(MainWindow *mainwin, guint action,
2567                                GtkWidget *widget)
2568 {
2569         summary_execute(mainwin->summaryview);
2570 }
2571
2572 static void update_summary_cb(MainWindow *mainwin, guint action,
2573                               GtkWidget *widget)
2574 {
2575         FolderItem *fitem;
2576         FolderView *folderview = mainwin->folderview;
2577
2578         if (!mainwin->summaryview->folder_item) return;
2579         if (!folderview->opened) return;
2580
2581         folder_update_op_count();
2582
2583         fitem = gtk_ctree_node_get_row_data(GTK_CTREE(folderview->ctree),
2584                                             folderview->opened);
2585         if (!fitem) return;
2586
2587         summary_show(mainwin->summaryview, fitem, TRUE);
2588 }
2589
2590 static void prev_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2591 {
2592         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2593 }
2594
2595 static void next_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2596 {
2597         summary_step(mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2598 }
2599
2600 static void next_unread_cb(MainWindow *mainwin, guint action,
2601                            GtkWidget *widget)
2602 {
2603         summary_select_next_unread(mainwin->summaryview);
2604 }
2605
2606 static void next_marked_cb(MainWindow *mainwin, guint action,
2607                            GtkWidget *widget)
2608 {
2609         summary_select_next_marked(mainwin->summaryview);
2610 }
2611
2612 static void prev_marked_cb(MainWindow *mainwin, guint action,
2613                            GtkWidget *widget)
2614 {
2615         summary_select_prev_marked(mainwin->summaryview);
2616 }
2617
2618 static void next_labeled_cb(MainWindow *mainwin, guint action,
2619                            GtkWidget *widget)
2620 {
2621         summary_select_next_labeled(mainwin->summaryview);
2622 }
2623
2624 static void prev_labeled_cb(MainWindow *mainwin, guint action,
2625                            GtkWidget *widget)
2626 {
2627         summary_select_prev_labeled(mainwin->summaryview);
2628 }
2629
2630 static void goto_folder_cb(MainWindow *mainwin, guint action,
2631                            GtkWidget *widget)
2632 {
2633         FolderItem *to_folder;
2634
2635         to_folder = foldersel_folder_sel(NULL, NULL);
2636
2637         if (to_folder)
2638                 folderview_select(mainwin->folderview, to_folder);
2639 }
2640
2641 static void copy_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2642 {
2643         messageview_copy_clipboard(mainwin->messageview);
2644 }
2645
2646 static void allsel_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
2647 {
2648         if (GTK_WIDGET_HAS_FOCUS(mainwin->summaryview->ctree))
2649                 summary_select_all(mainwin->summaryview);
2650         else if (mainwin->summaryview->msg_is_toggled_on &&
2651                  GTK_WIDGET_HAS_FOCUS(mainwin->messageview->textview->text))
2652                 messageview_select_all(mainwin->messageview);
2653 }
2654
2655 static void prefs_common_open_cb(MainWindow *mainwin, guint action,
2656                                  GtkWidget *widget)
2657 {
2658         prefs_common_open();
2659 }
2660
2661 static void prefs_filter_open_cb(MainWindow *mainwin, guint action,
2662                                  GtkWidget *widget)
2663 {
2664         prefs_filter_open();
2665 }
2666
2667 static void prefs_scoring_open_cb(MainWindow *mainwin, guint action,
2668                                   GtkWidget *widget)
2669 {
2670         prefs_scoring_open(NULL);
2671 }
2672
2673 static void prefs_filtering_open_cb(MainWindow *mainwin, guint action,
2674                                     GtkWidget *widget)
2675 {
2676         prefs_filtering_open();
2677 }
2678
2679 static void prefs_account_open_cb(MainWindow *mainwin, guint action,
2680                                   GtkWidget *widget)
2681 {
2682         if (!cur_account) {
2683                 new_account_cb(mainwin, 0, widget);
2684         } else {
2685                 gboolean prev_default = cur_account->is_default;
2686
2687                 prefs_account_open(cur_account);
2688                 if (!prev_default && cur_account->is_default)
2689                         account_set_as_default(cur_account);
2690                 account_save_config_all();
2691                 account_set_menu();
2692                 main_window_reflect_prefs_all();
2693         }
2694 }
2695
2696 static void new_account_cb(MainWindow *mainwin, guint action,
2697                            GtkWidget *widget)
2698 {
2699         account_edit_open();
2700         if (!compose_get_compose_list()) account_add();
2701 }
2702
2703 static void account_menu_cb(GtkMenuItem *menuitem, gpointer data)
2704 {
2705         cur_account = (PrefsAccount *)data;
2706         main_window_reflect_prefs_all();
2707 }
2708
2709 static void manual_open_cb(MainWindow *mainwin, guint action,
2710                            GtkWidget *widget)
2711 {
2712         manual_open((ManualLang)action);
2713 }
2714
2715 static void scan_tree_func(Folder *folder, FolderItem *item, gpointer data)
2716 {
2717         MainWindow *mainwin = (MainWindow *)data;
2718         gchar *str;
2719
2720         if (item->path)
2721                 str = g_strdup_printf(_("Scanning folder %s%c%s ..."),
2722                                       LOCAL_FOLDER(folder)->rootpath,
2723                                       G_DIR_SEPARATOR,
2724                                       item->path);
2725         else
2726                 str = g_strdup_printf(_("Scanning folder %s ..."),
2727                                       LOCAL_FOLDER(folder)->rootpath);
2728
2729         STATUSBAR_PUSH(mainwin, str);
2730         STATUSBAR_POP(mainwin);
2731         g_free(str);
2732 }
2733
2734 static void activate_compose_button (MainWindow *mainwin,
2735                                 ToolbarStyle style,
2736                                 ComposeButtonType type)
2737 {
2738         if (style == TOOLBAR_NONE) 
2739                 return;
2740
2741         if (style == TOOLBAR_BOTH) {    
2742                 gtk_widget_hide(mainwin->compose_mail_btn_plain);
2743                 gtk_widget_hide(mainwin->compose_news_btn_plain);
2744                 gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? mainwin->compose_mail_btn 
2745                         : mainwin->compose_news_btn);
2746                 gtk_widget_show(type == COMPOSEBUTTON_NEWS ? mainwin->compose_news_btn
2747                         : mainwin->compose_mail_btn);
2748                 mainwin->compose_btn_type = type;       
2749         }
2750         else {
2751                 gtk_widget_hide(mainwin->compose_news_btn);
2752                 gtk_widget_hide(mainwin->compose_mail_btn);
2753                 gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? mainwin->compose_mail_btn_plain 
2754                         : mainwin->compose_news_btn_plain);
2755                 gtk_widget_show(type == COMPOSEBUTTON_NEWS ? mainwin->compose_news_btn_plain
2756                         : mainwin->compose_mail_btn_plain);
2757                 mainwin->compose_btn_type = type;               
2758         }
2759 }
2760
2761 static void prefs_templates_open_cb(MainWindow *mainwin, guint action,
2762                                     GtkWidget *widget)
2763 {
2764         prefs_templates_open();
2765 }