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