2006-05-18 [paul] 2.2.0cvs32
[claws.git] / src / toolbar.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /*
21  * General functions for accessing address book files.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <dirent.h>
35 #include <sys/stat.h>
36 #include <math.h>
37 #include <setjmp.h>
38
39 #include "mainwindow.h"
40 #include "summaryview.h"
41 #include "compose.h"
42 #include "utils.h"
43 #include "xml.h"
44 #include "mgutils.h"
45 #include "prefs_gtk.h"
46 #include "codeconv.h"
47 #include "stock_pixmap.h"
48 #include "manage_window.h"
49 #include "gtkutils.h"
50 #include "toolbar.h"
51 #include "menu.h"
52 #include "inc.h"
53 #include "action.h"
54 #include "prefs_actions.h"
55 #include "prefs_common.h"
56 #include "prefs_toolbar.h"
57 #include "alertpanel.h"
58
59 /* elements */
60 #define TOOLBAR_TAG_INDEX        "toolbar"
61 #define TOOLBAR_TAG_ITEM         "item"
62 #define TOOLBAR_TAG_SEPARATOR    "separator"
63
64 #define TOOLBAR_ICON_FILE   "file"    
65 #define TOOLBAR_ICON_TEXT   "text"     
66 #define TOOLBAR_ICON_ACTION "action"    
67
68 gboolean      toolbar_is_duplicate              (gint           action,
69                                                  ToolbarType    source);
70 static void   toolbar_parse_item                (XMLFile        *file,
71                                                  ToolbarType    source);
72
73 static gint   toolbar_ret_val_from_text         (const gchar    *text);
74 static gchar *toolbar_ret_text_from_val         (gint           val);
75
76 static void   toolbar_set_default_main          (void);
77 static void   toolbar_set_default_compose       (void);
78 static void   toolbar_set_default_msgview       (void);
79
80 static void     toolbar_style                   (ToolbarType     type, 
81                                                  guint           action, 
82                                                  gpointer        data);
83
84 static MainWindow *get_mainwin                  (gpointer data);
85 static void activate_compose_button             (Toolbar        *toolbar,
86                                                  ToolbarStyle    style,
87                                                  ComposeButtonType type);
88
89 /* toolbar callbacks */
90 static void toolbar_reply                       (gpointer        data, 
91                                                  guint           action);
92 static void toolbar_delete_cb                   (GtkWidget      *widget,
93                                                  gpointer        data);
94 static void toolbar_trash_cb                    (GtkWidget      *widget,
95                                                  gpointer        data);
96
97 static void toolbar_compose_cb                  (GtkWidget      *widget,
98                                                  gpointer        data);
99
100 static void toolbar_learn_cb                    (GtkWidget      *widget,
101                                                  gpointer        data);
102
103 static void toolbar_reply_cb                    (GtkWidget      *widget,
104                                                  gpointer        data);
105
106 static void toolbar_reply_to_all_cb             (GtkWidget      *widget,
107                                                  gpointer        data);
108
109 static void toolbar_reply_to_list_cb            (GtkWidget      *widget,
110                                                  gpointer        data);
111
112 static void toolbar_reply_to_sender_cb          (GtkWidget      *widget,
113                                                  gpointer        data);
114
115 static void toolbar_forward_cb                  (GtkWidget      *widget,
116                                                  gpointer        data);
117
118 static void toolbar_prev_unread_cb              (GtkWidget      *widget,
119                                                  gpointer        data);
120 static void toolbar_next_unread_cb              (GtkWidget      *widget,
121                                                  gpointer        data);
122
123 static void toolbar_ignore_thread_cb            (GtkWidget      *widget,
124                                                  gpointer        data);
125
126 static void toolbar_print_cb                    (GtkWidget      *widget,
127                                                  gpointer        data);
128
129 static void toolbar_actions_execute_cb          (GtkWidget      *widget,
130                                                  gpointer        data);
131
132
133 static void toolbar_send_cb                     (GtkWidget      *widget,
134                                                  gpointer        data);
135 static void toolbar_send_later_cb               (GtkWidget      *widget,
136                                                  gpointer        data);
137 static void toolbar_draft_cb                    (GtkWidget      *widget,
138                                                  gpointer        data);
139 static void toolbar_insert_cb                   (GtkWidget      *widget,
140                                                  gpointer        data);
141 static void toolbar_attach_cb                   (GtkWidget      *widget,
142                                                  gpointer        data);
143 static void toolbar_sig_cb                      (GtkWidget      *widget,
144                                                  gpointer        data);
145 static void toolbar_ext_editor_cb               (GtkWidget      *widget,
146                                                  gpointer        data);
147 static void toolbar_linewrap_current_cb         (GtkWidget      *widget,
148                                                  gpointer        data);
149 static void toolbar_linewrap_all_cb             (GtkWidget      *widget,
150                                                  gpointer        data);
151 static void toolbar_addrbook_cb                 (GtkWidget      *widget, 
152                                                  gpointer        data);
153 #ifdef USE_ASPELL
154 static void toolbar_check_spelling_cb           (GtkWidget      *widget, 
155                                                  gpointer        data);
156 #endif
157
158 struct {
159         gchar *index_str;
160         const gchar *descr;
161 } toolbar_text [] = {
162         { "A_RECEIVE_ALL",      N_("Receive Mail on all Accounts")         },
163         { "A_RECEIVE_CUR",      N_("Receive Mail on current Account")      },
164         { "A_SEND_QUEUED",      N_("Send Queued Messages")                 },
165         { "A_COMPOSE_EMAIL",    N_("Compose Email")                        },
166         { "A_COMPOSE_NEWS",     N_("Compose News")                         },
167         { "A_REPLY_MESSAGE",    N_("Reply to Message")                     },
168         { "A_REPLY_SENDER",     N_("Reply to Sender")                      },
169         { "A_REPLY_ALL",        N_("Reply to All")                         },
170         { "A_REPLY_ML",         N_("Reply to Mailing-list")                },
171         { "A_FORWARD",          N_("Forward Message")                      }, 
172         { "A_TRASH",            N_("Trash Message")                        },
173         { "A_DELETE_REAL",      N_("Delete Message")                       },
174         { "A_EXECUTE",          N_("Execute")                              },
175         { "A_GOTO_PREV",        N_("Go to Previous Unread Message")        },
176         { "A_GOTO_NEXT",        N_("Go to Next Unread Message")            },
177         { "A_IGNORE_THREAD",    N_("Ignore thread")                        },
178         { "A_PRINT",            N_("Print")                                },
179         { "A_LEARN_SPAM",       N_("Learn Spam or Ham")                    },
180
181         { "A_SEND",             N_("Send Message")                         },
182         { "A_SENDL",            N_("Put into queue folder and send later") },
183         { "A_DRAFT",            N_("Save to draft folder")                 },
184         { "A_INSERT",           N_("Insert file")                          },   
185         { "A_ATTACH",           N_("Attach file")                          },
186         { "A_SIG",              N_("Insert signature")                     },
187         { "A_EXTEDITOR",        N_("Edit with external editor")            },
188         { "A_LINEWRAP_CURRENT", N_("Wrap long lines of current paragraph") }, 
189         { "A_LINEWRAP_ALL",     N_("Wrap all long lines")                  }, 
190         { "A_ADDRBOOK",         N_("Address book")                         },
191 #ifdef USE_ASPELL
192         { "A_CHECK_SPELLING",   N_("Check spelling")                       },
193 #endif
194         { "A_SYL_ACTIONS",      N_("Sylpheed-Claws Actions Feature")       }, 
195         { "A_SEPARATOR",        "Separator"                             }
196 };
197
198 /* struct holds configuration files and a list of
199  * currently active toolbar items 
200  * TOOLBAR_MAIN, TOOLBAR_COMPOSE and TOOLBAR_MSGVIEW
201  * give us an index
202  */
203 struct {
204         const gchar  *conf_file;
205         GSList       *item_list;
206 } toolbar_config[3] = {
207         { "toolbar_main.xml",    NULL},
208         { "toolbar_compose.xml", NULL}, 
209         { "toolbar_msgview.xml", NULL}
210 };
211
212 static GtkItemFactoryEntry reply_entries[] =
213 {
214         {N_("/Reply with _quote"), NULL,    toolbar_reply, COMPOSE_REPLY_WITH_QUOTE, NULL},
215         {N_("/_Reply without quote"), NULL, toolbar_reply, COMPOSE_REPLY_WITHOUT_QUOTE, NULL}
216 };
217 static GtkItemFactoryEntry replyall_entries[] =
218 {
219         {N_("/Reply to all with _quote"), "<shift>A", toolbar_reply, COMPOSE_REPLY_TO_ALL_WITH_QUOTE, NULL},
220         {N_("/_Reply to all without quote"), "a",     toolbar_reply, COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE, NULL}
221 };
222 static GtkItemFactoryEntry replylist_entries[] =
223 {
224         {N_("/Reply to list with _quote"),    NULL, toolbar_reply, COMPOSE_REPLY_TO_LIST_WITH_QUOTE, NULL},
225         {N_("/_Reply to list without quote"), NULL, toolbar_reply, COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE, NULL}
226 };
227 static GtkItemFactoryEntry replysender_entries[] =
228 {
229         {N_("/Reply to sender with _quote"),    NULL, toolbar_reply, COMPOSE_REPLY_TO_SENDER_WITH_QUOTE, NULL},
230         {N_("/_Reply to sender without quote"), NULL, toolbar_reply, COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE, NULL}
231 };
232 static GtkItemFactoryEntry forward_entries[] =
233 {
234         {N_("/_Forward"),               "f",        toolbar_reply, COMPOSE_FORWARD_INLINE, NULL},
235         {N_("/For_ward as attachment"), "<shift>F", toolbar_reply, COMPOSE_FORWARD_AS_ATTACH, NULL},
236         {N_("/Redirec_t"),              NULL,       toolbar_reply, COMPOSE_REDIRECT, NULL}
237 };
238
239
240 gint toolbar_ret_val_from_descr(const gchar *descr)
241 {
242         gint i;
243
244         for (i = 0; i < N_ACTION_VAL; i++) {
245                 if (g_utf8_collate(gettext(toolbar_text[i].descr), descr) == 0)
246                                 return i;
247         }
248         
249         return -1;
250 }
251
252 gchar *toolbar_ret_descr_from_val(gint val)
253 {
254         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
255
256         return gettext(toolbar_text[val].descr);
257 }
258
259 static gint toolbar_ret_val_from_text(const gchar *text)
260 {
261         gint i;
262         
263         for (i = 0; i < N_ACTION_VAL; i++) {
264                 if (g_utf8_collate(toolbar_text[i].index_str, text) == 0)
265                                 return i;
266         }
267
268         return -1;
269 }
270
271 static gchar *toolbar_ret_text_from_val(gint val)
272 {
273         g_return_val_if_fail(val >=0 && val < N_ACTION_VAL, NULL);
274
275         return toolbar_text[val].index_str;
276 }
277
278 gboolean toolbar_is_duplicate(gint action, ToolbarType source)
279 {
280         GSList *cur;
281
282         if ((action == A_SEPARATOR) || (action == A_SYL_ACTIONS)) 
283                 return FALSE;
284
285         for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
286                 ToolbarItem *item = (ToolbarItem*) cur->data;
287                 
288                 if (item->index == action)
289                         return TRUE;
290         }
291         return FALSE;
292 }
293
294 /* depending on toolbar type this function 
295    returns a list of available toolbar events being 
296    displayed by prefs_toolbar
297 */
298 GList *toolbar_get_action_items(ToolbarType source)
299 {
300         GList *items = NULL;
301         gint i = 0;
302         
303         if (source == TOOLBAR_MAIN) {
304                 gint main_items[]   = { A_RECEIVE_ALL,   A_RECEIVE_CUR,   A_SEND_QUEUED,
305                                         A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER, 
306                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD, 
307                                         A_TRASH , A_DELETE_REAL,       A_EXECUTE,       A_GOTO_PREV, 
308                                         A_GOTO_NEXT,    A_IGNORE_THREAD,  A_PRINT,
309                                         A_ADDRBOOK,     A_LEARN_SPAM, A_SYL_ACTIONS };
310
311                 for (i = 0; i < sizeof main_items / sizeof main_items[0]; i++)  {
312                         items = g_list_append(items, gettext(toolbar_text[main_items[i]].descr));
313                 }       
314         }
315         else if (source == TOOLBAR_COMPOSE) {
316                 gint comp_items[] =   { A_SEND,          A_SENDL,        A_DRAFT,
317                                         A_INSERT,        A_ATTACH,       A_SIG,
318                                         A_EXTEDITOR,     A_LINEWRAP_CURRENT,     
319                                         A_LINEWRAP_ALL,  A_ADDRBOOK,
320 #ifdef USE_ASPELL
321                                         A_CHECK_SPELLING, 
322 #endif
323                                         A_SYL_ACTIONS };        
324
325                 for (i = 0; i < sizeof comp_items / sizeof comp_items[0]; i++) 
326                         items = g_list_append(items, gettext(toolbar_text[comp_items[i]].descr));
327         }
328         else if (source == TOOLBAR_MSGVIEW) {
329                 gint msgv_items[] =   { A_COMPOSE_EMAIL, A_REPLY_MESSAGE, A_REPLY_SENDER,
330                                         A_REPLY_ALL,     A_REPLY_ML,      A_FORWARD,
331                                         A_TRASH, A_DELETE_REAL,       A_GOTO_PREV,        A_GOTO_NEXT,
332                                         A_ADDRBOOK,      A_LEARN_SPAM, A_SYL_ACTIONS }; 
333
334                 for (i = 0; i < sizeof msgv_items / sizeof msgv_items[0]; i++) 
335                         items = g_list_append(items, gettext(toolbar_text[msgv_items[i]].descr));
336         }
337
338         return items;
339 }
340
341 static void toolbar_parse_item(XMLFile *file, ToolbarType source)
342 {
343         GList *attr;
344         gchar *name, *value;
345         ToolbarItem *item = NULL;
346         gboolean rewrite = FALSE;
347
348         attr = xml_get_current_tag_attr(file);
349         item = g_new0(ToolbarItem, 1);
350         while( attr ) {
351                 name = ((XMLAttr *)attr->data)->name;
352                 value = ((XMLAttr *)attr->data)->value;
353                 
354                 if (g_utf8_collate(name, TOOLBAR_ICON_FILE) == 0) 
355                         item->file = g_strdup (value);
356                 else if (g_utf8_collate(name, TOOLBAR_ICON_TEXT) == 0)
357                         item->text = g_strdup (value);
358                 else if (g_utf8_collate(name, TOOLBAR_ICON_ACTION) == 0)
359                         item->index = toolbar_ret_val_from_text(value);
360                 if (item->index == -1 && !strcmp(value, "A_DELETE")) {
361                         /* switch button */
362                         item->index = A_TRASH;
363                         g_free(item->file);
364                         item->file = g_strdup("trash_btn");
365                         g_free(item->text);
366                         item->text = g_strdup(_("Trash"));
367                         rewrite = TRUE;
368                 }
369                 attr = g_list_next(attr);
370         }
371         if (item->index != -1) {
372                 
373                 if (!toolbar_is_duplicate(item->index, source)) 
374                         toolbar_config[source].item_list = g_slist_append(toolbar_config[source].item_list,
375                                                                          item);
376         }
377         if (rewrite) {
378                 toolbar_save_config_file(source);
379         }
380 }
381
382 static void toolbar_set_default_main(void) 
383 {
384         struct {
385                 gint action;
386                 gint icon;
387                 gchar *text;
388         } default_toolbar[] = {
389                 { A_RECEIVE_ALL,   STOCK_PIXMAP_MAIL_RECEIVE_ALL,     _("Get Mail") },
390                 { A_SEPARATOR,     0,                                 ("")         }, 
391                 { A_SEND_QUEUED,   STOCK_PIXMAP_MAIL_SEND_QUEUE,      _("Send")    },
392                 { A_COMPOSE_EMAIL, STOCK_PIXMAP_MAIL_COMPOSE,         _("Compose") },
393                 { A_SEPARATOR,     0,                                 ("")         },
394                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
395                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
396                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
397                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
398                 { A_SEPARATOR,     0,                                 ("")         },
399                 { A_TRASH,         STOCK_PIXMAP_TRASH,                _("Trash")   },
400 #ifdef USE_SPAMASSASSIN_PLUGIN
401                 { A_LEARN_SPAM,    STOCK_PIXMAP_SPAM_BTN,             _("Spam")    },
402 #endif
403                 { A_SEPARATOR,     0,                                 ("")         },
404                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
405         };
406         
407         gint i;
408         
409         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
410                 
411                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
412                 
413                 if (default_toolbar[i].action != A_SEPARATOR) {
414                         
415                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
416                         
417                         toolbar_item->file  = g_strdup(file);
418                         toolbar_item->index = default_toolbar[i].action;
419                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
420                 } else {
421
422                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
423                         toolbar_item->index = A_SEPARATOR;
424                 }
425                 
426                 if (toolbar_item->index != -1) {
427                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MAIN)) 
428                                 toolbar_config[TOOLBAR_MAIN].item_list = 
429                                         g_slist_append(toolbar_config[TOOLBAR_MAIN].item_list, toolbar_item);
430                 }       
431         }
432 }
433
434 static void toolbar_set_default_compose(void)
435 {
436         struct {
437                 gint action;
438                 gint icon;
439                 gchar *text;
440         } default_toolbar[] = {
441                 { A_SEND,               STOCK_PIXMAP_MAIL_SEND,         _("Send")               },
442                 { A_SENDL,              STOCK_PIXMAP_MAIL_SEND_QUEUE,   _("Send later")         },
443                 { A_DRAFT,              STOCK_PIXMAP_MAIL,              _("Draft")              },
444                 { A_SEPARATOR,          0,                               ("")                   }, 
445                 { A_INSERT,             STOCK_PIXMAP_INSERT_FILE,       _("Insert")             },
446                 { A_ATTACH,             STOCK_PIXMAP_MAIL_ATTACH,       _("Attach")             },
447                 { A_SEPARATOR,          0,                               ("")                   },
448                 { A_ADDRBOOK,           STOCK_PIXMAP_ADDRESS_BOOK,      _("Address")            }
449         };
450         
451         gint i;
452
453         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
454                 
455                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
456                 
457                 if (default_toolbar[i].action != A_SEPARATOR) {
458                         
459                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
460                         
461                         toolbar_item->file  = g_strdup(file);
462                         toolbar_item->index = default_toolbar[i].action;
463                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
464                 } else {
465
466                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
467                         toolbar_item->index = A_SEPARATOR;
468                 }
469                 
470                 if (toolbar_item->index != -1) {
471                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_COMPOSE)) 
472                                 toolbar_config[TOOLBAR_COMPOSE].item_list = 
473                                         g_slist_append(toolbar_config[TOOLBAR_COMPOSE].item_list, toolbar_item);
474                 }       
475         }
476 }
477
478 static void toolbar_set_default_msgview(void)
479 {
480         struct {
481                 gint action;
482                 gint icon;
483                 gchar *text;
484         } default_toolbar[] = {
485                 { A_REPLY_MESSAGE, STOCK_PIXMAP_MAIL_REPLY,           _("Reply")   }, 
486                 { A_REPLY_ALL,     STOCK_PIXMAP_MAIL_REPLY_TO_ALL,    _("All")     },
487                 { A_REPLY_SENDER,  STOCK_PIXMAP_MAIL_REPLY_TO_AUTHOR, _("Sender")  },
488                 { A_FORWARD,       STOCK_PIXMAP_MAIL_FORWARD,         _("Forward") },
489                 { A_SEPARATOR,     0,                                 ("")         },
490                 { A_TRASH,         STOCK_PIXMAP_TRASH,                _("Trash")   },
491 #ifdef USE_SPAMASSASSIN_PLUGIN
492                 { A_LEARN_SPAM,    STOCK_PIXMAP_SPAM_BTN,             _("Spam")    },
493 #endif
494                 { A_GOTO_NEXT,     STOCK_PIXMAP_DOWN_ARROW,           _("Next")    }
495         };
496         
497         gint i;
498
499         for (i = 0; i < sizeof(default_toolbar) / sizeof(default_toolbar[0]); i++) {
500                 
501                 ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
502                 
503                 if (default_toolbar[i].action != A_SEPARATOR) {
504                         
505                         gchar *file = stock_pixmap_get_name((StockPixmap)default_toolbar[i].icon);
506                         
507                         toolbar_item->file  = g_strdup(file);
508                         toolbar_item->index = default_toolbar[i].action;
509                         toolbar_item->text  = g_strdup(default_toolbar[i].text);
510                 } else {
511
512                         toolbar_item->file  = g_strdup(TOOLBAR_TAG_SEPARATOR);
513                         toolbar_item->index = A_SEPARATOR;
514                 }
515                 
516                 if (toolbar_item->index != -1) {
517                         if ( !toolbar_is_duplicate(toolbar_item->index, TOOLBAR_MSGVIEW)) 
518                                 toolbar_config[TOOLBAR_MSGVIEW].item_list = 
519                                         g_slist_append(toolbar_config[TOOLBAR_MSGVIEW].item_list, toolbar_item);
520                 }       
521         }
522 }
523
524 void toolbar_set_default(ToolbarType source)
525 {
526         if (source == TOOLBAR_MAIN)
527                 toolbar_set_default_main();
528         else if  (source == TOOLBAR_COMPOSE)
529                 toolbar_set_default_compose();
530         else if  (source == TOOLBAR_MSGVIEW)
531                 toolbar_set_default_msgview();
532 }
533
534 void toolbar_save_config_file(ToolbarType source)
535 {
536         GSList *cur;
537         FILE *fp;
538         PrefFile *pfile;
539         gchar *fileSpec = NULL;
540
541         debug_print("save Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
542
543         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
544         pfile = prefs_write_open(fileSpec);
545         g_free( fileSpec );
546         if( pfile ) {
547                 fp = pfile->fp;
548                 fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n", CS_INTERNAL);
549
550                 fprintf(fp, "<%s>\n", TOOLBAR_TAG_INDEX);
551
552                 for (cur = toolbar_config[source].item_list; cur != NULL; cur = cur->next) {
553                         ToolbarItem *toolbar_item = (ToolbarItem*) cur->data;
554                         
555                         if (toolbar_item->index != A_SEPARATOR) {
556                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"",
557                                         TOOLBAR_TAG_ITEM, 
558                                         TOOLBAR_ICON_FILE, toolbar_item->file,
559                                         TOOLBAR_ICON_TEXT);
560                                 xml_file_put_escape_str(fp, toolbar_item->text);
561                                 fprintf(fp, "\" %s=\"%s\"/>\n",
562                                         TOOLBAR_ICON_ACTION, 
563                                         toolbar_ret_text_from_val(toolbar_item->index));
564                         } else {
565                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
566                         }
567                 }
568
569                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
570         
571                 if (prefs_file_close (pfile) < 0 ) 
572                         g_warning("failed to write toolbar configuration to file\n");
573         } else
574                 g_warning("failed to open toolbar configuration file for writing\n");
575 }
576
577 void toolbar_read_config_file(ToolbarType source)
578 {
579         XMLFile *file   = NULL;
580         gchar *fileSpec = NULL;
581         GList *attr;
582         gboolean retVal;
583         jmp_buf    jumper;
584
585         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
586
587         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
588         file = xml_open_file(fileSpec);
589         g_free(fileSpec);
590
591         toolbar_clear_list(source);
592
593         if (file) {
594                 if ((setjmp(jumper))
595                 || (xml_get_dtd(file))
596                 || (xml_parse_next_tag(file))
597                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
598                         xml_close_file(file);
599                         return;
600                 }
601
602                 attr = xml_get_current_tag_attr(file);
603                 
604                 retVal = TRUE;
605                 for (;;) {
606                         if (!file->level) 
607                                 break;
608                         /* Get item tag */
609                         if (xml_parse_next_tag(file)) 
610                                 longjmp(jumper, 1);
611
612                         /* Get next tag (icon, icon_text or icon_action) */
613                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
614                                 toolbar_parse_item(file, source);
615                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
616                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
617                         
618                                 item->file   = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
619                                 item->index  = A_SEPARATOR;
620                                 toolbar_config[source].item_list = 
621                                         g_slist_append(toolbar_config[source].item_list, item);
622                         }
623
624                 }
625                 xml_close_file(file);
626         }
627
628         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
629
630                 if (source == TOOLBAR_MAIN) 
631                         toolbar_set_default(TOOLBAR_MAIN);
632                 else if (source == TOOLBAR_COMPOSE) 
633                         toolbar_set_default(TOOLBAR_COMPOSE);
634                 else if (source == TOOLBAR_MSGVIEW) 
635                         toolbar_set_default(TOOLBAR_MSGVIEW);
636                 else {          
637                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
638                         return;
639                 }
640
641                 toolbar_save_config_file(source);
642         }
643 }
644
645 /*
646  * clears list of toolbar items read from configuration files
647  */
648 void toolbar_clear_list(ToolbarType source)
649 {
650         while (toolbar_config[source].item_list != NULL) {
651                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
652                 
653                 toolbar_config[source].item_list = 
654                         g_slist_remove(toolbar_config[source].item_list, item);
655
656                 g_free(item->file);
657                 g_free(item->text);
658                 g_free(item);   
659         }
660         g_slist_free(toolbar_config[source].item_list);
661 }
662
663
664 /* 
665  * return list of Toolbar items
666  */
667 GSList *toolbar_get_list(ToolbarType source)
668 {
669         GSList *list = NULL;
670
671         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
672                 list = toolbar_config[source].item_list;
673
674         return list;
675 }
676
677 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
678 {
679         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
680
681         toolbar_item->file  = g_strdup(t_item->file);
682         toolbar_item->text  = g_strdup(t_item->text);
683         toolbar_item->index = t_item->index;
684         
685         toolbar_config[source].item_list = 
686                 g_slist_append(toolbar_config[source].item_list,
687                                toolbar_item);
688 }
689
690 void toolbar_action_execute(GtkWidget    *widget,
691                             GSList       *action_list, 
692                             gpointer     data,
693                             gint         source) 
694 {
695         GSList *cur, *lop;
696         gchar *action, *action_p;
697         gboolean found = FALSE;
698         gint i = 0;
699
700         for (cur = action_list; cur != NULL;  cur = cur->next) {
701                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
702
703                 if (widget == act->widget) {
704                         
705                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
706                                 action = g_strdup((gchar*)lop->data);
707
708                                 action_p = strstr(action, ": ");
709                                 action_p[0] = 0x00;
710                                 if (g_utf8_collate(act->name, action) == 0) {
711                                         found = TRUE;
712                                         g_free(action);
713                                         break;
714                                 } else 
715                                         i++;
716                                 g_free(action);
717                         }
718                         if (found) 
719                                 break;
720                 }
721         }
722
723         if (found) 
724                 actions_execute(data, i, widget, source);
725         else
726                 g_warning ("Error: did not find Sylpheed Action to execute");
727 }
728
729 static void activate_compose_button (Toolbar           *toolbar,
730                                      ToolbarStyle      style,
731                                      ComposeButtonType type)
732 {
733         if ((!toolbar->compose_mail_btn) || (!toolbar->compose_news_btn))
734                 return;
735
736         gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? toolbar->compose_mail_btn 
737                         : toolbar->compose_news_btn);
738         gtk_widget_show(type == COMPOSEBUTTON_NEWS ? toolbar->compose_news_btn
739                         : toolbar->compose_mail_btn);
740         toolbar->compose_btn_type = type;       
741 }
742
743 void toolbar_set_compose_button(Toolbar            *toolbar, 
744                                 ComposeButtonType  compose_btn_type)
745 {
746         if (toolbar->compose_btn_type != compose_btn_type)
747                 activate_compose_button(toolbar, 
748                                         prefs_common.toolbar_style,
749                                         compose_btn_type);
750 }
751
752 static void activate_learn_button (Toolbar           *toolbar,
753                                      ToolbarStyle      style,
754                                      LearnButtonType type)
755 {
756         if ((!toolbar->learn_spam_btn) || (!toolbar->learn_ham_btn))
757                 return;
758
759         gtk_widget_hide(type == LEARN_SPAM ? toolbar->learn_ham_btn 
760                         : toolbar->learn_spam_btn);
761         gtk_widget_show(type == LEARN_SPAM ? toolbar->learn_spam_btn
762                         : toolbar->learn_ham_btn);
763         toolbar->learn_btn_type = type; 
764 }
765
766 void toolbar_set_learn_button(Toolbar            *toolbar, 
767                                 LearnButtonType  learn_btn_type)
768 {
769         if (toolbar->learn_btn_type != learn_btn_type)
770                 activate_learn_button(toolbar, 
771                                         prefs_common.toolbar_style,
772                                         learn_btn_type);
773 }
774
775 void toolbar_toggle(guint action, gpointer data)
776 {
777         MainWindow *mainwin = (MainWindow*)data;
778         GList *list;
779         GList *cur;
780
781         g_return_if_fail(mainwin != NULL);
782
783         toolbar_style(TOOLBAR_MAIN, action, mainwin);
784
785         list = compose_get_compose_list();
786         for (cur = list; cur != NULL; cur = cur->next) {
787                 toolbar_style(TOOLBAR_COMPOSE, action, cur->data);
788         }
789         list = messageview_get_msgview_list();
790         for (cur = list; cur != NULL; cur = cur->next) {
791                 toolbar_style(TOOLBAR_MSGVIEW, action, cur->data);
792         }
793         
794 }
795
796 void toolbar_set_style(GtkWidget *toolbar_wid, GtkWidget *handlebox_wid, guint action)
797 {
798         switch ((ToolbarStyle)action) {
799         case TOOLBAR_NONE:
800                 gtk_widget_hide(handlebox_wid);
801                 break;
802         case TOOLBAR_ICON:
803                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
804                                       GTK_TOOLBAR_ICONS);
805                 break;
806         case TOOLBAR_TEXT:
807                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
808                                       GTK_TOOLBAR_TEXT);
809                 break;
810         case TOOLBAR_BOTH:
811                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
812                                       GTK_TOOLBAR_BOTH);
813                 break;
814         case TOOLBAR_BOTH_HORIZ:
815                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
816                                       GTK_TOOLBAR_BOTH_HORIZ);
817                 break;
818         default:
819                 return;
820         }
821
822         prefs_common.toolbar_style = (ToolbarStyle)action;
823         gtk_widget_set_size_request(handlebox_wid, 1, -1);
824         
825         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
826                 gtk_widget_show(handlebox_wid);
827                 gtk_widget_queue_resize(handlebox_wid);
828         }
829 }
830 /*
831  * Change the style of toolbar
832  */
833 static void toolbar_style(ToolbarType type, guint action, gpointer data)
834 {
835         GtkWidget  *handlebox_wid;
836         GtkWidget  *toolbar_wid;
837         MainWindow *mainwin = (MainWindow*)data;
838         Compose    *compose = (Compose*)data;
839         MessageView *msgview = (MessageView*)data;
840         
841         g_return_if_fail(data != NULL);
842         
843         switch (type) {
844         case TOOLBAR_MAIN:
845                 handlebox_wid = mainwin->handlebox;
846                 toolbar_wid = mainwin->toolbar->toolbar;
847                 break;
848         case TOOLBAR_COMPOSE:
849                 handlebox_wid = compose->handlebox;
850                 toolbar_wid = compose->toolbar->toolbar;
851                 break;
852         case TOOLBAR_MSGVIEW: 
853                 handlebox_wid = msgview->handlebox;
854                 toolbar_wid = msgview->toolbar->toolbar;
855                 break;
856         default:
857
858                 return;
859         }
860         toolbar_set_style(toolbar_wid, handlebox_wid, action);
861 }
862
863 /* Toolbar handling */
864 static void toolbar_inc_cb(GtkWidget    *widget,
865                            gpointer      data)
866 {
867         ToolbarItem *toolbar_item = (ToolbarItem*)data;
868         MainWindow *mainwin;
869
870         g_return_if_fail(toolbar_item != NULL);
871
872         switch (toolbar_item->type) {
873         case TOOLBAR_MAIN:
874                 mainwin = (MainWindow*)toolbar_item->parent;    
875                 inc_mail_cb(mainwin, 0, NULL);
876                 break;
877         default:
878                 break;
879         }
880 }
881
882 static void toolbar_inc_all_cb(GtkWidget        *widget,
883                                gpointer          data)
884 {
885         ToolbarItem *toolbar_item = (ToolbarItem*)data;
886         MainWindow *mainwin;
887
888         g_return_if_fail(toolbar_item != NULL);
889
890         switch (toolbar_item->type) {
891         case TOOLBAR_MAIN:
892                 mainwin = (MainWindow*)toolbar_item->parent;
893                 inc_all_account_mail_cb(mainwin, 0, NULL);
894                 break;
895         default:
896                 break;
897         }
898 }
899
900 static void toolbar_send_queued_cb(GtkWidget *widget,gpointer data)
901 {
902         ToolbarItem *toolbar_item = (ToolbarItem*)data;
903         MainWindow *mainwin;
904
905         g_return_if_fail(toolbar_item != NULL);
906
907         switch (toolbar_item->type) {
908         case TOOLBAR_MAIN:
909                 mainwin = (MainWindow*)toolbar_item->parent;
910                 send_queue_cb(mainwin, 0, NULL);
911                 break;
912         default:
913                 break;
914         }
915 }
916
917 static void toolbar_exec_cb(GtkWidget   *widget,
918                             gpointer     data)
919 {
920         MainWindow *mainwin = get_mainwin(data);
921
922         g_return_if_fail(mainwin != NULL);
923         summary_execute(mainwin->summaryview);
924 }
925
926 /*
927  * Delete current/selected(s) message(s)
928  */
929 static void toolbar_trash_cb(GtkWidget *widget, gpointer data)
930 {
931         ToolbarItem *toolbar_item = (ToolbarItem*)data;
932         MainWindow *mainwin;
933
934         g_return_if_fail(toolbar_item != NULL);
935         g_return_if_fail(toolbar_item->parent);
936         
937         switch (toolbar_item->type) {
938         case TOOLBAR_MSGVIEW:
939                 messageview_delete((MessageView *)toolbar_item->parent);
940                 break;
941         case TOOLBAR_MAIN:
942                 mainwin = (MainWindow *)toolbar_item->parent;
943                 summary_delete_trash(mainwin->summaryview);
944                 break;
945         default: 
946                 debug_print("toolbar event not supported\n");
947                 break;
948         }
949 }
950
951 /*
952  * Delete current/selected(s) message(s)
953  */
954 static void toolbar_delete_cb(GtkWidget *widget, gpointer data)
955 {
956         ToolbarItem *toolbar_item = (ToolbarItem*)data;
957         MainWindow *mainwin;
958
959         g_return_if_fail(toolbar_item != NULL);
960         g_return_if_fail(toolbar_item->parent);
961         
962         switch (toolbar_item->type) {
963         case TOOLBAR_MSGVIEW:
964                 messageview_delete((MessageView *)toolbar_item->parent);
965                 break;
966         case TOOLBAR_MAIN:
967                 mainwin = (MainWindow *)toolbar_item->parent;
968                 summary_delete(mainwin->summaryview);
969                 break;
970         default: 
971                 debug_print("toolbar event not supported\n");
972                 break;
973         }
974 }
975
976
977 /*
978  * Compose new message
979  */
980 static void toolbar_compose_cb(GtkWidget *widget, gpointer data)
981 {
982         ToolbarItem *toolbar_item = (ToolbarItem*)data;
983         MainWindow *mainwin;
984         MessageView *msgview;
985
986         g_return_if_fail(toolbar_item != NULL);
987
988         switch (toolbar_item->type) {
989         case TOOLBAR_MAIN:
990                 mainwin = (MainWindow*)toolbar_item->parent;
991                 if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
992                         compose_news_cb(mainwin, 0, NULL);
993                 else
994                         compose_mail_cb(mainwin, 0, NULL);
995                 break;
996         case TOOLBAR_MSGVIEW:
997                 msgview = (MessageView*)toolbar_item->parent;
998                 compose_new_with_folderitem(NULL, 
999                                             msgview->msginfo->folder);
1000                 break;  
1001         default:
1002                 debug_print("toolbar event not supported\n");
1003         }
1004 }
1005
1006 static void toolbar_learn_cb(GtkWidget *widget, gpointer data)
1007 {
1008         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1009         MainWindow *mainwin;
1010         MessageView *msgview;
1011
1012         g_return_if_fail(toolbar_item != NULL);
1013
1014         switch (toolbar_item->type) {
1015         case TOOLBAR_MAIN:
1016                 mainwin = (MainWindow*)toolbar_item->parent;
1017                 if (mainwin->toolbar->learn_btn_type == LEARN_SPAM) 
1018                         mainwindow_learn(mainwin, TRUE);
1019                 else
1020                         mainwindow_learn(mainwin, FALSE);
1021                 break;
1022         case TOOLBAR_MSGVIEW:
1023                 msgview = (MessageView*)toolbar_item->parent;
1024                 if (msgview->toolbar->learn_btn_type == LEARN_SPAM) 
1025                         messageview_learn(msgview, TRUE);
1026                 else
1027                         messageview_learn(msgview, FALSE);
1028                 break;
1029         default:
1030                 debug_print("toolbar event not supported\n");
1031         }
1032 }
1033
1034
1035 /*
1036  * Reply Message
1037  */
1038 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
1039 {
1040         toolbar_reply(data, prefs_common.reply_with_quote ? 
1041                       COMPOSE_REPLY_WITH_QUOTE : COMPOSE_REPLY_WITHOUT_QUOTE);
1042 }
1043
1044
1045 /*
1046  * Reply message to Sender and All recipients
1047  */
1048 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
1049 {
1050         toolbar_reply(data,
1051                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1052                       : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE);
1053 }
1054
1055
1056 /*
1057  * Reply to Mailing List
1058  */
1059 static void toolbar_reply_to_list_cb(GtkWidget *widget, gpointer data)
1060 {
1061         toolbar_reply(data, 
1062                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1063                       : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE);
1064 }
1065
1066
1067 /*
1068  * Reply to sender of message
1069  */ 
1070 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
1071 {
1072         toolbar_reply(data, 
1073                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1074                       : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE);
1075 }
1076
1077 /*
1078  * Open addressbook
1079  */ 
1080 static void toolbar_addrbook_cb(GtkWidget *widget, gpointer data)
1081 {
1082         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1083         Compose *compose;
1084
1085         g_return_if_fail(toolbar_item != NULL);
1086
1087         switch (toolbar_item->type) {
1088         case TOOLBAR_MAIN:
1089         case TOOLBAR_MSGVIEW:
1090                 compose = NULL;
1091                 break;
1092         case TOOLBAR_COMPOSE:
1093                 compose = (Compose *)toolbar_item->parent;
1094                 break;
1095         default:
1096                 return;
1097         }
1098         addressbook_open(compose);
1099 }
1100
1101
1102 /*
1103  * Forward current/selected(s) message(s)
1104  */
1105 static void toolbar_forward_cb(GtkWidget *widget, gpointer data)
1106 {
1107         toolbar_reply(data, COMPOSE_FORWARD);
1108 }
1109
1110 /*
1111  * Goto Prev Unread Message
1112  */
1113 static void toolbar_prev_unread_cb(GtkWidget *widget, gpointer data)
1114 {
1115         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1116         MainWindow *mainwin;
1117         MessageView *msgview;
1118
1119         g_return_if_fail(toolbar_item != NULL);
1120
1121         switch (toolbar_item->type) {
1122         case TOOLBAR_MAIN:
1123                 mainwin = (MainWindow*)toolbar_item->parent;
1124                 summary_select_prev_unread(mainwin->summaryview);
1125                 break;
1126                 
1127         case TOOLBAR_MSGVIEW:
1128                 msgview = (MessageView*)toolbar_item->parent;
1129                 msgview->updating = TRUE;
1130                 summary_select_prev_unread(msgview->mainwin->summaryview);
1131                 msgview->updating = FALSE;
1132
1133                 if (msgview->deferred_destroy) {
1134                         debug_print("messageview got away!\n");
1135                         messageview_destroy(msgview);
1136                         return;
1137                 }
1138                 
1139                 /* Now we need to update the messageview window */
1140                 if (msgview->mainwin->summaryview->selected) {
1141                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
1142                         
1143                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
1144                                                                         msgview->mainwin->summaryview->selected);
1145                        
1146                         messageview_show(msgview, msginfo, 
1147                                          msgview->all_headers);
1148                 } else {
1149                         gtk_widget_destroy(msgview->window);
1150                 }
1151                 break;
1152         default:
1153                 debug_print("toolbar event not supported\n");
1154         }
1155 }
1156
1157 /*
1158  * Goto Next Unread Message
1159  */
1160 static void toolbar_next_unread_cb(GtkWidget *widget, gpointer data)
1161 {
1162         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1163         MainWindow *mainwin;
1164         MessageView *msgview;
1165
1166         g_return_if_fail(toolbar_item != NULL);
1167
1168         switch (toolbar_item->type) {
1169         case TOOLBAR_MAIN:
1170                 mainwin = (MainWindow*)toolbar_item->parent;
1171                 summary_select_next_unread(mainwin->summaryview);
1172                 break;
1173                 
1174         case TOOLBAR_MSGVIEW:
1175                 msgview = (MessageView*)toolbar_item->parent;
1176                 msgview->updating = TRUE;
1177                 summary_select_next_unread(msgview->mainwin->summaryview);
1178                 msgview->updating = FALSE;
1179
1180                 if (msgview->deferred_destroy) {
1181                         debug_print("messageview got away!\n");
1182                         messageview_destroy(msgview);
1183                         return;
1184                 }
1185
1186                 /* Now we need to update the messageview window */
1187                 if (msgview->mainwin->summaryview->selected) {
1188                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
1189                         
1190                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
1191                                                                         msgview->mainwin->summaryview->selected);
1192                        
1193                         messageview_show(msgview, msginfo, 
1194                                          msgview->all_headers);
1195                 } else {
1196                         gtk_widget_destroy(msgview->window);
1197                 }
1198                 break;
1199         default:
1200                 debug_print("toolbar event not supported\n");
1201         }
1202 }
1203
1204 static void toolbar_ignore_thread_cb(GtkWidget *widget, gpointer data)
1205 {
1206         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1207         MainWindow *mainwin;
1208
1209         g_return_if_fail(toolbar_item != NULL);
1210
1211         switch (toolbar_item->type) {
1212         case TOOLBAR_MAIN:
1213                 mainwin = (MainWindow *) toolbar_item->parent;
1214                 summary_toggle_ignore_thread(mainwin->summaryview);
1215                 break;
1216         case TOOLBAR_MSGVIEW:
1217                 /* TODO: see toolbar_next_unread_cb() if you need
1218                  * this in the message view */
1219                 break;
1220         default:
1221                 debug_print("toolbar event not supported\n");
1222                 break;
1223         }
1224 }
1225
1226 static void toolbar_print_cb(GtkWidget *widget, gpointer data)
1227 {
1228         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1229         MainWindow *mainwin;
1230
1231         g_return_if_fail(toolbar_item != NULL);
1232
1233         switch (toolbar_item->type) {
1234         case TOOLBAR_MAIN:
1235                 mainwin = (MainWindow *) toolbar_item->parent;
1236                 summary_print(mainwin->summaryview);
1237                 break;
1238         case TOOLBAR_MSGVIEW:
1239                 /* TODO: see toolbar_next_unread_cb() if you need
1240                  * this in the message view */
1241                 break;
1242         default:
1243                 debug_print("toolbar event not supported\n");
1244                 break;
1245         }
1246 }
1247
1248 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1249 {
1250         compose_toolbar_cb(A_SEND, data);
1251 }
1252
1253 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1254 {
1255         compose_toolbar_cb(A_SENDL, data);
1256 }
1257
1258 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1259 {
1260         compose_toolbar_cb(A_DRAFT, data);
1261 }
1262
1263 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1264 {
1265         compose_toolbar_cb(A_INSERT, data);
1266 }
1267
1268 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1269 {
1270         compose_toolbar_cb(A_ATTACH, data);
1271 }
1272
1273 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1274 {
1275         compose_toolbar_cb(A_SIG, data);
1276 }
1277
1278 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1279 {
1280         compose_toolbar_cb(A_EXTEDITOR, data);
1281 }
1282
1283 static void toolbar_linewrap_current_cb(GtkWidget *widget, gpointer data)
1284 {
1285         compose_toolbar_cb(A_LINEWRAP_CURRENT, data);
1286 }
1287
1288 static void toolbar_linewrap_all_cb(GtkWidget *widget, gpointer data)
1289 {
1290         compose_toolbar_cb(A_LINEWRAP_ALL, data);
1291 }
1292
1293 #ifdef USE_ASPELL
1294 static void toolbar_check_spelling_cb(GtkWidget *widget, gpointer data)
1295 {
1296         compose_toolbar_cb(A_CHECK_SPELLING, data);
1297 }
1298 #endif
1299 /*
1300  * Execute actions from toolbar
1301  */
1302 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1303 {
1304         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1305         GSList *action_list;
1306         MainWindow *mainwin;
1307         Compose *compose;
1308         MessageView *msgview;
1309         gpointer parent = toolbar_item->parent;
1310
1311         g_return_if_fail(toolbar_item != NULL);
1312
1313         switch (toolbar_item->type) {
1314         case TOOLBAR_MAIN:
1315                 mainwin = (MainWindow*)parent;
1316                 action_list = mainwin->toolbar->action_list;
1317                 break;
1318         case TOOLBAR_COMPOSE:
1319                 compose = (Compose*)parent;
1320                 action_list = compose->toolbar->action_list;
1321                 break;
1322         case TOOLBAR_MSGVIEW:
1323                 msgview = (MessageView*)parent;
1324                 action_list = msgview->toolbar->action_list;
1325                 break;
1326         default:
1327                 debug_print("toolbar event not supported\n");
1328                 return;
1329         }
1330         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1331 }
1332
1333 static MainWindow *get_mainwin(gpointer data)
1334 {
1335         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1336         MainWindow *mainwin = NULL;
1337         MessageView *msgview;
1338
1339         g_return_val_if_fail(toolbar_item != NULL, NULL);
1340
1341         switch(toolbar_item->type) {
1342         case TOOLBAR_MAIN:
1343                 mainwin = (MainWindow*)toolbar_item->parent;
1344                 break;
1345         case TOOLBAR_MSGVIEW:
1346                 msgview = (MessageView*)toolbar_item->parent;
1347                 mainwin = (MainWindow*)msgview->mainwin;
1348                 break;
1349         default:
1350                 break;
1351         }
1352
1353         return mainwin;
1354 }
1355
1356 static void toolbar_buttons_cb(GtkWidget   *widget, 
1357                                ToolbarItem *item)
1358 {
1359         gint num_items;
1360         gint i;
1361         struct {
1362                 gint   index;
1363                 void (*func)(GtkWidget *widget, gpointer data);
1364         } callbacks[] = {
1365                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1366                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1367                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1368                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1369                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1370                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1371                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1372                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1373                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1374                 { A_FORWARD,            toolbar_forward_cb              },
1375                 { A_TRASH,              toolbar_trash_cb                },
1376                 { A_DELETE_REAL,        toolbar_delete_cb               },
1377                 { A_EXECUTE,            toolbar_exec_cb                 },
1378                 { A_GOTO_PREV,          toolbar_prev_unread_cb          },
1379                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1380                 { A_IGNORE_THREAD,      toolbar_ignore_thread_cb        },
1381                 { A_PRINT,              toolbar_print_cb                },
1382                 { A_LEARN_SPAM,         toolbar_learn_cb                },
1383
1384                 { A_SEND,               toolbar_send_cb                 },
1385                 { A_SENDL,              toolbar_send_later_cb           },
1386                 { A_DRAFT,              toolbar_draft_cb                },
1387                 { A_INSERT,             toolbar_insert_cb               },
1388                 { A_ATTACH,             toolbar_attach_cb               },
1389                 { A_SIG,                toolbar_sig_cb                  },
1390                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1391                 { A_LINEWRAP_CURRENT,   toolbar_linewrap_current_cb     },
1392                 { A_LINEWRAP_ALL,       toolbar_linewrap_all_cb         },
1393                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1394 #ifdef USE_ASPELL
1395                 { A_CHECK_SPELLING,     toolbar_check_spelling_cb       },
1396 #endif
1397                 { A_SYL_ACTIONS,        toolbar_actions_execute_cb      }
1398         };
1399
1400         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1401
1402         for (i = 0; i < num_items; i++) {
1403                 if (callbacks[i].index == item->index) {
1404                         callbacks[i].func(widget, item);
1405                         return;
1406                 }
1407         }
1408 }
1409
1410 /**
1411  * Create a new toolbar with specified type
1412  * if a callback list is passed it will be used before the 
1413  * common callback list
1414  **/
1415 Toolbar *toolbar_create(ToolbarType      type, 
1416                         GtkWidget       *container,
1417                         gpointer         data)
1418 {
1419         ToolbarItem *toolbar_item;
1420
1421         GtkWidget *toolbar;
1422         GtkWidget *icon_wid = NULL;
1423         GtkWidget *icon_news;
1424         GtkWidget *icon_ham;
1425         GtkWidget *item;
1426         GtkWidget *item_news;
1427         GtkWidget *item_ham;
1428         guint n_menu_entries;
1429         ComboButton *getall_combo;
1430         ComboButton *reply_combo;
1431         ComboButton *replyall_combo;
1432         ComboButton *replylist_combo;
1433         ComboButton *replysender_combo;
1434         ComboButton *fwd_combo;
1435         ComboButton *compose_combo;
1436
1437         GtkTooltips *toolbar_tips;
1438         ToolbarSylpheedActions *action_item;
1439         GSList *cur;
1440         GSList *toolbar_list;
1441         Toolbar *toolbar_data;
1442
1443         
1444         toolbar_tips = gtk_tooltips_new();
1445         
1446         toolbar_read_config_file(type);
1447         toolbar_list = toolbar_get_list(type);
1448
1449         toolbar_data = g_new0(Toolbar, 1); 
1450
1451         toolbar = gtk_toolbar_new();
1452         gtk_container_add(GTK_CONTAINER(container), toolbar);
1453         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1454         gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
1455         gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
1456         
1457         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1458
1459                 if (g_ascii_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1460                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1461                         continue;
1462                 }
1463                 
1464                 toolbar_item = g_new0(ToolbarItem, 1); 
1465                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1466                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1467                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1468                 toolbar_item->parent = data;
1469                 toolbar_item->type = type;
1470
1471                 /* collect toolbar items in list to keep track */
1472                 toolbar_data->item_list = 
1473                         g_slist_append(toolbar_data->item_list, 
1474                                        toolbar_item);
1475                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1476                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1477                                                 toolbar_item->text,
1478                                                 (""),
1479                                                 (""),
1480                                                 icon_wid, G_CALLBACK(toolbar_buttons_cb), 
1481                                                 toolbar_item);
1482                 
1483                 switch (toolbar_item->index) {
1484
1485                 case A_RECEIVE_ALL:
1486                         toolbar_data->getall_btn = item;
1487                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1488                                              toolbar_data->getall_btn, 
1489                                            _("Receive Mail on all Accounts"), NULL);
1490                         getall_combo = gtkut_combo_button_create(toolbar_data->getall_btn, NULL, 0,
1491                                         "<GetAll>", (gpointer)toolbar_item);
1492                         gtk_button_set_relief(GTK_BUTTON(getall_combo->arrow),
1493                                               GTK_RELIEF_NONE);
1494                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1495                                                   GTK_WIDGET_PTR(getall_combo),
1496                                                   _("Receive Mail on selected Account"), "Reply");
1497                         toolbar_data->getall_combo = getall_combo;
1498                         break;
1499                 case A_RECEIVE_CUR:
1500                         toolbar_data->get_btn = item;
1501                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1502                                              toolbar_data->get_btn,
1503                                            _("Receive Mail on current Account"), NULL);
1504                         break;
1505                 case A_SEND_QUEUED:
1506                         toolbar_data->send_btn = item; 
1507                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1508                                              toolbar_data->send_btn,
1509                                            _("Send Queued Messages"), NULL);
1510                         break;
1511                 case A_COMPOSE_EMAIL:
1512                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1513                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1514                                                             _("Compose"),
1515                                                             (""),
1516                                                             (""),
1517                                                             icon_news, G_CALLBACK(toolbar_buttons_cb), 
1518                                                             toolbar_item);
1519                         toolbar_data->compose_mail_btn = item; 
1520                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1521                                              toolbar_data->compose_mail_btn,
1522                                            _("Compose Email"), NULL);
1523                         toolbar_data->compose_news_btn = item_news;
1524                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1525                                              toolbar_data->compose_news_btn,
1526                                            _("Compose News"), NULL);
1527                         compose_combo = gtkut_combo_button_create(toolbar_data->compose_mail_btn, NULL, 0,
1528                                         "<Compose>", (gpointer)toolbar_item);
1529                         gtk_button_set_relief(GTK_BUTTON(compose_combo->arrow),
1530                                               GTK_RELIEF_NONE);
1531                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1532                                                   GTK_WIDGET_PTR(compose_combo),
1533                                                   _("Compose with selected Account"), "Compose");
1534                         toolbar_data->compose_combo = compose_combo;
1535                         break;
1536                 case A_LEARN_SPAM:
1537                         icon_ham = stock_pixmap_widget(container, STOCK_PIXMAP_HAM_BTN);
1538                         item_ham = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1539                                                             _("Ham"),
1540                                                             (""),
1541                                                             (""),
1542                                                             icon_ham, G_CALLBACK(toolbar_buttons_cb), 
1543                                                             toolbar_item);
1544                         toolbar_data->learn_spam_btn = item; 
1545                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1546                                              toolbar_data->learn_spam_btn,
1547                                            _("Learn Spam"), NULL);
1548                         toolbar_data->learn_ham_btn = item_ham;
1549                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1550                                              toolbar_data->learn_ham_btn,
1551                                            _("Learn Ham"), NULL);                       
1552                         break;
1553                 case A_REPLY_MESSAGE:
1554                         toolbar_data->reply_btn = item;
1555                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1556                                              toolbar_data->reply_btn,
1557                                            _("Reply to Message"), NULL);
1558                         n_menu_entries = sizeof(reply_entries) / 
1559                                 sizeof(reply_entries[0]);
1560                         reply_combo = gtkut_combo_button_create(toolbar_data->reply_btn,
1561                                               reply_entries, n_menu_entries,
1562                                               "<Reply>", (gpointer)toolbar_item);
1563                         gtk_button_set_relief(GTK_BUTTON(reply_combo->arrow),
1564                                               GTK_RELIEF_NONE);
1565                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1566                                                   GTK_WIDGET_PTR(reply_combo),
1567                                                   _("Reply to Message"), "Reply");
1568                         toolbar_data->reply_combo = reply_combo;
1569                         break;
1570                 case A_REPLY_SENDER:
1571                         toolbar_data->replysender_btn = item;
1572                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1573                                              toolbar_data->replysender_btn,
1574                                            _("Reply to Sender"), NULL);
1575                         n_menu_entries = sizeof(replysender_entries) / 
1576                                 sizeof(replysender_entries[0]);
1577                         replysender_combo = gtkut_combo_button_create(toolbar_data->replysender_btn,
1578                                               replysender_entries, n_menu_entries,
1579                                               "<ReplySender>", (gpointer)toolbar_item);
1580                         gtk_button_set_relief(GTK_BUTTON(replysender_combo->arrow),
1581                                               GTK_RELIEF_NONE);
1582                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1583                                                   GTK_WIDGET_PTR(replysender_combo),
1584                                                   _("Reply to Sender"), "ReplySender");
1585                         toolbar_data->replysender_combo = replysender_combo;
1586                         break;
1587                 case A_REPLY_ALL:
1588                         toolbar_data->replyall_btn = item;
1589                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1590                                              toolbar_data->replyall_btn,
1591                                            _("Reply to All"), NULL);
1592                         n_menu_entries = sizeof(replyall_entries) / 
1593                                 sizeof(replyall_entries[0]);
1594                         replyall_combo = gtkut_combo_button_create(toolbar_data->replyall_btn,
1595                                               replyall_entries, n_menu_entries,
1596                                               "<ReplyAll>", (gpointer)toolbar_item);
1597                         gtk_button_set_relief(GTK_BUTTON(replyall_combo->arrow),
1598                                               GTK_RELIEF_NONE);
1599                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1600                                                   GTK_WIDGET_PTR(replyall_combo),
1601                                                   _("Reply to All"), "ReplyAll");
1602                         toolbar_data->replyall_combo = replyall_combo;
1603                         break;
1604                 case A_REPLY_ML:
1605                         toolbar_data->replylist_btn = item;
1606                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1607                                              toolbar_data->replylist_btn,
1608                                            _("Reply to Mailing-list"), NULL);
1609                         n_menu_entries = sizeof(replylist_entries) / 
1610                                 sizeof(replylist_entries[0]);
1611                         replylist_combo = gtkut_combo_button_create(toolbar_data->replylist_btn,
1612                                               replylist_entries, n_menu_entries,
1613                                               "<ReplyList>", (gpointer)toolbar_item);
1614                         gtk_button_set_relief(GTK_BUTTON(replylist_combo->arrow),
1615                                               GTK_RELIEF_NONE);
1616                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1617                                                   GTK_WIDGET_PTR(replylist_combo),
1618                                                   _("Reply to Mailing-list"), "ReplyList");
1619                         toolbar_data->replylist_combo = replylist_combo;
1620                         break;
1621                 case A_FORWARD:
1622                         toolbar_data->fwd_btn = item;
1623                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1624                                              toolbar_data->fwd_btn,
1625                                              _("Forward Message"), NULL);
1626                         n_menu_entries = sizeof(forward_entries) / 
1627                                 sizeof(forward_entries[0]);
1628                         fwd_combo = gtkut_combo_button_create(toolbar_data->fwd_btn,
1629                                               forward_entries, n_menu_entries,
1630                                               "<Forward>", (gpointer)toolbar_item);
1631                         gtk_button_set_relief(GTK_BUTTON(fwd_combo->arrow),
1632                                               GTK_RELIEF_NONE);
1633                         gtk_toolbar_append_widget(GTK_TOOLBAR(toolbar),
1634                                                   GTK_WIDGET_PTR(fwd_combo),
1635                                                   _("Forward Message"), "Fwd");
1636                         toolbar_data->fwd_combo = fwd_combo;
1637                         break;
1638                 case A_TRASH:
1639                         toolbar_data->trash_btn = item;
1640                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1641                                              toolbar_data->trash_btn,
1642                                              _("Trash Message"), NULL);
1643                         break;
1644                 case A_DELETE_REAL:
1645                         toolbar_data->delete_btn = item;
1646                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1647                                              toolbar_data->delete_btn,
1648                                              _("Delete Message"), NULL);
1649                         break;
1650                 case A_EXECUTE:
1651                         toolbar_data->exec_btn = item;
1652                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1653                                              toolbar_data->exec_btn,
1654                                            _("Execute"), NULL);
1655                         break;
1656                 case A_GOTO_PREV:
1657                         toolbar_data->prev_btn = item;
1658                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1659                                              toolbar_data->prev_btn,
1660                                              _("Go to Previous Unread Message"),
1661                                              NULL);
1662                         break;
1663                 case A_GOTO_NEXT:
1664                         toolbar_data->next_btn = item;
1665                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1666                                              toolbar_data->next_btn,
1667                                              _("Go to Next Unread Message"),
1668                                              NULL);
1669                         break;
1670                 
1671                 /* Compose Toolbar */
1672                 case A_SEND:
1673                         toolbar_data->send_btn = item;
1674                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1675                                              toolbar_data->send_btn, 
1676                                              _("Send Message"), NULL);
1677                         break;
1678                 case A_SENDL:
1679                         toolbar_data->sendl_btn = item;
1680                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1681                                              toolbar_data->sendl_btn,
1682                                              _("Put into queue folder and send later"), NULL);
1683                         break;
1684                 case A_DRAFT:
1685                         toolbar_data->draft_btn = item; 
1686                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1687                                              toolbar_data->draft_btn,
1688                                              _("Save to draft folder"), NULL);
1689                         break;
1690                 case A_INSERT:
1691                         toolbar_data->insert_btn = item; 
1692                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1693                                              toolbar_data->insert_btn,
1694                                              _("Insert file"), NULL);
1695                         break;
1696                 case A_ATTACH:
1697                         toolbar_data->attach_btn = item;
1698                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1699                                              toolbar_data->attach_btn,
1700                                              _("Attach file"), NULL);
1701                         break;
1702                 case A_SIG:
1703                         toolbar_data->sig_btn = item;
1704                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1705                                              toolbar_data->sig_btn,
1706                                              _("Insert signature"), NULL);
1707                         break;
1708                 case A_EXTEDITOR:
1709                         toolbar_data->exteditor_btn = item;
1710                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1711                                              toolbar_data->exteditor_btn,
1712                                              _("Edit with external editor"), NULL);
1713                         break;
1714                 case A_LINEWRAP_CURRENT:
1715                         toolbar_data->linewrap_current_btn = item;
1716                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1717                                              toolbar_data->linewrap_current_btn,
1718                                              _("Wrap long lines of current paragraph"), NULL);
1719                         break;
1720                 case A_LINEWRAP_ALL:
1721                         toolbar_data->linewrap_all_btn = item;
1722                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1723                                              toolbar_data->linewrap_all_btn,
1724                                              _("Wrap all long lines"), NULL);
1725                         break;
1726                 case A_ADDRBOOK:
1727                         toolbar_data->addrbook_btn = item;
1728                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1729                                              toolbar_data->addrbook_btn,
1730                                              _("Address book"), NULL);
1731                         break;
1732 #ifdef USE_ASPELL
1733                 case A_CHECK_SPELLING:
1734                         toolbar_data->spellcheck_btn = item;
1735                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1736                                              toolbar_data->spellcheck_btn,
1737                                              _("Check spelling"), NULL);
1738                         break;
1739 #endif
1740
1741                 case A_SYL_ACTIONS:
1742                         action_item = g_new0(ToolbarSylpheedActions, 1);
1743                         action_item->widget = item;
1744                         action_item->name   = g_strdup(toolbar_item->text);
1745
1746                         toolbar_data->action_list = 
1747                                 g_slist_append(toolbar_data->action_list,
1748                                                action_item);
1749
1750                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1751                                              item,
1752                                              action_item->name, NULL);
1753
1754                         gtk_widget_show(item);
1755                         break;
1756                 default:
1757                         /* find and set the tool tip text */
1758                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips),
1759                                              item,
1760                                              toolbar_ret_descr_from_val
1761                                                 (toolbar_item->index),
1762                                              NULL);
1763                         break;
1764                 }
1765
1766         }
1767         toolbar_data->toolbar = toolbar;
1768         if (type == TOOLBAR_MAIN)
1769                 activate_compose_button(toolbar_data, 
1770                                         prefs_common.toolbar_style, 
1771                                         toolbar_data->compose_btn_type);
1772         if (type != TOOLBAR_COMPOSE)
1773                 activate_learn_button(toolbar_data, prefs_common.toolbar_style,
1774                                 LEARN_SPAM);
1775         
1776         gtk_widget_show_all(toolbar);
1777         
1778         return toolbar_data; 
1779 }
1780
1781 /**
1782  * Free toolbar structures
1783  */ 
1784 void toolbar_destroy(Toolbar * toolbar) {
1785
1786         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
1787         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
1788 }
1789
1790 void toolbar_update(ToolbarType type, gpointer data)
1791 {
1792         Toolbar *toolbar_data;
1793         GtkWidget *handlebox;
1794         MainWindow *mainwin = (MainWindow*)data;
1795         Compose    *compose = (Compose*)data;
1796         MessageView *msgview = (MessageView*)data;
1797
1798         switch(type) {
1799         case TOOLBAR_MAIN:
1800                 toolbar_data = mainwin->toolbar;
1801                 handlebox    = mainwin->handlebox;
1802                 break;
1803         case TOOLBAR_COMPOSE:
1804                 toolbar_data = compose->toolbar;
1805                 handlebox    = compose->handlebox;
1806                 break;
1807         case TOOLBAR_MSGVIEW:
1808                 toolbar_data = msgview->toolbar;
1809                 handlebox    = msgview->handlebox;
1810                 break;
1811         default:
1812                 return;
1813         }
1814
1815         gtk_container_remove(GTK_CONTAINER(handlebox), 
1816                              GTK_WIDGET(toolbar_data->toolbar));
1817
1818         toolbar_init(toolbar_data);
1819         toolbar_data = toolbar_create(type, handlebox, data);
1820         switch(type) {
1821         case TOOLBAR_MAIN:
1822                 mainwin->toolbar = toolbar_data;
1823                 break;
1824         case TOOLBAR_COMPOSE:
1825                 compose->toolbar = toolbar_data;
1826                 break;
1827         case TOOLBAR_MSGVIEW:
1828                 msgview->toolbar = toolbar_data;
1829                 break;
1830         }
1831
1832         toolbar_style(type, prefs_common.toolbar_style, data);
1833
1834         if (type == TOOLBAR_MAIN) {
1835                 toolbar_main_set_sensitive((MainWindow*)data);
1836                 account_set_menu_only_toolbar();
1837         }
1838 }
1839
1840 void toolbar_main_set_sensitive(gpointer data)
1841 {
1842         SensitiveCond state;
1843         gboolean sensitive;
1844         MainWindow *mainwin = (MainWindow*)data;
1845         Toolbar *toolbar = mainwin->toolbar;
1846         GSList *cur;
1847         GSList *entry_list = NULL;
1848         
1849         typedef struct _Entry Entry;
1850         struct _Entry {
1851                 GtkWidget *widget;
1852                 SensitiveCond cond;
1853                 gboolean empty;
1854         };
1855
1856 #define SET_WIDGET_COND(w, c)     \
1857 { \
1858         Entry *e = g_new0(Entry, 1); \
1859         e->widget = w; \
1860         e->cond   = c; \
1861         entry_list = g_slist_append(entry_list, e); \
1862 }
1863
1864         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1865         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1866         if (toolbar->getall_btn)
1867                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->getall_combo),
1868                         M_HAVE_ACCOUNT|M_UNLOCKED);
1869         if (toolbar->compose_mail_btn)
1870                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->compose_combo),
1871                         M_HAVE_ACCOUNT);
1872         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
1873         SET_WIDGET_COND(toolbar->reply_btn,
1874                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1875         if (toolbar->reply_btn)
1876                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->reply_combo),
1877                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1878         SET_WIDGET_COND(toolbar->replyall_btn,
1879                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1880         if (toolbar->replyall_btn)
1881                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->replyall_combo),
1882                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1883         SET_WIDGET_COND(toolbar->replylist_btn,
1884                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1885         if (toolbar->replylist_btn) 
1886                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->replylist_combo),
1887                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1888         SET_WIDGET_COND(toolbar->replysender_btn,
1889                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1890         if (toolbar->replysender_btn)
1891                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->replysender_combo),
1892                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1893         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
1894         if (toolbar->fwd_btn)
1895                 SET_WIDGET_COND(GTK_WIDGET_PTR(toolbar->fwd_combo),
1896                         M_HAVE_ACCOUNT|M_TARGET_EXIST); 
1897         if (toolbar->fwd_combo) {
1898                 GtkWidget *submenu = gtk_item_factory_get_widget(toolbar->fwd_combo->factory, "/Redirect");
1899                 SET_WIDGET_COND(submenu, M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST); 
1900         }
1901
1902         if (prefs_common.next_unread_msg_dialog == NEXTUNREADMSGDIALOG_ASSUME_NO) {
1903                 SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
1904         } else {
1905                 SET_WIDGET_COND(toolbar->next_btn, 0);
1906         }
1907         
1908         if (toolbar->trash_btn)
1909                 SET_WIDGET_COND(toolbar->trash_btn,
1910                         M_TARGET_EXIST|M_ALLOW_DELETE);
1911         if (toolbar->delete_btn)
1912                 SET_WIDGET_COND(toolbar->delete_btn,
1913                         M_TARGET_EXIST|M_ALLOW_DELETE);
1914         if (toolbar->exec_btn)
1915                 SET_WIDGET_COND(toolbar->exec_btn, M_DELAY_EXEC);
1916         
1917         if (toolbar->learn_ham_btn)
1918                 SET_WIDGET_COND(toolbar->learn_ham_btn, M_TARGET_EXIST|M_CAN_LEARN_SPAM);
1919         if (toolbar->learn_spam_btn)
1920                 SET_WIDGET_COND(toolbar->learn_spam_btn, M_TARGET_EXIST|M_CAN_LEARN_SPAM);
1921
1922         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
1923                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
1924                 
1925                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
1926         }
1927
1928 #undef SET_WIDGET_COND
1929
1930         state = main_window_get_current_state(mainwin);
1931
1932         for (cur = entry_list; cur != NULL; cur = cur->next) {
1933                 Entry *e = (Entry*) cur->data;
1934
1935                 if (e->widget != NULL) {
1936                         sensitive = ((e->cond & state) == e->cond);
1937                         gtk_widget_set_sensitive(e->widget, sensitive); 
1938                 }
1939         }
1940         
1941         while (entry_list != NULL) {
1942                 Entry *e = (Entry*) entry_list->data;
1943
1944                 g_free(e);
1945                 entry_list = g_slist_remove(entry_list, e);
1946         }
1947
1948         g_slist_free(entry_list);
1949
1950         activate_compose_button(toolbar, 
1951                                 prefs_common.toolbar_style,
1952                                 toolbar->compose_btn_type);
1953 }
1954
1955 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
1956 {
1957         Compose *compose = (Compose*)data;
1958         GSList *items = compose->toolbar->action_list;
1959
1960         if (compose->toolbar->send_btn)
1961                 gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
1962         if (compose->toolbar->sendl_btn)
1963                 gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
1964         if (compose->toolbar->draft_btn )
1965                 gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
1966         if (compose->toolbar->insert_btn )
1967                 gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
1968         if (compose->toolbar->attach_btn)
1969                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
1970         if (compose->toolbar->sig_btn)
1971                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
1972         if (compose->toolbar->exteditor_btn)
1973                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
1974         if (compose->toolbar->linewrap_current_btn)
1975                 gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, sensitive);
1976         if (compose->toolbar->linewrap_all_btn)
1977                 gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, sensitive);
1978         if (compose->toolbar->addrbook_btn)
1979                 gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
1980 #ifdef USE_ASPELL
1981         if (compose->toolbar->spellcheck_btn)
1982                 gtk_widget_set_sensitive(compose->toolbar->spellcheck_btn, sensitive);
1983 #endif
1984         for (; items != NULL; items = g_slist_next(items)) {
1985                 ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
1986                 gtk_widget_set_sensitive(item->widget, sensitive);
1987         }
1988 }
1989
1990 /**
1991  * Initialize toolbar structure
1992  **/
1993 void toolbar_init(Toolbar * toolbar) {
1994
1995         toolbar->toolbar                = NULL;
1996         toolbar->get_btn                = NULL;
1997         toolbar->getall_btn             = NULL;
1998         toolbar->getall_combo           = NULL;
1999         toolbar->send_btn               = NULL;
2000         toolbar->compose_mail_btn       = NULL;
2001         toolbar->compose_news_btn       = NULL;
2002         toolbar->compose_combo          = NULL;
2003         toolbar->reply_btn              = NULL;
2004         toolbar->replysender_btn        = NULL;
2005         toolbar->replyall_btn           = NULL;
2006         toolbar->replylist_btn          = NULL;
2007         toolbar->fwd_btn                = NULL;
2008         toolbar->trash_btn              = NULL;
2009         toolbar->delete_btn             = NULL;
2010         toolbar->prev_btn               = NULL;
2011         toolbar->next_btn               = NULL;
2012         toolbar->exec_btn               = NULL;
2013
2014         /* compose buttons */ 
2015         toolbar->sendl_btn              = NULL;
2016         toolbar->draft_btn              = NULL;
2017         toolbar->insert_btn             = NULL;
2018         toolbar->attach_btn             = NULL;
2019         toolbar->sig_btn                = NULL; 
2020         toolbar->exteditor_btn          = NULL; 
2021         toolbar->linewrap_current_btn   = NULL; 
2022         toolbar->linewrap_all_btn       = NULL; 
2023         toolbar->addrbook_btn           = NULL; 
2024 #ifdef USE_ASPELL
2025         toolbar->spellcheck_btn         = NULL;
2026 #endif
2027
2028         toolbar_destroy(toolbar);
2029 }
2030
2031 /*
2032  */
2033 static void toolbar_reply(gpointer data, guint action)
2034 {
2035         ToolbarItem *toolbar_item = (ToolbarItem*)data;
2036         MainWindow *mainwin;
2037         MessageView *msgview;
2038         GSList *msginfo_list = NULL;
2039
2040         g_return_if_fail(toolbar_item != NULL);
2041
2042         switch (toolbar_item->type) {
2043         case TOOLBAR_MAIN:
2044                 mainwin = (MainWindow*)toolbar_item->parent;
2045                 msginfo_list = summary_get_selection(mainwin->summaryview);
2046                 msgview = (MessageView*)mainwin->messageview;
2047                 break;
2048         case TOOLBAR_MSGVIEW:
2049                 msgview = (MessageView*)toolbar_item->parent;
2050                 g_return_if_fail(msgview != NULL);      
2051                 msginfo_list = g_slist_append(msginfo_list, msgview->msginfo);
2052                 break;
2053         default:
2054                 return;
2055         }
2056
2057         g_return_if_fail(msgview != NULL);
2058         g_return_if_fail(msginfo_list != NULL);
2059         compose_reply_from_messageview(msgview, msginfo_list, action);
2060         g_slist_free(msginfo_list);
2061
2062         /* TODO: update reply state ion summaryview */
2063 }
2064
2065
2066 /* exported functions */
2067
2068 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2069 {
2070         MainWindow *mainwin = (MainWindow*)data;
2071
2072         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2073 }
2074
2075 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2076 {
2077         MainWindow *mainwin = (MainWindow*)data;
2078
2079         inc_all_account_mail(mainwin, FALSE, prefs_common.newmail_notify_manu);
2080 }
2081
2082 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2083 {
2084         GList *list;
2085         gboolean found;
2086
2087         if (prefs_common.work_offline)
2088                 if (alertpanel(_("Offline warning"), 
2089                                _("You're working offline. Override?"),
2090                                GTK_STOCK_NO, GTK_STOCK_YES,
2091                                NULL) != G_ALERTALTERNATE)
2092                 return;
2093
2094         /* ask for confirmation before sending queued messages only
2095            in online mode and if there is at least one message queued
2096            in any of the folder queue
2097         */
2098         if (prefs_common.confirm_send_queued_messages) {
2099                 found = FALSE;
2100                 /* check if there's a queued message */
2101                 for (list = folder_get_list(); !found && list != NULL; list = list->next) {
2102                         Folder *folder = list->data;
2103
2104                         found = !procmsg_queue_is_empty(folder->queue);
2105                 }
2106                 /* if necessary, ask for confirmation before sending */
2107                 if (found && !prefs_common.work_offline) {
2108                         if (alertpanel(_("Send queued messages"), 
2109                                    _("Send all queued messages?"),
2110                                    GTK_STOCK_CANCEL, _("_Send"),
2111                                    NULL) != G_ALERTALTERNATE)
2112                                 return;
2113                 }
2114         }
2115
2116         for (list = folder_get_list(); list != NULL; list = list->next) {
2117                 Folder *folder = list->data;
2118
2119                 if (folder->queue) {
2120                         if (procmsg_send_queue(folder->queue, 
2121                                                prefs_common.savemsg) < 0)
2122                                 alertpanel_error(_("Some errors occurred while "
2123                                                    "sending queued messages."));
2124                 }
2125         }
2126 }
2127
2128 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2129 {
2130         MainWindow *mainwin = (MainWindow*)data;
2131         PrefsAccount *ac = NULL;
2132         FolderItem *item = mainwin->summaryview->folder_item;   
2133         GList * list;
2134         GList * cur;
2135         
2136         if (item) {
2137                 ac = account_find_from_item(item);
2138                 if (ac && ac->protocol != A_NNTP) {
2139                         compose_new_with_folderitem(ac, item);          /* CLAWS */
2140                         return;
2141                 }
2142         }
2143
2144         /*
2145          * CLAWS - use current account
2146          */
2147         if (cur_account && (cur_account->protocol != A_NNTP)) {
2148                 compose_new_with_folderitem(cur_account, item);
2149                 return;
2150         }
2151
2152         /*
2153          * CLAWS - just get the first one
2154          */
2155         list = account_get_list();
2156         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2157                 ac = (PrefsAccount *) cur->data;
2158                 if (ac->protocol != A_NNTP) {
2159                         compose_new_with_folderitem(ac, item);
2160                         return;
2161                 }
2162         }
2163 }
2164
2165 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2166 {
2167         MainWindow *mainwin = (MainWindow*)data;
2168         PrefsAccount * ac = NULL;
2169         GList * list;
2170         GList * cur;
2171
2172         if (mainwin->summaryview->folder_item) {
2173                 ac = mainwin->summaryview->folder_item->folder->account;
2174                 if (ac && ac->protocol == A_NNTP) {
2175                         compose_new_with_folderitem(ac,
2176                                     mainwin->summaryview->folder_item);
2177                         return;
2178                 }
2179         }
2180
2181         list = account_get_list();
2182         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2183                 ac = (PrefsAccount *) cur->data;
2184                 if (ac->protocol == A_NNTP) {
2185                         compose_new_with_folderitem(ac,
2186                                     mainwin->summaryview->folder_item);
2187                         return;
2188                 }
2189         }
2190 }