keypress fixes
[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                         if (toolbar_item->index != A_SEPARATOR) 
530                                 fprintf(fp, "\t<%s %s=\"%s\" %s=\"%s\" %s=\"%s\"/>\n",
531                                         TOOLBAR_TAG_ITEM, 
532                                         TOOLBAR_ICON_FILE, toolbar_item->file,
533                                         TOOLBAR_ICON_TEXT, toolbar_item->text,
534                                         TOOLBAR_ICON_ACTION, 
535                                         toolbar_ret_text_from_val(toolbar_item->index));
536                         else 
537                                 fprintf(fp, "\t<%s/>\n", TOOLBAR_TAG_SEPARATOR); 
538                 }
539
540                 fprintf(fp, "</%s>\n", TOOLBAR_TAG_INDEX);      
541         
542                 if (prefs_file_close (pfile) < 0 ) 
543                         g_warning("failed to write toolbar configuration to file\n");
544         } else
545                 g_warning("failed to open toolbar configuration file for writing\n");
546 }
547
548 void toolbar_read_config_file(ToolbarType source)
549 {
550         XMLFile *file   = NULL;
551         gchar *fileSpec = NULL;
552         GList *attr;
553         gboolean retVal;
554         jmp_buf    jumper;
555
556         debug_print("read Toolbar Configuration from %s\n", toolbar_config[source].conf_file);
557
558         fileSpec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, toolbar_config[source].conf_file, NULL );
559         file = xml_open_file(fileSpec);
560         g_free(fileSpec);
561
562         toolbar_clear_list(source);
563
564         if (file) {
565                 if ((setjmp(jumper))
566                 || (xml_get_dtd(file))
567                 || (xml_parse_next_tag(file))
568                 || (!xml_compare_tag(file, TOOLBAR_TAG_INDEX))) {
569                         xml_close_file(file);
570                         return;
571                 }
572
573                 attr = xml_get_current_tag_attr(file);
574                 
575                 retVal = TRUE;
576                 for (;;) {
577                         if (!file->level) 
578                                 break;
579                         /* Get item tag */
580                         if (xml_parse_next_tag(file)) 
581                                 longjmp(jumper, 1);
582
583                         /* Get next tag (icon, icon_text or icon_action) */
584                         if (xml_compare_tag(file, TOOLBAR_TAG_ITEM)) {
585                                 toolbar_parse_item(file, source);
586                         } else if (xml_compare_tag(file, TOOLBAR_TAG_SEPARATOR)) {
587                                 ToolbarItem *item = g_new0(ToolbarItem, 1);
588                         
589                                 item->file   = g_strdup(toolbar_ret_descr_from_val(A_SEPARATOR));
590                                 item->index  = A_SEPARATOR;
591                                 toolbar_config[source].item_list = 
592                                         g_slist_append(toolbar_config[source].item_list, item);
593                         }
594
595                 }
596                 xml_close_file(file);
597         }
598
599         if ((!file) || (g_slist_length(toolbar_config[source].item_list) == 0)) {
600
601                 if (source == TOOLBAR_MAIN) 
602                         toolbar_set_default(TOOLBAR_MAIN);
603                 else if (source == TOOLBAR_COMPOSE) 
604                         toolbar_set_default(TOOLBAR_COMPOSE);
605                 else if (source == TOOLBAR_MSGVIEW) 
606                         toolbar_set_default(TOOLBAR_MSGVIEW);
607                 else {          
608                         g_warning("failed to write Toolbar Configuration to %s\n", toolbar_config[source].conf_file);
609                         return;
610                 }
611
612                 toolbar_save_config_file(source);
613         }
614 }
615
616 /*
617  * clears list of toolbar items read from configuration files
618  */
619 void toolbar_clear_list(ToolbarType source)
620 {
621         while (toolbar_config[source].item_list != NULL) {
622                 ToolbarItem *item = (ToolbarItem*) toolbar_config[source].item_list->data;
623                 
624                 toolbar_config[source].item_list = 
625                         g_slist_remove(toolbar_config[source].item_list, item);
626
627                 if (item->file)
628                         g_free(item->file);
629                 if (item->text)
630                         g_free(item->text);
631                 g_free(item);   
632         }
633         g_slist_free(toolbar_config[source].item_list);
634 }
635
636
637 /* 
638  * return list of Toolbar items
639  */
640 GSList *toolbar_get_list(ToolbarType source)
641 {
642         GSList *list = NULL;
643
644         if ((source == TOOLBAR_MAIN) || (source == TOOLBAR_COMPOSE) || (source == TOOLBAR_MSGVIEW))
645                 list = toolbar_config[source].item_list;
646
647         return list;
648 }
649
650 void toolbar_set_list_item(ToolbarItem *t_item, ToolbarType source)
651 {
652         ToolbarItem *toolbar_item = g_new0(ToolbarItem, 1);
653
654         toolbar_item->file  = g_strdup(t_item->file);
655         toolbar_item->text  = g_strdup(t_item->text);
656         toolbar_item->index = t_item->index;
657         
658         toolbar_config[source].item_list = 
659                 g_slist_append(toolbar_config[source].item_list,
660                                toolbar_item);
661 }
662
663 void toolbar_action_execute(GtkWidget    *widget,
664                             GSList       *action_list, 
665                             gpointer     data,
666                             gint         source) 
667 {
668         GSList *cur, *lop;
669         gchar *action, *action_p;
670         gboolean found = FALSE;
671         gint i = 0;
672
673         for (cur = action_list; cur != NULL;  cur = cur->next) {
674                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
675
676                 if (widget == act->widget) {
677                         
678                         for (lop = prefs_common.actions_list; lop != NULL; lop = lop->next) {
679                                 action = g_strdup((gchar*)lop->data);
680
681                                 action_p = strstr(action, ": ");
682                                 action_p[0] = 0x00;
683                                 if (g_strcasecmp(act->name, action) == 0) {
684                                         found = TRUE;
685                                         g_free(action);
686                                         break;
687                                 } else 
688                                         i++;
689                                 g_free(action);
690                         }
691                         if (found) 
692                                 break;
693                 }
694         }
695
696         if (found) 
697                 actions_execute(data, i, widget, source);
698         else
699                 g_warning ("Error: did not find Sylpheed Action to execute");
700 }
701
702 static void activate_compose_button (Toolbar           *toolbar,
703                                      ToolbarStyle      style,
704                                      ComposeButtonType type)
705 {
706         if ((!toolbar->compose_mail_btn) || (!toolbar->compose_news_btn))
707                 return;
708
709         gtk_widget_hide(type == COMPOSEBUTTON_NEWS ? toolbar->compose_mail_btn 
710                         : toolbar->compose_news_btn);
711         gtk_widget_show(type == COMPOSEBUTTON_NEWS ? toolbar->compose_news_btn
712                         : toolbar->compose_mail_btn);
713         toolbar->compose_btn_type = type;       
714 }
715
716 void toolbar_set_compose_button(Toolbar            *toolbar, 
717                                 ComposeButtonType  compose_btn_type)
718 {
719         if (toolbar->compose_btn_type != compose_btn_type)
720                 activate_compose_button(toolbar, 
721                                         prefs_common.toolbar_style,
722                                         compose_btn_type);
723 }
724
725 void toolbar_toggle(guint action, gpointer data)
726 {
727         MainWindow *mainwin = (MainWindow*)data;
728         GList *list;
729         GList *cur;
730
731         g_return_if_fail(mainwin != NULL);
732
733         toolbar_style(TOOLBAR_MAIN, action, mainwin);
734
735         list = compose_get_compose_list();
736         for (cur = list; cur != NULL; cur = cur->next) {
737                 toolbar_style(TOOLBAR_COMPOSE, action, cur->data);
738         }
739         list = messageview_get_msgview_list();
740         for (cur = list; cur != NULL; cur = cur->next) {
741                 toolbar_style(TOOLBAR_MSGVIEW, action, cur->data);
742         }
743         
744 }
745
746 void toolbar_set_style(GtkWidget *toolbar_wid, GtkWidget *handlebox_wid, guint action)
747 {
748         switch ((ToolbarStyle)action) {
749         case TOOLBAR_NONE:
750                 gtk_widget_hide(handlebox_wid);
751                 break;
752         case TOOLBAR_ICON:
753                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
754                                       GTK_TOOLBAR_ICONS);
755                 break;
756         case TOOLBAR_TEXT:
757                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
758                                       GTK_TOOLBAR_TEXT);
759                 break;
760         case TOOLBAR_BOTH:
761                 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar_wid),
762                                       GTK_TOOLBAR_BOTH);
763                 break;
764         default:
765                 return;
766         }
767
768         prefs_common.toolbar_style = (ToolbarStyle)action;
769         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
770                 gtk_widget_show(handlebox_wid);
771                 gtk_widget_queue_resize(handlebox_wid);
772         }
773 }
774 /*
775  * Change the style of toolbar
776  */
777 static void toolbar_style(ToolbarType type, guint action, gpointer data)
778 {
779         GtkWidget  *handlebox_wid;
780         GtkWidget  *toolbar_wid;
781         MainWindow *mainwin = (MainWindow*)data;
782         Compose    *compose = (Compose*)data;
783         MessageView *msgview = (MessageView*)data;
784         
785         g_return_if_fail(data != NULL);
786         
787         switch (type) {
788         case TOOLBAR_MAIN:
789                 handlebox_wid = mainwin->handlebox;
790                 toolbar_wid = mainwin->toolbar->toolbar;
791                 break;
792         case TOOLBAR_COMPOSE:
793                 handlebox_wid = compose->handlebox;
794                 toolbar_wid = compose->toolbar->toolbar;
795                 break;
796         case TOOLBAR_MSGVIEW: 
797                 handlebox_wid = msgview->handlebox;
798                 toolbar_wid = msgview->toolbar->toolbar;
799                 break;
800         default:
801
802                 return;
803         }
804         toolbar_set_style(toolbar_wid, handlebox_wid, action);
805 }
806
807 /* Toolbar handling */
808 static void toolbar_inc_cb(GtkWidget    *widget,
809                            gpointer      data)
810 {
811         ToolbarItem *toolbar_item = (ToolbarItem*)data;
812         MainWindow *mainwin;
813
814         g_return_if_fail(toolbar_item != NULL);
815
816         switch (toolbar_item->type) {
817         case TOOLBAR_MAIN:
818                 mainwin = (MainWindow*)toolbar_item->parent;    
819                 inc_mail_cb(mainwin, 0, NULL);
820                 break;
821         default:
822                 break;
823         }
824 }
825
826 static void toolbar_inc_all_cb(GtkWidget        *widget,
827                                gpointer          data)
828 {
829         ToolbarItem *toolbar_item = (ToolbarItem*)data;
830         MainWindow *mainwin;
831
832         g_return_if_fail(toolbar_item != NULL);
833
834         switch (toolbar_item->type) {
835         case TOOLBAR_MAIN:
836                 mainwin = (MainWindow*)toolbar_item->parent;
837                 inc_all_account_mail_cb(mainwin, 0, NULL);
838                 break;
839         default:
840                 break;
841         }
842 }
843
844 static void toolbar_send_queued_cb(GtkWidget *widget,gpointer data)
845 {
846         ToolbarItem *toolbar_item = (ToolbarItem*)data;
847         MainWindow *mainwin;
848
849         g_return_if_fail(toolbar_item != NULL);
850
851         switch (toolbar_item->type) {
852         case TOOLBAR_MAIN:
853                 mainwin = (MainWindow*)toolbar_item->parent;
854                 send_queue_cb(mainwin, 0, NULL);
855                 break;
856         default:
857                 break;
858         }
859 }
860
861 static void toolbar_exec_cb(GtkWidget   *widget,
862                             gpointer     data)
863 {
864         MainWindow *mainwin = get_mainwin(data);
865
866         g_return_if_fail(mainwin != NULL);
867         summary_execute(mainwin->summaryview);
868 }
869
870
871
872 /* popup callback functions */
873 static void toolbar_reply_popup_cb(GtkWidget       *widget, 
874                                    GdkEventButton  *event, 
875                                    gpointer         data)
876 {
877         Toolbar *toolbar_data = (Toolbar*)data;
878         
879         if (!event) return;
880         
881         if (event->button == 3) {
882                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
883                 gtk_menu_popup(GTK_MENU(toolbar_data->reply_popup), NULL, NULL,
884                        menu_button_position, widget,
885                        event->button, event->time);
886         }
887 }
888
889 static void toolbar_reply_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
890 {
891         ToolbarItem *toolbar_item = (ToolbarItem*)data;
892         GtkWidget *window;
893         GtkWidget *reply_btn;
894         MainWindow *mainwin;
895         MessageView *msgview;
896
897         g_return_if_fail(toolbar_item != NULL);
898
899         switch(toolbar_item->type) {
900         case TOOLBAR_MAIN:
901                 mainwin   = (MainWindow*)toolbar_item->parent;
902                 reply_btn = mainwin->toolbar->reply_btn;
903                 window    = mainwin->window;
904                 break;
905         case TOOLBAR_MSGVIEW:
906                 msgview   = (MessageView*)toolbar_item->parent;
907                 reply_btn = msgview->toolbar->reply_btn;
908                 window    = msgview->window;
909                 break;
910         default:
911                 return;
912         }
913
914         gtk_button_set_relief(GTK_BUTTON(reply_btn), GTK_RELIEF_NONE);
915         manage_window_focus_in(window, NULL, NULL);
916 }
917
918 static void toolbar_reply_to_all_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
919 {
920         Toolbar *toolbar_data = (Toolbar*)data;
921         
922         if (!event) return;
923         
924         if (event->button == 3) {
925                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
926                 gtk_menu_popup(GTK_MENU(toolbar_data->replyall_popup), NULL, NULL,
927                        menu_button_position, widget,
928                        event->button, event->time);
929         }
930 }
931
932 static void toolbar_reply_to_all_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
933 {
934         ToolbarItem *toolbar_item = (ToolbarItem*)data;
935         GtkWidget *window;
936         GtkWidget *replyall_btn;
937         MainWindow *mainwin;
938         MessageView *msgview;
939
940         g_return_if_fail(toolbar_item != NULL);
941
942         switch(toolbar_item->type) {
943         case TOOLBAR_MAIN:
944                 mainwin      = (MainWindow*)toolbar_item->parent; 
945                 replyall_btn = mainwin->toolbar->replyall_btn;
946                 window       = mainwin->window;
947                 break;
948         case TOOLBAR_MSGVIEW:
949                 msgview      = (MessageView*)toolbar_item->parent;
950                 replyall_btn = msgview->toolbar->replyall_btn;
951                 window       = msgview->window;
952                 break;
953         default:
954                 return;
955         }
956
957         gtk_button_set_relief(GTK_BUTTON(replyall_btn), GTK_RELIEF_NONE);
958         manage_window_focus_in(window, NULL, NULL);
959 }
960
961 static void toolbar_reply_to_list_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
962 {
963         Toolbar *toolbar_data = (Toolbar*)data;
964
965         if (event->button == 3) {
966                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
967                 gtk_menu_popup(GTK_MENU(toolbar_data->replylist_popup), NULL, NULL,
968                        menu_button_position, widget,
969                        event->button, event->time);
970         }
971 }
972
973 static void toolbar_reply_to_list_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
974 {
975         ToolbarItem *toolbar_item = (ToolbarItem*)data;
976         gpointer parent = toolbar_item->parent;
977         GtkWidget *window;
978         GtkWidget *replylist_btn;
979
980
981         g_return_if_fail(toolbar_item != NULL);
982
983         switch(toolbar_item->type) {
984         case TOOLBAR_MAIN:
985                 replylist_btn = ((MainWindow*)parent)->toolbar->replylist_btn;
986                 window        = ((MainWindow*)parent)->window;
987                 break;
988         case TOOLBAR_MSGVIEW:
989                 replylist_btn = ((MessageView*)parent)->toolbar->replylist_btn;
990                 window        = ((MessageView*)parent)->window;
991                 break;
992         default:
993                 return;
994         }
995
996         gtk_button_set_relief(GTK_BUTTON(replylist_btn), GTK_RELIEF_NONE);
997         manage_window_focus_in(window, NULL, NULL);
998 }
999
1000 static void toolbar_reply_to_sender_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
1001 {
1002         Toolbar *toolbar_data = (Toolbar*)data;
1003
1004         if (event->button == 3) {
1005                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
1006                 gtk_menu_popup(GTK_MENU(toolbar_data->replysender_popup), NULL, NULL,
1007                        menu_button_position, widget,
1008                        event->button, event->time);
1009         }
1010 }
1011
1012 static void toolbar_reply_to_sender_popup_closed_cb(GtkMenuShell *menu_shell, gpointer data)
1013 {
1014         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1015         gpointer parent = toolbar_item->parent;
1016         GtkWidget *window;
1017         GtkWidget *replysender_btn;
1018
1019         g_return_if_fail(toolbar_item != NULL);
1020
1021         switch(toolbar_item->type) {
1022         case TOOLBAR_MAIN:
1023                 replysender_btn = ((MainWindow*)parent)->toolbar->replysender_btn;
1024                 window          = ((MainWindow*)parent)->window;
1025                 break;
1026         case TOOLBAR_MSGVIEW:
1027                 replysender_btn = ((MessageView*)parent)->toolbar->replysender_btn;
1028                 window          = ((MessageView*)parent)->window;
1029                 break;
1030         default:
1031                 return;
1032         }
1033
1034         gtk_button_set_relief(GTK_BUTTON(replysender_btn), GTK_RELIEF_NONE);
1035         manage_window_focus_in(window, NULL, NULL);
1036 }
1037
1038 static void toolbar_forward_popup_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
1039 {
1040         Toolbar *toolbar_data = (Toolbar*)data;
1041
1042         if (event->button == 3) {
1043                 gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
1044                 gtk_menu_popup(GTK_MENU(toolbar_data->fwd_popup), NULL, NULL,
1045                                menu_button_position, widget,
1046                                event->button, event->time);
1047         }
1048 }
1049
1050 static void toolbar_forward_popup_closed_cb (GtkMenuShell *menu_shell, 
1051                                              gpointer     data)
1052 {
1053         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1054         gpointer parent = toolbar_item->parent;
1055         GtkWidget *window;
1056         GtkWidget *fwd_btn;
1057
1058         g_return_if_fail(toolbar_item != NULL);
1059
1060         switch(toolbar_item->type) {
1061         case TOOLBAR_MAIN:
1062                 fwd_btn = ((MainWindow*)parent)->toolbar->fwd_btn;
1063                 window  = ((MainWindow*)parent)->window;
1064                 break;
1065         case TOOLBAR_MSGVIEW:
1066                 fwd_btn = ((MessageView*)parent)->toolbar->fwd_btn;
1067                 window  = ((MessageView*)parent)->window;
1068                 break;
1069         default:
1070                 return;
1071         }
1072
1073         gtk_button_set_relief(GTK_BUTTON(fwd_btn), GTK_RELIEF_NONE);
1074         manage_window_focus_in(window, NULL, NULL);
1075 }
1076
1077 /*
1078  * Delete current/selected(s) message(s)
1079  */
1080 static void toolbar_delete_cb(GtkWidget *widget, gpointer data)
1081 {
1082         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1083         MainWindow *mainwin;
1084         MessageView *msgview;
1085
1086         g_return_if_fail(toolbar_item != NULL);
1087         
1088         switch (toolbar_item->type) {
1089         case TOOLBAR_MSGVIEW:
1090                 msgview = (MessageView*)toolbar_item->parent;
1091                 messageview_delete(msgview);
1092                 break;
1093         case TOOLBAR_MAIN:
1094                 mainwin = (MainWindow*)toolbar_item->parent;
1095                 summary_delete(mainwin->summaryview);
1096                 break;
1097         default: 
1098                 debug_print("toolbar event not supported\n");
1099                 break;
1100         }
1101 }
1102
1103
1104 /*
1105  * Compose new message
1106  */
1107 static void toolbar_compose_cb(GtkWidget *widget, gpointer data)
1108 {
1109         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1110         MainWindow *mainwin;
1111         MessageView *msgview;
1112
1113         g_return_if_fail(toolbar_item != NULL);
1114
1115         switch (toolbar_item->type) {
1116         case TOOLBAR_MAIN:
1117                 mainwin = (MainWindow*)toolbar_item->parent;
1118                 if (mainwin->toolbar->compose_btn_type == COMPOSEBUTTON_NEWS) 
1119                         compose_news_cb(mainwin, 0, NULL);
1120                 else
1121                         compose_mail_cb(mainwin, 0, NULL);
1122                 break;
1123         case TOOLBAR_MSGVIEW:
1124                 msgview = (MessageView*)toolbar_item->parent;
1125                 compose_new_with_folderitem(NULL, 
1126                                             msgview->msginfo->folder);
1127                 break;  
1128         default:
1129                 debug_print("toolbar event not supported\n");
1130         }
1131 }
1132
1133 static void toolbar_popup_cb(gpointer data, guint action, GtkWidget *widget)
1134 {
1135         toolbar_reply(data, action);
1136 }
1137
1138
1139 /*
1140  * Reply Message
1141  */
1142 static void toolbar_reply_cb(GtkWidget *widget, gpointer data)
1143 {
1144         toolbar_reply(data, prefs_common.reply_with_quote ? 
1145                       COMPOSE_REPLY_WITH_QUOTE : COMPOSE_REPLY_WITHOUT_QUOTE);
1146 }
1147
1148
1149 /*
1150  * Reply message to Sender and All recipients
1151  */
1152 static void toolbar_reply_to_all_cb(GtkWidget *widget, gpointer data)
1153 {
1154         toolbar_reply(data,
1155                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_ALL_WITH_QUOTE 
1156                       : COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE);
1157 }
1158
1159
1160 /*
1161  * Reply to Mailing List
1162  */
1163 static void toolbar_reply_to_list_cb(GtkWidget *widget, gpointer data)
1164 {
1165         toolbar_reply(data, 
1166                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_LIST_WITH_QUOTE 
1167                       : COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE);
1168 }
1169
1170
1171 /*
1172  * Reply to sender of message
1173  */ 
1174 static void toolbar_reply_to_sender_cb(GtkWidget *widget, gpointer data)
1175 {
1176         toolbar_reply(data, 
1177                       prefs_common.reply_with_quote ? COMPOSE_REPLY_TO_SENDER_WITH_QUOTE 
1178                       : COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE);
1179 }
1180
1181 /*
1182  * Open addressbook
1183  */ 
1184 static void toolbar_addrbook_cb(GtkWidget *widget, gpointer data)
1185 {
1186         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1187         Compose *compose;
1188
1189         g_return_if_fail(toolbar_item != NULL);
1190
1191         switch (toolbar_item->type) {
1192         case TOOLBAR_MAIN:
1193         case TOOLBAR_MSGVIEW:
1194                 compose = NULL;
1195                 break;
1196         case TOOLBAR_COMPOSE:
1197                 compose = (Compose *)toolbar_item->parent;
1198                 break;
1199         default:
1200                 return;
1201         }
1202         addressbook_open(compose);
1203 }
1204
1205
1206 /*
1207  * Forward current/selected(s) message(s)
1208  */
1209 static void toolbar_forward_cb(GtkWidget *widget, gpointer data)
1210 {
1211         toolbar_reply(data, COMPOSE_FORWARD);
1212 }
1213
1214
1215 /*
1216  * Goto Next Unread Message
1217  */
1218 static void toolbar_next_unread_cb(GtkWidget *widget, gpointer data)
1219 {
1220         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1221         MainWindow *mainwin;
1222         MessageView *msgview;
1223
1224         g_return_if_fail(toolbar_item != NULL);
1225
1226         switch (toolbar_item->type) {
1227         case TOOLBAR_MAIN:
1228                 mainwin = (MainWindow*)toolbar_item->parent;
1229                 summary_select_next_unread(mainwin->summaryview);
1230                 break;
1231                 
1232         case TOOLBAR_MSGVIEW:
1233                 msgview = (MessageView*)toolbar_item->parent;
1234                 summary_select_next_unread(msgview->mainwin->summaryview);
1235                 
1236                 /* Now we need to update the messageview window */
1237                 if (msgview->mainwin->summaryview->selected) {
1238                         GtkCTree *ctree = GTK_CTREE(msgview->mainwin->summaryview->ctree);
1239                         
1240                         MsgInfo * msginfo = gtk_ctree_node_get_row_data(ctree, 
1241                                                                         msgview->mainwin->summaryview->selected);
1242                        
1243                         messageview_show(msgview, msginfo, 
1244                                          msgview->all_headers);
1245                 } else {
1246                         gtk_widget_destroy(msgview->window);
1247                 }
1248                 break;
1249         default:
1250                 debug_print("toolbar event not supported\n");
1251         }
1252 }
1253
1254 static void toolbar_ignore_thread_cb(GtkWidget *widget, gpointer data)
1255 {
1256         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1257         MainWindow *mainwin;
1258
1259         g_return_if_fail(toolbar_item != NULL);
1260
1261         switch (toolbar_item->type) {
1262         case TOOLBAR_MAIN:
1263                 mainwin = (MainWindow *) toolbar_item->parent;
1264                 summary_toggle_ignore_thread(mainwin->summaryview);
1265                 break;
1266         case TOOLBAR_MSGVIEW:
1267                 /* TODO: see toolbar_next_unread_cb() if you need
1268                  * this in the message view */
1269                 break;
1270         default:
1271                 debug_print("toolbar event not supported\n");
1272                 break;
1273         }
1274 }
1275
1276 static void toolbar_print_cb(GtkWidget *widget, gpointer data)
1277 {
1278         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1279         MainWindow *mainwin;
1280
1281         g_return_if_fail(toolbar_item != NULL);
1282
1283         switch (toolbar_item->type) {
1284         case TOOLBAR_MAIN:
1285                 mainwin = (MainWindow *) toolbar_item->parent;
1286                 summary_print(mainwin->summaryview);
1287                 break;
1288         case TOOLBAR_MSGVIEW:
1289                 /* TODO: see toolbar_next_unread_cb() if you need
1290                  * this in the message view */
1291                 break;
1292         default:
1293                 debug_print("toolbar event not supported\n");
1294                 break;
1295         }
1296 }
1297
1298 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1299 {
1300         compose_toolbar_cb(A_SEND, data);
1301 }
1302
1303 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1304 {
1305         compose_toolbar_cb(A_SENDL, data);
1306 }
1307
1308 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1309 {
1310         compose_toolbar_cb(A_DRAFT, data);
1311 }
1312
1313 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1314 {
1315         compose_toolbar_cb(A_INSERT, data);
1316 }
1317
1318 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1319 {
1320         compose_toolbar_cb(A_ATTACH, data);
1321 }
1322
1323 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1324 {
1325         compose_toolbar_cb(A_SIG, data);
1326 }
1327
1328 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1329 {
1330         compose_toolbar_cb(A_EXTEDITOR, data);
1331 }
1332
1333 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
1334 {
1335         compose_toolbar_cb(A_LINEWRAP, data);
1336 }
1337
1338 /*
1339  * Execute actions from toolbar
1340  */
1341 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1342 {
1343         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1344         GSList *action_list;
1345         MainWindow *mainwin;
1346         Compose *compose;
1347         MessageView *msgview;
1348         gpointer parent = toolbar_item->parent;
1349
1350         g_return_if_fail(toolbar_item != NULL);
1351
1352         switch (toolbar_item->type) {
1353         case TOOLBAR_MAIN:
1354                 mainwin = (MainWindow*)parent;
1355                 action_list = mainwin->toolbar->action_list;
1356                 break;
1357         case TOOLBAR_COMPOSE:
1358                 compose = (Compose*)parent;
1359                 action_list = compose->toolbar->action_list;
1360                 break;
1361         case TOOLBAR_MSGVIEW:
1362                 msgview = (MessageView*)parent;
1363                 action_list = msgview->toolbar->action_list;
1364                 break;
1365         default:
1366                 debug_print("toolbar event not supported\n");
1367                 return;
1368         }
1369         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1370 }
1371
1372 static MainWindow *get_mainwin(gpointer data)
1373 {
1374         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1375         MainWindow *mainwin = NULL;
1376         MessageView *msgview;
1377
1378         g_return_val_if_fail(toolbar_item != NULL, NULL);
1379
1380         switch(toolbar_item->type) {
1381         case TOOLBAR_MAIN:
1382                 mainwin = (MainWindow*)toolbar_item->parent;
1383                 break;
1384         case TOOLBAR_MSGVIEW:
1385                 msgview = (MessageView*)toolbar_item->parent;
1386                 mainwin = (MainWindow*)msgview->mainwin;
1387                 break;
1388         default:
1389                 break;
1390         }
1391
1392         return mainwin;
1393 }
1394
1395 static GtkWidget *get_window_widget(ToolbarType type, gpointer data)
1396 {
1397         MainWindow *mainwin;
1398         MessageView *msgview;
1399
1400         switch (type) {
1401         case TOOLBAR_MAIN:
1402                 mainwin = (MainWindow*)data;
1403                 return mainwin->window;
1404                 break;
1405         case TOOLBAR_MSGVIEW:
1406                 msgview = (MessageView*)data;
1407                 return msgview->vbox;
1408                 break;
1409         default:
1410                 break;
1411         }
1412         return NULL;
1413 }
1414
1415 static void toolbar_buttons_cb(GtkWidget   *widget, 
1416                                ToolbarItem *item)
1417 {
1418         gint num_items;
1419         gint i;
1420         struct {
1421                 gint   index;
1422                 void (*func)(GtkWidget *widget, gpointer data);
1423         } callbacks[] = {
1424                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1425                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1426                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1427                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1428                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1429                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1430                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1431                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1432                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1433                 { A_FORWARD,            toolbar_forward_cb              },
1434                 { A_DELETE,             toolbar_delete_cb               },
1435                 { A_EXECUTE,            toolbar_exec_cb                 },
1436                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1437                 { A_IGNORE_THREAD,      toolbar_ignore_thread_cb        },
1438                 { A_PRINT,              toolbar_print_cb                },
1439
1440                 { A_SEND,               toolbar_send_cb                 },
1441                 { A_SENDL,              toolbar_send_later_cb           },
1442                 { A_DRAFT,              toolbar_draft_cb                },
1443                 { A_INSERT,             toolbar_insert_cb               },
1444                 { A_ATTACH,             toolbar_attach_cb               },
1445                 { A_SIG,                toolbar_sig_cb                  },
1446                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1447                 { A_LINEWRAP,           toolbar_linewrap_cb             },
1448                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1449
1450                 { A_SYL_ACTIONS,        toolbar_actions_execute_cb      }
1451         };
1452
1453         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1454
1455         for (i = 0; i < num_items; i++) {
1456                 if (callbacks[i].index == item->index) {
1457                         callbacks[i].func(widget, item);
1458                         return;
1459                 }
1460         }
1461 }
1462
1463 /**
1464  * Create a new toolbar with specified type
1465  * if a callback list is passed it will be used before the 
1466  * common callback list
1467  **/
1468 Toolbar *toolbar_create(ToolbarType      type, 
1469                         GtkWidget       *container,
1470                         gpointer         data)
1471 {
1472         ToolbarItem *toolbar_item;
1473
1474         GtkWidget *toolbar;
1475         GtkWidget *icon_wid = NULL;
1476         GtkWidget *icon_news;
1477         GtkWidget *item;
1478         GtkWidget *item_news;
1479         GtkWidget *window_wid;
1480
1481         guint n_menu_entries;
1482         GtkWidget *reply_popup;
1483         GtkWidget *replyall_popup;
1484         GtkWidget *replylist_popup;
1485         GtkWidget *replysender_popup;
1486         GtkWidget *fwd_popup;
1487
1488         GtkTooltips *toolbar_tips;
1489         ToolbarSylpheedActions *action_item;
1490         GSList *cur;
1491         GSList *toolbar_list;
1492         Toolbar *toolbar_data;
1493
1494         
1495         toolbar_tips = gtk_tooltips_new();
1496         
1497         toolbar_read_config_file(type);
1498         toolbar_list = toolbar_get_list(type);
1499
1500         toolbar_data = g_new0(Toolbar, 1); 
1501
1502         toolbar = gtk_toolbar_new();
1503         gtk_container_add(GTK_CONTAINER(container), toolbar);
1504         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1505         gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL);
1506         gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
1507         
1508         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1509
1510                 if (g_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1511                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1512                         continue;
1513                 }
1514                 
1515                 toolbar_item = g_new0(ToolbarItem, 1); 
1516                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1517                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1518                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1519                 toolbar_item->parent = data;
1520                 toolbar_item->type = type;
1521
1522                 /* collect toolbar items in list to keep track */
1523                 toolbar_data->item_list = 
1524                         g_slist_append(toolbar_data->item_list, 
1525                                        toolbar_item);
1526
1527                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1528                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1529                                                 toolbar_item->text,
1530                                                 (""),
1531                                                 (""),
1532                                                 icon_wid, G_CALLBACK(toolbar_buttons_cb), 
1533                                                 toolbar_item);
1534                 
1535                 switch (toolbar_item->index) {
1536
1537                 case A_RECEIVE_ALL:
1538                         toolbar_data->getall_btn = item;
1539                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1540                                              toolbar_data->getall_btn, 
1541                                            _("Receive Mail on all Accounts"), NULL);
1542                         break;
1543                 case A_RECEIVE_CUR:
1544                         toolbar_data->get_btn = item;
1545                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1546                                              toolbar_data->get_btn,
1547                                            _("Receive Mail on current Account"), NULL);
1548                         break;
1549                 case A_SEND_QUEUED:
1550                         toolbar_data->send_btn = item; 
1551                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1552                                              toolbar_data->send_btn,
1553                                            _("Send Queued Message(s)"), NULL);
1554                         break;
1555                 case A_COMPOSE_EMAIL:
1556                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1557                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1558                                                             _("News"),
1559                                                             (""),
1560                                                             (""),
1561                                                             icon_news, G_CALLBACK(toolbar_buttons_cb), 
1562                                                             toolbar_item);
1563                         toolbar_data->compose_mail_btn = item; 
1564                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1565                                              toolbar_data->compose_mail_btn,
1566                                            _("Compose Email"), NULL);
1567                         toolbar_data->compose_news_btn = item_news;
1568                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1569                                              toolbar_data->compose_news_btn,
1570                                            _("Compose News"), NULL);
1571                         break;
1572                 case A_REPLY_MESSAGE:
1573                         toolbar_data->reply_btn = item;
1574                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1575                                              toolbar_data->reply_btn,
1576                                            _("Reply to Message"), NULL);
1577                         gtk_signal_connect(GTK_OBJECT(toolbar_data->reply_btn), 
1578                                            "button_press_event",
1579                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_cb),
1580                                            toolbar_data);
1581                         n_menu_entries = sizeof(reply_popup_entries) /
1582                                 sizeof(reply_popup_entries[0]);
1583
1584                         window_wid = get_window_widget(type, data);
1585                         reply_popup = popupmenu_create(window_wid,
1586                                                        reply_popup_entries, n_menu_entries,
1587                                                        "<ReplyPopup>", (gpointer)toolbar_item);
1588
1589                         gtk_signal_connect(GTK_OBJECT(reply_popup), "selection_done",
1590                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_closed_cb), toolbar_item);
1591                         toolbar_data->reply_popup = reply_popup;
1592                         break;
1593                 case A_REPLY_SENDER:
1594                         toolbar_data->replysender_btn = item;
1595                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1596                                              toolbar_data->replysender_btn,
1597                                            _("Reply to Sender"), NULL);
1598                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replysender_btn), 
1599                                            "button_press_event",
1600                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_cb),
1601                                            toolbar_data);
1602                         n_menu_entries = sizeof(replysender_popup_entries) /
1603                                 sizeof(replysender_popup_entries[0]);
1604
1605                         window_wid = get_window_widget(type, data);
1606                         replysender_popup = popupmenu_create(window_wid, 
1607                                                              replysender_popup_entries, n_menu_entries,
1608                                                              "<ReplySenderPopup>", (gpointer)toolbar_item);
1609
1610                         gtk_signal_connect(GTK_OBJECT(replysender_popup), "selection_done",
1611                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_closed_cb), toolbar_item);
1612                         toolbar_data->replysender_popup = replysender_popup;
1613                         break;
1614                 case A_REPLY_ALL:
1615                         toolbar_data->replyall_btn = item;
1616                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1617                                              toolbar_data->replyall_btn,
1618                                            _("Reply to All"), NULL);
1619                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replyall_btn), 
1620                                            "button_press_event",
1621                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_cb),
1622                                            toolbar_data);
1623                         n_menu_entries = sizeof(replyall_popup_entries) /
1624                                 sizeof(replyall_popup_entries[0]);
1625
1626                         window_wid = get_window_widget(type, data);     
1627                         replyall_popup = popupmenu_create(window_wid, 
1628                                                           replyall_popup_entries, n_menu_entries,
1629                                                           "<ReplyAllPopup>", (gpointer)toolbar_item);
1630         
1631                         gtk_signal_connect(GTK_OBJECT(replyall_popup), "selection_done",
1632                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_closed_cb), toolbar_item);
1633                         toolbar_data->replyall_popup = replyall_popup;
1634                         break;
1635                 case A_REPLY_ML:
1636                         toolbar_data->replylist_btn = item;
1637                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1638                                              toolbar_data->replylist_btn,
1639                                            _("Reply to Mailing-list"), NULL);
1640                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replylist_btn), 
1641                                            "button_press_event",
1642                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_cb),
1643                                            toolbar_data);
1644                         n_menu_entries = sizeof(replylist_popup_entries) /
1645                                 sizeof(replylist_popup_entries[0]);
1646
1647                         window_wid = get_window_widget(type, data);
1648                         replylist_popup = popupmenu_create(window_wid, 
1649                                                            replylist_popup_entries, n_menu_entries,
1650                                                            "<ReplyMlPopup>", (gpointer)toolbar_item);
1651                 
1652                         gtk_signal_connect(GTK_OBJECT(replylist_popup), "selection_done",
1653                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_closed_cb), toolbar_item);
1654                         toolbar_data->replylist_popup = replylist_popup;
1655                         break;
1656                 case A_FORWARD:
1657                         toolbar_data->fwd_btn = item;
1658                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1659                                              toolbar_data->fwd_btn,
1660                                              _("Forward Message"), NULL);
1661                         gtk_signal_connect(GTK_OBJECT(toolbar_data->fwd_btn), 
1662                                            "button_press_event",
1663                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_cb),
1664                                            toolbar_data);
1665                         n_menu_entries = sizeof(fwd_popup_entries) /
1666                                 sizeof(fwd_popup_entries[0]);
1667
1668                         window_wid = get_window_widget(type, data);
1669                         fwd_popup = popupmenu_create(window_wid, 
1670                                                      fwd_popup_entries, n_menu_entries,
1671                                                      "<ForwardPopup>", (gpointer)toolbar_item);
1672
1673                         gtk_signal_connect(GTK_OBJECT(fwd_popup), "selection_done",
1674                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_closed_cb), toolbar_item);
1675                         toolbar_data->fwd_popup = fwd_popup;
1676                         break;
1677                 case A_DELETE:
1678                         toolbar_data->delete_btn = item;
1679                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1680                                              toolbar_data->delete_btn,
1681                                              _("Delete Message"), NULL);
1682                         break;
1683                 case A_EXECUTE:
1684                         toolbar_data->exec_btn = item;
1685                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1686                                              toolbar_data->exec_btn,
1687                                            _("Execute"), NULL);
1688                         break;
1689                 case A_GOTO_NEXT:
1690                         toolbar_data->next_btn = item;
1691                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1692                                              toolbar_data->next_btn,
1693                                              _("Goto Next Message"), NULL);
1694                         break;
1695                 
1696                 /* Compose Toolbar */
1697                 case A_SEND:
1698                         toolbar_data->send_btn = item;
1699                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1700                                              toolbar_data->send_btn, 
1701                                              _("Send Message"), NULL);
1702                         break;
1703                 case A_SENDL:
1704                         toolbar_data->sendl_btn = item;
1705                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1706                                              toolbar_data->sendl_btn,
1707                                              _("Put into queue folder and send later"), NULL);
1708                         break;
1709                 case A_DRAFT:
1710                         toolbar_data->draft_btn = item; 
1711                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1712                                              toolbar_data->draft_btn,
1713                                              _("Save to draft folder"), NULL);
1714                         break;
1715                 case A_INSERT:
1716                         toolbar_data->insert_btn = item; 
1717                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1718                                              toolbar_data->insert_btn,
1719                                              _("Insert file"), NULL);
1720                         break;
1721                 case A_ATTACH:
1722                         toolbar_data->attach_btn = item;
1723                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1724                                              toolbar_data->attach_btn,
1725                                              _("Attach file"), NULL);
1726                         break;
1727                 case A_SIG:
1728                         toolbar_data->sig_btn = item;
1729                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1730                                              toolbar_data->sig_btn,
1731                                              _("Insert signature"), NULL);
1732                         break;
1733                 case A_EXTEDITOR:
1734                         toolbar_data->exteditor_btn = item;
1735                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1736                                              toolbar_data->exteditor_btn,
1737                                              _("Edit with external editor"), NULL);
1738                         break;
1739                 case A_LINEWRAP:
1740                         toolbar_data->linewrap_btn = item;
1741                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1742                                              toolbar_data->linewrap_btn,
1743                                              _("Wrap all long lines"), NULL);
1744                         break;
1745                 case A_ADDRBOOK:
1746                         toolbar_data->addrbook_btn = item;
1747                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1748                                              toolbar_data->addrbook_btn,
1749                                              _("Address book"), NULL);
1750                         break;
1751
1752                 case A_SYL_ACTIONS:
1753                         action_item = g_new0(ToolbarSylpheedActions, 1);
1754                         action_item->widget = item;
1755                         action_item->name   = g_strdup(toolbar_item->text);
1756
1757                         toolbar_data->action_list = 
1758                                 g_slist_append(toolbar_data->action_list,
1759                                                action_item);
1760
1761                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1762                                              item,
1763                                              action_item->name, NULL);
1764
1765                         gtk_widget_show(item);
1766                         break;
1767                 default:
1768                         /* find and set the tool tip text */
1769                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips),
1770                                              item,
1771                                              toolbar_ret_descr_from_val
1772                                                 (toolbar_item->index),
1773                                              NULL);
1774                         break;
1775                 }
1776
1777         }
1778         toolbar_data->toolbar = toolbar;
1779         if (type == TOOLBAR_MAIN)
1780                 activate_compose_button(toolbar_data, 
1781                                         prefs_common.toolbar_style, 
1782                                         toolbar_data->compose_btn_type);
1783         
1784         gtk_widget_show_all(toolbar);
1785         
1786         return toolbar_data; 
1787 }
1788
1789 /**
1790  * Free toolbar structures
1791  */ 
1792 void toolbar_destroy(Toolbar * toolbar) {
1793
1794         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
1795         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
1796 }
1797
1798 void toolbar_update(ToolbarType type, gpointer data)
1799 {
1800         Toolbar *toolbar_data;
1801         GtkWidget *handlebox;
1802         MainWindow *mainwin = (MainWindow*)data;
1803         Compose    *compose = (Compose*)data;
1804         MessageView *msgview = (MessageView*)data;
1805
1806         switch(type) {
1807         case TOOLBAR_MAIN:
1808                 toolbar_data = mainwin->toolbar;
1809                 handlebox    = mainwin->handlebox;
1810                 break;
1811         case TOOLBAR_COMPOSE:
1812                 toolbar_data = compose->toolbar;
1813                 handlebox    = compose->handlebox;
1814                 break;
1815         case TOOLBAR_MSGVIEW:
1816                 toolbar_data = msgview->toolbar;
1817                 handlebox    = msgview->handlebox;
1818                 break;
1819         default:
1820                 return;
1821         }
1822
1823         gtk_container_remove(GTK_CONTAINER(handlebox), 
1824                              GTK_WIDGET(toolbar_data->toolbar));
1825
1826         toolbar_init(toolbar_data);
1827         toolbar_data = toolbar_create(type, handlebox, data);
1828         switch(type) {
1829         case TOOLBAR_MAIN:
1830                 mainwin->toolbar = toolbar_data;
1831                 break;
1832         case TOOLBAR_COMPOSE:
1833                 compose->toolbar = toolbar_data;
1834                 break;
1835         case TOOLBAR_MSGVIEW:
1836                 msgview->toolbar = toolbar_data;
1837                 break;
1838         }
1839
1840         toolbar_style(type, prefs_common.toolbar_style, data);
1841
1842         if (type == TOOLBAR_MAIN)
1843                 toolbar_main_set_sensitive((MainWindow*)data);
1844 }
1845
1846 void toolbar_main_set_sensitive(gpointer data)
1847 {
1848         SensitiveCond state;
1849         gboolean sensitive;
1850         MainWindow *mainwin = (MainWindow*)data;
1851         Toolbar *toolbar = mainwin->toolbar;
1852         GSList *cur;
1853         GSList *entry_list = NULL;
1854         
1855         typedef struct _Entry Entry;
1856         struct _Entry {
1857                 GtkWidget *widget;
1858                 SensitiveCond cond;
1859                 gboolean empty;
1860         };
1861
1862 #define SET_WIDGET_COND(w, c)     \
1863 { \
1864         Entry *e = g_new0(Entry, 1); \
1865         e->widget = w; \
1866         e->cond   = c; \
1867         entry_list = g_slist_append(entry_list, e); \
1868 }
1869
1870         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1871         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1872         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
1873         SET_WIDGET_COND(toolbar->reply_btn,
1874                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1875         SET_WIDGET_COND(toolbar->replyall_btn,
1876                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1877         SET_WIDGET_COND(toolbar->replylist_btn,
1878                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1879         SET_WIDGET_COND(toolbar->replysender_btn,
1880                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1881         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
1882
1883         SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
1884         SET_WIDGET_COND(toolbar->delete_btn,
1885                         M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED);
1886         SET_WIDGET_COND(toolbar->exec_btn, M_DELAY_EXEC);
1887
1888         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
1889                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
1890                 
1891                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
1892         }
1893
1894 #undef SET_WIDGET_COND
1895
1896         state = main_window_get_current_state(mainwin);
1897
1898         for (cur = entry_list; cur != NULL; cur = cur->next) {
1899                 Entry *e = (Entry*) cur->data;
1900
1901                 if (e->widget != NULL) {
1902                         sensitive = ((e->cond & state) == e->cond);
1903                         gtk_widget_set_sensitive(e->widget, sensitive); 
1904                 }
1905         }
1906         
1907         while (entry_list != NULL) {
1908                 Entry *e = (Entry*) entry_list->data;
1909
1910                 if (e)
1911                         g_free(e);
1912                 entry_list = g_slist_remove(entry_list, e);
1913         }
1914
1915         g_slist_free(entry_list);
1916
1917         activate_compose_button(toolbar, 
1918                                 prefs_common.toolbar_style,
1919                                 toolbar->compose_btn_type);
1920 }
1921
1922 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
1923 {
1924         Compose *compose = (Compose*)data;
1925         GSList *items = compose->toolbar->action_list;
1926
1927         if (compose->toolbar->send_btn)
1928                 gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
1929         if (compose->toolbar->sendl_btn)
1930                 gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
1931         if (compose->toolbar->draft_btn )
1932                 gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
1933         if (compose->toolbar->insert_btn )
1934                 gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
1935         if (compose->toolbar->attach_btn)
1936                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
1937         if (compose->toolbar->sig_btn)
1938                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
1939         if (compose->toolbar->exteditor_btn)
1940                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
1941         if (compose->toolbar->linewrap_btn)
1942                 gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, sensitive);
1943         if (compose->toolbar->addrbook_btn)
1944                 gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
1945         for (; items != NULL; items = g_slist_next(items)) {
1946                 ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
1947                 gtk_widget_set_sensitive(item->widget, sensitive);
1948         }
1949 }
1950
1951 /**
1952  * Initialize toolbar structure
1953  **/
1954 void toolbar_init(Toolbar * toolbar) {
1955
1956         toolbar->toolbar          = NULL;
1957         toolbar->get_btn          = NULL;
1958         toolbar->getall_btn       = NULL;
1959         toolbar->send_btn         = NULL;
1960         toolbar->compose_mail_btn = NULL;
1961         toolbar->compose_news_btn = NULL;
1962         toolbar->reply_btn        = NULL;
1963         toolbar->replysender_btn  = NULL;
1964         toolbar->replyall_btn     = NULL;
1965         toolbar->replylist_btn    = NULL;
1966         toolbar->fwd_btn          = NULL;
1967         toolbar->delete_btn       = NULL;
1968         toolbar->next_btn         = NULL;
1969         toolbar->exec_btn         = NULL;
1970
1971         /* compose buttons */ 
1972         toolbar->sendl_btn        = NULL;
1973         toolbar->draft_btn        = NULL;
1974         toolbar->insert_btn       = NULL;
1975         toolbar->attach_btn       = NULL;
1976         toolbar->sig_btn          = NULL;       
1977         toolbar->exteditor_btn    = NULL;       
1978         toolbar->linewrap_btn     = NULL;       
1979         toolbar->addrbook_btn     = NULL;       
1980
1981         toolbar_destroy(toolbar);
1982 }
1983
1984 /*
1985  */
1986 static void toolbar_reply(gpointer data, guint action)
1987 {
1988         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1989         MainWindow *mainwin;
1990         MessageView *msgview;
1991         GSList *msginfo_list = NULL;
1992         gchar *body;
1993
1994         g_return_if_fail(toolbar_item != NULL);
1995
1996         switch (toolbar_item->type) {
1997         case TOOLBAR_MAIN:
1998                 mainwin = (MainWindow*)toolbar_item->parent;
1999                 msginfo_list = summary_get_selection(mainwin->summaryview);
2000                 msgview = (MessageView*)mainwin->messageview;
2001                 break;
2002         case TOOLBAR_MSGVIEW:
2003                 msgview = (MessageView*)toolbar_item->parent;
2004                 msginfo_list = g_slist_append(msginfo_list, msgview->msginfo);
2005                 break;
2006         default:
2007                 return;
2008         }
2009
2010         g_return_if_fail(msgview != NULL);
2011         body = messageview_get_selection(msgview);
2012
2013         g_return_if_fail(msginfo_list != NULL);
2014         compose_reply_mode((ComposeMode)action, msginfo_list, body);
2015
2016         g_free(body);
2017         g_slist_free(msginfo_list);
2018
2019         /* TODO: update reply state ion summaryview */
2020 }
2021
2022
2023 /* exported functions */
2024
2025 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2026 {
2027         MainWindow *mainwin = (MainWindow*)data;
2028
2029         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2030 }
2031
2032 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2033 {
2034         MainWindow *mainwin = (MainWindow*)data;
2035
2036         inc_all_account_mail(mainwin, FALSE, prefs_common.newmail_notify_manu);
2037 }
2038
2039 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2040 {
2041         GList *list;
2042
2043         if (prefs_common.work_offline)
2044                 if (alertpanel(_("Offline warning"), 
2045                                _("You're working offline. Override?"),
2046                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
2047                 return;
2048
2049         for (list = folder_get_list(); list != NULL; list = list->next) {
2050                 Folder *folder = list->data;
2051
2052                 if (folder->queue) {
2053                         procmsg_send_queue(folder->queue, prefs_common.savemsg);
2054                         folder_item_scan(folder->queue);
2055                 }
2056         }
2057 }
2058
2059 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2060 {
2061         MainWindow *mainwin = (MainWindow*)data;
2062         PrefsAccount *ac = NULL;
2063         FolderItem *item = mainwin->summaryview->folder_item;   
2064         GList * list;
2065         GList * cur;
2066
2067         if (item) {
2068                 ac = account_find_from_item(item);
2069                 if (ac && ac->protocol != A_NNTP) {
2070                         compose_new_with_folderitem(ac, item);          /* CLAWS */
2071                         return;
2072                 }
2073         }
2074
2075         /*
2076          * CLAWS - use current account
2077          */
2078         if (cur_account && (cur_account->protocol != A_NNTP)) {
2079                 compose_new_with_folderitem(cur_account, item);
2080                 return;
2081         }
2082
2083         /*
2084          * CLAWS - just get the first one
2085          */
2086         list = account_get_list();
2087         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2088                 ac = (PrefsAccount *) cur->data;
2089                 if (ac->protocol != A_NNTP) {
2090                         compose_new_with_folderitem(ac, item);
2091                         return;
2092                 }
2093         }
2094 }
2095
2096 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2097 {
2098         MainWindow *mainwin = (MainWindow*)data;
2099         PrefsAccount * ac = NULL;
2100         GList * list;
2101         GList * cur;
2102
2103         if (mainwin->summaryview->folder_item) {
2104                 ac = mainwin->summaryview->folder_item->folder->account;
2105                 if (ac && ac->protocol == A_NNTP) {
2106                         compose_new(ac,
2107                                     mainwin->summaryview->folder_item->path,
2108                                     NULL);
2109                         return;
2110                 }
2111         }
2112
2113         list = account_get_list();
2114         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2115                 ac = (PrefsAccount *) cur->data;
2116                 if (ac->protocol == A_NNTP) {
2117                         compose_new(ac, NULL, NULL);
2118                         return;
2119                 }
2120         }
2121 }