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