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