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