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