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