* AUTHORS
[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         MessageView *msgview;
1259
1260         g_return_if_fail(toolbar_item != NULL);
1261
1262         switch (toolbar_item->type) {
1263         case TOOLBAR_MAIN:
1264                 mainwin = (MainWindow *) toolbar_item->parent;
1265                 summary_toggle_ignore_thread(mainwin->summaryview);
1266                 break;
1267         case TOOLBAR_MSGVIEW:
1268                 /* TODO: see toolbar_next_unread_cb() if you need
1269                  * this in the message view */
1270                 break;
1271         default:
1272                 debug_print("toolbar event not supported\n");
1273                 break;
1274         }
1275 }
1276
1277 static void toolbar_print_cb(GtkWidget *widget, gpointer data)
1278 {
1279         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1280         MainWindow *mainwin;
1281         MessageView *msgview;
1282
1283         g_return_if_fail(toolbar_item != NULL);
1284
1285         switch (toolbar_item->type) {
1286         case TOOLBAR_MAIN:
1287                 mainwin = (MainWindow *) toolbar_item->parent;
1288                 summary_print(mainwin->summaryview);
1289                 break;
1290         case TOOLBAR_MSGVIEW:
1291                 /* TODO: see toolbar_next_unread_cb() if you need
1292                  * this in the message view */
1293                 break;
1294         default:
1295                 debug_print("toolbar event not supported\n");
1296                 break;
1297         }
1298 }
1299
1300 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
1301 {
1302         compose_toolbar_cb(A_SEND, data);
1303 }
1304
1305 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
1306 {
1307         compose_toolbar_cb(A_SENDL, data);
1308 }
1309
1310 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
1311 {
1312         compose_toolbar_cb(A_DRAFT, data);
1313 }
1314
1315 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
1316 {
1317         compose_toolbar_cb(A_INSERT, data);
1318 }
1319
1320 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
1321 {
1322         compose_toolbar_cb(A_ATTACH, data);
1323 }
1324
1325 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
1326 {
1327         compose_toolbar_cb(A_SIG, data);
1328 }
1329
1330 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
1331 {
1332         compose_toolbar_cb(A_EXTEDITOR, data);
1333 }
1334
1335 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
1336 {
1337         compose_toolbar_cb(A_LINEWRAP, data);
1338 }
1339
1340 /*
1341  * Execute actions from toolbar
1342  */
1343 static void toolbar_actions_execute_cb(GtkWidget *widget, gpointer data)
1344 {
1345         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1346         GSList *action_list;
1347         MainWindow *mainwin;
1348         Compose *compose;
1349         MessageView *msgview;
1350         gpointer parent = toolbar_item->parent;
1351
1352         g_return_if_fail(toolbar_item != NULL);
1353
1354         switch (toolbar_item->type) {
1355         case TOOLBAR_MAIN:
1356                 mainwin = (MainWindow*)parent;
1357                 action_list = mainwin->toolbar->action_list;
1358                 break;
1359         case TOOLBAR_COMPOSE:
1360                 compose = (Compose*)parent;
1361                 action_list = compose->toolbar->action_list;
1362                 break;
1363         case TOOLBAR_MSGVIEW:
1364                 msgview = (MessageView*)parent;
1365                 action_list = msgview->toolbar->action_list;
1366                 break;
1367         default:
1368                 debug_print("toolbar event not supported\n");
1369                 return;
1370         }
1371         toolbar_action_execute(widget, action_list, parent, toolbar_item->type);        
1372 }
1373
1374 static MainWindow *get_mainwin(gpointer data)
1375 {
1376         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1377         MainWindow *mainwin = NULL;
1378         MessageView *msgview;
1379
1380         g_return_val_if_fail(toolbar_item != NULL, NULL);
1381
1382         switch(toolbar_item->type) {
1383         case TOOLBAR_MAIN:
1384                 mainwin = (MainWindow*)toolbar_item->parent;
1385                 break;
1386         case TOOLBAR_MSGVIEW:
1387                 msgview = (MessageView*)toolbar_item->parent;
1388                 mainwin = (MainWindow*)msgview->mainwin;
1389                 break;
1390         default:
1391                 break;
1392         }
1393
1394         return mainwin;
1395 }
1396
1397 static GtkWidget *get_window_widget(ToolbarType type, gpointer data)
1398 {
1399         MainWindow *mainwin;
1400         MessageView *msgview;
1401
1402         switch (type) {
1403         case TOOLBAR_MAIN:
1404                 mainwin = (MainWindow*)data;
1405                 return mainwin->window;
1406                 break;
1407         case TOOLBAR_MSGVIEW:
1408                 msgview = (MessageView*)data;
1409                 return msgview->vbox;
1410                 break;
1411         default:
1412                 break;
1413         }
1414         return NULL;
1415 }
1416
1417 static void toolbar_buttons_cb(GtkWidget   *widget, 
1418                                ToolbarItem *item)
1419 {
1420         gint num_items;
1421         gint i;
1422         struct {
1423                 gint   index;
1424                 void (*func)(GtkWidget *widget, gpointer data);
1425         } callbacks[] = {
1426                 { A_RECEIVE_ALL,        toolbar_inc_all_cb              },
1427                 { A_RECEIVE_CUR,        toolbar_inc_cb                  },
1428                 { A_SEND_QUEUED,        toolbar_send_queued_cb          },
1429                 { A_COMPOSE_EMAIL,      toolbar_compose_cb              },
1430                 { A_COMPOSE_NEWS,       toolbar_compose_cb              },
1431                 { A_REPLY_MESSAGE,      toolbar_reply_cb                },
1432                 { A_REPLY_SENDER,       toolbar_reply_to_sender_cb      },
1433                 { A_REPLY_ALL,          toolbar_reply_to_all_cb         },
1434                 { A_REPLY_ML,           toolbar_reply_to_list_cb        },
1435                 { A_FORWARD,            toolbar_forward_cb              },
1436                 { A_DELETE,             toolbar_delete_cb               },
1437                 { A_EXECUTE,            toolbar_exec_cb                 },
1438                 { A_GOTO_NEXT,          toolbar_next_unread_cb          },
1439                 { A_IGNORE_THREAD,      toolbar_ignore_thread_cb        },
1440                 { A_PRINT,              toolbar_print_cb                },
1441
1442                 { A_SEND,               toolbar_send_cb                 },
1443                 { A_SENDL,              toolbar_send_later_cb           },
1444                 { A_DRAFT,              toolbar_draft_cb                },
1445                 { A_INSERT,             toolbar_insert_cb               },
1446                 { A_ATTACH,             toolbar_attach_cb               },
1447                 { A_SIG,                toolbar_sig_cb                  },
1448                 { A_EXTEDITOR,          toolbar_ext_editor_cb           },
1449                 { A_LINEWRAP,           toolbar_linewrap_cb             },
1450                 { A_ADDRBOOK,           toolbar_addrbook_cb             },
1451
1452                 { A_SYL_ACTIONS,        toolbar_actions_execute_cb      }
1453         };
1454
1455         num_items = sizeof(callbacks)/sizeof(callbacks[0]);
1456
1457         for (i = 0; i < num_items; i++) {
1458                 if (callbacks[i].index == item->index) {
1459                         callbacks[i].func(widget, item);
1460                         return;
1461                 }
1462         }
1463 }
1464
1465 /**
1466  * Create a new toolbar with specified type
1467  * if a callback list is passed it will be used before the 
1468  * common callback list
1469  **/
1470 Toolbar *toolbar_create(ToolbarType      type, 
1471                         GtkWidget       *container,
1472                         gpointer         data)
1473 {
1474         ToolbarItem *toolbar_item;
1475
1476         GtkWidget *toolbar;
1477         GtkWidget *icon_wid = NULL;
1478         GtkWidget *icon_news;
1479         GtkWidget *item;
1480         GtkWidget *item_news;
1481         GtkWidget *window_wid;
1482
1483         guint n_menu_entries;
1484         GtkWidget *reply_popup;
1485         GtkWidget *replyall_popup;
1486         GtkWidget *replylist_popup;
1487         GtkWidget *replysender_popup;
1488         GtkWidget *fwd_popup;
1489
1490         GtkTooltips *toolbar_tips;
1491         ToolbarSylpheedActions *action_item;
1492         GSList *cur;
1493         GSList *toolbar_list;
1494         Toolbar *toolbar_data;
1495
1496         
1497         toolbar_tips = gtk_tooltips_new();
1498         
1499         toolbar_read_config_file(type);
1500         toolbar_list = toolbar_get_list(type);
1501
1502         toolbar_data = g_new0(Toolbar, 1); 
1503
1504         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
1505                                   GTK_TOOLBAR_BOTH);
1506         gtk_container_add(GTK_CONTAINER(container), toolbar);
1507         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
1508         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
1509         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
1510                                     GTK_TOOLBAR_SPACE_LINE);
1511         
1512         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
1513
1514                 if (g_strcasecmp(((ToolbarItem*)cur->data)->file, TOOLBAR_TAG_SEPARATOR) == 0) {
1515                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
1516                         continue;
1517                 }
1518                 
1519                 toolbar_item = g_new0(ToolbarItem, 1); 
1520                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
1521                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
1522                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
1523                 toolbar_item->parent = data;
1524                 toolbar_item->type = type;
1525
1526                 /* collect toolbar items in list to keep track */
1527                 toolbar_data->item_list = 
1528                         g_slist_append(toolbar_data->item_list, 
1529                                        toolbar_item);
1530
1531                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
1532                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1533                                                 toolbar_item->text,
1534                                                 (""),
1535                                                 (""),
1536                                                 icon_wid, toolbar_buttons_cb, 
1537                                                 toolbar_item);
1538                 
1539                 switch (toolbar_item->index) {
1540
1541                 case A_RECEIVE_ALL:
1542                         toolbar_data->getall_btn = item;
1543                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1544                                              toolbar_data->getall_btn, 
1545                                            _("Receive Mail on all Accounts"), NULL);
1546                         break;
1547                 case A_RECEIVE_CUR:
1548                         toolbar_data->get_btn = item;
1549                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1550                                              toolbar_data->get_btn,
1551                                            _("Receive Mail on current Account"), NULL);
1552                         break;
1553                 case A_SEND_QUEUED:
1554                         toolbar_data->send_btn = item; 
1555                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1556                                              toolbar_data->send_btn,
1557                                            _("Send Queued Message(s)"), NULL);
1558                         break;
1559                 case A_COMPOSE_EMAIL:
1560                         icon_news = stock_pixmap_widget(container, STOCK_PIXMAP_NEWS_COMPOSE);
1561                         item_news = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
1562                                                             _("News"),
1563                                                             (""),
1564                                                             (""),
1565                                                             icon_news, toolbar_buttons_cb, 
1566                                                             toolbar_item);
1567                         toolbar_data->compose_mail_btn = item; 
1568                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1569                                              toolbar_data->compose_mail_btn,
1570                                            _("Compose Email"), NULL);
1571                         toolbar_data->compose_news_btn = item_news;
1572                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1573                                              toolbar_data->compose_news_btn,
1574                                            _("Compose News"), NULL);
1575                         break;
1576                 case A_REPLY_MESSAGE:
1577                         toolbar_data->reply_btn = item;
1578                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1579                                              toolbar_data->reply_btn,
1580                                            _("Reply to Message"), NULL);
1581                         gtk_signal_connect(GTK_OBJECT(toolbar_data->reply_btn), 
1582                                            "button_press_event",
1583                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_cb),
1584                                            toolbar_data);
1585                         n_menu_entries = sizeof(reply_popup_entries) /
1586                                 sizeof(reply_popup_entries[0]);
1587
1588                         window_wid = get_window_widget(type, data);
1589                         reply_popup = popupmenu_create(window_wid,
1590                                                        reply_popup_entries, n_menu_entries,
1591                                                        "<ReplyPopup>", (gpointer)toolbar_item);
1592
1593                         gtk_signal_connect(GTK_OBJECT(reply_popup), "selection_done",
1594                                            GTK_SIGNAL_FUNC(toolbar_reply_popup_closed_cb), toolbar_item);
1595                         toolbar_data->reply_popup = reply_popup;
1596                         break;
1597                 case A_REPLY_SENDER:
1598                         toolbar_data->replysender_btn = item;
1599                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1600                                              toolbar_data->replysender_btn,
1601                                            _("Reply to Sender"), NULL);
1602                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replysender_btn), 
1603                                            "button_press_event",
1604                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_cb),
1605                                            toolbar_data);
1606                         n_menu_entries = sizeof(replysender_popup_entries) /
1607                                 sizeof(replysender_popup_entries[0]);
1608
1609                         window_wid = get_window_widget(type, data);
1610                         replysender_popup = popupmenu_create(window_wid, 
1611                                                              replysender_popup_entries, n_menu_entries,
1612                                                              "<ReplySenderPopup>", (gpointer)toolbar_item);
1613
1614                         gtk_signal_connect(GTK_OBJECT(replysender_popup), "selection_done",
1615                                            GTK_SIGNAL_FUNC(toolbar_reply_to_sender_popup_closed_cb), toolbar_item);
1616                         toolbar_data->replysender_popup = replysender_popup;
1617                         break;
1618                 case A_REPLY_ALL:
1619                         toolbar_data->replyall_btn = item;
1620                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1621                                              toolbar_data->replyall_btn,
1622                                            _("Reply to All"), NULL);
1623                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replyall_btn), 
1624                                            "button_press_event",
1625                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_cb),
1626                                            toolbar_data);
1627                         n_menu_entries = sizeof(replyall_popup_entries) /
1628                                 sizeof(replyall_popup_entries[0]);
1629
1630                         window_wid = get_window_widget(type, data);     
1631                         replyall_popup = popupmenu_create(window_wid, 
1632                                                           replyall_popup_entries, n_menu_entries,
1633                                                           "<ReplyAllPopup>", (gpointer)toolbar_item);
1634         
1635                         gtk_signal_connect(GTK_OBJECT(replyall_popup), "selection_done",
1636                                            GTK_SIGNAL_FUNC(toolbar_reply_to_all_popup_closed_cb), toolbar_item);
1637                         toolbar_data->replyall_popup = replyall_popup;
1638                         break;
1639                 case A_REPLY_ML:
1640                         toolbar_data->replylist_btn = item;
1641                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1642                                              toolbar_data->replylist_btn,
1643                                            _("Reply to Mailing-list"), NULL);
1644                         gtk_signal_connect(GTK_OBJECT(toolbar_data->replylist_btn), 
1645                                            "button_press_event",
1646                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_cb),
1647                                            toolbar_data);
1648                         n_menu_entries = sizeof(replylist_popup_entries) /
1649                                 sizeof(replylist_popup_entries[0]);
1650
1651                         window_wid = get_window_widget(type, data);
1652                         replylist_popup = popupmenu_create(window_wid, 
1653                                                            replylist_popup_entries, n_menu_entries,
1654                                                            "<ReplyMlPopup>", (gpointer)toolbar_item);
1655                 
1656                         gtk_signal_connect(GTK_OBJECT(replylist_popup), "selection_done",
1657                                            GTK_SIGNAL_FUNC(toolbar_reply_to_list_popup_closed_cb), toolbar_item);
1658                         toolbar_data->replylist_popup = replylist_popup;
1659                         break;
1660                 case A_FORWARD:
1661                         toolbar_data->fwd_btn = item;
1662                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1663                                              toolbar_data->fwd_btn,
1664                                              _("Forward Message"), NULL);
1665                         gtk_signal_connect(GTK_OBJECT(toolbar_data->fwd_btn), 
1666                                            "button_press_event",
1667                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_cb),
1668                                            toolbar_data);
1669                         n_menu_entries = sizeof(fwd_popup_entries) /
1670                                 sizeof(fwd_popup_entries[0]);
1671
1672                         window_wid = get_window_widget(type, data);
1673                         fwd_popup = popupmenu_create(window_wid, 
1674                                                      fwd_popup_entries, n_menu_entries,
1675                                                      "<ForwardPopup>", (gpointer)toolbar_item);
1676
1677                         gtk_signal_connect(GTK_OBJECT(fwd_popup), "selection_done",
1678                                            GTK_SIGNAL_FUNC(toolbar_forward_popup_closed_cb), toolbar_item);
1679                         toolbar_data->fwd_popup = fwd_popup;
1680                         break;
1681                 case A_DELETE:
1682                         toolbar_data->delete_btn = item;
1683                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1684                                              toolbar_data->delete_btn,
1685                                              _("Delete Message"), NULL);
1686                         break;
1687                 case A_EXECUTE:
1688                         toolbar_data->exec_btn = item;
1689                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1690                                              toolbar_data->exec_btn,
1691                                            _("Execute"), NULL);
1692                         break;
1693                 case A_GOTO_NEXT:
1694                         toolbar_data->next_btn = item;
1695                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1696                                              toolbar_data->next_btn,
1697                                              _("Goto Next Message"), NULL);
1698                         break;
1699                 
1700                 /* Compose Toolbar */
1701                 case A_SEND:
1702                         toolbar_data->send_btn = item;
1703                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1704                                              toolbar_data->send_btn, 
1705                                              _("Send Message"), NULL);
1706                         break;
1707                 case A_SENDL:
1708                         toolbar_data->sendl_btn = item;
1709                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1710                                              toolbar_data->sendl_btn,
1711                                              _("Put into queue folder and send later"), NULL);
1712                         break;
1713                 case A_DRAFT:
1714                         toolbar_data->draft_btn = item; 
1715                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1716                                              toolbar_data->draft_btn,
1717                                              _("Save to draft folder"), NULL);
1718                         break;
1719                 case A_INSERT:
1720                         toolbar_data->insert_btn = item; 
1721                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1722                                              toolbar_data->insert_btn,
1723                                              _("Insert file"), NULL);
1724                         break;
1725                 case A_ATTACH:
1726                         toolbar_data->attach_btn = item;
1727                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1728                                              toolbar_data->attach_btn,
1729                                              _("Attach file"), NULL);
1730                         break;
1731                 case A_SIG:
1732                         toolbar_data->sig_btn = item;
1733                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1734                                              toolbar_data->sig_btn,
1735                                              _("Insert signature"), NULL);
1736                         break;
1737                 case A_EXTEDITOR:
1738                         toolbar_data->exteditor_btn = item;
1739                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1740                                              toolbar_data->exteditor_btn,
1741                                              _("Edit with external editor"), NULL);
1742                         break;
1743                 case A_LINEWRAP:
1744                         toolbar_data->linewrap_btn = item;
1745                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1746                                              toolbar_data->linewrap_btn,
1747                                              _("Wrap all long lines"), NULL);
1748                         break;
1749                 case A_ADDRBOOK:
1750                         toolbar_data->addrbook_btn = item;
1751                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1752                                              toolbar_data->addrbook_btn,
1753                                              _("Address book"), NULL);
1754                         break;
1755
1756                 case A_SYL_ACTIONS:
1757                         action_item = g_new0(ToolbarSylpheedActions, 1);
1758                         action_item->widget = item;
1759                         action_item->name   = g_strdup(toolbar_item->text);
1760
1761                         toolbar_data->action_list = 
1762                                 g_slist_append(toolbar_data->action_list,
1763                                                action_item);
1764
1765                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
1766                                              item,
1767                                              action_item->name, NULL);
1768
1769                         gtk_widget_show(item);
1770                         break;
1771                 default:
1772                         /* find and set the tool tip text */
1773                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips),
1774                                              item,
1775                                              toolbar_ret_descr_from_val
1776                                                 (toolbar_item->index),
1777                                              NULL);
1778                         break;
1779                 }
1780
1781         }
1782         toolbar_data->toolbar = toolbar;
1783         if (type == TOOLBAR_MAIN)
1784                 activate_compose_button(toolbar_data, 
1785                                         prefs_common.toolbar_style, 
1786                                         toolbar_data->compose_btn_type);
1787         
1788         gtk_widget_show_all(toolbar);
1789         
1790         return toolbar_data; 
1791 }
1792
1793 /**
1794  * Free toolbar structures
1795  */ 
1796 void toolbar_destroy(Toolbar * toolbar) {
1797
1798         TOOLBAR_DESTROY_ITEMS(toolbar->item_list);      
1799         TOOLBAR_DESTROY_ACTIONS(toolbar->action_list);
1800 }
1801
1802 void toolbar_update(ToolbarType type, gpointer data)
1803 {
1804         Toolbar *toolbar_data;
1805         GtkWidget *handlebox;
1806         MainWindow *mainwin = (MainWindow*)data;
1807         Compose    *compose = (Compose*)data;
1808         MessageView *msgview = (MessageView*)data;
1809
1810         switch(type) {
1811         case TOOLBAR_MAIN:
1812                 toolbar_data = mainwin->toolbar;
1813                 handlebox    = mainwin->handlebox;
1814                 break;
1815         case TOOLBAR_COMPOSE:
1816                 toolbar_data = compose->toolbar;
1817                 handlebox    = compose->handlebox;
1818                 break;
1819         case TOOLBAR_MSGVIEW:
1820                 toolbar_data = msgview->toolbar;
1821                 handlebox    = msgview->handlebox;
1822                 break;
1823         default:
1824                 return;
1825         }
1826
1827         gtk_container_remove(GTK_CONTAINER(handlebox), 
1828                              GTK_WIDGET(toolbar_data->toolbar));
1829
1830         toolbar_init(toolbar_data);
1831         toolbar_data = toolbar_create(type, handlebox, data);
1832         switch(type) {
1833         case TOOLBAR_MAIN:
1834                 mainwin->toolbar = toolbar_data;
1835                 break;
1836         case TOOLBAR_COMPOSE:
1837                 compose->toolbar = toolbar_data;
1838                 break;
1839         case TOOLBAR_MSGVIEW:
1840                 msgview->toolbar = toolbar_data;
1841                 break;
1842         }
1843
1844         toolbar_style(type, prefs_common.toolbar_style, data);
1845
1846         if (type == TOOLBAR_MAIN)
1847                 toolbar_main_set_sensitive((MainWindow*)data);
1848 }
1849
1850 void toolbar_main_set_sensitive(gpointer data)
1851 {
1852         SensitiveCond state;
1853         gboolean sensitive;
1854         MainWindow *mainwin = (MainWindow*)data;
1855         Toolbar *toolbar = mainwin->toolbar;
1856         GSList *cur;
1857         GSList *entry_list = NULL;
1858         
1859         typedef struct _Entry Entry;
1860         struct _Entry {
1861                 GtkWidget *widget;
1862                 SensitiveCond cond;
1863                 gboolean empty;
1864         };
1865
1866 #define SET_WIDGET_COND(w, c)     \
1867 { \
1868         Entry *e = g_new0(Entry, 1); \
1869         e->widget = w; \
1870         e->cond   = c; \
1871         entry_list = g_slist_append(entry_list, e); \
1872 }
1873
1874         SET_WIDGET_COND(toolbar->get_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1875         SET_WIDGET_COND(toolbar->getall_btn, M_HAVE_ACCOUNT|M_UNLOCKED);
1876         SET_WIDGET_COND(toolbar->compose_news_btn, M_HAVE_ACCOUNT);
1877         SET_WIDGET_COND(toolbar->reply_btn,
1878                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1879         SET_WIDGET_COND(toolbar->replyall_btn,
1880                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1881         SET_WIDGET_COND(toolbar->replylist_btn,
1882                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1883         SET_WIDGET_COND(toolbar->replysender_btn,
1884                         M_HAVE_ACCOUNT|M_SINGLE_TARGET_EXIST);
1885         SET_WIDGET_COND(toolbar->fwd_btn, M_HAVE_ACCOUNT|M_TARGET_EXIST);
1886
1887         SET_WIDGET_COND(toolbar->next_btn, M_MSG_EXIST);
1888         SET_WIDGET_COND(toolbar->delete_btn,
1889                         M_TARGET_EXIST|M_ALLOW_DELETE|M_UNLOCKED);
1890         SET_WIDGET_COND(toolbar->exec_btn, M_DELAY_EXEC);
1891
1892         for (cur = toolbar->action_list; cur != NULL;  cur = cur->next) {
1893                 ToolbarSylpheedActions *act = (ToolbarSylpheedActions*)cur->data;
1894                 
1895                 SET_WIDGET_COND(act->widget, M_TARGET_EXIST|M_UNLOCKED);
1896         }
1897
1898 #undef SET_WIDGET_COND
1899
1900         state = main_window_get_current_state(mainwin);
1901
1902         for (cur = entry_list; cur != NULL; cur = cur->next) {
1903                 Entry *e = (Entry*) cur->data;
1904
1905                 if (e->widget != NULL) {
1906                         sensitive = ((e->cond & state) == e->cond);
1907                         gtk_widget_set_sensitive(e->widget, sensitive); 
1908                 }
1909         }
1910         
1911         while (entry_list != NULL) {
1912                 Entry *e = (Entry*) entry_list->data;
1913
1914                 if (e)
1915                         g_free(e);
1916                 entry_list = g_slist_remove(entry_list, e);
1917         }
1918
1919         g_slist_free(entry_list);
1920
1921         activate_compose_button(toolbar, 
1922                                 prefs_common.toolbar_style,
1923                                 toolbar->compose_btn_type);
1924 }
1925
1926 void toolbar_comp_set_sensitive(gpointer data, gboolean sensitive)
1927 {
1928         Compose *compose = (Compose*)data;
1929         GSList *items = compose->toolbar->action_list;
1930
1931         if (compose->toolbar->send_btn)
1932                 gtk_widget_set_sensitive(compose->toolbar->send_btn, sensitive);
1933         if (compose->toolbar->sendl_btn)
1934                 gtk_widget_set_sensitive(compose->toolbar->sendl_btn, sensitive);
1935         if (compose->toolbar->draft_btn )
1936                 gtk_widget_set_sensitive(compose->toolbar->draft_btn , sensitive);
1937         if (compose->toolbar->insert_btn )
1938                 gtk_widget_set_sensitive(compose->toolbar->insert_btn , sensitive);
1939         if (compose->toolbar->attach_btn)
1940                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, sensitive);
1941         if (compose->toolbar->sig_btn)
1942                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, sensitive);
1943         if (compose->toolbar->exteditor_btn)
1944                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, sensitive);
1945         if (compose->toolbar->linewrap_btn)
1946                 gtk_widget_set_sensitive(compose->toolbar->linewrap_btn, sensitive);
1947         if (compose->toolbar->addrbook_btn)
1948                 gtk_widget_set_sensitive(compose->toolbar->addrbook_btn, sensitive);
1949         for (; items != NULL; items = g_slist_next(items)) {
1950                 ToolbarSylpheedActions *item = (ToolbarSylpheedActions *)items->data;
1951                 gtk_widget_set_sensitive(item->widget, sensitive);
1952         }
1953 }
1954
1955 /**
1956  * Initialize toolbar structure
1957  **/
1958 void toolbar_init(Toolbar * toolbar) {
1959
1960         toolbar->toolbar          = NULL;
1961         toolbar->get_btn          = NULL;
1962         toolbar->getall_btn       = NULL;
1963         toolbar->send_btn         = NULL;
1964         toolbar->compose_mail_btn = NULL;
1965         toolbar->compose_news_btn = NULL;
1966         toolbar->reply_btn        = NULL;
1967         toolbar->replysender_btn  = NULL;
1968         toolbar->replyall_btn     = NULL;
1969         toolbar->replylist_btn    = NULL;
1970         toolbar->fwd_btn          = NULL;
1971         toolbar->delete_btn       = NULL;
1972         toolbar->next_btn         = NULL;
1973         toolbar->exec_btn         = NULL;
1974
1975         /* compose buttons */ 
1976         toolbar->sendl_btn        = NULL;
1977         toolbar->draft_btn        = NULL;
1978         toolbar->insert_btn       = NULL;
1979         toolbar->attach_btn       = NULL;
1980         toolbar->sig_btn          = NULL;       
1981         toolbar->exteditor_btn    = NULL;       
1982         toolbar->linewrap_btn     = NULL;       
1983         toolbar->addrbook_btn     = NULL;       
1984
1985         toolbar_destroy(toolbar);
1986 }
1987
1988 /*
1989  */
1990 static void toolbar_reply(gpointer data, guint action)
1991 {
1992         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1993         MainWindow *mainwin;
1994         MessageView *msgview;
1995         GSList *msginfo_list = NULL;
1996         gchar *body;
1997
1998         g_return_if_fail(toolbar_item != NULL);
1999
2000         switch (toolbar_item->type) {
2001         case TOOLBAR_MAIN:
2002                 mainwin = (MainWindow*)toolbar_item->parent;
2003                 msginfo_list = summary_get_selection(mainwin->summaryview);
2004                 msgview = (MessageView*)mainwin->messageview;
2005                 break;
2006         case TOOLBAR_MSGVIEW:
2007                 msgview = (MessageView*)toolbar_item->parent;
2008                 msginfo_list = g_slist_append(msginfo_list, msgview->msginfo);
2009                 break;
2010         default:
2011                 return;
2012         }
2013
2014         g_return_if_fail(msgview != NULL);
2015         body = messageview_get_selection(msgview);
2016
2017         g_return_if_fail(msginfo_list != NULL);
2018         compose_reply_mode((ComposeMode)action, msginfo_list, body);
2019
2020         g_free(body);
2021         g_slist_free(msginfo_list);
2022
2023         /* TODO: update reply state ion summaryview */
2024 }
2025
2026
2027 /* exported functions */
2028
2029 void inc_mail_cb(gpointer data, guint action, GtkWidget *widget)
2030 {
2031         MainWindow *mainwin = (MainWindow*)data;
2032
2033         inc_mail(mainwin, prefs_common.newmail_notify_manu);
2034 }
2035
2036 void inc_all_account_mail_cb(gpointer data, guint action, GtkWidget *widget)
2037 {
2038         MainWindow *mainwin = (MainWindow*)data;
2039
2040         inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
2041 }
2042
2043 void send_queue_cb(gpointer data, guint action, GtkWidget *widget)
2044 {
2045         GList *list;
2046
2047         if (prefs_common.work_offline)
2048                 if (alertpanel(_("Offline warning"), 
2049                                _("You're working offline. Override?"),
2050                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
2051                 return;
2052
2053         for (list = folder_get_list(); list != NULL; list = list->next) {
2054                 Folder *folder = list->data;
2055
2056                 if (folder->queue) {
2057                         procmsg_send_queue(folder->queue, prefs_common.savemsg);
2058                         folder_item_scan(folder->queue);
2059                 }
2060         }
2061 }
2062
2063 void compose_mail_cb(gpointer data, guint action, GtkWidget *widget)
2064 {
2065         MainWindow *mainwin = (MainWindow*)data;
2066         PrefsAccount *ac = NULL;
2067         FolderItem *item = mainwin->summaryview->folder_item;   
2068         GList * list;
2069         GList * cur;
2070
2071         if (item) {
2072                 ac = account_find_from_item(item);
2073                 if (ac && ac->protocol != A_NNTP) {
2074                         compose_new_with_folderitem(ac, item);          /* CLAWS */
2075                         return;
2076                 }
2077         }
2078
2079         /*
2080          * CLAWS - use current account
2081          */
2082         if (cur_account && (cur_account->protocol != A_NNTP)) {
2083                 compose_new_with_folderitem(cur_account, item);
2084                 return;
2085         }
2086
2087         /*
2088          * CLAWS - just get the first one
2089          */
2090         list = account_get_list();
2091         for (cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2092                 ac = (PrefsAccount *) cur->data;
2093                 if (ac->protocol != A_NNTP) {
2094                         compose_new_with_folderitem(ac, item);
2095                         return;
2096                 }
2097         }
2098 }
2099
2100 void compose_news_cb(gpointer data, guint action, GtkWidget *widget)
2101 {
2102         MainWindow *mainwin = (MainWindow*)data;
2103         PrefsAccount * ac = NULL;
2104         GList * list;
2105         GList * cur;
2106
2107         if (mainwin->summaryview->folder_item) {
2108                 ac = mainwin->summaryview->folder_item->folder->account;
2109                 if (ac && ac->protocol == A_NNTP) {
2110                         compose_new(ac,
2111                                     mainwin->summaryview->folder_item->path,
2112                                     NULL);
2113                         return;
2114                 }
2115         }
2116
2117         list = account_get_list();
2118         for(cur = list ; cur != NULL ; cur = g_list_next(cur)) {
2119                 ac = (PrefsAccount *) cur->data;
2120                 if (ac->protocol == A_NNTP) {
2121                         compose_new(ac, NULL, NULL);
2122                         return;
2123                 }
2124         }
2125 }