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